move getMapIdByValue to FieldMask.h
[hiphop-php.git] / hphp / tools / perf-lib.php
bloba4e526899581c5731ddde9a36402124ae1ba8bf9
1 <?hh
2 // Copyright 2004-present Facebook. All Rights Reserved.
4 function starts_with($str, $prefix) {
5 return strncmp($str, $prefix, strlen($prefix)) === 0;
8 <<__Memoize>>
9 function filter_func_raw(string $func): string {
10 return trim(shell_exec("echo '$func' | c++filt"));
13 // If $func looks like a mangled C++ symbol, attempt to demangle it, stripping
14 // off any trailing junk first.
15 function filter_func(string $func): string {
16 if (strncmp($func, '_Z', 2) === 0) {
17 $matches = null;
18 if (
19 preg_match_with_matches('/^(.+)\.isra\.\d+$/', $func, inout $matches) ===
21 ) {
22 $func = $matches[1];
24 return filter_func_raw($func);
27 return $func;
30 // Read perf samples from the given file stream into a Vector of stack traces.
31 // The stream should contain the output of "perf script -f comm,ip,sym".
32 function read_perf_samples($file, $desired_binary_prefix = 'hhvmworker') {
33 $samples = Vector {};
34 $skip_sample = false;
35 $stack = null;
36 $binary = null;
38 while ($line = fgets($file)) {
39 $line = trim($line);
41 if ($line === '') {
42 if ($stack) {
43 $samples[] = $stack;
44 $stack = null;
46 $skip_sample = false;
47 continue;
49 if ($skip_sample) {
50 continue;
53 $matches = null;
54 if (
55 preg_match_with_matches('/^[a-f0-9]+ (.+)$/', $line, inout $matches) === 1
56 ) {
57 if (!starts_with($matches[0], "ffffffff")) { // skip kernel frames
58 if (!$stack) $stack = Vector {};
59 $stack[] = filter_func($matches[1]);
61 } else {
62 if ($stack !== null) throw new Exception("Unexpected line $line");
63 $binary = $line;
64 $skip_sample = !starts_with($binary, $desired_binary_prefix);
68 if ($stack) $samples[] = $stack;
69 return $samples;