Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetBSD: Fix meminfo. #2122

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/bsdcommon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -350,3 +355,38 @@ bool bsdcommon::is_conky_already_running() {

return instances > 1;
}

// conky 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
}
4 changes: 4 additions & 0 deletions src/bsdcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

#include <stdint.h>

#include "conky.h"

namespace bsdcommon {
struct cpu_load {
uint64_t old_used;
Expand All @@ -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_*/
64 changes: 1 addition & 63 deletions src/netbsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}

Expand Down
Loading