blob: cb7e17e7a309a59284494516f19a8dcf4f405dc4 [file] [log] [blame]
njn920050d2005-12-21 19:45:29 +00001#! @PERL@
njnc9539842002-10-02 13:26:35 +00002
njn4f9c9342002-04-29 16:03:24 +00003##--------------------------------------------------------------------##
njn920050d2005-12-21 19:45:29 +00004##--- Cachegrind's annotator. cg_annotate.in ---##
njn4f9c9342002-04-29 16:03:24 +00005##--------------------------------------------------------------------##
6
nethercote137bc552003-11-14 17:47:54 +00007# This file is part of Cachegrind, a Valgrind tool for cache
njnc9539842002-10-02 13:26:35 +00008# profiling programs.
njn4f9c9342002-04-29 16:03:24 +00009#
njn53612422005-03-12 16:22:54 +000010# Copyright (C) 2002-2005 Nicholas Nethercote
njn2bc10122005-05-08 02:10:27 +000011# njn@valgrind.org
njn4f9c9342002-04-29 16:03:24 +000012#
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#
njn25e49d8e72002-09-23 09:36:25 +000028# The GNU General Public License is contained in the file COPYING.
njn4f9c9342002-04-29 16:03:24 +000029
30#----------------------------------------------------------------------------
njn920050d2005-12-21 19:45:29 +000031# The file format is simple, basically printing the cost centre for every
njn534f7812006-10-21 22:22:59 +000032# source line, grouped by files and functions. The details are in
33# Cachegrind's manual.
njn920050d2005-12-21 19:45:29 +000034
35#----------------------------------------------------------------------------
njn4f9c9342002-04-29 16:03:24 +000036# Performance improvements record, using cachegrind.out for cacheprof, doing no
37# source annotation (irrelevant ones removed):
38# user time
39# 1. turned off warnings in add_hash_a_to_b() 3.81 --> 3.48s
40# [now add_array_a_to_b()]
41# 6. make line_to_CC() return a ref instead of a hash 3.01 --> 2.77s
42#
43#10. changed file format to avoid file/fn name repetition 2.40s
44# (not sure why higher; maybe due to new '.' entries?)
45#11. changed file format to drop unnecessary end-line "."s 2.36s
46# (shrunk file by about 37%)
47#12. switched from hash CCs to array CCs 1.61s
48#13. only adding b[i] to a[i] if b[i] defined (was doing it if
49# either a[i] or b[i] was defined, but if b[i] was undefined
50# it just added 0) 1.48s
51#14. Stopped converting "." entries to undef and then back 1.16s
52#15. Using foreach $i (x..y) instead of for ($i = 0...) in
53# add_array_a_to_b() 1.11s
54#
55# Auto-annotating primes:
56#16. Finding count lengths by int((length-1)/3), not by
57# commifying (halves the number of commify calls) 1.68s --> 1.47s
58
njn920050d2005-12-21 19:45:29 +000059use warnings;
njn4f9c9342002-04-29 16:03:24 +000060use strict;
61
62#----------------------------------------------------------------------------
63# Overview: the running example in the comments is for:
64# - events = A,B,C,D
65# - --show=C,A,D
66# - --sort=D,C
67#----------------------------------------------------------------------------
68
69#----------------------------------------------------------------------------
70# Global variables, main data structures
71#----------------------------------------------------------------------------
72# CCs are arrays, the counts corresponding to @events, with 'undef'
73# representing '.'. This makes things fast (faster than using hashes for CCs)
74# but we have to use @sort_order and @show_order below to handle the --sort and
75# --show options, which is a bit tricky.
76#----------------------------------------------------------------------------
77
78# Total counts for summary (an array reference).
79my $summary_CC;
80
81# Totals for each function, for overall summary.
82# hash(filename:fn_name => CC array)
83my %fn_totals;
84
85# Individual CCs, organised by filename and line_num for easy annotation.
86# hash(filename => hash(line_num => CC array))
87my %all_ind_CCs;
88
89# Files chosen for annotation on the command line.
90# key = basename (trimmed of any directory), value = full filename
91my %user_ann_files;
92
93# Generic description string.
94my $desc = "";
95
96# Command line of profiled program.
97my $cmd;
98
99# Events in input file, eg. (A,B,C,D)
100my @events;
101
102# Events to show, from command line, eg. (C,A,D)
103my @show_events;
104
105# Map from @show_events indices to @events indices, eg. (2,0,3). Gives the
106# order in which we must traverse @events in order to show the @show_events,
107# eg. (@events[$show_order[1]], @events[$show_order[2]]...) = @show_events.
108# (Might help to think of it like a hash (0 => 2, 1 => 0, 2 => 3).)
109my @show_order;
110
111# Print out the function totals sorted by these events, eg. (D,C).
112my @sort_events;
113
114# Map from @sort_events indices to @events indices, eg. (3,2). Same idea as
njnbff88762002-05-13 20:27:54 +0000115# for @show_order.
njn4f9c9342002-04-29 16:03:24 +0000116my @sort_order;
117
njnbff88762002-05-13 20:27:54 +0000118# Thresholds, one for each sort event (or default to 1 if no sort events
119# specified). We print out functions and do auto-annotations until we've
120# handled this proportion of all the events thresholded.
121my @thresholds;
122
123my $default_threshold = 99;
njn4f9c9342002-04-29 16:03:24 +0000124
njn9b3366a2002-06-10 15:31:16 +0000125my $single_threshold = $default_threshold;
126
njn4f9c9342002-04-29 16:03:24 +0000127# If on, automatically annotates all files that are involved in getting over
njnbff88762002-05-13 20:27:54 +0000128# all the threshold counts.
njn4f9c9342002-04-29 16:03:24 +0000129my $auto_annotate = 0;
130
131# Number of lines to show around each annotated line.
132my $context = 8;
133
134# Directories in which to look for annotation files.
135my @include_dirs = ("");
136
137# Input file name
njn25e49d8e72002-09-23 09:36:25 +0000138my $input_file = undef;
njn4f9c9342002-04-29 16:03:24 +0000139
140# Version number
141my $version = "@VERSION@";
142
143# Usage message.
144my $usage = <<END
sewardj8693e012007-02-08 06:47:19 +0000145usage: cg_annotate [options] [source-files]
njn4f9c9342002-04-29 16:03:24 +0000146
147 options for the user, with defaults in [ ], are:
148 -h --help show this message
149 -v --version show version
sewardj8693e012007-02-08 06:47:19 +0000150 --<pid> read profile data from cachegrind.out.<pid>
151 --cachegrind-out-file=<file> read profile data from <file>
njn4f9c9342002-04-29 16:03:24 +0000152 --show=A,B,C only show figures for events A,B,C [all]
153 --sort=A,B,C sort columns by events A,B,C [event column order]
154 --threshold=<0--100> percentage of counts (of primary sort event) we
njnbff88762002-05-13 20:27:54 +0000155 are interested in [$default_threshold%]
njn4f9c9342002-04-29 16:03:24 +0000156 --auto=yes|no annotate all source files containing functions
157 that helped reach the event count threshold [no]
158 --context=N print N lines of context before and after
159 annotated lines [8]
sewardj45f4e7c2005-09-27 19:20:21 +0000160 -I<d> --include=<d> add <d> to list of directories to search for
njn4f9c9342002-04-29 16:03:24 +0000161 source files
162
sewardj8693e012007-02-08 06:47:19 +0000163 You must use either --<pid> or --cachegrind-out-file exactly once
164 in order that cg_annotate knows what file to read profile data from.
165
166 cg_annotate is Copyright (C) 2002-2007 Nicholas Nethercote.
167 and licensed under the GNU General Public License, version 2.
njnbd774032005-08-04 19:26:54 +0000168 Bug reports, feedback, admiration, abuse, etc, to: njn\@valgrind.org.
njn0e1b5142003-04-15 14:58:06 +0000169
njn4f9c9342002-04-29 16:03:24 +0000170END
171;
172
173# Used in various places of output.
174my $fancy = '-' x 80 . "\n";
175
176#-----------------------------------------------------------------------------
177# Argument and option handling
178#-----------------------------------------------------------------------------
179sub process_cmd_line()
180{
181 for my $arg (@ARGV) {
182
183 # Option handling
184 if ($arg =~ /^-/) {
185
186 # --version
187 if ($arg =~ /^-v$|^--version$/) {
njn4eef8d32002-11-14 16:18:55 +0000188 die("cg_annotate-$version\n");
njn4f9c9342002-04-29 16:03:24 +0000189
190 # --show=A,B,C
191 } elsif ($arg =~ /^--show=(.*)$/) {
192 @show_events = split(/,/, $1);
193
194 # --sort=A,B,C
njn920050d2005-12-21 19:45:29 +0000195 # Nb: You can specify thresholds individually, eg.
196 # --sort=A:99,B:95,C:90. These will override any --threshold
197 # argument.
njn4f9c9342002-04-29 16:03:24 +0000198 } elsif ($arg =~ /^--sort=(.*)$/) {
199 @sort_events = split(/,/, $1);
njn920050d2005-12-21 19:45:29 +0000200 my $th_specified = 0;
njnbff88762002-05-13 20:27:54 +0000201 foreach my $i (0 .. scalar @sort_events - 1) {
njn920050d2005-12-21 19:45:29 +0000202 if ($sort_events[$i] =~ /.*:([\d\.]+)%?$/) {
njnbff88762002-05-13 20:27:54 +0000203 my $th = $1;
204 ($th >= 0 && $th <= 100) or die($usage);
205 $sort_events[$i] =~ s/:.*//;
206 $thresholds[$i] = $th;
njn920050d2005-12-21 19:45:29 +0000207 $th_specified = 1;
njnbff88762002-05-13 20:27:54 +0000208 } else {
209 $thresholds[$i] = 0;
210 }
211 }
njn920050d2005-12-21 19:45:29 +0000212 if (not $th_specified) {
213 @thresholds = ();
214 }
njn4f9c9342002-04-29 16:03:24 +0000215
216 # --threshold=X (tolerates a trailing '%')
217 } elsif ($arg =~ /^--threshold=([\d\.]+)%?$/) {
njn9b3366a2002-06-10 15:31:16 +0000218 $single_threshold = $1;
njnbff88762002-05-13 20:27:54 +0000219 ($1 >= 0 && $1 <= 100) or die($usage);
njn4f9c9342002-04-29 16:03:24 +0000220
221 # --auto=yes|no
sewardj45f4e7c2005-09-27 19:20:21 +0000222 } elsif ($arg =~ /^--auto=yes$/) {
223 $auto_annotate = 1;
224 } elsif ($arg =~ /^--auto=no$/) {
225 $auto_annotate = 0;
njn4f9c9342002-04-29 16:03:24 +0000226
227 # --context=N
228 } elsif ($arg =~ /^--context=([\d\.]+)$/) {
229 $context = $1;
230 if ($context < 0) {
231 die($usage);
232 }
233
sewardj45f4e7c2005-09-27 19:20:21 +0000234 # We don't handle "-I name" -- there can be no space.
235 } elsif ($arg =~ /^-I$/) {
236 die("Sorry, no space is allowed after a -I flag\n");
237
238 # --include=A,B,C. Allow -I=name for backwards compatibility.
239 } elsif ($arg =~ /^(-I=|-I|--include=)(.*)$/) {
njn4f9c9342002-04-29 16:03:24 +0000240 my $inc = $2;
241 $inc =~ s|/$||; # trim trailing '/'
242 push(@include_dirs, "$inc/");
243
sewardj8693e012007-02-08 06:47:19 +0000244 # --cachegrind-out-file=<filename>
245 } elsif ($arg =~ /^--cachegrind-out-file=(.*)$/) {
246 if (not defined $input_file) {
247 $input_file = $1;
248 } else {
249 die("You may only specify one input file\n" .
250 "using the --<pid> and --cachegrind-out-file options.\n");
251 }
252
253 # --<pid>
njn25e49d8e72002-09-23 09:36:25 +0000254 } elsif ($arg =~ /^--(\d+)$/) {
255 my $pid = $1;
256 if (not defined $input_file) {
257 $input_file = "cachegrind.out.$pid";
258 } else {
sewardj8693e012007-02-08 06:47:19 +0000259 die("You may only specify one input file\n" .
260 "using the --<pid> and --cachegrind-out-file options.\n");
njn25e49d8e72002-09-23 09:36:25 +0000261 }
262
njn4f9c9342002-04-29 16:03:24 +0000263 } else { # -h and --help fall under this case
264 die($usage);
265 }
266
267 # Argument handling -- annotation file checking and selection.
njn25e49d8e72002-09-23 09:36:25 +0000268 # Stick filenames into a hash for quick 'n easy lookup throughout.
njn4f9c9342002-04-29 16:03:24 +0000269 } else {
270 my $readable = 0;
271 foreach my $include_dir (@include_dirs) {
272 if (-r $include_dir . $arg) {
273 $readable = 1;
274 }
275 }
276 $readable or die("File $arg not found in any of: @include_dirs\n");
277 $user_ann_files{$arg} = 1;
njn25e49d8e72002-09-23 09:36:25 +0000278 }
279 }
280
281 # Must have chosen an input file
282 if (not defined $input_file) {
283 die($usage);
njn4f9c9342002-04-29 16:03:24 +0000284 }
285}
286
287#-----------------------------------------------------------------------------
288# Reading of input file
289#-----------------------------------------------------------------------------
290sub max ($$)
291{
292 my ($x, $y) = @_;
293 return ($x > $y ? $x : $y);
294}
295
296# Add the two arrays; any '.' entries are ignored. Two tricky things:
297# 1. If $a2->[$i] is undefined, it defaults to 0 which is what we want; we turn
298# off warnings to allow this. This makes things about 10% faster than
299# checking for definedness ourselves.
njnbff88762002-05-13 20:27:54 +0000300# 2. We don't add an undefined count or a ".", even though it's value is 0,
301# because we don't want to make an $a2->[$i] that is undef become 0
302# unnecessarily.
njn4f9c9342002-04-29 16:03:24 +0000303sub add_array_a_to_b ($$)
304{
305 my ($a1, $a2) = @_;
306
307 my $n = max(scalar @$a1, scalar @$a2);
308 $^W = 0;
309 foreach my $i (0 .. $n-1) {
njnbff88762002-05-13 20:27:54 +0000310 $a2->[$i] += $a1->[$i] if (defined $a1->[$i] && "." ne $a1->[$i]);
njn4f9c9342002-04-29 16:03:24 +0000311 }
312 $^W = 1;
313}
314
315# Add each event count to the CC array. '.' counts become undef, as do
316# missing entries (implicitly).
317sub line_to_CC ($)
318{
319 my @CC = (split /\s+/, $_[0]);
320 (@CC <= @events) or die("Line $.: too many event counts\n");
321 return \@CC;
322}
323
324sub read_input_file()
325{
sewardj8693e012007-02-08 06:47:19 +0000326 open(INPUTFILE, "< $input_file")
327 || die "Cannot open $input_file for reading\n";
njn4f9c9342002-04-29 16:03:24 +0000328
329 # Read "desc:" lines.
330 my $line;
njnc68dfbb2003-04-29 11:16:46 +0000331 while ($line = <INPUTFILE>) {
njn4f9c9342002-04-29 16:03:24 +0000332 if ($line =~ s/desc:\s+//) {
333 $desc .= $line;
334 } else {
335 last;
336 }
337 }
338
339 # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above).
njndac0cda2006-10-21 18:22:35 +0000340 ($line =~ s/^cmd:\s+//) or die("Line $.: missing command line\n");
njn4f9c9342002-04-29 16:03:24 +0000341 $cmd = $line;
342 chomp($cmd); # Remove newline
343
344 # Read "events:" line. We make a temporary hash in which the Nth event's
345 # value is N, which is useful for handling --show/--sort options below.
346 $line = <INPUTFILE>;
njndac0cda2006-10-21 18:22:35 +0000347 (defined $line && $line =~ s/^events:\s+//)
njnc68dfbb2003-04-29 11:16:46 +0000348 or die("Line $.: missing events line\n");
njn4f9c9342002-04-29 16:03:24 +0000349 @events = split(/\s+/, $line);
350 my %events;
351 my $n = 0;
352 foreach my $event (@events) {
353 $events{$event} = $n;
354 $n++
355 }
356
357 # If no --show arg give, default to showing all events in the file.
358 # If --show option is used, check all specified events appeared in the
359 # "events:" line. Then initialise @show_order.
360 if (@show_events) {
361 foreach my $show_event (@show_events) {
362 (defined $events{$show_event}) or
363 die("--show event `$show_event' did not appear in input\n");
364 }
365 } else {
366 @show_events = @events;
367 }
368 foreach my $show_event (@show_events) {
369 push(@show_order, $events{$show_event});
370 }
371
372 # Do as for --show, but if no --sort arg given, default to sorting by
373 # column order (ie. first column event is primary sort key, 2nd column is
374 # 2ndary key, etc).
375 if (@sort_events) {
376 foreach my $sort_event (@sort_events) {
377 (defined $events{$sort_event}) or
378 die("--sort event `$sort_event' did not appear in input\n");
379 }
380 } else {
381 @sort_events = @events;
382 }
383 foreach my $sort_event (@sort_events) {
384 push(@sort_order, $events{$sort_event});
385 }
386
njn9b3366a2002-06-10 15:31:16 +0000387 # If multiple threshold args weren't given via --sort, stick in the single
388 # threshold (either from --threshold if used, or the default otherwise) for
389 # the primary sort event, and 0% for the rest.
njnbff88762002-05-13 20:27:54 +0000390 if (not @thresholds) {
391 foreach my $e (@sort_order) {
392 push(@thresholds, 0);
393 }
njn9b3366a2002-06-10 15:31:16 +0000394 $thresholds[0] = $single_threshold;
njnbff88762002-05-13 20:27:54 +0000395 }
396
njn4f9c9342002-04-29 16:03:24 +0000397 my $curr_file;
398 my $curr_fn;
399 my $curr_name;
400
401 my $curr_fn_CC = [];
402 my $curr_file_ind_CCs = {}; # hash(line_num => CC)
403
404 # Read body of input file.
405 while (<INPUTFILE>) {
406 s/#.*$//; # remove comments
407 if (s/^(\d+)\s+//) {
408 my $line_num = $1;
409 my $CC = line_to_CC($_);
410 add_array_a_to_b($CC, $curr_fn_CC);
411
412 # If curr_file is selected, add CC to curr_file list. We look for
413 # full filename matches; or, if auto-annotating, we have to
414 # remember everything -- we won't know until the end what's needed.
415 if ($auto_annotate || defined $user_ann_files{$curr_file}) {
416 my $tmp = $curr_file_ind_CCs->{$line_num};
417 $tmp = [] unless defined $tmp;
418 add_array_a_to_b($CC, $tmp);
419 $curr_file_ind_CCs->{$line_num} = $tmp;
420 }
421
422 } elsif (s/^fn=(.*)$//) {
423 # Commit result from previous function
424 $fn_totals{$curr_name} = $curr_fn_CC if (defined $curr_name);
425
426 # Setup new one
427 $curr_fn = $1;
428 $curr_name = "$curr_file:$curr_fn";
429 $curr_fn_CC = $fn_totals{$curr_name};
430 $curr_fn_CC = [] unless (defined $curr_fn_CC);
431
432 } elsif (s/^fl=(.*)$//) {
433 $all_ind_CCs{$curr_file} = $curr_file_ind_CCs
434 if (defined $curr_file);
435
436 $curr_file = $1;
437 $curr_file_ind_CCs = $all_ind_CCs{$curr_file};
438 $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs);
439
njn4f9c9342002-04-29 16:03:24 +0000440 } elsif (s/^\s*$//) {
441 # blank, do nothing
442
443 } elsif (s/^summary:\s+//) {
444 # Finish up handling final filename/fn_name counts
445 $fn_totals{"$curr_file:$curr_fn"} = $curr_fn_CC
446 if (defined $curr_file && defined $curr_fn);
447 $all_ind_CCs{$curr_file} =
448 $curr_file_ind_CCs if (defined $curr_file);
449
450 $summary_CC = line_to_CC($_);
451 (scalar(@$summary_CC) == @events)
452 or die("Line $.: summary event and total event mismatch\n");
453
454 } else {
455 warn("WARNING: line $. malformed, ignoring\n");
456 }
457 }
458
459 # Check if summary line was present
460 if (not defined $summary_CC) {
njnc68dfbb2003-04-29 11:16:46 +0000461 die("missing final summary line, aborting\n");
njn4f9c9342002-04-29 16:03:24 +0000462 }
463
464 close(INPUTFILE);
465}
466
467#-----------------------------------------------------------------------------
468# Print options used
469#-----------------------------------------------------------------------------
470sub print_options ()
471{
472 print($fancy);
473 print($desc);
474 print("Command: $cmd\n");
njn78cce3f2005-12-22 06:14:42 +0000475 print("Data file: $input_file\n");
njn4f9c9342002-04-29 16:03:24 +0000476 print("Events recorded: @events\n");
477 print("Events shown: @show_events\n");
478 print("Event sort order: @sort_events\n");
njnbff88762002-05-13 20:27:54 +0000479 print("Thresholds: @thresholds\n");
njn4f9c9342002-04-29 16:03:24 +0000480
481 my @include_dirs2 = @include_dirs; # copy @include_dirs
482 shift(@include_dirs2); # remove "" entry, which is always the first
483 unshift(@include_dirs2, "") if (0 == @include_dirs2);
484 my $include_dir = shift(@include_dirs2);
485 print("Include dirs: $include_dir\n");
486 foreach my $include_dir (@include_dirs2) {
487 print(" $include_dir\n");
488 }
489
490 my @user_ann_files = keys %user_ann_files;
491 unshift(@user_ann_files, "") if (0 == @user_ann_files);
492 my $user_ann_file = shift(@user_ann_files);
493 print("User annotated: $user_ann_file\n");
494 foreach $user_ann_file (@user_ann_files) {
495 print(" $user_ann_file\n");
496 }
497
498 my $is_on = ($auto_annotate ? "on" : "off");
499 print("Auto-annotation: $is_on\n");
500 print("\n");
501}
502
503#-----------------------------------------------------------------------------
504# Print summary and sorted function totals
505#-----------------------------------------------------------------------------
506sub mycmp ($$)
507{
508 my ($c, $d) = @_;
509
510 # Iterate through sort events (eg. 3,2); return result if two are different
511 foreach my $i (@sort_order) {
512 my ($x, $y);
513 $x = $c->[$i];
514 $y = $d->[$i];
515 $x = -1 unless defined $x;
516 $y = -1 unless defined $y;
517
518 my $cmp = $y <=> $x; # reverse sort
519 if (0 != $cmp) {
520 return $cmp;
521 }
522 }
523 # Exhausted events, equal
524 return 0;
525}
526
527sub commify ($) {
528 my ($val) = @_;
529 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
530 return $val;
531}
532
533# Because the counts can get very big, and we don't want to waste screen space
534# and make lines too long, we compute exactly how wide each column needs to be
535# by finding the widest entry for each one.
536sub compute_CC_col_widths (@)
537{
538 my @CCs = @_;
539 my $CC_col_widths = [];
540
541 # Initialise with minimum widths (from event names)
542 foreach my $event (@events) {
543 push(@$CC_col_widths, length($event));
544 }
545
546 # Find maximum width count for each column. @CC_col_width positions
547 # correspond to @CC positions.
548 foreach my $CC (@CCs) {
549 foreach my $i (0 .. scalar(@$CC)-1) {
550 if (defined $CC->[$i]) {
551 # Find length, accounting for commas that will be added
552 my $length = length $CC->[$i];
553 my $clength = $length + int(($length - 1) / 3);
554 $CC_col_widths->[$i] = max($CC_col_widths->[$i], $clength);
555 }
556 }
557 }
558 return $CC_col_widths;
559}
560
561# Print the CC with each column's size dictated by $CC_col_widths.
562sub print_CC ($$)
563{
564 my ($CC, $CC_col_widths) = @_;
565
566 foreach my $i (@show_order) {
567 my $count = (defined $CC->[$i] ? commify($CC->[$i]) : ".");
568 my $space = ' ' x ($CC_col_widths->[$i] - length($count));
569 print("$space$count ");
570 }
571}
572
573sub print_events ($)
574{
575 my ($CC_col_widths) = @_;
576
577 foreach my $i (@show_order) {
578 my $event = $events[$i];
579 my $event_width = length($event);
580 my $col_width = $CC_col_widths->[$i];
581 my $space = ' ' x ($col_width - $event_width);
njn602392b2002-04-30 11:34:54 +0000582 print("$space$event ");
njn4f9c9342002-04-29 16:03:24 +0000583 }
584}
585
586# Prints summary and function totals (with separate column widths, so that
587# function names aren't pushed over unnecessarily by huge summary figures).
588# Also returns a hash containing all the files that are involved in getting the
njnbff88762002-05-13 20:27:54 +0000589# events count above the thresholds (ie. all the interesting ones).
njn4f9c9342002-04-29 16:03:24 +0000590sub print_summary_and_fn_totals ()
591{
592 my @fn_fullnames = keys %fn_totals;
593
594 # Work out the size of each column for printing (summary and functions
595 # separately).
596 my $summary_CC_col_widths = compute_CC_col_widths($summary_CC);
597 my $fn_CC_col_widths = compute_CC_col_widths(values %fn_totals);
598
599 # Header and counts for summary
600 print($fancy);
601 print_events($summary_CC_col_widths);
602 print("\n");
603 print($fancy);
604 print_CC($summary_CC, $summary_CC_col_widths);
605 print(" PROGRAM TOTALS\n");
606 print("\n");
607
608 # Header for functions
609 print($fancy);
610 print_events($fn_CC_col_widths);
611 print(" file:function\n");
612 print($fancy);
613
614 # Sort function names into order dictated by --sort option.
615 @fn_fullnames = sort {
616 mycmp($fn_totals{$a}, $fn_totals{$b})
617 } @fn_fullnames;
618
njnbff88762002-05-13 20:27:54 +0000619
620 # Assertion
621 (scalar @sort_order == scalar @thresholds) or
622 die("sort_order length != thresholds length:\n",
623 " @sort_order\n @thresholds\n");
624
njn4f9c9342002-04-29 16:03:24 +0000625 my $threshold_files = {};
njnbff88762002-05-13 20:27:54 +0000626 # @curr_totals has the same shape as @sort_order and @thresholds
627 my @curr_totals = ();
628 foreach my $e (@thresholds) {
629 push(@curr_totals, 0);
630 }
njn4f9c9342002-04-29 16:03:24 +0000631
632 # Print functions, stopping when the threshold has been reached.
633 foreach my $fn_name (@fn_fullnames) {
634
njnbff88762002-05-13 20:27:54 +0000635 # Stop when we've reached all the thresholds
636 my $reached_all_thresholds = 1;
njnb94e77a2002-05-15 14:30:55 +0000637 foreach my $i (0 .. scalar @thresholds - 1) {
njnbff88762002-05-13 20:27:54 +0000638 my $prop = $curr_totals[$i] * 100 / $summary_CC->[$sort_order[$i]];
njn01f192e2003-07-04 15:59:49 +0000639 $reached_all_thresholds &&= ($prop >= $thresholds[$i]);
njnbff88762002-05-13 20:27:54 +0000640 }
641 last if $reached_all_thresholds;
njn4f9c9342002-04-29 16:03:24 +0000642
643 # Print function results
644 my $fn_CC = $fn_totals{$fn_name};
645 print_CC($fn_CC, $fn_CC_col_widths);
646 print(" $fn_name\n");
647
njnbff88762002-05-13 20:27:54 +0000648 # Update the threshold counts
njn4f9c9342002-04-29 16:03:24 +0000649 my $filename = $fn_name;
daywalkerd722c202002-05-01 21:52:05 +0000650 $filename =~ s/:.+$//; # remove function name
njn4f9c9342002-04-29 16:03:24 +0000651 $threshold_files->{$filename} = 1;
njnbff88762002-05-13 20:27:54 +0000652 foreach my $i (0 .. scalar @sort_order - 1) {
653 $curr_totals[$i] += $fn_CC->[$sort_order[$i]]
654 if (defined $fn_CC->[$sort_order[$i]]);
655 }
njn4f9c9342002-04-29 16:03:24 +0000656 }
657 print("\n");
658
659 return $threshold_files;
660}
661
662#-----------------------------------------------------------------------------
663# Annotate selected files
664#-----------------------------------------------------------------------------
665
666# Issue a warning that the source file is more recent than the input file.
667sub warning_on_src_more_recent_than_inputfile ($)
668{
669 my $src_file = $_[0];
670
671 my $warning = <<END
672@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
673@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
674@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
675@ Source file '$src_file' is more recent than input file '$input_file'.
676@ Annotations may not be correct.
677@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
678
679END
680;
681 print($warning);
682}
683
684# If there is information about lines not in the file, issue a warning
685# explaining possible causes.
686sub warning_on_nonexistent_lines ($$$)
687{
688 my ($src_more_recent_than_inputfile, $src_file, $excess_line_nums) = @_;
689 my $cause_and_solution;
690
691 if ($src_more_recent_than_inputfile) {
692 $cause_and_solution = <<END
693@@ cause: '$src_file' has changed since information was gathered.
694@@ If so, a warning will have already been issued about this.
695@@ solution: Recompile program and rerun under "valgrind --cachesim=yes" to
696@@ gather new information.
697END
698 # We suppress warnings about .h files
699 } elsif ($src_file =~ /\.h$/) {
700 $cause_and_solution = <<END
701@@ cause: bug in the Valgrind's debug info reader that screws up with .h
702@@ files sometimes
703@@ solution: none, sorry
704END
705 } else {
706 $cause_and_solution = <<END
707@@ cause: not sure, sorry
708END
709 }
710
711 my $warning = <<END
712@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
713@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
714@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
715@@
716@@ Information recorded about lines past the end of '$src_file'.
717@@
718@@ Probable cause and solution:
719$cause_and_solution@@
720@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
721END
722;
723 print($warning);
724}
725
726sub annotate_ann_files($)
727{
728 my ($threshold_files) = @_;
729
730 my %all_ann_files;
731 my @unfound_auto_annotate_files;
732 my $printed_totals_CC = [];
733
734 # If auto-annotating, add interesting files (but not "???")
735 if ($auto_annotate) {
736 delete $threshold_files->{"???"};
737 %all_ann_files = (%user_ann_files, %$threshold_files)
738 } else {
739 %all_ann_files = %user_ann_files;
740 }
741
742 # Track if we did any annotations.
743 my $did_annotations = 0;
744
745 LOOP:
746 foreach my $src_file (keys %all_ann_files) {
747
748 my $opened_file = "";
749 my $full_file_name = "";
njn61e11d02007-09-17 22:01:14 +0000750 # Nb: include_dirs already includes "", so it works in the case
751 # where the filename has the full path.
752 foreach my $include_dir (@include_dirs) {
njn4f9c9342002-04-29 16:03:24 +0000753 my $try_name = $include_dir . $src_file;
754 if (open(INPUTFILE, "< $try_name")) {
755 $opened_file = $try_name;
756 $full_file_name = ($include_dir eq ""
757 ? $src_file
758 : "$include_dir + $src_file");
759 last;
760 }
761 }
762
763 if (not $opened_file) {
764 # Failed to open the file. If chosen on the command line, die.
765 # If arose from auto-annotation, print a little message.
766 if (defined $user_ann_files{$src_file}) {
767 die("File $src_file not opened in any of: @include_dirs\n");
768
769 } else {
770 push(@unfound_auto_annotate_files, $src_file);
771 }
772
773 } else {
774 # File header (distinguish between user- and auto-selected files).
775 print("$fancy");
776 my $ann_type =
777 (defined $user_ann_files{$src_file} ? "User" : "Auto");
778 print("-- $ann_type-annotated source: $full_file_name\n");
779 print("$fancy");
780
781 # Get file's CCs
782 my $src_file_CCs = $all_ind_CCs{$src_file};
783 if (!defined $src_file_CCs) {
784 print(" No information has been collected for $src_file\n\n");
785 next LOOP;
786 }
787
788 $did_annotations = 1;
789
790 # Numeric, not lexicographic sort!
791 my @line_nums = sort {$a <=> $b} keys %$src_file_CCs;
792
793 # If $src_file more recent than cachegrind.out, issue warning
794 my $src_more_recent_than_inputfile = 0;
795 if ((stat $opened_file)[9] > (stat $input_file)[9]) {
796 $src_more_recent_than_inputfile = 1;
797 warning_on_src_more_recent_than_inputfile($src_file);
798 }
799
800 # Work out the size of each column for printing
801 my $CC_col_widths = compute_CC_col_widths(values %$src_file_CCs);
802
803 # Events header
804 print_events($CC_col_widths);
805 print("\n\n");
806
807 # Shift out 0 if it's in the line numbers (from unknown entries,
808 # likely due to bugs in Valgrind's stabs debug info reader)
809 shift(@line_nums) if (0 == $line_nums[0]);
810
811 # Finds interesting line ranges -- all lines with a CC, and all
812 # lines within $context lines of a line with a CC.
813 my $n = @line_nums;
814 my @pairs;
815 for (my $i = 0; $i < $n; $i++) {
816 push(@pairs, $line_nums[$i] - $context); # lower marker
817 while ($i < $n-1 &&
818 $line_nums[$i] + 2*$context >= $line_nums[$i+1]) {
819 $i++;
820 }
821 push(@pairs, $line_nums[$i] + $context); # upper marker
822 }
823
824 # Annotate chosen lines, tracking total counts of lines printed
825 $pairs[0] = 1 if ($pairs[0] < 1);
826 while (@pairs) {
827 my $low = shift @pairs;
828 my $high = shift @pairs;
829 while ($. < $low-1) {
830 my $tmp = <INPUTFILE>;
831 last unless (defined $tmp); # hack to detect EOF
832 }
833 my $src_line;
834 # Print line number, unless start of file
835 print("-- line $low " . '-' x 40 . "\n") if ($low != 1);
836 while (($. < $high) && ($src_line = <INPUTFILE>)) {
837 if (defined $line_nums[0] && $. == $line_nums[0]) {
838 print_CC($src_file_CCs->{$.}, $CC_col_widths);
839 add_array_a_to_b($src_file_CCs->{$.},
840 $printed_totals_CC);
841 shift(@line_nums);
842
843 } else {
844 print_CC( [], $CC_col_widths);
845 }
846
847 print(" $src_line");
848 }
849 # Print line number, unless EOF
850 if ($src_line) {
851 print("-- line $high " . '-' x 40 . "\n");
852 } else {
853 last;
854 }
855 }
856
857 # If there was info on lines past the end of the file...
858 if (@line_nums) {
859 foreach my $line_num (@line_nums) {
860 print_CC($src_file_CCs->{$line_num}, $CC_col_widths);
861 print(" <bogus line $line_num>\n");
862 }
863 print("\n");
864 warning_on_nonexistent_lines($src_more_recent_than_inputfile,
865 $src_file, \@line_nums);
866 }
867 print("\n");
868
869 # Print summary of counts attributed to file but not to any
870 # particular line (due to incomplete debug info).
871 if ($src_file_CCs->{0}) {
872 print_CC($src_file_CCs->{0}, $CC_col_widths);
873 print(" <counts for unidentified lines in $src_file>\n\n");
874 }
875
876 close(INPUTFILE);
877 }
878 }
879
880 # Print list of unfound auto-annotate selected files.
881 if (@unfound_auto_annotate_files) {
882 print("$fancy");
883 print("The following files chosen for auto-annotation could not be found:\n");
884 print($fancy);
885 foreach my $f (@unfound_auto_annotate_files) {
886 print(" $f\n");
887 }
888 print("\n");
889 }
890
891 # If we did any annotating, print what proportion of events were covered by
892 # annotated lines above.
893 if ($did_annotations) {
894 my $percent_printed_CC;
895 foreach (my $i = 0; $i < @$summary_CC; $i++) {
896 $percent_printed_CC->[$i] =
897 sprintf("%.0f",
898 $printed_totals_CC->[$i] / $summary_CC->[$i] * 100);
899 }
900 my $pp_CC_col_widths = compute_CC_col_widths($percent_printed_CC);
901 print($fancy);
902 print_events($pp_CC_col_widths);
903 print("\n");
904 print($fancy);
905 print_CC($percent_printed_CC, $pp_CC_col_widths);
906 print(" percentage of events annotated\n\n");
907 }
908}
909
910#----------------------------------------------------------------------------
911# "main()"
912#----------------------------------------------------------------------------
913process_cmd_line();
914read_input_file();
915print_options();
916my $threshold_files = print_summary_and_fn_totals();
917annotate_ann_files($threshold_files);
918
njn7cf0bd32002-06-08 13:36:03 +0000919##--------------------------------------------------------------------##
njn4eef8d32002-11-14 16:18:55 +0000920##--- end cg_annotate.in ---##
njn7cf0bd32002-06-08 13:36:03 +0000921##--------------------------------------------------------------------##
922
923