Crate prock

Crate prock 

Source
Expand description

Fast, low-overhead CPU statistics for process trees.

This crate provides multiple approaches for measuring CPU usage:

  1. Snapshot-based: Get raw CPU time at a point in time
  2. Delta-based: Track changes between two snapshots
  3. 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).
CumulativeTracker
Approach 3: Cumulative tracker.
DeltaTracker
Approach 2: Delta-based tracker.
DiskIo
Disk I/O statistics for a process.
LivenessInfo
Liveness information for a process, retrieved in a single syscall.
ProcessInfo
Info about a single process from a full system scan.
ProcessStats
Statistics for a single process.
TreeStats
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_pid is a descendant of parent_pid using 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).