Skip to content

Commit fb48c9a

Browse files
authored
ref: Remove SentrySystemWrapper from the dependency container (#6430)
1 parent 089769b commit fb48c9a

File tree

6 files changed

+37
-23
lines changed

6 files changed

+37
-23
lines changed

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,6 @@ - (SentrySwizzleWrapper *)swizzleWrapper SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_
369369
}
370370
#endif // SENTRY_UIKIT_AVAILABLE
371371

372-
#if SENTRY_TARGET_PROFILING_SUPPORTED
373-
- (SentrySystemWrapper *)systemWrapper SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
374-
{
375-
SENTRY_LAZY_INIT(_systemWrapper,
376-
[[SentrySystemWrapper alloc]
377-
initWithProcessorCount:self.processInfoWrapper.processorCount]);
378-
}
379-
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
380-
381372
- (SentryDispatchFactory *)dispatchFactory SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
382373
{
383374
SENTRY_LAZY_INIT(_dispatchFactory, [[SentryDispatchFactory alloc] init]);

Sources/Sentry/SentryMetricProfiler.m

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ @implementation SentryMetricReading
123123
return dict;
124124
}
125125

126+
@interface SentryMetricProfiler ()
127+
128+
@property SentrySystemWrapper *systemWrapper;
129+
130+
@end
131+
132+
# if SENTRY_TEST || SENTRY_TEST_CI
133+
static SentrySystemWrapper *_systemWrapperOverride = nil;
134+
# endif
135+
126136
@implementation SentryMetricProfiler {
127137
SentryDispatchSourceWrapper *_dispatchSource;
128138

@@ -140,11 +150,29 @@ - (instancetype)initWithMode:(SentryProfilerMode)mode
140150
if (self = [super init]) {
141151
// It doesn't make sense to acquire a lock in the init.
142152
[self clearNotThreadSafe];
153+
# if SENTRY_TEST || SENTRY_TEST_CI
154+
self.systemWrapper = _systemWrapperOverride
155+
? _systemWrapperOverride
156+
: [[SentrySystemWrapper alloc]
157+
initWithProcessorCount:SentryDependencyContainer.sharedInstance.processInfoWrapper
158+
.processorCount];
159+
# else
160+
self.systemWrapper = [[SentrySystemWrapper alloc]
161+
initWithProcessorCount:SentryDependencyContainer.sharedInstance.processInfoWrapper
162+
.processorCount];
163+
# endif
143164
_mode = mode;
144165
}
145166
return self;
146167
}
147168

