2018-08-08 23:42:52 +00:00
|
|
|
#include "modules/memory.hpp"
|
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config)
|
2019-05-22 10:06:24 +00:00
|
|
|
: ALabel(config, "memory", id, "{}%", 30) {
|
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:42:52 +00:00
|
|
|
};
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
2018-08-08 23:42:52 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto waybar::modules::Memory::update() -> void {
|
2018-11-08 20:09:56 +00:00
|
|
|
parseMeminfo();
|
2020-03-20 21:37:22 +00:00
|
|
|
|
|
|
|
unsigned long memtotal = meminfo_["MemTotal"];
|
|
|
|
unsigned long memfree;
|
|
|
|
if (meminfo_.count("MemAvailable")) {
|
|
|
|
// New kernels (3.4+) have an accurate available memory field.
|
|
|
|
memfree = meminfo_["MemAvailable"];
|
|
|
|
} else {
|
|
|
|
// Old kernel; give a best-effort approximation of available memory.
|
|
|
|
memfree = meminfo_["MemFree"] + meminfo_["Buffers"] + meminfo_["Cached"] +
|
|
|
|
meminfo_["SReclaimable"] - meminfo_["Shmem"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memtotal > 0 && memfree >= 0) {
|
|
|
|
auto total_ram_gigabytes = memtotal / std::pow(1024, 2);
|
|
|
|
int used_ram_percentage = 100 * (memtotal - memfree) / memtotal;
|
|
|
|
auto used_ram_gigabytes = (memtotal - memfree) / std::pow(1024, 2);
|
|
|
|
auto available_ram_gigabytes = memfree / std::pow(1024, 2);
|
2019-05-19 15:10:01 +00:00
|
|
|
|
2020-10-12 00:05:26 +00:00
|
|
|
auto format = format_;
|
|
|
|
auto state = getState(used_ram_percentage);
|
|
|
|
if (!state.empty() && config_["format-" + state].isString()) {
|
|
|
|
format = config_["format-" + state].asString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format.empty()) {
|
|
|
|
event_box_.hide();
|
|
|
|
} else {
|
|
|
|
event_box_.show();
|
|
|
|
label_.set_markup(fmt::format(format,
|
|
|
|
used_ram_percentage,
|
|
|
|
fmt::arg("total", total_ram_gigabytes),
|
|
|
|
fmt::arg("percentage", used_ram_percentage),
|
|
|
|
fmt::arg("used", used_ram_gigabytes),
|
|
|
|
fmt::arg("avail", available_ram_gigabytes)));
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:35:26 +00:00
|
|
|
if (tooltipEnabled()) {
|
2020-09-02 12:25:57 +00:00
|
|
|
if (config_["tooltip-format"].isString()) {
|
|
|
|
auto tooltip_format = config_["tooltip-format"].asString();
|
|
|
|
label_.set_tooltip_text(fmt::format(tooltip_format,
|
|
|
|
used_ram_percentage,
|
|
|
|
fmt::arg("total", total_ram_gigabytes),
|
|
|
|
fmt::arg("percentage", used_ram_percentage),
|
|
|
|
fmt::arg("used", used_ram_gigabytes),
|
|
|
|
fmt::arg("avail", available_ram_gigabytes)));
|
|
|
|
} else {
|
|
|
|
label_.set_tooltip_text(fmt::format("{:.{}f}GiB used", used_ram_gigabytes, 1));
|
|
|
|
}
|
2019-02-22 10:35:26 +00:00
|
|
|
}
|
2018-11-09 15:24:13 +00:00
|
|
|
} else {
|
2018-12-18 16:30:54 +00:00
|
|
|
event_box_.hide();
|
2018-11-09 15:24:13 +00:00
|
|
|
}
|
2020-04-12 16:30:21 +00:00
|
|
|
// Call parent update
|
|
|
|
ALabel::update();
|
2018-11-08 20:09:56 +00:00
|
|
|
}
|