procfs
A safe, idiomatic Rust interface for reading and parsing the Linux /proc pseudo-filesystem.
Repository Health
Technical Analysis
procfs is a Rust library that provides typed, panic-free access to the Linux /proc pseudo-filesystem, the kernel’s live interface for process, memory, network, and hardware state. Instead of hand-parsing text files under /proc, developers get strongly-typed structs and iterators for process listings, per-process stat/status/maps data, CPU and memory info, network sockets, kernel modules, and dozens of other subsystems.
The crate is split into two pieces: procfs-core, which holds platform-independent parsing logic and data structures, and procfs, which adds the actual file I/O via rustix. This separation keeps the parsing logic testable without a live Linux system, while the outer crate handles reading real /proc files, gzip-compressed kernel configs, and other OS-level access. It’s widely used by process monitors, system introspection tools, and container-aware utilities that need reliable, versioned access to kernel-exposed state.
What You Get
- Typed structs for /proc/[pid]/stat, status, maps, io, and other per-process files
- System-wide data: CPU info, meminfo, mounts, disk stats, network sockets, and kernel config
- A panic-free ProcResult/ProcError API that surfaces parsing failures instead of crashing
- Optional chrono, serde1, and backtrace feature flags for timestamps, serialization, and debugging
Common Use Cases
- Building process monitors and system dashboards that read live CPU/memory/process state
- Container and cgroup-aware tooling that inspects resource usage per process
- Writing ‘ps’/‘top’-style CLI utilities or health-check agents in Rust
Under The Hood
Architecture — The crate is split into two workspace members: procfs-core, which holds platform-independent parsing logic and data structures (SystemInfo, ProcResult/ProcError, the FromRead/FromBufRead/FromReadSI traits), and procfs, the outer crate that adds real file I/O against /proc via rustix and re-exports all of procfs-core’s public API. Modules are organized by procfs subsystem — process (per-PID stat/status/maps/io), net, sys, cgroups, keyring, iomem — with the Current/CurrentSI traits binding a parseable type to a fixed proc path (e.g. LoadAverage::current()), so reading system state is a one-line typed call rather than manual path/format string handling.
Tech Stack — Rust, edition 2018, MSRV 1.70, distributed as a two-crate Cargo workspace pinned to a shared workspace.version. Core runtime dependencies are minimal: rustix (safe libc/syscall bindings for fs/process/param/system access), bitflags, and optional chrono (timestamps), flate2 (gzip-compressed /proc/config.gz), serde (serde1 feature), and backtrace (capturing stack traces on InternalError). Dev-dependencies include criterion for benchmarking and procinfo/failure/libc for comparison and testing.
Code Quality — The crate declares #![deny(rustdoc::broken_intra_doc_links, rustdoc::invalid_html_tags)] to keep documentation accurate, and procfs/src/process/tests.rs alone carries over 30 unit tests exercising per-process parsing paths. The crate’s stated design goal is to be panic-free, surfacing malformed /proc data as a typed InternalError inside ProcResult rather than crashing the caller — a deliberate error-handling discipline uncommon in filesystem-parsing code. A criterion-based benchmark suite (bench/cpuinfo) guards parsing performance across releases.
API Design — Nearly every public type documents itself against the proc.5 man page, so users get kernel-accurate field semantics inline rather than needing external references. The Current/CurrentSI trait pair gives a consistent Type::current() idiom across dozens of unrelated /proc files, and the procfs/examples directory ships 15+ runnable programs (ps.rs, lsmod.rs, mountinfo.rs, self_memory.rs) covering the most common integration patterns, which meaningfully lowers the cost of a first successful read.