]> gitweb.factorcode.org Git - factor.git/blob - vm/sampling_profiler.cpp
vm: charge samples collected in prolog to parent
[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         atomic::fence();
9         profiling_sample_count returned(
10                 sample_count,
11                 gc_sample_count,
12                 jit_sample_count,
13                 foreign_sample_count,
14                 foreign_thread_sample_count);
15         atomic::fetch_subtract(&sample_count, returned.sample_count);
16         atomic::fetch_subtract(&gc_sample_count, returned.gc_sample_count);
17         atomic::fetch_subtract(&jit_sample_count, returned.jit_sample_count);
18         atomic::fetch_subtract(&foreign_sample_count, returned.foreign_sample_count);
19         atomic::fetch_subtract(&foreign_thread_sample_count, returned.foreign_thread_sample_count);
20         return returned;
21 }
22
23 void profiling_sample_count::clear() volatile
24 {
25         sample_count = 0;
26         gc_sample_count = 0;
27         jit_sample_count = 0;
28         foreign_sample_count = 0;
29         foreign_thread_sample_count = 0;
30         atomic::fence();
31 }
32
33 profiling_sample::profiling_sample(factor_vm *vm,
34         bool prolog_p,
35         profiling_sample_count const &counts,
36         cell thread)
37         :
38         counts(counts),
39         thread(thread)
40 {
41         vm->record_callstack_sample(&callstack_begin, &callstack_end, prolog_p);
42 }
43
44 void factor_vm::record_sample(bool prolog_p)
45 {
46         profiling_sample_count counts = safepoint.sample_counts.record_counts();
47         if (!counts.empty())
48                 samples.push_back(profiling_sample(this, prolog_p,
49                         counts, special_objects[OBJ_CURRENT_THREAD]));
50 }
51
52 void factor_vm::record_callstack_sample(cell *begin, cell *end, bool prolog_p)
53 {
54         *begin = sample_callstacks.size();
55         stack_frame *frame = ctx->bottom_frame();
56         if (prolog_p)
57         {
58                 assert(frame >= ctx->callstack_top);
59                 stack_frame *next_frame = frame_successor(frame);
60                 while (next_frame >= ctx->callstack_top)
61                 {
62                         sample_callstacks.push_back(frame_code(frame)->owner);
63                         frame = next_frame;
64                         next_frame = frame_successor(next_frame);
65                 }
66         }
67         else
68         {
69                 while (frame >= ctx->callstack_top)
70                 {
71                         sample_callstacks.push_back(frame_code(frame)->owner);
72                         frame = frame_successor(frame);
73                 }
74         }
75
76         *end = sample_callstacks.size();
77 }
78
79 void factor_vm::set_sampling_profiler(fixnum rate)
80 {
81         bool sampling_p = !!rate;
82         if (sampling_p == !!atomic::load(&sampling_profiler_p))
83                 return;
84         
85         if (sampling_p)
86                 start_sampling_profiler(rate);
87         else
88                 end_sampling_profiler();
89 }
90
91 void factor_vm::clear_samples()
92 {
93         // Swapping into temporaries releases the vector's allocated storage,
94         // whereas clear() would leave the allocation as-is
95         std::vector<profiling_sample> sample_graveyard;
96         std::vector<cell> sample_callstack_graveyard;
97         samples.swap(sample_graveyard);
98         sample_callstacks.swap(sample_callstack_graveyard);
99 }
100
101 void factor_vm::start_sampling_profiler(fixnum rate)
102 {
103         samples_per_second = rate;
104         safepoint.sample_counts.clear();
105         clear_samples();
106         samples.reserve(10*rate);
107         sample_callstacks.reserve(100*rate);
108         atomic::store(&sampling_profiler_p, true);
109         start_sampling_profiler_timer();
110 }
111
112 void factor_vm::end_sampling_profiler()
113 {
114         atomic::store(&sampling_profiler_p, false);
115         end_sampling_profiler_timer();
116         record_sample(false);
117 }
118
119 void factor_vm::primitive_sampling_profiler()
120 {
121         set_sampling_profiler(to_fixnum(ctx->pop()));
122 }
123
124 void factor_vm::primitive_get_samples()
125 {
126         if (atomic::load(&sampling_profiler_p) || samples.empty()) {
127                 ctx->push(false_object);
128         } else {
129                 data_root<array> samples_array(allot_array(samples.size(), false_object),this);
130                 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
131                 cell to_i = 0;
132
133                 for (; from_iter != samples.end(); ++from_iter, ++to_i)
134                 {
135                         data_root<array> sample(allot_array(7, false_object),this);
136
137                         set_array_nth(sample.untagged(),0,tag_fixnum(from_iter->counts.sample_count));
138                         set_array_nth(sample.untagged(),1,tag_fixnum(from_iter->counts.gc_sample_count));
139                         set_array_nth(sample.untagged(),2,tag_fixnum(from_iter->counts.jit_sample_count));
140                         set_array_nth(sample.untagged(),3,tag_fixnum(from_iter->counts.foreign_sample_count));
141                         set_array_nth(sample.untagged(),4,tag_fixnum(from_iter->counts.foreign_thread_sample_count));
142
143                         set_array_nth(sample.untagged(),5,from_iter->thread);
144
145                         cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
146                         data_root<array> callstack(allot_array(callstack_size,false_object),this);
147
148                         std::vector<cell>::const_iterator
149                                 callstacks_begin = sample_callstacks.begin(),
150                                 c_from_iter = callstacks_begin + from_iter->callstack_begin,
151                                 c_from_iter_end = callstacks_begin + from_iter->callstack_end;
152                         cell c_to_i = 0;
153
154                         for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
155                                 set_array_nth(callstack.untagged(),c_to_i,*c_from_iter);
156
157                         set_array_nth(sample.untagged(),6,callstack.value());
158
159                         set_array_nth(samples_array.untagged(),to_i,sample.value());
160                 }
161                 ctx->push(samples_array.value());
162         }
163 }
164
165 void factor_vm::primitive_clear_samples()
166 {
167         clear_samples();
168 }
169
170 }