use std::path::Path;
use serde::Deserialize;
use serde::Serialize;
use crate::machine::MachineDescription;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub(crate) enum FixedProcfsFile {
Version,
Cmdline,
Osrelease,
Ostype,
KernelVersion,
BootId,
EntropyAvail,
OverflowUid,
OverflowGid,
PidMax,
CapLastCap,
OvercommitMemory,
MaxMapCount,
MmapMinAddr,
Modules,
Filesystems,
Interrupts,
Softirqs,
Swaps,
Buddyinfo,
Zoneinfo,
Schedstat,
KeyUsers,
Vmstat,
Cgroups,
TtyDrivers,
Meminfo,
Loadavg,
FileNr,
FileMax,
InodeNr,
InodeState,
DentryState,
AioNr,
AioMaxNr,
PipeMaxSize,
}
impl FixedProcfsFile {
pub(crate) const ALL: [Self; 36] = [
Self::Version,
Self::Cmdline,
Self::Osrelease,
Self::Ostype,
Self::KernelVersion,
Self::BootId,
Self::EntropyAvail,
Self::OverflowUid,
Self::OverflowGid,
Self::PidMax,
Self::CapLastCap,
Self::OvercommitMemory,
Self::MaxMapCount,
Self::MmapMinAddr,
Self::Modules,
Self::Filesystems,
Self::Interrupts,
Self::Softirqs,
Self::Swaps,
Self::Buddyinfo,
Self::Zoneinfo,
Self::Schedstat,
Self::KeyUsers,
Self::Vmstat,
Self::Cgroups,
Self::TtyDrivers,
Self::Meminfo,
Self::Loadavg,
Self::FileNr,
Self::FileMax,
Self::InodeNr,
Self::InodeState,
Self::DentryState,
Self::AioNr,
Self::AioMaxNr,
Self::PipeMaxSize,
];
pub(crate) fn c_path(self) -> &'static std::ffi::CStr {
match self {
Self::Version => c"/proc/version",
Self::Cmdline => c"/proc/cmdline",
Self::Osrelease => c"/proc/sys/kernel/osrelease",
Self::Ostype => c"/proc/sys/kernel/ostype",
Self::KernelVersion => c"/proc/sys/kernel/version",
Self::BootId => c"/proc/sys/kernel/random/boot_id",
Self::EntropyAvail => c"/proc/sys/kernel/random/entropy_avail",
Self::OverflowUid => c"/proc/sys/kernel/overflowuid",
Self::OverflowGid => c"/proc/sys/kernel/overflowgid",
Self::PidMax => c"/proc/sys/kernel/pid_max",
Self::CapLastCap => c"/proc/sys/kernel/cap_last_cap",
Self::OvercommitMemory => c"/proc/sys/vm/overcommit_memory",
Self::MaxMapCount => c"/proc/sys/vm/max_map_count",
Self::MmapMinAddr => c"/proc/sys/vm/mmap_min_addr",
Self::Modules => c"/proc/modules",
Self::Filesystems => c"/proc/filesystems",
Self::Interrupts => c"/proc/interrupts",
Self::Softirqs => c"/proc/softirqs",
Self::Swaps => c"/proc/swaps",
Self::Buddyinfo => c"/proc/buddyinfo",
Self::Zoneinfo => c"/proc/zoneinfo",
Self::Schedstat => c"/proc/schedstat",
Self::KeyUsers => c"/proc/key-users",
Self::Vmstat => c"/proc/vmstat",
Self::Cgroups => c"/proc/cgroups",
Self::TtyDrivers => c"/proc/tty/drivers",
Self::Meminfo => c"/proc/meminfo",
Self::Loadavg => c"/proc/loadavg",
Self::FileNr => c"/proc/sys/fs/file-nr",
Self::FileMax => c"/proc/sys/fs/file-max",
Self::InodeNr => c"/proc/sys/fs/inode-nr",
Self::InodeState => c"/proc/sys/fs/inode-state",
Self::DentryState => c"/proc/sys/fs/dentry-state",
Self::AioNr => c"/proc/sys/fs/aio-nr",
Self::AioMaxNr => c"/proc/sys/fs/aio-max-nr",
Self::PipeMaxSize => c"/proc/sys/fs/pipe-max-size",
}
}
pub(crate) fn path(self) -> &'static Path {
Path::new(
self.c_path()
.to_str()
.expect("fixed procfs paths are ASCII"),
)
}
pub(crate) fn from_path(path: &str) -> Option<Self> {
Self::ALL
.into_iter()
.find(|file| file.c_path().to_bytes() == path.as_bytes())
}
pub(crate) fn permissions(self) -> u32 {
match self {
Self::OverflowUid
| Self::OverflowGid
| Self::PidMax
| Self::OvercommitMemory
| Self::MaxMapCount
| Self::MmapMinAddr
| Self::FileMax
| Self::AioMaxNr
| Self::PipeMaxSize => 0o644,
_ => 0o444,
}
}
pub(crate) fn is_integer(self) -> bool {
!self.is_seq_file()
&& !matches!(
self,
Self::Osrelease | Self::Ostype | Self::KernelVersion | Self::BootId
)
}
pub(crate) fn is_seq_file(self) -> bool {
!self.c_path().to_bytes().starts_with(b"/proc/sys/")
}
pub(crate) fn has_read_iter(self) -> bool {
!matches!(self, Self::Modules | Self::Swaps)
}
pub(crate) fn contents(self, machine: &MachineDescription) -> Vec<u8> {
let kernel = &machine.kernel;
let limits = &machine.limits;
let cpus = machine.cpus;
let table = match self {
Self::Modules => Some(String::new()),
Self::Filesystems => Some(FILESYSTEMS.to_owned()),
Self::Interrupts => Some(interrupts(cpus)),
Self::Softirqs => Some(softirqs(cpus)),
Self::Swaps => Some("Filename\t\t\t\tType\t\tSize\t\tUsed\t\tPriority\n".to_owned()),
Self::Buddyinfo => Some(buddyinfo()),
Self::Zoneinfo => Some(zoneinfo(cpus)),
Self::Schedstat => Some(schedstat(cpus)),
Self::KeyUsers => Some(KEY_USERS.to_owned()),
Self::Vmstat => Some(vmstat()),
Self::Cgroups => Some(cgroups()),
Self::TtyDrivers => Some(TTY_DRIVERS.to_owned()),
Self::Meminfo => Some(meminfo(machine.memory)),
Self::Loadavg => Some("0.00 0.00 0.00 1/1 1\n".to_owned()),
_ => None,
};
if let Some(table) = table {
return table.into_bytes();
}
let text = match self {
Self::Version => kernel.banner(),
Self::Cmdline => kernel.command_line(),
Self::Osrelease => kernel.release.clone(),
Self::Ostype => kernel.sysname.clone(),
Self::KernelVersion => kernel.version(),
Self::BootId => kernel.boot_id.clone(),
Self::EntropyAvail => limits.entropy_avail.to_string(),
Self::OverflowUid => limits.overflow_uid.to_string(),
Self::OverflowGid => limits.overflow_gid.to_string(),
Self::PidMax => limits.pid_max.to_string(),
Self::CapLastCap => limits.cap_last_cap.to_string(),
Self::OvercommitMemory => limits.overcommit_memory.to_string(),
Self::MaxMapCount => limits.max_map_count.to_string(),
Self::MmapMinAddr => limits.mmap_min_addr.to_string(),
Self::FileNr => format!("0\t0\t{}", limits.file_max),
Self::FileMax => limits.file_max.to_string(),
Self::InodeNr => "0\t0".to_owned(),
Self::InodeState => "0\t0\t0\t0\t0\t0\t0".to_owned(),
Self::DentryState => "0\t0\t45\t0\t0\t0".to_owned(),
Self::AioNr => "0".to_owned(),
Self::AioMaxNr => limits.aio_max_nr.to_string(),
Self::PipeMaxSize => limits.pipe_max_size.to_string(),
_ => unreachable!("{self:?} is a table"),
};
format!("{text}\n").into_bytes()
}
}
const FILESYSTEMS: &str = include_str!("tables/filesystems.txt");
const KEY_USERS: &str = " 0: 0 0/0 0/1000000 0/25000000\n";
const TTY_DRIVERS: &str = "\
/dev/tty /dev/tty 5 0 system:/dev/tty
/dev/console /dev/console 5 1 system:console
/dev/ptmx /dev/ptmx 5 2 system
/dev/vc/0 /dev/vc/0 4 0 system:vtmaster
serial_8250 /dev/ttyS 4 64-95 serial
pty_slave /dev/pts 136 0-1048575 pty:slave
pty_master /dev/ptm 128 0-1048575 pty:master
unknown /dev/tty 4 1-63 console
";
const INTERRUPT_LINES: [(u32, u32, &str); 2] = [(0, 2, "timer"), (8, 8, "rtc0")];
pub(super) const NR_IRQS: usize = 16;
const ARCH_INTERRUPTS: [(&str, &str); 15] = [
("NMI", "Non-maskable interrupts"),
("LOC", "Local timer interrupts"),
("PMI", "Performance monitoring interrupts"),
("IWI", "IRQ work interrupts"),
("RES", "Rescheduling interrupts"),
("CAL", "Function call interrupts"),
("TLB", "TLB shootdowns"),
("TRM", "Thermal event interrupt"),
("THR", "Threshold APIC interrupts"),
("DFR", "Deferred Error APIC interrupts"),
("MCE", "Machine check exceptions"),
("MCP", "Machine check polls"),
("PIN", "Posted-interrupt notification event"),
("NPI", "Nested posted-interrupt event"),
("PIW", "Posted-interrupt wakeup event"),
];
fn interrupts(cpus: u32) -> String {
let counts = || " 0".repeat(cpus as usize);
let mut out = " ".repeat(11);
for cpu in 0..cpus {
out += &format!("CPU{cpu:<8}");
}
out.push('\n');
for (line, pin, action) in INTERRUPT_LINES {
out += &format!(
"{line:>3}:{} {:<14} {pin:>14}-{:<8} {action}\n",
counts(),
"IO-APIC",
"edge"
);
}
for (name, description) in ARCH_INTERRUPTS {
out += &format!("{name:>3}:{} {description}\n", counts());
}
out
}
pub(super) const SOFTIRQS: [&str; 10] = [
"HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "IRQ_POLL", "TASKLET", "SCHED", "HRTIMER", "RCU",
];
fn softirqs(cpus: u32) -> String {
let mut out = " ".repeat(20);
for cpu in 0..cpus {
out += &format!("CPU{cpu:<8}");
}
out.push('\n');
for name in SOFTIRQS {
out += &format!("{name:>12}:{}\n", " 0".repeat(cpus as usize));
}
out
}
fn buddyinfo() -> String {
["DMA", "DMA32", "Normal"]
.map(|zone| format!("Node 0, zone {zone:>8} {}\n", " 0 ".repeat(11)))
.concat()
}
fn zoneinfo(cpus: u32) -> String {
let pagesets = (0..cpus)
.map(|cpu| ZONEINFO_PAGESET.replace("@CPU@", &cpu.to_string()))
.collect::<String>();
ZONEINFO.replace("@PAGESETS@\n", &pagesets)
}
const ZONEINFO: &str = include_str!("tables/zoneinfo.txt");
const ZONEINFO_PAGESET: &str = include_str!("tables/zoneinfo_pageset.txt");
fn schedstat(cpus: u32) -> String {
let mut out = "version 17\ntimestamp 0\n".to_owned();
for cpu in 0..cpus {
out += &format!("cpu{cpu} 0 0 0 0 0 0 0 0 0\n");
}
out
}
fn vmstat() -> String {
include_str!("tables/vmstat_names.txt")
.lines()
.map(|name| format!("{name} 0\n"))
.collect()
}
const CGROUP_CONTROLLERS: [&str; 10] = [
"cpu",
"cpuacct",
"blkio",
"devices",
"freezer",
"net_cls",
"perf_event",
"net_prio",
"hugetlb",
"pids",
];
fn cgroups() -> String {
let mut out = "#subsys_name\thierarchy\tnum_cgroups\tenabled\n".to_owned();
for controller in CGROUP_CONTROLLERS {
out += &format!("{controller}\t0\t1\t1\n");
}
out
}
fn meminfo(memory: u64) -> String {
let kb = memory / 1024;
[
("MemTotal:", kb),
("MemFree:", kb),
("MemAvailable:", kb),
("Buffers:", 0),
("Cached:", 0),
("SwapCached:", 0),
("Active:", 0),
("Inactive:", 0),
("Shmem:", 0),
("SReclaimable:", 0),
("SwapTotal:", 0),
("SwapFree:", 0),
]
.map(|(label, value)| format!("{label:<16}{value:>8} kB\n"))
.concat()
}
#[cfg(test)]
mod tests {
use chrono::TimeZone;
use chrono::Utc;
use super::*;
fn machine() -> MachineDescription {
MachineDescription::new(Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap())
}
fn contents(file: FixedProcfsFile) -> String {
String::from_utf8(file.contents(&machine())).unwrap()
}
#[test]
fn fixed_procfs_files_have_their_documented_values() {
use FixedProcfsFile::*;
let expected = [
(
Version,
"Linux version 5.2.0 #1 SMP Thu Jan 01 00:00:00 UTC 2026\n",
),
(Cmdline, "BOOT_IMAGE=/vmlinuz-5.2.0 quiet\n"),
(Osrelease, "5.2.0\n"),
(Ostype, "Linux\n"),
(KernelVersion, "#1 SMP Thu Jan 01 00:00:00 UTC 2026\n"),
(BootId, "6865726d-6974-4000-8000-000000000000\n"),
(EntropyAvail, "256\n"),
(OverflowUid, "65534\n"),
(OverflowGid, "65534\n"),
(PidMax, "4194304\n"),
(CapLastCap, "40\n"),
(OvercommitMemory, "0\n"),
(MaxMapCount, "65530\n"),
(MmapMinAddr, "65536\n"),
(Modules, ""),
(Swaps, "Filename\t\t\t\tType\t\tSize\t\tUsed\t\tPriority\n"),
(KeyUsers, " 0: 0 0/0 0/1000000 0/25000000\n"),
(
Interrupts,
"\x20 CPU0 \n\
\x20 0: 0 IO-APIC 2-edge timer\n\
\x20 8: 0 IO-APIC 8-edge rtc0\n\
NMI: 0 Non-maskable interrupts\n\
LOC: 0 Local timer interrupts\n\
PMI: 0 Performance monitoring interrupts\n\
IWI: 0 IRQ work interrupts\n\
RES: 0 Rescheduling interrupts\n\
CAL: 0 Function call interrupts\n\
TLB: 0 TLB shootdowns\n\
TRM: 0 Thermal event interrupt\n\
THR: 0 Threshold APIC interrupts\n\
DFR: 0 Deferred Error APIC interrupts\n\
MCE: 0 Machine check exceptions\n\
MCP: 0 Machine check polls\n\
PIN: 0 Posted-interrupt notification event\n\
NPI: 0 Nested posted-interrupt event\n\
PIW: 0 Posted-interrupt wakeup event\n",
),
(
Softirqs,
" CPU0 \n\
\x20 HI: 0\n\
\x20 TIMER: 0\n\
\x20 NET_TX: 0\n\
\x20 NET_RX: 0\n\
\x20 BLOCK: 0\n\
\x20 IRQ_POLL: 0\n\
\x20 TASKLET: 0\n\
\x20 SCHED: 0\n\
\x20 HRTIMER: 0\n\
\x20 RCU: 0\n",
),
(
Buddyinfo,
"Node 0, zone DMA 0 0 0 0 0 0 0 0 0 0 0 \n\
Node 0, zone DMA32 0 0 0 0 0 0 0 0 0 0 0 \n\
Node 0, zone Normal 0 0 0 0 0 0 0 0 0 0 0 \n",
),
(
Schedstat,
"version 17\ntimestamp 0\ncpu0 0 0 0 0 0 0 0 0 0\n",
),
(
Cgroups,
"#subsys_name\thierarchy\tnum_cgroups\tenabled\n\
cpu\t0\t1\t1\ncpuacct\t0\t1\t1\nblkio\t0\t1\t1\ndevices\t0\t1\t1\n\
freezer\t0\t1\t1\nnet_cls\t0\t1\t1\nperf_event\t0\t1\t1\n\
net_prio\t0\t1\t1\nhugetlb\t0\t1\t1\npids\t0\t1\t1\n",
),
(
Meminfo,
"MemTotal: 976562 kB\n\
MemFree: 976562 kB\n\
MemAvailable: 976562 kB\n\
Buffers: 0 kB\n\
Cached: 0 kB\n\
SwapCached: 0 kB\n\
Active: 0 kB\n\
Inactive: 0 kB\n\
Shmem: 0 kB\n\
SReclaimable: 0 kB\n\
SwapTotal: 0 kB\n\
SwapFree: 0 kB\n",
),
(Loadavg, "0.00 0.00 0.00 1/1 1\n"),
(FileNr, "0\t0\t9223372036854775807\n"),
(FileMax, "9223372036854775807\n"),
(InodeNr, "0\t0\n"),
(InodeState, "0\t0\t0\t0\t0\t0\t0\n"),
(DentryState, "0\t0\t45\t0\t0\t0\n"),
(AioNr, "0\n"),
(AioMaxNr, "65536\n"),
(PipeMaxSize, "8192\n"),
];
for (file, text) in expected {
assert_eq!(contents(file), text, "{file:?}");
}
let long = [Filesystems, Zoneinfo, Vmstat, TtyDrivers];
assert_eq!(expected.len() + long.len(), FixedProcfsFile::ALL.len());
}
#[test]
fn the_long_tables_are_well_formed_and_count_nothing() {
let filesystems = contents(FixedProcfsFile::Filesystems);
for line in filesystems.lines() {
let (flag, name) = line.split_once('\t').unwrap();
assert!(matches!(flag, "" | "nodev") && !name.is_empty(), "{line}");
}
for name in [
"\n\text4\n",
"\nnodev\tproc\n",
"\nnodev\tsysfs\n",
"\nnodev\ttmpfs\n",
] {
assert!(format!("\n{filesystems}").contains(name), "{name}");
}
let vmstat = contents(FixedProcfsFile::Vmstat);
assert_eq!(vmstat.lines().count(), 206);
assert!(
vmstat
.lines()
.all(|line| line.ends_with(" 0") && line.split(' ').count() == 2)
);
assert!(vmstat.lines().any(|line| line == "nr_free_pages 0"));
let tty = contents(FixedProcfsFile::TtyDrivers);
assert!(
tty.lines()
.any(|line| line.starts_with("pty_slave /dev/pts 136 "))
);
assert_eq!(tty.lines().count(), 8);
let zoneinfo = contents(FixedProcfsFile::Zoneinfo);
for line in zoneinfo.lines() {
if line.starts_with("Node 0, zone ") || line.starts_with(" cpu: ") {
continue;
}
let numbers = line
.split(|c: char| !c.is_ascii_alphanumeric() && c != '_')
.filter(|word| word.chars().all(|c| c.is_ascii_digit()) && !word.is_empty());
for number in numbers {
assert_eq!(number, "0", "{line}");
}
}
assert!(!zoneinfo.contains('@'), "an unexpanded marker");
}
#[test]
fn the_tables_describe_the_machine_consistently() {
for (line, _, _) in INTERRUPT_LINES {
assert!((line as usize) < NR_IRQS, "{line}");
}
for cpus in [1, 2, 3] {
let machine = MachineDescription { cpus, ..machine() };
let table = |file: FixedProcfsFile| String::from_utf8(file.contents(&machine)).unwrap();
let columns = |text: &str| text.lines().next().unwrap().split_whitespace().count();
assert_eq!(columns(&table(FixedProcfsFile::Interrupts)), cpus as usize);
assert_eq!(columns(&table(FixedProcfsFile::Softirqs)), cpus as usize);
for line in table(FixedProcfsFile::Interrupts).lines().skip(1) {
let counts = line.split_once(": ").unwrap().1;
assert_eq!(
counts
.split_whitespace()
.take_while(|word| *word == "0")
.count(),
cpus as usize,
"{line}"
);
}
assert_eq!(
table(FixedProcfsFile::Schedstat)
.lines()
.filter(|line| line.starts_with("cpu"))
.count(),
cpus as usize
);
let zoneinfo = table(FixedProcfsFile::Zoneinfo);
let populated = zoneinfo.matches(" pagesets\n").count();
assert_eq!(populated, 3);
assert_eq!(
zoneinfo.matches(" cpu: ").count(),
populated * cpus as usize
);
}
let zoneinfo = contents(FixedProcfsFile::Zoneinfo);
let populated = zoneinfo
.split("Node 0, zone ")
.filter(|zone| zone.contains(" pagesets\n"))
.map(|zone| zone.lines().next().unwrap().trim().to_owned())
.collect::<Vec<_>>();
let buddyinfo = contents(FixedProcfsFile::Buddyinfo)
.lines()
.map(|line| line.split_whitespace().nth(3).unwrap().to_owned())
.collect::<Vec<_>>();
assert_eq!(populated, buddyinfo);
let machine = MachineDescription {
memory: 2 << 30,
..machine()
};
let meminfo = String::from_utf8(FixedProcfsFile::Meminfo.contents(&machine)).unwrap();
assert!(meminfo.starts_with("MemTotal: 2097152 kB\n"));
}
#[test]
fn procfs_agrees_with_uname() {
let machine = machine();
let kernel = &machine.kernel;
let version = contents(FixedProcfsFile::Version);
assert!(version.starts_with(&format!("{} version {} ", kernel.sysname, kernel.release)));
assert!(version.trim_end().ends_with(&kernel.version()));
assert_eq!(
contents(FixedProcfsFile::Osrelease).trim_end(),
kernel.release
);
assert_eq!(contents(FixedProcfsFile::Ostype).trim_end(), kernel.sysname);
assert_eq!(
contents(FixedProcfsFile::KernelVersion).trim_end(),
kernel.version()
);
}
#[test]
fn a_rendered_fixed_file_reads_its_contents() {
for fixed in FixedProcfsFile::ALL {
let mut file = super::super::ProcfsFile::from_path(fixed.path())
.unwrap()
.into_rendered();
let template = file.rendering_template().to_vec();
file.initialize(
template,
super::super::ProcfsSnapshotContext {
machine: machine(),
..Default::default()
},
);
assert_eq!(
file.take(usize::MAX),
Some(fixed.contents(&machine())),
"{fixed:?}"
);
}
}
#[test]
fn integer_files_end_after_a_partial_read() {
let open = |fixed: FixedProcfsFile| {
let mut file = super::super::ProcfsFile::from_path(fixed.path())
.unwrap()
.into_rendered();
let template = file.rendering_template().to_vec();
file.initialize(
template,
super::super::ProcfsSnapshotContext {
machine: machine(),
..Default::default()
},
);
file
};
let mut pid_max = open(FixedProcfsFile::PidMax);
assert_eq!(pid_max.take(2), Some(b"41".to_vec()));
assert_eq!(pid_max.take(64), Some(Vec::new()));
assert_eq!(pid_max.take_at(0, 64), Some(b"4194304\n".to_vec()));
let mut osrelease = open(FixedProcfsFile::Osrelease);
assert_eq!(osrelease.take(2), Some(b"5.".to_vec()));
assert_eq!(osrelease.take(64), Some(b"2.0\n".to_vec()));
let mut version = open(FixedProcfsFile::Version);
assert_eq!(version.take(6), Some(b"Linux ".to_vec()));
assert_eq!(version.take(7), Some(b"version".to_vec()));
for fixed in FixedProcfsFile::ALL {
let text = contents(fixed);
let integer = fixed.c_path().to_bytes().starts_with(b"/proc/sys/")
&& text
.split_whitespace()
.all(|word| word.parse::<u64>().is_ok());
assert_eq!(fixed.is_integer(), integer, "{fixed:?}");
}
}
#[test]
fn fixed_procfs_files_are_found_by_path_alone() {
for file in FixedProcfsFile::ALL {
assert_eq!(
FixedProcfsFile::from_path(file.path().to_str().unwrap()),
Some(file)
);
}
assert_eq!(
FixedProcfsFile::from_path("/proc/sys/kernel/hostname"),
None
);
assert_eq!(FixedProcfsFile::from_path("/proc/self/cmdline"), None);
}
}