From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH sys 2/2] sys: procfs: read_meminfo: use MemAvailable from kernel to compute memused
Date: Thu, 13 Mar 2025 12:45:35 +0100 [thread overview]
Message-ID: <20250313114535.99912-2-dietmar@proxmox.com> (raw)
In-Reply-To: <20250313114535.99912-1-dietmar@proxmox.com>
The current code does not consider "SReclaimable" as cached memory,
which can lead to totally wrong values for memfree/memused.
This fix uses MemAvailable to compute memused, and returns MemFree
without adding buffers and caches. This corrensponds to the
values displayed by the "free" command line tool (total, used, free, avilable),
where used+available=total.
The value for buffers+cache can be estimated with: available - free
Also adds a simple test case for the parser.
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
---
proxmox-sys/src/linux/procfs/mod.rs | 85 +++++++++++++++++++++++++++--
1 file changed, 80 insertions(+), 5 deletions(-)
diff --git a/proxmox-sys/src/linux/procfs/mod.rs b/proxmox-sys/src/linux/procfs/mod.rs
index f9dbb5bc..46452d8f 100644
--- a/proxmox-sys/src/linux/procfs/mod.rs
+++ b/proxmox-sys/src/linux/procfs/mod.rs
@@ -432,24 +432,29 @@ fn parse_proc_meminfo(text: &str) -> Result<ProcFsMemInfo, Error> {
swapused: 0,
};
- let (mut buffers, mut cached) = (0, 0);
+ let mut available = 0;
+
for line in text.lines() {
let mut content_iter = line.split_whitespace();
if let (Some(key), Some(value)) = (content_iter.next(), content_iter.next()) {
match key {
"MemTotal:" => meminfo.memtotal = value.parse::<u64>()? * 1024,
"MemFree:" => meminfo.memfree = value.parse::<u64>()? * 1024,
+ "MemAvailable:" => available = value.parse::<u64>()? * 1024,
"SwapTotal:" => meminfo.swaptotal = value.parse::<u64>()? * 1024,
"SwapFree:" => meminfo.swapfree = value.parse::<u64>()? * 1024,
- "Buffers:" => buffers = value.parse::<u64>()? * 1024,
- "Cached:" => cached = value.parse::<u64>()? * 1024,
_ => continue,
}
}
}
- meminfo.memfree += buffers + cached;
- meminfo.memused = meminfo.memtotal - meminfo.memfree;
+ // see free.c and meminfo.h from https://gitlab.com/procps-ng/procps
+ // Note: USED+FREE != TOTAL (USED+FREE+BUFFER+CACHED+SRECLAIMABLE == TOTAL)
+
+ meminfo.memused = meminfo.memtotal - available;
+
+ // Available memory is: meminfo.memtotal - meminfo.memused
+ // Buffers + CACHE = meminfo.memtotal - meminfo.memused - meminfo.free
meminfo.swapused = meminfo.swaptotal - meminfo.swapfree;
@@ -462,6 +467,76 @@ fn parse_proc_meminfo(text: &str) -> Result<ProcFsMemInfo, Error> {
Ok(meminfo)
}
+#[test]
+fn test_read_proc_meminfo() {
+ let meminfo = parse_proc_meminfo(
+ "MemTotal: 32752584 kB
+MemFree: 2106048 kB
+MemAvailable: 13301592 kB
+Buffers: 0 kB
+Cached: 490072 kB
+SwapCached: 0 kB
+Active: 658700 kB
+Inactive: 59528 kB
+Active(anon): 191996 kB
+Inactive(anon): 49880 kB
+Active(file): 466704 kB
+Inactive(file): 9648 kB
+Unevictable: 16008 kB
+Mlocked: 12936 kB
+SwapTotal: 3 kB
+SwapFree: 2 kB
+Zswap: 0 kB
+Zswapped: 0 kB
+Dirty: 0 kB
+Writeback: 0 kB
+AnonPages: 244204 kB
+Mapped: 66032 kB
+Shmem: 9960 kB
+KReclaimable: 11525744 kB
+Slab: 21002876 kB
+SReclaimable: 11525744 kB
+SUnreclaim: 9477132 kB
+KernelStack: 6816 kB
+PageTables: 4812 kB
+SecPageTables: 0 kB
+NFS_Unstable: 0 kB
+Bounce: 0 kB
+WritebackTmp: 0 kB
+CommitLimit: 16376292 kB
+Committed_AS: 316368 kB
+VmallocTotal: 34359738367 kB
+VmallocUsed: 983836 kB
+VmallocChunk: 0 kB
+Percpu: 12096 kB
+HardwareCorrupted: 0 kB
+AnonHugePages: 0 kB
+ShmemHugePages: 0 kB
+ShmemPmdMapped: 0 kB
+FileHugePages: 0 kB
+FilePmdMapped: 0 kB
+Unaccepted: 0 kB
+HugePages_Total: 0
+HugePages_Free: 0
+HugePages_Rsvd: 0
+HugePages_Surp: 0
+Hugepagesize: 2048 kB
+Hugetlb: 0 kB
+DirectMap4k: 237284 kB
+DirectMap2M: 13281280 kB
+DirectMap1G: 22020096 kB
+",
+ )
+ .expect("successful parsed a sample /proc/meminfo entry");
+
+ assert_eq!(meminfo.memtotal, 33538646016);
+ assert_eq!(meminfo.memused, 19917815808);
+ assert_eq!(meminfo.memfree, 2156593152);
+ assert_eq!(meminfo.swapfree, 2048);
+ assert_eq!(meminfo.swaptotal, 3072);
+ assert_eq!(meminfo.swapused, 1024);
+}
+
#[derive(Clone, Debug)]
pub struct ProcFsCPUInfo {
pub user_hz: f64,
--
2.39.5
_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel
prev parent reply other threads:[~2025-03-13 11:46 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-13 11:45 [pbs-devel] [PATCH sys 1/2] sys: procfs: split read_meminfo into read and parse functions Dietmar Maurer
2025-03-13 11:45 ` Dietmar Maurer [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250313114535.99912-2-dietmar@proxmox.com \
--to=dietmar@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal