5 profiling_sample_count profiling_sample_count::record_counts() volatile {
7 profiling_sample_count returned(sample_count, gc_sample_count,
8 jit_sample_count, foreign_sample_count,
9 foreign_thread_sample_count);
10 atomic::fetch_subtract(&sample_count, returned.sample_count);
11 atomic::fetch_subtract(&gc_sample_count, returned.gc_sample_count);
12 atomic::fetch_subtract(&jit_sample_count, returned.jit_sample_count);
13 atomic::fetch_subtract(&foreign_sample_count, returned.foreign_sample_count);
14 atomic::fetch_subtract(&foreign_thread_sample_count,
15 returned.foreign_thread_sample_count);
19 void profiling_sample_count::clear() volatile {
23 foreign_sample_count = 0;
24 foreign_thread_sample_count = 0;
28 profiling_sample::profiling_sample(factor_vm* vm, bool prolog_p,
29 profiling_sample_count const& counts,
31 : counts(counts), thread(thread) {
32 vm->record_callstack_sample(&callstack_begin, &callstack_end, prolog_p);
35 void factor_vm::record_sample(bool prolog_p) {
36 profiling_sample_count counts = safepoint.sample_counts.record_counts();
38 samples.push_back(profiling_sample(this, prolog_p, counts,
39 special_objects[OBJ_CURRENT_THREAD]));
42 struct record_callstack_sample_iterator {
43 std::vector<cell>* sample_callstacks;
46 record_callstack_sample_iterator(std::vector<cell>* sample_callstacks,
48 : sample_callstacks(sample_callstacks), skip_p(prolog_p) {}
50 void operator()(cell frame_top, cell size, code_block* owner, cell addr) {
54 sample_callstacks->push_back(owner->owner);
58 void factor_vm::record_callstack_sample(cell* begin, cell* end, bool prolog_p) {
59 *begin = sample_callstacks.size();
61 record_callstack_sample_iterator recorder(&sample_callstacks, prolog_p);
62 iterate_callstack(ctx, recorder);
64 *end = sample_callstacks.size();
66 std::reverse(sample_callstacks.begin() + *begin, sample_callstacks.end());
69 void factor_vm::set_sampling_profiler(fixnum rate) {
70 bool sampling_p = !!rate;
71 if (sampling_p == !!atomic::load(&sampling_profiler_p))
75 start_sampling_profiler(rate);
77 end_sampling_profiler();
80 void factor_vm::clear_samples() {
81 // Swapping into temporaries releases the vector's allocated storage,
82 // whereas clear() would leave the allocation as-is
83 std::vector<profiling_sample> sample_graveyard;
84 std::vector<cell> sample_callstack_graveyard;
85 samples.swap(sample_graveyard);
86 sample_callstacks.swap(sample_callstack_graveyard);
89 void factor_vm::start_sampling_profiler(fixnum rate) {
90 samples_per_second = rate;
91 safepoint.sample_counts.clear();
93 samples.reserve(10 * rate);
94 sample_callstacks.reserve(100 * rate);
95 atomic::store(&sampling_profiler_p, true);
96 start_sampling_profiler_timer();
99 void factor_vm::end_sampling_profiler() {
100 atomic::store(&sampling_profiler_p, false);
101 end_sampling_profiler_timer();
102 record_sample(false);
105 void factor_vm::primitive_sampling_profiler() {
106 set_sampling_profiler(to_fixnum(ctx->pop()));
109 /* Allocates memory */
110 void factor_vm::primitive_get_samples() {
111 if (atomic::load(&sampling_profiler_p) || samples.empty()) {
112 ctx->push(false_object);
114 data_root<array> samples_array(allot_array(samples.size(), false_object),
116 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
119 for (; from_iter != samples.end(); ++from_iter, ++to_i) {
120 data_root<array> sample(allot_array(7, false_object), this);
122 set_array_nth(sample.untagged(), 0,
123 tag_fixnum(from_iter->counts.sample_count));
124 set_array_nth(sample.untagged(), 1,
125 tag_fixnum(from_iter->counts.gc_sample_count));
126 set_array_nth(sample.untagged(), 2,
127 tag_fixnum(from_iter->counts.jit_sample_count));
128 set_array_nth(sample.untagged(), 3,
129 tag_fixnum(from_iter->counts.foreign_sample_count));
130 set_array_nth(sample.untagged(), 4,
131 tag_fixnum(from_iter->counts.foreign_thread_sample_count));
133 set_array_nth(sample.untagged(), 5, from_iter->thread);
135 cell callstack_size =
136 from_iter->callstack_end - from_iter->callstack_begin;
137 data_root<array> callstack(allot_array(callstack_size, false_object),
140 std::vector<cell>::const_iterator callstacks_begin =
141 sample_callstacks.begin(),
144 from_iter->callstack_begin,
147 from_iter->callstack_end;
150 for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
151 set_array_nth(callstack.untagged(), c_to_i, *c_from_iter);
153 set_array_nth(sample.untagged(), 6, callstack.value());
155 set_array_nth(samples_array.untagged(), to_i, sample.value());
157 ctx->push(samples_array.value());
161 void factor_vm::primitive_clear_samples() { clear_samples(); }