]> gitweb.factorcode.org Git - factor.git/blob - vm/sampling_profiler.cpp
vm: fix data roots in get-samples primitive
[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                 foreign_sample_count,
13                 foreign_thread_sample_count);
14         atomic::subtract(&sample_count, returned.sample_count);
15         atomic::subtract(&gc_sample_count, returned.gc_sample_count);
16         atomic::subtract(&foreign_sample_count, returned.foreign_sample_count);
17         atomic::subtract(&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         atomic::fence();
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(fixnum rate)
61 {
62         bool sampling_p = !!rate;
63         if (sampling_p == sampling_profiler_p)
64                 return;
65         
66         if (sampling_p)
67                 start_sampling_profiler(rate);
68         else
69                 end_sampling_profiler();
70 }
71
72 void factor_vm::clear_samples()
73 {
74         // Swapping into temporaries releases the vector's allocated storage,
75         // whereas clear() would leave the allocation as-is
76         std::vector<profiling_sample> sample_graveyard;
77         std::vector<code_block*> sample_callstack_graveyard;
78         samples.swap(sample_graveyard);
79         sample_callstacks.swap(sample_callstack_graveyard);
80 }
81
82 void factor_vm::start_sampling_profiler(fixnum rate)
83 {
84         samples_per_second = rate;
85         safepoint_sample_counts.clear();
86         clear_samples();
87         samples.reserve(10*rate);
88         sample_callstacks.reserve(100*rate);
89         sampling_profiler_p = true;
90         start_sampling_profiler_timer();
91 }
92
93 void factor_vm::end_sampling_profiler()
94 {
95         sampling_profiler_p = false;
96         atomic::fence();
97         end_sampling_profiler_timer();
98         record_sample();
99 }
100
101 void factor_vm::primitive_sampling_profiler()
102 {
103         set_sampling_profiler(to_fixnum(ctx->pop()));
104 }
105
106 void factor_vm::primitive_get_samples()
107 {
108         if (sampling_profiler_p || samples.empty()) {
109                 ctx->push(false_object);
110         } else {
111                 data_root<array> samples_array(allot_array(samples.size(), false_object),this);
112                 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
113                 cell to_i = 0;
114
115                 for (; from_iter != samples.end(); ++from_iter, ++to_i)
116                 {
117                         data_root<array> sample(allot_array(6, false_object),this);
118
119                         set_array_nth(sample.untagged(),0,tag_fixnum(from_iter->counts.sample_count));
120                         set_array_nth(sample.untagged(),1,tag_fixnum(from_iter->counts.gc_sample_count));
121                         set_array_nth(sample.untagged(),2,tag_fixnum(from_iter->counts.foreign_sample_count));
122                         set_array_nth(sample.untagged(),3,tag_fixnum(from_iter->counts.foreign_thread_sample_count));
123
124                         cell ctx = allot_alien((void*)from_iter->ctx);
125                         set_array_nth(sample.untagged(),4,ctx);
126
127                         cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
128                         data_root<array> callstack(allot_array(callstack_size,false_object),this);
129
130                         std::vector<code_block*>::const_iterator
131                                 callstacks_begin = sample_callstacks.begin(),
132                                 c_from_iter = callstacks_begin + from_iter->callstack_begin,
133                                 c_from_iter_end = callstacks_begin + 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)->owner);
138
139                         set_array_nth(sample.untagged(),5,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 void factor_vm::primitive_clear_samples()
148 {
149         clear_samples();
150 }
151
152 void factor_vm::enqueue_safepoint_sample(cell samples, cell pc, bool foreign_thread_p)
153 {
154         if (atomic::fence(), sampling_profiler_p)
155         {
156                 atomic::add(&safepoint_sample_counts.sample_count, samples);
157                 if (foreign_thread_p)
158                         atomic::add(&safepoint_sample_counts.foreign_thread_sample_count, samples);
159                 else {
160                         if (atomic::fence(), current_gc)
161                                 atomic::add(&safepoint_sample_counts.gc_sample_count, samples);
162                         if (pc != 0 && !code->seg->in_segment_p(pc))
163                                 atomic::add(&safepoint_sample_counts.foreign_sample_count, samples);
164                 }
165                 code->guard_safepoint();
166         }
167 }
168
169 }