Waybar/src/modules/cpu/common.cpp

110 lines
3.8 KiB
C++
Raw Normal View History

2018-08-08 23:54:33 +00:00
#include "modules/cpu.hpp"
2021-09-19 10:41:59 +00:00
// 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
2018-12-18 16:30:54 +00:00
waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config)
2022-11-24 11:28:52 +00:00
: ALabel(config, "cpu", id, "{usage}%", 10) {
2018-11-23 10:57:37 +00:00
thread_ = [this] {
2018-08-20 12:50:45 +00:00
dp.emit();
2018-11-23 10:57:37 +00:00
thread_.sleep_for(interval_);
2018-08-08 23:54:33 +00:00
};
}
2018-08-08 23:54:33 +00:00
2019-04-18 15:52:00 +00:00
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();
2021-02-02 22:33:33 +00:00
auto [max_frequency, min_frequency, avg_frequency] = getCpuFrequency();
if (tooltipEnabled()) {
2022-11-24 11:28:52 +00:00
label_.set_tooltip_text(tooltip);
2019-02-22 10:35:26 +00:00
}
auto format = format_;
2021-09-19 10:41:59 +00:00
auto total_usage = cpu_usage.empty() ? 0 : cpu_usage[0];
auto state = getState(total_usage);
if (!state.empty() && config_["format-" + state].isString()) {
format = config_["format-" + state].asString();
}
if (format.empty()) {
event_box_.hide();
} else {
event_box_.show();
2021-08-23 05:30:07 +00:00
auto icons = std::vector<std::string>{state};
2021-09-19 10:41:59 +00:00
fmt::dynamic_format_arg_store<fmt::format_context> store;
store.push_back(fmt::arg("load", cpu_load));
store.push_back(fmt::arg("usage", total_usage));
store.push_back(fmt::arg("icon", getIcon(total_usage, 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));
for (size_t i = 1; i < cpu_usage.size(); ++i) {
2022-04-06 06:37:19 +00:00
auto core_i = i - 1;
auto core_format = fmt::format("usage{}", core_i);
store.push_back(fmt::arg(core_format.c_str(), cpu_usage[i]));
auto icon_format = fmt::format("icon{}", core_i);
store.push_back(fmt::arg(icon_format.c_str(), getIcon(cpu_usage[i], icons)));
2021-09-19 10:41:59 +00:00
}
2022-11-24 11:28:52 +00:00
label_.set_markup(fmt::vformat(format, store));
}
2020-04-12 16:30:21 +00:00
// Call parent update
2022-11-24 11:28:52 +00:00
ALabel::update();
2018-11-15 13:44:43 +00:00
}
2021-03-12 19:58:51 +00:00
double waybar::modules::Cpu::getCpuLoad() {
double load[1];
if (getloadavg(load, 1) != -1) {
2022-02-22 23:33:33 +00:00
return std::ceil(load[0] * 100.0) / 100.0;
2018-11-15 13:44:43 +00:00
}
throw std::runtime_error("Can't get Cpu load");
}
2021-09-19 10:41:59 +00:00
std::tuple<std::vector<uint16_t>, std::string> waybar::modules::Cpu::getCpuUsage() {
2018-11-15 13:44:43 +00:00
if (prev_times_.empty()) {
prev_times_ = parseCpuinfo();
2018-12-26 10:13:36 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(100));
2018-08-09 10:05:48 +00:00
}
2018-11-15 13:44:43 +00:00
std::vector<std::tuple<size_t, size_t>> curr_times = parseCpuinfo();
2022-04-06 06:37:19 +00:00
std::string tooltip;
std::vector<uint16_t> usage;
2018-11-15 13:44:43 +00:00
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;
2022-04-06 06:37:19 +00:00
uint16_t tmp = 100 * (1 - delta_idle / delta_total);
if (i == 0) {
2018-11-15 13:44:43 +00:00
tooltip = fmt::format("Total: {}%", tmp);
} else {
2018-11-15 13:44:43 +00:00
tooltip = tooltip + fmt::format("\nCore{}: {}%", i - 1, tmp);
}
2021-09-19 10:41:59 +00:00
usage.push_back(tmp);
}
2018-11-15 13:44:43 +00:00
prev_times_ = curr_times;
return {usage, tooltip};
}
2021-02-02 22:33:33 +00:00
std::tuple<float, float, float> waybar::modules::Cpu::getCpuFrequency() {
std::vector<float> frequencies = parseCpuFrequencies();
2022-05-23 11:47:14 +00:00
if (frequencies.empty()) {
return {0.f, 0.f, 0.f};
}
2021-02-02 22:33:33 +00:00
auto [min, max] = std::minmax_element(std::begin(frequencies), std::end(frequencies));
2022-04-06 06:37:19 +00:00
float avg_frequency =
std::accumulate(std::begin(frequencies), std::end(frequencies), 0.0) / frequencies.size();
2021-02-02 22:33:33 +00:00
// 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;
2022-04-06 06:37:19 +00:00
return {max_frequency, min_frequency, avg_frequency};
2021-02-02 22:33:33 +00:00
}