6 profiling_sample::profiling_sample(factor_vm *vm,
11 sample_count(sample_count),
12 gc_sample_count(gc_sample_count),
15 vm->record_callstack_sample(&callstack_begin, &callstack_end);
18 void factor_vm::record_sample()
20 cell recorded_sample_count;
21 cell recorded_gc_sample_count;
23 recorded_sample_count = safepoint_sample_count;
24 recorded_gc_sample_count = safepoint_gc_sample_count;
25 if (recorded_sample_count == 0 && recorded_gc_sample_count == 0)
28 /* Another sample signal could be raised while we record these counts */
29 FACTOR_ATOMIC_SUB(&safepoint_sample_count, recorded_sample_count);
30 FACTOR_ATOMIC_SUB(&safepoint_gc_sample_count, recorded_gc_sample_count);
32 samples.push_back(profiling_sample(
34 recorded_sample_count,
35 recorded_gc_sample_count,
40 void factor_vm::record_callstack_sample(cell *begin, cell *end)
42 *begin = sample_callstacks.size();
43 stack_frame *frame = ctx->bottom_frame();
45 while (frame >= ctx->callstack_top) {
46 sample_callstacks.push_back(frame_code(frame));
47 frame = frame_successor(frame);
50 *end = sample_callstacks.size();
53 void factor_vm::set_sampling_profiler(bool sampling_p)
55 if (sampling_p == sampling_profiler_p)
59 start_sampling_profiler();
61 end_sampling_profiler();
64 void factor_vm::clear_samples()
66 // Swapping into temporaries releases the vector's allocated storage,
67 // whereas clear() would leave the allocation as-is
68 std::vector<profiling_sample> sample_graveyard;
69 std::vector<code_block*> sample_callstack_graveyard;
70 samples.swap(sample_graveyard);
71 sample_callstacks.swap(sample_callstack_graveyard);
74 void factor_vm::start_sampling_profiler()
76 safepoint_sample_count = 0;
77 safepoint_gc_sample_count = 0;
79 samples.reserve(10*FACTOR_PROFILE_SAMPLES_PER_SECOND);
80 sample_callstacks.reserve(100*FACTOR_PROFILE_SAMPLES_PER_SECOND);
81 sampling_profiler_p = true;
82 start_sampling_profiler_timer();
85 void factor_vm::end_sampling_profiler()
87 end_sampling_profiler_timer();
89 sampling_profiler_p = false;
92 void factor_vm::primitive_sampling_profiler()
94 set_sampling_profiler(to_boolean(ctx->pop()));
97 void factor_vm::primitive_get_samples()
99 if (sampling_profiler_p || samples.empty()) {
100 ctx->push(false_object);
102 data_root<array> samples_array(allot_array(samples.size(), false_object),this);
103 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
106 for (; from_iter != samples.end(); ++from_iter, ++to_i)
108 data_root<array> sample(allot_array(4, false_object),this);
110 set_array_nth(sample.untagged(),0,from_unsigned_cell(from_iter->sample_count));
111 set_array_nth(sample.untagged(),1,from_unsigned_cell(from_iter->gc_sample_count));
112 set_array_nth(sample.untagged(),2,allot_alien((void*)from_iter->ctx));
114 cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
115 data_root<array> callstack(allot_array(callstack_size,false_object),this);
117 std::vector<code_block*>::const_iterator
118 callstacks_begin = sample_callstacks.begin(),
119 c_from_iter = callstacks_begin + from_iter->callstack_begin,
120 c_from_iter_end = callstacks_begin + from_iter->callstack_end;
123 for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
124 set_array_nth(callstack.untagged(),c_to_i,(*c_from_iter)->owner);
126 set_array_nth(sample.untagged(),3,callstack.value());
128 set_array_nth(samples_array.untagged(),to_i,sample.value());
130 ctx->push(samples_array.value());
134 void factor_vm::primitive_clear_samples()