FPRSessionManager.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/AppActivity/FPRSessionManager.h"
  15. #import "FirebasePerformance/Sources/AppActivity/FPRSessionManager+Private.h"
  16. #import "FirebasePerformance/Sources/Configurations/FPRConfigurations.h"
  17. #import "FirebasePerformance/Sources/FPRConsoleLogger.h"
  18. #import <UIKit/UIKit.h>
  19. NSString *const kFPRSessionIdUpdatedNotification = @"kFPRSessionIdUpdatedNotification";
  20. NSString *const kFPRSessionIdNotificationKey = @"kFPRSessionIdNotificationKey";
  21. @interface FPRSessionManager ()
  22. @property(nonatomic, readwrite) NSNotificationCenter *sessionNotificationCenter;
  23. @end
  24. @implementation FPRSessionManager
  25. + (FPRSessionManager *)sharedInstance {
  26. static FPRSessionManager *instance;
  27. static dispatch_once_t onceToken;
  28. dispatch_once(&onceToken, ^{
  29. NSNotificationCenter *notificationCenter = [[NSNotificationCenter alloc] init];
  30. FPRGaugeManager *gaugeManager = [FPRGaugeManager sharedInstance];
  31. instance = [[FPRSessionManager alloc] initWithGaugeManager:gaugeManager
  32. notificationCenter:notificationCenter];
  33. });
  34. return instance;
  35. }
  36. - (FPRSessionManager *)initWithGaugeManager:(FPRGaugeManager *)gaugeManager
  37. notificationCenter:(NSNotificationCenter *)notificationCenter {
  38. self = [super init];
  39. if (self) {
  40. _gaugeManager = gaugeManager;
  41. _sessionNotificationCenter = notificationCenter;
  42. // Empty string is immediately replaced when FirebaseCore runs Fireperf's
  43. // FIRComponentCreationBlock, because in the creation block we register Fireperf with Sessions,
  44. // and the registration function immediately propagates real sessionId. This is at an early time
  45. // in initialization that any trace is yet to be created.
  46. _sessionDetails = [[FPRSessionDetails alloc] initWithSessionId:@""
  47. options:FPRSessionOptionsNone];
  48. }
  49. return self;
  50. }
  51. - (void)stopGaugesIfRunningTooLong {
  52. NSUInteger maxSessionLength = [[FPRConfigurations sharedInstance] maxSessionLengthInMinutes];
  53. if ([self.sessionDetails sessionLengthInMinutesFromDate:[NSDate date]] >= maxSessionLength) {
  54. [self.gaugeManager stopCollectingGauges:FPRGaugeCPU | FPRGaugeMemory];
  55. }
  56. }
  57. /**
  58. * Stops current session, and create a new session with new session id.
  59. *
  60. * @param sessionIdString New session id.
  61. */
  62. - (void)updateSessionId:(NSString *)sessionIdString {
  63. FPRSessionOptions sessionOptions = FPRSessionOptionsNone;
  64. if ([self isGaugeCollectionEnabledForSessionId:sessionIdString]) {
  65. [self.gaugeManager startCollectingGauges:FPRGaugeCPU | FPRGaugeMemory
  66. forSessionId:sessionIdString];
  67. sessionOptions = FPRSessionOptionsGauges;
  68. } else {
  69. [self.gaugeManager stopCollectingGauges:FPRGaugeCPU | FPRGaugeMemory];
  70. }
  71. FPRLogDebug(kFPRSessionId, @"Session Id changed - %@", sessionIdString);
  72. FPRSessionDetails *sessionInfo = [[FPRSessionDetails alloc] initWithSessionId:sessionIdString
  73. options:sessionOptions];
  74. self.sessionDetails = sessionInfo;
  75. NSMutableDictionary<NSString *, FPRSessionDetails *> *userInfo =
  76. [[NSMutableDictionary alloc] init];
  77. [userInfo setObject:sessionInfo forKey:kFPRSessionIdNotificationKey];
  78. [self.sessionNotificationCenter postNotificationName:kFPRSessionIdUpdatedNotification
  79. object:self
  80. userInfo:[userInfo copy]];
  81. }
  82. - (void)collectAllGaugesOnce {
  83. [self.gaugeManager collectAllGauges];
  84. }
  85. /**
  86. * Checks if the provided sessionId can have gauge data collection enabled.
  87. *
  88. * @param sessionId Session Id for which the check is done.
  89. * @return YES if gauge collection is enabled, NO otherwise.
  90. */
  91. - (BOOL)isGaugeCollectionEnabledForSessionId:(NSString *)sessionId {
  92. float_t sessionSamplePercentage = [[FPRConfigurations sharedInstance] sessionsSamplingPercentage];
  93. double randomNumberBetween0And1 = ((double)arc4random() / UINT_MAX);
  94. BOOL sessionsEnabled = randomNumberBetween0And1 * 100 < sessionSamplePercentage;
  95. return sessionsEnabled;
  96. }
  97. @end