Skip to content

Commit

Permalink
Merge pull request #1037 from Moonlight-Angel/cpu-frequency
Browse files Browse the repository at this point in the history
Add cpu min/max/avg frequencies
  • Loading branch information
Alexays authored Mar 25, 2021
2 parents c21dc68 + 99643ba commit 600afaf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/modules/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class Cpu : public ALabel {
private:
double 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_;

Expand Down
6 changes: 6 additions & 0 deletions man/waybar-cpu.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
11 changes: 11 additions & 0 deletions src/modules/cpu/bsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include <sys/types.h>
#include <sys/sysctl.h>
#include <spdlog/spdlog.h>
#include <cstdlib> // malloc
#include <unistd.h> // sysconf
#include <cmath> // NAN

#if defined(__NetBSD__) || defined(__OpenBSD__)
# include <sys/sched.h>
Expand Down Expand Up @@ -95,3 +97,12 @@ std::vector<std::tuple<size_t, size_t>> waybar::modules::Cpu::parseCpuinfo() {
free(cp_time);
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;
}
21 changes: 20 additions & 1 deletion src/modules/cpu/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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 };
}
21 changes: 21 additions & 0 deletions src/modules/cpu/linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 600afaf

Please sign in to comment.