src/runloop.cpp

filter: Statementstmt 84.9% 540 / 636 Branchbran 58.0% 289 / 498 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.1% 829 / 1134 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/tracefile.cpp ↑ Summary src/snappy/csnappy_decompress.c »
1 #include "runloop.h"
2 #include "ppport.h"
3  
4 #if !defined(_WIN32)
5 #include <time.h>
6 #include <pthread.h>
7 #endif
8  
9 #ifdef __APPLE__
10 #include <mach/mach_time.h>
11 #endif
12  
13 #include <new> // for placement new
14  
15 #include "tracecollector.h"
16 #include "tracefile.h"
17 #include "rand.h"
18  
19 #include <string>
20  
21 #ifndef OP_ENTRY_PROBE
22 # define OP_ENTRY_PROBE
23 #endif
24  
25 using namespace devel::statprofiler;
26 using namespace std;
27  
28 extern MGVTBL Devel_StatProfiler_eval_idx_vtbl;
29 extern MGVTBL Devel_StatProfiler_save_eval_txt_vtbl;
30  
31 #define MY_CXT_KEY "Devel::StatProfiler::_guts" XS_VERSION
32  
33 namespace {
34 enum SourceCodeKind {
35 NONE = 0,
36 TRACED_EVALS = 1,
37 ALL_EVALS = 2,
38 ALL_EVALS_ALWAYS = 3,
39 };
40  
41 #if defined(_WIN32)
42 struct Mutex {
43 Mutex() {
44 BOOL ok = InitializeCriticalSectionAndSpinCount(&critical_section, 3000);
45  
46 if (!ok)
47 Perl_croak_nocontext("Devel::StatProfiler: error %d initializing critical section", GetLastError());
48 }
49  
50 ~Mutex() {
51 DeleteCriticalSection(&critical_section);
52 }
53  
54 void lock() {
55 EnterCriticalSection(&critical_section);
56 }
57  
58 void unlock() {
59 LeaveCriticalSection(&critical_section);
60 }
61 private:
62 CRITICAL_SECTION critical_section;
63 };
64 #else
65 struct Mutex {
66 136 Mutex() {
67 136 reinit();
68 136 }
69  
70 136 ~Mutex() {
71 136 int rc = pthread_mutex_destroy(&mutex);
72  
73 136 ▶ if (rc)
Branch: 1 branch
BranchTrueFalseTotal
if (rc) 0 136 136
74 0 Perl_croak_nocontext("Devel::StatProfiler: error %d destroying mutex", rc);
75 136 }
76  
77 51413803 void lock() {
78 51413803 int rc = pthread_mutex_lock(&mutex);
79  
80 51414019 ▶ if (rc)
Branch: 1 branch
BranchTrueFalseTotal
if (rc) 0 51414019 51414019
81 0 Perl_croak_nocontext("Devel::StatProfiler: error %d locking mutex", rc);
82 51414019 }
83  
84 51414008 void unlock() {
85 51414008 int rc = pthread_mutex_unlock(&mutex);
86  
87 51413993 ▶ if (rc)
Branch: 1 branch
BranchTrueFalseTotal
if (rc) 0 51413993 51413993
88 0 Perl_croak_nocontext("Devel::StatProfiler: error %d unlocking mutex", rc);
89 51413993 }
90  
91 147 void reinit() {
92 147 memset(&mutex, 0, sizeof(mutex)); // just in case
93  
94 147 int rc = pthread_mutex_init(&mutex, NULL);
95  
96 147 ▶ if (rc)
Branch: 1 branch
BranchTrueFalseTotal
if (rc) 0 147 147
97 0 Perl_croak_nocontext("Devel::StatProfiler: error %d initializing mutex", rc);
98 147 }
99 private:
100 pthread_mutex_t mutex;
101 };
102 #endif
103  
104 struct Cxt {
105 string filename;
106 bool is_template;
107 bool enabled, using_trampoline, resuming, outer_runloop;
108 runops_proc_t original_runloop;
109 OP *switch_op;
110 unsigned int rand_id;
111 uint32_t id[ID_SIZE], parent_id[ID_SIZE];
112 unsigned int ordinal, parent_ordinal;
113 pid_t pid, tid;
114 TraceFileWriter *trace;
115 HV *sections, *global_metadata;
116 bool restore_sections, id_written;
117 unsigned int pred_counter;
118 #ifdef USE_ITHREADS
119 tTHX interp;
120 #endif
121  
122 Cxt(pTHX);
123 Cxt(pTHX_ const Cxt &cxt, CLONE_PARAMS *clone_params);
124 ~Cxt();
125  
126 TraceFileWriter *create_trace(pTHX);
127  
128 void enter_runloop();
129 void leave_runloop();
130  
131 bool is_running() const;
132 bool is_any_running() const;
133  
134 void new_id();
135 void pid_changed();
136  
137 void restart();
138 };
139  
140 struct CounterCxt {
141 unsigned int start_delay;
142  
143 151 CounterCxt(unsigned int delay) :
144 151 start_delay(delay) { }
145 };
146  
147 struct GloballyDestroyed {
148 public:
149 static bool destroyed;
150  
151 68 GloballyDestroyed() {
152 68 destroyed = false;
153 68 }
154  
155 68 ~GloballyDestroyed() {
156 68 destroyed = true;
157 68 }
158 };
159  
160 bool GloballyDestroyed::destroyed = true;
161 }
162  
163 typedef struct Cxt my_cxt_t;
164  
165 // real and test versions
166 static void
167 increment_counter(CounterCxt *arg);
168 static void
169 test_increment_counter(CounterCxt *arg);
170  
171 static void
172 copy_hv(pTHX_ HV *src, HV *dest);
173  
174 static void
175 restore_section_state(pTHX_ pMY_CXT);
176  
177 namespace {
178 #if !defined(_WIN32)
179 // call pthread_atfork once
180 pthread_once_t call_atfork = PTHREAD_ONCE_INIT;
181 #endif
182 // global refcount for the counter thread
183 volatile int refcount = 0;
184 // hold this mutex before reading/writing refcount
185 Mutex refcount_mutex;
186 // global thread identifier
187 int thread_id = 1;
188 #ifdef USE_ITHREADS
189 // hold this mutex before reading/writing tid
190 Mutex tid_mutex;
191 #endif
192 // global counter, written by increment_counter(), read by the runloops
193 unsigned int counter = 0;
194 unsigned int counter_fraction = 0;
195 #if defined(_WIN32)
196 LONGLONG performance_counter_frequency;
197 #elif defined(__APPLE__)
198 mach_timebase_info_data_t monotonic_clock_info;
199 #endif
200 // sampling interval, in microseconds
201 unsigned int sampling_interval = 10000;
202 // random start delay, to improve distribution
203 unsigned int random_start = 0;
204 // number of stack frames to collect
205 unsigned int stack_collect_depth = 20;
206 // Something largeish: 10MB
207 size_t max_output_file_size = 10 * 1024*1024;
208 // which source code needs saving, see StatProfiler.pm
209 SourceCodeKind source_code_kind = NONE;
210 bool seeded = false;
211 // hooks for saving eval text
212 BHK scope_hooks;
213 // global "we're being destroyed" flag
214 GloballyDestroyed destroyed;
215  
216 // used for testing, but so small we always allocate them
217 Mutex test_counter_increment_mutex;
218 unsigned int test_counter_increment = 0;
219 void (* increment_counter_function)(CounterCxt *cxt) = &increment_counter;
220 }
221  
222 // this must be after the variables above, otherwise it gets destroyed
223 // later, and it depends (at least) on the refcount mutex
224 START_MY_CXT;
225  
226 static void
227 0 copy_hv(pTHX_ HV *src, HV *dest)
228 {
229 char *key;
230 I32 klen;
231  
232 0 ▶ hv_iterinit(src);
Branch: 1 branch
BranchTrueFalseTotal
hv_iterinit(src); 0 0 0
233  
234 0 ▶ while (SV *value = hv_iternextsv(src, &key, &klen)) {
Branch: 2 branches
BranchTrueFalseTotal
while (SV *value = hv_iternextsv(src, &key, &klen)) { 0 0 0
while (SV *value = hv_iternextsv(src, &key, &klen)) { 0 0 0
235 0 ▶ SV **targ = hv_fetch(dest, key, klen, 1);
Branch: 1 branch
BranchTrueFalseTotal
SV **targ = hv_fetch(dest, key, klen, 1); 0 0 0
236  
237 0 ▶ SvSetSV(*targ, value);
Branch: 2 branches
BranchTrueFalseTotal
SvSetSV(*targ, value); 0 0 0
SvSetSV(*targ, value); 0 0 0
238 0 }
239 0 }
240  
241  
242 static bool
243 start_counter_thread();
244  
245  
246 static int
247 147 new_thread_id()
248 {
249 #ifdef USE_ITHREADS
250 tid_mutex.lock();
251 int id = ++thread_id;
252 tid_mutex.unlock();
253  
254 return id;
255 #else
256 147 return ++thread_id;
257 #endif
258 }
259  
260  
261 136 Cxt::Cxt(pTHX) :
262 272 ▶ filename("statprof.out"),
Branch: 1 branch
BranchTrueFalseTotal
filename("statprof.out"), 136 0 136
263 136 is_template(true),
264 136 enabled(true),
265 136 using_trampoline(false),
266 136 resuming(false),
267 136 outer_runloop(false),
268 136 original_runloop(NULL),
269 136 switch_op(NULL),
270 136 ▶ rand_id(rand_seed()),
Branch: 1 branch
BranchTrueFalseTotal
rand_id(rand_seed()), 136 0 136
271 136 ordinal(0),
272 136 parent_ordinal(0),
273 136 pid(getpid()),
274 136 tid(new_thread_id()),
275 136 sections(NULL),
276 136 global_metadata(NULL),
277 136 restore_sections(false),
278 136 id_written(false),
279 136 pred_counter(0),
280 136 trace(NULL)
281 {
282 #ifdef USE_ITHREADS
283 interp = aTHX;
284 #endif
285  
286 136 ▶ new_id();
Branch: 1 branch
BranchTrueFalseTotal
new_id(); 136 0 136
287  
288 136 ▶ sections = newHV();
Branch: 1 branch
BranchTrueFalseTotal
sections = newHV(); 136 0 136
289 136 }
290  
291 0 Cxt::Cxt(pTHX_ const Cxt &cxt, CLONE_PARAMS *clone_params) :
292 0 filename(cxt.filename),
293 0 is_template(cxt.is_template),
294 0 enabled(cxt.enabled),
295 0 using_trampoline(false),
296 0 resuming(false),
297 0 outer_runloop(false),
298 0 original_runloop(NULL),
299 0 switch_op(cxt.switch_op),
300 0 rand_id(cxt.rand_id),
301 0 ordinal(0),
302 0 parent_ordinal(cxt.ordinal),
303 0 pid(cxt.pid),
304 0 tid(new_thread_id()),
305 0 sections(NULL),
306 0 global_metadata(NULL),
307 0 restore_sections(true),
308 0 id_written(false),
309 0 pred_counter(0),
310 0 trace(NULL)
311 {
312 #ifdef USE_ITHREADS
313 interp = aTHX;
314  
315 new_id();
316 memcpy(parent_id, cxt.id, sizeof(id));
317 sections = MUTABLE_HV(sv_dup_inc((SV*) cxt.sections, clone_params));
318  
319 if (cxt.global_metadata) {
320 global_metadata = MUTABLE_HV(sv_dup_inc((SV*) cxt.global_metadata, clone_params));
321 }
322 #else
323 0 croak("Only called for threads");
324 #endif
325 }
326  
327 68 Cxt::~Cxt() {
328 dTHX;
329  
330 68 SvREFCNT_dec(sections);
331 68 SvREFCNT_dec(global_metadata);
332 68 ▶ if (outer_runloop)
Branch: 1 branch
BranchTrueFalseTotal
if (outer_runloop) 0 68 68
333 0 Perl_croak(aTHX_ "Devel::StatProfiler: deleting context for a running runloop");
334 68 ▶ if (original_runloop)
Branch: 1 branch
BranchTrueFalseTotal
if (original_runloop) 60 8 68
335 60 PL_runops = original_runloop;
336 68 ▶ if (trace)
Branch: 1 branch
BranchTrueFalseTotal
if (trace) 58 10 68
337 58 trace->close(TraceFileWriter::write_end_tag);
338 68 ▶ delete trace;
Branch: 1 branch
BranchTrueFalseTotal
delete trace; 58 10 68
339 68 }
340  
341 void
342 10 Cxt::restart()
343 {
344 10 ▶ if (id_written) {
Branch: 1 branch
BranchTrueFalseTotal
if (id_written) { 1 9 10
345 1 parent_ordinal = ordinal;
346 1 memcpy(parent_id, id, sizeof(id));
347 1 id_written = false;
348 }
349  
350 10 ordinal = 0;
351 10 new_id();
352 10 }
353  
354 TraceFileWriter *
355 110820 Cxt::create_trace(pTHX)
356 {
357 110820 ▶ if (!trace)
Branch: 1 branch
BranchTrueFalseTotal
if (!trace) 58 110762 110820
358 58 ▶ trace = new TraceFileWriter(aTHX);
Branch: 2 branches
BranchTrueFalseTotal
trace = new TraceFileWriter(aTHX); 58 0 58
trace = new TraceFileWriter(aTHX); 0 0 0
359  
360 110820 ▶ if (!trace->is_valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!trace->is_valid()) { 59 110761 110820
361 59 ++ordinal;
362  
363 59 trace->open(filename, is_template, id, ordinal);
364 59 ▶ if (trace->is_valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (trace->is_valid()) { 59 0 59
365 59 trace->write_header(sampling_interval, stack_collect_depth,
366 59 id, ordinal, parent_id, parent_ordinal,
367 global_metadata);
368 59 id_written = true;
369  
370 59 ▶ if (restore_sections) {
Branch: 1 branch
BranchTrueFalseTotal
if (restore_sections) { 0 59 59
371 0 restore_sections = false;
372 #ifdef PERL_IMPLICIT_CONTEXT
373 restore_section_state(aTHX_ this);
374 #else
375 0 restore_section_state(aTHX);
376 #endif
377 }
378 }
379 }
380  
381 110820 return trace;
382 }
383  
384 void
385 848 Cxt::enter_runloop()
386 {
387 848 ▶ if (outer_runloop)
Branch: 1 branch
BranchTrueFalseTotal
if (outer_runloop) 0 848 848
388 0 croak("Excess call to enter_runloop");
389  
390 848 refcount_mutex.lock();
391  
392 848 ▶ if (++refcount == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (++refcount == 1) { 140 708 848
393 140 ▶ if (!start_counter_thread()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!start_counter_thread()) { 0 140 140
394 0 --refcount;
395 0 refcount_mutex.unlock();
396 0 croak("Error %d while starting counter thread", errno);
397 }
398 }
399  
400 848 outer_runloop = true;
401 848 refcount_mutex.unlock();
402 848 }
403  
404 void
405 848 Cxt::leave_runloop()
406 {
407 848 ▶ if (!outer_runloop)
Branch: 1 branch
BranchTrueFalseTotal
if (!outer_runloop) 0 848 848
408 0 croak("Excess call to leave_runloop");
409  
410 848 refcount_mutex.lock();
411 848 outer_runloop = false;
412  
413 848 --refcount;
414  
415 848 refcount_mutex.unlock();
416 848 }
417  
418 bool
419 117 Cxt::is_running() const
420 {
421 117 ▶ return outer_runloop || (trace && trace->is_valid());
Branch: 3 branches
BranchTrueFalseTotal
return outer_runloop || (trace && trace->is_valid()); 106 11 117
return outer_runloop || (trace && trace->is_valid()); 0 106 106
return outer_runloop || (trace && trace->is_valid()); 0 0 0
422 }
423  
424 bool
425 46 Cxt::is_any_running() const
426 {
427 46 ▶ if (is_running())
Branch: 1 branch
BranchTrueFalseTotal
if (is_running()) 0 46 46
428 0 return true;
429  
430 // no point in locking refcount_mutex here, if correctness is
431 // needed, the mutex needs to be held around the whole section
432 // using the result
433 46 return refcount > 1;
434 }
435  
436 void
437 157 Cxt::new_id()
438 {
439 157 id[0] = pid;
440 157 id[1] = tid;
441 157 id[2] = time(NULL);
442  
443 628 ▶ for (int i = 0; i < 3; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < 3; ++i) { 471 157 628
444 471 rand(&rand_id);
445 471 id[i + 3] = rand_id;
446 }
447  
448 157 ordinal = 0;
449 157 }
450  
451 void
452 11 Cxt::pid_changed()
453 {
454 11 ▶ if (id_written) {
Branch: 1 branch
BranchTrueFalseTotal
if (id_written) { 11 0 11
455 11 memcpy(parent_id, id, sizeof(parent_id));
456 11 parent_ordinal = ordinal;
457 11 id_written = false;
458 }
459  
460 11 pid = getpid();
461 11 tid = new_thread_id();
462  
463 11 new_id();
464 11 }
465  
466 static void
467 11 restore_section_state(pTHX_ pMY_CXT)
468 {
469 11 ▶ if (!MY_CXT.trace->is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (!MY_CXT.trace->is_valid()) 0 11 11
470 0 return;
471  
472 char *key;
473 I32 klen;
474  
475 11 hv_iterinit(MY_CXT.sections);
476 13 ▶ while (HE *hek = hv_iternext(MY_CXT.sections)) {
Branch: 1 branch
BranchTrueFalseTotal
while (HE *hek = hv_iternext(MY_CXT.sections)) { 2 11 13
477 2 MY_CXT.trace->start_section(hv_iterkeysv(hek));
478 2 }
479 }
480  
481 static void
482 860 reopen_output_file(pTHX_ pMY_CXT)
483 {
484 860 MY_CXT.trace->close();
485  
486 860 ++MY_CXT.ordinal;
487  
488 860 MY_CXT.trace->open(MY_CXT.filename, MY_CXT.is_template,
489 MY_CXT.id, MY_CXT.ordinal);
490 860 ▶ if (MY_CXT.trace->is_valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.trace->is_valid()) { 860 0 860
491 860 MY_CXT.trace->write_header(sampling_interval, stack_collect_depth,
492 MY_CXT.id, MY_CXT.ordinal, MY_CXT.parent_id, MY_CXT.parent_ordinal,
493 MY_CXT.global_metadata);
494 860 MY_CXT.id_written = true;
495 }
496 // XXX check if we need to write other metadata
497 860 }
498  
499 #define INCREMENT_COUNTER() \
500 counter_fraction += delta % sampling_interval; \
501 counter += delta / sampling_interval + counter_fraction / sampling_interval; \
502 counter_fraction %= sampling_interval; \
503 previous_tick = current_tick
504  
505 // #define DEBUG_INCREMENT_COUNTER
506 static void
507 12 increment_counter(CounterCxt *cxt)
508 {
509 #if defined(_WIN32)
510 unsigned int delay_msec = cxt->start_delay / 1000;
511 LONGLONG previous_tick, current_tick;
512  
513 QueryPerformanceCounter((LARGE_INTEGER *) &previous_tick);
514  
515 #ifdef DEBUG_INCREMENT_COUNTER
516 DWORD debug_start = GetTickCount(), debug_end;
517 unsigned int counter_start = counter;
518 #endif
519 #else
520 12 unsigned int delay_sec = cxt->start_delay / 1000000,
521 12 delay_nsec = cxt->start_delay % 1000000 * 1000;
522  
523 #if defined(__linux__)
524 struct timespec previous_tick, current_tick;
525  
526 12 clock_gettime(CLOCK_MONOTONIC, &previous_tick);
527 #elif defined(__APPLE__)
528 uint64_t previous_tick, current_tick;
529  
530 previous_tick = mach_absolute_time();
531 #endif
532  
533 #ifdef DEBUG_INCREMENT_COUNTER
534 struct timeval debug_start, debug_end;
535 unsigned int counter_start = counter;
536  
537 gettimeofday(&debug_start, NULL);
538 #endif
539 #endif
540 12 int countdown = 0;
541  
542 12 ▶ delete cxt;
Branch: 1 branch
BranchTrueFalseTotal
delete cxt; 12 0 12
543  
544 for (;;) {
545 #if defined(_WIN32)
546 Sleep(delay_msec ? delay_msec : 1);
547 QueryPerformanceCounter((LARGE_INTEGER *) &current_tick);
548  
549 LONGLONG delta = ((current_tick - previous_tick) * 1000000) / performance_counter_frequency;
550 INCREMENT_COUNTER();
551 delay_msec = sampling_interval / 1000;
552 #else
553 498 timespec sleep = {delay_sec, delay_nsec};
554 498 ▶ while (nanosleep(&sleep, &sleep) == EINTR)
Branch: 2 branches
BranchTrueFalseTotal
while (nanosleep(&sleep, &sleep) == EINTR) 486 0 486
while (nanosleep(&sleep, &sleep) == EINTR) 0 486 486
555 ;
556 #if defined(__linux__)
557 486 clock_gettime(CLOCK_MONOTONIC, &current_tick);
558  
559 486 unsigned int delta = (current_tick.tv_sec - previous_tick.tv_sec) * 1000000 + (current_tick.tv_nsec - previous_tick.tv_nsec) / 1000;
560 486 INCREMENT_COUNTER();
561 #elif defined(__APPLE__)
562 current_tick = mach_absolute_time();
563  
564 uint64_t delta = (current_tick - previous_tick) * monotonic_clock_info.numer / (monotonic_clock_info.denom * 1000);
565 INCREMENT_COUNTER();
566 #else
567 ++counter;
568 #endif
569  
570 486 delay_sec = sampling_interval / 1000000;
571 486 delay_nsec = sampling_interval % 1000000 * 1000;
572 #endif
573  
574 // TODO here we probably need a memory barrier, to ensure the
575 // write in the other thread is seen
576 486 ▶ if (GloballyDestroyed::destroyed)
Branch: 1 branch
BranchTrueFalseTotal
if (GloballyDestroyed::destroyed) 0 486 486
577 0 return;
578  
579 // we only synchronize when checking whether the thread needs
580 // to really terminate, but there is no need to lock around
581 // the checks used to start/stop the countdown
582 486 ▶ if (countdown) {
Branch: 1 branch
BranchTrueFalseTotal
if (countdown) { 59 427 486
583 // if we're counting down, check whether something
584 // re-incremented the refcount, and if so keep running
585 59 ▶ if (refcount > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (refcount > 1) 0 59 59
586 0 countdown = 0;
587 59 ▶ else if (--countdown == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (--countdown == 0) { 0 59 59
588 // if the refcount is still 1, decrement it and terminate
589 0 ▶ refcount_mutex.lock();
Branch: 1 branch
BranchTrueFalseTotal
refcount_mutex.lock(); 0 0 0
590  
591 0 ▶ if (refcount == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (refcount == 1) { 0 0 0
592 0 --refcount;
593 0 ▶ refcount_mutex.unlock();
Branch: 1 branch
BranchTrueFalseTotal
refcount_mutex.unlock(); 0 0 0
594  
595 0 return;
596 } else {
597 // fall through and keep running
598 0 ▶ refcount_mutex.unlock();
Branch: 1 branch
BranchTrueFalseTotal
refcount_mutex.unlock(); 0 0 0
599 }
600 }
601 427 ▶ } else if (refcount == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (refcount == 1) { 6 421 427
602 // count down for 1 second or 1 interval (whatever is smaller)
603 6 ▶ countdown = sampling_interval > 1000000 ? 1 : 1000000 / sampling_interval;
Branch: 1 branch
BranchTrueFalseTotal
countdown = sampling_interval > 1000000 ? 1 : 1000000 / sampling_interval; 6 0 6
604  
605 #ifdef DEBUG_INCREMENT_COUNTER
606 unsigned int increments = counter - counter_start;
607 if (increments) {
608 #ifdef _WIN32
609 debug_end = GetTickCount();
610  
611 printf("Counter ticks: %lu, Interval: %ld\n", increments, (debug_end - debug_start) * 1000 / increments);
612 #else
613 gettimeofday(&debug_end, NULL);
614  
615 printf("Counter ticks: %u, Interval: %ld\n\n", increments, ((debug_end.tv_sec - debug_start.tv_sec) * 1000000 + debug_end.tv_usec - debug_start.tv_usec) / increments);
616 #endif
617 }
618 #endif
619 }
620 486 }
621 }
622  
623  
624 #if defined(_WIN32)
625  
626 DWORD WINAPI
627 thread_function(LPVOID arg)
628 {
629 increment_counter_function(static_cast<CounterCxt *>(arg));
630  
631 return 0;
632 }
633  
634 #else
635  
636 static void *
637 151 thread_function(void *arg)
638 {
639 151 increment_counter_function(static_cast<CounterCxt *>(arg));
640  
641 128 return NULL;
642 }
643  
644 #endif
645  
646 static bool
647 151 start_counter_thread()
648 {
649 151 bool ok = true;
650  
651 // init random seed
652 151 ▶ if (!seeded) {
Branch: 1 branch
BranchTrueFalseTotal
if (!seeded) { 57 94 151
653 57 seeded = true;
654 57 ▶ random_start = rand_seed();
Branch: 1 branch
BranchTrueFalseTotal
random_start = rand_seed(); 57 0 57
655 }
656  
657 151 ▶ rand(&random_start);
Branch: 1 branch
BranchTrueFalseTotal
rand(&random_start); 151 0 151
658  
659 #if !defined(_WIN32)
660 pthread_attr_t attr;
661 151 ▶ if (pthread_attr_init(&attr))
Branch: 1 branch
BranchTrueFalseTotal
if (pthread_attr_init(&attr)) 0 151 151
662 0 return false;
663 #endif
664  
665 // discard low-order bits (they tend to be less random and we
666 // don't need them anyway)
667 151 ▶ CounterCxt *cxt = new CounterCxt((random_start >> 8) % sampling_interval);
Branch: 1 branch
BranchTrueFalseTotal
CounterCxt *cxt = new CounterCxt((random_start >> 8) % sampling_interval); 151 0 151
668  
669 #if !defined(_WIN32)
670 pthread_t thread;
671 151 int stack_size = max((size_t) PTHREAD_STACK_MIN, (size_t) 65536);
672  
673 151 ▶ ok = ok && !pthread_attr_setstacksize(&attr, stack_size);
Branch: 2 branches
BranchTrueFalseTotal
ok = ok && !pthread_attr_setstacksize(&attr, stack_size); 151 0 151
ok = ok && !pthread_attr_setstacksize(&attr, stack_size); 151 0 151
674 151 ▶ ok = ok && !pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
Branch: 2 branches
BranchTrueFalseTotal
ok = ok && !pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 151 0 151
ok = ok && !pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 151 0 151
675 151 ▶ ok = ok && !pthread_create(&thread, &attr, thread_function, cxt);
Branch: 2 branches
BranchTrueFalseTotal
ok = ok && !pthread_create(&thread, &attr, thread_function, cxt); 151 0 151
ok = ok && !pthread_create(&thread, &attr, thread_function, cxt); 151 0 151
676  
677 151 int old_errno = errno;
678 151 pthread_attr_destroy(&attr);
679  
680 151 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 0 151 151
681 0 ▶ delete cxt;
Branch: 1 branch
BranchTrueFalseTotal
delete cxt; 0 0 0
682 0 errno = old_errno;
683 }
684 #else
685 HANDLE thread = CreateThread(
686 NULL,
687 65536,
688 thread_function,
689 cxt,
690 STACK_SIZE_PARAM_IS_A_RESERVATION,
691 NULL
692 );
693  
694 // detach
695 if (thread)
696 CloseHandle(thread);
697 else
698 delete cxt;
699  
700 ok = !!thread;
701 #endif
702  
703 // implicit reference held by the counter thread
704 151 ++refcount;
705  
706 151 return ok;
707 }
708  
709  
710 // taken from pp_entersub in pp_hot.c, modified to return NULL rather
711 // than croak()ing or trying autoload
712 //
713 // since this runs after the call, some branches can likely be
714 // simplified (for example the SvGETMAGIC(), and the checks for
715 // SvOK()/strict refs)
716 static CV *
717 3866 get_cv_from_sv(pTHX_ OP* op, SV *sv, GV **name)
718 {
719 3866 CV *cv = NULL;
720 3866 GV *gv = *name = NULL;
721  
722 3866 switch (SvTYPE(sv)) {
723 /* This is overwhelming the most common case: */
724 case SVt_PVGV:
725 3799 we_have_a_glob:
726 3799 ▶ if (!(cv = GvCVu((const GV *)sv))) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(cv = GvCVu((const GV *)sv))) { 3799 0 3799
if (!(cv = GvCVu((const GV *)sv))) { 0 3799 3799
727 HV *stash;
728 0 ▶ cv = sv_2cv(sv, &stash, &gv, 0);
Branch: 1 branch
BranchTrueFalseTotal
cv = sv_2cv(sv, &stash, &gv, 0); 0 0 0
729 }
730 3799 ▶ if (!cv)
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) 0 3799 3799
731 0 return NULL;
732 3799 break;
733 0 case SVt_PVLV:
734 0 ▶ if(isGV_with_GP(sv)) goto we_have_a_glob;
Branch: 3 branches
BranchTrueFalseTotal
if(isGV_with_GP(sv)) goto we_have_a_glob; 0 0 0
if(isGV_with_GP(sv)) goto we_have_a_glob; 0 0 0
if(isGV_with_GP(sv)) goto we_have_a_glob; 0 0 0
735 /*FALLTHROUGH*/
736 default:
737 5 ▶ if (sv == &PL_sv_yes) /* unfound import, ignore */
Branch: 1 branch
BranchTrueFalseTotal
if (sv == &PL_sv_yes) /* unfound import, ignore */ 0 5 5
738 0 return NULL;
739 /* SvGETMAGIC(sv) already called by pp_entersub/pp_goto */
740 5 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 4 1 5
741 4 ▶ if (SvAMAGIC(sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (SvAMAGIC(sv)) { 4 0 4
if (SvAMAGIC(sv)) { 1 3 4
if (SvAMAGIC(sv)) { 1 0 1
742 1 ▶ sv = amagic_deref_call(sv, to_cv_amg);
Branch: 1 branch
BranchTrueFalseTotal
sv = amagic_deref_call(sv, to_cv_amg); 1 0 1
743 /* Don't SPAGAIN here. */
744 }
745 }
746 else {
747 const char *sym;
748 STRLEN len;
749 1 ▶ if (!SvOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) 0 1 1
750 0 return NULL;
751 1 sym = SvPV_nomg_const(sv, len);
752 1 ▶ if (op->op_private & HINT_STRICT_REFS)
Branch: 1 branch
BranchTrueFalseTotal
if (op->op_private & HINT_STRICT_REFS) 0 1 1
753 0 return NULL;
754 1 ▶ cv = get_cvn_flags(sym, len, GV_ADD|SvUTF8(sv));
Branch: 1 branch
BranchTrueFalseTotal
cv = get_cvn_flags(sym, len, GV_ADD|SvUTF8(sv)); 1 0 1
755 1 break;
756 }
757 4 cv = MUTABLE_CV(SvRV(sv));
758 4 ▶ if (SvTYPE(cv) == SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(cv) == SVt_PVCV) 4 0 4
759 4 break;
760 /* FALL THROUGH */
761 case SVt_PVHV:
762 case SVt_PVAV:
763 0 return NULL;
764 /* This is the second most common case: */
765 62 case SVt_PVCV:
766 62 cv = MUTABLE_CV(sv);
767 62 break;
768 }
769  
770 3866 ▶ if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv)))
Branch: 11 branches
BranchTrueFalseTotal
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 3866 0 3866
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 3866 0 3866
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 3866 0 3866
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 3866 0 3866
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 3866 0 3866
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 3866 0 3866
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 3866 0 3866
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 0 3866 3866
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 0 0 0
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 0 0 0
if (cv && !gv && CvGV(cv) && isGV_with_GP(CvGV(cv))) 3866 0 3866
771 3866 ▶ gv = CvGV(cv);
Branch: 1 branch
BranchTrueFalseTotal
gv = CvGV(cv); 3866 0 3866
772  
773 3866 *name = gv;
774 3866 return cv;
775 }
776  
777 static void
778 24676 collect_sample(pTHX_ pMY_CXT_ TraceFileWriter *trace, unsigned int pred_counter, OP *op, OP *prev_op, SV *called_sv)
779 {
780 24676 ▶ if (!trace->is_valid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!trace->is_valid()) { 0 24676 24676
781 433 return;
782 }
783  
784 // Makes sure we assign the correct weight to a trace (possibly 0)
785 // when an inner runloop already collected a trace for this line
786 24676 ▶ if (MY_CXT.pred_counter > pred_counter) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.pred_counter > pred_counter) { 462 24214 24676
787 462 pred_counter = MY_CXT.pred_counter;
788 462 ▶ if (pred_counter == counter)
Branch: 1 branch
BranchTrueFalseTotal
if (pred_counter == counter) 433 29 462
789 433 return;
790 }
791 24243 ▶ if (trace->position() > max_output_file_size && MY_CXT.is_template) {
Branch: 4 branches
BranchTrueFalseTotal
if (trace->position() > max_output_file_size && MY_CXT.is_template) { 24243 0 24243
if (trace->position() > max_output_file_size && MY_CXT.is_template) { 832 23411 24243
if (trace->position() > max_output_file_size && MY_CXT.is_template) { 832 0 832
if (trace->position() > max_output_file_size && MY_CXT.is_template) { 832 23411 24243
792 // Start new output file
793 832 ▶ reopen_output_file(aTHX_ aMY_CXT);
Branch: 1 branch
BranchTrueFalseTotal
reopen_output_file(aTHX_ aMY_CXT); 832 0 832
794 }
795  
796 24243 ▶ const char *op_name = prev_op ? OP_NAME(prev_op) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
const char *op_name = prev_op ? OP_NAME(prev_op) : NULL; 24226 17 24243
const char *op_name = prev_op ? OP_NAME(prev_op) : NULL; 0 24226 24226
const char *op_name = prev_op ? OP_NAME(prev_op) : NULL; 0 0 0
797 24243 GV *xsub_cv_name = NULL;
798 24243 CV *xsub_cv = NULL;
799  
800 // this condition is going to be always true for the OP_ENTERSUB
801 // created by Perl_call_sv, which means the if () is going
802 // to be entered for some non-XSUBs as well
803 24243 ▶ if (prev_op &&
Branch: 1 branch
BranchTrueFalseTotal
if (prev_op && 24226 17 24243
804 24226 ▶ (prev_op->op_type == OP_ENTERSUB ||
Branch: 1 branch
BranchTrueFalseTotal
(prev_op->op_type == OP_ENTERSUB || 20356 3870 24226
805 20356 ▶ prev_op->op_type == OP_GOTO) &&
Branch: 1 branch
BranchTrueFalseTotal
prev_op->op_type == OP_GOTO) && 0 20356 20356
806 3870 ▶ (op == prev_op->op_next) && called_sv) {
Branch: 2 branches
BranchTrueFalseTotal
(op == prev_op->op_next) && called_sv) { 3866 4 3870
(op == prev_op->op_next) && called_sv) { 3866 0 3866
807 // if the sub call is a normal Perl sub, op should be
808 // pointing to CvSTART(), the fact is points to
809 // op_next implies the call was to an XSUB
810 3866 ▶ xsub_cv = get_cv_from_sv(aTHX_ prev_op, called_sv, &xsub_cv_name);
Branch: 1 branch
BranchTrueFalseTotal
xsub_cv = get_cv_from_sv(aTHX_ prev_op, called_sv, &xsub_cv_name); 3866 0 3866
811  
812 3866 ▶ if (xsub_cv && CvISXSUB(xsub_cv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (xsub_cv && CvISXSUB(xsub_cv)) { 3866 0 3866
if (xsub_cv && CvISXSUB(xsub_cv)) { 3866 0 3866
813 3866 op_name = "_xsub";
814 } else {
815 0 xsub_cv = NULL;
816 #if 0 // DEBUG
817 const char *package = "__ANON__", *name = "(unknown)";
818  
819 if (cv_name) {
820 package = HvNAME(GvSTASH(cv_name));
821 name = GvNAME(cv_name);
822 }
823  
824 warn("Called sub %s::%s is not an XSUB", package, name);
825 #endif
826 }
827 }
828  
829 24243 ▶ trace->start_sample(counter - pred_counter, op_name);
Branch: 1 branch
BranchTrueFalseTotal
trace->start_sample(counter - pred_counter, op_name); 24243 0 24243
830 24243 ▶ if (xsub_cv)
Branch: 1 branch
BranchTrueFalseTotal
if (xsub_cv) 3866 20377 24243
831 3866 ▶ trace->add_frame(FRAME_XSUB, xsub_cv, xsub_cv_name, NULL);
Branch: 1 branch
BranchTrueFalseTotal
trace->add_frame(FRAME_XSUB, xsub_cv, xsub_cv_name, NULL); 3866 0 3866
832 24243 ▶ collect_trace(aTHX_ *trace, stack_collect_depth,
Branch: 1 branch
BranchTrueFalseTotal
collect_trace(aTHX_ *trace, stack_collect_depth, 24243 0 24243
833 source_code_kind != NONE);
834 24243 ▶ trace->end_sample();
Branch: 1 branch
BranchTrueFalseTotal
trace->end_sample(); 24243 0 24243
835  
836 // for outer runloops, see comment at the start of this function
837 24243 MY_CXT.pred_counter = counter;
838 }
839  
840 static int
841 1285 write_eval_if_needed(pTHX_ SV *evalcv, MAGIC *mg)
842 {
843 1285 SV *eval_text = mg->mg_obj;
844 1285 ▶ MAGIC *marker = SvMAGICAL(eval_text) ? mg_findext(eval_text, PERL_MAGIC_ext, &Devel_StatProfiler_eval_idx_vtbl) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
MAGIC *marker = SvMAGICAL(eval_text) ? mg_findext(eval_text, PERL_MAGIC_ext, &Devel_StatProfiler_eval_idx_vtbl) : NULL; 1285 0 1285
845  
846 1285 ▶ if (marker) {
Branch: 1 branch
BranchTrueFalseTotal
if (marker) { 1285 0 1285
847 1285 EvalCollected *collected = (EvalCollected *) marker->mg_ptr;
848  
849 1285 ▶ if (!collected->saved && collected->sub_gen != PL_breakable_sub_gen) {
Branch: 2 branches
BranchTrueFalseTotal
if (!collected->saved && collected->sub_gen != PL_breakable_sub_gen) { 1261 24 1285
if (!collected->saved && collected->sub_gen != PL_breakable_sub_gen) { 387 874 1261
850 dMY_CXT;
851  
852 387 TraceFileWriter *trace = MY_CXT.create_trace(aTHX);
853 387 ▶ if (trace->is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (trace->is_valid()) 387 0 387
854 387 trace->add_eval_source(eval_text, collected->evalseq);
855 }
856 1285 collected->saved = true;
857 }
858  
859 1285 return 0;
860 }
861  
862 static void
863 10005 enter_eval_hook(pTHX_ OP *o)
864 {
865 10005 ▶ if (o->op_type != OP_ENTEREVAL || source_code_kind == NONE)
Branch: 2 branches
BranchTrueFalseTotal
if (o->op_type != OP_ENTEREVAL || source_code_kind == NONE) 8420 1585 10005
if (o->op_type != OP_ENTEREVAL || source_code_kind == NONE) 5790 2630 8420
866 7375 return;
867  
868 dMY_CXT;
869  
870 2630 ▶ if (source_code_kind != ALL_EVALS_ALWAYS && !MY_CXT.enabled)
Branch: 2 branches
BranchTrueFalseTotal
if (source_code_kind != ALL_EVALS_ALWAYS && !MY_CXT.enabled) 2574 56 2630
if (source_code_kind != ALL_EVALS_ALWAYS && !MY_CXT.enabled) 1284 1290 2574
871 1284 return;
872  
873 1346 SV *eval_text = cxstack[cxstack_ix].blk_eval.cur_text;
874  
875 1346 get_or_attach_evalcollected(aTHX_ eval_text, true); // overwrite OK
876  
877 1346 ▶ if (source_code_kind == ALL_EVALS_ALWAYS ||
Branch: 1 branch
BranchTrueFalseTotal
if (source_code_kind == ALL_EVALS_ALWAYS || 1290 56 1346
878 1290 ▶ source_code_kind == ALL_EVALS) {
Branch: 1 branch
BranchTrueFalseTotal
source_code_kind == ALL_EVALS) { 1229 61 1290
879 // attach magic to PL_beginav, to have write_eval_if_needed()
880 // called at the end of the eval
881 1285 sv_magicext((SV *) PL_beginav, eval_text, PERL_MAGIC_ext,
882 &Devel_StatProfiler_save_eval_txt_vtbl,
883 NULL, 0);
884 }
885 }
886  
887 static int
888 110266 runloop(pTHX)
889 {
890 dVAR;
891 dMY_CXT;
892 110266 OP *op = PL_op;
893 110266 OP *prev_op = NULL; // Could use PL_op for this, but PL_op might have indirection slowdown
894 110266 SV *called_sv = NULL;
895 110266 unsigned int pred_counter = counter;
896 110266 TraceFileWriter *trace = MY_CXT.create_trace(aTHX);
897  
898 110266 ▶ if (!trace->is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (!trace->is_valid()) 0 110266 110266
899 0 warn("Failed to open trace file");
900  
901 110266 ▶ OP_ENTRY_PROBE(OP_NAME(op));
Branch: 1 branch
BranchTrueFalseTotal
OP_ENTRY_PROBE(OP_NAME(op)); 0 110266 110266
902 303504376 ▶ while ((PL_op = op = op->op_ppaddr(aTHX))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((PL_op = op = op->op_ppaddr(aTHX))) { 303394110 109511 303503621
903 303394110 ▶ if (UNLIKELY( counter != pred_counter )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY( counter != pred_counter )) { 24659 303369451 303394110
904 24659 collect_sample(aTHX_ aMY_CXT_ trace, pred_counter, op, prev_op, called_sv);
905 24659 pred_counter = counter;
906 }
907 // here we save the argument to entersub/goto so, if it ends
908 // up calling an XSUB, we can retrieve sub call information
909 // later (there is the possibility of the called sub modifying
910 // called_sv through an alias, but is such a corner case that
911 // is not worth the trouble)
912 303394110 called_sv = *PL_stack_sp;
913 303394110 prev_op = op;
914 303394110 ▶ OP_ENTRY_PROBE(OP_NAME(op));
Branch: 1 branch
BranchTrueFalseTotal
OP_ENTRY_PROBE(OP_NAME(op)); 0 303394110 303394110
915 }
916  
917 // this is here so XS subs called using Perl_call_sv are correctly
918 // tracked (example: tied hash implemented in XS)
919 109511 ▶ if (UNLIKELY( counter != pred_counter )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNLIKELY( counter != pred_counter )) { 17 109494 109511
920 17 collect_sample(aTHX_ aMY_CXT_ trace, pred_counter, op, prev_op, called_sv);
921 }
922  
923 109511 ▶ PERL_ASYNC_CHECK();
Branch: 1 branch
BranchTrueFalseTotal
PERL_ASYNC_CHECK(); 0 109511 109511
924  
925 109511 TAINT_NOT;
926 109511 return 0;
927 }
928  
929  
930 static int
931 1059 trampoline(pTHX)
932 {
933 dMY_CXT;
934 dJMPENV;
935 int res, exc;
936 1059 volatile bool entered = false; // changed between setjmp and longjmp
937  
938 1059 MY_CXT.using_trampoline = true;
939  
940 1059 JMPENV_PUSH(exc);
941 4022 switch (exc) {
942 case 0:
943 4011 redo_body:
944 do {
945 4250 MY_CXT.resuming = false;
946  
947 4250 ▶ if (MY_CXT.enabled)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.enabled) 1354 2896 4250
948 1354 PL_runops = runloop;
949 else
950 2896 PL_runops = MY_CXT.original_runloop;
951  
952 4250 ▶ if (MY_CXT.enabled && !entered)
Branch: 3 branches
BranchTrueFalseTotal
if (MY_CXT.enabled && !entered) 1354 2896 4250
if (MY_CXT.enabled && !entered) 731 623 1354
if (MY_CXT.enabled && !entered) 731 3519 4250
953 731 ▶ MY_CXT.enter_runloop();
Branch: 1 branch
BranchTrueFalseTotal
MY_CXT.enter_runloop(); 731 0 731
954 3519 ▶ else if (!MY_CXT.enabled && entered)
Branch: 3 branches
BranchTrueFalseTotal
else if (!MY_CXT.enabled && entered) 2896 623 3519
else if (!MY_CXT.enabled && entered) 137 2759 2896
else if (!MY_CXT.enabled && entered) 137 3382 3519
955 137 ▶ MY_CXT.leave_runloop();
Branch: 1 branch
BranchTrueFalseTotal
MY_CXT.leave_runloop(); 137 0 137
956 4250 entered = MY_CXT.enabled;
957  
958 4250 ▶ res = CALLRUNOPS(aTHX);
Branch: 1 branch
BranchTrueFalseTotal
res = CALLRUNOPS(aTHX); 1287 0 1287
959 1287 ▶ PL_op = MY_CXT.resuming ? MY_CXT.switch_op->op_next : NULL;
Branch: 1 branch
BranchTrueFalseTotal
PL_op = MY_CXT.resuming ? MY_CXT.switch_op->op_next : NULL; 239 1048 1287
960 1287 ▶ } while (PL_op);
Branch: 1 branch
BranchTrueFalseTotal
} while (PL_op); 239 1048 1287
961  
962 1048 break;
963 2952 case 3:
964 /* die caught by an inner eval - continue inner loop */
965 2952 ▶ if (PL_restartop && PL_restartjmpenv == PL_top_env) {
Branch: 2 branches
BranchTrueFalseTotal
if (PL_restartop && PL_restartjmpenv == PL_top_env) { 2952 0 2952
if (PL_restartop && PL_restartjmpenv == PL_top_env) { 2952 0 2952
966 2952 PL_restartjmpenv = NULL;
967 2952 PL_op = PL_restartop;
968 2952 PL_restartop = 0;
969 2952 goto redo_body;
970 }
971 /* FALL THROUGH */
972 default:
973 11 JMPENV_POP;
974 11 MY_CXT.using_trampoline = false;
975 11 PL_runops = trampoline;
976 11 ▶ if (entered)
Branch: 1 branch
BranchTrueFalseTotal
if (entered) 11 0 11
977 11 ▶ MY_CXT.leave_runloop();
Branch: 1 branch
BranchTrueFalseTotal
MY_CXT.leave_runloop(); 11 0 11
978  
979 11 ▶ JMPENV_JUMP(exc);
Branch: 4 branches
BranchTrueFalseTotal
JMPENV_JUMP(exc); 9 2 11
JMPENV_JUMP(exc); 2 0 2
JMPENV_JUMP(exc); 0 0 0
JMPENV_JUMP(exc); 0 0 0
980 /* NOTREACHED */
981 }
982  
983 1048 JMPENV_POP;
984 1048 MY_CXT.using_trampoline = false;
985 1048 PL_runops = trampoline;
986 1048 ▶ if (entered)
Branch: 1 branch
BranchTrueFalseTotal
if (entered) 583 465 1048
987 583 ▶ MY_CXT.leave_runloop();
Branch: 1 branch
BranchTrueFalseTotal
MY_CXT.leave_runloop(); 583 0 583
988  
989 1048 return res;
990 }
991  
992  
993 static bool
994 471 switch_runloop(pTHX_ pMY_CXT_ bool enable)
995 {
996 471 bool actually_running = MY_CXT.outer_runloop;
997  
998 471 MY_CXT.enabled = enable;
999 471 ▶ if (enable == actually_running)
Branch: 1 branch
BranchTrueFalseTotal
if (enable == actually_running) 3 468 471
1000 3 return false;
1001  
1002 // the easy case, just let the trampoline handle the switch
1003 468 ▶ if (MY_CXT.using_trampoline)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.using_trampoline) 239 229 468
1004 239 return true;
1005  
1006 229 ▶ if (!enable) {
Branch: 1 branch
BranchTrueFalseTotal
if (!enable) { 112 117 229
1007 // this will exit the currently-running profiling runloop, and
1008 // continue at the line marked >>HERE<< below
1009 112 return true;
1010 } else {
1011 dJMPENV;
1012 int exc;
1013  
1014 117 ▶ MY_CXT.enter_runloop();
Branch: 1 branch
BranchTrueFalseTotal
MY_CXT.enter_runloop(); 117 0 117
1015 117 PL_runops = runloop;
1016 117 PL_op = NORMAL;
1017  
1018 117 JMPENV_PUSH(exc);
1019 120 switch (exc) {
1020 case 0:
1021 118 redo_body:
1022 118 ▶ CALLRUNOPS(aTHX); // execution resumes >>HERE<<
Branch: 1 branch
BranchTrueFalseTotal
CALLRUNOPS(aTHX); // execution resumes >>HERE<< 115 0 115
1023 115 break;
1024  
1025 1 case 3:
1026 /* die caught by an inner eval - continue inner loop */
1027 1 ▶ if (PL_restartop && PL_restartjmpenv == PL_top_env) {
Branch: 2 branches
BranchTrueFalseTotal
if (PL_restartop && PL_restartjmpenv == PL_top_env) { 1 0 1
if (PL_restartop && PL_restartjmpenv == PL_top_env) { 1 0 1
1028 1 PL_restartjmpenv = NULL;
1029 1 PL_op = PL_restartop;
1030 1 PL_restartop = 0;
1031 1 goto redo_body;
1032 }
1033 /* FALL THROUGH */
1034 default:
1035 2 JMPENV_POP;
1036 2 PL_runops = MY_CXT.original_runloop;
1037 2 ▶ MY_CXT.leave_runloop();
Branch: 1 branch
BranchTrueFalseTotal
MY_CXT.leave_runloop(); 2 0 2
1038  
1039 2 ▶ JMPENV_JUMP(exc);
Branch: 4 branches
BranchTrueFalseTotal
JMPENV_JUMP(exc); 0 2 2
JMPENV_JUMP(exc); 2 0 2
JMPENV_JUMP(exc); 0 0 0
JMPENV_JUMP(exc); 0 0 0
1040 /* NOTREACHED */
1041 }
1042  
1043 115 JMPENV_POP;
1044 // for embedded interpreters where the code is called via call_sv,
1045 // preserve the tracing status between call_sv invocations
1046 115 ▶ if (MY_CXT.enabled && MY_CXT.outer_runloop)
Branch: 2 branches
BranchTrueFalseTotal
if (MY_CXT.enabled && MY_CXT.outer_runloop) 3 112 115
if (MY_CXT.enabled && MY_CXT.outer_runloop) 3 0 3
1047 3 PL_runops = trampoline;
1048 else
1049 112 PL_runops = MY_CXT.original_runloop;
1050 115 ▶ MY_CXT.leave_runloop();
Branch: 1 branch
BranchTrueFalseTotal
MY_CXT.leave_runloop(); 115 0 115
1051  
1052 // if not resuming, the program ended for real, othwerwise restore
1053 // PL_op so the outer runloop keeps executing
1054 115 ▶ if (MY_CXT.resuming)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.resuming) 112 3 115
1055 112 PL_op = MY_CXT.switch_op;
1056  
1057 115 return false;
1058 }
1059 }
1060  
1061 static OP *
1062 472 set_profiler_state(pTHX)
1063 {
1064 472 dSP;
1065 dMY_CXT;
1066 472 int state = POPi;
1067  
1068 472 switch (state) {
1069 434 case 0: // disable
1070 case 1: // enable
1071 434 ▶ if (state == 1 && (!MY_CXT.trace || !MY_CXT.trace->is_valid()))
Branch: 4 branches
BranchTrueFalseTotal
if (state == 1 && (!MY_CXT.trace || !MY_CXT.trace->is_valid())) 220 214 434
if (state == 1 && (!MY_CXT.trace || !MY_CXT.trace->is_valid())) 211 9 220
if (state == 1 && (!MY_CXT.trace || !MY_CXT.trace->is_valid())) 1 210 211
if (state == 1 && (!MY_CXT.trace || !MY_CXT.trace->is_valid())) 10 424 434
1072 10 MY_CXT.restart();
1073 434 MY_CXT.resuming = switch_runloop(aTHX_ aMY_CXT_ state == 1);
1074 432 break;
1075 38 case 2: // stop
1076 38 ▶ if (MY_CXT.enabled)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.enabled) 37 1 38
1077 37 MY_CXT.resuming = switch_runloop(aTHX_ aMY_CXT_ false);
1078 38 ▶ if (MY_CXT.trace)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.trace) 38 0 38
1079 38 MY_CXT.trace->close(TraceFileWriter::write_end_tag);
1080 38 hv_clear(MY_CXT.sections);
1081 38 break;
1082 }
1083  
1084 // PL_op can be NULL if we aren't using the trampoline and the
1085 // program ran to completion with tracing still on
1086 470 ▶ if (MY_CXT.resuming || !PL_op)
Branch: 2 branches
BranchTrueFalseTotal
if (MY_CXT.resuming || !PL_op) 119 351 470
if (MY_CXT.resuming || !PL_op) 3 116 119
1087 354 return (OP *) NULL;
1088 else
1089 116 return NORMAL;
1090 }
1091  
1092  
1093 static void
1094 64 cleanup_runloop(pTHX_ void *ptr)
1095 {
1096 dMY_CXT;
1097  
1098 // declared static and destroyed during global destruction
1099 #ifdef PERL_IMPLICIT_CONTEXT
1100 MY_CXT.~Cxt();
1101 #endif
1102 64 }
1103  
1104  
1105 #if !defined(_WIN32)
1106  
1107 static void
1108 47 prepare_fork()
1109 {
1110 dTHX;
1111 dMY_CXT;
1112  
1113 47 ▶ if (MY_CXT.trace)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.trace) 28 19 47
1114 28 MY_CXT.trace->flush();
1115 47 refcount_mutex.lock();
1116 47 }
1117  
1118  
1119 static void
1120 36 parent_after_fork()
1121 {
1122 dTHX;
1123 dMY_CXT;
1124  
1125 36 refcount_mutex.unlock();
1126 // ensures that any eval text the child depends on is in an
1127 // already closed file, ready for processing
1128 36 ▶ if (MY_CXT.trace && MY_CXT.trace->is_valid()) {
Branch: 3 branches
BranchTrueFalseTotal
if (MY_CXT.trace && MY_CXT.trace->is_valid()) { 17 19 36
if (MY_CXT.trace && MY_CXT.trace->is_valid()) { 17 0 17
if (MY_CXT.trace && MY_CXT.trace->is_valid()) { 17 19 36
1129 17 MY_CXT.trace->close();
1130 17 ▶ if (MY_CXT.outer_runloop)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.outer_runloop) 17 0 17
1131 17 reopen_output_file(aTHX_ aMY_CXT);
1132 }
1133 36 }
1134  
1135  
1136 static void
1137 11 child_after_fork()
1138 {
1139 dTHX;
1140 dMY_CXT;
1141  
1142 11 bool running = MY_CXT.is_running();
1143  
1144 11 ▶ if (MY_CXT.trace)
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.trace) 11 0 11
1145 11 MY_CXT.trace->shut();
1146  
1147 11 refcount_mutex.reinit();
1148 11 ▶ refcount = running ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
refcount = running ? 1 : 0; 11 0 11
1149  
1150 11 ▶ if (running && !start_counter_thread())
Branch: 3 branches
BranchTrueFalseTotal
if (running && !start_counter_thread()) 11 0 11
if (running && !start_counter_thread()) 0 11 11
if (running && !start_counter_thread()) 0 11 11
1151 0 croak("Error %d while restarting counter thread", errno);
1152  
1153 11 MY_CXT.pid_changed();
1154 11 MY_CXT.ordinal = 0;
1155 11 ▶ if (running && MY_CXT.trace) {
Branch: 2 branches
BranchTrueFalseTotal
if (running && MY_CXT.trace) { 11 0 11
if (running && MY_CXT.trace) { 11 0 11
1156 11 reopen_output_file(aTHX_ aMY_CXT);
1157 11 restore_section_state(aTHX_ aMY_CXT);
1158 }
1159 11 }
1160  
1161  
1162 static void
1163 68 init_atfork()
1164 {
1165 68 pthread_atfork(prepare_fork, parent_after_fork, child_after_fork);
1166 68 }
1167  
1168 #endif
1169  
1170  
1171 void
1172 68 devel::statprofiler::init_runloop(pTHX)
1173 {
1174 MY_CXT_INIT;
1175 68 ▶ new(&MY_CXT) Cxt(aTHX);
Branch: 2 branches
BranchTrueFalseTotal
new(&MY_CXT) Cxt(aTHX); 68 0 68
new(&MY_CXT) Cxt(aTHX); 0 0 0
1176  
1177 68 Perl_call_atexit(aTHX_ cleanup_runloop, NULL);
1178 #if !defined(_WIN32)
1179 68 pthread_once(&call_atfork, init_atfork);
1180 #endif
1181  
1182 #if defined(_WIN32)
1183 QueryPerformanceFrequency((LARGE_INTEGER *) &performance_counter_frequency);
1184 #elif defined(__APPLE__)
1185 mach_timebase_info(&monotonic_clock_info);
1186 #endif
1187  
1188 68 CV *enable_profiler = get_cv("Devel::StatProfiler::_set_profiler_state", 0);
1189  
1190 204 ▶ for (OP *o = CvSTART(enable_profiler); o; o = o->op_next) {
Branch: 1 branch
BranchTrueFalseTotal
for (OP *o = CvSTART(enable_profiler); o; o = o->op_next) { 204 0 204
1191 204 ▶ if (o->op_type == OP_SRAND) {
Branch: 1 branch
BranchTrueFalseTotal
if (o->op_type == OP_SRAND) { 68 136 204
1192 68 o->op_ppaddr = set_profiler_state;
1193 68 MY_CXT.switch_op = o;
1194 68 break;
1195 }
1196 }
1197 68 }
1198  
1199  
1200 void
1201 0 devel::statprofiler::clone_runloop(pTHX)
1202 {
1203 Cxt *original_cxt;
1204 CLONE_PARAMS clone_params;
1205  
1206 {
1207 dMY_CXT;
1208 0 original_cxt = &MY_CXT;
1209  
1210 0 clone_params.stashes = NULL;
1211 0 clone_params.flags = 0;
1212 #ifdef USE_ITHREADS
1213 clone_params.proto_perl = MY_CXT.interp;
1214 #endif
1215 }
1216  
1217 MY_CXT_CLONE;
1218 0 ▶ new(&MY_CXT) Cxt(aTHX_ *original_cxt, &clone_params);
Branch: 2 branches
BranchTrueFalseTotal
new(&MY_CXT) Cxt(aTHX_ *original_cxt, &clone_params); 0 0 0
new(&MY_CXT) Cxt(aTHX_ *original_cxt, &clone_params); 0 0 0
1219  
1220 // ensures that any eval text the child depends on is in an
1221 // already closed file, ready for processing
1222 0 ▶ if (original_cxt->trace && original_cxt->trace->is_valid()) {
Branch: 3 branches
BranchTrueFalseTotal
if (original_cxt->trace && original_cxt->trace->is_valid()) { 0 0 0
if (original_cxt->trace && original_cxt->trace->is_valid()) { 0 0 0
if (original_cxt->trace && original_cxt->trace->is_valid()) { 0 0 0
1223 0 ▶ reopen_output_file(aTHX_
Branch: 1 branch
BranchTrueFalseTotal
reopen_output_file(aTHX_ 0 0 0
1224 #ifdef PERL_IMPLICIT_CONTEXT
1225 original_cxt
1226 #endif
1227 );
1228 }
1229 0 }
1230  
1231  
1232 void
1233 60 devel::statprofiler::install_runloop()
1234 {
1235 dTHX;
1236 dMY_CXT;
1237  
1238 60 MY_CXT.original_runloop = PL_runops;
1239 60 ▶ if (MY_CXT.enabled) // for the -nostart case
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.enabled) // for the -nostart case 49 11 60
1240 49 PL_runops = trampoline;
1241  
1242 60 BhkENTRY_set(&scope_hooks, bhk_eval, enter_eval_hook);
1243 60 Perl_blockhook_register(aTHX_ &scope_hooks);
1244 60 }
1245  
1246  
1247 void
1248 11 devel::statprofiler::set_enabled(bool enabled)
1249 {
1250 dTHX;
1251 dMY_CXT;
1252  
1253 11 MY_CXT.enabled = enabled;
1254 11 }
1255  
1256 void
1257 59 devel::statprofiler::set_output_file(const char *path, bool is_template)
1258 {
1259 dTHX;
1260 dMY_CXT;
1261  
1262 59 ▶ if (MY_CXT.is_running()) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.is_running()) { 0 59 59
1263 0 warn("Trying to change output file while profiling is in progress");
1264 0 return;
1265 }
1266  
1267 59 MY_CXT.filename = path;
1268 59 MY_CXT.is_template = is_template;
1269 }
1270  
1271 void
1272 46 devel::statprofiler::set_sampling_interval(unsigned int interval)
1273 {
1274 dTHX;
1275 dMY_CXT;
1276  
1277 46 ▶ if (MY_CXT.is_any_running()) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.is_any_running()) { 0 46 46
1278 0 warn("Trying to change sampling interval while profiling is in progress");
1279 0 return;
1280 }
1281  
1282 46 ▶ if (interval == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (interval == 0) { 0 46 46
1283 0 warn("Setting sampling interval to less than a microsecond not "
1284 "supported, defaulting to one microsecond");
1285 0 sampling_interval = 1;
1286 } else
1287 46 sampling_interval = interval;
1288 }
1289  
1290 void
1291 2 devel::statprofiler::set_max_output_file_size(size_t max_size)
1292 {
1293 // Changing this at run time should be safe.
1294 2 max_output_file_size = max_size;
1295 2 }
1296  
1297 void
1298 0 devel::statprofiler::set_stack_collection_depth(unsigned int num_stack_frames)
1299 {
1300 dTHX;
1301 dMY_CXT;
1302  
1303 0 ▶ if (MY_CXT.is_any_running()) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.is_any_running()) { 0 0 0
1304 0 warn("Trying to change stack collection depth while profiling is in progress");
1305 0 return;
1306 }
1307  
1308 0 stack_collect_depth = num_stack_frames;
1309 }
1310  
1311 void
1312 17 devel::statprofiler::set_save_source(unsigned int save_source)
1313 {
1314 dTHX;
1315 dMY_CXT;
1316  
1317 17 source_code_kind = (SourceCodeKind) save_source;
1318 17 }
1319  
1320 void
1321 0 devel::statprofiler::set_global_metadata(pTHX_ HV *metadata)
1322 {
1323 dMY_CXT;
1324  
1325 0 SvREFCNT_dec(MY_CXT.global_metadata);
1326 0 MY_CXT.global_metadata = newHV();
1327 0 copy_hv(aTHX_ metadata, MY_CXT.global_metadata);
1328 0 }
1329  
1330 void
1331 57 devel::statprofiler::write_custom_metadata(pTHX_ SV *key, SV *value)
1332 {
1333 dMY_CXT;
1334  
1335 57 ▶ if (MY_CXT.enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.enabled) { 56 1 57
1336 56 TraceFileWriter *trace = MY_CXT.create_trace(aTHX);
1337 56 ▶ if (trace->is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (trace->is_valid()) 56 0 56
1338 56 trace->write_custom_metadata(key, value);
1339 }
1340 57 }
1341  
1342 void
1343 57 devel::statprofiler::start_section(pTHX_ SV *section_name)
1344 {
1345 dMY_CXT;
1346  
1347 57 ▶ if (MY_CXT.enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.enabled) { 56 1 57
1348 56 TraceFileWriter *trace = MY_CXT.create_trace(aTHX);
1349 56 ▶ if (trace->is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (trace->is_valid()) 56 0 56
1350 56 trace->start_section(section_name);
1351  
1352 56 HE *depth = hv_fetch_ent(MY_CXT.sections, section_name, 1, 0);
1353 56 ▶ if (!SvOK(HeVAL(depth)))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(HeVAL(depth))) 56 0 56
1354 56 sv_setuv(HeVAL(depth), 1);
1355 else
1356 0 sv_setuv(HeVAL(depth), 1 + SvUV(HeVAL(depth)));
1357 }
1358 57 }
1359  
1360 void
1361 56 devel::statprofiler::end_section(pTHX_ SV *section_name)
1362 {
1363 dMY_CXT;
1364  
1365 56 ▶ if (MY_CXT.enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.enabled) { 55 1 56
1366 55 TraceFileWriter *trace =MY_CXT.create_trace(aTHX);
1367 55 ▶ if (trace->is_valid())
Branch: 1 branch
BranchTrueFalseTotal
if (trace->is_valid()) 55 0 55
1368 55 trace->end_section(section_name);
1369  
1370 // we don't want to die here on mismatched sections
1371 55 HE *depth = hv_fetch_ent(MY_CXT.sections, section_name, 0, 0);
1372 55 ▶ if (depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth) { 55 0 55
1373 55 ▶ UV depth_count = SvOK(HeVAL(depth)) ? SvUV(HeVAL(depth)) : 1;
Branch: 1 branch
BranchTrueFalseTotal
UV depth_count = SvOK(HeVAL(depth)) ? SvUV(HeVAL(depth)) : 1; 55 0 55
1374  
1375 55 ▶ if (depth_count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth_count == 1) { 55 0 55
1376 55 hv_delete_ent(MY_CXT.sections, section_name, G_DISCARD, 0);
1377 } else {
1378 0 sv_setuv(HeVAL(depth), depth_count - 1);
1379 }
1380 }
1381 }
1382 56 }
1383  
1384 int
1385 209 devel::statprofiler::get_precision()
1386 {
1387 209 ▶ if (increment_counter_function == &test_increment_counter)
Branch: 1 branch
BranchTrueFalseTotal
if (increment_counter_function == &test_increment_counter) 209 0 209
1388 209 return 1;
1389  
1390 #if defined(_WIN32)
1391 // the actual clock might have been set to be more accurate by
1392 // some program (e.g. Chrome) but it's better to assume the worst
1393 return 15600;
1394 #elif defined(__APPLE__)
1395 // actually, it seems to be accurate to the microsecond
1396 return 100;
1397 #else
1398 timespec res;
1399  
1400 0 ▶ if (clock_getres(CLOCK_REALTIME, &res))
Branch: 1 branch
BranchTrueFalseTotal
if (clock_getres(CLOCK_REALTIME, &res)) 0 0 0
1401 0 return -1;
1402  
1403 0 return res.tv_sec * 1000000 + res.tv_nsec / 1000;
1404 #endif
1405 }
1406  
1407 bool
1408 1 devel::statprofiler::is_running()
1409 {
1410 dTHX;
1411 dMY_CXT;
1412  
1413 1 return MY_CXT.is_running();
1414 }
1415  
1416 // disgusting test hackery
1417 //
1418 // overrides Time::HiRes::{usleep,sleep,time}, OP_FTDIR, OP_UNSTACK,
1419 // OP_NEXTSTATE and the counter thread to simulate a controlled
1420 // version of time, to make the tests less sensitive to system load
1421 //
1422 // enable with Devel::StatProfiler::Test::test_enable()
1423  
1424 // fake opcode implementation for ftdir/unstack/nextstate
1425 static Perl_ppaddr_t orig_ftdir, orig_unstack, orig_subst, orig_nextstate;
1426  
1427 static OP *
1428 88450 test_ftdir(pTHX)
1429 {
1430 88450 test_force_sample(43);
1431  
1432 88450 return orig_ftdir(aTHX);
1433 }
1434  
1435 static OP *
1436 19011556 test_unstack(pTHX)
1437 {
1438 19011556 test_force_sample(23);
1439  
1440 19011556 return orig_unstack(aTHX);
1441 }
1442  
1443 static OP *
1444 64595 test_subst(pTHX)
1445 {
1446 64595 test_force_sample(29);
1447  
1448 64595 return orig_subst(aTHX);
1449 }
1450  
1451 static OP *
1452 31024089 test_nextstate(pTHX)
1453 {
1454 31024089 test_force_sample(13);
1455  
1456 31024089 return orig_nextstate(aTHX);
1457 }
1458  
1459 // overrides the XS implementation for Time::HiRes functions using the
1460 // one provided below
1461 static void
1462 165 fake_hires_function(const char *name)
1463 {
1464 dTHX;
1465  
1466 char src[100], dst[100];
1467  
1468 165 strcpy(src, "Devel::StatProfiler::Test::test_hires_");
1469 165 strcat(src, name);
1470  
1471 165 strcpy(dst, "Time::HiRes::");
1472 165 strcat(dst, name);
1473  
1474 165 ▶ CV *src_cv = get_cv(src, 0);
Branch: 1 branch
BranchTrueFalseTotal
CV *src_cv = get_cv(src, 0); 165 0 165
1475 165 ▶ if (!src_cv)
Branch: 1 branch
BranchTrueFalseTotal
if (!src_cv) 0 165 165
1476 0 croak("Unable to get source XSUB for '%s'", src);
1477  
1478 165 ▶ CV *dst_cv = get_cv(dst, 0);
Branch: 1 branch
BranchTrueFalseTotal
CV *dst_cv = get_cv(dst, 0); 165 0 165
1479 165 ▶ if (!dst_cv)
Branch: 1 branch
BranchTrueFalseTotal
if (!dst_cv) 0 165 165
1480 0 croak("Unable to get source XSUB for '%s'", dst);
1481  
1482 165 CvXSUB(dst_cv) = CvXSUB(src_cv);
1483 165 }
1484  
1485 void
1486 55 devel::statprofiler::test_enable()
1487 {
1488 55 increment_counter_function = &test_increment_counter;
1489  
1490 55 fake_hires_function("usleep");
1491 55 fake_hires_function("sleep");
1492 55 fake_hires_function("time");
1493  
1494 55 orig_ftdir = PL_ppaddr[OP_FTDIR];
1495 55 orig_unstack = PL_ppaddr[OP_UNSTACK];
1496 55 orig_subst = PL_ppaddr[OP_SUBST];
1497 55 orig_nextstate = PL_ppaddr[OP_NEXTSTATE];
1498  
1499 55 PL_ppaddr[OP_FTDIR] = test_ftdir;
1500 55 PL_ppaddr[OP_UNSTACK] = test_unstack;
1501 55 PL_ppaddr[OP_SUBST] = test_subst;
1502 55 PL_ppaddr[OP_NEXTSTATE] = test_nextstate;
1503 55 }
1504  
1505 double
1506 9722 devel::statprofiler::test_hires_usleep(unsigned int usec)
1507 {
1508 9722 test_force_sample(usec);
1509  
1510 9722 return usec;
1511 }
1512  
1513 double
1514 26 devel::statprofiler::test_hires_sleep(double sleep)
1515 {
1516 26 test_hires_usleep(sleep * 1000000);
1517  
1518 26 return sleep;
1519 }
1520  
1521 double
1522 88 devel::statprofiler::test_hires_time()
1523 {
1524 88 return 1234567890 + counter * (sampling_interval / 1000000.0);
1525 }
1526  
1527 #if defined(_WIN32)
1528  
1529 static void
1530 win32_nanosleep_busywait(unsigned nsec) {
1531 LONGLONG current, wanted;
1532  
1533 QueryPerformanceCounter((LARGE_INTEGER *) &wanted);
1534 wanted += nsec * performance_counter_frequency / 1000000000;
1535  
1536 do {
1537 QueryPerformanceCounter((LARGE_INTEGER *) &current);
1538 } while (current < wanted);
1539 }
1540  
1541 #endif
1542  
1543 void
1544 50198412 devel::statprofiler::test_force_sample(unsigned int increment)
1545 {
1546 50198412 ▶ if (increment_counter_function != &test_increment_counter)
Branch: 1 branch
BranchTrueFalseTotal
if (increment_counter_function != &test_increment_counter) 0 50198412 50198412
1547 0 return;
1548  
1549 dTHX;
1550 dMY_CXT;
1551  
1552 50198412 ▶ static unsigned int seed = rand_seed();
Branch: 4 branches
BranchTrueFalseTotal
static unsigned int seed = rand_seed(); 55 50198357 50198412
static unsigned int seed = rand_seed(); 55 0 55
static unsigned int seed = rand_seed(); 55 0 55
static unsigned int seed = rand_seed(); 0 0 0
1553  
1554 // we could just increment the counter by increment and be done
1555 // with it, but this way the test is more realistic (the counter
1556 // is incremented by a separate thread, and we test the thread is
1557 // running)
1558 50198412 rand(&seed);
1559  
1560 50198412 test_counter_increment_mutex.lock();
1561 50198412 test_counter_increment += increment + (seed % increment) / 5;
1562 50198412 ▶ if (test_counter_increment < sampling_interval) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_counter_increment < sampling_interval) { 49298600 899812 50198412
1563 49298600 test_counter_increment_mutex.unlock();
1564 49298600 return;
1565 }
1566 899812 test_counter_increment_mutex.unlock();
1567  
1568 // ugly and inefficient, but good enough for testing
1569 928396 ▶ for (;MY_CXT.outer_runloop;) {
Branch: 1 branch
BranchTrueFalseTotal
for (;MY_CXT.outer_runloop;) { 52458 875938 928396
1570 52458 ▶ test_counter_increment_mutex.lock();
Branch: 1 branch
BranchTrueFalseTotal
test_counter_increment_mutex.lock(); 52458 0 52458
1571 52458 ▶ if (test_counter_increment < sampling_interval) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_counter_increment < sampling_interval) { 23874 28584 52458
1572 23874 ▶ test_counter_increment_mutex.unlock();
Branch: 1 branch
BranchTrueFalseTotal
test_counter_increment_mutex.unlock(); 23874 0 23874
1573 23874 break;
1574 }
1575 28584 ▶ test_counter_increment_mutex.unlock();
Branch: 1 branch
BranchTrueFalseTotal
test_counter_increment_mutex.unlock(); 28584 0 28584
1576  
1577 #if defined(_WIN32)
1578 win32_nanosleep_busywait(100000);
1579 #else
1580 28584 timespec sleep = {0, 100000};
1581 28584 ▶ while (nanosleep(&sleep, &sleep) == EINTR)
Branch: 2 branches
BranchTrueFalseTotal
while (nanosleep(&sleep, &sleep) == EINTR) 28584 0 28584
while (nanosleep(&sleep, &sleep) == EINTR) 0 28584 28584
1582 ;
1583 #endif
1584 }
1585  
1586 899812 ▶ if (!MY_CXT.outer_runloop) {
Branch: 1 branch
BranchTrueFalseTotal
if (!MY_CXT.outer_runloop) { 875938 23874 899812
1587 875938 test_counter_increment_mutex.lock();
1588 875938 counter += test_counter_increment / sampling_interval;
1589 875938 test_counter_increment %= sampling_interval;
1590 875938 test_counter_increment_mutex.unlock();
1591 }
1592 }
1593  
1594 static void
1595 139 test_increment_counter(CounterCxt *cxt)
1596 {
1597 139 ▶ delete cxt;
Branch: 1 branch
BranchTrueFalseTotal
delete cxt; 139 0 139
1598  
1599 for (;;) {
1600 #if defined(_WIN32)
1601 win32_nanosleep_busywait(100000);
1602 #else
1603 285350 timespec sleep = {0, 100000};
1604 285350 ▶ while (nanosleep(&sleep, &sleep) == EINTR)
Branch: 2 branches
BranchTrueFalseTotal
while (nanosleep(&sleep, &sleep) == EINTR) 285339 0 285339
while (nanosleep(&sleep, &sleep) == EINTR) 0 285339 285339
1605 ;
1606 #endif
1607  
1608 285339 ▶ test_counter_increment_mutex.lock();
Branch: 1 branch
BranchTrueFalseTotal
test_counter_increment_mutex.lock(); 285339 0 285339
1609 285339 ▶ if (test_counter_increment >= sampling_interval) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_counter_increment >= sampling_interval) { 23890 261449 285339
1610 23890 counter += test_counter_increment / sampling_interval;
1611 23890 test_counter_increment %= sampling_interval;
1612 }
1613 285339 ▶ test_counter_increment_mutex.unlock();
Branch: 1 branch
BranchTrueFalseTotal
test_counter_increment_mutex.unlock(); 285339 0 285339
1614  
1615 285339 ▶ if (refcount == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (refcount == 1) { 129 285210 285339
1616 129 ▶ refcount_mutex.lock();
Branch: 1 branch
BranchTrueFalseTotal
refcount_mutex.lock(); 129 0 129
1617  
1618 // avoid the complex termination logic in the non-test function
1619 129 ▶ if (refcount == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (refcount == 1) { 128 1 129
1620 128 --refcount;
1621 128 ▶ refcount_mutex.unlock();
Branch: 1 branch
BranchTrueFalseTotal
refcount_mutex.unlock(); 128 0 128
1622  
1623 128 return;
1624 } else {
1625 1 ▶ refcount_mutex.unlock();
Branch: 1 branch
BranchTrueFalseTotal
refcount_mutex.unlock(); 1 0 1
1626 }
1627 }
1628 285211 }
1629 }
1630  
1631 MGVTBL Devel_StatProfiler_save_eval_txt_vtbl = {
1632 NULL, // get
1633 NULL, // set
1634 NULL, // len
1635 NULL, // clear
1636 write_eval_if_needed, // free
1637 };
« src/tracefile.cpp ↑ Summary src/snappy/csnappy_decompress.c »