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-2013 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;
54 # Where to create tmp files. See also function VG_(tmpdir) in m_libcfile.c.
55 my $tmp_dir = $ENV{"TMPDIR"};
56 $tmp_dir = "@VG_TMPDIR@" if (! $tmp_dir);
57 $tmp_dir = "/tmp" if (! $tmp_dir);
60 my $tmp_file = "$tmp_dir/ms_print.tmp.$$";
63 my $version = "@VERSION@";
65 # Args passed, for printing.
70 usage: ms_print [options] massif-out-file
72 options for the user, with defaults in [ ], are:
73 -h --help show this message
74 --version show version
75 --threshold=<m.n> significance threshold, in percent [$threshold]
76 --x=<4..1000> graph width, in columns [72]
77 --y=<4..1000> graph height, in rows [20]
79 ms_print is Copyright (C) 2007-2013 Nicholas Nethercote.
80 and licensed under the GNU General Public License, version 2.
81 Bug reports, feedback, admiration, abuse, etc, to: njn\@valgrind.org.
86 # Used in various places of output.
88 my $fancy_nl = $fancy . "\n";
90 # Returns 0 if the denominator is 0.
94 return ($y ? $x / $y : 0);
97 #-----------------------------------------------------------------------------
98 # Argument and option handling
99 #-----------------------------------------------------------------------------
100 sub process_cmd_line()
104 # Grab a copy of the arguments, for printing later.
105 for my $arg (@ARGV) {
106 $ms_print_args .= " $arg"; # The arguments.
109 for my $arg (@ARGV) {
115 if ($arg =~ /^--version$/) {
116 die("ms_print-$version\n");
118 # --threshold=X (tolerates a trailing '%')
119 } elsif ($arg =~ /^--threshold=([\d\.]+)%?$/) {
121 ($1 >= 0 && $1 <= 100) or die($usage);
123 } elsif ($arg =~ /^--x=(\d+)$/) {
125 (4 <= $graph_x && $graph_x <= 1000) or die($usage);
127 } elsif ($arg =~ /^--y=(\d+)$/) {
129 (4 <= $graph_y && $graph_y <= 1000) or die($usage);
131 } else { # -h and --help fall under this case
135 # Not an option. Remember it as a filename.
140 # Must have chosen exactly one input file.
142 $input_file = $files[0];
148 #-----------------------------------------------------------------------------
149 # Reading the input file: auxiliary functions
150 #-----------------------------------------------------------------------------
152 # Gets the next line, stripping comments and skipping blanks.
153 # Returns undef at EOF.
156 while (my $line = <INPUTFILE>) {
157 $line =~ s/#.*$//; # remove comments
158 if ($line !~ /^\s*$/) {
159 return $line; # return $line if non-empty
162 return undef; # EOF: return undef
165 sub equals_num_line($$)
167 my ($line, $fieldname) = @_;
169 or die("Line $.: expected \"$fieldname\" line, got end of file\n");
170 $line =~ s/^$fieldname=(.*)\s*$//
171 or die("Line $.: expected \"$fieldname\" line, got:\n$line");
175 sub is_significant_XPt($$$)
177 my ($is_top_node, $xpt_szB, $total_szB) = @_;
178 ($xpt_szB <= $total_szB) or die;
179 # Nb: we always consider the alloc-XPt significant, even if the size is
181 return $is_top_node || 0 == $threshold ||
182 ( $total_szB != 0 && $xpt_szB * 100 / $total_szB >= $threshold );
185 #-----------------------------------------------------------------------------
186 # Reading the input file: reading heap trees
187 #-----------------------------------------------------------------------------
189 # Forward declaration, because it's recursive.
190 sub read_heap_tree($$$$$);
192 # Return pair: if the tree was significant, both are zero. If it was
193 # insignificant, the first element is 1 and the second is the number of
195 sub read_heap_tree($$$$$)
197 # Read the line and determine if it is significant.
198 my ($is_top_node, $this_prefix, $child_midfix, $arrow, $mem_total_B) = @_;
199 my $line = get_line();
200 (defined $line and $line =~ /^\s*n(\d+):\s*(\d+)(.*)$/)
201 or die("Line $.: expected a tree node line, got:\n$line\n");
205 my $perc = safe_div_0(100 * $bytes, $mem_total_B);
206 # Nb: we always print the alloc-XPt, even if its size is zero.
207 my $is_significant = is_significant_XPt($is_top_node, $bytes, $mem_total_B);
209 # We precede this node's line with "$this_prefix.$arrow". We precede
210 # any children of this node with "$this_prefix$child_midfix$arrow".
211 if ($is_significant) {
212 # Nb: $details might have '%' in it, so don't embed directly in the
215 "$this_prefix$arrow%05.2f%% (%sB)%s\n", $perc, commify($bytes),
219 # Now read all the children.
220 my $n_insig_children = 0;
221 my $total_insig_children_szB = 0;
222 my $this_prefix2 = $this_prefix . $child_midfix;
223 for (my $i = 0; $i < $n_children; $i++) {
224 # If child is the last sibling, the midfix is empty.
225 my $child_midfix2 = ( $i+1 == $n_children ? " " : "| " );
226 my ($is_child_insignificant, $child_insig_bytes) =
227 # '0' means it's not the top node of the tree.
228 read_heap_tree(0, $this_prefix2, $child_midfix2, "->",
230 $n_insig_children += $is_child_insignificant;
231 $total_insig_children_szB += $child_insig_bytes;
234 if ($is_significant) {
235 # If this was significant but any children were insignificant, print
236 # the "in N places" line for them.
237 if ($n_insig_children > 0) {
238 $perc = safe_div_0(100 * $total_insig_children_szB, $mem_total_B);
239 printf(TMPFILE "%s->%05.2f%% (%sB) in %d+ places, all below "
240 . "ms_print's threshold (%05.2f%%)\n",
241 $this_prefix2, $perc, commify($total_insig_children_szB),
242 $n_insig_children, $threshold);
243 print(TMPFILE "$this_prefix2\n");
246 # If this node has no children, print an extra (mostly) empty line.
247 if (0 == $n_children) {
248 print(TMPFILE "$this_prefix2\n");
257 #-----------------------------------------------------------------------------
258 # Reading the input file: main
259 #-----------------------------------------------------------------------------
263 my ($szB, $szB_scaled) = @_;
265 # For the label, if $szB is 999B or below, we print it as an integer.
266 # Otherwise, we print it as a float with 5 characters (including the '.').
267 # Examples (for bytes):
273 # 102400 --> 100.0 KB
274 # 1024000 --> 0.977 MB
275 # 1048576 --> 1.000 MB
277 if ($szB < 1000) { return sprintf("%5d", $szB); }
278 elsif ($szB_scaled < 10) { return sprintf("%5.3f", $szB_scaled); }
279 elsif ($szB_scaled < 100) { return sprintf("%5.2f", $szB_scaled); }
280 else { return sprintf("%5.1f", $szB_scaled); }
283 # Work out the units for the max value, measured in instructions.
288 # We repeat until the number is less than 1000.
291 # Nb: 'k' is the "kilo" (1000) prefix.
292 if ($nI_scaled >= 1000) { $unit = "ki"; $nI_scaled /= 1024; }
293 if ($nI_scaled >= 1000) { $unit = "Mi"; $nI_scaled /= 1024; }
294 if ($nI_scaled >= 1000) { $unit = "Gi"; $nI_scaled /= 1024; }
295 if ($nI_scaled >= 1000) { $unit = "Ti"; $nI_scaled /= 1024; }
296 if ($nI_scaled >= 1000) { $unit = "Pi"; $nI_scaled /= 1024; }
297 if ($nI_scaled >= 1000) { $unit = "Ei"; $nI_scaled /= 1024; }
298 if ($nI_scaled >= 1000) { $unit = "Zi"; $nI_scaled /= 1024; }
299 if ($nI_scaled >= 1000) { $unit = "Yi"; $nI_scaled /= 1024; }
301 return (max_label_2($nI, $nI_scaled), $unit);
304 # Work out the units for the max value, measured in bytes.
309 # We repeat until the number is less than 1000, but we divide by 1024 on
311 my $szB_scaled = $szB;
313 # Nb: 'K' or 'k' are acceptable as the "binary kilo" (1024) prefix.
314 # (Strictly speaking, should use "KiB" (kibibyte), "MiB" (mebibyte), etc,
315 # but they're not in common use.)
316 if ($szB_scaled >= 1000) { $unit = "KB"; $szB_scaled /= 1024; }
317 if ($szB_scaled >= 1000) { $unit = "MB"; $szB_scaled /= 1024; }
318 if ($szB_scaled >= 1000) { $unit = "GB"; $szB_scaled /= 1024; }
319 if ($szB_scaled >= 1000) { $unit = "TB"; $szB_scaled /= 1024; }
320 if ($szB_scaled >= 1000) { $unit = "PB"; $szB_scaled /= 1024; }
321 if ($szB_scaled >= 1000) { $unit = "EB"; $szB_scaled /= 1024; }
322 if ($szB_scaled >= 1000) { $unit = "ZB"; $szB_scaled /= 1024; }
323 if ($szB_scaled >= 1000) { $unit = "YB"; $szB_scaled /= 1024; }
325 return (max_label_2($szB, $szB_scaled), $unit);
328 # Work out the units for the max value, measured in ms/s/h.
333 # We scale from millisecond to seconds to hours.
335 # XXX: this allows a number with 6 chars, eg. "3599.0 s"
336 my $szB_scaled = $szB;
338 if ($szB_scaled >= 1000) { $unit = "s"; $szB_scaled /= 1000; }
339 if ($szB_scaled >= 3600) { $unit = "h"; $szB_scaled /= 3600; }
341 return (max_label_2($szB, $szB_scaled), $unit);
344 # This prints four things:
345 # - the output header
347 # - the snapshot summaries (number, list of detailed ones)
350 # The first three parts can't be printed until we've read the whole input file;
351 # but the fourth part is much easier to print while we're reading the file. So
352 # we print the fourth part to a tmp file, and then dump the tmp file at the
355 sub read_input_file()
357 my $desc = ""; # Concatenated description lines.
358 my $peak_mem_total_szB = 0;
360 # Info about each snapshot.
361 my @snapshot_nums = ();
363 my @mem_total_Bs = ();
364 my @is_detaileds = ();
365 my $peak_num = -1; # An initial value that will be ok if no peak
366 # entry is in the file.
368 #-------------------------------------------------------------------------
369 # Read start of input file.
370 #-------------------------------------------------------------------------
371 open(INPUTFILE, "< $input_file")
372 || die "Cannot open $input_file for reading\n";
374 # Read "desc:" lines.
376 while ($line = get_line()) {
377 if ($line =~ s/^desc://) {
384 # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above).
385 ($line =~ /^cmd:\s*(.*)$/) or die("Line $.: missing 'cmd' line\n");
388 # Read "time_unit:" line.
390 ($line =~ /^time_unit:\s*(.*)$/) or
391 die("Line $.: missing 'time_unit' line\n");
394 #-------------------------------------------------------------------------
395 # Print snapshot list header to $tmp_file.
396 #-------------------------------------------------------------------------
397 open(TMPFILE, "> $tmp_file")
398 || die "Cannot open $tmp_file for writing\n";
400 my $time_column = sprintf("%14s", "time($time_unit)");
401 my $column_format = "%3s %14s %16s %16s %13s %12s\n";
404 sprintf($column_format
413 print(TMPFILE $header);
415 #-------------------------------------------------------------------------
416 # Read body of input file.
417 #-------------------------------------------------------------------------
419 while (defined $line) {
420 my $snapshot_num = equals_num_line($line, "snapshot");
421 my $time = equals_num_line(get_line(), "time");
422 my $mem_heap_B = equals_num_line(get_line(), "mem_heap_B");
423 my $mem_heap_extra_B = equals_num_line(get_line(), "mem_heap_extra_B");
424 my $mem_stacks_B = equals_num_line(get_line(), "mem_stacks_B");
425 my $mem_total_B = $mem_heap_B + $mem_heap_extra_B + $mem_stacks_B;
426 my $heap_tree = equals_num_line(get_line(), "heap_tree");
428 # Print the snapshot data to $tmp_file.
429 printf(TMPFILE $column_format,
432 , commify($mem_total_B)
433 , commify($mem_heap_B)
434 , commify($mem_heap_extra_B)
435 , commify($mem_stacks_B)
438 # Remember the snapshot data.
439 push(@snapshot_nums, $snapshot_num);
441 push(@mem_total_Bs, $mem_total_B);
442 push(@is_detaileds, ( $heap_tree eq "empty" ? 0 : 1 ));
443 $peak_mem_total_szB = $mem_total_B
444 if $mem_total_B > $peak_mem_total_szB;
446 # Read the heap tree, and if it's detailed, print it and a subsequent
447 # snapshot list header to $tmp_file.
448 if ($heap_tree eq "empty") {
450 } elsif ($heap_tree =~ "(detailed|peak)") {
451 # If "peak", remember the number.
452 if ($heap_tree eq "peak") {
453 $peak_num = $snapshot_num;
455 # '1' means it's the top node of the tree.
456 read_heap_tree(1, "", "", "", $mem_total_B);
458 # Print the header, unless there are no more snapshots.
461 print(TMPFILE $header);
464 die("Line $.: expected 'empty' or '...' after 'heap_tree='\n");
471 #-------------------------------------------------------------------------
473 #-------------------------------------------------------------------------
475 print("Command: $cmd\n");
476 print("Massif arguments: $desc");
477 print("ms_print arguments:$ms_print_args\n");
481 #-------------------------------------------------------------------------
483 #-------------------------------------------------------------------------
485 # Row 0 ([0..graph_x][0]) is the X-axis.
486 # Column 0 ([0][0..graph_y]) is the Y-axis.
487 # The rest ([1][1]..[graph_x][graph_y]) is the usable graph area.
492 my $n_snapshots = scalar(@snapshot_nums);
493 ($n_snapshots > 0) or die;
494 my $end_time = $times[$n_snapshots-1];
495 ($end_time >= 0) or die;
498 $graph[0][0] = '+'; # axes join point
499 for ($x = 1; $x <= $graph_x; $x++) { $graph[$x][0] = '-'; } # X-axis
500 for ($y = 1; $y <= $graph_y; $y++) { $graph[0][$y] = '|'; } # Y-axis
501 $graph[$graph_x][0] = '>'; # X-axis arrow
502 $graph[0][$graph_y] = '^'; # Y-axis arrow
503 for ($x = 1; $x <= $graph_x; $x++) { # usable area
504 for ($y = 1; $y <= $graph_y; $y++) {
505 $graph[$x][$y] = ' ';
509 #-------------------------------------------------------------------------
510 # Write snapshot bars into graph[][].
511 #-------------------------------------------------------------------------
512 # Each row represents K bytes, which is 1/graph_y of the peak size
513 # (and K can be non-integral). When drawing the column for a snapshot,
514 # in order to fill the slot in row y (where the first row drawn on is
515 # row 1) with a full-char (eg. ':'), it must be >= y*K. For example, if
516 # K = 10 bytes, then the values 0, 4, 5, 9, 10, 14, 15, 19, 20, 24, 25,
517 # 29, 30 would be drawn like this (showing one per column):
521 # 30 | : 3 3 * 10 = 30
522 # 20 | ::::: 2 2 * 10 = 20
523 # 10 | ::::::::: 1 1 * 10 = 10
527 my $detailed_char = '@';
528 my $normal_char = ':';
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;
542 for (my $i = 0; $i < $n_snapshots; $i++) {
544 # Work out which column this snapshot belongs to.
546 my $x_pos_frac = ($times[$i] / ($end_time)) * $graph_x;
547 $x = int($x_pos_frac) + 1; # +1 due to Y-axis
548 # The final snapshot will spill over into the n+1th column, which
549 # doesn't get shown. So we fudge that one and pull it back a
550 # column, as if the end_time was actually end_time+epsilon.
551 if ($times[$i] == $end_time) {
552 ($x == $graph_x+1) or die;
556 # If there was a gap between the previous snapshot's column and this
557 # one, we draw a horizontal line in the gap (so long as it doesn't
558 # trash the x-axis). Without this, graphs with a few sparse
559 # snapshots look funny -- as if the memory usage is in temporary
561 if ($prev_y_max > 0) {
562 for (my $x2 = $prev_x + 1; $x2 < $x; $x2++) {
563 $graph[$x2][$prev_y_max] = $prev_char;
567 # Choose the column char.
569 if ($i == $peak_num) { $char = $peak_char; }
570 elsif ($is_detaileds[$i]) { $char = $detailed_char; }
571 else { $char = $normal_char; }
573 # Grow this snapshot bar from bottom to top.
575 for ($y = 1; $y <= $graph_y; $y++) {
576 if ($mem_total_Bs[$i] >= $y * $K) {
577 # Priority order for chars: peak > detailed > normal
578 my $should_draw_char =
579 (($char eq $peak_char)
581 ($char eq $detailed_char and
582 $graph[$x][$y] ne $peak_char
585 ($char eq $normal_char and
586 $graph[$x][$y] ne $peak_char and
587 $graph[$x][$y] ne $detailed_char
591 if ($should_draw_char) {
592 $graph[$x][$y] = $char;
597 $prev_y_max = $y_max;
601 #-------------------------------------------------------------------------
603 #-------------------------------------------------------------------------
604 my ($y_label, $y_unit) = B_max_label($peak_mem_total_szB);
605 my ($x_label, $x_unit);
606 if ($time_unit eq "i") { ($x_label, $x_unit) = i_max_label($end_time) }
607 elsif ($time_unit eq "ms") { ($x_label, $x_unit) = t_max_label($end_time) }
608 elsif ($time_unit eq "B") { ($x_label, $x_unit) = B_max_label($end_time) }
609 else { die "bad time_unit: $time_unit\n"; }
611 printf(" %2s\n", $y_unit);
612 for ($y = $graph_y; $y >= 0; $y--) {
613 if ($graph_y == $y) { # top row
615 } elsif (0 == $y) { # bottom row
617 } else { # anywhere else
621 # Axis and data for the row.
622 for ($x = 0; $x <= $graph_x; $x++) {
623 printf("%s", $graph[$x][$y]);
631 printf(" 0%s%5s\n", ' ' x ($graph_x-5), $x_label);
633 #-------------------------------------------------------------------------
634 # Print snapshot numbers.
635 #-------------------------------------------------------------------------
637 print("Number of snapshots: $n_snapshots\n");
638 print(" Detailed snapshots: [");
639 my $first_detailed = 1;
640 for (my $i = 0; $i < $n_snapshots; $i++) {
641 if ($is_detaileds[$i]) {
642 if ($first_detailed) {
648 if ($i == $peak_num) {
655 #-------------------------------------------------------------------------
656 # Print snapshots, from $tmp_file.
657 #-------------------------------------------------------------------------
658 open(TMPFILE, "< $tmp_file")
659 || die "Cannot open $tmp_file for reading\n";
661 while (my $line = <TMPFILE>) {
667 #-----------------------------------------------------------------------------
669 #-----------------------------------------------------------------------------
672 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
677 #----------------------------------------------------------------------------
679 #----------------------------------------------------------------------------
683 ##--------------------------------------------------------------------##
684 ##--- end ms_print.in ---##
685 ##--------------------------------------------------------------------##