1 #define PERL_NO_GET_CONTEXT
8 # define dTHR int dummy_thr
11 /*#define DBG_SUB 1 */
12 /*#define DBG_TIMER 1 */
15 # define DBG_SUB_NOTIFY(A,B) warn(A, B)
17 # define DBG_SUB_NOTIFY(A,B) /* nothing */
21 # define DBG_TIMER_NOTIFY(A) warn(A)
23 # define DBG_TIMER_NOTIFY(A) /* nothing */
26 /* HZ == clock ticks per second */
28 # define HZ ((I32)CLK_TCK)
30 # include <starlet.h> /* prototype for sys$gettim() */
31 # define Times(ptr) (dprof_times(aTHX_ ptr))
35 # define HZ ((I32)CLK_TCK)
40 # ifdef OS2 /* times() has significant overhead */
41 # define Times(ptr) (dprof_times(aTHX_ ptr))
42 # define INCL_DOSPROFILE
43 # define INCL_DOSERRORS
45 # define toLongLong(arg) (*(long long*)&(arg))
46 # define DPROF_HZ g_dprof_ticks
48 # define Times(ptr) (times(ptr))
53 XS(XS_Devel__DProf_END); /* used by prof_mark() */
55 /* Everything is built on times(2). See its manpage for a description
60 clock_t tms_utime; /* cpu time spent in user space */
61 clock_t tms_stime; /* cpu time spent in system */
62 clock_t realtime; /* elapsed real time, in ticks */
68 typedef union prof_any PROFANY;
72 char* out_file_name; /* output file (defaults to tmon.out) */
73 PerlIO* fp; /* pointer to tmon.out file */
74 long TIMES_LOCATION; /* Where in the file to store the time totals */
75 int SAVE_STACK; /* How much data to buffer until end of run */
76 int prof_pid; /* pid of profiled process */
77 struct tms prof_start;
79 clock_t rprof_start; /* elapsed real time ticks */
99 #ifdef PERL_IMPLICIT_CONTEXT
106 prof_state_t g_prof_state;
108 #define g_dprof_ticks g_prof_state.dprof_ticks
109 #define g_out_file_name g_prof_state.out_file_name
110 #define g_fp g_prof_state.fp
111 #define g_TIMES_LOCATION g_prof_state.TIMES_LOCATION
112 #define g_SAVE_STACK g_prof_state.SAVE_STACK
113 #define g_prof_pid g_prof_state.prof_pid
114 #define g_prof_start g_prof_state.prof_start
115 #define g_prof_end g_prof_state.prof_end
116 #define g_rprof_start g_prof_state.rprof_start
117 #define g_rprof_end g_prof_state.rprof_end
118 #define g_wprof_u g_prof_state.wprof_u
119 #define g_wprof_s g_prof_state.wprof_s
120 #define g_wprof_r g_prof_state.wprof_r
121 #define g_otms_utime g_prof_state.otms_utime
122 #define g_otms_stime g_prof_state.otms_stime
123 #define g_orealtime g_prof_state.orealtime
124 #define g_profstack g_prof_state.profstack
125 #define g_profstack_max g_prof_state.profstack_max
126 #define g_profstack_ix g_prof_state.profstack_ix
127 #define g_cv_hash g_prof_state.cv_hash
128 #define g_total g_prof_state.total
129 #define g_lastid g_prof_state.lastid
130 #define g_default_perldb g_prof_state.default_perldb
131 #define g_depth g_prof_state.depth
132 #ifdef PERL_IMPLICIT_CONTEXT
133 # define g_THX g_prof_state.aTHX
136 # define g_frequ g_prof_state.frequ
137 # define g_start_cnt g_prof_state.start_cnt
141 dprof_times(pTHX_ struct tms *t)
148 if (CheckOSError(DosTmrQueryFreq(&g_frequ)))
149 croak("DosTmrQueryFreq: %s", SvPV(perl_get_sv("!",TRUE),na));
151 g_frequ = g_frequ/DPROF_HZ; /* count per tick */
152 if (CheckOSError(DosTmrQueryTime(&cnt)))
153 croak("DosTmrQueryTime: %s",
154 SvPV(perl_get_sv("!",TRUE),na));
155 g_start_cnt = toLongLong(cnt);
158 if (CheckOSError(DosTmrQueryTime(&cnt)))
159 croak("DosTmrQueryTime: %s", SvPV(perl_get_sv("!",TRUE),na));
161 return (t->tms_utime = (toLongLong(cnt) - g_start_cnt)/g_frequ);
165 /* Get wall time and convert to 10 ms intervals to
166 * produce the return value dprof expects */
167 # if defined(__DECC) && defined (__ALPHA)
170 _ckvmssts(sys$gettim(&vmstime));
172 retval = vmstime & 0x7fffffff;
174 /* (Older hw or ccs don't have an atomic 64-bit type, so we
175 * juggle 32-bit ints (and a float) to produce a time_t result
176 * with minimal loss of information.) */
177 long int vmstime[2],remainder,divisor = 100000;
178 _ckvmssts(sys$gettim((unsigned long int *)vmstime));
179 vmstime[1] &= 0x7fff; /* prevent overflow in EDIV */
180 _ckvmssts(lib$ediv(&divisor,vmstime,(long int *)&retval,&remainder));
182 /* Fill in the struct tms using the CRTL routine . . .*/
183 times((tbuffer_t *)t);
184 return (clock_t) retval;
185 # else /* !VMS && !OS2 */
192 prof_dumpa(pTHX_ opcode ptype, U32 id)
194 if (ptype == OP_LEAVESUB) {
195 PerlIO_printf(g_fp,"- %"UVxf"\n", (UV)id);
197 else if(ptype == OP_ENTERSUB) {
198 PerlIO_printf(g_fp,"+ %"UVxf"\n", (UV)id);
200 else if(ptype == OP_GOTO) {
201 PerlIO_printf(g_fp,"* %"UVxf"\n", (UV)id);
203 else if(ptype == OP_DIE) {
204 PerlIO_printf(g_fp,"/ %"UVxf"\n", (UV)id);
207 PerlIO_printf(g_fp,"Profiler unknown prof code %d\n", ptype);
212 prof_dumps(pTHX_ U32 id, char *pname, char *gname)
214 PerlIO_printf(g_fp,"& %"UVxf" %s %s\n", (UV)id, pname, gname);
218 prof_dumpt(pTHX_ long tms_utime, long tms_stime, long realtime)
220 PerlIO_printf(g_fp,"@ %ld %ld %ld\n", tms_utime, tms_stime, realtime);
224 prof_dump_until(pTHX_ long ix)
228 clock_t realtime1, realtime2;
230 realtime1 = Times(&t1);
233 opcode ptype = g_profstack[base++].ptype;
234 if (ptype == OP_TIME) {
235 long tms_utime = g_profstack[base++].tms_utime;
236 long tms_stime = g_profstack[base++].tms_stime;
237 long realtime = g_profstack[base++].realtime;
239 prof_dumpt(aTHX_ tms_utime, tms_stime, realtime);
241 else if (ptype == OP_GV) {
242 U32 id = g_profstack[base++].id;
243 char *pname = g_profstack[base++].name;
244 char *gname = g_profstack[base++].name;
246 prof_dumps(aTHX_ id, pname, gname);
249 U32 id = g_profstack[base++].id;
250 prof_dumpa(aTHX_ ptype, id);
254 realtime2 = Times(&t2);
255 if (realtime2 != realtime1 || t1.tms_utime != t2.tms_utime
256 || t1.tms_stime != t2.tms_stime) {
257 g_wprof_r += realtime2 - realtime1;
258 g_wprof_u += t2.tms_utime - t1.tms_utime;
259 g_wprof_s += t2.tms_stime - t1.tms_stime;
261 PerlIO_printf(g_fp,"+ & Devel::DProf::write\n");
262 PerlIO_printf(g_fp,"@ %"IVdf" %"IVdf" %"IVdf"\n",
263 /* The (IV) casts are one possibility:
264 * the Painfully Correct Way would be to
266 (IV)(t2.tms_utime - t1.tms_utime),
267 (IV)(t2.tms_stime - t1.tms_stime),
268 (IV)(realtime2 - realtime1));
269 PerlIO_printf(g_fp,"- & Devel::DProf::write\n");
270 g_otms_utime = t2.tms_utime;
271 g_otms_stime = t2.tms_stime;
272 g_orealtime = realtime2;
278 prof_mark(pTHX_ opcode ptype)
281 clock_t realtime, rdelta, udelta, sdelta;
287 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
290 if (g_profstack_ix + 5 > g_profstack_max) {
291 g_profstack_max = g_profstack_max * 3 / 2;
292 Renew(g_profstack, g_profstack_max, PROFANY);
296 realtime = Times(&t);
297 rdelta = realtime - g_orealtime;
298 udelta = t.tms_utime - g_otms_utime;
299 sdelta = t.tms_stime - g_otms_stime;
300 if (rdelta || udelta || sdelta) {
302 g_profstack[g_profstack_ix++].ptype = OP_TIME;
303 g_profstack[g_profstack_ix++].tms_utime = udelta;
304 g_profstack[g_profstack_ix++].tms_stime = sdelta;
305 g_profstack[g_profstack_ix++].realtime = rdelta;
307 else { /* Write it to disk now so's not to eat up core */
308 if (g_prof_pid == (int)getpid()) {
309 prof_dumpt(aTHX_ udelta, sdelta, rdelta);
313 g_orealtime = realtime;
314 g_otms_stime = t.tms_stime;
315 g_otms_utime = t.tms_utime;
323 cv = INT2PTR(CV*,SvIVX(Sub));
324 svp = hv_fetch(g_cv_hash, (char*)&cv, sizeof(CV*), TRUE);
328 sv_setiv(*svp, id = ++g_lastid);
329 pname = ((GvSTASH(gv) && HvNAME(GvSTASH(gv)))
330 ? HvNAME(GvSTASH(gv))
333 if (CvXSUB(cv) == XS_Devel__DProf_END)
335 if (g_SAVE_STACK) { /* Store it for later recording -JH */
336 g_profstack[g_profstack_ix++].ptype = OP_GV;
337 g_profstack[g_profstack_ix++].id = id;
338 g_profstack[g_profstack_ix++].name = pname;
339 g_profstack[g_profstack_ix++].name = gname;
341 else { /* Write it to disk now so's not to eat up core */
342 /* Only record the parent's info */
343 if (g_prof_pid == (int)getpid()) {
344 prof_dumps(aTHX_ id, pname, gname);
348 PL_perldb = 0; /* Do not debug the kid. */
357 if (g_SAVE_STACK) { /* Store it for later recording -JH */
358 g_profstack[g_profstack_ix++].ptype = ptype;
359 g_profstack[g_profstack_ix++].id = id;
361 /* Only record the parent's info */
362 if (g_SAVE_STACK < g_profstack_ix) {
363 if (g_prof_pid == (int)getpid())
364 prof_dump_until(aTHX_ g_profstack_ix);
366 PL_perldb = 0; /* Do not debug the kid. */
370 else { /* Write it to disk now so's not to eat up core */
372 /* Only record the parent's info */
373 if (g_prof_pid == (int)getpid()) {
374 prof_dumpa(aTHX_ ptype, id);
378 PL_perldb = 0; /* Do not debug the kid. */
383 # define defstash PL_defstash
386 /* Counts overhead of prof_mark and extra XS call. */
388 test_time(pTHX_ clock_t *r, clock_t *u, clock_t *s)
391 CV *cv = perl_get_cv("Devel::DProf::NONESUCH_noxs", FALSE);
393 HV *oldstash = PL_curstash;
395 clock_t realtime1, realtime2;
396 U32 ototal = g_total;
397 U32 ostack = g_SAVE_STACK;
398 U32 operldb = PL_perldb;
400 g_SAVE_STACK = 1000000;
401 realtime1 = Times(&t1);
405 /* Disable debugging of perl_call_sv on second pass: */
406 PL_curstash = (k == 0 ? PL_defstash : PL_debstash);
407 PL_perldb = g_default_perldb;
410 g_profstack_ix = 0; /* Do not let the stack grow */
412 /* prof_mark(aTHX_ OP_ENTERSUB); */
414 PUSHMARK(PL_stack_sp);
415 perl_call_sv((SV*)cv, G_SCALAR);
417 /* prof_mark(aTHX_ OP_LEAVESUB); */
420 PL_curstash = oldstash;
421 if (k == 0) { /* Put time with debugging */
422 realtime2 = Times(&t2);
423 *r = realtime2 - realtime1;
424 *u = t2.tms_utime - t1.tms_utime;
425 *s = t2.tms_stime - t1.tms_stime;
427 else { /* Subtract time without debug */
428 realtime1 = Times(&t1);
429 *r -= realtime1 - realtime2;
430 *u -= t1.tms_utime - t2.tms_utime;
431 *s -= t1.tms_stime - t2.tms_stime;
436 g_SAVE_STACK = ostack;
441 prof_recordheader(pTHX)
445 /* g_fp is opened in the BOOT section */
446 PerlIO_printf(g_fp, "#fOrTyTwO\n");
447 PerlIO_printf(g_fp, "$hz=%"IVdf";\n", (IV)DPROF_HZ);
448 PerlIO_printf(g_fp, "$XS_VERSION='DProf %s';\n", XS_VERSION);
449 PerlIO_printf(g_fp, "# All values are given in HZ\n");
450 test_time(aTHX_ &r, &u, &s);
452 "$over_utime=%"IVdf"; $over_stime=%"IVdf"; $over_rtime=%"IVdf";\n",
453 /* The (IV) casts are one possibility:
454 * the Painfully Correct Way would be to
456 (IV)u, (IV)s, (IV)r);
457 PerlIO_printf(g_fp, "$over_tests=10000;\n");
459 g_TIMES_LOCATION = PerlIO_tell(g_fp);
461 /* Pad with whitespace. */
462 /* This should be enough even for very large numbers. */
463 PerlIO_printf(g_fp, "%*s\n", 240 , "");
465 PerlIO_printf(g_fp, "\n");
466 PerlIO_printf(g_fp, "PART2\n");
474 /* g_fp is opened in the BOOT section */
476 /* Now that we know the runtimes, fill them in at the recorded
482 prof_dump_until(aTHX_ g_profstack_ix);
484 PerlIO_seek(g_fp, g_TIMES_LOCATION, SEEK_SET);
485 /* Write into reserved 240 bytes: */
487 "$rrun_utime=%"IVdf"; $rrun_stime=%"IVdf"; $rrun_rtime=%"IVdf";",
488 /* The (IV) casts are one possibility:
489 * the Painfully Correct Way would be to
491 (IV)(g_prof_end.tms_utime-g_prof_start.tms_utime-g_wprof_u),
492 (IV)(g_prof_end.tms_stime-g_prof_start.tms_stime-g_wprof_s),
493 (IV)(g_rprof_end-g_rprof_start-g_wprof_r));
494 PerlIO_printf(g_fp, "\n$total_marks=%"IVdf, (IV)g_total);
502 check_depth(pTHX_ void *foo)
504 U32 need_depth = (U32)foo;
505 if (need_depth != g_depth) {
506 if (need_depth > g_depth) {
507 warn("garbled call depth when profiling");
510 I32 marks = g_depth - need_depth;
512 /* warn("Check_depth: got %d, expected %d\n", g_depth, need_depth); */
514 prof_mark(aTHX_ OP_DIE);
516 g_depth = need_depth;
528 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
530 #ifdef PERL_IMPLICIT_CONTEXT
531 /* profile only the interpreter that loaded us */
534 perl_call_sv(INT2PTR(SV*,SvIV(Sub)), GIMME | G_NODEBUG);
539 HV *oldstash = PL_curstash;
541 DBG_SUB_NOTIFY("XS DBsub(%s)\n", SvPV(Sub, na));
543 SAVEDESTRUCTOR_X(check_depth, (void*)g_depth);
546 prof_mark(aTHX_ OP_ENTERSUB);
548 perl_call_sv(INT2PTR(SV*,SvIV(Sub)), GIMME | G_NODEBUG);
549 prof_mark(aTHX_ OP_LEAVESUB);
557 #ifdef PERL_IMPLICIT_CONTEXT
561 prof_mark(aTHX_ OP_GOTO);
566 #endif /* for_real */
570 MODULE = Devel::DProf PACKAGE = DB
577 HV *oldstash = PL_curstash;
578 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
579 /* SP -= items; added by xsubpp */
580 DBG_SUB_NOTIFY("XS DBsub(%s)\n", SvPV(Sub, na));
582 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
584 prof_mark(aTHX_ OP_ENTERSUB);
587 PL_curstash = PL_debstash; /* To disable debugging of perl_call_sv */
588 perl_call_sv(Sub, GIMME);
589 PL_curstash = oldstash;
591 prof_mark(aTHX_ OP_LEAVESUB);
593 /* PUTBACK; added by xsubpp */
598 MODULE = Devel::DProf PACKAGE = Devel::DProf
605 /* maybe the process forked--we want only
606 * the parent's profile.
609 #ifdef PERL_IMPLICIT_CONTEXT
612 g_prof_pid == (int)getpid())
614 g_rprof_end = Times(&g_prof_end);
615 DBG_TIMER_NOTIFY("Profiler timer is off.\n");
626 g_TIMES_LOCATION = 42;
627 g_SAVE_STACK = 1<<14;
628 g_profstack_max = 128;
629 #ifdef PERL_IMPLICIT_CONTEXT
633 /* Before we go anywhere make sure we were invoked
634 * properly, else we'll dump core.
637 croak("DProf: run perl with -d to use DProf.\n");
639 /* When we hook up the XS DB::sub we'll be redefining
640 * the DB::sub from the PM file. Turn off warnings
644 I32 warn_tmp = PL_dowarn;
646 newXS("DB::sub", XS_DB_sub, file);
647 newXS("DB::goto", XS_DB_goto, file);
648 PL_dowarn = warn_tmp;
651 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
654 char *buffer = getenv("PERL_DPROF_BUFFER");
657 g_SAVE_STACK = atoi(buffer);
660 buffer = getenv("PERL_DPROF_TICKS");
663 g_dprof_ticks = atoi(buffer); /* Used under OS/2 only */
669 buffer = getenv("PERL_DPROF_OUT_FILE_NAME");
670 g_out_file_name = savepv(buffer ? buffer : "tmon.out");
673 if ((g_fp = PerlIO_open(g_out_file_name, "w")) == NULL)
674 croak("DProf: unable to write '%s', errno = %d\n",
675 g_out_file_name, errno);
677 g_default_perldb = PERLDBf_NONAME | PERLDBf_SUB | PERLDBf_GOTO;
679 g_prof_pid = (int)getpid();
681 New(0, g_profstack, g_profstack_max, PROFANY);
682 prof_recordheader(aTHX);
683 DBG_TIMER_NOTIFY("Profiler timer is on.\n");
684 g_orealtime = g_rprof_start = Times(&g_prof_start);
685 g_otms_utime = g_prof_start.tms_utime;
686 g_otms_stime = g_prof_start.tms_stime;
687 PL_perldb = g_default_perldb;