2022-04-06 12:55:08 +00:00
|
|
|
// clang-format off
|
2022-04-06 06:37:19 +00:00
|
|
|
#include <sys/types.h>
|
2022-04-06 12:55:08 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
// clang-format on
|
2022-04-06 06:37:19 +00:00
|
|
|
#include <unistd.h> // getpagesize
|
|
|
|
|
|
|
|
#include "modules/memory.hpp"
|
2019-08-09 10:40:33 +00:00
|
|
|
|
|
|
|
#if defined(__DragonFly__)
|
2022-04-06 06:37:19 +00:00
|
|
|
#include <sys/vmmeter.h> // struct vmstats
|
2019-08-09 10:40:33 +00:00
|
|
|
#elif defined(__NetBSD__)
|
2022-04-06 06:37:19 +00:00
|
|
|
#include <uvm/uvm_extern.h> // struct uvmexp_sysctl
|
2019-08-09 10:40:33 +00:00
|
|
|
#elif defined(__OpenBSD__)
|
2022-04-06 06:37:19 +00:00
|
|
|
#include <uvm/uvmexp.h> // struct uvmexp
|
2019-08-09 10:40:33 +00:00
|
|
|
#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");
|
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
return static_cast<uint64_t>(vms.v_free_count + vms.v_inactive_count + vms.v_cache_count) *
|
|
|
|
getpagesize();
|
2019-08-09 10:40:33 +00:00
|
|
|
#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);
|
2022-04-06 06:37:19 +00:00
|
|
|
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();
|
2019-08-09 10:40:33 +00:00
|
|
|
#elif defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
#ifdef VM_UVMEXP2
|
2022-04-06 06:37:19 +00:00
|
|
|
#undef VM_UVMEXP
|
|
|
|
#define VM_UVMEXP VM_UVMEXP2
|
|
|
|
#define uvmexp uvmexp_sysctl
|
2019-08-09 10:40:33 +00:00
|
|
|
#else
|
2022-04-06 06:37:19 +00:00
|
|
|
#define filepages vnodepages
|
|
|
|
#define execpages vtextpages
|
2019-08-09 10:40:33 +00:00
|
|
|
#endif
|
|
|
|
int mib[] = {
|
2022-04-06 06:37:19 +00:00
|
|
|
CTL_VM,
|
|
|
|
VM_UVMEXP,
|
2019-08-09 10:40:33 +00:00
|
|
|
};
|
|
|
|
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");
|
|
|
|
}
|
2022-04-06 06:37:19 +00:00
|
|
|
return static_cast<uint64_t>(uvmexp.free + uvmexp.inactive + uvmexp.filepages +
|
|
|
|
uvmexp.execpages) *
|
|
|
|
uvmexp.pagesize;
|
2019-08-09 10:40:33 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void waybar::modules::Memory::parseMeminfo() {
|
|
|
|
meminfo_["MemTotal"] = get_total_memory() / 1024;
|
|
|
|
meminfo_["MemAvailable"] = get_free_memory() / 1024;
|
|
|
|
}
|