syswatch 0.8.0

Single-host, read-only system diagnostics TUI. Twelve tabs covering CPU, memory, disks, processes, GPU, power, services, network, plus a Timeline scrubber and an Insights anomaly engine. Sibling to netwatch.
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
414
415
416
417
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::Paragraph,
    Frame,
};

use crate::app::{App, Snapshot};
use crate::collect::ProcTick;
use crate::ui::{
    graph::GraphStyle,
    palette as p,
    widgets::{block_bar_styled, human_bytes, mem_pct, panel},
};

pub fn draw(f: &mut Frame, area: Rect, app: &App, snap: &Snapshot) {
    // Drop the SWAP panel entirely when no swap is configured — those 7 rows
    // go to the process list instead (issue #12).
    let has_swap = snap.mem.swap_total_bytes > 0;
    // The filter strip costs a process row, so it only appears once there
    // is a filter to show or edit — same reasoning as issue #12. The key
    // itself is advertised in the footer and help popup (issue #20).
    let show_filter = app.filter_input || app.filter_active.is_some();

    let mut constraints = vec![Constraint::Length(7)];
    if has_swap {
        constraints.push(Constraint::Length(7));
    }
    if show_filter {
        constraints.push(Constraint::Length(1));
    }
    constraints.push(Constraint::Min(0));

    let v = Layout::default()
        .direction(Direction::Vertical)
        .constraints(constraints)
        .split(area);

    let mut row = 0;
    draw_ram_bar(f, v[row], snap, app.graph_style);
    row += 1;
    if has_swap {
        draw_swap(f, v[row], snap, app.graph_style);
        row += 1;
    }
    if show_filter {
        draw_filter_strip(f, v[row], app, snap);
        row += 1;
    }
    draw_proc_breakdown(f, v[row], app, snap);
}

/// Filter prompt while typing; match count once applied.
fn draw_filter_strip(f: &mut Frame, area: Rect, app: &App, snap: &Snapshot) {
    let line = if app.filter_input {
        crate::ui::widgets::filter_input_line(&app.filter_buf)
    } else {
        let needle = app.filter_active.as_deref().unwrap_or_default();
        Line::from(Span::styled(
            format!(
                " {}/{} procs  filter: \"{}\"   / f:edit",
                filtered_procs(&snap.procs, Some(needle)).len(),
                snap.procs.len(),
                needle
            ),
            Style::default().fg(p::brand()),
        ))
    };
    f.render_widget(
        Paragraph::new(line).style(Style::default().bg(p::bg())),
        area,
    );
}

/// Narrow the process list by the shared filter, preserving input order —
/// the breakdown does its own memory-weighted sort afterwards, so unlike
/// the Procs tab this must not impose one.
fn filtered_procs(procs: &[ProcTick], filter: Option<&str>) -> Vec<ProcTick> {
    let needle = filter.map(|s| s.to_lowercase());
    procs
        .iter()
        .filter(|p| match needle.as_deref() {
            None => true,
            Some(n) => crate::tabs::procs::proc_matches(p, n),
        })
        .cloned()
        .collect()
}

fn draw_ram_bar(f: &mut Frame, area: Rect, snap: &Snapshot, style: GraphStyle) {
    let block = panel("RAM");
    let inner = block.inner(area);
    f.render_widget(block, area);

    let total = snap.mem.total_bytes.max(1);
    let used = snap.mem.used_bytes;
    let avail = snap.mem.available_bytes;
    let pct = used as f32 / total as f32;
    let color = if pct >= 0.9 {
        p::status_error()
    } else if pct >= 0.7 {
        p::status_warn()
    } else {
        p::status_good()
    };

    let mut header_spans = vec![
        Span::styled("used ", Style::default().fg(p::text_muted())),
        Span::styled(
            human_bytes(used),
            Style::default().fg(color).add_modifier(Modifier::BOLD),
        ),
        Span::styled(" / ", Style::default().fg(p::text_muted())),
        Span::styled(human_bytes(total), Style::default().fg(p::text_primary())),
        Span::styled(
            format!("   ({:>4.1}%)", pct * 100.0),
            Style::default().fg(p::text_muted()),
        ),
        Span::styled("    available ", Style::default().fg(p::text_muted())),
        Span::styled(human_bytes(avail), Style::default().fg(p::text_primary())),
    ];
    // PSI: stall time is the honest pressure signal — %used can be
    // high while nothing is starved, and vice versa.
    if let Some(psi) = &snap.pressure {
        let stall_color = if psi.mem_full >= 5.0 {
            p::status_error()
        } else if psi.mem_some >= 10.0 {
            p::status_warn()
        } else {
            p::text_primary()
        };
        header_spans.push(Span::styled(
            "    stall ",
            Style::default().fg(p::text_muted()),
        ));
        header_spans.push(Span::styled(
            format!("{:.1}% some / {:.1}% full", psi.mem_some, psi.mem_full),
            Style::default().fg(stall_color),
        ));
    }
    let header = Line::from(header_spans);
    let bar = block_bar_styled(pct, inner.width, color, style);
    f.render_widget(
        Paragraph::new(vec![header, Line::from(""), bar]).style(Style::default().bg(p::bg())),
        inner,
    );
}

