#!perl # This finds which specific lines are the hot points in a profile, # assuming that the code was compiled with debugging information. # # Parameters: method name, profile(s) to analyze # For example: # perl wherehot.pl simpleConstant2 SlowProduct.31.hprof.txt # # You can give multiple profile arguments - # you can even say: # perl wherehot.pl simpleConstant2 SlowProduct.*.hprof.txt # use strict; use vars qw[$state $limit $method $methodregexp $limitregexp $uselast]; # $state = "cpu", "trace", "other" $state = "other"; $method = shift; $limit = undef; if ($method eq "-l") { $limit = shift; $method = shift; } $uselast = 0; if ($method =~ s/^-//) {$uselast=1;} $methodregexp = qr/\t((?:.*\.)?(?:$method))\((.*:(?:\d+|Unknown line))\)/; if ($limit) { $limitregexp = qr/[\t.](?:$limit)\(/; $limitregexp = qr/\((?:$limit)\)/ if $limit =~ /:/; } else { $limitregexp = $methodregexp; } if (!$method or !@ARGV) { print < total samples my %linetimes = (); # trace number -> description my %traces = (); my $sampletot = 0; my $trace = 0; my $limitmatched = 1; while (<>) { if ($state eq "other") { if (/^TRACE (\d+):/) { $trace = $1; $state = "trace"; $limitmatched = 0; } elsif (/^CPU SAMPLES BEGIN .total = (\d+)/) { $sampletot += $1; $state = "cpu"; } } elsif ($state eq "trace") { if (/^\t/) { if (($uselast or !$traces{$trace}) and /$methodregexp/) { my ($method,$line) = ($1,$2); $line = "Unknown" if $line =~ /Unknown/; $traces{$trace} = "$line in $method"; } if (/$limitregexp/) { $limitmatched = 1; } } else { if (!$limitmatched) { delete $traces{$trace}; } $state = "other"; redo; } } elsif ($state eq "cpu") { if (/CPU SAMPLES END/) { $state = "other"; next; } elsif (/^\s*\d+(.*)/) { my @data = split(/\s+/, $1); if ($traces{$data[4]}) { $linetimes{$traces{$data[4]}} += $data[3]; } } } } continue { if (eof) { $state = "other"; %traces = (); } } print "$sampletot\tTotal Samples\n"; foreach my $key (sort {$linetimes{$b} <=> $linetimes{$a}} keys(%linetimes)) { printf("%d\t%s\n", $linetimes{$key}, $key); } #### -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/