6 profiling_sample_count profiling_sample_count::record_counts() volatile
9 profiling_sample_count returned(
14 foreign_thread_sample_count);
15 atomic::fetch_subtract(&sample_count, returned.sample_count);
16 atomic::fetch_subtract(&gc_sample_count, returned.gc_sample_count);
17 atomic::fetch_subtract(&jit_sample_count, returned.jit_sample_count);
18 atomic::fetch_subtract(&foreign_sample_count, returned.foreign_sample_count);
19 atomic::fetch_subtract(&foreign_thread_sample_count, returned.foreign_thread_sample_count);
23 void profiling_sample_count::clear() volatile
28 foreign_sample_count = 0;
29 foreign_thread_sample_count = 0;
33 profiling_sample::profiling_sample(factor_vm *vm,
35 profiling_sample_count const &counts,
41 vm->record_callstack_sample(&callstack_begin, &callstack_end, prolog_p);
44 void factor_vm::record_sample(bool prolog_p)
46 profiling_sample_count counts = safepoint.sample_counts.record_counts();
48 samples.push_back(profiling_sample(this, prolog_p,
49 counts, special_objects[OBJ_CURRENT_THREAD]));
52 void factor_vm::record_callstack_sample(cell *begin, cell *end, bool prolog_p)
54 *begin = sample_callstacks.size();
55 stack_frame *frame = ctx->bottom_frame();
58 assert(frame >= ctx->callstack_top);
59 stack_frame *next_frame = frame_successor(frame);
60 while (next_frame >= ctx->callstack_top)
62 sample_callstacks.push_back(frame_code(frame)->owner);
64 next_frame = frame_successor(next_frame);
69 while (frame >= ctx->callstack_top)
71 sample_callstacks.push_back(frame_code(frame)->owner);
72 frame = frame_successor(frame);
76 *end = sample_callstacks.size();
79 void factor_vm::set_sampling_profiler(fixnum rate)
81 bool sampling_p = !!rate;
82 if (sampling_p == !!atomic::load(&sampling_profiler_p))
86 start_sampling_profiler(rate);
88 end_sampling_profiler();
91 void factor_vm::clear_samples()
93 // Swapping into temporaries releases the vector's allocated storage,
94 // whereas clear() would leave the allocation as-is
95 std::vector<profiling_sample> sample_graveyard;
96 std::vector<cell> sample_callstack_graveyard;
97 samples.swap(sample_graveyard);
98 sample_callstacks.swap(sample_callstack_graveyard);
101 void factor_vm::start_sampling_profiler(fixnum rate)
103 samples_per_second = rate;
104 safepoint.sample_counts.clear();
106 samples.reserve(10*rate);
107 sample_callstacks.reserve(100*rate);
108 atomic::store(&sampling_profiler_p, true);
109 start_sampling_profiler_timer();
112 void factor_vm::end_sampling_profiler()
114 atomic::store(&sampling_profiler_p, false);
115 end_sampling_profiler_timer();
116 record_sample(false);
119 void factor_vm::primitive_sampling_profiler()
121 set_sampling_profiler(to_fixnum(ctx->pop()));
124 void factor_vm::primitive_get_samples()
126 if (atomic::load(&sampling_profiler_p) || samples.empty()) {
127 ctx->push(false_object);
129 data_root<array> samples_array(allot_array(samples.size(), false_object),this);
130 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
133 for (; from_iter != samples.end(); ++from_iter, ++to_i)
135 data_root<array> sample(allot_array(7, false_object),this);
137 set_array_nth(sample.untagged(),0,tag_fixnum(from_iter->counts.sample_count));
138 set_array_nth(sample.untagged(),1,tag_fixnum(from_iter->counts.gc_sample_count));
139 set_array_nth(sample.untagged(),2,tag_fixnum(from_iter->counts.jit_sample_count));
140 set_array_nth(sample.untagged(),3,tag_fixnum(from_iter->counts.foreign_sample_count));
141 set_array_nth(sample.untagged(),4,tag_fixnum(from_iter->counts.foreign_thread_sample_count));
143 set_array_nth(sample.untagged(),5,from_iter->thread);
145 cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
146 data_root<array> callstack(allot_array(callstack_size,false_object),this);
148 std::vector<cell>::const_iterator
149 callstacks_begin = sample_callstacks.begin(),
150 c_from_iter = callstacks_begin + from_iter->callstack_begin,
151 c_from_iter_end = callstacks_begin + from_iter->callstack_end;
154 for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
155 set_array_nth(callstack.untagged(),c_to_i,*c_from_iter);
157 set_array_nth(sample.untagged(),6,callstack.value());
159 set_array_nth(samples_array.untagged(),to_i,sample.value());
161 ctx->push(samples_array.value());
165 void factor_vm::primitive_clear_samples()