Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// Copyright 2020 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
#import "FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector.h"
16
#import "FirebasePerformance/Sources/Gauges/Memory/FPRMemoryGaugeCollector+Private.h"
17
 
18
#import "FirebasePerformance/Sources/AppActivity/FPRSessionManager.h"
19
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
20
#import "FirebasePerformance/Sources/FPRConsoleLogger.h"
21
 
22
#include <malloc/malloc.h>
23
 
24
@interface FPRMemoryGaugeCollector ()
25
 
26
/** @brief Timer property used for the frequency of CPU data collection. */
27
@property(nonatomic) dispatch_source_t timerSource;
28
 
29
/** @brief Gauge collector queue on which the gauge data collected. */
30
@property(nonatomic) dispatch_queue_t gaugeCollectorQueue;
31
 
32
/** @brief Boolean to see if the timer is active or paused. */
33
@property(nonatomic) BOOL timerPaused;
34
 
35
@end
36
 
37
FPRMemoryGaugeData *fprCollectMemoryMetric() {
38
  NSDate *collectionTime = [NSDate date];
39
 
40
  struct mstats ms = mstats();
41
  FPRMemoryGaugeData *gaugeData = [[FPRMemoryGaugeData alloc] initWithCollectionTime:collectionTime
42
                                                                            heapUsed:ms.bytes_used
43
                                                                       heapAvailable:ms.bytes_free];
44
  return gaugeData;
45
}
46
 
47
@implementation FPRMemoryGaugeCollector
48
 
49
- (instancetype)initWithDelegate:(id<FPRMemoryGaugeCollectorDelegate>)delegate {
50
  self = [super init];
51
  if (self) {
52
    _delegate = delegate;
53
    _gaugeCollectorQueue =
54
        dispatch_queue_create("com.google.firebase.FPRMemoryGaugeCollector", DISPATCH_QUEUE_SERIAL);
55
    _configurations = [FPRConfigurations sharedInstance];
56
    _timerPaused = YES;
57
    [self updateSamplingFrequencyForApplicationState:[FPRAppActivityTracker sharedInstance]
58
                                                         .applicationState];
59
  }
60
  return self;
61
}
62
 
63
- (void)stopCollecting {
64
  if (self.timerPaused == NO) {
65
    dispatch_source_cancel(self.timerSource);
66
    self.timerPaused = YES;
67
  }
68
}
69
 
70
- (void)resumeCollecting {
71
  [self updateSamplingFrequencyForApplicationState:[FPRAppActivityTracker sharedInstance]
72
                                                       .applicationState];
73
}
74
 
75
- (void)updateSamplingFrequencyForApplicationState:(FPRApplicationState)applicationState {
76
  uint32_t frequencyInMs = (applicationState == FPRApplicationStateBackground)
77
                               ? [self.configurations memorySamplingFrequencyInBackgroundInMS]
78
                               : [self.configurations memorySamplingFrequencyInForegroundInMS];
79
  [self captureMemoryGaugeAtFrequency:frequencyInMs];
80
}
81
 
82
#pragma mark - Internal methods.
83
 
84
/**
85
 * Captures the memory gauge at a defined frequency.
86
 *
87
 * @param frequencyInMs Frequency at which the memory gauges are collected.
88
 */
89
- (void)captureMemoryGaugeAtFrequency:(uint32_t)frequencyInMs {
90
  [self stopCollecting];
91
  if (frequencyInMs > 0) {
92
    self.timerSource =
93
        dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.gaugeCollectorQueue);
94
    dispatch_source_set_timer(self.timerSource,
95
                              dispatch_time(DISPATCH_TIME_NOW, frequencyInMs * NSEC_PER_MSEC),
96
                              frequencyInMs * NSEC_PER_MSEC, (1ull * NSEC_PER_SEC) / 10);
97
    FPRMemoryGaugeCollector __weak *weakSelf = self;
98
    dispatch_source_set_event_handler(weakSelf.timerSource, ^{
99
      FPRMemoryGaugeCollector *strongSelf = weakSelf;
100
      if (strongSelf) {
101
        [strongSelf collectMetric];
102
      }
103
    });
104
    dispatch_resume(self.timerSource);
105
    self.timerPaused = NO;
106
  } else {
107
    FPRLogDebug(kFPRMemoryCollection, @"Memory metric collection is disabled.");
108
  }
109
}
110
 
111
- (void)collectMetric {
112
  FPRMemoryGaugeData *gaugeMetric = fprCollectMemoryMetric();
113
  [self.delegate memoryGaugeCollector:self gaugeData:gaugeMetric];
114
}
115
 
116
@end