Skip to main content

mtop/
json.rs

1//! Machine-readable snapshot for `--json`: stable ordering, explicit units,
2//! so two runs can be diffed (`mtop --json > a; mtop --json > b; diff a b`).
3
4use std::io::Write;
5use std::time::{SystemTime, UNIX_EPOCH};
6
7use serde_json::{Value, json};
8
9use crate::model::Snapshot;
10use crate::procfs::Rollup;
11
12fn rollup_json(r: &Rollup) -> Value {
13    json!({
14        "rss_kb": r.rss_kb,
15        "pss_kb": r.pss_kb,
16        "uss_kb": r.uss_kb,
17        "swap_kb": r.swap_kb,
18        "anon_kb": r.anon_kb,
19        "pss_anon_kb": r.pss_anon_kb,
20        "pss_file_kb": r.pss_file_kb,
21        "pss_shmem_kb": r.pss_shmem_kb,
22        "swap_pss_kb": r.swap_pss_kb,
23        "thp_kb": r.thp_kb,
24        "ksm_kb": r.ksm_kb,
25    })
26}
27
28/// Keep only the requested dotted paths, e.g. `procs.pid, procs.pss.pss_kb`.
29/// `schema` and `at_unix_ms` are always kept: a document without them cannot be
30/// identified or ordered.
31pub fn render(snap: &Snapshot, fields: &[String]) -> Result<String, String> {
32    let doc = build_doc(snap);
33    if fields.is_empty() {
34        return Ok(pretty(&doc));
35    }
36    let mut paths: Vec<Vec<String>> = Vec::with_capacity(fields.len());
37    for raw in fields {
38        let path: Vec<String> = raw
39            .split('.')
40            .map(str::trim)
41            .filter(|s| !s.is_empty())
42            .map(str::to_string)
43            .collect();
44        if path.is_empty() {
45            return Err(format!("empty field path {raw:?}"));
46        }
47        if !exists(&doc, &path) {
48            return Err(format!(
49                "unknown field path {raw:?}; top-level keys are: {}",
50                doc.as_object()
51                    .map(|m| m.keys().cloned().collect::<Vec<_>>().join(", "))
52                    .unwrap_or_default()
53            ));
54        }
55        paths.push(path);
56    }
57    let projected = project(&doc, &paths);
58    Ok(pretty(&projected))
59}
60
61fn pretty(value: &Value) -> String {
62    serde_json::to_string_pretty(value).unwrap_or_else(|_| "{}".into())
63}
64
65fn exists(value: &Value, path: &[String]) -> bool {
66    let Some(first) = path.first() else {
67        return true;
68    };
69    match value {
70        Value::Object(map) => map.get(first).is_some_and(|v| exists(v, &path[1..])),
71        // Every element is a record; the path applies to each of them.
72        Value::Array(items) => items.iter().any(|item| exists(item, path)),
73        // A null section (e.g. `detail` without --pid) is allowed: the caller
74        // asked for a path that would exist once there is data.
75        Value::Null => true,
76        _ => false,
77    }
78}
79
80#[derive(Default)]
81struct Field {
82    whole: bool,
83    children: std::collections::BTreeMap<String, Field>,
84}
85
86fn build_trie(paths: &[Vec<String>]) -> Field {
87    let mut root = Field::default();
88    for path in paths {
89        let mut node = &mut root;
90        for seg in path {
91            node = node.children.entry(seg.clone()).or_default();
92        }
93        node.whole = true;
94    }
95    root
96}
97
98fn project_with(value: &Value, field: &Field) -> Value {
99    if field.whole {
100        return value.clone();
101    }
102    match value {
103        Value::Object(map) => {
104            let mut out = serde_json::Map::new();
105            for (key, child) in &field.children {
106                if let Some(v) = map.get(key) {
107                    out.insert(key.clone(), project_with(v, child));
108                }
109            }
110            Value::Object(out)
111        }
112        // The path applies to every record in the array.
113        Value::Array(items) => {
114            Value::Array(items.iter().map(|item| project_with(item, field)).collect())
115        }
116        other => other.clone(),
117    }
118}
119
120fn project(doc: &Value, paths: &[Vec<String>]) -> Value {
121    let mut out = project_with(doc, &build_trie(paths));
122    if let Value::Object(map) = &mut out {
123        for key in ["schema", "at_unix_ms"] {
124            if !map.contains_key(key)
125                && let Some(v) = doc.get(key)
126            {
127                map.insert(key.to_string(), v.clone());
128            }
129        }
130    }
131    out
132}
133
134fn build_doc(snap: &Snapshot) -> Value {
135    let now_ms = SystemTime::now()
136        .duration_since(UNIX_EPOCH)
137        .map(|d| d.as_millis())
138        .unwrap_or(0);
139
140    // Sorted by pid, not by the UI sort: a diff should only change when memory
141    // changes, not when the view changes.
142    let mut rows: Vec<&crate::model::ProcRow> = snap.rows.iter().collect();
143    rows.sort_by_key(|r| r.pid);
144
145    let procs: Vec<Value> = rows
146        .iter()
147        .map(|row| {
148            let pss = snap.pss.get(&row.pid);
149            json!({
150                "pid": row.pid,
151                "comm": row.comm,
152                "rss_kb": row.rss_kb,
153                "delta_kb": row.delta_kb,
154                "denied": snap.denied.contains(&row.pid),
155                "no_mm": snap.no_mm.contains(&row.pid),
156                "pss_age_ms": pss.map(|p| p.at.elapsed().as_millis() as u64),
157                "pss": pss.map(|p| rollup_json(&p.rollup)).unwrap_or(Value::Null),
158            })
159        })
160        .collect();
161
162    let detail = snap.detail.as_ref().map(|d| {
163        json!({
164            "pid": d.pid,
165            "comm": d.comm,
166            "cmdline": d.cmdline,
167            "parse_ms": d.parse_ms,
168            "rollup": rollup_json(&d.rollup),
169            "vma_count": d.report.vma_count,
170            "groups": d.report.groups.iter().map(|g| json!({
171                "label": g.label,
172                "size_kb": g.size_kb,
173                "pss_kb": g.pss_kb,
174                "uss_kb": g.uss_kb,
175                "anon_kb": g.anon_kb,
176                "swap_kb": g.swap_kb,
177                "vmas": g.count,
178            })).collect::<Vec<_>>(),
179        })
180    });
181
182    let doc = json!({
183        "schema": "mtop.snapshot.v1",
184        "at_unix_ms": now_ms,
185        // The process that wrote this snapshot. A later --diff must ignore it:
186        // its pid is new every run, so it would show up as a phantom +3 MB.
187        "self_pid": std::process::id(),
188        "mem": {
189            "total_kb": snap.mem.total_kb,
190            "avail_kb": snap.mem.avail_kb,
191            "cached_kb": snap.mem.cached_kb,
192            "swap_total_kb": snap.mem.swap_total_kb,
193            "swap_free_kb": snap.mem.swap_free_kb,
194        },
195        "pressure": {
196            "some10": snap.pressure.some10,
197            "full10": snap.pressure.full10,
198        },
199        "sampling": {
200            "sweep_ms": snap.sweep_ms,
201            "pss_ms": snap.pss_ms,
202            "procs": snap.rows.len(),
203            "covered": snap.covered,
204            "denied": snap.denied.len(),
205            "no_mm": snap.no_mm.len(),
206            "covered_pss_kb": snap.covered_pss_kb,
207        },
208        "cgroups": snap.cgroups.iter().map(|c| json!({
209            "path": c.path,
210            "current_kb": c.current_kb,
211            "anon_kb": c.anon_kb,
212            "file_kb": c.file_kb,
213            "kernel_kb": c.kernel_kb,
214            "slab_kb": c.slab_kb,
215            "shmem_kb": c.shmem_kb,
216            "limit_kb": c.limit_kb,
217            "oom_kills": c.oom_kills,
218            "oom_recent": c.oom_recent,
219            "psi_some10": c.psi_some10,
220            "psi_full10": c.psi_full10,
221        })).collect::<Vec<_>>(),
222        "detail_error": snap.detail_error,
223        "detail": detail,
224        "procs": procs,
225    });
226    doc
227}
228
229pub fn write_str(text: &str) -> std::io::Result<()> {
230    let mut out = std::io::stdout().lock();
231    out.write_all(text.as_bytes())?;
232    if !text.ends_with('\n') {
233        out.write_all(b"\n")?;
234    }
235    out.flush()
236}
237
238#[cfg(test)]
239mod tests {
240    use super::*;
241    use serde_json::json;
242
243    fn doc() -> Value {
244        json!({
245            "schema": "mtop.snapshot.v1",
246            "at_unix_ms": 42,
247            "mem": {"total_kb": 1, "avail_kb": 2},
248            "procs": [
249                {"pid": 7, "comm": "a", "rss_kb": 10, "pss": {"pss_kb": 5, "uss_kb": 4}},
250                {"pid": 8, "comm": "b", "rss_kb": 20, "pss": null}
251            ]
252        })
253    }
254
255    fn project_str(fields: &[&str]) -> Value {
256        let paths: Vec<Vec<String>> = fields
257            .iter()
258            .map(|f| f.split('.').map(str::to_string).collect())
259            .collect();
260        project(&doc(), &paths)
261    }
262
263    #[test]
264    fn flat_and_nested_paths_are_projected_per_record() {
265        let out = project_str(&["procs.pid", "procs.pss.pss_kb"]);
266        let procs = out["procs"].as_array().unwrap();
267        assert_eq!(procs[0], json!({"pid": 7, "pss": {"pss_kb": 5}}));
268        // A null section stays null instead of turning into an empty object.
269        assert_eq!(procs[1], json!({"pid": 8, "pss": null}));
270        assert!(out.get("mem").is_none(), "unselected section leaked");
271    }
272
273    #[test]
274    fn meta_keys_are_always_kept() {
275        let out = project_str(&["mem.total_kb"]);
276        assert_eq!(out["schema"], "mtop.snapshot.v1");
277        assert_eq!(out["at_unix_ms"], 42);
278        assert_eq!(out["mem"], json!({"total_kb": 1}));
279    }
280
281    #[test]
282    fn a_whole_section_path_keeps_it_intact() {
283        let out = project_str(&["mem"]);
284        assert_eq!(out["mem"], doc()["mem"]);
285    }
286
287    #[test]
288    fn validation_rejects_typos_but_allows_null_sections() {
289        let d = doc();
290        let path = |s: &str| -> Vec<String> { s.split('.').map(str::to_string).collect() };
291        assert!(exists(&d, &path("procs.pid")));
292        assert!(exists(&d, &path("procs.pss.uss_kb")));
293        assert!(!exists(&d, &path("procs.nope")));
294        assert!(!exists(&d, &path("nope")));
295        // `detail` is null without --pid; asking for its fields is valid.
296        let with_null = json!({"schema": "x", "detail": null});
297        assert!(exists(&with_null, &path("detail.pid")));
298    }
299}