Skip to content

Commit

Permalink
Fix issues related to memory usage reporting for kFreeBSD-based platf…
Browse files Browse the repository at this point in the history
…orms:

* 'inactive' pages are not treated as used memory as it should be.
* 'wired' pages contain VFS buffer (considered as cache by htop), but the
  amount didn't get subtracted from used memory.
* VFS buffer as reported by the kernel should belong to htop's 'cached'
  memory, but htop currently classified it as 'buffers', which is a different
  concept originated from Linux.
  • Loading branch information
Low-power authored and BenBE committed Jan 7, 2025
1 parent 6638971 commit 141452e
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 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 @@ -345,21 +349,25 @@ static void FreeBSDMachine_scanMemoryInfo(Machine* super) {
sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(memWire), &len, NULL, 0);
memWire *= this->pageSizeKb;

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 = memActive + memWire;
super->usedMem = memActive + memWire + memInactive - buffersMem;

struct kvm_swap swap[16];
int nswap = kvm_getswapinfo(this->kd, swap, ARRAYSIZE(swap), 0);
Expand Down

0 comments on commit 141452e

Please sign in to comment.