[ebd57f8] | 1 | #! /usr/bin/env perl |
---|
| 2 | use strict; |
---|
| 3 | use File::Basename; |
---|
| 4 | |
---|
[81dea5b] | 5 | my $memlog_fn = $ARGV[0]; |
---|
| 6 | my $out_dir = $ARGV[1] || '.'; |
---|
| 7 | |
---|
[6bfe6a1] | 8 | my $print_raw_proc_name = 0; |
---|
| 9 | |
---|
[81dea5b] | 10 | if (! -f $memlog_fn) { |
---|
| 11 | print "Usage: $0 <memlog file> [<output directory>]\n"; |
---|
| 12 | exit 1; |
---|
| 13 | } |
---|
| 14 | |
---|
[747d2b1] | 15 | # The version of addr2line and friends that you use can make a big difference, |
---|
| 16 | # especially on BE ppc64, where older versions of addr2line did not account |
---|
| 17 | # correctly for the function descriptor setup. At ALCF, on the BG/Q, we have |
---|
| 18 | # newer versions not in the default search path (from bgclang). |
---|
| 19 | my $alcf_bu_dir = '/soft/compilers/bgclang/current/binutils/bin'; |
---|
| 20 | if (-d $alcf_bu_dir) { |
---|
| 21 | $ENV{'PATH'} = $alcf_bu_dir . ':' . $ENV{'PATH'}; |
---|
| 22 | } |
---|
| 23 | |
---|
[81dea5b] | 24 | open(MEMLOG, $memlog_fn) || die "Can't open $memlog_fn: $!"; |
---|
| 25 | |
---|
[0edbc98] | 26 | # The first step is to determine the high-water mark. |
---|
| 27 | my $max_rss = 0; |
---|
| 28 | foreach my $line (<MEMLOG>) { |
---|
| 29 | chomp($line); |
---|
| 30 | my @parts = split(/\t/, $line); |
---|
| 31 | |
---|
| 32 | my $op = shift(@parts); |
---|
| 33 | my $state = shift(@parts); |
---|
| 34 | |
---|
| 35 | my ($time, $then_max_rss, $tid) = split(/\s+/, $state); |
---|
| 36 | if ($max_rss < $then_max_rss) { |
---|
| 37 | $max_rss = $then_max_rss; |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | seek(MEMLOG, 0, 0); |
---|
| 42 | |
---|
| 43 | # Scan the log for malloc/free pairings. We're interested only in active |
---|
| 44 | # allocations at the time when the rss reaches the final maxrss. |
---|
| 45 | my $max_rss_time = 0; |
---|
| 46 | my %malloc_lines; |
---|
| 47 | foreach my $line (<MEMLOG>) { |
---|
| 48 | chomp($line); |
---|
| 49 | my @parts = split(/\t/, $line); |
---|
| 50 | |
---|
| 51 | my $op = shift(@parts); |
---|
| 52 | my $state = shift(@parts); |
---|
| 53 | |
---|
| 54 | if ($op =~ /^M:/) { |
---|
| 55 | my ($size, $ptr) = ($op =~ /^M: (\d+) 0x(\w+)/); |
---|
| 56 | $malloc_lines{$ptr} = $line; |
---|
| 57 | } elsif ($op =~ /^F:/) { |
---|
| 58 | my ($ptr) = ($op =~ /^F: 0x(\w+)/); |
---|
| 59 | delete $malloc_lines{$ptr}; |
---|
| 60 | } else { |
---|
| 61 | next; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | # If we've reached the max rss, we've seen all we need to see. |
---|
| 65 | my ($time, $then_max_rss, $tid) = split(/\s+/, $state); |
---|
| 66 | $max_rss_time = $time; |
---|
| 67 | if ($then_max_rss == $max_rss) { |
---|
| 68 | last; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | close(MEMLOG); |
---|
| 73 | |
---|
| 74 | # Convert maxrss, currently in KB, to bytes. |
---|
| 75 | $max_rss *= 1024; |
---|
| 76 | |
---|
[ebd57f8] | 77 | my $total_size = 0; |
---|
| 78 | my %roots; |
---|
[c4f89cf] | 79 | my %all_nodes; |
---|
[0edbc98] | 80 | foreach my $line (values %malloc_lines) { |
---|
[ebd57f8] | 81 | my @parts = split(/\t/, $line); |
---|
| 82 | |
---|
| 83 | my $op = shift(@parts); |
---|
| 84 | my $state = shift(@parts); |
---|
| 85 | |
---|
| 86 | # Only dealing with allocations here... |
---|
| 87 | if ($op !~ /^M:/) { |
---|
| 88 | next; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | my ($size, $ptr) = ($op =~ /^M: (\d+) 0x(\w+)/); |
---|
[0edbc98] | 92 | my ($time, $then_max_rss, $tid) = split(/\s+/, $state); |
---|
[ebd57f8] | 93 | |
---|
| 94 | $total_size += $size; |
---|
| 95 | |
---|
| 96 | sub level_parts($) { |
---|
| 97 | my $level = @_[0]; |
---|
| 98 | my ($file_name, $proc_name, $off, $pc, $relpc) = |
---|
| 99 | ($level =~ /^(.*) \((.*)\+0x(\w+)\) \[0x(\w+) \(0x(\w+)\)\]/); |
---|
| 100 | |
---|
| 101 | return ($file_name, $proc_name, $off, $pc, $relpc); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | # Put the top of the stack first. |
---|
| 105 | @parts = reverse(@parts); |
---|
| 106 | |
---|
| 107 | my $parent = \%roots; |
---|
| 108 | for (my $i = 0; $i < scalar(@parts); ++$i) { |
---|
| 109 | my $level = $parts[$i]; |
---|
| 110 | my ($file_name, $proc_name, $off, $pc, $relpc) = level_parts($level); |
---|
| 111 | |
---|
| 112 | # Skip this level if we don't even know from what file it came. |
---|
| 113 | if ($file_name eq '?') { |
---|
| 114 | next; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | # print STDERR "parsed: $file_name, $proc_name, $off, $pc, $relpc\n"; |
---|
| 118 | |
---|
[c4f89cf] | 119 | if (!exists $all_nodes{$pc}) { |
---|
| 120 | $all_nodes{$pc}->{'file_name'} = $file_name; |
---|
| 121 | $all_nodes{$pc}->{'proc_name'} = $proc_name; |
---|
| 122 | $all_nodes{$pc}->{'off'} = $off; |
---|
| 123 | $all_nodes{$pc}->{'pc'} = $pc; |
---|
| 124 | $all_nodes{$pc}->{'relpc'} = $relpc; |
---|
| 125 | } |
---|
[ebd57f8] | 126 | |
---|
[c4f89cf] | 127 | if (!exists $parent->{$pc}) { |
---|
| 128 | $parent->{$pc} = $all_nodes{$pc}; |
---|
[ebd57f8] | 129 | } |
---|
| 130 | |
---|
| 131 | $parent->{$pc}->{'size'} += $size; |
---|
| 132 | |
---|
| 133 | my ($next_file_name, $next_proc_name, $next_off, $next_pc, $next_relpc); |
---|
| 134 | if ($i < scalar(@parts)-1) { |
---|
| 135 | my $next_level = $parts[$i+1]; |
---|
| 136 | ($next_file_name, $next_proc_name, $next_off, $next_pc, $next_relpc) = |
---|
| 137 | level_parts($next_level); |
---|
| 138 | $parent->{$pc}->{'child_sizes'}->{$next_pc} += $size; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | if (!exists $parent->{'children'}) { |
---|
| 142 | $parent->{'children'} = {}; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | $parent = $parent->{'children'}; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
[81dea5b] | 149 | my $dot_fn = "$out_dir/" . basename($memlog_fn) . ".dot"; |
---|
| 150 | my $ps_fn = "$out_dir/" . basename($memlog_fn) . ".ps"; |
---|
| 151 | my $pdf_fn = "$out_dir/" . basename($memlog_fn) . ".pdf"; |
---|
| 152 | |
---|
| 153 | open(DOT, ">$dot_fn") || die "Can't open $dot_fn: $!"; |
---|
| 154 | |
---|
[ebd57f8] | 155 | sub format_bytes($) { |
---|
| 156 | my @sizes = qw( B KB MB GB TB PB ); |
---|
| 157 | my $size = $_[0]; |
---|
| 158 | |
---|
| 159 | my $i = 0; |
---|
| 160 | while ($size > 1024) { |
---|
| 161 | $size /= 1024; |
---|
| 162 | ++$i; |
---|
| 163 | } |
---|
| 164 | |
---|
[4bd7a88] | 165 | return sprintf("%.3f $sizes[$i]", $size); |
---|
[ebd57f8] | 166 | } |
---|
| 167 | |
---|
[bdaf020] | 168 | print DOT ("digraph \"memlog\" {\n"); |
---|
[81dea5b] | 169 | print DOT ("size=\"8,11\";\n"); |
---|
| 170 | print DOT ("node [width=0.375,height=0.25];\n"); |
---|
[ebd57f8] | 171 | |
---|
[bdaf020] | 172 | printf DOT ("Legend [shape=box, fontsize=100, shape=oval," . |
---|
| 173 | "label=\"Total: %s active at maxrss = %s after %s s\"];\n", |
---|
| 174 | format_bytes($total_size), format_bytes($max_rss), $max_rss_time); |
---|
| 175 | |
---|
[ebd57f8] | 176 | my %cached_names; |
---|
| 177 | sub get_name($) { |
---|
| 178 | my $node = $_[0]; |
---|
| 179 | my $pc = $node->{'pc'}; |
---|
| 180 | |
---|
| 181 | if (exists $cached_names{$pc}) { |
---|
| 182 | return $cached_names{$pc}; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | my $ret = ''; |
---|
| 186 | |
---|
| 187 | # Prefer the relative offset (that is what we want for shared libraries), but |
---|
| 188 | # if is not available, use the full offset (which is what we want for the |
---|
| 189 | # base executable). |
---|
| 190 | my $exe_off = $node->{'relpc'}; |
---|
| 191 | if (!$exe_off) { |
---|
| 192 | $exe_off = $pc; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | my $file_name = $node->{'file_name'}; |
---|
| 196 | my ($func, $loc) = `addr2line -e $file_name -f 0x$exe_off`; |
---|
| 197 | chomp($func); |
---|
| 198 | chomp($loc); |
---|
| 199 | |
---|
| 200 | if ($func !~ /^\?/) { |
---|
[6520ef5] | 201 | # In general, this function name might look something like: |
---|
| 202 | # 00000329.plt_call.wcsnrtombs@@GLIBC_2.3+0 |
---|
[10599da] | 203 | $func =~ s/@.*//; # Remove trailing symbol version strings |
---|
[ebd57f8] | 204 | $func =~ s/.*\.//; |
---|
| 205 | $func = `c++filt '$func'`; |
---|
| 206 | chomp($func); |
---|
| 207 | |
---|
[081a0fa] | 208 | $ret .= $func . '\n'; |
---|
| 209 | |
---|
| 210 | if ($loc !~ /^\?/) { |
---|
| 211 | $ret .= $loc . '\n'; |
---|
| 212 | } |
---|
[4bd7a88] | 213 | } elsif ($node->{'proc_name'} ne '?') { |
---|
| 214 | my $proc_name = $node->{'proc_name'}; |
---|
| 215 | $proc_name = `c++filt '$proc_name'`; |
---|
| 216 | chomp($proc_name); |
---|
| 217 | |
---|
| 218 | $ret .= $proc_name . '\n'; |
---|
[ebd57f8] | 219 | } |
---|
| 220 | |
---|
[6bfe6a1] | 221 | $ret .= $node->{'file_name'}; |
---|
| 222 | if ($print_raw_proc_name and $node->{'proc_name'} ne '?') { |
---|
| 223 | $ret .= '\n' . $node->{'proc_name'} . '+0x' . $node->{'off'}; |
---|
[ebd57f8] | 224 | } |
---|
| 225 | |
---|
| 226 | $cached_names{$pc} = $ret; |
---|
| 227 | return $ret; |
---|
| 228 | } |
---|
| 229 | |
---|
[4bd7a88] | 230 | my $skip_frac = 0.01; |
---|
| 231 | my %skipped; |
---|
| 232 | |
---|
[c4f89cf] | 233 | foreach my $pc (keys %all_nodes) { |
---|
| 234 | my $node = $all_nodes{$pc}; |
---|
[ebd57f8] | 235 | my $name = get_name($node); |
---|
| 236 | |
---|
| 237 | my $local_size = $node->{'size'}; |
---|
[4bd7a88] | 238 | if ($local_size * 1.0 / $total_size < $skip_frac) { |
---|
[c4f89cf] | 239 | $skipped{$pc} = 1; |
---|
[4bd7a88] | 240 | next; |
---|
| 241 | } |
---|
[ebd57f8] | 242 | |
---|
| 243 | my $fs = 8.0; |
---|
| 244 | if ($local_size > 0) { |
---|
[4bd7a88] | 245 | $fs = 50.0 * (abs($local_size * 1.0 / $total_size))**0.125; |
---|
[ebd57f8] | 246 | } |
---|
| 247 | |
---|
[81dea5b] | 248 | printf DOT ("N%s [label=\"%s\\n%s\", shape=box, fontsize=%.1f%s];\n", |
---|
[c4f89cf] | 249 | $pc, $name, format_bytes($local_size), $fs); |
---|
[ebd57f8] | 250 | } |
---|
| 251 | |
---|
[c4f89cf] | 252 | foreach my $pc (keys %all_nodes) { |
---|
| 253 | my $node = $all_nodes{$pc}; |
---|
| 254 | |
---|
[ebd57f8] | 255 | my $local_size = $node->{'size'}; |
---|
[c4f89cf] | 256 | if ($skipped{$pc}) { |
---|
[4bd7a88] | 257 | next; |
---|
| 258 | } |
---|
[ebd57f8] | 259 | |
---|
| 260 | foreach my $cpc (keys %{$node->{'child_sizes'}}) { |
---|
[4bd7a88] | 261 | if ($skipped{$cpc}) { |
---|
| 262 | next; |
---|
| 263 | } |
---|
| 264 | |
---|
[ebd57f8] | 265 | my $child_size = $node->{'child_sizes'}->{$cpc}; |
---|
| 266 | my $frac = $child_size * 1.0 / $local_size; |
---|
| 267 | |
---|
[4bd7a88] | 268 | my $weight = 100.0 * sqrt($frac); |
---|
[6520ef5] | 269 | my $style = sprintf("setlinewidth(%f)", 8.0 * sqrt($frac)); |
---|
[ebd57f8] | 270 | |
---|
[6520ef5] | 271 | my $fs = 40.0 * $frac**0.125; |
---|
| 272 | |
---|
[81dea5b] | 273 | printf DOT ("N%s -> N%s [label=\"%s\", weight=%d, style=\"%s\", fontsize=%.1f];\n", |
---|
[6520ef5] | 274 | $pc, $cpc, format_bytes($child_size), $weight, $style, $fs); |
---|
[ebd57f8] | 275 | } |
---|
| 276 | } |
---|
| 277 | |
---|
[81dea5b] | 278 | print DOT ("}\n"); |
---|
| 279 | |
---|
| 280 | close(DOT); |
---|
| 281 | |
---|
| 282 | system("dot -Tps2 < '$dot_fn' > '$ps_fn'"); |
---|
| 283 | system("ps2pdf '$ps_fn' '$pdf_fn'"); |
---|
| 284 | |
---|
| 285 | exit 0; |
---|
[ebd57f8] | 286 | |
---|