1 /* XXX DProf could use some cleanups for PERL_IMPLICIT_CONTEXT */
11 # define dTHR int dummy_thr
14 /*#define DBG_SUB 1 /* */
15 /*#define DBG_TIMER 1 /* */
18 # define DBG_SUB_NOTIFY(A,B) warn( A, B )
20 # define DBG_SUB_NOTIFY(A,B) /* nothing */
24 # define DBG_TIMER_NOTIFY(A) warn( A )
26 # define DBG_TIMER_NOTIFY(A) /* nothing */
29 static U32 dprof_ticks;
31 /* HZ == clock ticks per second */
35 # include <starlet.h> /* prototype for sys$gettim() */
36 clock_t dprof_times(struct tms *bufptr) {
38 /* Get wall time and convert to 10 ms intervals to
39 * produce the return value dprof expects */
40 # if defined(__DECC) && defined (__ALPHA)
43 _ckvmssts(sys$gettim(&vmstime));
45 retval = vmstime & 0x7fffffff;
47 /* (Older hw or ccs don't have an atomic 64-bit type, so we
48 * juggle 32-bit ints (and a float) to produce a time_t result
49 * with minimal loss of information.) */
50 long int vmstime[2],remainder,divisor = 100000;
51 _ckvmssts(sys$gettim((unsigned long int *)vmstime));
52 vmstime[1] &= 0x7fff; /* prevent overflow in EDIV */
53 _ckvmssts(lib$ediv(&divisor,vmstime,(long int *)&retval,&remainder));
55 /* Fill in the struct tms using the CRTL routine . . .*/
56 times((tbuffer_t *)bufptr);
57 return (clock_t) retval;
59 # define Times(ptr) (dprof_times(ptr))
68 # ifdef OS2 /* times() has significant overhead */
69 # define Times(ptr) (dprof_times(ptr))
70 # define INCL_DOSPROFILE
71 # define INCL_DOSERRORS
73 # define toLongLong(arg) (*(long long*)&(arg))
74 # define DPROF_HZ dprof_ticks
77 static long long start_cnt;
79 dprof_times(struct tms *t)
85 if (CheckOSError(DosTmrQueryFreq(&frequ)))
86 croak("DosTmrQueryFreq: %s", SvPV(perl_get_sv("!",TRUE),na));
88 frequ = frequ/DPROF_HZ; /* count per tick */
89 if (CheckOSError(DosTmrQueryTime(&cnt)))
90 croak("DosTmrQueryTime: %s",
91 SvPV(perl_get_sv("!",TRUE),na));
92 start_cnt = toLongLong(cnt);
95 if (CheckOSError(DosTmrQueryTime(&cnt)))
96 croak("DosTmrQueryTime: %s", SvPV(perl_get_sv("!",TRUE),na));
98 return (t->tms_utime = (toLongLong(cnt) - start_cnt)/frequ);
101 # define Times(ptr) (times(ptr))
106 XS(XS_Devel__DProf_END); /* used by prof_mark() */
108 static SV * Sub; /* pointer to $DB::sub */
109 static PerlIO *fp; /* pointer to tmon.out file */
112 static long TIMES_LOCATION=42;/* Where in the file to store the time totals */
113 static int SAVE_STACK = 1<<14; /* How much data to buffer until */
116 static int prof_pid; /* pid of profiled process */
118 /* Everything is built on times(2). See its manpage for a description
123 struct tms prof_start,
127 clock_t rprof_start, /* elapsed real time, in ticks */
129 wprof_u, wprof_s, wprof_r;
132 clock_t tms_utime; /* cpu time spent in user space */
133 clock_t tms_stime; /* cpu time spent in system */
134 clock_t realtime; /* elapsed real time, in ticks */
140 typedef union prof_any PROFANY;
142 static PROFANY *profstack;
143 static int profstack_max = 128;
144 static int profstack_ix = 0;
147 prof_dump(opcode ptype, char *name)
149 if(ptype == OP_LEAVESUB){
150 PerlIO_printf(fp,"- & %s\n", name );
151 } else if(ptype == OP_ENTERSUB) {
152 PerlIO_printf(fp,"+ & %s\n", name );
153 } else if(ptype == OP_DIE) {
154 PerlIO_printf(fp,"/ & %s\n", name );
156 PerlIO_printf(fp,"Profiler unknown prof code %d\n", ptype);
162 prof_dumpa(opcode ptype, U32 id)
164 if(ptype == OP_LEAVESUB){
165 PerlIO_printf(fp,"- %lx\n", id );
166 } else if(ptype == OP_ENTERSUB) {
167 PerlIO_printf(fp,"+ %lx\n", id );
168 } else if(ptype == OP_GOTO) {
169 PerlIO_printf(fp,"* %lx\n", id );
170 } else if(ptype == OP_DIE) {
171 PerlIO_printf(fp,"/ %lx\n", id );
173 PerlIO_printf(fp,"Profiler unknown prof code %d\n", ptype);
178 prof_dumps(U32 id, char *pname, char *gname)
180 PerlIO_printf(fp,"& %lx %s %s\n", id, pname, gname);
183 static clock_t otms_utime, otms_stime, orealtime;
186 prof_dumpt(long tms_utime, long tms_stime, long realtime)
188 PerlIO_printf(fp,"@ %ld %ld %ld\n", tms_utime, tms_stime, realtime);
192 prof_dump_until(long ix)
196 clock_t realtime1, realtime2;
198 realtime1 = Times(&t1);
201 opcode ptype = profstack[base++].ptype;
202 if (ptype == OP_TIME) {
203 long tms_utime = profstack[base++].tms_utime;
204 long tms_stime = profstack[base++].tms_stime;
205 long realtime = profstack[base++].realtime;
207 prof_dumpt(tms_utime, tms_stime, realtime);
208 } else if (ptype == OP_GV) {
209 U32 id = profstack[base++].id;
210 char *pname = profstack[base++].name;
211 char *gname = profstack[base++].name;
213 prof_dumps(id, pname, gname);
215 #ifdef PERLDBf_NONAME
216 U32 id = profstack[base++].id;
217 prof_dumpa(ptype, id);
219 char *name = profstack[base++].name;
220 prof_dump(ptype, name);
225 realtime2 = Times(&t2);
226 if (realtime2 != realtime1 || t1.tms_utime != t2.tms_utime
227 || t1.tms_stime != t2.tms_stime) {
228 wprof_r += realtime2 - realtime1;
229 wprof_u += t2.tms_utime - t1.tms_utime;
230 wprof_s += t2.tms_stime - t1.tms_stime;
232 PerlIO_printf(fp,"+ & Devel::DProf::write\n" );
233 PerlIO_printf(fp,"@ %ld %ld %ld\n",
234 t2.tms_utime - t1.tms_utime, t2.tms_stime - t1.tms_stime,
235 realtime2 - realtime1);
236 PerlIO_printf(fp,"- & Devel::DProf::write\n" );
237 otms_utime = t2.tms_utime;
238 otms_stime = t2.tms_stime;
239 orealtime = realtime2;
245 static U32 total = 0;
248 prof_mark( opcode ptype )
251 clock_t realtime, rdelta, udelta, sdelta;
259 if( profstack_ix + 5 > profstack_max ){
260 profstack_max = profstack_max * 3 / 2;
261 Renew( profstack, profstack_max, PROFANY );
265 realtime = Times(&t);
266 rdelta = realtime - orealtime;
267 udelta = t.tms_utime - otms_utime;
268 sdelta = t.tms_stime - otms_stime;
269 if (rdelta || udelta || sdelta) {
271 profstack[profstack_ix++].ptype = OP_TIME;
272 profstack[profstack_ix++].tms_utime = udelta;
273 profstack[profstack_ix++].tms_stime = sdelta;
274 profstack[profstack_ix++].realtime = rdelta;
275 } else { /* Write it to disk now so's not to eat up core */
276 if (prof_pid == (int)getpid()) {
277 prof_dumpt(udelta, sdelta, rdelta);
281 orealtime = realtime;
282 otms_stime = t.tms_stime;
283 otms_utime = t.tms_utime;
286 #ifdef PERLDBf_NONAME
293 cv = (CV*)SvIVX(Sub);
294 svp = hv_fetch(cv_hash, (char*)&cv, sizeof(CV*), TRUE);
298 sv_setiv(*svp, id = ++lastid);
299 pname = ((GvSTASH(gv) && HvNAME(GvSTASH(gv)))
300 ? HvNAME(GvSTASH(gv))
303 if (CvXSUB(cv) == XS_Devel__DProf_END)
305 if (SAVE_STACK) { /* Store it for later recording -JH */
306 profstack[profstack_ix++].ptype = OP_GV;
307 profstack[profstack_ix++].id = id;
308 profstack[profstack_ix++].name = pname;
309 profstack[profstack_ix++].name = gname;
310 } else { /* Write it to disk now so's not to eat up core */
312 /* Only record the parent's info */
313 if (prof_pid == (int)getpid()) {
314 prof_dumps(id, pname, gname);
317 perldb = 0; /* Do not debug the kid. */
324 pv = SvPV( Sub, len );
327 /* Attempt to make CODE refs slightly identifiable by
328 * including their package name.
331 if( sv && SvTYPE(sv) == SVt_PVCV ){
333 hvname = HvNAME(CvSTASH(sv));
335 else if( CvXSUB(sv) == &XS_Devel__DProf_END ){
336 /*warn( "prof_mark() found dprof::end");*/
337 return; /* don't profile Devel::DProf::END */
340 croak( "DProf prof_mark() lost on CODE ref %s\n", pv );
342 len += strlen( hvname ) + 2; /* +2 for ::'s */
346 croak( "DProf prof_mark() lost on supposed CODE ref %s.\n", pv );
348 name = (char *)safemalloc( len * sizeof(char) + 1 );
349 strcpy( name, hvname );
350 strcat( name, "::" );
354 if( *(pv+len-1) == 'D' ){
355 /* It could be an &AUTOLOAD. */
357 /* I measured a bunch of *.pl and *.pm (from Perl
358 * distribution and other misc things) and found
359 * 780 fully-qualified names. They averaged
360 * about 19 chars each. Only 1 of those names
361 * ended with 'D' and wasn't an &AUTOLOAD--it
362 * was &overload::OVERLOAD.
366 if( strcmp( pv+len-9, ":AUTOLOAD" ) == 0 ){
367 /* The sub name is in $AUTOLOAD */
368 sv = perl_get_sv( pv, 0 );
370 croak("DProf prof_mark() lost on AUTOLOAD (%s).\n", pv );
373 DBG_SUB_NOTIFY( " AUTOLOAD(%s)\n", pv );
378 #endif /* PERLDBf_NONAME */
381 if (SAVE_STACK) { /* Store it for later recording -JH */
382 profstack[profstack_ix++].ptype = ptype;
383 #ifdef PERLDBf_NONAME
384 profstack[profstack_ix++].id = id;
386 profstack[profstack_ix++].name = name;
388 /* Only record the parent's info */
389 if (SAVE_STACK < profstack_ix) {
390 if (prof_pid == (int)getpid())
391 prof_dump_until(profstack_ix);
393 perldb = 0; /* Do not debug the kid. */
396 } else { /* Write it to disk now so's not to eat up core */
398 /* Only record the parent's info */
399 if (prof_pid == (int)getpid()) {
400 #ifdef PERLDBf_NONAME
401 prof_dumpa(ptype, id);
403 prof_dump(ptype, name);
407 perldb = 0; /* Do not debug the kid. */
411 static U32 default_perldb;
414 # define defstash PL_defstash
417 /* Counts overhead of prof_mark and extra XS call. */
419 test_time(clock_t *r, clock_t *u, clock_t *s)
422 CV *cv = perl_get_cv("Devel::DProf::NONESUCH_noxs", FALSE);
424 HV *oldstash = curstash;
426 clock_t realtime1, realtime2;
428 U32 ostack = SAVE_STACK;
429 U32 operldb = perldb;
431 SAVE_STACK = 1000000;
432 realtime1 = Times(&t1);
436 /* Disable debugging of perl_call_sv on second pass: */
437 curstash = (k == 0 ? defstash : debstash);
438 perldb = default_perldb;
441 profstack_ix = 0; /* Do not let the stack grow */
443 /* prof_mark( OP_ENTERSUB ); */
445 PUSHMARK( stack_sp );
446 perl_call_sv( (SV*)cv, G_SCALAR );
448 /* prof_mark( OP_LEAVESUB ); */
452 if (k == 0) { /* Put time with debugging */
453 realtime2 = Times(&t2);
454 *r = realtime2 - realtime1;
455 *u = t2.tms_utime - t1.tms_utime;
456 *s = t2.tms_stime - t1.tms_stime;
457 } else { /* Subtract time without debug */
458 realtime1 = Times(&t1);
459 *r -= realtime1 - realtime2;
460 *u -= t1.tms_utime - t2.tms_utime;
461 *s -= t1.tms_stime - t2.tms_stime;
475 /* fp is opened in the BOOT section */
476 PerlIO_printf(fp, "#fOrTyTwO\n" );
477 PerlIO_printf(fp, "$hz=%d;\n", DPROF_HZ );
478 PerlIO_printf(fp, "$XS_VERSION='DProf %s';\n", XS_VERSION );
479 PerlIO_printf(fp, "# All values are given in HZ\n" );
480 test_time(&r, &u, &s);
481 PerlIO_printf(fp, "$over_utime=%ld; $over_stime=%ld; $over_rtime=%ld;\n",
483 PerlIO_printf(fp, "$over_tests=10000;\n");
485 TIMES_LOCATION = PerlIO_tell(fp);
487 /* Pad with whitespace. */
488 /* This should be enough even for very large numbers. */
489 PerlIO_printf(fp, "%*s\n", 240 , "");
491 PerlIO_printf(fp, "\n");
492 PerlIO_printf(fp, "PART2\n" );
500 /* fp is opened in the BOOT section */
502 /* Now that we know the runtimes, fill them in at the recorded
508 prof_dump_until(profstack_ix);
510 PerlIO_seek(fp, TIMES_LOCATION, SEEK_SET);
511 /* Write into reserved 240 bytes: */
512 PerlIO_printf(fp, "$rrun_utime=%ld; $rrun_stime=%ld; $rrun_rtime=%ld;",
513 prof_end.tms_utime - prof_start.tms_utime - wprof_u,
514 prof_end.tms_stime - prof_start.tms_stime - wprof_s,
515 rprof_end - rprof_start - wprof_r );
516 PerlIO_printf(fp, "\n$total_marks=%ld;", total);
523 static U32 depth = 0;
526 check_depth(pTHX_ void *foo)
528 U32 need_depth = (U32)foo;
529 if (need_depth != depth) {
530 if (need_depth > depth) {
531 warn("garbled call depth when profiling");
533 I32 marks = depth - need_depth;
535 /* warn("Check_depth: got %d, expected %d\n", depth, need_depth); */
551 HV *oldstash = curstash;
555 DBG_SUB_NOTIFY( "XS DBsub(%s)\n", SvPV(Sub, na) );
557 #ifndef PERLDBf_NONAME /* Was needed on older Perls */
558 sv_setiv( DBsingle, 0 ); /* disable DB single-stepping */
561 SAVEDESTRUCTOR(check_depth, (void*)depth);
564 prof_mark( OP_ENTERSUB );
565 PUSHMARK( ORIGMARK );
568 perl_call_sv( (SV*)SvIV(Sub), GIMME | G_NODEBUG);
570 curstash = debstash; /* To disable debugging of perl_call_sv */
571 #ifdef PERLDBf_NONAME
572 perl_call_sv( (SV*)SvIV(Sub), GIMME );
574 perl_call_sv( Sub, GIMME );
579 prof_mark( OP_LEAVESUB );
589 prof_mark( OP_GOTO );
593 #endif /* for_real */
597 MODULE = Devel::DProf PACKAGE = DB
604 HV *oldstash = curstash;
605 /* SP -= items; added by xsubpp */
606 DBG_SUB_NOTIFY( "XS DBsub(%s)\n", SvPV(Sub, na) );
608 sv_setiv( DBsingle, 0 ); /* disable DB single-stepping */
610 prof_mark( OP_ENTERSUB );
611 PUSHMARK( ORIGMARK );
613 curstash = debstash; /* To disable debugging of perl_call_sv
615 perl_call_sv( Sub, GIMME );
618 prof_mark( OP_LEAVESUB );
620 /* PUTBACK; added by xsubpp */
624 MODULE = Devel::DProf PACKAGE = Devel::DProf
630 /* maybe the process forked--we want only
631 * the parent's profile.
633 if( prof_pid == (int)getpid() ){
634 rprof_end = Times(&prof_end);
635 DBG_TIMER_NOTIFY("Profiler timer is off.\n");
644 /* Before we go anywhere make sure we were invoked
645 * properly, else we'll dump core.
648 croak("DProf: run perl with -d to use DProf.\n");
650 /* When we hook up the XS DB::sub we'll be redefining
651 * the DB::sub from the PM file. Turn off warnings
655 I32 warn_tmp = dowarn;
657 newXS("DB::sub", XS_DB_sub, file);
658 newXS("DB::goto", XS_DB_goto, file);
662 Sub = GvSV(DBsub); /* name of current sub */
663 sv_setiv( DBsingle, 0 ); /* disable DB single-stepping */
666 char *buffer = getenv("PERL_DPROF_BUFFER");
669 SAVE_STACK = atoi(buffer);
672 buffer = getenv("PERL_DPROF_TICKS");
675 dprof_ticks = atoi(buffer); /* Used under OS/2 only */
681 if( (fp = PerlIO_open( "tmon.out", "w" )) == NULL )
682 croak("DProf: unable to write tmon.out, errno = %d\n", errno );
683 #ifdef PERLDBf_NONAME
684 default_perldb = PERLDBf_NONAME | PERLDBf_SUB; /* no name needed. */
686 default_perldb = default_perldb | PERLDBf_GOTO;
691 default_perldb = PERLDBf_SUB; /* debug subroutines only. */
694 prof_pid = (int)getpid();
696 New( 0, profstack, profstack_max, PROFANY );
700 DBG_TIMER_NOTIFY("Profiler timer is on.\n");
701 orealtime = rprof_start = Times(&prof_start);
702 otms_utime = prof_start.tms_utime;
703 otms_stime = prof_start.tms_stime;
704 perldb = default_perldb;