public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH sys 1/2] sys: procfs: split read_meminfo into read and parse functions
@ 2025-03-13 11:45 Dietmar Maurer
  2025-03-13 11:45 ` [pbs-devel] [PATCH sys 2/2] sys: procfs: read_meminfo: use MemAvailable from kernel to compute memused Dietmar Maurer
  2025-03-20 17:43 ` [pbs-devel] applied: [PATCH sys 1/2] sys: procfs: split read_meminfo into read and parse functions Thomas Lamprecht
  0 siblings, 2 replies; 4+ messages in thread
From: Dietmar Maurer @ 2025-03-13 11:45 UTC (permalink / raw)
  To: pbs-devel

So that we can write tests.

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
---
 proxmox-sys/src/linux/procfs/mod.rs | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/proxmox-sys/src/linux/procfs/mod.rs b/proxmox-sys/src/linux/procfs/mod.rs
index 0875fcf8..f9dbb5bc 100644
--- a/proxmox-sys/src/linux/procfs/mod.rs
+++ b/proxmox-sys/src/linux/procfs/mod.rs
@@ -416,8 +416,12 @@ pub struct ProcFsMemInfo {
 
 pub fn read_meminfo() -> Result<ProcFsMemInfo, Error> {
     let path = "/proc/meminfo";
-    let file = OpenOptions::new().read(true).open(path)?;
 
+    let meminfo_str = std::fs::read_to_string(path)?;
+    parse_proc_meminfo(&meminfo_str)
+}
+
+fn parse_proc_meminfo(text: &str) -> Result<ProcFsMemInfo, Error> {
     let mut meminfo = ProcFsMemInfo {
         memtotal: 0,
         memfree: 0,
@@ -429,9 +433,8 @@ pub fn read_meminfo() -> Result<ProcFsMemInfo, Error> {
     };
 
     let (mut buffers, mut cached) = (0, 0);
-    for line in BufReader::new(&file).lines() {
-        let content = line?;
-        let mut content_iter = content.split_whitespace();
+    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,
-- 
2.39.5


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-03-20 17:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [pbs-devel] [PATCH sys 2/2] sys: procfs: read_meminfo: use MemAvailable from kernel to compute memused Dietmar Maurer
2025-03-18 11:09   ` Fabian Grünbichler
2025-03-20 17:43 ` [pbs-devel] applied: [PATCH sys 1/2] sys: procfs: split read_meminfo into read and parse functions Thomas Lamprecht

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