njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1 | #! /usr/bin/perl -w |
| 2 | ##--------------------------------------------------------------------## |
| 3 | ##--- The cache simulation framework: instrumentation, recording ---## |
| 4 | ##--- and results printing. ---## |
| 5 | ##--- vg_annotate ---## |
| 6 | ##--------------------------------------------------------------------## |
| 7 | |
| 8 | # This file is part of Valgrind, an x86 protected-mode emulator |
| 9 | # designed for debugging and profiling binaries on x86-Unixes. |
| 10 | # |
sewardj | 3c23d43 | 2002-06-01 23:43:49 +0000 | [diff] [blame] | 11 | # Copyright (C) 2002 Nicholas Nethercote |
| 12 | # njn25@cam.ac.uk |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 13 | # |
| 14 | # This program is free software; you can redistribute it and/or |
| 15 | # modify it under the terms of the GNU General Public License as |
| 16 | # published by the Free Software Foundation; either version 2 of the |
| 17 | # License, or (at your option) any later version. |
| 18 | # |
| 19 | # This program is distributed in the hope that it will be useful, but |
| 20 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | # General Public License for more details. |
| 23 | # |
| 24 | # You should have received a copy of the GNU General Public License |
| 25 | # along with this program; if not, write to the Free Software |
| 26 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | # 02111-1307, USA. |
| 28 | # |
| 29 | # The GNU General Public License is contained in the file LICENSE. |
| 30 | |
| 31 | #---------------------------------------------------------------------------- |
njn | 75b30a9 | 2002-05-03 17:54:51 +0000 | [diff] [blame] | 32 | # Annotator for cachegrind. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 33 | # |
njn | 75b30a9 | 2002-05-03 17:54:51 +0000 | [diff] [blame] | 34 | # File format is described in /docs/techdocs.html. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 35 | # |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 36 | # 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 | |
| 59 | use strict; |
| 60 | |
| 61 | #---------------------------------------------------------------------------- |
| 62 | # Overview: the running example in the comments is for: |
| 63 | # - events = A,B,C,D |
| 64 | # - --show=C,A,D |
| 65 | # - --sort=D,C |
| 66 | #---------------------------------------------------------------------------- |
| 67 | |
| 68 | #---------------------------------------------------------------------------- |
| 69 | # Global variables, main data structures |
| 70 | #---------------------------------------------------------------------------- |
| 71 | # CCs are arrays, the counts corresponding to @events, with 'undef' |
| 72 | # representing '.'. This makes things fast (faster than using hashes for CCs) |
| 73 | # but we have to use @sort_order and @show_order below to handle the --sort and |
| 74 | # --show options, which is a bit tricky. |
| 75 | #---------------------------------------------------------------------------- |
| 76 | |
| 77 | # Total counts for summary (an array reference). |
| 78 | my $summary_CC; |
| 79 | |
| 80 | # Totals for each function, for overall summary. |
| 81 | # hash(filename:fn_name => CC array) |
| 82 | my %fn_totals; |
| 83 | |
| 84 | # Individual CCs, organised by filename and line_num for easy annotation. |
| 85 | # hash(filename => hash(line_num => CC array)) |
| 86 | my %all_ind_CCs; |
| 87 | |
| 88 | # Files chosen for annotation on the command line. |
| 89 | # key = basename (trimmed of any directory), value = full filename |
| 90 | my %user_ann_files; |
| 91 | |
| 92 | # Generic description string. |
| 93 | my $desc = ""; |
| 94 | |
| 95 | # Command line of profiled program. |
| 96 | my $cmd; |
| 97 | |
| 98 | # Events in input file, eg. (A,B,C,D) |
| 99 | my @events; |
| 100 | |
| 101 | # Events to show, from command line, eg. (C,A,D) |
| 102 | my @show_events; |
| 103 | |
| 104 | # Map from @show_events indices to @events indices, eg. (2,0,3). Gives the |
| 105 | # order in which we must traverse @events in order to show the @show_events, |
| 106 | # eg. (@events[$show_order[1]], @events[$show_order[2]]...) = @show_events. |
| 107 | # (Might help to think of it like a hash (0 => 2, 1 => 0, 2 => 3).) |
| 108 | my @show_order; |
| 109 | |
| 110 | # Print out the function totals sorted by these events, eg. (D,C). |
| 111 | my @sort_events; |
| 112 | |
| 113 | # Map from @sort_events indices to @events indices, eg. (3,2). Same idea as |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 114 | # for @show_order. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 115 | my @sort_order; |
| 116 | |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 117 | # Thresholds, one for each sort event (or default to 1 if no sort events |
| 118 | # specified). We print out functions and do auto-annotations until we've |
| 119 | # handled this proportion of all the events thresholded. |
| 120 | my @thresholds; |
| 121 | |
| 122 | my $default_threshold = 99; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 123 | |
| 124 | # If on, automatically annotates all files that are involved in getting over |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 125 | # all the threshold counts. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 126 | my $auto_annotate = 0; |
| 127 | |
| 128 | # Number of lines to show around each annotated line. |
| 129 | my $context = 8; |
| 130 | |
| 131 | # Directories in which to look for annotation files. |
| 132 | my @include_dirs = (""); |
| 133 | |
| 134 | # Input file name |
| 135 | my $input_file = "cachegrind.out"; |
| 136 | |
| 137 | # Version number |
| 138 | my $version = "@VERSION@"; |
| 139 | |
| 140 | # Usage message. |
| 141 | my $usage = <<END |
| 142 | usage: vg_annotate [options] [source-files] |
| 143 | |
| 144 | options for the user, with defaults in [ ], are: |
| 145 | -h --help show this message |
| 146 | -v --version show version |
| 147 | --show=A,B,C only show figures for events A,B,C [all] |
| 148 | --sort=A,B,C sort columns by events A,B,C [event column order] |
| 149 | --threshold=<0--100> percentage of counts (of primary sort event) we |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 150 | are interested in [$default_threshold%] |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 151 | --auto=yes|no annotate all source files containing functions |
| 152 | that helped reach the event count threshold [no] |
| 153 | --context=N print N lines of context before and after |
| 154 | annotated lines [8] |
| 155 | -I --include=<dir> add <dir> to list of directories to search for |
| 156 | source files |
| 157 | |
| 158 | Valgrind is Copyright (C) 2000-2002 Julian Seward |
| 159 | and licensed under the GNU General Public License, version 2. |
| 160 | Bug reports, feedback, admiration, abuse, etc, to: jseward\@acm.org. |
| 161 | |
| 162 | END |
| 163 | ; |
| 164 | |
| 165 | # Used in various places of output. |
| 166 | my $fancy = '-' x 80 . "\n"; |
| 167 | |
| 168 | #----------------------------------------------------------------------------- |
| 169 | # Argument and option handling |
| 170 | #----------------------------------------------------------------------------- |
| 171 | sub process_cmd_line() |
| 172 | { |
| 173 | for my $arg (@ARGV) { |
| 174 | |
| 175 | # Option handling |
| 176 | if ($arg =~ /^-/) { |
| 177 | |
| 178 | # --version |
| 179 | if ($arg =~ /^-v$|^--version$/) { |
sewardj | aa6fecc | 2002-04-29 17:27:07 +0000 | [diff] [blame] | 180 | die("vg_annotate-$version\n"); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 181 | |
| 182 | # --show=A,B,C |
| 183 | } elsif ($arg =~ /^--show=(.*)$/) { |
| 184 | @show_events = split(/,/, $1); |
| 185 | |
| 186 | # --sort=A,B,C |
| 187 | } elsif ($arg =~ /^--sort=(.*)$/) { |
| 188 | @sort_events = split(/,/, $1); |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 189 | foreach my $i (0 .. scalar @sort_events - 1) { |
| 190 | if ($sort_events[$i] =~#/.*:(\d+)$/) { |
| 191 | /.*:([\d\.]+)%?$/) { |
| 192 | my $th = $1; |
| 193 | ($th >= 0 && $th <= 100) or die($usage); |
| 194 | $sort_events[$i] =~ s/:.*//; |
| 195 | $thresholds[$i] = $th; |
| 196 | } else { |
| 197 | $thresholds[$i] = 0; |
| 198 | } |
| 199 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 200 | |
| 201 | # --threshold=X (tolerates a trailing '%') |
| 202 | } elsif ($arg =~ /^--threshold=([\d\.]+)%?$/) { |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 203 | $thresholds[0] = $1; |
| 204 | ($1 >= 0 && $1 <= 100) or die($usage); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 205 | |
| 206 | # --auto=yes|no |
| 207 | } elsif ($arg =~ /^--auto=(yes|no)$/) { |
| 208 | $auto_annotate = 1 if ($1 eq "yes"); |
| 209 | $auto_annotate = 0 if ($1 eq "no"); |
| 210 | |
| 211 | # --context=N |
| 212 | } elsif ($arg =~ /^--context=([\d\.]+)$/) { |
| 213 | $context = $1; |
| 214 | if ($context < 0) { |
| 215 | die($usage); |
| 216 | } |
| 217 | |
| 218 | # --include=A,B,C |
| 219 | } elsif ($arg =~ /^(-I|--include)=(.*)$/) { |
| 220 | my $inc = $2; |
| 221 | $inc =~ s|/$||; # trim trailing '/' |
| 222 | push(@include_dirs, "$inc/"); |
| 223 | |
| 224 | } else { # -h and --help fall under this case |
| 225 | die($usage); |
| 226 | } |
| 227 | |
| 228 | # Argument handling -- annotation file checking and selection. |
| 229 | # Stick filenames into a hash for quick 'n easy lookup throughout |
| 230 | } else { |
| 231 | my $readable = 0; |
| 232 | foreach my $include_dir (@include_dirs) { |
| 233 | if (-r $include_dir . $arg) { |
| 234 | $readable = 1; |
| 235 | } |
| 236 | } |
| 237 | $readable or die("File $arg not found in any of: @include_dirs\n"); |
| 238 | $user_ann_files{$arg} = 1; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | #----------------------------------------------------------------------------- |
| 244 | # Reading of input file |
| 245 | #----------------------------------------------------------------------------- |
| 246 | sub max ($$) |
| 247 | { |
| 248 | my ($x, $y) = @_; |
| 249 | return ($x > $y ? $x : $y); |
| 250 | } |
| 251 | |
| 252 | # Add the two arrays; any '.' entries are ignored. Two tricky things: |
| 253 | # 1. If $a2->[$i] is undefined, it defaults to 0 which is what we want; we turn |
| 254 | # off warnings to allow this. This makes things about 10% faster than |
| 255 | # checking for definedness ourselves. |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 256 | # 2. We don't add an undefined count or a ".", even though it's value is 0, |
| 257 | # because we don't want to make an $a2->[$i] that is undef become 0 |
| 258 | # unnecessarily. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 259 | sub add_array_a_to_b ($$) |
| 260 | { |
| 261 | my ($a1, $a2) = @_; |
| 262 | |
| 263 | my $n = max(scalar @$a1, scalar @$a2); |
| 264 | $^W = 0; |
| 265 | foreach my $i (0 .. $n-1) { |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 266 | $a2->[$i] += $a1->[$i] if (defined $a1->[$i] && "." ne $a1->[$i]); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 267 | } |
| 268 | $^W = 1; |
| 269 | } |
| 270 | |
| 271 | # Add each event count to the CC array. '.' counts become undef, as do |
| 272 | # missing entries (implicitly). |
| 273 | sub line_to_CC ($) |
| 274 | { |
| 275 | my @CC = (split /\s+/, $_[0]); |
| 276 | (@CC <= @events) or die("Line $.: too many event counts\n"); |
| 277 | return \@CC; |
| 278 | } |
| 279 | |
| 280 | sub read_input_file() |
| 281 | { |
| 282 | open(INPUTFILE, "< $input_file") || die "File $input_file not opened\n"; |
| 283 | |
| 284 | # Read "desc:" lines. |
| 285 | my $line; |
| 286 | # This gives a "uninitialized value in substitution (s///)" warning; hmm... |
| 287 | #while ($line = <INPUTFILE> && $line =~ s/desc:\s+//) { |
| 288 | # $desc .= "$line\n"; |
| 289 | #} |
| 290 | while (1) { |
| 291 | $line = <INPUTFILE>; |
| 292 | if ($line =~ s/desc:\s+//) { |
| 293 | $desc .= $line; |
| 294 | } else { |
| 295 | last; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above). |
| 300 | ($line =~ s/cmd:\s+//) or die("Line $.: missing command line\n"); |
| 301 | $cmd = $line; |
| 302 | chomp($cmd); # Remove newline |
| 303 | |
| 304 | # Read "events:" line. We make a temporary hash in which the Nth event's |
| 305 | # value is N, which is useful for handling --show/--sort options below. |
| 306 | $line = <INPUTFILE>; |
| 307 | ($line =~ s/events:\s+//) or die("Line $.: missing events line\n"); |
| 308 | @events = split(/\s+/, $line); |
| 309 | my %events; |
| 310 | my $n = 0; |
| 311 | foreach my $event (@events) { |
| 312 | $events{$event} = $n; |
| 313 | $n++ |
| 314 | } |
| 315 | |
| 316 | # If no --show arg give, default to showing all events in the file. |
| 317 | # If --show option is used, check all specified events appeared in the |
| 318 | # "events:" line. Then initialise @show_order. |
| 319 | if (@show_events) { |
| 320 | foreach my $show_event (@show_events) { |
| 321 | (defined $events{$show_event}) or |
| 322 | die("--show event `$show_event' did not appear in input\n"); |
| 323 | } |
| 324 | } else { |
| 325 | @show_events = @events; |
| 326 | } |
| 327 | foreach my $show_event (@show_events) { |
| 328 | push(@show_order, $events{$show_event}); |
| 329 | } |
| 330 | |
| 331 | # Do as for --show, but if no --sort arg given, default to sorting by |
| 332 | # column order (ie. first column event is primary sort key, 2nd column is |
| 333 | # 2ndary key, etc). |
| 334 | if (@sort_events) { |
| 335 | foreach my $sort_event (@sort_events) { |
| 336 | (defined $events{$sort_event}) or |
| 337 | die("--sort event `$sort_event' did not appear in input\n"); |
| 338 | } |
| 339 | } else { |
| 340 | @sort_events = @events; |
| 341 | } |
| 342 | foreach my $sort_event (@sort_events) { |
| 343 | push(@sort_order, $events{$sort_event}); |
| 344 | } |
| 345 | |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 346 | # If no --threshold args give, default to 99% for the primary sort event, |
| 347 | # and 0% for the rest. |
| 348 | if (not @thresholds) { |
| 349 | foreach my $e (@sort_order) { |
| 350 | push(@thresholds, 0); |
| 351 | } |
| 352 | $thresholds[0] = $default_threshold; |
| 353 | } |
| 354 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 355 | my $curr_file; |
| 356 | my $curr_fn; |
| 357 | my $curr_name; |
| 358 | |
| 359 | my $curr_fn_CC = []; |
| 360 | my $curr_file_ind_CCs = {}; # hash(line_num => CC) |
| 361 | |
| 362 | # Read body of input file. |
| 363 | while (<INPUTFILE>) { |
| 364 | s/#.*$//; # remove comments |
| 365 | if (s/^(\d+)\s+//) { |
| 366 | my $line_num = $1; |
| 367 | my $CC = line_to_CC($_); |
| 368 | add_array_a_to_b($CC, $curr_fn_CC); |
| 369 | |
| 370 | # If curr_file is selected, add CC to curr_file list. We look for |
| 371 | # full filename matches; or, if auto-annotating, we have to |
| 372 | # remember everything -- we won't know until the end what's needed. |
| 373 | if ($auto_annotate || defined $user_ann_files{$curr_file}) { |
| 374 | my $tmp = $curr_file_ind_CCs->{$line_num}; |
| 375 | $tmp = [] unless defined $tmp; |
| 376 | add_array_a_to_b($CC, $tmp); |
| 377 | $curr_file_ind_CCs->{$line_num} = $tmp; |
| 378 | } |
| 379 | |
| 380 | } elsif (s/^fn=(.*)$//) { |
| 381 | # Commit result from previous function |
| 382 | $fn_totals{$curr_name} = $curr_fn_CC if (defined $curr_name); |
| 383 | |
| 384 | # Setup new one |
| 385 | $curr_fn = $1; |
| 386 | $curr_name = "$curr_file:$curr_fn"; |
| 387 | $curr_fn_CC = $fn_totals{$curr_name}; |
| 388 | $curr_fn_CC = [] unless (defined $curr_fn_CC); |
| 389 | |
| 390 | } elsif (s/^fl=(.*)$//) { |
| 391 | $all_ind_CCs{$curr_file} = $curr_file_ind_CCs |
| 392 | if (defined $curr_file); |
| 393 | |
| 394 | $curr_file = $1; |
| 395 | $curr_file_ind_CCs = $all_ind_CCs{$curr_file}; |
| 396 | $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs); |
| 397 | |
| 398 | } elsif (s/^(fi|fe)=(.*)$//) { |
| 399 | (defined $curr_name) or die("Line $.: Unexpected fi/fe line\n"); |
| 400 | $fn_totals{$curr_name} = $curr_fn_CC; |
| 401 | $all_ind_CCs{$curr_file} = $curr_file_ind_CCs; |
| 402 | |
| 403 | $curr_file = $2; |
| 404 | $curr_name = "$curr_file:$curr_fn"; |
| 405 | $curr_file_ind_CCs = $all_ind_CCs{$curr_file}; |
| 406 | $curr_file_ind_CCs = {} unless (defined $curr_file_ind_CCs); |
| 407 | $curr_fn_CC = $fn_totals{$curr_name}; |
| 408 | $curr_fn_CC = [] unless (defined $curr_fn_CC); |
| 409 | |
| 410 | } elsif (s/^\s*$//) { |
| 411 | # blank, do nothing |
| 412 | |
| 413 | } elsif (s/^summary:\s+//) { |
| 414 | # Finish up handling final filename/fn_name counts |
| 415 | $fn_totals{"$curr_file:$curr_fn"} = $curr_fn_CC |
| 416 | if (defined $curr_file && defined $curr_fn); |
| 417 | $all_ind_CCs{$curr_file} = |
| 418 | $curr_file_ind_CCs if (defined $curr_file); |
| 419 | |
| 420 | $summary_CC = line_to_CC($_); |
| 421 | (scalar(@$summary_CC) == @events) |
| 422 | or die("Line $.: summary event and total event mismatch\n"); |
| 423 | |
| 424 | } else { |
| 425 | warn("WARNING: line $. malformed, ignoring\n"); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | # Check if summary line was present |
| 430 | if (not defined $summary_CC) { |
| 431 | warn("WARNING: missing final summary line, no summary will be printed\n"); |
| 432 | } |
| 433 | |
| 434 | close(INPUTFILE); |
| 435 | } |
| 436 | |
| 437 | #----------------------------------------------------------------------------- |
| 438 | # Print options used |
| 439 | #----------------------------------------------------------------------------- |
| 440 | sub print_options () |
| 441 | { |
| 442 | print($fancy); |
| 443 | print($desc); |
| 444 | print("Command: $cmd\n"); |
| 445 | print("Events recorded: @events\n"); |
| 446 | print("Events shown: @show_events\n"); |
| 447 | print("Event sort order: @sort_events\n"); |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 448 | print("Thresholds: @thresholds\n"); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 449 | |
| 450 | my @include_dirs2 = @include_dirs; # copy @include_dirs |
| 451 | shift(@include_dirs2); # remove "" entry, which is always the first |
| 452 | unshift(@include_dirs2, "") if (0 == @include_dirs2); |
| 453 | my $include_dir = shift(@include_dirs2); |
| 454 | print("Include dirs: $include_dir\n"); |
| 455 | foreach my $include_dir (@include_dirs2) { |
| 456 | print(" $include_dir\n"); |
| 457 | } |
| 458 | |
| 459 | my @user_ann_files = keys %user_ann_files; |
| 460 | unshift(@user_ann_files, "") if (0 == @user_ann_files); |
| 461 | my $user_ann_file = shift(@user_ann_files); |
| 462 | print("User annotated: $user_ann_file\n"); |
| 463 | foreach $user_ann_file (@user_ann_files) { |
| 464 | print(" $user_ann_file\n"); |
| 465 | } |
| 466 | |
| 467 | my $is_on = ($auto_annotate ? "on" : "off"); |
| 468 | print("Auto-annotation: $is_on\n"); |
| 469 | print("\n"); |
| 470 | } |
| 471 | |
| 472 | #----------------------------------------------------------------------------- |
| 473 | # Print summary and sorted function totals |
| 474 | #----------------------------------------------------------------------------- |
| 475 | sub mycmp ($$) |
| 476 | { |
| 477 | my ($c, $d) = @_; |
| 478 | |
| 479 | # Iterate through sort events (eg. 3,2); return result if two are different |
| 480 | foreach my $i (@sort_order) { |
| 481 | my ($x, $y); |
| 482 | $x = $c->[$i]; |
| 483 | $y = $d->[$i]; |
| 484 | $x = -1 unless defined $x; |
| 485 | $y = -1 unless defined $y; |
| 486 | |
| 487 | my $cmp = $y <=> $x; # reverse sort |
| 488 | if (0 != $cmp) { |
| 489 | return $cmp; |
| 490 | } |
| 491 | } |
| 492 | # Exhausted events, equal |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | sub commify ($) { |
| 497 | my ($val) = @_; |
| 498 | 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/); |
| 499 | return $val; |
| 500 | } |
| 501 | |
| 502 | # Because the counts can get very big, and we don't want to waste screen space |
| 503 | # and make lines too long, we compute exactly how wide each column needs to be |
| 504 | # by finding the widest entry for each one. |
| 505 | sub compute_CC_col_widths (@) |
| 506 | { |
| 507 | my @CCs = @_; |
| 508 | my $CC_col_widths = []; |
| 509 | |
| 510 | # Initialise with minimum widths (from event names) |
| 511 | foreach my $event (@events) { |
| 512 | push(@$CC_col_widths, length($event)); |
| 513 | } |
| 514 | |
| 515 | # Find maximum width count for each column. @CC_col_width positions |
| 516 | # correspond to @CC positions. |
| 517 | foreach my $CC (@CCs) { |
| 518 | foreach my $i (0 .. scalar(@$CC)-1) { |
| 519 | if (defined $CC->[$i]) { |
| 520 | # Find length, accounting for commas that will be added |
| 521 | my $length = length $CC->[$i]; |
| 522 | my $clength = $length + int(($length - 1) / 3); |
| 523 | $CC_col_widths->[$i] = max($CC_col_widths->[$i], $clength); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | return $CC_col_widths; |
| 528 | } |
| 529 | |
| 530 | # Print the CC with each column's size dictated by $CC_col_widths. |
| 531 | sub print_CC ($$) |
| 532 | { |
| 533 | my ($CC, $CC_col_widths) = @_; |
| 534 | |
| 535 | foreach my $i (@show_order) { |
| 536 | my $count = (defined $CC->[$i] ? commify($CC->[$i]) : "."); |
| 537 | my $space = ' ' x ($CC_col_widths->[$i] - length($count)); |
| 538 | print("$space$count "); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | sub print_events ($) |
| 543 | { |
| 544 | my ($CC_col_widths) = @_; |
| 545 | |
| 546 | foreach my $i (@show_order) { |
| 547 | my $event = $events[$i]; |
| 548 | my $event_width = length($event); |
| 549 | my $col_width = $CC_col_widths->[$i]; |
| 550 | my $space = ' ' x ($col_width - $event_width); |
njn | 602392b | 2002-04-30 11:34:54 +0000 | [diff] [blame] | 551 | print("$space$event "); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | # Prints summary and function totals (with separate column widths, so that |
| 556 | # function names aren't pushed over unnecessarily by huge summary figures). |
| 557 | # Also returns a hash containing all the files that are involved in getting the |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 558 | # events count above the thresholds (ie. all the interesting ones). |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 559 | sub print_summary_and_fn_totals () |
| 560 | { |
| 561 | my @fn_fullnames = keys %fn_totals; |
| 562 | |
| 563 | # Work out the size of each column for printing (summary and functions |
| 564 | # separately). |
| 565 | my $summary_CC_col_widths = compute_CC_col_widths($summary_CC); |
| 566 | my $fn_CC_col_widths = compute_CC_col_widths(values %fn_totals); |
| 567 | |
| 568 | # Header and counts for summary |
| 569 | print($fancy); |
| 570 | print_events($summary_CC_col_widths); |
| 571 | print("\n"); |
| 572 | print($fancy); |
| 573 | print_CC($summary_CC, $summary_CC_col_widths); |
| 574 | print(" PROGRAM TOTALS\n"); |
| 575 | print("\n"); |
| 576 | |
| 577 | # Header for functions |
| 578 | print($fancy); |
| 579 | print_events($fn_CC_col_widths); |
| 580 | print(" file:function\n"); |
| 581 | print($fancy); |
| 582 | |
| 583 | # Sort function names into order dictated by --sort option. |
| 584 | @fn_fullnames = sort { |
| 585 | mycmp($fn_totals{$a}, $fn_totals{$b}) |
| 586 | } @fn_fullnames; |
| 587 | |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 588 | |
| 589 | # Assertion |
| 590 | (scalar @sort_order == scalar @thresholds) or |
| 591 | die("sort_order length != thresholds length:\n", |
| 592 | " @sort_order\n @thresholds\n"); |
| 593 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 594 | my $threshold_files = {}; |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 595 | # @curr_totals has the same shape as @sort_order and @thresholds |
| 596 | my @curr_totals = (); |
| 597 | foreach my $e (@thresholds) { |
| 598 | push(@curr_totals, 0); |
| 599 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 600 | |
| 601 | # Print functions, stopping when the threshold has been reached. |
| 602 | foreach my $fn_name (@fn_fullnames) { |
| 603 | |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 604 | # Stop when we've reached all the thresholds |
| 605 | my $reached_all_thresholds = 1; |
njn | b94e77a | 2002-05-15 14:30:55 +0000 | [diff] [blame] | 606 | foreach my $i (0 .. scalar @thresholds - 1) { |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 607 | my $prop = $curr_totals[$i] * 100 / $summary_CC->[$sort_order[$i]]; |
| 608 | $reached_all_thresholds &= ($prop >= $thresholds[$i]); |
| 609 | } |
| 610 | last if $reached_all_thresholds; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 611 | |
| 612 | # Print function results |
| 613 | my $fn_CC = $fn_totals{$fn_name}; |
| 614 | print_CC($fn_CC, $fn_CC_col_widths); |
| 615 | print(" $fn_name\n"); |
| 616 | |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 617 | # Update the threshold counts |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 618 | my $filename = $fn_name; |
daywalker | d722c20 | 2002-05-01 21:52:05 +0000 | [diff] [blame] | 619 | $filename =~ s/:.+$//; # remove function name |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 620 | $threshold_files->{$filename} = 1; |
njn | bff8876 | 2002-05-13 20:27:54 +0000 | [diff] [blame] | 621 | foreach my $i (0 .. scalar @sort_order - 1) { |
| 622 | $curr_totals[$i] += $fn_CC->[$sort_order[$i]] |
| 623 | if (defined $fn_CC->[$sort_order[$i]]); |
| 624 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 625 | } |
| 626 | print("\n"); |
| 627 | |
| 628 | return $threshold_files; |
| 629 | } |
| 630 | |
| 631 | #----------------------------------------------------------------------------- |
| 632 | # Annotate selected files |
| 633 | #----------------------------------------------------------------------------- |
| 634 | |
| 635 | # Issue a warning that the source file is more recent than the input file. |
| 636 | sub warning_on_src_more_recent_than_inputfile ($) |
| 637 | { |
| 638 | my $src_file = $_[0]; |
| 639 | |
| 640 | my $warning = <<END |
| 641 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 642 | @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ |
| 643 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 644 | @ Source file '$src_file' is more recent than input file '$input_file'. |
| 645 | @ Annotations may not be correct. |
| 646 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 647 | |
| 648 | END |
| 649 | ; |
| 650 | print($warning); |
| 651 | } |
| 652 | |
| 653 | # If there is information about lines not in the file, issue a warning |
| 654 | # explaining possible causes. |
| 655 | sub warning_on_nonexistent_lines ($$$) |
| 656 | { |
| 657 | my ($src_more_recent_than_inputfile, $src_file, $excess_line_nums) = @_; |
| 658 | my $cause_and_solution; |
| 659 | |
| 660 | if ($src_more_recent_than_inputfile) { |
| 661 | $cause_and_solution = <<END |
| 662 | @@ cause: '$src_file' has changed since information was gathered. |
| 663 | @@ If so, a warning will have already been issued about this. |
| 664 | @@ solution: Recompile program and rerun under "valgrind --cachesim=yes" to |
| 665 | @@ gather new information. |
| 666 | END |
| 667 | # We suppress warnings about .h files |
| 668 | } elsif ($src_file =~ /\.h$/) { |
| 669 | $cause_and_solution = <<END |
| 670 | @@ cause: bug in the Valgrind's debug info reader that screws up with .h |
| 671 | @@ files sometimes |
| 672 | @@ solution: none, sorry |
| 673 | END |
| 674 | } else { |
| 675 | $cause_and_solution = <<END |
| 676 | @@ cause: not sure, sorry |
| 677 | END |
| 678 | } |
| 679 | |
| 680 | my $warning = <<END |
| 681 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 682 | @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ |
| 683 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 684 | @@ |
| 685 | @@ Information recorded about lines past the end of '$src_file'. |
| 686 | @@ |
| 687 | @@ Probable cause and solution: |
| 688 | $cause_and_solution@@ |
| 689 | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 690 | END |
| 691 | ; |
| 692 | print($warning); |
| 693 | } |
| 694 | |
| 695 | sub annotate_ann_files($) |
| 696 | { |
| 697 | my ($threshold_files) = @_; |
| 698 | |
| 699 | my %all_ann_files; |
| 700 | my @unfound_auto_annotate_files; |
| 701 | my $printed_totals_CC = []; |
| 702 | |
| 703 | # If auto-annotating, add interesting files (but not "???") |
| 704 | if ($auto_annotate) { |
| 705 | delete $threshold_files->{"???"}; |
| 706 | %all_ann_files = (%user_ann_files, %$threshold_files) |
| 707 | } else { |
| 708 | %all_ann_files = %user_ann_files; |
| 709 | } |
| 710 | |
| 711 | # Track if we did any annotations. |
| 712 | my $did_annotations = 0; |
| 713 | |
| 714 | LOOP: |
| 715 | foreach my $src_file (keys %all_ann_files) { |
| 716 | |
| 717 | my $opened_file = ""; |
| 718 | my $full_file_name = ""; |
| 719 | foreach my $include_dir (@include_dirs) { |
| 720 | my $try_name = $include_dir . $src_file; |
| 721 | if (open(INPUTFILE, "< $try_name")) { |
| 722 | $opened_file = $try_name; |
| 723 | $full_file_name = ($include_dir eq "" |
| 724 | ? $src_file |
| 725 | : "$include_dir + $src_file"); |
| 726 | last; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | if (not $opened_file) { |
| 731 | # Failed to open the file. If chosen on the command line, die. |
| 732 | # If arose from auto-annotation, print a little message. |
| 733 | if (defined $user_ann_files{$src_file}) { |
| 734 | die("File $src_file not opened in any of: @include_dirs\n"); |
| 735 | |
| 736 | } else { |
| 737 | push(@unfound_auto_annotate_files, $src_file); |
| 738 | } |
| 739 | |
| 740 | } else { |
| 741 | # File header (distinguish between user- and auto-selected files). |
| 742 | print("$fancy"); |
| 743 | my $ann_type = |
| 744 | (defined $user_ann_files{$src_file} ? "User" : "Auto"); |
| 745 | print("-- $ann_type-annotated source: $full_file_name\n"); |
| 746 | print("$fancy"); |
| 747 | |
| 748 | # Get file's CCs |
| 749 | my $src_file_CCs = $all_ind_CCs{$src_file}; |
| 750 | if (!defined $src_file_CCs) { |
| 751 | print(" No information has been collected for $src_file\n\n"); |
| 752 | next LOOP; |
| 753 | } |
| 754 | |
| 755 | $did_annotations = 1; |
| 756 | |
| 757 | # Numeric, not lexicographic sort! |
| 758 | my @line_nums = sort {$a <=> $b} keys %$src_file_CCs; |
| 759 | |
| 760 | # If $src_file more recent than cachegrind.out, issue warning |
| 761 | my $src_more_recent_than_inputfile = 0; |
| 762 | if ((stat $opened_file)[9] > (stat $input_file)[9]) { |
| 763 | $src_more_recent_than_inputfile = 1; |
| 764 | warning_on_src_more_recent_than_inputfile($src_file); |
| 765 | } |
| 766 | |
| 767 | # Work out the size of each column for printing |
| 768 | my $CC_col_widths = compute_CC_col_widths(values %$src_file_CCs); |
| 769 | |
| 770 | # Events header |
| 771 | print_events($CC_col_widths); |
| 772 | print("\n\n"); |
| 773 | |
| 774 | # Shift out 0 if it's in the line numbers (from unknown entries, |
| 775 | # likely due to bugs in Valgrind's stabs debug info reader) |
| 776 | shift(@line_nums) if (0 == $line_nums[0]); |
| 777 | |
| 778 | # Finds interesting line ranges -- all lines with a CC, and all |
| 779 | # lines within $context lines of a line with a CC. |
| 780 | my $n = @line_nums; |
| 781 | my @pairs; |
| 782 | for (my $i = 0; $i < $n; $i++) { |
| 783 | push(@pairs, $line_nums[$i] - $context); # lower marker |
| 784 | while ($i < $n-1 && |
| 785 | $line_nums[$i] + 2*$context >= $line_nums[$i+1]) { |
| 786 | $i++; |
| 787 | } |
| 788 | push(@pairs, $line_nums[$i] + $context); # upper marker |
| 789 | } |
| 790 | |
| 791 | # Annotate chosen lines, tracking total counts of lines printed |
| 792 | $pairs[0] = 1 if ($pairs[0] < 1); |
| 793 | while (@pairs) { |
| 794 | my $low = shift @pairs; |
| 795 | my $high = shift @pairs; |
| 796 | while ($. < $low-1) { |
| 797 | my $tmp = <INPUTFILE>; |
| 798 | last unless (defined $tmp); # hack to detect EOF |
| 799 | } |
| 800 | my $src_line; |
| 801 | # Print line number, unless start of file |
| 802 | print("-- line $low " . '-' x 40 . "\n") if ($low != 1); |
| 803 | while (($. < $high) && ($src_line = <INPUTFILE>)) { |
| 804 | if (defined $line_nums[0] && $. == $line_nums[0]) { |
| 805 | print_CC($src_file_CCs->{$.}, $CC_col_widths); |
| 806 | add_array_a_to_b($src_file_CCs->{$.}, |
| 807 | $printed_totals_CC); |
| 808 | shift(@line_nums); |
| 809 | |
| 810 | } else { |
| 811 | print_CC( [], $CC_col_widths); |
| 812 | } |
| 813 | |
| 814 | print(" $src_line"); |
| 815 | } |
| 816 | # Print line number, unless EOF |
| 817 | if ($src_line) { |
| 818 | print("-- line $high " . '-' x 40 . "\n"); |
| 819 | } else { |
| 820 | last; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | # If there was info on lines past the end of the file... |
| 825 | if (@line_nums) { |
| 826 | foreach my $line_num (@line_nums) { |
| 827 | print_CC($src_file_CCs->{$line_num}, $CC_col_widths); |
| 828 | print(" <bogus line $line_num>\n"); |
| 829 | } |
| 830 | print("\n"); |
| 831 | warning_on_nonexistent_lines($src_more_recent_than_inputfile, |
| 832 | $src_file, \@line_nums); |
| 833 | } |
| 834 | print("\n"); |
| 835 | |
| 836 | # Print summary of counts attributed to file but not to any |
| 837 | # particular line (due to incomplete debug info). |
| 838 | if ($src_file_CCs->{0}) { |
| 839 | print_CC($src_file_CCs->{0}, $CC_col_widths); |
| 840 | print(" <counts for unidentified lines in $src_file>\n\n"); |
| 841 | } |
| 842 | |
| 843 | close(INPUTFILE); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | # Print list of unfound auto-annotate selected files. |
| 848 | if (@unfound_auto_annotate_files) { |
| 849 | print("$fancy"); |
| 850 | print("The following files chosen for auto-annotation could not be found:\n"); |
| 851 | print($fancy); |
| 852 | foreach my $f (@unfound_auto_annotate_files) { |
| 853 | print(" $f\n"); |
| 854 | } |
| 855 | print("\n"); |
| 856 | } |
| 857 | |
| 858 | # If we did any annotating, print what proportion of events were covered by |
| 859 | # annotated lines above. |
| 860 | if ($did_annotations) { |
| 861 | my $percent_printed_CC; |
| 862 | foreach (my $i = 0; $i < @$summary_CC; $i++) { |
| 863 | $percent_printed_CC->[$i] = |
| 864 | sprintf("%.0f", |
| 865 | $printed_totals_CC->[$i] / $summary_CC->[$i] * 100); |
| 866 | } |
| 867 | my $pp_CC_col_widths = compute_CC_col_widths($percent_printed_CC); |
| 868 | print($fancy); |
| 869 | print_events($pp_CC_col_widths); |
| 870 | print("\n"); |
| 871 | print($fancy); |
| 872 | print_CC($percent_printed_CC, $pp_CC_col_widths); |
| 873 | print(" percentage of events annotated\n\n"); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | #---------------------------------------------------------------------------- |
| 878 | # "main()" |
| 879 | #---------------------------------------------------------------------------- |
| 880 | process_cmd_line(); |
| 881 | read_input_file(); |
| 882 | print_options(); |
| 883 | my $threshold_files = print_summary_and_fn_totals(); |
| 884 | annotate_ann_files($threshold_files); |
| 885 | |