FPRCounterList.m 3.4 KB

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