|
16 | 16 | # endif // SENTRY_HAS_UIKIT |
17 | 17 |
|
18 | 18 | /** |
19 | | - * a mapping of profilers to the tracers that started them that are still in-flight and will need to |
20 | | - * query them for their profiling data when they finish. this helps resolve the incongruity between |
21 | | - * the different timeout durations between tracers (500s) and profilers (30s), where a transaction |
22 | | - * may start a profiler that then times out, and then a new transaction starts a new profiler, and |
23 | | - * we must keep the aborted one around until its associated transaction finishes. |
| 19 | + * a mapping of profilers to the number of tracers that started them that are still in-flight and |
| 20 | + * will need to query them for their profiling data when they finish. this helps resolve the |
| 21 | + * incongruity between the different timeout durations between tracers (500s) and profilers (30s), |
| 22 | + * where a transaction may start a profiler that then times out, and then a new transaction starts a |
| 23 | + * new profiler, and we must keep the aborted one around until its associated transaction finishes. |
24 | 24 | */ |
25 | 25 | static NSMutableDictionary</* SentryProfiler.profileId */ NSString *, |
26 | | - NSMutableSet<SentryTracer *> *> *_gProfilersToTracers; |
| 26 | + /* number of in-flight tracers */ NSNumber *> *_gProfilersToTracers; |
27 | 27 |
|
28 | 28 | /** provided for fast access to a profiler given a tracer */ |
29 | 29 | static NSMutableDictionary</* SentryTracer.tracerId */ NSString *, SentryProfiler *> |
30 | 30 | *_gTracersToProfilers; |
31 | 31 |
|
| 32 | +namespace { |
| 33 | + |
| 34 | +/** |
| 35 | + * Remove a profiler from tracking given the id of the tracer it's associated with. |
| 36 | + * @warning Must be called from a synchronized context. |
| 37 | + */ |
| 38 | +void |
| 39 | +_unsafe_cleanUpProfiler(SentryProfiler *profiler, NSString *tracerKey) |
| 40 | +{ |
| 41 | + const auto profilerKey = profiler.profileId.sentryIdString; |
| 42 | + |
| 43 | + [_gTracersToProfilers removeObjectForKey:tracerKey]; |
| 44 | + _gProfilersToTracers[profilerKey] = @(_gProfilersToTracers[profilerKey].unsignedIntValue - 1); |
| 45 | + if ([_gProfilersToTracers[profilerKey] unsignedIntValue] == 0) { |
| 46 | + [_gProfilersToTracers removeObjectForKey:profilerKey]; |
| 47 | + if ([profiler isRunning]) { |
| 48 | + [profiler stopForReason:SentryProfilerTruncationReasonNormal]; |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace |
| 54 | + |
32 | 55 | std::mutex _gStateLock; |
33 | 56 |
|
34 | 57 | void |
|
48 | 71 |
|
49 | 72 | if (_gProfilersToTracers == nil) { |
50 | 73 | _gProfilersToTracers = [NSMutableDictionary</* SentryProfiler.profileId */ NSString *, |
51 | | - NSMutableSet<SentryTracer *> *> dictionaryWithObject:[NSMutableSet setWithObject:tracer] |
52 | | - forKey:profilerKey]; |
| 74 | + /* number of in-flight tracers */ NSNumber *> |
| 75 | + dictionary]; |
53 | 76 | _gTracersToProfilers = |
54 | 77 | [NSMutableDictionary</* SentryTracer.tracerId */ NSString *, SentryProfiler *> |
55 | | - dictionaryWithObject:profiler |
56 | | - forKey:tracerKey]; |
57 | | - return; |
| 78 | + dictionary]; |
58 | 79 | } |
59 | 80 |
|
60 | | - if (_gProfilersToTracers[profilerKey] == nil) { |
61 | | - _gProfilersToTracers[profilerKey] = [NSMutableSet setWithObject:tracer]; |
62 | | - } else { |
63 | | - [_gProfilersToTracers[profilerKey] addObject:tracer]; |
| 81 | + _gProfilersToTracers[profilerKey] = @(_gProfilersToTracers[profilerKey].unsignedIntValue + 1); |
| 82 | + _gTracersToProfilers[tracerKey] = profiler; |
| 83 | +} |
| 84 | + |
| 85 | +void |
| 86 | +discardProfilerForTracer(SentryTracer *tracer) |
| 87 | +{ |
| 88 | + std::lock_guard<std::mutex> l(_gStateLock); |
| 89 | + |
| 90 | + SENTRY_CASSERT(_gTracersToProfilers != nil && _gProfilersToTracers != nil, |
| 91 | + @"Structures should have already been initialized by the time they are being queried"); |
| 92 | + |
| 93 | + const auto tracerKey = tracer.traceId.sentryIdString; |
| 94 | + const auto profiler = _gTracersToProfilers[tracerKey]; |
| 95 | + |
| 96 | + if (profiler == nil) { |
| 97 | + return; |
64 | 98 | } |
65 | 99 |
|
66 | | - _gTracersToProfilers[tracerKey] = profiler; |
| 100 | + _unsafe_cleanUpProfiler(profiler, tracerKey); |
| 101 | + |
| 102 | +# if SENTRY_HAS_UIKIT |
| 103 | + if (_gProfilersToTracers.count == 0) { |
| 104 | + [SentryDependencyContainer.sharedInstance.framesTracker resetProfilingTimestamps]; |
| 105 | + } |
| 106 | +# endif // SENTRY_HAS_UIKIT |
67 | 107 | } |
68 | 108 |
|
69 | 109 | SentryProfiler *_Nullable profilerForFinishedTracer(SentryTracer *tracer) |
|
81 | 121 | return nil; |
82 | 122 | } |
83 | 123 |
|
84 | | - const auto profilerKey = profiler.profileId.sentryIdString; |
85 | | - |
86 | | - [_gTracersToProfilers removeObjectForKey:tracerKey]; |
87 | | - [_gProfilersToTracers[profilerKey] removeObject:tracer]; |
88 | | - if ([_gProfilersToTracers[profilerKey] count] == 0) { |
89 | | - [_gProfilersToTracers removeObjectForKey:profilerKey]; |
90 | | - if ([profiler isRunning]) { |
91 | | - [profiler stopForReason:SentryProfilerTruncationReasonNormal]; |
92 | | - } |
93 | | - } |
| 124 | + _unsafe_cleanUpProfiler(profiler, tracerKey); |
94 | 125 |
|
95 | 126 | # if SENTRY_HAS_UIKIT |
96 | 127 | profiler._screenFrameData = |
|
111 | 142 | [_gTracersToProfilers removeAllObjects]; |
112 | 143 | [_gProfilersToTracers removeAllObjects]; |
113 | 144 | } |
| 145 | + |
| 146 | +NSUInteger |
| 147 | +currentProfiledTracers() |
| 148 | +{ |
| 149 | + std::lock_guard<std::mutex> l(_gStateLock); |
| 150 | + return [_gTracersToProfilers count]; |
| 151 | +} |
114 | 152 | # endif // defined(TEST) || defined(TESTCI) |
115 | 153 |
|
116 | 154 | #endif // SENTRY_TARGET_PROFILING_SUPPORTED |
0 commit comments