pub struct SystemMonitor { /* private fields */ }Expand description
A utility for monitoring system resource usage of a process.
SystemMonitor can be configured to track a specific process ID or the current process.
It periodically samples CPU usage, memory usage, disk I/O, and network I/O,
and writes the metrics to a specified output file or to the tracing log.
§Example
use std::time::Duration;
use xet_runtime::logging::SystemMonitor;
use xet_runtime::utils::TemplatedPathBuf;
let monitor = SystemMonitor::follow_process(
Duration::from_secs(5),
Some(TemplatedPathBuf::from("monitor_{PID}_{TIMESTAMP}.log")),
)?;
// ... application logic ...
monitor.stop()?;Implementations§
Source§impl SystemMonitor
impl SystemMonitor
Sourcepub fn follow_process(
sample_interval: Duration,
log_path: Option<TemplatedPathBuf>,
) -> Result<Self, SystemMonitorError>
pub fn follow_process( sample_interval: Duration, log_path: Option<TemplatedPathBuf>, ) -> Result<Self, SystemMonitorError>
Creates a new SystemMonitor that follows the current process.
Monitoring starts immediately upon creation. The background thread begins sampling system metrics at the specified interval.
§Arguments
sample_interval- The interval at which to sample system metrics.log_path- Optional path template for the output log file. If None, logs to tracing at INFO level.
Sourcepub fn with_pid(
pid: Pid,
sample_interval: Duration,
log_path: Option<TemplatedPathBuf>,
) -> Result<Self, SystemMonitorError>
pub fn with_pid( pid: Pid, sample_interval: Duration, log_path: Option<TemplatedPathBuf>, ) -> Result<Self, SystemMonitorError>
Creates a new SystemMonitor that follows a specific process ID.
Monitoring starts immediately upon creation. The background thread begins sampling system metrics at the specified interval.
§Arguments
pid- The process ID to monitor.sample_interval- The interval at which to sample system metrics.log_path- Optional path template for the output log file. If None, logs to tracing at INFO level.
Sourcepub fn start(&self) -> Result<(), SystemMonitorError>
pub fn start(&self) -> Result<(), SystemMonitorError>
Starts the monitoring thread.
This function is called automatically by follow_process() and with_pid(),
so it typically doesn’t need to be called manually. If the monitor is already
running, this is a no-op.
§Errors
Returns an error if:
- The log path is invalid or cannot be written to
- The monitored process no longer exists
- Internal synchronization fails