Skip to content

Commit 076b1f4

Browse files
authored
Merge pull request #1604 from Kobzol/runtime-db
Store more runtime benchmark information into DB
2 parents 2cabb6a + 25c6d3c commit 076b1f4

File tree

4 files changed

+268
-80
lines changed

4 files changed

+268
-80
lines changed

collector/src/bin/collector.rs

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use collector::benchmark::scenario::Scenario;
1010
use collector::benchmark::{
1111
compile_benchmark_dir, get_compile_benchmarks, runtime_benchmark_dir, Benchmark, BenchmarkName,
1212
};
13-
use collector::utils;
14-
use database::{ArtifactId, Commit, CommitType, Pool};
13+
use collector::{runtime, utils, CollectorCtx, CollectorStepBuilder};
14+
use database::{ArtifactId, Commit, CommitType, Connection, Pool};
1515
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator};
1616
use std::ffi::OsStr;
1717
use std::fs;
@@ -59,21 +59,19 @@ impl BenchmarkErrors {
5959

6060
fn bench(
6161
rt: &mut Runtime,
62-
pool: database::Pool,
63-
artifact_id: &ArtifactId,
62+
mut conn: Box<dyn Connection>,
6463
profiles: &[Profile],
6564
scenarios: &[Scenario],
66-
bench_rustc: bool,
6765
compiler: Compiler<'_>,
6866
benchmarks: &[Benchmark],
6967
iterations: Option<usize>,
7068
is_self_profile: bool,
69+
mut collector: CollectorCtx,
7170
) -> BenchmarkErrors {
72-
let mut conn = rt.block_on(pool.connection());
7371
let mut errors = BenchmarkErrors::new();
7472
eprintln!(
7573
"Benchmarking {} for triple {}",
76-
artifact_id, compiler.triple
74+
collector.artifact_id, compiler.triple
7775
);
7876

7977
if is_self_profile {
@@ -82,24 +80,7 @@ fn bench(
8280
}
8381
}
8482

85-
let mut steps = benchmarks
86-
.iter()
87-
.map(|b| b.name.to_string())
88-
.collect::<Vec<_>>();
89-
if bench_rustc {
90-
steps.push("rustc".to_string());
91-
}
92-
93-
// Make sure there is no observable time when the artifact ID is available
94-
// but the in-progress steps are not.
95-
let artifact_row_id = {
96-
let mut tx = rt.block_on(conn.transaction());
97-
let artifact_row_id = rt.block_on(tx.conn().artifact_id(&artifact_id));
98-
rt.block_on(tx.conn().collector_start(artifact_row_id, &steps));
99-
100-
rt.block_on(tx.commit()).unwrap();
101-
artifact_row_id
102-
};
83+
let bench_rustc = collector.bench_rustc;
10384

10485
let start = Instant::now();
10586
let mut skipped = false;
@@ -109,8 +90,7 @@ fn bench(
10990
category: Category,
11091
print_intro: &dyn Fn(),
11192
measure: &dyn Fn(&mut BenchProcessor) -> anyhow::Result<()>| {
112-
let is_fresh =
113-
rt.block_on(conn.collector_start_step(artifact_row_id, &benchmark_name.0));
93+
let is_fresh = rt.block_on(collector.start_compile_step(conn.as_mut(), benchmark_name));
11494
if !is_fresh {
11595
skipped = true;
11696
eprintln!("skipping {} -- already benchmarked", benchmark_name);
@@ -129,8 +109,8 @@ fn bench(
129109
rt,
130110
tx.conn(),
131111
benchmark_name,
132-
&artifact_id,
133-
artifact_row_id,
112+
&collector.artifact_id,
113+
collector.artifact_row_id,
134114
is_self_profile,
135115
);
136116
let result = measure(&mut processor);
@@ -141,15 +121,12 @@ fn bench(
141121
);
142122
errors.incr();
143123
rt.block_on(tx.conn().record_error(
144-
artifact_row_id,
124+
collector.artifact_row_id,
145125
&benchmark_name.0,
146126
&format!("{:?}", s),
147127
));
148128
};
149-
rt.block_on(
150-
tx.conn()
151-
.collector_end_step(artifact_row_id, &benchmark_name.0),
152-
);
129+
rt.block_on(collector.end_compile_step(tx.conn(), benchmark_name));
153130
rt.block_on(tx.commit()).expect("committed");
154131
};
155132

@@ -188,7 +165,7 @@ fn bench(
188165
if skipped {
189166
log::info!("skipping duration record -- skipped parts of run");
190167
} else {
191-
rt.block_on(conn.record_duration(artifact_row_id, end));
168+
rt.block_on(conn.record_duration(collector.artifact_row_id, end));
192169
}
193170

194171
rt.block_on(async move {
@@ -733,12 +710,21 @@ fn main_result() -> anyhow::Result<i32> {
733710
)?;
734711
let pool = Pool::open(&db.db);
735712

713+
let suite = runtime::discover_benchmarks(&toolchain, &runtime_benchmark_dir)?;
714+
let artifact_id = ArtifactId::Tag(toolchain.id.clone());
715+
let (conn, collector) = rt.block_on(async {
716+
let mut conn = pool.connection().await;
717+
let collector = CollectorStepBuilder::default()
718+
.record_runtime_benchmarks(&suite)
719+
.start_collection(conn.as_mut(), artifact_id)
720+
.await;
721+
(conn, collector)
722+
});
736723
let fut = bench_runtime(
737-
pool,
738-
ArtifactId::Tag(toolchain.id.clone()),
739-
toolchain,
724+
conn,
725+
suite,
726+
collector,
740727
BenchmarkFilter::new(local.exclude, local.include),
741-
runtime_benchmark_dir,
742728
iterations,
743729
);
744730
rt.block_on(fut)?;
@@ -774,17 +760,24 @@ fn main_result() -> anyhow::Result<i32> {
774760
)?;
775761
benchmarks.retain(|b| b.category().is_primary_or_secondary());
776762

763+
let artifact_id = ArtifactId::Tag(toolchain.id.clone());
764+
let (conn, collector) = rt.block_on(init_compile_collector(
765+
&pool,
766+
&benchmarks,
767+
bench_rustc.bench_rustc,
768+
artifact_id,
769+
));
770+
777771
let res = bench(
778772
&mut rt,
779-
pool,
780-
&ArtifactId::Tag(toolchain.id.clone()),
773+
conn,
781774
&profiles,
782775
&scenarios,
783-
bench_rustc.bench_rustc,
784776
Compiler::from_toolchain(&toolchain, &target_triple),
785777
&benchmarks,
786778
Some(iterations),
787779
self_profile.self_profile,
780+
collector,
788781
);
789782
res.fail_if_nonzero()?;
790783
Ok(0)
@@ -841,17 +834,23 @@ fn main_result() -> anyhow::Result<i32> {
841834
)?;
842835
benchmarks.retain(|b| b.category().is_primary_or_secondary());
843836

837+
let artifact_id = ArtifactId::Commit(commit);
838+
let (conn, collector) = rt.block_on(init_compile_collector(
839+
&pool,
840+
&benchmarks,
841+
bench_rustc.bench_rustc,
842+
artifact_id,
843+
));
844844
let res = bench(
845845
&mut rt,
846-
pool,
847-
&ArtifactId::Commit(commit),
846+
conn,
848847
&Profile::all(),
849848
&Scenario::all(),
850-
bench_rustc.bench_rustc,
851849
Compiler::from_sysroot(&sysroot),
852850
&benchmarks,
853851
runs.map(|v| v as usize),
854852
self_profile.self_profile,
853+
collector,
855854
);
856855

857856
client.post(&format!("{}/perf/onpush", site_url)).send()?;
@@ -1014,6 +1013,20 @@ fn main_result() -> anyhow::Result<i32> {
10141013
}
10151014
}
10161015

1016+
async fn init_compile_collector(
1017+
pool: &database::Pool,
1018+
benchmarks: &[Benchmark],
1019+
bench_rustc: bool,
1020+
artifact_id: ArtifactId,
1021+
) -> (Box<dyn Connection>, CollectorCtx) {
1022+
let mut conn = pool.connection().await;
1023+
let collector = CollectorStepBuilder::default()
1024+
.record_compile_benchmarks(&benchmarks, bench_rustc)
1025+
.start_collection(conn.as_mut(), artifact_id)
1026+
.await;
1027+
(conn, collector)
1028+
}
1029+
10171030
fn bench_published_artifact(
10181031
toolchain: String,
10191032
pool: Pool,
@@ -1061,13 +1074,18 @@ fn bench_published_artifact(
10611074
let mut benchmarks = get_compile_benchmarks(&benchmark_dir, None, None, None)?;
10621075
benchmarks.retain(|b| b.category().is_stable());
10631076

1077+
let artifact_id = ArtifactId::Tag(toolchain);
1078+
let (conn, collector) = rt.block_on(init_compile_collector(
1079+
&pool,
1080+
&benchmarks,
1081+
/* bench_rustc */ false,
1082+
artifact_id,
1083+
));
10641084
let res = bench(
10651085
rt,
1066-
pool,
1067-
&ArtifactId::Tag(toolchain),
1086+
conn,
10681087
&profiles,
10691088
&scenarios,
1070-
/* bench_rustc */ false,
10711089
Compiler {
10721090
rustc: Path::new(rustc.trim()),
10731091
rustdoc: Some(Path::new(rustdoc.trim())),
@@ -1078,6 +1096,7 @@ fn bench_published_artifact(
10781096
&benchmarks,
10791097
Some(3),
10801098
/* is_self_profile */ false,
1099+
collector,
10811100
);
10821101
res.fail_if_nonzero()?;
10831102
Ok(())

collector/src/lib.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub mod runtime;
1212
pub mod toolchain;
1313
pub mod utils;
1414

15+
use crate::benchmark::{Benchmark, BenchmarkName};
16+
use crate::runtime::{BenchmarkGroup, BenchmarkSuite};
17+
use database::{ArtifactId, ArtifactIdNumber, Connection};
1518
use process::Stdio;
1619

1720
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Deserialize)]
@@ -244,3 +247,100 @@ pub async fn master_commits() -> anyhow::Result<Vec<MasterCommit>> {
244247
let response = reqwest::get("https://triage.rust-lang.org/bors-commit-list").await?;
245248
Ok(response.json().await?)
246249
}
250+
251+
/// Collects information about steps that will be benchmarked during a single artifact run.
252+
#[derive(Default)]
253+
pub struct CollectorStepBuilder {
254+
steps: Vec<String>,
255+
bench_rustc: bool,
256+
}
257+
258+
impl CollectorStepBuilder {
259+
pub fn record_compile_benchmarks(
260+
mut self,
261+
benchmarks: &[Benchmark],
262+
bench_rustc: bool,
263+
) -> Self {
264+
self.steps
265+
.extend(benchmarks.iter().map(|b| b.name.to_string()));
266+
if bench_rustc {
267+
self.steps.push("rustc".to_string());
268+
self.bench_rustc = true;
269+
}
270+
self
271+
}
272+
273+
pub fn record_runtime_benchmarks(mut self, suite: &BenchmarkSuite) -> Self {
274+
self.steps
275+
.extend(suite.groups.iter().map(runtime_group_step_name));
276+
self
277+
}
278+
279+
pub async fn start_collection(
280+
self,
281+
conn: &mut dyn Connection,
282+
artifact_id: ArtifactId,
283+
) -> CollectorCtx {
284+
// Make sure there is no observable time when the artifact ID is available
285+
// but the in-progress steps are not.
286+
let artifact_row_id = {
287+
let mut tx = conn.transaction().await;
288+
let artifact_row_id = tx.conn().artifact_id(&artifact_id).await;
289+
tx.conn()
290+
.collector_start(artifact_row_id, &self.steps)
291+
.await;
292+
tx.commit().await.unwrap();
293+
artifact_row_id
294+
};
295+
CollectorCtx {
296+
artifact_id,
297+
artifact_row_id,
298+
bench_rustc: self.bench_rustc,
299+
}
300+
}
301+
}
302+
303+
/// Represents an in-progress run for a given artifact.
304+
pub struct CollectorCtx {
305+
pub artifact_id: ArtifactId,
306+
pub artifact_row_id: ArtifactIdNumber,
307+
pub bench_rustc: bool,
308+
}
309+
310+
impl CollectorCtx {
311+
pub async fn start_compile_step(
312+
&mut self,
313+
conn: &mut dyn Connection,
314+
benchmark_name: &BenchmarkName,
315+
) -> bool {
316+
conn.collector_start_step(self.artifact_row_id, &benchmark_name.0)
317+
.await
318+
}
319+
320+
pub async fn end_compile_step(
321+
&mut self,
322+
conn: &mut dyn Connection,
323+
benchmark_name: &BenchmarkName,
324+
) {
325+
conn.collector_end_step(self.artifact_row_id, &benchmark_name.0)
326+
.await
327+
}
328+
329+
pub async fn start_runtime_step(
330+
&mut self,
331+
conn: &mut dyn Connection,
332+
group: &BenchmarkGroup,
333+
) -> bool {
334+
conn.collector_start_step(self.artifact_row_id, &runtime_group_step_name(group))
335+
.await
336+
}
337+
338+
pub async fn end_runtime_step(&mut self, conn: &mut dyn Connection, group: &BenchmarkGroup) {
339+
conn.collector_end_step(self.artifact_row_id, &runtime_group_step_name(group))
340+
.await
341+
}
342+
}
343+
344+
fn runtime_group_step_name(group: &BenchmarkGroup) -> String {
345+
format!("runtime:{}", group.name())
346+
}

0 commit comments

Comments
 (0)