Skip to content

Commit

Permalink
Make '--output' optional benfred#229
Browse files Browse the repository at this point in the history
If no filename is specified by the user it is auto generated from
filename/pid the current time and the appropriate extension.
  • Loading branch information
Nudin committed Feb 12, 2021
1 parent 5f534b7 commit c6e9fd4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
41 changes: 38 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ serde_json = "1.0"
rand = "0.8"
rand_distr = "0.4"
remoteprocess = {version="0.4.2", features=["unwind"]}
chrono = "0.4.19"

[target.'cfg(unix)'.dependencies]
termios = "0.3.2"
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Config {
.value_name("filename")
.help("Output filename")
.takes_value(true)
.required(true))
.required(false))
.arg(Arg::with_name("format")
.short("f")
.long("format")
Expand Down
33 changes: 26 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern crate lazy_static;
extern crate libc;
#[macro_use]
extern crate log;
extern crate chrono;
#[cfg(unwind)]
extern crate lru;
extern crate memmap;
Expand Down Expand Up @@ -66,6 +67,8 @@ use stack_trace::{StackTrace, Frame};
use console_viewer::ConsoleViewer;
use config::{Config, FileFormat, RecordDuration};

use chrono::{SecondsFormat, Local};

#[cfg(unix)]
fn permission_denied(err: &Error) -> bool {
err.iter_chain().any(|cause| {
Expand Down Expand Up @@ -154,8 +157,24 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
};

let filename = match config.filename.as_ref() {
Some(filename) => filename,
None => return Err(format_err!("A filename is required to record samples"))
Some(filename) => filename.clone(),
None => {
let ext = match config.format.as_ref() {
Some(FileFormat::flamegraph) => "svg",
Some(FileFormat::speedscope) => "json",
Some(FileFormat::raw) => "txt",
None => return Err(format_err!("A file format is required to record samples"))
};
let local_time = Local::now().to_rfc3339_opts(SecondsFormat::Secs, true);
let name = match config.python_program.as_ref() {
Some(prog) => prog[0].to_string(),
None => match config.pid.as_ref() {
Some(pid) => pid.to_string(),
None => String::from("unknown")
}
};
format!("{}-{}.{}", name, local_time, ext)
}
};

let sampler = sampler::Sampler::new(pid, config)?;
Expand Down Expand Up @@ -294,25 +313,25 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
}

{
let mut out_file = std::fs::File::create(filename)?;
let mut out_file = std::fs::File::create(&filename)?;
output.write(&mut out_file)?;
}

match config.format.as_ref().unwrap() {
FileFormat::flamegraph => {
println!("{}Wrote flamegraph data to '{}'. Samples: {} Errors: {}", lede, filename, samples, errors);
println!("{}Wrote flamegraph data to '{}'. Samples: {} Errors: {}", lede, &filename, samples, errors);
// open generated flame graph in the browser on OSX (theory being that on linux
// you might be SSH'ed into a server somewhere and this isn't desired, but on
// that is pretty unlikely for osx) (note to self: xdg-open will open on linux)
#[cfg(target_os = "macos")]
std::process::Command::new("open").arg(filename).spawn()?;
std::process::Command::new("open").arg(&filename).spawn()?;
},
FileFormat::speedscope => {
println!("{}Wrote speedscope file to '{}'. Samples: {} Errors: {}", lede, filename, samples, errors);
println!("{}Wrote speedscope file to '{}'. Samples: {} Errors: {}", lede, &filename, samples, errors);
println!("{}Visit https://www.speedscope.app/ to view", lede);
},
FileFormat::raw => {
println!("{}Wrote raw flamegraph data to '{}'. Samples: {} Errors: {}", lede, filename, samples, errors);
println!("{}Wrote raw flamegraph data to '{}'. Samples: {} Errors: {}", lede, &filename, samples, errors);
println!("{}You can use the flamegraph.pl script from https://github.com/brendangregg/flamegraph to generate a SVG", lede);
}
};
Expand Down

0 comments on commit c6e9fd4

Please sign in to comment.