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]);
11 static cell growarr_nth(array *growarr, cell slot) {
12 return array_nth(untag<array>(growarr->data()[1]), slot);
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());
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);
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());
35 set_array_nth(contents.untagged(), count, elt.value());
36 set_array_nth(growarr.untagged(), 0, tag_fixnum(count + 1));
39 profiling_sample profiling_sample::record_counts() volatile {
41 profiling_sample 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);
53 void profiling_sample::clear_counts() volatile {
57 foreign_sample_count = 0;
58 foreign_thread_sample_count = 0;
62 // Allocates memory (sample_callstacks2->add)
63 void factor_vm::record_sample(bool prolog_p) {
64 profiling_sample result = current_sample.record_counts();
68 // Appends the callstack, which is just a sequence of quotation or
69 // word references, to sample_callstacks.
70 cell callstacks_cell = special_objects[OBJ_SAMPLE_CALLSTACKS];
71 data_root<array> callstacks = data_root<array>(callstacks_cell, this);
72 cell begin = growarr_capacity(callstacks.untagged());
74 bool skip_p = prolog_p;
75 auto recorder = [&](cell frame_top, cell size, code_block* owner, cell addr) {
82 growarr_add(callstacks.untagged(), owner->owner);
85 iterate_callstack(ctx, recorder);
86 cell end = growarr_capacity(callstacks.untagged());
89 result.thread = special_objects[OBJ_CURRENT_THREAD];
90 result.callstack_begin = begin;
91 result.callstack_end = end;
92 samples.push_back(result);
95 void factor_vm::set_sampling_profiler(fixnum rate) {
96 bool running_p = (atomic::load(&sampling_profiler_p) != 0);
97 if (rate > 0 && !running_p)
98 start_sampling_profiler(rate);
99 else if (rate == 0 && running_p)
100 end_sampling_profiler();
103 void factor_vm::start_sampling_profiler(fixnum rate) {
104 special_objects[OBJ_SAMPLE_CALLSTACKS] = tag<array>(allot_growarr());
105 samples_per_second = rate;
106 current_sample.clear_counts();
107 // Release the memory consumed by collecting samples.
109 samples.shrink_to_fit();
110 samples.reserve(10 * rate);
111 atomic::store(&sampling_profiler_p, true);
112 start_sampling_profiler_timer();
115 void factor_vm::end_sampling_profiler() {
116 atomic::store(&sampling_profiler_p, false);
117 end_sampling_profiler_timer();
118 record_sample(false);
121 void factor_vm::primitive_sampling_profiler() {
122 set_sampling_profiler(to_fixnum(ctx->pop()));
126 void factor_vm::primitive_get_samples() {
127 if (atomic::load(&sampling_profiler_p) || samples.empty()) {
128 ctx->push(false_object);
131 data_root<array> samples_array(allot_array(samples.size(), false_object),
133 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
136 cell callstacks_cell = special_objects[OBJ_SAMPLE_CALLSTACKS];
137 data_root<array> callstacks = data_root<array>(callstacks_cell, this);
139 for (; from_iter != samples.end(); ++from_iter, ++to_i) {
140 data_root<array> sample(allot_array(7, false_object), this);
142 set_array_nth(sample.untagged(), 0,
143 tag_fixnum(from_iter->sample_count));
144 set_array_nth(sample.untagged(), 1,
145 tag_fixnum(from_iter->gc_sample_count));
146 set_array_nth(sample.untagged(), 2,
147 tag_fixnum(from_iter->jit_sample_count));
148 set_array_nth(sample.untagged(), 3,
149 tag_fixnum(from_iter->foreign_sample_count));
150 set_array_nth(sample.untagged(), 4,
151 tag_fixnum(from_iter->foreign_thread_sample_count));
153 set_array_nth(sample.untagged(), 5, from_iter->thread);
155 cell callstack_size =
156 from_iter->callstack_end - from_iter->callstack_begin;
157 data_root<array> callstack(allot_array(callstack_size, false_object),
160 for (cell i = 0; i < callstack_size; i++) {
161 cell block_owner = growarr_nth(callstacks.untagged(),
162 from_iter->callstack_begin + i);
163 set_array_nth(callstack.untagged(), i, block_owner);
165 set_array_nth(sample.untagged(), 6, callstack.value());
166 set_array_nth(samples_array.untagged(), to_i, sample.value());
168 ctx->push(samples_array.value());