blob: af0ea71356ecb06af294ca94cfb752cdaed567e8 [file] [log] [blame]
weidendoa17f2a32006-03-20 10:27:30 +00001#! /usr/bin/perl -w
2##--------------------------------------------------------------------##
3##--- Control supervision of applications run with callgrind ---##
4##--- callgrind_control ---##
5##--------------------------------------------------------------------##
6
7# This file is part of Callgrind, a cache-simulator and call graph
8# tracer built on Valgrind.
9#
10# Copyright (C) 2003,2004,2005 Josef Weidendorfer
11# Josef.Weidendorfer@gmx.de
12#
13# This program is free software; you can redistribute it and/or
14# modify it under the terms of the GNU General Public License as
15# published by the Free Software Foundation; either version 2 of the
16# License, or (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful, but
19# WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21# General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26# 02111-1307, USA.
27
28sub getCallgrindPids {
29
30 @pids = ();
31 foreach $f (</tmp/callgrind.info.*>) {
32 ($pid) = ($f =~ /info\.(\d+)/);
33 if ($pid eq "") { next; }
34 $mapfile = "/proc/$pid/maps";
35 if (!-e $mapfile) { next; }
36
37 open MAP, "<$mapfile";
38 $found = 0;
39 while(<MAP>) {
weidendo4e28a852006-04-21 00:58:58 +000040 # works both for VG 3.0 and VG 3.1+
weidendoa17f2a32006-03-20 10:27:30 +000041 if (/callgrind/) { $found = 1; }
42 }
43 close MAP;
44 if ($found == 0) { next; }
45
46 open INFO, "<$f";
47 while(<INFO>) {
48 if (/version: (\d+)/) { $mversion{$pid} = $1; }
49 if (/cmd: (.+)$/) { $cmd{$pid} = $1; }
50 if (/control: (.+)$/) { $control{$pid} = $1; }
51 if (/base: (.+)$/) { $base{$pid} = $1; }
52 if (/result: (.+)$/) { $result{$pid} = $1; }
53 }
54 close INFO;
55
56 if ($mversion{$pid} > 1) {
weidendo5b409942006-05-01 01:38:32 +000057 print " PID $pid: Unsupported command interface (version $mversion{$pid}) ?!\n\n";
weidendoa17f2a32006-03-20 10:27:30 +000058 next;
59 }
60
61 push(@pids, $pid);
62 }
63}
64
65sub printHeader {
66 if ($headerPrinted) { return; }
67 $headerPrinted = 1;
68 if ($beQuiet) { return; }
69
70 print "Observe the status and control currently active callgrind runs.\n";
71 print "(C) 2003-2005, Josef Weidendorfer (Josef.Weidendorfer\@gmx.de)\n\n";
72}
73
74sub printVersion {
75 print "callgrind_control-@VERSION@\n";
76 exit;
77}
78
79sub printHelp {
80 printHeader;
81
82 print "Usage: callgrind_control [options] [ <PID>|<Name> ...]\n\n";
83 print "If no PIDs/Names are given, an action is applied to all currently\n";
84 print "active Callgrind runs. Default action is printing short information.\n\n";
85 print "Options:\n";
86 print " -h Print this help text\n";
87 print " -v Print version\n";
88 print " -q Be quiet\n";
89 print " -l Print more information\n";
90 print " -s Print status information\n";
91 print " -b Print backtrace information\n";
92 print " -e [A,..] Print event counters for A,.. [default: all]\n";
93 print " -d [str] Request a profile dump, include <str> as trigger hint\n";
94 print " -z Zero all cost counters\n";
95 print " -k Kill\n";
96 print " -i on/off Switch instrumentation state on/off\n";
97 print " -w <dir> Manually specify the working directory of a callgrind run\n";
98 print "\n";
99 exit;
100}
101
102
103#
104# Parts more or less copied from ct_annotate (author: Nicholas Nethercote)
105#
106
107sub prepareEvents {
108
109 @events = split(/\s+/, $events);
110 %events = ();
111 $n = 0;
112 foreach $event (@events) {
113 $events{$event} = $n;
114 $n++;
115 }
116 if (@show_events) {
117 foreach my $show_event (@show_events) {
118 (defined $events{$show_event}) or
119 print "Warning: Event `$show_event' is not being collected\n";
120 }
121 } else {
122 @show_events = @events;
123 }
124 @show_order = ();
125 foreach my $show_event (@show_events) {
126 push(@show_order, $events{$show_event});
127 }
128}
129
130sub max ($$)
131{
132 my ($x, $y) = @_;
133 return ($x > $y ? $x : $y);
134}
135
136sub line_to_CC ($)
137{
138 my @CC = (split /\s+/, $_[0]);
139 (@CC <= @events) or die("Line $.: too many event counts\n");
140 return \@CC;
141}
142
143sub commify ($) {
144 my ($val) = @_;
145 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
146 return $val;
147}
148
149sub compute_CC_col_widths (@)
150{
151 my @CCs = @_;
152 my $CC_col_widths = [];
153
154 # Initialise with minimum widths (from event names)
155 foreach my $event (@events) {
156 push(@$CC_col_widths, length($event));
157 }
158
159 # Find maximum width count for each column. @CC_col_width positions
160 # correspond to @CC positions.
161 foreach my $CC (@CCs) {
162 foreach my $i (0 .. scalar(@$CC)-1) {
163 if (defined $CC->[$i]) {
164 # Find length, accounting for commas that will be added
165 my $length = length $CC->[$i];
166 my $clength = $length + int(($length - 1) / 3);
167 $CC_col_widths->[$i] = max($CC_col_widths->[$i], $clength);
168 }
169 }
170 }
171 return $CC_col_widths;
172}
173
174# Print the CC with each column's size dictated by $CC_col_widths.
175sub print_CC ($$)
176{
177 my ($CC, $CC_col_widths) = @_;
178
179 foreach my $i (@show_order) {
180 my $count = (defined $CC->[$i] ? commify($CC->[$i]) : ".");
181 my $space = ' ' x ($CC_col_widths->[$i] - length($count));
182 print("$space$count ");
183 }
184}
185
186sub print_events ($)
187{
188 my ($CC_col_widths) = @_;
189
190 foreach my $i (@show_order) {
191 my $event = $events[$i];
192 my $event_width = length($event);
193 my $col_width = $CC_col_widths->[$i];
194 my $space = ' ' x ($col_width - $event_width);
195 print("$space$event ");
196 }
197}
198
199
200
201#
202# Main
203#
204
205getCallgrindPids;
206
207$requestEvents = 0;
208$requestDump = 0;
209$switchInstr = 0;
210$headerPrinted = 0;
211$beQuiet = 0;
212$dumpHint = "";
213$gotW = 0;
214$workingDir = "";
215
216%spids = ();
217foreach $arg (@ARGV) {
218 if ($arg =~ /^-/) {
219 if ($requestDump == 1) { $requestDump = 2; }
220 if ($requestEvents == 1) { $requestEvents = 2; }
221 if ($gotW == 1) { $gotW = 2; }
222
223 if ($arg =~ /^-?-h/) { printHelp; }
224 if ($arg =~ /^-?-v/) { printVersion; }
225 if ($arg =~ /^-q/) { $beQuiet = 1; next; }
226 if ($arg =~ /^-l/) { $printLong = 1; next; }
227 if ($arg =~ /^-s/) { $printStatus = 1; next; }
228 if ($arg =~ /^-b/) { $printBacktrace = 1; next; }
229 if ($arg =~ /^-d/) { $requestDump = 1; next; }
230 if ($arg =~ /^-z/) { $requestZero = 1; next; }
231 if ($arg =~ /^-k/) { $requestKill = 1; next; }
232 if ($arg =~ /^-e/) { $requestEvents = 1; next; }
233 if ($arg =~ /^-i/) { $switchInstr = 1; next; }
234 if ($arg =~ /^-w/) { $gotW = 1; next; }
235
236 printHeader;
237 print "Unknown option '$arg'.\n\n";
238 printHelp;
239 }
240
241 if ($arg =~ /^[A-Za-z_]/) {
242 # arguments of -d/-e/-i are non-numeric
243 if ($requestDump == 1) {
244 $requestDump = 2;
245 $dumpHint = $arg;
246 next;
247 }
248
249 if ($requestEvents == 1) {
250 $requestEvents = 2;
251 @show_events = split(/,/, $arg);
252 next;
253 }
254
255 if ($switchInstr == 1) {
256 $switchInstr = 2;
257 $switchInstrMode = "+";
258 if (($arg eq "off") || ($arg eq "no")) {
259 $switchInstrMode = "-";
260 }
261 next;
262 }
263 }
264
265 if ($gotW == 1) {
266 $gotW = 2;
267 $workingDir = $arg;
268 if (!-d $workingDir) {
269 print "Error: directory '$workingDir' does not exist.\n";
270 printHelp;
271 }
272 next;
273 }
274
275 if (defined $cmd{$arg}) { $spids{$arg} = 1; next; }
276 $nameFound = 0;
277 foreach $p (@pids) {
278 if ($cmd{$p} =~ /^$arg/) {
279 $nameFound = 1;
280 $spids{$p} = 1;
281 }
282 }
283 if ($nameFound) { next; }
284
285 printHeader;
286 print "Non-existent Callgrind task with PID/Name '$arg'.\n\n";
287 printHelp;
288}
289
290if ($workingDir ne "") {
291 # Generate dummy information for dummy pid 0
292 $pid = "0";
weidendo4e28a852006-04-21 00:58:58 +0000293 $mversion{$pid} = "1.0";
weidendoa17f2a32006-03-20 10:27:30 +0000294 $cmd{$pid} = "???";
295 $base{$pid} = $workingDir;
296 $control{$pid} = "$workingDir/callgrind.cmd";
weidendo4e28a852006-04-21 00:58:58 +0000297 $result{$pid} = "$workingDir/callgrind.res";
weidendoa17f2a32006-03-20 10:27:30 +0000298
299 # Only handle this faked callgrind run
300 @pids = ($pid);
301}
302
303if (scalar @pids == 0) {
304 print "No active callgrind runs detected.\n";
305 #print "Detection fails when /proc/*/maps is not readable.\n";
306 print "[Detection can fail on some systems; to work around this,\n";
307 print " specify the working directory of a callgrind run with '-w']\n";
308 exit;
309}
310
311@spids = keys %spids;
312if (scalar @spids >0) { @pids = @spids; }
313
314$command = "";
315$waitForAnswer = 0;
316if ($requestDump) {
317 $command = "Dump";
318 if ($dumpHint ne "") { $command .= " ".$dumpHint; }
319}
320if ($requestZero) { $command = "Zero"; }
321if ($requestKill) { $command = "Kill"; }
322if ($switchInstr) { $command = $switchInstrMode."Instrumentation"; }
323if ($printStatus || $printBacktrace || $requestEvents) {
324 $command = "Status";
325 $waitForAnswer = 1;
326}
327
328foreach $pid (@pids) {
329 $pidstr = "PID $pid: ";
weidendo4e28a852006-04-21 00:58:58 +0000330 if ($pid >0) { print $pidstr.$cmd{$pid}; }
weidendoa17f2a32006-03-20 10:27:30 +0000331
332 if ($command eq "") {
333 if ($printLong) {
334 #print " " x length $pidstr;
335 print " (in $base{$pid})\n";
336 }
337 else {
338 print "\n";
339 }
340 next;
341 }
342 else {
343 if (! (open CONTROL, ">$control{$pid}")) {
344 print " [sending '$command' failed: permission denied]\n";
345 next;
346 }
347 print " [requesting '$command'...]\n";
348 print CONTROL $command;
349 close CONTROL;
350
351 while(-e $control{$pid}) {
352 # sleep for 250 ms
353 select(undef, undef, undef, 0.25);
354 }
355 }
356
weidendo4e28a852006-04-21 00:58:58 +0000357 #print "Reading ".$result{$pid}. "...\n";
weidendoa17f2a32006-03-20 10:27:30 +0000358 if ($result{$pid} eq "") { $waitForAnswer=0; }
359 if (!$waitForAnswer) { print " OK.\n"; next; }
360
361 if (! (open RESULT, "<$result{$pid}")) {
362 print " Warning: Can't open expected result file $result{$pid}.\n";
363 next;
364 }
365
366 @tids = ();
367 $ctid = 0;
368 %fcount = ();
369 %func = ();
370 %calls = ();
371 %events = ();
372 @events = ();
373 %totals = ();
374
375 $exec_bbs = 0;
376 $dist_bbs = 0;
377 $exec_calls = 0;
378 $dist_calls = 0;
379 $dist_ctxs = 0;
380 $dist_funcs = 0;
381 $threads = 0;
382 $events = "";
383
384 while(<RESULT>) {
385 if (/function-(\d+)-(\d+): (.+)$/) {
386 if ($ctid != $1) {
387 $ctid = $1;
388 push(@tids, $ctid);
389 $fcount{$ctid} = 0;
390 }
391 $fcount{$ctid}++;
392 $func{$ctid,$fcount{$ctid}} = $3;
393 }
394 elsif (/calls-(\d+)-(\d+): (.+)$/) {
395 if ($ctid != $1) { next; }
396 $calls{$ctid,$fcount{$ctid}} = $3;
397 }
398 elsif (/events-(\d+)-(\d+): (.+)$/) {
399 if ($ctid != $1) { next; }
400 $events{$ctid,$fcount{$ctid}} = line_to_CC($3);
401 }
402 elsif (/events-(\d+): (.+)$/) {
403 if (scalar @events == 0) { next; }
404 $totals{$1} = line_to_CC($2);
405 }
406 elsif (/executed-bbs: (\d+)/) { $exec_bbs = $1; }
407 elsif (/distinct-bbs: (\d+)/) { $dist_bbs = $1; }
408 elsif (/executed-calls: (\d+)/) { $exec_calls = $1; }
409 elsif (/distinct-calls: (\d+)/) { $dist_calls = $1; }
410 elsif (/distinct-functions: (\d+)/) { $dist_funcs = $1; }
411 elsif (/distinct-contexts: (\d+)/) { $dist_ctxs = $1; }
412 elsif (/events: (.+)$/) { $events = $1; prepareEvents; }
413 elsif (/threads: (\d+)$/) { $threads = $1; }
414 elsif (/instrumentation: (\w+)$/) { $instrumentation = $1; }
415 }
416
417 unlink $result{$pid};
418
419 if ($instrumentation eq "off") {
420 print " No information available as instrumentation is switched off.\n\n";
421 exit;
422 }
423
424 if ($printStatus) {
425 if ($requestEvents <1) {
426 print " Number of threads: $threads\n";
427 print " Events collected: $events\n";
428 }
429
430 print " Functions: ".commify($dist_funcs);
431 print " (executed ".commify($exec_calls);
432 print ", contexts ".commify($dist_ctxs).")\n";
433
434 print " Basic blocks: ".commify($dist_bbs);
435 print " (executed ".commify($exec_bbs);
436 print ", call sites ".commify($dist_calls).")\n";
437 }
438
439 if ($requestEvents >0) {
440 $totals_width = compute_CC_col_widths(values %totals);
441 print "\n Totals:";
442 print_events($totals_width);
443 print("\n");
444 foreach $tid (@tids) {
445 print " Th".substr(" ".$tid,-2)." ";
446 print_CC($totals{$tid}, $totals_width);
447 print("\n");
448 }
449 }
450
451 if ($printBacktrace) {
452
453 if ($requestEvents >0) {
454 $totals_width = compute_CC_col_widths(values %events);
455 }
456
457 foreach $tid (@tids) {
458 print "\n Frame: ";
459 if ($requestEvents >0) {
460 print_events($totals_width);
461 }
462 print "Backtrace for Thread $tid\n";
463
464 $i = $fcount{$tid};
465 $c = 0;
466 while($i>0 && $c<100) {
467 $fc = substr(" $c",-2);
468 print " [$fc] ";
469 if ($requestEvents >0) {
470 print_CC($events{$tid,$i-1}, $totals_width);
471 }
472 print $func{$tid,$i};
473 if ($i > 1) {
474 print " (".$calls{$tid,$i-1}." x)";
475 }
476 print "\n";
477 $i--;
478 $c++;
479 }
480 print "\n";
481 }
482 }
483 print "\n";
484}
485