From cc2a4987efde6f3cf8ef8ab96d6793c8220375cb Mon Sep 17 00:00:00 2001 From: Jos Dehaes Date: Sat, 23 Oct 2021 23:38:00 +0200 Subject: [PATCH] temp --- src/freebsd/btop_collect.cpp | 31 +++++++++++++++++++++++++++++-- src/osx/btop_collect.cpp | 2 ++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index 40001ff..87cb97a 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -66,7 +66,6 @@ namespace Cpu { vector available_fields = {"total"}; vector available_sensors = {"Auto"}; cpu_info current_cpu; - fs::path freq_path = "/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq"; bool got_sensors = false, cpu_temp_only = false; //* Populate found_sensors map @@ -144,7 +143,6 @@ namespace Shared { arg_max = sysconf(_SC_ARG_MAX); //? Init for namespace Cpu - if (not fs::exists(Cpu::freq_path) or access(Cpu::freq_path.c_str(), R_OK) == -1) Cpu::freq_path.clear(); Cpu::current_cpu.core_percent.insert(Cpu::current_cpu.core_percent.begin(), Shared::coreCount, {}); Cpu::current_cpu.temp.insert(Cpu::current_cpu.temp.begin(), Shared::coreCount + 1, {}); Cpu::core_old_totals.insert(Cpu::core_old_totals.begin(), Shared::coreCount, 0); @@ -230,12 +228,41 @@ namespace Cpu { bool get_sensors() { got_sensors = false; if (Config::getB("show_coretemp") and Config::getB("check_temp")) { + int32_t temp; + size_t size = sizeof(temp); + if (sysctlbyname("dev.cpu.0.temperature", &temp, &size, NULL, 0) < 0) { + Logger::warning("Could not get temp sensor - maybe you need to load the coretemp module"); + } else { + got_sensors = true; + } } return got_sensors; } void update_sensors() { current_cpu.temp_max = 95; // we have no idea how to get the critical temp + int temp; + size_t size = sizeof(temp); + sysctlbyname("hw.acpi.thermal.tz0.temperature", &temp, &size, NULL, 0); + temp = (temp - 2732) / 10; // since it's an int, it's multiplied by 10, and offset to absolute zero... + current_cpu.temp.at(0).push_back(temp); + if (current_cpu.temp.at(0).size() > 20) + current_cpu.temp.at(0).pop_front(); + + for (int i = 0; i < Shared::coreCount; i++) { + string s = "dev.cpu." + std::to_string(i) + ".temperature"; + if (sysctlbyname(s.c_str(), &temp, &size, NULL, 0) < 0) { + Logger::warning("Could not get temp sensor - maybe you need to load the coretemp module"); + } else { + temp = (temp - 2732) / 10; + if (cmp_less(i + 1, current_cpu.temp.size())) { + current_cpu.temp.at(i + 1).push_back(temp); + if (current_cpu.temp.at(i + 1).size() > 20) + current_cpu.temp.at(i + 1).pop_front(); + } + } + } + } string get_cpuHz() { diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index 3ee4d62..f5c43d7 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -261,6 +261,8 @@ namespace Cpu { auto sensor = sensors.getSensors(); if (sensor.size() > 0) { current_cpu.temp.at(0).push_back((long long)sensor[0]); + 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++) {