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/CPU/FPRCPUGaugeCollector.h"
|
|
|
16 |
#import "FirebasePerformance/Sources/Gauges/CPU/FPRCPUGaugeCollector+Private.h"
|
|
|
17 |
|
|
|
18 |
#import "FirebasePerformance/Sources/AppActivity/FPRSessionManager.h"
|
|
|
19 |
#import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
|
|
|
20 |
|
|
|
21 |
#import "FirebasePerformance/Sources/FPRConsoleLogger.h"
|
|
|
22 |
|
|
|
23 |
#import <assert.h>
|
|
|
24 |
#import <mach/mach.h>
|
|
|
25 |
|
|
|
26 |
@interface FPRCPUGaugeCollector ()
|
|
|
27 |
|
|
|
28 |
/** @brief Timer property used for the frequency of CPU data collection. */
|
|
|
29 |
@property(nonatomic) dispatch_source_t timerSource;
|
|
|
30 |
|
|
|
31 |
/** @brief Gauge collector queue on which the gauge data collected. */
|
|
|
32 |
@property(nonatomic) dispatch_queue_t gaugeCollectorQueue;
|
|
|
33 |
|
|
|
34 |
/** @brief Boolean to see if the timer is active or paused. */
|
|
|
35 |
@property(nonatomic) BOOL timerPaused;
|
|
|
36 |
|
|
|
37 |
@end
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Fetches the CPU metric and returns an instance of FPRCPUGaugeData.
|
|
|
41 |
*
|
|
|
42 |
* This implementation is inspired by the following references:
|
|
|
43 |
* http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_basic_info.html
|
|
|
44 |
* https://stackoverflow.com/a/8382889
|
|
|
45 |
*
|
|
|
46 |
* @return Instance of FPRCPUGaugeData.
|
|
|
47 |
*/
|
|
|
48 |
FPRCPUGaugeData *fprCollectCPUMetric() {
|
|
|
49 |
kern_return_t kernelReturnValue;
|
|
|
50 |
mach_msg_type_number_t task_info_count;
|
|
|
51 |
task_info_data_t taskInfo;
|
|
|
52 |
thread_array_t threadList;
|
|
|
53 |
mach_msg_type_number_t threadCount;
|
|
|
54 |
task_basic_info_t taskBasicInfo;
|
|
|
55 |
thread_basic_info_t threadBasicInfo;
|
|
|
56 |
|
|
|
57 |
NSDate *collectionTime = [NSDate date];
|
|
|
58 |
// Get the task info to find out the CPU time used by terminated threads.
|
|
|
59 |
task_info_count = TASK_BASIC_INFO_COUNT;
|
|
|
60 |
kernelReturnValue =
|
|
|
61 |
task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)taskInfo, &task_info_count);
|
|
|
62 |
if (kernelReturnValue != KERN_SUCCESS) {
|
|
|
63 |
return nil;
|
|
|
64 |
}
|
|
|
65 |
taskBasicInfo = (task_basic_info_t)taskInfo;
|
|
|
66 |
|
|
|
67 |
// Get the current set of threads and find their CPU time.
|
|
|
68 |
kernelReturnValue = task_threads(mach_task_self(), &threadList, &threadCount);
|
|
|
69 |
if (kernelReturnValue != KERN_SUCCESS) {
|
|
|
70 |
return nil;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
uint64_t totalUserTimeUsec =
|
|
|
74 |
taskBasicInfo->user_time.seconds * USEC_PER_SEC + taskBasicInfo->user_time.microseconds;
|
|
|
75 |
uint64_t totalSystemTimeUsec =
|
|
|
76 |
taskBasicInfo->system_time.seconds * USEC_PER_SEC + taskBasicInfo->system_time.microseconds;
|
|
|
77 |
thread_info_data_t threadInfo;
|
|
|
78 |
mach_msg_type_number_t threadInfoCount;
|
|
|
79 |
|
|
|
80 |
for (int i = 0; i < (int)threadCount; i++) {
|
|
|
81 |
threadInfoCount = THREAD_INFO_MAX;
|
|
|
82 |
kernelReturnValue =
|
|
|
83 |
thread_info(threadList[i], THREAD_BASIC_INFO, (thread_info_t)threadInfo, &threadInfoCount);
|
|
|
84 |
if (kernelReturnValue != KERN_SUCCESS) {
|
|
|
85 |
return nil;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
threadBasicInfo = (thread_basic_info_t)threadInfo;
|
|
|
89 |
if (!(threadBasicInfo->flags & TH_FLAGS_IDLE)) {
|
|
|
90 |
totalUserTimeUsec += threadBasicInfo->user_time.seconds * USEC_PER_SEC +
|
|
|
91 |
threadBasicInfo->user_time.microseconds;
|
|
|
92 |
totalSystemTimeUsec += threadBasicInfo->system_time.seconds * USEC_PER_SEC +
|
|
|
93 |
threadBasicInfo->system_time.microseconds;
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
kernelReturnValue =
|
|
|
98 |
vm_deallocate(mach_task_self(), (vm_offset_t)threadList, threadCount * sizeof(thread_t));
|
|
|
99 |
assert(kernelReturnValue == KERN_SUCCESS);
|
|
|
100 |
|
|
|
101 |
FPRCPUGaugeData *gaugeData = [[FPRCPUGaugeData alloc] initWithCollectionTime:collectionTime
|
|
|
102 |
systemTime:totalSystemTimeUsec
|
|
|
103 |
userTime:totalUserTimeUsec];
|
|
|
104 |
return gaugeData;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
@implementation FPRCPUGaugeCollector
|
|
|
108 |
|
|
|
109 |
- (instancetype)initWithDelegate:(id<FPRCPUGaugeCollectorDelegate>)delegate {
|
|
|
110 |
self = [super init];
|
|
|
111 |
if (self) {
|
|
|
112 |
_delegate = delegate;
|
|
|
113 |
_gaugeCollectorQueue =
|
|
|
114 |
dispatch_queue_create("com.google.firebase.FPRCPUGaugeCollector", DISPATCH_QUEUE_SERIAL);
|
|
|
115 |
_configurations = [FPRConfigurations sharedInstance];
|
|
|
116 |
_timerPaused = YES;
|
|
|
117 |
[self updateSamplingFrequencyForApplicationState:[FPRAppActivityTracker sharedInstance]
|
|
|
118 |
.applicationState];
|
|
|
119 |
}
|
|
|
120 |
return self;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
- (void)stopCollecting {
|
|
|
124 |
if (self.timerPaused == NO) {
|
|
|
125 |
dispatch_source_cancel(self.timerSource);
|
|
|
126 |
self.timerPaused = YES;
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
- (void)resumeCollecting {
|
|
|
131 |
[self updateSamplingFrequencyForApplicationState:[FPRAppActivityTracker sharedInstance]
|
|
|
132 |
.applicationState];
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
- (void)updateSamplingFrequencyForApplicationState:(FPRApplicationState)applicationState {
|
|
|
136 |
uint32_t frequencyInMs = (applicationState == FPRApplicationStateBackground)
|
|
|
137 |
? [self.configurations cpuSamplingFrequencyInBackgroundInMS]
|
|
|
138 |
: [self.configurations cpuSamplingFrequencyInForegroundInMS];
|
|
|
139 |
[self captureCPUGaugeAtFrequency:frequencyInMs];
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Captures the CPU gauge at a defined frequency.
|
|
|
144 |
*
|
|
|
145 |
* @param frequencyInMs Frequency at which the CPU gauges are collected.
|
|
|
146 |
*/
|
|
|
147 |
- (void)captureCPUGaugeAtFrequency:(uint32_t)frequencyInMs {
|
|
|
148 |
[self stopCollecting];
|
|
|
149 |
if (frequencyInMs > 0) {
|
|
|
150 |
self.timerSource =
|
|
|
151 |
dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.gaugeCollectorQueue);
|
|
|
152 |
dispatch_source_set_timer(self.timerSource,
|
|
|
153 |
dispatch_time(DISPATCH_TIME_NOW, frequencyInMs * NSEC_PER_MSEC),
|
|
|
154 |
frequencyInMs * NSEC_PER_MSEC, (1ull * NSEC_PER_SEC) / 10);
|
|
|
155 |
FPRCPUGaugeCollector __weak *weakSelf = self;
|
|
|
156 |
dispatch_source_set_event_handler(weakSelf.timerSource, ^{
|
|
|
157 |
FPRCPUGaugeCollector *strongSelf = weakSelf;
|
|
|
158 |
if (strongSelf) {
|
|
|
159 |
[strongSelf collectMetric];
|
|
|
160 |
}
|
|
|
161 |
});
|
|
|
162 |
dispatch_resume(self.timerSource);
|
|
|
163 |
self.timerPaused = NO;
|
|
|
164 |
} else {
|
|
|
165 |
FPRLogDebug(kFPRCPUCollection, @"CPU metric collection is disabled.");
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
- (void)collectMetric {
|
|
|
170 |
FPRCPUGaugeData *gaugeMetric = fprCollectCPUMetric();
|
|
|
171 |
[self.delegate cpuGaugeCollector:self gaugeData:gaugeMetric];
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
@end
|