memory: port parseMeminfo to BSDs

This commit is contained in:
Jan Beich
2019-08-09 10:40:33 +00:00
parent c844d7ac2e
commit c4f7cdeec4
7 changed files with 126 additions and 20 deletions

View File

@ -0,0 +1,89 @@
#include "modules/memory.hpp"
#include <sys/types.h>
#include <sys/sysctl.h>
#include <unistd.h> // getpagesize
#if defined(__DragonFly__)
# include <sys/vmmeter.h> // struct vmstats
#elif defined(__NetBSD__)
# include <uvm/uvm_extern.h> // struct uvmexp_sysctl
#elif defined(__OpenBSD__)
# include <uvm/uvmexp.h> // struct uvmexp
#endif
static uint64_t get_total_memory() {
#if defined(HW_MEMSIZE) || defined(HW_PHYSMEM64)
uint64_t physmem;
#else
u_long physmem;
#endif
int mib[] = {
CTL_HW,
#if defined(HW_MEMSIZE)
HW_MEMSIZE,
#elif defined(HW_PHYSMEM64)
HW_PHYSMEM64,
#else
HW_PHYSMEM,
#endif
};
u_int miblen = sizeof(mib) / sizeof(mib[0]);
size_t sz = sizeof(physmem);
if (sysctl(mib, miblen, &physmem, &sz, NULL, 0)) {
throw std::runtime_error("sysctl hw.physmem failed");
}
return physmem;
}
static uint64_t get_free_memory() {
#if defined(__DragonFly__)
struct vmstats vms;
size_t sz = sizeof(vms);
if (sysctlbyname("vm.vmstats", &vms, &sz, NULL, 0)) {
throw std::runtime_error("sysctl vm.vmstats failed");
}
return static_cast<uint64_t>
(vms.v_free_count + vms.v_inactive_count + vms.v_cache_count)
* getpagesize();
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
u_int v_free_count = 0, v_inactive_count = 0, v_cache_count = 0;
size_t sz = sizeof(u_int);
sysctlbyname("vm.stats.vm.v_free_count",
&v_free_count, &sz, NULL, 0);
sysctlbyname("vm.stats.vm.v_inactive_count",
&v_inactive_count, &sz, NULL, 0);
sysctlbyname("vm.stats.vm.v_cache_count",
&v_cache_count, &sz, NULL, 0);
return static_cast<uint64_t>
(v_free_count + v_inactive_count + v_cache_count)
* getpagesize();
#elif defined(__NetBSD__) || defined(__OpenBSD__)
#ifdef VM_UVMEXP2
# undef VM_UVMEXP
# define VM_UVMEXP VM_UVMEXP2
# define uvmexp uvmexp_sysctl
#else
# define filepages vnodepages
# define execpages vtextpages
#endif
int mib[] = {
CTL_VM,
VM_UVMEXP,
};
u_int miblen = sizeof(mib) / sizeof(mib[0]);
struct uvmexp uvmexp;
size_t sz = sizeof(uvmexp);
if (sysctl(mib, miblen, &uvmexp, &sz, NULL, 0)) {
throw std::runtime_error("sysctl vm.uvmexp failed");
}
return static_cast<uint64_t>
(uvmexp.free + uvmexp.inactive + uvmexp.filepages + uvmexp.execpages)
* uvmexp.pagesize;
#endif
}
void waybar::modules::Memory::parseMeminfo() {
meminfo_["MemTotal"] = get_total_memory() / 1024;
meminfo_["MemAvailable"] = get_free_memory() / 1024;
}

View File

@ -0,0 +1,47 @@
#include "modules/memory.hpp"
waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config)
: ALabel(config, "memory", id, "{}%", 30) {
thread_ = [this] {
dp.emit();
thread_.sleep_for(interval_);
};
}
auto waybar::modules::Memory::update() -> void {
parseMeminfo();
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);
getState(used_ram_percentage);
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)));
if (tooltipEnabled()) {
label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1));
}
event_box_.show();
} else {
event_box_.hide();
}
// Call parent update
ALabel::update();
}

View File

@ -0,0 +1,20 @@
#include "modules/memory.hpp"
void waybar::modules::Memory::parseMeminfo() {
const std::string data_dir_ = "/proc/meminfo";
std::ifstream info(data_dir_);
if (!info.is_open()) {
throw std::runtime_error("Can't open " + data_dir_);
}
std::string line;
while (getline(info, line)) {
auto posDelim = line.find(':');
if (posDelim == std::string::npos) {
continue;
}
std::string name = line.substr(0, posDelim);
int64_t value = std::stol(line.substr(posDelim + 1));
meminfo_[name] = value;
}
}