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