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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//! Process monitoring
use serde::{Deserialize, Serialize};
/// Process information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessInfo {
/// Process ID
pub pid: u32,
/// User running the process
pub user: String,
/// GPU used (I = Integrated, dX = Discrete)
pub gpu: String,
/// Process type (Graphic, System, etc.), or `None` where it was not read.
pub process_type: Option<String>,
/// Priority
pub priority: i32,
/// Process state (R, S, D, Z, T, etc.), or `None` where it was not read.
/// Windows used to report `R` for any process with a thread, which was
/// every process.
pub state: Option<char>,
/// CPU usage percentage, `None` where it was not read. Windows used to
/// report `0.0` when the process times could not be queried.
pub cpu_percent: Option<f32>,
/// Memory used in KB
pub memory_kb: u64,
/// GPU memory used in KB, `None` where it was not read. Windows never
/// filled this in; it was `0` and read as "no GPU memory".
pub gpu_memory_kb: Option<u64>,
/// Process name
pub name: String,
}
/// Process statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessStats {
/// GPU processes
pub processes: Vec<ProcessInfo>,
/// Total GPU memory used by all processes, `None` where the platform reader
/// did not measure it. `0` here used to mean both "no GPU memory" and "not
/// read".
pub total_gpu_memory_kb: Option<u64>,
}
impl ProcessStats {
/// A zeroed struct for a platform reader to fill in.
///
/// **This reads nothing.** It returns no processes and zero GPU memory, and it was called `new()`
/// until 6.0.0 — a name that reads like a constructor that gathers
/// data. Two GUI defects, one in the HTTP server and one in the
/// library's own `snapshot_process` came from exactly that misreading,
/// found over three separate occasions.
///
/// The real values come from the per-platform readers. Use those
/// unless you are a reader building your own starting struct.
pub fn empty() -> Self {
Self {
processes: Vec::new(),
total_gpu_memory_kb: None,
}
}
/// Get process count
pub fn process_count(&self) -> usize {
self.processes.len()
}
/// Get processes sorted by GPU memory usage
pub fn sorted_by_gpu_memory(&self) -> Vec<&ProcessInfo> {
let mut procs: Vec<&ProcessInfo> = self.processes.iter().collect();
// `None` (not read) is the smallest `Option`, so under `Reverse` it ranks
// after every measured figure.
procs.sort_by_key(|p| std::cmp::Reverse(p.gpu_memory_kb));
procs
}
/// Get processes sorted by CPU usage
pub fn sorted_by_cpu(&self) -> Vec<&ProcessInfo> {
let mut procs: Vec<&ProcessInfo> = self.processes.iter().collect();
procs.sort_by(|a, b| {
b.cpu_percent
.partial_cmp(&a.cpu_percent)
.unwrap_or(std::cmp::Ordering::Equal)
});
// `None` compares below every value, so unread CPU ranks last.
procs
}
}
impl Default for ProcessStats {
fn default() -> Self {
Self::empty()
}
}
#[cfg(target_os = "linux")]
pub(crate) mod linux {
use super::*;
use crate::error::IronError;
use crate::error::Result;
use std::fs;
use std::path::Path;
/// Read process statistics (Linux)
pub fn read_process_stats() -> Result<ProcessStats> {
let mut stats = ProcessStats::empty();
// Check if nvmap is available (Jetson)
let nvmap_path = "/sys/kernel/debug/nvmap/iovmm/maps";
if !Path::new(nvmap_path).exists() {
// Not a Jetson or nvmap not available. Nothing was read, so the
// total stays `None`: it used to be `0`, which is a measured "no GPU
// memory in use".
return Ok(stats);
}
// Parse nvmap table
let (total, process_table) = parse_nvmap_table(nvmap_path)?;
stats.total_gpu_memory_kb = total;
// Read uptime for CPU calculation
let uptime = read_uptime()?;
// Get detailed info for each process
for (pid, user, name, gpu_mem) in process_table {
if let Ok(proc_info) = get_process_info(pid, &user, &name, gpu_mem, uptime) {
stats.processes.push(proc_info);
}
}
Ok(stats)
}
/// (pid, name, kind, bytes) for one nvmap client.
///
/// Named because the bare tuple was long enough that clippy could not tell
/// what any position meant — and neither could a reader.
type NvmapRow = (u32, String, String, u64);
/// The total is `None` when its line is absent or unparseable. A client
/// whose size does not parse is left out rather than reported as holding
/// zero bytes of GPU memory.
fn parse_nvmap_table(path: &str) -> Result<(Option<u64>, Vec<NvmapRow>)> {
let content = fs::read_to_string(path)?;
let mut processes = Vec::new();
let mut total = None;
for line in content.lines() {
// Parse lines like: "user process PID sizeU"
let parts: Vec<&str> = line.split_whitespace().collect();
// The total line is checked before the field count: it may carry
// only the size, and the count check used to drop it.
if parts.first() == Some(&"total") {
if let Some(size_str) = parts.get(1) {
total = parse_size_with_unit(size_str);
}
continue;
}
if parts.len() >= 4 {
// Parse process line
if let Ok(pid) = parts[2].parse::<u32>() {
if let Some(size) = parse_size_with_unit(parts[3]) {
let user = parts[0].to_string();
let name = parts[1].to_string();
processes.push((pid, user, name, size));
}
}
}
}
Ok((total, processes))
}
/// Parse a size such as `1234K`, `5M` or `1G`. `None` when the number does
/// not parse. It used to be `0`, which read as a client holding no memory.
fn parse_size_with_unit(size_str: &str) -> Option<u64> {
// Security: Safely get last character without unwrap
let last_char = size_str.chars().last()?;
let (num_str, unit) = if last_char.is_alphabetic() {
let last_idx = size_str.len() - last_char.len_utf8();
(&size_str[..last_idx], last_char)
} else {
(size_str, 'K')
};
let num: u64 = num_str.parse().ok()?;
Some(match unit {
'K' | 'k' => num,
'M' | 'm' => num * 1024,
'G' | 'g' => num * 1024 * 1024,
_ => num,
})
}
fn read_uptime() -> Result<f64> {
let uptime_str = fs::read_to_string("/proc/uptime")?;
let uptime: f64 = uptime_str
.split_whitespace()
.next()
.ok_or_else(|| IronError::Parse("Invalid uptime format".to_string()))?
.parse()
.map_err(|e| IronError::Parse(format!("Failed to parse uptime: {}", e)))?;
Ok(uptime)
}
/// Parse one numeric field of `/proc/<pid>` as read. Errors rather than
/// returning a default: a caller would otherwise report 0 for a value it
/// never obtained.
fn parse_proc_field<T: std::str::FromStr>(text: &str, field: &str, pid: u32) -> Result<T> {
text.parse().map_err(|_| {
IronError::Parse(format!(
"/proc/{}: {} is not a number: {:?}",
pid, field, text
))
})
}
fn get_process_info(
pid: u32,
user: &str,
name: &str,
gpu_mem: u64,
uptime: f64,
) -> Result<ProcessInfo> {
let proc_path = format!("/proc/{}", pid);
// Check if process still exists
if !Path::new(&proc_path).exists() {
return Err(IronError::DeviceNotFound(format!(
"Process {} not found",
pid
)));
}
// Read /proc/[pid]/stat
let stat_content = fs::read_to_string(format!("{}/stat", proc_path))?;
let stat_parts: Vec<&str> = stat_content.split_whitespace().collect();
if stat_parts.len() < 22 {
return Err(IronError::Parse("Invalid stat format".to_string()));
}
// Extract fields (indices from man proc). A field that does not parse is
// an error: a zero CPU time would read as an idle process.
let state = stat_parts[2].chars().next().unwrap_or('?');
let utime: f64 = parse_proc_field(stat_parts[13], "utime", pid)?;
let stime: f64 = parse_proc_field(stat_parts[14], "stime", pid)?;
let priority: i32 = parse_proc_field(stat_parts[17], "priority", pid)?;
let starttime: f64 = parse_proc_field(stat_parts[21], "starttime", pid)?;
// Calculate CPU percentage
let clk_tck = crate::platform::linux::clock_ticks_per_second()
.ok_or_else(|| IronError::Other("sysconf(_SC_CLK_TCK) returned no tick rate".into()))?
as f64;
let total_time = (utime + stime) / clk_tck;
let proc_uptime = (uptime - (starttime / clk_tck)).max(1.0);
let cpu_percent = Some((100.0 * (total_time / proc_uptime)) as f32);
// Read /proc/[pid]/statm for memory
let statm_content = fs::read_to_string(format!("{}/statm", proc_path))?;
let statm_parts: Vec<&str> = statm_content.split_whitespace().collect();
// A missing or unparseable RSS is an error, not an empty process.
let rss_pages: u64 = match statm_parts.get(1) {
Some(&text) => parse_proc_field(text, "statm resident", pid)?,
None => {
return Err(IronError::Parse(format!(
"/proc/{}/statm has no resident field",
pid
)))
}
};
let vm_rss = rss_pages
* crate::platform::linux::page_size().ok_or_else(|| {
IronError::Other("sysconf(_SC_PAGESIZE) returned no page size".into())
})?
/ 1024;
Ok(ProcessInfo {
pid,
user: user.to_string(),
gpu: "I".to_string(), // Integrated GPU
// Not read. This was "Graphic" on every process, the same constant
// the Windows reader used to report as "System".
process_type: None,
priority,
state: Some(state),
cpu_percent,
memory_kb: vm_rss,
gpu_memory_kb: Some(gpu_mem),
name: name.to_string(),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn nvmap_sizes_are_parsed_or_refused() {
assert_eq!(parse_size_with_unit("1234K"), Some(1234));
assert_eq!(parse_size_with_unit("5M"), Some(5 * 1024));
assert_eq!(parse_size_with_unit("1G"), Some(1024 * 1024));
assert_eq!(parse_size_with_unit("77"), Some(77));
assert_eq!(parse_size_with_unit(""), None);
assert_eq!(parse_size_with_unit("K"), None);
assert_eq!(parse_size_with_unit("abcM"), None);
}
#[test]
fn a_client_with_an_unreadable_size_is_left_out_not_zeroed() {
let dir = std::env::temp_dir().join(format!(
"ironmon-nvmap-{}-{}",
std::process::id(),
"unreadable-size"
));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
let path = dir.join("maps");
let table = [
"user name PID size",
"total 2048K",
"root Xorg 100 512K",
"root broken 101 ?M",
]
.join("\n");
fs::write(&path, table).unwrap();
let (total, rows) = parse_nvmap_table(path.to_str().unwrap()).unwrap();
assert_eq!(total, Some(2048));
assert_eq!(
rows.len(),
1,
"the unparseable client must not appear as 0 KB"
);
assert_eq!(rows[0].0, 100);
let _ = fs::remove_dir_all(dir);
}
}
}