fn draw_swap(f: &mut Frame, area: Rect, snap: &Snapshot, style: GraphStyle) {
    let block = panel("Swap");
    let inner = block.inner(area);
    f.render_widget(block, area);

    let total = snap.mem.swap_total_bytes;
    let used = snap.mem.swap_used_bytes;
    let pct = if total > 0 {
        used as f32 / total as f32
    } else {
        0.0
    };
    let color = if pct >= 0.75 {
        p::status_error()
    } else if pct >= 0.25 {
        p::status_warn()
    } else {
        p::status_good()
    };

    let header = if total == 0 {
        Line::from(vec![Span::styled(
            "no swap configured",
            Style::default().fg(p::text_muted()),
        )])
    } else {
        Line::from(vec![
            Span::styled("used ", Style::default().fg(p::text_muted())),
            Span::styled(
                human_bytes(used),
                Style::default().fg(color).add_modifier(Modifier::BOLD),
            ),
            Span::styled(" / ", Style::default().fg(p::text_muted())),
            Span::styled(human_bytes(total), Style::default().fg(p::text_primary())),
            Span::styled(
                format!("   ({:>4.1}%)", pct * 100.0),
                Style::default().fg(p::text_muted()),
            ),
        ])
    };
    let bar = block_bar_styled(pct, inner.width, color, style);
    f.render_widget(
        Paragraph::new(vec![header, Line::from(""), bar]).style(Style::default().bg(p::bg())),
        inner,
    );
}

