152 lines
4.7 KiB
Objective-C
152 lines
4.7 KiB
Objective-C
#import "MMCPUThermalProvider.h"
|
|
#import <sys/sysctl.h>
|
|
|
|
@interface MMCPUThermalProvider () {
|
|
MMAppleSMCClient *_smcClient;
|
|
NSMutableArray<NSString *> *_discoveredCoreKeys;
|
|
NSString *_packageKey;
|
|
BOOL _probed;
|
|
}
|
|
@end
|
|
|
|
@implementation MMCPUThermalProvider
|
|
|
|
- (instancetype)init {
|
|
return [self initWithSMCClient:[MMAppleSMCClient sharedClient]];
|
|
}
|
|
|
|
- (instancetype)initWithSMCClient:(MMAppleSMCClient *)client {
|
|
self = [super init];
|
|
if (self) {
|
|
_smcClient = client;
|
|
_discoveredCoreKeys = [NSMutableArray array];
|
|
[self probeSensors];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)probeSensors {
|
|
if (!_smcClient.isAvailable) return;
|
|
|
|
// Probe package temperature key
|
|
NSArray<NSString *> *packageCandidates = @[@"TC0P", @"TC0D", @"TC0H", @"TCXC"];
|
|
for (NSString *candidate in packageCandidates) {
|
|
NSNumber *val = [_smcClient readNumericValueForKey:candidate error:nil];
|
|
if (val && [val doubleValue] > 0.0 && [val doubleValue] < 125.0) {
|
|
_packageKey = candidate;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Probe core keys (TC0C, TC1C, etc.)
|
|
int coreCount = 1;
|
|
size_t size = sizeof(coreCount);
|
|
sysctlbyname("hw.physicalcpu", &coreCount, &size, NULL, 0);
|
|
if (coreCount <= 0) coreCount = 4;
|
|
|
|
for (int i = 0; i < coreCount && i < 32; i++) {
|
|
NSString *key = [NSString stringWithFormat:@"TC%dC", i];
|
|
NSNumber *val = [_smcClient readNumericValueForKey:key error:nil];
|
|
if (val && [val doubleValue] > 0.0 && [val doubleValue] < 125.0) {
|
|
[_discoveredCoreKeys addObject:key];
|
|
}
|
|
}
|
|
|
|
_probed = YES;
|
|
}
|
|
|
|
- (MMTelemetryDomain)domain {
|
|
return MMTelemetryDomainThermal;
|
|
}
|
|
|
|
- (NSString *)providerIdentifier {
|
|
return @"com.i3omb.macmonitor.telemetry.thermal.cpu";
|
|
}
|
|
|
|
- (BOOL)isAvailable {
|
|
return _smcClient.isAvailable;
|
|
}
|
|
|
|
- (MMAppleSMCClient *)smcClient {
|
|
return _smcClient;
|
|
}
|
|
|
|
- (NSArray<NSString *> *)discoveredCoreKeys {
|
|
return [_discoveredCoreKeys copy];
|
|
}
|
|
|
|
- (NSString *)packageKey {
|
|
return _packageKey;
|
|
}
|
|
|
|
+ (NSDictionary<NSString *, id> *)calculateThermalMetricsWithPackageTemp:(nullable NSNumber *)pkgTemp
|
|
coreTemps:(NSDictionary<NSString *, NSNumber *> *)coreDict
|
|
thermalState:(NSProcessInfoThermalState)state {
|
|
double peak = pkgTemp ? [pkgTemp doubleValue] : 0.0;
|
|
double sum = 0.0;
|
|
int count = 0;
|
|
|
|
NSMutableArray<NSDictionary<NSString *, id> *> *coresList = [NSMutableArray arrayWithCapacity:coreDict.count];
|
|
NSArray<NSString *> *sortedKeys = [coreDict.allKeys sortedArrayUsingSelector:@selector(compare:)];
|
|
|
|
for (NSUInteger idx = 0; idx < sortedKeys.count; idx++) {
|
|
NSString *k = sortedKeys[idx];
|
|
double temp = [coreDict[k] doubleValue];
|
|
if (temp > peak) peak = temp;
|
|
sum += temp;
|
|
count++;
|
|
|
|
[coresList addObject:@{
|
|
@"coreIndex": @(idx),
|
|
@"key": k,
|
|
@"temperature": @(temp)
|
|
}];
|
|
}
|
|
|
|
double avg = (count > 0) ? (sum / count) : (pkgTemp ? [pkgTemp doubleValue] : 0.0);
|
|
|
|
NSString *stateStr = @"Nominal";
|
|
switch (state) {
|
|
case NSProcessInfoThermalStateNominal: stateStr = @"Nominal"; break;
|
|
case NSProcessInfoThermalStateFair: stateStr = @"Fair"; break;
|
|
case NSProcessInfoThermalStateSerious: stateStr = @"Serious"; break;
|
|
case NSProcessInfoThermalStateCritical: stateStr = @"Critical"; break;
|
|
}
|
|
|
|
return @{
|
|
@"packageTemperature": pkgTemp ?: @(0.0),
|
|
@"averageCoreTemperature": @(avg),
|
|
@"peakCoreTemperature": @(peak),
|
|
@"coreTemperatures": coresList,
|
|
@"thermalPressureState": stateStr,
|
|
@"isThrottling": @(state >= NSProcessInfoThermalStateSerious)
|
|
};
|
|
}
|
|
|
|
- (nullable NSDictionary<NSString *, id> *)sampleTelemetryWithError:(NSError **)error {
|
|
if (!_probed && _smcClient.isAvailable) {
|
|
[self probeSensors];
|
|
}
|
|
|
|
NSNumber *pkgTemp = nil;
|
|
if (_packageKey) {
|
|
pkgTemp = [_smcClient readNumericValueForKey:_packageKey error:nil];
|
|
}
|
|
|
|
NSMutableDictionary<NSString *, NSNumber *> *coreTemps = [NSMutableDictionary dictionary];
|
|
for (NSString *coreKey in _discoveredCoreKeys) {
|
|
NSNumber *t = [_smcClient readNumericValueForKey:coreKey error:nil];
|
|
if (t) {
|
|
coreTemps[coreKey] = t;
|
|
}
|
|
}
|
|
|
|
NSProcessInfoThermalState thermalState = [NSProcessInfo processInfo].thermalState;
|
|
|
|
return [MMCPUThermalProvider calculateThermalMetricsWithPackageTemp:pkgTemp
|
|
coreTemps:coreTemps
|
|
thermalState:thermalState];
|
|
}
|
|
|
|
@end
|