njn | d5a8d24 | 2007-11-02 20:44:57 +0000 | [diff] [blame] | 1 | #! @PERL@ |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2 | |
| 3 | ##--------------------------------------------------------------------## |
| 4 | ##--- Massif's results printer ms_print.in ---## |
| 5 | ##--------------------------------------------------------------------## |
| 6 | |
| 7 | # This file is part of Massif, a Valgrind tool for profiling memory |
| 8 | # usage of programs. |
| 9 | # |
| 10 | # Copyright (C) 2007-2007 Nicholas Nethercote |
| 11 | # njn@valgrind.org |
| 12 | # |
| 13 | # This program is free software; you can redistribute it and/or |
| 14 | # modify it under the terms of the GNU General Public License as |
| 15 | # published by the Free Software Foundation; either version 2 of the |
| 16 | # License, or (at your option) any later version. |
| 17 | # |
| 18 | # This program is distributed in the hope that it will be useful, but |
| 19 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | # General Public License for more details. |
| 22 | # |
| 23 | # You should have received a copy of the GNU General Public License |
| 24 | # along with this program; if not, write to the Free Software |
| 25 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | # 02111-1307, USA. |
| 27 | # |
| 28 | # The GNU General Public License is contained in the file COPYING. |
| 29 | |
| 30 | use warnings; |
| 31 | use strict; |
| 32 | |
| 33 | #---------------------------------------------------------------------------- |
| 34 | # Global variables, main data structures |
| 35 | #---------------------------------------------------------------------------- |
| 36 | |
| 37 | # Command line of profiled program. |
| 38 | my $cmd; |
| 39 | |
| 40 | # Time unit used in profile. |
| 41 | my $time_unit; |
| 42 | |
| 43 | # Threshold dictating what percentage an entry must represent for us to |
| 44 | # bother showing it. |
| 45 | my $threshold = 1.0; |
| 46 | |
| 47 | # Graph x and y dimensions. |
| 48 | my $graph_x = 72; |
| 49 | my $graph_y = 20; |
| 50 | |
| 51 | # Input file name |
| 52 | my $input_file = undef; |
| 53 | |
| 54 | # Tmp file name. |
| 55 | my $tmp_file = "ms_print.tmp.$$"; |
| 56 | |
njn | d5a8d24 | 2007-11-02 20:44:57 +0000 | [diff] [blame] | 57 | # Version number. |
| 58 | my $version = "@VERSION@"; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 59 | |
| 60 | # Args passed, for printing. |
| 61 | my $ms_print_args; |
| 62 | |
| 63 | # Usage message. |
| 64 | my $usage = <<END |
| 65 | usage: ms_print [options] <file> |
| 66 | |
| 67 | options for the user, with defaults in [ ], are: |
| 68 | -h --help show this message |
| 69 | -v --version show version |
| 70 | --threshold=<n.n> significance threshold, in percent [$threshold] |
| 71 | --x=<n> graph width, in columns; min=4, max=1000 [72] |
| 72 | --y=<n> graph height, in rows; min=4, max=1000 [20] |
| 73 | |
| 74 | ms_print is Copyright (C) 2007-2007 Nicholas Nethercote. |
| 75 | and licensed under the GNU General Public License, version 2. |
| 76 | Bug reports, feedback, admiration, abuse, etc, to: njn\@valgrind.org. |
| 77 | |
| 78 | END |
| 79 | ; |
| 80 | |
| 81 | # Used in various places of output. |
| 82 | my $fancy = '-' x 80; |
| 83 | my $fancy_nl = $fancy . "\n"; |
| 84 | |
| 85 | # Returns 0 if the denominator is 0. |
| 86 | sub safe_div_0($$) |
| 87 | { |
| 88 | my ($x, $y) = @_; |
| 89 | return ($y ? $x / $y : 0); |
| 90 | } |
| 91 | |
| 92 | #----------------------------------------------------------------------------- |
| 93 | # Argument and option handling |
| 94 | #----------------------------------------------------------------------------- |
| 95 | sub process_cmd_line() |
| 96 | { |
| 97 | my @files; |
| 98 | |
| 99 | # Grab a copy of the arguments, for printing later. |
| 100 | for my $arg (@ARGV) { |
| 101 | $ms_print_args .= " $arg"; # The arguments. |
| 102 | } |
| 103 | |
| 104 | for my $arg (@ARGV) { |
| 105 | |
| 106 | # Option handling |
| 107 | if ($arg =~ /^-/) { |
| 108 | |
| 109 | # --version |
| 110 | if ($arg =~ /^-v$|^--version$/) { |
| 111 | die("ms_print-$version\n"); |
| 112 | |
| 113 | # --threshold=X (tolerates a trailing '%') |
| 114 | } elsif ($arg =~ /^--threshold=([\d\.]+)%?$/) { |
| 115 | $threshold = $1; |
| 116 | ($1 >= 0 && $1 <= 100) or die($usage); |
| 117 | |
| 118 | } elsif ($arg =~ /^--x=(\d+)$/) { |
| 119 | $graph_x = $1; |
| 120 | (4 <= $graph_x && $graph_x <= 1000) or die($usage); |
| 121 | |
| 122 | } elsif ($arg =~ /^--y=(\d+)$/) { |
| 123 | $graph_y = $1; |
| 124 | (4 <= $graph_y && $graph_y <= 1000) or die($usage); |
| 125 | |
| 126 | } else { # -h and --help fall under this case |
| 127 | die($usage); |
| 128 | } |
| 129 | } else { |
| 130 | # Not an option. Remember it as a filename. |
| 131 | push(@files, $arg); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | # Must have chosen exactly one input file. |
| 136 | if (scalar @files) { |
| 137 | $input_file = $files[0]; |
| 138 | } else { |
| 139 | die($usage); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #----------------------------------------------------------------------------- |
| 144 | # Reading the input file: auxiliary functions |
| 145 | #----------------------------------------------------------------------------- |
| 146 | |
| 147 | # Gets the next line, stripping comments and skipping blanks. |
| 148 | # Returns undef at EOF. |
| 149 | sub get_line() |
| 150 | { |
| 151 | while (my $line = <INPUTFILE>) { |
| 152 | $line =~ s/#.*$//; # remove comments |
| 153 | if ($line !~ /^\s*$/) { |
| 154 | return $line; # return $line if non-empty |
| 155 | } |
| 156 | } |
| 157 | return undef; # EOF: return undef |
| 158 | } |
| 159 | |
| 160 | sub equals_num_line($$) |
| 161 | { |
| 162 | my ($line, $fieldname) = @_; |
| 163 | defined($line) |
| 164 | or die("Line $.: expected \"$fieldname\" line, got end of file\n"); |
| 165 | $line =~ s/^$fieldname=(.*)\s*$// |
| 166 | or die("Line $.: expected \"$fieldname\" line, got:\n$line"); |
| 167 | return $1; |
| 168 | } |
| 169 | |
| 170 | sub is_significant_XPt($$$) |
| 171 | { |
| 172 | my ($is_top_node, $xpt_szB, $total_szB) = @_; |
| 173 | ($xpt_szB <= $total_szB) or die; |
| 174 | # Nb: we always consider the alloc-XPt significant, even if the size is |
| 175 | # zero. |
| 176 | return $is_top_node || 0 == $threshold || |
| 177 | ( $total_szB != 0 && $xpt_szB * 100 / $total_szB >= $threshold ); |
| 178 | } |
| 179 | |
| 180 | #----------------------------------------------------------------------------- |
| 181 | # Reading the input file: reading heap trees |
| 182 | #----------------------------------------------------------------------------- |
| 183 | |
| 184 | # Forward declaration, because it's recursive. |
| 185 | sub read_heap_tree($$$$$); |
| 186 | |
| 187 | # Return pair: if the tree was significant, both are zero. If it was |
| 188 | # insignificant, the first element is 1 and the second is the number of |
| 189 | # bytes. |
| 190 | sub read_heap_tree($$$$$) |
| 191 | { |
| 192 | # Read the line and determine if it is significant. |
| 193 | my ($is_top_node, $this_prefix, $child_midfix, $arrow, $mem_total_B) = @_; |
| 194 | my $line = get_line(); |
| 195 | (defined $line and $line =~ /^\s*n(\d+):\s*(\d+)(.*)$/) |
| 196 | or die("Line $.: expected a tree node line, got:\n$line\n"); |
| 197 | my $n_children = $1; |
| 198 | my $bytes = $2; |
| 199 | my $details = $3; |
| 200 | my $perc = safe_div_0(100 * $bytes, $mem_total_B); |
| 201 | # Nb: we always print the alloc-XPt, even if its size is zero. |
| 202 | my $is_significant = is_significant_XPt($is_top_node, $bytes, $mem_total_B); |
| 203 | |
| 204 | # We precede this node's line with "$this_prefix.$arrow". We precede |
| 205 | # any children of this node with "$this_prefix$child_midfix$arrow". |
| 206 | if ($is_significant) { |
| 207 | # Nb: $details might have '%' in it, so don't embed directly in the |
| 208 | # format string. |
| 209 | printf(TMPFILE |
| 210 | "$this_prefix$arrow%05.2f%% (%sB)%s\n", $perc, commify($bytes), |
| 211 | $details); |
| 212 | } |
| 213 | |
| 214 | # Now read all the children. |
| 215 | my $n_insig_children = 0; |
| 216 | my $total_insig_children_szB = 0; |
| 217 | my $this_prefix2 = $this_prefix . $child_midfix; |
| 218 | for (my $i = 0; $i < $n_children; $i++) { |
| 219 | # If child is the last sibling, the midfix is empty. |
| 220 | my $child_midfix2 = ( $i+1 == $n_children ? " " : "| " ); |
| 221 | my ($is_child_insignificant, $child_insig_bytes) = |
| 222 | # '0' means it's not the top node of the tree. |
| 223 | read_heap_tree(0, $this_prefix2, $child_midfix2, "->", |
| 224 | $mem_total_B); |
| 225 | $n_insig_children += $is_child_insignificant; |
| 226 | $total_insig_children_szB += $child_insig_bytes; |
| 227 | } |
| 228 | |
| 229 | if ($is_significant) { |
| 230 | # If this was significant but any children were insignificant, print |
| 231 | # the "in N places" line for them. |
| 232 | if ($n_insig_children > 0) { |
| 233 | $perc = safe_div_0(100 * $total_insig_children_szB, $mem_total_B); |
| 234 | printf(TMPFILE "%s->%05.2f%% (%sB) in %d+ places, all below " |
| 235 | . "ms_print's threshold (%05.2f%%)\n", |
| 236 | $this_prefix2, $perc, commify($total_insig_children_szB), |
| 237 | $n_insig_children, $threshold); |
| 238 | print(TMPFILE "$this_prefix2\n"); |
| 239 | } |
| 240 | |
| 241 | # If this node has no children, print an extra (mostly) empty line. |
| 242 | if (0 == $n_children) { |
| 243 | print(TMPFILE "$this_prefix2\n"); |
| 244 | } |
| 245 | return (0, 0); |
| 246 | |
| 247 | } else { |
| 248 | return (1, $bytes); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | #----------------------------------------------------------------------------- |
| 253 | # Reading the input file: main |
| 254 | #----------------------------------------------------------------------------- |
| 255 | |
| 256 | sub max_label_2($$) |
| 257 | { |
| 258 | my ($szB, $szB_scaled) = @_; |
| 259 | |
| 260 | # For the label, if $szB is 999B or below, we print it as an integer. |
| 261 | # Otherwise, we print it as a float with 5 characters (including the '.'). |
| 262 | # Examples (for bytes): |
| 263 | # 1 --> 1 B |
| 264 | # 999 --> 999 B |
| 265 | # 1000 --> 0.977 KB |
| 266 | # 1024 --> 1.000 KB |
| 267 | # 10240 --> 10.00 KB |
| 268 | # 102400 --> 100.0 KB |
| 269 | # 1024000 --> 0.977 MB |
| 270 | # 1048576 --> 1.000 MB |
| 271 | # |
| 272 | if ($szB < 1000) { return sprintf("%5d", $szB); } |
| 273 | elsif ($szB_scaled < 10) { return sprintf("%5.3f", $szB_scaled); } |
| 274 | elsif ($szB_scaled < 100) { return sprintf("%5.2f", $szB_scaled); } |
| 275 | else { return sprintf("%5.1f", $szB_scaled); } |
| 276 | } |
| 277 | |
| 278 | # Work out the units for the max value, measured in bytes. |
| 279 | sub B_max_label($) |
| 280 | { |
| 281 | my ($szB) = @_; |
| 282 | |
| 283 | # We repeat until the number is less than 1000, but we divide by 1024 on |
| 284 | # each scaling. |
| 285 | my $szB_scaled = $szB; |
| 286 | my $unit = "B"; |
| 287 | if ($szB_scaled >= 1000) { $unit = "KB"; $szB_scaled /= 1024; } |
| 288 | if ($szB_scaled >= 1000) { $unit = "MB"; $szB_scaled /= 1024; } |
| 289 | if ($szB_scaled >= 1000) { $unit = "GB"; $szB_scaled /= 1024; } |
| 290 | if ($szB_scaled >= 1000) { $unit = "TB"; $szB_scaled /= 1024; } |
| 291 | if ($szB_scaled >= 1000) { $unit = "PB"; $szB_scaled /= 1024; } |
| 292 | if ($szB_scaled >= 1000) { $unit = "EB"; $szB_scaled /= 1024; } |
| 293 | if ($szB_scaled >= 1000) { $unit = "ZB"; $szB_scaled /= 1024; } |
| 294 | if ($szB_scaled >= 1000) { $unit = "YB"; $szB_scaled /= 1024; } |
| 295 | |
| 296 | return (max_label_2($szB, $szB_scaled), $unit); |
| 297 | } |
| 298 | |
| 299 | # Work out the units for the max value, measured in ms/s/h. |
| 300 | sub t_max_label($) |
| 301 | { |
| 302 | my ($szB) = @_; |
| 303 | |
| 304 | # We scale from millisecond to seconds to hours. |
| 305 | # |
| 306 | # XXX: this allows a number with 6 chars, eg. "3599.0 s" |
| 307 | my $szB_scaled = $szB; |
| 308 | my $unit = "ms"; |
| 309 | if ($szB_scaled >= 1000) { $unit = "s"; $szB_scaled /= 1000; } |
| 310 | if ($szB_scaled >= 3600) { $unit = "h"; $szB_scaled /= 3600; } |
| 311 | |
| 312 | return (max_label_2($szB, $szB_scaled), $unit); |
| 313 | } |
| 314 | |
| 315 | # This prints four things: |
| 316 | # - the output header |
| 317 | # - the graph |
| 318 | # - the snapshot summaries (number, list of detailed ones) |
| 319 | # - the snapshots |
| 320 | # |
| 321 | # The first three parts can't be printed until we've read the whole input file; |
| 322 | # but the fourth part is much easier to print while we're reading the file. So |
| 323 | # we print the fourth part to a tmp file, and then dump the tmp file at the |
| 324 | # end. |
| 325 | # |
| 326 | sub read_input_file() |
| 327 | { |
| 328 | my $desc = ""; # Concatenated description lines. |
| 329 | my $peak_mem_total_szB = 0; |
| 330 | |
| 331 | # Info about each snapshot. |
| 332 | my @snapshot_nums = (); |
| 333 | my @times = (); |
| 334 | my @mem_total_Bs = (); |
| 335 | my @is_detaileds = (); |
| 336 | my $peak_num = -1; # An initial value that will be ok if no peak |
| 337 | # entry is in the file. |
| 338 | |
| 339 | #------------------------------------------------------------------------- |
| 340 | # Read start of input file. |
| 341 | #------------------------------------------------------------------------- |
| 342 | open(INPUTFILE, "< $input_file") |
| 343 | || die "Cannot open $input_file for reading\n"; |
| 344 | |
| 345 | # Read "desc:" lines. |
| 346 | my $line; |
| 347 | while ($line = get_line()) { |
| 348 | if ($line =~ s/^desc://) { |
| 349 | $desc .= $line; |
| 350 | } else { |
| 351 | last; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above). |
| 356 | ($line =~ /^cmd:\s*(.*)$/) or die("Line $.: missing 'cmd' line\n"); |
| 357 | $cmd = $1; |
| 358 | |
| 359 | # Read "time_unit:" line. |
| 360 | $line = get_line(); |
| 361 | ($line =~ /^time_unit:\s*(.*)$/) or |
| 362 | die("Line $.: missing 'time_unit' line\n"); |
| 363 | $time_unit = $1; |
| 364 | |
| 365 | #------------------------------------------------------------------------- |
| 366 | # Print snapshot list header to $tmp_file. |
| 367 | #------------------------------------------------------------------------- |
| 368 | open(TMPFILE, "> $tmp_file") |
| 369 | || die "Cannot open $tmp_file for reading\n"; |
| 370 | |
| 371 | my $time_column = sprintf("%14s", "time($time_unit)"); |
| 372 | my $column_format = "%3s %14s %16s %16s %13s %12s\n"; |
| 373 | my $header = |
| 374 | $fancy_nl . |
| 375 | sprintf($column_format |
| 376 | , "n" |
| 377 | , $time_column |
| 378 | , "total(B)" |
| 379 | , "useful-heap(B)" |
| 380 | , "admin-heap(B)" |
| 381 | , "stacks(B)" |
| 382 | ) . |
| 383 | $fancy_nl; |
| 384 | print(TMPFILE $header); |
| 385 | |
| 386 | #------------------------------------------------------------------------- |
| 387 | # Read body of input file. |
| 388 | #------------------------------------------------------------------------- |
| 389 | $line = get_line(); |
| 390 | while (defined $line) { |
| 391 | my $snapshot_num = equals_num_line($line, "snapshot"); |
| 392 | my $time = equals_num_line(get_line(), "time"); |
| 393 | my $mem_heap_B = equals_num_line(get_line(), "mem_heap_B"); |
| 394 | my $mem_heap_admin_B = equals_num_line(get_line(), "mem_heap_admin_B"); |
| 395 | my $mem_stacks_B = equals_num_line(get_line(), "mem_stacks_B"); |
| 396 | my $mem_total_B = $mem_heap_B + $mem_heap_admin_B + $mem_stacks_B; |
| 397 | my $heap_tree = equals_num_line(get_line(), "heap_tree"); |
| 398 | |
| 399 | # Print the snapshot data to $tmp_file. |
| 400 | printf(TMPFILE $column_format, |
| 401 | , $snapshot_num |
| 402 | , commify($time) |
| 403 | , commify($mem_total_B) |
| 404 | , commify($mem_heap_B) |
| 405 | , commify($mem_heap_admin_B) |
| 406 | , commify($mem_stacks_B) |
| 407 | ); |
| 408 | |
| 409 | # Remember the snapshot data. |
| 410 | push(@snapshot_nums, $snapshot_num); |
| 411 | push(@times, $time); |
| 412 | push(@mem_total_Bs, $mem_total_B); |
| 413 | push(@is_detaileds, ( $heap_tree eq "empty" ? 0 : 1 )); |
| 414 | $peak_mem_total_szB = $mem_total_B |
| 415 | if $mem_total_B > $peak_mem_total_szB; |
| 416 | |
| 417 | # Read the heap tree, and if it's detailed, print it and a subsequent |
| 418 | # snapshot list header to $tmp_file. |
| 419 | if ($heap_tree eq "empty") { |
| 420 | $line = get_line(); |
| 421 | } elsif ($heap_tree =~ "(detailed|peak)") { |
| 422 | # If "peak", remember the number. |
| 423 | if ($heap_tree eq "peak") { |
| 424 | $peak_num = $snapshot_num; |
| 425 | } |
| 426 | # '1' means it's the top node of the tree. |
| 427 | read_heap_tree(1, "", "", "", $mem_total_B); |
| 428 | |
| 429 | # Print the header, unless there are no more snapshots. |
| 430 | $line = get_line(); |
| 431 | if (defined $line) { |
| 432 | print(TMPFILE $header); |
| 433 | } |
| 434 | } else { |
| 435 | die("Line $.: expected 'empty' or '...' after 'heap_tree='\n"); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | close(INPUTFILE); |
| 440 | close(TMPFILE); |
| 441 | |
| 442 | #------------------------------------------------------------------------- |
| 443 | # Print header. |
| 444 | #------------------------------------------------------------------------- |
| 445 | print($fancy_nl); |
| 446 | print("Command: $cmd\n"); |
| 447 | print("Massif arguments: $desc"); |
| 448 | print("ms_print arguments:$ms_print_args\n"); |
| 449 | print($fancy_nl); |
| 450 | print("\n\n"); |
| 451 | |
| 452 | #------------------------------------------------------------------------- |
| 453 | # Setup for graph. |
| 454 | #------------------------------------------------------------------------- |
| 455 | # The ASCII graph. |
| 456 | # Row 0 ([0..graph_x][0]) is the X-axis. |
| 457 | # Column 0 ([0][0..graph_y]) is the Y-axis. |
| 458 | # The rest ([1][1]..[graph_x][graph_y]) is the usable graph area. |
| 459 | my @graph; |
| 460 | my $x; |
| 461 | my $y; |
| 462 | |
| 463 | my $n_snapshots = scalar(@snapshot_nums); |
| 464 | ($n_snapshots > 0) or die; |
| 465 | my $end_time = $times[$n_snapshots-1]; |
| 466 | ($end_time >= 0) or die; |
| 467 | |
| 468 | # Setup graph[][]. |
| 469 | $graph[0][0] = '+'; # axes join point |
| 470 | for ($x = 1; $x <= $graph_x; $x++) { $graph[$x][0] = '-'; } # X-axis |
| 471 | for ($y = 1; $y <= $graph_y; $y++) { $graph[0][$y] = '|'; } # Y-axis |
| 472 | $graph[$graph_x][0] = '>'; # X-axis arrow |
| 473 | $graph[0][$graph_y] = '^'; # Y-axis arrow |
| 474 | for ($x = 1; $x <= $graph_x; $x++) { # usable area |
| 475 | for ($y = 1; $y <= $graph_y; $y++) { |
| 476 | $graph[$x][$y] = ' '; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | #------------------------------------------------------------------------- |
| 481 | # Write snapshot bars into graph[][]. |
| 482 | #------------------------------------------------------------------------- |
| 483 | # Each row represents K bytes, which is 1/graph_y of the peak size |
| 484 | # (and K can be non-integral). When drawing the column for a snapshot, |
| 485 | # in order to fill the slot in row y (where the first row drawn on is |
| 486 | # row 1) with a half-char (eg. '.'), it must be >= (y - 1/2)*K. In |
| 487 | # order to fill a row/column spot with a full-char (eg. ':'), it must be |
| 488 | # >= y*K. For example, if K = 10 bytes, then the values 0, 4, 5, 9, 10, |
| 489 | # 14, 15, 19, 20, 24, 25, 29, 30 would be drawn like this (showing one |
| 490 | # per column): |
| 491 | # |
| 492 | # y (y - 1/2) * K y * K |
| 493 | # - ------------- ----------- |
| 494 | # 30 | ..: 3 (3 - 1/2) * 10 = 25 3 * 10 = 30 |
| 495 | # 20 | ..::::: 2 (2 - 1/2) * 10 = 15 2 * 10 = 20 |
| 496 | # 10 | ..::::::::: 1 (1 - 1/2) * 10 = 5 1 * 10 = 10 |
| 497 | # 0 +------------- |
| 498 | |
| 499 | my $peak_full_char = '#'; |
| 500 | my $detailed_full_char = '@'; |
| 501 | my $normal_full_char = ':'; |
| 502 | my $half_char = '.'; |
| 503 | |
| 504 | # Work out how many bytes each row represents. If the peak size was 0, |
| 505 | # make it 1 so that the Y-axis covers a non-zero range of values. |
| 506 | # Likewise for end_time. |
| 507 | if (0 == $peak_mem_total_szB) { $peak_mem_total_szB = 1; } |
| 508 | if (0 == $end_time ) { $end_time = 1; } |
| 509 | my $K = $peak_mem_total_szB / $graph_y; |
| 510 | |
| 511 | # If we leave end_time as is, the final snapshot will spill over past |
| 512 | # the last column. So we add a small epsilon to it to prevent this from |
| 513 | # happening. |
| 514 | $end_time += 0.001; |
| 515 | |
| 516 | for (my $i = 0; $i < $n_snapshots; $i++) { |
| 517 | |
| 518 | # Work out which column this snapshot belongs to. |
| 519 | my $x_pos_frac = ($times[$i] / ($end_time)) * $graph_x; |
| 520 | $x = int($x_pos_frac) + 1; # +1 due to Y-axis |
| 521 | # The final snapshot will spill over into the n+1th column, which |
| 522 | # doesn't get shown. So we fudge that one and pull it back a |
| 523 | # column, as if the end_time was actually end_time+epsilon. |
| 524 | if ($times[$i] == $end_time) { |
| 525 | ($x == $graph_x+1) or die; |
| 526 | $x = $graph_x; |
| 527 | } |
| 528 | |
| 529 | # Draw the column if: |
| 530 | # - it's the peak column, or |
| 531 | # - it's a detailed column, and we won't overwrite the peak column, or |
| 532 | # - it's a normal column, and we won't overwrite the peak column or a |
| 533 | # detailed column. |
| 534 | my $should_draw_column = |
| 535 | (($i == $peak_num) or |
| 536 | ($is_detaileds[$i] and $graph[$x][0] ne $peak_full_char) or |
| 537 | ($graph[$x][0] ne $peak_full_char and |
| 538 | $graph[$x][0] ne $detailed_full_char)); |
| 539 | |
| 540 | if ($should_draw_column) { |
| 541 | # If it's detailed, mark the X-axis. Also choose the full-slot |
| 542 | # char. |
| 543 | my $full_char; |
| 544 | if ($i == $peak_num) { |
| 545 | $full_char = $peak_full_char; |
| 546 | $graph[$x][0] = $full_char; |
| 547 | } elsif ($is_detaileds[$i]) { |
| 548 | $full_char = $detailed_full_char; |
| 549 | $graph[$x][0] = $full_char; |
| 550 | } else { |
| 551 | $full_char = $normal_full_char; |
| 552 | } |
| 553 | # Grow this snapshot bar from bottom to top. |
| 554 | for ($y = 1; $y <= $graph_y; $y++) { |
| 555 | if ($mem_total_Bs[$i] >= ($y - 1/2) * $K) { |
| 556 | $graph[$x][$y] = $half_char; |
| 557 | } |
| 558 | if ($mem_total_Bs[$i] >= $y * $K) { |
| 559 | $graph[$x][$y] = $full_char; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | #------------------------------------------------------------------------- |
| 566 | # Print graph[][]. |
| 567 | #------------------------------------------------------------------------- |
| 568 | my ($y_label, $y_unit) = B_max_label($peak_mem_total_szB); |
| 569 | my ($x_label, $x_unit) = ( $time_unit eq "ms" |
| 570 | ? t_max_label($end_time) |
| 571 | : B_max_label($end_time) ); |
| 572 | |
| 573 | printf(" %2s\n", $y_unit); |
| 574 | for ($y = $graph_y; $y >= 0; $y--) { |
| 575 | if ($graph_y == $y) { # top row |
| 576 | print($y_label); |
| 577 | } elsif (0 == $y) { # bottom row |
| 578 | print(" 0 "); |
| 579 | } else { # anywhere else |
| 580 | print(" "); |
| 581 | } |
| 582 | |
| 583 | # Axis and data for the row. |
| 584 | for ($x = 0; $x <= $graph_x; $x++) { |
| 585 | printf("%s", $graph[$x][$y]); |
| 586 | } |
| 587 | if (0 == $y) { |
| 588 | print("$x_unit\n"); |
| 589 | } else { |
| 590 | print("\n"); |
| 591 | } |
| 592 | } |
| 593 | printf(" 0%s%5s\n", ' ' x ($graph_x-5), $x_label); |
| 594 | |
| 595 | #------------------------------------------------------------------------- |
| 596 | # Print snapshot numbers. |
| 597 | #------------------------------------------------------------------------- |
| 598 | print("\n"); |
| 599 | print("Number of snapshots: $n_snapshots\n"); |
| 600 | print(" Detailed snapshots: ["); |
| 601 | my $first_detailed = 1; |
| 602 | for (my $i = 0; $i < $n_snapshots; $i++) { |
| 603 | if ($is_detaileds[$i]) { |
| 604 | if ($first_detailed) { |
| 605 | printf("$i"); |
| 606 | $first_detailed = 0; |
| 607 | } else { |
| 608 | printf(", $i"); |
| 609 | } |
| 610 | if ($i == $peak_num) { |
| 611 | print(" (peak)"); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | print("]\n"); |
| 616 | |
| 617 | #------------------------------------------------------------------------- |
| 618 | # Print snapshots, from $tmp_file. |
| 619 | #------------------------------------------------------------------------- |
| 620 | open(TMPFILE, "< $tmp_file") |
| 621 | || die "Cannot open $tmp_file for reading\n"; |
| 622 | |
| 623 | while (my $line = <TMPFILE>) { |
| 624 | print($line); |
| 625 | } |
| 626 | unlink($tmp_file); |
| 627 | } |
| 628 | |
| 629 | #----------------------------------------------------------------------------- |
| 630 | # Misc functions |
| 631 | #----------------------------------------------------------------------------- |
| 632 | sub commify ($) { |
| 633 | my ($val) = @_; |
| 634 | 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/); |
| 635 | return $val; |
| 636 | } |
| 637 | |
| 638 | |
| 639 | #---------------------------------------------------------------------------- |
| 640 | # "main()" |
| 641 | #---------------------------------------------------------------------------- |
| 642 | process_cmd_line(); |
| 643 | read_input_file(); |
| 644 | |
| 645 | ##--------------------------------------------------------------------## |
| 646 | ##--- end ms_print.in ---## |
| 647 | ##--------------------------------------------------------------------## |
| 648 | |
| 649 | |