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

use Inferno to generate flamegraphs #73

Merged
merged 1 commit into from
Nov 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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased
### Added
- `flamegraph`: new tool that uses the `inferno` crate to generate flamegraph svg files ([GH-73])

## [0.4.0] - 2019-10-24
### Added
- `measureme`: Added RAII-based API for recording events ([GH-70])
Expand Down Expand Up @@ -37,3 +41,4 @@
[GH-59]: https://github.com/rust-lang/measureme/pull/59
[GH-60]: https://github.com/rust-lang/measureme/pull/60
[GH-70]: https://github.com/rust-lang/measureme/pull/70
[GH-73]: https://github.com/rust-lang/measureme/pull/73
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ members = [
"mmview",
"stack_collapse",
"summarize",
"tools_lib",
"flamegraph",
]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ It contains two main modes:

[Learn more](./stack_collapse/README.md)

### flamegraph

`flamegraph` reads `measureme` profiling data and outputs [Flame Graph](https://github.com/brendangregg/FlameGraph).

[Learn more](./flamegraph/README.md)

### crox

`crox` turns `measureme` profiling data into files that can be visualized by the Chromium performance tools.
Expand Down
12 changes: 12 additions & 0 deletions flamegraph/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "flamegraph"
version = "0.1.0"
authors = ["Wesley Wiser <wwiser@gmail.com>", "Michael Woerister <michaelwoerister@posteo>"]
edition = "2018"
license = "MIT OR Apache-2.0"

[dependencies]
measureme = { path = "../measureme" }
tools_lib = { path = "../tools_lib" }
structopt = "0.2"
inferno = { version="0.9.1", default-features = false }
17 changes: 17 additions & 0 deletions flamegraph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# flamegraph

flamegraph is a tool to produce [Flame Graph](https://github.com/brendangregg/FlameGraph) from `measureme` data.

## Example

```bash
$ git clone https://github.com/rust-lang/regex.git

$ cd regex

$ cargo rustc -- -Z self-profile

$ flamegraph pid-{pid}

$ open rustc.svg
```
53 changes: 53 additions & 0 deletions flamegraph/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::error::Error;
use std::fs::File;
use std::io::BufWriter;
use std::path::PathBuf;
use std::time::Duration;

use measureme::ProfilingData;

use structopt::StructOpt;

use tools_lib::stack_collapse::collapse_stacks;

use inferno::flamegraph::{from_lines, Options as FlamegraphOptions};

#[derive(StructOpt, Debug)]
struct Opt {
file_prefix: PathBuf,

/// The sampling interval in milliseconds
#[structopt(short = "i", long = "interval", default_value = "1")]
interval: u64,
}

fn main() -> Result<(), Box<dyn Error>> {
let opt = Opt::from_args();

let profiling_data = ProfilingData::new(&opt.file_prefix)?;

let first_event_time = {
let current_time = profiling_data.iter().next().unwrap().timestamp;
current_time + Duration::from_millis(opt.interval)
};

let recorded_stacks = collapse_stacks(profiling_data.iter(), first_event_time, opt.interval)
.iter()
.map(|(unique_stack, count)| format!("{} {}", unique_stack, count))
.collect::<Vec<_>>();

let file = BufWriter::new(File::create("rustc.svg")?);
let mut flamegraph_options = FlamegraphOptions::default();

from_lines(
&mut flamegraph_options,
recorded_stacks.iter().map(|s| s.as_ref()),
file,
)
.expect(
"unable to generate a flamegraph \
from the collapsed stack data",
);

Ok(())
}
1 change: 1 addition & 0 deletions stack_collapse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ license = "MIT OR Apache-2.0"

[dependencies]
measureme = { path = "../measureme" }
tools_lib = { path = "../tools_lib" }
structopt = "0.2"
4 changes: 1 addition & 3 deletions stack_collapse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use measureme::ProfilingData;

use structopt::StructOpt;

mod stack_collapse;

use stack_collapse::collapse_stacks;
use tools_lib::stack_collapse::collapse_stacks;

#[derive(StructOpt, Debug)]
struct Opt {
Expand Down
9 changes: 9 additions & 0 deletions tools_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "tools_lib"
version = "0.1.0"
authors = ["Wesley Wiser <wwiser@gmail.com>", "Michael Woerister <michaelwoerister@posteo>"]
edition = "2018"
license = "MIT OR Apache-2.0"

[dependencies]
measureme = { path = "../measureme" }
1 change: 1 addition & 0 deletions tools_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod stack_collapse;
File renamed without changes.