NetBSD: Fix meminfo.

Fixes #2108.
This commit is contained in:
Tóth János
2024-12-22 19:45:06 +01:00
parent b0cb25059b
commit b01a5d21ff
3 changed files with 45 additions and 63 deletions

View File

@@ -32,6 +32,11 @@
#include <string.h>
#if defined(__NetBSD__)
#include <uvm/uvm_param.h>
#include <uvm/uvm_extern.h>
#endif
#include "top.h"
static kvm_t *kd = nullptr;
@@ -350,3 +355,38 @@ bool bsdcommon::is_conky_already_running() {
return instances > 1;
}
// conyk uses kilobytes
static unsigned long long to_conky_size(uint64_t size, uint64_t pagesize) {
return (size >> 10) * pagesize;
}
void bsdcommon::update_meminfo(struct information &info) {
size_t len;
#if defined(__NetBSD__)
int mib[2] = {CTL_VM, VM_UVMEXP2};
// NOTE(gmb): https://github.com/NetBSD/src/blob/trunk/sys/uvm/uvm_extern.h
struct uvmexp_sysctl meminfo;
#else
#error Not supported BSD system.
#endif
len = sizeof(meminfo);
if (sysctl(mib, 2, &meminfo, &len, NULL, 0) == -1 ) {
NORM_ERR("sysctl() failed");
return;
}
#if defined(__NetBSD__)
// TODO(gmb): Try to fill all memory related fields.
info.memmax = to_conky_size(meminfo.npages, meminfo.pagesize);
info.memfree = info.memeasyfree = to_conky_size(meminfo.free, meminfo.pagesize);
info.mem = info.memmax - info.memfree;
info.swapmax = to_conky_size(meminfo.swpages, meminfo.pagesize);
info.swap = to_conky_size(meminfo.swpginuse, meminfo.pagesize);
info.swapfree = info.swapmax - info.swap;
#else
#error Not supported BSD system.
#endif
}

View File

@@ -40,6 +40,8 @@
#include <stdint.h>
#include "conky.h"
namespace bsdcommon {
struct cpu_load {
uint64_t old_used;
@@ -57,6 +59,8 @@ namespace bsdcommon {
void get_number_of_running_processes(short unsigned int *run_procs);
void update_top_info();
bool is_conky_already_running();
void update_meminfo(struct information &info);
}
#endif /*BSDCOMMON_H_*/

View File

@@ -58,41 +58,6 @@ static int nkd_init = 0;
static u_int32_t sensvalue;
static char errbuf[_POSIX2_LINE_MAX];
static int swapmode(int *retavail, int *retfree) {
int n;
struct swapent *sep;
*retavail = 0;
*retfree = 0;
n = swapctl(SWAP_NSWAP, 0, 0);
if (n < 1) {
NORM_ERR("could not get swap information");
return 0;
}
sep = (struct swapent *)malloc(n * (sizeof(*sep)));
if (sep == nullptr) {
NORM_ERR("memory allocation failed");
return 0;
}
if (swapctl(SWAP_STATS, (void *)sep, n) < n) {
NORM_ERR("could not get swap stats");
return 0;
}
for (; n > 0; n--) {
*retavail += (int)dbtob(sep[n - 1].se_nblks);
*retfree += (int)dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
}
*retavail = (int)(*retavail / 1024);
*retfree = (int)(*retfree / 1024);
return 1;
}
void prepare_update() {}
int update_uptime() {
@@ -120,34 +85,7 @@ int check_mount(struct text_object *obj) {
}
int update_meminfo() {
int mib[] = {CTL_VM, VM_UVMEXP2};
int total_pages, inactive_pages, free_pages;
int swap_avail, swap_free;
const int pagesize = getpagesize();
struct uvmexp_sysctl uvmexp;
size_t size = sizeof(uvmexp);
if (sysctl(mib, 2, &uvmexp, &size, nullptr, 0) < 0) {
NORM_ERR("could not get memory info");
return 1;
}
total_pages = uvmexp.npages;
free_pages = uvmexp.free;
inactive_pages = uvmexp.inactive;
info.memmax = (total_pages * pagesize) >> 10;
info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
info.memwithbuffers = info.mem;
info.memeasyfree = info.memfree = info.memmax - info.mem;
info.legacymem = info.mem;
if (swapmode(&swap_avail, &swap_free) >= 0) {
info.swapmax = swap_avail;
info.swap = (swap_avail - swap_free);
info.swapfree = swap_free;
}
bsdcommon::update_meminfo(info);
return 1;
}