From d1602e383c88d1f546b36704037133648b0e5ac4 Mon Sep 17 00:00:00 2001 From: Mann mit Hut Date: Sat, 29 Jul 2023 15:56:51 +0200 Subject: [PATCH] cpu module: Reuse getCpuUsage of cpu_usage module --- include/modules/cpu.hpp | 2 - include/modules/cpu_usage.hpp | 6 +- meson.build | 2 - src/modules/cpu/bsd.cpp | 102 ------------------------------- src/modules/cpu/common.cpp | 28 +-------- src/modules/cpu/linux.cpp | 31 ---------- src/modules/cpu_usage/common.cpp | 14 ++--- 7 files changed, 13 insertions(+), 172 deletions(-) delete mode 100644 src/modules/cpu/bsd.cpp delete mode 100644 src/modules/cpu/linux.cpp diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index 57331163..aff4c508 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -22,8 +22,6 @@ class Cpu : public ALabel { private: double getCpuLoad(); - std::tuple, std::string> getCpuUsage(); - std::vector> parseCpuinfo(); std::vector> prev_times_; diff --git a/include/modules/cpu_usage.hpp b/include/modules/cpu_usage.hpp index 48f9194c..4349f705 100644 --- a/include/modules/cpu_usage.hpp +++ b/include/modules/cpu_usage.hpp @@ -20,9 +20,11 @@ class CpuUsage : public ALabel { virtual ~CpuUsage() = default; auto update() -> void override; + // This is a static member because it is also used by the cpu module. + static std::tuple, std::string> getCpuUsage(std::vector>&); + private: - std::tuple, std::string> getCpuUsage(); - std::vector> parseCpuinfo(); + static std::vector> parseCpuinfo(); std::vector> prev_times_; diff --git a/meson.build b/meson.build index 2a0922cb..3bb79d43 100644 --- a/meson.build +++ b/meson.build @@ -189,7 +189,6 @@ if is_linux src_files += files( 'src/modules/battery.cpp', 'src/modules/cpu/common.cpp', - 'src/modules/cpu/linux.cpp', 'src/modules/cpu_frequency/common.cpp', 'src/modules/cpu_frequency/linux.cpp', 'src/modules/cpu_usage/common.cpp', @@ -201,7 +200,6 @@ elif is_dragonfly or is_freebsd or is_netbsd or is_openbsd add_project_arguments('-DHAVE_CPU_BSD', language: 'cpp') add_project_arguments('-DHAVE_MEMORY_BSD', language: 'cpp') src_files += files( - 'src/modules/cpu/bsd.cpp', 'src/modules/cpu/common.cpp', 'src/modules/cpu_usage/bsd.cpp', 'src/modules/cpu_usage/common.cpp', diff --git a/src/modules/cpu/bsd.cpp b/src/modules/cpu/bsd.cpp deleted file mode 100644 index 96f2e51c..00000000 --- a/src/modules/cpu/bsd.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include -// clang-format off -#include -#include -// clang-format on -#include // sysconf - -#include // NAN -#include // malloc - -#include "modules/cpu.hpp" - -#if defined(__NetBSD__) || defined(__OpenBSD__) -#include -#else -#include -#endif - -#if defined(__NetBSD__) -typedef uint64_t cp_time_t; -#else -typedef long cp_time_t; -#endif -#if defined(__NetBSD__) || defined(__OpenBSD__) -typedef uint64_t pcp_time_t; -#else -typedef long pcp_time_t; -#endif - -std::vector> waybar::modules::Cpu::parseCpuinfo() { - cp_time_t sum_cp_time[CPUSTATES]; - size_t sum_sz = sizeof(sum_cp_time); - int ncpu = sysconf(_SC_NPROCESSORS_CONF); - size_t sz = CPUSTATES * (ncpu + 1) * sizeof(pcp_time_t); - pcp_time_t *cp_time = static_cast(malloc(sz)), *pcp_time = cp_time; -#if defined(__NetBSD__) - int mib[] = { - CTL_KERN, - KERN_CP_TIME, - }; - if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), sum_cp_time, &sum_sz, NULL, 0)) { - throw std::runtime_error("sysctl kern.cp_time failed"); - } - for (int state = 0; state < CPUSTATES; state++) { - cp_time[state] = sum_cp_time[state]; - } - pcp_time += CPUSTATES; - if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), pcp_time, &sz, NULL, 0)) { - throw std::runtime_error("sysctl kern.cp_time failed"); - } -#elif defined(__OpenBSD__) - { - int mib[] = { - CTL_KERN, - KERN_CPTIME, - }; - if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), sum_cp_time, &sum_sz, NULL, 0)) { - throw std::runtime_error("sysctl kern.cp_time failed"); - } - } - for (int state = 0; state < CPUSTATES; state++) { - cp_time[state] = sum_cp_time[state]; - } - pcp_time = cp_time; - sz /= ncpu + 1; - { - int mib[] = { - CTL_KERN, - KERN_CPTIME2, - 0, - }; - for (int cpu = 0; cpu < ncpu; cpu++) { - mib[2] = cpu; - pcp_time += CPUSTATES; - if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), pcp_time, &sz, NULL, 0)) { - throw std::runtime_error("sysctl kern.cp_time2 failed"); - } - } - } -#else - if (sysctlbyname("kern.cp_time", sum_cp_time, &sum_sz, NULL, 0)) { - throw std::runtime_error("sysctl kern.cp_time failed"); - } - for (int state = 0; state < CPUSTATES; state++) { - cp_time[state] = sum_cp_time[state]; - } - pcp_time += CPUSTATES; - if (sysctlbyname("kern.cp_times", pcp_time, &sz, NULL, 0)) { - throw std::runtime_error("sysctl kern.cp_times failed"); - } -#endif - std::vector> cpuinfo; - for (int cpu = 0; cpu < ncpu + 1; cpu++) { - pcp_time_t total = 0, *single_cp_time = &cp_time[cpu * CPUSTATES]; - for (int state = 0; state < CPUSTATES; state++) { - total += single_cp_time[state]; - } - cpuinfo.emplace_back(single_cp_time[CP_IDLE], total); - } - free(cp_time); - return cpuinfo; -} diff --git a/src/modules/cpu/common.cpp b/src/modules/cpu/common.cpp index a27b7f52..5259dcb9 100644 --- a/src/modules/cpu/common.cpp +++ b/src/modules/cpu/common.cpp @@ -1,5 +1,6 @@ #include "modules/cpu.hpp" #include "modules/cpu_frequency.hpp" +#include "modules/cpu_usage.hpp" // In the 80000 version of fmt library authors decided to optimize imports // and moved declarations required for fmt::dynamic_format_arg_store in new @@ -21,7 +22,7 @@ waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config) auto waybar::modules::Cpu::update() -> void { // TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both auto cpu_load = getCpuLoad(); - auto [cpu_usage, tooltip] = getCpuUsage(); + auto [cpu_usage, tooltip] = CpuUsage::getCpuUsage(prev_times_); auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency(); if (tooltipEnabled()) { label_.set_tooltip_text(tooltip); @@ -66,28 +67,3 @@ double waybar::modules::Cpu::getCpuLoad() { } throw std::runtime_error("Can't get Cpu load"); } - -std::tuple, std::string> waybar::modules::Cpu::getCpuUsage() { - if (prev_times_.empty()) { - prev_times_ = parseCpuinfo(); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } - std::vector> curr_times = parseCpuinfo(); - std::string tooltip; - std::vector usage; - for (size_t i = 0; i < curr_times.size(); ++i) { - auto [curr_idle, curr_total] = curr_times[i]; - auto [prev_idle, prev_total] = prev_times_[i]; - const float delta_idle = curr_idle - prev_idle; - const float delta_total = curr_total - prev_total; - uint16_t tmp = 100 * (1 - delta_idle / delta_total); - if (i == 0) { - tooltip = fmt::format("Total: {}%", tmp); - } else { - tooltip = tooltip + fmt::format("\nCore{}: {}%", i - 1, tmp); - } - usage.push_back(tmp); - } - prev_times_ = curr_times; - return {usage, tooltip}; -} diff --git a/src/modules/cpu/linux.cpp b/src/modules/cpu/linux.cpp deleted file mode 100644 index 88d71ee7..00000000 --- a/src/modules/cpu/linux.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include - -#include "modules/cpu.hpp" - -std::vector> waybar::modules::Cpu::parseCpuinfo() { - const std::string data_dir_ = "/proc/stat"; - std::ifstream info(data_dir_); - if (!info.is_open()) { - throw std::runtime_error("Can't open " + data_dir_); - } - std::vector> cpuinfo; - std::string line; - while (getline(info, line)) { - if (line.substr(0, 3).compare("cpu") != 0) { - break; - } - std::stringstream sline(line.substr(5)); - std::vector times; - for (size_t time = 0; sline >> time; times.push_back(time)) - ; - - size_t idle_time = 0; - size_t total_time = 0; - if (times.size() >= 4) { - idle_time = times[3]; - total_time = std::accumulate(times.begin(), times.end(), 0); - } - cpuinfo.emplace_back(idle_time, total_time); - } - return cpuinfo; -} diff --git a/src/modules/cpu_usage/common.cpp b/src/modules/cpu_usage/common.cpp index bd56cf1a..ffa54ae0 100644 --- a/src/modules/cpu_usage/common.cpp +++ b/src/modules/cpu_usage/common.cpp @@ -19,7 +19,7 @@ waybar::modules::CpuUsage::CpuUsage(const std::string& id, const Json::Value& co auto waybar::modules::CpuUsage::update() -> void { // TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both - auto [cpu_usage, tooltip] = getCpuUsage(); + auto [cpu_usage, tooltip] = CpuUsage::getCpuUsage(prev_times_); if (tooltipEnabled()) { label_.set_tooltip_text(tooltip); } @@ -52,17 +52,17 @@ auto waybar::modules::CpuUsage::update() -> void { ALabel::update(); } -std::tuple, std::string> waybar::modules::CpuUsage::getCpuUsage() { - if (prev_times_.empty()) { - prev_times_ = parseCpuinfo(); +std::tuple, std::string> waybar::modules::CpuUsage::getCpuUsage(std::vector>& prev_times) { + if (prev_times.empty()) { + prev_times = CpuUsage::parseCpuinfo(); std::this_thread::sleep_for(std::chrono::milliseconds(100)); } - std::vector> curr_times = parseCpuinfo(); + std::vector> curr_times = CpuUsage::parseCpuinfo(); std::string tooltip; std::vector usage; for (size_t i = 0; i < curr_times.size(); ++i) { auto [curr_idle, curr_total] = curr_times[i]; - auto [prev_idle, prev_total] = prev_times_[i]; + auto [prev_idle, prev_total] = prev_times[i]; const float delta_idle = curr_idle - prev_idle; const float delta_total = curr_total - prev_total; uint16_t tmp = 100 * (1 - delta_idle / delta_total); @@ -73,6 +73,6 @@ std::tuple, std::string> waybar::modules::CpuUsage::getCpu } usage.push_back(tmp); } - prev_times_ = curr_times; + prev_times = curr_times; return {usage, tooltip}; }