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()(void* frame_top, cell frame_size, code_block* owner,
55 sample_callstacks->push_back(owner->owner);
59 void factor_vm::record_callstack_sample(cell* begin, cell* end, bool prolog_p) {
60 *begin = sample_callstacks.size();
62 record_callstack_sample_iterator recorder(&sample_callstacks, prolog_p);
63 iterate_callstack(ctx, recorder);
65 *end = sample_callstacks.size();
67 std::reverse(sample_callstacks.begin() + *begin, sample_callstacks.end());
70 void factor_vm::set_sampling_profiler(fixnum rate) {
71 bool sampling_p = !!rate;
72 if (sampling_p == !!atomic::load(&sampling_profiler_p))
76 start_sampling_profiler(rate);
78 end_sampling_profiler();
81 void factor_vm::clear_samples() {
82 // Swapping into temporaries releases the vector's allocated storage,
83 // whereas clear() would leave the allocation as-is
84 std::vector<profiling_sample> sample_graveyard;
85 std::vector<cell> sample_callstack_graveyard;
86 samples.swap(sample_graveyard);
87 sample_callstacks.swap(sample_callstack_graveyard);
90 void factor_vm::start_sampling_profiler(fixnum rate) {
91 samples_per_second = rate;
92 safepoint.sample_counts.clear();
94 samples.reserve(10 * rate);
95 sample_callstacks.reserve(100 * rate);
96 atomic::store(&sampling_profiler_p, true);
97 start_sampling_profiler_timer();
100 void factor_vm::end_sampling_profiler() {
101 atomic::store(&sampling_profiler_p, false);
102 end_sampling_profiler_timer();
103 record_sample(false);
106 void factor_vm::primitive_sampling_profiler() {
107 set_sampling_profiler(to_fixnum(ctx->pop()));
110 /* Allocates memory */
111 void factor_vm::primitive_get_samples() {
112 if (atomic::load(&sampling_profiler_p) || samples.empty()) {
113 ctx->push(false_object);
115 data_root<array> samples_array(allot_array(samples.size(), false_object),
117 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
120 for (; from_iter != samples.end(); ++from_iter, ++to_i) {
121 data_root<array> sample(allot_array(7, false_object), this);
123 set_array_nth(sample.untagged(), 0,
124 tag_fixnum(from_iter->counts.sample_count));
125 set_array_nth(sample.untagged(), 1,
126 tag_fixnum(from_iter->counts.gc_sample_count));
127 set_array_nth(sample.untagged(), 2,
128 tag_fixnum(from_iter->counts.jit_sample_count));
129 set_array_nth(sample.untagged(), 3,
130 tag_fixnum(from_iter->counts.foreign_sample_count));
131 set_array_nth(sample.untagged(), 4,
132 tag_fixnum(from_iter->counts.foreign_thread_sample_count));
134 set_array_nth(sample.untagged(), 5, from_iter->thread);
136 cell callstack_size =
137 from_iter->callstack_end - from_iter->callstack_begin;
138 data_root<array> callstack(allot_array(callstack_size, false_object),
141 std::vector<cell>::const_iterator callstacks_begin =
142 sample_callstacks.begin(),
145 from_iter->callstack_begin,
148 from_iter->callstack_end;
151 for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
152 set_array_nth(callstack.untagged(), c_to_i, *c_from_iter);
154 set_array_nth(sample.untagged(), 6, callstack.value());
156 set_array_nth(samples_array.untagged(), to_i, sample.value());
158 ctx->push(samples_array.value());
162 void factor_vm::primitive_clear_samples() { clear_samples(); }