From 53c8a0325b7567b84da2f01ebd5acad33db4c0cb Mon Sep 17 00:00:00 2001 From: aristocratos Date: Mon, 25 Oct 2021 13:01:53 +0200 Subject: [PATCH] Cpu temp set to average of pACC and eACC for mac m1 --- src/osx/btop_collect.cpp | 39 ++++++++++++++++----------------------- src/osx/sensors.cpp | 28 ++++++++++++---------------- src/osx/sensors.hpp | 5 +---- 3 files changed, 29 insertions(+), 43 deletions(-) diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index f5c43d7..fd5bf14 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -170,6 +170,7 @@ namespace Cpu { string cpuName; string cpuHz; bool has_battery = true; + bool macM1 = false; tuple current_bat; const array time_names = {"user", "nice", "system", "idle"}; @@ -233,8 +234,10 @@ namespace Cpu { got_sensors = false; if (Config::getB("show_coretemp") and Config::getB("check_temp")) { ThermalSensors sensors; - if (sensors.getSensors().size() > 0) { + if (sensors.getSensors() > 0) { got_sensors = true; + cpu_temp_only = true; + macM1 = true; } else { // try SMC (intel) SMCConnection smcCon; @@ -257,23 +260,12 @@ namespace Cpu { void update_sensors() { current_cpu.temp_max = 95; // we have no idea how to get the critical temp try { - ThermalSensors sensors; - auto sensor = sensors.getSensors(); - if (sensor.size() > 0) { - current_cpu.temp.at(0).push_back((long long)sensor[0]); + if (macM1) { + ThermalSensors sensors; + current_cpu.temp.at(0).push_back(sensors.getSensors()); if (current_cpu.temp.at(0).size() > 20) current_cpu.temp.at(0).pop_front(); - - if (Config::getB("show_coretemp") and not cpu_temp_only) { - for (int core = 1; core <= Shared::coreCount; core++) { - long long temp = (long long)sensor[core]; - if (cmp_less(core, current_cpu.temp.size())) { - current_cpu.temp.at(core).push_back(temp); - if (current_cpu.temp.at(core).size() > 20) - current_cpu.temp.at(core).pop_front(); - } - } - } + } else { SMCConnection smcCon; int threadsPerCore = Shared::coreCount / Shared::physicalCoreCount; @@ -292,6 +284,7 @@ namespace Cpu { } } } catch (std::runtime_error &e) { + got_sensors = false; Logger::error("failed getting CPU temp"); } } @@ -1182,19 +1175,19 @@ namespace Proc { Logger::error("Failed getting CPU load info"); } cpu_load_info = (processor_cpu_load_info_data_t *)info.info_array; - cputimes = (cpu_load_info[0].cpu_ticks[CPU_STATE_USER] - + cpu_load_info[0].cpu_ticks[CPU_STATE_NICE] - + cpu_load_info[0].cpu_ticks[CPU_STATE_SYSTEM] - + cpu_load_info[0].cpu_ticks[CPU_STATE_IDLE]); + for (natural_t i = 0; i < cpu_count; i++) { + cputimes = (cpu_load_info[i].cpu_ticks[CPU_STATE_USER] + + cpu_load_info[i].cpu_ticks[CPU_STATE_NICE] + + cpu_load_info[i].cpu_ticks[CPU_STATE_SYSTEM] + + cpu_load_info[i].cpu_ticks[CPU_STATE_IDLE]); + } } should_filter = true; int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; vector found; size_t size = 0; - struct timeval currentTime; - gettimeofday(¤tTime, NULL); - const double timeNow = currentTime.tv_sec + (currentTime.tv_usec / 1'000'000); + const double timeNow = (time_micros() / 1'000'000); if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0 || size == 0) { Logger::error("Unable to get size of kproc_infos"); diff --git a/src/osx/sensors.cpp b/src/osx/sensors.cpp index 851b284..6b8213d 100644 --- a/src/osx/sensors.cpp +++ b/src/osx/sensors.cpp @@ -7,6 +7,7 @@ #include #include #include +#include extern "C" { typedef struct __IOHIDEvent *IOHIDEventRef; @@ -54,14 +55,14 @@ double getValue(IOHIDServiceClientRef sc) { } // extern C -unordered_flat_map Cpu::ThermalSensors::getSensors() { - unordered_flat_map 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); + vector temps; if (matchingsrvs) { long count = CFArrayGetCount(matchingsrvs); for (int i = 0; i < count; i++) { @@ -72,19 +73,13 @@ unordered_flat_map 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 +89,6 @@ unordered_flat_map 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()); } diff --git a/src/osx/sensors.hpp b/src/osx/sensors.hpp index 1056437..48287ee 100644 --- a/src/osx/sensors.hpp +++ b/src/osx/sensors.hpp @@ -1,10 +1,7 @@ -#include - -using robin_hood::unordered_flat_map; namespace Cpu { class ThermalSensors { public: - unordered_flat_map getSensors(); + long long getSensors(); }; } // namespace Cpu