Skip to content

Commit 1689abb

Browse files
committed
start collecting compiler metrics optionally
1 parent 8ef19e5 commit 1689abb

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

docker-compose.yml

+2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ services:
1919
- "/var/run/docker.sock:/var/run/docker.sock"
2020
- ".rustwide-docker:/opt/docsrs/rustwide"
2121
- "cratesio-index:/opt/docsrs/prefix/crates.io-index"
22+
- "./ignored/cratesfyi-prefix/metrics:/opt/docsrs/prefix/metrics"
2223
- "./static:/opt/docsrs/static:ro"
2324
environment:
2425
DOCSRS_RUSTWIDE_WORKSPACE: /opt/docsrs/rustwide
26+
DOCSRS_COMPILER_METRICS_PATH: /opt/docsrs/prefix/metrics
2527
DOCSRS_DATABASE_URL: postgresql://cratesfyi:password@db
2628
DOCSRS_STORAGE_BACKEND: s3
2729
S3_ENDPOINT: http://s3:9000

src/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ pub struct Config {
7878
// for the remote archives?
7979
pub(crate) local_archive_cache_path: PathBuf,
8080

81+
// Where to collect metrics for the metrics initiative.
82+
// When empty, we won't collect metrics.
83+
pub(crate) compiler_metrics_collection_path: Option<PathBuf>,
84+
8185
// Content Security Policy
8286
pub(crate) csp_report_only: bool,
8387

@@ -226,6 +230,8 @@ impl Config {
226230
prefix.join("archive_cache"),
227231
)?,
228232

233+
compiler_metrics_collection_path: maybe_env("DOCSRS_COMPILER_METRICS_PATH")?,
234+
229235
temp_dir,
230236

231237
rustwide_workspace: env("DOCSRS_RUSTWIDE_WORKSPACE", PathBuf::from(".workspace"))?,

src/docbuilder/rustwide_builder.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,18 @@ impl RustwideBuilder {
862862
})
863863
};
864864

865+
if let Some(compiler_metric_target_dir) = &self.config.compiler_metrics_collection_path {
866+
let metric_output = build.host_target_dir().join("metrics/");
867+
info!(
868+
"found {} files in metric dir, copy over to {} (exists: {})",
869+
fs::read_dir(&metric_output)?.count(),
870+
&compiler_metric_target_dir.to_string_lossy(),
871+
&compiler_metric_target_dir.exists(),
872+
);
873+
copy_dir_all(&metric_output, compiler_metric_target_dir)?;
874+
fs::remove_dir_all(&metric_output)?;
875+
}
876+
865877
// For proc-macros, cargo will put the output in `target/doc`.
866878
// Move it to the target-specific directory for consistency with other builds.
867879
// NOTE: don't rename this if the build failed, because `target/doc` won't exist.
@@ -945,7 +957,7 @@ impl RustwideBuilder {
945957
];
946958

947959
rustdoc_flags_extras.extend(UNCONDITIONAL_ARGS.iter().map(|&s| s.to_owned()));
948-
let cargo_args = metadata.cargo_args(&cargo_args, &rustdoc_flags_extras);
960+
let mut cargo_args = metadata.cargo_args(&cargo_args, &rustdoc_flags_extras);
949961

950962
// If the explicit target is not a tier one target, we need to install it.
951963
let has_build_std = cargo_args.windows(2).any(|args| {
@@ -966,6 +978,21 @@ impl RustwideBuilder {
966978
command = command.env(key, val);
967979
}
968980

981+
if self.config.compiler_metrics_collection_path.is_some() {
982+
// set the `./target/metrics/` directory inside the build container
983+
// as a target directory for the metric files.
984+
let flag = "-Zmetrics-dir=/opt/rustwide/target/metrics";
985+
986+
// this is how we can reach it from outside the container.
987+
fs::create_dir_all(build.host_target_dir().join("metrics/"))?;
988+
989+
let rustflags = toml::Value::try_from(vec![flag])
990+
.expect("serializing a string should never fail")
991+
.to_string();
992+
cargo_args.push("--config".into());
993+
cargo_args.push(format!("build.rustflags={rustflags}"));
994+
}
995+
969996
Ok(command.args(&cargo_args))
970997
}
971998

0 commit comments

Comments
 (0)