diff options
Diffstat (limited to 'src/osx/sensors.cpp')
-rw-r--r-- | src/osx/sensors.cpp | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/src/osx/sensors.cpp b/src/osx/sensors.cpp index 851b284..56aca68 100644 --- a/src/osx/sensors.cpp +++ b/src/osx/sensors.cpp @@ -3,10 +3,9 @@ #include <CoreFoundation/CoreFoundation.h> #include <IOKit/hidsystem/IOHIDEventSystemClient.h> -#include <btop_tools.hpp> -#include <iostream> -#include <map> #include <string> +#include <numeric> +#include <vector> extern "C" { typedef struct __IOHIDEvent *IOHIDEventRef; @@ -38,7 +37,8 @@ CFDictionaryRef matching(int page, int usage) { nums[1] = CFNumberCreate(0, kCFNumberSInt32Type, &usage); CFDictionaryRef dict = CFDictionaryCreate(0, (const void **)keys, (const void **)nums, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); - CFRelease(keys); + CFRelease(keys[0]); + CFRelease(keys[1]); return dict; } @@ -54,14 +54,14 @@ double getValue(IOHIDServiceClientRef sc) { } // extern C -unordered_flat_map<int, double> Cpu::ThermalSensors::getSensors() { - unordered_flat_map<int, double> cpuValues; +long long Cpu::ThermalSensors::getSensors() { CFDictionaryRef thermalSensors = matching(0xff00, 5); // 65280_10 = FF00_16 // thermalSensors's PrimaryUsagePage should be 0xff00 for M1 chip, instead of 0xff05 // can be checked by ioreg -lfx IOHIDEventSystemClientRef system = IOHIDEventSystemClientCreate(kCFAllocatorDefault); IOHIDEventSystemClientSetMatching(system, thermalSensors); CFArrayRef matchingsrvs = IOHIDEventSystemClientCopyServices(system); + std::vector<double> temps; if (matchingsrvs) { long count = CFArrayGetCount(matchingsrvs); for (int i = 0; i < count; i++) { @@ -72,19 +72,13 @@ unordered_flat_map<int, double> Cpu::ThermalSensors::getSensors() { char buf[200]; CFStringGetCString(name, buf, 200, kCFStringEncodingASCII); std::string n(buf); - if (n.starts_with("PMU tdie")) { - // this is just a guess, nobody knows which sensors mean what - // on my system PMU tdie 3 and 9 are missing... - // there is also PMU tdev1-8 but it has negative values?? - // there is also eACC for efficiency package but it only has 2 entries - // and pACC for performance but it has 7 entries (2 - 9) WTF - std::string indexString = n.substr(8, 1); - int index = stoi(indexString); - cpuValues[index - 1] = getValue(sc); - Logger::debug("T " + n + "=" + std::to_string(cpuValues[index - 1])); - } else if (n == "SOC MTR Temp Sensor0") { - // package T for Apple Silicon - also a guess - cpuValues[0] = getValue(sc); + // this is just a guess, nobody knows which sensors mean what + // on my system PMU tdie 3 and 9 are missing... + // there is also PMU tdev1-8 but it has negative values?? + // there is also eACC for efficiency package but it only has 2 entries + // and pACC for performance but it has 7 entries (2 - 9) WTF + if (n.starts_with("eACC") or n.starts_with("pACC")) { + temps.push_back(getValue(sc)); } CFRelease(name); } @@ -94,5 +88,6 @@ unordered_flat_map<int, double> Cpu::ThermalSensors::getSensors() { } CFRelease(system); CFRelease(thermalSensors); - return cpuValues; + if (temps.empty()) return 0ll; + return round(std::accumulate(temps.begin(), temps.end(), 0ll) / temps.size()); } |