Waybar/src/modules/cpu.cpp

91 lines
2.6 KiB
C++
Raw Normal View History

2018-08-08 23:54:33 +00:00
#include "modules/cpu.hpp"
2018-08-20 12:50:45 +00:00
waybar::modules::Cpu::Cpu(const Json::Value& config)
2018-11-15 13:44:43 +00:00
: ALabel(config, "{usage}%")
2018-08-08 23:54:33 +00:00
{
2018-08-16 12:29:41 +00:00
label_.set_name("cpu");
2018-10-26 07:27:16 +00:00
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 10;
2018-08-16 12:29:41 +00:00
thread_ = [this, interval] {
2018-08-20 12:50:45 +00:00
dp.emit();
2018-08-16 12:29:41 +00:00
thread_.sleep_for(chrono::seconds(interval));
2018-08-08 23:54:33 +00:00
};
}
2018-08-08 23:54:33 +00:00
2018-08-09 10:05:48 +00:00
auto waybar::modules::Cpu::update() -> void
{
2018-11-15 13:44:43 +00:00
try {
2018-11-16 09:02:12 +00:00
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both
2018-11-15 13:44:43 +00:00
auto cpu_load = getCpuLoad();
auto [cpu_usage, tooltip] = getCpuUsage();
label_.set_tooltip_text(tooltip);
2018-11-21 19:49:09 +00:00
label_.set_markup(fmt::format(format_,
2018-11-15 13:44:43 +00:00
fmt::arg("load", cpu_load), fmt::arg("usage", cpu_usage)));
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
uint16_t waybar::modules::Cpu::getCpuLoad()
{
struct sysinfo info = {0};
if (sysinfo(&info) == 0) {
float f_load = 1.f / (1u << SI_LOAD_SHIFT);
uint16_t load = info.loads[0] * f_load * 100 / get_nprocs();
return load;
}
throw std::runtime_error("Can't get Cpu load");
}
std::tuple<uint16_t, std::string> waybar::modules::Cpu::getCpuUsage()
{
if (prev_times_.empty()) {
prev_times_ = parseCpuinfo();
std::this_thread::sleep_for(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();
std::string tooltip;
2018-11-15 13:44:43 +00:00
uint16_t usage = 0;
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;
uint16_t tmp = 100 * (1 - delta_idle / delta_total);
if (i == 0) {
2018-11-15 13:44:43 +00:00
usage = tmp;
tooltip = fmt::format("Total: {}%", tmp);
} else {
2018-11-15 13:44:43 +00:00
tooltip = tooltip + fmt::format("\nCore{}: {}%", i - 1, tmp);
}
}
2018-11-15 13:44:43 +00:00
prev_times_ = curr_times;
return {usage, tooltip};
}
2018-11-15 13:44:43 +00:00
std::vector<std::tuple<size_t, size_t>> waybar::modules::Cpu::parseCpuinfo()
{
std::ifstream info(data_dir_);
if (!info.is_open()) {
throw std::runtime_error("Can't open " + data_dir_);
}
std::vector< std::tuple<size_t, size_t> > cpuinfo;
std::string line;
while (getline(info, line)) {
if (line.substr(0,3).compare("cpu") != 0) {
break;
}
std::stringstream sline(line.substr(5));
std::vector<size_t> times;
for (size_t time; sline >> time; times.push_back(time));
size_t idle_time = 0;
size_t total_time = 0;
if (times.size() >= 4) {
idle_time = times[3];
total_time = std::accumulate(times.begin(), times.end(), 0);
}
2018-11-15 13:44:43 +00:00
cpuinfo.push_back({idle_time, total_time});
}
return cpuinfo;
2018-08-09 10:05:48 +00:00
}