File: pprofp

package info (click to toggle)
php4-apd 0.4p2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,696 kB
  • ctags: 398
  • sloc: sh: 8,306; ansic: 2,188; awk: 70; makefile: 62
file content (350 lines) | stat: -rwxr-xr-x 11,428 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/php4
<?php
require "/usr/share/php/Console/Getopt.php";
error_reporting(0);
$con = new Console_Getopt;
$args = $con->readPHPArgv();
array_shift($args);
$shortoptions = 'acg:hiIlmMrRsStTuUO:vzZ';
$retval = $con->getopt( $args, $shortoptions);
if(is_object($retval)) {
    usage();
}
$opt['O'] = 20;
foreach ($retval[0] as $kv_array) {
    $opt[$kv_array[0]] = $kv_array[1];
}
($dataFile = $retval[1][0]) || usage();
if(($DATA = fopen($dataFile, "r")) == FALSE) {
    print "Failed to open $dataFile for reading\n";
    exit(1);
}

parse_info('HEADER', $DATA, $cfg);
if(!array_key_exists('hz', $cfg)) {
    print "File does not appear to be a pprof trace file\n";
}

$callstack = array();
$calls = array();
$indent_cur = 0;
$file_hash = array();
$mem = array();
$t_rtime = 0;
$t_stime = 0;
$t_utime = 0;
$c_rtimes = array();
$c_stimes = array();
$c_utimes = array();
$rtimes = array();
$stimes = array();
$utimes = array();
$stotal = 0;
$last_memory = 0;

while($line = fgets($DATA)) {
    $line = rtrim($line);
    if(preg_match("/^END_TRACE/", $line)){
        break;
    }
    list($token, $data) = preg_split("/ /",$line, 2);
    if($token == '!') {
	list ($index, $file) = preg_split("/ /", $data, 2);
	$file_hash[$index] = $file;
	continue;
    }
    if( $token == '&') {
        list ($index, $name, $type) = preg_split("/ /", $data, 3);
        $symbol_hash[$index] = $name;
	$symbol_type[$index] = $type;
        continue;
    }
    if( $token == '+') {
        list($index, $file, $line) = preg_split("/ /",$data, 3);
        if(array_key_exists('i',$opt) && $symbol_type[$index] == 1) {
            continue;
        }	
        $index_cur = $index;
        $calls[$index_cur]++;
        array_push($callstack, $index_cur);
        if(array_key_exists('T', $opt)) {
            if(array_key_exists('c', $opt)) {
                printf("%2.02f ", $rtotal/$cfg['hz']);
            }
            print str_repeat("  ", $indent_cur).$symbol_hash[$index_cur]."\n";
	    if(array_key_exists('m', $opt)) {
		print str_repeat("  ", $indent_cur)."C: $file_hash[$file]:$line M: $memory\n";
	    }
	}
        elseif(array_key_exists('t', $opt)) {
            if ( $indent_last == $indent_cur && $index_last == $index_cur ) {
                $repcnt++;
            }
            else {
                if ( $repcnt ) {
                    $repstr = ' ('.++$repcnt.'x)';
                }
                if(array_key_exists('c', $opt)) {
                    printf("%2.02f ", $rtotal/$cfg['hz']);
                }
                print str_repeat("  ", $indent_last).$symbol_hash[$index_last].$repstr."\n";
		if(array_key_exists('m', $opt)) {
		   print str_repeat("  ", $indent_cur)."C: $file_hash[$file_last]:$line_last M: $memory\n";
		}
                $repstr = '';
                $repcnt = 0;
                $index_last = $index_cur;
                $indent_last = $indent_cur;
		$file_last = $file;
		$line_last = $line;
            }
        }
	$indent_cur++;
        continue;
    }
    if( $token == '@') {
        list($t_utime, $t_stime, $t_rtime) = preg_split("/ /", $data);
        continue;
    }
    if ($token == '-') {
        list  ($index, $memory) = preg_split("/ /", $data, 2);
        if(array_key_exists('i',$opt) && $symbol_type[$index] == 1)
        {
            continue;
        }
        $mem[$index] += ($memory - $last_memory);
        $last_memory = $memory;
        $indent_cur--;
        $utimes[$index] += $t_utime;
        $utotal += $t_utime;
        $stimes[$index] += $t_stime;
        $stotal += $t_stime;
        $rtimes[$index] += $t_rtime;
        $rtotal += $t_rtime;
	$seen = array();
        foreach ($callstack as $stack_element) {
            if(array_key_exists($stack_element, $seen)){
                continue;
            }
            $seen[$stack_element] = 1;
            $c_utimes[$stack_element] += $t_utime;
            $c_stimes[$stack_element] += $t_stime;
            $c_rtimes[$stack_element] += $t_rtime;
        }
	$t_utime = 0;
	$t_stime = 0;
	$t_rtime = 0;

        $tmp = array_pop($callstack);
        continue;
    }
}
parse_info('FOOTER', $DATA, $cfg);
$sort = 'by_time';
if(array_key_exists('l', $opt)) { $sort = 'by_calls'; }
if(array_key_exists('m', $opt)) { $sort = 'by_mem'; }
if(array_key_exists('a', $opt)) { $sort = 'by_name'; }
if(array_key_exists('v', $opt)) { $sort = 'by_avgcpu'; }
if(array_key_exists('r', $opt)) { $sort = 'by_rtime'; }
if(array_key_exists('R', $opt)) { $sort = 'by_c_rtime'; }
if(array_key_exists('s', $opt)) { $sort = 'by_stime'; }
if(array_key_exists('S', $opt)) { $sort = 'by_c_stime'; }
if(array_key_exists('u', $opt)) { $sort = 'by_utime'; }
if(array_key_exists('U', $opt)) { $sort = 'by_c_utime'; }
if(array_key_exists('Z', $opt)) { $sort = 'by_c_time'; }

