]> gitweb.factorcode.org Git - factor.git/blob - vm/sampling_profiler.cpp
vm: bottom_frame method for contexts
[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::start_sampling_profiler()
65 {
66         safepoint_sample_count = 0;
67         safepoint_gc_sample_count = 0;
68         samples.clear();
69         samples.reserve(10*FACTOR_PROFILE_SAMPLES_PER_SECOND);
70         sample_callstacks.reserve(100*FACTOR_PROFILE_SAMPLES_PER_SECOND);
71         sampling_profiler_p = true;
72         start_sampling_profiler_timer();
73 }
74
75 void factor_vm::end_sampling_profiler()
76 {
77         end_sampling_profiler_timer();
78         record_sample();
79         sampling_profiler_p = false;
80 }
81
82 void factor_vm::primitive_sampling_profiler()
83 {
84         set_sampling_profiler(to_boolean(ctx->pop()));
85 }
86
87
88 }