iowatchd 0.4.0

Monitors disk IO usage and logs which processes are consuming it
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Per-process IO: snapshots of `/proc/<pid>/io`, ranking and the alert report.

use std::collections::HashMap;
use std::fs;
use std::time::Instant;

use crate::config::Config;
use crate::disk::{read_pressure, Metrics, Pressure};
use crate::logger::{json_str, Logger};
use crate::signals::nap;

/// Bytes actually read from / written to storage by a process
/// (`read_bytes` and `write_bytes` from `/proc/<pid>/io`; page cache is ignored).
#[derive(Clone, Copy, Default, Debug, PartialEq)]
struct ProcIo {
    read: u64,
    write: u64,
}

/// Reads the IO counters of a PID. Returns `None` if the process exited or
/// we lack permission.
fn read_proc_io(pid: u32) -> Option<ProcIo> {
    parse_proc_io(&fs::read_to_string(format!("/proc/{pid}/io")).ok()?)
}

fn parse_proc_io(text: &str) -> Option<ProcIo> {
    let mut io = ProcIo::default();
    let mut found = false;
    for line in text.lines() {
        if let Some(v) = line.strip_prefix("read_bytes:") {
            io.read = v.trim().parse().ok()?;
            found = true;
        } else if let Some(v) = line.strip_prefix("write_bytes:") {
            io.write = v.trim().parse().ok()?;
        }
    }
    found.then_some(io)
}

/// Takes a "snapshot" of the IO counters of every visible process.
fn snapshot_processes() -> HashMap<u32, ProcIo> {
    let mut map = HashMap::new();
    let Ok(dir) = fs::read_dir("/proc") else {
        return map;
    };
    for entry in dir.flatten() {
        // Numeric directories under /proc are PIDs.
        if let Some(pid) = entry
            .file_name()
            .to_str()
            .and_then(|s| s.parse::<u32>().ok())
        {
            if let Some(io) = read_proc_io(pid) {
                map.insert(pid, io);
            }
        }
    }
    map
}

/// IO done by a process during the measurement window.
struct ProcUsage {
    pid: u32,
    read: u64,
    write: u64,
}

/// Compares two snapshots and returns the `n` processes that did the most IO,
/// in descending order (reads + writes).
fn top_processes(
    before: &HashMap<u32, ProcIo>,
    after: &HashMap<u32, ProcIo>,
    n: usize,
) -> Vec<ProcUsage> {
    let mut usage: Vec<ProcUsage> = after
        .iter()
        .map(|(&pid, now)| {
            // Process started during the window: counts from zero.
            let old = before.get(&pid).copied().unwrap_or_default();
            ProcUsage {
                pid,
                read: now.read.saturating_sub(old.read),
                write: now.write.saturating_sub(old.write),
            }
        })
        .filter(|u| u.read + u.write > 0)
        .collect();
    usage.sort_by_key(|u| std::cmp::Reverse(u.read + u.write));
    usage.truncate(n);
    usage
}

/// Short process name (`/proc/<pid>/comm`).
fn process_name(pid: u32) -> String {
    fs::read_to_string(format!("/proc/{pid}/comm"))
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|_| "?".into())
}

/// Full command line, truncated so it does not clutter the log.
fn process_cmdline(pid: u32) -> String {
    let raw = fs::read(format!("/proc/{pid}/cmdline")).unwrap_or_default();
    let text: String = String::from_utf8_lossy(&raw)
        .replace('\0', " ")
        .trim()
        .to_string();
    if text.chars().count() > 100 {
        let cut: String = text.chars().take(100).collect();
        format!("{cut}...")
    } else {
        text
    }
}

/// Process owner: the UID from `/proc/<pid>/status` translated via `/etc/passwd`.
fn process_user(pid: u32, users: &HashMap<u32, String>) -> String {
    let uid = fs::read_to_string(format!("/proc/{pid}/status"))
        .ok()
        .and_then(|s| {
            s.lines()
                .find_map(|l| l.strip_prefix("Uid:"))
                .and_then(|v| {
                    v.split_whitespace()
                        .next()
                        .and_then(|u| u.parse::<u32>().ok())
                })
        });
    match uid {
        Some(u) => users.get(&u).cloned().unwrap_or_else(|| u.to_string()),
        None => "?".into(),
    }
}

/// UID -> name map built from `/etc/passwd`.
fn load_users() -> HashMap<u32, String> {
    fs::read_to_string("/etc/passwd")
        .unwrap_or_default()
        .lines()
        .filter_map(|l| {
            let mut f = l.split(':');
            let name = f.next()?;
            let uid = f.nth(1)?.parse().ok()?;
            Some((uid, name.to_string()))
        })
        .collect()
}

