3 ##--------------------------------------------------------------------##
4 ##--- Massif's results printer ms_print.in ---##
5 ##--------------------------------------------------------------------##
7 # This file is part of Massif, a Valgrind tool for profiling memory
10 # Copyright (C) 2007-2007 Nicholas Nethercote
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.
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.
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
28 # The GNU General Public License is contained in the file COPYING.
33 #----------------------------------------------------------------------------
34 # Global variables, main data structures
35 #----------------------------------------------------------------------------
37 # Command line of profiled program.
40 # Time unit used in profile.
43 # Threshold dictating what percentage an entry must represent for us to
47 # Graph x and y dimensions.
52 my $input_file = undef;
55 my $tmp_file = "ms_print.tmp.$$";
58 my $version = "@VERSION@";
60 # Args passed, for printing.
65 usage: ms_print [options] massif-out-file
67 options for the user, with defaults in [ ], are:
68 -h --help show this message
69 --version show version
70 --threshold=<m.n> significance threshold, in percent [$threshold]
71 --x=<4..1000> graph width, in columns [72]
72 --y=<4..1000> graph height, in rows [20]
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.
81 # Used in various places of output.
83 my $fancy_nl = $fancy . "\n";
85 # Returns 0 if the denominator is 0.
89 return ($y ? $x / $y : 0);
92 #-----------------------------------------------------------------------------
93 # Argument and option handling
94 #-----------------------------------------------------------------------------
95 sub process_cmd_line()
99 # Grab a copy of the arguments, for printing later.
100 for my $arg (@ARGV) {
101 $ms_print_args .= " $arg"; # The arguments.
104 for my $arg (@ARGV) {
110 if ($arg =~ /^--version$/) {
111 die("ms_print-$version\n");
113 # --threshold=X (tolerates a trailing '%')
114 } elsif ($arg =~ /^--threshold=([\d\.]+)%?$/) {
116 ($1 >= 0 && $1 <= 100) or die($usage);
118 } elsif ($arg =~ /^--x=(\d+)$/) {
120 (4 <= $graph_x && $graph_x <= 1000) or die($usage);
122 } elsif ($arg =~ /^--y=(\d+)$/) {
124 (4 <= $graph_y && $graph_y <= 1000) or die($usage);
126 } else { # -h and --help fall under this case
130 # Not an option. Remember it as a filename.
135 # Must have chosen exactly one input file.
137 $input_file = $files[0];
143 #-----------------------------------------------------------------------------
144 # Reading the input file: auxiliary functions
145 #-----------------------------------------------------------------------------
147 # Gets the next line, stripping comments and skipping blanks.
148 # Returns undef at EOF.
151 while (my $line = <INPUTFILE>) {
152 $line =~ s/#.*$//; # remove comments
153 if ($line !~ /^\s*$/) {
154 return $line; # return $line if non-empty
157 return undef; # EOF: return undef
160 sub equals_num_line($$)
162 my ($line, $fieldname) = @_;
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");
170 sub is_significant_XPt($$$)
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
176 return $is_top_node || 0 == $threshold ||
177 ( $total_szB != 0 && $xpt_szB * 100 / $total_szB >= $threshold );
180 #-----------------------------------------------------------------------------
181 # Reading the input file: reading heap trees
182 #-----------------------------------------------------------------------------
184 # Forward declaration, because it's recursive.
185 sub read_heap_tree($$$$$);
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
190 sub read_heap_tree($$$$$)
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");
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);
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
210 "$this_prefix$arrow%05.2f%% (%sB)%s\n", $perc, commify($bytes),
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, "->",
225 $n_insig_children += $is_child_insignificant;
226 $total_insig_children_szB += $child_insig_bytes;
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");
241 # If this node has no children, print an extra (mostly) empty line.
242 if (0 == $n_children) {
243 print(TMPFILE "$this_prefix2\n");
252 #-----------------------------------------------------------------------------
253 # Reading the input file: main
254 #-----------------------------------------------------------------------------
258 my ($szB, $szB_scaled) = @_;
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):
268 # 102400 --> 100.0 KB
269 # 1024000 --> 0.977 MB
270 # 1048576 --> 1.000 MB
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); }
278 # Work out the units for the max value, measured in instructions.
283 # We repeat until the number is less than 1000.
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; }
296 return (max_label_2($nI, $nI_scaled), $unit);
299 # Work out the units for the max value, measured in bytes.
304 # We repeat until the number is less than 1000, but we divide by 1024 on
306 my $szB_scaled = $szB;
308 # 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.)
311 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; }
320 return (max_label_2($szB, $szB_scaled), $unit);
323 # Work out the units for the max value, measured in ms/s/h.
328 # We scale from millisecond to seconds to hours.
330 # XXX: this allows a number with 6 chars, eg. "3599.0 s"
331 my $szB_scaled = $szB;
333 if ($szB_scaled >= 1000) { $unit = "s"; $szB_scaled /= 1000; }
334 if ($szB_scaled >= 3600) { $unit = "h"; $szB_scaled /= 3600; }
336 return (max_label_2($szB, $szB_scaled), $unit);
339 # This prints four things:
340 # - the output header
342 # - the snapshot summaries (number, list of detailed ones)
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
350 sub read_input_file()
352 my $desc = ""; # Concatenated description lines.
353 my $peak_mem_total_szB = 0;
355 # Info about each snapshot.
356 my @snapshot_nums = ();
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.
363 #-------------------------------------------------------------------------
364 # Read start of input file.
365 #-------------------------------------------------------------------------
366 open(INPUTFILE, "< $input_file")
367 || die "Cannot open $input_file for reading\n";
369 # Read "desc:" lines.
371 while ($line = get_line()) {
372 if ($line =~ s/^desc://) {
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");
383 # Read "time_unit:" line.
385 ($line =~ /^time_unit:\s*(.*)$/) or
386 die("Line $.: missing 'time_unit' line\n");
389 #-------------------------------------------------------------------------
390 # Print snapshot list header to $tmp_file.
391 #-------------------------------------------------------------------------
392 open(TMPFILE, "> $tmp_file")
393 || die "Cannot open $tmp_file for reading\n";
395 my $time_column = sprintf("%14s", "time($time_unit)");
396 my $column_format = "%3s %14s %16s %16s %13s %12s\n";
399 sprintf($column_format
408 print(TMPFILE $header);
410 #-------------------------------------------------------------------------
411 # Read body of input file.
412 #-------------------------------------------------------------------------
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");
418 my $mem_heap_extra_B = equals_num_line(get_line(), "mem_heap_extra_B");
419 my $mem_stacks_B = equals_num_line(get_line(), "mem_stacks_B");
420 my $mem_total_B = $mem_heap_B + $mem_heap_extra_B + $mem_stacks_B;
421 my $heap_tree = equals_num_line(get_line(), "heap_tree");
423 # Print the snapshot data to $tmp_file.
424 printf(TMPFILE $column_format,
427 , commify($mem_total_B)
428 , commify($mem_heap_B)
429 , commify($mem_heap_extra_B)
430 , commify($mem_stacks_B)
433 # Remember the snapshot data.
434 push(@snapshot_nums, $snapshot_num);
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;
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") {
445 } elsif ($heap_tree =~ "(detailed|peak)") {
446 # If "peak", remember the number.
447 if ($heap_tree eq "peak") {
448 $peak_num = $snapshot_num;
450 # '1' means it's the top node of the tree.
451 read_heap_tree(1, "", "", "", $mem_total_B);
453 # Print the header, unless there are no more snapshots.
456 print(TMPFILE $header);
459 die("Line $.: expected 'empty' or '...' after 'heap_tree='\n");
466 #-------------------------------------------------------------------------
468 #-------------------------------------------------------------------------
470 print("Command: $cmd\n");
471 print("Massif arguments: $desc");
472 print("ms_print arguments:$ms_print_args\n");
476 #-------------------------------------------------------------------------
478 #-------------------------------------------------------------------------
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.
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;
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] = ' ';
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 full-char (eg. ':'), it must be >= y*K. For example, if
511 # K = 10 bytes, then the values 0, 4, 5, 9, 10, 14, 15, 19, 20, 24, 25,
512 # 29, 30 would be drawn like this (showing one per column):
516 # 30 | : 3 3 * 10 = 30
517 # 20 | ::::: 2 2 * 10 = 20
518 # 10 | ::::::::: 1 1 * 10 = 10
522 my $detailed_char = '@';
523 my $normal_char = ':';
525 # Work out how many bytes each row represents. If the peak size was 0,
526 # make it 1 so that the Y-axis covers a non-zero range of values.
527 # Likewise for end_time.
528 if (0 == $peak_mem_total_szB) { $peak_mem_total_szB = 1; }
529 if (0 == $end_time ) { $end_time = 1; }
530 my $K = $peak_mem_total_szB / $graph_y;
537 for (my $i = 0; $i < $n_snapshots; $i++) {
539 # Work out which column this snapshot belongs to.
541 my $x_pos_frac = ($times[$i] / ($end_time)) * $graph_x;
542 $x = int($x_pos_frac) + 1; # +1 due to Y-axis
543 # The final snapshot will spill over into the n+1th column, which
544 # doesn't get shown. So we fudge that one and pull it back a
545 # column, as if the end_time was actually end_time+epsilon.
546 if ($times[$i] == $end_time) {
547 ($x == $graph_x+1) or die;
551 # If there was a gap between the previous snapshot's column and this
552 # one, we draw a horizontal line in the gap (so long as it doesn't
553 # trash the x-axis). Without this, graphs with a few sparse
554 # snapshots look funny -- as if the memory usage is in temporary
556 if ($prev_y_max > 0) {
557 for (my $x2 = $prev_x + 1; $x2 < $x; $x2++) {
558 $graph[$x2][$prev_y_max] = $prev_char;
562 # Choose the column char.
564 if ($i == $peak_num) { $char = $peak_char; }
565 elsif ($is_detaileds[$i]) { $char = $detailed_char; }
566 else { $char = $normal_char; }
568 # Grow this snapshot bar from bottom to top.
570 for ($y = 1; $y <= $graph_y; $y++) {
571 if ($mem_total_Bs[$i] >= $y * $K) {
572 # Priority order for chars: peak > detailed > normal
573 my $should_draw_char =
574 (($char eq $peak_char)
576 ($char eq $detailed_char and
577 $graph[$x][$y] ne $peak_char
580 ($char eq $normal_char and
581 $graph[$x][$y] ne $peak_char and
582 $graph[$x][$y] ne $detailed_char
586 if ($should_draw_char) {
587 $graph[$x][$y] = $char;
592 $prev_y_max = $y_max;
596 #-------------------------------------------------------------------------
598 #-------------------------------------------------------------------------
599 my ($y_label, $y_unit) = B_max_label($peak_mem_total_szB);
600 my ($x_label, $x_unit);
601 if ($time_unit eq "i") { ($x_label, $x_unit) = i_max_label($end_time) }
602 elsif ($time_unit eq "ms") { ($x_label, $x_unit) = t_max_label($end_time) }
603 elsif ($time_unit eq "B") { ($x_label, $x_unit) = B_max_label($end_time) }
604 else { die "bad time_unit: $time_unit\n"; }
606 printf(" %2s\n", $y_unit);
607 for ($y = $graph_y; $y >= 0; $y--) {
608 if ($graph_y == $y) { # top row
610 } elsif (0 == $y) { # bottom row
612 } else { # anywhere else
616 # Axis and data for the row.
617 for ($x = 0; $x <= $graph_x; $x++) {
618 printf("%s", $graph[$x][$y]);
626 printf(" 0%s%5s\n", ' ' x ($graph_x-5), $x_label);
628 #-------------------------------------------------------------------------
629 # Print snapshot numbers.
630 #-------------------------------------------------------------------------
632 print("Number of snapshots: $n_snapshots\n");
633 print(" Detailed snapshots: [");
634 my $first_detailed = 1;
635 for (my $i = 0; $i < $n_snapshots; $i++) {
636 if ($is_detaileds[$i]) {
637 if ($first_detailed) {
643 if ($i == $peak_num) {
650 #-------------------------------------------------------------------------
651 # Print snapshots, from $tmp_file.
652 #-------------------------------------------------------------------------
653 open(TMPFILE, "< $tmp_file")
654 || die "Cannot open $tmp_file for reading\n";
656 while (my $line = <TMPFILE>) {
662 #-----------------------------------------------------------------------------
664 #-----------------------------------------------------------------------------
667 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
672 #----------------------------------------------------------------------------
674 #----------------------------------------------------------------------------
678 ##--------------------------------------------------------------------##
679 ##--- end ms_print.in ---##
680 ##--------------------------------------------------------------------##