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