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
|
|
|
};
|
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;
|
|
|
|
if (meminfo_.count("SwapTotal")) {
|
|
|
|
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;
|
|
|
|
if (meminfo_.count("SwapFree")) {
|
|
|
|
swapfree = meminfo_["SwapFree"];
|
|
|
|
}
|
2020-03-20 17:37:22 -04:00
|
|
|
if (meminfo_.count("MemAvailable")) {
|
|
|
|
// 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-10-26 17:26:15 +02:00
|
|
|
float total_ram_gigabytes =
|
|
|
|
0.01 * round(memtotal / 10485.76); // 100*10485.76 = 2^20 = 1024^2 = GiB/KiB
|
|
|
|
float total_swap_gigabytes = 0.01 * round(swaptotal / 10485.76);
|
2022-04-06 08:37:19 +02:00
|
|
|
int used_ram_percentage = 100 * (memtotal - memfree) / memtotal;
|
|
|
|
int used_swap_percentage = 0;
|
2021-08-29 16:34:29 +03:00
|
|
|
if (swaptotal && swapfree) {
|
|
|
|
used_swap_percentage = 100 * (swaptotal - swapfree) / swaptotal;
|
|
|
|
}
|
2022-10-26 17:26:15 +02:00
|
|
|
float used_ram_gigabytes = 0.01 * round((memtotal - memfree) / 10485.76);
|
|
|
|
float used_swap_gigabytes = 0.01 * round((swaptotal - swapfree) / 10485.76);
|
|
|
|
float available_ram_gigabytes = 0.01 * round(memfree / 10485.76);
|
|
|
|
float available_swap_gigabytes = 0.01 * round(swapfree / 10485.76);
|
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)),
|
2022-04-06 08:37:19 +02:00
|
|
|
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 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();
|
2022-11-24 12:28:52 +01:00
|
|
|
label_.set_tooltip_text(fmt::format(
|
2023-01-16 13:24:55 -08:00
|
|
|
fmt::runtime(tooltip_format), used_ram_percentage,
|
|
|
|
fmt::arg("total", total_ram_gigabytes), fmt::arg("swapTotal", total_swap_gigabytes),
|
2022-04-06 08:37:19 +02:00
|
|
|
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 14:25:57 +02:00
|
|
|
} else {
|
2022-11-24 12:28:52 +01:00
|
|
|
label_.set_tooltip_text(fmt::format("{:.{}f}GiB used", used_ram_gigabytes, 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
|
|
|
}
|