6 profiling_sample_count profiling_sample_count::record_counts() volatile
9 profiling_sample_count returned(
13 foreign_thread_sample_count);
14 atomic::subtract(&sample_count, returned.sample_count);
15 atomic::subtract(&gc_sample_count, returned.gc_sample_count);
16 atomic::subtract(&foreign_sample_count, returned.foreign_sample_count);
17 atomic::subtract(&foreign_thread_sample_count, returned.foreign_thread_sample_count);
21 void profiling_sample_count::clear() volatile
25 foreign_sample_count = 0;
26 foreign_thread_sample_count = 0;
30 profiling_sample::profiling_sample(factor_vm *vm,
31 profiling_sample_count const &counts,
37 vm->record_callstack_sample(&callstack_begin, &callstack_end);
40 void factor_vm::record_sample()
42 profiling_sample_count counts = safepoint_sample_counts.record_counts();
44 samples.push_back(profiling_sample(this, counts, ctx));
47 void factor_vm::record_callstack_sample(cell *begin, cell *end)
49 *begin = sample_callstacks.size();
50 stack_frame *frame = ctx->bottom_frame();
52 while (frame >= ctx->callstack_top) {
53 sample_callstacks.push_back(frame_code(frame));
54 frame = frame_successor(frame);
57 *end = sample_callstacks.size();
60 void factor_vm::set_sampling_profiler(fixnum rate)
62 bool sampling_p = !!rate;
63 if (sampling_p == sampling_profiler_p)
67 start_sampling_profiler(rate);
69 end_sampling_profiler();
72 void factor_vm::clear_samples()
74 // Swapping into temporaries releases the vector's allocated storage,
75 // whereas clear() would leave the allocation as-is
76 std::vector<profiling_sample> sample_graveyard;
77 std::vector<code_block*> sample_callstack_graveyard;
78 samples.swap(sample_graveyard);
79 sample_callstacks.swap(sample_callstack_graveyard);
82 void factor_vm::start_sampling_profiler(fixnum rate)
84 samples_per_second = rate;
85 safepoint_sample_counts.clear();
87 samples.reserve(10*rate);
88 sample_callstacks.reserve(100*rate);
89 sampling_profiler_p = true;
90 start_sampling_profiler_timer();
93 void factor_vm::end_sampling_profiler()
95 sampling_profiler_p = false;
97 end_sampling_profiler_timer();
101 void factor_vm::primitive_sampling_profiler()
103 set_sampling_profiler(to_fixnum(ctx->pop()));
106 void factor_vm::primitive_get_samples()
108 if (sampling_profiler_p || samples.empty()) {
109 ctx->push(false_object);
111 data_root<array> samples_array(allot_array(samples.size(), false_object),this);
112 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
115 for (; from_iter != samples.end(); ++from_iter, ++to_i)
117 data_root<array> sample(allot_array(6, false_object),this);
119 set_array_nth(sample.untagged(),0,tag_fixnum(from_iter->counts.sample_count));
120 set_array_nth(sample.untagged(),1,tag_fixnum(from_iter->counts.gc_sample_count));
121 set_array_nth(sample.untagged(),2,tag_fixnum(from_iter->counts.foreign_sample_count));
122 set_array_nth(sample.untagged(),3,tag_fixnum(from_iter->counts.foreign_thread_sample_count));
124 cell ctx = allot_alien((void*)from_iter->ctx);
125 set_array_nth(sample.untagged(),4,ctx);
127 cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
128 data_root<array> callstack(allot_array(callstack_size,false_object),this);
130 std::vector<code_block*>::const_iterator
131 callstacks_begin = sample_callstacks.begin(),
132 c_from_iter = callstacks_begin + from_iter->callstack_begin,
133 c_from_iter_end = callstacks_begin + from_iter->callstack_end;
136 for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
137 set_array_nth(callstack.untagged(),c_to_i,(*c_from_iter)->owner);
139 set_array_nth(sample.untagged(),5,callstack.value());
141 set_array_nth(samples_array.untagged(),to_i,sample.value());
143 ctx->push(samples_array.value());
147 void factor_vm::primitive_clear_samples()
152 void factor_vm::enqueue_safepoint_sample(cell samples, cell pc, bool foreign_thread_p)
154 if (atomic::fence(), sampling_profiler_p)
156 atomic::add(&safepoint_sample_counts.sample_count, samples);
157 if (foreign_thread_p)
158 atomic::add(&safepoint_sample_counts.foreign_thread_sample_count, samples);
160 if (atomic::fence(), current_gc)
161 atomic::add(&safepoint_sample_counts.gc_sample_count, samples);
162 if (pc != 0 && !code->seg->in_segment_p(pc))
163 atomic::add(&safepoint_sample_counts.foreign_sample_count, samples);
165 code->guard_safepoint();