]> gitweb.factorcode.org Git - factor.git/blob - vm/sampling_profiler.cpp
vm: groundwork for sampling profiler
[factor.git] / vm / sampling_profiler.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 void factor_vm::record_sample()
7 {
8         cell recorded_sample_count;
9         cell recorded_gc_sample_count;
10
11         recorded_sample_count = safepoint_sample_count;
12         recorded_gc_sample_count = safepoint_gc_sample_count;
13         if (recorded_sample_count == 0 && recorded_gc_sample_count == 0)
14                 return;
15
16         /* Another sample signal could be raised while we record these counts */
17         FACTOR_ATOMIC_SUB(&safepoint_sample_count, recorded_sample_count);
18         FACTOR_ATOMIC_SUB(&safepoint_gc_sample_count, recorded_gc_sample_count);
19
20         samples.push_back(profiling_sample(
21                 recorded_sample_count,
22                 recorded_gc_sample_count,
23                 ctx,
24                 capture_callstack(ctx)
25         ));
26 }
27
28 void factor_vm::set_sampling_profiler(bool sampling_p)
29 {
30         if (sampling_p == sampling_profiler_p)
31                 return;
32         
33         if (sampling_p)
34                 start_sampling_profiler();
35         else
36                 end_sampling_profiler();
37 }
38
39 void factor_vm::start_sampling_profiler()
40 {
41         safepoint_sample_count = 0;
42         safepoint_gc_sample_count = 0;
43         samples.clear();
44         samples.reserve(10*FACTOR_PROFILE_SAMPLES_PER_SECOND);
45         sampling_profiler_p = true;
46         start_sampling_profiler_timer();
47 }
48
49 void factor_vm::end_sampling_profiler()
50 {
51         end_sampling_profiler_timer();
52         record_sample();
53         sampling_profiler_p = false;
54 }
55
56 void factor_vm::primitive_sampling_profiler()
57 {
58         set_sampling_profiler(to_boolean(ctx->pop()));
59 }
60
61
62 }