]> git.saurik.com Git - apple/security.git/blob - Analytics/SFAnalyticsActivityTracker.m
Security-58286.51.6.tar.gz
[apple/security.git] / Analytics / SFAnalyticsActivityTracker.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 "SFAnalyticsActivityTracker.h"
27 #import "SFAnalyticsActivityTracker+Internal.h"
28 #import "SFAnalytics.h"
29 #import <mach/mach_time.h>
30 #import "utilities/debugging.h"
31
32 @implementation SFAnalyticsActivityTracker {
33 dispatch_queue_t _queue;
34 NSString* _name;
35 Class _clientClass;
36 NSNumber* _measurement;
37 uint64_t _start;
38 BOOL _canceled;
39 }
40
41 - (instancetype)initWithName:(NSString*)name clientClass:(Class)className {
42 if (![name isKindOfClass:[NSString class]] || ![className isSubclassOfClass:[SFAnalytics class]] ) {
43 secerror("Cannot instantiate SFActivityTracker without name and client class");
44 return nil;
45 }
46
47 if (self = [super init]) {
48 _queue = dispatch_queue_create("SFAnalyticsActivityTracker queue", DISPATCH_QUEUE_SERIAL);
49 _name = name;
50 _clientClass = className;
51 _measurement = nil;
52 _canceled = NO;
53 _start = 0;
54 }
55 return self;
56 }
57
58 - (void)performAction:(void (^)(void))action
59 {
60 _start = mach_absolute_time();
61 action();
62 [self stop];
63 }
64
65 - (void)start
66 {
67 if (_canceled)
68 return;
69 NSAssert(_start == 0, @"SFAnalyticsActivityTracker user called start twice");
70 _start = mach_absolute_time();
71 }
72
73 - (void)stop
74 {
75 uint64_t end = mach_absolute_time();
76 static mach_timebase_info_data_t sTimebaseInfo;
77 if ( sTimebaseInfo.denom == 0 ) {
78 (void)mach_timebase_info(&sTimebaseInfo);
79 }
80 if (_canceled)
81 return;
82
83 NSAssert(_start != 0, @"SFAnalyticsActivityTracker user called stop w/o calling start");
84
85 _measurement = @([_measurement doubleValue] + (1.0f * (end - _start) * (1.0f * sTimebaseInfo.numer / sTimebaseInfo.denom)));
86 _start = 0;
87 }
88
89 - (void)cancel
90 {
91 _canceled = YES;
92 }
93
94 - (void)dealloc
95 {
96 if (!_canceled && _measurement != nil) {
97 [[_clientClass logger] logMetric:_measurement withName:_name];
98 }
99 }
100
101 @end
102
103 #endif