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-2017 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, see <http://www.gnu.org/licenses/>.
26 # The GNU General Public License is contained in the file COPYING.
31 #----------------------------------------------------------------------------
32 # Global variables, main data structures
33 #----------------------------------------------------------------------------
35 # Command line of profiled program.
38 # Time unit used in profile.
41 # Threshold dictating what percentage an entry must represent for us to
45 # Graph x and y dimensions.
50 my $input_file = undef;
52 # Where to create tmp files. See also function VG_(tmpdir) in m_libcfile.c.
53 my $tmp_dir = $ENV{"TMPDIR"};
54 $tmp_dir = "@VG_TMPDIR@" if (! $tmp_dir);
55 $tmp_dir = "/tmp" if (! $tmp_dir);
58 my $tmp_file = "$tmp_dir/ms_print.tmp.$$";
61 my $version = "@VERSION@";
63 # Args passed, for printing.
68 usage: ms_print [options] massif-out-file
70 options for the user, with defaults in [ ], are:
71 -h --help show this message
72 --version show version
73 --threshold=<m.n> significance threshold, in percent [$threshold]
74 --x=<4..1000> graph width, in columns [72]
75 --y=<4..1000> graph height, in rows [20]
77 ms_print is Copyright (C) 2007-2017 Nicholas Nethercote.
78 and licensed under the GNU General Public License, version 2.
79 Bug reports, feedback, admiration, abuse, etc, to: njn\@valgrind.org.
84 # Used in various places of output.
86 my $fancy_nl = $fancy . "\n";
88 # Returns 0 if the denominator is 0.
92 return ($y ? $x / $y : 0);
95 #-----------------------------------------------------------------------------
96 # Argument and option handling
97 #-----------------------------------------------------------------------------
98 sub process_cmd_line()
102 # Grab a copy of the arguments, for printing later.
103 for my $arg (@ARGV) {
104 $ms_print_args .= " $arg"; # The arguments.
107 for my $arg (@ARGV) {
113 if ($arg =~ /^--version$/) {
114 die("ms_print-$version\n");
116 # --threshold=X (tolerates a trailing '%')
117 } elsif ($arg =~ /^--threshold=([\d\.]+)%?$/) {
119 ($1 >= 0 && $1 <= 100) or die($usage);
121 } elsif ($arg =~ /^--x=(\d+)$/) {
123 (4 <= $graph_x && $graph_x <= 1000) or die($usage);
125 } elsif ($arg =~ /^--y=(\d+)$/) {
127 (4 <= $graph_y && $graph_y <= 1000) or die($usage);
129 } else { # -h and --help fall under this case
133 # Not an option. Remember it as a filename.
138 # Must have chosen exactly one input file.
140 $input_file = $files[0];
146 #-----------------------------------------------------------------------------
147 # Reading the input file: auxiliary functions
148 #-----------------------------------------------------------------------------
150 # Gets the next line, stripping comments and skipping blanks.
151 # Returns undef at EOF.
154 while (my $line = <INPUTFILE>) {
155 $line =~ s/#.*$//; # remove comments
156 if ($line !~ /^\s*$/) {
157 return $line; # return $line if non-empty
160 return undef; # EOF: return undef
163 sub equals_num_line($$)
165 my ($line, $fieldname) = @_;
167 or die("Line $.: expected \"$fieldname\" line, got end of file\n");
168 $line =~ s/^$fieldname=(.*)\s*$//
169 or die("Line $.: expected \"$fieldname\" line, got:\n$line");
173 sub is_significant_XPt($$$)
175 my ($is_top_node, $xpt_szB, $total_szB) = @_;
176 ($xpt_szB <= $total_szB) or die;
177 # Nb: we always consider the alloc-XPt significant, even if the size is
179 return $is_top_node || 0 == $threshold ||
180 ( $total_szB != 0 && $xpt_szB * 100 / $total_szB >= $threshold );
183 #-----------------------------------------------------------------------------
184 # Reading the input file: reading heap trees
185 #-----------------------------------------------------------------------------
187 # Forward declaration, because it's recursive.
188 sub read_heap_tree($$$$$);
190 # Return pair: if the tree was significant, both are zero. If it was
191 # insignificant, the first element is 1 and the second is the number of
193 sub read_heap_tree($$$$$)
195 # Read the line and determine if it is significant.
196 my ($is_top_node, $this_prefix, $child_midfix, $arrow, $mem_total_B) = @_;
197 my $line = get_line();
198 (defined $line and $line =~ /^\s*n(\d+):\s*(\d+)(.*)$/)
199 or die("Line $.: expected a tree node line, got:\n$line\n");
203 my $perc = safe_div_0(100 * $bytes, $mem_total_B);
204 # Nb: we always print the alloc-XPt, even if its size is zero.
205 my $is_significant = is_significant_XPt($is_top_node, $bytes, $mem_total_B);
207 # We precede this node's line with "$this_prefix.$arrow". We precede
208 # any children of this node with "$this_prefix$child_midfix$arrow".
209 if ($is_significant) {
210 # Nb: $details might have '%' in it, so don't embed directly in the
213 "$this_prefix$arrow%05.2f%% (%sB)%s\n", $perc, commify($bytes),
217 # Now read all the children.
218 my $n_insig_children = 0;
219 my $total_insig_children_szB = 0;
220 my $this_prefix2 = $this_prefix . $child_midfix;
221 for (my $i = 0; $i < $n_children; $i++) {
222 # If child is the last sibling, the midfix is empty.
223 my $child_midfix2 = ( $i+1 == $n_children ? " " : "| " );
224 my ($is_child_insignificant, $child_insig_bytes) =
225 # '0' means it's not the top node of the tree.
226 read_heap_tree(0, $this_prefix2, $child_midfix2, "->",
228 $n_insig_children += $is_child_insignificant;
229 $total_insig_children_szB += $child_insig_bytes;
232 if ($is_significant) {
233 # If this was significant but any children were insignificant, print
234 # the "in N places" line for them.
235 if ($n_insig_children > 0) {
236 $perc = safe_div_0(100 * $total_insig_children_szB, $mem_total_B);
237 printf(TMPFILE "%s->%05.2f%% (%sB) in %d+ places, all below "
238 . "ms_print's threshold (%05.2f%%)\n",
239 $this_prefix2, $perc, commify($total_insig_children_szB),
240 $n_insig_children, $threshold);
241 print(TMPFILE "$this_prefix2\n");
244 # If this node has no children, print an extra (mostly) empty line.
245 if (0 == $n_children) {
246 print(TMPFILE "$this_prefix2\n");
255 #-----------------------------------------------------------------------------
256 # Reading the input file: main
257 #-----------------------------------------------------------------------------
261 my ($szB, $szB_scaled) = @_;
263 # For the label, if $szB is 999B or below, we print it as an integer.
264 # Otherwise, we print it as a float with 5 characters (including the '.').
265 # Examples (for bytes):
271 # 102400 --> 100.0 KB
272 # 1024000 --> 0.977 MB
273 # 1048576 --> 1.000 MB
275 if ($szB < 1000) { return sprintf("%5d", $szB); }
276 elsif ($szB_scaled < 10) { return sprintf("%5.3f", $szB_scaled); }
277 elsif ($szB_scaled < 100) { return sprintf("%5.2f", $szB_scaled); }
278 else { return sprintf("%5.1f", $szB_scaled); }
281 # Work out the units for the max value, measured in instructions.
286 # We repeat until the number is less than 1000.
289 # Nb: 'k' is the "kilo" (1000) prefix.
290 if ($nI_scaled >= 1000) { $unit = "ki"; $nI_scaled /= 1024; }
291 if ($nI_scaled >= 1000) { $unit = "Mi"; $nI_scaled /= 1024; }
292 if ($nI_scaled >= 1000) { $unit = "Gi"; $nI_scaled /= 1024; }
293 if ($nI_scaled >= 1000) { $unit = "Ti"; $nI_scaled /= 1024; }
294 if ($nI_scaled >= 1000) { $unit = "Pi"; $nI_scaled /= 1024; }
295 if ($nI_scaled >= 1000) { $unit = "Ei"; $nI_scaled /= 1024; }
296 if ($nI_scaled >= 1000) { $unit = "Zi"; $nI_scaled /= 1024; }
297 if ($nI_scaled >= 1000) { $unit = "Yi"; $nI_scaled /= 1024; }
299 return (max_label_2($nI, $nI_scaled), $unit);
302 # Work out the units for the max value, measured in bytes.
307 # We repeat until the number is less than 1000, but we divide by 1024 on
309 my $szB_scaled = $szB;
311 # Nb: 'K' or 'k' are acceptable as the "binary kilo" (1024) prefix.
312 # (Strictly speaking, should use "KiB" (kibibyte), "MiB" (mebibyte), etc,
313 # but they're not in common use.)
314 if ($szB_scaled >= 1000) { $unit = "KB"; $szB_scaled /= 1024; }
315 if ($szB_scaled >= 1000) { $unit = "MB"; $szB_scaled /= 1024; }
316 if ($szB_scaled >= 1000) { $unit = "GB"; $szB_scaled /= 1024; }
317 if ($szB_scaled >= 1000) { $unit = "TB"; $szB_scaled /= 1024; }
318 if ($szB_scaled >= 1000) { $unit = "PB"; $szB_scaled /= 1024; }
319 if ($szB_scaled >= 1000) { $unit = "EB"; $szB_scaled /= 1024; }
320 if ($szB_scaled >= 1000) { $unit = "ZB"; $szB_scaled /= 1024; }
321 if ($szB_scaled >= 1000) { $unit = "YB"; $szB_scaled /= 1024; }
323 return (max_label_2($szB, $szB_scaled), $unit);
326 # Work out the units for the max value, measured in ms/s/h.
331 # We scale from millisecond to seconds to hours.
333 # XXX: this allows a number with 6 chars, eg. "3599.0 s"
334 my $szB_scaled = $szB;
336 if ($szB_scaled >= 1000) { $unit = "s"; $szB_scaled /= 1000; }
337 if ($szB_scaled >= 3600) { $unit = "h"; $szB_scaled /= 3600; }
339 return (max_label_2($szB, $szB_scaled), $unit);
342 # This prints four things:
343 # - the output header
345 # - the snapshot summaries (number, list of detailed ones)
348 # The first three parts can't be printed until we've read the whole input file;
349 # but the fourth part is much easier to print while we're reading the file. So
350 # we print the fourth part to a tmp file, and then dump the tmp file at the
353 sub read_input_file()
355 my $desc = ""; # Concatenated description lines.
356 my $peak_mem_total_szB = 0;
358 # Info about each snapshot.
359 my @snapshot_nums = ();
361 my @mem_total_Bs = ();
362 my @is_detaileds = ();
363 my $peak_num = -1; # An initial value that will be ok if no peak
364 # entry is in the file.
366 #-------------------------------------------------------------------------
367 # Read start of input file.
368 #-------------------------------------------------------------------------
369 open(INPUTFILE, "< $input_file")
370 || die "Cannot open $input_file for reading\n";
372 # Read "desc:" lines.
374 while ($line = get_line()) {
375 if ($line =~ s/^desc://) {
382 # Read "cmd:" line (Nb: will already be in $line from "desc:" loop above).
383 ($line =~ /^cmd:\s*(.*)$/) or die("Line $.: missing 'cmd' line\n");
386 # Read "time_unit:" line.
388 ($line =~ /^time_unit:\s*(.*)$/) or
389 die("Line $.: missing 'time_unit' line\n");
392 #-------------------------------------------------------------------------
393 # Print snapshot list header to $tmp_file.
394 #-------------------------------------------------------------------------
395 open(TMPFILE, "> $tmp_file")
396 || die "Cannot open $tmp_file for writing\n";
398 my $time_column = sprintf("%14s", "time($time_unit)");
399 my $column_format = "%3s %14s %16s %16s %13s %12s\n";
402 sprintf($column_format
411 print(TMPFILE $header);
413 #-------------------------------------------------------------------------
414 # Read body of input file.
415 #-------------------------------------------------------------------------
417 while (defined $line) {
418 my $snapshot_num = equals_num_line($line, "snapshot");
419 my $time = equals_num_line(get_line(), "time");
420 my $mem_heap_B = equals_num_line(get_line(), "mem_heap_B");
421 my $mem_heap_extra_B = equals_num_line(get_line(), "mem_heap_extra_B");
422 my $mem_stacks_B = equals_num_line(get_line(), "mem_stacks_B");
423 my $mem_total_B = $mem_heap_B + $mem_heap_extra_B + $mem_stacks_B;
424 my $heap_tree = equals_num_line(get_line(), "heap_tree");
426 # Print the snapshot data to $tmp_file.
427 printf(TMPFILE $column_format,
430 , commify($mem_total_B)
431 , commify($mem_heap_B)
432 , commify($mem_heap_extra_B)
433 , commify($mem_stacks_B)
436 # Remember the snapshot data.
437 push(@snapshot_nums, $snapshot_num);
439 push(@mem_total_Bs, $mem_total_B);
440 push(@is_detaileds, ( $heap_tree eq "empty" ? 0 : 1 ));
441 $peak_mem_total_szB = $mem_total_B
442 if $mem_total_B > $peak_mem_total_szB;
444 # Read the heap tree, and if it's detailed, print it and a subsequent
445 # snapshot list header to $tmp_file.
446 if ($heap_tree eq "empty") {
448 } elsif ($heap_tree =~ "(detailed|peak)") {
449 # If "peak", remember the number.
450 if ($heap_tree eq "peak") {
451 $peak_num = $snapshot_num;
453 # '1' means it's the top node of the tree.
454 read_heap_tree(1, "", "", "", $mem_total_B);
456 # Print the header, unless there are no more snapshots.
459 print(TMPFILE $header);
462 die("Line $.: expected 'empty' or '...' after 'heap_tree='\n");
469 #-------------------------------------------------------------------------
471 #-------------------------------------------------------------------------
473 print("Command: $cmd\n");
474 print("Massif arguments: $desc");
475 print("ms_print arguments:$ms_print_args\n");
479 #-------------------------------------------------------------------------
481 #-------------------------------------------------------------------------
483 # Row 0 ([0..graph_x][0]) is the X-axis.
484 # Column 0 ([0][0..graph_y]) is the Y-axis.
485 # The rest ([1][1]..[graph_x][graph_y]) is the usable graph area.
490 my $n_snapshots = scalar(@snapshot_nums);
491 ($n_snapshots > 0) or die;
492 my $end_time = $times[$n_snapshots-1];
493 ($end_time >= 0) or die;
496 $graph[0][0] = '+'; # axes join point
497 for ($x = 1; $x <= $graph_x; $x++) { $graph[$x][0] = '-'; } # X-axis
498 for ($y = 1; $y <= $graph_y; $y++) { $graph[0][$y] = '|'; } # Y-axis
499 $graph[$graph_x][0] = '>'; # X-axis arrow
500 $graph[0][$graph_y] = '^'; # Y-axis arrow
501 for ($x = 1; $x <= $graph_x; $x++) { # usable area
502 for ($y = 1; $y <= $graph_y; $y++) {
503 $graph[$x][$y] = ' ';
507 #-------------------------------------------------------------------------
508 # Write snapshot bars into graph[][].
509 #-------------------------------------------------------------------------
510 # Each row represents K bytes, which is 1/graph_y of the peak size
511 # (and K can be non-integral). When drawing the column for a snapshot,
512 # in order to fill the slot in row y (where the first row drawn on is
513 # row 1) with a full-char (eg. ':'), it must be >= y*K. For example, if
514 # K = 10 bytes, then the values 0, 4, 5, 9, 10, 14, 15, 19, 20, 24, 25,
515 # 29, 30 would be drawn like this (showing one per column):
519 # 30 | : 3 3 * 10 = 30
520 # 20 | ::::: 2 2 * 10 = 20
521 # 10 | ::::::::: 1 1 * 10 = 10
525 my $detailed_char = '@';
526 my $normal_char = ':';
528 # Work out how many bytes each row represents. If the peak size was 0,
529 # make it 1 so that the Y-axis covers a non-zero range of values.
530 # Likewise for end_time.
531 if (0 == $peak_mem_total_szB) { $peak_mem_total_szB = 1; }
532 if (0 == $end_time ) { $end_time = 1; }
533 my $K = $peak_mem_total_szB / $graph_y;
540 for (my $i = 0; $i < $n_snapshots; $i++) {
542 # Work out which column this snapshot belongs to.
544 my $x_pos_frac = ($times[$i] / ($end_time)) * $graph_x;
545 $x = int($x_pos_frac) + 1; # +1 due to Y-axis
546 # The final snapshot will spill over into the n+1th column, which
547 # doesn't get shown. So we fudge that one and pull it back a
548 # column, as if the end_time was actually end_time+epsilon.
549 if ($times[$i] == $end_time) {
550 ($x == $graph_x+1) or die;
554 # If there was a gap between the previous snapshot's column and this
555 # one, we draw a horizontal line in the gap (so long as it doesn't
556 # trash the x-axis). Without this, graphs with a few sparse
557 # snapshots look funny -- as if the memory usage is in temporary
559 if ($prev_y_max > 0) {
560 for (my $x2 = $prev_x + 1; $x2 < $x; $x2++) {
561 $graph[$x2][$prev_y_max] = $prev_char;
565 # Choose the column char.
567 if ($i == $peak_num) { $char = $peak_char; }
568 elsif ($is_detaileds[$i]) { $char = $detailed_char; }
569 else { $char = $normal_char; }
571 # Grow this snapshot bar from bottom to top.
573 for ($y = 1; $y <= $graph_y; $y++) {
574 if ($mem_total_Bs[$i] >= $y * $K) {
575 # Priority order for chars: peak > detailed > normal
576 my $should_draw_char =
577 (($char eq $peak_char)
579 ($char eq $detailed_char and
580 $graph[$x][$y] ne $peak_char
583 ($char eq $normal_char and
584 $graph[$x][$y] ne $peak_char and
585 $graph[$x][$y] ne $detailed_char
589 if ($should_draw_char) {
590 $graph[$x][$y] = $char;
595 $prev_y_max = $y_max;
599 #-------------------------------------------------------------------------
601 #-------------------------------------------------------------------------
602 my ($y_label, $y_unit) = B_max_label($peak_mem_total_szB);
603 my ($x_label, $x_unit);
604 if ($time_unit eq "i") { ($x_label, $x_unit) = i_max_label($end_time) }
605 elsif ($time_unit eq "ms") { ($x_label, $x_unit) = t_max_label($end_time) }
606 elsif ($time_unit eq "B") { ($x_label, $x_unit) = B_max_label($end_time) }
607 else { die "bad time_unit: $time_unit\n"; }
609 printf(" %2s\n", $y_unit);
610 for ($y = $graph_y; $y >= 0; $y--) {
611 if ($graph_y == $y) { # top row
613 } elsif (0 == $y) { # bottom row
615 } else { # anywhere else
619 # Axis and data for the row.
620 for ($x = 0; $x <= $graph_x; $x++) {
621 printf("%s", $graph[$x][$y]);
629 printf(" 0%s%5s\n", ' ' x ($graph_x-5), $x_label);
631 #-------------------------------------------------------------------------
632 # Print snapshot numbers.
633 #-------------------------------------------------------------------------
635 print("Number of snapshots: $n_snapshots\n");
636 print(" Detailed snapshots: [");
637 my $first_detailed = 1;
638 for (my $i = 0; $i < $n_snapshots; $i++) {
639 if ($is_detaileds[$i]) {
640 if ($first_detailed) {
646 if ($i == $peak_num) {
653 #-------------------------------------------------------------------------
654 # Print snapshots, from $tmp_file.
655 #-------------------------------------------------------------------------
656 open(TMPFILE, "< $tmp_file")
657 || die "Cannot open $tmp_file for reading\n";
659 while (my $line = <TMPFILE>) {
665 #-----------------------------------------------------------------------------
667 #-----------------------------------------------------------------------------
670 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/);
675 #----------------------------------------------------------------------------
677 #----------------------------------------------------------------------------
681 ##--------------------------------------------------------------------##
682 ##--- end ms_print.in ---##
683 ##--------------------------------------------------------------------##