Skip to content

Commit

Permalink
unstable feature usage metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
yaahc committed Sep 10, 2024
1 parent 17b322f commit 25ee72d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3681,6 +3681,8 @@ version = "0.0.0"
dependencies = [
"rustc_data_structures",
"rustc_span",
"serde",
"serde_json",
]

[[package]]
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -
}
}

if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir {
features.dump_feature_usage_metrics(metrics_dir);
}

features
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition = "2021"
# tidy-alphabetical-start
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_span = { path = "../rustc_span" }
serde = { version = "1.0.125", features = [ "derive" ] }
serde_json = "1.0.59"
# tidy-alphabetical-end
56 changes: 56 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! List of the unstable feature gates.
use std::path::Path;

use rustc_data_structures::fx::FxHashSet;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::Span;
Expand Down Expand Up @@ -649,6 +651,60 @@ declare_features! (
// -------------------------------------------------------------------------
);

impl Features {
pub fn dump_feature_usage_metrics(&self, metrics_dir: &Path) {
#[derive(serde::Serialize)]
struct LibFeature {
symbol: String,
}

#[derive(serde::Serialize)]
struct LangFeature {
symbol: String,
since: Option<String>,
}

#[derive(serde::Serialize)]
struct FeatureUsage {
lib_features: Vec<LibFeature>,
lang_features: Vec<LangFeature>,
}

// TODO (DECIDE): How fine grained do we want to track feature usage?

Check failure on line 673 in compiler/rustc_feature/src/unstable.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
// Jane Preference: i want to track usage for code that gets used, not code
// that is in development, but I don't know how we'd hook into this for code
// that doesn't participate in the crates.io ecosystem, those crates won't even
// necessarily have releases or versioning norms that match other crates, so we
// may have to just track per compilation and aggressively collapse metrics to
// avoid unnecessary disk usage.
// TODO avoid filename collisions between runs

Check failure on line 680 in compiler/rustc_feature/src/unstable.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
let feature_metrics_file = metrics_dir.join("unstable_feature_usage.json");
let Ok(feature_metrics_file) = std::fs::File::create(feature_metrics_file) else {
return;
};
let feature_metrics_file = std::io::BufWriter::new(feature_metrics_file);

let lib_features = self
.declared_lib_features
.iter()
.map(|(symbol, _)| LibFeature { symbol: symbol.to_string() })
.collect();

let lang_features = self
.declared_lang_features
.iter()
.map(|(symbol, _, since)| LangFeature {
symbol: symbol.to_string(),
since: since.map(|since| since.to_string()),
})
.collect();

let feature_usage = FeatureUsage { lib_features, lang_features };

let _ = serde_json::to_writer(feature_metrics_file, &feature_usage);
}
}

/// Some features are not allowed to be used together at the same time, if
/// the two are present, produce an error.
///
Expand Down

0 comments on commit 25ee72d

Please sign in to comment.