blob: 36c2b9f23eb32cc6eb2d34b11f375fdbc3ba78e4 [file] [log] [blame]
sewardj2f685952002-12-22 19:32:23 +00001#! @PERL@ -w
njnc9539842002-10-02 13:26:35 +00002
njn4f9c9342002-04-29 16:03:24 +00003##--------------------------------------------------------------------##
4##--- The cache simulation framework: instrumentation, recording ---##
5##--- and results printing. ---##
njn4eef8d32002-11-14 16:18:55 +00006##--- cg_annotate.in ---##
njn4f9c9342002-04-29 16:03:24 +00007##--------------------------------------------------------------------##
8
njnc9539842002-10-02 13:26:35 +00009# This file is part of Cachegrind, a Valgrind skin for cache
10# profiling programs.
njn4f9c9342002-04-29 16:03:24 +000011#
njn0e1b5142003-04-15 14:58:06 +000012# Copyright (C) 2002-2003 Nicholas Nethercote
sewardj3c23d432002-06-01 23:43:49 +000013# njn25@cam.ac.uk
njn4f9c9342002-04-29 16:03:24 +000014#
15# This program is free software; you can redistribute it and/or
16# modify it under the terms of the GNU General Public License as
17# published by the Free Software Foundation; either version 2 of the
18# License, or (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful, but
21# WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23# General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program; if not, write to the Free Software
27# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28# 02111-1307, USA.
29#
njn25e49d8e72002-09-23 09:36:25 +000030# The GNU General Public License is contained in the file COPYING.
njn4f9c9342002-04-29 16:03:24 +000031
32#----------------------------------------------------------------------------
njn75b30a92002-05-03 17:54:51 +000033# Annotator for cachegrind.
njn4f9c9342002-04-29 16:03:24 +000034#
njn75b30a92002-05-03 17:54:51 +000035# File format is described in /docs/techdocs.html.
njn4f9c9342002-04-29 16:03:24 +000036#
njn4f9c9342002-04-29 16:03:24 +000037# Performance improvements record, using cachegrind.out for cacheprof, doing no
38# source annotation (irrelevant ones removed):
39# user time
40# 1. turned off warnings in add_hash_a_to_b() 3.81 --> 3.48s
41# [now add_array_a_to_b()]
42# 6. make line_to_CC() return a ref instead of a hash 3.01 --> 2.77s
43#
44#10. changed file format to avoid file/fn name repetition 2.40s
45# (not sure why higher; maybe due to new '.' entries?)
46#11. changed file format to drop unnecessary end-line "."s 2.36s
47# (shrunk file by about 37%)
48#12. switched from hash CCs to array CCs 1.61s
49#13. only adding b[i] to a[i] if b[i] defined (was doing it if
50# either a[i] or b[i] was defined, but if b[i] was undefined
51# it just added 0) 1.48s
52#14. Stopped converting "." entries to undef and then back 1.16s
53#15. Using foreach $i (x..y) instead of for ($i = 0...) in
54# add_array_a_to_b() 1.11s
55#
56# Auto-annotating primes:
57#16. Finding count lengths by int((length-1)/3), not by
58# commifying (halves the number of commify calls) 1.68s --> 1.47s
59
60use 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
njn4eef8d32002-11-14 16:18:55 +0000145usage: cg_annotate [options] --<pid> [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
150 --show=A,B,C only show figures for events A,B,C [all]
151 --sort=A,B,C sort columns by events A,B,C [event column order]
152 --threshold=<0--100> percentage of counts (of primary sort event) we
njnbff88762002-05-13 20:27:54 +0000153 are interested in [$default_threshold%]
njn4f9c9342002-04-29 16:03:24 +0000154 --auto=yes|no annotate all source files containing functions
155 that helped reach the event count threshold [no]
156 --context=N print N lines of context before and after
157 annotated lines [8]
158 -I --include=<dir> add <dir> to list of directories to search for
159 source files
160
njn0e1b5142003-04-15 14:58:06 +0000161 Cachegrind is Copyright (C) 2002-2003 Nicholas Nethercote.
162 Both are licensed under the GNU General Public License, version 2.
163 Bug reports, feedback, admiration, abuse, etc, to: njn25\@cam.ac.uk.
164
njn4f9c9342002-04-29 16:03:24 +0000165END
166;
167
168# Used in various places of output.
169my $fancy = '-' x 80 . "\n";
170
171#-----------------------------------------------------------------------------
172# Argument and option handling
173#-----------------------------------------------------------------------------
174sub process_cmd_line()
175{
176 for my $arg (@ARGV) {
177
178 # Option handling
179 if ($arg =~ /^-/) {
180
181 # --version
182 if ($arg =~ /^-v$|^--version$/) {
njn4eef8d32002-11-14 16:18:55 +0000183 die("cg_annotate-$version\n");
njn4f9c9342002-04-29 16:03:24 +0000184
185 # --show=A,B,C
186 } elsif ($arg =~ /^--show=(.*)$/) {
187 @show_events = split(/,/, $1);
188
189 # --sort=A,B,C
190 } elsif ($arg =~ /^--sort=(.*)$/) {
191 @sort_events = split(/,/, $1);
njnbff88762002-05-13 20:27:54 +0000192 foreach my $i (0 .. scalar @sort_events - 1) {
193 if ($sort_events[$i] =~#/.*:(\d+)$/) {
194 /.*:([\d\.]+)%?$/) {
195 my $th = $1;
196 ($th >= 0 && $th <= 100) or die($usage);
197 $sort_events[$i] =~ s/:.*//;
198 $thresholds[$i] = $th;
199 } else {
200 $thresholds[$i] = 0;
201 }
202 }
njn4f9c9342002-04-29 16:03:24 +0000203
204 # --threshold=X (tolerates a trailing '%')
205 } elsif ($arg =~ /^--threshold=([\d\.]+)%?$/) {
njn9b3366a2002-06-10 15:31:16 +0000206 $single_threshold = $1;
njnbff88762002-05-13 20:27:54 +0000207 ($1 >= 0 && $1 <= 100) or die($usage);
njn4f9c9342002-04-29 16:03:24 +0000208
209 # --auto=yes|no
210 } elsif ($arg =~ /^--auto=(yes|no)$/) {
211 $auto_annotate = 1 if ($1 eq "yes");
212 $auto_annotate = 0 if ($1 eq "no");
213
214 # --context=N
215 } elsif ($arg =~ /^--context=([\d\.]+)$/) {
216 $context = $1;
217 if ($context < 0) {
218 die($usage);
219 }
220
221 # --include=A,B,C
222 } elsif ($arg =~ /^(-I|--include)=(.*)$/) {
223 my $inc = $2;
224 $inc =~ s|/$||; # trim trailing '/'
225 push(@include_dirs, "$inc/");
226
njn25e49d8e72002-09-23 09:36:25 +0000227 } elsif ($arg =~ /^--(\d+)$/) {
228 my $pid = $1;
229 if (not defined $input_file) {
230 $input_file = "cachegrind.out.$pid";
231 } else {
232 die("One cachegrind.out.<pid> file at a time, please\n");
233 }
234
njn4f9c9342002-04-29 16:03:24 +0000235 } else { # -h and --help fall under this case
236 die($usage);
237 }
238
239 # Argument handling -- annotation file checking and selection.
njn25e49d8e72002-09-23 09:36:25 +0000240 # Stick filenames into a hash for quick 'n easy lookup throughout.
njn4f9c9342002-04-29 16:03:24 +0000241 } else {
242 my $readable = 0;
243 foreach my $include_dir (@include_dirs) {
244 if (-r $include_dir . $arg) {
245 $readable = 1;
246 }
247 }
248 $readable or die("File $arg not found in any of: @include_dirs\n");
249 $user_ann_files{$arg} = 1;
njn25e49d8e72002-09-23 09:36:25 +0000250 }
251 }
252
253 # Must have chosen an input file
254 if (not defined $input_file) {
255 die($usage);
njn4f9c9342002-04-29 16:03:24 +0000256 }
257}
258
259#-----------------------------------------------------------------------------
260# Reading of input file
261#-----------------------------------------------------------------------------
262sub max ($$)
263{
264 my ($x, $y) = @_;
265 return ($x > $y ? $x : $y);
266}
267
268# Add the two arrays; any '.' entries are ignored. Two tricky things:
269# 1. If $a2->[$i] is undefined, it defaults to 0 which is what we want; we turn
270# off warnings to allow this. This makes things about 10% faster than
271# checking for definedness ourselves.
njnbff88762002-05-13 20:27:54 +0000272# 2. We don't add an undefined count or a ".", even though it's value is 0,
273# because we don't want to make an $a2->[$i] that is undef become 0
274# unnecessarily.
njn4f9c9342002-04-29 16:03:24 +0000275sub add_array_a_to_b ($$)
276{
277 my ($a1, $a2) = @_;
278
279 my $n = max(scalar @$a1, scalar @$a2);
280 $^W = 0;
281 foreach my $i (0 .. $n-1) {
njnbff88762002-05-13 20:27:54 +0000282 $a2->[$i] += $a1->[$i] if (defined $a1->[$i] && "." ne $a1->[$i]);
njn4f9c9342002-04-29 16:03:24 +0000283 }
284 $^W = 1;
285}
286
287# Add each event count to the CC array. '.' counts become undef, as do
288# missing entries (implicitly).
289sub line_to_CC ($)
290{
291 my @CC = (split /\s+/, $_[0]);
292 (@CC <= @events) or die("Line $.: too many event counts\n");
293 return \@CC;
294}
295
296sub read_input_file()
297{
298 open(INPUTFILE, "< $input_file") || die "File $input_file not opened\n";
299
300 # Read "desc:" lines.
301 my $line;
302 # This gives a "uninitialized value in substitution (s///)" warning; hmm...
303 #while ($line = <INPUTFILE> && $line =~ s/desc:\s+//) {
304 # $desc .= "$line\n";
305 #}
306 while (1) {
307 $line = <INPUTFILE>;
308 if ($line =~ s/desc:\s+//) {
309 $desc .= $line;
310 } else {
311 last;
312 }
313 }
314
315 # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above).
316 ($line =~ s/cmd:\s+//) or die("Line $.: missing command line\n");
317 $cmd = $line;
318 chomp($cmd); # Remove newline
319
320 # Read "events:" line. We make a temporary hash in which the Nth event's
321 # value is N, which is useful for handling --show/--sort options below.
322 $line = <INPUTFILE>;
323 ($line =~ s/events:\s+//) or die("Line $.: missing events line\n");
324 @events = split(/\s+/, $line);
325 my %events;
326 my $n = 0;
327 foreach my $event (@events) {
328 $events{$event} = $n;
329 $n++
330 }
331
332 # If no --show arg give, default to showing all events in the file.
333 # If --show option is used, check all specified events appeared in the
334 # "events:" line. Then initialise @show_order.
335 if (@show_events) {
336 foreach my $show_event (@show_events) {
337 (defined $events{$show_event}) or
338 die("--show event `$show_event' did not appear in input\n");
339 }
340 } else {
341 @show_events = @events;
342 }
343 foreach my $show_event (@show_events) {
344 push(@show_order, $events{$show_event});
345 }
346
347 # Do as for --show, but if no --sort arg given, default to sorting by
348 # column order (ie. first column event is primary sort key, 2nd column is
349 # 2ndary key, etc).
350 if (@sort_events) {
351 foreach my $sort_event (@sort_events) {
352 (defined $events{$sort_event}) or
353 die("--sort event `$sort_event' did not appear in input\n");
354 }
355 } else {
356 @sort_events = @events;
357 }
358 foreach my $sort_event (@sort_events) {
359 push(@sort_order, $events{$sort_event});
360 }
361
njn9b3366a2002-06-10 15:31:16 +0000362 # If multiple threshold args weren't given via --sort, stick in the single
363 # threshold (either from --threshold if used, or the default otherwise) for
364 # the primary sort event, and 0% for the rest.
njnbff88762002-05-13 20:27:54 +0000365 if (not @thresholds) {
366 foreach my $e (@sort_order) {
367 push(@thresholds, 0);
368 }
njn9b3366a2002-06-10 15:31:16 +0000369 $thresholds[0] = $single_threshold;
njnbff88762002-05-13 20:27:54 +0000370 }
371
njn4f9c9342002-04-29 16:03:24 +0000372 my $curr_file;
373 my $curr_fn;
374 my $curr_name;
375
376 my $curr_fn_CC = [];
377 my $curr_file_ind_CCs = {}; # hash(line_num => CC)
378
379 # Read body of input file.
380 while (<INPUTFILE>) {
381 s/#.*$//; # remove comments
382 if (s/^(\d+)\s+//) {
383 my $line_num = $1;
384 my $CC = line_to_CC($_);
385 add_array_a_to_b($CC, $curr_fn_CC);
386
387 # If curr_file is selected, add CC to curr_file list. We look for
388 # full filename matches; or, if auto-annotating, we have to
389 # remember everything -- we won't know until the end what's needed.
390 if ($auto_annotate || defined $user_ann_files{$curr_file}) {
391 my $tmp = $curr_file_ind_CCs->{$line_num};
392 $tmp = [] unless defined $tmp;
393 add_array_a_to_b($CC, $tmp);
394 $curr_file_ind_CCs->{$line_num} = $tmp;
395 }
396
397 } elsif (s/^fn=(.*)$//) {
398 # Commit result from previous function
399 $fn_totals{$curr_name} = $curr_fn_CC if (defined $curr_name);
400
401 # Setup new one
402 $curr_fn = $1;
403 $curr_name = "$curr_file:$curr_fn";
404 $curr_fn_CC = $fn_totals{$curr_name};
405 $curr_fn_CC = [] unless (defined $curr_fn_CC);
406
407 } elsif (s/^fl=(.*)$//) {
408 $all_ind_CCs{$curr_file} = $curr_file_ind_CCs
409 if (defined $curr_file);
410
411 $curr_file = $1;
412 $curr_file_ind_CCs = $all_ind_CCs{$curr_file};
413 $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs);
414
415 } elsif (s/^(fi|fe)=(.*)$//) {
416 (defined $curr_name) or die("Line $.: Unexpected fi/fe line\n");
417 $fn_totals{$curr_name} = $curr_fn_CC;
418 $all_ind_CCs{$curr_file} = $curr_file_ind_CCs;
419
420 $curr_file = $2;
421 $curr_name = "$curr_file:$curr_fn";
422 $curr_file_ind_CCs = $all_ind_CCs{$curr_file};
423 $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs);
424 $curr_fn_CC = $fn_totals{$curr_name};
425 $curr_fn_CC = [] unless (defined $curr_fn_CC);
426
427 } elsif (s/^\s*$//) {
428 # blank, do nothing
429
430 } elsif (s/^summary:\s+//) {
431 # Finish up handling final filename/fn_name counts
432 $fn_totals{"$curr_file:$curr_fn"} = $curr_fn_CC
433 if (defined $curr_file && defined $curr_fn);
434 $all_ind_CCs{$curr_file} =
435 $curr_file_ind_CCs if (defined $curr_file);
436
437 $summary_CC = line_to_CC($_);
438 (scalar(@$summary_CC) == @events)
439 or die("Line $.: summary event and total event mismatch\n");
440
441 } else {
442 warn("WARNING: line $. malformed, ignoring\n");
443 }
444 }
445
446 # Check if summary line was present
447 if (not defined $summary_CC) {
448 warn("WARNING: missing final summary line, no summary will be printed\n");
449 }
450
451 close(INPUTFILE);
452}
453
454#-----------------------------------------------------------------------------
455# Print options used
456#-----------------------------------------------------------------------------
457sub print_options ()
458{
459 print($fancy);
460 print($desc);
461 print("Command: $cmd\n");
462 print("Events recorded: @events\n");
463 print("Events shown: @show_events\n");
464 print("Event sort order: @sort_events\n");
njnbff88762002-05-13 20:27:54 +0000465 print("Thresholds: @thresholds\n");
njn4f9c9342002-04-29 16:03:24 +0000466
467 my @include_dirs2 = @include_dirs; # copy @include_dirs
468 shift(@include_dirs2); # remove "" entry, which is always the first
469 unshift(@include_dirs2, "") if (0 == @include_dirs2);
470 my $include_dir = shift(@include_dirs2);
471 print("Include dirs: $include_dir\n");
472 foreach my $include_dir (@include_dirs2) {
473 print(" $include_dir\n");
474 }
475
476 my @user_ann_files = keys %user_ann_files;
477 unshift(@user_ann_files, "") if (0 == @user_ann_files);
478 my $user_ann_file = shift(@user_ann_files);
479 print("User annotated: $user_ann_file\n");
480 foreach $user_ann_file (@user_ann_files) {
481 print(" $user_ann_file\n");
482 }
483
484 my $is_on = ($auto_annotate ? "on" : "off");
485 print("Auto-annotation: $is_on\n");
486 print("\n");
487}
488
489#-----------------------------------------------------------------------------
490# Print summary and sorted function totals
491#-----------------------------------------------------------------------------
492sub mycmp ($$)
493{
494 my ($c, $d) = @_;
495
496 # Iterate through sort events (eg. 3,2); return result if two are different
497 foreach my $i (@sort_order) {
498 my ($x, $y);
499 $x = $c->[$i];
500 $y = $d->[$i];
501 $x = -1 unless defined $x;
502 $y = -1 unless defined $y;
503
504 my $cmp = $y <=> $x; # reverse sort
505 if (0 != $cmp) {
506 return $cmp;
507 }
508 }
509 # Exhausted events, equal
510 return 0;
511}
512
513sub commify ($) {
514 my ($val) = @_;
515 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
516 return $val;
517}
518
519# Because the counts can get very big, and we don't want to waste screen space
520# and make lines too long, we compute exactly how wide each column needs to be
521# by finding the widest entry for each one.
522sub compute_CC_col_widths (@)
523{
524 my @CCs = @_;
525 my $CC_col_widths = [];
526
527 # Initialise with minimum widths (from event names)
528 foreach my $event (@events) {
529 push(@$CC_col_widths, length($event));
530 }
531
532 # Find maximum width count for each column. @CC_col_width positions
533 # correspond to @CC positions.
534 foreach my $CC (@CCs) {
535 foreach my $i (0 .. scalar(@$CC)-1) {
536 if (defined $CC->[$i]) {
537 # Find length, accounting for commas that will be added
538 my $length = length $CC->[$i];
539 my $clength = $length + int(($length - 1) / 3);
540 $CC_col_widths->[$i] = max($CC_col_widths->[$i], $clength);
541 }
542 }
543 }
544 return $CC_col_widths;
545}
546
547# Print the CC with each column's size dictated by $CC_col_widths.
548sub print_CC ($$)
549{
550 my ($CC, $CC_col_widths) = @_;
551
552 foreach my $i (@show_order) {
553 my $count = (defined $CC->[$i] ? commify($CC->[$i]) : ".");
554 my $space = ' ' x ($CC_col_widths->[$i] - length($count));
555 print("$space$count ");
556 }
557}
558
559sub print_events ($)
560{
561 my ($CC_col_widths) = @_;
562
563 foreach my $i (@show_order) {
564 my $event = $events[$i];
565 my $event_width = length($event);
566 my $col_width = $CC_col_widths->[$i];
567 my $space = ' ' x ($col_width - $event_width);
njn602392b2002-04-30 11:34:54 +0000568 print("$space$event ");
njn4f9c9342002-04-29 16:03:24 +0000569 }
570}
571
572# Prints summary and function totals (with separate column widths, so that
573# function names aren't pushed over unnecessarily by huge summary figures).
574# Also returns a hash containing all the files that are involved in getting the
njnbff88762002-05-13 20:27:54 +0000575# events count above the thresholds (ie. all the interesting ones).
njn4f9c9342002-04-29 16:03:24 +0000576sub print_summary_and_fn_totals ()
577{
578 my @fn_fullnames = keys %fn_totals;
579
580 # Work out the size of each column for printing (summary and functions
581 # separately).
582 my $summary_CC_col_widths = compute_CC_col_widths($summary_CC);
583 my $fn_CC_col_widths = compute_CC_col_widths(values %fn_totals);
584
585 # Header and counts for summary
586 print($fancy);
587 print_events($summary_CC_col_widths);
588 print("\n");
589 print($fancy);
590 print_CC($summary_CC, $summary_CC_col_widths);
591 print(" PROGRAM TOTALS\n");
592 print("\n");
593
594 # Header for functions
595 print($fancy);
596 print_events($fn_CC_col_widths);
597 print(" file:function\n");
598 print($fancy);
599
600 # Sort function names into order dictated by --sort option.
601 @fn_fullnames = sort {
602 mycmp($fn_totals{$a}, $fn_totals{$b})
603 } @fn_fullnames;
604
njnbff88762002-05-13 20:27:54 +0000605
606 # Assertion
607 (scalar @sort_order == scalar @thresholds) or
608 die("sort_order length != thresholds length:\n",
609 " @sort_order\n @thresholds\n");
610
njn4f9c9342002-04-29 16:03:24 +0000611 my $threshold_files = {};
njnbff88762002-05-13 20:27:54 +0000612 # @curr_totals has the same shape as @sort_order and @thresholds
613 my @curr_totals = ();
614 foreach my $e (@thresholds) {
615 push(@curr_totals, 0);
616 }
njn4f9c9342002-04-29 16:03:24 +0000617
618 # Print functions, stopping when the threshold has been reached.
619 foreach my $fn_name (@fn_fullnames) {
620
njnbff88762002-05-13 20:27:54 +0000621 # Stop when we've reached all the thresholds
622 my $reached_all_thresholds = 1;
njnb94e77a2002-05-15 14:30:55 +0000623 foreach my $i (0 .. scalar @thresholds - 1) {
njnbff88762002-05-13 20:27:54 +0000624 my $prop = $curr_totals[$i] * 100 / $summary_CC->[$sort_order[$i]];
625 $reached_all_thresholds &= ($prop >= $thresholds[$i]);
626 }
627 last if $reached_all_thresholds;
njn4f9c9342002-04-29 16:03:24 +0000628
629 # Print function results
630 my $fn_CC = $fn_totals{$fn_name};
631 print_CC($fn_CC, $fn_CC_col_widths);
632 print(" $fn_name\n");
633
njnbff88762002-05-13 20:27:54 +0000634 # Update the threshold counts
njn4f9c9342002-04-29 16:03:24 +0000635 my $filename = $fn_name;
daywalkerd722c202002-05-01 21:52:05 +0000636 $filename =~ s/:.+$//; # remove function name
njn4f9c9342002-04-29 16:03:24 +0000637 $threshold_files->{$filename} = 1;
njnbff88762002-05-13 20:27:54 +0000638 foreach my $i (0 .. scalar @sort_order - 1) {
639 $curr_totals[$i] += $fn_CC->[$sort_order[$i]]
640 if (defined $fn_CC->[$sort_order[$i]]);
641 }
njn4f9c9342002-04-29 16:03:24 +0000642 }
643 print("\n");
644
645 return $threshold_files;
646}
647
648#-----------------------------------------------------------------------------
649# Annotate selected files
650#-----------------------------------------------------------------------------
651
652# Issue a warning that the source file is more recent than the input file.
653sub warning_on_src_more_recent_than_inputfile ($)
654{
655 my $src_file = $_[0];
656
657 my $warning = <<END
658@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
659@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
660@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
661@ Source file '$src_file' is more recent than input file '$input_file'.
662@ Annotations may not be correct.
663@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
664
665END
666;
667 print($warning);
668}
669
670# If there is information about lines not in the file, issue a warning
671# explaining possible causes.
672sub warning_on_nonexistent_lines ($$$)
673{
674 my ($src_more_recent_than_inputfile, $src_file, $excess_line_nums) = @_;
675 my $cause_and_solution;
676
677 if ($src_more_recent_than_inputfile) {
678 $cause_and_solution = <<END
679@@ cause: '$src_file' has changed since information was gathered.
680@@ If so, a warning will have already been issued about this.
681@@ solution: Recompile program and rerun under "valgrind --cachesim=yes" to
682@@ gather new information.
683END
684 # We suppress warnings about .h files
685 } elsif ($src_file =~ /\.h$/) {
686 $cause_and_solution = <<END
687@@ cause: bug in the Valgrind's debug info reader that screws up with .h
688@@ files sometimes
689@@ solution: none, sorry
690END
691 } else {
692 $cause_and_solution = <<END
693@@ cause: not sure, sorry
694END
695 }
696
697 my $warning = <<END
698@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
699@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
700@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
701@@
702@@ Information recorded about lines past the end of '$src_file'.
703@@
704@@ Probable cause and solution:
705$cause_and_solution@@
706@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
707END
708;
709 print($warning);
710}
711
712sub annotate_ann_files($)
713{
714 my ($threshold_files) = @_;
715
716 my %all_ann_files;
717 my @unfound_auto_annotate_files;
718 my $printed_totals_CC = [];
719
720 # If auto-annotating, add interesting files (but not "???")
721 if ($auto_annotate) {
722 delete $threshold_files->{"???"};
723 %all_ann_files = (%user_ann_files, %$threshold_files)
724 } else {
725 %all_ann_files = %user_ann_files;
726 }
727
728 # Track if we did any annotations.
729 my $did_annotations = 0;
730
731 LOOP:
732 foreach my $src_file (keys %all_ann_files) {
733
734 my $opened_file = "";
735 my $full_file_name = "";
736 foreach my $include_dir (@include_dirs) {
737 my $try_name = $include_dir . $src_file;
738 if (open(INPUTFILE, "< $try_name")) {
739 $opened_file = $try_name;
740 $full_file_name = ($include_dir eq ""
741 ? $src_file
742 : "$include_dir + $src_file");
743 last;
744 }
745 }
746
747 if (not $opened_file) {
748 # Failed to open the file. If chosen on the command line, die.
749 # If arose from auto-annotation, print a little message.
750 if (defined $user_ann_files{$src_file}) {
751 die("File $src_file not opened in any of: @include_dirs\n");
752
753 } else {
754 push(@unfound_auto_annotate_files, $src_file);
755 }
756
757 } else {
758 # File header (distinguish between user- and auto-selected files).
759 print("$fancy");
760 my $ann_type =
761 (defined $user_ann_files{$src_file} ? "User" : "Auto");
762 print("-- $ann_type-annotated source: $full_file_name\n");
763 print("$fancy");
764
765 # Get file's CCs
766 my $src_file_CCs = $all_ind_CCs{$src_file};
767 if (!defined $src_file_CCs) {
768 print(" No information has been collected for $src_file\n\n");
769 next LOOP;
770 }
771
772 $did_annotations = 1;
773
774 # Numeric, not lexicographic sort!
775 my @line_nums = sort {$a <=> $b} keys %$src_file_CCs;
776
777 # If $src_file more recent than cachegrind.out, issue warning
778 my $src_more_recent_than_inputfile = 0;
779 if ((stat $opened_file)[9] > (stat $input_file)[9]) {
780 $src_more_recent_than_inputfile = 1;
781 warning_on_src_more_recent_than_inputfile($src_file);
782 }
783
784 # Work out the size of each column for printing
785 my $CC_col_widths = compute_CC_col_widths(values %$src_file_CCs);
786
787 # Events header
788 print_events($CC_col_widths);
789 print("\n\n");
790
791 # Shift out 0 if it's in the line numbers (from unknown entries,
792 # likely due to bugs in Valgrind's stabs debug info reader)
793 shift(@line_nums) if (0 == $line_nums[0]);
794
795 # Finds interesting line ranges -- all lines with a CC, and all
796 # lines within $context lines of a line with a CC.
797 my $n = @line_nums;
798 my @pairs;
799 for (my $i = 0; $i < $n; $i++) {
800 push(@pairs, $line_nums[$i] - $context); # lower marker
801 while ($i < $n-1 &&
802 $line_nums[$i] + 2*$context >= $line_nums[$i+1]) {
803 $i++;
804 }
805 push(@pairs, $line_nums[$i] + $context); # upper marker
806 }
807
808 # Annotate chosen lines, tracking total counts of lines printed
809 $pairs[0] = 1 if ($pairs[0] < 1);
810 while (@pairs) {
811 my $low = shift @pairs;
812 my $high = shift @pairs;
813 while ($. < $low-1) {
814 my $tmp = <INPUTFILE>;
815 last unless (defined $tmp); # hack to detect EOF
816 }
817 my $src_line;
818 # Print line number, unless start of file
819 print("-- line $low " . '-' x 40 . "\n") if ($low != 1);
820 while (($. < $high) && ($src_line = <INPUTFILE>)) {
821 if (defined $line_nums[0] && $. == $line_nums[0]) {
822 print_CC($src_file_CCs->{$.}, $CC_col_widths);
823 add_array_a_to_b($src_file_CCs->{$.},
824 $printed_totals_CC);
825 shift(@line_nums);
826
827 } else {
828 print_CC( [], $CC_col_widths);
829 }
830
831 print(" $src_line");
832 }
833 # Print line number, unless EOF
834 if ($src_line) {
835 print("-- line $high " . '-' x 40 . "\n");
836 } else {
837 last;
838 }
839 }
840
841 # If there was info on lines past the end of the file...
842 if (@line_nums) {
843 foreach my $line_num (@line_nums) {
844 print_CC($src_file_CCs->{$line_num}, $CC_col_widths);
845 print(" <bogus line $line_num>\n");
846 }
847 print("\n");
848 warning_on_nonexistent_lines($src_more_recent_than_inputfile,
849 $src_file, \@line_nums);
850 }
851 print("\n");
852
853 # Print summary of counts attributed to file but not to any
854 # particular line (due to incomplete debug info).
855 if ($src_file_CCs->{0}) {
856 print_CC($src_file_CCs->{0}, $CC_col_widths);
857 print(" <counts for unidentified lines in $src_file>\n\n");
858 }
859
860 close(INPUTFILE);
861 }
862 }
863
864 # Print list of unfound auto-annotate selected files.
865 if (@unfound_auto_annotate_files) {
866 print("$fancy");
867 print("The following files chosen for auto-annotation could not be found:\n");
868 print($fancy);
869 foreach my $f (@unfound_auto_annotate_files) {
870 print(" $f\n");
871 }
872 print("\n");
873 }
874
875 # If we did any annotating, print what proportion of events were covered by
876 # annotated lines above.
877 if ($did_annotations) {
878 my $percent_printed_CC;
879 foreach (my $i = 0; $i < @$summary_CC; $i++) {
880 $percent_printed_CC->[$i] =
881 sprintf("%.0f",
882 $printed_totals_CC->[$i] / $summary_CC->[$i] * 100);
883 }
884 my $pp_CC_col_widths = compute_CC_col_widths($percent_printed_CC);
885 print($fancy);
886 print_events($pp_CC_col_widths);
887 print("\n");
888 print($fancy);
889 print_CC($percent_printed_CC, $pp_CC_col_widths);
890 print(" percentage of events annotated\n\n");
891 }
892}
893
894#----------------------------------------------------------------------------
895# "main()"
896#----------------------------------------------------------------------------
897process_cmd_line();
898read_input_file();
899print_options();
900my $threshold_files = print_summary_and_fn_totals();
901annotate_ann_files($threshold_files);
902
njn7cf0bd32002-06-08 13:36:03 +0000903##--------------------------------------------------------------------##
njn4eef8d32002-11-14 16:18:55 +0000904##--- end cg_annotate.in ---##
njn7cf0bd32002-06-08 13:36:03 +0000905##--------------------------------------------------------------------##
906
907