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

Memory usage reporting for FreeBSD is incorrect #1581

Merged
merged 2 commits into from
Jan 7, 2025
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
24 changes: 15 additions & 9 deletions freebsd/FreeBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,21 @@ static void FreeBSDMachine_scanMemoryInfo(Machine* super) {
// @etosan:
// memory counter relationships seem to be these:
// total = active + wired + inactive + cache + free
// htop_used (unavail to anybody) = active + wired
// htop_cache (for cache meter) = buffers + cache
// user_free (avail to procs) = buffers + inactive + cache + free
// htop_used (unavail to anybody) = active + wired + inactive - buffer
// htop_cache (for cache meter) = buffer + cache
// htop_user_free (avail to procs) = buffer + cache + free
// htop_buffers (disk write buffer) = 0 (not applicable to FreeBSD)
//
// 'buffer' contain cache used by most file systems other than ZFS, and is
// included in 'wired'
//
// with ZFS ARC situation becomes bit muddled, as ARC behaves like "user_free"
// and belongs into cache, but is reported as wired by kernel
//
// htop_used = active + (wired - arc)
// htop_cache = buffers + cache + arc
u_long totalMem;
u_int memActive, memWire, cachedMem;
u_int memActive, memWire, memInactive, cachedMem;
long buffersMem;
size_t len;
struct vmtotal vmtotal;
Expand All @@ -340,28 +344,30 @@ static void FreeBSDMachine_scanMemoryInfo(Machine* super) {
len = sizeof(memActive);
sysctl(MIB_vm_stats_vm_v_active_count, 4, &(memActive), &len, NULL, 0);
memActive *= this->pageSizeKb;
this->memActive = memActive;

len = sizeof(memWire);
sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(memWire), &len, NULL, 0);
memWire *= this->pageSizeKb;
this->memWire = memWire;

len = sizeof(memInactive);
sysctl(MIB_vm_stats_vm_v_inactive_count, 4, &(memInactive), &len, NULL, 0);
memInactive *= this->pageSizeKb;

len = sizeof(buffersMem);
sysctl(MIB_vfs_bufspace, 2, &(buffersMem), &len, NULL, 0);
buffersMem /= 1024;
super->buffersMem = buffersMem;
super->cachedMem = buffersMem;

len = sizeof(cachedMem);
sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(cachedMem), &len, NULL, 0);
cachedMem *= this->pageSizeKb;
super->cachedMem = cachedMem;
super->cachedMem += cachedMem;

len = sizeof(vmtotal);
sysctl(MIB_vm_vmtotal, 2, &(vmtotal), &len, NULL, 0);
super->sharedMem = vmtotal.t_rmshr * this->pageSizeKb;

super->usedMem = this->memActive + this->memWire;
super->usedMem = memActive + memWire + memInactive - buffersMem;

struct kvm_swap swap[16];
int nswap = kvm_getswapinfo(this->kd, swap, ARRAYSIZE(swap), 0);
Expand Down
3 changes: 0 additions & 3 deletions freebsd/FreeBSDMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ typedef struct FreeBSDMachine_ {
int pageSizeKb;
int kernelFScale;

unsigned long long int memWire;
unsigned long long int memActive;

ZfsArcStats zfs;

CPUData* cpus;
Expand Down
Loading