@@ -154,6 +154,7 @@ where
154
154
155
155
pub struct PsOptions < ' a > {
156
156
pub rollup : bool ,
157
+ pub always_print_something : bool ,
157
158
pub min_cpu_percent : Option < f64 > ,
158
159
pub min_mem_percent : Option < f64 > ,
159
160
pub min_cpu_time : Option < usize > ,
@@ -323,6 +324,8 @@ pub fn create_snapshot(
323
324
opts
324
325
} ;
325
326
327
+ let mut did_print = false ;
328
+ let must_print = opts. always_print_something ;
326
329
if opts. rollup {
327
330
// This is a little complicated because jobs with job_id 0 cannot be rolled up.
328
331
//
@@ -372,14 +375,37 @@ pub fn create_snapshot(
372
375
}
373
376
}
374
377
for r in rolledup {
375
- print_record ( & mut writer, & print_params, & r) ;
378
+ did_print = print_record ( & mut writer, & print_params, & r, false ) || did_print ;
376
379
}
377
380
} else {
378
381
for ( _, proc_info) in proc_by_pid {
379
- print_record ( & mut writer, & print_params, & proc_info) ;
382
+ did_print = print_record ( & mut writer, & print_params, & proc_info, false ) || did_print ;
380
383
}
381
384
}
382
385
386
+ if !did_print && must_print {
387
+ // Print a synthetic record
388
+ let synth = ProcInfo {
389
+ user : "_sonar_" ,
390
+ _uid : 0 ,
391
+ command : "_heartbeat_" ,
392
+ pid : 0 ,
393
+ rolledup : 0 ,
394
+ is_system_job : true ,
395
+ job_id : 0 ,
396
+ cpu_percentage : 0.0 ,
397
+ cputime_sec : 0 ,
398
+ mem_percentage : 0.0 ,
399
+ mem_size_kib : 0 ,
400
+ gpu_cards : empty_gpuset ( ) ,
401
+ gpu_percentage : 0.0 ,
402
+ gpu_mem_percentage : 0.0 ,
403
+ gpu_mem_size_kib : 0 ,
404
+ gpu_status : GpuStatus :: Ok ,
405
+ } ;
406
+ print_record ( & mut writer, & print_params, & synth, true ) ;
407
+ }
408
+
383
409
writer. flush ( ) . unwrap ( ) ;
384
410
}
385
411
@@ -391,7 +417,7 @@ struct PrintParameters<'a> {
391
417
opts : & ' a PsOptions < ' a >
392
418
}
393
419
394
- fn print_record < W : io:: Write > ( writer : & mut Writer < W > , params : & PrintParameters , proc_info : & ProcInfo ) {
420
+ fn print_record < W : io:: Write > ( writer : & mut Writer < W > , params : & PrintParameters , proc_info : & ProcInfo , must_print : bool ) -> bool {
395
421
let mut included = false ;
396
422
397
423
// The logic here is that if any of the inclusion filters are provided, then the set of those
@@ -419,8 +445,8 @@ fn print_record<W: io::Write>(writer: &mut Writer<W>, params: &PrintParameters,
419
445
included = true ;
420
446
}
421
447
422
- if !included {
423
- return ;
448
+ if !included && !must_print {
449
+ return false ;
424
450
}
425
451
426
452
// The exclusion filters apply after the inclusion filters and the record must pass all of the
@@ -440,8 +466,8 @@ fn print_record<W: io::Write>(writer: &mut Writer<W>, params: &PrintParameters,
440
466
}
441
467
}
442
468
443
- if !included {
444
- return ;
469
+ if !included && !must_print {
470
+ return false ;
445
471
}
446
472
447
473
let gpus_comma_separated =
@@ -483,4 +509,5 @@ fn print_record<W: io::Write>(writer: &mut Writer<W>, params: &PrintParameters,
483
509
fields. push ( format ! ( "rolledup={}" , proc_info. rolledup) ) ;
484
510
}
485
511
writer. write_record ( fields) . unwrap ( ) ;
512
+ return true ;
486
513
}
0 commit comments