cpu module: Reuse getCpuFrequency of cpu_frequency module
This commit is contained in:
parent
dce6a98f38
commit
c45f6681b3
|
@ -23,9 +23,7 @@ class Cpu : public ALabel {
|
||||||
private:
|
private:
|
||||||
double getCpuLoad();
|
double getCpuLoad();
|
||||||
std::tuple<std::vector<uint16_t>, std::string> getCpuUsage();
|
std::tuple<std::vector<uint16_t>, std::string> getCpuUsage();
|
||||||
std::tuple<float, float, float> getCpuFrequency();
|
|
||||||
std::vector<std::tuple<size_t, size_t>> parseCpuinfo();
|
std::vector<std::tuple<size_t, size_t>> parseCpuinfo();
|
||||||
std::vector<float> parseCpuFrequencies();
|
|
||||||
|
|
||||||
std::vector<std::tuple<size_t, size_t>> prev_times_;
|
std::vector<std::tuple<size_t, size_t>> prev_times_;
|
||||||
|
|
||||||
|
|
|
@ -20,9 +20,11 @@ class CpuFrequency : public ALabel {
|
||||||
virtual ~CpuFrequency() = default;
|
virtual ~CpuFrequency() = default;
|
||||||
auto update() -> void override;
|
auto update() -> void override;
|
||||||
|
|
||||||
|
// This is a static member because it is also used by the cpu module.
|
||||||
|
static std::tuple<float, float, float> getCpuFrequency();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::tuple<float, float, float> getCpuFrequency();
|
static std::vector<float> parseCpuFrequencies();
|
||||||
std::vector<float> parseCpuFrequencies();
|
|
||||||
|
|
||||||
util::SleeperThread thread_;
|
util::SleeperThread thread_;
|
||||||
};
|
};
|
||||||
|
|
|
@ -100,13 +100,3 @@ std::vector<std::tuple<size_t, size_t>> waybar::modules::Cpu::parseCpuinfo() {
|
||||||
free(cp_time);
|
free(cp_time);
|
||||||
return cpuinfo;
|
return cpuinfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<float> waybar::modules::Cpu::parseCpuFrequencies() {
|
|
||||||
static std::vector<float> frequencies;
|
|
||||||
if (frequencies.empty()) {
|
|
||||||
spdlog::warn(
|
|
||||||
"cpu/bsd: parseCpuFrequencies is not implemented, expect garbage in {*_frequency}");
|
|
||||||
frequencies.push_back(NAN);
|
|
||||||
}
|
|
||||||
return frequencies;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "modules/cpu.hpp"
|
#include "modules/cpu.hpp"
|
||||||
|
#include "modules/cpu_frequency.hpp"
|
||||||
|
|
||||||
// In the 80000 version of fmt library authors decided to optimize imports
|
// In the 80000 version of fmt library authors decided to optimize imports
|
||||||
// and moved declarations required for fmt::dynamic_format_arg_store in new
|
// and moved declarations required for fmt::dynamic_format_arg_store in new
|
||||||
|
@ -21,7 +22,7 @@ auto waybar::modules::Cpu::update() -> void {
|
||||||
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both
|
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both
|
||||||
auto cpu_load = getCpuLoad();
|
auto cpu_load = getCpuLoad();
|
||||||
auto [cpu_usage, tooltip] = getCpuUsage();
|
auto [cpu_usage, tooltip] = getCpuUsage();
|
||||||
auto [max_frequency, min_frequency, avg_frequency] = getCpuFrequency();
|
auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency();
|
||||||
if (tooltipEnabled()) {
|
if (tooltipEnabled()) {
|
||||||
label_.set_tooltip_text(tooltip);
|
label_.set_tooltip_text(tooltip);
|
||||||
}
|
}
|
||||||
|
@ -90,20 +91,3 @@ std::tuple<std::vector<uint16_t>, std::string> waybar::modules::Cpu::getCpuUsage
|
||||||
prev_times_ = curr_times;
|
prev_times_ = curr_times;
|
||||||
return {usage, tooltip};
|
return {usage, tooltip};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<float, float, float> waybar::modules::Cpu::getCpuFrequency() {
|
|
||||||
std::vector<float> frequencies = parseCpuFrequencies();
|
|
||||||
if (frequencies.empty()) {
|
|
||||||
return {0.f, 0.f, 0.f};
|
|
||||||
}
|
|
||||||
auto [min, max] = std::minmax_element(std::begin(frequencies), std::end(frequencies));
|
|
||||||
float avg_frequency =
|
|
||||||
std::accumulate(std::begin(frequencies), std::end(frequencies), 0.0) / frequencies.size();
|
|
||||||
|
|
||||||
// Round frequencies with double decimal precision to get GHz
|
|
||||||
float max_frequency = std::ceil(*max / 10.0) / 100.0;
|
|
||||||
float min_frequency = std::ceil(*min / 10.0) / 100.0;
|
|
||||||
avg_frequency = std::ceil(avg_frequency / 10.0) / 100.0;
|
|
||||||
|
|
||||||
return {max_frequency, min_frequency, avg_frequency};
|
|
||||||
}
|
|
||||||
|
|
|
@ -29,47 +29,3 @@ std::vector<std::tuple<size_t, size_t>> waybar::modules::Cpu::parseCpuinfo() {
|
||||||
}
|
}
|
||||||
return cpuinfo;
|
return cpuinfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<float> waybar::modules::Cpu::parseCpuFrequencies() {
|
|
||||||
const std::string file_path_ = "/proc/cpuinfo";
|
|
||||||
std::ifstream info(file_path_);
|
|
||||||
if (!info.is_open()) {
|
|
||||||
throw std::runtime_error("Can't open " + file_path_);
|
|
||||||
}
|
|
||||||
std::vector<float> frequencies;
|
|
||||||
std::string line;
|
|
||||||
while (getline(info, line)) {
|
|
||||||
if (line.substr(0, 7).compare("cpu MHz") != 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string frequency_str = line.substr(line.find(":") + 2);
|
|
||||||
float frequency = std::strtol(frequency_str.c_str(), nullptr, 10);
|
|
||||||
frequencies.push_back(frequency);
|
|
||||||
}
|
|
||||||
info.close();
|
|
||||||
|
|
||||||
if (frequencies.size() <= 0) {
|
|
||||||
std::string cpufreq_dir = "/sys/devices/system/cpu/cpufreq";
|
|
||||||
if (std::filesystem::exists(cpufreq_dir)) {
|
|
||||||
std::vector<std::string> frequency_files = {"/cpuinfo_min_freq", "/cpuinfo_max_freq"};
|
|
||||||
for (auto& p : std::filesystem::directory_iterator(cpufreq_dir)) {
|
|
||||||
for (auto freq_file : frequency_files) {
|
|
||||||
std::string freq_file_path = p.path().string() + freq_file;
|
|
||||||
if (std::filesystem::exists(freq_file_path)) {
|
|
||||||
std::string freq_value;
|
|
||||||
std::ifstream freq(freq_file_path);
|
|
||||||
if (freq.is_open()) {
|
|
||||||
getline(freq, freq_value);
|
|
||||||
float frequency = std::strtol(freq_value.c_str(), nullptr, 10);
|
|
||||||
frequencies.push_back(frequency / 1000);
|
|
||||||
freq.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return frequencies;
|
|
||||||
}
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ waybar::modules::CpuFrequency::CpuFrequency(const std::string& id, const Json::V
|
||||||
|
|
||||||
auto waybar::modules::CpuFrequency::update() -> void {
|
auto waybar::modules::CpuFrequency::update() -> void {
|
||||||
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both
|
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both
|
||||||
auto [max_frequency, min_frequency, avg_frequency] = getCpuFrequency();
|
auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency();
|
||||||
if (tooltipEnabled()) {
|
if (tooltipEnabled()) {
|
||||||
auto tooltip =
|
auto tooltip =
|
||||||
fmt::format("Minimum frequency: {}\nAverage frequency: {}\nMaximum frequency: {}\n",
|
fmt::format("Minimum frequency: {}\nAverage frequency: {}\nMaximum frequency: {}\n",
|
||||||
|
@ -50,7 +50,7 @@ auto waybar::modules::CpuFrequency::update() -> void {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::tuple<float, float, float> waybar::modules::CpuFrequency::getCpuFrequency() {
|
std::tuple<float, float, float> waybar::modules::CpuFrequency::getCpuFrequency() {
|
||||||
std::vector<float> frequencies = parseCpuFrequencies();
|
std::vector<float> frequencies = CpuFrequency::parseCpuFrequencies();
|
||||||
if (frequencies.empty()) {
|
if (frequencies.empty()) {
|
||||||
return {0.f, 0.f, 0.f};
|
return {0.f, 0.f, 0.f};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue