]> gitweb.factorcode.org Git - factor.git/blob - vm/sampling_profiler.cpp
VM: storing some sample data in the heap (#337)
[factor.git] / vm / sampling_profiler.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 // This is like the growable_array class, except the whole of it
6 // exists on the Factor heap. growarr = growable array.
7 static cell growarr_capacity(array *growarr) {
8   return untag_fixnum(growarr->data()[0]);
9 }
10
11 static cell growarr_nth(array *growarr, cell slot) {
12   return array_nth(untag<array>(growarr->data()[1]), slot);
13 }
14
15 // Allocates memory
16 array* factor_vm::allot_growarr() {
17   data_root<array> contents(allot_array(10, false_object), this);
18   array *growarr = allot_uninitialized_array<array>(2);
19   set_array_nth(growarr, 0, tag_fixnum(0));
20   set_array_nth(growarr, 1, contents.value());
21   return growarr;
22 }
23
24 // Allocates memory
25 void factor_vm::growarr_add(array *growarr_, cell elt_) {
26   data_root<array> growarr(growarr_, this);
27   data_root<object> elt(elt_, this);
28   data_root<array> contents(growarr.untagged()->data()[1], this);
29
30   cell count = growarr_capacity(growarr.untagged());
31   if (count == array_capacity(contents.untagged())) {
32     contents.set_untagged(reallot_array(contents.untagged(), 2 * count));
33     set_array_nth(growarr.untagged(), 1, contents.value());
34   }
35   set_array_nth(contents.untagged(), count, elt.value());
36   set_array_nth(growarr.untagged(), 0, tag_fixnum(count + 1));
37 }
38
39 profiling_sample_count profiling_sample_count::record_counts() volatile {
40   atomic::fence();
41   profiling_sample_count returned(sample_count, gc_sample_count,
42                                   jit_sample_count, foreign_sample_count,
43                                   foreign_thread_sample_count);
44   atomic::fetch_subtract(&sample_count, returned.sample_count);
45   atomic::fetch_subtract(&gc_sample_count, returned.gc_sample_count);
46   atomic::fetch_subtract(&jit_sample_count, returned.jit_sample_count);
47   atomic::fetch_subtract(&foreign_sample_count, returned.foreign_sample_count);
48   atomic::fetch_subtract(&foreign_thread_sample_count,
49                          returned.foreign_thread_sample_count);
50   return returned;
51 }
52
53 void profiling_sample_count::clear() volatile {
54   sample_count = 0;
55   gc_sample_count = 0;
56   jit_sample_count = 0;
57   foreign_sample_count = 0;
58   foreign_thread_sample_count = 0;
59   atomic::fence();
60 }
61
62 profiling_sample::profiling_sample(profiling_sample_count const& counts, cell thread,
63                                    cell callstack_begin, cell callstack_end)
64     : counts(counts), thread(thread),
65       callstack_begin(callstack_begin),
66       callstack_end(callstack_end) { }
67
68 // Allocates memory (sample_callstacks2->add)
69 void factor_vm::record_sample(bool prolog_p) {
70   profiling_sample_count counts = sample_counts.record_counts();
71   if (counts.empty()) {
72     return;
73   }
74   // Appends the callstack, which is just a sequence of quotation or
75   // word references, to sample_callstacks.
76   cell callstacks_cell = special_objects[OBJ_SAMPLE_CALLSTACKS];
77   data_root<array> callstacks = data_root<array>(callstacks_cell, this);
78   cell begin = growarr_capacity(callstacks.untagged());
79
80   bool skip_p = prolog_p;
81   auto recorder = [&](cell frame_top, cell size, code_block* owner, cell addr) {
82     if (skip_p)
83       skip_p = false;
84     else {
85       growarr_add(callstacks.untagged(), owner->owner);
86     }
87   };
88   iterate_callstack(ctx, recorder);
89   cell end = growarr_capacity(callstacks.untagged());
90
91   // Add the sample.
92   cell thread = special_objects[OBJ_CURRENT_THREAD];
93   samples.push_back(profiling_sample(counts, thread, begin, end));
94 }
95
96 void factor_vm::set_sampling_profiler(fixnum rate) {
97   bool running_p = (atomic::load(&sampling_profiler_p) != 0);
98   if (rate > 0 && !running_p)
99     start_sampling_profiler(rate);
100   else if (rate == 0 && running_p)
101     end_sampling_profiler();
102 }
103
104 void factor_vm::start_sampling_profiler(fixnum rate) {
105   special_objects[OBJ_SAMPLE_CALLSTACKS] = tag<array>(allot_growarr());
106   samples_per_second = rate;
107   sample_counts.clear();
108   // Release the memory consumed by collecting samples.
109   samples.clear();
110   samples.shrink_to_fit();
111   samples.reserve(10 * rate);
112   atomic::store(&sampling_profiler_p, true);
113   start_sampling_profiler_timer();
114 }
115
116 void factor_vm::end_sampling_profiler() {
117   atomic::store(&sampling_profiler_p, false);
118   end_sampling_profiler_timer();
119   record_sample(false);
120 }
121
122 void factor_vm::primitive_sampling_profiler() {
123   set_sampling_profiler(to_fixnum(ctx->pop()));
124 }
125
126 // Allocates memory
127 void factor_vm::primitive_get_samples() {
128   if (atomic::load(&sampling_profiler_p) || samples.empty()) {
129     ctx->push(false_object);
130     return;
131   }
132   data_root<array> samples_array(allot_array(samples.size(), false_object),
133                                  this);
134   std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
135   cell to_i = 0;
136
137   cell callstacks_cell = special_objects[OBJ_SAMPLE_CALLSTACKS];
138   data_root<array> callstacks = data_root<array>(callstacks_cell, this);
139
140   for (; from_iter != samples.end(); ++from_iter, ++to_i) {
141     data_root<array> sample(allot_array(7, false_object), this);
142
143     set_array_nth(sample.untagged(), 0,
144                   tag_fixnum(from_iter->counts.sample_count));
145     set_array_nth(sample.untagged(), 1,
146                   tag_fixnum(from_iter->counts.gc_sample_count));
147     set_array_nth(sample.untagged(), 2,
148                   tag_fixnum(from_iter->counts.jit_sample_count));
149     set_array_nth(sample.untagged(), 3,
150                   tag_fixnum(from_iter->counts.foreign_sample_count));
151     set_array_nth(sample.untagged(), 4,
152                   tag_fixnum(from_iter->counts.foreign_thread_sample_count));
153
154     set_array_nth(sample.untagged(), 5, from_iter->thread);
155
156     cell callstack_size =
157         from_iter->callstack_end - from_iter->callstack_begin;
158     data_root<array> callstack(allot_array(callstack_size, false_object),
159                                this);
160
161     for (cell i = 0; i < callstack_size; i++) {
162       cell block_owner = growarr_nth(callstacks.untagged(),
163                                      from_iter->callstack_begin + i);
164       set_array_nth(callstack.untagged(), i, block_owner);
165     }
166     set_array_nth(sample.untagged(), 6, callstack.value());
167     set_array_nth(samples_array.untagged(), to_i, sample.value());
168   }
169   ctx->push(samples_array.value());
170 }
171
172 }