underhill_core/
inspect_proc.rs

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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use anyhow::Context;
use inspect::Request;
use inspect::Response;
use inspect::SensitivityLevel;
use pal_async::timer::PolledTimer;
use std::time::Duration;
use vmcore::vm_task::VmTaskDriver;

/// Writes inspection results based on the contents of /proc/meminfo
fn inspect_meminfo(req: Request<'_>) {
    const FIELDS: &[&str] = &[
        "MemTotal",
        "MemFree",
        "Mapped",
        "Slab",
        "AnonPages",
        "SUnreclaim",
        "KernelStack",
        "PageTables",
        "Shmem",
        "Percpu",
        "Committed_AS",
    ];

    let mut resp = req.respond();
    fn inner(resp: &mut Response<'_>) -> anyhow::Result<()> {
        let contents = fs_err::read_to_string("/proc/meminfo")?;
        for line in contents.lines() {
            let (name, rest) = line.split_once(':').context("line had no :")?;
            if FIELDS.contains(&name) {
                // Remove the units for lines that have it, just report a number
                let value = rest
                    .split_ascii_whitespace()
                    .next()
                    .context("line had no value")?;
                let value = value.parse::<u64>().context("value failed to parse")?;
                resp.sensitivity_field(name, SensitivityLevel::Safe, value);
            }
        }

        Ok(())
    }

    if let Err(e) = inner(&mut resp) {
        resp.sensitivity_field("error", SensitivityLevel::Safe, inspect::AsDebug(e));
    }
}

/// Writes inspection results based on the contents of /proc/interrupts
fn inspect_interrupts(req: Request<'_>) {
    fn inner(req: Request<'_>) -> Option<()> {
        let contents = std::fs::read("/proc/interrupts").ok()?;
        let mut resp = req.respond();
        let mut lines = std::str::from_utf8(&contents).ok()?.split('\n');
        let cpu_count = lines.next()?.split_ascii_whitespace().count();
        for line in lines {
            let (irq, mut rest) = line.split_once(':')?;
            let irq = irq.trim_start();
            resp.sensitivity_field_mut(
                irq,
                SensitivityLevel::Safe,
                &mut inspect::adhoc_mut(|req| {
                    let mut resp = req.respond();
                    resp.sensitivity_field_mut(
                        "cpu",
                        SensitivityLevel::Safe,
                        &mut inspect::adhoc_mut(|req| {
                            let mut resp = req.respond();
                            for cpu in 0..cpu_count {
                                let Some((count, next)) = rest.trim_start().split_once(' ') else {
                                    return;
                                };
                                let Ok(count) = count.parse::<u64>() else {
                                    return;
                                };
                                // Skip empty counts to keep sizes down.
                                if count != 0 {
                                    resp.sensitivity_field(
                                        &cpu.to_string(),
                                        SensitivityLevel::Safe,
                                        inspect::AsCounter(count),
                                    );
                                }
                                rest = next;
                            }
                        }),
                    );
                    let rest = rest.trim_start();
                    let (name, actions) = rest.split_once("  ").unwrap_or((rest, ""));
                    resp.sensitivity_field("name", SensitivityLevel::Safe, name)
                        .sensitivity_field("actions", SensitivityLevel::Safe, actions.trim());
                    if let Ok(irq) = irq.parse::<u32>() {
                        resp.sensitivity_field(
                            "affinity",
                            SensitivityLevel::Safe,
                            std::fs::read_to_string(format!("/proc/irq/{irq}/smp_affinity_list"))
                                .ok()
                                .as_ref()
                                .map(|s| s.trim()),
                        );
                    }
                }),
            );
        }
        Some(())
    }

    inner(req);
}

/// Writes inspection results based on the contents of /proc/<pid>/status for userspace processes
fn inspect_userspace_procs(req: Request<'_>) {
    const KTHREADD_PID_STRING: &str = "2"; // String so we don't have to parse to check it
    const FIELDS: &[&str] = &["VmSize", "VmPeak", "VmRSS", "VmHWM", "RssAnon"];
    let mut resp = req.respond();

    fn inner(resp: &mut Response<'_>) -> anyhow::Result<()> {
        'entries: for proc_entry in fs_err::read_dir("/proc")? {
            let proc_entry = proc_entry?;
            if !proc_entry.file_type()?.is_dir() {
                continue;
            }
            if proc_entry.file_name() == KTHREADD_PID_STRING {
                continue;
            }
            if !proc_entry
                .file_name()
                .as_encoded_bytes()
                .iter()
                .all(|b| b.is_ascii_digit())
            {
                continue;
            }

            let status = fs_err::read_to_string(proc_entry.path().join("status"))?;
            let mut proc_name = None;

            for line in status.lines() {
                let (name, rest) = line.split_once(':').context("line had no :")?;
                if name == "Name" {
                    proc_name = Some(rest.trim());
                }
                if name == "PPid" {
                    if rest.trim() == KTHREADD_PID_STRING {
                        continue 'entries;
                    }
                }
            }

            let proc_name = proc_name.context("no process name found for non-kernel process")?;
            resp.sensitivity_child(proc_name, SensitivityLevel::Safe, |req| {
                fn inner2(resp: &mut Response<'_>, status: &str) -> anyhow::Result<()> {
                    for line in status.lines() {
                        let (name, rest) = line.split_once(':').context("line had no :")?;
                        if FIELDS.contains(&name) {
                            // Remove the units for lines that have it, just report a number
                            let value = rest
                                .split_ascii_whitespace()
                                .next()
                                .context("line had no value")?;
                            let value = value.parse::<u64>().context("value failed to parse")?;
                            resp.sensitivity_field(name, SensitivityLevel::Safe, value);
                        }
                    }
                    Ok(())
                }

                let mut resp = req.respond();
                if let Err(e) = inner2(&mut resp, &status) {
                    resp.sensitivity_field("error", SensitivityLevel::Safe, inspect::AsDebug(e));
                }
            });
        }
        Ok(())
    }

    if let Err(e) = inner(&mut resp) {
        resp.sensitivity_field("error", SensitivityLevel::Safe, inspect::AsDebug(e));
    }
}

/// Used for on-demand inspect calls.
pub fn inspect_proc(req: Request<'_>) {
    req.respond()
        .sensitivity_child("meminfo", SensitivityLevel::Safe, inspect_meminfo)
        .sensitivity_child("interrupts", SensitivityLevel::Safe, inspect_interrupts)
        .sensitivity_child("processes", SensitivityLevel::Safe, inspect_userspace_procs);
}

/// Used for periodic automatic logging.
pub async fn periodic_telemetry_task(driver: VmTaskDriver) {
    let mut timer = PolledTimer::new(&driver);
    // Wait 15 minutes before initial logging to give the guest time to boot and begin doing work
    timer.sleep(Duration::from_secs(60 * 15)).await;

    loop {
        let mut inspection = inspect::inspect(
            "",
            inspect::adhoc_mut(|r| {
                r.respond()
                    .child("meminfo", inspect_meminfo)
                    .child("processes", inspect_userspace_procs);
            }),
        );
        inspection.resolve().await;
        let results = inspection.results();
        let json = results.json();
        // The below message needs to be valid JSON for ease of processing
        tracing::info!("{{\"periodic_memory_status\":{}}}", json);
        // Wait a day before logging again
        timer.sleep(Duration::from_secs(60 * 60 * 24)).await;
    }
}