057f01e91b0cff9dca3deb1a7b8dd584b7f58158
[p5sagit/p5-mst-13.2.git] / ext / Devel / DProf / DProf.xs
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 /* define DBG_SUB to cause a warning on each subroutine entry. */
7 /*#define DBG_SUB 1      */
8
9 /* define DBG_TIMER to cause a warning when the timer is turned on and off. */
10 /*#define DBG_TIMER 1  */
11
12 #ifdef DEBUGGING
13 #define ASSERT(x) assert(x)
14 #else
15 #define ASSERT(x)
16 #endif
17
18 static CV *
19 db_get_cv(pTHX_ SV *sv)
20 {
21         CV *cv;
22
23         if (SvIOK(sv)) {                        /* if (PERLDB_SUB_NN) { */
24             cv = INT2PTR(CV*,SvIVX(sv));
25         } else {
26             if (SvPOK(sv)) {
27                 cv = get_cv(SvPVX_const(sv), TRUE);
28             } else if (SvROK(sv)) {
29                 cv = (CV*)SvRV(sv);
30             } else {
31                 croak("DProf: don't know what subroutine to profile");
32             }
33         }
34         return cv;
35 }
36
37 #ifdef DBG_SUB
38 #  define DBG_SUB_NOTIFY(A) dprof_dbg_sub_notify(aTHX_ A)
39 void
40 dprof_dbg_sub_notify(pTHX_ SV *Sub) {
41     CV   *cv = db_get_cv(aTHX_ Sub);
42     GV   *gv = cv ? CvGV(cv) : NULL;
43     if (cv && gv) {
44         warn("XS DBsub(%s::%s)\n",
45              ((GvSTASH(gv) && HvNAME_get(GvSTASH(gv))) ?
46               HvNAME_get(GvSTASH(gv)) : "(null)"),
47              GvNAME(gv));
48     } else {
49         warn("XS DBsub(unknown) at %x", Sub);
50     }
51 }
52 #else
53 #  define DBG_SUB_NOTIFY(A)  /* nothing */
54 #endif
55
56
57 #ifdef DBG_TIMER
58 #  define DBG_TIMER_NOTIFY(A) warn(A)
59 #else
60 #  define DBG_TIMER_NOTIFY(A)  /* nothing */
61 #endif
62
63 /* HZ == clock ticks per second */
64 #ifdef VMS
65 #  define HZ ((I32)CLK_TCK)
66 #  define DPROF_HZ HZ
67 #  include <starlet.h>  /* prototype for sys$gettim() */
68 #  include <lib$routines.h>
69 #  define Times(ptr) (dprof_times(aTHX_ ptr))
70 #else
71 #  ifndef HZ
72 #    ifdef CLK_TCK
73 #      define HZ ((I32)CLK_TCK)
74 #    else
75 #      define HZ 60
76 #    endif
77 #  endif
78 #  ifdef OS2                            /* times() has significant overhead */
79 #    define Times(ptr) (dprof_times(aTHX_ ptr))
80 #    define INCL_DOSPROFILE
81 #    define INCL_DOSERRORS
82 #    include <os2.h>
83 #    define toLongLong(arg) (*(long long*)&(arg))
84 #    define DPROF_HZ g_dprof_ticks
85 #  else
86 #    define Times(ptr) (times(ptr))
87 #    define DPROF_HZ HZ
88 #  endif 
89 #endif
90
91 XS(XS_Devel__DProf_END);        /* used by prof_mark() */
92
93 /* Everything is built on times(2).  See its manpage for a description
94  * of the timings.
95  */
96
97 union prof_any {
98         clock_t tms_utime;  /* cpu time spent in user space */
99         clock_t tms_stime;  /* cpu time spent in system */
100         clock_t realtime;   /* elapsed real time, in ticks */
101         char *name;
102         U32 id;
103         opcode ptype;
104 };
105
106 typedef union prof_any PROFANY;
107
108 typedef struct {
109     U32         dprof_ticks;
110     char*       out_file_name;  /* output file (defaults to tmon.out) */
111     PerlIO*     fp;             /* pointer to tmon.out file */
112     Off_t       TIMES_LOCATION; /* Where in the file to store the time totals */
113     int         SAVE_STACK;     /* How much data to buffer until end of run */
114     int         prof_pid;       /* pid of profiled process */
115     struct tms  prof_start;
116     struct tms  prof_end;
117     clock_t     rprof_start;    /* elapsed real time ticks */
118     clock_t     rprof_end;
119     clock_t     wprof_u;
120     clock_t     wprof_s;
121     clock_t     wprof_r;
122     clock_t     otms_utime;
123     clock_t     otms_stime;
124     clock_t     orealtime;
125     PROFANY*    profstack;
126     int         profstack_max;
127     int         profstack_ix;
128     HV*         cv_hash;        /* cache of CV to identifier mappings */
129     SV*         key_hash;       /* key for cv_hash */
130     U32         total;
131     U32         lastid;
132     U32         default_perldb;
133     UV          depth;
134 #ifdef OS2
135     ULONG       frequ;
136     long long   start_cnt;
137 #endif
138 #ifdef PERL_IMPLICIT_CONTEXT
139     PerlInterpreter *my_perl;
140 #endif
141 } prof_state_t;
142
143 prof_state_t g_prof_state;
144
145 #define g_dprof_ticks           g_prof_state.dprof_ticks
146 #define g_out_file_name         g_prof_state.out_file_name
147 #define g_fp                    g_prof_state.fp
148 #define g_TIMES_LOCATION        g_prof_state.TIMES_LOCATION
149 #define g_SAVE_STACK            g_prof_state.SAVE_STACK
150 #define g_prof_pid              g_prof_state.prof_pid
151 #define g_prof_start            g_prof_state.prof_start
152 #define g_prof_end              g_prof_state.prof_end
153 #define g_rprof_start           g_prof_state.rprof_start
154 #define g_rprof_end             g_prof_state.rprof_end
155 #define g_wprof_u               g_prof_state.wprof_u
156 #define g_wprof_s               g_prof_state.wprof_s
157 #define g_wprof_r               g_prof_state.wprof_r
158 #define g_otms_utime            g_prof_state.otms_utime
159 #define g_otms_stime            g_prof_state.otms_stime
160 #define g_orealtime             g_prof_state.orealtime
161 #define g_profstack             g_prof_state.profstack
162 #define g_profstack_max         g_prof_state.profstack_max
163 #define g_profstack_ix          g_prof_state.profstack_ix
164 #define g_cv_hash               g_prof_state.cv_hash
165 #define g_key_hash              g_prof_state.key_hash
166 #define g_total                 g_prof_state.total
167 #define g_lastid                g_prof_state.lastid
168 #define g_default_perldb        g_prof_state.default_perldb
169 #define g_depth                 g_prof_state.depth
170 #ifdef PERL_IMPLICIT_CONTEXT
171 #  define g_THX                 g_prof_state.my_perl
172 #endif
173 #ifdef OS2
174 #  define g_frequ               g_prof_state.frequ
175 #  define g_start_cnt           g_prof_state.start_cnt
176 #endif
177
178 clock_t
179 dprof_times(pTHX_ struct tms *t)
180 {
181 #ifdef OS2
182     ULONG rc;
183     QWORD cnt;
184     STRLEN n_a;
185     
186     if (!g_frequ) {
187         if (CheckOSError(DosTmrQueryFreq(&g_frequ)))
188             croak("DosTmrQueryFreq: %s", SvPV(perl_get_sv("!",TRUE),n_a));
189         else
190             g_frequ = g_frequ/DPROF_HZ; /* count per tick */
191         if (CheckOSError(DosTmrQueryTime(&cnt)))
192             croak("DosTmrQueryTime: %s",
193                   SvPV(perl_get_sv("!",TRUE), n_a));
194         g_start_cnt = toLongLong(cnt);
195     }
196
197     if (CheckOSError(DosTmrQueryTime(&cnt)))
198             croak("DosTmrQueryTime: %s", SvPV(perl_get_sv("!",TRUE), n_a));
199     t->tms_stime = 0;
200     return (t->tms_utime = (toLongLong(cnt) - g_start_cnt)/g_frequ);
201 #else           /* !OS2 */
202 #  ifdef VMS
203     clock_t retval;
204     /* Get wall time and convert to 10 ms intervals to
205      * produce the return value dprof expects */
206 #    if defined(__DECC) && defined (__ALPHA)
207 #      include <ints.h>
208     uint64 vmstime;
209     _ckvmssts(sys$gettim(&vmstime));
210     vmstime /= 100000;
211     retval = vmstime & 0x7fffffff;
212 #    else
213     /* (Older hw or ccs don't have an atomic 64-bit type, so we
214      * juggle 32-bit ints (and a float) to produce a time_t result
215      * with minimal loss of information.) */
216     long int vmstime[2],remainder,divisor = 100000;
217     _ckvmssts(sys$gettim((unsigned long int *)vmstime));
218     vmstime[1] &= 0x7fff;  /* prevent overflow in EDIV */
219     _ckvmssts(lib$ediv(&divisor,vmstime,(long int *)&retval,&remainder));
220 #    endif
221     /* Fill in the struct tms using the CRTL routine . . .*/
222     times((tbuffer_t *)t);
223     return (clock_t) retval;
224 #  else         /* !VMS && !OS2 */
225     return times(t);
226 #  endif
227 #endif
228 }
229
230 static void
231 prof_dumpa(pTHX_ opcode ptype, U32 id)
232 {
233     if (ptype == OP_LEAVESUB) {
234         PerlIO_printf(g_fp,"- %"UVxf"\n", (UV)id);
235     }
236     else if(ptype == OP_ENTERSUB) {
237         PerlIO_printf(g_fp,"+ %"UVxf"\n", (UV)id);
238     }
239     else if(ptype == OP_GOTO) {
240         PerlIO_printf(g_fp,"* %"UVxf"\n", (UV)id);
241     }
242     else if(ptype == OP_DIE) {
243         PerlIO_printf(g_fp,"/ %"UVxf"\n", (UV)id);
244     }
245     else {
246         PerlIO_printf(g_fp,"Profiler unknown prof code %d\n", ptype);
247     }
248 }   
249
250 static void
251 prof_dumps(pTHX_ U32 id, char *pname, char *gname)
252 {
253     PerlIO_printf(g_fp,"& %"UVxf" %s %s\n", (UV)id, pname, gname);
254 }   
255
256 static void
257 prof_dumpt(pTHX_ long tms_utime, long tms_stime, long realtime)
258 {
259     PerlIO_printf(g_fp,"@ %ld %ld %ld\n", tms_utime, tms_stime, realtime);
260 }   
261
262 static void
263 prof_dump_until(pTHX_ long ix)
264 {
265     long base = 0;
266     struct tms t1, t2;
267     clock_t realtime1, realtime2;
268
269     realtime1 = Times(&t1);
270
271     while (base < ix) {
272         opcode ptype = g_profstack[base++].ptype;
273         if (ptype == OP_TIME) {
274             long tms_utime = g_profstack[base++].tms_utime;
275             long tms_stime = g_profstack[base++].tms_stime;
276             long realtime = g_profstack[base++].realtime;
277
278             prof_dumpt(aTHX_ tms_utime, tms_stime, realtime);
279         }
280         else if (ptype == OP_GV) {
281             U32 id = g_profstack[base++].id;
282             char *pname = g_profstack[base++].name;
283             char *gname = g_profstack[base++].name;
284
285             prof_dumps(aTHX_ id, pname, gname);
286         }
287         else {
288             U32 id = g_profstack[base++].id;
289             prof_dumpa(aTHX_ ptype, id);
290         }
291     }
292     PerlIO_flush(g_fp);
293     realtime2 = Times(&t2);
294     if (realtime2 != realtime1 || t1.tms_utime != t2.tms_utime
295         || t1.tms_stime != t2.tms_stime) {
296         g_wprof_r += realtime2 - realtime1;
297         g_wprof_u += t2.tms_utime - t1.tms_utime;
298         g_wprof_s += t2.tms_stime - t1.tms_stime;
299
300         PerlIO_printf(g_fp,"+ & Devel::DProf::write\n");
301         PerlIO_printf(g_fp,"@ %"IVdf" %"IVdf" %"IVdf"\n", 
302                       /* The (IV) casts are one possibility:
303                        * the Painfully Correct Way would be to
304                        * have Clock_t_f. */
305                       (IV)(t2.tms_utime - t1.tms_utime),
306                       (IV)(t2.tms_stime - t1.tms_stime), 
307                       (IV)(realtime2 - realtime1));
308         PerlIO_printf(g_fp,"- & Devel::DProf::write\n");
309         g_otms_utime = t2.tms_utime;
310         g_otms_stime = t2.tms_stime;
311         g_orealtime = realtime2;
312         PerlIO_flush(g_fp);
313     }
314 }
315
316 static void
317 set_cv_key(pTHX_ CV *cv, char *pname, char *gname)
318 {
319         SvGROW(g_key_hash, sizeof(CV**) + strlen(pname) + strlen(gname) + 3);
320         sv_setpvn(g_key_hash, (char*)&cv, sizeof(CV**));
321         sv_catpv(g_key_hash, pname);
322         sv_catpv(g_key_hash, "::");
323         sv_catpv(g_key_hash, gname);
324 }
325
326 static void
327 prof_mark(pTHX_ opcode ptype)
328 {
329     struct tms t;
330     clock_t realtime, rdelta, udelta, sdelta;
331     U32 id;
332     SV *Sub = GvSV(PL_DBsub);   /* name of current sub */
333
334     if (g_SAVE_STACK) {
335         if (g_profstack_ix + 10 > g_profstack_max) {
336                 g_profstack_max = g_profstack_max * 3 / 2;
337                 Renew(g_profstack, g_profstack_max, PROFANY);
338         }
339     }
340
341     realtime = Times(&t);
342     rdelta = realtime - g_orealtime;
343     udelta = t.tms_utime - g_otms_utime;
344     sdelta = t.tms_stime - g_otms_stime;
345     if (rdelta || udelta || sdelta) {
346         if (g_SAVE_STACK) {
347             ASSERT(g_profstack_ix + 4 <= g_profstack_max);
348             g_profstack[g_profstack_ix++].ptype = OP_TIME;
349             g_profstack[g_profstack_ix++].tms_utime = udelta;
350             g_profstack[g_profstack_ix++].tms_stime = sdelta;
351             g_profstack[g_profstack_ix++].realtime = rdelta;
352         }
353         else { /* Write it to disk now so's not to eat up core */
354             if (g_prof_pid == (int)getpid()) {
355                 prof_dumpt(aTHX_ udelta, sdelta, rdelta);
356                 PerlIO_flush(g_fp);
357             }
358         }
359         g_orealtime = realtime;
360         g_otms_stime = t.tms_stime;
361         g_otms_utime = t.tms_utime;
362     }
363
364     {
365         SV **svp;
366         char *gname, *pname;
367         CV *cv;
368         GV *gv;
369
370         cv = db_get_cv(aTHX_ Sub);
371         gv = CvGV(cv);
372         pname = GvSTASH(gv) ? HvNAME_get(GvSTASH(gv)) : 0;
373         pname = pname ? pname : (char *) "(null)";
374         gname = GvNAME(gv);
375
376         set_cv_key(aTHX_ cv, pname, gname);
377         svp = hv_fetch(g_cv_hash, SvPVX_const(g_key_hash), SvCUR(g_key_hash), TRUE);
378         if (!SvOK(*svp)) {
379             sv_setiv(*svp, id = ++g_lastid);
380             if (CvXSUB(cv) == XS_Devel__DProf_END)
381                 return;
382             if (g_SAVE_STACK) { /* Store it for later recording  -JH */
383                 ASSERT(g_profstack_ix + 4 <= g_profstack_max);
384                 g_profstack[g_profstack_ix++].ptype = OP_GV;
385                 g_profstack[g_profstack_ix++].id = id;
386                 g_profstack[g_profstack_ix++].name = pname;
387                 g_profstack[g_profstack_ix++].name = gname;
388             }
389             else { /* Write it to disk now so's not to eat up core */
390                 /* Only record the parent's info */
391                 if (g_prof_pid == (int)getpid()) {
392                     prof_dumps(aTHX_ id, pname, gname);
393                     PerlIO_flush(g_fp);
394                 }
395                 else
396                     PL_perldb = 0;              /* Do not debug the kid. */
397             }
398         }
399         else {
400             id = SvIV(*svp);
401         }
402     }
403
404     g_total++;
405     if (g_SAVE_STACK) { /* Store it for later recording  -JH */
406         ASSERT(g_profstack_ix + 2 <= g_profstack_max);
407         g_profstack[g_profstack_ix++].ptype = ptype;
408         g_profstack[g_profstack_ix++].id = id;
409
410         /* Only record the parent's info */
411         if (g_SAVE_STACK < g_profstack_ix) {
412             if (g_prof_pid == (int)getpid())
413                 prof_dump_until(aTHX_ g_profstack_ix);
414             else
415                 PL_perldb = 0;          /* Do not debug the kid. */
416             g_profstack_ix = 0;
417         }
418     }
419     else { /* Write it to disk now so's not to eat up core */
420
421         /* Only record the parent's info */
422         if (g_prof_pid == (int)getpid()) {
423             prof_dumpa(aTHX_ ptype, id);
424             PerlIO_flush(g_fp);
425         }
426         else
427             PL_perldb = 0;              /* Do not debug the kid. */
428     }
429 }
430
431 #ifdef PL_NEEDED
432 #  define defstash PL_defstash
433 #endif
434
435 /* Counts overhead of prof_mark and extra XS call. */
436 static void
437 test_time(pTHX_ clock_t *r, clock_t *u, clock_t *s)
438 {
439     CV *cv = perl_get_cv("Devel::DProf::NONESUCH_noxs", FALSE);
440     int i, j, k = 0;
441     HV *oldstash = PL_curstash;
442     struct tms t1, t2;
443     clock_t realtime1 = 0, realtime2 = 0;
444     U32 ototal = g_total;
445     U32 ostack = g_SAVE_STACK;
446     U32 operldb = PL_perldb;
447
448     g_SAVE_STACK = 1000000;
449     realtime1 = Times(&t1);
450     
451     while (k < 2) {
452         i = 0;
453             /* Disable debugging of perl_call_sv on second pass: */
454         PL_curstash = (k == 0 ? PL_defstash : PL_debstash);
455         PL_perldb = g_default_perldb;
456         while (++i <= 100) {
457             j = 0;
458             g_profstack_ix = 0;         /* Do not let the stack grow */
459             while (++j <= 100) {
460 /*              prof_mark(aTHX_ OP_ENTERSUB); */
461
462                 PUSHMARK(PL_stack_sp);
463                 perl_call_sv((SV*)cv, G_SCALAR);
464                 PL_stack_sp--;
465 /*              prof_mark(aTHX_ OP_LEAVESUB); */
466             }
467         }
468         PL_curstash = oldstash;
469         if (k == 0) {                   /* Put time with debugging */
470             realtime2 = Times(&t2);
471             *r = realtime2 - realtime1;
472             *u = t2.tms_utime - t1.tms_utime;
473             *s = t2.tms_stime - t1.tms_stime;
474         }
475         else {                          /* Subtract time without debug */
476             realtime1 = Times(&t1);
477             *r -= realtime1 - realtime2;
478             *u -= t1.tms_utime - t2.tms_utime;
479             *s -= t1.tms_stime - t2.tms_stime;      
480         }
481         k++;
482     }
483     g_total = ototal;
484     g_SAVE_STACK = ostack;
485     PL_perldb = operldb;
486 }
487
488 static void
489 prof_recordheader(pTHX)
490 {
491     clock_t r, u, s;
492
493     /* g_fp is opened in the BOOT section */
494     PerlIO_printf(g_fp, "#fOrTyTwO\n");
495     PerlIO_printf(g_fp, "$hz=%"IVdf";\n", (IV)DPROF_HZ);
496     PerlIO_printf(g_fp, "$XS_VERSION='DProf %s';\n", XS_VERSION);
497     PerlIO_printf(g_fp, "# All values are given in HZ\n");
498     test_time(aTHX_ &r, &u, &s);
499     PerlIO_printf(g_fp,
500                   "$over_utime=%"IVdf"; $over_stime=%"IVdf"; $over_rtime=%"IVdf";\n",
501                   /* The (IV) casts are one possibility:
502                    * the Painfully Correct Way would be to
503                    * have Clock_t_f. */
504                   (IV)u, (IV)s, (IV)r);
505     PerlIO_printf(g_fp, "$over_tests=10000;\n");
506
507     g_TIMES_LOCATION = PerlIO_tell(g_fp);
508
509     /* Pad with whitespace. */
510     /* This should be enough even for very large numbers. */
511     PerlIO_printf(g_fp, "%*s\n", 240 , "");
512
513     PerlIO_printf(g_fp, "\n");
514     PerlIO_printf(g_fp, "PART2\n");
515
516     PerlIO_flush(g_fp);
517 }
518
519 static void
520 prof_record(pTHX)
521 {
522     /* g_fp is opened in the BOOT section */
523
524     /* Now that we know the runtimes, fill them in at the recorded
525        location -JH */
526
527     if (g_SAVE_STACK) {
528         prof_dump_until(aTHX_ g_profstack_ix);
529     }
530     PerlIO_seek(g_fp, g_TIMES_LOCATION, SEEK_SET);
531     /* Write into reserved 240 bytes: */
532     PerlIO_printf(g_fp,
533                   "$rrun_utime=%"IVdf"; $rrun_stime=%"IVdf"; $rrun_rtime=%"IVdf";",
534                   /* The (IV) casts are one possibility:
535                    * the Painfully Correct Way would be to
536                    * have Clock_t_f. */
537                   (IV)(g_prof_end.tms_utime-g_prof_start.tms_utime-g_wprof_u),
538                   (IV)(g_prof_end.tms_stime-g_prof_start.tms_stime-g_wprof_s),
539                   (IV)(g_rprof_end-g_rprof_start-g_wprof_r));
540     PerlIO_printf(g_fp, "\n$total_marks=%"IVdf, (IV)g_total);
541     
542     PerlIO_close(g_fp);
543 }
544
545 #define NONESUCH()
546
547 static void
548 check_depth(pTHX_ void *foo)
549 {
550     U32 need_depth = PTR2UV(foo);
551     if (need_depth != g_depth) {
552         if (need_depth > g_depth) {
553             warn("garbled call depth when profiling");
554         }
555         else {
556             IV marks = g_depth - need_depth;
557
558 /*          warn("Check_depth: got %d, expected %d\n", g_depth, need_depth); */
559             while (marks--) {
560                 prof_mark(aTHX_ OP_DIE);
561             }
562             g_depth = need_depth;
563         }
564     }
565 }
566
567 #define for_real
568 #ifdef for_real
569
570 XS(XS_DB_sub)
571 {
572     dMARK;
573     dORIGMARK;
574     SV *Sub = GvSV(PL_DBsub);           /* name of current sub */
575
576 #ifdef PERL_IMPLICIT_CONTEXT
577     /* profile only the interpreter that loaded us */
578     if (g_THX != aTHX) {
579         PUSHMARK(ORIGMARK);
580         perl_call_sv((SV*)db_get_cv(aTHX_ Sub), GIMME_V | G_NODEBUG);
581     }
582     else
583 #endif
584     {
585         HV *oldstash = PL_curstash;
586         I32 old_scopestack_ix = PL_scopestack_ix;
587         I32 old_cxstack_ix = cxstack_ix;
588
589         DBG_SUB_NOTIFY(Sub);
590
591         SAVEDESTRUCTOR_X(check_depth, INT2PTR(void*,g_depth));
592         g_depth++;
593
594         prof_mark(aTHX_ OP_ENTERSUB);
595         PUSHMARK(ORIGMARK);
596         perl_call_sv((SV*)db_get_cv(aTHX_ Sub), GIMME_V | G_NODEBUG);
597         PL_curstash = oldstash;
598
599         /* Make sure we are on the same context and scope as before the call
600          * to the sub. If the called sub was exited via a goto, next or
601          * last then this will try to croak(), however perl may still crash
602          * with a segfault. */
603         if (PL_scopestack_ix != old_scopestack_ix || cxstack_ix != old_cxstack_ix)
604             croak("panic: Devel::DProf inconsistent subroutine return");
605
606         prof_mark(aTHX_ OP_LEAVESUB);
607         g_depth--;
608     }
609     return;
610 }
611
612 XS(XS_DB_goto)
613 {
614 #ifdef PERL_IMPLICIT_CONTEXT
615     if (g_THX == aTHX)
616 #endif
617     {
618         prof_mark(aTHX_ OP_GOTO);
619         return;
620     }
621 }
622
623 #endif /* for_real */
624
625 #ifdef testing
626
627         MODULE = Devel::DProf           PACKAGE = DB
628
629         void
630         sub(...)
631         PPCODE:
632             {
633                 dORIGMARK;
634                 HV *oldstash = PL_curstash;
635                 SV *Sub = GvSV(PL_DBsub);       /* name of current sub */
636                 /* SP -= items;  added by xsubpp */
637                 DBG_SUB_NOTIFY(Sub);
638
639                 sv_setiv(PL_DBsingle, 0);       /* disable DB single-stepping */
640
641                 prof_mark(aTHX_ OP_ENTERSUB);
642                 PUSHMARK(ORIGMARK);
643
644                 PL_curstash = PL_debstash;      /* To disable debugging of perl_call_sv */
645                 perl_call_sv(Sub, GIMME_V);
646                 PL_curstash = oldstash;
647
648                 prof_mark(aTHX_ OP_LEAVESUB);
649                 SPAGAIN;
650                 /* PUTBACK;  added by xsubpp */
651             }
652
653 #endif /* testing */
654
655 MODULE = Devel::DProf           PACKAGE = Devel::DProf
656
657 void
658 END()
659 PPCODE:
660     {
661         if (PL_DBsub) {
662             /* maybe the process forked--we want only
663              * the parent's profile.
664              */
665             if (
666 #ifdef PERL_IMPLICIT_CONTEXT
667                 g_THX == aTHX &&
668 #endif
669                 g_prof_pid == (int)getpid())
670             {
671                 g_rprof_end = Times(&g_prof_end);
672                 DBG_TIMER_NOTIFY("Profiler timer is off.\n");
673                 prof_record(aTHX);
674             }
675         }
676     }
677
678 void
679 NONESUCH()
680
681 BOOT:
682     {
683         g_TIMES_LOCATION = 42;
684         g_SAVE_STACK = 1<<14;
685         g_profstack_max = 128;
686 #ifdef PERL_IMPLICIT_CONTEXT
687         g_THX = aTHX;
688 #endif
689
690         /* Before we go anywhere make sure we were invoked
691          * properly, else we'll dump core.
692          */
693         if (!PL_DBsub)
694             croak("DProf: run perl with -d to use DProf.\n");
695
696         /* When we hook up the XS DB::sub we'll be redefining
697          * the DB::sub from the PM file.  Turn off warnings
698          * while we do this.
699          */
700         {
701             bool warn_tmp = PL_dowarn;
702             PL_dowarn = 0;
703             newXS("DB::sub", XS_DB_sub, file);
704             newXS("DB::goto", XS_DB_goto, file);
705             PL_dowarn = warn_tmp;
706         }
707
708         sv_setiv(PL_DBsingle, 0);       /* disable DB single-stepping */
709
710         {
711             char *buffer = getenv("PERL_DPROF_BUFFER");
712
713             if (buffer) {
714                 g_SAVE_STACK = atoi(buffer);
715             }
716
717             buffer = getenv("PERL_DPROF_TICKS");
718
719             if (buffer) {
720                 g_dprof_ticks = atoi(buffer); /* Used under OS/2 only */
721             }
722             else {
723                 g_dprof_ticks = HZ;
724             }
725
726             buffer = getenv("PERL_DPROF_OUT_FILE_NAME");
727             g_out_file_name = savepv(buffer ? buffer : "tmon.out");
728         }
729
730         if ((g_fp = PerlIO_open(g_out_file_name, "w")) == NULL)
731             croak("DProf: unable to write '%s', errno = %d\n",
732                   g_out_file_name, errno);
733
734         g_default_perldb = PERLDBf_NONAME | PERLDBf_SUB | PERLDBf_GOTO;
735         g_cv_hash = newHV();
736         g_key_hash = newSV(256);
737         g_prof_pid = (int)getpid();
738
739         Newx(g_profstack, g_profstack_max, PROFANY);
740         prof_recordheader(aTHX);
741         DBG_TIMER_NOTIFY("Profiler timer is on.\n");
742         g_orealtime = g_rprof_start = Times(&g_prof_start);
743         g_otms_utime = g_prof_start.tms_utime;
744         g_otms_stime = g_prof_start.tms_stime;
745         PL_perldb = g_default_perldb;
746     }