Skip to content

Commit

Permalink
introduce build metrics version numbers to handle breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pietroalbini committed May 25, 2023
1 parent 3c9b076 commit 0553f71
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/bootstrap/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ use std::io::BufWriter;
use std::time::{Duration, Instant, SystemTime};
use sysinfo::{CpuExt, System, SystemExt};

// Update this number whenever a breaking change is made to the build metrics.
//
// Versions:
// 0: initial version
// 1: replaced JsonNode::Test with JsonNode::TestSuite
const CURRENT_METADATA_VERSION: usize = 1;

pub(crate) struct BuildMetrics {
state: RefCell<MetricsState>,
}
Expand Down Expand Up @@ -145,7 +152,20 @@ impl BuildMetrics {
// Some of our CI builds consist of multiple independent CI invocations. Ensure all the
// previous invocations are still present in the resulting file.
let mut invocations = match std::fs::read(&dest) {
Ok(contents) => t!(serde_json::from_slice::<JsonRoot>(&contents)).invocations,
Ok(contents) => {
// We first parse just the metadata_version field to have the check succeed even if
// the rest of the contents are not valid anymore.
let version: OnlyMetadataVersion = t!(serde_json::from_slice(&contents));
if version.metadata_version == CURRENT_METADATA_VERSION {
t!(serde_json::from_slice::<JsonRoot>(&contents)).invocations
} else {
println!(
"warning: overriding existing build/metrics.json, as it's not \
compatible with build metrics format version {CURRENT_METADATA_VERSION}."
);
Vec::new()
}
}
Err(err) => {
if err.kind() != std::io::ErrorKind::NotFound {
panic!("failed to open existing metrics file at {}: {err}", dest.display());
Expand All @@ -163,7 +183,8 @@ impl BuildMetrics {
children: steps.into_iter().map(|step| self.prepare_json_step(step)).collect(),
});

let json = JsonRoot { system_stats, invocations };
let json =
JsonRoot { metadata_version: CURRENT_METADATA_VERSION, system_stats, invocations };

t!(std::fs::create_dir_all(dest.parent().unwrap()));
let mut file = BufWriter::new(t!(File::create(&dest)));
Expand Down Expand Up @@ -214,6 +235,8 @@ struct StepMetrics {
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
struct JsonRoot {
#[serde(default)] // For version 0 the field was not present.
metadata_version: usize,
system_stats: JsonInvocationSystemStats,
invocations: Vec<JsonInvocation>,
}
Expand Down Expand Up @@ -299,3 +322,9 @@ struct JsonInvocationSystemStats {
struct JsonStepSystemStats {
cpu_utilization_percent: f64,
}

#[derive(Deserialize)]
struct OnlyMetadataVersion {
#[serde(default)] // For version 0 the field was not present.
metadata_version: usize,
}

0 comments on commit 0553f71

Please sign in to comment.