]> gitweb.factorcode.org Git - factor.git/blob - vm/sampling_profiler.cpp
vm: factor out sample counting code
[factor.git] / vm / sampling_profiler.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 profiling_sample_count profiling_sample_count::record_counts() volatile
7 {
8         FACTOR_MEMORY_BARRIER();
9         profiling_sample_count returned(
10                 sample_count,
11                 gc_sample_count,
12                 foreign_sample_count,
13                 foreign_thread_sample_count);
14         FACTOR_ATOMIC_SUB(&sample_count, returned.sample_count);
15         FACTOR_ATOMIC_SUB(&gc_sample_count, returned.gc_sample_count);
16         FACTOR_ATOMIC_SUB(&foreign_sample_count, returned.foreign_sample_count);
17         FACTOR_ATOMIC_SUB(&foreign_thread_sample_count, returned.foreign_thread_sample_count);
18         return returned;
19 }
20
21 void profiling_sample_count::clear() volatile
22 {
23         sample_count = 0;
24         gc_sample_count = 0;
25         foreign_sample_count = 0;
26         foreign_thread_sample_count = 0;
27         FACTOR_MEMORY_BARRIER();
28 }
29
30 profiling_sample::profiling_sample(factor_vm *vm,
31         profiling_sample_count const &counts,
32         context *ctx)
33         :
34         counts(counts),
35         ctx(ctx)
36 {
37         vm->record_callstack_sample(&callstack_begin, &callstack_end);
38 }
39
40 void factor_vm::record_sample()
41 {
42         profiling_sample_count counts = safepoint_sample_counts.record_counts();
43         if (!counts.empty())
44                 samples.push_back(profiling_sample(this, counts, ctx));
45 }
46
47 void factor_vm::record_callstack_sample(cell *begin, cell *end)
48 {
49         *begin = sample_callstacks.size();
50         stack_frame *frame = ctx->bottom_frame();
51
52         while (frame >= ctx->callstack_top) {
53                 sample_callstacks.push_back(frame_code(frame));
54                 frame = frame_successor(frame);
55         }
56
57         *end = sample_callstacks.size();
58 }
59
60 void factor_vm::set_sampling_profiler(bool sampling_p)
61 {
62         if (sampling_p == sampling_profiler_p)
63                 return;
64         
65         if (sampling_p)
66                 start_sampling_profiler();
67         else
68                 end_sampling_profiler();
69 }
70
71 void factor_vm::clear_samples()
72 {
73         // Swapping into temporaries releases the vector's allocated storage,
74         // whereas clear() would leave the allocation as-is
75         std::vector<profiling_sample> sample_graveyard;
76         std::vector<code_block*> sample_callstack_graveyard;
77         samples.swap(sample_graveyard);
78         sample_callstacks.swap(sample_callstack_graveyard);
79 }
80
81 void factor_vm::start_sampling_profiler()
82 {
83         safepoint_sample_counts.clear();
84         clear_samples();
85         samples.reserve(10*FACTOR_PROFILE_SAMPLES_PER_SECOND);
86         sample_callstacks.reserve(100*FACTOR_PROFILE_SAMPLES_PER_SECOND);
87         sampling_profiler_p = true;
88         start_sampling_profiler_timer();
89 }
90
91 void factor_vm::end_sampling_profiler()
92 {
93         sampling_profiler_p = false;
94         FACTOR_MEMORY_BARRIER();
95         end_sampling_profiler_timer();
96         record_sample();
97 }
98
99 void factor_vm::primitive_sampling_profiler()
100 {
101         set_sampling_profiler(to_boolean(ctx->pop()));
102 }
103
104 void factor_vm::primitive_get_samples()
105 {
106         if (sampling_profiler_p || samples.empty()) {
107                 ctx->push(false_object);
108         } else {
109                 data_root<array> samples_array(allot_array(samples.size(), false_object),this);
110                 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
111                 cell to_i = 0;
112
113                 for (; from_iter != samples.end(); ++from_iter, ++to_i)
114                 {
115                         data_root<array> sample(allot_array(6, false_object),this);
116
117                         set_array_nth(sample.untagged(),0,from_unsigned_cell(from_iter->counts.sample_count));
118                         set_array_nth(sample.untagged(),1,from_unsigned_cell(from_iter->counts.gc_sample_count));
119                         set_array_nth(sample.untagged(),2,from_unsigned_cell(from_iter->counts.foreign_sample_count));
120                         set_array_nth(sample.untagged(),3,from_unsigned_cell(from_iter->counts.foreign_thread_sample_count));
121                         set_array_nth(sample.untagged(),4,allot_alien((void*)from_iter->ctx));
122
123                         cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
124                         data_root<array> callstack(allot_array(callstack_size,false_object),this);
125
126                         std::vector<code_block*>::const_iterator
127                                 callstacks_begin = sample_callstacks.begin(),
128                                 c_from_iter = callstacks_begin + from_iter->callstack_begin,
129                                 c_from_iter_end = callstacks_begin + 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)->owner);
134
135                         set_array_nth(sample.untagged(),5,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 void factor_vm::primitive_clear_samples()
144 {
145         clear_samples();
146 }
147
148 }