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)
|
2021-05-27 13:40:54 +00:00
|
|
|
: AButton(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"];
|
2021-08-29 13:34:29 +00:00
|
|
|
unsigned long swaptotal = 0;
|
|
|
|
if (meminfo_.count("SwapTotal")) {
|
|
|
|
swaptotal = meminfo_["SwapTotal"];
|
|
|
|
}
|
2020-03-20 21:37:22 +00:00
|
|
|
unsigned long memfree;
|
2021-08-29 13:34:29 +00:00
|
|
|
unsigned long swapfree = 0;
|
|
|
|
if (meminfo_.count("SwapFree")) {
|
|
|
|
swapfree = meminfo_["SwapFree"];
|
|
|
|
}
|
2020-03-20 21:37:22 +00:00
|
|
|
if (meminfo_.count("MemAvailable")) {
|
|
|
|
// New kernels (3.4+) have an accurate available memory field.
|
2021-10-28 17:10:46 +00:00
|
|
|
memfree = meminfo_["MemAvailable"] + meminfo_["zfs_size"];
|
2020-03-20 21:37:22 +00:00
|
|
|
} else {
|
|
|
|
// Old kernel; give a best-effort approximation of available memory.
|
|
|
|
memfree = meminfo_["MemFree"] + meminfo_["Buffers"] + meminfo_["Cached"] +
|
2021-10-28 17:10:46 +00:00
|
|
|
meminfo_["SReclaimable"] - meminfo_["Shmem"] + meminfo_["zfs_size"];
|
2020-03-20 21:37:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (memtotal > 0 && memfree >= 0) {
|
|
|
|
auto total_ram_gigabytes = memtotal / std::pow(1024, 2);
|
2021-08-29 13:34:29 +00:00
|
|
|
auto total_swap_gigabytes = swaptotal / std::pow(1024, 2);
|
2022-04-06 06:37:19 +00:00
|
|
|
int used_ram_percentage = 100 * (memtotal - memfree) / memtotal;
|
|
|
|
int used_swap_percentage = 0;
|
2021-08-29 13:34:29 +00:00
|
|
|
if (swaptotal && swapfree) {
|
|
|
|
used_swap_percentage = 100 * (swaptotal - swapfree) / swaptotal;
|
|
|
|
}
|
2020-03-20 21:37:22 +00:00
|
|
|
auto used_ram_gigabytes = (memtotal - memfree) / std::pow(1024, 2);
|
2021-08-29 13:34:29 +00:00
|
|
|
auto used_swap_gigabytes = (swaptotal - swapfree) / std::pow(1024, 2);
|
2020-03-20 21:37:22 +00:00
|
|
|
auto available_ram_gigabytes = memfree / std::pow(1024, 2);
|
2021-08-29 13:34:29 +00:00
|
|
|
auto available_swap_gigabytes = swapfree / 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();
|
2021-08-23 05:30:07 +00:00
|
|
|
auto icons = std::vector<std::string>{state};
|
2021-05-27 13:40:54 +00:00
|
|
|
label_->set_markup(fmt::format(
|
2022-04-06 06:37:19 +00:00
|
|
|
format, used_ram_percentage, fmt::arg("icon", getIcon(used_ram_percentage, icons)),
|
|
|
|
fmt::arg("total", total_ram_gigabytes), fmt::arg("swapTotal", total_swap_gigabytes),
|
|
|
|
fmt::arg("percentage", used_ram_percentage),
|
|
|
|
fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram_gigabytes),
|
|
|
|
fmt::arg("swapUsed", used_swap_gigabytes), fmt::arg("avail", available_ram_gigabytes),
|
|
|
|
fmt::arg("swapAvail", available_swap_gigabytes)));
|
2020-10-12 00:05:26 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2021-06-23 08:43:28 +00:00
|
|
|
button_.set_tooltip_text(fmt::format(
|
2022-04-06 06:37:19 +00:00
|
|
|
tooltip_format, used_ram_percentage, fmt::arg("total", total_ram_gigabytes),
|
|
|
|
fmt::arg("swapTotal", total_swap_gigabytes),
|
|
|
|
fmt::arg("percentage", used_ram_percentage),
|
|
|
|
fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram_gigabytes),
|
|
|
|
fmt::arg("swapUsed", used_swap_gigabytes), fmt::arg("avail", available_ram_gigabytes),
|
|
|
|
fmt::arg("swapAvail", available_swap_gigabytes)));
|
2020-09-02 12:25:57 +00:00
|
|
|
} else {
|
2021-06-23 08:43:28 +00:00
|
|
|
button_.set_tooltip_text(fmt::format("{:.{}f}GiB used", used_ram_gigabytes, 1));
|
2020-09-02 12:25:57 +00:00
|
|
|
}
|
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
|
2021-05-27 13:40:54 +00:00
|
|
|
AButton::update();
|
2018-11-08 20:09:56 +00:00
|
|
|
}
|