Skip to content

Commit

Permalink
Trim reversed stacks to prevent sort check from failing (#338)
Browse files Browse the repository at this point in the history
If the last line of the stack starts with a space, then the space is used in sorting.
However, the space is removed when the line is then fed into the flamegraph, which
makes the sorting invalid. This commit adds a trim to the reverse process to fix this.
  • Loading branch information
bobrik authored Jan 5, 2025
1 parent 306edee commit 41de77c
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ where
}
stack.push(' ');
stack.push_str(&line[samples_idx..]);
// Trim to handle the case where functions names internally contain `;`.
// This can happen, for example, with types like `[u8; 8]` in Rust.
// See https://github.com/jonhoo/inferno/pull/338.
let stack = stack.trim();
reversed.push(&stack);
}
let mut reversed: Vec<&str> = reversed.iter().collect();
Expand Down
167 changes: 167 additions & 0 deletions tests/data/flamegraph/fractional-samples/with-space-reversed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/data/flamegraph/fractional-samples/with-space.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cksum;_start;__libc_start_main;main;cksum 31.23
cksum;cksum 6.1
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.4
cksum;main;core::array::<impl core::default::Default for [T; _]>::default::h67c9877e6f2a615c 19.0
noploop;[unknown] 2.567
noploop;main 274.321
12 changes: 12 additions & 0 deletions tests/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,18 @@ fn flamegraph_reversed_stack_ordering_with_fractional_samples() {
test_flamegraph(input_file, expected_result_file, options).unwrap();
}

#[test]
fn flamegraph_reversed_stack_ordering_with_space() {
let input_file = "./tests/data/flamegraph/fractional-samples/with-space.txt";
let expected_result_file = "./tests/data/flamegraph/fractional-samples/with-space-reversed.svg";

let mut options = flamegraph::Options::default();
options.hash = true;
options.reverse_stack_order = true;

test_flamegraph(input_file, expected_result_file, options).unwrap();
}

#[test]
fn flamegraph_should_warn_about_no_sort_when_reversing_stack_ordering() {
let mut options = flamegraph::Options::default();
Expand Down

0 comments on commit 41de77c

Please sign in to comment.