-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Collect statistics about MIR optimizations #76769
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
//! # Support for collecting simple statistics | ||
//! | ||
//! Statistics are useful for collecting metrics from optimization passes, like | ||
//! the number of simplifications performed. To avoid introducing overhead, the | ||
//! collection of statistics is enabled only when rustc is compiled with | ||
//! debug-assertions. | ||
//! | ||
//! Statistics are static variables defined in the module they are used, and | ||
//! lazy registered in the global collector on the first use. Once registered, | ||
//! the collector will obtain their values at the end of compilation process | ||
//! when requested with -Zmir-opt-stats option. | ||
|
||
use parking_lot::{const_mutex, Mutex}; | ||
use std::io::{self, stdout, Write as _}; | ||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; | ||
|
||
static COLLECTOR: Collector = Collector::new(); | ||
|
||
/// Enables the collection of statistics. | ||
/// To be effective it has to be called before the first use of a statistics. | ||
pub fn try_enable() -> Result<(), ()> { | ||
COLLECTOR.try_enable() | ||
} | ||
|
||
/// Prints all statistics collected so far. | ||
pub fn print() { | ||
COLLECTOR.print(); | ||
} | ||
|
||
pub struct Statistic { | ||
category: &'static str, | ||
name: &'static str, | ||
initialized: AtomicBool, | ||
value: AtomicUsize, | ||
} | ||
|
||
struct Collector(Mutex<State>); | ||
|
||
struct State { | ||
enabled: bool, | ||
stats: Vec<&'static Statistic>, | ||
} | ||
|
||
#[derive(Eq, PartialEq, Ord, PartialOrd)] | ||
struct Snapshot { | ||
category: &'static str, | ||
name: &'static str, | ||
value: usize, | ||
} | ||
|
||
impl Statistic { | ||
pub const fn new(category: &'static str, name: &'static str) -> Self { | ||
Statistic { | ||
category, | ||
name, | ||
initialized: AtomicBool::new(false), | ||
value: AtomicUsize::new(0), | ||
} | ||
} | ||
|
||
pub fn name(&self) -> &'static str { | ||
self.name | ||
} | ||
|
||
pub fn category(&self) -> &'static str { | ||
self.category.rsplit("::").next().unwrap() | ||
} | ||
|
||
#[inline] | ||
pub fn register(&'static self) { | ||
if cfg!(debug_assertions) { | ||
if !self.initialized.load(Ordering::Acquire) { | ||
COLLECTOR.register(self); | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn increment(&'static self, value: usize) { | ||
if cfg!(debug_assertions) { | ||
self.value.fetch_add(value, Ordering::Relaxed); | ||
tmiasko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.register(); | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn update_max(&'static self, value: usize) { | ||
if cfg!(debug_assertions) { | ||
self.value.fetch_max(value, Ordering::Relaxed); | ||
self.register(); | ||
} | ||
} | ||
|
||
fn snapshot(&'static self) -> Snapshot { | ||
Snapshot { | ||
name: self.name(), | ||
category: self.category(), | ||
value: self.value.load(Ordering::Relaxed), | ||
} | ||
} | ||
} | ||
|
||
impl Collector { | ||
const fn new() -> Self { | ||
Collector(const_mutex(State { enabled: false, stats: Vec::new() })) | ||
} | ||
|
||
fn try_enable(&self) -> Result<(), ()> { | ||
if cfg!(debug_assertions) { | ||
self.0.lock().enabled = true; | ||
Ok(()) | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
|
||
fn snapshot(&self) -> Vec<Snapshot> { | ||
self.0.lock().stats.iter().copied().map(Statistic::snapshot).collect() | ||
} | ||
|
||
fn register(&self, s: &'static Statistic) { | ||
let mut state = self.0.lock(); | ||
if !s.initialized.load(Ordering::Relaxed) { | ||
if state.enabled { | ||
state.stats.push(s); | ||
} | ||
s.initialized.store(true, Ordering::Release); | ||
} | ||
} | ||
|
||
fn print(&self) { | ||
let mut stats = self.snapshot(); | ||
stats.sort(); | ||
match self.write(&stats) { | ||
Ok(_) => {} | ||
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {} | ||
Err(e) => panic!(e), | ||
} | ||
} | ||
|
||
fn write(&self, stats: &[Snapshot]) -> io::Result<()> { | ||
let mut cat_width = 0; | ||
let mut val_width = 0; | ||
|
||
for s in stats { | ||
cat_width = cat_width.max(s.category.len()); | ||
val_width = val_width.max(s.value.to_string().len()); | ||
} | ||
|
||
let mut out = Vec::new(); | ||
for s in stats { | ||
write!( | ||
&mut out, | ||
"{val:val_width$} {cat:cat_width$} {name}\n", | ||
val = s.value, | ||
val_width = val_width, | ||
cat = s.category, | ||
cat_width = cat_width, | ||
name = s.name, | ||
)?; | ||
} | ||
|
||
stdout().write_all(&out) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be part of
Session
orTyCtxt
. There may be multiple rustc sessions running in a single process. (for example when using RLS)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I experimented with different implementation approaches:
I strongly prefer variants that put statistics definitions together with MIR passes that uses them. In that approach, adding and removing counters is trivial, additionally any unused counters are detected as such during compilation.
Values can be stored in the context if we want to support multiple rustc sessions in a single process, although it requires passing context to all those places where counters are used. But is there any real use-case? Why would you use rustc with debug-assertions and MIR optimizations counters inside the RLS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @bjorn3. We've tried pretty hard to move these sorts of things into
Session
andTyCtxt
to allow running multiple sessions in the same process (both for RLS and eventually for one rustc process compiling multiple crates simultaneously).Since this is essentially unstable, diagnostic data and doesn't effect the compilation artifacts, it may be worth bending that rule because, as you point out, having the counters defined in their MIR passes is quite nice. I don't personally feel comfortable approving that since this is a cross-cutting concern for the compiler as a whole and there may be others on the compiler team that have strong opinions about breaking that rule.