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
BackgroundTaskthat reads the Linux/procand cgroup files of a process and its descendants everyintervalseconds, and reports the increase of each counter as aSystemStats. 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 byrun_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 asroot_pid.By default each sample is logged as one human-readable summary line.
callbackreceives each fullSystemStats; to log or store a machine-readable record per interval, sendSystemStats.summary()(about 2 KB) rather thanSystemStats.to_dict(), whose per-process and per-thread detail grows to tens of kilobytes for a worker with hundreds of threads.output_pathappends the full detail as JSON lines to a local file. Subclasses can overrideinterval_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 toloop.run_in_executor(executor, ...), so they never run on the loop. With the defaultexecutor=Nonethat 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 ofspdl.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.callbackruns on the event loop; keep it short or make it a coroutine that offloads its work. Without the kernel’schildrenfiles, 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 wayProcessGroupStatsMonitorskips with a warning where it cannot run.Added in version 0.9.0.
The arguments follow
ProcessGroupStatsMonitor: acallbackreceiving each sample, and anintervalof 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_rollupfor 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
childrenfiles, how often (in seconds) to scan/procfor 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.Noneuses 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
callbackand appends tooutput_pathif they are given, and logs a summary line if neither is. Override to send the stats elsewhere.