proxmox/proxmox-sys
Thomas Lamprecht 58d6e8d492 sys: memory info: use MemAvailable from kernel to compute used memory
The old code was wrong and overestimated the memory used because it
did not take into account things like "SReclaimable", a part of slab
(in-kernel memory allocator) describing things like caches that can be
reclaimed, plus the memory for "Active(file)" and "Inactive(file)",
and other internal kernel things that even though small for each one,
can add up quickly.

Most of these metrics are exposed and could be included in the
calculation, but this will simply become obsolete in the future as the
kernel changes how it does things and how it calculates such available
memory, as it has done many times in the past.

To solve this problem for the long term, the MemAvailable field was
added to /proc/meminfo as of kernel 3.14. It describes "the amount of
memory available for a new workload without pushing the system into
swap". While it is only an estimate, it is as good as it gets, and
since it comes from the kernel, we can always assume that it is
correct for the currently booted kernel.

So, switch over to this metric for calculating the used memory by
subtracting MemAvailable from MemTotal.

Also adds a simple test case for the parser.

This commit is based on a patch from Dietmar [1].

[0]: https://git.kernel.org/torvalds/c/34e431b0ae398fc54ea69ff85ec700722c9da773
[1]: https://lore.proxmox.com/all/20250313114535.99912-2-dietmar@proxmox.com/

Originally-by: Dietmar Maurer <dietmar@proxmox.com>
 [TL: rewrite comments and commit message from scratch]
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2025-04-07 20:49:00 +02:00
..
debian sys: bump to 0.6.6-1 2025-03-19 12:19:43 +01:00
src sys: memory info: use MemAvailable from kernel to compute used memory 2025-04-07 20:49:00 +02:00
tests sys: add truncate option to OpenOptions in test case 2025-03-06 15:15:43 +01:00
Cargo.toml sys: bump to 0.6.6-1 2025-03-19 12:19:43 +01:00