blob: 2c0cad23a9e2c59dcb0d06342a1101c19b355d0d [file] [log] [blame]
njnd5a8d242007-11-02 20:44:57 +00001#! @PERL@
njn734b8052007-11-01 04:40:37 +00002
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
30use warnings;
31use strict;
32
33#----------------------------------------------------------------------------
34# Global variables, main data structures
35#----------------------------------------------------------------------------
36
37# Command line of profiled program.
38my $cmd;
39
40# Time unit used in profile.
41my $time_unit;
42
43# Threshold dictating what percentage an entry must represent for us to
44# bother showing it.
45my $threshold = 1.0;
46
47# Graph x and y dimensions.
48my $graph_x = 72;
49my $graph_y = 20;
50
51# Input file name
52my $input_file = undef;
53
54# Tmp file name.
55my $tmp_file = "ms_print.tmp.$$";
56
njnd5a8d242007-11-02 20:44:57 +000057# Version number.
58my $version = "@VERSION@";
njn734b8052007-11-01 04:40:37 +000059
60# Args passed, for printing.
61my $ms_print_args;
62
63# Usage message.
64my $usage = <<END
65usage: 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
78END
79;
80
81# Used in various places of output.
82my $fancy = '-' x 80;
83my $fancy_nl = $fancy . "\n";
84
85# Returns 0 if the denominator is 0.
86sub safe_div_0($$)
87{
88 my ($x, $y) = @_;
89 return ($y ? $x / $y : 0);
90}
91
92#-----------------------------------------------------------------------------
93# Argument and option handling
94#-----------------------------------------------------------------------------
95sub 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.
149sub 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
160sub 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
170sub 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.
185sub 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.
190sub 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
256sub 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
njn1a2741a2007-11-26 21:59:04 +0000278# Work out the units for the max value, measured in instructions.
279sub i_max_label($)
280{
281 my ($nI) = @_;
282
283 # We repeat until the number is less than 1000.
284 my $nI_scaled = $nI;
285 my $unit = "i";
286 # Nb: 'k' is the "kilo" (1000) prefix.
287 if ($nI_scaled >= 1000) { $unit = "ki"; $nI_scaled /= 1024; }
288 if ($nI_scaled >= 1000) { $unit = "Mi"; $nI_scaled /= 1024; }
289 if ($nI_scaled >= 1000) { $unit = "Gi"; $nI_scaled /= 1024; }
290 if ($nI_scaled >= 1000) { $unit = "Ti"; $nI_scaled /= 1024; }
291 if ($nI_scaled >= 1000) { $unit = "Pi"; $nI_scaled /= 1024; }
292 if ($nI_scaled >= 1000) { $unit = "Ei"; $nI_scaled /= 1024; }
293 if ($nI_scaled >= 1000) { $unit = "Zi"; $nI_scaled /= 1024; }
294 if ($nI_scaled >= 1000) { $unit = "Yi"; $nI_scaled /= 1024; }
295
296 return (max_label_2($nI, $nI_scaled), $unit);
297}
298
njn734b8052007-11-01 04:40:37 +0000299# Work out the units for the max value, measured in bytes.
300sub B_max_label($)
301{
302 my ($szB) = @_;
303
304 # We repeat until the number is less than 1000, but we divide by 1024 on
305 # each scaling.
306 my $szB_scaled = $szB;
307 my $unit = "B";
njn1a2741a2007-11-26 21:59:04 +0000308 # Nb: 'K' or 'k' are acceptable as the "binary kilo" (1024) prefix.
309 # (Strictly speaking, should use "KiB" (kibibyte), "MiB" (mebibyte), etc,
310 # but they're not in common use.)
njn734b8052007-11-01 04:40:37 +0000311 if ($szB_scaled >= 1000) { $unit = "KB"; $szB_scaled /= 1024; }
312 if ($szB_scaled >= 1000) { $unit = "MB"; $szB_scaled /= 1024; }
313 if ($szB_scaled >= 1000) { $unit = "GB"; $szB_scaled /= 1024; }
314 if ($szB_scaled >= 1000) { $unit = "TB"; $szB_scaled /= 1024; }
315 if ($szB_scaled >= 1000) { $unit = "PB"; $szB_scaled /= 1024; }
316 if ($szB_scaled >= 1000) { $unit = "EB"; $szB_scaled /= 1024; }
317 if ($szB_scaled >= 1000) { $unit = "ZB"; $szB_scaled /= 1024; }
318 if ($szB_scaled >= 1000) { $unit = "YB"; $szB_scaled /= 1024; }
319
320 return (max_label_2($szB, $szB_scaled), $unit);
321}
322
323# Work out the units for the max value, measured in ms/s/h.
324sub t_max_label($)
325{
326 my ($szB) = @_;
327
328 # We scale from millisecond to seconds to hours.
329 #
330 # XXX: this allows a number with 6 chars, eg. "3599.0 s"
331 my $szB_scaled = $szB;
332 my $unit = "ms";
333 if ($szB_scaled >= 1000) { $unit = "s"; $szB_scaled /= 1000; }
334 if ($szB_scaled >= 3600) { $unit = "h"; $szB_scaled /= 3600; }
335
336 return (max_label_2($szB, $szB_scaled), $unit);
337}
338
339# This prints four things:
340# - the output header
341# - the graph
342# - the snapshot summaries (number, list of detailed ones)
343# - the snapshots
344#
345# The first three parts can't be printed until we've read the whole input file;
346# but the fourth part is much easier to print while we're reading the file. So
347# we print the fourth part to a tmp file, and then dump the tmp file at the
348# end.
349#
350sub read_input_file()
351{
352 my $desc = ""; # Concatenated description lines.
353 my $peak_mem_total_szB = 0;
354
355 # Info about each snapshot.
356 my @snapshot_nums = ();
357 my @times = ();
358 my @mem_total_Bs = ();
359 my @is_detaileds = ();
360 my $peak_num = -1; # An initial value that will be ok if no peak
361 # entry is in the file.
362
363 #-------------------------------------------------------------------------
364 # Read start of input file.
365 #-------------------------------------------------------------------------
366 open(INPUTFILE, "< $input_file")
367 || die "Cannot open $input_file for reading\n";
368
369 # Read "desc:" lines.
370 my $line;
371 while ($line = get_line()) {
372 if ($line =~ s/^desc://) {
373 $desc .= $line;
374 } else {
375 last;
376 }
377 }
378
379 # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above).
380 ($line =~ /^cmd:\s*(.*)$/) or die("Line $.: missing 'cmd' line\n");
381 $cmd = $1;
382
383 # Read "time_unit:" line.
384 $line = get_line();
385 ($line =~ /^time_unit:\s*(.*)$/) or
386 die("Line $.: missing 'time_unit' line\n");
387 $time_unit = $1;
388
389 #-------------------------------------------------------------------------
390 # Print snapshot list header to $tmp_file.
391 #-------------------------------------------------------------------------
392 open(TMPFILE, "> $tmp_file")
393 || die "Cannot open $tmp_file for reading\n";
394
395 my $time_column = sprintf("%14s", "time($time_unit)");
396 my $column_format = "%3s %14s %16s %16s %13s %12s\n";
397 my $header =
398 $fancy_nl .
399 sprintf($column_format
400 , "n"
401 , $time_column
402 , "total(B)"
403 , "useful-heap(B)"
njn32397c02007-11-10 04:08:08 +0000404 , "extra-heap(B)"
njn734b8052007-11-01 04:40:37 +0000405 , "stacks(B)"
406 ) .
407 $fancy_nl;
408 print(TMPFILE $header);
409
410 #-------------------------------------------------------------------------
411 # Read body of input file.
412 #-------------------------------------------------------------------------
413 $line = get_line();
414 while (defined $line) {
415 my $snapshot_num = equals_num_line($line, "snapshot");
416 my $time = equals_num_line(get_line(), "time");
417 my $mem_heap_B = equals_num_line(get_line(), "mem_heap_B");
njn32397c02007-11-10 04:08:08 +0000418 my $mem_heap_extra_B = equals_num_line(get_line(), "mem_heap_extra_B");
njn734b8052007-11-01 04:40:37 +0000419 my $mem_stacks_B = equals_num_line(get_line(), "mem_stacks_B");
njn32397c02007-11-10 04:08:08 +0000420 my $mem_total_B = $mem_heap_B + $mem_heap_extra_B + $mem_stacks_B;
njn734b8052007-11-01 04:40:37 +0000421 my $heap_tree = equals_num_line(get_line(), "heap_tree");
422
423 # Print the snapshot data to $tmp_file.
424 printf(TMPFILE $column_format,
425 , $snapshot_num
426 , commify($time)
427 , commify($mem_total_B)
428 , commify($mem_heap_B)
njn32397c02007-11-10 04:08:08 +0000429 , commify($mem_heap_extra_B)
njn734b8052007-11-01 04:40:37 +0000430 , commify($mem_stacks_B)
431 );
432
433 # Remember the snapshot data.
434 push(@snapshot_nums, $snapshot_num);
435 push(@times, $time);
436 push(@mem_total_Bs, $mem_total_B);
437 push(@is_detaileds, ( $heap_tree eq "empty" ? 0 : 1 ));
438 $peak_mem_total_szB = $mem_total_B
439 if $mem_total_B > $peak_mem_total_szB;
440
441 # Read the heap tree, and if it's detailed, print it and a subsequent
442 # snapshot list header to $tmp_file.
443 if ($heap_tree eq "empty") {
444 $line = get_line();
445 } elsif ($heap_tree =~ "(detailed|peak)") {
446 # If "peak", remember the number.
447 if ($heap_tree eq "peak") {
448 $peak_num = $snapshot_num;
449 }
450 # '1' means it's the top node of the tree.
451 read_heap_tree(1, "", "", "", $mem_total_B);
452
453 # Print the header, unless there are no more snapshots.
454 $line = get_line();
455 if (defined $line) {
456 print(TMPFILE $header);
457 }
458 } else {
459 die("Line $.: expected 'empty' or '...' after 'heap_tree='\n");
460 }
461 }
462
463 close(INPUTFILE);
464 close(TMPFILE);
465
466 #-------------------------------------------------------------------------
467 # Print header.
468 #-------------------------------------------------------------------------
469 print($fancy_nl);
470 print("Command: $cmd\n");
471 print("Massif arguments: $desc");
472 print("ms_print arguments:$ms_print_args\n");
473 print($fancy_nl);
474 print("\n\n");
475
476 #-------------------------------------------------------------------------
477 # Setup for graph.
478 #-------------------------------------------------------------------------
479 # The ASCII graph.
480 # Row 0 ([0..graph_x][0]) is the X-axis.
481 # Column 0 ([0][0..graph_y]) is the Y-axis.
482 # The rest ([1][1]..[graph_x][graph_y]) is the usable graph area.
483 my @graph;
484 my $x;
485 my $y;
486
487 my $n_snapshots = scalar(@snapshot_nums);
488 ($n_snapshots > 0) or die;
489 my $end_time = $times[$n_snapshots-1];
490 ($end_time >= 0) or die;
491
492 # Setup graph[][].
493 $graph[0][0] = '+'; # axes join point
494 for ($x = 1; $x <= $graph_x; $x++) { $graph[$x][0] = '-'; } # X-axis
495 for ($y = 1; $y <= $graph_y; $y++) { $graph[0][$y] = '|'; } # Y-axis
496 $graph[$graph_x][0] = '>'; # X-axis arrow
497 $graph[0][$graph_y] = '^'; # Y-axis arrow
498 for ($x = 1; $x <= $graph_x; $x++) { # usable area
499 for ($y = 1; $y <= $graph_y; $y++) {
500 $graph[$x][$y] = ' ';
501 }
502 }
503
504 #-------------------------------------------------------------------------
505 # Write snapshot bars into graph[][].
506 #-------------------------------------------------------------------------
507 # Each row represents K bytes, which is 1/graph_y of the peak size
508 # (and K can be non-integral). When drawing the column for a snapshot,
509 # in order to fill the slot in row y (where the first row drawn on is
510 # row 1) with a half-char (eg. '.'), it must be >= (y - 1/2)*K. In
511 # order to fill a row/column spot with a full-char (eg. ':'), it must be
512 # >= y*K. For example, if K = 10 bytes, then the values 0, 4, 5, 9, 10,
513 # 14, 15, 19, 20, 24, 25, 29, 30 would be drawn like this (showing one
514 # per column):
515 #
516 # y (y - 1/2) * K y * K
517 # - ------------- -----------
518 # 30 | ..: 3 (3 - 1/2) * 10 = 25 3 * 10 = 30
519 # 20 | ..::::: 2 (2 - 1/2) * 10 = 15 2 * 10 = 20
520 # 10 | ..::::::::: 1 (1 - 1/2) * 10 = 5 1 * 10 = 10
521 # 0 +-------------
522
523 my $peak_full_char = '#';
524 my $detailed_full_char = '@';
525 my $normal_full_char = ':';
njn15aeb152007-11-26 22:27:02 +0000526 my $peak_half_char = ',';
527 my $detailed_half_char = ',';
528 my $normal_half_char = '.';
njn734b8052007-11-01 04:40:37 +0000529
530 # Work out how many bytes each row represents. If the peak size was 0,
531 # make it 1 so that the Y-axis covers a non-zero range of values.
532 # Likewise for end_time.
533 if (0 == $peak_mem_total_szB) { $peak_mem_total_szB = 1; }
534 if (0 == $end_time ) { $end_time = 1; }
535 my $K = $peak_mem_total_szB / $graph_y;
536
njn734b8052007-11-01 04:40:37 +0000537 for (my $i = 0; $i < $n_snapshots; $i++) {
538
539 # Work out which column this snapshot belongs to.
540 my $x_pos_frac = ($times[$i] / ($end_time)) * $graph_x;
541 $x = int($x_pos_frac) + 1; # +1 due to Y-axis
542 # The final snapshot will spill over into the n+1th column, which
543 # doesn't get shown. So we fudge that one and pull it back a
544 # column, as if the end_time was actually end_time+epsilon.
545 if ($times[$i] == $end_time) {
546 ($x == $graph_x+1) or die;
547 $x = $graph_x;
548 }
549
550 # Draw the column if:
551 # - it's the peak column, or
552 # - it's a detailed column, and we won't overwrite the peak column, or
553 # - it's a normal column, and we won't overwrite the peak column or a
554 # detailed column.
555 my $should_draw_column =
556 (($i == $peak_num) or
njn1a2741a2007-11-26 21:59:04 +0000557 ($is_detaileds[$i] and $graph[$x][1] ne $peak_full_char) or
558 ($graph[$x][1] ne $peak_full_char and
559 $graph[$x][1] ne $detailed_full_char));
njn734b8052007-11-01 04:40:37 +0000560
561 if ($should_draw_column) {
562 # If it's detailed, mark the X-axis. Also choose the full-slot
563 # char.
njn15aeb152007-11-26 22:27:02 +0000564 my ($full_char, $half_char);
njn734b8052007-11-01 04:40:37 +0000565 if ($i == $peak_num) {
566 $full_char = $peak_full_char;
njn15aeb152007-11-26 22:27:02 +0000567 $half_char = $peak_half_char;
njn734b8052007-11-01 04:40:37 +0000568 } elsif ($is_detaileds[$i]) {
569 $full_char = $detailed_full_char;
njn15aeb152007-11-26 22:27:02 +0000570 $half_char = $detailed_half_char;
njn734b8052007-11-01 04:40:37 +0000571 } else {
572 $full_char = $normal_full_char;
njn15aeb152007-11-26 22:27:02 +0000573 $half_char = $normal_half_char;
njn734b8052007-11-01 04:40:37 +0000574 }
575 # Grow this snapshot bar from bottom to top.
576 for ($y = 1; $y <= $graph_y; $y++) {
577 if ($mem_total_Bs[$i] >= ($y - 1/2) * $K) {
578 $graph[$x][$y] = $half_char;
579 }
580 if ($mem_total_Bs[$i] >= $y * $K) {
581 $graph[$x][$y] = $full_char;
582 }
583 }
584 }
585 }
586
587 #-------------------------------------------------------------------------
588 # Print graph[][].
589 #-------------------------------------------------------------------------
590 my ($y_label, $y_unit) = B_max_label($peak_mem_total_szB);
njn1a2741a2007-11-26 21:59:04 +0000591 my ($x_label, $x_unit);
njn86a34be2008-01-29 21:33:25 +0000592 if ($time_unit eq "i") { ($x_label, $x_unit) = i_max_label($end_time) }
593 elsif ($time_unit eq "ms") { ($x_label, $x_unit) = t_max_label($end_time) }
594 elsif ($time_unit eq "B") { ($x_label, $x_unit) = B_max_label($end_time) }
595 else { die "bad time_unit: $time_unit\n"; }
njn734b8052007-11-01 04:40:37 +0000596
597 printf(" %2s\n", $y_unit);
598 for ($y = $graph_y; $y >= 0; $y--) {
599 if ($graph_y == $y) { # top row
600 print($y_label);
601 } elsif (0 == $y) { # bottom row
602 print(" 0 ");
603 } else { # anywhere else
604 print(" ");
605 }
606
607 # Axis and data for the row.
608 for ($x = 0; $x <= $graph_x; $x++) {
609 printf("%s", $graph[$x][$y]);
610 }
611 if (0 == $y) {
612 print("$x_unit\n");
613 } else {
614 print("\n");
615 }
616 }
617 printf(" 0%s%5s\n", ' ' x ($graph_x-5), $x_label);
618
619 #-------------------------------------------------------------------------
620 # Print snapshot numbers.
621 #-------------------------------------------------------------------------
622 print("\n");
623 print("Number of snapshots: $n_snapshots\n");
624 print(" Detailed snapshots: [");
625 my $first_detailed = 1;
626 for (my $i = 0; $i < $n_snapshots; $i++) {
627 if ($is_detaileds[$i]) {
628 if ($first_detailed) {
629 printf("$i");
630 $first_detailed = 0;
631 } else {
632 printf(", $i");
633 }
634 if ($i == $peak_num) {
635 print(" (peak)");
636 }
637 }
638 }
njn1a2741a2007-11-26 21:59:04 +0000639 print("]\n\n");
njn734b8052007-11-01 04:40:37 +0000640
641 #-------------------------------------------------------------------------
642 # Print snapshots, from $tmp_file.
643 #-------------------------------------------------------------------------
644 open(TMPFILE, "< $tmp_file")
645 || die "Cannot open $tmp_file for reading\n";
646
647 while (my $line = <TMPFILE>) {
648 print($line);
649 }
650 unlink($tmp_file);
651}
652
653#-----------------------------------------------------------------------------
654# Misc functions
655#-----------------------------------------------------------------------------
656sub commify ($) {
657 my ($val) = @_;
658 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
659 return $val;
660}
661
662
663#----------------------------------------------------------------------------
664# "main()"
665#----------------------------------------------------------------------------
666process_cmd_line();
667read_input_file();
668
669##--------------------------------------------------------------------##
670##--- end ms_print.in ---##
671##--------------------------------------------------------------------##
672
673