forked from nushell/nushell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps.rs
212 lines (196 loc) · 7.09 KB
/
ps.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
#[cfg(windows)]
use itertools::Itertools;
use nu_engine::CallExt;
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
))]
use nu_protocol::Span;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature,
Type, Value,
};
#[cfg(all(
unix,
not(target_os = "freebsd"),
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
))]
use procfs::WithCurrentSystemInfo;
use std::time::Duration;
#[derive(Clone)]
pub struct Ps;
#[cfg(not(target_os = "freebsd"))]
impl Command for Ps {
fn name(&self) -> &str {
"ps"
}
fn signature(&self) -> Signature {
Signature::build("ps")
.input_output_types(vec![(Type::Nothing, Type::Table(vec![]))])
.switch(
"long",
"list all available columns for each entry",
Some('l'),
)
.filter()
.category(Category::System)
}
fn usage(&self) -> &str {
"View information about system processes."
}
fn search_terms(&self) -> Vec<&str> {
vec!["procedures", "operations", "tasks", "ops"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
run_ps(engine_state, stack, call)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "List the system processes",
example: "ps",
result: None,
},
Example {
description: "List the top 5 system processes with the highest memory usage",
example: "ps | sort-by mem | last 5",
result: None,
},
Example {
description: "List the top 3 system processes with the highest CPU usage",
example: "ps | sort-by cpu | last 3",
result: None,
},
Example {
description: "List the system processes with 'nu' in their names",
example: "ps | where name =~ 'nu'",
result: None,
},
Example {
description: "Get the parent process id of the current nu process",
example: "ps | where pid == $nu.pid | get ppid",
result: None,
},
]
}
}
#[cfg(not(target_os = "freebsd"))]
fn run_ps(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
) -> Result<PipelineData, ShellError> {
let mut output = vec![];
let span = call.head;
let long = call.has_flag(engine_state, stack, "long")?;
for proc in nu_system::collect_proc(Duration::from_millis(100), false) {
let mut record = Record::new();
record.push("pid", Value::int(proc.pid() as i64, span));
record.push("ppid", Value::int(proc.ppid() as i64, span));
record.push("name", Value::string(proc.name(), span));
#[cfg(not(windows))]
{
// Hide status on Windows until we can find a good way to support it
record.push("status", Value::string(proc.status(), span));
}
record.push("cpu", Value::float(proc.cpu_usage(), span));
record.push("mem", Value::filesize(proc.mem_size() as i64, span));
record.push("virtual", Value::filesize(proc.virtual_size() as i64, span));
if long {
record.push("command", Value::string(proc.command(), span));
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "windows"),
not(target_os = "android"),
))]
{
let proc_stat = proc
.curr_proc
.stat()
.map_err(|e| ShellError::GenericError {
error: "Error getting process stat".into(),
msg: e.to_string(),
span: Some(Span::unknown()),
help: None,
inner: vec![],
})?;
// If we can't get the start time, just use the current time
let proc_start = proc_stat
.starttime()
.get()
.unwrap_or_else(|_| chrono::Local::now());
record.push("start_time", Value::date(proc_start.into(), span));
record.push("user_id", Value::int(proc.curr_proc.owner() as i64, span));
// These work and may be helpful, but it just seemed crowded
// record.push("group_id", Value::int(proc_stat.pgrp as i64, span));
// record.push("session_id", Value::int(proc_stat.session as i64, span));
// This may be helpful for ctrl+z type of checking, once we get there
// record.push("tpg_id", Value::int(proc_stat.tpgid as i64, span));
record.push("priority", Value::int(proc_stat.priority, span));
record.push("process_threads", Value::int(proc_stat.num_threads, span));
record.push("cwd", Value::string(proc.cwd(), span));
}
#[cfg(windows)]
{
//TODO: There's still more information we can cram in there if we want to
// see the ProcessInfo struct for more information
record.push(
"start_time",
Value::date(proc.start_time.fixed_offset(), span),
);
record.push(
"user",
Value::string(
proc.user.clone().name.unwrap_or("unknown".to_string()),
span,
),
);
record.push(
"user_sid",
Value::string(
proc.user
.clone()
.sid
.iter()
.map(|r| r.to_string())
.join("-"),
span,
),
);
record.push("priority", Value::int(proc.priority as i64, span));
record.push("cwd", Value::string(proc.cwd(), span));
record.push(
"environment",
Value::list(
proc.environ()
.iter()
.map(|x| Value::string(x.to_string(), span))
.collect(),
span,
),
);
}
#[cfg(target_os = "macos")]
{
record.push("cwd", Value::string(proc.cwd(), span));
}
}
output.push(Value::record(record, span));
}
Ok(output
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
}