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/Timer/FPRCounterList.h"
|
|
|
16 |
|
|
|
17 |
@interface FPRCounterList ()
|
|
|
18 |
|
|
|
19 |
@property(nonatomic) NSMutableDictionary<NSString *, NSNumber *> *counterDictionary;
|
|
|
20 |
|
|
|
21 |
/** Serial queue to manage incrementing counters. */
|
|
|
22 |
@property(nonatomic, readwrite) dispatch_queue_t counterSerialQueue;
|
|
|
23 |
|
|
|
24 |
@end
|
|
|
25 |
|
|
|
26 |
@implementation FPRCounterList
|
|
|
27 |
|
|
|
28 |
- (instancetype)init {
|
|
|
29 |
self = [super init];
|
|
|
30 |
if (self) {
|
|
|
31 |
_counterDictionary = [[NSMutableDictionary alloc] init];
|
|
|
32 |
_counterSerialQueue = dispatch_queue_create("com.google.perf.counters", DISPATCH_QUEUE_SERIAL);
|
|
|
33 |
}
|
|
|
34 |
return self;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
- (void)incrementCounterNamed:(NSString *)counterName by:(NSInteger)incrementValue {
|
|
|
38 |
dispatch_sync(self.counterSerialQueue, ^{
|
|
|
39 |
if (counterName) {
|
|
|
40 |
NSNumber *number = self.counterDictionary[counterName];
|
|
|
41 |
if (number != nil) {
|
|
|
42 |
int64_t value = [number longLongValue];
|
|
|
43 |
value += incrementValue;
|
|
|
44 |
number = @(value);
|
|
|
45 |
} else {
|
|
|
46 |
number = @(incrementValue);
|
|
|
47 |
}
|
|
|
48 |
self.counterDictionary[counterName] = number;
|
|
|
49 |
}
|
|
|
50 |
});
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
- (NSDictionary *)counters {
|
|
|
54 |
__block NSDictionary *countersDictionary;
|
|
|
55 |
dispatch_sync(self.counterSerialQueue, ^{
|
|
|
56 |
countersDictionary = [self.counterDictionary copy];
|
|
|
57 |
});
|
|
|
58 |
return countersDictionary;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
- (NSUInteger)numberOfCounters {
|
|
|
62 |
__block NSUInteger numberOfCounters;
|
|
|
63 |
dispatch_sync(self.counterSerialQueue, ^{
|
|
|
64 |
numberOfCounters = self.counterDictionary.count;
|
|
|
65 |
});
|
|
|
66 |
return numberOfCounters;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
#pragma mark - Methods related to metrics
|
|
|
70 |
|
|
|
71 |
- (void)incrementMetric:(nonnull NSString *)metricName byInt:(int64_t)incrementValue {
|
|
|
72 |
dispatch_async(self.counterSerialQueue, ^{
|
|
|
73 |
if (metricName) {
|
|
|
74 |
NSNumber *number = self.counterDictionary[metricName];
|
|
|
75 |
if (number != nil) {
|
|
|
76 |
int64_t value = [number longLongValue];
|
|
|
77 |
value += incrementValue;
|
|
|
78 |
number = @(value);
|
|
|
79 |
} else {
|
|
|
80 |
number = @(incrementValue);
|
|
|
81 |
}
|
|
|
82 |
self.counterDictionary[metricName] = number;
|
|
|
83 |
}
|
|
|
84 |
});
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
- (int64_t)valueForIntMetric:(nonnull NSString *)metricName {
|
|
|
88 |
__block int64_t metricValue = 0;
|
|
|
89 |
dispatch_sync(self.counterSerialQueue, ^{
|
|
|
90 |
if (metricName) {
|
|
|
91 |
NSNumber *value = self.counterDictionary[metricName];
|
|
|
92 |
if (value != nil) {
|
|
|
93 |
metricValue = [value longLongValue];
|
|
|
94 |
} else {
|
|
|
95 |
metricValue = 0;
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
});
|
|
|
99 |
return metricValue;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
- (void)deleteMetric:(nonnull NSString *)metricName {
|
|
|
103 |
if (metricName) {
|
|
|
104 |
dispatch_sync(self.counterSerialQueue, ^{
|
|
|
105 |
[self.counterDictionary removeObjectForKey:metricName];
|
|
|
106 |
});
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
- (void)setIntValue:(int64_t)value forMetric:(nonnull NSString *)metricName {
|
|
|
111 |
dispatch_async(self.counterSerialQueue, ^{
|
|
|
112 |
NSNumber *newValue = @(value);
|
|
|
113 |
self.counterDictionary[metricName] = newValue;
|
|
|
114 |
});
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
- (BOOL)isValid {
|
|
|
118 |
// TODO(b/175054970): Rename this class to metrics list and see if this method makes sense.
|
|
|
119 |
return YES;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
@end
|