Skip to content

Commit 4a1fd63

Browse files
authored
Give docs a proper update (#61)
1 parent 65587d8 commit 4a1fd63

9 files changed

Lines changed: 346 additions & 109 deletions

File tree

src/bin/collapse-perf.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::io;
22
use std::path::PathBuf;
33
use structopt::StructOpt;
44

5-
use inferno::collapse::perf::{Options, Perf};
6-
use inferno::collapse::Frontend;
5+
use inferno::collapse::perf::{Folder, Options};
6+
use inferno::collapse::Collapse;
77

88
#[derive(Debug, StructOpt)]
99
#[structopt(
@@ -77,5 +77,5 @@ impl Opt {
7777

7878
fn main() -> io::Result<()> {
7979
let (infile, options) = Opt::from_args().into_parts();
80-
Perf::from_options(options).collapse_file(infile.as_ref())
80+
Folder::from(options).collapse_file(infile.as_ref())
8181
}

src/collapse/mod.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//! Tools for collapsing the output of various profilers (e.g. [perf]) into the
2-
//! format expected by flamegraph.
3-
//!
4-
//! [perf]: https://en.wikipedia.org/wiki/Perf_(Linux)
5-
1+
/// Stack collapsing for the output of [`perf script`](https://linux.die.net/man/1/perf-script).
2+
///
3+
/// See the [crate-level documentation] for details.
4+
///
5+
/// [crate-level documentation]: ../../index.html
66
pub mod perf;
77

88
use std::fs::File;
@@ -11,32 +11,27 @@ use std::path::Path;
1111

1212
const READER_CAPACITY: usize = 128 * 1024;
1313

14-
/// This trait represents the ability to collapse the output of various profilers, such as
15-
/// [perf], into the format expected by flamegraph.
14+
/// The abstract behavior of stack collapsing.
15+
///
16+
/// Implementors of this trait are providing a way to take the stack traces produced by a
17+
/// particular profiler's output (like `perf script`) and produce lines in the folded stack format
18+
/// expected by [`crate::flamegraph::from_sorted_lines`].
19+
///
20+
/// See also the [crate-level documentation] for details.
1621
///
17-
/// [perf]: https://en.wikipedia.org/wiki/Perf_(Linux)
18-
pub trait Frontend {
19-
/// Collapses the contents of the provided `reader` into the format expected by flamegraph.
20-
/// Writes the output to the provided `writer`.
21-
///
22-
/// # Errors
23-
///
24-
/// Return an [`io::Error`] if unsuccessful.
25-
///
26-
/// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html
22+
/// [crate-level documentation]: ../index.html
23+
// https://github.com/rust-lang/rust/issues/45040
24+
// #[doc(spotlight)]
25+
pub trait Collapse {
26+
/// Collapses the contents of the provided `reader` and writes folded stack lines to the
27+
/// provided `writer`.
2728
fn collapse<R, W>(&mut self, reader: R, writer: W) -> io::Result<()>
2829
where
2930
R: io::BufRead,
3031
W: io::Write;
3132

32-
/// Collapses the contents of a file (or of STDIN if `infile` is `None`) into the
33-
/// format expected by flamegraph. Writes the output to STDOUT.
34-
///
35-
/// # Errors
36-
///
37-
/// Return an [`io::Error`] if unsuccessful.
38-
///
39-
/// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html
33+
/// Collapses the contents of a file (or of STDIN if `infile` is `None`) and writes folded
34+
/// stack lines to `STDOUT`.
4035
fn collapse_file<P>(&mut self, infile: Option<P>) -> io::Result<()>
4136
where
4237
P: AsRef<Path>,

src/collapse/perf.rs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,58 @@
1+
use super::Collapse;
12
use smallvec::SmallVec;
23
use std::borrow::Cow;
34
use std::collections::{HashMap, VecDeque};
45
use std::fs::File;
56
use std::io;
67
use std::io::prelude::*;
78

8-
use super::Frontend;
9-
109
const TIDY_GENERIC: bool = true;
1110
const TIDY_JAVA: bool = true;
1211

13-
/// Settings one may pass during the construction of a `Perf` (via its `from_options` method).
12+
/// Settings that change how frames are named from the incoming stack traces.
13+
///
14+
/// All options default to off.
1415
#[derive(Clone, Debug, Default)]
1516
pub struct Options {
16-
/// include PID with process names [1]
17+
/// Include PID in the root frame.
18+
///
19+
/// If disabled, the root frame is given the name of the profiled process.
1720
pub include_pid: bool,
1821

19-
/// include TID and PID with process names [1]
22+
/// Include TID and PID in the root frame.
23+
///
24+
/// Implies `include_pid`.
2025
pub include_tid: bool,
2126

22-
/// include raw addresses where symbols can't be found
27+
/// Include raw addresses (e.g., `0xbfff0836`) where symbols can't be found.
2328
pub include_addrs: bool,
2429

25-
/// annotate jit functions with a _[j]
30+
/// Annotate JIT functions with a `_[j]` suffix.
2631
pub annotate_jit: bool,
2732

28-
/// annotate kernel functions with a _[k]
33+
/// Annotate kernel functions with a `_[k]` suffix.
2934
pub annotate_kernel: bool,
3035

31-
/// un-inline using addr2line
36+
/// Resolve function names (including inlined ones) using `addr2line`.
37+
///
38+
/// When this option is disabled, only the function name observed by `perf` is included, which
39+
/// may not include inlined functions in the call stack.
40+
///
41+
/// When this option is enabled, the function names observed by `perf` are ignored, and each
42+
/// observed function address is instead resolved using
43+
/// [`addr2line`](https://docs.rs/addr2line/). This is slower, but will use binary debug info
44+
/// to resolve even inline call-chains.
3245
pub show_inline: bool,
3346

34-
/// adds source context to inline
47+
/// If enabled, each frame name is also annoted with the file name and line number of the
48+
/// sampled source line.
49+
///
50+
/// Implies `show_inline`.
3551
pub show_context: bool,
3652

37-
/// event type filter, defaults to first encountered event
53+
/// Only consider samples of the given event type (see `perf list`).
54+
///
55+
/// If this option is set to `None`, it will be set to the first encountered event type.
3856
pub event_filter: Option<String>,
3957
}
4058

@@ -51,9 +69,12 @@ impl Default for EventFilterState {
5169
}
5270
}
5371

54-
/// The perf implementation of `Frontend`.
72+
/// A stack collapser for the output of `perf script`.
73+
///
74+
/// To construct one, either use `perf::Folder::default()` or create an [`Options`] and use
75+
/// `perf::Folder::from(options)`.
5576
#[derive(Default)]
56-
pub struct Perf {
77+
pub struct Folder {
5778
/// All lines until the next empty line are stack lines.
5879
in_event: bool,
5980

@@ -79,7 +100,7 @@ pub struct Perf {
79100
opt: Options,
80101
}
81102

82-
impl Frontend for Perf {
103+
impl Collapse for Folder {
83104
fn collapse<R, W>(&mut self, mut reader: R, writer: W) -> io::Result<()>
84105
where
85106
R: BufRead,
@@ -108,9 +129,10 @@ impl Frontend for Perf {
108129
}
109130
}
110131

111-
impl Perf {
112-
/// Constructs a `Perf` with the provided `Options`.
113-
pub fn from_options(opt: Options) -> Self {
132+
impl From<Options> for Folder {
133+
fn from(mut opt: Options) -> Self {
134+
opt.include_pid = opt.include_pid || opt.include_tid;
135+
opt.show_inline = opt.show_inline || opt.show_context;
114136
Self {
115137
in_event: false,
116138
skip_stack: false,
@@ -122,7 +144,9 @@ impl Perf {
122144
opt,
123145
}
124146
}
147+
}
125148

149+
impl Folder {
126150
fn on_line(&mut self, line: &str) {
127151
if !self.in_event {
128152
self.on_event_line(line)

src/flamegraph/attrs.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ macro_rules! unwrap_or_continue {
1818
pub struct FuncFrameAttrsMap(HashMap<String, FrameAttrs>);
1919

2020
impl FuncFrameAttrsMap {
21-
/// Parse a FuncFrameAttrsMap from a file.
22-
/// Each line should be a function name followed by a tab,
23-
/// then a sequence of tab separated name=value pairs.
21+
/// Parse frame attributes from a file.
22+
///
23+
/// Each line should consist of a function name, a tab (`\t`), and then a sequence of
24+
/// tab-separated `name=value` pairs.
2425
pub fn from_file(path: &PathBuf) -> io::Result<FuncFrameAttrsMap> {
2526
let file = BufReader::new(File::open(path)?);
2627
FuncFrameAttrsMap::from_reader(file)
2728
}
2829

29-
/// Parse a FuncFrameAttrsMap from a reader.
30-
/// Each line should be a function name followed by a tab,
31-
/// then a sequence of tab separated name=value pairs.
30+
/// Parse frame attributes from a `BufRead`.
31+
///
32+
/// Each line should consist of a function name, a tab (`\t`), and then a sequence of
33+
/// tab-separated `name=value` pairs.
3234
pub fn from_reader<R: BufRead>(mut reader: R) -> io::Result<FuncFrameAttrsMap> {
3335
let mut funcattr_map = FuncFrameAttrsMap::default();
3436
let mut line = String::new();
@@ -77,56 +79,56 @@ impl FuncFrameAttrsMap {
7779
}
7880

7981
/// Return FrameAttrs for the given function name if it exists
80-
pub fn frameattrs_for_func(&self, func: &str) -> Option<&FrameAttrs> {
82+
pub(super) fn frameattrs_for_func(&self, func: &str) -> Option<&FrameAttrs> {
8183
self.0.get(func)
8284
}
8385
}
8486

8587
/// Attributes to set on the SVG elements of a frame
8688
#[derive(PartialEq, Eq, Debug, Default)]
87-
pub struct FrameAttrs {
89+
pub(super) struct FrameAttrs {
8890
/// The text to include in the `title` element.
8991
/// If set to None, the title is dynamically generated based on the function name.
90-
pub title: Option<String>,
92+
pub(super) title: Option<String>,
9193

92-
pub g: GElementAttrs,
93-
pub a: AElementAttrs,
94+
pub(super) g: GElementAttrs,
95+
pub(super) a: AElementAttrs,
9496
}
9597

9698
/// Attributes to set on the SVG `g` element.
9799
/// Any of them set to `None` will get the default value.
98100
#[derive(PartialEq, Eq, Debug, Default)]
99-
pub struct GElementAttrs {
101+
pub(super) struct GElementAttrs {
100102
/// Defaults to "func_g"
101-
pub class: Option<String>,
103+
pub(super) class: Option<String>,
102104

103105
/// Will not be included if None
104-
pub style: Option<String>,
106+
pub(super) style: Option<String>,
105107

106108
/// Defaults to "s(this)"
107-
pub onmouseover: Option<String>,
109+
pub(super) onmouseover: Option<String>,
108110

109111
/// Defaults to "c()"
110-
pub onmouseout: Option<String>,
112+
pub(super) onmouseout: Option<String>,
111113

112114
/// Defaults to "zoom(this)"
113-
pub onclick: Option<String>,
115+
pub(super) onclick: Option<String>,
114116

115117
/// Extra attributes to include
116-
pub extra: Vec<(String, String)>,
118+
pub(super) extra: Vec<(String, String)>,
117119
}
118120

119121
/// Attributes to set on the SVG `a` element
120122
#[derive(PartialEq, Eq, Debug, Default)]
121-
pub struct AElementAttrs {
123+
pub(super) struct AElementAttrs {
122124
/// If set to None the `a` tag will not be added
123-
pub href: Option<String>,
125+
pub(super) href: Option<String>,
124126

125127
/// Defaults to "_top"
126-
pub target: Option<String>,
128+
pub(super) target: Option<String>,
127129

128130
/// Extra attributes to include
129-
pub extra: Vec<(String, String)>,
131+
pub(super) extra: Vec<(String, String)>,
130132
}
131133

132134
fn parse_extra_attrs(attrs: &mut Vec<(String, String)>, s: &str) {

0 commit comments

Comments
 (0)