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

Conversation

Low-power
Copy link
Contributor

Firstly the biggest issue, htop didn't count the 'inactive' memory (vm.stats.vm.v_inactive_count) as used memory, but as free; this is incorrect. The inactive pages reported by the kernel are memory pages allocated by user space programs that are not recently used, and are therefore available for swapping out in case system is short in memory; these are definitely not free pages. This part of memory is usually considerably big in most systems, as a result htop had wrongly reported a big part of memory as 'free'.

Additionally the VFS buffer size as reported in vfs.bufspace (which belongs to caches in htop's view) is actually included in the 'wired' memory (vm.stats.vm.v_wire_count). The total used memory in htop should be subtracted by this size.

htop also mistakenly mapped kFreeBSD 'VFS buffer' into its own 'buffers' memory class, but this Linux-originated 'buffers' memory don't have the same meaning. VFS buffer is actually used for file system caches, utilized by most file systems other than ZFS.

Along with the bug fixes, the corresponding comments has also been corrected and extended to better document the memory class mapping between htop and kFreeBSD.

@BenBE BenBE added documentation 📖 Improvements or additions to documentation enhancement Extension or improvement to existing feature FreeBSD 👹 FreeBSD related issues labels Jan 7, 2025
@BenBE BenBE added this to the 3.4.0 milestone Jan 7, 2025
Copy link
Member

@BenBE BenBE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Are the values memWire, memActive, and memInactive from the FreeBSDMaschine structure used anywhere outside FreeBSDMaschine_scanMemoryInformation?

@Low-power
Copy link
Contributor Author

Are the values memWire, memActive, and memInactive from the FreeBSDMaschine structure used anywhere outside FreeBSDMaschine_scanMemoryInformation?

I don't think they are. They can be turned into on-stack variables, but I'm lazy to touch them at this moment...

@BenBE
Copy link
Member

BenBE commented Jan 7, 2025

Could you prepend the following patch to your PR?

diff --git a/freebsd/FreeBSDMachine.c b/freebsd/FreeBSDMachine.c
index d781414b..3a478679 100644
--- a/freebsd/FreeBSDMachine.c
+++ b/freebsd/FreeBSDMachine.c
@@ -340,12 +340,10 @@ 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(buffersMem);
    sysctl(MIB_vfs_bufspace, 2, &(buffersMem), &len, NULL, 0);
@@ -361,7 +359,7 @@ static void FreeBSDMachine_scanMemoryInfo(Machine* super) {
    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;
 
    struct kvm_swap swap[16];
    int nswap = kvm_getswapinfo(this->kd, swap, ARRAYSIZE(swap), 0);
diff --git a/freebsd/FreeBSDMachine.h b/freebsd/FreeBSDMachine.h
index f34b568e..0f9c33f2 100644
--- a/freebsd/FreeBSDMachine.h
+++ b/freebsd/FreeBSDMachine.h
@@ -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;

TIA.

…' object

They are used only temporarily in function 'FreeBSDMachine_scanMemoryInfo'.
…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.
@Low-power Low-power force-pushed the freebsd-memory-usage-correction branch from a349a20 to 5da22ef Compare January 7, 2025 13:09
@Low-power
Copy link
Contributor Author

Rebased.

@BenBE BenBE merged commit 141452e into htop-dev:main Jan 7, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation 📖 Improvements or additions to documentation enhancement Extension or improvement to existing feature FreeBSD 👹 FreeBSD related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants