src/tools/procfs.rs: implement read_memory_usage()

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
This commit is contained in:
Christian Ebner 2019-04-12 13:21:21 +02:00 committed by Dietmar Maurer
parent 3005ef48b6
commit 13abfdc57a

View File

@ -3,6 +3,7 @@ use failure::*;
use std::fs::OpenOptions;
use std::io::{BufRead, BufReader};
use std::collections::HashSet;
use std::process;
use crate::tools;
use lazy_static::lazy_static;
use regex::Regex;
@ -208,3 +209,27 @@ pub fn read_cpuinfo() -> Result<ProcFsCPUInfo, Error> {
Ok(cpuinfo)
}
#[derive(Debug)]
pub struct ProcFsMemUsage {
pub size: u64,
pub resident: u64,
pub shared: u64,
}
pub fn read_memory_usage() -> Result<ProcFsMemUsage, Error> {
let path = format!("/proc/{}/statm", process::id());
let line = tools::file_read_firstline(&path)?;
let mut values = line.split_whitespace().map(|v| v.parse::<u64>());
let ps = 4096;
match (values.next(), values.next(), values.next()) {
(Some(Ok(size)), Some(Ok(resident)), Some(Ok(shared))) =>
Ok(ProcFsMemUsage {
size: size * ps,
resident: resident * ps,
shared: shared * ps,
}),
_ => bail!("Error while parsing '{}'", path),
}
}