/// Formats bytes/s in a human-readable way.
pub fn human_rate(bytes_per_s: f64) -> String {
    const MB: f64 = 1024.0 * 1024.0;
    if bytes_per_s >= MB {
        format!("{:.1}MB/s", bytes_per_s / MB)
    } else {
        format!("{:.0}KB/s", bytes_per_s / 1024.0)
    }
}

/// Extracts the systemd unit / container scope from the contents of
/// `/proc/<pid>/cgroup`, e.g. `app-brave@1234.service` or `docker-1a2b3c4d5e6f`.
/// Returns an empty string when the process is in the root cgroup.
fn parse_cgroup_unit(text: &str) -> String {
    // cgroup v2 has a single `0::<path>` line; v1 lists several hierarchies.
    let path = text
        .lines()
        .find_map(|l| l.strip_prefix("0::"))
        .or_else(|| {
            text.lines()
                .find(|l| l.contains("name=systemd:"))
                .and_then(|l| l.split_once("name=systemd:").map(|(_, p)| p))
        })
        .unwrap_or("");
    let parts: Vec<&str> = path.split('/').filter(|p| !p.is_empty()).collect();
    // The outermost service/scope names the application (inner ones are
    // often anonymous, like terminal tabs); `user@N.service` is only the
    // per-user manager.
    let unit = parts
        .iter()
        .find(|p| (p.ends_with(".service") || p.ends_with(".scope")) && !p.starts_with("user@"))
        .or(parts.last());
    unit.map(|u| shorten_unit(u)).unwrap_or_default()
}

/// Drops the `.service`/`.scope` suffix and abbreviates container IDs
/// (hex strings of 32+ characters) to 12 characters.
fn shorten_unit(unit: &str) -> String {
    let base = unit
        .strip_suffix(".service")
        .or_else(|| unit.strip_suffix(".scope"))
        .unwrap_or(unit);
    base.split('-')
        .map(|piece| {
            if piece.len() >= 32 && piece.bytes().all(|b| b.is_ascii_hexdigit()) {
                &piece[..12]
            } else {
                piece
            }
        })
        .collect::<Vec<_>>()
        .join("-")
}

/// The service or container a process belongs to.
fn process_unit(pid: u32) -> String {
    fs::read_to_string(format!("/proc/{pid}/cgroup"))
        .map(|t| parse_cgroup_unit(&t))
        .unwrap_or_default()
}

/// The disk-level details shown in the alert header, e.g.
/// ` (await=12.3ms queue=1.5 read=0KB/s write=48KB/s psi=3.2%)`.
fn metrics_text(m: &Metrics, psi: Option<Pressure>) -> String {
    let mut parts = Vec::new();
    if let Some(a) = m.await_ms {
        parts.push(format!("await={a:.1}ms"));
    }
    parts.push(format!("queue={:.1}", m.queue_depth));
    parts.push(format!("read={}", human_rate(m.read_bytes_per_s)));
    parts.push(format!("write={}", human_rate(m.write_bytes_per_s)));
    if let Some(p) = psi {
        parts.push(format!("psi={:.1}%", p.some_avg10));
    }
    format!(" ({})", parts.join(" "))
}

/// The same details as JSON members (without braces).
fn metrics_json(m: &Metrics, psi: Option<Pressure>) -> String {
    let mut out = format!(
        "\"queue_depth\":{:.2},\"disk_read_bytes_per_s\":{:.0},\"disk_write_bytes_per_s\":{:.0}",
        m.queue_depth, m.read_bytes_per_s, m.write_bytes_per_s
    );
    if let Some(a) = m.await_ms {
        out.push_str(&format!(",\"await_ms\":{a:.2}"));
    }
    if let Some(p) = psi {
        out.push_str(&format!(
            ",\"psi_some_avg10\":{:.2},\"psi_full_avg10\":{:.2}",
            p.some_avg10, p.full_avg10
        ));
    }
    out
}

/// One-line disk details for samples (`--once`, `--verbose`).
pub fn sample_text(m: &Metrics) -> String {
    metrics_text(m, read_pressure())
}

/// Per-process IO of other users is only readable by root (or with
/// CAP_SYS_PTRACE). Returns a warning when that is not the case.
pub fn unprivileged_warning() -> Option<&'static str> {
    extern "C" {
        fn geteuid() -> u32;
    }
    // SAFETY: plain libc call, no arguments.
    let root = unsafe { geteuid() } == 0;
    // Ambient capabilities (the systemd unit) also work: probe PID 1.
    let can_read_pid1 = fs::read_to_string("/proc/1/io").is_ok();
    (!root && !can_read_pid1)
        .then_some("not running as root: only your own processes can be inspected")
}

