1 #define PERL_NO_GET_CONTEXT
6 /* define DBG_SUB to cause a warning on each subroutine entry. */
9 /* define DBG_TIMER to cause a warning when the timer is turned on and off. */
10 /*#define DBG_TIMER 1 */
13 #define ASSERT(x) assert(x)
19 db_get_cv(pTHX_ SV *sv)
23 if (SvIOK(sv)) { /* if (PERLDB_SUB_NN) { */
24 cv = INT2PTR(CV*,SvIVX(sv));
27 cv = get_cv(SvPVX_const(sv), TRUE);
28 } else if (SvROK(sv)) {
31 croak("DProf: don't know what subroutine to profile");
38 # define DBG_SUB_NOTIFY(A) dprof_dbg_sub_notify(aTHX_ A)
40 dprof_dbg_sub_notify(pTHX_ SV *Sub) {
41 CV * const cv = db_get_cv(aTHX_ Sub);
42 GV * const gv = cv ? CvGV(cv) : NULL;
44 warn("XS DBsub(%s::%s)\n",
45 ((GvSTASH(gv) && HvNAME_get(GvSTASH(gv))) ?
46 HvNAME_get(GvSTASH(gv)) : "(null)"),
49 warn("XS DBsub(unknown) at %x", Sub);
53 # define DBG_SUB_NOTIFY(A) /* nothing */
58 # define DBG_TIMER_NOTIFY(A) warn(A)
60 # define DBG_TIMER_NOTIFY(A) /* nothing */
63 /* HZ == clock ticks per second */
65 # define HZ ((I32)CLK_TCK)
67 # include <starlet.h> /* prototype for sys$gettim() */
68 # include <lib$routines.h>
69 # define Times(ptr) (dprof_times(aTHX_ ptr))
70 # define NEEDS_DPROF_TIMES
74 # define HZ ((I32)CLK_TCK)
79 # ifdef OS2 /* times() has significant overhead */
80 # define Times(ptr) (dprof_times(aTHX_ ptr))
81 # define NEEDS_DPROF_TIMES
82 # define INCL_DOSPROFILE
83 # define INCL_DOSERRORS
85 # define toLongLong(arg) (*(long long*)&(arg))
86 # define DPROF_HZ g_dprof_ticks
88 # define Times(ptr) (times(ptr))
93 XS(XS_Devel__DProf_END); /* used by prof_mark() */
95 /* Everything is built on times(2). See its manpage for a description
100 clock_t tms_utime; /* cpu time spent in user space */
101 clock_t tms_stime; /* cpu time spent in system */
102 clock_t realtime; /* elapsed real time, in ticks */
108 typedef union prof_any PROFANY;
112 const char* out_file_name; /* output file (defaults to tmon.out) */
113 PerlIO* fp; /* pointer to tmon.out file */
114 Off_t TIMES_LOCATION; /* Where in the file to store the time totals */
115 int SAVE_STACK; /* How much data to buffer until end of run */
116 int prof_pid; /* pid of profiled process */
117 struct tms prof_start;
119 clock_t rprof_start; /* elapsed real time ticks */
130 HV* cv_hash; /* cache of CV to identifier mappings */
131 SV* key_hash; /* key for cv_hash */
140 #ifdef PERL_IMPLICIT_CONTEXT
141 PerlInterpreter *my_perl;
145 prof_state_t g_prof_state;
147 #define g_dprof_ticks g_prof_state.dprof_ticks
148 #define g_out_file_name g_prof_state.out_file_name
149 #define g_fp g_prof_state.fp
150 #define g_TIMES_LOCATION g_prof_state.TIMES_LOCATION
151 #define g_SAVE_STACK g_prof_state.SAVE_STACK
152 #define g_prof_pid g_prof_state.prof_pid
153 #define g_prof_start g_prof_state.prof_start
154 #define g_prof_end g_prof_state.prof_end
155 #define g_rprof_start g_prof_state.rprof_start
156 #define g_rprof_end g_prof_state.rprof_end
157 #define g_wprof_u g_prof_state.wprof_u
158 #define g_wprof_s g_prof_state.wprof_s
159 #define g_wprof_r g_prof_state.wprof_r
160 #define g_otms_utime g_prof_state.otms_utime
161 #define g_otms_stime g_prof_state.otms_stime
162 #define g_orealtime g_prof_state.orealtime
163 #define g_profstack g_prof_state.profstack
164 #define g_profstack_max g_prof_state.profstack_max
165 #define g_profstack_ix g_prof_state.profstack_ix
166 #define g_cv_hash g_prof_state.cv_hash
167 #define g_key_hash g_prof_state.key_hash
168 #define g_total g_prof_state.total
169 #define g_lastid g_prof_state.lastid
170 #define g_default_perldb g_prof_state.default_perldb
171 #define g_depth g_prof_state.depth
172 #ifdef PERL_IMPLICIT_CONTEXT
173 # define g_THX g_prof_state.my_perl
176 # define g_frequ g_prof_state.frequ
177 # define g_start_cnt g_prof_state.start_cnt
180 #ifdef NEEDS_DPROF_TIMES
182 dprof_times(pTHX_ struct tms *t)
189 if (CheckOSError(DosTmrQueryFreq(&g_frequ)))
190 croak("DosTmrQueryFreq: %s", SvPV(perl_get_sv("!",TRUE),n_a));
192 g_frequ = g_frequ/DPROF_HZ; /* count per tick */
193 if (CheckOSError(DosTmrQueryTime(&cnt)))
194 croak("DosTmrQueryTime: %s",
195 SvPV_nolen_const(perl_get_sv("!",TRUE)));
196 g_start_cnt = toLongLong(cnt);
199 if (CheckOSError(DosTmrQueryTime(&cnt)))
200 croak("DosTmrQueryTime: %s", SvPV(perl_get_sv("!",TRUE), n_a));
202 return (t->tms_utime = (toLongLong(cnt) - g_start_cnt)/g_frequ);
206 /* Get wall time and convert to 10 ms intervals to
207 * produce the return value dprof expects */
208 # if defined(__DECC) && defined (__ALPHA)
211 _ckvmssts(sys$gettim(&vmstime));
213 retval = vmstime & 0x7fffffff;
215 /* (Older hw or ccs don't have an atomic 64-bit type, so we
216 * juggle 32-bit ints (and a float) to produce a time_t result
217 * with minimal loss of information.) */
218 long int vmstime[2],remainder,divisor = 100000;
219 _ckvmssts(sys$gettim((unsigned long int *)vmstime));
220 vmstime[1] &= 0x7fff; /* prevent overflow in EDIV */
221 _ckvmssts(lib$ediv(&divisor,vmstime,(long int *)&retval,&remainder));
223 /* Fill in the struct tms using the CRTL routine . . .*/
224 times((tbuffer_t *)t);
225 return (clock_t) retval;
226 # else /* !VMS && !OS2 */
234 prof_dumpa(pTHX_ opcode ptype, U32 id)
236 if (ptype == OP_LEAVESUB) {
237 PerlIO_printf(g_fp,"- %"UVxf"\n", (UV)id);
239 else if(ptype == OP_ENTERSUB) {
240 PerlIO_printf(g_fp,"+ %"UVxf"\n", (UV)id);
242 else if(ptype == OP_GOTO) {
243 PerlIO_printf(g_fp,"* %"UVxf"\n", (UV)id);
245 else if(ptype == OP_DIE) {
246 PerlIO_printf(g_fp,"/ %"UVxf"\n", (UV)id);
249 PerlIO_printf(g_fp,"Profiler unknown prof code %d\n", ptype);
254 prof_dumps(pTHX_ U32 id, const char *pname, const char *gname)
256 PerlIO_printf(g_fp,"& %"UVxf" %s %s\n", (UV)id, pname, gname);
260 prof_dumpt(pTHX_ long tms_utime, long tms_stime, long realtime)
262 PerlIO_printf(g_fp,"@ %ld %ld %ld\n", tms_utime, tms_stime, realtime);
266 prof_dump_until(pTHX_ long ix)
272 const clock_t realtime1 = Times(&t1);
275 const opcode ptype = g_profstack[base++].ptype;
276 if (ptype == OP_TIME) {
277 const long tms_utime = g_profstack[base++].tms_utime;
278 const long tms_stime = g_profstack[base++].tms_stime;
279 const long realtime = g_profstack[base++].realtime;
281 prof_dumpt(aTHX_ tms_utime, tms_stime, realtime);
283 else if (ptype == OP_GV) {
284 const U32 id = g_profstack[base++].id;
285 const char * const pname = g_profstack[base++].name;
286 const char * const gname = g_profstack[base++].name;
288 prof_dumps(aTHX_ id, pname, gname);
291 const U32 id = g_profstack[base++].id;
292 prof_dumpa(aTHX_ ptype, id);
296 realtime2 = Times(&t2);
297 if (realtime2 != realtime1 || t1.tms_utime != t2.tms_utime
298 || t1.tms_stime != t2.tms_stime) {
299 g_wprof_r += realtime2 - realtime1;
300 g_wprof_u += t2.tms_utime - t1.tms_utime;
301 g_wprof_s += t2.tms_stime - t1.tms_stime;
303 PerlIO_printf(g_fp,"+ & Devel::DProf::write\n");
304 PerlIO_printf(g_fp,"@ %"IVdf" %"IVdf" %"IVdf"\n",
305 /* The (IV) casts are one possibility:
306 * the Painfully Correct Way would be to
308 (IV)(t2.tms_utime - t1.tms_utime),
309 (IV)(t2.tms_stime - t1.tms_stime),
310 (IV)(realtime2 - realtime1));
311 PerlIO_printf(g_fp,"- & Devel::DProf::write\n");
312 g_otms_utime = t2.tms_utime;
313 g_otms_stime = t2.tms_stime;
314 g_orealtime = realtime2;
320 set_cv_key(pTHX_ CV *cv, const char *pname, const char *gname)
322 SvGROW(g_key_hash, sizeof(CV**) + strlen(pname) + strlen(gname) + 3);
323 sv_setpvn(g_key_hash, (char*)&cv, sizeof(CV**));
324 sv_catpv(g_key_hash, pname);
325 sv_catpv(g_key_hash, "::");
326 sv_catpv(g_key_hash, gname);
330 prof_mark(pTHX_ opcode ptype)
333 clock_t realtime, rdelta, udelta, sdelta;
335 SV * const Sub = GvSV(PL_DBsub); /* name of current sub */
338 if (g_profstack_ix + 10 > g_profstack_max) {
339 g_profstack_max = g_profstack_max * 3 / 2;
340 Renew(g_profstack, g_profstack_max, PROFANY);
344 realtime = Times(&t);
345 rdelta = realtime - g_orealtime;
346 udelta = t.tms_utime - g_otms_utime;
347 sdelta = t.tms_stime - g_otms_stime;
348 if (rdelta || udelta || sdelta) {
350 ASSERT(g_profstack_ix + 4 <= g_profstack_max);
351 g_profstack[g_profstack_ix++].ptype = OP_TIME;
352 g_profstack[g_profstack_ix++].tms_utime = udelta;
353 g_profstack[g_profstack_ix++].tms_stime = sdelta;
354 g_profstack[g_profstack_ix++].realtime = rdelta;
356 else { /* Write it to disk now so's not to eat up core */
357 if (g_prof_pid == (int)getpid()) {
358 prof_dumpt(aTHX_ udelta, sdelta, rdelta);
362 g_orealtime = realtime;
363 g_otms_stime = t.tms_stime;
364 g_otms_utime = t.tms_utime;
371 CV * const cv = db_get_cv(aTHX_ Sub);
372 GV * const gv = CvGV(cv);
373 pname = GvSTASH(gv) ? HvNAME_get(GvSTASH(gv)) : NULL;
374 pname = pname ? pname : (char *) "(null)";
377 set_cv_key(aTHX_ cv, pname, gname);
378 svp = hv_fetch(g_cv_hash, SvPVX_const(g_key_hash), SvCUR(g_key_hash), TRUE);
380 sv_setiv(*svp, id = ++g_lastid);
381 if (CvXSUB(cv) == XS_Devel__DProf_END)
383 if (g_SAVE_STACK) { /* Store it for later recording -JH */
384 ASSERT(g_profstack_ix + 4 <= g_profstack_max);
385 g_profstack[g_profstack_ix++].ptype = OP_GV;
386 g_profstack[g_profstack_ix++].id = id;
387 g_profstack[g_profstack_ix++].name = pname;
388 g_profstack[g_profstack_ix++].name = gname;
390 else { /* Write it to disk now so's not to eat up core */
391 /* Only record the parent's info */
392 if (g_prof_pid == (int)getpid()) {
393 prof_dumps(aTHX_ id, pname, gname);
397 PL_perldb = 0; /* Do not debug the kid. */
406 if (g_SAVE_STACK) { /* Store it for later recording -JH */
407 ASSERT(g_profstack_ix + 2 <= g_profstack_max);
408 g_profstack[g_profstack_ix++].ptype = ptype;
409 g_profstack[g_profstack_ix++].id = id;
411 /* Only record the parent's info */
412 if (g_SAVE_STACK < g_profstack_ix) {
413 if (g_prof_pid == (int)getpid())
414 prof_dump_until(aTHX_ g_profstack_ix);
416 PL_perldb = 0; /* Do not debug the kid. */
420 else { /* Write it to disk now so's not to eat up core */
422 /* Only record the parent's info */
423 if (g_prof_pid == (int)getpid()) {
424 prof_dumpa(aTHX_ ptype, id);
428 PL_perldb = 0; /* Do not debug the kid. */
433 # define defstash PL_defstash
436 /* Counts overhead of prof_mark and extra XS call. */
438 test_time(pTHX_ clock_t *r, clock_t *u, clock_t *s)
440 CV * const cv = perl_get_cv("Devel::DProf::NONESUCH_noxs", FALSE);
441 HV * const oldstash = PL_curstash;
443 const U32 ototal = g_total;
444 const U32 ostack = g_SAVE_STACK;
445 const U32 operldb = PL_perldb;
448 clock_t realtime1 = Times(&t1);
449 clock_t realtime2 = 0;
451 g_SAVE_STACK = 1000000;
455 /* Disable debugging of perl_call_sv on second pass: */
456 PL_curstash = (k == 0 ? PL_defstash : PL_debstash);
457 PL_perldb = g_default_perldb;
460 g_profstack_ix = 0; /* Do not let the stack grow */
462 /* prof_mark(aTHX_ OP_ENTERSUB); */
464 PUSHMARK(PL_stack_sp);
465 perl_call_sv((SV*)cv, G_SCALAR);
467 /* prof_mark(aTHX_ OP_LEAVESUB); */
470 PL_curstash = oldstash;
471 if (k == 0) { /* Put time with debugging */
472 realtime2 = Times(&t2);
473 *r = realtime2 - realtime1;
474 *u = t2.tms_utime - t1.tms_utime;
475 *s = t2.tms_stime - t1.tms_stime;
477 else { /* Subtract time without debug */
478 realtime1 = Times(&t1);
479 *r -= realtime1 - realtime2;
480 *u -= t1.tms_utime - t2.tms_utime;
481 *s -= t1.tms_stime - t2.tms_stime;
486 g_SAVE_STACK = ostack;
491 prof_recordheader(pTHX)
495 /* g_fp is opened in the BOOT section */
496 PerlIO_printf(g_fp, "#fOrTyTwO\n");
497 PerlIO_printf(g_fp, "$hz=%"IVdf";\n", (IV)DPROF_HZ);
498 PerlIO_printf(g_fp, "$XS_VERSION='DProf %s';\n", XS_VERSION);
499 PerlIO_printf(g_fp, "# All values are given in HZ\n");
500 test_time(aTHX_ &r, &u, &s);
502 "$over_utime=%"IVdf"; $over_stime=%"IVdf"; $over_rtime=%"IVdf";\n",
503 /* The (IV) casts are one possibility:
504 * the Painfully Correct Way would be to
506 (IV)u, (IV)s, (IV)r);
507 PerlIO_printf(g_fp, "$over_tests=10000;\n");
509 g_TIMES_LOCATION = PerlIO_tell(g_fp);
511 /* Pad with whitespace. */
512 /* This should be enough even for very large numbers. */
513 PerlIO_printf(g_fp, "%*s\n", 240 , "");
515 PerlIO_printf(g_fp, "\n");
516 PerlIO_printf(g_fp, "PART2\n");
524 /* g_fp is opened in the BOOT section */
526 /* Now that we know the runtimes, fill them in at the recorded
530 prof_dump_until(aTHX_ g_profstack_ix);
532 PerlIO_seek(g_fp, g_TIMES_LOCATION, SEEK_SET);
533 /* Write into reserved 240 bytes: */
535 "$rrun_utime=%"IVdf"; $rrun_stime=%"IVdf"; $rrun_rtime=%"IVdf";",
536 /* The (IV) casts are one possibility:
537 * the Painfully Correct Way would be to
539 (IV)(g_prof_end.tms_utime-g_prof_start.tms_utime-g_wprof_u),
540 (IV)(g_prof_end.tms_stime-g_prof_start.tms_stime-g_wprof_s),
541 (IV)(g_rprof_end-g_rprof_start-g_wprof_r));
542 PerlIO_printf(g_fp, "\n$total_marks=%"IVdf, (IV)g_total);
550 check_depth(pTHX_ void *foo)
552 const U32 need_depth = PTR2UV(foo);
553 if (need_depth != g_depth) {
554 if (need_depth > g_depth) {
555 warn("garbled call depth when profiling");
558 IV marks = g_depth - need_depth;
560 /* warn("Check_depth: got %d, expected %d\n", g_depth, need_depth); */
562 prof_mark(aTHX_ OP_DIE);
564 g_depth = need_depth;
577 SV * const Sub = GvSV(PL_DBsub); /* name of current sub */
579 #ifdef PERL_IMPLICIT_CONTEXT
580 /* profile only the interpreter that loaded us */
583 perl_call_sv((SV*)db_get_cv(aTHX_ Sub), GIMME_V | G_NODEBUG);
588 HV * const oldstash = PL_curstash;
589 const I32 old_scopestack_ix = PL_scopestack_ix;
590 const I32 old_cxstack_ix = cxstack_ix;
594 SAVEDESTRUCTOR_X(check_depth, INT2PTR(void*,g_depth));
597 prof_mark(aTHX_ OP_ENTERSUB);
599 perl_call_sv((SV*)db_get_cv(aTHX_ Sub), GIMME_V | G_NODEBUG);
600 PL_curstash = oldstash;
602 /* Make sure we are on the same context and scope as before the call
603 * to the sub. If the called sub was exited via a goto, next or
604 * last then this will try to croak(), however perl may still crash
605 * with a segfault. */
606 if (PL_scopestack_ix != old_scopestack_ix || cxstack_ix != old_cxstack_ix)
607 croak("panic: Devel::DProf inconsistent subroutine return");
609 prof_mark(aTHX_ OP_LEAVESUB);
618 #ifdef PERL_IMPLICIT_CONTEXT
622 prof_mark(aTHX_ OP_GOTO);
627 #endif /* for_real */
631 MODULE = Devel::DProf PACKAGE = DB
638 HV * const oldstash = PL_curstash;
639 SV * const Sub = GvSV(PL_DBsub); /* name of current sub */
640 /* SP -= items; added by xsubpp */
643 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
645 prof_mark(aTHX_ OP_ENTERSUB);
648 PL_curstash = PL_debstash; /* To disable debugging of perl_call_sv */
649 perl_call_sv(Sub, GIMME_V);
650 PL_curstash = oldstash;
652 prof_mark(aTHX_ OP_LEAVESUB);
654 /* PUTBACK; added by xsubpp */
659 MODULE = Devel::DProf PACKAGE = Devel::DProf
666 /* maybe the process forked--we want only
667 * the parent's profile.
670 #ifdef PERL_IMPLICIT_CONTEXT
673 g_prof_pid == (int)getpid())
675 g_rprof_end = Times(&g_prof_end);
676 DBG_TIMER_NOTIFY("Profiler timer is off.\n");
687 g_TIMES_LOCATION = 42;
688 g_SAVE_STACK = 1<<14;
689 g_profstack_max = 128;
690 #ifdef PERL_IMPLICIT_CONTEXT
694 /* Before we go anywhere make sure we were invoked
695 * properly, else we'll dump core.
698 croak("DProf: run perl with -d to use DProf.\n");
700 /* When we hook up the XS DB::sub we'll be redefining
701 * the DB::sub from the PM file. Turn off warnings
705 const bool warn_tmp = PL_dowarn;
707 newXS("DB::sub", XS_DB_sub, file);
708 newXS("DB::goto", XS_DB_goto, file);
709 PL_dowarn = warn_tmp;
712 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
715 const char *buffer = getenv("PERL_DPROF_BUFFER");
718 g_SAVE_STACK = atoi(buffer);
721 buffer = getenv("PERL_DPROF_TICKS");
724 g_dprof_ticks = atoi(buffer); /* Used under OS/2 only */
730 buffer = getenv("PERL_DPROF_OUT_FILE_NAME");
731 g_out_file_name = savepv(buffer ? buffer : "tmon.out");
734 if ((g_fp = PerlIO_open(g_out_file_name, "w")) == NULL)
735 croak("DProf: unable to write '%s', errno = %d\n",
736 g_out_file_name, errno);
738 g_default_perldb = PERLDBf_NONAME | PERLDBf_SUB | PERLDBf_GOTO;
740 g_key_hash = newSV(256);
741 g_prof_pid = (int)getpid();
743 Newx(g_profstack, g_profstack_max, PROFANY);
744 prof_recordheader(aTHX);
745 DBG_TIMER_NOTIFY("Profiler timer is on.\n");
746 g_orealtime = g_rprof_start = Times(&g_prof_start);
747 g_otms_utime = g_prof_start.tms_utime;
748 g_otms_stime = g_prof_start.tms_stime;
749 PL_perldb = g_default_perldb;