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