|
| 1 | +#import "SentryDelayedFramesTracker.h" |
| 2 | + |
| 3 | +#if SENTRY_HAS_UIKIT |
| 4 | + |
| 5 | +# import "SentryCurrentDateProvider.h" |
| 6 | +# import "SentryDelayedFrame.h" |
| 7 | +# import "SentryLog.h" |
| 8 | +# import "SentryTime.h" |
| 9 | + |
| 10 | +NS_ASSUME_NONNULL_BEGIN |
| 11 | + |
| 12 | +@interface |
| 13 | +SentryDelayedFramesTracker () |
| 14 | + |
| 15 | +@property (nonatomic, assign) CFTimeInterval keepDelayedFramesDuration; |
| 16 | +@property (nonatomic, strong, readonly) SentryCurrentDateProvider *dateProvider; |
| 17 | +@property (nonatomic, strong) NSMutableArray<SentryDelayedFrame *> *delayedFrames; |
| 18 | + |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation SentryDelayedFramesTracker |
| 22 | + |
| 23 | +- (instancetype)initWithKeepDelayedFramesDuration:(CFTimeInterval)keepDelayedFramesDuration |
| 24 | + dateProvider:(SentryCurrentDateProvider *)dateProvider |
| 25 | +{ |
| 26 | + if (self = [super init]) { |
| 27 | + _keepDelayedFramesDuration = keepDelayedFramesDuration; |
| 28 | + _dateProvider = dateProvider; |
| 29 | + [self resetDelayedFramesTimeStamps]; |
| 30 | + } |
| 31 | + return self; |
| 32 | +} |
| 33 | + |
| 34 | +- (void)resetDelayedFramesTimeStamps |
| 35 | +{ |
| 36 | + _delayedFrames = [NSMutableArray array]; |
| 37 | + SentryDelayedFrame *initialFrame = |
| 38 | + [[SentryDelayedFrame alloc] initWithStartTimestamp:[self.dateProvider systemTime] |
| 39 | + expectedDuration:0 |
| 40 | + actualDuration:0]; |
| 41 | + [_delayedFrames addObject:initialFrame]; |
| 42 | +} |
| 43 | + |
| 44 | +- (void)recordDelayedFrame:(uint64_t)startSystemTimestamp |
| 45 | + expectedDuration:(CFTimeInterval)expectedDuration |
| 46 | + actualDuration:(CFTimeInterval)actualDuration |
| 47 | +{ |
| 48 | + @synchronized(self.delayedFrames) { |
| 49 | + [self removeOldDelayedFrames]; |
| 50 | + |
| 51 | + SentryDelayedFrame *delayedFrame = |
| 52 | + [[SentryDelayedFrame alloc] initWithStartTimestamp:startSystemTimestamp |
| 53 | + expectedDuration:expectedDuration |
| 54 | + actualDuration:actualDuration]; |
| 55 | + [self.delayedFrames addObject:delayedFrame]; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Removes delayed frame that are older than current time minus `keepDelayedFramesDuration`. |
| 61 | + * @note Make sure to call this in a @synchronized block. |
| 62 | + */ |
| 63 | +- (void)removeOldDelayedFrames |
| 64 | +{ |
| 65 | + u_int64_t transactionMaxDurationNS = timeIntervalToNanoseconds(_keepDelayedFramesDuration); |
| 66 | + |
| 67 | + uint64_t removeFramesBeforeSystemTimeStamp |
| 68 | + = _dateProvider.systemTime - transactionMaxDurationNS; |
| 69 | + if (_dateProvider.systemTime < transactionMaxDurationNS) { |
| 70 | + removeFramesBeforeSystemTimeStamp = 0; |
| 71 | + } |
| 72 | + |
| 73 | + NSUInteger left = 0; |
| 74 | + NSUInteger right = self.delayedFrames.count; |
| 75 | + |
| 76 | + while (left < right) { |
| 77 | + NSUInteger mid = (left + right) / 2; |
| 78 | + SentryDelayedFrame *midFrame = self.delayedFrames[mid]; |
| 79 | + |
| 80 | + uint64_t frameEndSystemTimeStamp |
| 81 | + = midFrame.startSystemTimestamp + timeIntervalToNanoseconds(midFrame.actualDuration); |
| 82 | + if (frameEndSystemTimeStamp >= removeFramesBeforeSystemTimeStamp) { |
| 83 | + right = mid; |
| 84 | + } else { |
| 85 | + left = mid + 1; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + [self.delayedFrames removeObjectsInRange:NSMakeRange(0, left)]; |
| 90 | +} |
| 91 | + |
| 92 | +- (CFTimeInterval)getFramesDelay:(uint64_t)startSystemTimestamp |
| 93 | + endSystemTimestamp:(uint64_t)endSystemTimestamp |
| 94 | + isRunning:(BOOL)isRunning |
| 95 | + thisFrameTimestamp:(CFTimeInterval)thisFrameTimestamp |
| 96 | + previousFrameTimestamp:(CFTimeInterval)previousFrameTimestamp |
| 97 | + slowFrameThreshold:(CFTimeInterval)slowFrameThreshold |
| 98 | +{ |
| 99 | + CFTimeInterval cantCalculateFrameDelayReturnValue = -1.0; |
| 100 | + |
| 101 | + if (isRunning == NO) { |
| 102 | + SENTRY_LOG_DEBUG(@"Not calculating frames delay because frames tracker isn't running."); |
| 103 | + return cantCalculateFrameDelayReturnValue; |
| 104 | + } |
| 105 | + |
| 106 | + if (startSystemTimestamp >= endSystemTimestamp) { |
| 107 | + SENTRY_LOG_DEBUG(@"Not calculating frames delay because startSystemTimestamp is before " |
| 108 | + @"endSystemTimestamp"); |
| 109 | + return cantCalculateFrameDelayReturnValue; |
| 110 | + } |
| 111 | + |
| 112 | + if (endSystemTimestamp > self.dateProvider.systemTime) { |
| 113 | + SENTRY_LOG_DEBUG( |
| 114 | + @"Not calculating frames delay because endSystemTimestamp is in the future."); |
| 115 | + return cantCalculateFrameDelayReturnValue; |
| 116 | + } |
| 117 | + |
| 118 | + if (previousFrameTimestamp < 0) { |
| 119 | + SENTRY_LOG_DEBUG(@"Not calculating frames delay because no frames yet recorded."); |
| 120 | + return cantCalculateFrameDelayReturnValue; |
| 121 | + } |
| 122 | + |
| 123 | + NSArray<SentryDelayedFrame *> *frames; |
| 124 | + @synchronized(self.delayedFrames) { |
| 125 | + uint64_t oldestDelayedFrameStartTimestamp = UINT64_MAX; |
| 126 | + SentryDelayedFrame *oldestDelayedFrame = self.delayedFrames.firstObject; |
| 127 | + if (oldestDelayedFrame != nil) { |
| 128 | + oldestDelayedFrameStartTimestamp = oldestDelayedFrame.startSystemTimestamp; |
| 129 | + } |
| 130 | + |
| 131 | + if (oldestDelayedFrameStartTimestamp > startSystemTimestamp) { |
| 132 | + SENTRY_LOG_DEBUG(@"Not calculating frames delay because the record of delayed frames " |
| 133 | + @"doesn't go back enough in time."); |
| 134 | + return cantCalculateFrameDelayReturnValue; |
| 135 | + } |
| 136 | + |
| 137 | + // Copy as late as possible to avoid allocating unnecessary memory. |
| 138 | + frames = self.delayedFrames.copy; |
| 139 | + } |
| 140 | + |
| 141 | + // Check if there is an delayed frame going on but not recorded yet. |
| 142 | + CFTimeInterval frameDuration = thisFrameTimestamp - previousFrameTimestamp; |
| 143 | + CFTimeInterval ongoingDelayedFrame = 0.0; |
| 144 | + if (frameDuration > slowFrameThreshold) { |
| 145 | + ongoingDelayedFrame = frameDuration - slowFrameThreshold; |
| 146 | + } |
| 147 | + |
| 148 | + CFTimeInterval delay = ongoingDelayedFrame; |
| 149 | + |
| 150 | + // We need to calculate the intersections of the queried TimestampInterval |
| 151 | + // (startSystemTimestamp - endSystemTimestamp) with the recorded frame delays. Doing that |
| 152 | + // with NSDateInterval makes things easier. Therefore, we convert the system timestamps to |
| 153 | + // NSDate objects, although they don't represent the correct dates. We only need to know how |
| 154 | + // long the intersections are to calculate the frame delay and not precisely when. |
| 155 | + |
| 156 | + NSDate *startDate = [NSDate |
| 157 | + dateWithTimeIntervalSinceReferenceDate:nanosecondsToTimeInterval(startSystemTimestamp)]; |
| 158 | + NSDate *endDate = [NSDate |
| 159 | + dateWithTimeIntervalSinceReferenceDate:nanosecondsToTimeInterval(endSystemTimestamp)]; |
| 160 | + NSDateInterval *queryDateInterval = [[NSDateInterval alloc] initWithStartDate:startDate |
| 161 | + endDate:endDate]; |
| 162 | + |
| 163 | + // Iterate in reverse order, as younger frame delays are more likely to match the queried |
| 164 | + // period. |
| 165 | + for (SentryDelayedFrame *frame in frames.reverseObjectEnumerator) { |
| 166 | + |
| 167 | + uint64_t frameEndSystemTimeStamp |
| 168 | + = frame.startSystemTimestamp + timeIntervalToNanoseconds(frame.actualDuration); |
| 169 | + if (frameEndSystemTimeStamp < startSystemTimestamp) { |
| 170 | + break; |
| 171 | + } |
| 172 | + |
| 173 | + CFTimeInterval delayStartTime |
| 174 | + = nanosecondsToTimeInterval(frame.startSystemTimestamp) + frame.expectedDuration; |
| 175 | + NSDate *frameDelayStartDate = |
| 176 | + [NSDate dateWithTimeIntervalSinceReferenceDate:delayStartTime]; |
| 177 | + |
| 178 | + NSDateInterval *frameDelayDateInterval = [[NSDateInterval alloc] |
| 179 | + initWithStartDate:frameDelayStartDate |
| 180 | + duration:(frame.actualDuration - frame.expectedDuration)]; |
| 181 | + |
| 182 | + if ([queryDateInterval intersectsDateInterval:frameDelayDateInterval]) { |
| 183 | + NSDateInterval *intersection = |
| 184 | + [queryDateInterval intersectionWithDateInterval:frameDelayDateInterval]; |
| 185 | + delay = delay + intersection.duration; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + return delay; |
| 190 | +} |
| 191 | + |
| 192 | +@end |
| 193 | + |
| 194 | +NS_ASSUME_NONNULL_END |
| 195 | + |
| 196 | +#endif // SENTRY_HAS_UIKIT |
0 commit comments