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

Add collapser for sample on macOS #133

Merged
merged 11 commits into from
Jul 6, 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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ name = "inferno-collapse-dtrace"
path = "src/bin/collapse-dtrace.rs"
required-features = ["cli"]

[[bin]]
name = "inferno-collapse-sample"
path = "src/bin/collapse-sample.rs"
required-features = ["cli"]

[[bin]]
name = "inferno-collapse-guess"
path = "src/bin/collapse-guess.rs"
Expand Down
13 changes: 12 additions & 1 deletion benches/collapse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use criterion::*;
use inferno::collapse::dtrace;
use inferno::collapse::perf;
use inferno::collapse::sample;
use inferno::collapse::Collapse;
use libflate::gzip::Decoder;
use std::fs::File;
Expand Down Expand Up @@ -56,5 +57,15 @@ fn perf(c: &mut Criterion) {
);
}

criterion_group!(benches, dtrace, perf);
fn sample(c: &mut Criterion) {
let infile = "tests/data/collapse-sample/sample.txt";
collapse_benchmark(
c,
sample::Folder::from(sample::Options::default()),
"sample",
infile,
);
}

criterion_group!(benches, dtrace, perf, sample);
criterion_main!(benches);
62 changes: 62 additions & 0 deletions src/bin/collapse-sample.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use env_logger::Env;
use std::io;
use std::path::PathBuf;
use structopt::StructOpt;

use inferno::collapse::sample::{Folder, Options};
use inferno::collapse::Collapse;

#[derive(Debug, StructOpt)]
#[structopt(
name = "inferno-collapse-sample",
author = "",
after_help = "\
[1] This processes the result of the sample command on macOS:
sample 1234 -file out.sample_stacks"
)]
struct Opt {
/// Don't include modules with function names
#[structopt(long = "no-modules")]
no_modules: bool,

/// Silence all log output
#[structopt(short = "q", long = "quiet")]
quiet: bool,

/// Verbose logging mode (-v, -vv, -vvv)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
verbose: usize,

/// sample output file, or STDIN if not specified
infile: Option<PathBuf>,
}

impl Opt {
fn into_parts(self) -> (Option<PathBuf>, Options) {
(
self.infile,
Options {
no_modules: self.no_modules,
},
)
}
}

fn main() -> io::Result<()> {
let opt = Opt::from_args();

// Initialize logger
if !opt.quiet {
env_logger::Builder::from_env(Env::default().default_filter_or(match opt.verbose {
0 => "warn",
1 => "info",
2 => "debug",
_ => "trace",
}))
.default_format_timestamp(false)
.init();
}

let (infile, options) = opt.into_parts();
Folder::from(options).collapse_file(infile.as_ref(), io::stdout().lock())
}
6 changes: 4 additions & 2 deletions src/collapse/guess.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Collapse;
use super::{dtrace, perf};
use super::{dtrace, perf, sample};
use log::{error, info};
use std::io::prelude::*;
use std::io::{self, Cursor};
Expand All @@ -22,10 +22,11 @@ impl Collapse for Folder {
{
let mut perf = perf::Folder::from(perf::Options::default());
let mut dtrace = dtrace::Folder::from(dtrace::Options::default());
let mut sample = sample::Folder::from(sample::Options::default());

// Each Collapse impl gets its own flag in this array.
// It gets set to true when the impl has been ruled out.
let mut not_applicable = [false; 2];
let mut not_applicable = [false; 3];

let mut buffer = String::new();
loop {
Expand Down Expand Up @@ -57,6 +58,7 @@ impl Collapse for Folder {
}
try_collapse_impl!(perf, 0);
try_collapse_impl!(dtrace, 1);
try_collapse_impl!(sample, 2);

if eof {
break;
Expand Down
9 changes: 9 additions & 0 deletions src/collapse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ pub mod dtrace;
/// [crate-level documentation]: ../../index.html
pub mod perf;

/// Stack collapsing for the output of [`sample`](https://gist.github.com/loderunner/36724cc9ee8db66db305#profiling-with-sample) on macOS.
///
/// See the [crate-level documentation] for details.
///
/// [crate-level documentation]: ../../index.html
pub mod sample;

pub(crate) mod util;

use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
Expand Down
11 changes: 10 additions & 1 deletion src/collapse/perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,17 @@ impl Folder {
let pc = line.next()?.trim_end();
let mut line = line.next()?.rsplitn(2, ' ');
let mut module = line.next()?;
// module is always wrapped in (), so remove those

// Module should always be wrapped in (), so remove those if they exist.
// We first check for their existence because it's possible this is being
// called from `is_applicable` on a non-perf profile. This both prevents
// a panic if `module.len() < 1` and helps detect whether or not we're
// parsing a `perf` profile and not something else.
if !module.starts_with('(') || !module.ends_with(')') {
jasonrhansen marked this conversation as resolved.
Show resolved Hide resolved
return None;
}
module = &module[1..(module.len() - 1)];

let rawfunc = match line.next()?.trim() {
// Sometimes there are two spaces betwen the pc and the (, like:
// 7f1e2215d058 (/lib/x86_64-linux-gnu/libc-2.15.so)
Expand Down
Loading