Skip to content

Commit

Permalink
add screen brightness/swDimming/capturing; battery state; diff task e…
Browse files Browse the repository at this point in the history
…vents for result
  • Loading branch information
armcknight committed Jul 26, 2023
1 parent 275a26e commit 55b9217
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
11 changes: 11 additions & 0 deletions Samples/iOS-Swift/iOS-Swift/Tools/SentryBenchmarking.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));
* @warning apparently setting batteryMonitoringEnabled YES from multiple threads can cause a crash
*/
@property float batteryLevel;
@property UIDeviceBatteryState batteryState;
/** From @c NSProcessInfo.lowPowerModeEnabled */
@property BOOL lowPowerModeEnabled;
/** From @c NSProcessInfo.thermalState */
Expand Down Expand Up @@ -71,6 +72,15 @@ extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));
@property SentryTaskEventsReading *taskEvents;
@end

@interface SentryScreenReading : NSObject
- (instancetype)initWithError:(NSError **)error;
@property CGFloat displayBrightness;
/** A comment in UIScreen.h states this specifically can incur performance costs. */
@property BOOL wantsSoftwareDimming;
/** Screen is being captured, eg by AirPlay, mirroring etc. */
@property BOOL captured;
@end

/**
* For data that must be sampled because there are a variable amount of data points (threads may be
* created or destroyed during the benchmark) or cannot be summed (like CPU usage percentage), a
Expand All @@ -81,6 +91,7 @@ extern uint64_t dispatch_benchmark(size_t count, void (^block)(void));
@property NSDictionary<NSString *, SentryThreadBasicInfo *> *threadInfos;
@property NSArray<NSNumber *> *cpuUsagePerCore;
@property SentryPowerReading *power;
@property SentryScreenReading *device;
@end

/**
Expand Down
79 changes: 67 additions & 12 deletions Samples/iOS-Swift/iOS-Swift/Tools/SentryBenchmarking.mm
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,15 @@ - (instancetype)initWithStart:(SentryBenchmarkReading *)start
end.power.info.task_pset_switches - start.power.info.task_pset_switches
};

// TODO: diff all task events
// _results.contextSwitches = end.contextSwitches - start.contextSwitches;
_results.taskEvents = [[SentryTaskEventsReading alloc] init];
_results.taskEvents.data = { end.taskEvents.data.faults - start.taskEvents.data.faults,
end.taskEvents.data.pageins - start.taskEvents.data.pageins,
end.taskEvents.data.cow_faults - start.taskEvents.data.cow_faults,
end.taskEvents.data.messages_sent - start.taskEvents.data.messages_sent,
end.taskEvents.data.messages_received - start.taskEvents.data.messages_received,
end.taskEvents.data.syscalls_mach - start.taskEvents.data.syscalls_mach,
end.taskEvents.data.syscalls_unix - start.taskEvents.data.syscalls_unix,
end.taskEvents.data.csw - start.taskEvents.data.csw };

_sampledResults = aggregatedSampleResult;

Expand Down Expand Up @@ -413,9 +420,14 @@ - (instancetype)initWithError:(NSError *__autoreleasing _Nullable *)error
}

_info = powerInfo;
_batteryLevel = UIDevice.currentDevice.batteryLevel;
_thermalState = NSProcessInfo.processInfo.thermalState;
_lowPowerModeEnabled = NSProcessInfo.processInfo.lowPowerModeEnabled;

UIDevice *_Nonnull device = UIDevice.currentDevice;
_batteryLevel = device.batteryLevel;
_batteryState = device.batteryState;

NSProcessInfo *_Nonnull processInfo = NSProcessInfo.processInfo;
_thermalState = processInfo.thermalState;
_lowPowerModeEnabled = processInfo.lowPowerModeEnabled;
}
return self;
}
Expand Down Expand Up @@ -443,16 +455,35 @@ - (NSString *)_thermalStateName
return @"critical";
default:
NSAssert(NO, @"unknown thermal state: %lld", (long long)_thermalState);
return nil;
}
}

- (NSString *)nameForBatteryState:(UIDeviceBatteryState)state
{
switch (state) {
case UIDeviceBatteryStateCharging:
return @"charging";
case UIDeviceBatteryStateFull:
return @"full";
case UIDeviceBatteryStateUnknown:
return @"unknown";
case UIDeviceBatteryStateUnplugged:
return @"unplugged";
default:
NSAssert(NO, @"unexpected battery state value: %ld", (long)state);
return nil;
}
}

- (NSString *)description
{
const auto string =
[NSMutableString stringWithFormat:@"battery level: %.3f; low power mode? %@; thermalState: "
@"%@; totalCPU: %llu; totalGPU: %llu",
_batteryLevel, _lowPowerModeEnabled ? @YES : @NO, [self _thermalStateName],
[self totalCPU], [self totalGPU]];
const auto string = [NSMutableString
stringWithFormat:
@"battery state: %@; battery level: %.3f; low power mode? %@; thermalState: "
@"%@; totalCPU: %llu; totalGPU: %llu",
[self nameForBatteryState:_batteryState], _batteryLevel, _lowPowerModeEnabled ? @YES : @NO,
[self _thermalStateName], [self totalCPU], [self totalGPU]];
#if defined(__arm__) || defined(__arm64__)
[string appendFormat:@"; task energy: %llu nanojoules", _info.task_energy];
#endif // defined(__arm__) || defined(__arm64__)
Expand Down Expand Up @@ -572,6 +603,28 @@ - (NSString *)description

@end

@implementation SentryScreenReading

- (instancetype)initWithError:(NSError **)error
{
if ((self = [super init])) {
UIScreen *screen = UIScreen.mainScreen;
_displayBrightness = screen.brightness;
_wantsSoftwareDimming = screen.wantsSoftwareDimming;
_captured = screen.captured;
}
return self;
}

- (NSString *)description
{
return [NSString
stringWithFormat:@"screen brightness: %.1f; software dimming: %@; captured: %@",
_displayBrightness, _wantsSoftwareDimming ? @"YES" : @"NO", _captured ? @"YES" : @"NO"];
}

@end

@implementation SentryBenchmarkSample

- (instancetype)initWithError:(NSError **)error
Expand All @@ -580,6 +633,7 @@ - (instancetype)initWithError:(NSError **)error
_machTime = machTime();
_threadInfos = cpuInfoByThread();
_cpuUsagePerCore = cpuUsagePerCore(error);
_device = [[SentryScreenReading alloc] initWithError:nil];
_power = [[SentryPowerReading alloc] initWithError:nil];

#if defined(__arm__) || defined(__arm64__)
Expand All @@ -594,8 +648,9 @@ - (NSString *)description
const auto cores = [NSMutableArray array];
[_cpuUsagePerCore enumerateObjectsUsingBlock:^(NSNumber *_Nonnull obj, NSUInteger idx,
BOOL *_Nonnull stop) { [cores addObject:obj.stringValue]; }];
return [NSString stringWithFormat:@"mach time: %llu; Thread infos: %@\nCore infos: %@",
_machTime, _threadInfos, [cores componentsJoinedByString:@", "]];
return [NSString
stringWithFormat:@"mach time: %llu;\ndevice info: %@\nThread infos: %@\nCore infos: %@",
_machTime, _device, _threadInfos, [cores componentsJoinedByString:@", "]];
}

@end
Expand Down

0 comments on commit 55b9217

Please sign in to comment.