Skip to content

Commit

Permalink
Merge pull request #53 from ASankaran/profile-by-pid
Browse files Browse the repository at this point in the history
Profile by pid
  • Loading branch information
jonas-schievink authored Dec 2, 2019
2 parents cdaee7c + 092d1f1 commit 9e278f6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/bin/cargo-flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::path::{Path, PathBuf};

use structopt::StructOpt;

use flamegraph::Workload;

#[derive(Debug, StructOpt)]
#[structopt(raw(
setting = "structopt::clap::AppSettings::TrailingVarArg"
Expand Down Expand Up @@ -285,8 +287,8 @@ fn main() {
.take()
.unwrap_or("flamegraph.svg".into());

flamegraph::generate_flamegraph_by_running_command(
workload,
flamegraph::generate_flamegraph_for_workload(
Workload::Command(workload),
&flamegraph_filename,
opt.root,
);
Expand Down
35 changes: 28 additions & 7 deletions src/bin/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::path::PathBuf;

use structopt::StructOpt;

use flamegraph::Workload;

#[derive(Debug, StructOpt)]
#[structopt(raw(
setting = "structopt::clap::AppSettings::TrailingVarArg"
Expand All @@ -19,16 +21,35 @@ struct Opt {
#[structopt(long = "root")]
root: bool,

/// Profile a running process by pid
#[structopt(
short = "p",
long = "pid"
)]
pid: Option<u32>,

trailing_arguments: Vec<String>,
}

fn workload(opt: &Opt) -> Vec<String> {
if opt.trailing_arguments.is_empty() {
eprintln!("no workload given to generate a flamegraph for!");
std::process::exit(1);
}
fn workload(opt: &Opt) -> Workload {
match opt.pid {
Some(p) => {
if !opt.trailing_arguments.is_empty() {
eprintln!("only a pid or command can be specified!");
std::process::exit(1);
}

Workload::Pid(p)
},
None => {
if opt.trailing_arguments.is_empty() {
eprintln!("no workload given to generate a flamegraph for!");
std::process::exit(1);
}

opt.trailing_arguments.clone()
Workload::Command(opt.trailing_arguments.clone())
}
}
}

fn main() {
Expand All @@ -41,7 +62,7 @@ fn main() {
.take()
.unwrap_or("flamegraph.svg".into());

flamegraph::generate_flamegraph_by_running_command(
flamegraph::generate_flamegraph_for_workload(
workload,
flamegraph_filename,
opt.root,
Expand Down
35 changes: 28 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ use inferno::{
#[cfg(unix)]
use signal_hook;

pub enum Workload {
Command(Vec<String>),
Pid(u32),
}

#[cfg(target_os = "linux")]
mod arch {
use super::*;
Expand All @@ -38,7 +43,7 @@ mod arch {
child command to exit";

pub(crate) fn initial_command(
workload: Vec<String>,
workload: Workload,
sudo: bool,
) -> Command {
let mut command = if sudo {
Expand All @@ -55,7 +60,15 @@ mod arch {
command.arg(arg);
}

command.args(&workload);
match workload {
Workload::Command(c) => {
command.args(&c);
},
Workload::Pid(p) => {
command.arg("-p");
command.arg(p.to_string());
},
}

command
}
Expand All @@ -80,7 +93,7 @@ mod arch {
child command to exit";

pub(crate) fn initial_command(
workload: Vec<String>,
workload: Workload,
sudo: bool,
) -> Command {
let mut command = if sudo {
Expand All @@ -103,8 +116,16 @@ mod arch {
command.arg("-o");
command.arg("cargo-flamegraph.stacks");

command.arg("-c");
command.args(&workload);
match workload {
Workload::Command(c) => {
command.arg("-c");
command.args(&c);
},
Workload::Pid(p) => {
command.arg("-p");
command.arg(p.to_string());
},
}

command
}
Expand Down Expand Up @@ -147,10 +168,10 @@ fn terminated_by_error(status: ExitStatus) -> bool {
!status.success()
}

pub fn generate_flamegraph_by_running_command<
pub fn generate_flamegraph_for_workload<
P: AsRef<std::path::Path>,
>(
workload: Vec<String>,
workload: Workload,
flamegraph_filename: P,
sudo: bool,
) {
Expand Down

0 comments on commit 9e278f6

Please sign in to comment.