]> gitweb.factorcode.org Git - factor.git/blob - vm/sampling_profiler.cpp
VM: Refactor sampling_profiler.cpp/hpp to Factor style
[factor.git] / vm / sampling_profiler.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 profiling_sample_count profiling_sample_count::record_counts() volatile {
6   atomic::fence();
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);
16   return returned;
17 }
18
19 void profiling_sample_count::clear() volatile {
20   sample_count = 0;
21   gc_sample_count = 0;
22   jit_sample_count = 0;
23   foreign_sample_count = 0;
24   foreign_thread_sample_count = 0;
25   atomic::fence();
26 }
27
28 profiling_sample::profiling_sample(factor_vm* vm, bool prolog_p,
29                                    profiling_sample_count const& counts,
30                                    cell thread)
31     : counts(counts), thread(thread) {
32   vm->record_callstack_sample(&callstack_begin, &callstack_end, prolog_p);
33 }
34
35 void factor_vm::record_sample(bool prolog_p) {
36   profiling_sample_count counts = safepoint.sample_counts.record_counts();
37   if (!counts.empty())
38     samples.push_back(profiling_sample(this, prolog_p, counts,
39                                        special_objects[OBJ_CURRENT_THREAD]));
40 }
41
42 struct record_callstack_sample_iterator {
43   std::vector<cell>* sample_callstacks;
44   bool skip_p;
45
46   record_callstack_sample_iterator(std::vector<cell>* sample_callstacks,
47                                    bool prolog_p)
48       : sample_callstacks(sample_callstacks), skip_p(prolog_p) {}
49
50   void operator()(void* frame_top, cell frame_size, code_block* owner,
51                   void* addr) {
52     if (skip_p)
53       skip_p = false;
54     else
55       sample_callstacks->push_back(owner->owner);
56   }
57 };
58
59 void factor_vm::record_callstack_sample(cell* begin, cell* end, bool prolog_p) {
60   *begin = sample_callstacks.size();
61
62   record_callstack_sample_iterator recorder(&sample_callstacks, prolog_p);
63   iterate_callstack(ctx, recorder);
64
65   *end = sample_callstacks.size();
66
67   std::reverse(sample_callstacks.begin() + *begin, sample_callstacks.end());
68 }
69
70 void factor_vm::set_sampling_profiler(fixnum rate) {
71   bool sampling_p = !!rate;
72   if (sampling_p == !!atomic::load(&sampling_profiler_p))
73     return;
74
75   if (sampling_p)
76     start_sampling_profiler(rate);
77   else
78     end_sampling_profiler();
79 }
80
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);
88 }
89
90 void factor_vm::start_sampling_profiler(fixnum rate) {
91   samples_per_second = rate;
92   safepoint.sample_counts.clear();
93   clear_samples();
94   samples.reserve(10 * rate);
95   sample_callstacks.reserve(100 * rate);
96   atomic::store(&sampling_profiler_p, true);
97   start_sampling_profiler_timer();
98 }
99
100 void factor_vm::end_sampling_profiler() {
101   atomic::store(&sampling_profiler_p, false);
102   end_sampling_profiler_timer();
103   record_sample(false);
104 }
105
106 void factor_vm::primitive_sampling_profiler() {
107   set_sampling_profiler(to_fixnum(ctx->pop()));
108 }
109
110 /* Allocates memory */
111 void factor_vm::primitive_get_samples() {
112   if (atomic::load(&sampling_profiler_p) || samples.empty()) {
113     ctx->push(false_object);
114   } else {
115     data_root<array> samples_array(allot_array(samples.size(), false_object),
116                                    this);
117     std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
118     cell to_i = 0;
119
120     for (; from_iter != samples.end(); ++from_iter, ++to_i) {
121       data_root<array> sample(allot_array(7, false_object), this);
122
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));
133
134       set_array_nth(sample.untagged(), 5, from_iter->thread);
135
136       cell callstack_size =
137           from_iter->callstack_end - from_iter->callstack_begin;
138       data_root<array> callstack(allot_array(callstack_size, false_object),
139                                  this);
140
141       std::vector<cell>::const_iterator callstacks_begin =
142                                             sample_callstacks.begin(),
143                                         c_from_iter =
144                                             callstacks_begin +
145                                             from_iter->callstack_begin,
146                                         c_from_iter_end =
147                                             callstacks_begin +
148                                             from_iter->callstack_end;
149       cell c_to_i = 0;
150
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);
153
154       set_array_nth(sample.untagged(), 6, callstack.value());
155
156       set_array_nth(samples_array.untagged(), to_i, sample.value());
157     }
158     ctx->push(samples_array.value());
159   }
160 }
161
162 void factor_vm::primitive_clear_samples() { clear_samples(); }
163
164 }