Skip to main content

mtop/
model.rs

1use std::collections::{HashMap, HashSet};
2use std::sync::Arc;
3use std::time::{Duration, Instant};
4
5use crate::cgroup::CgroupMem;
6use crate::procfs::{MemInfo, Pressure, SmapsReport};
7
8#[derive(Clone, Copy, PartialEq, Eq, Debug)]
9pub enum SortKey {
10    Rss,
11    Pss,
12    Uss,
13    Delta,
14    Ratio,
15    Pid,
16    Comm,
17}
18
19impl SortKey {
20    pub const ALL: [SortKey; 7] = [
21        SortKey::Rss,
22        SortKey::Pss,
23        SortKey::Uss,
24        SortKey::Delta,
25        SortKey::Ratio,
26        SortKey::Pid,
27        SortKey::Comm,
28    ];
29
30    pub fn label(self) -> &'static str {
31        match self {
32            SortKey::Rss => "RSS",
33            SortKey::Pss => "PSS",
34            SortKey::Uss => "USS",
35            SortKey::Delta => "DELTA",
36            SortKey::Ratio => "PSS/RSS",
37            SortKey::Pid => "PID",
38            SortKey::Comm => "COMM",
39        }
40    }
41
42    pub fn next(self) -> SortKey {
43        let i = Self::ALL.iter().position(|k| *k == self).unwrap_or(0);
44        Self::ALL[(i + 1) % Self::ALL.len()]
45    }
46}
47
48#[derive(Clone, Debug)]
49pub struct ProcRow {
50    pub pid: u32,
51    pub comm: String,
52    /// kB, from statm (cheap, every tick).
53    pub rss_kb: u64,
54    /// MB/s-ish: kB change since previous sweep.
55    pub delta_kb: i64,
56}
57
58#[derive(Clone, Copy, Debug)]
59pub struct PssInfo {
60    pub rollup: crate::procfs::Rollup,
61    pub at: Instant,
62}
63
64#[derive(Clone, Debug)]
65pub struct Detail {
66    pub pid: u32,
67    pub comm: String,
68    pub cmdline: String,
69    pub report: SmapsReport,
70    /// Process-level rollup, the only source of the anon/file/shmem PSS split.
71    pub rollup: crate::procfs::Rollup,
72    pub parse_ms: f32,
73    pub at: Instant,
74}
75
76#[derive(Clone, Debug)]
77pub struct Snapshot {
78    pub rows: Vec<ProcRow>,
79    /// Only pids the sampler has spent page-table-walk budget on.
80    pub pss: HashMap<u32, PssInfo>,
81    pub mem: MemInfo,
82    /// System-wide PSI for memory (/proc/pressure/memory), read every tick.
83    pub pressure: Pressure,
84    /// Increments on every slow scan (cgroups/pid map). The UI uses it to avoid
85    /// pushing the same 5 s sample 5 times at 1 Hz.
86    pub scan_seq: u64,
87    /// cgroup v2 memory accounting (empty on v1 or when unreadable).
88    pub cgroups: Vec<CgroupMem>,
89    /// pid -> cgroup path, refreshed on the slow cadence; Arc so the per-tick
90    /// snapshot clone stays cheap.
91    pub pid_cgroup: Arc<HashMap<u32, String>>,
92    /// pid -> executable path, same cadence.
93    pub pid_exe: Arc<HashMap<u32, String>>,
94    pub detail: Option<Detail>,
95    /// Why the detail view is empty (usually EACCES on a process we don't own).
96    pub detail_error: Option<String>,
97    pub sweep_ms: f32,
98    pub pss_ms: f32,
99    /// Sum of PSS for entries younger than PSS_MAX_AGE, and how many that is.
100    pub covered_pss_kb: u64,
101    pub covered: usize,
102    /// Pids whose smaps_rollup needs privileges (EACCES). `sudo` fixes these.
103    pub denied: HashSet<u32>,
104    /// Pids with no address space (kernel threads) - no privilege will help.
105    pub no_mm: HashSet<u32>,
106    pub at: Instant,
107}
108
109#[derive(Clone, Copy, Debug)]
110pub enum Focus {
111    Set { pid: Option<u32>, want_detail: bool },
112}
113
114/// A cached rollup younger than this is shown as authoritative (dimmed past it).
115pub const PSS_MAX_AGE: Duration = Duration::from_millis(6000);
116/// /proc/PID/smaps_rollup requires ptrace-read permission: other users'
117/// processes (and kernel threads) answer EACCES/ESRCH. Do not retry them every
118/// tick — measured, 234 of 450 pids are unreadable as a normal user, and
119/// retrying them consumed half the per-tick budget and starved readable pids.
120pub const PSS_DENIED_TTL: Duration = Duration::from_secs(30);
121/// Never re-walk a process's page table more often than this, unless focused.
122/// With ~465 pids a full set costs ~700 ms, so the 150 ms/tick budget refreshes
123/// everything about every 5 s — stale numbers, not missing ones.
124pub const PSS_REFRESH_AFTER: Duration = Duration::from_millis(2000);