Skip to content

Gate advanced features of citool to reduce compilation time #139678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ jobs:
COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
run: |
cd src/ci/citool
CARGO_INCREMENTAL=0 cargo test
CARGO_INCREMENTAL=0 cargo run calculate-job-matrix >> $GITHUB_OUTPUT
CARGO_INCREMENTAL=0 cargo test --no-default-features
CARGO_INCREMENTAL=0 cargo run --no-default-features calculate-job-matrix >> $GITHUB_OUTPUT
id: jobs
job:
name: ${{ matrix.full_name }}
Expand Down
15 changes: 11 additions & 4 deletions src/ci/citool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ version = "0.1.0"
edition = "2021"

[dependencies]
# Basic dependencies needed for calculating the CI job matrix
anyhow = "1"
clap = { version = "4.5", features = ["derive"] }
csv = "1"
diff = "0.1"
glob-match = "0.2"
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.9"
serde_json = "1"
ureq = { version = "3", features = ["json"] }

build_helper = { path = "../../build_helper" }
csv = { version = "1", optional = true }
diff = { version = "0.1", optional = true }
ureq = { version = "3", features = ["json"], optional = true }

build_helper = { path = "../../build_helper", optional = true }

[features]
default = ["extended"]
# Features needed for more advanced analysis and network requests
extended = ["dep:csv", "dep:diff", "dep:ureq", "dep:build_helper"]

[dev-dependencies]
insta = "1"
Expand Down
44 changes: 34 additions & 10 deletions src/ci/citool/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
#[cfg(feature = "extended")]
mod analysis;
#[cfg(feature = "extended")]
mod cpu_usage;
#[cfg(feature = "extended")]
mod datadog;
#[cfg(feature = "extended")]
mod github;
mod jobs;
#[cfg(feature = "extended")]
mod metrics;
mod utils;

use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::process::Command;

use analysis::output_bootstrap_stats;
use anyhow::Context;
use clap::Parser;
use jobs::JobDatabase;
use serde_yaml::Value;

use crate::analysis::{output_largest_duration_changes, output_test_diffs};
use crate::cpu_usage::load_cpu_usage;
use crate::datadog::upload_datadog_metric;
use crate::github::JobInfoResolver;
use crate::jobs::RunType;
use crate::metrics::{JobMetrics, download_auto_job_metrics, download_job_metrics, load_metrics};
use crate::utils::load_env_var;

const CI_DIRECTORY: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/..");
Expand Down Expand Up @@ -90,7 +88,11 @@ fn yaml_map_to_json(map: &BTreeMap<String, Value>) -> BTreeMap<String, serde_jso
.collect()
}

fn run_workflow_locally(db: JobDatabase, job_type: JobType, name: String) -> anyhow::Result<()> {
fn run_workflow_locally(
db: jobs::JobDatabase,
job_type: JobType,
name: String,
) -> anyhow::Result<()> {
let jobs = match job_type {
JobType::Auto => &db.auto_jobs,
JobType::PR => &db.pr_jobs,
Expand Down Expand Up @@ -128,7 +130,11 @@ fn run_workflow_locally(db: JobDatabase, job_type: JobType, name: String) -> any
if !result.success() { Err(anyhow::anyhow!("Job failed")) } else { Ok(()) }
}

#[cfg(feature = "extended")]
fn upload_ci_metrics(cpu_usage_csv: &Path) -> anyhow::Result<()> {
use crate::cpu_usage::load_cpu_usage;
use crate::datadog::upload_datadog_metric;

let usage = load_cpu_usage(cpu_usage_csv).context("Cannot load CPU usage from input CSV")?;
eprintln!("CPU usage\n{usage:?}");

Expand All @@ -140,11 +146,18 @@ fn upload_ci_metrics(cpu_usage_csv: &Path) -> anyhow::Result<()> {
Ok(())
}

#[cfg(feature = "extended")]
fn postprocess_metrics(
metrics_path: PathBuf,
parent: Option<String>,
job_name: Option<String>,
) -> anyhow::Result<()> {
use std::collections::HashMap;

use crate::analysis::{output_bootstrap_stats, output_test_diffs};
use crate::github::JobInfoResolver;
use crate::metrics::{JobMetrics, download_job_metrics, load_metrics};

let metrics = load_metrics(&metrics_path)?;

let mut job_info_resolver = JobInfoResolver::new();
Expand Down Expand Up @@ -179,7 +192,12 @@ fn postprocess_metrics(
Ok(())
}

fn post_merge_report(db: JobDatabase, current: String, parent: String) -> anyhow::Result<()> {
#[cfg(feature = "extended")]
fn post_merge_report(db: jobs::JobDatabase, current: String, parent: String) -> anyhow::Result<()> {
use crate::analysis::{output_largest_duration_changes, output_test_diffs};
use crate::github::JobInfoResolver;
use crate::metrics::download_auto_job_metrics;

let metrics = download_auto_job_metrics(&db, &parent, &current)?;

println!("\nComparing {parent} (parent) -> {current} (this PR)\n");
Expand Down Expand Up @@ -208,6 +226,7 @@ enum Args {
#[clap(long = "type", default_value = "auto")]
job_type: JobType,
},
#[cfg(feature = "extended")]
/// Postprocess the metrics.json file generated by bootstrap and output
/// various statistics.
/// If `--parent` and `--job-name` are provided, also display a diff
Expand All @@ -222,11 +241,13 @@ enum Args {
#[clap(long, requires("parent"))]
job_name: Option<String>,
},
#[cfg(feature = "extended")]
/// Upload CI metrics to Datadog.
UploadBuildMetrics {
/// Path to a CSV containing the CI job CPU usage.
cpu_usage_csv: PathBuf,
},
#[cfg(feature = "extended")]
/// Generate a report of test execution changes between two rustc commits.
PostMergeReport {
/// Parent commit to use as a base of the comparison.
Expand Down Expand Up @@ -268,12 +289,15 @@ fn main() -> anyhow::Result<()> {
Args::RunJobLocally { job_type, name } => {
run_workflow_locally(load_db(default_jobs_file)?, job_type, name)?;
}
#[cfg(feature = "extended")]
Args::UploadBuildMetrics { cpu_usage_csv } => {
upload_ci_metrics(&cpu_usage_csv)?;
}
#[cfg(feature = "extended")]
Args::PostprocessMetrics { metrics_path, parent, job_name } => {
postprocess_metrics(metrics_path, parent, job_name)?;
}
#[cfg(feature = "extended")]
Args::PostMergeReport { current, parent } => {
post_merge_report(load_db(default_jobs_file)?, current, parent)?;
}
Expand Down
2 changes: 2 additions & 0 deletions src/ci/citool/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(unused)]
Copy link
Member

@marcoieni marcoieni Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you write a comment explaining why this is here?


use std::path::Path;

use anyhow::Context;
Expand Down
9 changes: 8 additions & 1 deletion src/ci/citool/tests/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ fn pr_jobs() {

fn get_matrix(event_name: &str, commit_msg: &str, branch_ref: &str) -> String {
let output = Command::new("cargo")
.args(["run", "-q", "calculate-job-matrix", "--jobs-file", TEST_JOBS_YML_PATH])
.args([
"run",
"--no-default-features",
"-q",
"calculate-job-matrix",
"--jobs-file",
TEST_JOBS_YML_PATH,
])
.env("GITHUB_EVENT_NAME", event_name)
.env("COMMIT_MESSAGE", commit_msg)
.env("GITHUB_REF", branch_ref)
Expand Down
Loading