202 lines
7.0 KiB
Objective-C
202 lines
7.0 KiB
Objective-C
#import "MMCPULoadProvider.h"
|
|
#import <sys/sysctl.h>
|
|
#import <os/lock.h>
|
|
|
|
@interface MMCPULoadProvider () {
|
|
os_unfair_lock _lock;
|
|
processor_cpu_load_info_t _previousCpuInfo;
|
|
mach_msg_type_number_t _previousCpuInfoCount;
|
|
natural_t _coreCount;
|
|
uint64_t _maxFrequencyHz;
|
|
}
|
|
@end
|
|
|
|
@implementation MMCPULoadProvider
|
|
|
|
- (instancetype)init {
|
|
self = [super init];
|
|
if (self) {
|
|
_lock = OS_UNFAIR_LOCK_INIT;
|
|
_previousCpuInfo = NULL;
|
|
_previousCpuInfoCount = 0;
|
|
|
|
// Read CPU core count
|
|
natural_t cCount = 1;
|
|
size_t size = sizeof(cCount);
|
|
sysctlbyname("hw.logicalcpu", &cCount, &size, NULL, 0);
|
|
_coreCount = cCount;
|
|
|
|
// Read maximum frequency
|
|
uint64_t maxFreq = 0;
|
|
size = sizeof(maxFreq);
|
|
if (sysctlbyname("hw.cpufrequency_max", &maxFreq, &size, NULL, 0) != 0) {
|
|
sysctlbyname("hw.cpufrequency", &maxFreq, &size, NULL, 0);
|
|
}
|
|
_maxFrequencyHz = maxFreq;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
if (_previousCpuInfo != NULL) {
|
|
free(_previousCpuInfo);
|
|
_previousCpuInfo = NULL;
|
|
}
|
|
}
|
|
|
|
- (MMTelemetryDomain)domain {
|
|
return MMTelemetryDomainCPU;
|
|
}
|
|
|
|
- (NSString *)providerIdentifier {
|
|
return @"com.i3omb.macmonitor.telemetry.cpuload";
|
|
}
|
|
|
|
- (BOOL)isAvailable {
|
|
return YES;
|
|
}
|
|
|
|
- (natural_t)coreCount {
|
|
return _coreCount;
|
|
}
|
|
|
|
+ (NSDictionary<NSString *, id> *)calculateLoadFromPreviousTicks:(const struct processor_cpu_load_info * _Nullable)prevTicks
|
|
currentTicks:(const struct processor_cpu_load_info *)currTicks
|
|
coreCount:(natural_t)coreCount {
|
|
if (!currTicks || coreCount == 0) return @{};
|
|
|
|
NSMutableArray<NSDictionary<NSString *, NSNumber *> *> *coresList = [NSMutableArray arrayWithCapacity:coreCount];
|
|
double totalUserPercent = 0.0;
|
|
double totalSystemPercent = 0.0;
|
|
double totalIdlePercent = 0.0;
|
|
double totalActivePercent = 0.0;
|
|
|
|
for (natural_t i = 0; i < coreCount; i++) {
|
|
uint64_t user = currTicks[i].cpu_ticks[CPU_STATE_USER];
|
|
uint64_t system = currTicks[i].cpu_ticks[CPU_STATE_SYSTEM];
|
|
uint64_t idle = currTicks[i].cpu_ticks[CPU_STATE_IDLE];
|
|
uint64_t nice = currTicks[i].cpu_ticks[CPU_STATE_NICE];
|
|
|
|
uint64_t deltaUser = 0;
|
|
uint64_t deltaSystem = 0;
|
|
uint64_t deltaIdle = 0;
|
|
uint64_t deltaNice = 0;
|
|
|
|
if (prevTicks != NULL) {
|
|
deltaUser = (user >= prevTicks[i].cpu_ticks[CPU_STATE_USER]) ? (user - prevTicks[i].cpu_ticks[CPU_STATE_USER]) : 0;
|
|
deltaSystem = (system >= prevTicks[i].cpu_ticks[CPU_STATE_SYSTEM]) ? (system - prevTicks[i].cpu_ticks[CPU_STATE_SYSTEM]) : 0;
|
|
deltaIdle = (idle >= prevTicks[i].cpu_ticks[CPU_STATE_IDLE]) ? (idle - prevTicks[i].cpu_ticks[CPU_STATE_IDLE]) : 0;
|
|
deltaNice = (nice >= prevTicks[i].cpu_ticks[CPU_STATE_NICE]) ? (nice - prevTicks[i].cpu_ticks[CPU_STATE_NICE]) : 0;
|
|
}
|
|
|
|
uint64_t deltaTotal = deltaUser + deltaSystem + deltaIdle + deltaNice;
|
|
double uPct = 0.0, sPct = 0.0, iPct = 100.0, aPct = 0.0;
|
|
|
|
if (deltaTotal > 0) {
|
|
uPct = ((double)deltaUser / (double)deltaTotal) * 100.0;
|
|
sPct = ((double)deltaSystem / (double)deltaTotal) * 100.0;
|
|
iPct = ((double)deltaIdle / (double)deltaTotal) * 100.0;
|
|
aPct = 100.0 - iPct;
|
|
if (aPct < 0.0) aPct = 0.0;
|
|
if (aPct > 100.0) aPct = 100.0;
|
|
}
|
|
|
|
totalUserPercent += uPct;
|
|
totalSystemPercent += sPct;
|
|
totalIdlePercent += iPct;
|
|
totalActivePercent += aPct;
|
|
|
|
[coresList addObject:@{
|
|
@"coreIndex": @(i),
|
|
@"userPercent": @(uPct),
|
|
@"systemPercent": @(sPct),
|
|
@"idlePercent": @(iPct),
|
|
@"totalPercent": @(aPct)
|
|
}];
|
|
}
|
|
|
|
double avgUser = coreCount > 0 ? (totalUserPercent / coreCount) : 0.0;
|
|
double avgSystem = coreCount > 0 ? (totalSystemPercent / coreCount) : 0.0;
|
|
double avgIdle = coreCount > 0 ? (totalIdlePercent / coreCount) : 100.0;
|
|
double avgTotal = coreCount > 0 ? (totalActivePercent / coreCount) : 0.0;
|
|
|
|
return @{
|
|
@"cores": coresList,
|
|
@"coreCount": @(coreCount),
|
|
@"userPercent": @(avgUser),
|
|
@"systemPercent": @(avgSystem),
|
|
@"idlePercent": @(avgIdle),
|
|
@"totalPercent": @(avgTotal)
|
|
};
|
|
}
|
|
|
|
- (nullable NSDictionary<NSString *, id> *)sampleTelemetryWithError:(NSError **)error {
|
|
natural_t numCPUs = 0;
|
|
processor_info_array_t cpuInfo = NULL;
|
|
mach_msg_type_number_t numCpuInfo = 0;
|
|
|
|
kern_return_t kr = host_processor_info(
|
|
mach_host_self(),
|
|
PROCESSOR_CPU_LOAD_INFO,
|
|
&numCPUs,
|
|
&cpuInfo,
|
|
&numCpuInfo
|
|
);
|
|
|
|
if (kr != KERN_SUCCESS || cpuInfo == NULL) {
|
|
if (error) {
|
|
*error = [NSError errorWithDomain:@"com.i3omb.MacMonitor.CPU"
|
|
code:kr
|
|
userInfo:@{NSLocalizedDescriptionKey: @"host_processor_info failed to read CPU ticks"}];
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
os_unfair_lock_lock(&_lock);
|
|
|
|
processor_cpu_load_info_t currentCpuInfo = (processor_cpu_load_info_t)cpuInfo;
|
|
NSDictionary<NSString *, id> *metrics = [MMCPULoadProvider calculateLoadFromPreviousTicks:_previousCpuInfo
|
|
currentTicks:currentCpuInfo
|
|
coreCount:numCPUs];
|
|
|
|
// Free previous malloc buffer
|
|
if (_previousCpuInfo != NULL) {
|
|
free(_previousCpuInfo);
|
|
_previousCpuInfo = NULL;
|
|
}
|
|
|
|
// Allocate new buffer to preserve state for the next delta
|
|
vm_size_t bufferSize = numCpuInfo * sizeof(integer_t);
|
|
_previousCpuInfo = (processor_cpu_load_info_t)malloc(bufferSize);
|
|
if (_previousCpuInfo != NULL) {
|
|
memcpy(_previousCpuInfo, cpuInfo, bufferSize);
|
|
_previousCpuInfoCount = numCpuInfo;
|
|
}
|
|
|
|
// Deallocate the buffer returned by the kernel
|
|
vm_deallocate(mach_task_self(), (vm_address_t)cpuInfo, numCpuInfo * sizeof(integer_t));
|
|
|
|
os_unfair_lock_unlock(&_lock);
|
|
|
|
// Read current frequency
|
|
uint64_t currentFreq = 0;
|
|
size_t size = sizeof(currentFreq);
|
|
if (sysctlbyname("hw.cpufrequency", ¤tFreq, &size, NULL, 0) != 0) {
|
|
currentFreq = _maxFrequencyHz;
|
|
}
|
|
|
|
// Thermal State
|
|
NSProcessInfoThermalState thermalState = [NSProcessInfo processInfo].thermalState;
|
|
BOOL isThrottled = (thermalState >= NSProcessInfoThermalStateSerious);
|
|
|
|
NSMutableDictionary<NSString *, id> *result = [metrics mutableCopy];
|
|
result[@"frequencyHz"] = @(currentFreq);
|
|
result[@"maxFrequencyHz"] = @(_maxFrequencyHz);
|
|
result[@"thermalState"] = @(thermalState);
|
|
result[@"isThrottled"] = @(isThrottled);
|
|
|
|
return [result copy];
|
|
}
|
|
|
|
@end
|