]> git.saurik.com Git - apple/security.git/blob - Analytics/SFAnalyticsMultiSampler.m
Security-58286.270.3.0.1.tar.gz
[apple/security.git] / Analytics / SFAnalyticsMultiSampler.m
1 /*
2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if __OBJC2__
25
26 #import "SFAnalyticsMultiSampler+Internal.h"
27 #import "SFAnalytics+Internal.h"
28 #import "SFAnalyticsDefines.h"
29 #import "utilities/debugging.h"
30 #include <notify.h>
31 #include <dispatch/dispatch.h>
32
33 @implementation SFAnalyticsMultiSampler {
34 NSTimeInterval _samplingInterval;
35 dispatch_source_t _timer;
36 NSString* _name;
37 MultiSamplerDictionary (^_block)(void);
38 int _notificationToken;
39 Class _clientClass;
40 BOOL _oncePerReport;
41 BOOL _activeTimer;
42 }
43
44 @synthesize name = _name;
45 @synthesize samplingInterval = _samplingInterval;
46
47 - (instancetype)initWithName:(NSString*)name interval:(NSTimeInterval)interval block:(MultiSamplerDictionary (^)(void))block clientClass:(Class)clientClass
48 {
49 if (self = [super init]) {
50 if (![clientClass isSubclassOfClass:[SFAnalytics class]]) {
51 secerror("SFAnalyticsSampler created without valid client class (%@)", clientClass);
52 return nil;
53 }
54
55 if (!name || (interval < 1.0f && interval != SFAnalyticsSamplerIntervalOncePerReport) || !block) {
56 secerror("SFAnalyticsSampler created without proper data");
57 return nil;
58 }
59
60 _clientClass = clientClass;
61 _block = block;
62 _name = name;
63 _samplingInterval = interval;
64 [self newTimer];
65 }
66 return self;
67 }
68
69 - (void)newTimer
70 {
71 if (_activeTimer) {
72 [self pauseSampling];
73 }
74
75 _oncePerReport = (_samplingInterval == SFAnalyticsSamplerIntervalOncePerReport);
76 if (_oncePerReport) {
77 [self setupOnceTimer];
78 } else {
79 [self setupPeriodicTimer];
80 }
81 }
82
83 - (void)setupOnceTimer
84 {
85 __weak __typeof(self) weakSelf = self;
86 notify_register_dispatch(SFAnalyticsFireSamplersNotification, &_notificationToken, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(int token) {
87 __strong __typeof(self) strongSelf = weakSelf;
88 if (!strongSelf) {
89 secnotice("SFAnalyticsSampler", "sampler went away before we could run its once-per-report block");
90 notify_cancel(token);
91 return;
92 }
93
94 MultiSamplerDictionary data = strongSelf->_block();
95 [data enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSNumber * _Nonnull obj, BOOL * _Nonnull stop) {
96 [[strongSelf->_clientClass logger] logMetric:obj withName:key oncePerReport:strongSelf->_oncePerReport];
97 }];
98 });
99 _activeTimer = YES;
100 }
101
102 - (void)setupPeriodicTimer
103 {
104 _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
105 dispatch_source_set_timer(_timer, dispatch_walltime(0, _samplingInterval * NSEC_PER_SEC), _samplingInterval * NSEC_PER_SEC, _samplingInterval * NSEC_PER_SEC / 50.0); // give 2% leeway on timer
106
107 __weak __typeof(self) weakSelf = self;
108 dispatch_source_set_event_handler(_timer, ^{
109 __strong __typeof(self) strongSelf = weakSelf;
110 if (!strongSelf) {
111 secnotice("SFAnalyticsSampler", "sampler went away before we could run its once-per-report block");
112 return;
113 }
114
115 MultiSamplerDictionary data = strongSelf->_block();
116 [data enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSNumber * _Nonnull obj, BOOL * _Nonnull stop) {
117 [[strongSelf->_clientClass logger] logMetric:obj withName:key oncePerReport:strongSelf->_oncePerReport];
118 }];
119 });
120 dispatch_resume(_timer);
121
122 _activeTimer = YES;
123 }
124
125 - (void)setSamplingInterval:(NSTimeInterval)interval
126 {
127 if (interval < 1.0f && !(interval == SFAnalyticsSamplerIntervalOncePerReport)) {
128 secerror("SFAnalyticsSampler: interval %f is not supported", interval);
129 return;
130 }
131
132 _samplingInterval = interval;
133 [self newTimer];
134 }
135
136 - (NSTimeInterval)samplingInterval {
137 return _samplingInterval;
138 }
139
140 - (MultiSamplerDictionary)sampleNow
141 {
142 MultiSamplerDictionary data = _block();
143 [data enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSNumber * _Nonnull obj, BOOL * _Nonnull stop) {
144 [[self->_clientClass logger] logMetric:obj withName:key oncePerReport:self->_oncePerReport];
145 }];
146 return data;
147 }
148
149 - (void)pauseSampling
150 {
151 if (!_activeTimer) {
152 return;
153 }
154
155 if (_oncePerReport) {
156 notify_cancel(_notificationToken);
157 _notificationToken = 0;
158 } else {
159 dispatch_source_cancel(_timer);
160 }
161 _activeTimer = NO;
162 }
163
164 - (void)resumeSampling
165 {
166 [self newTimer];
167 }
168
169 - (void)dealloc
170 {
171 [self pauseSampling];
172 }
173
174 @end
175
176 #endif