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

Fix #173 Templated C++ functions cut off after opening parenthesis in template arguments. #174

Merged
merged 4 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
89 changes: 73 additions & 16 deletions src/collapse/perf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,25 +609,59 @@ fn tidy_generic(mut func: String) -> String {
// see https://github.com/brendangregg/FlameGraph/pull/72
// - C++ anonymous namespace annotations.
// see https://github.com/brendangregg/FlameGraph/pull/93
if let Some(first_paren) = func.find('(') {
if func[first_paren..].starts_with("anonymous namespace)") {
// C++ anonymous namespace
} else {
let mut is_go = false;
if let Some(c) = func.get((first_paren - 1)..first_paren) {
// if .get(-1) is None, can't be a dot
if c == "." {
// assume it's a Go method name, so do nothing
is_go = true;
}
let mut angle_bracket_depth = 0;
let mut parentheses_depth = 0;
let mut is_go_function = false;
let mut last_dot_index = Option::<usize>::None;
let mut idx = 0usize;
let mut end_of_param_list = false;
func = func
.chars()
.filter_map(|c| {
if parentheses_depth == 0 {
is_go_function = false;
}

if !is_go {
// kill it with fire!
func.truncate(first_paren);
match c {
'<' => {
angle_bracket_depth += 1;
}
'>' => {
angle_bracket_depth -= 1;
}
'(' => {
// ignore parentheses inside Rust/C++ templates
if angle_bracket_depth == 0 {
if parentheses_depth == 0 && last_dot_index == Some(idx) {
is_go_function = true;
}
parentheses_depth += 1;
}
}
')' => {
if angle_bracket_depth == 0 {
parentheses_depth -= 1;
// Filter everything after the closing parenthesis of the parameter list
// to keep the behavior consistent with the previous implementation
end_of_param_list = !is_go_function && parentheses_depth == 0;
}
}
'.' => {
// insert index + 1 so we can associate it with the opening parentheses for Golang
last_dot_index = Some(idx + 1);
}
_ => (),
};

idx += 1usize;
// Filter chars depending on state
if !is_go_function && (end_of_param_list || parentheses_depth > 0) {
None
} else {
Some(c)
}
}
}
})
.collect();
SoftwareApe marked this conversation as resolved.
Show resolved Hide resolved

// The perl version here strips ' and "; we don't do that.
// see https://github.com/brendangregg/FlameGraph/commit/817c6ea3b92417349605e5715fe6a7cb8cbc9776
Expand Down Expand Up @@ -664,6 +698,28 @@ mod tests {
use crate::collapse::common;
use crate::collapse::Collapse;

// Test some interesting edge cased for tidy_generic
#[test]
fn test_tidy_generic() {
let test_expectations = [
(
"go/build.(*importReader).readByte",
"go/build.(*importReader).readByte",
),
("foo<Vec::<usize>>(Vec<usize>)", "foo<Vec::<usize>>"),
(".run()V", ".run"),
("base(BasicType) const", "base"),
(
"std::function<void (int, int)>::operator(int, int)",
"std::function<void (int, int)>::operator",
),
];

for (input, expected) in test_expectations.iter() {
assert_eq!(&tidy_generic(input.to_string()), expected);
}
}

lazy_static! {
static ref INPUT: Vec<PathBuf> = {
[
Expand All @@ -685,6 +741,7 @@ mod tests {
"./tests/data/collapse-perf/go-stacks.txt",
"./tests/data/collapse-perf/java-inline.txt",
"./tests/data/collapse-perf/weird-stack-line.txt",
"./tests/data/collapse-perf/cpp-stacks-std-function.txt",
]
.iter()
.map(PathBuf::from)
Expand Down
7 changes: 7 additions & 0 deletions tests/collapse-guess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ fn collapse_guess_perf_go_stacks() {
test_collapse_guess(test_file, result_file, true).unwrap()
}

#[test]
fn collapse_guess_perf_cpp_stacks() {
let test_file = "./tests/data/collapse-perf/cpp-stacks-std-function.txt";
let result_file = "./tests/data/collapse-perf/results/cpp-stacks-std-function-collapsed.txt";
test_collapse_guess(test_file, result_file, true).unwrap()
}

#[test]
fn collapse_guess_perf_java_inline() {
let test_file = "./tests/data/collapse-perf/java-inline.txt";
Expand Down
Loading