Skip to content

Commit

Permalink
Remove unique and move VerboseTimingGuard fields into a new struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Mar 21, 2023
1 parent f60d2eb commit 6c57dda
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
1 change: 0 additions & 1 deletion compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,6 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
start_rss.unwrap(),
end_rss,
tcx.sess.opts.unstable_opts.time_passes_format,
true,
);
}

Expand Down
60 changes: 29 additions & 31 deletions compiler/rustc_data_structures/src/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl SelfProfilerRef {
let message_and_format =
self.print_verbose_generic_activities.map(|format| (event_label.to_owned(), format));

VerboseTimingGuard::start(message_and_format, self.generic_activity(event_label), false)
VerboseTimingGuard::start(message_and_format, self.generic_activity(event_label))
}

/// Like `verbose_generic_activity`, but with an extra arg.
Expand All @@ -239,21 +239,9 @@ impl SelfProfilerRef {
VerboseTimingGuard::start(
message_and_format,
self.generic_activity_with_arg(event_label, event_arg),
false,
)
}

/// Like `verbose_generic_activity`, but `event_label` must be unique for a rustc session.
pub fn unique_verbose_generic_activity(
&self,
event_label: &'static str,
) -> VerboseTimingGuard<'_> {
let message_and_format =
self.print_verbose_generic_activities.map(|format| (event_label.to_owned(), format));

VerboseTimingGuard::start(message_and_format, self.generic_activity(event_label), true)
}

/// Start profiling a generic activity. Profiling continues until the
/// TimingGuard returned from this call is dropped.
#[inline(always)]
Expand Down Expand Up @@ -729,22 +717,31 @@ impl<'a> TimingGuard<'a> {
}
}

struct VerboseInfo {
start_time: Instant,
start_rss: Option<usize>,
message: String,
format: TimePassesFormat,
}

#[must_use]
pub struct VerboseTimingGuard<'a> {
start_and_message: Option<(Instant, Option<usize>, String, TimePassesFormat, bool)>,
info: Option<VerboseInfo>,
_guard: TimingGuard<'a>,
}

impl<'a> VerboseTimingGuard<'a> {
pub fn start(
message_and_format: Option<(String, TimePassesFormat)>,
_guard: TimingGuard<'a>,
unique: bool,
) -> Self {
VerboseTimingGuard {
_guard,
start_and_message: message_and_format.map(|(msg, format)| {
(Instant::now(), get_resident_set_size(), msg, format, unique)
info: message_and_format.map(|(message, format)| VerboseInfo {
start_time: Instant::now(),
start_rss: get_resident_set_size(),
message,
format,
}),
}
}
Expand All @@ -758,10 +755,10 @@ impl<'a> VerboseTimingGuard<'a> {

impl Drop for VerboseTimingGuard<'_> {
fn drop(&mut self) {
if let Some((start_time, start_rss, ref message, format, unique)) = self.start_and_message {
if let Some(info) = &self.info {
let end_rss = get_resident_set_size();
let dur = start_time.elapsed();
print_time_passes_entry(message, dur, start_rss, end_rss, format, unique);
let dur = info.start_time.elapsed();
print_time_passes_entry(&info.message, dur, info.start_rss, end_rss, info.format);
}
}
}
Expand All @@ -772,18 +769,19 @@ pub fn print_time_passes_entry(
start_rss: Option<usize>,
end_rss: Option<usize>,
format: TimePassesFormat,
unique: bool,
) {
if format == TimePassesFormat::Json {
let json = json!({
"pass": what,
"time": dur.as_secs_f64(),
"rss_start": start_rss,
"rss_end": end_rss,
"unique": unique,
});
eprintln!("time: {}", json.to_string());
return;
match format {
TimePassesFormat::Json => {
let json = json!({
"pass": what,
"time": dur.as_secs_f64(),
"rss_start": start_rss,
"rss_end": end_rss,
});
eprintln!("time: {}", json.to_string());
return;
}
TimePassesFormat::Text => (),
}

// Print the pass if its duration is greater than 5 ms, or it changed the
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ pub fn main() -> ! {

if let Some(format) = callbacks.time_passes {
let end_rss = get_resident_set_size();
print_time_passes_entry("total", start_time.elapsed(), start_rss, end_rss, format, true);
print_time_passes_entry("total", start_time.elapsed(), start_rss, end_rss, format);
}

process::exit(exit_code)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_session/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::path::{Path, PathBuf};

impl Session {
pub fn timer(&self, what: &'static str) -> VerboseTimingGuard<'_> {
self.prof.unique_verbose_generic_activity(what)
self.prof.verbose_generic_activity(what)
}
pub fn time<R>(&self, what: &'static str, f: impl FnOnce() -> R) -> R {
self.prof.unique_verbose_generic_activity(what).run(f)
self.prof.verbose_generic_activity(what).run(f)
}
}

Expand Down

0 comments on commit 6c57dda

Please sign in to comment.