From f742aced7f658e33400c44ea24919376dd59d13f Mon Sep 17 00:00:00 2001 From: Denis Cornehl Date: Sun, 4 May 2025 20:14:41 +0200 Subject: [PATCH] start collecting compiler metrics optionally --- docker-compose.yml | 2 ++ src/config.rs | 6 ++++++ src/docbuilder/rustwide_builder.rs | 29 ++++++++++++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index aabf44cc8..78ad3cbad 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,9 +19,11 @@ services: - "/var/run/docker.sock:/var/run/docker.sock" - ".rustwide-docker:/opt/docsrs/rustwide" - "cratesio-index:/opt/docsrs/prefix/crates.io-index" + - "./ignored/cratesfyi-prefix/metrics:/opt/docsrs/prefix/metrics" - "./static:/opt/docsrs/static:ro" environment: DOCSRS_RUSTWIDE_WORKSPACE: /opt/docsrs/rustwide + DOCSRS_COMPILER_METRICS_PATH: /opt/docsrs/prefix/metrics DOCSRS_DATABASE_URL: postgresql://cratesfyi:password@db DOCSRS_STORAGE_BACKEND: s3 S3_ENDPOINT: http://s3:9000 diff --git a/src/config.rs b/src/config.rs index 40c885521..923823d52 100644 --- a/src/config.rs +++ b/src/config.rs @@ -78,6 +78,10 @@ pub struct Config { // for the remote archives? pub(crate) local_archive_cache_path: PathBuf, + // Where to collect metrics for the metrics initiative. + // When empty, we won't collect metrics. + pub(crate) compiler_metrics_collection_path: Option, + // Content Security Policy pub(crate) csp_report_only: bool, @@ -226,6 +230,8 @@ impl Config { prefix.join("archive_cache"), )?, + compiler_metrics_collection_path: maybe_env("DOCSRS_COMPILER_METRICS_PATH")?, + temp_dir, rustwide_workspace: env("DOCSRS_RUSTWIDE_WORKSPACE", PathBuf::from(".workspace"))?, diff --git a/src/docbuilder/rustwide_builder.rs b/src/docbuilder/rustwide_builder.rs index 9dada2196..b8887eea1 100644 --- a/src/docbuilder/rustwide_builder.rs +++ b/src/docbuilder/rustwide_builder.rs @@ -862,6 +862,18 @@ impl RustwideBuilder { }) }; + if let Some(compiler_metric_target_dir) = &self.config.compiler_metrics_collection_path { + let metric_output = build.host_target_dir().join("metrics/"); + info!( + "found {} files in metric dir, copy over to {} (exists: {})", + fs::read_dir(&metric_output)?.count(), + &compiler_metric_target_dir.to_string_lossy(), + &compiler_metric_target_dir.exists(), + ); + copy_dir_all(&metric_output, compiler_metric_target_dir)?; + fs::remove_dir_all(&metric_output)?; + } + // For proc-macros, cargo will put the output in `target/doc`. // Move it to the target-specific directory for consistency with other builds. // NOTE: don't rename this if the build failed, because `target/doc` won't exist. @@ -945,7 +957,7 @@ impl RustwideBuilder { ]; rustdoc_flags_extras.extend(UNCONDITIONAL_ARGS.iter().map(|&s| s.to_owned())); - let cargo_args = metadata.cargo_args(&cargo_args, &rustdoc_flags_extras); + let mut cargo_args = metadata.cargo_args(&cargo_args, &rustdoc_flags_extras); // If the explicit target is not a tier one target, we need to install it. let has_build_std = cargo_args.windows(2).any(|args| { @@ -966,6 +978,21 @@ impl RustwideBuilder { command = command.env(key, val); } + if self.config.compiler_metrics_collection_path.is_some() { + // set the `./target/metrics/` directory inside the build container + // as a target directory for the metric files. + let flag = "-Zmetrics-dir=/opt/rustwide/target/metrics"; + + // this is how we can reach it from outside the container. + fs::create_dir_all(build.host_target_dir().join("metrics/"))?; + + let rustflags = toml::Value::try_from(vec![flag]) + .expect("serializing a string should never fail") + .to_string(); + cargo_args.push("--config".into()); + cargo_args.push(format!("build.rustflags={rustflags}")); + } + Ok(command.args(&cargo_args)) }