Skip to content

Commit 273e131

Browse files
authored
Merge pull request #1652 from Kobzol/timeout-async
Add compilation benchmark timeout
2 parents 9e94ced + f61d018 commit 273e131

File tree

10 files changed

+503
-424
lines changed

10 files changed

+503
-424
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collector/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ semver = "1.0"
2121
reqwest = { version = "0.11", features = ["json"] }
2222
xz2 = "0.1.3"
2323
tar = "0.4"
24-
tokio = { version = "1.6", features = ["rt"] }
24+
tokio = { version = "1.6", features = ["rt", "process"] }
2525
database = { path = "../database" }
2626
intern = { path = "../intern" }
2727
futures = "0.3.5"

collector/src/bin/collector.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ use std::cmp::Ordering;
1717
use std::ffi::OsStr;
1818
use std::fs;
1919
use std::fs::File;
20+
use std::future::Future;
2021
use std::io::BufWriter;
2122
use std::io::Write;
2223
use std::path::{Path, PathBuf};
2324
use std::process;
2425
use std::process::{Command, Stdio};
26+
use std::time::Duration;
2527
use std::{str, time::Instant};
2628
use tokio::runtime::Runtime;
2729

@@ -34,6 +36,7 @@ use collector::runtime::{
3436
use collector::toolchain::{
3537
create_toolchain_from_published_version, get_local_toolchain, Sysroot, Toolchain,
3638
};
39+
use collector::utils::wait_for_future;
3740

3841
fn n_normal_benchmarks_remaining(n: usize) -> String {
3942
let suffix = if n == 1 { "" } else { "s" };
@@ -262,7 +265,13 @@ fn profile(
262265
let benchmark_id = format!("{} ({}/{})", benchmark.name, i + 1, benchmarks.len());
263266
eprintln!("Executing benchmark {benchmark_id}");
264267
let mut processor = ProfileProcessor::new(profiler, out_dir, &toolchain.id);
265-
let result = benchmark.measure(&mut processor, profiles, scenarios, toolchain, Some(1));
268+
let result = wait_for_future(benchmark.measure(
269+
&mut processor,
270+
profiles,
271+
scenarios,
272+
toolchain,
273+
Some(1),
274+
));
266275
eprintln!("Finished benchmark {benchmark_id}");
267276

268277
if let Err(ref s) = result {
@@ -611,6 +620,7 @@ fn main_result() -> anyhow::Result<i32> {
611620
builder
612621
.worker_threads(1)
613622
.max_blocking_threads(1)
623+
.enable_time()
614624
.enable_io();
615625
let mut rt = builder.build().expect("built runtime");
616626

@@ -1077,6 +1087,18 @@ fn bench_published_artifact(
10771087
)
10781088
}
10791089

1090+
const COMPILE_BENCHMARK_TIMEOUT: Duration = Duration::from_secs(60 * 30);
1091+
1092+
async fn with_timeout<F: Future<Output = anyhow::Result<()>>>(fut: F) -> anyhow::Result<()> {
1093+
match tokio::time::timeout(COMPILE_BENCHMARK_TIMEOUT, fut).await {
1094+
Ok(res) => res,
1095+
Err(_) => Err(anyhow::anyhow!(
1096+
"Benchmark timeouted in {} seconds",
1097+
COMPILE_BENCHMARK_TIMEOUT.as_secs()
1098+
)),
1099+
}
1100+
}
1101+
10801102
/// Perform compile benchmarks.
10811103
fn bench_compile(
10821104
rt: &mut Runtime,
@@ -1121,7 +1143,6 @@ fn bench_compile(
11211143
print_intro();
11221144

11231145
let mut processor = BenchProcessor::new(
1124-
rt,
11251146
tx.conn(),
11261147
benchmark_name,
11271148
&shared.artifact_id,
@@ -1157,13 +1178,14 @@ fn bench_compile(
11571178
)
11581179
},
11591180
&|processor| {
1160-
benchmark.measure(
1181+
rt.block_on(with_timeout(benchmark.measure(
11611182
processor,
11621183
&config.profiles,
11631184
&config.scenarios,
11641185
&shared.toolchain,
11651186
config.iterations,
1166-
)
1187+
)))
1188+
.with_context(|| anyhow::anyhow!("Cannot compile {}", benchmark.name))
11671189
},
11681190
)
11691191
}
@@ -1175,8 +1197,7 @@ fn bench_compile(
11751197
Category::Primary,
11761198
&|| eprintln!("Special benchmark commencing (due to `--bench-rustc`)"),
11771199
&|processor| {
1178-
processor
1179-
.measure_rustc(&shared.toolchain)
1200+
rt.block_on(with_timeout(processor.measure_rustc(&shared.toolchain)))
11801201
.context("measure rustc")
11811202
},
11821203
);

collector/src/compile/benchmark/mod.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::compile::benchmark::profile::Profile;
44
use crate::compile::benchmark::scenario::Scenario;
55
use crate::compile::execute::{CargoProcess, Processor};
66
use crate::toolchain::Toolchain;
7+
use crate::utils::wait_for_future;
78
use anyhow::{bail, Context};
89
use log::debug;
910
use std::collections::{HashMap, HashSet};
@@ -189,7 +190,7 @@ impl Benchmark {
189190
}
190191

191192
/// Run a specific benchmark under a processor + profiler combination.
192-
pub fn measure(
193+
pub async fn measure(
193194
&self,
194195
processor: &mut dyn Processor,
195196
profiles: &[Profile],
@@ -261,9 +262,11 @@ impl Benchmark {
261262
for (profile, prep_dir) in &profile_dirs {
262263
let server = server.clone();
263264
let thread = s.spawn::<_, anyhow::Result<()>>(move || {
264-
self.mk_cargo_process(toolchain, prep_dir.path(), *profile)
265-
.jobserver(server)
266-
.run_rustc(false)?;
265+
wait_for_future(
266+
self.mk_cargo_process(toolchain, prep_dir.path(), *profile)
267+
.jobserver(server)
268+
.run_rustc(false),
269+
)?;
267270
Ok(())
268271
});
269272
threads.push(thread);
@@ -310,7 +313,8 @@ impl Benchmark {
310313
if scenarios.contains(&Scenario::Full) {
311314
self.mk_cargo_process(toolchain, cwd, profile)
312315
.processor(processor, Scenario::Full, "Full", None)
313-
.run_rustc(true)?;
316+
.run_rustc(true)
317+
.await?;
314318
}
315319

316320
// Rustdoc does not support incremental compilation
@@ -321,15 +325,17 @@ impl Benchmark {
321325
self.mk_cargo_process(toolchain, cwd, profile)
322326
.incremental(true)
323327
.processor(processor, Scenario::IncrFull, "IncrFull", None)
324-
.run_rustc(true)?;
328+
.run_rustc(true)
329+
.await?;
325330
}
326331

327332
// An incremental build with no changes (fastest incremental case).
328333
if scenarios.contains(&Scenario::IncrUnchanged) {
329334
self.mk_cargo_process(toolchain, cwd, profile)
330335
.incremental(true)
331336
.processor(processor, Scenario::IncrUnchanged, "IncrUnchanged", None)
332-
.run_rustc(true)?;
337+
.run_rustc(true)
338+
.await?;
333339
}
334340

335341
if scenarios.contains(&Scenario::IncrPatched) {
@@ -348,7 +354,8 @@ impl Benchmark {
348354
&scenario_str,
349355
Some(patch),
350356
)
351-
.run_rustc(true)?;
357+
.run_rustc(true)
358+
.await?;
352359
}
353360
}
354361
}

0 commit comments

Comments
 (0)