/// Measures per-process IO over the configured window and writes the report
/// to the log. Returns the logged message (for the notification hook).
pub fn report_top_processes(cfg: &Config, logger: &Logger, m: &Metrics) -> String {
    let util = m.util;
    let window = cfg.process_window;
    let before = snapshot_processes();
    let start = Instant::now();
    nap(window);
    let after = snapshot_processes();
    // Use the real elapsed time so the rates are accurate.
    let secs = start.elapsed().as_secs_f64().max(0.001);
    let psi = read_pressure();
    let head = format!(
        "ALERT IO={util:.0}% on {}{}",
        cfg.device,
        metrics_text(m, psi)
    );
    let base_json = format!(
        "\"device\":{},\"util\":{util:.0},{}",
        json_str(&cfg.device),
        metrics_json(m, psi)
    );

    let top = top_processes(&before, &after, cfg.top_n);
    if top.is_empty() {
        let hint = if unprivileged_warning().is_some() {
            "run as root to see other users' processes"
        } else {
            "kernel/journal IO?"
        };
        let msg = format!("{head} | no visible process did IO in the window ({hint})");
        logger.emit("alert", &msg, &format!("{base_json},\"processes\":[]"));
        return msg;
    }

    let users = load_users();
    let mut list = Vec::new();
    let mut json_list = Vec::new();
    for (i, u) in top.iter().enumerate() {
        let (name, user, unit, cmd) = (
            process_name(u.pid),
            process_user(u.pid, &users),
            process_unit(u.pid),
            process_cmdline(u.pid),
        );
        let (read, write) = (u.read as f64 / secs, u.write as f64 / secs);
        let unit_text = if unit.is_empty() {
            String::new()
        } else {
            format!(" unit={unit}")
        };
        list.push(format!(
            "{}) {} pid={} user={}{} R={} W={} cmd=\"{}\"",
            i + 1,
            name,
            u.pid,
            user,
            unit_text,
            human_rate(read),
            human_rate(write),
            cmd
        ));
        json_list.push(format!(
            "{{\"pid\":{},\"name\":{},\"user\":{},\"unit\":{},\"read_bytes_per_s\":{:.0},\"write_bytes_per_s\":{:.0},\"cmd\":{}}}",
            u.pid,
            json_str(&name),
            json_str(&user),
            json_str(&unit),
            read,
            write,
            json_str(&cmd)
        ));
    }
    let msg = format!("{head} | {}", list.join(" | "));
    logger.emit(
        "alert",
        &msg,
        &format!("{base_json},\"processes\":[{}]", json_list.join(",")),
    );
    msg
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cgroup_unit_is_extracted() {
        assert_eq!(
            parse_cgroup_unit(
                "0::/user.slice/user-1000.slice/user@1000.service/app.slice/app-brave@1.service\n"
            ),
            "app-brave@1"
        );
        assert_eq!(
            parse_cgroup_unit("0::/user.slice/user-1000.slice/user@1000.service/app.slice/app-org.kde.konsole-1521.scope/tab(1529).scope\n"),
            "app-org.kde.konsole-1521"
        );
        assert_eq!(
            parse_cgroup_unit(&format!(
                "0::/system.slice/docker-{}.scope\n",
                "a1b2c3d4e5f6".repeat(5)
            )),
            "docker-a1b2c3d4e5f6"
        );
        assert_eq!(parse_cgroup_unit("0::/\n"), "");
        assert_eq!(
            parse_cgroup_unit("12:cpu:/x\n1:name=systemd:/system.slice/cron.service\n"),
            "cron"
        );
        assert_eq!(parse_cgroup_unit(""), "");
    }

    #[test]
    fn proc_io_reads_real_bytes() {
        let t = "rchar: 10\nwchar: 20\nread_bytes: 4096\nwrite_bytes: 8192\n";
        assert_eq!(
            parse_proc_io(t),
            Some(ProcIo {
                read: 4096,
                write: 8192
            })
        );
        assert_eq!(parse_proc_io("rchar: 1\n"), None);
    }

    #[test]
    fn top_sorts_and_ignores_processes_without_io() {
        let before: HashMap<u32, ProcIo> = [(1, ProcIo { read: 0, write: 0 })].into();
        let after: HashMap<u32, ProcIo> = [
            (1, ProcIo { read: 0, write: 0 }),
            (
                2,
                ProcIo {
                    read: 100,
                    write: 0,
                },
            ),
            (
                3,
                ProcIo {
                    read: 0,
                    write: 900,
                },
            ),
        ]
        .into();
        let top = top_processes(&before, &after, 5);
        assert_eq!(top.iter().map(|u| u.pid).collect::<Vec<_>>(), vec![3, 2]);
        assert_eq!(top_processes(&before, &after, 1).len(), 1);
    }
}