From e0cd1d30b4e4bb4856971de38fa1b977113fa9d7 Mon Sep 17 00:00:00 2001 From: Tim Upthegrove Date: Thu, 7 Nov 2024 09:47:21 +0900 Subject: [PATCH] Show indicators that perf script is running Perf script can take a long time to run. This change adds an indicator to show that it is running so that the user knows that their application is not causing the terminal to hang. Addresses #270 --- Cargo.lock | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 16 ++++++++++++- 3 files changed, 82 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index af0c9a1..070f9b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -349,6 +349,19 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "unicode-width", + "windows-sys 0.52.0", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -463,6 +476,12 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encoding_rs" version = "0.8.35" @@ -493,6 +512,7 @@ dependencies = [ "cargo_metadata", "clap", "clap_complete", + "indicatif", "inferno", "opener", "shlex", @@ -850,6 +870,19 @@ dependencies = [ "hashbrown 0.15.1", ] +[[package]] +name = "indicatif" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "763a5a8f45087d6bcea4222e7b72c291a054edf80e4ef6efd2a4979878c7bea3" +dependencies = [ + "console", + "instant", + "number_prefix", + "portable-atomic", + "unicode-width", +] + [[package]] name = "inferno" version = "0.11.21" @@ -880,6 +913,15 @@ dependencies = [ "smallvec", ] +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", +] + [[package]] name = "ipnet" version = "2.10.1" @@ -918,6 +960,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.161" @@ -1045,6 +1093,12 @@ dependencies = [ "itoa", ] +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + [[package]] name = "object" version = "0.30.4" @@ -1144,6 +1198,12 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" +[[package]] +name = "portable-atomic" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" + [[package]] name = "powerfmt" version = "0.2.0" @@ -1667,6 +1727,12 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "untrusted" version = "0.9.0" diff --git a/Cargo.toml b/Cargo.toml index a44cb02..54270a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ anyhow = "1.0.43" cargo_metadata = "0.18" clap = { version = "4.0.11", features = ["derive"] } clap_complete = "4.0.2" +indicatif = "0.17.8" inferno = { version = "0.11.0", default_features = false, features = ["multithreaded", "nameattr"] } opener = "0.7.1" shlex = "1.1.0" diff --git a/src/lib.rs b/src/lib.rs index 58b0d03..5923bb4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,6 +34,9 @@ pub enum Workload { #[cfg(target_os = "linux")] mod arch { + use indicatif::{ProgressBar, ProgressStyle}; + use std::time::Duration; + use super::*; pub const SPAWN_ERROR: &str = "could not spawn perf"; @@ -116,7 +119,18 @@ mod arch { command.arg(perf_output); } - let output = command.output().context("unable to call perf script")?; + // perf script can take a long time to run. Notify the user that it is running + // by using a spinner. Note that if this function exits before calling + // spinner.finish(), then the spinner will be completely removed from the terminal. + let spinner = ProgressBar::new_spinner().with_prefix("Running perf script"); + spinner.set_style( + ProgressStyle::with_template("{prefix} [{elapsed}]: {spinner:.green}").unwrap(), + ); + spinner.enable_steady_tick(Duration::from_millis(500)); + + let result = command.output().context("unable to call perf script"); + spinner.finish(); + let output = result?; if !output.status.success() { anyhow::bail!(format!( "unable to run 'perf script': ({}) {}",