memview 1.0.1

Linux-only ncdu-like TUI for attributing RAM across processes, tmpfs, shm, and kernel counters
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::fmt::{self, Display, Formatter};
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::path::PathBuf;
use std::time::{Duration, SystemTime};

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Pid(pub i32);

impl Display for Pid {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.0, f)
    }
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Bytes(pub u64);

impl Bytes {
    pub const ZERO: Self = Self(0);
    const KIB: f64 = 1024.0;
    const UNITS: [&str; 6] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];

    #[must_use]
    pub fn from_kib(kib: u64) -> Self {
        Self(kib.saturating_mul(1024))
    }

    #[must_use]
    pub fn from_blocks_512(blocks: u64) -> Self {
        Self(blocks.saturating_mul(512))
    }

    #[must_use]
    pub fn as_f64(self) -> f64 {
        self.0 as f64
    }

    #[must_use]
    pub fn pct_of(self, total: Self) -> f64 {
        if total.0 == 0 {
            0.0
        } else {
            (self.as_f64() * 100.0) / total.as_f64()
        }
    }

    #[must_use]
    pub fn human_iec(self) -> String {
        if self.0 < 1024 {
            return format!("{} B", self.0);
        }

        let mut value = self.as_f64();
        let mut unit = 0usize;
        while value >= Self::KIB && unit + 1 < Self::UNITS.len() {
            value /= Self::KIB;
            unit += 1;
        }
        format!("{value:.1} {}", Self::UNITS[unit])
    }

    #[must_use]
    pub fn human_exact(self) -> String {
        format!("{} ({})", self.human_iec(), self.0)
    }
}

impl Add for Bytes {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_add(rhs.0))
    }
}

impl AddAssign for Bytes {
    fn add_assign(&mut self, rhs: Self) {
        self.0 = self.0.saturating_add(rhs.0);
    }
}

impl Sub for Bytes {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_sub(rhs.0))
    }
}

impl SubAssign for Bytes {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 = self.0.saturating_sub(rhs.0);
    }
}

impl Display for Bytes {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(&self.human_iec())
    }
}

macro_rules! memory_rollup_apply {
    ($lhs:expr, $rhs:expr, $op:tt) => {{
        $lhs.size $op $rhs.size;
        $lhs.rss $op $rhs.rss;
        $lhs.pss $op $rhs.pss;
        $lhs.pss_dirty $op $rhs.pss_dirty;
        $lhs.pss_anon $op $rhs.pss_anon;
        $lhs.pss_file $op $rhs.pss_file;
        $lhs.pss_shmem $op $rhs.pss_shmem;
        $lhs.shared_clean $op $rhs.shared_clean;
        $lhs.shared_dirty $op $rhs.shared_dirty;
        $lhs.private_clean $op $rhs.private_clean;
        $lhs.private_dirty $op $rhs.private_dirty;
        $lhs.referenced $op $rhs.referenced;
        $lhs.anonymous $op $rhs.anonymous;
        $lhs.lazy_free $op $rhs.lazy_free;
        $lhs.anon_huge_pages $op $rhs.anon_huge_pages;
        $lhs.shmem_pmd_mapped $op $rhs.shmem_pmd_mapped;
        $lhs.file_pmd_mapped $op $rhs.file_pmd_mapped;
        $lhs.shared_hugetlb $op $rhs.shared_hugetlb;
        $lhs.private_hugetlb $op $rhs.private_hugetlb;
        $lhs.swap $op $rhs.swap;
        $lhs.swap_pss $op $rhs.swap_pss;
        $lhs.locked $op $rhs.locked;
    }};
}

#[derive(Clone, Copy, Debug, Default)]
pub struct MemoryRollup {
    pub size: Bytes,
    pub rss: Bytes,
    pub pss: Bytes,
    pub pss_dirty: Bytes,
    pub pss_anon: Bytes,
    pub pss_file: Bytes,
    pub pss_shmem: Bytes,
    pub shared_clean: Bytes,
    pub shared_dirty: Bytes,
    pub private_clean: Bytes,
    pub private_dirty: Bytes,
    pub referenced: Bytes,
    pub anonymous: Bytes,
    pub lazy_free: Bytes,
    pub anon_huge_pages: Bytes,
    pub shmem_pmd_mapped: Bytes,
    pub file_pmd_mapped: Bytes,
    pub shared_hugetlb: Bytes,
    pub private_hugetlb: Bytes,
    pub swap: Bytes,
    pub swap_pss: Bytes,
    pub locked: Bytes,
}

