Expand description
Fast, low-overhead CPU statistics for process trees.
This crate provides multiple approaches for measuring CPU usage:
- Snapshot-based: Get raw CPU time at a point in time
- Delta-based: Track changes between two snapshots
- Cumulative: Track total CPU from a baseline (good for short-lived processes)
All approaches use direct syscalls for minimal overhead (~1-5µs per process).
Structs§
- AllStats
- Combined stats from reading multiple /proc files.
- CpuTime
- CPU time for a process (user + system time in nanoseconds).
- Cumulative
Tracker - Approach 3: Cumulative tracker.
- Delta
Tracker - Approach 2: Delta-based tracker.
- DiskIo
- Disk I/O statistics for a process.
- Liveness
Info - Liveness information for a process, retrieved in a single syscall.
- Process
Info - Info about a single process from a full system scan.
- Process
Stats - Statistics for a single process.
- Tree
Stats - Statistics for a process tree (process + all descendants).
Functions§
- build_
children_ map - Build a HashMap of pid -> children from a full process scan.
- build_
parent_ map - Build a HashMap of pid -> parent pid for ALL processes on the system.
- collect_
descendants_ from_ map - Collect all descendant PIDs from a children map (built from full scan).
- collect_
tree_ pids - Collect all PIDs in a process tree (BFS traversal).
- get_
all_ stats - Get all stats (CPU, memory, disk I/O) by reading multiple /proc files.
- get_
children - Get direct child PIDs of a process.
- get_
cpu_ time - Get CPU time for a process by reading
/proc/[pid]/stat. - get_
cpu_ time_ with_ children - Get CPU time for a process INCLUDING time from waited-for (exited) children.
- get_
disk_ io - Get disk I/O statistics for a process.
- get_
liveness_ info - Get liveness information for a process in a single file read.
- get_
memory - Get memory usage (resident set size) for a process in bytes.
- get_
memory_ virtual - Get virtual memory size for a process in bytes.
- get_
ppid - Get parent PID of a process.
- get_
process_ comm - Get the kernel’s internal command name for a process.
- get_
process_ environ - Get environment variables for a process.
- get_
process_ path - Get the full executable path for a process.
- get_
start_ time - Get process start time as seconds since Unix epoch.
- get_tty
- Get the controlling TTY device name for a process (like
ps -o tty=). - is_
descendant_ of - Check if
child_pidis a descendant ofparent_pidusing pre-built map. - list_
all_ pids - List all PIDs on the system.
- scan_
all_ processes - Scan all processes on the system and return their info.
- snapshot_
process - Approach 1: Snapshot-based measurement.
- snapshot_
tree - Snapshot an entire process tree.
- snapshot_
tree_ from_ scan - Get stats for a process tree using the full scan approach.
Type Aliases§
- CpuPercent
- CPU usage as a percentage (0.0 - 100.0+ for multi-core).