[ebd57f8] | 1 | #! /usr/bin/env perl |
---|
| 2 | use strict; |
---|
| 3 | use File::Basename; |
---|
[c49b79c] | 4 | use Getopt::Long; |
---|
| 5 | |
---|
[9fcaba3] | 6 | my $find_leaks = 0; |
---|
[c49b79c] | 7 | my $print_raw_proc_name = 0; |
---|
| 8 | my $quiet = 0; |
---|
| 9 | my $help = 0; |
---|
| 10 | |
---|
| 11 | sub print_usage($) { |
---|
| 12 | my $ec = $_[0]; |
---|
| 13 | my $usage = <<EOM; |
---|
[5df7203] | 14 | Usage: $0 [options] <memlog file or glob> [<output directory>] |
---|
[c49b79c] | 15 | options: |
---|
[9fcaba3] | 16 | --leaks |
---|
| 17 | Provide information on leaks instead of peak usage |
---|
[c49b79c] | 18 | --print-symbol-names |
---|
| 19 | Include symbol names and offsets in the output |
---|
| 20 | --quiet or -q |
---|
| 21 | Don't print status messages while running |
---|
| 22 | EOM |
---|
| 23 | |
---|
| 24 | print $usage; |
---|
| 25 | exit($ec); |
---|
| 26 | } |
---|
| 27 | |
---|
[3533094] | 28 | GetOptions("help|h|?" => \$help, |
---|
[9fcaba3] | 29 | "leaks" => \$find_leaks, |
---|
[c49b79c] | 30 | "print-symbol-names" => \$print_raw_proc_name, |
---|
| 31 | "quiet|q" => \$quiet) |
---|
| 32 | or print_usage(1); |
---|
| 33 | |
---|
| 34 | if ($help) { |
---|
| 35 | print_usage(0); |
---|
| 36 | } |
---|
[ebd57f8] | 37 | |
---|
[81dea5b] | 38 | my $memlog_fn = $ARGV[0]; |
---|
| 39 | my $out_dir = $ARGV[1] || '.'; |
---|
| 40 | |
---|
| 41 | if (! -f $memlog_fn) { |
---|
[5df7203] | 42 | my @pot_fns = glob($memlog_fn); |
---|
| 43 | if (scalar(@pot_fns)) { |
---|
| 44 | if (!$quiet) { |
---|
| 45 | print "Searching all files matching '$memlog_fn'\n"; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | my $pot_max_rss = 0; |
---|
| 49 | my $pos_max_rss_fn; |
---|
| 50 | foreach my $pot_fn (@pot_fns) { |
---|
| 51 | my $last_line = `tail -n 1 '$pot_fn'`; |
---|
| 52 | chomp($last_line); |
---|
| 53 | |
---|
| 54 | my @parts = split(/\t/, $last_line); |
---|
| 55 | |
---|
| 56 | my $op = shift(@parts); |
---|
| 57 | my $state = shift(@parts); |
---|
| 58 | |
---|
| 59 | my ($time, $then_max_rss, $tid) = split(/\s+/, $state); |
---|
| 60 | if ($pot_max_rss < $then_max_rss) { |
---|
| 61 | $pot_max_rss = $then_max_rss; |
---|
| 62 | $pos_max_rss_fn = $pot_fn; |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | if (defined $pos_max_rss_fn) { |
---|
| 67 | $memlog_fn = $pos_max_rss_fn; |
---|
| 68 | goto have_memlog_fn; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
[c49b79c] | 72 | print_usage(1); |
---|
[81dea5b] | 73 | } |
---|
[5df7203] | 74 | have_memlog_fn: |
---|
[81dea5b] | 75 | |
---|
[747d2b1] | 76 | # The version of addr2line and friends that you use can make a big difference, |
---|
| 77 | # especially on BE ppc64, where older versions of addr2line did not account |
---|
| 78 | # correctly for the function descriptor setup. At ALCF, on the BG/Q, we have |
---|
| 79 | # newer versions not in the default search path (from bgclang). |
---|
| 80 | my $alcf_bu_dir = '/soft/compilers/bgclang/current/binutils/bin'; |
---|
| 81 | if (-d $alcf_bu_dir) { |
---|
| 82 | $ENV{'PATH'} = $alcf_bu_dir . ':' . $ENV{'PATH'}; |
---|
| 83 | } |
---|
| 84 | |
---|
[81dea5b] | 85 | open(MEMLOG, $memlog_fn) || die "Can't open $memlog_fn: $!"; |
---|
| 86 | |
---|
[0edbc98] | 87 | # The first step is to determine the high-water mark. |
---|
| 88 | my $max_rss = 0; |
---|
| 89 | foreach my $line (<MEMLOG>) { |
---|
| 90 | chomp($line); |
---|
| 91 | my @parts = split(/\t/, $line); |
---|
| 92 | |
---|
| 93 | my $op = shift(@parts); |
---|
| 94 | my $state = shift(@parts); |
---|
| 95 | |
---|
| 96 | my ($time, $then_max_rss, $tid) = split(/\s+/, $state); |
---|
| 97 | if ($max_rss < $then_max_rss) { |
---|
| 98 | $max_rss = $then_max_rss; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | seek(MEMLOG, 0, 0); |
---|
| 103 | |
---|
| 104 | # Scan the log for malloc/free pairings. We're interested only in active |
---|
| 105 | # allocations at the time when the rss reaches the final maxrss. |
---|
[9fcaba3] | 106 | # If we're finding leaks, then go to the very end. |
---|
| 107 | my $active_alloc_time = 0; |
---|
[0edbc98] | 108 | my %malloc_lines; |
---|
| 109 | foreach my $line (<MEMLOG>) { |
---|
| 110 | chomp($line); |
---|
| 111 | my @parts = split(/\t/, $line); |
---|
| 112 | |
---|
| 113 | my $op = shift(@parts); |
---|
| 114 | my $state = shift(@parts); |
---|
| 115 | |
---|
| 116 | if ($op =~ /^M:/) { |
---|
| 117 | my ($size, $ptr) = ($op =~ /^M: (\d+) 0x(\w+)/); |
---|
| 118 | $malloc_lines{$ptr} = $line; |
---|
| 119 | } elsif ($op =~ /^F:/) { |
---|
| 120 | my ($ptr) = ($op =~ /^F: 0x(\w+)/); |
---|
| 121 | delete $malloc_lines{$ptr}; |
---|
| 122 | } else { |
---|
| 123 | next; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | my ($time, $then_max_rss, $tid) = split(/\s+/, $state); |
---|
[9fcaba3] | 127 | $active_alloc_time = $time; |
---|
| 128 | |
---|
| 129 | if (!$find_leaks) { |
---|
| 130 | # If we've reached the max rss, we've seen all we need to see. |
---|
| 131 | if ($then_max_rss == $max_rss) { |
---|
| 132 | last; |
---|
| 133 | } |
---|
[0edbc98] | 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | close(MEMLOG); |
---|
| 138 | |
---|
| 139 | # Convert maxrss, currently in KB, to bytes. |
---|
| 140 | $max_rss *= 1024; |
---|
| 141 | |
---|
[ebd57f8] | 142 | my $total_size = 0; |
---|
| 143 | my %roots; |
---|
[c4f89cf] | 144 | my %all_nodes; |
---|
[0edbc98] | 145 | foreach my $line (values %malloc_lines) { |
---|
[ebd57f8] | 146 | my @parts = split(/\t/, $line); |
---|
| 147 | |
---|
| 148 | my $op = shift(@parts); |
---|
| 149 | my $state = shift(@parts); |
---|
| 150 | |
---|
| 151 | # Only dealing with allocations here... |
---|
| 152 | if ($op !~ /^M:/) { |
---|
| 153 | next; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | my ($size, $ptr) = ($op =~ /^M: (\d+) 0x(\w+)/); |
---|
[0edbc98] | 157 | my ($time, $then_max_rss, $tid) = split(/\s+/, $state); |
---|
[ebd57f8] | 158 | |
---|
| 159 | $total_size += $size; |
---|
| 160 | |
---|
| 161 | sub level_parts($) { |
---|
| 162 | my $level = @_[0]; |
---|
| 163 | my ($file_name, $proc_name, $off, $pc, $relpc) = |
---|
| 164 | ($level =~ /^(.*) \((.*)\+0x(\w+)\) \[0x(\w+) \(0x(\w+)\)\]/); |
---|
| 165 | |
---|
| 166 | return ($file_name, $proc_name, $off, $pc, $relpc); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | # Put the top of the stack first. |
---|
| 170 | @parts = reverse(@parts); |
---|
| 171 | |
---|
| 172 | my $parent = \%roots; |
---|
| 173 | for (my $i = 0; $i < scalar(@parts); ++$i) { |
---|
| 174 | my $level = $parts[$i]; |
---|
| 175 | my ($file_name, $proc_name, $off, $pc, $relpc) = level_parts($level); |
---|
| 176 | |
---|
| 177 | # Skip this level if we don't even know from what file it came. |
---|
| 178 | if ($file_name eq '?') { |
---|
| 179 | next; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | # print STDERR "parsed: $file_name, $proc_name, $off, $pc, $relpc\n"; |
---|
| 183 | |
---|
[c4f89cf] | 184 | if (!exists $all_nodes{$pc}) { |
---|
| 185 | $all_nodes{$pc}->{'file_name'} = $file_name; |
---|
| 186 | $all_nodes{$pc}->{'proc_name'} = $proc_name; |
---|
| 187 | $all_nodes{$pc}->{'off'} = $off; |
---|
| 188 | $all_nodes{$pc}->{'pc'} = $pc; |
---|
| 189 | $all_nodes{$pc}->{'relpc'} = $relpc; |
---|
| 190 | } |
---|
[ebd57f8] | 191 | |
---|
[c4f89cf] | 192 | if (!exists $parent->{$pc}) { |
---|
| 193 | $parent->{$pc} = $all_nodes{$pc}; |
---|
[ebd57f8] | 194 | } |
---|
| 195 | |
---|
| 196 | $parent->{$pc}->{'size'} += $size; |
---|
| 197 | |
---|
| 198 | my ($next_file_name, $next_proc_name, $next_off, $next_pc, $next_relpc); |
---|
| 199 | if ($i < scalar(@parts)-1) { |
---|
| 200 | my $next_level = $parts[$i+1]; |
---|
| 201 | ($next_file_name, $next_proc_name, $next_off, $next_pc, $next_relpc) = |
---|
| 202 | level_parts($next_level); |
---|
| 203 | $parent->{$pc}->{'child_sizes'}->{$next_pc} += $size; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | if (!exists $parent->{'children'}) { |
---|
| 207 | $parent->{'children'} = {}; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | $parent = $parent->{'children'}; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
[20b45de] | 214 | my $txt_fn = "$out_dir/" . basename($memlog_fn) . ".txt"; |
---|
[81dea5b] | 215 | my $dot_fn = "$out_dir/" . basename($memlog_fn) . ".dot"; |
---|
| 216 | my $ps_fn = "$out_dir/" . basename($memlog_fn) . ".ps"; |
---|
| 217 | my $pdf_fn = "$out_dir/" . basename($memlog_fn) . ".pdf"; |
---|
| 218 | |
---|
[c49b79c] | 219 | if (!$quiet) { |
---|
| 220 | print "Creating $txt_fn\n"; |
---|
| 221 | } |
---|
| 222 | |
---|
[20b45de] | 223 | open(TXT, ">$txt_fn") || die "Can't open $txt_fn: $!"; |
---|
[c49b79c] | 224 | |
---|
| 225 | if (!$quiet) { |
---|
| 226 | print "Creating $dot_fn\n"; |
---|
| 227 | } |
---|
| 228 | |
---|
[81dea5b] | 229 | open(DOT, ">$dot_fn") || die "Can't open $dot_fn: $!"; |
---|
| 230 | |
---|
[ebd57f8] | 231 | sub format_bytes($) { |
---|
| 232 | my @sizes = qw( B KB MB GB TB PB ); |
---|
| 233 | my $size = $_[0]; |
---|
| 234 | |
---|
| 235 | my $i = 0; |
---|
| 236 | while ($size > 1024) { |
---|
| 237 | $size /= 1024; |
---|
| 238 | ++$i; |
---|
| 239 | } |
---|
| 240 | |
---|
[4bd7a88] | 241 | return sprintf("%.3f $sizes[$i]", $size); |
---|
[ebd57f8] | 242 | } |
---|
| 243 | |
---|
[bdaf020] | 244 | print DOT ("digraph \"memlog\" {\n"); |
---|
[81dea5b] | 245 | print DOT ("size=\"8,11\";\n"); |
---|
| 246 | print DOT ("node [width=0.375,height=0.25];\n"); |
---|
[ebd57f8] | 247 | |
---|
[9fcaba3] | 248 | my $find_type = $find_leaks ? " (leaks)" : ""; |
---|
[bdaf020] | 249 | printf DOT ("Legend [shape=box, fontsize=100, shape=oval," . |
---|
[9fcaba3] | 250 | "label=\"Total: %s active$find_type at maxrss = %s after %s s\"];\n", |
---|
| 251 | format_bytes($total_size), format_bytes($max_rss), |
---|
| 252 | $active_alloc_time); |
---|
[bdaf020] | 253 | |
---|
[9fcaba3] | 254 | printf TXT ("memlog: Total: %s active$find_type at maxrss = %s after %s s\n\n", |
---|
| 255 | format_bytes($total_size), format_bytes($max_rss), |
---|
| 256 | $active_alloc_time); |
---|
[20b45de] | 257 | |
---|
[ebd57f8] | 258 | my %cached_names; |
---|
| 259 | sub get_name($) { |
---|
| 260 | my $node = $_[0]; |
---|
| 261 | my $pc = $node->{'pc'}; |
---|
| 262 | |
---|
| 263 | if (exists $cached_names{$pc}) { |
---|
| 264 | return $cached_names{$pc}; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | my $ret = ''; |
---|
| 268 | |
---|
| 269 | # Prefer the relative offset (that is what we want for shared libraries), but |
---|
| 270 | # if is not available, use the full offset (which is what we want for the |
---|
| 271 | # base executable). |
---|
| 272 | my $exe_off = $node->{'relpc'}; |
---|
| 273 | if (!$exe_off) { |
---|
| 274 | $exe_off = $pc; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | my $file_name = $node->{'file_name'}; |
---|
[518793d] | 278 | |
---|
| 279 | # If we don't have an absolute path, this is probably the base dynamic |
---|
| 280 | # executable, so ask the shell which one it used (not foolproof because we |
---|
| 281 | # might not have the same PATH now, but hopefully is generally the right |
---|
| 282 | # thing). |
---|
| 283 | if ($file_name !~ /^\//) { |
---|
| 284 | $file_name = `which '$file_name'`; |
---|
| 285 | chomp($file_name); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | my ($func, $loc) = `addr2line -e '$file_name' -f 0x$exe_off`; |
---|
[ebd57f8] | 289 | chomp($func); |
---|
| 290 | chomp($loc); |
---|
| 291 | |
---|
| 292 | if ($func !~ /^\?/) { |
---|
[6520ef5] | 293 | # In general, this function name might look something like: |
---|
| 294 | # 00000329.plt_call.wcsnrtombs@@GLIBC_2.3+0 |
---|
[10599da] | 295 | $func =~ s/@.*//; # Remove trailing symbol version strings |
---|
[ebd57f8] | 296 | $func =~ s/.*\.//; |
---|
| 297 | $func = `c++filt '$func'`; |
---|
| 298 | chomp($func); |
---|
| 299 | |
---|
[1bd82e0] | 300 | # It sometimes happens that addr2line is a bit too smart: when debugging |
---|
| 301 | # information is available, it might print a local alias for the |
---|
| 302 | # function instead of the full name (for example, printing 'List' |
---|
| 303 | # instead of 'Foo::List<int>::List(int, int const&)'). |
---|
| 304 | if ($node->{'proc_name'} ne '?') { |
---|
| 305 | my $proc_name = $node->{'proc_name'}; |
---|
| 306 | $proc_name = `c++filt '$proc_name'`; |
---|
| 307 | chomp($proc_name); |
---|
| 308 | |
---|
| 309 | if (length($proc_name) > length($func)) { |
---|
| 310 | $func = $proc_name; |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | |
---|
[081a0fa] | 314 | $ret .= $func . '\n'; |
---|
| 315 | |
---|
[77ebdb8] | 316 | if ($loc !~ /^[:?]/) { |
---|
[081a0fa] | 317 | $ret .= $loc . '\n'; |
---|
| 318 | } |
---|
[4bd7a88] | 319 | } elsif ($node->{'proc_name'} ne '?') { |
---|
| 320 | my $proc_name = $node->{'proc_name'}; |
---|
| 321 | $proc_name = `c++filt '$proc_name'`; |
---|
| 322 | chomp($proc_name); |
---|
| 323 | |
---|
| 324 | $ret .= $proc_name . '\n'; |
---|
[ebd57f8] | 325 | } |
---|
| 326 | |
---|
[6bfe6a1] | 327 | $ret .= $node->{'file_name'}; |
---|
| 328 | if ($print_raw_proc_name and $node->{'proc_name'} ne '?') { |
---|
| 329 | $ret .= '\n' . $node->{'proc_name'} . '+0x' . $node->{'off'}; |
---|
[ebd57f8] | 330 | } |
---|
| 331 | |
---|
| 332 | $cached_names{$pc} = $ret; |
---|
| 333 | return $ret; |
---|
| 334 | } |
---|
| 335 | |
---|
[4bd7a88] | 336 | my $skip_frac = 0.01; |
---|
| 337 | my %skipped; |
---|
| 338 | |
---|
[c4f89cf] | 339 | foreach my $pc (keys %all_nodes) { |
---|
| 340 | my $node = $all_nodes{$pc}; |
---|
[ebd57f8] | 341 | |
---|
| 342 | my $local_size = $node->{'size'}; |
---|
[4bd7a88] | 343 | if ($local_size * 1.0 / $total_size < $skip_frac) { |
---|
[c4f89cf] | 344 | $skipped{$pc} = 1; |
---|
[4bd7a88] | 345 | next; |
---|
| 346 | } |
---|
[ebd57f8] | 347 | |
---|
| 348 | my $fs = 8.0; |
---|
| 349 | if ($local_size > 0) { |
---|
[4bd7a88] | 350 | $fs = 50.0 * (abs($local_size * 1.0 / $total_size))**0.125; |
---|
[ebd57f8] | 351 | } |
---|
| 352 | |
---|
[20b45de] | 353 | my $name = get_name($node); |
---|
| 354 | |
---|
[81dea5b] | 355 | printf DOT ("N%s [label=\"%s\\n%s\", shape=box, fontsize=%.1f%s];\n", |
---|
[c4f89cf] | 356 | $pc, $name, format_bytes($local_size), $fs); |
---|
[ebd57f8] | 357 | } |
---|
| 358 | |
---|
[c4f89cf] | 359 | foreach my $pc (keys %all_nodes) { |
---|
| 360 | if ($skipped{$pc}) { |
---|
[4bd7a88] | 361 | next; |
---|
| 362 | } |
---|
[ebd57f8] | 363 | |
---|
[20b45de] | 364 | my $node = $all_nodes{$pc}; |
---|
| 365 | my $local_size = $node->{'size'}; |
---|
| 366 | |
---|
[ebd57f8] | 367 | foreach my $cpc (keys %{$node->{'child_sizes'}}) { |
---|
[4bd7a88] | 368 | if ($skipped{$cpc}) { |
---|
| 369 | next; |
---|
| 370 | } |
---|
| 371 | |
---|
[ebd57f8] | 372 | my $child_size = $node->{'child_sizes'}->{$cpc}; |
---|
| 373 | my $frac = $child_size * 1.0 / $local_size; |
---|
| 374 | |
---|
[4bd7a88] | 375 | my $weight = 100.0 * sqrt($frac); |
---|
[6520ef5] | 376 | my $style = sprintf("setlinewidth(%f)", 8.0 * sqrt($frac)); |
---|
[ebd57f8] | 377 | |
---|
[6520ef5] | 378 | my $fs = 40.0 * $frac**0.125; |
---|
| 379 | |
---|
[81dea5b] | 380 | printf DOT ("N%s -> N%s [label=\"%s\", weight=%d, style=\"%s\", fontsize=%.1f];\n", |
---|
[6520ef5] | 381 | $pc, $cpc, format_bytes($child_size), $weight, $style, $fs); |
---|
[ebd57f8] | 382 | } |
---|
| 383 | } |
---|
| 384 | |
---|
[81dea5b] | 385 | print DOT ("}\n"); |
---|
| 386 | |
---|
[20b45de] | 387 | foreach my $pc (sort { $all_nodes{$::b}->{'size'} <=> |
---|
| 388 | $all_nodes{$::a}->{'size'} } keys %all_nodes) { |
---|
| 389 | if ($skipped{$pc}) { |
---|
| 390 | next; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | print TXT ('*' x 80) . "\n\n"; |
---|
| 394 | |
---|
| 395 | my $node = $all_nodes{$pc}; |
---|
| 396 | |
---|
| 397 | my $local_size = $node->{'size'}; |
---|
| 398 | printf TXT ("%s - %.1f%%\n", format_bytes($local_size), |
---|
| 399 | $local_size * 100.0 / $total_size); |
---|
| 400 | |
---|
| 401 | my $name = get_name($node); |
---|
| 402 | $name =~ s/\\n/\n/g; |
---|
| 403 | |
---|
| 404 | print TXT "$name\n$pc\n"; |
---|
| 405 | |
---|
| 406 | print TXT "\nMEMORY ALLOCATED BY CALLEES:\n"; |
---|
| 407 | |
---|
| 408 | foreach my $cpc (sort { $node->{'child_sizes'}->{$::b} <=> |
---|
| 409 | $node->{'child_sizes'}->{$::a} } |
---|
| 410 | keys %{$node->{'child_sizes'}}) { |
---|
| 411 | if ($skipped{$cpc}) { |
---|
| 412 | next; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | my $child_node = $all_nodes{$cpc}; |
---|
| 416 | my $child_size = $node->{'child_sizes'}->{$cpc}; |
---|
| 417 | |
---|
| 418 | printf TXT ("\t%s - %.1f%%\n", format_bytes($child_size), |
---|
| 419 | $child_size * 100.0 / $local_size); |
---|
| 420 | |
---|
| 421 | my $child_name = get_name($child_node); |
---|
| 422 | $child_name =~ s/\\n/\n\t/g; |
---|
| 423 | print TXT "\t$child_name\n\t$cpc\n"; |
---|
| 424 | |
---|
| 425 | print TXT "\n"; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | print TXT "\n"; |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | close(TXT); |
---|
[81dea5b] | 432 | close(DOT); |
---|
| 433 | |
---|
[c49b79c] | 434 | if (!$quiet) { |
---|
| 435 | print "Creating $ps_fn\n"; |
---|
| 436 | } |
---|
| 437 | |
---|
[81dea5b] | 438 | system("dot -Tps2 < '$dot_fn' > '$ps_fn'"); |
---|
[c49b79c] | 439 | |
---|
| 440 | if (!$quiet) { |
---|
| 441 | print "Creating $pdf_fn\n"; |
---|
| 442 | } |
---|
| 443 | |
---|
[81dea5b] | 444 | system("ps2pdf '$ps_fn' '$pdf_fn'"); |
---|
| 445 | |
---|
| 446 | exit 0; |
---|
[ebd57f8] | 447 | |
---|