From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH sys 1/2] sys: procfs: split read_meminfo into read and parse functions
Date: Thu, 13 Mar 2025 12:45:34 +0100 [thread overview]
Message-ID: <20250313114535.99912-1-dietmar@proxmox.com> (raw)
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
next reply other threads:[~2025-03-13 11:45 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-13 11:45 Dietmar Maurer [this message]
2025-03-13 11:45 ` [pbs-devel] [PATCH sys 2/2] sys: procfs: read_meminfo: use MemAvailable from kernel to compute memused Dietmar Maurer
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-1-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