diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-05-09 21:50:06 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-09 21:50:06 +0200 |
| commit | 9371122459eb86d0113bf0f284ae8eef3b585436 (patch) | |
| tree | 708837b160af1581334ffdd0114a618e60230753 | |
| parent | 7a5bbe0527b287d43f805b219e62f47a6794f3a8 (diff) | |
| parent | 71e767f3405ed454ad14d9b339f5a44f064e49d2 (diff) | |
| download | rust-9371122459eb86d0113bf0f284ae8eef3b585436.tar.gz rust-9371122459eb86d0113bf0f284ae8eef3b585436.zip | |
Rollup merge of #140815 - yaahc:rustdoc-metrics, r=GuillaumeGomez
also export metrics from librustdoc Addresses the issue mentioned here: [#t-docs-rs > metrics intitiative @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/356853-t-docs-rs/topic/metrics.20intitiative/near/515714331) The previous implementation only emitted metrics from rustc, but it turns out running `cargo doc` only calls `rustc` for dependencies, and not for the root crate being documented. We are planning to gather a sample dataset from docs.rs ci via `cargo doc` so as things stood this would not emit any metrics for any of the crates themselves that were published. This change adds the same logic from `rustc_driver_impl` to `librustdoc` to also dump metrics at the end of its execution if they are enabled. Note: The hash's generated by librustdoc will likely be completely different from the ones generated by rustc. This is because rustc is actually doing the various passes needed to fully calculate the stable version hash. My understanding of how rustdoc works is that the hashes generated will be working with partial information due to it only doing the work required to generate docs. The hashes will still be unique per crate and will work for the purposes of the metrics proof of concept, it would not be possible to correlate metrics generated by rustdoc with those generated by rustc for the same crate. This is fine for the purposes of the PoC but a future full implementation of metrics may want to address this issue.
| -rw-r--r-- | src/librustdoc/lib.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index fd1c89d91bd..b4210e7b518 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -73,9 +73,11 @@ extern crate tikv_jemalloc_sys as jemalloc_sys; use std::env::{self, VarError}; use std::io::{self, IsTerminal}; +use std::path::Path; use std::process; use rustc_errors::DiagCtxtHandle; +use rustc_hir::def_id::LOCAL_CRATE; use rustc_interface::interface; use rustc_middle::ty::TyCtxt; use rustc_session::config::{ErrorOutputType, RustcOptGroup, make_crate_type_option}; @@ -904,6 +906,10 @@ fn main_args(early_dcx: &mut EarlyDiagCtxt, at_args: &[String]) { rustc_interface::passes::write_dep_info(tcx); } + if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir { + dump_feature_usage_metrics(tcx, metrics_dir); + } + if run_check { // Since we're in "check" mode, no need to generate anything beyond this point. return; @@ -923,3 +929,16 @@ fn main_args(early_dcx: &mut EarlyDiagCtxt, at_args: &[String]) { }) }) } + +fn dump_feature_usage_metrics(tcxt: TyCtxt<'_>, metrics_dir: &Path) { + let hash = tcxt.crate_hash(LOCAL_CRATE); + let crate_name = tcxt.crate_name(LOCAL_CRATE); + let metrics_file_name = format!("unstable_feature_usage_metrics-{crate_name}-{hash}.json"); + let metrics_path = metrics_dir.join(metrics_file_name); + if let Err(error) = tcxt.features().dump_feature_usage_metrics(metrics_path) { + // FIXME(yaahc): once metrics can be enabled by default we will want "failure to emit + // default metrics" to only produce a warning when metrics are enabled by default and emit + // an error only when the user manually enables metrics + tcxt.dcx().err(format!("cannot emit feature usage metrics: {error}")); + } +} |