/// Per-process memory breakdown. RSS double-counts shared pages, so
/// where the platform sampler delivered honest accounting we lead with
/// it: phys_footprint on macOS (Activity Monitor's Memory column),
/// PSS + private/shared/swap from smaps_rollup on Linux. Detail is
/// sampled for the top procs by RSS and only for procs the user can
/// inspect without sudo — rows without it show "—" and fall back to
/// RSS-based ordering.
fn draw_proc_breakdown(f: &mut Frame, area: Rect, app: &App, snap: &Snapshot) {
    let total_mem = snap.mem.total_bytes.max(1);

    // Column sets keyed off what the sampler could actually read this
    // session — same pattern as the procs tab's NET / GPU columns.
    let show_footprint = snap.procs.iter().any(|p| p.mem_footprint.is_some());
    let show_smaps = snap.procs.iter().any(|p| p.mem_pss.is_some());

    let block = panel(if show_footprint {
        "Process memory breakdown (footprint = real pressure; rss double-counts shared)"
    } else if show_smaps {
        "Process memory breakdown (pss sums to real use; private frees on exit)"
    } else {
        "Process memory breakdown (by RSS)"
    });
    let inner = block.inner(area);
    f.render_widget(block, area);

    // Order by the most honest metric available per row, falling back
    // to RSS so undetailed rows still rank sensibly.
    let mut sorted = filtered_procs(&snap.procs, app.filter_active.as_deref());
    sorted.sort_by_key(|p| std::cmp::Reverse(p.mem_footprint.or(p.mem_pss).unwrap_or(p.mem_rss)));
    let take = inner.height.saturating_sub(1) as usize;

    let header_style = Style::default()
        .fg(p::text_muted())
        .add_modifier(Modifier::BOLD);
    let mut header: Vec<Span> = vec![
        Span::styled(format!("{:>7} ", "PID"), header_style),
        Span::styled(format!("{:<10} ", "USER"), header_style),
        // Width 5 to match the `{:>5.1}` data cell — see issue #11.
        Span::styled(format!("{:>5} ", "%MEM"), header_style),
    ];
    if show_footprint {
        header.push(Span::styled(format!("{:>10} ", "FOOTPRNT"), header_style));
    }
    if show_smaps {
        header.push(Span::styled(format!("{:>10} ", "PSS"), header_style));
        header.push(Span::styled(format!("{:>10} ", "PRIVATE"), header_style));
        header.push(Span::styled(format!("{:>10} ", "SHARED"), header_style));
        header.push(Span::styled(format!("{:>10} ", "SWAP"), header_style));
    }
    let show_peak = snap.procs.iter().any(|p| p.mem_peak.is_some());
    if show_peak {
        header.push(Span::styled(format!("{:>10} ", "PEAK"), header_style));
    }
    header.push(Span::styled(format!("{:>10} ", "RSS"), header_style));
    if !show_footprint && !show_smaps {
        header.push(Span::styled(format!("{:>10} ", "VIRT"), header_style));
    }
    header.push(Span::styled("COMMAND", header_style));
    let mut lines: Vec<Line> = vec![Line::from(header)];

    // None → "—": the proc exists but detail wasn't readable (other
    // user without sudo) or it ranked below the sampler's top-N cap.
    let detail = |v: Option<u64>| match v {
        Some(b) => human_bytes(b),
        None => "".into(),
    };
    let detail_fg = |v: Option<u64>| {
        if v.is_some() {
            p::brand()
        } else {
            p::text_muted()
        }
    };

    let rendered_rows = sorted.iter().take(take).count();
    for (i, proc_) in sorted.iter().take(take).enumerate() {
        let row_alpha = if app.user_config.graph_fade {
            crate::ui::graph::row_fade_alpha(i, rendered_rows)
        } else {
            1.0
        };
        let mut spans = vec![
            Span::styled(
                format!("{:>7} ", proc_.pid),
                Style::default().fg(p::text_primary()),
            ),
            Span::styled(
                format!("{:<10.10} ", proc_.user),
                Style::default().fg(p::text_muted()),
            ),
            Span::styled(
                format!("{:>5.1} ", mem_pct(proc_.mem_rss, total_mem)),
                Style::default().fg(p::text_primary()),
            ),
        ];
        if show_footprint {
            spans.push(Span::styled(
                format!("{:>10} ", detail(proc_.mem_footprint)),
                Style::default().fg(detail_fg(proc_.mem_footprint)),
            ));
        }
        if show_smaps {
            for v in [
                proc_.mem_pss,
                proc_.mem_private,
                proc_.mem_shared,
                proc_.mem_swap,
            ] {
                spans.push(Span::styled(
                    format!("{:>10} ", detail(v)),
                    Style::default().fg(detail_fg(v)),
                ));
            }
        }
        if show_peak {
            spans.push(Span::styled(
                format!("{:>10} ", detail(proc_.mem_peak)),
                Style::default().fg(detail_fg(proc_.mem_peak)),
            ));
        }
        spans.push(Span::styled(
            format!("{:>10} ", human_bytes(proc_.mem_rss)),
            Style::default().fg(if show_footprint || show_smaps {
                p::text_muted()
            } else {
                p::brand()
            }),
        ));
        if !show_footprint && !show_smaps {
            spans.push(Span::styled(
                format!("{:>10} ", human_bytes(proc_.mem_virt)),
                Style::default().fg(p::text_muted()),
            ));
        }
        spans.push(Span::styled(
            proc_.name.clone(),
            Style::default().fg(p::text_primary()),
        ));
        let spans = if (row_alpha - 1.0).abs() < f32::EPSILON {
            spans
        } else {
            crate::ui::graph::fade_spans_fg(spans, p::bg(), row_alpha)
        };
        lines.push(Line::from(spans));
    }
    f.render_widget(
        Paragraph::new(lines).style(Style::default().bg(p::bg())),
        inner,
    );
}

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

    fn proc(pid: u32, name: &str, cmd: &str, user: &str, rss: u64) -> ProcTick {
        ProcTick {
            pid,
            name: name.into(),
            cmd: cmd.into(),
            user: user.into(),
            mem_rss: rss,
            ..Default::default()
        }
    }

    fn fixture() -> Vec<ProcTick> {
        vec![
            proc(1, "kernel_task", "", "root", 900),
            proc(
                2,
                "Chrome Helper",
                "/Applications/Chrome --renderer",
                "matt",
                500,
            ),
            proc(3, "postgres", "postgres: writer", "postgres", 300),
        ]
    }

    fn names(v: &[ProcTick]) -> Vec<&str> {
        v.iter().map(|p| p.name.as_str()).collect()
    }

    // ── filter (issue #20) ──────────────────────────────────────────────

    #[test]
    fn no_filter_keeps_every_proc() {
        assert_eq!(filtered_procs(&fixture(), None).len(), 3);
    }

    #[test]
    fn filter_matches_name_cmd_or_user_case_insensitively() {
        assert_eq!(
            names(&filtered_procs(&fixture(), Some("CHROME"))),
            ["Chrome Helper"]
        );
        assert_eq!(
            names(&filtered_procs(&fixture(), Some("renderer"))),
            ["Chrome Helper"]
        );
        assert_eq!(
            names(&filtered_procs(&fixture(), Some("root"))),
            ["kernel_task"]
        );
    }

    /// The breakdown re-sorts by footprint/PSS/RSS afterwards, so the
    /// filter must leave input order alone — imposing the Procs tab's
    /// sort here would silently reorder the memory ranking.
    #[test]
    fn filter_preserves_input_order() {
        let out = filtered_procs(&fixture(), Some("e"));
        assert_eq!(names(&out), ["kernel_task", "Chrome Helper", "postgres"]);
    }

    #[test]
    fn filter_with_no_matches_is_empty() {
        assert!(filtered_procs(&fixture(), Some("zzz")).is_empty());
    }
}