Skip to content

Commit a13a464

Browse files
committed
Periodically check latest git SHA in collector and exit if it's not up-to-date
1 parent 216dcfa commit a13a464

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

collector/src/bin/collector.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,11 @@ enum Commands {
713713
#[arg(long)]
714714
git_sha: Option<String>,
715715

716+
/// Periodically check if the collector's commit SHA matches the commit SHA of the
717+
/// rustc-perf repository.
718+
#[arg(long)]
719+
check_git_sha: bool,
720+
716721
#[command(flatten)]
717722
db: DbOption,
718723
},
@@ -1360,6 +1365,7 @@ Make sure to modify `{dir}/perf-config.json` if the category/artifact don't matc
13601365
Commands::BenchmarkJobQueue {
13611366
collector_name,
13621367
git_sha,
1368+
check_git_sha,
13631369
db,
13641370
} => {
13651371
log_db(&db);
@@ -1368,7 +1374,7 @@ Make sure to modify `{dir}/perf-config.json` if the category/artifact don't matc
13681374
Some(sha) => sha,
13691375
None => {
13701376
let mut cmd = Command::new("git");
1371-
cmd.args(&["rev-parse", "HEAD"]);
1377+
cmd.args(["rev-parse", "HEAD"]);
13721378
let stdout = command_output(&mut cmd)
13731379
.context("Cannot determine current commit SHA")?
13741380
.stdout;
@@ -1395,6 +1401,13 @@ Make sure to modify `{dir}/perf-config.json` if the category/artifact don't matc
13951401
));
13961402
}
13971403

1404+
log::info!(
1405+
"Starting collector with target {}, benchmark set {} and commit {}",
1406+
collector_config.target(),
1407+
collector_config.benchmark_set().get_id(),
1408+
collector_config.commit_sha().expect("missing commit SHA")
1409+
);
1410+
13981411
let benchmarks =
13991412
get_compile_benchmarks(&compile_benchmark_dir, CompileBenchmarkFilter::All)?;
14001413

@@ -1403,6 +1416,7 @@ Make sure to modify `{dir}/perf-config.json` if the category/artifact don't matc
14031416
conn,
14041417
&collector_config,
14051418
benchmarks,
1419+
check_git_sha,
14061420
))?;
14071421

14081422
Ok(0)
@@ -1418,8 +1432,10 @@ async fn run_job_queue_benchmarks(
14181432
mut conn: Box<dyn Connection>,
14191433
collector: &CollectorConfig,
14201434
all_compile_benchmarks: Vec<Benchmark>,
1435+
check_git_sha: bool,
14211436
) -> anyhow::Result<()> {
1422-
// TODO: check collector SHA vs site SHA
1437+
let mut last_request_tag = None;
1438+
14231439
while let Some((benchmark_job, artifact_id)) = conn
14241440
.dequeue_benchmark_job(
14251441
collector.name(),
@@ -1428,6 +1444,22 @@ async fn run_job_queue_benchmarks(
14281444
)
14291445
.await?
14301446
{
1447+
// Here we check if we should update our commit SHA, if rustc-perf has been updated.
1448+
// We only check for updates when we switch *benchmark requests*, not *benchmark jobs*,
1449+
// to avoid changing code in the middle of benchmarking the same request.
1450+
// Note that if an update happens, the job that we have just dequeued will have its deque
1451+
// counter increased. But since updates are relatively rare, that shouldn't be a big deal,
1452+
// it will be dequeued again when the collector starts again.
1453+
if check_git_sha
1454+
&& last_request_tag.is_some()
1455+
&& last_request_tag.as_deref() != Some(benchmark_job.request_tag())
1456+
&& needs_git_update(collector)
1457+
{
1458+
log::warn!("Exiting collector to update itself from git.");
1459+
return Ok(());
1460+
}
1461+
last_request_tag = Some(benchmark_job.request_tag().to_string());
1462+
14311463
log::info!("Dequeued job {benchmark_job:?}, artifact_id {artifact_id:?}");
14321464
let result = run_benchmark_job(
14331465
conn.as_mut(),
@@ -1491,6 +1523,39 @@ async fn run_job_queue_benchmarks(
14911523
Ok(())
14921524
}
14931525

1526+
/// Returns true if the commit SHA of collector does not match the latest commit SHA of the master
1527+
/// branch of https://github.com/rust-lang/rustc-perf.
1528+
fn needs_git_update(collector: &CollectorConfig) -> bool {
1529+
let Some(commit_sha) = collector.commit_sha() else {
1530+
return false;
1531+
};
1532+
1533+
let mut cmd = Command::new("git");
1534+
cmd.arg("ls-remote")
1535+
.arg("https://github.com/rust-lang/rustc-perf")
1536+
.arg("HEAD");
1537+
let upstream_sha = match command_output(&mut cmd) {
1538+
Ok(output) => String::from_utf8(output.stdout)
1539+
.unwrap()
1540+
.split_whitespace()
1541+
.next()
1542+
.unwrap()
1543+
.to_string(),
1544+
Err(error) => {
1545+
log::error!("Cannot determine latest SHA of rustc-perf: {error:?}");
1546+
return false;
1547+
}
1548+
};
1549+
if commit_sha != upstream_sha {
1550+
log::warn!(
1551+
"Commit {commit_sha} of collector is outdated, latest commit is {upstream_sha}."
1552+
);
1553+
true
1554+
} else {
1555+
false
1556+
}
1557+
}
1558+
14941559
/// Error that happened during benchmarking of a job.
14951560
enum BenchmarkJobError {
14961561
/// The error is non-recoverable.

0 commit comments

Comments
 (0)