Introduce cpu_frequency module

This commit is contained in:
Mann mit Hut 2022-12-21 17:45:53 +01:00
parent 729564cc27
commit c36fe3a004
No known key found for this signature in database
GPG Key ID: 02D4D60B8250CB8C
6 changed files with 150 additions and 0 deletions

View File

@ -38,6 +38,7 @@
#endif #endif
#if defined(HAVE_CPU_LINUX) || defined(HAVE_CPU_BSD) #if defined(HAVE_CPU_LINUX) || defined(HAVE_CPU_BSD)
#include "modules/cpu.hpp" #include "modules/cpu.hpp"
#include "modules/cpu_frequency.hpp"
#include "modules/load.hpp" #include "modules/load.hpp"
#endif #endif
#include "modules/idle_inhibitor.hpp" #include "modules/idle_inhibitor.hpp"

View File

@ -0,0 +1,30 @@
#pragma once
#include <fmt/format.h>
#include <cstdint>
#include <fstream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
#include "AButton.hpp"
#include "util/sleeper_thread.hpp"
namespace waybar::modules {
class CpuFrequency : public AButton {
public:
CpuFrequency(const std::string&, const Json::Value&);
~CpuFrequency() = default;
auto update() -> void;
private:
std::tuple<float, float, float> getCpuFrequency();
std::vector<float> parseCpuFrequencies();
util::SleeperThread thread_;
};
} // namespace waybar::modules

View File

@ -190,6 +190,8 @@ if is_linux
'src/modules/battery.cpp', 'src/modules/battery.cpp',
'src/modules/cpu/common.cpp', 'src/modules/cpu/common.cpp',
'src/modules/cpu/linux.cpp', 'src/modules/cpu/linux.cpp',
'src/modules/cpu_frequency/common.cpp',
'src/modules/cpu_frequency/linux.cpp',
'src/modules/memory/common.cpp', 'src/modules/memory/common.cpp',
'src/modules/memory/linux.cpp', 'src/modules/memory/linux.cpp',
) )

View File

@ -99,6 +99,9 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name) const {
if (ref == "cpu") { if (ref == "cpu") {
return new waybar::modules::Cpu(id, config_[name]); return new waybar::modules::Cpu(id, config_[name]);
} }
if (ref == "cpu_frequency") {
return new waybar::modules::CpuFrequency(id, config_[name]);
}
if (ref == "load") { if (ref == "load") {
return new waybar::modules::Load(id, config_[name]); return new waybar::modules::Load(id, config_[name]);
} }

View File

@ -0,0 +1,67 @@
#include "modules/cpu_frequency.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
// header fmt/args.h
#if (FMT_VERSION >= 80000)
#include <fmt/args.h>
#else
#include <fmt/core.h>
#endif
waybar::modules::CpuFrequency::CpuFrequency(const std::string& id, const Json::Value& config)
: AButton(config, "cpu_frequency", id, "{avg_frequency}", 10) {
thread_ = [this] {
dp.emit();
thread_.sleep_for(interval_);
};
}
auto waybar::modules::CpuFrequency::update() -> void {
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both
auto [max_frequency, min_frequency, avg_frequency] = getCpuFrequency();
if (tooltipEnabled()) {
auto tooltip =
fmt::format("Minimum frequency: {}\nAverage frequency: {}\nMaximum frequency: {}\n",
min_frequency, avg_frequency, max_frequency);
button_.set_tooltip_text(tooltip);
}
auto format = format_;
auto state = getState(avg_frequency);
if (!state.empty() && config_["format-" + state].isString()) {
format = config_["format-" + state].asString();
}
if (format.empty()) {
event_box_.hide();
} else {
event_box_.show();
auto icons = std::vector<std::string>{state};
fmt::dynamic_format_arg_store<fmt::format_context> store;
store.push_back(fmt::arg("icon", getIcon(avg_frequency, icons)));
store.push_back(fmt::arg("max_frequency", max_frequency));
store.push_back(fmt::arg("min_frequency", min_frequency));
store.push_back(fmt::arg("avg_frequency", avg_frequency));
label_->set_markup(fmt::vformat(format, store));
}
// Call parent update
AButton::update();
}
std::tuple<float, float, float> waybar::modules::CpuFrequency::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};
}

View File

@ -0,0 +1,47 @@
#include <filesystem>
#include "modules/cpu_frequency.hpp"
std::vector<float> waybar::modules::CpuFrequency::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;
}