| 1 |
#!/usr/bin/perl |
| 2 |
########################################################## |
| 3 |
## This script is part of the Devel::NYTProf distribution |
| 4 |
## Released under the same terms as Perl 5.8.0 |
| 5 |
## See http://metacpan.org/release/Devel-NYTProf/ |
| 6 |
## |
| 7 |
########################################################## |
| 8 |
use warnings; |
| 9 |
use strict; |
| 10 |
|
| 11 |
use Getopt::Long; |
| 12 |
|
| 13 |
use Devel::NYTProf::Data; |
| 14 |
|
| 15 |
|
| 16 |
my %opt = ( |
| 17 |
file => 'nytprof.out', |
| 18 |
out => 'nytprof.callgrind', |
| 19 |
); |
| 20 |
|
| 21 |
GetOptions( \%opt, qw/file|f=s out|o=s help|h/ ) |
| 22 |
or usage(); |
| 23 |
|
| 24 |
usage() if $opt{help}; |
| 25 |
|
| 26 |
|
| 27 |
print "Reading $opt{file} ...\n"; |
| 28 |
|
| 29 |
my $profile = Devel::NYTProf::Data->new( { filename => $opt{file}, |
| 30 |
quiet => 1 } ); |
| 31 |
|
| 32 |
print "Writing $opt{out} ...\n"; |
| 33 |
|
| 34 |
# calltree format specification |
| 35 |
# http://kcachegrind.sourceforge.net/cgi-bin/show.cgi/KcacheGrindCalltreeFormat |
| 36 |
|
| 37 |
open my $fh, '>', $opt{out} |
| 38 |
or die "Can't write to $opt{out}: $!\n"; |
| 39 |
|
| 40 |
print $fh "events: Ticks".$/; |
| 41 |
print $fh $/; |
| 42 |
|
| 43 |
|
| 44 |
my %callmap; |
| 45 |
my $subname_subinfo_map = $profile->subname_subinfo_map; |
| 46 |
|
| 47 |
for my $sub (values %$subname_subinfo_map) { |
| 48 |
|
| 49 |
my $callers = $sub->caller_fid_line_places; |
| 50 |
next unless ($callers && %$callers); |
| 51 |
|
| 52 |
my $fi = eval { $sub->fileinfo }; |
| 53 |
|
| 54 |
print $fh 'fl='.( $fi ? $fi->filename : "Unknown").$/; |
| 55 |
print $fh 'fn='.$sub->subname.$/; |
| 56 |
print $fh join(' ',$sub->first_line, int($sub->excl_time * 1_000_000)).$/; |
| 57 |
print $fh $/; |
| 58 |
|
| 59 |
my @callers; |
| 60 |
while ( my ( $fid, $fid_line_info ) = each %$callers ) { |
| 61 |
for my $line ( keys %$fid_line_info ) { |
| 62 |
my ( $count, $incl_time, $excl_time, undef, undef, undef, |
| 63 |
undef, $calling_subs) = @{ $fid_line_info->{$line} }; |
| 64 |
|
| 65 |
my @subnames = sort keys %$calling_subs; |
| 66 |
|
| 67 |
ref $_ and $_ = sprintf "%s (merge of %d subs)", $_->[0], scalar @$_ |
| 68 |
for @subnames; |
| 69 |
my $subname = (@subnames) ? join( " or ", @subnames ) : "__main"; |
| 70 |
|
| 71 |
my $fi = $profile->fileinfo_of($fid); |
| 72 |
my $filename = $fi->filename($fid); |
| 73 |
my $line_desc = "line $line of $filename"; |
| 74 |
|
| 75 |
# chase string eval chain back to a real file |
| 76 |
while ( my ( $outer_fileinfo, $outer_line ) = $fi->outer ) { |
| 77 |
( $filename, $line ) = ( $outer_fileinfo->filename, $outer_line ); |
| 78 |
$line_desc .= sprintf " at line %s of %s", $line, $filename; |
| 79 |
$fi = $outer_fileinfo; |
| 80 |
} |
| 81 |
|
| 82 |
push @{ $callmap{$subname} }, [ $filename, $line, $sub, $count, $incl_time, $excl_time ]; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
for (keys %callmap) { |
| 89 |
for my $entry (@{$callmap{$_}}) { |
| 90 |
my ($filename, $line, $sub, $count, $incl_time, $excl_time) = @$entry; |
| 91 |
print $fh "fl=$filename$/"; |
| 92 |
print $fh 'fn='.$_.$/; |
| 93 |
print $fh "cfl=".(eval { $sub->fileinfo->filename } || 'Unknown').$/; |
| 94 |
print $fh "cfn=".$sub->subname.$/; |
| 95 |
# calls=(Call Count) (Destination position) |
| 96 |
# (Source position) (Inclusive cost of call) |
| 97 |
print $fh "calls=$count ".$sub->first_line.$/; |
| 98 |
print $fh "$line ".int(1_000_000 * $incl_time).$/; |
| 99 |
print $fh $/; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
sub usage { |
| 104 |
print <<END; |
| 105 |
usage: [perl] nytprofcg [opts] |
| 106 |
--file <file>, -f <file> Specify NYTProf data file [default: nytprof.out] |
| 107 |
--out <file>, -o <file> Specify output file [default: nytprof.callgrind] |
| 108 |
--help, -h Print this message |
| 109 |
|
| 110 |
This script is part of the Devel::NYTProf distribution. |
| 111 |
Released under the same terms as Perl 5.8.0 |
| 112 |
See http://metacpan.org/release/Devel-NYTProf/ |
| 113 |
END |
| 114 |
exit 1; |
| 115 |
} |
| 116 |
|
| 117 |
__END__ |
| 118 |
|
| 119 |
=head1 NAME |
| 120 |
|
| 121 |
nytprofcg - Convert an NYTProf profile into Callgrind format |
| 122 |
|
| 123 |
=head1 SYNOPSIS |
| 124 |
|
| 125 |
$ nytprofcg --file=nytprof.out --out=nytprof.callgrind |
| 126 |
|
| 127 |
$ nytprofcg # same as above |
| 128 |
|
| 129 |
=head1 DESCRIPTION |
| 130 |
|
| 131 |
Reads a profile data file generated by Devel::NYTProf and writes out the |
| 132 |
subroutine call graph information it contains in Callgrind format. |
| 133 |
|
| 134 |
The output Callgrind file can be loaded into the C<kcachegrind> GUI for |
| 135 |
interactive exploration. |
| 136 |
|
| 137 |
For more information see L<http://kcachegrind.github.io/html/Home.html> |
| 138 |
|
| 139 |
=cut |