Skip to content

Commit 41de77c

Browse files
authored
Trim reversed stacks to prevent sort check from failing (#338)
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.
1 parent 306edee commit 41de77c

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

src/flamegraph/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ where
429429
}
430430
stack.push(' ');
431431
stack.push_str(&line[samples_idx..]);
432+
// Trim to handle the case where functions names internally contain `;`.
433+
// This can happen, for example, with types like `[u8; 8]` in Rust.
434+
// See https://github.com/jonhoo/inferno/pull/338.
435+
let stack = stack.trim();
432436
reversed.push(&stack);
433437
}
434438
let mut reversed: Vec<&str> = reversed.iter().collect();
Lines changed: 167 additions & 0 deletions
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cksum;_start;__libc_start_main;main;cksum 31.23
2+
cksum;cksum 6.1
3+
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
4+
cksum;main;core::array::<impl core::default::Default for [T; _]>::default::h67c9877e6f2a615c 19.0
5+
noploop;[unknown] 2.567
6+
noploop;main 274.321

tests/flamegraph.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,18 @@ fn flamegraph_reversed_stack_ordering_with_fractional_samples() {
806806
test_flamegraph(input_file, expected_result_file, options).unwrap();
807807
}
808808

809+
#[test]
810+
fn flamegraph_reversed_stack_ordering_with_space() {
811+
let input_file = "./tests/data/flamegraph/fractional-samples/with-space.txt";
812+
let expected_result_file = "./tests/data/flamegraph/fractional-samples/with-space-reversed.svg";
813+
814+
let mut options = flamegraph::Options::default();
815+
options.hash = true;
816+
options.reverse_stack_order = true;
817+
818+
test_flamegraph(input_file, expected_result_file, options).unwrap();
819+
}
820+
809821
#[test]
810822
fn flamegraph_should_warn_about_no_sort_when_reversing_stack_ordering() {
811823
let mut options = flamegraph::Options::default();

0 commit comments

Comments
 (0)