home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / bin / dprofpp.bat < prev    next >
Encoding:
DOS Batch File  |  2002-12-01  |  23.9 KB  |  908 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!perl
  14. #line 15
  15.     eval 'exec perl -S $0 "$@"'
  16.     if 0;
  17.  
  18. require 5.003;
  19.  
  20. my $VERSION = '20000000.00_01';
  21.  
  22. =head1 NAME
  23.  
  24. dprofpp - display perl profile data
  25.  
  26. =head1 SYNOPSIS
  27.  
  28. dprofpp [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]
  29.   
  30. dprofpp B<-T> [B<-F>] [B<-g subroutine>] [profile]
  31.  
  32. dprofpp B<-t> [B<-F>] [B<-g subroutine>] [profile]
  33.  
  34. dprofpp B<-G> <regexp> [B<-P>] [profile]
  35.  
  36. dprofpp B<-p script> [B<-Q>] [other opts]
  37.  
  38. dprofpp B<-V> [profile]
  39.  
  40. =head1 DESCRIPTION
  41.  
  42. The I<dprofpp> command interprets profile data produced by a profiler, such
  43. as the Devel::DProf profiler.  Dprofpp will read the file F<tmon.out> and
  44. will display the 15 subroutines which are using the most time.  By default
  45. the times for each subroutine are given exclusive of the times of their
  46. child subroutines.
  47.  
  48. To profile a Perl script run the perl interpreter with the B<-d> switch.  So
  49. to profile script F<test.pl> with Devel::DProf the following command should
  50. be used.
  51.  
  52.     $ perl5 -d:DProf test.pl
  53.  
  54. Then run dprofpp to analyze the profile.  The output of dprofpp depends
  55. on the flags to the program and the version of Perl you're using.
  56.  
  57.     $ dprofpp -u
  58.     Total Elapsed Time =    1.67 Seconds
  59.          User Time =    0.61 Seconds
  60.     Exclusive Times
  61.     %Time Seconds     #Calls sec/call Name
  62.      52.4   0.320          2   0.1600 main::foo
  63.      45.9   0.280        200   0.0014 main::bar
  64.      0.00   0.000          1   0.0000 DynaLoader::import
  65.      0.00   0.000          1   0.0000 main::baz
  66.  
  67. The dprofpp tool can also run the profiler before analyzing the profile
  68. data.  The above two commands can be executed with one dprofpp command.
  69.  
  70.     $ dprofpp -u -p test.pl
  71.  
  72. Consult L<Devel::DProf/"PROFILE FORMAT"> for a description of the raw profile.
  73.  
  74. =head1 OUTPUT
  75.  
  76. Columns are:
  77.  
  78. =over 4
  79.  
  80. =item %Time
  81.  
  82. Percentage of time spent in this routine.
  83.  
  84. =item #Calls
  85.  
  86. Number of calls to this routine.
  87.  
  88. =item sec/call
  89.  
  90. Average number of seconds per call to this routine.
  91.  
  92. =item Name
  93.  
  94. Name of routine.
  95.  
  96. =item CumulS
  97.  
  98. Time (in seconds) spent in this routine and routines called from it.
  99.  
  100. =item ExclSec
  101.  
  102. Time (in seconds) spent in this routine (not including those called
  103. from it).
  104.  
  105. =item Csec/c
  106.  
  107. Average time (in seconds) spent in each call of this routine
  108. (including those called from it).
  109.  
  110. =back
  111.  
  112. =head1 OPTIONS
  113.  
  114. =over 5
  115.  
  116. =item B<-a>
  117.  
  118. Sort alphabetically by subroutine names.
  119.  
  120. =item B<-d>
  121.  
  122. Reverse whatever sort is used
  123.  
  124. =item B<-A>
  125.  
  126. Count timing for autoloaded subroutine as timing for C<*::AUTOLOAD>.
  127. Otherwise the time to autoload it is counted as time of the subroutine
  128. itself (there is no way to separate autoload time from run time).
  129.  
  130. This is going to be irrelevant with newer Perls.  They will inform
  131. C<Devel::DProf> I<when> the C<AUTOLOAD> switches to actual subroutine,
  132. so a separate statistics for C<AUTOLOAD> will be collected no matter
  133. whether this option is set.
  134.  
  135. =item B<-R>
  136.  
  137. Count anonymous subroutines defined in the same package separately.
  138.  
  139. =item B<-E>
  140.  
  141. (default)  Display all subroutine times exclusive of child subroutine times.
  142.  
  143. =item B<-F>
  144.  
  145. Force the generation of fake exit timestamps if dprofpp reports that the
  146. profile is garbled.  This is only useful if dprofpp determines that the
  147. profile is garbled due to missing exit timestamps.  You're on your own if
  148. you do this.  Consult the BUGS section.
  149.  
  150. =item B<-I>
  151.  
  152. Display all subroutine times inclusive of child subroutine times.
  153.  
  154. =item B<-l>
  155.  
  156. Sort by number of calls to the subroutines.  This may help identify
  157. candidates for inlining.
  158.  
  159. =item B<-O cnt>
  160.  
  161. Show only I<cnt> subroutines.  The default is 15.
  162.  
  163. =item B<-p script>
  164.  
  165. Tells dprofpp that it should profile the given script and then interpret its
  166. profile data.  See B<-Q>.
  167.  
  168. =item B<-Q>
  169.  
  170. Used with B<-p> to tell dprofpp to quit after profiling the script, without
  171. interpreting the data.
  172.  
  173. =item B<-q>
  174.  
  175. Do not display column headers.
  176.  
  177. =item B<-r>
  178.  
  179. Display elapsed real times rather than user+system times.
  180.  
  181. =item B<-s>
  182.  
  183. Display system times rather than user+system times.
  184.  
  185. =item B<-T>
  186.  
  187. Display subroutine call tree to stdout.  Subroutine statistics are
  188. not displayed.
  189.  
  190. =item B<-t>
  191.  
  192. Display subroutine call tree to stdout.  Subroutine statistics are not
  193. displayed.  When a function is called multiple consecutive times at the same
  194. calling level then it is displayed once with a repeat count.
  195.  
  196. =item B<-S>
  197.  
  198. Display I<merged> subroutine call tree to stdout.  Statistics is
  199. displayed for each branch of the tree.  
  200.  
  201. When a function is called multiple (I<not necessarily consecutive>)
  202. times in the same branch then all these calls go into one branch of
  203. the next level.  A repeat count is output together with combined
  204. inclusive, exclusive and kids time.
  205.  
  206. Branches are sorted w.r.t. inclusive time.
  207.  
  208. =item B<-U>
  209.  
  210. Do not sort.  Display in the order found in the raw profile.
  211.  
  212. =item B<-u>
  213.  
  214. Display user times rather than user+system times.
  215.  
  216. =item B<-V>
  217.  
  218. Print dprofpp's version number and exit.  If a raw profile is found then its
  219. XS_VERSION variable will be displayed, too.
  220.  
  221. =item B<-v>
  222.  
  223. Sort by average time spent in subroutines during each call.  This may help
  224. identify candidates for inlining. 
  225.  
  226. =item B<-z>
  227.  
  228. (default) Sort by amount of user+system time used.  The first few lines
  229. should show you which subroutines are using the most time.
  230.  
  231. =item B<-g> C<subroutine>
  232.  
  233. Ignore subroutines except C<subroutine> and whatever is called from it.
  234.  
  235. =item B<-G> <regexp>
  236.  
  237. Aggregate "Group" all calls matching the pattern together.
  238. For example this can be used to group all calls of a set of packages
  239.  
  240.   -G "(package1::)|(package2::)|(package3::)"
  241.  
  242. or to group subroutines by name:
  243.  
  244.   -G "getNum"
  245.  
  246. =item B<-P>
  247.  
  248. Used with -G to aggregate "Pull"  together all calls that did not match -G.
  249.  
  250. =item B<-f> <regexp>
  251.  
  252. Filter all calls matching the pattern.
  253.  
  254. =back
  255.  
  256. =head1 ENVIRONMENT
  257.  
  258. The environment variable B<DPROFPP_OPTS> can be set to a string containing
  259. options for dprofpp.  You might use this if you prefer B<-I> over B<-E> or
  260. if you want B<-F> on all the time.
  261.  
  262. This was added fairly lazily, so there are some undesirable side effects.
  263. Options on the commandline should override options in DPROFPP_OPTS--but
  264. don't count on that in this version.
  265.  
  266. =head1 BUGS
  267.  
  268. Applications which call _exit() or exec() from within a subroutine
  269. will leave an incomplete profile.  See the B<-F> option.
  270.  
  271. Any bugs in Devel::DProf, or any profiler generating the profile data, could
  272. be visible here.  See L<Devel::DProf/BUGS>.
  273.  
  274. Mail bug reports and feature requests to the perl5-porters mailing list at
  275. F<E<lt>perl5-porters@perl.orgE<gt>>.  Bug reports should include the
  276. output of the B<-V> option.
  277.  
  278. =head1 FILES
  279.  
  280.     dprofpp        - profile processor
  281.     tmon.out    - raw profile
  282.  
  283. =head1 SEE ALSO
  284.  
  285. L<perl>, L<Devel::DProf>, times(2)
  286.  
  287. =cut
  288.  
  289. use Getopt::Std 'getopts';
  290. use Config '%Config';
  291.  
  292. Setup: {
  293.     my $options = 'O:g:G:Pf:dlzaAvuTtqrRsUFEIp:QVS';
  294.  
  295.     $Monfile = 'tmon.out';
  296.     if( exists $ENV{DPROFPP_OPTS} ){
  297.         my @tmpargv = @ARGV;
  298.         @ARGV = split( ' ', $ENV{DPROFPP_OPTS} );
  299.         getopts( $options );
  300.         if( @ARGV ){
  301.             # there was a filename.
  302.             $Monfile = shift;
  303.         }
  304.         @ARGV = @tmpargv;
  305.     }
  306.  
  307.     getopts( $options );
  308.     if( @ARGV ){
  309.         # there was a filename, it overrides any earlier name.
  310.         $Monfile = shift;
  311.     }
  312.  
  313. # -O cnt    Specifies maximum number of subroutines to display.
  314. # -a        Sort by alphabetic name of subroutines.
  315. # -z        Sort by user+system time spent in subroutines. (default)
  316. # -l        Sort by number of calls to subroutines.
  317. # -v        Sort by average amount of time spent in subroutines.
  318. # -T        Show call tree.
  319. # -t        Show call tree, compressed.
  320. # -q        Do not print column headers.
  321. # -u        Use user time rather than user+system time.
  322. # -s        Use system time rather than user+system time.
  323. # -r        Use real elapsed time rather than user+system time.
  324. # -U        Do not sort subroutines.
  325. # -E        Sub times are reported exclusive of child times. (default)
  326. # -I        Sub times are reported inclusive of child times.
  327. # -V        Print dprofpp's version.
  328. # -p script    Specifies name of script to be profiled.
  329. # -Q        Used with -p to indicate the dprofpp should quit after
  330. #        profiling the script, without interpreting the data.
  331. # -A        count autoloaded to *AUTOLOAD
  332. # -R        count anonyms separately even if from the same package
  333. # -g subr    count only those who are SUBR or called from SUBR
  334. # -S        Create statistics for all the depths
  335.  
  336. # -G        Group all calls matching the pattern together.
  337. # -P        Used with -G to pull all other calls together.
  338. # -f        Filter all calls mathcing the pattern.
  339. # -d        Reverse sort
  340.  
  341.     if( defined $opt_V ){
  342.         my $fh = 'main::fh';
  343.         print "$0 version: $VERSION\n";
  344.         open( $fh, "<$Monfile" ) && do {
  345.             local $XS_VERSION = 'early';
  346.             header($fh);
  347.             close( $fh );
  348.             print "XS_VERSION: $XS_VERSION\n";
  349.         };
  350.         exit(0);
  351.     }
  352.     $cnt = $opt_O || 15;
  353.     $sort = 'by_time';
  354.     $sort = 'by_ctime' if defined $opt_I;
  355.     $sort = 'by_calls' if defined $opt_l;
  356.     $sort = 'by_alpha' if defined $opt_a;
  357.     $sort = 'by_avgcpu' if defined $opt_v;
  358.     
  359.     if(defined $opt_d){
  360.         $sort = "r".$sort;
  361.     }
  362.     $incl_excl = 'Exclusive';
  363.     $incl_excl = 'Inclusive' if defined $opt_I;
  364.     $whichtime = 'User+System';
  365.     $whichtime = 'System' if defined $opt_s;
  366.     $whichtime = 'Real' if defined $opt_r;
  367.     $whichtime = 'User' if defined $opt_u;
  368.  
  369.     if( defined $opt_p ){
  370.         my $prof = 'DProf';
  371.         my $startperl = $Config{'startperl'};
  372.  
  373.         $startperl =~ s/^#!//; # remove shebang
  374.         run_profiler( $opt_p, $prof, $startperl );
  375.         $Monfile = 'tmon.out';  # because that's where it is
  376.         exit(0) if defined $opt_Q;
  377.     }
  378.     elsif( defined $opt_Q ){
  379.         die "-Q is meaningful only when used with -p\n";
  380.     }
  381. }
  382.  
  383. Main: {
  384.     my $monout = $Monfile;
  385.     my $fh = 'main::fh';
  386.     local $names = {};
  387.     local $times = {};   # times in hz
  388.     local $ctimes = {};  # Cumulative times in hz
  389.     local $calls = {};
  390.     local $persecs = {}; # times in seconds
  391.     local $idkeys = [];
  392.     local $runtime; # runtime in seconds
  393.     my @a = ();
  394.     my $a;
  395.     local $rrun_utime = 0;    # user time in hz
  396.     local $rrun_stime = 0;    # system time in hz
  397.     local $rrun_rtime = 0;    # elapsed run time in hz
  398.     local $rrun_ustime = 0;    # user+system time in hz
  399.     local $hz = 0;
  400.     local $deep_times = {count => 0 , kids => {}, incl_time => 0};
  401.     local $time_precision = 2;
  402.     local $overhead = 0;
  403.  
  404.     open( $fh, "<$monout" ) || die "Unable to open $monout\n";
  405.  
  406.     header($fh);
  407.  
  408.     $rrun_ustime = $rrun_utime + $rrun_stime;
  409.  
  410.     $~ = 'STAT';
  411.     if( ! $opt_q ){
  412.         $^ = 'CSTAT_top';
  413.     }
  414.  
  415.     parsestack( $fh, $names, $calls, $times, $ctimes, $idkeys );
  416.  
  417.     #filter calls
  418.     if( $opt_f ){
  419.         for(my $i = 0;$i < @$idkeys - 2;){
  420.             $key = $$idkeys[$i];
  421.             if($key =~ /$opt_f/){
  422.                 splice(@$idkeys, $i, 1);
  423.                 $runtime -= $$times{$key};
  424.                 next;
  425.             }
  426.             $i++;
  427.         }
  428.     }
  429.  
  430.     if( $opt_G ){
  431.         group($names, $calls, $times, $ctimes, $idkeys );
  432.     }
  433.  
  434.     settime( \$runtime, $hz ) unless $opt_g;
  435.  
  436.     exit(0) if $opt_T || $opt_t;
  437.  
  438.     if( $opt_v ){
  439.         percalc( $calls, ($opt_I ? $ctimes : $times), $persecs, $idkeys );
  440.     }
  441.     if( ! $opt_U ){
  442.         @a = sort $sort @$idkeys;
  443.         $a = \@a;
  444.     }
  445.     else {
  446.         $a = $idkeys;
  447.     }
  448.     display( $runtime, $hz, $names, $calls, $times, $ctimes, $cnt, $a,
  449.          $deep_times);
  450. }
  451.  
  452. sub group{
  453.     my ($names, $calls, $times, $ctimes, $idkeys ) = @_;
  454.         print "Option G Grouping: [$opt_G]\n";
  455.         # create entries to store grouping
  456.         $$names{$opt_G} = $opt_G;
  457.         $$calls{$opt_G} = 0;
  458.         $$times{$opt_G} = 0;
  459.         $$ctimes{$opt_G} = 0;
  460.         $$idkeys[@$idkeys] = $opt_G;
  461.         # Sum calls for the grouping
  462.  
  463.         my $other = "other";
  464.         if($opt_P){
  465.             $$names{$other} = $other;
  466.             $$calls{$other} = 0;
  467.             $$times{$other} = 0;
  468.             $$ctimes{$other} = 0;
  469.             $$idkeys[@$idkeys] = $other;
  470.         }
  471.  
  472.         for(my $i = 0;$i < @$idkeys - 2;){
  473.             $key = $$idkeys[$i];
  474.             if($key =~ /$opt_G/){
  475.                 $$calls{$opt_G} += $$calls{$key};
  476.                 $$times{$opt_G} += $$times{$key};
  477.                 $$ctimes{$opt_G} += $$ctimes{$key};
  478.                 splice(@$idkeys, $i, 1);
  479.                 next;
  480.             }else{
  481.                 if($opt_P){
  482.                     $$calls{$other} += $$calls{$key};
  483.                     $$times{$other} += $$times{$key};
  484.                     $$ctimes{$other} += $$ctimes{$key};
  485.                     splice(@$idkeys, $i, 1);
  486.                     next;
  487.                 }
  488.             }
  489.             $i++;
  490.         }
  491.         print "Grouping [$opt_G] Calls: [$$calls{$opt_G}]\n".
  492.               "Grouping [$opt_G] Times: [$$times{$opt_G}]\n".
  493.               "Grouping [$opt_G] IncTimes: [$$ctimes{$opt_G}]\n";
  494. }
  495.  
  496. # Sets $runtime to user, system, real, or user+system time.  The
  497. # result is given in seconds.
  498. #
  499. sub settime {
  500.   my( $runtime, $hz ) = @_;
  501.  
  502.   $hz ||= 1;
  503.   
  504.   if( $opt_r ){
  505.     $$runtime = ($rrun_rtime - $overhead - $over_rtime * $total_marks/$over_tests/2)/$hz;
  506.   }
  507.   elsif( $opt_s ){
  508.     $$runtime = ($rrun_stime - $overhead - $over_stime * $total_marks/$over_tests/2)/$hz;
  509.   }
  510.   elsif( $opt_u ){
  511.     $$runtime = ($rrun_utime - $overhead - $over_utime * $total_marks/$over_tests/2)/$hz;
  512.   }
  513.   else{
  514.     $$runtime = ($rrun_ustime - $overhead - ($over_utime + $over_stime) * $total_marks/$over_tests/2)/$hz;
  515.   }
  516.   $$runtime = 0 unless $$runtime > 0;
  517. }
  518.  
  519. sub exclusives_in_tree {
  520.   my( $deep_times ) = @_;
  521.   my $kids_time = 0;
  522.   my $kid;
  523.   # When summing, take into account non-rounded-up kids time.
  524.   for $kid (keys %{$deep_times->{kids}}) {
  525.     $kids_time += $deep_times->{kids}{$kid}{incl_time};
  526.   }
  527.   $kids_time = 0 unless $kids_time >= 0;
  528.   $deep_times->{excl_time} = $deep_times->{incl_time} - $kids_time;
  529.   $deep_times->{excl_time} = 0 unless $deep_times->{excl_time} >= 0;
  530.   for $kid (keys %{$deep_times->{kids}}) {
  531.     exclusives_in_tree($deep_times->{kids}{$kid});
  532.   }
  533.   $deep_times->{incl_time} = 0 unless $deep_times->{incl_time} >= 0;
  534.   $deep_times->{kids_time} = $kids_time;
  535. }
  536.  
  537. sub kids_by_incl { $kids{$b}{incl_time} <=> $kids{$a}{excl_time} 
  538.            or $a cmp $b }
  539.  
  540. sub display_tree {
  541.   my( $deep_times, $name, $level ) = @_;
  542.   exclusives_in_tree($deep_times);
  543.   
  544.   my $kid;
  545.   local *kids = $deep_times->{kids}; # %kids
  546.  
  547.   my $time;
  548.   if (%kids) {
  549.     $time = sprintf '%.*fs = (%.*f + %.*f)', 
  550.       $time_precision, $deep_times->{incl_time}/$hz,
  551.         $time_precision, $deep_times->{excl_time}/$hz,
  552.           $time_precision, $deep_times->{kids_time}/$hz;
  553.   } else {
  554.     $time = sprintf '%.*f', $time_precision, $deep_times->{incl_time}/$hz;
  555.   }
  556.   print ' ' x (2*$level), "$name x $deep_times->{count}  \t${time}s\n"
  557.     if $deep_times->{count};
  558.  
  559.   for $kid (sort kids_by_incl keys %kids) {
  560.     display_tree( $deep_times->{kids}{$kid}, $kid, $level + 1 );
  561.   }  
  562. }
  563.  
  564. # Report the times in seconds.
  565. sub display {
  566.     my( $runtime, $hz, $names, $calls, $times, $ctimes, $cnt, 
  567.         $idkeys, $deep_times ) = @_;
  568.     my( $x, $key, $s, $cs );
  569.     #format: $ncalls, $name, $secs, $percall, $pcnt
  570.  
  571.     if ($opt_S) {
  572.       display_tree( $deep_times, 'toplevel', -1 )
  573.     } else {
  574.       for( $x = 0; $x < @$idkeys; ++$x ){
  575.         $key = $idkeys->[$x];
  576.         $ncalls = $calls->{$key};
  577.         $name = $names->{$key};
  578.         $s = $times->{$key}/$hz;
  579.         $secs = sprintf("%.3f", $s );
  580.         $cs = $ctimes->{$key}/$hz;
  581.         $csecs = sprintf("%.3f", $cs );
  582.         $percall = sprintf("%.4f", $s/$ncalls );
  583.         $cpercall = sprintf("%.4f", $cs/$ncalls );
  584.         $pcnt = sprintf("%.2f",
  585.                 $runtime? ((($opt_I ? $csecs : $secs) / $runtime) * 100.0): 0 );
  586.         write;
  587.         $pcnt = $secs = $ncalls = $percall = "";
  588.         write while( length $name );
  589.         last unless --$cnt;
  590.       }      
  591.     }
  592. }
  593.  
  594. sub move_keys {
  595.   my ($source, $dest) = @_;
  596.   my $kid;
  597.   
  598.   for $kid (keys %$source) {
  599.     if (exists $dest->{$kid}) {
  600.       $dest->{count} += $source->{count};
  601.       $dest->{incl_time} += $source->{incl_time};
  602.       move_keys($source->{kids},$dest->{kids});
  603.     } else {
  604.       $dest->{$kid} = delete $source->{$kid};
  605.     }
  606.   }
  607. }
  608.  
  609. sub add_to_tree {
  610.   my ($curdeep_times, $name, $t) = @_;
  611.   if ($name ne $curdeep_times->[-1]{name} and $opt_A) {
  612.     $name = $curdeep_times->[-1]{name};
  613.   }
  614.   die "Shorted?!" unless @$curdeep_times >= 2;
  615.   $curdeep_times->[-2]{kids}{$name} = { count => 0 , kids => {}, 
  616.                     incl_time => 0,
  617.                       } 
  618.     unless exists $curdeep_times->[-2]{kids}{$name};
  619.   my $entry = $curdeep_times->[-2]{kids}{$name};
  620.   # Now transfer to the new node (could not do earlier, since name can change)
  621.   $entry->{count}++;
  622.   $entry->{incl_time} += $t - $curdeep_times->[-1]{enter_stamp};
  623.   # Merge the kids?
  624.   move_keys($curdeep_times->[-1]->{kids},$entry->{kids});
  625.   pop @$curdeep_times;
  626. }
  627.  
  628.  
  629. sub parsestack {
  630.     my( $fh, $names, $calls, $times, $ctimes, $idkeys ) = @_;
  631.     my( $dir, $name );
  632.     my( $t, $syst, $realt, $usert );
  633.     my( $x, $z, $c, $id, $pack );
  634.     my @stack = ();
  635.     my @tstack = ();
  636.     my $tab = 3;
  637.     my $in = 0;
  638.  
  639.     # remember last call depth and function name
  640.     my $l_in = $in;
  641.     my $l_name = '';
  642.     my $repcnt = 0;
  643.     my $repstr = '';
  644.     my $dprof_t = 0;
  645.     my $dprof_stamp;
  646.     my %cv_hash;
  647.     my $in_level = not defined $opt_g; # Level deep in report grouping
  648.     my $curdeep_times = [$deep_times];
  649.  
  650.     my $over_per_call;
  651.     if   ( $opt_u )    {    $over_per_call = $over_utime        }
  652.     elsif( $opt_s )    {    $over_per_call = $over_stime        }
  653.     elsif( $opt_r )    {    $over_per_call = $over_rtime        }
  654.     else        {    $over_per_call = $over_utime + $over_stime }
  655.     $over_per_call /= 2*$over_tests; # distribute over entry and exit
  656.  
  657.     while(<$fh>){
  658.         next if /^#/;
  659.         last if /^PART/;
  660.  
  661.         chop;
  662.         if (/^&/) {
  663.           ($dir, $id, $pack, $name) = split;
  664.           if ($opt_R and ($name =~ /(?:::)?(__ANON__|END)$/)) {
  665.             $name .= "($id)";
  666.           }
  667.           $cv_hash{$id} = "$pack\::$name";
  668.           next;
  669.         }
  670.         ($dir, $usert, $syst, $realt, $name) = split;
  671.  
  672.         my $ot = $t;
  673.         if ( $dir eq '/' ) {
  674.           $syst = $stack[-1][0];
  675.           $usert = '&';
  676.           $dir = '-';
  677.           #warn("Inserted exit for $stack[-1][0].\n")
  678.         }
  679.         if (defined $realt) { # '+ times nam' '- times nam' or '@ incr'
  680.           if   ( $opt_u )    {    $t = $usert        }
  681.           elsif( $opt_s )    {    $t = $syst        }
  682.           elsif( $opt_r )    {    $t = $realt        }
  683.           else            {    $t = $usert + $syst    }
  684.           $t += $ot, next if $dir eq '@'; # Increments there
  685.         } else {
  686.           # "- id" or "- & name"
  687.           $name = defined $syst ? $syst : $cv_hash{$usert};
  688.         }
  689.  
  690.         next unless $in_level or $name eq $opt_g or $dir eq '*';
  691.         if ( $dir eq '-' or $dir eq '*' ) {
  692.               my $ename = $dir eq '*' ? $stack[-1][0]  : $name;
  693.             $overhead += $over_per_call;
  694.               if ($name eq "Devel::DProf::write") {
  695.               $dprof_t += $t - $dprof_stamp;
  696.               next;
  697.               } elsif (defined $opt_g and $ename eq $opt_g) {
  698.               $in_level--;
  699.             }
  700.             add_to_tree($curdeep_times, $ename,
  701.                     $t - $dprof_t - $overhead) if $opt_S;
  702.             exitstamp( \@stack, \@tstack, 
  703.                    $t - $dprof_t - $overhead, 
  704.                    $times, $ctimes, $ename, \$in, $tab, 
  705.                    $curdeep_times );
  706.         } 
  707.         next unless $in_level or $name eq $opt_g;
  708.         if( $dir eq '+' or $dir eq '*' ){
  709.               if ($name eq "Devel::DProf::write") {
  710.               $dprof_stamp = $t;
  711.               next;
  712.               } elsif (defined $opt_g and $name eq $opt_g) {
  713.               $in_level++;
  714.               }
  715.             $overhead += $over_per_call;
  716.             if( $opt_T ){
  717.                 print ' ' x $in, "$name\n";
  718.                 $in += $tab;
  719.             }
  720.             elsif( $opt_t ){
  721.                 # suppress output on same function if the
  722.                 # same calling level is called.
  723.                 if ($l_in == $in and $l_name eq $name) {
  724.                     $repcnt++;
  725.                 } else {
  726.                     $repstr = ' ('.++$repcnt.'x)'
  727.                          if $repcnt;
  728.                     print ' ' x $l_in, "$l_name$repstr\n"
  729.                         if $l_name ne '';
  730.                     $repstr = '';
  731.                     $repcnt = 0;
  732.                     $l_in = $in;
  733.                     $l_name = $name;
  734.                 }
  735.                 $in += $tab;
  736.             }
  737.             if( ! defined $names->{$name} ){
  738.                 $names->{$name} = $name;
  739.                 $times->{$name} = 0;
  740.                 $ctimes->{$name} = 0;
  741.                 push( @$idkeys, $name );
  742.             }
  743.             $calls->{$name}++;
  744.             push @$curdeep_times, { kids => {}, 
  745.                         name => $name, 
  746.                         enter_stamp => $t - $dprof_t - $overhead,
  747.                           } if $opt_S;
  748.             $x = [ $name, $t - $dprof_t - $overhead ];
  749.             push( @stack, $x );
  750.  
  751.             # my children will put their time here
  752.             push( @tstack, 0 );
  753.         } elsif ($dir ne '-'){
  754.             die "Bad profile: $_";
  755.             }
  756.     }
  757.     if( $opt_t ){
  758.         $repstr = ' ('.++$repcnt.'x)' if $repcnt;
  759.         print ' ' x $l_in, "$l_name$repstr\n";
  760.     }
  761.  
  762.     if( @stack ){
  763.         if( ! $opt_F ){
  764.             warn "Garbled profile is missing some exit time stamps:\n";
  765.             foreach $x (@stack) {
  766.                 print $x->[0],"\n";
  767.             }
  768.             die "Try rerunning dprofpp with -F.\n";
  769.             # I don't want -F to be default behavior--yet
  770.             #  9/18/95 dmr
  771.         }
  772.         else{
  773.             warn( "Faking " . scalar( @stack ) . " exit timestamp(s).\n");
  774.             foreach $x ( reverse @stack ){
  775.                 $name = $x->[0];
  776.                 exitstamp( \@stack, \@tstack, 
  777.                        $t - $dprof_t - $overhead, $times, 
  778.                        $ctimes, $name, \$in, $tab, 
  779.                        $curdeep_times );
  780.                 add_to_tree($curdeep_times, $name,
  781.                         $t - $dprof_t - $overhead)
  782.                   if $opt_S;
  783.             }
  784.         }
  785.     }
  786.     if (defined $opt_g) {
  787.       $runtime = $ctimes->{$opt_g}/$hz;
  788.       $runtime = 0 unless $runtime > 0;
  789.     }
  790. }
  791.  
  792. sub exitstamp {
  793.     my($stack, $tstack, $t, $times, $ctimes, $name, $in, $tab, $deep) = @_;
  794.     my( $x, $c, $z );
  795.  
  796.     $x = pop( @$stack );
  797.     if( ! defined $x ){
  798.         die "Garbled profile, missing an enter time stamp";
  799.     }
  800.     if( $x->[0] ne $name and $opt_G and ($name =~ /$opt_G/)){
  801.       if ($x->[0] =~ /(?:::)?AUTOLOAD$/) {
  802.         if ($opt_A) {
  803.           $name = $x->[0];
  804.         }
  805.       } elsif ( $opt_F ) {
  806.         warn( "Garbled profile, faking exit timestamp:\n\t$name => $x->[0].\n");
  807.         $name = $x->[0];
  808.       } else {
  809.         foreach $z (@stack, $x) {
  810.           print $z->[0],"\n";
  811.         }
  812.         die "Garbled profile, unexpected exit time stamp";
  813.       }
  814.     }
  815.     if( $opt_T || $opt_t ){
  816.         $$in -= $tab;
  817.     }
  818.     # collect childtime
  819.     $c = pop( @$tstack );
  820.     # total time this func has been active
  821.     $z = $t - $x->[1];
  822.     $ctimes->{$name} += $z;
  823.     $times->{$name} += ($z > $c)? $z - $c: 0;
  824.     # pass my time to my parent
  825.     if( @$tstack ){
  826.         $c = pop( @$tstack );
  827.         push( @$tstack, $c + $z );
  828.     }
  829. }
  830.  
  831.  
  832. sub header {
  833.     my $fh = shift;
  834.     chop($_ = <$fh>);
  835.     if( ! /^#fOrTyTwO$/ ){
  836.         die "Not a perl profile";
  837.     }
  838.     while(<$fh>){
  839.         next if /^#/;
  840.         last if /^PART/;
  841.         eval;
  842.     }
  843.     $over_tests = 1 unless $over_tests;
  844.     $time_precision = length int ($hz - 1);    # log ;-)
  845. }
  846.  
  847.  
  848. # Report avg time-per-function in seconds
  849. sub percalc {
  850.     my( $calls, $times, $persecs, $idkeys ) = @_;
  851.     my( $x, $t, $n, $key );
  852.  
  853.     for( $x = 0; $x < @$idkeys; ++$x ){
  854.         $key = $idkeys->[$x];
  855.         $n = $calls->{$key};
  856.         $t = $times->{$key} / $hz;
  857.         $persecs->{$key} = $t ? $t / $n : 0;
  858.     }
  859. }
  860.  
  861.  
  862. # Runs the given script with the given profiler and the given perl.
  863. sub run_profiler {
  864.     my $script = shift;
  865.     my $profiler = shift;
  866.     my $startperl = shift;
  867.     my @script_parts = split /\s+/, $script;
  868.  
  869.     system $startperl, "-d:$profiler", @script_parts;
  870.     if( $? / 256 > 0 ){
  871.         my $cmd = join ' ', @script_parts;
  872.         die "Failed: $startperl -d:$profiler $cmd: $!";
  873.     }
  874. }
  875.  
  876.  
  877. sub by_time { $times->{$b} <=> $times->{$a} }
  878. sub by_ctime { $ctimes->{$b} <=> $ctimes->{$a} }
  879. sub by_calls { $calls->{$b} <=> $calls->{$a} }
  880. sub by_alpha { $names->{$a} cmp $names->{$b} }
  881. sub by_avgcpu { $persecs->{$b} <=> $persecs->{$a} }
  882. # Reversed
  883. sub rby_time { $times->{$a} <=> $times->{$b} }
  884. sub rby_ctime { $ctimes->{$a} <=> $ctimes->{$b} }
  885. sub rby_calls { $calls->{$a} <=> $calls->{$b} }
  886. sub rby_alpha { $names->{$b} cmp $names->{$a} }
  887. sub rby_avgcpu { $persecs->{$a} <=> $persecs->{$b} }
  888.  
  889.  
  890. format CSTAT_top =
  891. Total Elapsed Time = @>>>>>>> Seconds
  892. (($rrun_rtime - $overhead - $over_rtime * $total_marks/$over_tests/2) / $hz)
  893.   @>>>>>>>>>> Time = @>>>>>>> Seconds
  894. $whichtime, $runtime
  895. @<<<<<<<< Times
  896. $incl_excl
  897. %Time ExclSec CumulS #Calls sec/call Csec/c  Name
  898. .
  899.  
  900. format STAT =
  901.  ^>>>   ^>>>> ^>>>>> ^>>>>>   ^>>>>> ^>>>>>  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  902. $pcnt, $secs, $csecs, $ncalls, $percall, $cpercall, $name
  903. .
  904.  
  905.  
  906. __END__
  907. :endofperl
  908.