2018-08-09 01:42:52 +02:00
|
|
|
#include "modules/memory.hpp"
|
|
|
|
|
|
2018-12-18 17:30:54 +01:00
|
|
|
waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config)
|
2022-11-24 12:28:52 +01:00
|
|
|
: ALabel(config, "memory", id, "{}%", 30) {
|
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:42:52 +02:00
|
|
|
};
|
2026-04-05 19:01:36 -04:00
|
|
|
if (config["unit"].isString()) {
|
|
|
|
|
unit_ = config["unit"].asString();
|
|
|
|
|
}
|
2018-08-18 11:43:48 +02:00
|
|
|
}
|
2018-08-09 01:42:52 +02:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
auto waybar::modules::Memory::update() -> void {
|
2018-11-08 21:09:56 +01:00
|
|
|
parseMeminfo();
|
2020-03-20 17:37:22 -04:00
|
|
|
|
|
|
|
|
unsigned long memtotal = meminfo_["MemTotal"];
|
2021-08-29 16:34:29 +03:00
|
|
|
unsigned long swaptotal = 0;
|
2026-04-05 19:01:36 -04:00
|
|
|
if (meminfo_.contains("SwapTotal")) {
|
2021-08-29 16:34:29 +03:00
|
|
|
swaptotal = meminfo_["SwapTotal"];
|
|
|
|
|
}
|
2020-03-20 17:37:22 -04:00
|
|
|
unsigned long memfree;
|
2021-08-29 16:34:29 +03:00
|
|
|
unsigned long swapfree = 0;
|
2026-04-05 19:01:36 -04:00
|
|
|
if (meminfo_.contains("SwapFree")) {
|
2021-08-29 16:34:29 +03:00
|
|
|
swapfree = meminfo_["SwapFree"];
|
|
|
|
|
}
|
2026-04-05 19:01:36 -04:00
|
|
|
if (meminfo_.contains("MemAvailable")) {
|
2020-03-20 17:37:22 -04:00
|
|
|
// New kernels (3.4+) have an accurate available memory field.
|
2021-10-28 19:10:46 +02:00
|
|
|
memfree = meminfo_["MemAvailable"] + meminfo_["zfs_size"];
|
2020-03-20 17:37:22 -04:00
|
|
|
} else {
|
|
|
|
|
// Old kernel; give a best-effort approximation of available memory.
|
|
|
|
|
memfree = meminfo_["MemFree"] + meminfo_["Buffers"] + meminfo_["Cached"] +
|
2021-10-28 19:10:46 +02:00
|
|
|
meminfo_["SReclaimable"] - meminfo_["Shmem"] + meminfo_["zfs_size"];
|
2020-03-20 17:37:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (memtotal > 0 && memfree >= 0) {
|
2022-04-06 08:37:19 +02:00
|
|
|
int used_ram_percentage = 100 * (memtotal - memfree) / memtotal;
|
|
|
|
|
int used_swap_percentage = 0;
|
2026-04-05 19:01:36 -04:00
|
|
|
if ((bool) swaptotal) {
|
2021-08-29 16:34:29 +03:00
|
|
|
used_swap_percentage = 100 * (swaptotal - swapfree) / swaptotal;
|
|
|
|
|
}
|
2026-04-05 19:01:36 -04:00
|
|
|
|
|
|
|
|
float divisor = calc_divisor(unit_);
|
|
|
|
|
float total_ram = memtotal / divisor;
|
|
|
|
|
float total_swap = swaptotal / divisor;
|
|
|
|
|
float used_ram = (memtotal - memfree) / divisor;
|
|
|
|
|
float used_swap = (swaptotal - swapfree) / divisor;
|
|
|
|
|
float available_ram = memfree / divisor;
|
|
|
|
|
float available_swap = swapfree / divisor;
|
2019-05-19 16:10:01 +01:00
|
|
|
|
2020-10-12 02:05:26 +02: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 07:30:07 +02:00
|
|
|
auto icons = std::vector<std::string>{state};
|
2022-11-24 12:28:52 +01:00
|
|
|
label_.set_markup(fmt::format(
|
2023-01-16 13:24:55 -08:00
|
|
|
fmt::runtime(format), used_ram_percentage,
|
|
|
|
|
fmt::arg("icon", getIcon(used_ram_percentage, icons)),
|
2026-04-05 19:01:36 -04:00
|
|
|
fmt::arg("total", total_ram), fmt::arg("swapTotal", total_swap),
|
2022-04-06 08:37:19 +02:00
|
|
|
fmt::arg("percentage", used_ram_percentage),
|
2025-06-19 15:55:35 +00:00
|
|
|
fmt::arg("swapState", swaptotal == 0 ? "Off" : "On"),
|
2026-04-05 19:01:36 -04:00
|
|
|
fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram),
|
|
|
|
|
fmt::arg("swapUsed", used_swap), fmt::arg("avail", available_ram),
|
|
|
|
|
fmt::arg("swapAvail", available_swap)));
|
2020-10-12 02:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-22 11:35:26 +01:00
|
|
|
if (tooltipEnabled()) {
|
2020-09-02 14:25:57 +02:00
|
|
|
if (config_["tooltip-format"].isString()) {
|
|
|
|
|
auto tooltip_format = config_["tooltip-format"].asString();
|
2026-02-13 02:17:46 +05:30
|
|
|
label_.set_tooltip_markup(fmt::format(
|
2023-01-16 13:24:55 -08:00
|
|
|
fmt::runtime(tooltip_format), used_ram_percentage,
|
2026-04-05 19:01:36 -04:00
|
|
|
fmt::arg("total", total_ram), fmt::arg("swapTotal", total_swap),
|
2022-04-06 08:37:19 +02:00
|
|
|
fmt::arg("percentage", used_ram_percentage),
|
2025-06-19 15:55:35 +00:00
|
|
|
fmt::arg("swapState", swaptotal == 0 ? "Off" : "On"),
|
2026-04-05 19:01:36 -04:00
|
|
|
fmt::arg("swapPercentage", used_swap_percentage), fmt::arg("used", used_ram),
|
|
|
|
|
fmt::arg("swapUsed", used_swap), fmt::arg("avail", available_ram),
|
|
|
|
|
fmt::arg("swapAvail", available_swap)));
|
2020-09-02 14:25:57 +02:00
|
|
|
} else {
|
2026-04-05 19:01:36 -04:00
|
|
|
label_.set_tooltip_markup(fmt::format("{:.{}f}GiB used", used_ram, 1));
|
2020-09-02 14:25:57 +02:00
|
|
|
}
|
2019-02-22 11:35:26 +01:00
|
|
|
}
|
2018-11-09 16:24:13 +01:00
|
|
|
} else {
|
2018-12-18 17:30:54 +01:00
|
|
|
event_box_.hide();
|
2018-11-09 16:24:13 +01:00
|
|
|
}
|
2020-04-12 18:30:21 +02:00
|
|
|
// Call parent update
|
2022-11-24 12:28:52 +01:00
|
|
|
ALabel::update();
|
2018-11-08 21:09:56 +01:00
|
|
|
}
|
2026-04-05 19:01:36 -04:00
|
|
|
|
|
|
|
|
float waybar::modules::Memory::calc_divisor(const std::string& divisor) {
|
|
|
|
|
if (divisor == "kB") {
|
|
|
|
|
return 1.0;
|
|
|
|
|
} else if (divisor == "kiB") {
|
|
|
|
|
return 1.024;
|
|
|
|
|
} else if (divisor == "MB") {
|
|
|
|
|
return 1.000 * 1000.0;
|
|
|
|
|
} else if (divisor == "MiB") {
|
|
|
|
|
return 1.024 * 1024.0;
|
|
|
|
|
} else if (divisor == "GB") {
|
|
|
|
|
return 1.000 * 1000.0 * 1000.0;
|
|
|
|
|
} else if (divisor == "GiB") {
|
|
|
|
|
return 1.024 * 1024.0 * 1024.0;
|
|
|
|
|
} else if (divisor == "TB") {
|
|
|
|
|
return 1.000 * 1000.0 * 1000.0 * 1000.0;
|
|
|
|
|
} else if (divisor == "TiB") {
|
|
|
|
|
return 1.024 * 1024.0 * 1024.0 * 1024.0;
|
|
|
|
|
} else { // default to GiB if it is anything that we don't recongnise
|
|
|
|
|
return 1.024 * 1024.0 * 1024.0;
|
|
|
|
|
}
|
|
|
|
|
}
|