1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
use crate::client::slurm_utils::{parse_slurm_cpu_time, parse_slurm_memory};
use log::{debug, error, info, warn};
use rusqlite::{Connection, Result as SqliteResult};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Receiver, Sender, channel};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};
use sysinfo::{
CpuRefreshKind, Pid, ProcessExt, ProcessRefreshKind, RefreshKind, System, SystemExt,
};
const DB_FILENAME_PREFIX: &str = "resource_metrics";
/// Configuration for resource monitoring
#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct ResourceMonitorConfig {
pub enabled: bool,
pub granularity: MonitorGranularity,
pub sample_interval_seconds: i32,
pub generate_plots: bool,
}
impl Default for ResourceMonitorConfig {
fn default() -> Self {
ResourceMonitorConfig {
enabled: false,
granularity: MonitorGranularity::Summary,
sample_interval_seconds: 10,
generate_plots: false,
}
}
}
#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "snake_case")]
pub enum MonitorGranularity {
Summary,
TimeSeries,
}
/// Metrics collected for a single job
#[derive(Debug, Clone)]
pub struct JobMetrics {
pub peak_memory_bytes: u64,
pub avg_memory_bytes: u64,
pub peak_cpu_percent: f64,
pub avg_cpu_percent: f64,
sample_count: usize,
total_memory_bytes: u64,
total_cpu_percent: f64,
}
impl JobMetrics {
fn new() -> Self {
JobMetrics {
peak_memory_bytes: 0,
avg_memory_bytes: 0,
peak_cpu_percent: 0.0,
avg_cpu_percent: 0.0,
sample_count: 0,
total_memory_bytes: 0,
total_cpu_percent: 0.0,
}
}
/// Upper bound for a plausible CPU percentage. Even on a 1024-core node at
/// 100 % per core the value would be 102400 %. Anything above this threshold
/// is treated as a garbage sample (e.g. sstat returning stale data for an
/// OOM-killed step, or sysinfo reading /proc for a dying process).
const MAX_PLAUSIBLE_CPU_PERCENT: f64 = 100_000.0;
fn add_sample(&mut self, cpu_percent: f64, memory_bytes: u64) {
// Sanitize: reject garbage CPU values (NaN, infinity, negative, or unreasonably high).
let cpu_percent = if cpu_percent.is_finite()
&& (0.0..=Self::MAX_PLAUSIBLE_CPU_PERCENT).contains(&cpu_percent)
{
cpu_percent
} else {
0.0
};
self.sample_count += 1;
self.total_cpu_percent += cpu_percent;
self.total_memory_bytes += memory_bytes;
if cpu_percent > self.peak_cpu_percent {
self.peak_cpu_percent = cpu_percent;
}
if memory_bytes > self.peak_memory_bytes {
self.peak_memory_bytes = memory_bytes;
}
self.avg_cpu_percent = self.total_cpu_percent / self.sample_count as f64;
self.avg_memory_bytes = self.total_memory_bytes / self.sample_count as u64;
}
}
/// Source of resource samples for a monitored job.
enum MonitorJobSource {
/// Local execution: walk the process tree via sysinfo.
Local { pid: u32 },
/// Slurm step: poll `sstat` for live accounting data (TimeSeries mode only).
///
/// `prev_ave_cpu_s` and `prev_sample_at` are used to derive an instantaneous
/// CPU-utilisation rate from the monotonically-increasing `AveCPU` counter that
/// sstat returns.
Slurm {
slurm_job_id: String,
step_name: String,
/// Numeric step ID (e.g., "1") discovered via `squeue --steps`.
/// `None` until the step is registered in Slurm's accounting.
numeric_step_id: Option<String>,
/// AveCPU value (in seconds) from the previous sstat poll.
/// `None` until the first successful sstat sample (used to skip the first
/// sample whose cumulative AveCPU has no valid baseline for delta computation).
prev_ave_cpu_s: Option<f64>,
/// Wall-clock time of the previous sstat poll.
prev_sample_at: Instant,
},
}
/// Commands sent to the monitoring thread
enum MonitorCommand {
StartMonitoring {
pid: u32,
job_id: i64,
job_name: String,
},
/// Register a Slurm step for sstat-based monitoring (TimeSeries mode).
/// `pid` is the srun PID, used as the map key so that `stop_monitoring(pid)` works
/// without API changes.
StartMonitoringSlurm {
pid: u32,
slurm_job_id: String,
step_name: String,
/// Numeric step ID (e.g., "1") discovered at launch time. `None` if discovery
/// failed, in which case the monitor will attempt batch discovery via squeue --steps.
numeric_step_id: Option<String>,
job_id: i64,
job_name: String,
},
StopMonitoring {
pid: u32,
/// Channel to send back the collected metrics for this PID.
response_tx: Sender<Option<JobMetrics>>,
},
Shutdown,
}
/// Active job being monitored
struct MonitoredJob {
job_id: i64,
/// PID used as map key. For Slurm jobs this is the srun PID.
#[allow(dead_code)]
pid: u32,
source: MonitorJobSource,
metrics: JobMetrics,
}
/// Resource monitor manages a single background thread that monitors all running jobs
pub struct ResourceMonitor {
tx: Sender<MonitorCommand>,
handle: Option<JoinHandle<()>>,
config: ResourceMonitorConfig,
}
impl ResourceMonitor {
/// Create a new resource monitor
pub fn new(
config: ResourceMonitorConfig,
output_dir: PathBuf,
unique_label: String,
) -> Result<Self, Box<dyn std::error::Error>> {
let (tx, rx) = channel();
let config_clone = config.clone();
let handle = thread::spawn(move || {
if let Err(e) = run_monitoring_loop(config_clone, output_dir, unique_label, rx) {
error!("Resource monitoring thread failed: {}", e);
}
});
Ok(ResourceMonitor {
tx,
handle: Some(handle),
config,
})
}
/// Returns `true` when the monitor is configured for `TimeSeries` granularity.
pub fn is_timeseries(&self) -> bool {
matches!(self.config.granularity, MonitorGranularity::TimeSeries)
}
/// Start monitoring a local process (sysinfo process-tree walk).
pub fn start_monitoring(
&self,
pid: u32,
job_id: i64,
job_name: String,
) -> Result<(), Box<dyn std::error::Error>> {
self.tx.send(MonitorCommand::StartMonitoring {
pid,
job_id,
job_name,
})?;
debug!("Started monitoring job {} with PID {}", job_id, pid);
Ok(())
}
/// Register a Slurm step for sstat-based monitoring (`TimeSeries` mode only).
///
/// In `TimeSeries` mode the per-sample data is written to the time-series database,
/// enabling detailed resource utilization plots over time.
///
/// In `Summary` mode this method should **not** be called — sacct backfill after job
/// completion provides authoritative peak memory (MaxRSS) and average CPU data without
/// the overhead of periodic sstat/squeue polling.
///
/// `pid` must be the srun process PID so that the existing `stop_monitoring(pid)` API
/// continues to work without changes.
pub fn start_monitoring_slurm(
&self,
pid: u32,
slurm_job_id: String,
step_name: String,
numeric_step_id: Option<String>,
job_id: i64,
job_name: String,
) -> Result<(), Box<dyn std::error::Error>> {
self.tx.send(MonitorCommand::StartMonitoringSlurm {
pid,
slurm_job_id,
step_name,
numeric_step_id,
job_id,
job_name,
})?;
debug!(
"Started sstat monitoring for job {} (srun PID {})",
job_id, pid
);
Ok(())
}
/// Stop monitoring a process and return its metrics.
///
/// Sends a stop command to the monitoring thread and waits for it to return
/// the collected metrics via a response channel, with a 5-second timeout.
pub fn stop_monitoring(&self, pid: u32) -> Option<JobMetrics> {
let (response_tx, response_rx) = channel();
if let Err(e) = self
.tx
.send(MonitorCommand::StopMonitoring { pid, response_tx })
{
error!("Failed to send stop monitoring command: {}", e);
return None;
}
match response_rx.recv_timeout(Duration::from_secs(5)) {
Ok(metrics) => metrics,
Err(e) => {
warn!(
"Timed out or error waiting for metrics from monitoring thread for PID {}: {}",
pid, e
);
None
}
}
}
/// Shutdown the monitoring thread
pub fn shutdown(self) {
if let Err(e) = self.tx.send(MonitorCommand::Shutdown) {
error!("Failed to send shutdown command: {}", e);
return;
}
if let Some(handle) = self.handle {
// Wait up to 10 seconds for shutdown
let start = Instant::now();
while !handle.is_finished() && start.elapsed() < Duration::from_secs(10) {
thread::sleep(Duration::from_millis(100));
}
if !handle.is_finished() {
warn!("Resource monitor thread did not shutdown within 10 seconds");
} else {
let _ = handle.join();
info!("Resource monitor thread shutdown successfully");
}
}
}
}
/// Main monitoring loop that runs in a background thread
fn run_monitoring_loop(
config: ResourceMonitorConfig,
output_dir: PathBuf,
unique_label: String,
rx: Receiver<MonitorCommand>,
) -> Result<(), Box<dyn std::error::Error>> {
// Use new_with_specifics to only refresh processes, CPU, and memory, avoiding user enumeration
// which can crash on HPC systems with large LDAP user databases
let refresh_kind = RefreshKind::new()
.with_processes(ProcessRefreshKind::everything())
.with_cpu(CpuRefreshKind::everything())
.with_memory();
let mut sys = System::new_with_specifics(refresh_kind);
let mut monitored_jobs: HashMap<u32, MonitoredJob> = HashMap::new();
let sample_interval = Duration::from_secs(config.sample_interval_seconds as u64);
// Allow tests to substitute a fake sstat binary via TORC_FAKE_SSTAT.
let sstat_binary = std::env::var("TORC_FAKE_SSTAT").unwrap_or_else(|_| "sstat".to_string());
// Initialize database if using TimeSeries
let mut db_conn = match config.granularity {
MonitorGranularity::TimeSeries => Some(init_timeseries_db(&output_dir, &unique_label)?),
MonitorGranularity::Summary => None,
};
info!(
"Resource monitoring started: granularity={:?}, sample_interval={}s",
config.granularity, config.sample_interval_seconds
);
let mut last_sample_time = Instant::now();
loop {
// Process all pending commands (non-blocking)
while let Ok(cmd) = rx.try_recv() {
match cmd {
MonitorCommand::StartMonitoring {
pid,
job_id,
job_name,
} => {
// Store job metadata in database
if let Some(ref mut conn) = db_conn
&& let Err(e) = store_job_metadata(conn, job_id, &job_name)
{
error!("Failed to store job metadata for job {}: {}", job_id, e);
}
monitored_jobs.insert(
pid,
MonitoredJob {
job_id,
pid,
source: MonitorJobSource::Local { pid },
metrics: JobMetrics::new(),
},
);
debug!("Now monitoring {} jobs", monitored_jobs.len());
}
MonitorCommand::StartMonitoringSlurm {
pid,
slurm_job_id,
step_name,
numeric_step_id,
job_id,
job_name,
} => {
if let Some(ref mut conn) = db_conn
&& let Err(e) = store_job_metadata(conn, job_id, &job_name)
{
error!("Failed to store job metadata for job {}: {}", job_id, e);
}
monitored_jobs.insert(
pid,
MonitoredJob {
job_id,
pid,
source: MonitorJobSource::Slurm {
slurm_job_id,
step_name,
numeric_step_id,
prev_ave_cpu_s: None,
prev_sample_at: Instant::now(),
},
metrics: JobMetrics::new(),
},
);
debug!(
"Now monitoring {} jobs (Slurm sstat mode)",
monitored_jobs.len()
);
}
MonitorCommand::StopMonitoring { pid, response_tx } => {
let metrics = monitored_jobs.remove(&pid).map(|job| job.metrics);
debug!(
"Stopped monitoring PID {}, {} jobs remaining",
pid,
monitored_jobs.len()
);
// Send metrics back; ignore error if receiver was dropped.
let _ = response_tx.send(metrics);
}
MonitorCommand::Shutdown => {
info!("Resource monitor received shutdown command");
return Ok(());
}
}
}
// Sample all monitored jobs if interval has elapsed
if last_sample_time.elapsed() >= sample_interval && !monitored_jobs.is_empty() {
// Refresh sysinfo once for all local jobs.
let has_local_jobs = monitored_jobs
.values()
.any(|j| matches!(j.source, MonitorJobSource::Local { .. }));
if has_local_jobs {
sys.refresh_processes();
}
// Batch-discover numeric step IDs for any Slurm steps that need them.
// Run `squeue --steps` once per unique slurm_job_id (typically just one)
// instead of once per step.
let needs_discovery: Vec<String> = monitored_jobs
.values()
.filter_map(|j| match &j.source {
MonitorJobSource::Slurm {
slurm_job_id,
numeric_step_id: None,
..
} => Some(slurm_job_id.clone()),
_ => None,
})
.collect::<std::collections::HashSet<_>>()
.into_iter()
.collect();
for job_id in &needs_discovery {
let step_map = discover_step_ids(job_id);
for job in monitored_jobs.values_mut() {
if let MonitorJobSource::Slurm {
slurm_job_id,
step_name,
numeric_step_id,
..
} = &mut job.source
&& numeric_step_id.is_none()
&& slurm_job_id == job_id
&& let Some(id) = step_map.get(step_name.as_str())
{
debug!(
"Discovered numeric step ID for {}: {}.{}",
step_name, slurm_job_id, id
);
*numeric_step_id = Some(id.clone());
}
}
}
// Batch-fetch sstat data for all Slurm steps in a single subprocess call.
let slurm_steps: Vec<String> = monitored_jobs
.values()
.filter_map(|j| match &j.source {
MonitorJobSource::Slurm {
slurm_job_id,
numeric_step_id: Some(step_id),
..
} => Some(format!("{}.{}", slurm_job_id, step_id)),
_ => None,
})
.collect();
let sstat_data = collect_sstat_samples_batch(&slurm_steps, &sstat_binary);
// Capture a single timestamp right after the batch call so that all Slurm
// steps use the same reference point for elapsed-time / CPU% calculations.
// Using per-job Instant::now() would skew deltas as the loop iterates.
let sstat_sample_at = Instant::now();
if !slurm_steps.is_empty() {
debug!(
"Batched sstat query for {} steps, got {} results",
slurm_steps.len(),
sstat_data.len()
);
}
let timestamp = chrono::Utc::now().timestamp();
for (pid, job) in monitored_jobs.iter_mut() {
let (cpu_percent, memory_bytes, num_processes) = match &mut job.source {
MonitorJobSource::Local { pid: local_pid } => {
collect_process_tree_stats(*local_pid, &sys)
}
MonitorJobSource::Slurm {
slurm_job_id,
numeric_step_id,
prev_ave_cpu_s,
prev_sample_at,
..
} => {
let step_id = match numeric_step_id {
Some(id) => id.as_str(),
None => {
// Step not registered yet; skip this sample.
continue;
}
};
let job_step = format!("{}.{}", slurm_job_id, step_id);
let (ave_cpu_s, max_rss) = match sstat_data.get(&job_step) {
Some(sample) => *sample,
None => {
// sstat returned no data for this step; skip.
continue;
}
};
let elapsed_s = sstat_sample_at
.duration_since(*prev_sample_at)
.as_secs_f64();
*prev_sample_at = sstat_sample_at;
// On the first successful sample, AveCPU is cumulative since step
// start and we have no valid baseline for delta computation.
// Record the baseline and skip.
if prev_ave_cpu_s.is_none() {
*prev_ave_cpu_s = Some(ave_cpu_s);
continue;
}
let cpu_percent = if elapsed_s > 0.0 {
((ave_cpu_s - prev_ave_cpu_s.unwrap_or(0.0)) / elapsed_s * 100.0)
.max(0.0)
} else {
0.0
};
*prev_ave_cpu_s = Some(ave_cpu_s);
debug!(
"sstat sample for step {}: AveCPU={:.3}s => cpu_pct={:.1}%, \
MaxRSS={}B",
job_step, ave_cpu_s, cpu_percent, max_rss
);
(cpu_percent, max_rss, 1)
}
};
job.metrics.add_sample(cpu_percent, memory_bytes);
// Store in database if using TimeSeries
if let Some(ref mut conn) = db_conn
&& let Err(e) = store_sample(
conn,
job.job_id,
timestamp,
cpu_percent,
memory_bytes,
num_processes,
)
{
error!("Failed to store sample for job {}: {}", job.job_id, e);
}
debug!(
"Job {} (PID {}): CPU={:.1}%, Mem={:.1}MB, Procs={}",
job.job_id,
pid,
cpu_percent,
memory_bytes as f64 / (1024.0 * 1024.0),
num_processes
);
}
last_sample_time = Instant::now();
}
// Sleep briefly to avoid busy-waiting
thread::sleep(Duration::from_millis(100));
}
}
/// Collect CPU and memory stats for a process and all its children
fn collect_process_tree_stats(root_pid: u32, sys: &System) -> (f64, u64, usize) {
let mut pids_to_check = vec![Pid::from(root_pid as usize)];
let mut visited = HashSet::new();
let mut total_cpu = 0.0;
let mut total_memory = 0;
while let Some(pid) = pids_to_check.pop() {
if visited.contains(&pid) {
continue;
}
visited.insert(pid);
if let Some(process) = sys.process(pid) {
total_cpu += process.cpu_usage() as f64;
total_memory += process.memory(); // sysinfo already gives bytes
// Find all children of this process
for (child_pid, child_proc) in sys.processes() {
if child_proc.parent() == Some(pid) && !visited.contains(child_pid) {
pids_to_check.push(*child_pid);
}
}
}
}
(total_cpu, total_memory, visited.len())
}
/// Discover the numeric step ID for a named step, retrying a few times for Slurm registration.
///
/// Called at srun launch time. Returns `None` if the step doesn't appear within ~1 second.
pub fn discover_step_id_with_retries(slurm_job_id: &str, step_name: &str) -> Option<String> {
for attempt in 0..5 {
let map = discover_step_ids(slurm_job_id);
if let Some(id) = map.get(step_name) {
return Some(id.clone());
}
if attempt < 4 {
std::thread::sleep(std::time::Duration::from_millis(200));
}
}
debug!(
"Could not discover numeric step ID for {} in job {} after retries",
step_name, slurm_job_id
);
None
}
/// Discover numeric step IDs for all steps in a Slurm job via `squeue --steps`.
///
/// Slurm assigns numeric IDs to steps (e.g., `.0`, `.1`, `.2`). Our srun commands set
/// `--job-name=<step_name>` which appears in squeue output. We parse that to build a
/// map from step name to numeric ID, since `sstat` requires numeric step IDs on HPE Cray
/// and other Slurm installations.
///
/// Returns an empty map if squeue fails or no steps are found.
fn discover_step_ids(slurm_job_id: &str) -> HashMap<String, String> {
let mut map = HashMap::new();
// squeue --steps is much more compact than scontrol show step, producing one
// line per step instead of a verbose multi-line block. Critical for allocations
// with thousands of concurrent steps.
let output = match std::process::Command::new("squeue")
.args(["--steps", "-j", slurm_job_id, "-o", "%i|%j", "--noheader"])
.output()
{
Ok(o) => o,
Err(e) => {
debug!(
"squeue --steps for job {} failed to execute: {}",
slurm_job_id, e
);
return map;
}
};
if !output.status.success() {
debug!(
"squeue --steps for job {} returned non-zero: {}",
slurm_job_id,
String::from_utf8_lossy(&output.stderr).trim()
);
return map;
}
let stdout = String::from_utf8_lossy(&output.stdout);
// Each line is "<jobid>.<stepid>|<stepname>", e.g., "12893801.2|my-sleep-job"
for line in stdout.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
if let Some((step_full_id, name)) = line.split_once('|') {
// Extract the part after the dot: "12893801.2" -> "2"
if let Some(numeric_id) = step_full_id.split('.').nth(1) {
map.insert(name.to_string(), numeric_id.to_string());
}
}
}
map
}
/// Raw sstat data for a single step: `(ave_cpu_seconds, max_rss_bytes)`.
type SstatRawSample = (f64, u64);
/// Poll `sstat` for multiple Slurm steps in a single subprocess invocation and return
/// the raw accounting counters keyed by `"jobid.stepid"`.
///
/// This batches all step queries into one `sstat -j <comma-separated-steps>` call,
/// dramatically reducing subprocess overhead when monitoring many concurrent jobs.
///
/// Returns an empty map when sstat is unavailable or returns no data.
fn collect_sstat_samples_batch(
job_steps: &[String],
sstat_binary: &str,
) -> HashMap<String, SstatRawSample> {
let mut results = HashMap::new();
if job_steps.is_empty() {
return results;
}
let steps_arg = job_steps.join(",");
let output = match std::process::Command::new(sstat_binary)
.args([
"-j",
&steps_arg,
"--format",
"JobID,AveCPU,MaxRSS",
"-P", // pipe-separated
"-n", // no header
])
.output()
{
Ok(o) => o,
Err(e) => {
debug!("sstat batch query failed to execute: {}", e);
return results;
}
};
if !output.status.success() {
debug!(
"sstat batch query returned non-zero for steps [{}]: {}",
steps_arg,
String::from_utf8_lossy(&output.stderr).trim()
);
return results;
}
let stdout = String::from_utf8_lossy(&output.stdout);
debug!("sstat batch output: {:?}", stdout.trim());
// Each line is "jobid.stepid|AveCPU|MaxRSS", e.g., "12893801.1|00:01:30|1024K"
for line in stdout.lines() {
let fields: Vec<&str> = line.split('|').collect();
if fields.len() < 3 {
continue;
}
let job_step_id = fields[0].trim();
let ave_cpu_s = parse_slurm_cpu_time(fields[1]).unwrap_or(0.0);
let max_rss = parse_slurm_memory(fields[2]).unwrap_or(0).max(0) as u64;
results.insert(job_step_id.to_string(), (ave_cpu_s, max_rss));
}
results
}
/// Initialize the TimeSeries database
fn init_timeseries_db(output_dir: &Path, unique_label: &str) -> SqliteResult<Connection> {
// Create resource_utilization subdirectory
let resource_util_dir = output_dir.join("resource_utilization");
if let Err(e) = std::fs::create_dir_all(&resource_util_dir) {
error!("Failed to create resource_utilization directory: {}", e);
return Err(rusqlite::Error::InvalidPath(resource_util_dir.clone()));
}
let db_path = resource_util_dir.join(format!("{}_{}.db", DB_FILENAME_PREFIX, unique_label));
info!(
"Initializing resource metrics database at: {}",
db_path.display()
);
let conn = Connection::open(&db_path)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS job_resource_samples (
job_id INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
cpu_percent REAL NOT NULL,
memory_bytes INTEGER NOT NULL,
num_processes INTEGER NOT NULL,
PRIMARY KEY (job_id, timestamp)
)",
[],
)?;
conn.execute(
"CREATE INDEX IF NOT EXISTS idx_job_resource_samples_job_id
ON job_resource_samples(job_id)",
[],
)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS job_metadata (
job_id INTEGER PRIMARY KEY,
job_name TEXT NOT NULL
)",
[],
)?;
Ok(conn)
}
/// Store job metadata in the TimeSeries database
fn store_job_metadata(conn: &mut Connection, job_id: i64, job_name: &str) -> SqliteResult<()> {
conn.execute(
"INSERT OR REPLACE INTO job_metadata (job_id, job_name)
VALUES (?1, ?2)",
rusqlite::params![job_id, job_name],
)?;
Ok(())
}
/// Store a sample in the TimeSeries database
fn store_sample(
conn: &mut Connection,
job_id: i64,
timestamp: i64,
cpu_percent: f64,
memory_bytes: u64,
num_processes: usize,
) -> SqliteResult<()> {
conn.execute(
"INSERT INTO job_resource_samples (job_id, timestamp, cpu_percent, memory_bytes, num_processes)
VALUES (?1, ?2, ?3, ?4, ?5)",
rusqlite::params![job_id, timestamp, cpu_percent, memory_bytes as i64, num_processes as i64],
)?;
Ok(())
}