impl MemoryRollup {
    #[must_use]
    pub fn uss(self) -> Bytes {
        self.private_clean + self.private_dirty + self.private_hugetlb
    }

    #[must_use]
    pub fn shared(self) -> Bytes {
        self.shared_clean + self.shared_dirty + self.shared_hugetlb
    }

    #[must_use]
    pub fn metric(self, metric: Metric) -> Bytes {
        match metric {
            Metric::Pss => self.pss,
            Metric::Uss => self.uss(),
            Metric::Rss => self.rss,
            Metric::SwapPss => self.swap_pss,
            Metric::Anonymous => self.pss_anon.max(self.anonymous),
            Metric::File => self.pss_file,
            Metric::Shmem => self.pss_shmem.max(self.shared()),
        }
    }
}

impl Add for MemoryRollup {
    type Output = Self;

    fn add(mut self, rhs: Self) -> Self::Output {
        self += rhs;
        self
    }
}

impl AddAssign for MemoryRollup {
    fn add_assign(&mut self, rhs: Self) {
        memory_rollup_apply!(self, rhs, +=);
    }
}

#[derive(Clone, Debug)]
pub struct MeminfoEntry {
    pub key: String,
    pub value: Bytes,
}

#[derive(Clone, Debug, Default)]
pub struct Meminfo {
    pub entries: Vec<MeminfoEntry>,
    pub table: BTreeMap<String, Bytes>,
}

impl Meminfo {
    #[must_use]
    pub fn get(&self, key: &str) -> Bytes {
        self.table.get(key).copied().unwrap_or(Bytes::ZERO)
    }
}

#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ObjectKind {
    Anonymous,
    SharedAnonymous,
    Heap,
    Stack,
    File,
    Tmpfs,
    Memfd,
    SysV,
    Vdso,
    Vvar,
    Vsyscall,
    Pseudo,
}

impl ObjectKind {
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Anonymous => "anon",
            Self::SharedAnonymous => "shmem",
            Self::Heap => "heap",
            Self::Stack => "stack",
            Self::File => "file",
            Self::Tmpfs => "tmpfs",
            Self::Memfd => "memfd",
            Self::SysV => "sysv",
            Self::Vdso => "vdso",
            Self::Vvar => "vvar",
            Self::Vsyscall => "vsyscall",
            Self::Pseudo => "pseudo",
        }
    }
}

#[derive(Clone, Debug)]
pub struct ObjectUsage {
    pub kind: ObjectKind,
    pub label: String,
    pub rollup: MemoryRollup,
    pub regions: usize,
}

#[derive(Clone, Debug)]
pub struct ObjectConsumer {
    pub pid: Pid,
    pub name: String,
    pub command: String,
    pub rollup: MemoryRollup,
}

#[derive(Clone, Debug)]
pub struct SharedObject {
    pub kind: ObjectKind,
    pub label: String,
    pub rollup: MemoryRollup,
    pub regions: usize,
    pub mapped_processes: usize,
    pub consumers: Vec<ObjectConsumer>,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LedgerState {
    Exact,
    Approximate,
    Inaccessible,
    Deferred,
}

impl LedgerState {
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Exact => "exact",
            Self::Approximate => "approx",
            Self::Inaccessible => "inaccessible",
            Self::Deferred => "deferred",
        }
    }

    #[must_use]
    pub fn is_inaccessible(self) -> bool {
        matches!(self, Self::Approximate | Self::Inaccessible)
    }
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ProcessTreeStats {
    pub observed_processes: usize,
    pub inaccessible_rollups: usize,
    pub inaccessible_maps: usize,
}

