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 */
33 # define HZ ((I32)CLK_TCK)
35 # include <starlet.h> /* prototype for sys$gettim() */
36 clock_t dprof_times(struct tms *bufptr) {
39 /* Get wall time and convert to 10 ms intervals to
40 * produce the return value dprof expects */
41 # if defined(__DECC) && defined (__ALPHA)
44 _ckvmssts(sys$gettim(&vmstime));
46 retval = vmstime & 0x7fffffff;
48 /* (Older hw or ccs don't have an atomic 64-bit type, so we
49 * juggle 32-bit ints (and a float) to produce a time_t result
50 * with minimal loss of information.) */
51 long int vmstime[2],remainder,divisor = 100000;
52 _ckvmssts(sys$gettim((unsigned long int *)vmstime));
53 vmstime[1] &= 0x7fff; /* prevent overflow in EDIV */
54 _ckvmssts(lib$ediv(&divisor,vmstime,(long int *)&retval,&remainder));
56 /* Fill in the struct tms using the CRTL routine . . .*/
57 times((tbuffer_t *)bufptr);
58 return (clock_t) retval;
60 # define Times(ptr) (dprof_times(ptr))
64 # define HZ ((I32)CLK_TCK)
69 # ifdef OS2 /* times() has significant overhead */
70 # define Times(ptr) (dprof_times(ptr))
71 # define INCL_DOSPROFILE
72 # define INCL_DOSERRORS
74 # define toLongLong(arg) (*(long long*)&(arg))
75 # define DPROF_HZ dprof_ticks
78 static long long start_cnt;
80 dprof_times(struct tms *t)
86 if (CheckOSError(DosTmrQueryFreq(&frequ)))
87 croak("DosTmrQueryFreq: %s", SvPV(perl_get_sv("!",TRUE),na));
89 frequ = frequ/DPROF_HZ; /* count per tick */
90 if (CheckOSError(DosTmrQueryTime(&cnt)))
91 croak("DosTmrQueryTime: %s",
92 SvPV(perl_get_sv("!",TRUE),na));
93 start_cnt = toLongLong(cnt);
96 if (CheckOSError(DosTmrQueryTime(&cnt)))
97 croak("DosTmrQueryTime: %s", SvPV(perl_get_sv("!",TRUE),na));
99 return (t->tms_utime = (toLongLong(cnt) - start_cnt)/frequ);
102 # define Times(ptr) (times(ptr))
107 XS(XS_Devel__DProf_END); /* used by prof_mark() */
109 static SV * Sub; /* pointer to $DB::sub */
110 static PerlIO *fp; /* pointer to tmon.out file */
113 static long TIMES_LOCATION=42;/* Where in the file to store the time totals */
114 static int SAVE_STACK = 1<<14; /* How much data to buffer until */
117 static int prof_pid; /* pid of profiled process */
119 /* Everything is built on times(2). See its manpage for a description
124 struct tms prof_start,
128 clock_t rprof_start, /* elapsed real time, in ticks */
130 wprof_u, wprof_s, wprof_r;
133 clock_t tms_utime; /* cpu time spent in user space */
134 clock_t tms_stime; /* cpu time spent in system */
135 clock_t realtime; /* elapsed real time, in ticks */
141 typedef union prof_any PROFANY;
143 static PROFANY *profstack;
144 static int profstack_max = 128;
145 static int profstack_ix = 0;
148 prof_dump(opcode ptype, char *name)
150 if(ptype == OP_LEAVESUB){
151 PerlIO_printf(fp,"- & %s\n", name );
152 } else if(ptype == OP_ENTERSUB) {
153 PerlIO_printf(fp,"+ & %s\n", name );
154 } else if(ptype == OP_DIE) {
155 PerlIO_printf(fp,"/ & %s\n", name );
157 PerlIO_printf(fp,"Profiler unknown prof code %d\n", ptype);
163 prof_dumpa(opcode ptype, U32 id)
165 if(ptype == OP_LEAVESUB){
166 PerlIO_printf(fp,"- %lx\n", id );
167 } else if(ptype == OP_ENTERSUB) {
168 PerlIO_printf(fp,"+ %lx\n", id );
169 } else if(ptype == OP_GOTO) {
170 PerlIO_printf(fp,"* %lx\n", id );
171 } else if(ptype == OP_DIE) {
172 PerlIO_printf(fp,"/ %lx\n", id );
174 PerlIO_printf(fp,"Profiler unknown prof code %d\n", ptype);
179 prof_dumps(U32 id, char *pname, char *gname)
181 PerlIO_printf(fp,"& %lx %s %s\n", id, pname, gname);
184 static clock_t otms_utime, otms_stime, orealtime;
187 prof_dumpt(long tms_utime, long tms_stime, long realtime)
189 PerlIO_printf(fp,"@ %ld %ld %ld\n", tms_utime, tms_stime, realtime);
193 prof_dump_until(long ix)
197 clock_t realtime1, realtime2;
199 realtime1 = Times(&t1);
202 opcode ptype = profstack[base++].ptype;
203 if (ptype == OP_TIME) {
204 long tms_utime = profstack[base++].tms_utime;
205 long tms_stime = profstack[base++].tms_stime;
206 long realtime = profstack[base++].realtime;
208 prof_dumpt(tms_utime, tms_stime, realtime);
209 } else if (ptype == OP_GV) {
210 U32 id = profstack[base++].id;
211 char *pname = profstack[base++].name;
212 char *gname = profstack[base++].name;
214 prof_dumps(id, pname, gname);
216 #ifdef PERLDBf_NONAME
217 U32 id = profstack[base++].id;
218 prof_dumpa(ptype, id);
220 char *name = profstack[base++].name;
221 prof_dump(ptype, name);
226 realtime2 = Times(&t2);
227 if (realtime2 != realtime1 || t1.tms_utime != t2.tms_utime
228 || t1.tms_stime != t2.tms_stime) {
229 wprof_r += realtime2 - realtime1;
230 wprof_u += t2.tms_utime - t1.tms_utime;
231 wprof_s += t2.tms_stime - t1.tms_stime;
233 PerlIO_printf(fp,"+ & Devel::DProf::write\n" );
234 PerlIO_printf(fp,"@ %ld %ld %ld\n",
235 t2.tms_utime - t1.tms_utime, t2.tms_stime - t1.tms_stime,
236 realtime2 - realtime1);
237 PerlIO_printf(fp,"- & Devel::DProf::write\n" );
238 otms_utime = t2.tms_utime;
239 otms_stime = t2.tms_stime;
240 orealtime = realtime2;
246 static U32 total = 0;
249 prof_mark( opcode ptype )
252 clock_t realtime, rdelta, udelta, sdelta;
260 if( profstack_ix + 5 > profstack_max ){
261 profstack_max = profstack_max * 3 / 2;
262 Renew( profstack, profstack_max, PROFANY );
266 realtime = Times(&t);
267 rdelta = realtime - orealtime;
268 udelta = t.tms_utime - otms_utime;
269 sdelta = t.tms_stime - otms_stime;
270 if (rdelta || udelta || sdelta) {
272 profstack[profstack_ix++].ptype = OP_TIME;
273 profstack[profstack_ix++].tms_utime = udelta;
274 profstack[profstack_ix++].tms_stime = sdelta;
275 profstack[profstack_ix++].realtime = rdelta;
276 } else { /* Write it to disk now so's not to eat up core */
277 if (prof_pid == (int)getpid()) {
278 prof_dumpt(udelta, sdelta, rdelta);
282 orealtime = realtime;
283 otms_stime = t.tms_stime;
284 otms_utime = t.tms_utime;
287 #ifdef PERLDBf_NONAME
295 cv = (CV*)PTR_CAST SvIVX(Sub);
296 svp = hv_fetch(cv_hash, (char*)&cv, sizeof(CV*), TRUE);
300 sv_setiv(*svp, id = ++lastid);
301 pname = ((GvSTASH(gv) && HvNAME(GvSTASH(gv)))
302 ? HvNAME(GvSTASH(gv))
305 if (CvXSUB(cv) == XS_Devel__DProf_END)
307 if (SAVE_STACK) { /* Store it for later recording -JH */
308 profstack[profstack_ix++].ptype = OP_GV;
309 profstack[profstack_ix++].id = id;
310 profstack[profstack_ix++].name = pname;
311 profstack[profstack_ix++].name = gname;
312 } else { /* Write it to disk now so's not to eat up core */
314 /* Only record the parent's info */
315 if (prof_pid == (int)getpid()) {
316 prof_dumps(id, pname, gname);
319 perldb = 0; /* Do not debug the kid. */
326 pv = SvPV( Sub, len );
329 /* Attempt to make CODE refs slightly identifiable by
330 * including their package name.
333 if( sv && SvTYPE(sv) == SVt_PVCV ){
335 hvname = HvNAME(CvSTASH(sv));
337 else if( CvXSUB(sv) == &XS_Devel__DProf_END ){
338 /*warn( "prof_mark() found dprof::end");*/
339 return; /* don't profile Devel::DProf::END */
342 croak( "DProf prof_mark() lost on CODE ref %s\n", pv );
344 len += strlen( hvname ) + 2; /* +2 for ::'s */
348 croak( "DProf prof_mark() lost on supposed CODE ref %s.\n", pv );
350 name = (char *)safemalloc( len * sizeof(char) + 1 );
351 strcpy( name, hvname );
352 strcat( name, "::" );
356 if( *(pv+len-1) == 'D' ){
357 /* It could be an &AUTOLOAD. */
359 /* I measured a bunch of *.pl and *.pm (from Perl
360 * distribution and other misc things) and found
361 * 780 fully-qualified names. They averaged
362 * about 19 chars each. Only 1 of those names
363 * ended with 'D' and wasn't an &AUTOLOAD--it
364 * was &overload::OVERLOAD.
368 if( strcmp( pv+len-9, ":AUTOLOAD" ) == 0 ){
369 /* The sub name is in $AUTOLOAD */
370 sv = perl_get_sv( pv, 0 );
372 croak("DProf prof_mark() lost on AUTOLOAD (%s).\n", pv );
375 DBG_SUB_NOTIFY( " AUTOLOAD(%s)\n", pv );
380 #endif /* PERLDBf_NONAME */
383 if (SAVE_STACK) { /* Store it for later recording -JH */
384 profstack[profstack_ix++].ptype = ptype;
385 #ifdef PERLDBf_NONAME
386 profstack[profstack_ix++].id = id;
388 profstack[profstack_ix++].name = name;
390 /* Only record the parent's info */
391 if (SAVE_STACK < profstack_ix) {
392 if (prof_pid == (int)getpid())
393 prof_dump_until(profstack_ix);
395 perldb = 0; /* Do not debug the kid. */
398 } else { /* Write it to disk now so's not to eat up core */
400 /* Only record the parent's info */
401 if (prof_pid == (int)getpid()) {
402 #ifdef PERLDBf_NONAME
403 prof_dumpa(ptype, id);
405 prof_dump(ptype, name);
409 perldb = 0; /* Do not debug the kid. */
413 static U32 default_perldb;
416 # define defstash PL_defstash
419 /* Counts overhead of prof_mark and extra XS call. */
421 test_time(clock_t *r, clock_t *u, clock_t *s)
425 CV *cv = perl_get_cv("Devel::DProf::NONESUCH_noxs", FALSE);
427 HV *oldstash = curstash;
429 clock_t realtime1, realtime2;
431 U32 ostack = SAVE_STACK;
432 U32 operldb = perldb;
434 SAVE_STACK = 1000000;
435 realtime1 = Times(&t1);
439 /* Disable debugging of perl_call_sv on second pass: */
440 curstash = (k == 0 ? defstash : debstash);
441 perldb = default_perldb;
444 profstack_ix = 0; /* Do not let the stack grow */
446 /* prof_mark( OP_ENTERSUB ); */
448 PUSHMARK( stack_sp );
449 perl_call_sv( (SV*)cv, G_SCALAR );
451 /* prof_mark( OP_LEAVESUB ); */
455 if (k == 0) { /* Put time with debugging */
456 realtime2 = Times(&t2);
457 *r = realtime2 - realtime1;
458 *u = t2.tms_utime - t1.tms_utime;
459 *s = t2.tms_stime - t1.tms_stime;
460 } else { /* Subtract time without debug */
461 realtime1 = Times(&t1);
462 *r -= realtime1 - realtime2;
463 *u -= t1.tms_utime - t2.tms_utime;
464 *s -= t1.tms_stime - t2.tms_stime;
478 /* fp is opened in the BOOT section */
479 PerlIO_printf(fp, "#fOrTyTwO\n" );
480 PerlIO_printf(fp, "$hz=%d;\n", DPROF_HZ );
481 PerlIO_printf(fp, "$XS_VERSION='DProf %s';\n", XS_VERSION );
482 PerlIO_printf(fp, "# All values are given in HZ\n" );
483 test_time(&r, &u, &s);
484 PerlIO_printf(fp, "$over_utime=%ld; $over_stime=%ld; $over_rtime=%ld;\n",
486 PerlIO_printf(fp, "$over_tests=10000;\n");
488 TIMES_LOCATION = PerlIO_tell(fp);
490 /* Pad with whitespace. */
491 /* This should be enough even for very large numbers. */
492 PerlIO_printf(fp, "%*s\n", 240 , "");
494 PerlIO_printf(fp, "\n");
495 PerlIO_printf(fp, "PART2\n" );
503 /* fp is opened in the BOOT section */
505 /* Now that we know the runtimes, fill them in at the recorded
511 prof_dump_until(profstack_ix);
513 PerlIO_seek(fp, TIMES_LOCATION, SEEK_SET);
514 /* Write into reserved 240 bytes: */
515 PerlIO_printf(fp, "$rrun_utime=%ld; $rrun_stime=%ld; $rrun_rtime=%ld;",
516 prof_end.tms_utime - prof_start.tms_utime - wprof_u,
517 prof_end.tms_stime - prof_start.tms_stime - wprof_s,
518 rprof_end - rprof_start - wprof_r );
519 PerlIO_printf(fp, "\n$total_marks=%ld;", total);
526 static U32 depth = 0;
529 check_depth(pTHX_ void *foo)
531 U32 need_depth = (U32)foo;
532 if (need_depth != depth) {
533 if (need_depth > depth) {
534 warn("garbled call depth when profiling");
536 I32 marks = depth - need_depth;
538 /* warn("Check_depth: got %d, expected %d\n", depth, need_depth); */
554 HV *oldstash = curstash;
558 DBG_SUB_NOTIFY( "XS DBsub(%s)\n", SvPV(Sub, na) );
560 #ifndef PERLDBf_NONAME /* Was needed on older Perls */
561 sv_setiv( DBsingle, 0 ); /* disable DB single-stepping */
564 SAVEDESTRUCTOR(check_depth, (void*)depth);
567 prof_mark( OP_ENTERSUB );
568 PUSHMARK( ORIGMARK );
571 perl_call_sv( (SV*)PTR_CAST SvIV(Sub), GIMME | G_NODEBUG);
573 curstash = debstash; /* To disable debugging of perl_call_sv */
574 #ifdef PERLDBf_NONAME
575 perl_call_sv( (SV*)SvIV(Sub), GIMME );
577 perl_call_sv( Sub, GIMME );
582 prof_mark( OP_LEAVESUB );
592 prof_mark( OP_GOTO );
596 #endif /* for_real */
600 MODULE = Devel::DProf PACKAGE = DB
607 HV *oldstash = curstash;
608 /* SP -= items; added by xsubpp */
609 DBG_SUB_NOTIFY( "XS DBsub(%s)\n", SvPV(Sub, na) );
611 sv_setiv( DBsingle, 0 ); /* disable DB single-stepping */
613 prof_mark( OP_ENTERSUB );
614 PUSHMARK( ORIGMARK );
616 curstash = debstash; /* To disable debugging of perl_call_sv
618 perl_call_sv( Sub, GIMME );
621 prof_mark( OP_LEAVESUB );
623 /* PUTBACK; added by xsubpp */
627 MODULE = Devel::DProf PACKAGE = Devel::DProf
633 /* maybe the process forked--we want only
634 * the parent's profile.
636 if( prof_pid == (int)getpid() ){
637 rprof_end = Times(&prof_end);
638 DBG_TIMER_NOTIFY("Profiler timer is off.\n");
647 /* Before we go anywhere make sure we were invoked
648 * properly, else we'll dump core.
651 croak("DProf: run perl with -d to use DProf.\n");
653 /* When we hook up the XS DB::sub we'll be redefining
654 * the DB::sub from the PM file. Turn off warnings
658 I32 warn_tmp = dowarn;
660 newXS("DB::sub", XS_DB_sub, file);
661 newXS("DB::goto", XS_DB_goto, file);
665 Sub = GvSV(DBsub); /* name of current sub */
666 sv_setiv( DBsingle, 0 ); /* disable DB single-stepping */
669 char *buffer = getenv("PERL_DPROF_BUFFER");
672 SAVE_STACK = atoi(buffer);
675 buffer = getenv("PERL_DPROF_TICKS");
678 dprof_ticks = atoi(buffer); /* Used under OS/2 only */
684 if( (fp = PerlIO_open( "tmon.out", "w" )) == NULL )
685 croak("DProf: unable to write tmon.out, errno = %d\n", errno );
686 #ifdef PERLDBf_NONAME
687 default_perldb = PERLDBf_NONAME | PERLDBf_SUB; /* no name needed. */
689 default_perldb = default_perldb | PERLDBf_GOTO;
694 default_perldb = PERLDBf_SUB; /* debug subroutines only. */
697 prof_pid = (int)getpid();
699 New( 0, profstack, profstack_max, PROFANY );
703 DBG_TIMER_NOTIFY("Profiler timer is on.\n");
704 orealtime = rprof_start = Times(&prof_start);
705 otms_utime = prof_start.tms_utime;
706 otms_stime = prof_start.tms_stime;
707 perldb = default_perldb;