-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
213 lines (197 loc) · 6.4 KB
/
main.rs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
extern crate procrs;
extern crate argparse;
#[macro_use]
extern crate prettytable;
use prettytable::Table;
use prettytable::row::Row;
use prettytable::format::FormatBuilder;
use std::collections::HashMap;
use std::iter::repeat;
use std::cmp::Ordering;
use procrs::pid::*;
use procrs::TaskId;
use argparse::{ArgumentParser, StoreTrue, Store};
mod columns;
fn main() {
let opts = parse_args();
let (query, long, perf, verbose, tree, threads) =
(opts.query, opts.long, opts.perf, opts.verbose, opts.tree, opts.threads);
let mut pids: Vec<_> = match threads {
false => {
PidIter::new_query(query)
.unwrap()
.collect::<Result<_, _>>().unwrap()
},
true => {
TidIter::new_query(query)
.unwrap()
.collect::<Result<_, _>>().unwrap()
}
};
let mut name_indent = HashMap::new();
if verbose {
for pid in pids {
println!("{:?}", pid);
}
return
}
if opts.tree {
pids = treeify_names(pids, &mut name_indent);
} else {
pids.sort_by(|p1, p2|
match threads {
false => p1.stat.pid.cmp(&p2.stat.pid),
true => {
let cmp = p1.status.tgid.cmp(&p2.status.tgid);
if let Ordering::Equal = cmp { return Ordering::Equal; }
p1.stat.pid.cmp(&p2.stat.pid)
}
}
);
};
// Assume hertz is 100.
// TODO: Look this up via syscall (no /proc value for it)
let hertz = 100;
let minute_hertz = hertz * 60;
let hour_hertz = minute_hertz * 60;
let mut table = Table::init(
pids.iter().map(|p| {
// When we have a tree, the name is prepended with an indent.
let mut name = match tree {
false => String::new(),
true => name_indent.remove(&p.stat.pid).unwrap()
};
name.push_str(&p.stat.comm);
let mut row = Vec::new();
match threads {
false => row.push(cell!(p.stat.pid)),
true => {
row.push(cell!(p.status.tgid));
row.push(cell!(p.status.pid));
}
};
row.push(cell!(p.stat.ppid));
if long {
}
match (long, perf) {
(_, false) => {},
(_, true) => {
let rss = p.status.vmrss.map(|m| (m / 1024).to_string()).unwrap_or("".to_owned());
let raw_time = p.stat.utime + p.stat.stime;
let second_utime = raw_time / hertz % 60;
let minute_utime = raw_time / minute_hertz % 60;
let hour_utime = raw_time / hour_hertz % 60;
let cputime = format!(
"{:02}:{:02}:{:02}",
hour_utime,
minute_utime,
second_utime
);
row.push(cell!(rss));
row.push(cell!(cputime));
}
}
row.push(cell!(name));
if long {
row.push(cell!(p.cmdline.join(" ")));
}
Row::new(row)
}).collect::<Vec<_>>()
);
let mut titles = Vec::new();
titles.push(cell!("Pid"));
if threads {
titles.push(cell!("Tid"));
}
titles.push(cell!("Ppid"));
// TODO: Possible remove Ppid from when long is false,
// and have Cmd/Args as separate columns for long.
match (long, perf) {
(_, false) =>
titles.extend_from_slice(&[cell!("Cmd")]),
(_, true) =>
titles.extend_from_slice(&[cell!("RSS"), cell!("Time"), cell!("Cmd")])
};
if long {
titles.push(cell!("Cmdline"));
}
table.set_titles(Row::new(titles));
table.set_format(
FormatBuilder::new()
.column_separator(' ')
.build()
);
table.printstd();
}
// Given a vector of Pid structs, treeify their names, and return them in the right order.
// This is similar to ps -AH.
fn treeify_names(pids: Vec<Pid>, name_indents: &mut HashMap<TaskId, String>) -> Vec<Pid> {
let mut child_pids = HashMap::new();
for pid in pids {
let ppid = pid.stat.ppid;
child_pids.entry(ppid)
.or_insert(Vec::new())
.push(pid);
}
enumerate_children(0, &mut child_pids, name_indents, -1)
}
// Enumerate children pids, and return them.
fn enumerate_children(pid: TaskId, child_pids: &mut HashMap<TaskId, Vec<Pid>>,
name_indents: &mut HashMap<TaskId, String>, indent: i32) -> Vec<Pid> {
name_indents.insert(pid,
match indent {
i if i >= 0 =>
repeat(" ").take(i as usize).collect::<String>(),
_ => "".to_owned()
}
);
let mut pids = Vec::new();
let ppids = match child_pids.remove(&pid) {
Some(v) => v,
None => { return pids; }
};
for pid in ppids {
let pid_num = pid.stat.pid;
pids.push(pid);
pids.append(
&mut enumerate_children(pid_num, child_pids, name_indents, indent + 1)
);
}
pids
}
struct ProgOpts {
query: PidQuery,
tree: bool,
threads: bool,
perf: bool,
long: bool,
verbose: bool
}
fn parse_args() -> ProgOpts {
let mut opts = ProgOpts {
query: PidQuery::NoneQuery,
tree: false,
threads: false,
perf: false,
long: false,
verbose: false
};
{
let mut ap = ArgumentParser::new();
ap.set_description("Query linux processes");
ap.refer(&mut opts.threads)
.add_option(&["-t", "--threads"], StoreTrue, "Display individual threads");
ap.refer(&mut opts.tree)
.add_option(&["-T", "--tree"], StoreTrue, "Display commands in tree hierarchy");
ap.refer(&mut opts.perf)
.add_option(&["-p", "--perf"], StoreTrue, "Display columns about performance");
ap.refer(&mut opts.long)
.add_option(&["-l", "--long"], StoreTrue, "Display columns with more information");
ap.refer(&mut opts.verbose)
.add_option(&["-v", "--verbose"], StoreTrue, "Verbose output");
ap.refer(&mut opts.query)
.add_argument("query", Store, "Optional query to search by, pid or string");
ap.parse_args_or_exit();
}
opts
}