Quick integration of mainline changes to date
[p5sagit/p5-mst-13.2.git] / ext / Devel / DProf / DProf.xs
CommitLineData
146174a9 1#define PERL_NO_GET_CONTEXT
583a019e 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
7399586d 11/*#define DBG_SUB 1 */
12/*#define DBG_TIMER 1 */
583a019e 13
14#ifdef DBG_SUB
146174a9 15# define DBG_SUB_NOTIFY(A,B) warn(A, B)
583a019e 16#else
17# define DBG_SUB_NOTIFY(A,B) /* nothing */
18#endif
19
20#ifdef DBG_TIMER
146174a9 21# define DBG_TIMER_NOTIFY(A) warn(A)
583a019e 22#else
23# define DBG_TIMER_NOTIFY(A) /* nothing */
24#endif
25
583a019e 26/* HZ == clock ticks per second */
27#ifdef VMS
552e38a9 28# define HZ ((I32)CLK_TCK)
583a019e 29# define DPROF_HZ HZ
30# include <starlet.h> /* prototype for sys$gettim() */
146174a9 31# define Times(ptr) (dprof_times(aTHX_ ptr))
583a019e 32#else
33# ifndef HZ
34# ifdef CLK_TCK
552e38a9 35# define HZ ((I32)CLK_TCK)
583a019e 36# else
37# define HZ 60
38# endif
39# endif
40# ifdef OS2 /* times() has significant overhead */
146174a9 41# define Times(ptr) (dprof_times(aTHX_ ptr))
583a019e 42# define INCL_DOSPROFILE
43# define INCL_DOSERRORS
44# include <os2.h>
45# define toLongLong(arg) (*(long long*)&(arg))
146174a9 46# define DPROF_HZ g_dprof_ticks
583a019e 47# else
48# define Times(ptr) (times(ptr))
49# define DPROF_HZ HZ
50# endif
51#endif
52
53XS(XS_Devel__DProf_END); /* used by prof_mark() */
54
583a019e 55/* Everything is built on times(2). See its manpage for a description
56 * of the timings.
57 */
58
583a019e 59union 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
68typedef union prof_any PROFANY;
69
146174a9 70typedef 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
106prof_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
583a019e 139
146174a9 140clock_t
141dprof_times(pTHX_ struct tms *t)
583a019e 142{
146174a9 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);
583a019e 156 }
146174a9 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}
583a019e 190
191static void
146174a9 192prof_dumpa(pTHX_ opcode ptype, U32 id)
583a019e 193{
146174a9 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);
583a019e 208 }
209}
210
211static void
146174a9 212prof_dumps(pTHX_ U32 id, char *pname, char *gname)
583a019e 213{
146174a9 214 PerlIO_printf(g_fp,"& %"UVxf" %s %s\n", (UV)id, pname, gname);
583a019e 215}
216
583a019e 217static void
146174a9 218prof_dumpt(pTHX_ long tms_utime, long tms_stime, long realtime)
583a019e 219{
146174a9 220 PerlIO_printf(g_fp,"@ %ld %ld %ld\n", tms_utime, tms_stime, realtime);
583a019e 221}
222
223static void
146174a9 224prof_dump_until(pTHX_ long ix)
583a019e 225{
226 long base = 0;
227 struct tms t1, t2;
228 clock_t realtime1, realtime2;
229
230 realtime1 = Times(&t1);
231
146174a9 232 while (base < ix) {
233 opcode ptype = g_profstack[base++].ptype;
583a019e 234 if (ptype == OP_TIME) {
146174a9 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);
583a019e 251 }
252 }
146174a9 253 PerlIO_flush(g_fp);
583a019e 254 realtime2 = Times(&t2);
255 if (realtime2 != realtime1 || t1.tms_utime != t2.tms_utime
256 || t1.tms_stime != t2.tms_stime) {
146174a9 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);
583a019e 274 }
275}
276
583a019e 277static void
146174a9 278prof_mark(pTHX_ opcode ptype)
583a019e 279{
146174a9 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 }
583a019e 295
146174a9 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);
583a019e 311 }
583a019e 312 }
146174a9 313 g_orealtime = realtime;
314 g_otms_stime = t.tms_stime;
315 g_otms_utime = t.tms_utime;
316 }
583a019e 317
146174a9 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);
583a019e 346 }
146174a9 347 else
348 PL_perldb = 0; /* Do not debug the kid. */
583a019e 349 }
350 }
146174a9 351 else {
352 id = SvIV(*svp);
353 }
354 }
583a019e 355
146174a9 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 */
583a019e 371
146174a9 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 }
583a019e 380}
381
583a019e 382#ifdef PL_NEEDED
383# define defstash PL_defstash
384#endif
385
386/* Counts overhead of prof_mark and extra XS call. */
387static void
146174a9 388test_time(pTHX_ clock_t *r, clock_t *u, clock_t *s)
583a019e 389{
390 dTHR;
391 CV *cv = perl_get_cv("Devel::DProf::NONESUCH_noxs", FALSE);
392 int i, j, k = 0;
146174a9 393 HV *oldstash = PL_curstash;
583a019e 394 struct tms t1, t2;
395 clock_t realtime1, realtime2;
146174a9 396 U32 ototal = g_total;
397 U32 ostack = g_SAVE_STACK;
398 U32 operldb = PL_perldb;
583a019e 399
146174a9 400 g_SAVE_STACK = 1000000;
583a019e 401 realtime1 = Times(&t1);
402
403 while (k < 2) {
404 i = 0;
405 /* Disable debugging of perl_call_sv on second pass: */
146174a9 406 PL_curstash = (k == 0 ? PL_defstash : PL_debstash);
407 PL_perldb = g_default_perldb;
583a019e 408 while (++i <= 100) {
409 j = 0;
146174a9 410 g_profstack_ix = 0; /* Do not let the stack grow */
583a019e 411 while (++j <= 100) {
146174a9 412/* prof_mark(aTHX_ OP_ENTERSUB); */
583a019e 413
146174a9 414 PUSHMARK(PL_stack_sp);
415 perl_call_sv((SV*)cv, G_SCALAR);
416 PL_stack_sp--;
417/* prof_mark(aTHX_ OP_LEAVESUB); */
583a019e 418 }
419 }
146174a9 420 PL_curstash = oldstash;
583a019e 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;
146174a9 426 }
427 else { /* Subtract time without debug */
583a019e 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 }
146174a9 435 g_total = ototal;
436 g_SAVE_STACK = ostack;
437 PL_perldb = operldb;
583a019e 438}
439
440static void
146174a9 441prof_recordheader(pTHX)
583a019e 442{
146174a9 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);
583a019e 469}
470
471static void
146174a9 472prof_record(pTHX)
583a019e 473{
146174a9 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 */
583a019e 478
146174a9 479 clock_t r, u, s;
583a019e 480
146174a9 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);
583a019e 495
146174a9 496 PerlIO_close(g_fp);
583a019e 497}
498
499#define NONESUCH()
500
583a019e 501static void
d7b9cf63 502check_depth(pTHX_ void *foo)
583a019e 503{
504 U32 need_depth = (U32)foo;
146174a9 505 if (need_depth != g_depth) {
506 if (need_depth > g_depth) {
583a019e 507 warn("garbled call depth when profiling");
146174a9 508 }
509 else {
510 I32 marks = g_depth - need_depth;
583a019e 511
146174a9 512/* warn("Check_depth: got %d, expected %d\n", g_depth, need_depth); */
583a019e 513 while (marks--) {
146174a9 514 prof_mark(aTHX_ OP_DIE);
583a019e 515 }
146174a9 516 g_depth = need_depth;
583a019e 517 }
518 }
519}
520
521#define for_real
522#ifdef for_real
523
524XS(XS_DB_sub)
525{
146174a9 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;
583a019e 540
146174a9 541 DBG_SUB_NOTIFY("XS DBsub(%s)\n", SvPV(Sub, na));
583a019e 542
146174a9 543 SAVEDESTRUCTOR_X(check_depth, (void*)g_depth);
544 g_depth++;
583a019e 545
146174a9 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;
583a019e 553}
554
555XS(XS_DB_goto)
556{
146174a9 557#ifdef PERL_IMPLICIT_CONTEXT
558 if (g_THX == aTHX)
559#endif
560 {
561 prof_mark(aTHX_ OP_GOTO);
583a019e 562 return;
146174a9 563 }
583a019e 564}
565
566#endif /* for_real */
567
568#ifdef testing
569
570 MODULE = Devel::DProf PACKAGE = DB
571
572 void
573 sub(...)
146174a9 574 PPCODE:
575 {
583a019e 576 dORIGMARK;
146174a9 577 HV *oldstash = PL_curstash;
578 SV *Sub = GvSV(PL_DBsub); /* name of current sub */
583a019e 579 /* SP -= items; added by xsubpp */
146174a9 580 DBG_SUB_NOTIFY("XS DBsub(%s)\n", SvPV(Sub, na));
583a019e 581
146174a9 582 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
583a019e 583
146174a9 584 prof_mark(aTHX_ OP_ENTERSUB);
585 PUSHMARK(ORIGMARK);
583a019e 586
146174a9 587 PL_curstash = PL_debstash; /* To disable debugging of perl_call_sv */
588 perl_call_sv(Sub, GIMME);
589 PL_curstash = oldstash;
583a019e 590
146174a9 591 prof_mark(aTHX_ OP_LEAVESUB);
583a019e 592 SPAGAIN;
593 /* PUTBACK; added by xsubpp */
146174a9 594 }
583a019e 595
596#endif /* testing */
597
598MODULE = Devel::DProf PACKAGE = Devel::DProf
599
600void
601END()
146174a9 602PPCODE:
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 }
583a019e 620
621void
622NONESUCH()
623
624BOOT:
146174a9 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
583a019e 633 /* Before we go anywhere make sure we were invoked
634 * properly, else we'll dump core.
635 */
146174a9 636 if (!PL_DBsub)
637 croak("DProf: run perl with -d to use DProf.\n");
583a019e 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 {
146174a9 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;
583a019e 649 }
650
146174a9 651 sv_setiv(PL_DBsingle, 0); /* disable DB single-stepping */
583a019e 652
653 {
654 char *buffer = getenv("PERL_DPROF_BUFFER");
655
656 if (buffer) {
146174a9 657 g_SAVE_STACK = atoi(buffer);
583a019e 658 }
659
660 buffer = getenv("PERL_DPROF_TICKS");
661
662 if (buffer) {
146174a9 663 g_dprof_ticks = atoi(buffer); /* Used under OS/2 only */
664 }
665 else {
666 g_dprof_ticks = HZ;
583a019e 667 }
583a019e 668
146174a9 669 buffer = getenv("PERL_DPROF_OUT_FILE_NAME");
670 g_out_file_name = savepv(buffer ? buffer : "tmon.out");
671 }
583a019e 672
146174a9 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);
583a019e 676
146174a9 677 g_default_perldb = PERLDBf_NONAME | PERLDBf_SUB | PERLDBf_GOTO;
678 g_cv_hash = newHV();
679 g_prof_pid = (int)getpid();
583a019e 680
146174a9 681 New(0, g_profstack, g_profstack_max, PROFANY);
682 prof_recordheader(aTHX);
583a019e 683 DBG_TIMER_NOTIFY("Profiler timer is on.\n");
146174a9 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 }