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)
149 if (CheckOSError(DosTmrQueryFreq(&g_frequ)))
150 croak("DosTmrQueryFreq: %s", SvPV(perl_get_sv("!",TRUE),n_a));
152 g_frequ = g_frequ/DPROF_HZ; /* count per tick */
153 if (CheckOSError(DosTmrQueryTime(&cnt)))
154 croak("DosTmrQueryTime: %s",
155 SvPV(perl_get_sv("!",TRUE), n_a));
156 g_start_cnt = toLongLong(cnt);
159 if (CheckOSError(DosTmrQueryTime(&cnt)))
160 croak("DosTmrQueryTime: %s", SvPV(perl_get_sv("!",TRUE), n_a));
162 return (t->tms_utime = (toLongLong(cnt) - g_start_cnt)/g_frequ);
166 /* Get wall time and convert to 10 ms intervals to
167 * produce the return value dprof expects */
168 # if defined(__DECC) && defined (__ALPHA)
171 _ckvmssts(sys$gettim(&vmstime));
173 retval = vmstime & 0x7fffffff;
175 /* (Older hw or ccs don't have an atomic 64-bit type, so we
176 * juggle 32-bit ints (and a float) to produce a time_t result
177 * with minimal loss of information.) */
178 long int vmstime[2],remainder,divisor = 100000;
179 _ckvmssts(sys$gettim((unsigned long int *)vmstime));
180 vmstime[1] &= 0x7fff; /* prevent overflow in EDIV */
181 _ckvmssts(lib$ediv(&divisor,vmstime,(long int *)&retval,&remainder));
183 /* Fill in the struct tms using the CRTL routine . . .*/
184 times((tbuffer_t *)t);
185 return (clock_t) retval;
186 # else /* !VMS && !OS2 */
193 prof_dumpa(pTHX_ opcode ptype, U32 id)
195 if (ptype == OP_LEAVESUB) {
196 PerlIO_printf(g_fp,"- %"UVxf"\n", (UV)id);
198 else if(ptype == OP_ENTERSUB) {
199 PerlIO_printf(g_fp,"+ %"UVxf"\n", (UV)id);
201 else if(ptype == OP_GOTO) {
202 PerlIO_printf(g_fp,"* %"UVxf"\n", (UV)id);
204 else if(ptype == OP_DIE) {
205 PerlIO_printf(g_fp,"/ %"UVxf"\n", (UV)id);
208 PerlIO_printf(g_fp,"Profiler unknown prof code %d\n", ptype);
213 prof_dumps(pTHX_ U32 id, char *pname, char *gname)
215 PerlIO_printf(g_fp,"& %"UVxf" %s %s\n", (UV)id, pname, gname);
219 prof_dumpt(pTHX_ long tms_utime, long tms_stime, long realtime)
221 PerlIO_printf(g_fp,"@ %ld %ld %ld\n", tms_utime, tms_stime, realtime);
225 prof_dump_until(pTHX_ long ix)
229 clock_t realtime1, realtime2;
231 realtime1 = Times(&t1);
234 opcode ptype = g_profstack[base++].ptype;
235 if (ptype == OP_TIME) {
236 long tms_utime = g_profstack[base++].tms_utime;
237 long tms_stime = g_profstack[base++].tms_stime;
238 long realtime = g_profstack[base++].realtime;
240 prof_dumpt(aTHX_ tms_utime, tms_stime, realtime);
242 else if (ptype == OP_GV) {
243 U32 id = g_profstack[base++].id;
244 char *pname = g_profstack[base++].name;
245 char *gname = g_profstack[base++].name;
247 prof_dumps(aTHX_ id, pname, gname);
250 U32 id = g_profstack[base++].id;
251 prof_dumpa(aTHX_ ptype, id);
255 realtime2 = Times(&t2);
256 if (realtime2 != realtime1 || t1.tms_utime != t2.tms_utime
257 || t1.tms_stime != t2.tms_stime) {
258 g_wprof_r += realtime2 - realtime1;
259 g_wprof_u += t2.tms_utime - t1.tms_utime;
260 g_wprof_s += t2.tms_stime - t1.tms_stime;
262 PerlIO_printf(g_fp,"+ & Devel::DProf::write\n");
263 PerlIO_printf(g_fp,"@ %"IVdf" %"IVdf" %"IVdf"\n",
264 /* The (IV) casts are one possibility:
265 * the Painfully Correct Way would be to
267 (IV)(t2.tms_utime - t1.tms_utime),
268 (IV)(t2.tms_stime - t1.tms_stime),
269 (IV)(realtime2 - realtime1));
270 PerlIO_printf(g_fp,"- & Devel::DProf::write\n");
271 g_otms_utime = t2.tms_utime;
272 g_otms_stime = t2.tms_stime;
273 g_orealtime = realtime2;
279 prof_mark(pTHX_ opcode ptype)
282 clock_t realtime, rdelta, udelta, sdelta;
288 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
291 if (g_profstack_ix + 5 > g_profstack_max) {
292 g_profstack_max = g_profstack_max * 3 / 2;
293 Renew(g_profstack, g_profstack_max, PROFANY);
297 realtime = Times(&t);
298 rdelta = realtime - g_orealtime;
299 udelta = t.tms_utime - g_otms_utime;
300 sdelta = t.tms_stime - g_otms_stime;
301 if (rdelta || udelta || sdelta) {
303 g_profstack[g_profstack_ix++].ptype = OP_TIME;
304 g_profstack[g_profstack_ix++].tms_utime = udelta;
305 g_profstack[g_profstack_ix++].tms_stime = sdelta;
306 g_profstack[g_profstack_ix++].realtime = rdelta;
308 else { /* Write it to disk now so's not to eat up core */
309 if (g_prof_pid == (int)getpid()) {
310 prof_dumpt(aTHX_ udelta, sdelta, rdelta);
314 g_orealtime = realtime;
315 g_otms_stime = t.tms_stime;
316 g_otms_utime = t.tms_utime;
324 cv = INT2PTR(CV*,SvIVX(Sub));
325 svp = hv_fetch(g_cv_hash, (char*)&cv, sizeof(CV*), TRUE);
329 sv_setiv(*svp, id = ++g_lastid);
330 pname = ((GvSTASH(gv) && HvNAME(GvSTASH(gv)))
331 ? HvNAME(GvSTASH(gv))
334 if (CvXSUB(cv) == XS_Devel__DProf_END)
336 if (g_SAVE_STACK) { /* Store it for later recording -JH */
337 g_profstack[g_profstack_ix++].ptype = OP_GV;
338 g_profstack[g_profstack_ix++].id = id;
339 g_profstack[g_profstack_ix++].name = pname;
340 g_profstack[g_profstack_ix++].name = gname;
342 else { /* Write it to disk now so's not to eat up core */
343 /* Only record the parent's info */
344 if (g_prof_pid == (int)getpid()) {
345 prof_dumps(aTHX_ id, pname, gname);
349 PL_perldb = 0; /* Do not debug the kid. */
358 if (g_SAVE_STACK) { /* Store it for later recording -JH */
359 g_profstack[g_profstack_ix++].ptype = ptype;
360 g_profstack[g_profstack_ix++].id = id;
362 /* Only record the parent's info */
363 if (g_SAVE_STACK < g_profstack_ix) {
364 if (g_prof_pid == (int)getpid())
365 prof_dump_until(aTHX_ g_profstack_ix);
367 PL_perldb = 0; /* Do not debug the kid. */
371 else { /* Write it to disk now so's not to eat up core */
373 /* Only record the parent's info */
374 if (g_prof_pid == (int)getpid()) {
375 prof_dumpa(aTHX_ ptype, id);
379 PL_perldb = 0; /* Do not debug the kid. */
384 # define defstash PL_defstash
387 /* Counts overhead of prof_mark and extra XS call. */
389 test_time(pTHX_ clock_t *r, clock_t *u, clock_t *s)
392 CV *cv = perl_get_cv("Devel::DProf::NONESUCH_noxs", FALSE);
394 HV *oldstash = PL_curstash;
396 clock_t realtime1, realtime2;
397 U32 ototal = g_total;
398 U32 ostack = g_SAVE_STACK;
399 U32 operldb = PL_perldb;
401 g_SAVE_STACK = 1000000;
402 realtime1 = Times(&t1);
406 /* Disable debugging of perl_call_sv on second pass: */
407 PL_curstash = (k == 0 ? PL_defstash : PL_debstash);
408 PL_perldb = g_default_perldb;
411 g_profstack_ix = 0; /* Do not let the stack grow */
413 /* prof_mark(aTHX_ OP_ENTERSUB); */
415 PUSHMARK(PL_stack_sp);
416 perl_call_sv((SV*)cv, G_SCALAR);
418 /* prof_mark(aTHX_ OP_LEAVESUB); */
421 PL_curstash = oldstash;
422 if (k == 0) { /* Put time with debugging */
423 realtime2 = Times(&t2);
424 *r = realtime2 - realtime1;
425 *u = t2.tms_utime - t1.tms_utime;
426 *s = t2.tms_stime - t1.tms_stime;
428 else { /* Subtract time without debug */
429 realtime1 = Times(&t1);
430 *r -= realtime1 - realtime2;
431 *u -= t1.tms_utime - t2.tms_utime;
432 *s -= t1.tms_stime - t2.tms_stime;
437 g_SAVE_STACK = ostack;
442 prof_recordheader(pTHX)
446 /* g_fp is opened in the BOOT section */
447 PerlIO_printf(g_fp, "#fOrTyTwO\n");
448 PerlIO_printf(g_fp, "$hz=%"IVdf";\n", (IV)DPROF_HZ);
449 PerlIO_printf(g_fp, "$XS_VERSION='DProf %s';\n", XS_VERSION);
450 PerlIO_printf(g_fp, "# All values are given in HZ\n");
451 test_time(aTHX_ &r, &u, &s);
453 "$over_utime=%"IVdf"; $over_stime=%"IVdf"; $over_rtime=%"IVdf";\n",
454 /* The (IV) casts are one possibility:
455 * the Painfully Correct Way would be to
457 (IV)u, (IV)s, (IV)r);
458 PerlIO_printf(g_fp, "$over_tests=10000;\n");
460 g_TIMES_LOCATION = PerlIO_tell(g_fp);
462 /* Pad with whitespace. */
463 /* This should be enough even for very large numbers. */
464 PerlIO_printf(g_fp, "%*s\n", 240 , "");
466 PerlIO_printf(g_fp, "\n");
467 PerlIO_printf(g_fp, "PART2\n");
475 /* g_fp is opened in the BOOT section */
477 /* Now that we know the runtimes, fill them in at the recorded
483 prof_dump_until(aTHX_ g_profstack_ix);
485 PerlIO_seek(g_fp, g_TIMES_LOCATION, SEEK_SET);
486 /* Write into reserved 240 bytes: */
488 "$rrun_utime=%"IVdf"; $rrun_stime=%"IVdf"; $rrun_rtime=%"IVdf";",
489 /* The (IV) casts are one possibility:
490 * the Painfully Correct Way would be to
492 (IV)(g_prof_end.tms_utime-g_prof_start.tms_utime-g_wprof_u),
493 (IV)(g_prof_end.tms_stime-g_prof_start.tms_stime-g_wprof_s),
494 (IV)(g_rprof_end-g_rprof_start-g_wprof_r));
495 PerlIO_printf(g_fp, "\n$total_marks=%"IVdf, (IV)g_total);
503 check_depth(pTHX_ void *foo)
505 U32 need_depth = PTR2UV(foo);
506 if (need_depth != g_depth) {
507 if (need_depth > g_depth) {
508 warn("garbled call depth when profiling");
511 I32 marks = g_depth - need_depth;
513 /* warn("Check_depth: got %d, expected %d\n", g_depth, need_depth); */
515 prof_mark(aTHX_ OP_DIE);
517 g_depth = need_depth;
529 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
531 #ifdef PERL_IMPLICIT_CONTEXT
532 /* profile only the interpreter that loaded us */
535 perl_call_sv(INT2PTR(SV*,SvIV(Sub)), GIMME | G_NODEBUG);
540 HV *oldstash = PL_curstash;
542 DBG_SUB_NOTIFY("XS DBsub(%s)\n", SvPV_nolen(Sub));
544 SAVEDESTRUCTOR_X(check_depth, (void*)g_depth);
547 prof_mark(aTHX_ OP_ENTERSUB);
549 perl_call_sv(INT2PTR(SV*,SvIV(Sub)), GIMME | G_NODEBUG);
550 prof_mark(aTHX_ OP_LEAVESUB);
558 #ifdef PERL_IMPLICIT_CONTEXT
562 prof_mark(aTHX_ OP_GOTO);
567 #endif /* for_real */
571 MODULE = Devel::DProf PACKAGE = DB
578 HV *oldstash = PL_curstash;
579 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
580 /* SP -= items; added by xsubpp */
581 DBG_SUB_NOTIFY("XS DBsub(%s)\n", SvPV_nolen(Sub));
583 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
585 prof_mark(aTHX_ OP_ENTERSUB);
588 PL_curstash = PL_debstash; /* To disable debugging of perl_call_sv */
589 perl_call_sv(Sub, GIMME);
590 PL_curstash = oldstash;
592 prof_mark(aTHX_ OP_LEAVESUB);
594 /* PUTBACK; added by xsubpp */
599 MODULE = Devel::DProf PACKAGE = Devel::DProf
606 /* maybe the process forked--we want only
607 * the parent's profile.
610 #ifdef PERL_IMPLICIT_CONTEXT
613 g_prof_pid == (int)getpid())
615 g_rprof_end = Times(&g_prof_end);
616 DBG_TIMER_NOTIFY("Profiler timer is off.\n");
627 g_TIMES_LOCATION = 42;
628 g_SAVE_STACK = 1<<14;
629 g_profstack_max = 128;
630 #ifdef PERL_IMPLICIT_CONTEXT
634 /* Before we go anywhere make sure we were invoked
635 * properly, else we'll dump core.
638 croak("DProf: run perl with -d to use DProf.\n");
640 /* When we hook up the XS DB::sub we'll be redefining
641 * the DB::sub from the PM file. Turn off warnings
645 I32 warn_tmp = PL_dowarn;
647 newXS("DB::sub", XS_DB_sub, file);
648 newXS("DB::goto", XS_DB_goto, file);
649 PL_dowarn = warn_tmp;
652 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
655 char *buffer = getenv("PERL_DPROF_BUFFER");
658 g_SAVE_STACK = atoi(buffer);
661 buffer = getenv("PERL_DPROF_TICKS");
664 g_dprof_ticks = atoi(buffer); /* Used under OS/2 only */
670 buffer = getenv("PERL_DPROF_OUT_FILE_NAME");
671 g_out_file_name = savepv(buffer ? buffer : "tmon.out");
674 if ((g_fp = PerlIO_open(g_out_file_name, "w")) == NULL)
675 croak("DProf: unable to write '%s', errno = %d\n",
676 g_out_file_name, errno);
678 g_default_perldb = PERLDBf_NONAME | PERLDBf_SUB | PERLDBf_GOTO;
680 g_prof_pid = (int)getpid();
682 New(0, g_profstack, g_profstack_max, PROFANY);
683 prof_recordheader(aTHX);
684 DBG_TIMER_NOTIFY("Profiler timer is on.\n");
685 g_orealtime = g_rprof_start = Times(&g_prof_start);
686 g_otms_utime = g_prof_start.tms_utime;
687 g_otms_stime = g_prof_start.tms_stime;
688 PL_perldb = g_default_perldb;