#[derive(Clone, Debug)]
pub struct ProcessNode {
    pub pid: Pid,
    pub ppid: Option<Pid>,
    pub name: String,
    pub command: String,
    pub username: String,
    pub state: String,
    pub threads: u32,
    pub rollup: MemoryRollup,
    pub subtree: MemoryRollup,
    pub children: Vec<usize>,
    pub objects: Vec<ObjectUsage>,
    pub rollup_state: LedgerState,
    pub mappings_state: LedgerState,
}

impl ProcessNode {
    #[must_use]
    pub fn title(&self) -> String {
        if self.command.is_empty() {
            format!("{} [{}]", self.name, self.pid)
        } else {
            format!("{} [{}] {}", self.name, self.pid, self.command)
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct ProcessTree {
    pub roots: Vec<usize>,
    pub nodes: Vec<ProcessNode>,
    pub stats: ProcessTreeStats,
}

#[derive(Clone, Debug)]
pub struct SysvSegment {
    pub id: i32,
    pub attachments: u32,
    pub owner_uid: u32,
    pub size: Bytes,
    pub rss: Bytes,
    pub swap: Bytes,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TmpfsNodeKind {
    Mount,
    Directory,
    File,
    Symlink,
    Socket,
    Fifo,
    CharDevice,
    BlockDevice,
    Other,
}

impl TmpfsNodeKind {
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Mount => "mount",
            Self::Directory => "dir",
            Self::File => "file",
            Self::Symlink => "link",
            Self::Socket => "sock",
            Self::Fifo => "fifo",
            Self::CharDevice => "char",
            Self::BlockDevice => "block",
            Self::Other => "other",
        }
    }
}

#[derive(Clone, Debug)]
pub struct TmpfsNode {
    pub path: PathBuf,
    pub name: String,
    pub kind: TmpfsNodeKind,
    pub allocated: Bytes,
    pub logical: Bytes,
    pub children: Vec<TmpfsNode>,
}

#[derive(Clone, Debug)]
pub struct TmpfsMount {
    pub mount_point: PathBuf,
    pub source: String,
    pub size_limit: Option<Bytes>,
    pub root: TmpfsNode,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Metric {
    Pss,
    Uss,
    Rss,
    SwapPss,
    Anonymous,
    File,
    Shmem,
}

impl Metric {
    const ALL: [Self; 7] = [
        Self::Pss,
        Self::Uss,
        Self::Rss,
        Self::SwapPss,
        Self::Anonymous,
        Self::File,
        Self::Shmem,
    ];

    #[must_use]
    pub fn next(self) -> Self {
        let index = Self::ALL
            .iter()
            .position(|metric| *metric == self)
            .unwrap_or(0);
        Self::ALL[(index + 1) % Self::ALL.len()]
    }

    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Pss => "PSS",
            Self::Uss => "USS",
            Self::Rss => "RSS",
            Self::SwapPss => "SwapPSS",
            Self::Anonymous => "Anon",
            Self::File => "File",
            Self::Shmem => "Shmem",
        }
    }

    #[must_use]
    pub fn cmp_rollup(self, lhs: MemoryRollup, rhs: MemoryRollup) -> Ordering {
        rhs.metric(self).cmp(&lhs.metric(self))
    }
}

#[derive(Clone, Debug, Default)]
pub struct Overview {
    pub process_count: usize,
    pub inaccessible_rollups: usize,
    pub inaccessible_maps: usize,
    pub process_pss_total: Bytes,
    pub process_uss_total: Bytes,
    pub process_rss_total: Bytes,
    pub process_swap_pss_total: Bytes,
    pub process_pss_anon_total: Bytes,
    pub process_pss_file_total: Bytes,
    pub process_pss_shmem_total: Bytes,
    pub tmpfs_allocated_total: Bytes,
    pub sysv_rss_total: Bytes,
}

#[derive(Clone, Debug)]
pub struct Snapshot {
    pub captured_at: SystemTime,
    pub elapsed: Duration,
    pub meminfo: Meminfo,
    pub overview: Overview,
    pub process_tree: ProcessTree,
    pub shared_objects: Vec<SharedObject>,
    pub sysv_segments: Vec<SysvSegment>,
    pub tmpfs_mounts: Vec<TmpfsMount>,
    pub warnings: Vec<String>,
}