169+
# if SENTRY_TEST || SENTRY_TEST_CI
170+
+ (void)setSystemWrapperOverride:(SentrySystemWrapper *)value
171+
{
172+
_systemWrapperOverride = value;
173+
}
174+
# endif
175+
148176
- (void)dealloc
149177
{
150178
[self stop];
@@ -249,8 +277,7 @@ - (void)registerSampler
249277
- (void)recordMemoryFootprint
250278
{
251279
NSError *error;
252-
SentryRAMBytes footprintBytes =
253-
[SentryDependencyContainer.sharedInstance.systemWrapper memoryFootprintBytes:&error];
280+
SentryRAMBytes footprintBytes = [self.systemWrapper memoryFootprintBytes:&error];
254281

255282
if (error) {
256283
SENTRY_LOG_ERROR(@"Failed to read memory footprint: %@", error);
@@ -265,8 +292,7 @@ - (void)recordMemoryFootprint
265292
- (void)recordCPUsage
266293
{
267294
NSError *error;
268-
NSNumber *result =
269-
[SentryDependencyContainer.sharedInstance.systemWrapper cpuUsageWithError:&error];
295+
NSNumber *result = [self.systemWrapper cpuUsageWithError:&error];
270296

271297
if (error) {
272298
SENTRY_LOG_ERROR(@"Failed to read CPU usages: %@", error);
@@ -287,8 +313,7 @@ - (void)recordCPUsage
287313
- (void)recordEnergyUsageEstimate
288314
{
289315
NSError *error;
290-
NSNumber *reading =
291-
[SentryDependencyContainer.sharedInstance.systemWrapper cpuEnergyUsageWithError:&error];
316+
NSNumber *reading = [self.systemWrapper cpuEnergyUsageWithError:&error];
292317
if (error) {
293318
SENTRY_LOG_ERROR(@"Failed to read CPU energy usage: %@", error);
294319
return;

Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
@class SentrySwizzleWrapper;
1717
@class SentrySysctl;
1818
@class SentryThreadsafeApplication;
19-
@class SentrySystemWrapper;
2019
@class SentryThreadWrapper;
2120
@class SentryFileIOTracker;
2221
@class SentryScopePersistentStore;
@@ -119,9 +118,6 @@ SENTRY_NO_INIT
119118

120119
- (nullable id<SentryApplication>)application;
121120

122-
#if SENTRY_TARGET_PROFILING_SUPPORTED
123-
@property (nonatomic, strong) SentrySystemWrapper *systemWrapper;
124-
#endif // SENTRY_TARGET_PROFILING_SUPPORTED
125121
@property (nonatomic, strong) SentryDispatchFactory *dispatchFactory;
126122
@property (nonatomic, strong) SentryNSTimerFactory *timerFactory;
127123

Sources/Sentry/include/SentryMetricProfiler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# import "SentryProfilerDefines.h"
77

88
@class SentryTransaction;
9+
@class SentrySystemWrapper;
910

1011
NS_ASSUME_NONNULL_BEGIN
1112

@@ -68,6 +69,10 @@ SENTRY_NO_INIT
6869
*/
6970
- (NSDictionary *)copyMetricProfilerData;
7071

72+
# if SENTRY_TEST || SENTRY_TEST_CI
73+
+ (void)setSystemWrapperOverride:(SentrySystemWrapper *)value;
74+
# endif
75+
7176
@end
7277

7378
NS_ASSUME_NONNULL_END

Tests/SentryProfilerTests/SentryProfileTestFixture.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ class SentryProfileTestFixture {
4444
SentryDependencyContainer.sharedInstance().dispatchQueueWrapper = dispatchQueueWrapper
4545
SentryDependencyContainer.sharedInstance().dateProvider = currentDateProvider
4646
SentryDependencyContainer.sharedInstance().random = TestRandom(value: fixedRandomValue)
47-
SentryDependencyContainer.sharedInstance().systemWrapper = systemWrapper
4847
SentryDependencyContainer.sharedInstance().processInfoWrapper = processInfoWrapper
4948
SentryDependencyContainer.sharedInstance().dispatchFactory = dispatchFactory
5049
SentryDependencyContainer.sharedInstance().notificationCenterWrapper = notificationCenter
50+
SentryMetricProfiler.setSystemWrapperOverride(systemWrapper)
5151

5252
timeoutTimerFactory = TestSentryNSTimerFactory(currentDateProvider: self.currentDateProvider)
5353
SentryDependencyContainer.sharedInstance().timerFactory = timeoutTimerFactory

Tests/SentryTests/Helper/SentryDependencyContainerTests.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ final class SentryDependencyContainerTests: XCTestCase {
124124
XCTAssertNotNil(SentryDependencyContainer.sharedInstance().getANRTracker(2.0, isV2Enabled: true))
125125
#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
126126

127-
#if os(iOS) || os(macOS) || targetEnvironment(macCatalyst)
128-
XCTAssertNotNil(SentryDependencyContainer.sharedInstance().systemWrapper)
129-
#endif // os(iOS) || os(macOS) || targetEnvironment(macCatalyst)
130127
XCTAssertNotNil(SentryDependencyContainer.sharedInstance().dispatchFactory)
131128
XCTAssertNotNil(SentryDependencyContainer.sharedInstance().timerFactory)
132129

0 commit comments

Comments
 (0)