Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profile by pid #53

Merged
merged 4 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -26,6 +26,11 @@ use inferno::{

use signal_hook;

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

#[cfg(target_os = "linux")]
mod arch {
use super::*;
Expand All @@ -37,7 +42,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 @@ -54,7 +59,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 @@ -79,7 +92,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 @@ -102,8 +115,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 @@ -146,10 +167,10 @@ fn terminated_by_error(status: ExitStatus) -> bool {
!exit_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