stackcollapse-xdebug.php (5691B)
1 #!/usr/bin/php 2 <?php 3 # 4 # Copyright 2018 Miriam Lauter (lauter.miriam@gmail.com). All rights reserved. 5 # 6 # This program is free software; you can redistribute it and/or 7 # modify it under the terms of the GNU General Public License 8 # as published by the Free Software Foundation; either version 2 9 # of the License, or (at your option) any later version. 10 # 11 # This program is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 # GNU General Public License for more details. 15 # 16 # You should have received a copy of the GNU General Public License 17 # along with this program; if not, write to the Free Software Foundation, 18 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 # 20 # (http://www.gnu.org/copyleft/gpl.html) 21 # 22 # 13-Apr-2018 Miriam Lauter Created this. 23 24 ini_set('error_log', null); 25 $optind = null; 26 $args = getopt("htc", ["help"], $optind); 27 if (isset($args['h']) || isset($args['help'])) { 28 usage(); 29 } 30 31 function usage($exit = 0) { 32 echo <<<EOT 33 stackcollapse-xdebug.php collapse php function traces into single lines. 34 35 Parses php samples generated by xdebug with xdebug.trace_format = 1 36 and outputs stacks as single lines, with methods separated by semicolons, 37 and then a space and an occurrence count. For use with flamegraph.pl. 38 See https://github.com/brendangregg/FlameGraph. 39 40 USAGE: ./stackcollapse-xdebug.php [OPTIONS] infile > outfile 41 -h --help Show this message 42 -t Weight stack counts by duration using the time index in the trace (default) 43 -c Invocation counts only. Simply count stacks in the trace and sum duplicates, don't weight by duration. 44 45 Example input: 46 For more info on xdebug and generating traces see 47 https://xdebug.org/docs/execution_trace. 48 49 Version: 2.0.0RC4-dev 50 TRACE START [2007-05-06 18:29:01] 51 1 0 0 0.010870 114112 {main} 1 ../trace.php 0 52 2 1 0 0.032009 114272 str_split 0 ../trace.php 8 53 2 1 1 0.032073 116632 54 2 2 0 0.033505 117424 ret_ord 1 ../trace.php 10 55 3 3 0 0.033531 117584 ord 0 ../trace.php 5 56 3 3 1 0.033551 117584 57 ... 58 TRACE END [2007-05-06 18:29:01] 59 60 Example output: 61 62 - c 63 {main};str_split 1 64 {main};ret_ord;ord 6 65 66 -t 67 {main} 23381 68 {main};str_split 64 69 {main};ret_ord 215 70 {main};ret_ord;ord 106 71 72 EOT; 73 74 exit($exit); 75 } 76 77 function collapseStack(array $stack, string $func_name_key): string { 78 return implode(';', array_column($stack, $func_name_key)); 79 } 80 81 function addCurrentStackToStacks(array $stack, float $dur, array &$stacks) { 82 $collapsed = implode(';', $stack); 83 $duration = SCALE_FACTOR * $dur; 84 85 if (array_key_exists($collapsed, $stacks)) { 86 $stacks[$collapsed] += $duration; 87 } else { 88 $stacks[$collapsed] = $duration; 89 } 90 } 91 92 function isEOTrace(string $l) { 93 $pattern = "/^(\\t|TRACE END)/"; 94 return preg_match($pattern, $l); 95 } 96 97 $filename = $argv[$optind] ?? null; 98 if ($filename === null) { 99 usage(1); 100 } 101 102 $do_time = !isset($args['c']); 103 104 // First make sure our file is consistently formatted with only one \t delimiting each field 105 $out = []; 106 $retval = null; 107 exec("sed -in 's/\t\+/\t/g' " . escapeshellarg($filename), $out, $retval); 108 if ($retval !== 0) { 109 usage(1); 110 } 111 112 $handle = fopen($filename, 'r'); 113 114 if ($handle === false) { 115 echo "Unable to open $filename \n\n"; 116 usage(1); 117 } 118 119 // Loop till we find TRACE START 120 while ($l = fgets($handle)) { 121 if (strpos($l, "TRACE START") === 0) { 122 break; 123 } 124 } 125 126 const SCALE_FACTOR = 1000000; 127 $stacks = []; 128 $current_stack = []; 129 $was_exit = false; 130 $prev_start_time = 0; 131 132 if ($do_time) { 133 // Weight counts by duration 134 // Xdebug trace time indices have 6 sigfigs of precision 135 // We have a perfect trace, but let's instead pretend that 136 // this was collected by sampling at 10^6 Hz 137 // then each millionth of a second this stack took to execute is 1 count 138 while ($l = fgets($handle)) { 139 if (isEOTrace($l)) { 140 break; 141 } 142 143 $parts = explode("\t", $l); 144 list($level, $fn_no, $is_exit, $time) = $parts; 145 146 if ($is_exit) { 147 if (empty($current_stack)) { 148 echo "[WARNING] Found function exit without corresponding entrance. Discarding line. Check your input.\n"; 149 continue; 150 } 151 152 addCurrentStackToStacks($current_stack, $time - $prev_start_time, $stacks); 153 array_pop($current_stack); 154 } else { 155 $func_name = $parts[5]; 156 157 if (!empty($current_stack)) { 158 addCurrentStackToStacks($current_stack, $time - $prev_start_time, $stacks); 159 } 160 161 $current_stack[] = $func_name; 162 } 163 $prev_start_time = $time; 164 } 165 } else { 166 // Counts only 167 while ($l = fgets($handle)) { 168 if (isEOTrace($l)) { 169 break; 170 } 171 172 $parts = explode("\t", $l); 173 list($level, $fn_no, $is_exit) = $parts; 174 175 if ($is_exit === "1") { 176 if (!$was_exit) { 177 $collapsed = implode(";", $current_stack); 178 if (array_key_exists($collapsed, $stacks)) { 179 $stacks[$collapsed]++; 180 } else { 181 $stacks[$collapsed] = 1; 182 } 183 } 184 185 array_pop($current_stack); 186 $was_exit = true; 187 } else { 188 $func_name = $parts[5]; 189 $current_stack[] = $func_name; 190 $was_exit = false; 191 } 192 } 193 } 194 195 foreach ($stacks as $stack => $count) { 196 echo "$stack $count\n"; 197 }