1 #define PERL_NO_GET_CONTEXT
7 /*#define DBG_TIMER 1 */
10 # define DBG_SUB_NOTIFY(A,B) warn(A, B)
12 # define DBG_SUB_NOTIFY(A,B) /* nothing */
16 # define DBG_TIMER_NOTIFY(A) warn(A)
18 # define DBG_TIMER_NOTIFY(A) /* nothing */
21 /* HZ == clock ticks per second */
23 # define HZ ((I32)CLK_TCK)
25 # include <starlet.h> /* prototype for sys$gettim() */
26 # include <lib$routines.h>
27 # define Times(ptr) (dprof_times(aTHX_ ptr))
31 # define HZ ((I32)CLK_TCK)
36 # ifdef OS2 /* times() has significant overhead */
37 # define Times(ptr) (dprof_times(aTHX_ ptr))
38 # define INCL_DOSPROFILE
39 # define INCL_DOSERRORS
41 # define toLongLong(arg) (*(long long*)&(arg))
42 # define DPROF_HZ g_dprof_ticks
44 # define Times(ptr) (times(ptr))
49 XS(XS_Devel__DProf_END); /* used by prof_mark() */
51 /* Everything is built on times(2). See its manpage for a description
56 clock_t tms_utime; /* cpu time spent in user space */
57 clock_t tms_stime; /* cpu time spent in system */
58 clock_t realtime; /* elapsed real time, in ticks */
64 typedef union prof_any PROFANY;
68 char* out_file_name; /* output file (defaults to tmon.out) */
69 PerlIO* fp; /* pointer to tmon.out file */
70 long TIMES_LOCATION; /* Where in the file to store the time totals */
71 int SAVE_STACK; /* How much data to buffer until end of run */
72 int prof_pid; /* pid of profiled process */
73 struct tms prof_start;
75 clock_t rprof_start; /* elapsed real time ticks */
95 #ifdef PERL_IMPLICIT_CONTEXT
102 prof_state_t g_prof_state;
104 #define g_dprof_ticks g_prof_state.dprof_ticks
105 #define g_out_file_name g_prof_state.out_file_name
106 #define g_fp g_prof_state.fp
107 #define g_TIMES_LOCATION g_prof_state.TIMES_LOCATION
108 #define g_SAVE_STACK g_prof_state.SAVE_STACK
109 #define g_prof_pid g_prof_state.prof_pid
110 #define g_prof_start g_prof_state.prof_start
111 #define g_prof_end g_prof_state.prof_end
112 #define g_rprof_start g_prof_state.rprof_start
113 #define g_rprof_end g_prof_state.rprof_end
114 #define g_wprof_u g_prof_state.wprof_u
115 #define g_wprof_s g_prof_state.wprof_s
116 #define g_wprof_r g_prof_state.wprof_r
117 #define g_otms_utime g_prof_state.otms_utime
118 #define g_otms_stime g_prof_state.otms_stime
119 #define g_orealtime g_prof_state.orealtime
120 #define g_profstack g_prof_state.profstack
121 #define g_profstack_max g_prof_state.profstack_max
122 #define g_profstack_ix g_prof_state.profstack_ix
123 #define g_cv_hash g_prof_state.cv_hash
124 #define g_total g_prof_state.total
125 #define g_lastid g_prof_state.lastid
126 #define g_default_perldb g_prof_state.default_perldb
127 #define g_depth g_prof_state.depth
128 #ifdef PERL_IMPLICIT_CONTEXT
129 # define g_THX g_prof_state.aTHX
132 # define g_frequ g_prof_state.frequ
133 # define g_start_cnt g_prof_state.start_cnt
137 dprof_times(pTHX_ struct tms *t)
145 if (CheckOSError(DosTmrQueryFreq(&g_frequ)))
146 croak("DosTmrQueryFreq: %s", SvPV(perl_get_sv("!",TRUE),n_a));
148 g_frequ = g_frequ/DPROF_HZ; /* count per tick */
149 if (CheckOSError(DosTmrQueryTime(&cnt)))
150 croak("DosTmrQueryTime: %s",
151 SvPV(perl_get_sv("!",TRUE), n_a));
152 g_start_cnt = toLongLong(cnt);
155 if (CheckOSError(DosTmrQueryTime(&cnt)))
156 croak("DosTmrQueryTime: %s", SvPV(perl_get_sv("!",TRUE), n_a));
158 return (t->tms_utime = (toLongLong(cnt) - g_start_cnt)/g_frequ);
162 /* Get wall time and convert to 10 ms intervals to
163 * produce the return value dprof expects */
164 # if defined(__DECC) && defined (__ALPHA)
167 _ckvmssts(sys$gettim(&vmstime));
169 retval = vmstime & 0x7fffffff;
171 /* (Older hw or ccs don't have an atomic 64-bit type, so we
172 * juggle 32-bit ints (and a float) to produce a time_t result
173 * with minimal loss of information.) */
174 long int vmstime[2],remainder,divisor = 100000;
175 _ckvmssts(sys$gettim((unsigned long int *)vmstime));
176 vmstime[1] &= 0x7fff; /* prevent overflow in EDIV */
177 _ckvmssts(lib$ediv(&divisor,vmstime,(long int *)&retval,&remainder));
179 /* Fill in the struct tms using the CRTL routine . . .*/
180 times((tbuffer_t *)t);
181 return (clock_t) retval;
182 # else /* !VMS && !OS2 */
189 prof_dumpa(pTHX_ opcode ptype, U32 id)
191 if (ptype == OP_LEAVESUB) {
192 PerlIO_printf(g_fp,"- %"UVxf"\n", (UV)id);
194 else if(ptype == OP_ENTERSUB) {
195 PerlIO_printf(g_fp,"+ %"UVxf"\n", (UV)id);
197 else if(ptype == OP_GOTO) {
198 PerlIO_printf(g_fp,"* %"UVxf"\n", (UV)id);
200 else if(ptype == OP_DIE) {
201 PerlIO_printf(g_fp,"/ %"UVxf"\n", (UV)id);
204 PerlIO_printf(g_fp,"Profiler unknown prof code %d\n", ptype);
209 prof_dumps(pTHX_ U32 id, char *pname, char *gname)
211 PerlIO_printf(g_fp,"& %"UVxf" %s %s\n", (UV)id, pname, gname);
215 prof_dumpt(pTHX_ long tms_utime, long tms_stime, long realtime)
217 PerlIO_printf(g_fp,"@ %ld %ld %ld\n", tms_utime, tms_stime, realtime);
221 prof_dump_until(pTHX_ long ix)
225 clock_t realtime1, realtime2;
227 realtime1 = Times(&t1);
230 opcode ptype = g_profstack[base++].ptype;
231 if (ptype == OP_TIME) {
232 long tms_utime = g_profstack[base++].tms_utime;
233 long tms_stime = g_profstack[base++].tms_stime;
234 long realtime = g_profstack[base++].realtime;
236 prof_dumpt(aTHX_ tms_utime, tms_stime, realtime);
238 else if (ptype == OP_GV) {
239 U32 id = g_profstack[base++].id;
240 char *pname = g_profstack[base++].name;
241 char *gname = g_profstack[base++].name;
243 prof_dumps(aTHX_ id, pname, gname);
246 U32 id = g_profstack[base++].id;
247 prof_dumpa(aTHX_ ptype, id);
251 realtime2 = Times(&t2);
252 if (realtime2 != realtime1 || t1.tms_utime != t2.tms_utime
253 || t1.tms_stime != t2.tms_stime) {
254 g_wprof_r += realtime2 - realtime1;
255 g_wprof_u += t2.tms_utime - t1.tms_utime;
256 g_wprof_s += t2.tms_stime - t1.tms_stime;
258 PerlIO_printf(g_fp,"+ & Devel::DProf::write\n");
259 PerlIO_printf(g_fp,"@ %"IVdf" %"IVdf" %"IVdf"\n",
260 /* The (IV) casts are one possibility:
261 * the Painfully Correct Way would be to
263 (IV)(t2.tms_utime - t1.tms_utime),
264 (IV)(t2.tms_stime - t1.tms_stime),
265 (IV)(realtime2 - realtime1));
266 PerlIO_printf(g_fp,"- & Devel::DProf::write\n");
267 g_otms_utime = t2.tms_utime;
268 g_otms_stime = t2.tms_stime;
269 g_orealtime = realtime2;
275 prof_mark(pTHX_ opcode ptype)
278 clock_t realtime, rdelta, udelta, sdelta;
284 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
287 if (g_profstack_ix + 5 > g_profstack_max) {
288 g_profstack_max = g_profstack_max * 3 / 2;
289 Renew(g_profstack, g_profstack_max, PROFANY);
293 realtime = Times(&t);
294 rdelta = realtime - g_orealtime;
295 udelta = t.tms_utime - g_otms_utime;
296 sdelta = t.tms_stime - g_otms_stime;
297 if (rdelta || udelta || sdelta) {
299 g_profstack[g_profstack_ix++].ptype = OP_TIME;
300 g_profstack[g_profstack_ix++].tms_utime = udelta;
301 g_profstack[g_profstack_ix++].tms_stime = sdelta;
302 g_profstack[g_profstack_ix++].realtime = rdelta;
304 else { /* Write it to disk now so's not to eat up core */
305 if (g_prof_pid == (int)getpid()) {
306 prof_dumpt(aTHX_ udelta, sdelta, rdelta);
310 g_orealtime = realtime;
311 g_otms_stime = t.tms_stime;
312 g_otms_utime = t.tms_utime;
320 cv = INT2PTR(CV*,SvIVX(Sub));
321 svp = hv_fetch(g_cv_hash, (char*)&cv, sizeof(CV*), TRUE);
325 sv_setiv(*svp, id = ++g_lastid);
326 pname = ((GvSTASH(gv) && HvNAME(GvSTASH(gv)))
327 ? HvNAME(GvSTASH(gv))
330 if (CvXSUB(cv) == XS_Devel__DProf_END)
332 if (g_SAVE_STACK) { /* Store it for later recording -JH */
333 g_profstack[g_profstack_ix++].ptype = OP_GV;
334 g_profstack[g_profstack_ix++].id = id;
335 g_profstack[g_profstack_ix++].name = pname;
336 g_profstack[g_profstack_ix++].name = gname;
338 else { /* Write it to disk now so's not to eat up core */
339 /* Only record the parent's info */
340 if (g_prof_pid == (int)getpid()) {
341 prof_dumps(aTHX_ id, pname, gname);
345 PL_perldb = 0; /* Do not debug the kid. */
354 if (g_SAVE_STACK) { /* Store it for later recording -JH */
355 g_profstack[g_profstack_ix++].ptype = ptype;
356 g_profstack[g_profstack_ix++].id = id;
358 /* Only record the parent's info */
359 if (g_SAVE_STACK < g_profstack_ix) {
360 if (g_prof_pid == (int)getpid())
361 prof_dump_until(aTHX_ g_profstack_ix);
363 PL_perldb = 0; /* Do not debug the kid. */
367 else { /* Write it to disk now so's not to eat up core */
369 /* Only record the parent's info */
370 if (g_prof_pid == (int)getpid()) {
371 prof_dumpa(aTHX_ ptype, id);
375 PL_perldb = 0; /* Do not debug the kid. */
380 # define defstash PL_defstash
383 /* Counts overhead of prof_mark and extra XS call. */
385 test_time(pTHX_ clock_t *r, clock_t *u, clock_t *s)
387 CV *cv = perl_get_cv("Devel::DProf::NONESUCH_noxs", FALSE);
389 HV *oldstash = PL_curstash;
391 clock_t realtime1, realtime2;
392 U32 ototal = g_total;
393 U32 ostack = g_SAVE_STACK;
394 U32 operldb = PL_perldb;
396 g_SAVE_STACK = 1000000;
397 realtime1 = Times(&t1);
401 /* Disable debugging of perl_call_sv on second pass: */
402 PL_curstash = (k == 0 ? PL_defstash : PL_debstash);
403 PL_perldb = g_default_perldb;
406 g_profstack_ix = 0; /* Do not let the stack grow */
408 /* prof_mark(aTHX_ OP_ENTERSUB); */
410 PUSHMARK(PL_stack_sp);
411 perl_call_sv((SV*)cv, G_SCALAR);
413 /* prof_mark(aTHX_ OP_LEAVESUB); */
416 PL_curstash = oldstash;
417 if (k == 0) { /* Put time with debugging */
418 realtime2 = Times(&t2);
419 *r = realtime2 - realtime1;
420 *u = t2.tms_utime - t1.tms_utime;
421 *s = t2.tms_stime - t1.tms_stime;
423 else { /* Subtract time without debug */
424 realtime1 = Times(&t1);
425 *r -= realtime1 - realtime2;
426 *u -= t1.tms_utime - t2.tms_utime;
427 *s -= t1.tms_stime - t2.tms_stime;
432 g_SAVE_STACK = ostack;
437 prof_recordheader(pTHX)
441 /* g_fp is opened in the BOOT section */
442 PerlIO_printf(g_fp, "#fOrTyTwO\n");
443 PerlIO_printf(g_fp, "$hz=%"IVdf";\n", (IV)DPROF_HZ);
444 PerlIO_printf(g_fp, "$XS_VERSION='DProf %s';\n", XS_VERSION);
445 PerlIO_printf(g_fp, "# All values are given in HZ\n");
446 test_time(aTHX_ &r, &u, &s);
448 "$over_utime=%"IVdf"; $over_stime=%"IVdf"; $over_rtime=%"IVdf";\n",
449 /* The (IV) casts are one possibility:
450 * the Painfully Correct Way would be to
452 (IV)u, (IV)s, (IV)r);
453 PerlIO_printf(g_fp, "$over_tests=10000;\n");
455 g_TIMES_LOCATION = PerlIO_tell(g_fp);
457 /* Pad with whitespace. */
458 /* This should be enough even for very large numbers. */
459 PerlIO_printf(g_fp, "%*s\n", 240 , "");
461 PerlIO_printf(g_fp, "\n");
462 PerlIO_printf(g_fp, "PART2\n");
470 /* g_fp is opened in the BOOT section */
472 /* Now that we know the runtimes, fill them in at the recorded
478 prof_dump_until(aTHX_ g_profstack_ix);
480 PerlIO_seek(g_fp, g_TIMES_LOCATION, SEEK_SET);
481 /* Write into reserved 240 bytes: */
483 "$rrun_utime=%"IVdf"; $rrun_stime=%"IVdf"; $rrun_rtime=%"IVdf";",
484 /* The (IV) casts are one possibility:
485 * the Painfully Correct Way would be to
487 (IV)(g_prof_end.tms_utime-g_prof_start.tms_utime-g_wprof_u),
488 (IV)(g_prof_end.tms_stime-g_prof_start.tms_stime-g_wprof_s),
489 (IV)(g_rprof_end-g_rprof_start-g_wprof_r));
490 PerlIO_printf(g_fp, "\n$total_marks=%"IVdf, (IV)g_total);
498 check_depth(pTHX_ void *foo)
500 U32 need_depth = PTR2UV(foo);
501 if (need_depth != g_depth) {
502 if (need_depth > g_depth) {
503 warn("garbled call depth when profiling");
506 I32 marks = g_depth - need_depth;
508 /* warn("Check_depth: got %d, expected %d\n", g_depth, need_depth); */
510 prof_mark(aTHX_ OP_DIE);
512 g_depth = need_depth;
524 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
526 #ifdef PERL_IMPLICIT_CONTEXT
527 /* profile only the interpreter that loaded us */
530 perl_call_sv(INT2PTR(SV*,SvIV(Sub)), GIMME | G_NODEBUG);
535 HV *oldstash = PL_curstash;
537 DBG_SUB_NOTIFY("XS DBsub(%s)\n", SvPV_nolen(Sub));
539 SAVEDESTRUCTOR_X(check_depth, (void*)g_depth);
542 prof_mark(aTHX_ OP_ENTERSUB);
544 perl_call_sv(INT2PTR(SV*,SvIV(Sub)), GIMME | G_NODEBUG);
545 prof_mark(aTHX_ OP_LEAVESUB);
553 #ifdef PERL_IMPLICIT_CONTEXT
557 prof_mark(aTHX_ OP_GOTO);
562 #endif /* for_real */
566 MODULE = Devel::DProf PACKAGE = DB
573 HV *oldstash = PL_curstash;
574 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
575 /* SP -= items; added by xsubpp */
576 DBG_SUB_NOTIFY("XS DBsub(%s)\n", SvPV_nolen(Sub));
578 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
580 prof_mark(aTHX_ OP_ENTERSUB);
583 PL_curstash = PL_debstash; /* To disable debugging of perl_call_sv */
584 perl_call_sv(Sub, GIMME);
585 PL_curstash = oldstash;
587 prof_mark(aTHX_ OP_LEAVESUB);
589 /* PUTBACK; added by xsubpp */
594 MODULE = Devel::DProf PACKAGE = Devel::DProf
601 /* maybe the process forked--we want only
602 * the parent's profile.
605 #ifdef PERL_IMPLICIT_CONTEXT
608 g_prof_pid == (int)getpid())
610 g_rprof_end = Times(&g_prof_end);
611 DBG_TIMER_NOTIFY("Profiler timer is off.\n");
622 g_TIMES_LOCATION = 42;
623 g_SAVE_STACK = 1<<14;
624 g_profstack_max = 128;
625 #ifdef PERL_IMPLICIT_CONTEXT
629 /* Before we go anywhere make sure we were invoked
630 * properly, else we'll dump core.
633 croak("DProf: run perl with -d to use DProf.\n");
635 /* When we hook up the XS DB::sub we'll be redefining
636 * the DB::sub from the PM file. Turn off warnings
640 I32 warn_tmp = PL_dowarn;
642 newXS("DB::sub", XS_DB_sub, file);
643 newXS("DB::goto", XS_DB_goto, file);
644 PL_dowarn = warn_tmp;
647 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
650 char *buffer = getenv("PERL_DPROF_BUFFER");
653 g_SAVE_STACK = atoi(buffer);
656 buffer = getenv("PERL_DPROF_TICKS");
659 g_dprof_ticks = atoi(buffer); /* Used under OS/2 only */
665 buffer = getenv("PERL_DPROF_OUT_FILE_NAME");
666 g_out_file_name = savepv(buffer ? buffer : "tmon.out");
669 if ((g_fp = PerlIO_open(g_out_file_name, "w")) == NULL)
670 croak("DProf: unable to write '%s', errno = %d\n",
671 g_out_file_name, errno);
673 g_default_perldb = PERLDBf_NONAME | PERLDBf_SUB | PERLDBf_GOTO;
675 g_prof_pid = (int)getpid();
677 New(0, g_profstack, g_profstack_max, PROFANY);
678 prof_recordheader(aTHX);
679 DBG_TIMER_NOTIFY("Profiler timer is on.\n");
680 g_orealtime = g_rprof_start = Times(&g_prof_start);
681 g_otms_utime = g_prof_start.tms_utime;
682 g_otms_stime = g_prof_start.tms_stime;
683 PL_perldb = g_default_perldb;