Skip to content

Commit

Permalink
Trim reversed stacks to prevent sort check from failing
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 committed Dec 21, 2024
1 parent 306edee commit 183b090
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ where
}
stack.push(' ');
stack.push_str(&line[samples_idx..]);
let stack = stack.trim();
reversed.push(&stack);
}
let mut reversed: Vec<&str> = reversed.iter().collect();
Expand Down
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; cksum 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/fractional-with-space.txt";
let expected_result_file = "./tests/data/flamegraph/fractional-samples/fractional-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 183b090

Please sign in to comment.