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