spdl.pipeline.SystemMonitor

class SystemMonitor(callback: Callable[[SystemStats], Awaitable[None] | None] | None = None, interval: float = 60.0, *, output_path: str | None = None, root_pid: int | None = None, include_descendants: bool = True, threads: bool = True, smaps_rollup: bool = False, backend: Literal['auto', 'native', 'python'] = 'auto', discovery_interval: float = 30.0, executor: Executor | None = None)[source]

Periodically sample CPU, page faults, memory, I/O and cgroup pressure of a pipeline.

A BackgroundTask that reads the Linux /proc and cgroup files of a process and its descendants every interval seconds, and reports the increase of each counter as a SystemStats. Run in the main process, it covers the main process, the forkserver and every worker of a .to() region, with per-process and per-thread page faults, user and kernel CPU time, context switches, resident and anonymous memory and I/O, plus the cgroup’s CPU throttling, memory and pressure stall (PSI):

pipeline = build_pipeline(cfg, num_threads=8, background_tasks=[SystemMonitor])

It can also run inside each region worker, which monitors that worker alone:

builder.to(ProcessPoolExecutorConfig(max_workers=8, mp_context="forkserver"),
           background_tasks=[partial(SystemMonitor, interval=10)])

There it runs as long as the worker’s nested pipeline: the worker’s lifetime with a continuous source, otherwise one input stream, after which a new monitor takes a fresh baseline and reports one interval later. Use one placement or the other: a monitor in the main process already includes the workers. With the "forkserver" start method, a pipeline run by run_pipeline_in_subprocess() starts its region workers through the main process’s forkserver, so they are not descendants of that subprocess: monitor from the main process, or pass the main process’s ID as root_pid.

By default each sample is logged as one human-readable summary line. callback receives each full SystemStats; to log or store a machine-readable record per interval, send SystemStats.summary() (about 2 KB) rather than SystemStats.to_dict(), whose per-process and per-thread detail grows to tens of kilobytes for a worker with hundreds of threads. output_path appends the full detail as JSON lines to a local file. Subclasses can override interval_stats_callback().

The monitor is a coroutine on the pipeline’s event loop and owns no thread or executor. Reading and parsing the files, and writing output_path, are handed to loop.run_in_executor(executor, ...), so they never run on the loop. With the default executor=None that is the running loop’s default executor: in a pipeline, the thread pool that runs the stages, so each sample briefly occupies one of its slots. With the native reader of spdl.io (used when available) a sample takes about 35 microseconds per thread monitored, mostly kernel time, with the GIL held only to hand over the arguments and the results: about 1 ms for a process with 20 threads, 18 ms for one with 500, and 24 ms for a family of 35 processes with 644 threads. The pure-Python fallback reader takes about twice as long without contention, but re-acquires the GIL for every file, and while other threads run Python code a sample can take seconds. Without the native reader, or to keep sampling out of the stages’ pool, pass a dedicated executor; the caller owns it and shuts it down. callback runs on the event loop; keep it short or make it a coroutine that offloads its work. Without the kernel’s children files, new processes are found by scanning /proc; only processes that appeared since the previous sample are read.

Linux only. On other platforms the monitor can be constructed and passed to a pipeline, but run() logs a warning and returns without sampling, the way ProcessGroupStatsMonitor skips with a warning where it cannot run.

Added in version 0.9.0.

The arguments follow ProcessGroupStatsMonitor: a callback receiving each sample, and an interval of 60 seconds by default. For debugging, an interval of 10-20 seconds resolves the warm-up and epoch boundaries better.

Parameters:
  • callback – Called with each SystemStats. May be a coroutine function.

  • interval – Seconds between samples.

  • output_path – Append each sample to this file as one JSON line. {pid} in the path is replaced with the monitoring process’s ID, which gives each region worker its own file. A new file is created owner-read/write only (0o600); an existing file keeps its mode.

  • root_pid – The process to monitor. Defaults to the process running the monitor.

  • include_descendants – Also monitor the descendants of root_pid.

  • threads – Report per-thread counters, grouped by thread name.

  • smaps_rollup – Read smaps_rollup for PSS. This walks every memory mapping of a process while holding its memory-map lock, which can delay that process’s page faults, so it is off by default.

  • backend – "native" to require the native reader, "python" to use the pure-Python one, or "auto".

  • discovery_interval – Without the kernel’s children files, how often (in seconds) to scan /proc for new descendants. The family is also re-scanned as soon as a known member exits.

  • executor – Where to read the files and write output_path. None uses the running loop’s default executor. The monitor never creates or shuts down an executor.

Methods

interval_stats_callback(stats)

Handle one sample.

run()

Sample until cancelled.

async interval_stats_callback(stats: SystemStats) → None[source]

Handle one sample.

The default calls callback and appends to output_path if they are given, and logs a summary line if neither is. Override to send the stats elsewhere.

async run() → None[source]

Sample until cancelled. A last sample covering the partial interval is reported on cancellation. Returns at once, with a warning, on platforms other than Linux.