From ac118c9924a59aeebf46fb91f5f8452ec0b6000f Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Thu, 21 Nov 2019 17:01:29 +0000 Subject: [PATCH] ci: Parse the smaps file with Rust Instead of using bash and awk, using Rust allows us to retrieve information about a VM process with the right permissions as we are not forced to spawn a new child process. Signed-off-by: Rob Bradford --- src/main.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index b93f884e7..09ba7549c 100755 --- a/src/main.rs +++ b/src/main.rs @@ -414,6 +414,8 @@ mod tests { #![allow(dead_code)] use ssh2::Session; use std::fs; + use std::io; + use std::io::BufRead; use std::io::{Read, Write}; use std::net::TcpStream; use std::process::{Command, Stdio}; @@ -3046,23 +3048,20 @@ mod tests { } fn get_pss(pid: u32) -> u32 { - let output = Command::new("bash") - .arg("-c") - .arg( - format!( - "cat /proc/{}/smaps | grep -i pss | awk '{{Total+=$2}} END {{print Total}}'", - pid - ) - .as_str(), - ) - .output() - .unwrap(); + let smaps = fs::File::open(format!("/proc/{}/smaps", pid)).unwrap(); + let reader = io::BufReader::new(smaps); - std::str::from_utf8(output.stdout.as_slice()) - .unwrap() - .trim() - .parse::() - .unwrap() + let mut total = 0; + for line in reader.lines() { + let l = line.unwrap(); + // Lines look like this: + // Pss: 176 kB + if l.contains("Pss") { + let values: Vec<&str> = l.rsplit(' ').collect(); + total += values[1].trim().parse::().unwrap() + } + } + total } fn test_memory_mergeable(mergeable: bool) {