d59c9dfe119ae69315a4646788665718ff92c9cc
[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 /* For older Perls */
7 #ifndef dTHR
8 #  define dTHR int dummy_thr
9 #endif  /* dTHR */ 
10
11 /*#define DBG_SUB 1      */
12 /*#define DBG_TIMER 1    */
13
14 #ifdef DBG_SUB
15 #  define DBG_SUB_NOTIFY(A,B) warn(A, B)
16 #else
17 #  define DBG_SUB_NOTIFY(A,B)  /* nothing */
18 #endif
19
20 #ifdef DBG_TIMER
21 #  define DBG_TIMER_NOTIFY(A) warn(A)
22 #else
23 #  define DBG_TIMER_NOTIFY(A)  /* nothing */
24 #endif
25
26 /* HZ == clock ticks per second */
27 #ifdef VMS
28 #  define HZ ((I32)CLK_TCK)
29 #  define DPROF_HZ HZ
30 #  include <starlet.h>  /* prototype for sys$gettim() */
31 #  define Times(ptr) (dprof_times(aTHX_ ptr))
32 #else
33 #  ifndef HZ
34 #    ifdef CLK_TCK
35 #      define HZ ((I32)CLK_TCK)
36 #    else
37 #      define HZ 60
38 #    endif
39 #  endif
40 #  ifdef OS2                            /* times() has significant overhead */
41 #    define Times(ptr) (dprof_times(aTHX_ ptr))
42 #    define INCL_DOSPROFILE
43 #    define INCL_DOSERRORS
44 #    include <os2.h>
45 #    define toLongLong(arg) (*(long long*)&(arg))
46 #    define DPROF_HZ g_dprof_ticks
47 #  else
48 #    define Times(ptr) (times(ptr))
49 #    define DPROF_HZ HZ
50 #  endif 
51 #endif
52
53 XS(XS_Devel__DProf_END);        /* used by prof_mark() */
54
55 /* Everything is built on times(2).  See its manpage for a description
56  * of the timings.
57  */
58
59 union prof_any {
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 */
63         char *name;
64         U32 id;
65         opcode ptype;
66 };
67
68 typedef union prof_any PROFANY;
69
70 typedef struct {
71     U32         dprof_ticks;
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;
78     struct tms  prof_end;
79     clock_t     rprof_start;    /* elapsed real time ticks */
80     clock_t     rprof_end;
81     clock_t     wprof_u;
82     clock_t     wprof_s;
83     clock_t     wprof_r;
84     clock_t     otms_utime;
85     clock_t     otms_stime;
86     clock_t     orealtime;
87     PROFANY*    profstack;
88     int         profstack_max;
89     int         profstack_ix;
90     HV*         cv_hash;
91     U32         total;
92     U32         lastid;
93     U32         default_perldb;
94     U32         depth;
95 #ifdef OS2
96     ULONG       frequ;
97     long long   start_cnt;
98 #endif
99 #ifdef PERL_IMPLICIT_CONTEXT
100 #  define register
101     pTHX;
102 #  undef register
103 #endif
104 } prof_state_t;
105
106 prof_state_t g_prof_state;
107
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
134 #endif
135 #ifdef OS2
136 #  define g_frequ               g_prof_state.frequ
137 #  define g_start_cnt           g_prof_state.start_cnt
138 #endif
139
140 clock_t
141 dprof_times(pTHX_ struct tms *t)
142 {
143 #ifdef OS2
144     ULONG rc;
145     QWORD cnt;
146     
147     if (!g_frequ) {
148         if (CheckOSError(DosTmrQueryFreq(&g_frequ)))
149             croak("DosTmrQueryFreq: %s", SvPV(perl_get_sv("!",TRUE),na));
150         else
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);
156     }
157
158     if (CheckOSError(DosTmrQueryTime(&cnt)))
159             croak("DosTmrQueryTime: %s", SvPV(perl_get_sv("!",TRUE),na));
160     t->tms_stime = 0;
161     return (t->tms_utime = (toLongLong(cnt) - g_start_cnt)/g_frequ);
162 #else           /* !OS2 */
163 #  ifdef VMS
164     clock_t retval;
165     /* Get wall time and convert to 10 ms intervals to
166      * produce the return value dprof expects */
167 #    if defined(__DECC) && defined (__ALPHA)
168 #      include <ints.h>
169     uint64 vmstime;
170     _ckvmssts(sys$gettim(&vmstime));
171     vmstime /= 100000;
172     retval = vmstime & 0x7fffffff;
173 #    else
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));
181 #    endif
182     /* Fill in the struct tms using the CRTL routine . . .*/
183     times((tbuffer_t *)t);
184     return (clock_t) retval;
185 #  else         /* !VMS && !OS2 */
186     return times(t);
187 #  endif
188 #endif
189 }
190
191 static void
192 prof_dumpa(pTHX_ opcode ptype, U32 id)
193 {
194     if (ptype == OP_LEAVESUB) {
195         PerlIO_printf(g_fp,"- %"UVxf"\n", (UV)id);
196     }
197     else if(ptype == OP_ENTERSUB) {
198         PerlIO_printf(g_fp,"+ %"UVxf"\n", (UV)id);
199     }
200     else if(ptype == OP_GOTO) {
201         PerlIO_printf(g_fp,"* %"UVxf"\n", (UV)id);
202     }
203     else if(ptype == OP_DIE) {
204         PerlIO_printf(g_fp,"/ %"UVxf"\n", (UV)id);
205     }
206     else {
207         PerlIO_printf(g_fp,"Profiler unknown prof code %d\n", ptype);
208     }
209 }   
210
211 static void
212 prof_dumps(pTHX_ U32 id, char *pname, char *gname)
213 {
214     PerlIO_printf(g_fp,"& %"UVxf" %s %s\n", (UV)id, pname, gname);
215 }   
216
217 static void
218 prof_dumpt(pTHX_ long tms_utime, long tms_stime, long realtime)
219 {
220     PerlIO_printf(g_fp,"@ %ld %ld %ld\n", tms_utime, tms_stime, realtime);
221 }   
222
223 static void
224 prof_dump_until(pTHX_ long ix)
225 {
226     long base = 0;
227     struct tms t1, t2;
228     clock_t realtime1, realtime2;
229
230     realtime1 = Times(&t1);
231
232     while (base < ix) {
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;
238
239             prof_dumpt(aTHX_ tms_utime, tms_stime, realtime);
240         }
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;
245
246             prof_dumps(aTHX_ id, pname, gname);
247         }
248         else {
249             U32 id = g_profstack[base++].id;
250             prof_dumpa(aTHX_ ptype, id);
251         }
252     }
253     PerlIO_flush(g_fp);
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;
260
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
265                        * have Clock_t_f. */
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;
273         PerlIO_flush(g_fp);
274     }
275 }
276
277 static void
278 prof_mark(pTHX_ opcode ptype)
279 {
280     struct tms t;
281     clock_t realtime, rdelta, udelta, sdelta;
282     char *name, *pv;
283     char *hvname;
284     STRLEN len;
285     SV *sv;
286     U32 id;
287     SV *Sub = GvSV(PL_DBsub);   /* name of current sub */
288
289     if (g_SAVE_STACK) {
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);
293         }
294     }
295
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) {
301         if (g_SAVE_STACK) {
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;
306         }
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);
310                 PerlIO_flush(g_fp);
311             }
312         }
313         g_orealtime = realtime;
314         g_otms_stime = t.tms_stime;
315         g_otms_utime = t.tms_utime;
316     }
317
318     {
319         SV **svp;
320         char *gname, *pname;
321         CV *cv;
322
323         cv = INT2PTR(CV*,SvIVX(Sub));
324         svp = hv_fetch(g_cv_hash, (char*)&cv, sizeof(CV*), TRUE);
325         if (!SvOK(*svp)) {
326             GV *gv = CvGV(cv);
327                 
328             sv_setiv(*svp, id = ++g_lastid);
329             pname = ((GvSTASH(gv) && HvNAME(GvSTASH(gv))) 
330                      ? HvNAME(GvSTASH(gv)) 
331                      : "(null)");
332             gname = GvNAME(gv);
333             if (CvXSUB(cv) == XS_Devel__DProf_END)
334                 return;
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;
340             }
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);
345                     PerlIO_flush(g_fp);
346                 }
347                 else
348                     PL_perldb = 0;              /* Do not debug the kid. */
349             }
350         }
351         else {
352             id = SvIV(*svp);
353         }
354     }
355
356     g_total++;
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;
360
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);
365             else
366                 PL_perldb = 0;          /* Do not debug the kid. */
367             g_profstack_ix = 0;
368         }
369     }
370     else { /* Write it to disk now so's not to eat up core */
371
372         /* Only record the parent's info */
373         if (g_prof_pid == (int)getpid()) {
374             prof_dumpa(aTHX_ ptype, id);
375             PerlIO_flush(g_fp);
376         }
377         else
378             PL_perldb = 0;              /* Do not debug the kid. */
379     }
380 }
381
382 #ifdef PL_NEEDED
383 #  define defstash PL_defstash
384 #endif
385
386 /* Counts overhead of prof_mark and extra XS call. */
387 static void
388 test_time(pTHX_ clock_t *r, clock_t *u, clock_t *s)
389 {
390     dTHR;
391     CV *cv = perl_get_cv("Devel::DProf::NONESUCH_noxs", FALSE);
392     int i, j, k = 0;
393     HV *oldstash = PL_curstash;
394     struct tms t1, t2;
395     clock_t realtime1, realtime2;
396     U32 ototal = g_total;
397     U32 ostack = g_SAVE_STACK;
398     U32 operldb = PL_perldb;
399
400     g_SAVE_STACK = 1000000;
401     realtime1 = Times(&t1);
402     
403     while (k < 2) {
404         i = 0;
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;
408         while (++i <= 100) {
409             j = 0;
410             g_profstack_ix = 0;         /* Do not let the stack grow */
411             while (++j <= 100) {
412 /*              prof_mark(aTHX_ OP_ENTERSUB); */
413
414                 PUSHMARK(PL_stack_sp);
415                 perl_call_sv((SV*)cv, G_SCALAR);
416                 PL_stack_sp--;
417 /*              prof_mark(aTHX_ OP_LEAVESUB); */
418             }
419         }
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;
426         }
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;      
432         }
433         k++;
434     }
435     g_total = ototal;
436     g_SAVE_STACK = ostack;
437     PL_perldb = operldb;
438 }
439
440 static void
441 prof_recordheader(pTHX)
442 {
443     clock_t r, u, s;
444
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);
451     PerlIO_printf(g_fp,
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
455                    * have Clock_t_f. */
456                   (IV)u, (IV)s, (IV)r);
457     PerlIO_printf(g_fp, "$over_tests=10000;\n");
458
459     g_TIMES_LOCATION = PerlIO_tell(g_fp);
460
461     /* Pad with whitespace. */
462     /* This should be enough even for very large numbers. */
463     PerlIO_printf(g_fp, "%*s\n", 240 , "");
464
465     PerlIO_printf(g_fp, "\n");
466     PerlIO_printf(g_fp, "PART2\n");
467
468     PerlIO_flush(g_fp);
469 }
470
471 static void
472 prof_record(pTHX)
473 {
474     /* g_fp is opened in the BOOT section */
475
476     /* Now that we know the runtimes, fill them in at the recorded
477        location -JH */
478
479     clock_t r, u, s;
480
481     if (g_SAVE_STACK) {
482         prof_dump_until(aTHX_ g_profstack_ix);
483     }
484     PerlIO_seek(g_fp, g_TIMES_LOCATION, SEEK_SET);
485     /* Write into reserved 240 bytes: */
486     PerlIO_printf(g_fp,
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
490                    * have Clock_t_f. */
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);
495     
496     PerlIO_close(g_fp);
497 }
498
499 #define NONESUCH()
500
501 static void
502 check_depth(pTHX_ void *foo)
503 {
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");
508         }
509         else {
510             I32 marks = g_depth - need_depth;
511
512 /*          warn("Check_depth: got %d, expected %d\n", g_depth, need_depth); */
513             while (marks--) {
514                 prof_mark(aTHX_ OP_DIE);
515             }
516             g_depth = need_depth;
517         }
518     }
519 }
520
521 #define for_real
522 #ifdef for_real
523
524 XS(XS_DB_sub)
525 {
526     dXSARGS;
527     dORIGMARK;
528     SV *Sub = GvSV(PL_DBsub);           /* name of current sub */
529
530 #ifdef PERL_IMPLICIT_CONTEXT
531     /* profile only the interpreter that loaded us */
532     if (g_THX != aTHX) {
533         PUSHMARK(ORIGMARK);
534         perl_call_sv(INT2PTR(SV*,SvIV(Sub)), GIMME | G_NODEBUG);
535     }
536     else
537 #endif
538     {
539         HV *oldstash = PL_curstash;
540
541         DBG_SUB_NOTIFY("XS DBsub(%s)\n", SvPV(Sub, na));
542
543         SAVEDESTRUCTOR_X(check_depth, (void*)g_depth);
544         g_depth++;
545
546         prof_mark(aTHX_ OP_ENTERSUB);
547         PUSHMARK(ORIGMARK);
548         perl_call_sv(INT2PTR(SV*,SvIV(Sub)), GIMME | G_NODEBUG);
549         prof_mark(aTHX_ OP_LEAVESUB);
550         g_depth--;
551     }
552     return;
553 }
554
555 XS(XS_DB_goto)
556 {
557 #ifdef PERL_IMPLICIT_CONTEXT
558     if (g_THX == aTHX)
559 #endif
560     {
561         prof_mark(aTHX_ OP_GOTO);
562         return;
563     }
564 }
565
566 #endif /* for_real */
567
568 #ifdef testing
569
570         MODULE = Devel::DProf           PACKAGE = DB
571
572         void
573         sub(...)
574         PPCODE:
575             {
576                 dORIGMARK;
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));
581
582                 sv_setiv(PL_DBsingle, 0);       /* disable DB single-stepping */
583
584                 prof_mark(aTHX_ OP_ENTERSUB);
585                 PUSHMARK(ORIGMARK);
586
587                 PL_curstash = PL_debstash;      /* To disable debugging of perl_call_sv */
588                 perl_call_sv(Sub, GIMME);
589                 PL_curstash = oldstash;
590
591                 prof_mark(aTHX_ OP_LEAVESUB);
592                 SPAGAIN;
593                 /* PUTBACK;  added by xsubpp */
594             }
595
596 #endif /* testing */
597
598 MODULE = Devel::DProf           PACKAGE = Devel::DProf
599
600 void
601 END()
602 PPCODE:
603     {
604         if (PL_DBsub) {
605             /* maybe the process forked--we want only
606              * the parent's profile.
607              */
608             if (
609 #ifdef PERL_IMPLICIT_CONTEXT
610                 g_THX == aTHX &&
611 #endif
612                 g_prof_pid == (int)getpid())
613             {
614                 g_rprof_end = Times(&g_prof_end);
615                 DBG_TIMER_NOTIFY("Profiler timer is off.\n");
616                 prof_record(aTHX);
617             }
618         }
619     }
620
621 void
622 NONESUCH()
623
624 BOOT:
625     {
626         g_TIMES_LOCATION = 42;
627         g_SAVE_STACK = 1<<14;
628         g_profstack_max = 128;
629 #ifdef PERL_IMPLICIT_CONTEXT
630         g_THX = aTHX;
631 #endif
632
633         /* Before we go anywhere make sure we were invoked
634          * properly, else we'll dump core.
635          */
636         if (!PL_DBsub)
637             croak("DProf: run perl with -d to use DProf.\n");
638
639         /* When we hook up the XS DB::sub we'll be redefining
640          * the DB::sub from the PM file.  Turn off warnings
641          * while we do this.
642          */
643         {
644             I32 warn_tmp = PL_dowarn;
645             PL_dowarn = 0;
646             newXS("DB::sub", XS_DB_sub, file);
647             newXS("DB::goto", XS_DB_goto, file);
648             PL_dowarn = warn_tmp;
649         }
650
651         sv_setiv(PL_DBsingle, 0);       /* disable DB single-stepping */
652
653         {
654             char *buffer = getenv("PERL_DPROF_BUFFER");
655
656             if (buffer) {
657                 g_SAVE_STACK = atoi(buffer);
658             }
659
660             buffer = getenv("PERL_DPROF_TICKS");
661
662             if (buffer) {
663                 g_dprof_ticks = atoi(buffer); /* Used under OS/2 only */
664             }
665             else {
666                 g_dprof_ticks = HZ;
667             }
668
669             buffer = getenv("PERL_DPROF_OUT_FILE_NAME");
670             g_out_file_name = savepv(buffer ? buffer : "tmon.out");
671         }
672
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);
676
677         g_default_perldb = PERLDBf_NONAME | PERLDBf_SUB | PERLDBf_GOTO;
678         g_cv_hash = newHV();
679         g_prof_pid = (int)getpid();
680
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;
688     }