feat(cpu): add both usage and load

This commit is contained in:
Alexis 2018-11-15 14:44:43 +01:00
parent 1665003d23
commit 94b9f0a399
4 changed files with 61 additions and 27 deletions

View File

@ -1,9 +1,11 @@
#pragma once #pragma once
#include <fmt/format.h> #include <fmt/format.h>
#include <sys/sysinfo.h>
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <numeric> #include <numeric>
#include <iostream>
#include "util/chrono.hpp" #include "util/chrono.hpp"
#include "ALabel.hpp" #include "ALabel.hpp"
@ -15,8 +17,11 @@ class Cpu : public ALabel {
auto update() -> void; auto update() -> void;
private: private:
static inline const std::string data_dir_ = "/proc/stat"; static inline const std::string data_dir_ = "/proc/stat";
uint16_t getCpuLoad();
std::tuple<uint16_t, std::string> getCpuUsage();
std::vector<std::tuple<size_t, size_t>> parseCpuinfo(); std::vector<std::tuple<size_t, size_t>> parseCpuinfo();
std::vector< std::tuple<size_t, size_t> > prevTimes_;
std::vector<std::tuple<size_t, size_t>> prev_times_;
waybar::util::SleeperThread thread_; waybar::util::SleeperThread thread_;
}; };

View File

@ -37,7 +37,7 @@
"format-alt": "{:%Y-%m-%d}" "format-alt": "{:%Y-%m-%d}"
}, },
"cpu": { "cpu": {
"format": "{}% " "format": "{usage}% "
}, },
"memory": { "memory": {
"format": "{}% " "format": "{}% "

View File

@ -1,4 +1,5 @@
#include "client.hpp" #include "client.hpp"
#include <iostream>
waybar::Client::Client(int argc, char* argv[]) waybar::Client::Client(int argc, char* argv[])
: gtk_app(Gtk::Application::create(argc, argv, "fr.arouillard.waybar")), : gtk_app(Gtk::Application::create(argc, argv, "fr.arouillard.waybar")),
@ -40,7 +41,7 @@ waybar::Client::Client(int argc, char* argv[])
"/etc/xdg/waybar/style.css", "/etc/xdg/waybar/style.css",
"./resources/style.css", "./resources/style.css",
}); });
std::cout << "Resources files: " + config_file + ", " + css_file << std::endl;
} }
void waybar::Client::handleGlobal(void *data, struct wl_registry *registry, void waybar::Client::handleGlobal(void *data, struct wl_registry *registry,

View File

@ -1,7 +1,9 @@
#include "modules/cpu.hpp" #include "modules/cpu.hpp"
using ctx = fmt::format_context;
waybar::modules::Cpu::Cpu(const Json::Value& config) waybar::modules::Cpu::Cpu(const Json::Value& config)
: ALabel(config, "{}%") : ALabel(config, "{usage}%")
{ {
label_.set_name("cpu"); label_.set_name("cpu");
uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 10; uint32_t interval = config_["interval"].isUInt() ? config_["interval"].asUInt() : 10;
@ -13,27 +15,53 @@ waybar::modules::Cpu::Cpu(const Json::Value& config)
auto waybar::modules::Cpu::update() -> void auto waybar::modules::Cpu::update() -> void
{ {
if (prevTimes_.size() < 1) { try {
prevTimes_ = parseCpuinfo(); // TODO: as creating dynamic fmt::arg arrays is buggy we have to do this
auto cpu_load = getCpuLoad();
auto [cpu_usage, tooltip] = getCpuUsage();
label_.set_tooltip_text(tooltip);
label_.set_text(fmt::format(format_,
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)); std::this_thread::sleep_for(chrono::milliseconds(100));
} }
std::vector< std::tuple<size_t, size_t> > currTimes = parseCpuinfo(); std::vector<std::tuple<size_t, size_t>> curr_times = parseCpuinfo();
std::string tooltip; std::string tooltip;
for (size_t i = 0; i < currTimes.size(); ++i) { uint16_t usage = 0;
auto [currIdle, currTotal] = currTimes[i]; for (size_t i = 0; i < curr_times.size(); ++i) {
auto [prevIdle, prevTotal] = prevTimes_[i]; auto [curr_idle, curr_total] = curr_times[i];
const float deltaIdle = currIdle - prevIdle; auto [prev_idle, prev_total] = prev_times_[i];
const float deltaTotal = currTotal - prevTotal; const float delta_idle = curr_idle - prev_idle;
uint16_t load = 100 * (1 - deltaIdle / deltaTotal); const float delta_total = curr_total - prev_total;
uint16_t tmp = 100 * (1 - delta_idle / delta_total);
if (i == 0) { if (i == 0) {
label_.set_text(fmt::format(format_, load)); usage = tmp;
tooltip = fmt::format("Total: {}%", load); tooltip = fmt::format("Total: {}%", tmp);
} else { } else {
tooltip = tooltip + fmt::format("\nCore{}: {}%", i - 1, load); tooltip = tooltip + fmt::format("\nCore{}: {}%", i - 1, tmp);
} }
} }
label_.set_tooltip_text(tooltip); prev_times_ = curr_times;
prevTimes_ = currTimes; return {usage, tooltip};
} }
std::vector<std::tuple<size_t, size_t>> waybar::modules::Cpu::parseCpuinfo() std::vector<std::tuple<size_t, size_t>> waybar::modules::Cpu::parseCpuinfo()