home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / bin / dprofpp.bat < prev    next >
Encoding:
DOS Batch File  |  1997-08-10  |  13.4 KB  |  562 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. perl -S %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
  4. goto endofperl
  5. @rem ';
  6. #line 7
  7. #perl
  8.     eval 'exec perl -S $0 "$@"'
  9.     if 0;
  10.  
  11. require 5.003;
  12.  
  13. my $VERSION = 19970614;
  14.  
  15. =head1 NAME
  16.  
  17. dprofpp - display perl profile data
  18.  
  19. =head1 SYNOPSIS
  20.  
  21. dprofpp [B<-a>|B<-z>|B<-l>|B<-v>|B<-U>] [B<-s>|B<-r>|B<-u>] [B<-q>] [B<-F>] [B<-I|-E>] [B<-O cnt>] [profile]
  22.  
  23. dprofpp B<-T> [B<-F>] [profile]
  24.  
  25. dprofpp B<-t> [B<-F>] [profile]
  26.  
  27. dprofpp B<-p script> [B<-Q>] [other opts]
  28.  
  29. dprofpp B<-V> [profile]
  30.  
  31. =head1 DESCRIPTION
  32.  
  33. The I<dprofpp> command interprets profile data produced by a profiler, such
  34. as the Devel::DProf profiler.  Dprofpp will read the file F<tmon.out> and
  35. will display the 15 subroutines which are using the most time.  By default
  36. the times for each subroutine are given exclusive of the times of their
  37. child subroutines.
  38.  
  39. To profile a Perl script run the perl interpreter with the B<-d> switch.  So
  40. to profile script F<test.pl> with Devel::DProf the following command should
  41. be used.
  42.  
  43.     $ perl5 -d:DProf test.pl
  44.  
  45. Then run dprofpp to analyze the profile.
  46.  
  47.     $ dprofpp -u
  48.     Total Elapsed Time =    1.67 Seconds
  49.          User Time =    0.61 Seconds
  50.     Exclusive Times
  51.     %Time Seconds     #Calls sec/call Name
  52.      52.4   0.320          2   0.1600 main::foo
  53.      45.9   0.280        200   0.0014 main::bar
  54.      0.00   0.000          1   0.0000 DynaLoader::import
  55.      0.00   0.000          1   0.0000 main::baz
  56.  
  57. The dprofpp tool can also run the profiler before analyzing the profile
  58. data.  The above two commands can be executed with one dprofpp command.
  59.  
  60.     $ dprofpp -u -p test.pl
  61.  
  62. Consult L<Devel::DProf/"PROFILE FORMAT"> for a description of the raw profile.
  63.  
  64. =head1 OPTIONS
  65.  
  66. =over 5
  67.  
  68. =item B<-a>
  69.  
  70. Sort alphabetically by subroutine names.
  71.  
  72. =item B<-E>
  73.  
  74. (default)  Display all subroutine times exclusive of child subroutine times.
  75.  
  76. =item B<-F>
  77.  
  78. Force the generation of fake exit timestamps if dprofpp reports that the
  79. profile is garbled.  This is only useful if dprofpp determines that the
  80. profile is garbled due to missing exit timestamps.  You're on your own if
  81. you do this.  Consult the BUGS section.
  82.  
  83. =item B<-I>
  84.  
  85. Display all subroutine times inclusive of child subroutine times.
  86.  
  87. =item B<-l>
  88.  
  89. Sort by number of calls to the subroutines.  This may help identify
  90. candidates for inlining.
  91.  
  92. =item B<-O cnt>
  93.  
  94. Show only I<cnt> subroutines.  The default is 15.
  95.  
  96. =item B<-p script>
  97.  
  98. Tells dprofpp that it should profile the given script and then interpret its
  99. profile data.  See B<-Q>.
  100.  
  101. =item B<-Q>
  102.  
  103. Used with B<-p> to tell dprofpp to quit after profiling the script, without
  104. interpreting the data.
  105.  
  106. =item B<-q>
  107.  
  108. Do not display column headers.
  109.  
  110. =item B<-r>
  111.  
  112. Display elapsed real times rather than user+system times.
  113.  
  114. =item B<-s>
  115.  
  116. Display system times rather than user+system times.
  117.  
  118. =item B<-T>
  119.  
  120. Display subroutine call tree to stdout.  Subroutine statistics are
  121. not displayed.
  122.  
  123. =item B<-t>
  124.  
  125. Display subroutine call tree to stdout.  Subroutine statistics are not
  126. displayed.  When a function is called multiple consecutive times at the same
  127. calling level then it is displayed once with a repeat count.
  128.  
  129. =item B<-U>
  130.  
  131. Do not sort.  Display in the order found in the raw profile.
  132.  
  133. =item B<-u>
  134.  
  135. Display user times rather than user+system times.
  136.  
  137. =item B<-V>
  138.  
  139. Print dprofpp's version number and exit.  If a raw profile is found then its
  140. XS_VERSION variable will be displayed, too.
  141.  
  142. =item B<-v>
  143.  
  144. Sort by average time spent in subroutines during each call.  This may help
  145. identify candidates for inlining. 
  146.  
  147. =item B<-z>
  148.  
  149. (default) Sort by amount of user+system time used.  The first few lines
  150. should show you which subroutines are using the most time.
  151.  
  152. =back
  153.  
  154. =head1 ENVIRONMENT
  155.  
  156. The environment variable B<DPROFPP_OPTS> can be set to a string containing
  157. options for dprofpp.  You might use this if you prefer B<-I> over B<-E> or
  158. if you want B<-F> on all the time.
  159.  
  160. This was added fairly lazily, so there are some undesirable side effects.
  161. Options on the commandline should override options in DPROFPP_OPTS--but
  162. don't count on that in this version.
  163.  
  164. =head1 BUGS
  165.  
  166. Applications which call C<die> from within an eval for exception handling
  167. (catch/throw) or for setjmp/longjmp may not generate a readable profile.
  168. See the B<-F> option.
  169.  
  170. Applications which call C<exit> from within a subroutine will leave an
  171. incomplete profile.  See the B<-F> option.
  172.  
  173. Any bugs in Devel::DProf, or any profiler generating the profile data, could
  174. be visible here.  See L<Devel::DProf/BUGS>.
  175.  
  176. Mail bug reports and feature requests to the perl5-porters mailing list at
  177. F<E<lt>perl5-porters@africa.nicoh.comE<gt>>.  Bug reports should include the
  178. output of the B<-V> option.
  179.  
  180. =head1 FILES
  181.  
  182.     dprofpp        - profile processor
  183.     tmon.out    - raw profile
  184.  
  185. =head1 SEE ALSO
  186.  
  187. L<perl>, L<Devel::DProf>, times(2)
  188.  
  189. =cut
  190.  
  191. use Getopt::Std 'getopts';
  192. use Config '%Config';
  193.  
  194. Setup: {
  195.     my $options = 'O:lzavuTtqrsUFEIp:QV';
  196.  
  197.     $Monfile = 'tmon.out';
  198.     if( exists $ENV{DPROFPP_OPTS} ){
  199.         my @tmpargv = @ARGV;
  200.         @ARGV = split( ' ', $ENV{DPROFPP_OPTS} );
  201.         getopts( $options );
  202.         if( @ARGV ){
  203.             # there was a filename.
  204.             $Monfile = shift;
  205.         }
  206.         @ARGV = @tmpargv;
  207.     }
  208.  
  209.     getopts( $options );
  210.     if( @ARGV ){
  211.         # there was a filename, it overrides any earlier name.
  212.         $Monfile = shift;
  213.     }
  214.  
  215. # -O cnt    Specifies maximum number of subroutines to display.
  216. # -a        Sort by alphabetic name of subroutines.
  217. # -z        Sort by user+system time spent in subroutines. (default)
  218. # -l        Sort by number of calls to subroutines.
  219. # -v        Sort by average amount of time spent in subroutines.
  220. # -T        Show call tree.
  221. # -t        Show call tree, compressed.
  222. # -q        Do not print column headers.
  223. # -u        Use user time rather than user+system time.
  224. # -s        Use system time rather than user+system time.
  225. # -r        Use real elapsed time rather than user+system time.
  226. # -U        Do not sort subroutines.
  227. # -E        Sub times are reported exclusive of child times. (default)
  228. # -I        Sub times are reported inclusive of child times.
  229. # -V        Print dprofpp's version.
  230. # -p script    Specifies name of script to be profiled.
  231. # -Q        Used with -p to indicate the dprofpp should quit after
  232. #        profiling the script, without interpreting the data.
  233.  
  234.     if( defined $opt_V ){
  235.         my $fh = 'main::fh';
  236.         print "$0 version: $VERSION\n";
  237.         open( $fh, "<$Monfile" ) && do {
  238.             local $XS_VERSION = 'early';
  239.             header($fh);
  240.             close( $fh );
  241.             print "XS_VERSION: $XS_VERSION\n";
  242.         };
  243.         exit(0);
  244.     }
  245.     $cnt = $opt_O || 15;
  246.     $sort = 'by_time';
  247.     $sort = 'by_calls' if defined $opt_l;
  248.     $sort = 'by_alpha' if defined $opt_a;
  249.     $sort = 'by_avgcpu' if defined $opt_v;
  250.     $incl_excl = 'Exclusive';
  251.     $incl_excl = 'Inclusive' if defined $opt_I;
  252.     $whichtime = 'User+System';
  253.     $whichtime = 'System' if defined $opt_s;
  254.     $whichtime = 'Real' if defined $opt_r;
  255.     $whichtime = 'User' if defined $opt_u;
  256.  
  257.     if( defined $opt_p ){
  258.         my $prof = 'DProf';
  259.         my $startperl = $Config{'startperl'};
  260.  
  261.         $startperl =~ s/^#!//; # remove shebang
  262.         run_profiler( $opt_p, $prof, $startperl );
  263.         $Monfile = 'tmon.out';  # because that's where it is
  264.         exit(0) if defined $opt_Q;
  265.     }
  266.     elsif( defined $opt_Q ){
  267.         die "-Q is meaningful only when used with -p\n";
  268.     }
  269. }
  270.  
  271. Main: {
  272.     my $monout = $Monfile;
  273.     my $fh = 'main::fh';
  274.     local $names = {};
  275.     local $times = {};  # times in hz
  276.     local $calls = {};
  277.     local $persecs = {}; # times in seconds
  278.     local $idkeys = [];
  279.     local $runtime; # runtime in seconds
  280.     my @a = ();
  281.     my $a;
  282.     local $rrun_utime = 0;    # user time in hz
  283.     local $rrun_stime = 0;    # system time in hz
  284.     local $rrun_rtime = 0;    # elapsed run time in hz
  285.     local $rrun_ustime = 0;    # user+system time in hz
  286.     local $hz = 0;
  287.  
  288.     open( $fh, "<$monout" ) || die "Unable to open $monout\n";
  289.  
  290.     header($fh);
  291.  
  292.     $rrun_ustime = $rrun_utime + $rrun_stime;
  293.  
  294.     settime( \$runtime, $hz );
  295.  
  296.     $~ = 'STAT';
  297.     if( ! $opt_q ){
  298.         $^ = 'CSTAT_top';
  299.     }
  300.  
  301.     parsestack( $fh, $names, $calls, $times, $idkeys );
  302.  
  303.     exit(0) if $opt_T || $opt_t;
  304.  
  305.     if( $opt_v ){
  306.         percalc( $calls, $times, $persecs, $idkeys );
  307.     }
  308.     if( ! $opt_U ){
  309.         @a = sort $sort @$idkeys;
  310.         $a = \@a;
  311.     }
  312.     else {
  313.         $a = $idkeys;
  314.     }
  315.     display( $runtime, $hz, $names, $calls, $times, $cnt, $a );
  316. }
  317.  
  318.  
  319. # Sets $runtime to user, system, real, or user+system time.  The
  320. # result is given in seconds.
  321. #
  322. sub settime {
  323.     my( $runtime, $hz ) = @_;
  324.  
  325.     if( $opt_r ){
  326.         $$runtime = $rrun_rtime/$hz;
  327.     }
  328.     elsif( $opt_s ){
  329.         $$runtime = $rrun_stime/$hz;
  330.     }
  331.     elsif( $opt_u ){
  332.         $$runtime = $rrun_utime/$hz;
  333.     }
  334.     else{
  335.         $$runtime = $rrun_ustime/$hz;
  336.     }
  337. }
  338.  
  339.  
  340. # Report the times in seconds.
  341. sub display {
  342.     my( $runtime, $hz, $names, $calls , $times, $cnt, $idkeys ) = @_;
  343.     my( $x, $key, $s );
  344.     #format: $ncalls, $name, $secs, $percall, $pcnt
  345.  
  346.     for( $x = 0; $x < @$idkeys; ++$x ){
  347.         $key = $idkeys->[$x];
  348.         $ncalls = $calls->{$key};
  349.         $name = $names->{$key};
  350.         $s = $times->{$key}/$hz;
  351.         $secs = sprintf("%.3f", $s );
  352.         $percall = sprintf("%.4f", $s/$ncalls );
  353.         $pcnt = sprintf("%.2f",
  354.                 $runtime? (($secs / $runtime) * 100.0): 0 );
  355.         write;
  356.         $pcnt = $secs = $ncalls = $percall = "";
  357.         write while( length $name );
  358.         last unless --$cnt;
  359.     }
  360. }
  361.  
  362.  
  363. sub parsestack {
  364.     my( $fh, $names, $calls, $times, $idkeys ) = @_;
  365.     my( $dir, $name );
  366.     my( $t, $syst, $realt, $usert );
  367.     my( $x, $z, $c );
  368.     my @stack = ();
  369.     my @tstack = ();
  370.     my $tab = 3;
  371.     my $in = 0;
  372.  
  373.     # remember last call depth and function name
  374.     my $l_in = $in;
  375.     my $l_name = '';
  376.     my $repcnt = 0;
  377.     my $repstr = '';
  378.  
  379.     while(<$fh>){
  380.         next if /^#/;
  381.         last if /^PART/;
  382.         chop;
  383.         ($dir, $usert, $syst, $realt, $name) = split;
  384.  
  385.         if   ( $opt_u )    {    $t = $usert        }
  386.         elsif( $opt_s )    {    $t = $syst        }
  387.         elsif( $opt_r )    {    $t = $realt        }
  388.         else        {    $t = $usert + $syst    }
  389.  
  390.         if( $dir eq '+' ){
  391.             if( $opt_T ){
  392.                 print ' ' x $in, "$name\n";
  393.                 $in += $tab;
  394.             }
  395.             elsif( $opt_t ){
  396.                 # suppress output on same function if the
  397.                 # same calling level is called.
  398.                 if ($l_in == $in and $l_name eq $name) {
  399.                     $repcnt++;
  400.                 } else {
  401.                     $repstr = ' ('.++$repcnt.'x)'
  402.                          if $repcnt;
  403.                     print ' ' x $l_in, "$l_name$repstr\n"
  404.                         if $l_name ne '';
  405.                     $repstr = '';
  406.                     $repcnt = 0;
  407.                     $l_in = $in;
  408.                     $l_name = $name;
  409.                 }
  410.                 $in += $tab;
  411.             }
  412.             if( ! defined $names->{$name} ){
  413.                 $names->{$name} = $name;
  414.                 $times->{$name} = 0;
  415.                 push( @$idkeys, $name );
  416.             }
  417.             $calls->{$name}++;
  418.             $x = [ $name, $t ];
  419.             push( @stack, $x );
  420.  
  421.             # my children will put their time here
  422.             push( @tstack, 0 );
  423.  
  424.             next;
  425.         }
  426.         if( $dir eq '-' ){
  427.             exitstamp( \@stack, \@tstack, $t, $times,
  428.                   $name, \$in, $tab );
  429.             next;
  430.         }
  431.         die "Bad profile: $_";
  432.     }
  433.     if( $opt_t ){
  434.         $repstr = ' ('.++$repcnt.'x)' if $repcnt;
  435.         print ' ' x $l_in, "$l_name$repstr\n";
  436.     }
  437.  
  438.     if( @stack ){
  439.         if( ! $opt_F ){
  440.             warn "Garbled profile is missing some exit time stamps:\n";
  441.             foreach $x (@stack) {
  442.                 print $x->[0],"\n";
  443.             }
  444.             die "Try rerunning dprofpp with -F.\n";
  445.             # I don't want -F to be default behavior--yet
  446.             #  9/18/95 dmr
  447.         }
  448.         else{
  449.             warn( "Faking " . scalar( @stack ) . " exit timestamp(s).\n");
  450.             foreach $x ( reverse @stack ){
  451.                 $name = $x->[0];
  452.                 exitstamp( \@stack, \@tstack, $t, $times,
  453.                       $name, \$in, $tab );
  454.             }
  455.         }
  456.     }
  457. }
  458.  
  459. sub exitstamp {
  460.     my( $stack, $tstack, $t, $times, $name, $in, $tab ) = @_;
  461.     my( $x, $c, $z );
  462.  
  463.     $x = pop( @$stack );
  464.     if( ! defined $x ){
  465.         die "Garbled profile, missing an enter time stamp";
  466.     }
  467.     if( $x->[0] ne $name ){
  468.         die "Garbled profile, unexpected exit time stamp";
  469.     }
  470.     if( $opt_T || $opt_t ){
  471.         $$in -= $tab;
  472.     }
  473.     # collect childtime
  474.     $c = pop( @$tstack );
  475.     # total time this func has been active
  476.     $z = $t - $x->[1];
  477.     # If collecting inclusive times...
  478.     if( defined $opt_I ){
  479.         # do not subtract my child time
  480.         $times->{$name} += $z;
  481.     }
  482.     # else collecting exclusive times...
  483.     else{
  484.         # less time spent in child funcs.
  485.         #   prepare to accept that the children may account
  486.         #   for all of my time.
  487.         $times->{$name} += ($z > $c)? $z - $c: $c - $z;
  488.     }
  489.  
  490.     # pass my time to my parent
  491.     if( @$tstack ){
  492.         $c = pop( @$tstack );
  493.         push( @$tstack, $c + $z );
  494.     }
  495. }
  496.  
  497.  
  498. sub header {
  499.     my $fh = shift;
  500.     chop($_ = <$fh>);
  501.     if( ! /^#fOrTyTwO$/ ){
  502.         die "Not a perl profile";
  503.     }
  504.     while(<$fh>){
  505.         next if /^#/;
  506.         last if /^PART/;
  507.         eval;
  508.     }
  509. }
  510.  
  511.  
  512. # Report avg time-per-function in seconds
  513. sub percalc {
  514.     my( $calls, $times, $persecs, $idkeys ) = @_;
  515.     my( $x, $t, $n, $key );
  516.  
  517.     for( $x = 0; $x < @$idkeys; ++$x ){
  518.         $key = $idkeys->[$x];
  519.         $n = $calls->{$key};
  520.         $t = $times->{$key} / $hz;
  521.         $persecs->{$key} = $t ? $t / $n : 0;
  522.     }
  523. }
  524.  
  525.  
  526. # Runs the given script with the given profiler and the given perl.
  527. sub run_profiler {
  528.     my $script = shift;
  529.     my $profiler = shift;
  530.     my $startperl = shift;
  531.  
  532.     system $startperl, "-d:$profiler", $script;
  533.     if( $? / 256 > 0 ){
  534.         die "Failed: $startperl -d:$profiler $script: $!";
  535.     }
  536. }
  537.  
  538.  
  539. sub by_time { $times->{$b} <=> $times->{$a} }
  540. sub by_calls { $calls->{$b} <=> $calls->{$a} }
  541. sub by_alpha { $names->{$a} cmp $names->{$b} }
  542. sub by_avgcpu { $persecs->{$b} <=> $persecs->{$a} }
  543.  
  544.  
  545. format CSTAT_top =
  546. Total Elapsed Time = @>>>>>> Seconds
  547. ($rrun_rtime / $hz)
  548.   @>>>>>>>>>> Time = @>>>>>> Seconds
  549. $whichtime, $runtime
  550. @<<<<<<<< Times
  551. $incl_excl
  552. %Time Seconds     #Calls sec/call Name
  553. .
  554.  
  555. format STAT =
  556.  ^>>>   ^>>>> ^>>>>>>>>>   ^>>>>> ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  557. $pcnt, $secs, $ncalls, $percall, $name
  558. .
  559.  
  560. __END__
  561. :endofperl
  562.