|
| 1 | +use std::ffi::OsString; |
| 2 | + |
| 3 | +use clap::Parser; |
| 4 | +use inferno::collapse::Collapse; |
| 5 | + |
| 6 | +#[derive(Parser, Debug)] |
| 7 | +#[clap(name = "blondie")] |
| 8 | +#[clap(bin_name = "blondie")] |
| 9 | +#[clap(author = "Nicolas Abram Lujan <[email protected]>")] |
| 10 | +#[clap(version = "0.1-alpha2")] |
| 11 | +#[clap(about = "CPU call stack sampling", long_about = None)] |
| 12 | +struct Blondie { |
| 13 | + /// If kernel stacks should be included in the output |
| 14 | + #[clap(short, long)] |
| 15 | + kernel_stacks: bool, |
| 16 | + /// Output filename. Defaults to flamegraph.svg |
| 17 | + #[clap(short, long, parse(from_os_str))] |
| 18 | + out: Option<std::path::PathBuf>, |
| 19 | + /// Don't redirect stdout/stderr from the target process |
| 20 | + #[clap(short, long)] |
| 21 | + hide_output: bool, |
| 22 | + |
| 23 | + #[clap(subcommand)] |
| 24 | + subcommand: Subcommands, |
| 25 | +} |
| 26 | +#[derive(clap::Subcommand, Debug)] |
| 27 | +enum Subcommands { |
| 28 | + /// Generate a flamegraph. Default output is ./flamegraph.svg |
| 29 | + #[clap(trailing_var_arg = true)] |
| 30 | + Flamegraph { |
| 31 | + /// Output filename for trace text callstacks. Defaults to nowhere. |
| 32 | + #[clap(short, long, parse(from_os_str))] |
| 33 | + trace_file: Option<std::path::PathBuf>, |
| 34 | + /// Output filename for inferno collapsed stacks. Defaults to nowhere. |
| 35 | + #[clap(short, long, parse(from_os_str))] |
| 36 | + collapsed_file: Option<std::path::PathBuf>, |
| 37 | + /// The command to run |
| 38 | + command: OsString, |
| 39 | + /// Arguments for the command to run |
| 40 | + args: Vec<OsString>, |
| 41 | + }, |
| 42 | + /// Generate a text file with the folded callstacks. Default output is ./folded_stacks.txt |
| 43 | + #[clap(trailing_var_arg = true)] |
| 44 | + FoldedText { |
| 45 | + /// The command to run |
| 46 | + command: OsString, |
| 47 | + /// Arguments for the command to run |
| 48 | + args: Vec<OsString>, |
| 49 | + }, |
| 50 | +} |
| 51 | + |
| 52 | +fn main() -> Result<(), blondie::Error> { |
| 53 | + let args = Blondie::parse(); |
| 54 | + |
| 55 | + let (command, command_args) = match &args.subcommand { |
| 56 | + Subcommands::Flamegraph { command, args, .. } => (command.clone(), args.clone()), |
| 57 | + Subcommands::FoldedText { command, args } => (command.clone(), args.clone()), |
| 58 | + }; |
| 59 | + let mut command_builder = std::process::Command::new(command); |
| 60 | + command_builder.args(command_args); |
| 61 | + let trace_ctx = blondie::trace_command(command_builder, args.kernel_stacks)?; |
| 62 | + |
| 63 | + match &args.subcommand { |
| 64 | + Subcommands::Flamegraph { |
| 65 | + trace_file, |
| 66 | + collapsed_file, |
| 67 | + .. |
| 68 | + } => { |
| 69 | + let filename = args.out.unwrap_or("./flamegraph.svg".into()); |
| 70 | + |
| 71 | + let mut trace_output = Vec::new(); |
| 72 | + trace_ctx.write_dtrace(&mut trace_output)?; |
| 73 | + if let Some(trace_file) = trace_file { |
| 74 | + std::fs::write(trace_file, &trace_output).unwrap(); |
| 75 | + } |
| 76 | + |
| 77 | + let mut collapsed_output = Vec::new(); |
| 78 | + let collapse_options = inferno::collapse::dtrace::Options::default(); |
| 79 | + inferno::collapse::dtrace::Folder::from(collapse_options) |
| 80 | + .collapse(&trace_output[..], &mut collapsed_output) |
| 81 | + .expect("unable to collapse generated profile data"); |
| 82 | + if let Some(collapsed_file) = collapsed_file { |
| 83 | + std::fs::write(collapsed_file, &collapsed_output).unwrap(); |
| 84 | + } |
| 85 | + |
| 86 | + let flamegraph_file = std::fs::File::create(&filename).expect(&format!( |
| 87 | + "Error creating flamegraph file {}", |
| 88 | + filename.into_os_string().into_string().unwrap() |
| 89 | + )); |
| 90 | + let flamegraph_writer = std::io::BufWriter::new(flamegraph_file); |
| 91 | + let mut inferno_opts = inferno::flamegraph::Options::default(); |
| 92 | + inferno::flamegraph::from_reader( |
| 93 | + &mut inferno_opts, |
| 94 | + &collapsed_output[..], |
| 95 | + flamegraph_writer, |
| 96 | + ) |
| 97 | + .expect("unable to generate a flamegraph from the collapsed stack data"); |
| 98 | + } |
| 99 | + Subcommands::FoldedText { .. } => { |
| 100 | + let filename = args.out.unwrap_or("./folded_stacks.txt".into()); |
| 101 | + |
| 102 | + let f = std::fs::File::create(&filename).expect(&format!( |
| 103 | + "Unable to create file {}", |
| 104 | + filename.into_os_string().into_string().unwrap() |
| 105 | + )); |
| 106 | + let mut f = std::io::BufWriter::new(f); |
| 107 | + |
| 108 | + trace_ctx.write_dtrace(&mut f)?; |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + Ok(()) |
| 113 | +} |
0 commit comments