1N/A#!/usr/local/bin/perl
1N/A
1N/Ause Config;
1N/Ause File::Basename qw(&basename &dirname);
1N/Ause File::Spec;
1N/A
1N/A# List explicitly here the variables you want Configure to
1N/A# generate. Metaconfig only looks for shell variables, so you
1N/A# have to mention them as if they were shell variables, not
1N/A# %Config entries. Thus you write
1N/A# $startperl
1N/A# to ensure Configure will look for $Config{startperl}.
1N/A
1N/A# This forces PL files to create target in same directory as PL file.
1N/A# This is so that make depend always knows where to find PL derivatives.
1N/Achdir(dirname($0));
1N/A($file = basename($0)) =~ s/\.PL$//i;
1N/A$file .= '.COM' if ($^O eq 'VMS');
1N/A
1N/Amy $dprof_pm = File::Spec->catfile(File::Spec->updir, 'ext', 'Devel', 'DProf', 'DProf.pm');
1N/Amy $VERSION = 0;
1N/Aopen( PM, "<$dprof_pm" ) || die "Can't open $dprof_pm: $!";
1N/Awhile(<PM>){
1N/A if( /^\$Devel::DProf::VERSION\s*=\s*'([\d._]+)'/ ){
1N/A $VERSION = $1;
1N/A last;
1N/A }
1N/A}
1N/Aclose PM;
1N/Aif( $VERSION == 0 ){
1N/A die "Did not find VERSION in $dprof_pm";
1N/A}
1N/Amy $stty = 'undef';
1N/Aforeach my $s (qw(/bin/stty /usr/bin/stty)) {
1N/A if (-x $s) {
1N/A $stty = qq["$s"];
1N/A last;
1N/A }
1N/A}
1N/Aopen OUT,">$file" or die "Can't create $file: $!";
1N/A
1N/Aprint "Extracting $file (with variable substitutions)\n";
1N/A
1N/A# In this section, perl variables will be expanded during extraction.
1N/A# You can use $Config{...} to use Configure variables.
1N/A
1N/Aprint OUT <<"!GROK!THIS!";
1N/A$Config{'startperl'}
1N/A eval 'exec perl -S \$0 "\$@"'
1N/A if 0;
1N/A
1N/Arequire 5.003;
1N/A
1N/Amy \$VERSION = '$VERSION';
1N/Amy \$stty = $stty;
1N/A
1N/A!GROK!THIS!
1N/A
1N/A# In the following, perl variables are not expanded during extraction.
1N/A
1N/Aprint OUT <<'!NO!SUBS!';
1N/A=head1 NAME
1N/A
1N/Adprofpp - display perl profile data
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/Adprofpp [B<-a>|B<-z>|B<-l>|B<-v>|B<-U>] [B<-d>] [B<-s>|B<-r>|B<-u>] [B<-q>] [B<-F>] [B<-I|-E>] [B<-O cnt>] [B<-A>] [B<-R>] [B<-S>] [B<-g subroutine>] [B<-G> <regexp> [B<-P>]] [B<-f> <regexp>] [profile]
1N/A
1N/Adprofpp B<-T> [B<-F>] [B<-g subroutine>] [profile]
1N/A
1N/Adprofpp B<-t> [B<-F>] [B<-g subroutine>] [profile]
1N/A
1N/Adprofpp B<-G> <regexp> [B<-P>] [profile]
1N/A
1N/Adprofpp B<-p script> [B<-Q>] [other opts]
1N/A
1N/Adprofpp B<-V> [profile]
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AThe I<dprofpp> command interprets profile data produced by a profiler, such
1N/Aas the Devel::DProf profiler. Dprofpp will read the file F<tmon.out> and
1N/Awill display the 15 subroutines which are using the most time. By default
1N/Athe times for each subroutine are given exclusive of the times of their
1N/Achild subroutines.
1N/A
1N/ATo profile a Perl script run the perl interpreter with the B<-d> switch. So
1N/Ato profile script F<test.pl> with Devel::DProf the following command should
1N/Abe used.
1N/A
1N/A $ perl5 -d:DProf test.pl
1N/A
1N/AThen run dprofpp to analyze the profile. The output of dprofpp depends
1N/Aon the flags to the program and the version of Perl you're using.
1N/A
1N/A $ dprofpp -u
1N/A Total Elapsed Time = 1.67 Seconds
1N/A User Time = 0.61 Seconds
1N/A Exclusive Times
1N/A %Time Seconds #Calls sec/call Name
1N/A 52.4 0.320 2 0.1600 main::foo
1N/A 45.9 0.280 200 0.0014 main::bar
1N/A 0.00 0.000 1 0.0000 DynaLoader::import
1N/A 0.00 0.000 1 0.0000 main::baz
1N/A
1N/AThe dprofpp tool can also run the profiler before analyzing the profile
1N/Adata. The above two commands can be executed with one dprofpp command.
1N/A
1N/A $ dprofpp -u -p test.pl
1N/A
1N/AConsult L<Devel::DProf/"PROFILE FORMAT"> for a description of the raw profile.
1N/A
1N/A=head1 OUTPUT
1N/A
1N/AColumns are:
1N/A
1N/A=over 4
1N/A
1N/A=item %Time
1N/A
1N/APercentage of time spent in this routine.
1N/A
1N/A=item #Calls
1N/A
1N/ANumber of calls to this routine.
1N/A
1N/A=item sec/call
1N/A
1N/AAverage number of seconds per call to this routine.
1N/A
1N/A=item Name
1N/A
1N/AName of routine.
1N/A
1N/A=item CumulS
1N/A
1N/ATime (in seconds) spent in this routine and routines called from it.
1N/A
1N/A=item ExclSec
1N/A
1N/ATime (in seconds) spent in this routine (not including those called
1N/Afrom it).
1N/A
1N/A=item Csec/c
1N/A
1N/AAverage time (in seconds) spent in each call of this routine
1N/A(including those called from it).
1N/A
1N/A=back
1N/A
1N/A=head1 OPTIONS
1N/A
1N/A=over 5
1N/A
1N/A=item B<-a>
1N/A
1N/ASort alphabetically by subroutine names.
1N/A
1N/A=item B<-d>
1N/A
1N/AReverse whatever sort is used
1N/A
1N/A=item B<-A>
1N/A
1N/ACount timing for autoloaded subroutine as timing for C<*::AUTOLOAD>.
1N/AOtherwise the time to autoload it is counted as time of the subroutine
1N/Aitself (there is no way to separate autoload time from run time).
1N/A
1N/AThis is going to be irrelevant with newer Perls. They will inform
1N/AC<Devel::DProf> I<when> the C<AUTOLOAD> switches to actual subroutine,
1N/Aso a separate statistics for C<AUTOLOAD> will be collected no matter
1N/Awhether this option is set.
1N/A
1N/A=item B<-R>
1N/A
1N/ACount anonymous subroutines defined in the same package separately.
1N/A
1N/A=item B<-E>
1N/A
1N/A(default) Display all subroutine times exclusive of child subroutine times.
1N/A
1N/A=item B<-F>
1N/A
1N/AForce the generation of fake exit timestamps if dprofpp reports that the
1N/Aprofile is garbled. This is only useful if dprofpp determines that the
1N/Aprofile is garbled due to missing exit timestamps. You're on your own if
1N/Ayou do this. Consult the BUGS section.
1N/A
1N/A=item B<-I>
1N/A
1N/ADisplay all subroutine times inclusive of child subroutine times.
1N/A
1N/A=item B<-l>
1N/A
1N/ASort by number of calls to the subroutines. This may help identify
1N/Acandidates for inlining.
1N/A
1N/A=item B<-O cnt>
1N/A
1N/AShow only I<cnt> subroutines. The default is 15.
1N/A
1N/A=item B<-p script>
1N/A
1N/ATells dprofpp that it should profile the given script and then interpret its
1N/Aprofile data. See B<-Q>.
1N/A
1N/A=item B<-Q>
1N/A
1N/AUsed with B<-p> to tell dprofpp to quit after profiling the script, without
1N/Ainterpreting the data.
1N/A
1N/A=item B<-q>
1N/A
1N/ADo not display column headers.
1N/A
1N/A=item B<-r>
1N/A
1N/ADisplay elapsed real times rather than user+system times.
1N/A
1N/A=item B<-s>
1N/A
1N/ADisplay system times rather than user+system times.
1N/A
1N/A=item B<-T>
1N/A
1N/ADisplay subroutine call tree to stdout. Subroutine statistics are
1N/Anot displayed.
1N/A
1N/A=item B<-t>
1N/A
1N/ADisplay subroutine call tree to stdout. Subroutine statistics are not
1N/Adisplayed. When a function is called multiple consecutive times at the same
1N/Acalling level then it is displayed once with a repeat count.
1N/A
1N/A=item B<-S>
1N/A
1N/ADisplay I<merged> subroutine call tree to stdout. Statistics are
1N/Adisplayed for each branch of the tree.
1N/A
1N/AWhen a function is called multiple (I<not necessarily consecutive>)
1N/Atimes in the same branch then all these calls go into one branch of
1N/Athe next level. A repeat count is output together with combined
1N/Ainclusive, exclusive and kids time.
1N/A
1N/ABranches are sorted w.r.t. inclusive time.
1N/A
1N/A=item B<-U>
1N/A
1N/ADo not sort. Display in the order found in the raw profile.
1N/A
1N/A=item B<-u>
1N/A
1N/ADisplay user times rather than user+system times.
1N/A
1N/A=item B<-V>
1N/A
1N/APrint dprofpp's version number and exit. If a raw profile is found then its
1N/AXS_VERSION variable will be displayed, too.
1N/A
1N/A=item B<-v>
1N/A
1N/ASort by average time spent in subroutines during each call. This may help
1N/Aidentify candidates for inlining.
1N/A
1N/A=item B<-z>
1N/A
1N/A(default) Sort by amount of user+system time used. The first few lines
1N/Ashould show you which subroutines are using the most time.
1N/A
1N/A=item B<-g> C<subroutine>
1N/A
1N/AIgnore subroutines except C<subroutine> and whatever is called from it.
1N/A
1N/A=item B<-G> <regexp>
1N/A
1N/AAggregate "Group" all calls matching the pattern together.
1N/AFor example this can be used to group all calls of a set of packages
1N/A
1N/A -G "(package1::)|(package2::)|(package3::)"
1N/A
1N/Aor to group subroutines by name:
1N/A
1N/A -G "getNum"
1N/A
1N/A=item B<-P>
1N/A
1N/AUsed with -G to aggregate "Pull" together all calls that did not match -G.
1N/A
1N/A=item B<-f> <regexp>
1N/A
1N/AFilter all calls matching the pattern.
1N/A
1N/A=back
1N/A
1N/A=head1 ENVIRONMENT
1N/A
1N/AThe environment variable B<DPROFPP_OPTS> can be set to a string containing
1N/Aoptions for dprofpp. You might use this if you prefer B<-I> over B<-E> or
1N/Aif you want B<-F> on all the time.
1N/A
1N/AThis was added fairly lazily, so there are some undesirable side effects.
1N/AOptions on the commandline should override options in DPROFPP_OPTS--but
1N/Adon't count on that in this version.
1N/A
1N/A=head1 BUGS
1N/A
1N/AApplications which call _exit() or exec() from within a subroutine
1N/Awill leave an incomplete profile. See the B<-F> option.
1N/A
1N/AAny bugs in Devel::DProf, or any profiler generating the profile data, could
1N/Abe visible here. See L<Devel::DProf/BUGS>.
1N/A
1N/AMail bug reports and feature requests to the perl5-porters mailing list at
1N/AF<E<lt>perl5-porters@perl.orgE<gt>>. Bug reports should include the
1N/Aoutput of the B<-V> option.
1N/A
1N/A=head1 FILES
1N/A
1N/A dprofpp - profile processor
1N/A tmon.out - raw profile
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AL<perl>, L<Devel::DProf>, times(2)
1N/A
1N/A=cut
1N/A
1N/Ause Getopt::Std 'getopts';
1N/Ause Config '%Config';
1N/A
1N/ASetup: {
1N/A my $options = 'O:g:G:Pf:dlzaAvuTtqrRsUFEIp:QVS';
1N/A
1N/A $Monfile = 'tmon.out';
1N/A if( exists $ENV{DPROFPP_OPTS} ){
1N/A my @tmpargv = @ARGV;
1N/A @ARGV = split( ' ', $ENV{DPROFPP_OPTS} );
1N/A getopts( $options );
1N/A if( @ARGV ){
1N/A # there was a filename.
1N/A $Monfile = shift;
1N/A }
1N/A @ARGV = @tmpargv;
1N/A }
1N/A
1N/A getopts( $options );
1N/A if( @ARGV ){
1N/A # there was a filename, it overrides any earlier name.
1N/A $Monfile = shift;
1N/A }
1N/A
1N/A# -O cnt Specifies maximum number of subroutines to display.
1N/A# -a Sort by alphabetic name of subroutines.
1N/A# -z Sort by user+system time spent in subroutines. (default)
1N/A# -l Sort by number of calls to subroutines.
1N/A# -v Sort by average amount of time spent in subroutines.
1N/A# -T Show call tree.
1N/A# -t Show call tree, compressed.
1N/A# -q Do not print column headers.
1N/A# -u Use user time rather than user+system time.
1N/A# -s Use system time rather than user+system time.
1N/A# -r Use real elapsed time rather than user+system time.
1N/A# -U Do not sort subroutines.
1N/A# -E Sub times are reported exclusive of child times. (default)
1N/A# -I Sub times are reported inclusive of child times.
1N/A# -V Print dprofpp's version.
1N/A# -p script Specifies name of script to be profiled.
1N/A# -Q Used with -p to indicate the dprofpp should quit after
1N/A# profiling the script, without interpreting the data.
1N/A# -A count autoloaded to *AUTOLOAD
1N/A# -R count anonyms separately even if from the same package
1N/A# -g subr count only those who are SUBR or called from SUBR
1N/A# -S Create statistics for all the depths
1N/A
1N/A# -G Group all calls matching the pattern together.
1N/A# -P Used with -G to pull all other calls together.
1N/A# -f Filter all calls mathcing the pattern.
1N/A# -d Reverse sort
1N/A
1N/A if( defined $opt_V ){
1N/A my $fh = 'main::fh';
1N/A print "$0 version: $VERSION\n";
1N/A open( $fh, "<$Monfile" ) && do {
1N/A local $XS_VERSION = 'early';
1N/A header($fh);
1N/A close( $fh );
1N/A print "XS_VERSION: $XS_VERSION\n";
1N/A };
1N/A exit(0);
1N/A }
1N/A $cnt = $opt_O || 15;
1N/A $sort = 'by_time';
1N/A $sort = 'by_ctime' if defined $opt_I;
1N/A $sort = 'by_calls' if defined $opt_l;
1N/A $sort = 'by_alpha' if defined $opt_a;
1N/A $sort = 'by_avgcpu' if defined $opt_v;
1N/A
1N/A if(defined $opt_d){
1N/A $sort = "r".$sort;
1N/A }
1N/A $incl_excl = 'Exclusive';
1N/A $incl_excl = 'Inclusive' if defined $opt_I;
1N/A $whichtime = 'User+System';
1N/A $whichtime = 'System' if defined $opt_s;
1N/A $whichtime = 'Real' if defined $opt_r;
1N/A $whichtime = 'User' if defined $opt_u;
1N/A
1N/A if( defined $opt_p ){
1N/A my $prof = 'DProf';
1N/A my $startperl = $Config{'startperl'};
1N/A
1N/A $startperl =~ s/^#!//; # remove shebang
1N/A run_profiler( $opt_p, $prof, $startperl );
1N/A $Monfile = 'tmon.out'; # because that's where it is
1N/A exit(0) if defined $opt_Q;
1N/A }
1N/A elsif( defined $opt_Q ){
1N/A die "-Q is meaningful only when used with -p\n";
1N/A }
1N/A}
1N/A
1N/AMain: {
1N/A my $monout = $Monfile;
1N/A my $fh = 'main::fh';
1N/A local $names = {};
1N/A local $times = {}; # times in hz
1N/A local $ctimes = {}; # Cumulative times in hz
1N/A local $calls = {};
1N/A local $persecs = {}; # times in seconds
1N/A local $idkeys = [];
1N/A local $runtime; # runtime in seconds
1N/A my @a = ();
1N/A my $a;
1N/A local $rrun_utime = 0; # user time in hz
1N/A local $rrun_stime = 0; # system time in hz
1N/A local $rrun_rtime = 0; # elapsed run time in hz
1N/A local $rrun_ustime = 0; # user+system time in hz
1N/A local $hz = 0;
1N/A local $deep_times = {count => 0 , kids => {}, incl_time => 0};
1N/A local $time_precision = 2;
1N/A local $overhead = 0;
1N/A
1N/A open( $fh, "<$monout" ) || die "Unable to open $monout\n";
1N/A
1N/A header($fh);
1N/A
1N/A $rrun_ustime = $rrun_utime + $rrun_stime;
1N/A
1N/A $~ = 'STAT';
1N/A if( ! $opt_q ){
1N/A $^ = 'CSTAT_top';
1N/A }
1N/A
1N/A parsestack( $fh, $names, $calls, $times, $ctimes, $idkeys );
1N/A
1N/A #filter calls
1N/A if( $opt_f ){
1N/A for(my $i = 0;$i < @$idkeys - 2;){
1N/A $key = $$idkeys[$i];
1N/A if($key =~ /$opt_f/){
1N/A splice(@$idkeys, $i, 1);
1N/A $runtime -= $$times{$key};
1N/A next;
1N/A }
1N/A $i++;
1N/A }
1N/A }
1N/A
1N/A if( $opt_G ){
1N/A group($names, $calls, $times, $ctimes, $idkeys );
1N/A }
1N/A
1N/A settime( \$runtime, $hz ) unless $opt_g;
1N/A
1N/A exit(0) if $opt_T || $opt_t;
1N/A
1N/A if( $opt_v ){
1N/A percalc( $calls, ($opt_I ? $ctimes : $times), $persecs, $idkeys );
1N/A }
1N/A if( ! $opt_U ){
1N/A @a = sort $sort @$idkeys;
1N/A $a = \@a;
1N/A }
1N/A else {
1N/A $a = $idkeys;
1N/A }
1N/A display( $runtime, $hz, $names, $calls, $times, $ctimes, $cnt, $a,
1N/A $deep_times);
1N/A}
1N/A
1N/Asub group{
1N/A my ($names, $calls, $times, $ctimes, $idkeys ) = @_;
1N/A print "Option G Grouping: [$opt_G]\n";
1N/A # create entries to store grouping
1N/A $$names{$opt_G} = $opt_G;
1N/A $$calls{$opt_G} = 0;
1N/A $$times{$opt_G} = 0;
1N/A $$ctimes{$opt_G} = 0;
1N/A $$idkeys[@$idkeys] = $opt_G;
1N/A # Sum calls for the grouping
1N/A
1N/A my $other = "other";
1N/A if($opt_P){
1N/A $$names{$other} = $other;
1N/A $$calls{$other} = 0;
1N/A $$times{$other} = 0;
1N/A $$ctimes{$other} = 0;
1N/A $$idkeys[@$idkeys] = $other;
1N/A }
1N/A
1N/A for(my $i = 0;$i < @$idkeys - 2;){
1N/A $key = $$idkeys[$i];
1N/A if($key =~ /$opt_G/){
1N/A $$calls{$opt_G} += $$calls{$key};
1N/A $$times{$opt_G} += $$times{$key};
1N/A $$ctimes{$opt_G} += $$ctimes{$key};
1N/A splice(@$idkeys, $i, 1);
1N/A next;
1N/A }else{
1N/A if($opt_P){
1N/A $$calls{$other} += $$calls{$key};
1N/A $$times{$other} += $$times{$key};
1N/A $$ctimes{$other} += $$ctimes{$key};
1N/A splice(@$idkeys, $i, 1);
1N/A next;
1N/A }
1N/A }
1N/A $i++;
1N/A }
1N/A print "Grouping [$opt_G] Calls: [$$calls{$opt_G}]\n".
1N/A "Grouping [$opt_G] Times: [$$times{$opt_G}]\n".
1N/A "Grouping [$opt_G] IncTimes: [$$ctimes{$opt_G}]\n";
1N/A}
1N/A
1N/A# Sets $runtime to user, system, real, or user+system time. The
1N/A# result is given in seconds.
1N/A#
1N/Asub settime {
1N/A my( $runtime, $hz ) = @_;
1N/A
1N/A $hz ||= 1;
1N/A
1N/A if( $opt_r ){
1N/A $$runtime = ($rrun_rtime - $overhead)/$hz;
1N/A }
1N/A elsif( $opt_s ){
1N/A $$runtime = ($rrun_stime - $overhead)/$hz;
1N/A }
1N/A elsif( $opt_u ){
1N/A $$runtime = ($rrun_utime - $overhead)/$hz;
1N/A }
1N/A else{
1N/A $$runtime = ($rrun_ustime - $overhead)/$hz;
1N/A }
1N/A $$runtime = 0 unless $$runtime > 0;
1N/A}
1N/A
1N/Asub exclusives_in_tree {
1N/A my( $deep_times ) = @_;
1N/A my $kids_time = 0;
1N/A my $kid;
1N/A # When summing, take into account non-rounded-up kids time.
1N/A for $kid (keys %{$deep_times->{kids}}) {
1N/A $kids_time += $deep_times->{kids}{$kid}{incl_time};
1N/A }
1N/A $kids_time = 0 unless $kids_time >= 0;
1N/A $deep_times->{excl_time} = $deep_times->{incl_time} - $kids_time;
1N/A $deep_times->{excl_time} = 0 unless $deep_times->{excl_time} >= 0;
1N/A for $kid (keys %{$deep_times->{kids}}) {
1N/A exclusives_in_tree($deep_times->{kids}{$kid});
1N/A }
1N/A $deep_times->{incl_time} = 0 unless $deep_times->{incl_time} >= 0;
1N/A $deep_times->{kids_time} = $kids_time;
1N/A}
1N/A
1N/Asub kids_by_incl { $kids{$b}{incl_time} <=> $kids{$a}{excl_time}
1N/A or $a cmp $b }
1N/A
1N/Asub display_tree {
1N/A my( $deep_times, $name, $level ) = @_;
1N/A exclusives_in_tree($deep_times);
1N/A
1N/A my $kid;
1N/A
1N/A my $time;
1N/A if (%{$deep_times->{kids}}) {
1N/A $time = sprintf '%.*fs = (%.*f + %.*f)',
1N/A $time_precision, $deep_times->{incl_time}/$hz,
1N/A $time_precision, $deep_times->{excl_time}/$hz,
1N/A $time_precision, $deep_times->{kids_time}/$hz;
1N/A } else {
1N/A $time = sprintf '%.*f', $time_precision, $deep_times->{incl_time}/$hz;
1N/A }
1N/A print ' ' x (2*$level), "$name x $deep_times->{count} \t${time}s\n"
1N/A if $deep_times->{count};
1N/A
1N/A for $kid (sort kids_by_incl %{$deep_times->{kids}}) {
1N/A display_tree( $deep_times->{kids}{$kid}, $kid, $level + 1 );
1N/A }
1N/A}
1N/A
1N/A# Report the times in seconds.
1N/Asub display {
1N/A my( $runtime, $hz, $names, $calls, $times, $ctimes, $cnt,
1N/A $idkeys, $deep_times ) = @_;
1N/A my( $x, $key, $s, $cs );
1N/A #format: $ncalls, $name, $secs, $percall, $pcnt
1N/A
1N/A if ($opt_S) {
1N/A display_tree( $deep_times, 'toplevel', -1 )
1N/A } else {
1N/A for( $x = 0; $x < @$idkeys; ++$x ){
1N/A $key = $idkeys->[$x];
1N/A $ncalls = $calls->{$key};
1N/A $name = $names->{$key};
1N/A $s = $times->{$key}/$hz;
1N/A $secs = sprintf("%.3f", $s );
1N/A $cs = $ctimes->{$key}/$hz;
1N/A $csecs = sprintf("%.3f", $cs );
1N/A $percall = sprintf("%.4f", $s/$ncalls );
1N/A $cpercall = sprintf("%.4f", $cs/$ncalls );
1N/A $pcnt = sprintf("%.2f",
1N/A $runtime? ((($opt_I ? $csecs : $secs) / $runtime) * 100.0): 0 );
1N/A write;
1N/A $pcnt = $secs = $ncalls = $percall = "";
1N/A write while( length $name );
1N/A last unless --$cnt;
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub move_keys {
1N/A my ($source, $dest) = @_;
1N/A
1N/A for my $kid_name (keys %$source) {
1N/A my $source_kid = delete $source->{$kid_name};
1N/A
1N/A if (my $dest_kid = $dest->{$kid_name}) {
1N/A $dest_kid->{count} += $source_kid->{count};
1N/A $dest_kid->{incl_time} += $source_kid->{incl_time};
1N/A move_keys($source_kid->{kids},$dest_kid->{kids});
1N/A } else {
1N/A $dest->{$kid_name} = $source_kid;
1N/A }
1N/A }
1N/A}
1N/A
1N/Asub add_to_tree {
1N/A my ($curdeep_times, $name, $t) = @_;
1N/A if ($name ne $curdeep_times->[-1]{name} and $opt_A) {
1N/A $name = $curdeep_times->[-1]{name};
1N/A }
1N/A die "Shorted?!" unless @$curdeep_times >= 2;
1N/A my $entry = $curdeep_times->[-2]{kids}{$name} ||= {
1N/A count => 0,
1N/A kids => {},
1N/A incl_time => 0,
1N/A };
1N/A # Now transfer to the new node (could not do earlier, since name can change)
1N/A $entry->{count}++;
1N/A $entry->{incl_time} += $t - $curdeep_times->[-1]{enter_stamp};
1N/A # Merge the kids?
1N/A move_keys($curdeep_times->[-1]->{kids},$entry->{kids});
1N/A pop @$curdeep_times;
1N/A}
1N/A
1N/A
1N/Asub parsestack {
1N/A my( $fh, $names, $calls, $times, $ctimes, $idkeys ) = @_;
1N/A my( $dir, $name );
1N/A my( $t, $syst, $realt, $usert );
1N/A my( $x, $z, $c, $id, $pack );
1N/A my @stack = ();
1N/A my @tstack = ();
1N/A my %outer;
1N/A my $tab = 3;
1N/A my $in = 0;
1N/A
1N/A # remember last call depth and function name
1N/A my $l_in = $in;
1N/A my $l_name = '';
1N/A my $repcnt = 0;
1N/A my $repstr = '';
1N/A my $dprof_stamp;
1N/A my %cv_hash;
1N/A my $in_level = not defined $opt_g; # Level deep in report grouping
1N/A my $curdeep_times = [$deep_times];
1N/A
1N/A my $over_per_call;
1N/A if ( $opt_u ) { $over_per_call = $over_utime }
1N/A elsif( $opt_s ) { $over_per_call = $over_stime }
1N/A elsif( $opt_r ) { $over_per_call = $over_rtime }
1N/A else { $over_per_call = $over_utime + $over_stime }
1N/A $over_per_call /= 2*$over_tests; # distribute over entry and exit
1N/A
1N/A while(<$fh>){
1N/A next if /^#/;
1N/A last if /^PART/;
1N/A
1N/A chop;
1N/A if (/^&/) {
1N/A ($dir, $id, $pack, $name) = split;
1N/A if ($opt_R and ($name =~ /(?:::)?(__ANON__|END)$/)) {
1N/A $name .= "($id)";
1N/A }
1N/A $cv_hash{$id} = "$pack\::$name";
1N/A next;
1N/A }
1N/A ($dir, $usert, $syst, $realt, $name) = split;
1N/A
1N/A my $ot = $t;
1N/A if ( $dir eq '/' ) {
1N/A $syst = $stack[-1][0];
1N/A $usert = '&';
1N/A $dir = '-';
1N/A #warn("Inserted exit for $stack[-1][0].\n")
1N/A }
1N/A if (defined $realt) { # '+ times nam' '- times nam' or '@ incr'
1N/A if ( $opt_u ) { $t = $usert }
1N/A elsif( $opt_s ) { $t = $syst }
1N/A elsif( $opt_r ) { $t = $realt }
1N/A else { $t = $usert + $syst }
1N/A $t += $ot, next if $dir eq '@'; # Increments there
1N/A } else {
1N/A # "- id" or "- & name"
1N/A $name = defined $syst ? $syst : $cv_hash{$usert};
1N/A }
1N/A
1N/A next unless $in_level or $name eq $opt_g;
1N/A if ( $dir eq '-' or $dir eq '*' ) {
1N/A my $ename = $dir eq '*' ? $stack[-1][0] : $name;
1N/A $overhead += $over_per_call;
1N/A if ($name eq "Devel::DProf::write") {
1N/A $overhead += $t - $dprof_stamp;
1N/A next;
1N/A } elsif (defined $opt_g and $ename eq $opt_g) {
1N/A $in_level--;
1N/A }
1N/A add_to_tree($curdeep_times, $ename,
1N/A $t - $overhead) if $opt_S;
1N/A exitstamp( \@stack, \@tstack,
1N/A $t - $overhead,
1N/A $times, $ctimes, $ename, \$in, $tab,
1N/A $curdeep_times, \%outer );
1N/A }
1N/A next unless $in_level or $name eq $opt_g;
1N/A if( $dir eq '+' or $dir eq '*' ){
1N/A if ($name eq "Devel::DProf::write") {
1N/A $dprof_stamp = $t;
1N/A next;
1N/A } elsif (defined $opt_g and $name eq $opt_g) {
1N/A $in_level++;
1N/A }
1N/A $overhead += $over_per_call;
1N/A if( $opt_T ){
1N/A print ' ' x $in, "$name\n";
1N/A $in += $tab;
1N/A }
1N/A elsif( $opt_t ){
1N/A # suppress output on same function if the
1N/A # same calling level is called.
1N/A if ($l_in == $in and $l_name eq $name) {
1N/A $repcnt++;
1N/A } else {
1N/A $repstr = ' ('.++$repcnt.'x)'
1N/A if $repcnt;
1N/A print ' ' x $l_in, "$l_name$repstr\n"
1N/A if $l_name ne '';
1N/A $repstr = '';
1N/A $repcnt = 0;
1N/A $l_in = $in;
1N/A $l_name = $name;
1N/A }
1N/A $in += $tab;
1N/A }
1N/A if( ! defined $names->{$name} ){
1N/A $names->{$name} = $name;
1N/A $times->{$name} = 0;
1N/A $ctimes->{$name} = 0;
1N/A push( @$idkeys, $name );
1N/A }
1N/A $calls->{$name}++;
1N/A $outer{$name}++;
1N/A push @$curdeep_times, { kids => {},
1N/A name => $name,
1N/A enter_stamp => $t - $overhead,
1N/A } if $opt_S;
1N/A $x = [ $name, $t - $overhead ];
1N/A push( @stack, $x );
1N/A
1N/A # my children will put their time here
1N/A push( @tstack, 0 );
1N/A } elsif ($dir ne '-'){
1N/A die "Bad profile: $_";
1N/A }
1N/A }
1N/A if( $opt_t ){
1N/A $repstr = ' ('.++$repcnt.'x)' if $repcnt;
1N/A print ' ' x $l_in, "$l_name$repstr\n";
1N/A }
1N/A
1N/A while (my ($key, $count) = each %outer) {
1N/A next unless $count;
1N/A warn "$key has $count unstacked calls in outer\n";
1N/A }
1N/A
1N/A if( @stack ){
1N/A if( ! $opt_F ){
1N/A warn "Garbled profile is missing some exit time stamps:\n";
1N/A foreach $x (@stack) {
1N/A print $x->[0],"\n";
1N/A }
1N/A die "Try rerunning dprofpp with -F.\n";
1N/A # I don't want -F to be default behavior--yet
1N/A # 9/18/95 dmr
1N/A }
1N/A else{
1N/A warn( "Faking " . scalar( @stack ) . " exit timestamp(s).\n");
1N/A foreach $x ( reverse @stack ){
1N/A $name = $x->[0];
1N/A exitstamp( \@stack, \@tstack,
1N/A $t - $overhead, $times,
1N/A $ctimes, $name, \$in, $tab,
1N/A $curdeep_times, \%outer );
1N/A add_to_tree($curdeep_times, $name,
1N/A $t - $overhead)
1N/A if $opt_S;
1N/A }
1N/A }
1N/A }
1N/A if (defined $opt_g) {
1N/A $runtime = $ctimes->{$opt_g}/$hz;
1N/A $runtime = 0 unless $runtime > 0;
1N/A }
1N/A}
1N/A
1N/Asub exitstamp {
1N/A my($stack, $tstack, $t, $times, $ctimes, $name, $in, $tab, $deep, $outer) = @_;
1N/A my( $x, $c, $z );
1N/A
1N/A $x = pop( @$stack );
1N/A if( ! defined $x ){
1N/A die "Garbled profile, missing an enter time stamp";
1N/A }
1N/A if( $x->[0] ne $name and $opt_G and ($name =~ /$opt_G/)){
1N/A if ($x->[0] =~ /(?:::)?AUTOLOAD$/) {
1N/A if ($opt_A) {
1N/A $name = $x->[0];
1N/A }
1N/A } elsif ( $opt_F ) {
1N/A warn( "Garbled profile, faking exit timestamp:\n\t$name => $x->[0].\n");
1N/A $name = $x->[0];
1N/A } else {
1N/A foreach $z (@stack, $x) {
1N/A print $z->[0],"\n";
1N/A }
1N/A die "Garbled profile, unexpected exit time stamp";
1N/A }
1N/A }
1N/A if( $opt_T || $opt_t ){
1N/A $$in -= $tab;
1N/A }
1N/A # collect childtime
1N/A $c = pop( @$tstack );
1N/A # total time this func has been active
1N/A $z = $t - $x->[1];
1N/A $ctimes->{$name} += $z
1N/A unless --$outer->{$name};
1N/A $times->{$name} += $z - $c;
1N/A # pass my time to my parent
1N/A if( @$tstack ){
1N/A $c = pop( @$tstack );
1N/A push( @$tstack, $c + $z );
1N/A }
1N/A}
1N/A
1N/A
1N/Asub header {
1N/A my $fh = shift;
1N/A chop($_ = <$fh>);
1N/A if( ! /^#fOrTyTwO$/ ){
1N/A die "Not a perl profile";
1N/A }
1N/A while(<$fh>){
1N/A next if /^#/;
1N/A last if /^PART/;
1N/A eval;
1N/A }
1N/A $over_tests = 1 unless $over_tests;
1N/A $time_precision = length int ($hz - 1); # log ;-)
1N/A}
1N/A
1N/A
1N/A# Report avg time-per-function in seconds
1N/Asub percalc {
1N/A my( $calls, $times, $persecs, $idkeys ) = @_;
1N/A my( $x, $t, $n, $key );
1N/A
1N/A for( $x = 0; $x < @$idkeys; ++$x ){
1N/A $key = $idkeys->[$x];
1N/A $n = $calls->{$key};
1N/A $t = $times->{$key} / $hz;
1N/A $persecs->{$key} = $t ? $t / $n : 0;
1N/A }
1N/A}
1N/A
1N/A
1N/A# Runs the given script with the given profiler and the given perl.
1N/Asub run_profiler {
1N/A my $script = shift;
1N/A my $profiler = shift;
1N/A my $startperl = shift;
1N/A my @script_parts = split /\s+/, $script;
1N/A
1N/A system $startperl, "-d:$profiler", @script_parts;
1N/A if( $? / 256 > 0 ){
1N/A my $cmd = join ' ', @script_parts;
1N/A die "Failed: $startperl -d:$profiler $cmd: $!";
1N/A }
1N/A}
1N/A
1N/A
1N/Asub by_time { $times->{$b} <=> $times->{$a} }
1N/Asub by_ctime { $ctimes->{$b} <=> $ctimes->{$a} }
1N/Asub by_calls { $calls->{$b} <=> $calls->{$a} }
1N/Asub by_alpha { $names->{$a} cmp $names->{$b} }
1N/Asub by_avgcpu { $persecs->{$b} <=> $persecs->{$a} }
1N/A# Reversed
1N/Asub rby_time { $times->{$a} <=> $times->{$b} }
1N/Asub rby_ctime { $ctimes->{$a} <=> $ctimes->{$b} }
1N/Asub rby_calls { $calls->{$a} <=> $calls->{$b} }
1N/Asub rby_alpha { $names->{$b} cmp $names->{$a} }
1N/Asub rby_avgcpu { $persecs->{$a} <=> $persecs->{$b} }
1N/A
1N/A
1N/Aformat CSTAT_top =
1N/ATotal Elapsed Time = @>>>>>>> Seconds
1N/A(($rrun_rtime - $overhead) / $hz)
1N/A @>>>>>>>>>> Time = @>>>>>>> Seconds
1N/A$whichtime, $runtime
1N/A@<<<<<<<< Times
1N/A$incl_excl
1N/A%Time ExclSec CumulS #Calls sec/call Csec/c Name
1N/A.
1N/A
1N/ABEGIN {
1N/A my $fmt = ' ^>>> ^>>>> ^>>>>> ^>>>>> ^>>>>> ^>>>>> ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<';
1N/A if (-t STDOUT and defined $stty and my ($cols) = `$stty -a` =~ /\bcolumns\s+(\d+)/)
1N/A {
1N/A $fmt .= '<' x ($cols - length $fmt) if $cols > 80;
1N/A }
1N/A
1N/A eval "format STAT = \n$fmt" . '
1N/A$pcnt, $secs, $csecs, $ncalls, $percall, $cpercall, $name
1N/A.';
1N/A}
1N/A!NO!SUBS!
1N/A
1N/Aclose OUT or die "Can't close $file: $!";
1N/Achmod 0755, $file or die "Can't reset permissions for $file: $!\n";
1N/Aexec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
1N/A