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

WIP: Add simple pmc collapse to replace stackcollapse-pmc.pl. #214

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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 @@ -80,6 +80,11 @@ name = "inferno-collapse-vtune"
path = "src/bin/collapse-vtune.rs"
required-features = ["cli"]

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

[[bin]]
name = "inferno-collapse-guess"
path = "src/bin/collapse-guess.rs"
Expand Down
6 changes: 4 additions & 2 deletions benches/collapse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use std::fs::File;
use std::io::{self, Read};

use criterion::*;
use inferno::collapse::{dtrace, perf, sample, Collapse};
use inferno::collapse::{dtrace, perf, pmc, sample, Collapse};
use lazy_static::lazy_static;
use libflate::gzip::Decoder;

const INFILE_DTRACE: &str = "flamegraph/example-dtrace-stacks.txt";
const INFILE_PERF: &str = "flamegraph/example-perf-stacks.txt.gz";
const INFILE_SAMPLE: &str = "tests/data/collapse-sample/large.txt.gz";
const INFILE_PMC: &str = "tests/data/collapse-pmc/large.txt.gz";
const SAMPLE_SIZE: usize = 100;

lazy_static! {
Expand Down Expand Up @@ -96,7 +97,8 @@ macro_rules! benchmark_multi {
benchmark_multi!(dtrace, "dtrace", INFILE_DTRACE);
benchmark_multi!(perf, "perf", INFILE_PERF);
benchmark_single!(sample, "sample", INFILE_SAMPLE);
benchmark_multi!(pmc, "pmc", INFILE_PMC);

criterion_group!(benches, dtrace, perf, sample);
criterion_group!(benches, dtrace, perf, pmc, sample);

criterion_main!(benches);
9 changes: 9 additions & 0 deletions compare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ zcat < tests/data/collapse-sample/large.txt.gz > "$f"
echo "==> sample <=="
hyperfine --warmup 20 -m 50 "$BIN/inferno-collapse-sample $f" "./flamegraph/stackcollapse-sample.awk $f"
rm "$f"

echo
echo
cargo build --release --bin inferno-collapse-pmc
f=tests/data/collapse-pmc/large.txt
zcat < tests/data/collapse-pmc/large.txt.gz > "$f"
echo "==> pmc <=="
hyperfine --warmup 20 -m 50 "$BIN/inferno-collapse-pmc $f" "./flamegraph/stackcollapse-pmc.pl $f"
rm "$f"
86 changes: 86 additions & 0 deletions src/bin/collapse-pmc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::io;
use std::path::PathBuf;

use env_logger::Env;
use inferno::collapse::pmc::{Folder, Options};
use inferno::collapse::{Collapse, DEFAULT_NTHREADS};
use lazy_static::lazy_static;
use structopt::StructOpt;

lazy_static! {
static ref NTHREADS: String = format!("{}", *DEFAULT_NTHREADS);
}

#[derive(Debug, StructOpt)]
#[structopt(
name = "inferno-collapse-pmc",
about,
after_help = "\
[1] pmcstat must be used in callchain mode (-G).
For example:
To capture, use:
pmcstat -S unhalted-cycles -O pmc.out
To convert to callchain, you can use:
pmcstat -R pmc.out -z16 -G pmc.graph
Then collapse all stacks to flamegraph format
inferno-collapse-pmc pmc.graph
"
)]
struct Opt {
// ************* //
// *** FLAGS *** //
// ************* //
/// 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,

// *************** //
// *** OPTIONS *** //
// *************** //
/// Number of threads to use
#[structopt(
short = "n",
long = "nthreads",
default_value = &NTHREADS,
value_name = "UINT"
)]
nthreads: usize,

// ************ //
// *** ARGS *** //
// ************ //
#[structopt(value_name = "PATH")]
/// Pmcstat -G output file, or STDIN if not specified
infile: Option<PathBuf>,
}

impl Opt {
fn into_parts(self) -> (Option<PathBuf>, Options) {
let mut options = Options::default();
options.nthreads = self.nthreads;
(self.infile, options)
}
}

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",
}))
.format_timestamp(None)
.init();
}

let (infile, options) = opt.into_parts();
Folder::from(options).collapse_file_to_stdout(infile.as_ref())
}
12 changes: 10 additions & 2 deletions src/collapse/guess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::{self, Cursor};

use log::{error, info};

use crate::collapse::{self, dtrace, perf, sample, vtune, Collapse};
use crate::collapse::{self, dtrace, perf, pmc, sample, vtune, Collapse};

const LINES_PER_ITERATION: usize = 10;

Expand Down Expand Up @@ -69,10 +69,17 @@ impl Collapse for Folder {
};
let mut sample = sample::Folder::default();
let mut vtune = vtune::Folder::default();
let mut pmc = {
let options = pmc::Options {
nthreads: self.opt.nthreads,
..Default::default()
};
pmc::Folder::from(options)
};

// 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; 4];
let mut not_applicable = [false; 5];

let mut buffer = String::new();
loop {
Expand Down Expand Up @@ -106,6 +113,7 @@ impl Collapse for Folder {
try_collapse_impl!(dtrace, 1);
try_collapse_impl!(sample, 2);
try_collapse_impl!(vtune, 3);
try_collapse_impl!(pmc, 4);

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

/// Stack collapsing for the output of [`pmcstat`](https://www.freebsd.org/cgi/man.cgi?query=pmcstat&sektion=8) in callchain mode (-G).
///
/// See the [crate-level documentation] for details.
///
/// [crate-level documentation]: ../../index.html
pub mod pmc;

// DEFAULT_NTHREADS is public because we use it in the help text of the binaries,
// but it doesn't need to be exposed to library users, hence #[doc(hidden)].
#[doc(hidden)]
Expand Down
Loading