From 7204374719f4021ce06c25e7dd72b09a56923954 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 09:44:21 +0200 Subject: Report instructions in addition to time They hopefully will be more stable on CI --- crates/ra_prof/Cargo.toml | 1 + crates/ra_prof/src/lib.rs | 2 + crates/ra_prof/src/stop_watch.rs | 72 ++++++++++++++++++++++++++ crates/rust-analyzer/src/cli/analysis_stats.rs | 40 +++++++------- 4 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 crates/ra_prof/src/stop_watch.rs (limited to 'crates') diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml index 6c214501e..e41cb5f52 100644 --- a/crates/ra_prof/Cargo.toml +++ b/crates/ra_prof/Cargo.toml @@ -15,6 +15,7 @@ once_cell = "1.3.1" backtrace = { version = "0.3.44", optional = true } cfg-if = "0.1.10" libc = "0.2.73" +perf-event = "0.4" [features] cpu_profiler = [] diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs index ba5609703..eb50965ae 100644 --- a/crates/ra_prof/src/lib.rs +++ b/crates/ra_prof/src/lib.rs @@ -1,5 +1,6 @@ //! A collection of tools for profiling rust-analyzer. +mod stop_watch; mod memory_usage; #[cfg(feature = "cpu_profiler")] mod google_cpu_profiler; @@ -11,6 +12,7 @@ use std::cell::RefCell; pub use crate::{ hprof::{init, init_from, profile}, memory_usage::{Bytes, MemoryUsage}, + stop_watch::{StopWatch, StopWatchSpan}, }; /// Prints backtrace to stderr, useful for debugging. diff --git a/crates/ra_prof/src/stop_watch.rs b/crates/ra_prof/src/stop_watch.rs new file mode 100644 index 000000000..54bfb0559 --- /dev/null +++ b/crates/ra_prof/src/stop_watch.rs @@ -0,0 +1,72 @@ +use crate::MemoryUsage; +use std::{ + fmt, + time::{Duration, Instant}, +}; + +pub struct StopWatch { + time: Instant, + counter: Option, + memory: Option, +} + +pub struct StopWatchSpan { + pub time: Duration, + pub instructions: Option, + pub memory: Option, +} + +impl StopWatch { + pub fn start() -> StopWatch { + let mut counter = perf_event::Builder::new().build().ok(); + if let Some(counter) = &mut counter { + let _ = counter.enable(); + } + let time = Instant::now(); + StopWatch { time, counter, memory: None } + } + pub fn memory(mut self, yes: bool) -> StopWatch { + if yes { + self.memory = Some(MemoryUsage::current()); + } + self + } + pub fn elapsed(&mut self) -> StopWatchSpan { + let time = self.time.elapsed(); + let instructions = self.counter.as_mut().and_then(|it| it.read().ok()); + let memory = self.memory.map(|it| MemoryUsage::current() - it); + StopWatchSpan { time, instructions, memory } + } +} + +impl fmt::Display for StopWatchSpan { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:.2?}", self.time)?; + if let Some(mut instructions) = self.instructions { + let mut prefix = ""; + if instructions > 10000 { + instructions /= 1000; + prefix = "k" + } + if instructions > 10000 { + instructions /= 1000; + prefix = "m" + } + write!(f, ", {}{}i", instructions, prefix)?; + } + if let Some(memory) = self.memory { + write!(f, ", {}", memory)?; + } + Ok(()) + } +} + +// Unclear if we need this: +// https://github.com/jimblandy/perf-event/issues/8 +impl Drop for StopWatch { + fn drop(&mut self) { + if let Some(mut counter) = self.counter.take() { + let _ = counter.disable(); + } + } +} diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index cf0d82b62..37b8337ef 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -3,7 +3,7 @@ use std::{ path::Path, - time::{Instant, SystemTime, UNIX_EPOCH}, + time::{SystemTime, UNIX_EPOCH}, }; use hir::{ @@ -29,6 +29,7 @@ use crate::{ }, print_memory_usage, }; +use ra_prof::StopWatch; /// Need to wrap Snapshot to provide `Clone` impl for `map_with` struct Snap(DB); @@ -54,11 +55,12 @@ pub fn analysis_stats( Rand32::new(seed) }; - let db_load_time = Instant::now(); + let mut db_load_sw = StopWatch::start().memory(memory_usage); let (host, vfs) = load_cargo(path, load_output_dirs, with_proc_macro)?; let db = host.raw_database(); - eprintln!("Database loaded {:?}", db_load_time.elapsed()); - let analysis_time = Instant::now(); + eprintln!("Database loaded {}", db_load_sw.elapsed()); + + let mut analysis_sw = StopWatch::start().memory(memory_usage); let mut num_crates = 0; let mut visited_modules = FxHashSet::default(); let mut visit_queue = Vec::new(); @@ -110,8 +112,7 @@ pub fn analysis_stats( eprintln!("Total modules found: {}", visited_modules.len()); eprintln!("Total declarations: {}", num_decls); eprintln!("Total functions: {}", funcs.len()); - let item_collection_memory = ra_prof::memory_usage(); - eprintln!("Item Collection: {:?}, {}", analysis_time.elapsed(), item_collection_memory); + eprintln!("Item Collection: {}", analysis_sw.elapsed()); if randomize { shuffle(&mut rng, &mut funcs); @@ -123,7 +124,7 @@ pub fn analysis_stats( }; if parallel { - let inference_time = Instant::now(); + let mut inference_sw = StopWatch::start().memory(memory_usage); let snap = Snap(db.snapshot()); funcs .par_iter() @@ -133,14 +134,10 @@ pub fn analysis_stats( snap.0.infer(f_id.into()); }) .count(); - eprintln!( - "Parallel Inference: {:?}, {}", - inference_time.elapsed(), - ra_prof::memory_usage() - ); + eprintln!("Parallel Inference: {}", inference_sw.elapsed()); } - let inference_time = Instant::now(); + let mut inference_sw = StopWatch::start().memory(memory_usage); bar.tick(); let mut num_exprs = 0; let mut num_exprs_unknown = 0; @@ -291,14 +288,17 @@ pub fn analysis_stats( eprintln!("Type mismatches: {}", num_type_mismatches); report_metric("type mismatches", num_type_mismatches, "#"); - let inference_time = inference_time.elapsed(); - let total_memory = ra_prof::memory_usage(); - eprintln!("Inference: {:?}, {}", inference_time, total_memory - item_collection_memory); + eprintln!("Inference: {}", inference_sw.elapsed()); - let analysis_time = analysis_time.elapsed(); - eprintln!("Total: {:?}, {}", analysis_time, total_memory); - report_metric("total time", analysis_time.as_millis() as u64, "ms"); - report_metric("total memory", total_memory.allocated.megabytes() as u64, "MB"); + let total_span = analysis_sw.elapsed(); + eprintln!("Total: {}", total_span); + report_metric("total time", total_span.time.as_millis() as u64, "ms"); + if let Some(instructions) = total_span.instructions { + report_metric("total time", instructions, "#instr"); + } + if let Some(memory) = total_span.memory { + report_metric("total memory", memory.allocated.megabytes() as u64, "MB"); + } if memory_usage { print_memory_usage(host, vfs); -- cgit v1.2.3 From e9983f98a964a940bfe5283b02f0f2dc16d3095a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 10:30:01 +0200 Subject: Do not show progress with parallel analysis --- crates/rust-analyzer/src/cli/analysis_stats.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates') diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 37b8337ef..73ec3204b 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -120,6 +120,7 @@ pub fn analysis_stats( let mut bar = match verbosity { Verbosity::Quiet | Verbosity::Spammy => ProgressReport::hidden(), + _ if parallel => ProgressReport::hidden(), _ => ProgressReport::new(funcs.len() as u64), }; -- cgit v1.2.3 From 5e498546e8275c86e0b3f711b069b2787c6385f2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 10:40:55 +0200 Subject: Fix non-linux compilation --- crates/ra_prof/Cargo.toml | 2 ++ crates/ra_prof/src/stop_watch.rs | 29 +++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) (limited to 'crates') diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml index e41cb5f52..c82b9f76d 100644 --- a/crates/ra_prof/Cargo.toml +++ b/crates/ra_prof/Cargo.toml @@ -15,6 +15,8 @@ once_cell = "1.3.1" backtrace = { version = "0.3.44", optional = true } cfg-if = "0.1.10" libc = "0.2.73" + +[target.'cfg(target_os = "linux")'.dependencies] perf-event = "0.4" [features] diff --git a/crates/ra_prof/src/stop_watch.rs b/crates/ra_prof/src/stop_watch.rs index 54bfb0559..c52c92ce5 100644 --- a/crates/ra_prof/src/stop_watch.rs +++ b/crates/ra_prof/src/stop_watch.rs @@ -1,11 +1,13 @@ -use crate::MemoryUsage; use std::{ fmt, time::{Duration, Instant}, }; +use crate::MemoryUsage; + pub struct StopWatch { time: Instant, + #[cfg(target_os = "linux")] counter: Option, memory: Option, } @@ -18,12 +20,21 @@ pub struct StopWatchSpan { impl StopWatch { pub fn start() -> StopWatch { - let mut counter = perf_event::Builder::new().build().ok(); - if let Some(counter) = &mut counter { - let _ = counter.enable(); - } + #[cfg(target_os = "linux")] + let counter = { + let mut counter = perf_event::Builder::new().build().ok(); + if let Some(counter) = &mut counter { + let _ = counter.enable(); + } + counter + }; let time = Instant::now(); - StopWatch { time, counter, memory: None } + StopWatch { + time, + #[cfg(target_os = "linux")] + counter, + memory: None, + } } pub fn memory(mut self, yes: bool) -> StopWatch { if yes { @@ -33,7 +44,12 @@ impl StopWatch { } pub fn elapsed(&mut self) -> StopWatchSpan { let time = self.time.elapsed(); + + #[cfg(target_os = "linux")] let instructions = self.counter.as_mut().and_then(|it| it.read().ok()); + #[cfg(not(target_os = "linux"))] + let instructions = None; + let memory = self.memory.map(|it| MemoryUsage::current() - it); StopWatchSpan { time, instructions, memory } } @@ -65,6 +81,7 @@ impl fmt::Display for StopWatchSpan { // https://github.com/jimblandy/perf-event/issues/8 impl Drop for StopWatch { fn drop(&mut self) { + #[cfg(target_os = "linux")] if let Some(mut counter) = self.counter.take() { let _ = counter.disable(); } -- cgit v1.2.3 From f22af66c3763c4b2a9d16621473cb6979fb2f36d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 10:43:47 +0200 Subject: Fixes --- crates/ra_prof/src/stop_watch.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'crates') diff --git a/crates/ra_prof/src/stop_watch.rs b/crates/ra_prof/src/stop_watch.rs index c52c92ce5..8b8ec25a5 100644 --- a/crates/ra_prof/src/stop_watch.rs +++ b/crates/ra_prof/src/stop_watch.rs @@ -1,3 +1,4 @@ +//! Like `std::time::Instant`, but also measures memory & CPU cycles. use std::{ fmt, time::{Duration, Instant}, @@ -76,14 +77,3 @@ impl fmt::Display for StopWatchSpan { Ok(()) } } - -// Unclear if we need this: -// https://github.com/jimblandy/perf-event/issues/8 -impl Drop for StopWatch { - fn drop(&mut self) { - #[cfg(target_os = "linux")] - if let Some(mut counter) = self.counter.take() { - let _ = counter.disable(); - } - } -} -- cgit v1.2.3