Skip to content

Commit 4815fef

Browse files
authored
Merge pull request #112 from lars-t-hansen/w-107-always-print-something
Fix #107: Always print at least one record
2 parents 4f88d61 + 697b519 commit 4815fef

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fn main() {
8383
} => {
8484
let opts = ps::PsOptions {
8585
rollup: *rollup,
86+
always_print_something: true,
8687
min_cpu_percent: *min_cpu_percent,
8788
min_mem_percent: *min_mem_percent,
8889
min_cpu_time: *min_cpu_time,

src/ps.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ where
154154

155155
pub struct PsOptions<'a> {
156156
pub rollup: bool,
157+
pub always_print_something: bool,
157158
pub min_cpu_percent: Option<f64>,
158159
pub min_mem_percent: Option<f64>,
159160
pub min_cpu_time: Option<usize>,
@@ -323,6 +324,8 @@ pub fn create_snapshot(
323324
opts
324325
};
325326

327+
let mut did_print = false;
328+
let must_print = opts.always_print_something;
326329
if opts.rollup {
327330
// This is a little complicated because jobs with job_id 0 cannot be rolled up.
328331
//
@@ -372,14 +375,37 @@ pub fn create_snapshot(
372375
}
373376
}
374377
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;
376379
}
377380
} else {
378381
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;
380383
}
381384
}
382385

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+
383409
writer.flush().unwrap();
384410
}
385411

@@ -391,7 +417,7 @@ struct PrintParameters<'a> {
391417
opts: &'a PsOptions<'a>
392418
}
393419

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 {
395421
let mut included = false;
396422

397423
// 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,
419445
included = true;
420446
}
421447

422-
if !included {
423-
return;
448+
if !included && !must_print {
449+
return false;
424450
}
425451

426452
// 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,
440466
}
441467
}
442468

443-
if !included {
444-
return;
469+
if !included && !must_print {
470+
return false;
445471
}
446472

447473
let gpus_comma_separated =
@@ -483,4 +509,5 @@ fn print_record<W: io::Write>(writer: &mut Writer<W>, params: &PrintParameters,
483509
fields.push(format!("rolledup={}", proc_info.rolledup));
484510
}
485511
writer.write_record(fields).unwrap();
512+
return true;
486513
}

0 commit comments

Comments
 (0)