blob: 68703b163a63d516cc2b24a2d7dffb79bb7c18b7 [file] [log] [blame]
weidendoa17f2a32006-03-20 10:27:30 +00001#! /usr/bin/perl -w
2##--------------------------------------------------------------------##
3##--- The cache simulation framework: instrumentation, recording ---##
4##--- and results printing. ---##
5##--- callgrind_annotate ---##
6##--------------------------------------------------------------------##
7
8# This file is part of Callgrind, a cache-simulator and call graph
9# tracer built on Valgrind.
10#
11# Copyright (C) 2003 Josef Weidendorfer
12# Josef.Weidendorfer@gmx.de
13#
14# This file is based heavily on vg_annotate, part of Valgrind.
15# Copyright (C) 2002 Nicholas Nethercote
16# njn25@cam.ac.uk
17#
18# This program is free software; you can redistribute it and/or
19# modify it under the terms of the GNU General Public License as
20# published by the Free Software Foundation; either version 2 of the
21# License, or (at your option) any later version.
22#
23# This program is distributed in the hope that it will be useful, but
24# WITHOUT ANY WARRANTY; without even the implied warranty of
25# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26# General Public License for more details.
27#
28# You should have received a copy of the GNU General Public License
29# along with this program; if not, write to the Free Software
30# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
31# 02111-1307, USA.
32#
33# The GNU General Public License is contained in the file COPYING.
34
35#----------------------------------------------------------------------------
36# Annotator for cachegrind/callgrind.
37#
38# File format is described in /docs/techdocs.html.
39#
40# Performance improvements record, using cachegrind.out for cacheprof, doing no
41# source annotation (irrelevant ones removed):
42# user time
43# 1. turned off warnings in add_hash_a_to_b() 3.81 --> 3.48s
44# [now add_array_a_to_b()]
45# 6. make line_to_CC() return a ref instead of a hash 3.01 --> 2.77s
46#
47#10. changed file format to avoid file/fn name repetition 2.40s
48# (not sure why higher; maybe due to new '.' entries?)
49#11. changed file format to drop unnecessary end-line "."s 2.36s
50# (shrunk file by about 37%)
51#12. switched from hash CCs to array CCs 1.61s
52#13. only adding b[i] to a[i] if b[i] defined (was doing it if
53# either a[i] or b[i] was defined, but if b[i] was undefined
54# it just added 0) 1.48s
55#14. Stopped converting "." entries to undef and then back 1.16s
56#15. Using foreach $i (x..y) instead of for ($i = 0...) in
57# add_array_a_to_b() 1.11s
58#
59# Auto-annotating primes:
60#16. Finding count lengths by int((length-1)/3), not by
61# commifying (halves the number of commify calls) 1.68s --> 1.47s
62
63use strict;
64
65#----------------------------------------------------------------------------
66# Overview: the running example in the comments is for:
67# - events = A,B,C,D
68# - --show=C,A,D
69# - --sort=D,C
70#----------------------------------------------------------------------------
71
72#----------------------------------------------------------------------------
73# Global variables, main data structures
74#----------------------------------------------------------------------------
75# CCs are arrays, the counts corresponding to @events, with 'undef'
76# representing '.'. This makes things fast (faster than using hashes for CCs)
77# but we have to use @sort_order and @show_order below to handle the --sort and
78# --show options, which is a bit tricky.
79#----------------------------------------------------------------------------
80
81# Total counts for summary (an array reference).
82my $summary_CC;
83
84# Totals for each function, for overall summary.
85# hash(filename:fn_name => CC array)
86my %fn_totals;
87
88# Individual CCs, organised by filename and line_num for easy annotation.
89# hash(filename => hash(line_num => CC array))
90my %all_ind_CCs;
91
92# Files chosen for annotation on the command line.
93# key = basename (trimmed of any directory), value = full filename
94my %user_ann_files;
95
96# Generic description string.
97my $desc = "";
98
99# Command line of profiled program.
100my $cmd = "";
101
102# Info on the profiled process.
weidendo9e2b7b82006-08-31 19:29:13 +0000103my $creator = "";
weidendoa17f2a32006-03-20 10:27:30 +0000104my $pid = "";
105my $part = "";
106my $thread = "";
107
108# Positions used for cost lines; default: line numbers
109my $has_line = 1;
110my $has_addr = 0;
111
112# Events in input file, eg. (A,B,C,D)
113my @events;
114my $events;
115
116# Events to show, from command line, eg. (C,A,D)
117my @show_events;
118
119# Map from @show_events indices to @events indices, eg. (2,0,3). Gives the
120# order in which we must traverse @events in order to show the @show_events,
121# eg. (@events[$show_order[1]], @events[$show_order[2]]...) = @show_events.
122# (Might help to think of it like a hash (0 => 2, 1 => 0, 2 => 3).)
123my @show_order;
124
125# Print out the function totals sorted by these events, eg. (D,C).
126my @sort_events;
127
128# Map from @sort_events indices to @events indices, eg. (3,2). Same idea as
129# for @show_order.
130my @sort_order;
131
132# Thresholds, one for each sort event (or default to 1 if no sort events
133# specified). We print out functions and do auto-annotations until we've
134# handled this proportion of all the events thresholded.
135my @thresholds;
136
137my $default_threshold = 99;
138
139my $single_threshold = $default_threshold;
140
141# If on, automatically annotates all files that are involved in getting over
142# all the threshold counts.
143my $auto_annotate = 0;
144
145# Number of lines to show around each annotated line.
146my $context = 8;
147
148# Directories in which to look for annotation files.
149my @include_dirs = ("");
150
151# Verbose mode
152my $verbose = "1";
153
154# Inclusive statistics (with subroutine events)
155my $inclusive = 0;
156
157# Inclusive totals for each function, for overall summary.
158# hash(filename:fn_name => CC array)
159my %cfn_totals;
160
161# hash( file:func => [ called file:func ])
162my $called_funcs;
163
164# hash( file:func => [ calling file:func ])
165my $calling_funcs;
166
167# hash( file:func,line => [called file:func ])
168my $called_from_line;
169
170# hash( file:func,line => file:func
171my %func_of_line;
172
173# hash (file:func => object name)
174my %obj_name;
175
176# Print out the callers of a function
177my $tree_caller = 0;
178
179# Print out the called functions
180my $tree_calling = 0;
181
182# hash( file:func,cfile:cfunc => call CC[])
183my %call_CCs;
184
185# hash( file:func,cfile:cfunc => call counter)
186my %call_counter;
187
188# hash(context, index) => realname for compressed traces
189my %compressed;
190
191# Input file name, will be set in process_cmd_line
192my $input_file = "";
193
194# Version number
195my $version = "@VERSION@";
196
197# Usage message.
198my $usage = <<END
199usage: callgrind_annotate [options] [data-file [source-files]]
200
201 options for the user, with defaults in [ ], are:
202 -h --help show this message
203 -v --version show version
204 --show=A,B,C only show figures for events A,B,C [all]
205 --sort=A,B,C sort columns by events A,B,C [event column order]
206 --threshold=<0--100> percentage of counts (of primary sort event) we
207 are interested in [$default_threshold%]
208 --auto=yes|no annotate all source files containing functions
209 that helped reach the event count threshold [no]
210 --context=N print N lines of context before and after
211 annotated lines [8]
212 --inclusive=yes|no add subroutine costs to functions calls [no]
213 --tree=none|caller| print for each function their callers,
214 calling|both the called functions or both [none]
215 -I --include=<dir> add <dir> to list of directories to search for
216 source files
217
218END
219;
220
221# Used in various places of output.
222my $fancy = '-' x 80 . "\n";
223
224#-----------------------------------------------------------------------------
225# Argument and option handling
226#-----------------------------------------------------------------------------
227sub process_cmd_line()
228{
229 for my $arg (@ARGV) {
230
231 # Option handling
232 if ($arg =~ /^-/) {
233
234 # --version
235 if ($arg =~ /^-v$|^--version$/) {
236 die("callgrind_annotate-$version\n");
237
238 # --show=A,B,C
239 } elsif ($arg =~ /^--show=(.*)$/) {
240 @show_events = split(/,/, $1);
241
242 # --sort=A,B,C
243 } elsif ($arg =~ /^--sort=(.*)$/) {
244 @sort_events = split(/,/, $1);
245 foreach my $i (0 .. scalar @sort_events - 1) {
246 if ($sort_events[$i] =~#/.*:(\d+)$/) {
247 /.*:([\d\.]+)%?$/) {
248 my $th = $1;
249 ($th >= 0 && $th <= 100) or die($usage);
250 $sort_events[$i] =~ s/:.*//;
251 $thresholds[$i] = $th;
252 } else {
253 $thresholds[$i] = 0;
254 }
255 }
256
257 # --threshold=X (tolerates a trailing '%')
258 } elsif ($arg =~ /^--threshold=([\d\.]+)%?$/) {
259 $single_threshold = $1;
260 ($1 >= 0 && $1 <= 100) or die($usage);
261
262 # --auto=yes|no
263 } elsif ($arg =~ /^--auto=(yes|no)$/) {
264 $auto_annotate = 1 if ($1 eq "yes");
265 $auto_annotate = 0 if ($1 eq "no");
266
267 # --context=N
268 } elsif ($arg =~ /^--context=([\d\.]+)$/) {
269 $context = $1;
270 if ($context < 0) {
271 die($usage);
272 }
273
274 # --inclusive=yes|no
275 } elsif ($arg =~ /^--inclusive=(yes|no)$/) {
276 $inclusive = 1 if ($1 eq "yes");
277 $inclusive = 0 if ($1 eq "no");
278
279 # --tree=none|caller|calling|both
280 } elsif ($arg =~ /^--tree=(none|caller|calling|both)$/) {
281 $tree_caller = 1 if ($1 eq "caller" || $1 eq "both");
282 $tree_calling = 1 if ($1 eq "calling" || $1 eq "both");
283
284 # --include=A,B,C
285 } elsif ($arg =~ /^(-I|--include)=(.*)$/) {
286 my $inc = $2;
287 $inc =~ s|/$||; # trim trailing '/'
288 push(@include_dirs, "$inc/");
289
290 } else { # -h and --help fall under this case
291 die($usage);
292 }
293
294 # Argument handling -- annotation file checking and selection.
295 # Stick filenames into a hash for quick 'n easy lookup throughout
296 } else {
297 if ($input_file eq "") {
298 $input_file = $arg;
299 }
300 else {
301 my $readable = 0;
302 foreach my $include_dir (@include_dirs) {
303 if (-r $include_dir . $arg) {
304 $readable = 1;
305 }
306 }
307 $readable or die("File $arg not found in any of: @include_dirs\n");
308 $user_ann_files{$arg} = 1;
309 }
310 }
311 }
312
313 if ($input_file eq "") {
weidendo9e2b7b82006-08-31 19:29:13 +0000314 $input_file = (<callgrind.out*>)[0];
weidendoa17f2a32006-03-20 10:27:30 +0000315 if (!defined $input_file) {
weidendo9e2b7b82006-08-31 19:29:13 +0000316 $input_file = (<cachegrind.out*>)[0];
weidendoa17f2a32006-03-20 10:27:30 +0000317 }
weidendo9e2b7b82006-08-31 19:29:13 +0000318
319 (defined $input_file) or die($usage);
weidendoa17f2a32006-03-20 10:27:30 +0000320 print "Reading data from '$input_file'...\n";
321 }
322}
323
324#-----------------------------------------------------------------------------
325# Reading of input file
326#-----------------------------------------------------------------------------
327sub max ($$)
328{
329 my ($x, $y) = @_;
330 return ($x > $y ? $x : $y);
331}
332
333# Add the two arrays; any '.' entries are ignored. Two tricky things:
334# 1. If $a2->[$i] is undefined, it defaults to 0 which is what we want; we turn
335# off warnings to allow this. This makes things about 10% faster than
336# checking for definedness ourselves.
337# 2. We don't add an undefined count or a ".", even though it's value is 0,
338# because we don't want to make an $a2->[$i] that is undef become 0
339# unnecessarily.
340sub add_array_a_to_b ($$)
341{
342 my ($a1, $a2) = @_;
343
344 my $n = max(scalar @$a1, scalar @$a2);
345 $^W = 0;
346 foreach my $i (0 .. $n-1) {
347 $a2->[$i] += $a1->[$i] if (defined $a1->[$i] && "." ne $a1->[$i]);
348 }
349 $^W = 1;
350}
351
352# Add each event count to the CC array. '.' counts become undef, as do
353# missing entries (implicitly).
354sub line_to_CC ($)
355{
356 my @CC = (split /\s+/, $_[0]);
357 (@CC <= @events) or die("Line $.: too many event counts\n");
358 return \@CC;
359}
360
361sub uncompressed_name($$)
362{
363 my ($context, $name) = @_;
364
365 if ($name =~ /^\((\d+)\)\s*(.*)$/) {
366 my $index = $1;
367 my $realname = $2;
368
369 if ($realname eq "") {
370 $realname = $compressed{$context,$index};
371 }
372 else {
373 $compressed{$context,$index} = $realname;
374 }
375 return $realname;
376 }
377 return $name;
378}
379
380sub read_input_file()
381{
382 open(INPUTFILE, "< $input_file") || die "File $input_file not opened\n";
383
384 my $line;
385
386 # Read header
387 while(<INPUTFILE>) {
388
389 # remove comments
390 s/#.*$//;
391
392 if (/^$/) { ; }
393
394 elsif (/^version:\s*(\d+)/) {
395 # Can't read format with major version > 1
396 ($1<2) or die("Can't read format with major version $1.\n");
397 }
398
399 elsif (/^pid:\s+(.*)$/) { $pid = $1; }
400 elsif (/^thread:\s+(.*)$/) { $thread = $1; }
401 elsif (/^part:\s+(.*)$/) { $part = $1; }
402 elsif (/^desc:\s+(.*)$/) {
403 my $dline = $1;
404 # suppress profile options in description output
405 if ($dline =~ /^Option:/) {;}
406 else { $desc .= "$dline\n"; }
407 }
408 elsif (/^cmd:\s+(.*)$/) { $cmd = $1; }
weidendo9e2b7b82006-08-31 19:29:13 +0000409 elsif (/^creator:\s+(.*)$/) { $creator = $1; }
weidendoa17f2a32006-03-20 10:27:30 +0000410 elsif (/^positions:\s+(.*)$/) {
411 my $positions = $1;
412 $has_line = ($positions =~ /line/);
413 $has_addr = ($positions =~ /(addr|instr)/);
414 }
415 elsif (/^events:\s+(.*)$/) {
416 $events = $1;
417
418 # events line is last in header
419 last;
420 }
421 else {
422 warn("WARNING: header line $. malformed, ignoring\n");
423 if ($verbose) { chomp; warn(" line: '$_'\n"); }
424 }
425 }
426
427 # Check for needed header entries
428 ($cmd ne "") or die("Line $.: missing command line\n");
429
430 # Read "events:" line. We make a temporary hash in which the Nth event's
431 # value is N, which is useful for handling --show/--sort options below.
432 ($events ne "") or die("Line $.: missing events line\n");
433 @events = split(/\s+/, $events);
434 my %events;
435 my $n = 0;
436 foreach my $event (@events) {
437 $events{$event} = $n;
438 $n++
439 }
440
441 # If no --show arg give, default to showing all events in the file.
442 # If --show option is used, check all specified events appeared in the
443 # "events:" line. Then initialise @show_order.
444 if (@show_events) {
445 foreach my $show_event (@show_events) {
446 (defined $events{$show_event}) or
447 die("--show event `$show_event' did not appear in input\n");
448 }
449 } else {
450 @show_events = @events;
451 }
452 foreach my $show_event (@show_events) {
453 push(@show_order, $events{$show_event});
454 }
455
456 # Do as for --show, but if no --sort arg given, default to sorting by
457 # column order (ie. first column event is primary sort key, 2nd column is
458 # 2ndary key, etc).
459 if (@sort_events) {
460 foreach my $sort_event (@sort_events) {
461 (defined $events{$sort_event}) or
462 die("--sort event `$sort_event' did not appear in input\n");
463 }
464 } else {
465 @sort_events = @events;
466 }
467 foreach my $sort_event (@sort_events) {
468 push(@sort_order, $events{$sort_event});
469 }
470
471 # If multiple threshold args weren't given via --sort, stick in the single
472 # threshold (either from --threshold if used, or the default otherwise) for
473 # the primary sort event, and 0% for the rest.
474 if (not @thresholds) {
475 foreach my $e (@sort_order) {
476 push(@thresholds, 0);
477 }
478 $thresholds[0] = $single_threshold;
479 }
480
481 my $curr_obj = "";
482 my $curr_file;
483 my $curr_fn;
484 my $curr_name;
485 my $curr_line_num = 0;
weidendo7b43dde2006-08-31 22:54:36 +0000486 my $prev_line_num = 0;
weidendoa17f2a32006-03-20 10:27:30 +0000487
488 my $curr_cobj = "";
489 my $curr_cfile = "";
490 my $curr_cfunc = "";
491 my $curr_cname;
492 my $curr_call_counter = 0;
493 my $curr_cfn_CC = [];
494
495 my $curr_fn_CC = [];
496 my $curr_file_ind_CCs = {}; # hash(line_num => CC)
497
498 # Read body of input file.
499 while (<INPUTFILE>) {
weidendo7b43dde2006-08-31 22:54:36 +0000500 $prev_line_num = $curr_line_num;
501
weidendoa17f2a32006-03-20 10:27:30 +0000502 s/#.*$//; # remove comments
weidendo7b43dde2006-08-31 22:54:36 +0000503 s/^\+(\d+)/$prev_line_num+$1/e;
504 s/^\-(\d+)/$prev_line_num-$1/e;
505 s/^\*/$prev_line_num/e;
506 if (s/^(-?\d+|0x\w+)\s+//) {
weidendoa17f2a32006-03-20 10:27:30 +0000507 $curr_line_num = $1;
508 if ($has_addr) {
509 if ($has_line) {
weidendo7b43dde2006-08-31 22:54:36 +0000510 s/^\+(\d+)/$prev_line_num+$1/e;
511 s/^\-(\d+)/$prev_line_num-$1/e;
512 s/^\*/$prev_line_num/e;
weidendoa17f2a32006-03-20 10:27:30 +0000513
514 if (s/^(\d+)\s+//) { $curr_line_num = $1; }
515 }
516 else { $curr_line_num = 0; }
517 }
518 my $CC = line_to_CC($_);
519
520 if ($curr_call_counter>0) {
521# print "Read ($curr_name => $curr_cname) $curr_call_counter\n";
522
523 if (defined $call_CCs{$curr_name,$curr_cname}) {
524 add_array_a_to_b($CC, $call_CCs{$curr_name,$curr_cname});
525 $call_counter{$curr_name,$curr_cname} += $curr_call_counter;
526 }
527 else {
528 $call_CCs{$curr_name,$curr_cname} = $CC;
529 $call_counter{$curr_name,$curr_cname} = $curr_call_counter;
530 }
531
532 my $tmp = $called_from_line->{$curr_file,$curr_line_num};
533 if (!defined $tmp) {
534 $func_of_line{$curr_file,$curr_line_num} = $curr_name;
535 }
536 $tmp = {} unless defined $tmp;
537 $$tmp{$curr_cname} = 1;
538 $called_from_line->{$curr_file,$curr_line_num} = $tmp;
539 $call_CCs{$curr_name,$curr_cname,$curr_line_num} = $CC;
540 $call_counter{$curr_name,$curr_cname,$curr_line_num} = $curr_call_counter;
541
542 $curr_call_counter = 0;
543
544 # inclusive costs
545 $curr_cfn_CC = $cfn_totals{$curr_cname};
546 $curr_cfn_CC = [] unless (defined $curr_cfn_CC);
547 add_array_a_to_b($CC, $curr_cfn_CC);
548 $cfn_totals{$curr_cname} = $curr_cfn_CC;
549
550 if ($inclusive) {
551 add_array_a_to_b($CC, $curr_fn_CC);
552 }
553 next;
554 }
555
556 add_array_a_to_b($CC, $curr_fn_CC);
557
558 # If curr_file is selected, add CC to curr_file list. We look for
559 # full filename matches; or, if auto-annotating, we have to
560 # remember everything -- we won't know until the end what's needed.
561 if ($auto_annotate || defined $user_ann_files{$curr_file}) {
562 my $tmp = $curr_file_ind_CCs->{$curr_line_num};
563 $tmp = [] unless defined $tmp;
564 add_array_a_to_b($CC, $tmp);
565 $curr_file_ind_CCs->{$curr_line_num} = $tmp;
566 }
567
568 } elsif (s/^fn=(.*)$//) {
569 # Commit result from previous function
570 $fn_totals{$curr_name} = $curr_fn_CC if (defined $curr_name);
571
572 # Setup new one
573 $curr_fn = uncompressed_name("fn",$1);
574 $curr_name = "$curr_file:$curr_fn";
575 $obj_name{$curr_name} = $curr_obj;
576 $curr_fn_CC = $fn_totals{$curr_name};
577 $curr_fn_CC = [] unless (defined $curr_fn_CC);
578
579 } elsif (s/^ob=(.*)$//) {
580 $curr_obj = uncompressed_name("ob",$1);
581
582 } elsif (s/^fl=(.*)$//) {
583 $all_ind_CCs{$curr_file} = $curr_file_ind_CCs
584 if (defined $curr_file);
585
586 $curr_file = uncompressed_name("fl",$1);
587 $curr_file_ind_CCs = $all_ind_CCs{$curr_file};
588 $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs);
589
590 } elsif (s/^(fi|fe)=(.*)$//) {
591 (defined $curr_name) or die("Line $.: Unexpected fi/fe line\n");
592 $fn_totals{$curr_name} = $curr_fn_CC;
593 $all_ind_CCs{$curr_file} = $curr_file_ind_CCs;
594
595 $curr_file = uncompressed_name("fl",$2);
596 $curr_name = "$curr_file:$curr_fn";
597 $curr_file_ind_CCs = $all_ind_CCs{$curr_file};
598 $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs);
599 $curr_fn_CC = $fn_totals{$curr_name};
600 $curr_fn_CC = [] unless (defined $curr_fn_CC);
601
602 } elsif (s/^\s*$//) {
603 # blank, do nothing
604
605 } elsif (s/^cob=(.*)$//) {
606 $curr_cobj = uncompressed_name("ob",$1);
607
608 } elsif (s/^cfi=(.*)$//) {
609 $curr_cfile = uncompressed_name("fl",$1);
610
611 } elsif (s/^cfn=(.*)$//) {
612 $curr_cfunc = uncompressed_name("fn",$1);
613 if ($curr_cfile eq "") {
614 $curr_cname = "$curr_file:$curr_cfunc";
615 }
616 else {
617 $curr_cname = "$curr_cfile:$curr_cfunc";
618 $curr_cfile = "";
619 }
620
621 my $tmp = $calling_funcs->{$curr_cname};
622 $tmp = {} unless defined $tmp;
623 $$tmp{$curr_name} = 1;
624 $calling_funcs->{$curr_cname} = $tmp;
625
626 my $tmp2 = $called_funcs->{$curr_name};
627 $tmp2 = {} unless defined $tmp2;
628 $$tmp2{$curr_cname} = 1;
629 $called_funcs->{$curr_name} = $tmp2;
630
631 } elsif (s/^calls=(\d+)//) {
632 $curr_call_counter = $1;
633
634 } elsif (s/^(jump|jcnd)=//) {
635 #ignore jump information
636
weidendo24ef3452006-09-13 22:57:38 +0000637 } elsif (s/^jfi=(.*)$//) {
638 # side effect needed: possibly add compression mapping
639 uncompressed_name("fl",$1);
640 # ignore jump information
641
642 } elsif (s/^jfn=(.*)$//) {
643 # side effect needed: possibly add compression mapping
644 uncompressed_name("fn",$1);
645 # ignore jump information
646
weidendoa17f2a32006-03-20 10:27:30 +0000647 } elsif (s/^totals:\s+//) {
648 #ignore
649
650 } elsif (s/^summary:\s+//) {
651 $summary_CC = line_to_CC($_);
652
653 } else {
654 warn("WARNING: line $. malformed, ignoring\n");
655 if ($verbose) { chomp; warn(" line: '$_'\n"); }
656 }
657 }
658
659 # Check if summary line was present
660 if (not defined $summary_CC) {
661 warn("WARNING: missing final summary line, no summary will be printed\n");
662 }
663 else {
664 # Finish up handling final filename/fn_name counts
665 $fn_totals{"$curr_file:$curr_fn"} = $curr_fn_CC
666 if (defined $curr_file && defined $curr_fn);
667 $all_ind_CCs{$curr_file} =
668 $curr_file_ind_CCs if (defined $curr_file);
669
670 (scalar(@$summary_CC) == @events)
671 or die("Line $.: summary event and total event mismatch\n");
672 }
673
674 # Correct inclusive totals
675 if ($inclusive) {
676 foreach my $name (keys %cfn_totals) {
677 $fn_totals{$name} = $cfn_totals{$name};
678 }
679 }
680
681 close(INPUTFILE);
682}
683
684#-----------------------------------------------------------------------------
685# Print options used
686#-----------------------------------------------------------------------------
687sub print_options ()
688{
689 print($fancy);
weidendo9e2b7b82006-08-31 19:29:13 +0000690 print "Profile data file '$input_file'";
691 if ($creator ne "") { print " (creator: $creator)"; }
692 print "\n";
693
694 print($fancy);
weidendoa17f2a32006-03-20 10:27:30 +0000695 print($desc);
696 my $target = $cmd;
697 if ($pid ne "") {
698 $target .= " (PID $pid";
699 if ($part ne "") { $target .= ", part $part"; }
700 if ($thread ne "") { $target .= ", thread $thread"; }
701 $target .= ")";
702 }
703 print("Profiled target: $target\n");
704 print("Events recorded: @events\n");
705 print("Events shown: @show_events\n");
706 print("Event sort order: @sort_events\n");
707 print("Thresholds: @thresholds\n");
708
709 my @include_dirs2 = @include_dirs; # copy @include_dirs
710 shift(@include_dirs2); # remove "" entry, which is always the first
711 unshift(@include_dirs2, "") if (0 == @include_dirs2);
712 my $include_dir = shift(@include_dirs2);
713 print("Include dirs: $include_dir\n");
714 foreach my $include_dir (@include_dirs2) {
715 print(" $include_dir\n");
716 }
717
718 my @user_ann_files = keys %user_ann_files;
719 unshift(@user_ann_files, "") if (0 == @user_ann_files);
720 my $user_ann_file = shift(@user_ann_files);
721 print("User annotated: $user_ann_file\n");
722 foreach $user_ann_file (@user_ann_files) {
723 print(" $user_ann_file\n");
724 }
725
726 my $is_on = ($auto_annotate ? "on" : "off");
727 print("Auto-annotation: $is_on\n");
728 print("\n");
729}
730
731#-----------------------------------------------------------------------------
732# Print summary and sorted function totals
733#-----------------------------------------------------------------------------
734sub mycmp ($$)
735{
736 my ($c, $d) = @_;
737
738 # Iterate through sort events (eg. 3,2); return result if two are different
739 foreach my $i (@sort_order) {
740 my ($x, $y);
741 $x = $c->[$i];
742 $y = $d->[$i];
743 $x = -1 unless defined $x;
744 $y = -1 unless defined $y;
745
746 my $cmp = $y <=> $x; # reverse sort
747 if (0 != $cmp) {
748 return $cmp;
749 }
750 }
751 # Exhausted events, equal
752 return 0;
753}
754
755sub commify ($) {
756 my ($val) = @_;
757 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
758 return $val;
759}
760
761# Because the counts can get very big, and we don't want to waste screen space
762# and make lines too long, we compute exactly how wide each column needs to be
763# by finding the widest entry for each one.
764sub compute_CC_col_widths (@)
765{
766 my @CCs = @_;
767 my $CC_col_widths = [];
768
769 # Initialise with minimum widths (from event names)
770 foreach my $event (@events) {
771 push(@$CC_col_widths, length($event));
772 }
773
774 # Find maximum width count for each column. @CC_col_width positions
775 # correspond to @CC positions.
776 foreach my $CC (@CCs) {
777 foreach my $i (0 .. scalar(@$CC)-1) {
778 if (defined $CC->[$i]) {
779 # Find length, accounting for commas that will be added
780 my $length = length $CC->[$i];
781 my $clength = $length + int(($length - 1) / 3);
782 $CC_col_widths->[$i] = max($CC_col_widths->[$i], $clength);
783 }
784 }
785 }
786 return $CC_col_widths;
787}
788
789# Print the CC with each column's size dictated by $CC_col_widths.
790sub print_CC ($$)
791{
792 my ($CC, $CC_col_widths) = @_;
793
794 foreach my $i (@show_order) {
795 my $count = (defined $CC->[$i] ? commify($CC->[$i]) : ".");
796 my $space = ' ' x ($CC_col_widths->[$i] - length($count));
797 print("$space$count ");
798 }
799}
800
801sub print_events ($)
802{
803 my ($CC_col_widths) = @_;
804
805 foreach my $i (@show_order) {
806 my $event = $events[$i];
807 my $event_width = length($event);
808 my $col_width = $CC_col_widths->[$i];
809 my $space = ' ' x ($col_width - $event_width);
810 print("$space$event ");
811 }
812}
813
814# Prints summary and function totals (with separate column widths, so that
815# function names aren't pushed over unnecessarily by huge summary figures).
816# Also returns a hash containing all the files that are involved in getting the
817# events count above the thresholds (ie. all the interesting ones).
818sub print_summary_and_fn_totals ()
819{
820 my @fn_fullnames = keys %fn_totals;
821
822 # Work out the size of each column for printing (summary and functions
823 # separately).
824 my $summary_CC_col_widths = compute_CC_col_widths($summary_CC);
825 my $fn_CC_col_widths = compute_CC_col_widths(values %fn_totals);
826
827 # Header and counts for summary
828 print($fancy);
829 print_events($summary_CC_col_widths);
830 print("\n");
831 print($fancy);
832 print_CC($summary_CC, $summary_CC_col_widths);
833 print(" PROGRAM TOTALS\n");
834 print("\n");
835
836 # Header for functions
837 print($fancy);
838 print_events($fn_CC_col_widths);
839 print(" file:function\n");
840 print($fancy);
841
842 # Sort function names into order dictated by --sort option.
843 @fn_fullnames = sort {
844 mycmp($fn_totals{$a}, $fn_totals{$b})
845 } @fn_fullnames;
846
847
848 # Assertion
849 (scalar @sort_order == scalar @thresholds) or
850 die("sort_order length != thresholds length:\n",
851 " @sort_order\n @thresholds\n");
852
853 my $threshold_files = {};
854 # @curr_totals has the same shape as @sort_order and @thresholds
855 my @curr_totals = ();
856 foreach my $e (@thresholds) {
857 push(@curr_totals, 0);
858 }
859
860 # Print functions, stopping when the threshold has been reached.
861 foreach my $fn_name (@fn_fullnames) {
862
863 # Stop when we've reached all the thresholds
864 my $reached_all_thresholds = 1;
865 foreach my $i (0 .. scalar @thresholds - 1) {
866 my $prop = $curr_totals[$i] * 100;
867 if ($summary_CC->[$sort_order[$i]] >0) {
868 $prop = $prop / $summary_CC->[$sort_order[$i]];
869 }
870 $reached_all_thresholds &= ($prop >= $thresholds[$i]);
871 }
872 last if $reached_all_thresholds;
873
874 if ($tree_caller || $tree_calling) { print "\n"; }
875
876 if ($tree_caller && ($fn_name ne "???:???")) {
877 # Print function callers
878 my $tmp1 = $calling_funcs->{$fn_name};
879 if (defined $tmp1) {
880 foreach my $calling (keys %$tmp1) {
881 if (defined $call_counter{$calling,$fn_name}) {
882 print_CC($call_CCs{$calling,$fn_name}, $fn_CC_col_widths);
883 print" < $calling (";
884 print $call_counter{$calling,$fn_name} . "x)";
885 if (defined $obj_name{$calling}) {
886 print " [$obj_name{$calling}]";
887 }
888 print "\n";
889 }
890 }
891 }
892 }
893
894 # Print function results
895 my $fn_CC = $fn_totals{$fn_name};
896 print_CC($fn_CC, $fn_CC_col_widths);
897 if ($tree_caller || $tree_calling) { print " * "; }
898 print(" $fn_name");
899 if (defined $obj_name{$fn_name}) {
900 print " [$obj_name{$fn_name}]";
901 }
902 print "\n";
903
904 if ($tree_calling && ($fn_name ne "???:???")) {
905 # Print called functions
906 my $tmp2 = $called_funcs->{$fn_name};
907 if (defined $tmp2) {
908 foreach my $called (keys %$tmp2) {
909 if (defined $call_counter{$fn_name,$called}) {
910 print_CC($call_CCs{$fn_name,$called}, $fn_CC_col_widths);
911 print" > $called (";
912 print $call_counter{$fn_name,$called} . "x)";
913 if (defined $obj_name{$called}) {
914 print " [$obj_name{$called}]";
915 }
916 print "\n";
917 }
918 }
919 }
920 }
921
922 # Update the threshold counts
923 my $filename = $fn_name;
924 $filename =~ s/:.+$//; # remove function name
925 $threshold_files->{$filename} = 1;
926 foreach my $i (0 .. scalar @sort_order - 1) {
927 if ($inclusive) {
928 $curr_totals[$i] = $summary_CC->[$sort_order[$i]] -
929 $fn_CC->[$sort_order[$i]]
930 if (defined $fn_CC->[$sort_order[$i]]);
931 } else {
932 $curr_totals[$i] += $fn_CC->[$sort_order[$i]]
933 if (defined $fn_CC->[$sort_order[$i]]);
934 }
935 }
936 }
937 print("\n");
938
939 return $threshold_files;
940}
941
942#-----------------------------------------------------------------------------
943# Annotate selected files
944#-----------------------------------------------------------------------------
945
946# Issue a warning that the source file is more recent than the input file.
947sub warning_on_src_more_recent_than_inputfile ($)
948{
949 my $src_file = $_[0];
950
951 my $warning = <<END
952@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
953@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
954@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
955@ Source file '$src_file' is more recent than input file '$input_file'.
956@ Annotations may not be correct.
957@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
958
959END
960;
961 print($warning);
962}
963
964# If there is information about lines not in the file, issue a warning
965# explaining possible causes.
966sub warning_on_nonexistent_lines ($$$)
967{
968 my ($src_more_recent_than_inputfile, $src_file, $excess_line_nums) = @_;
969 my $cause_and_solution;
970
971 if ($src_more_recent_than_inputfile) {
972 $cause_and_solution = <<END
973@@ cause: '$src_file' has changed since information was gathered.
974@@ If so, a warning will have already been issued about this.
975@@ solution: Recompile program and rerun under "valgrind --cachesim=yes" to
976@@ gather new information.
977END
978 # We suppress warnings about .h files
979 } elsif ($src_file =~ /\.h$/) {
980 $cause_and_solution = <<END
981@@ cause: bug in the Valgrind's debug info reader that screws up with .h
982@@ files sometimes
983@@ solution: none, sorry
984END
985 } else {
986 $cause_and_solution = <<END
987@@ cause: not sure, sorry
988END
989 }
990
991 my $warning = <<END
992@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
993@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
994@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
995@@
996@@ Information recorded about lines past the end of '$src_file'.
997@@
998@@ Probable cause and solution:
999$cause_and_solution@@
1000@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1001END
1002;
1003 print($warning);
1004}
1005
1006sub annotate_ann_files($)
1007{
1008 my ($threshold_files) = @_;
1009
1010 my %all_ann_files;
1011 my @unfound_auto_annotate_files;
1012 my $printed_totals_CC = [];
1013
1014 # If auto-annotating, add interesting files (but not "???")
1015 if ($auto_annotate) {
1016 delete $threshold_files->{"???"};
1017 %all_ann_files = (%user_ann_files, %$threshold_files)
1018 } else {
1019 %all_ann_files = %user_ann_files;
1020 }
1021
1022 # Track if we did any annotations.
1023 my $did_annotations = 0;
1024
1025 LOOP:
1026 foreach my $src_file (keys %all_ann_files) {
1027
1028 my $opened_file = "";
1029 my $full_file_name = "";
1030 foreach my $include_dir (@include_dirs) {
1031 my $try_name = $include_dir . $src_file;
1032 if (open(INPUTFILE, "< $try_name")) {
1033 $opened_file = $try_name;
1034 $full_file_name = ($include_dir eq ""
1035 ? $src_file
1036 : "$include_dir + $src_file");
1037 last;
1038 }
1039 }
1040
1041 if (not $opened_file) {
1042 # Failed to open the file. If chosen on the command line, die.
1043 # If arose from auto-annotation, print a little message.
1044 if (defined $user_ann_files{$src_file}) {
1045 die("File $src_file not opened in any of: @include_dirs\n");
1046
1047 } else {
1048 push(@unfound_auto_annotate_files, $src_file);
1049 }
1050
1051 } else {
1052 # File header (distinguish between user- and auto-selected files).
1053 print("$fancy");
1054 my $ann_type =
1055 (defined $user_ann_files{$src_file} ? "User" : "Auto");
1056 print("-- $ann_type-annotated source: $full_file_name\n");
1057 print("$fancy");
1058
1059 # Get file's CCs
1060 my $src_file_CCs = $all_ind_CCs{$src_file};
1061 if (!defined $src_file_CCs) {
1062 print(" No information has been collected for $src_file\n\n");
1063 next LOOP;
1064 }
1065
1066 $did_annotations = 1;
1067
1068 # Numeric, not lexicographic sort!
1069 my @line_nums = sort {$a <=> $b} keys %$src_file_CCs;
1070
1071 # If $src_file more recent than cachegrind.out, issue warning
1072 my $src_more_recent_than_inputfile = 0;
1073 if ((stat $opened_file)[9] > (stat $input_file)[9]) {
1074 $src_more_recent_than_inputfile = 1;
1075 warning_on_src_more_recent_than_inputfile($src_file);
1076 }
1077
1078 # Work out the size of each column for printing
1079 my $CC_col_widths = compute_CC_col_widths(values %$src_file_CCs);
1080
1081 # Events header
1082 print_events($CC_col_widths);
1083 print("\n\n");
1084
1085 # Shift out 0 if it's in the line numbers (from unknown entries,
1086 # likely due to bugs in Valgrind's stabs debug info reader)
1087 shift(@line_nums) if (0 == $line_nums[0]);
1088
1089 # Finds interesting line ranges -- all lines with a CC, and all
1090 # lines within $context lines of a line with a CC.
1091 my $n = @line_nums;
1092 my @pairs;
1093 for (my $i = 0; $i < $n; $i++) {
1094 push(@pairs, $line_nums[$i] - $context); # lower marker
1095 while ($i < $n-1 &&
1096 $line_nums[$i] + 2*$context >= $line_nums[$i+1]) {
1097 $i++;
1098 }
1099 push(@pairs, $line_nums[$i] + $context); # upper marker
1100 }
1101
1102 # Annotate chosen lines, tracking total counts of lines printed
1103 $pairs[0] = 1 if ($pairs[0] < 1);
1104 while (@pairs) {
1105 my $low = shift @pairs;
1106 my $high = shift @pairs;
1107 while ($. < $low-1) {
1108 my $tmp = <INPUTFILE>;
1109 last unless (defined $tmp); # hack to detect EOF
1110 }
1111 my $src_line;
1112 # Print line number, unless start of file
1113 print("-- line $low " . '-' x 40 . "\n") if ($low != 1);
1114 while (($. < $high) && ($src_line = <INPUTFILE>)) {
1115 if (defined $line_nums[0] && $. == $line_nums[0]) {
1116 print_CC($src_file_CCs->{$.}, $CC_col_widths);
1117 add_array_a_to_b($src_file_CCs->{$.},
1118 $printed_totals_CC);
1119 shift(@line_nums);
1120
1121 } else {
1122 print_CC( [], $CC_col_widths);
1123 }
1124
1125 print(" $src_line");
1126
1127 my $tmp = $called_from_line->{$src_file,$.};
1128 my $func = $func_of_line{$src_file,$.};
1129 if (defined $tmp) {
1130 foreach my $called (keys %$tmp) {
1131 if (defined $call_CCs{$func,$called,$.}) {
1132 print_CC($call_CCs{$func,$called,$.}, $CC_col_widths);
1133 print " => $called (";
1134 print $call_counter{$func,$called,$.} . "x)\n";
1135 }
1136 }
1137 }
1138 }
1139 # Print line number, unless EOF
1140 if ($src_line) {
1141 print("-- line $high " . '-' x 40 . "\n");
1142 } else {
1143 last;
1144 }
1145 }
1146
1147 # If there was info on lines past the end of the file...
1148 if (@line_nums) {
1149 foreach my $line_num (@line_nums) {
1150 print_CC($src_file_CCs->{$line_num}, $CC_col_widths);
1151 print(" <bogus line $line_num>\n");
1152 }
1153 print("\n");
1154 warning_on_nonexistent_lines($src_more_recent_than_inputfile,
1155 $src_file, \@line_nums);
1156 }
1157 print("\n");
1158
1159 # Print summary of counts attributed to file but not to any
1160 # particular line (due to incomplete debug info).
1161 if ($src_file_CCs->{0}) {
1162 print_CC($src_file_CCs->{0}, $CC_col_widths);
1163 print(" <counts for unidentified lines in $src_file>\n\n");
1164 }
1165
1166 close(INPUTFILE);
1167 }
1168 }
1169
1170 # Print list of unfound auto-annotate selected files.
1171 if (@unfound_auto_annotate_files) {
1172 print("$fancy");
1173 print("The following files chosen for auto-annotation could not be found:\n");
1174 print($fancy);
1175 foreach my $f (@unfound_auto_annotate_files) {
1176 print(" $f\n");
1177 }
1178 print("\n");
1179 }
1180
1181 # If we did any annotating, print what proportion of events were covered by
1182 # annotated lines above.
1183 if ($did_annotations) {
1184 my $percent_printed_CC;
1185 foreach (my $i = 0; $i < @$summary_CC; $i++) {
1186 $percent_printed_CC->[$i] =
1187 sprintf("%.0f",
1188 $printed_totals_CC->[$i] / $summary_CC->[$i] * 100);
1189 }
1190 my $pp_CC_col_widths = compute_CC_col_widths($percent_printed_CC);
1191 print($fancy);
1192 print_events($pp_CC_col_widths);
1193 print("\n");
1194 print($fancy);
1195 print_CC($percent_printed_CC, $pp_CC_col_widths);
1196 print(" percentage of events annotated\n\n");
1197 }
1198}
1199
1200#----------------------------------------------------------------------------
1201# "main()"
1202#----------------------------------------------------------------------------
1203process_cmd_line();
1204read_input_file();
1205print_options();
1206my $threshold_files = print_summary_and_fn_totals();
1207annotate_ann_files($threshold_files);
1208
1209##--------------------------------------------------------------------##
1210##--- end vg_annotate.in ---##
1211##--------------------------------------------------------------------##
1212
1213