2018-08-09 01:54:33 +02:00
|
|
|
#include "modules/cpu.hpp"
|
|
|
|
|
2018-12-18 17:30:54 +01:00
|
|
|
waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config)
|
2019-04-18 17:52:00 +02:00
|
|
|
: ALabel(config, "{usage}%", 10) {
|
2018-08-16 14:29:41 +02:00
|
|
|
label_.set_name("cpu");
|
2018-12-18 17:30:54 +01:00
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
2018-11-23 11:57:37 +01:00
|
|
|
thread_ = [this] {
|
2018-08-20 14:50:45 +02:00
|
|
|
dp.emit();
|
2018-11-23 11:57:37 +01:00
|
|
|
thread_.sleep_for(interval_);
|
2018-08-09 01:54:33 +02:00
|
|
|
};
|
2018-08-18 11:43:48 +02:00
|
|
|
}
|
2018-08-09 01:54:33 +02:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
auto waybar::modules::Cpu::update() -> void {
|
2019-04-26 14:07:31 +02:00
|
|
|
std::vector<fmt::basic_format_arg<fmt::format_context>> args;
|
|
|
|
if (format_.find("{load}") != std::string::npos) {
|
|
|
|
auto cpu_load = getCpuLoad();
|
|
|
|
args.emplace_back(fmt::internal::make_arg<fmt::format_context>(fmt::arg("load", cpu_load)));
|
2019-02-22 11:35:26 +01:00
|
|
|
}
|
2019-04-26 14:07:31 +02:00
|
|
|
if (format_.find("{usage}") != std::string::npos) {
|
|
|
|
auto [cpu_usage, tooltip] = getCpuUsage();
|
|
|
|
args.emplace_back(fmt::internal::make_arg<fmt::format_context>(fmt::arg("usage", cpu_usage)));
|
|
|
|
if (tooltipEnabled()) {
|
|
|
|
label_.set_tooltip_text(tooltip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
label_.set_markup(
|
|
|
|
fmt::vformat(format_, fmt::basic_format_args<fmt::format_context>(args.data(), args.size())));
|
2018-11-15 14:44:43 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
uint16_t waybar::modules::Cpu::getCpuLoad() {
|
2018-11-15 14:44:43 +01:00
|
|
|
struct sysinfo info = {0};
|
|
|
|
if (sysinfo(&info) == 0) {
|
2019-04-24 12:37:24 +02:00
|
|
|
float f_load = 1.F / (1U << SI_LOAD_SHIFT);
|
2018-11-15 14:44:43 +01:00
|
|
|
uint16_t load = info.loads[0] * f_load * 100 / get_nprocs();
|
|
|
|
return load;
|
|
|
|
}
|
|
|
|
throw std::runtime_error("Can't get Cpu load");
|
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
std::tuple<uint16_t, std::string> waybar::modules::Cpu::getCpuUsage() {
|
2018-11-15 14:44:43 +01:00
|
|
|
if (prev_times_.empty()) {
|
|
|
|
prev_times_ = parseCpuinfo();
|
2018-12-26 11:13:36 +01:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2018-11-15 14:44:43 +01:00
|
|
|
std::vector<std::tuple<size_t, size_t>> curr_times = parseCpuinfo();
|
2019-04-18 17:52:00 +02:00
|
|
|
std::string tooltip;
|
|
|
|
uint16_t usage = 0;
|
2018-11-15 14:44:43 +01: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;
|
2019-04-18 17:52:00 +02:00
|
|
|
uint16_t tmp = 100 * (1 - delta_idle / delta_total);
|
2018-11-11 03:11:32 +01:00
|
|
|
if (i == 0) {
|
2018-11-15 14:44:43 +01:00
|
|
|
usage = tmp;
|
|
|
|
tooltip = fmt::format("Total: {}%", tmp);
|
2018-11-11 03:11:32 +01:00
|
|
|
} else {
|
2018-11-15 14:44:43 +01:00
|
|
|
tooltip = tooltip + fmt::format("\nCore{}: {}%", i - 1, tmp);
|
|
|
|
}
|
2018-11-11 03:11:32 +01:00
|
|
|
}
|
2018-11-15 14:44:43 +01:00
|
|
|
prev_times_ = curr_times;
|
|
|
|
return {usage, tooltip};
|
2018-11-11 03:11:32 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
std::vector<std::tuple<size_t, size_t>> waybar::modules::Cpu::parseCpuinfo() {
|
2018-11-11 03:11:32 +01:00
|
|
|
std::ifstream info(data_dir_);
|
|
|
|
if (!info.is_open()) {
|
|
|
|
throw std::runtime_error("Can't open " + data_dir_);
|
|
|
|
}
|
2019-04-18 17:52:00 +02:00
|
|
|
std::vector<std::tuple<size_t, size_t>> cpuinfo;
|
|
|
|
std::string line;
|
|
|
|
while (getline(info, line)) {
|
|
|
|
if (line.substr(0, 3).compare("cpu") != 0) {
|
2018-11-11 03:11:32 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-04-18 17:52:00 +02:00
|
|
|
std::stringstream sline(line.substr(5));
|
2018-11-11 03:11:32 +01:00
|
|
|
std::vector<size_t> times;
|
2019-04-24 12:37:24 +02:00
|
|
|
for (size_t time = 0; sline >> time; times.push_back(time))
|
2019-04-18 17:52:00 +02:00
|
|
|
;
|
2018-11-11 03:11:32 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-04-24 12:37:24 +02:00
|
|
|
cpuinfo.emplace_back(idle_time, total_time);
|
2018-11-11 03:11:32 +01:00
|
|
|
}
|
|
|
|
return cpuinfo;
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|