2011-10-28 18:42:33 -04:00
|
|
|
#include "master.hpp"
|
|
|
|
|
2013-05-11 22:28:05 -04:00
|
|
|
namespace factor {
|
|
|
|
|
2016-11-23 03:44:17 -05:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
2017-06-24 19:15:40 -04:00
|
|
|
profiling_sample profiling_sample::record_counts() volatile {
|
2013-05-11 22:28:05 -04:00
|
|
|
atomic::fence();
|
2017-06-24 19:15:40 -04:00
|
|
|
profiling_sample returned(sample_count, gc_sample_count,
|
|
|
|
jit_sample_count, foreign_sample_count,
|
|
|
|
foreign_thread_sample_count);
|
2013-05-11 22:28:05 -04:00
|
|
|
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;
|
2011-10-31 21:27:51 -04:00
|
|
|
}
|
|
|
|
|
2017-06-24 19:15:40 -04:00
|
|
|
void profiling_sample::clear_counts() volatile {
|
2013-05-11 22:28:05 -04:00
|
|
|
sample_count = 0;
|
|
|
|
gc_sample_count = 0;
|
|
|
|
jit_sample_count = 0;
|
|
|
|
foreign_sample_count = 0;
|
|
|
|
foreign_thread_sample_count = 0;
|
|
|
|
atomic::fence();
|
2011-10-31 21:27:51 -04:00
|
|
|
}
|
|
|
|
|
2020-08-14 13:27:54 -04:00
|
|
|
// Allocates memory
|
2013-05-11 22:28:05 -04:00
|
|
|
void factor_vm::record_sample(bool prolog_p) {
|
2017-06-24 19:15:40 -04:00
|
|
|
profiling_sample result = current_sample.record_counts();
|
|
|
|
if (result.empty()) {
|
2016-08-13 21:12:36 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-08-21 10:26:04 -04:00
|
|
|
// Appends the callstack, which is just a sequence of quotation or
|
|
|
|
// word references, to sample_callstacks.
|
2016-11-23 03:44:17 -05:00
|
|
|
cell callstacks_cell = special_objects[OBJ_SAMPLE_CALLSTACKS];
|
|
|
|
data_root<array> callstacks = data_root<array>(callstacks_cell, this);
|
|
|
|
cell begin = growarr_capacity(callstacks.untagged());
|
2013-05-11 22:28:05 -04:00
|
|
|
|
2016-05-04 19:57:52 -04:00
|
|
|
bool skip_p = prolog_p;
|
|
|
|
auto recorder = [&](cell frame_top, cell size, code_block* owner, cell addr) {
|
2018-07-13 01:48:58 -04:00
|
|
|
(void)frame_top;
|
|
|
|
(void)size;
|
|
|
|
(void)addr;
|
2013-05-11 22:28:05 -04:00
|
|
|
if (skip_p)
|
|
|
|
skip_p = false;
|
2016-11-23 03:44:17 -05:00
|
|
|
else {
|
|
|
|
growarr_add(callstacks.untagged(), owner->owner);
|
|
|
|
}
|
2016-05-04 19:57:52 -04:00
|
|
|
};
|
2013-05-11 22:28:05 -04:00
|
|
|
iterate_callstack(ctx, recorder);
|
2016-11-23 03:44:17 -05:00
|
|
|
cell end = growarr_capacity(callstacks.untagged());
|
2011-10-29 17:41:48 -04:00
|
|
|
|
2016-08-21 10:26:04 -04:00
|
|
|
// Add the sample.
|
2017-06-24 19:15:40 -04:00
|
|
|
result.thread = special_objects[OBJ_CURRENT_THREAD];
|
|
|
|
result.callstack_begin = begin;
|
|
|
|
result.callstack_end = end;
|
|
|
|
samples.push_back(result);
|
2011-10-29 17:41:48 -04:00
|
|
|
}
|
|
|
|
|
2020-08-14 13:27:54 -04:00
|
|
|
// Allocates memory
|
2020-08-14 13:40:54 -04:00
|
|
|
void factor_vm::set_profiling(fixnum rate) {
|
2020-08-14 13:27:18 -04:00
|
|
|
bool running_p = atomic::load(&sampling_profiler_p);
|
2016-05-18 19:25:53 -04:00
|
|
|
if (rate > 0 && !running_p)
|
2013-05-11 22:28:05 -04:00
|
|
|
start_sampling_profiler(rate);
|
2016-05-18 19:25:53 -04:00
|
|
|
else if (rate == 0 && running_p)
|
2013-05-11 22:28:05 -04:00
|
|
|
end_sampling_profiler();
|
2011-10-28 18:42:33 -04:00
|
|
|
}
|
|
|
|
|
2020-08-14 13:27:54 -04:00
|
|
|
// Allocates memory
|
2013-05-11 22:28:05 -04:00
|
|
|
void factor_vm::start_sampling_profiler(fixnum rate) {
|
2016-11-23 03:44:17 -05:00
|
|
|
special_objects[OBJ_SAMPLE_CALLSTACKS] = tag<array>(allot_growarr());
|
2013-05-11 22:28:05 -04:00
|
|
|
samples_per_second = rate;
|
2017-06-24 19:15:40 -04:00
|
|
|
current_sample.clear_counts();
|
2016-08-05 08:44:37 -04:00
|
|
|
// Release the memory consumed by collecting samples.
|
2016-05-06 13:56:13 -04:00
|
|
|
samples.clear();
|
2016-05-05 08:22:57 -04:00
|
|
|
samples.shrink_to_fit();
|
2013-05-11 22:28:05 -04:00
|
|
|
samples.reserve(10 * rate);
|
|
|
|
atomic::store(&sampling_profiler_p, true);
|
|
|
|
start_sampling_profiler_timer();
|
2011-10-28 18:42:33 -04:00
|
|
|
}
|
|
|
|
|
2013-05-11 22:28:05 -04:00
|
|
|
void factor_vm::end_sampling_profiler() {
|
|
|
|
atomic::store(&sampling_profiler_p, false);
|
|
|
|
end_sampling_profiler_timer();
|
|
|
|
record_sample(false);
|
2011-10-28 18:42:33 -04:00
|
|
|
}
|
|
|
|
|
2020-08-14 13:27:54 -04:00
|
|
|
// Allocates memory
|
2020-08-14 13:40:54 -04:00
|
|
|
void factor_vm::primitive_set_profiling() {
|
|
|
|
set_profiling(to_fixnum(ctx->pop()));
|
2011-10-28 18:42:33 -04:00
|
|
|
}
|
|
|
|
|
2016-08-21 10:26:04 -04:00
|
|
|
// Allocates memory
|
2013-05-11 22:28:05 -04:00
|
|
|
void factor_vm::primitive_get_samples() {
|
|
|
|
if (atomic::load(&sampling_profiler_p) || samples.empty()) {
|
|
|
|
ctx->push(false_object);
|
2016-11-23 03:44:17 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
data_root<array> samples_array(allot_array(samples.size(), false_object),
|
2013-05-11 22:28:05 -04:00
|
|
|
this);
|
2016-11-23 03:44:17 -05:00
|
|
|
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,
|
2017-06-24 19:15:40 -04:00
|
|
|
tag_fixnum(from_iter->sample_count));
|
2016-11-23 03:44:17 -05:00
|
|
|
set_array_nth(sample.untagged(), 1,
|
2017-06-24 19:15:40 -04:00
|
|
|
tag_fixnum(from_iter->gc_sample_count));
|
2016-11-23 03:44:17 -05:00
|
|
|
set_array_nth(sample.untagged(), 2,
|
2017-06-24 19:15:40 -04:00
|
|
|
tag_fixnum(from_iter->jit_sample_count));
|
2016-11-23 03:44:17 -05:00
|
|
|
set_array_nth(sample.untagged(), 3,
|
2017-06-24 19:15:40 -04:00
|
|
|
tag_fixnum(from_iter->foreign_sample_count));
|
2016-11-23 03:44:17 -05:00
|
|
|
set_array_nth(sample.untagged(), 4,
|
2017-06-24 19:15:40 -04:00
|
|
|
tag_fixnum(from_iter->foreign_thread_sample_count));
|
2016-11-23 03:44:17 -05:00
|
|
|
|
|
|
|
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);
|
2013-05-11 22:28:05 -04:00
|
|
|
}
|
2016-11-23 03:44:17 -05:00
|
|
|
set_array_nth(sample.untagged(), 6, callstack.value());
|
|
|
|
set_array_nth(samples_array.untagged(), to_i, sample.value());
|
2013-05-11 22:28:05 -04:00
|
|
|
}
|
2016-11-23 03:44:17 -05:00
|
|
|
ctx->push(samples_array.value());
|
2011-10-31 01:30:55 -04:00
|
|
|
}
|
|
|
|
|
2011-10-28 18:42:33 -04:00
|
|
|
}
|