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

Color diffusion mode #165

Merged
merged 7 commits into from
Mar 18, 2020
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
6 changes: 6 additions & 0 deletions src/bin/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ struct Opt {
)]
colors: Palette,

/// Color frames based on their width, highlighting expensive codepaths
#[structopt(long = "colordiffusion", conflicts_with = "colors")]
color_diffusion: bool,

/// Count type label
#[structopt(
long = "countname",
Expand Down Expand Up @@ -213,6 +217,7 @@ impl<'a> Opt {
options.pretty_xml = self.pretty_xml;
options.no_sort = self.no_sort;
options.no_javascript = self.no_javascript;
options.color_diffusion = self.color_diffusion;
options.reverse_stack_order = self.reverse;

// set style options
Expand Down Expand Up @@ -395,6 +400,7 @@ mod tests {
no_sort: false,
reverse_stack_order: true,
no_javascript: true,
color_diffusion: false,
};

assert_eq!(options, expected_options);
Expand Down
12 changes: 6 additions & 6 deletions src/flamegraph/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,20 +349,20 @@ pub(super) fn color(
pub(super) fn color_scale(value: isize, max: usize) -> Color {
match value.cmp(&0) {
Ordering::Equal => Color {
r: 255,
g: 255,
b: 255,
r: 250,
g: 250,
b: 250,
},
Ordering::Greater => {
// A positive value indicates _more_ samples,
// and hence more time spent, so we give it a red hue.
let c = (210 * (max as isize - value) / max as isize) as u8;
let c = 100 + (150 * (max as isize - value) / max as isize) as u8;
Color { r: 255, g: c, b: c }
}
Ordering::Less => {
// A negative value indicates _fewer_ samples,
// or a speed-up, so we give it a green hue.
let c = (210 * (max as isize + value) / max as isize) as u8;
// or a speed-up, so we give it a blue hue.
let c = 100 + (150 * (max as isize + value) / max as isize) as u8;
Color { r: c, g: c, b: 255 }
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ pub struct Options<'a> {
/// This is only meant to be used in tests.
#[doc(hidden)]
pub no_javascript: bool,

/// Diffusion-based color: the wider the frame, the more red it is. This
/// helps visually draw the eye towards frames that are wider, and therefore
/// more likely to need to be optimized. This is redundant information,
/// insofar as it's the same as the width of frames, but it still provides a
/// useful visual cue of what to focus on, especially if you are showing
/// flamegraphs to someone for the first time.
pub color_diffusion: bool,
}

impl<'a> Options<'a> {
Expand Down Expand Up @@ -278,6 +286,7 @@ impl<'a> Default for Options<'a> {
no_sort: Default::default(),
reverse_stack_order: Default::default(),
no_javascript: Default::default(),
color_diffusion: Default::default(),

#[cfg(feature = "nameattr")]
func_frameattrs: Default::default(),
Expand Down Expand Up @@ -575,6 +584,13 @@ where
color::VDGREY
} else if frame.location.function == "-" {
color::DGREY
} else if opt.color_diffusion {
// We want to visually highlight high priority regions for
// optimization: wider frames are redder. Typically when optimizing,
// a frame that is 50% of width is high priority, so it seems wrong
// to give it half the saturation of 100%. So we use sqrt to make
// the red dropoff less linear.
color::color_scale((((x2_pct - x1_pct) / 100.0).sqrt() * 2000.0) as isize, 2000)
} else if let Some(mut delta) = frame.delta {
if opt.negate_differentials {
delta = -delta;
Expand Down
40 changes: 20 additions & 20 deletions tests/data/flamegraph/differential/diff-negated.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 20 additions & 20 deletions tests/data/flamegraph/differential/diff.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading