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