diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a727c0a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/data diff --git a/README.md b/README.md index 0643058..8c786e0 100644 --- a/README.md +++ b/README.md @@ -1 +1,23 @@ -# mem_checker \ No newline at end of file +# mem_checker + +## How to use + +### setup + +``` +git clone https://github.com/TakanoTaiga/mem_checker.git +cd mem_checker +mkdir ./data +``` + +### record + +ps +``` +cargo run --bin auto_ps +``` + +pmap +``` +cargo run --bin auto_pmap +``` \ No newline at end of file diff --git a/src/bin/auto_pmap.rs b/src/bin/auto_pmap.rs new file mode 100644 index 0000000..7700d47 --- /dev/null +++ b/src/bin/auto_pmap.rs @@ -0,0 +1,50 @@ +use std::collections::HashMap; +use std::process::{Command, Stdio}; +use std::thread; +use std::time::Duration; +use std::path::Path; +use std::fs::OpenOptions; +use std::io::Write; + +fn main() { + loop { + let output = Command::new("pmap") + .args(["-x", "54875"]) + .stdout(Stdio::piped()) + .output() + .expect("Failed to execute command"); + + let output_str = String::from_utf8_lossy(&output.stdout); + let lines = output_str.lines().skip(1); + + let mut rss_map: HashMap = HashMap::new(); + + for line in lines { + let columns: Vec<&str> = line.split_whitespace().collect(); + + let rss = columns.get(2).and_then(|&s| s.parse::().ok()).unwrap_or(0); + let name = columns.get(5).unwrap_or(&""); + + if rss == 0 || !name.contains(".so") { + continue; + } + + *rss_map.entry(name.to_string()).or_insert(0) += rss; + } + + for (name, total_rss) in rss_map { + let file_name = format!("./data/{}.csv",name); + let path = Path::new(&file_name); + let mut file = OpenOptions::new() + .write(true) + .append(true) + .create(true) + .open(path) + .expect("Failed to open file"); + + writeln!(file, "{}",total_rss) + .expect("Failed to write to file"); + } + thread::sleep(Duration::from_secs(1)); + } +} diff --git a/src/main.rs b/src/bin/auto_ps.rs similarity index 100% rename from src/main.rs rename to src/bin/auto_ps.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e69de29