Add cpu frequency
This commit is contained in:
parent
cb1c7ea12c
commit
08ea5ebe1f
|
@ -22,7 +22,9 @@ class Cpu : public ALabel {
|
|||
private:
|
||||
uint16_t getCpuLoad();
|
||||
std::tuple<uint16_t, std::string> getCpuUsage();
|
||||
std::tuple<float, float, float> getCpuFrequency();
|
||||
std::vector<std::tuple<size_t, size_t>> parseCpuinfo();
|
||||
std::vector<float> parseCpuFrequencies();
|
||||
|
||||
std::vector<std::tuple<size_t, size_t>> prev_times_;
|
||||
|
||||
|
|
|
@ -79,6 +79,12 @@ The *cpu* module displays the current cpu utilization.
|
|||
|
||||
*{usage}*: Current cpu usage.
|
||||
|
||||
*{avg_frequency}*: Current cpu average frequency (based on all cores) in GHz.
|
||||
|
||||
*{max_frequency}*: Current cpu max frequency (based on the core with the highest frequency) in GHz.
|
||||
|
||||
*{min_frequency}*: Current cpu min frequency (based on the core with the lowest frequency) in GHz.
|
||||
|
||||
# EXAMPLE
|
||||
|
||||
```
|
||||
|
|
|
@ -95,3 +95,7 @@ std::vector<std::tuple<size_t, size_t>> waybar::modules::Cpu::parseCpuinfo() {
|
|||
free(cp_time);
|
||||
return cpuinfo;
|
||||
}
|
||||
|
||||
std::vector<float> waybar::modules::Cpu::parseCpuFrequencies() {
|
||||
throw std::runtime_error("Cpu frequency is not implemented on BSD.");
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ 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 [max_frequency, min_frequency, avg_frequency] = getCpuFrequency();
|
||||
if (tooltipEnabled()) {
|
||||
label_.set_tooltip_text(tooltip);
|
||||
}
|
||||
|
@ -25,7 +26,12 @@ auto waybar::modules::Cpu::update() -> void {
|
|||
event_box_.hide();
|
||||
} else {
|
||||
event_box_.show();
|
||||
label_.set_markup(fmt::format(format, fmt::arg("load", cpu_load), fmt::arg("usage", cpu_usage)));
|
||||
label_.set_markup(fmt::format(format,
|
||||
fmt::arg("load", cpu_load),
|
||||
fmt::arg("usage", cpu_usage),
|
||||
fmt::arg("max_frequency", max_frequency),
|
||||
fmt::arg("min_frequency", min_frequency),
|
||||
fmt::arg("avg_frequency", avg_frequency)));
|
||||
}
|
||||
|
||||
// Call parent update
|
||||
|
@ -64,3 +70,16 @@ std::tuple<uint16_t, std::string> waybar::modules::Cpu::getCpuUsage() {
|
|||
prev_times_ = curr_times;
|
||||
return {usage, tooltip};
|
||||
}
|
||||
|
||||
std::tuple<float, float, float> waybar::modules::Cpu::getCpuFrequency() {
|
||||
std::vector<float> frequencies = parseCpuFrequencies();
|
||||
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 };
|
||||
}
|
||||
|
|
|
@ -27,3 +27,24 @@ std::vector<std::tuple<size_t, size_t>> waybar::modules::Cpu::parseCpuinfo() {
|
|||
}
|
||||
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();
|
||||
return frequencies;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue