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 support for --reverse option #99

Merged
merged 7 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 7 additions & 2 deletions src/bin/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,12 @@ struct Opt {
/// Don't sort the input lines.
/// If you set this flag you need to be sure your
/// input stack lines are already sorted.
#[structopt(long = "no-sort")]
pub no_sort: bool,
#[structopt(name = "no-sort", long = "no-sort")]
no_sort: bool,

/// Generate stack-reversed flame graph.
#[structopt(long = "reverse", conflicts_with = "no-sort")]
reverse: bool,

/// Don't include static JavaScript in flame graph.
/// This flag is hidden since it's only meant to be used in
Expand Down Expand Up @@ -155,6 +159,7 @@ impl<'a> Opt {
options.pretty_xml = self.pretty_xml;
options.no_sort = self.no_sort;
options.no_javascript = self.no_javascript;
options.reverse_stack_order = self.reverse;

// set style options
options.subtitle = self.subtitle;
Expand Down
69 changes: 47 additions & 22 deletions src/flamegraph/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,58 @@ where

// Parse and remove the number of samples from the end of a line.
fn parse_nsamples(line: &mut &str, stripped_fractional_samples: &mut bool) -> Option<usize> {
let samplesi = line.rfind(' ')?;
let mut samples = &line[(samplesi + 1)..];

// Strip fractional part (if any);
// foobar 1.klwdjlakdj
//
// The Perl version keeps the fractional part but this can be problematic
// because of cumulative floating point errors. Instead we recommend to
// use the --factor option. See https://github.com/brendangregg/FlameGraph/pull/18
if let Some(doti) = samples.find('.') {
if !samples[..doti]
.chars()
.chain(samples[doti + 1..].chars())
.all(|c| c.is_digit(10))
{
return None;
}
if let Some((samplesi, doti)) = rfind_samples(line) {
let mut samples = &line[samplesi..];
// Strip fractional part (if any);
// foobar 1.klwdjlakdj
//
// The Perl version keeps the fractional part but this can be problematic
// because of cumulative floating point errors. Instead we recommend to
// use the --factor option. See https://github.com/brendangregg/FlameGraph/pull/18
//
// Warn if we're stripping a non-zero fractional part, but only the first time.
if !*stripped_fractional_samples && !samples[doti + 1..].chars().all(|c| c == '0') {
if !*stripped_fractional_samples
&& doti < samples.len() - 1
&& !samples[doti + 1..].chars().all(|c| c == '0')
{
*stripped_fractional_samples = true;
warn!("The input data has fractional sample counts that will be truncated to integers. If you need to retain the extra precision you can scale up the sample data and use the --factor option to scale it back down.");
}
samples = &samples[..doti];
let nsamples = samples.parse::<usize>().ok()?;
// remove nsamples part we just parsed from line
*line = line[..samplesi].trim_end();
Some(nsamples)
} else {
None
}
}

let nsamples = samples.parse::<usize>().ok()?;
// remove nsamples part we just parsed from line
*line = line[..samplesi].trim_end();
Some(nsamples)
// Tries to find a sample count at the end of a line.
//
// On success, the first value of the returned tuple will be the index to the sample count.
// If the sample count is fractional, the second value will be the offset of the dot within
// the sample count.
// If the sample count is not fractional, the second value returned is the offset
// to the last digit in the sample count.
//
// If no sample count is found, `None` will be returned.
pub(super) fn rfind_samples(line: &str) -> Option<(usize, usize)> {
let samplesi = line.rfind(' ')? + 1;
let samples = &line[samplesi..];
if let Some(doti) = samples.find('.') {
if samples[..doti]
.chars()
.chain(samples[doti + 1..].chars())
.all(|c| c.is_digit(10))
{
Some((samplesi, doti))
} else {
None
}
} else if !samples.chars().all(|c| c.is_digit(10)) {
None
} else {
Some((samplesi, line.len() - samplesi))
}
}
79 changes: 54 additions & 25 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use quick_xml::{
use std::fs::File;
use std::io::prelude::*;
use std::io::{self, BufReader};
use std::iter;
use std::path::PathBuf;
use str_stack::StrStack;
use svg::StyleOptions;
Expand Down Expand Up @@ -154,6 +155,12 @@ pub struct Options<'a> {
/// make sure the lines are sorted.
pub no_sort: bool,

/// Generate stack-reversed flame graph.
///
/// Note that stack lines must always be sorted after reversing the stacks so the `no_sort`
/// option will be ignored.
pub reverse_stack_order: bool,

/// Don't include static JavaScript in flame graph.
/// This is only meant to be used in tests.
#[doc(hidden)]
Expand Down Expand Up @@ -197,6 +204,7 @@ impl<'a> Default for Options<'a> {
negate_differentials: Default::default(),
pretty_xml: Default::default(),
no_sort: Default::default(),
reverse_stack_order: Default::default(),
no_javascript: Default::default(),
}
}
Expand Down Expand Up @@ -315,7 +323,7 @@ impl Rectangle {
}
}

/// Produce a flame graph from a sorted iterator over folded stack lines.
/// Produce a flame graph from an iterator over folded stack lines.
///
/// This function expects each folded stack to contain the following whitespace-separated fields:
///
Expand All @@ -331,19 +339,55 @@ impl Rectangle {
///
/// [differential flame graph]: http://www.brendangregg.com/blog/2014-11-09/differential-flame-graphs.html
#[allow(clippy::cyclomatic_complexity)]
pub fn from_sorted_lines<'a, I, W>(opt: &mut Options, lines: I, writer: W) -> quick_xml::Result<()>
pub fn from_lines<'a, I, W>(opt: &mut Options, lines: I, writer: W) -> quick_xml::Result<()>
where
I: IntoIterator<Item = &'a str>,
W: Write,
{
let (bgcolor1, bgcolor2) = color::bgcolor_for(opt.bgcolors, opt.colors);
let mut reversed = StrStack::new();
let (mut frames, time, ignored, delta_max) = if opt.reverse_stack_order {
if opt.no_sort {
warn!("Input lines are always sorted when `reverse_stack_order` is `true`. The `no_sort` option is being ignored.");
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
}
// Reverse order of stacks and sort.
let mut stack = String::new();
for line in lines {
stack.clear();
let samples_idx = merge::rfind_samples(line)
.map(|(i, _)| i)
.unwrap_or_else(|| line.len());
let samples_idx = merge::rfind_samples(&line[..samples_idx - 1])
.map(|(i, _)| i)
.unwrap_or(samples_idx);
for (i, func) in line[..samples_idx].trim().split(';').rev().enumerate() {
if i != 0 {
stack.push(';');
}
stack.push_str(func);
}
stack.push(' ');
stack.push_str(&line[samples_idx..]);
reversed.push(&stack);
}
let mut reversed: Vec<&str> = reversed.iter().collect();
reversed.sort_unstable();
merge::frames(reversed)?
} else if opt.no_sort {
// Lines don't need sorting.
merge::frames(lines)?
} else {
// Sort lines by default.
let mut lines: Vec<&str> = lines.into_iter().collect();
lines.sort_unstable();
merge::frames(lines)?
};

let mut buffer = StrStack::new();
let (mut frames, time, ignored, delta_max) = merge::frames(lines)?;
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
if ignored != 0 {
warn!("Ignored {} lines with invalid format", ignored);
}

let mut buffer = StrStack::new();

// let's start writing the svg!
let mut svg = if opt.pretty_xml {
Writer::new_with_indent(writer, b' ', 4)
Expand Down Expand Up @@ -397,6 +441,7 @@ where
let imageheight = ((depthmax + 1) * opt.frame_height) + opt.ypad1() + opt.ypad2();
svg::write_header(&mut svg, imageheight, &opt)?;

let (bgcolor1, bgcolor2) = color::bgcolor_for(opt.bgcolors, opt.colors);
let style_options = StyleOptions {
imageheight,
bgcolor1,
Expand Down Expand Up @@ -614,33 +659,21 @@ where
Ok(())
}

/// Produce a flame graph from a reader that contains a sorted sequence of folded stack lines.
/// Produce a flame graph from a reader that contains a sequence of folded stack lines.
///
/// See [`from_sorted_lines`] for the expected format of each line.
///
/// The resulting flame graph will be written out to `writer` in SVG format.
pub fn from_reader<R, W>(opt: &mut Options, mut reader: R, writer: W) -> quick_xml::Result<()>
pub fn from_reader<R, W>(opt: &mut Options, reader: R, writer: W) -> quick_xml::Result<()>
where
R: Read,
W: Write,
{
let mut input = String::new();
reader
.read_to_string(&mut input)
.map_err(quick_xml::Error::Io)?;

if opt.no_sort {
from_sorted_lines(opt, input.lines(), writer)
} else {
let mut lines: Vec<&str> = input.lines().collect();
lines.sort_unstable();
from_sorted_lines(opt, lines, writer)
}
from_readers(opt, iter::once(reader), writer)
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
}

/// Produce a flame graph from a set of readers that contain folded stack lines.
///
/// This function sorts all the read lines before processing them.
/// See [`from_sorted_lines`] for the expected format of each line.
///
/// The resulting flame graph will be written out to `writer` in SVG format.
Expand All @@ -656,11 +689,7 @@ where
.read_to_string(&mut input)
.map_err(quick_xml::Error::Io)?;
}

let mut lines: Vec<&str> = input.lines().collect();
lines.sort_unstable();

from_sorted_lines(opt, lines, writer)
from_lines(opt, input.lines(), writer)
}

/// Produce a flame graph from files that contain folded stack lines
Expand Down
1 change: 1 addition & 0 deletions tests/data/flamegraph/bad-lines/bad-lines.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ cksum;cksum 6
THIS IS A BAD LINE
cksum;cksum;__GI___fread_unlocked;_IO_file_xsgetn;_IO_file_read;entry_SYSCALL_64_fastpath_[k];sys_read_[k];vfs_read_[k];__vfs_read_[k];ext4_file_read_iter_[k] 1
cksum;main;cksum 19
THIS IS A BAD FRACTIONAL LINE 12V.43
noploop;[unknown] 2
noploop;main 274
Loading