forked from Ma233/swapview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapview.rs
More file actions
96 lines (85 loc) · 2.18 KB
/
swapview.rs
File metadata and controls
96 lines (85 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use std::fs::{File,read_dir};
use std::io::{Read,BufReader,BufRead};
const UNITS: [char; 4] = ['K', 'M', 'G', 'T'];
fn filesize(size: isize) -> String {
let mut left = size.abs() as f64;
let mut unit = -1;
while left > 1100. && unit < 3 {
left /= 1024.;
unit += 1;
}
if unit == -1 {
format!("{}B", size)
} else {
if size < 0 {
left = -left;
}
format!("{:.1}{}iB", left, UNITS[unit as usize])
}
}
fn chop_null(s: String) -> String {
let last = s.len() - 1;
let mut s = s;
if s.len() > 0 && s.as_bytes()[last] == 0 {
s.truncate(last);
}
s.replace("\0", " ")
}
fn get_comm_for(pid: usize) -> String {
let cmdline_path = format!("/proc/{}/cmdline", pid);
let mut buf = String::new();
let mut file = match File::open(&cmdline_path) {
Ok(f) => f,
Err(_) => return String::new(),
};
match file.read_to_string(&mut buf) {
Ok(_) => (),
Err(_) => return String::new(),
};
chop_null(buf)
}
fn get_swap_for(pid: usize) -> isize {
let mut s = 0;
let smaps_path = format!("/proc/{}/smaps", pid);
let file = match File::open(&smaps_path) {
Ok(f) => f,
Err(_) => return 0,
};
let reader = BufReader::new(file);
for l in reader.lines() {
let line = match l {
Ok(s) => s,
Err(_) => return 0,
};
if line.starts_with("Swap:") {
s += line.split_whitespace().nth(1).unwrap().parse::<isize>().unwrap();
}
}
s * 1024
}
fn get_swap() -> Vec<(usize, isize, String)> {
read_dir("/proc").unwrap().filter_map(|d| {
let path = d.unwrap().path();
path.file_name().unwrap().to_str().unwrap()
.parse().ok().and_then(|pid|
match get_swap_for(pid) {
0 => None,
swap => Some((pid, swap, get_comm_for(pid))),
}
)
}).collect()
}
fn main() {
// let format = "{:>5} {:>9} {}";
// let totalFmt = "Total: {:8}";
let mut swapinfo = get_swap();
swapinfo.sort_by(|&(_, a, _), &(_, b, _)| { a.cmp(&b) });
println!("{:>5} {:>9} {}", "PID", "SWAP", "COMMAND");
let mut total = 0;
for &(pid, swap, ref comm) in &swapinfo {
total += swap;
println!("{:>5} {:>9} {}", pid, filesize(swap), comm);
}
println!("Total: {:>8}", filesize(total));
}
// vim: se sw=2: