]> gitweb.factorcode.org Git - factor.git/blob - vm/sampling_profiler.cpp
VM: when starting the sampling profiler, the data needs to be cleared
[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 void factor_vm::record_callstack_sample(cell* begin, cell* end, bool prolog_p) {
43   *begin = sample_callstacks.size();
44
45   bool skip_p = prolog_p;
46   auto recorder = [&](cell frame_top, cell size, code_block* owner, cell addr) {
47     if (skip_p)
48       skip_p = false;
49     else
50       sample_callstacks.push_back(owner->owner);
51   };
52   iterate_callstack(ctx, recorder);
53
54   *end = sample_callstacks.size();
55
56   std::reverse(sample_callstacks.begin() + *begin, sample_callstacks.end());
57 }
58
59 void factor_vm::set_sampling_profiler(fixnum rate) {
60   bool sampling_p = !!rate;
61   if (sampling_p == !!atomic::load(&sampling_profiler_p))
62     return;
63
64   if (sampling_p)
65     start_sampling_profiler(rate);
66   else
67     end_sampling_profiler();
68 }
69
70 void factor_vm::start_sampling_profiler(fixnum rate) {
71   samples_per_second = rate;
72   safepoint.sample_counts.clear();
73   // Release the memory consumed by colleting samples.
74   samples.clear();
75   samples.shrink_to_fit();
76   sample_callstacks.clear();
77   sample_callstacks.shrink_to_fit();
78
79   samples.reserve(10 * rate);
80   sample_callstacks.reserve(100 * rate);
81   atomic::store(&sampling_profiler_p, true);
82   start_sampling_profiler_timer();
83 }
84
85 void factor_vm::end_sampling_profiler() {
86   atomic::store(&sampling_profiler_p, false);
87   end_sampling_profiler_timer();
88   record_sample(false);
89 }
90
91 void factor_vm::primitive_sampling_profiler() {
92   set_sampling_profiler(to_fixnum(ctx->pop()));
93 }
94
95 /* Allocates memory */
96 void factor_vm::primitive_get_samples() {
97   if (atomic::load(&sampling_profiler_p) || samples.empty()) {
98     ctx->push(false_object);
99   } else {
100     data_root<array> samples_array(allot_array(samples.size(), false_object),
101                                    this);
102     std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
103     cell to_i = 0;
104
105     for (; from_iter != samples.end(); ++from_iter, ++to_i) {
106       data_root<array> sample(allot_array(7, false_object), this);
107
108       set_array_nth(sample.untagged(), 0,
109                     tag_fixnum(from_iter->counts.sample_count));
110       set_array_nth(sample.untagged(), 1,
111                     tag_fixnum(from_iter->counts.gc_sample_count));
112       set_array_nth(sample.untagged(), 2,
113                     tag_fixnum(from_iter->counts.jit_sample_count));
114       set_array_nth(sample.untagged(), 3,
115                     tag_fixnum(from_iter->counts.foreign_sample_count));
116       set_array_nth(sample.untagged(), 4,
117                     tag_fixnum(from_iter->counts.foreign_thread_sample_count));
118
119       set_array_nth(sample.untagged(), 5, from_iter->thread);
120
121       cell callstack_size =
122           from_iter->callstack_end - from_iter->callstack_begin;
123       data_root<array> callstack(allot_array(callstack_size, false_object),
124                                  this);
125
126       std::vector<cell>::const_iterator callstacks_begin =
127                                             sample_callstacks.begin(),
128                                         c_from_iter =
129                                             callstacks_begin +
130                                             from_iter->callstack_begin,
131                                         c_from_iter_end =
132                                             callstacks_begin +
133                                             from_iter->callstack_end;
134       cell c_to_i = 0;
135
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);
138
139       set_array_nth(sample.untagged(), 6, callstack.value());
140
141       set_array_nth(samples_array.untagged(), to_i, sample.value());
142     }
143     ctx->push(samples_array.value());
144   }
145 }
146
147 }