Skip to content

Commit bf13a70

Browse files
committed
timeout: discard the self-sent SIGTERM between benchmark iterations
1 parent 2b8b9fa commit bf13a70

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

‎src/uu/timeout/benches/timeout_bench.rs‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ fn self_exe() -> String {
1919
.into_owned()
2020
}
2121

22+
/// On timeout, `timeout` also sends SIGTERM to its own process group, so the
23+
/// bench process gets one too. `timeout` keeps SIGTERM blocked, so the signal
24+
/// stays pending and the next iteration would see it straight away and stop
25+
/// before its timer fires. Setting the action to `SIG_IGN` drops it.
26+
#[cfg(unix)]
27+
fn discard_pending_sigterm() {
28+
// SAFETY: plain libc calls; the previous action is restored straight away.
29+
unsafe {
30+
let previous = libc::signal(libc::SIGTERM, libc::SIG_IGN);
31+
libc::signal(libc::SIGTERM, previous);
32+
}
33+
}
34+
35+
#[cfg(not(unix))]
36+
fn discard_pending_sigterm() {}
37+
2238
/// Benchmark the fast path where the command exits immediately.
2339
#[cfg(unix)]
2440
#[divan::bench]
@@ -40,25 +56,19 @@ fn timeout_quick_exit(bencher: Bencher) {
4056
.bench_values(|args| black_box(uumain(args)));
4157
}
4258

43-
/// Benchmark a command that runs longer than the threshold and receives the default signal.
44-
#[cfg(unix)]
45-
#[divan::bench]
46-
fn timeout_enforced(bencher: Bencher) {
47-
let args = get_bench_args(["0.02", "sleep", "0.2"]);
48-
bencher
49-
.with_inputs(|| args.clone().into_iter())
50-
.bench_values(|args| black_box(uumain(args)));
51-
}
52-
5359
/// Benchmark a command that runs longer than the threshold: timer expiry,
54-
/// job-object tree kill and exit-code plumbing.
55-
#[cfg(windows)]
60+
/// signal delivery and exit-code plumbing. The child is this binary rather
61+
/// than `sleep`, so it is always idle when killed and allocates the same
62+
/// amount on every run.
5663
#[divan::bench]
5764
fn timeout_enforced(bencher: Bencher) {
5865
let exe = self_exe();
5966
let args = get_bench_args(["0.02", exe.as_str(), CHILD_MARKER, "0.2"]);
6067
bencher
61-
.with_inputs(|| args.clone().into_iter())
68+
.with_inputs(|| {
69+
discard_pending_sigterm();
70+
args.clone().into_iter()
71+
})
6272
.bench_values(|args| black_box(uumain(args)));
6373
}
6474

@@ -71,7 +81,10 @@ fn timer_expiry_latency(bencher: Bencher, duration: &str) {
7181
let exe = self_exe();
7282
let args = get_bench_args([duration, exe.as_str(), CHILD_MARKER, "5"]);
7383
bencher
74-
.with_inputs(|| args.clone().into_iter())
84+
.with_inputs(|| {
85+
discard_pending_sigterm();
86+
args.clone().into_iter()
87+
})
7588
.bench_values(|args| black_box(uumain(args)));
7689
}
7790

0 commit comments

Comments
 (0)