A couple of fixes to perf rollup script
[hiphop-php.git] / hphp / tools / perf-lib.php
blob01c83b30f904a5d7425b0fc203e592511a35231b
1 <?php
2 // Copyright 2004-present Facebook. All Rights Reserved.
4 function starts_with($str, $prefix) {
5 return strncmp($str, $prefix, strlen($prefix)) === 0;
8 # If $func looks like a mangled C++ symbol, attempt to demangle it, stripping
9 # off any trailing junk first.
10 function filter_func(string $func): string {
11 static $cache = Map {};
13 if (strncmp($func, '_Z', 2) === 0) {
14 if (preg_match('/^(.+)\.isra\.\d+$/', $func, $matches) === 1) {
15 $func = $matches[1];
17 if (!isset($cache[$func])) {
18 $cache[$func] = trim(shell_exec("echo '$func' | c++filt"));
20 $func = $cache[$func];
23 return $func;
26 # Read perf samples from the given file stream into a Vector of stack traces.
27 # The stream should contain the output of "perf script -f comm,ip,sym".
28 function read_perf_samples($file, $desired_binary_prefix = 'hhvmworker') {
29 $samples = Vector {};
30 $skip_sample = false;
31 $stack = null;
32 $binary = null;
34 while ($line = fgets($file)) {
35 $line = trim($line);
37 if ($line === '') {
38 if ($stack) {
39 $samples[] = $stack;
40 $stack = null;
42 $skip_sample = false;
43 continue;
45 if ($skip_sample) {
46 continue;
49 if (preg_match('/^[a-f0-9]+ (.+)$/', $line, $matches) === 1) {
50 if (!$stack) $stack = Vector {};
51 $stack[] = filter_func($matches[1]);
52 } else {
53 if ($stack !== null) throw new Exception("Unexpected line $line");
54 $binary = $line;
55 $skip_sample = !starts_with($binary, $desired_binary_prefix);
59 if ($stack) $samples[] = $stack;
60 return $samples;