printf("
Trace for %s
Total Elapsed Time = %4.2f
Total System Time  = %4.2f
Total User Time    = %4.2f
", $cfg['caller'], $rtotal/$cfg['hz'], $stotal/$cfg['hz'], $utotal/$cfg['hz']);
print "\n
         Real         User        System             secs/    cumm
%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
--------------------------------------------------------------------------------------\n";
$l = 0;
$itotal = 0;
$percall = 0;
$cpercall = 0;

uksort($symbol_hash, $sort);
foreach (array_keys($symbol_hash) as $j) {
    if(array_key_exists('i', $opt) && $symbol_type[$j] == 1) {
        continue;
    }
    if ($l++ <  $opt['O']) {
        $pcnt = 100*($stimes[$j] + $utimes[$j])/($utotal + $stotal + $itotal);
        $c_pcnt = 100* ($c_stimes[$j] + $c_utimes[$j])/($utotal + $stotal + $itotal);
        $rsecs = $rtimes[$j]/$cfg['hz'];
        $ssecs = $stimes[$j]/$cfg['hz'];
        $usecs = $utimes[$j]/$cfg['hz'];
        $c_rsecs = $c_rtimes[$j]/$cfg['hz'];
        $c_ssecs = $c_stimes[$j]/$cfg['hz'];
        $c_usecs = $c_utimes[$j]/$cfg['hz'];
        $ncalls = $calls[$j];
	if(array_key_exists('z', $opt)) {
        	$percall = ($usecs + $ssecs)/$ncalls;
	        $cpercall = ($c_usecs + $c_ssecs)/$ncalls;
                if($utotal + $stotal) {
		    $pcnt = 100*($stimes[$j] + $utimes[$j])/($utotal + $stotal);
                }
                else {
                    $pcnt = 100;
                }
	}
	if(array_key_exists('Z', $opt)) {
        	$percall = ($usecs + $ssecs)/$ncalls;
	        $cpercall = ($c_usecs + $c_ssecs)/$ncalls;
                if($utotal + $stotal) {
		    $pcnt = 100*($c_stimes[$j] + $c_utimes[$j])/($utotal + $stotal);
                }
                else {
                    $pcnt = 100;
                }
	}
	if(array_key_exists('r', $opt)) {
        	$percall = ($rsecs)/$ncalls;
	        $cpercall = ($c_rsecs)/$ncalls;
                if($rtotal) {
		    $pcnt = 100*$rtimes[$j]/$rtotal;
                }
                else {
                    $pcnt = 100;
                }
	}
	if(array_key_exists('R', $opt)) {
        	$percall = ($rsecs)/$ncalls;
	        $cpercall = ($c_rsecs)/$ncalls;
                if($rtotal) {
		    $pcnt = 100*$c_rtimes[$j]/$rtotal;
                }
                else {
                    $pcnt = 100;
                }
	}
	if(array_key_exists('u', $opt)) {
        	$percall = ($usecs)/$ncalls;
	        $cpercall = ($c_usecs)/$ncalls;
                if($utotal) {
		    $pcnt = 100*$utimes[$j]/$utotal;
                } 
                else {
                    $pcnt = 100;
                }
	}
	if(array_key_exists('U', $opt)) {
        	$percall = ($usecs)/$ncalls;
	        $cpercall = ($c_usecs)/$ncalls;
                if($utotal) {
		    $pcnt = 100*$c_utimes[$j]/$utotal;
                }
                else {
                    $pcnt = 100;
                }
	}
	if(array_key_exists('s', $opt)) {
        	$percall = ($ssecs)/$ncalls;
	        $cpercall = ($c_ssecs)/$ncalls;
                if($stotal) {
		    $pcnt = 100*$stimes[$j]/$stotal;
                }
                else {
                    $pcnt = 100;
                }
	}
	if(array_key_exists('S', $opt)) {
        	$percall = ($ssecs)/$ncalls;
	        $cpercall = ($c_ssecs)/$ncalls;
                if($stotal) {
		    $pcnt = 100*$c_stimes[$j]/$stotal;
                }
                else {
                    $pcnt = 100;
                }
	}
//        $cpercall = ($c_usecs + $c_ssecs)/$ncalls;
        $mem_usage = $mem[$j];
        $name = $symbol_hash[$j];
        printf("%3.01f %2.02f %2.02f  %2.02f %2.02f  %2.02f %2.02f  %4d  %2.04f   %2.04f %12d %s\n", $pcnt, $rsecs, $c_rsecs, $usecs, $c_usecs, $ssecs, $c_ssecs, $ncalls, $percall, $cpercall, $mem_usage, $name);
    }
}


function usage() {
print <<<EOD
pprofp <flags> <trace file>
    Sort options
    -a          Sort by alphabetic names of subroutines.
    -l          Sort by number of calls to subroutines
    -m          Sort by memory used in a function call.
    -r          Sort by real time spent in subroutines.
    -R          Sort by real time spent in subroutines (inclusive of child calls).
    -s          Sort by system time spent in subroutines.
    -S          Sort by system time spent in subroutines (inclusive of child calls).
    -u          Sort by user time spent in subroutines.
    -U          Sort by user time spent in subroutines (inclusive of child calls).
    -v          Sort by average amount of time spent in subroutines.
    -z          Sort by user+system time spent in subroutines. (default)

    Display options
    -c          Display Real time elapsed alongside call tree.
    -i          Suppress reporting for php builtin functions
    -O <cnt>    Specifies maximum number of subroutines to display. (default 15)
    -t          Display compressed call tree.
    -T          Display uncompressed call tree.

EOD;
    exit(1);
}

function parse_info($tag, $datasource, &$cfg) {
    while($line = fgets($datasource)) {
        $line = rtrim($line);
        if(preg_match("/^END_$tag$/", $line)) {
            break;
        }
        if(preg_match("/(\w+)=(.*)/", $line, $matches)) {
            $cfg[$matches[1]] = $matches[2];
        }
    }
}

function num_cmp($a, $b) {
    if (intval($a) > intval($b)) { return 1;}
    elseif(intval($a) < intval($b)) { return -1;}
    else {return 0;}
}

function by_time($a,$b) {
    global $stimes;
    global $utimes;
    return num_cmp(($stimes[$b] + $utimes[$b]),($stimes[$a] + $utimes[$a]));
}

function by_c_time($a,$b) {
    global $c_stimes;
    global $c_utimes;
    return num_cmp(($c_stimes[$b] + $c_utimes[$b]),($c_stimes[$a] + $c_utimes[$a]));
}

function by_avgcpu($a,$b) {
    global $stimes;
    global $utimes;
    global $calls;
    return num_cmp(($stimes[$b] + $utimes[$b])/$calls[$b],($stimes[$a] + $utimes[$a])/$calls[$a]);
}

function by_calls($a, $b) {
    global $calls;
    return num_cmp($calls[$b], $calls[$a]);
}
function by_rtime($a,$b) { global $rtimes; return num_cmp($rtimes[$b], $rtimes[$a]);}
function by_c_rtime($a,$b) { global $c_rtimes; return num_cmp($c_rtimes[$b], $c_rtimes[$a]); }
function by_stime($a,$b) { global $stimes; return num_cmp($stimes[$b], $stimes[$a]); }
function by_c_stime($a,$b) { global $c_stimes; return num_cmp($c_stimes[$b], $c_stimes[$a]); }
function by_utime($a,$b) { global $utimes; return num_cmp($utimes[$b], $utimes[$a]); }
function by_c_utime($a,$b) { global $c_utimes; return num_cmp($c_utimes[$b], $c_utimes[$a]); }
function by_mem($a, $b) { global $mem; return num_cmp($mem[$b], $mem[$a]); }
    
?>