Skip to content

Commit

Permalink
chore: remove old tracing cmd, samply load
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Oct 18, 2024
1 parent 02f7572 commit 5c47c49
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 451 deletions.
1 change: 1 addition & 0 deletions crates/cli/output.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions crates/cli/src/bin/cargo-prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sp1_cli::{
commands::{
build::BuildCmd, build_toolchain::BuildToolchainCmd,
install_toolchain::InstallToolchainCmd, new::NewCmd, profile::ProfileCmd, prove::ProveCmd,
trace::TraceCmd, vkey::VkeyCmd,
vkey::VkeyCmd,
},
SP1_VERSION_MESSAGE,
};
Expand Down Expand Up @@ -32,7 +32,6 @@ pub enum ProveCliCommands {
Prove(ProveCmd),
BuildToolchain(BuildToolchainCmd),
InstallToolchain(InstallToolchainCmd),
Trace(TraceCmd),
Vkey(VkeyCmd),
Profile(ProfileCmd),
}
Expand All @@ -46,7 +45,6 @@ fn main() -> Result<()> {
ProveCliCommands::Prove(cmd) => cmd.run(),
ProveCliCommands::BuildToolchain(cmd) => cmd.run(),
ProveCliCommands::InstallToolchain(cmd) => cmd.run(),
ProveCliCommands::Trace(cmd) => cmd.run(),
ProveCliCommands::Vkey(cmd) => cmd.run(),
ProveCliCommands::Profile(cmd) => cmd.run(),
}
Expand Down
1 change: 0 additions & 1 deletion crates/cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ pub mod install_toolchain;
pub mod new;
pub mod profile;
pub mod prove;
pub mod trace;
pub mod vkey;
74 changes: 66 additions & 8 deletions crates/cli/src/commands/profile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;
use gecko_profile::{Frame, ProfileBuilder, ThreadBuilder};
use std::path::PathBuf;
use std::process::Stdio;

use anyhow::Result;
use goblin::elf::{sym::STT_FUNC, Elf};
Expand All @@ -16,7 +17,7 @@ pub struct ProfileCmd {

/// Path to the trace file. Simply run the program with `TRACE_FILE=trace.log` environment
/// variable. File must be one u64 program counter per line
#[arg(long, required = true)]
#[arg(short = 't', long, required = true)]
trace: PathBuf,

/// The output file to write the gecko profile to, should be a json
Expand All @@ -25,8 +26,16 @@ pub struct ProfileCmd {

/// The sample rate to use for the profile.
/// This is the number of instructions to between samples.
#[arg(long, default_value = "10")]
sample_rate: u64,
#[arg(short = 'r', long, default_value = "10")]
sample_rate: usize,

/// Name the circuit, this will be displayed in the UI
#[arg(short = 'n', long, default_value = "sp1")]
name: String,

/// Do not open the profiler visualizer after creating the profile.
#[arg(long)]
no_open: bool,
}

pub struct Sample {
Expand All @@ -36,15 +45,19 @@ pub struct Sample {

impl ProfileCmd {
pub fn run(&self) -> anyhow::Result<()> {
let samples = collect_samples(&self.elf, &self.trace, self.sample_rate)?;
let samples = collect_samples(&self.elf, &self.trace, self.sample_rate as u64)?;

println!("Got {} samples", samples.len());
println!(
"Collected {} samples from {} instructions",
samples.len(),
samples.len() * self.sample_rate
);

let start_time = std::time::Instant::now();
let mut profile_builder = ProfileBuilder::new(
start_time,
std::time::SystemTime::now(),
"sp1",
&self.name,
0,
std::time::Duration::from_micros(1),
);
Expand All @@ -62,10 +75,10 @@ impl ProfileCmd {
last_known_time,
frames.into_iter(),
// this is actually in instructions but
std::time::Duration::from_micros(self.sample_rate),
std::time::Duration::from_micros(self.sample_rate as u64),
);

last_known_time += std::time::Duration::from_micros(self.sample_rate);
last_known_time += std::time::Duration::from_micros(self.sample_rate as u64);
}

profile_builder.add_thread(thread_builder);
Expand All @@ -74,6 +87,11 @@ impl ProfileCmd {
let mut file = std::fs::File::create(&canon_path)?;
serde_json::to_writer(&mut file, &profile_builder.to_serializable())?;

if !self.no_open {
maybe_samply_install();
samply_load(&canon_path);
}

Ok(())
}
}
Expand Down Expand Up @@ -198,3 +216,43 @@ pub fn collect_samples(

Ok(samples)
}

/// Check if samply is installed otherwise install it.
fn maybe_samply_install() {
let samply = std::process::Command::new("samply")
.stdout(Stdio::null())
.arg("--version")
.status()
.is_ok();

if !samply {
println!("Installing samply");

let status = std::process::Command::new("cargo")
.stdout(Stdio::inherit())
.stdin(Stdio::null())
.arg("install")
.arg("--locked")
.arg("samply")
.status()
.expect("Failed to install samply");

if !status.success() {
panic!("Failed to install samply");
}
}
}

fn samply_load(file: impl AsRef<Path>) {
println!("Loading profile with samply");
let status = std::process::Command::new("samply")
.arg("load")
.arg(file.as_ref())
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.status();

if let Err(e) = status {
eprintln!("Failed to load samply profile: {}", e);
}
}
Loading

0 comments on commit 5c47c49

Please sign in to comment.