Reintroduce cpu-temp in system-info on macOS

This commit is contained in:
Kenneth Benzie 2025-07-29 15:36:50 +01:00
parent 3444e1083b
commit 3d3a56e0ee

View File

@ -8,6 +8,69 @@ if [ ! -d $cache_dir ]; then
mkdir -p $cache_dir
fi
if [ ! -f $cache_dir/cpu-temp ]; then
clang -x objective-c -Wall -O2 -g -c -o $cache_dir/cpu-temp.o - << EOF
#import <Foundation/Foundation.h>
#import <IOKit/hidsystem/IOHIDEventSystemClient.h>
#include <unistd.h>
#define IOHIDEventFieldBase(type) (type << 16)
#define kIOHIDEventTypeTemperature 15
typedef struct __IOHIDEvent *IOHIDEventRef;
typedef struct __IOHIDServiceClient *IOHIDServiceClientRef;
typedef double IOHIDFloat;
IOHIDEventSystemClientRef
IOHIDEventSystemClientCreate(CFAllocatorRef allocator);
int IOHIDEventSystemClientSetMatching(IOHIDEventSystemClientRef client,
CFDictionaryRef match);
IOHIDEventRef IOHIDServiceClientCopyEvent(IOHIDServiceClientRef, int64_t,
int32_t, int64_t);
CFStringRef IOHIDServiceClientCopyProperty(IOHIDServiceClientRef service,
CFStringRef property);
IOHIDFloat IOHIDEventGetFloatValue(IOHIDEventRef event, int32_t field);
int main(int argc, char *argv[]) {
NSDictionary *thermalSensors = @{
@"PrimaryUsagePage" : [NSNumber numberWithInt:0xFF00],
@"PrimaryUsage" : [NSNumber numberWithInt:5],
};
IOHIDEventSystemClientRef system =
IOHIDEventSystemClientCreate(kCFAllocatorDefault);
IOHIDEventSystemClientSetMatching(system,
(__bridge CFDictionaryRef)thermalSensors);
NSArray *serviceClients =
(__bridge NSArray *)IOHIDEventSystemClientCopyServices(system);
long count = [serviceClients count];
for (NSUInteger i = 0; i < count; i++) {
IOHIDServiceClientRef serviceClient =
(IOHIDServiceClientRef)serviceClients[i];
NSString *name = (NSString *)IOHIDServiceClientCopyProperty(
serviceClient, (__bridge CFStringRef) @"Product");
if ([name isEqualToString:[NSString stringWithUTF8String:argv[1]]]) {
IOHIDEventRef event = IOHIDServiceClientCopyEvent(
serviceClient, kIOHIDEventTypeTemperature, 0, 0);
NSNumber *value;
double temp = 0.0;
if (event != 0) {
temp = IOHIDEventGetFloatValue(
event, IOHIDEventFieldBase(kIOHIDEventTypeTemperature));
}
value = [NSNumber numberWithDouble:temp];
printf("%.1lf°C\n", [value doubleValue]);
break;
}
}
return 0;
}
EOF
clang -o $cache_dir/cpu-temp $cache_dir/cpu-temp.o -framework Foundation -framework IOKit
rm $cache_dir/cpu-temp.o
fi
# Cleanup cache file when interrupted.
trap '[ -f $cache_file ] && rm $cache_file; exit' INT
trap '[ -f $cache_file ] && rm $cache_file; exit' TERM
@ -17,8 +80,8 @@ ioreg -w0 -l | grep BatteryInstalled &> /dev/null && \
has_battery=true || has_battery=false
while true; do
# # Get the current CPU temperature.
# cpu_temp="`/usr/local/bin/osx-cpu-temp`"
# Get the current CPU temperature.
cpu_temp="`$cache_dir/cpu-temp 'PMU tdie0'` "
cpu_load=$(sudo powermetrics --format text \
--sample-rate 1200 --sample-count 1 --samplers cpu_power |