mirror of
https://github.com/aristocratos/btop.git
synced 2024-06-12 15:34:41 +12:00
don't iterate 3 times
This commit is contained in:
parent
d8408336e3
commit
808f09c974
|
@ -42,96 +42,50 @@ CFDictionaryRef matching(int page, int usage) {
|
||||||
return dict;
|
return dict;
|
||||||
}
|
}
|
||||||
|
|
||||||
CFArrayRef getProductNames(CFDictionaryRef sensors) {
|
double getValue(IOHIDServiceClientRef sc) {
|
||||||
IOHIDEventSystemClientRef system = IOHIDEventSystemClientCreate(kCFAllocatorDefault); // in CFBase.h = NULL
|
IOHIDEventRef event = IOHIDServiceClientCopyEvent(sc, kIOHIDEventTypeTemperature, 0, 0); // here we use ...CopyEvent
|
||||||
if (system) {
|
double temp = 0.0;
|
||||||
IOHIDEventSystemClientSetMatching(system, sensors);
|
if (event != 0) {
|
||||||
CFArrayRef matchingsrvs = IOHIDEventSystemClientCopyServices(system); // matchingsrvs = matching services
|
temp = IOHIDEventGetFloatValue(event, IOHIDEventFieldBase(kIOHIDEventTypeTemperature));
|
||||||
if (matchingsrvs) {
|
CFRelease(event);
|
||||||
long count = CFArrayGetCount(matchingsrvs);
|
|
||||||
CFMutableArrayRef array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
|
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
IOHIDServiceClientRef sc = (IOHIDServiceClientRef)CFArrayGetValueAtIndex(matchingsrvs, i);
|
|
||||||
CFStringRef name = IOHIDServiceClientCopyProperty(sc, CFSTR("Product")); // here we use ...CopyProperty
|
|
||||||
if (name) {
|
|
||||||
CFArrayAppendValue(array, name);
|
|
||||||
} else {
|
|
||||||
CFArrayAppendValue(array, CFSTR("noname")); // @ gives a Ref like in "CFStringRef name"
|
|
||||||
}
|
|
||||||
CFRelease(name);
|
|
||||||
}
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
CFRelease(system);
|
|
||||||
}
|
}
|
||||||
return nullptr;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
CFArrayRef getThermalValues(CFDictionaryRef sensors) {
|
} // extern C
|
||||||
IOHIDEventSystemClientRef system = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
|
|
||||||
IOHIDEventSystemClientSetMatching(system, sensors);
|
|
||||||
CFArrayRef matchingsrvs = IOHIDEventSystemClientCopyServices(system);
|
|
||||||
|
|
||||||
long count = CFArrayGetCount(matchingsrvs);
|
|
||||||
CFMutableArrayRef array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
|
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
IOHIDServiceClientRef sc = (IOHIDServiceClientRef)CFArrayGetValueAtIndex(matchingsrvs, i);
|
|
||||||
IOHIDEventRef event = IOHIDServiceClientCopyEvent(sc, kIOHIDEventTypeTemperature, 0, 0); // here we use ...CopyEvent
|
|
||||||
|
|
||||||
CFNumberRef value;
|
|
||||||
if (event != 0) {
|
|
||||||
double temp = IOHIDEventGetFloatValue(event, IOHIDEventFieldBase(kIOHIDEventTypeTemperature));
|
|
||||||
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &temp);
|
|
||||||
} else {
|
|
||||||
double temp = 0;
|
|
||||||
value = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &temp);
|
|
||||||
}
|
|
||||||
CFArrayAppendValue(array, value);
|
|
||||||
CFRelease(value);
|
|
||||||
}
|
|
||||||
CFRelease(system);
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unordered_flat_map<int, double> Cpu::ThermalSensors::getSensors() {
|
unordered_flat_map<int, double> Cpu::ThermalSensors::getSensors() {
|
||||||
unordered_flat_map<int, double> cpuValues;
|
unordered_flat_map<int, double> cpuValues;
|
||||||
CFDictionaryRef thermalSensors = matching(0xff00, 5); // 65280_10 = FF00_16
|
CFDictionaryRef thermalSensors = matching(0xff00, 5); // 65280_10 = FF00_16
|
||||||
// thermalSensors's PrimaryUsagePage should be 0xff00 for M1 chip, instead of 0xff05
|
// thermalSensors's PrimaryUsagePage should be 0xff00 for M1 chip, instead of 0xff05
|
||||||
// can be checked by ioreg -lfx
|
// can be checked by ioreg -lfx
|
||||||
CFArrayRef thermalNames = getProductNames(thermalSensors);
|
IOHIDEventSystemClientRef system = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
|
||||||
if (thermalNames) {
|
IOHIDEventSystemClientSetMatching(system, thermalSensors);
|
||||||
CFArrayRef thermalValues = getThermalValues(thermalSensors);
|
CFArrayRef matchingsrvs = IOHIDEventSystemClientCopyServices(system);
|
||||||
long count = CFArrayGetCount(thermalNames);
|
|
||||||
for (int i = 0; i < count; i++) {
|
long count = CFArrayGetCount(matchingsrvs);
|
||||||
CFStringRef nameRef = (CFStringRef)CFArrayGetValueAtIndex(thermalNames, i);
|
for (int i = 0; i < count; i++) {
|
||||||
char buf[200];
|
IOHIDServiceClientRef sc = (IOHIDServiceClientRef)CFArrayGetValueAtIndex(matchingsrvs, i);
|
||||||
CFStringGetCString(nameRef, buf, 200, kCFStringEncodingASCII);
|
if (sc) {
|
||||||
std::string n(buf);
|
CFStringRef name = IOHIDServiceClientCopyProperty(sc, CFSTR("Product")); // here we use ...CopyProperty
|
||||||
CFNumberRef value = (CFNumberRef)CFArrayGetValueAtIndex(thermalValues, i);
|
if (name) {
|
||||||
double temp = 0.0;
|
char buf[200];
|
||||||
CFNumberGetValue(value, kCFNumberDoubleType, &temp);
|
CFStringGetCString(name, buf, 200, kCFStringEncodingASCII);
|
||||||
if (n.starts_with("PMU tdie")) {
|
std::string n(buf);
|
||||||
// Apple Silicon
|
if (n.starts_with("PMU tdie")) {
|
||||||
std::string indexString = n.substr(8, 1);
|
// Apple Silicon
|
||||||
int index = stoi(indexString);
|
std::string indexString = n.substr(8, 1);
|
||||||
cpuValues[index - 1] = temp;
|
int index = stoi(indexString);
|
||||||
} else if (n.starts_with("TC") && n[3] == 'c') {
|
cpuValues[index - 1] = getValue(sc);
|
||||||
// intel mac
|
} else if (n == "SOC MTR Temp Sensor0") {
|
||||||
std::string indexString = n.substr(2, 1);
|
cpuValues[0] = getValue(sc); // package T for Apple Silicon
|
||||||
int index = stoi(indexString);
|
}
|
||||||
cpuValues[index] = temp;
|
CFRelease(name);
|
||||||
} else if (n == "TCAD") {
|
|
||||||
cpuValues[0] = temp; // package T for intel
|
|
||||||
} else if (n == "SOC MTR Temp Sensor0") {
|
|
||||||
cpuValues[0] = temp; // package T for Apple Silicon
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CFRelease(thermalNames);
|
|
||||||
CFRelease(thermalValues);
|
|
||||||
}
|
}
|
||||||
|
CFRelease(matchingsrvs);
|
||||||
CFRelease(thermalSensors);
|
CFRelease(thermalSensors);
|
||||||
|
CFRelease(system);
|
||||||
return cpuValues;
|
return cpuValues;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue