Waybar/src/modules/memory/common.cpp

88 lines
3.5 KiB
C++
Raw Normal View History

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)
2022-11-24 11:28:52 +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-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();
unsigned long memtotal = meminfo_["MemTotal"];
2021-08-29 13:34:29 +00:00
unsigned long swaptotal = 0;
if (meminfo_.count("SwapTotal")) {
swaptotal = meminfo_["SwapTotal"];
}
unsigned long memfree;
2021-08-29 13:34:29 +00:00
unsigned long swapfree = 0;
if (meminfo_.count("SwapFree")) {
swapfree = meminfo_["SwapFree"];
}
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"];
} 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"];
}
if (memtotal > 0 && memfree >= 0) {
2022-10-26 15:26:15 +00: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 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;
}
2022-10-26 15:26:15 +00: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);
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};
2022-11-24 11:28:52 +00:00
label_.set_markup(fmt::format(
fmt::runtime(format), used_ram_percentage,
fmt::arg("icon", getIcon(used_ram_percentage, icons)),
2022-04-06 06:37:19 +00: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)));
}
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();
2022-11-24 11:28:52 +00:00
label_.set_tooltip_text(fmt::format(
fmt::runtime(tooltip_format), used_ram_percentage,
fmt::arg("total", total_ram_gigabytes), fmt::arg("swapTotal", total_swap_gigabytes),
2022-04-06 06:37:19 +00: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 12:25:57 +00:00
} else {
2022-11-24 11:28:52 +00:00
label_.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
2022-11-24 11:28:52 +00:00
ALabel::update();
2018-11-08 20:09:56 +00:00
}