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