VM: Refactor sampling_profiler.cpp/hpp to Factor style
parent
2af4b6a7e6
commit
b2929acff6
|
@ -1,172 +1,164 @@
|
||||||
#include "master.hpp"
|
#include "master.hpp"
|
||||||
|
|
||||||
namespace factor
|
namespace factor {
|
||||||
{
|
|
||||||
|
|
||||||
profiling_sample_count profiling_sample_count::record_counts() volatile
|
profiling_sample_count profiling_sample_count::record_counts() volatile {
|
||||||
{
|
atomic::fence();
|
||||||
atomic::fence();
|
profiling_sample_count returned(sample_count, gc_sample_count,
|
||||||
profiling_sample_count returned(
|
jit_sample_count, foreign_sample_count,
|
||||||
sample_count,
|
foreign_thread_sample_count);
|
||||||
gc_sample_count,
|
atomic::fetch_subtract(&sample_count, returned.sample_count);
|
||||||
jit_sample_count,
|
atomic::fetch_subtract(&gc_sample_count, returned.gc_sample_count);
|
||||||
foreign_sample_count,
|
atomic::fetch_subtract(&jit_sample_count, returned.jit_sample_count);
|
||||||
foreign_thread_sample_count);
|
atomic::fetch_subtract(&foreign_sample_count, returned.foreign_sample_count);
|
||||||
atomic::fetch_subtract(&sample_count, returned.sample_count);
|
atomic::fetch_subtract(&foreign_thread_sample_count,
|
||||||
atomic::fetch_subtract(&gc_sample_count, returned.gc_sample_count);
|
returned.foreign_thread_sample_count);
|
||||||
atomic::fetch_subtract(&jit_sample_count, returned.jit_sample_count);
|
return returned;
|
||||||
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_count::clear() volatile
|
void profiling_sample_count::clear() volatile {
|
||||||
{
|
sample_count = 0;
|
||||||
sample_count = 0;
|
gc_sample_count = 0;
|
||||||
gc_sample_count = 0;
|
jit_sample_count = 0;
|
||||||
jit_sample_count = 0;
|
foreign_sample_count = 0;
|
||||||
foreign_sample_count = 0;
|
foreign_thread_sample_count = 0;
|
||||||
foreign_thread_sample_count = 0;
|
atomic::fence();
|
||||||
atomic::fence();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
profiling_sample::profiling_sample(factor_vm *vm,
|
profiling_sample::profiling_sample(factor_vm* vm, bool prolog_p,
|
||||||
bool prolog_p,
|
profiling_sample_count const& counts,
|
||||||
profiling_sample_count const &counts,
|
cell thread)
|
||||||
cell thread)
|
: counts(counts), thread(thread) {
|
||||||
:
|
vm->record_callstack_sample(&callstack_begin, &callstack_end, prolog_p);
|
||||||
counts(counts),
|
|
||||||
thread(thread)
|
|
||||||
{
|
|
||||||
vm->record_callstack_sample(&callstack_begin, &callstack_end, prolog_p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::record_sample(bool prolog_p)
|
void factor_vm::record_sample(bool prolog_p) {
|
||||||
{
|
profiling_sample_count counts = safepoint.sample_counts.record_counts();
|
||||||
profiling_sample_count counts = safepoint.sample_counts.record_counts();
|
if (!counts.empty())
|
||||||
if (!counts.empty())
|
samples.push_back(profiling_sample(this, prolog_p, counts,
|
||||||
samples.push_back(profiling_sample(this, prolog_p,
|
special_objects[OBJ_CURRENT_THREAD]));
|
||||||
counts, special_objects[OBJ_CURRENT_THREAD]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct record_callstack_sample_iterator {
|
struct record_callstack_sample_iterator {
|
||||||
std::vector<cell> *sample_callstacks;
|
std::vector<cell>* sample_callstacks;
|
||||||
bool skip_p;
|
bool skip_p;
|
||||||
|
|
||||||
record_callstack_sample_iterator(std::vector<cell> *sample_callstacks, bool prolog_p)
|
record_callstack_sample_iterator(std::vector<cell>* sample_callstacks,
|
||||||
: sample_callstacks(sample_callstacks), skip_p(prolog_p) {}
|
bool prolog_p)
|
||||||
|
: sample_callstacks(sample_callstacks), skip_p(prolog_p) {}
|
||||||
|
|
||||||
void operator()(void *frame_top, cell frame_size, code_block *owner, void *addr)
|
void operator()(void* frame_top, cell frame_size, code_block* owner,
|
||||||
{
|
void* addr) {
|
||||||
if (skip_p)
|
if (skip_p)
|
||||||
skip_p = false;
|
skip_p = false;
|
||||||
else
|
else
|
||||||
sample_callstacks->push_back(owner->owner);
|
sample_callstacks->push_back(owner->owner);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void factor_vm::record_callstack_sample(cell *begin, cell *end, bool prolog_p)
|
void factor_vm::record_callstack_sample(cell* begin, cell* end, bool prolog_p) {
|
||||||
{
|
*begin = sample_callstacks.size();
|
||||||
*begin = sample_callstacks.size();
|
|
||||||
|
|
||||||
record_callstack_sample_iterator recorder(&sample_callstacks, prolog_p);
|
record_callstack_sample_iterator recorder(&sample_callstacks, prolog_p);
|
||||||
iterate_callstack(ctx, recorder);
|
iterate_callstack(ctx, recorder);
|
||||||
|
|
||||||
*end = sample_callstacks.size();
|
*end = sample_callstacks.size();
|
||||||
|
|
||||||
std::reverse(sample_callstacks.begin() + *begin, sample_callstacks.end());
|
std::reverse(sample_callstacks.begin() + *begin, sample_callstacks.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::set_sampling_profiler(fixnum rate)
|
void factor_vm::set_sampling_profiler(fixnum rate) {
|
||||||
{
|
bool sampling_p = !!rate;
|
||||||
bool sampling_p = !!rate;
|
if (sampling_p == !!atomic::load(&sampling_profiler_p))
|
||||||
if (sampling_p == !!atomic::load(&sampling_profiler_p))
|
return;
|
||||||
return;
|
|
||||||
|
|
||||||
if (sampling_p)
|
if (sampling_p)
|
||||||
start_sampling_profiler(rate);
|
start_sampling_profiler(rate);
|
||||||
else
|
else
|
||||||
end_sampling_profiler();
|
end_sampling_profiler();
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::clear_samples()
|
void factor_vm::clear_samples() {
|
||||||
{
|
// Swapping into temporaries releases the vector's allocated storage,
|
||||||
// Swapping into temporaries releases the vector's allocated storage,
|
// whereas clear() would leave the allocation as-is
|
||||||
// whereas clear() would leave the allocation as-is
|
std::vector<profiling_sample> sample_graveyard;
|
||||||
std::vector<profiling_sample> sample_graveyard;
|
std::vector<cell> sample_callstack_graveyard;
|
||||||
std::vector<cell> sample_callstack_graveyard;
|
samples.swap(sample_graveyard);
|
||||||
samples.swap(sample_graveyard);
|
sample_callstacks.swap(sample_callstack_graveyard);
|
||||||
sample_callstacks.swap(sample_callstack_graveyard);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::start_sampling_profiler(fixnum rate)
|
void factor_vm::start_sampling_profiler(fixnum rate) {
|
||||||
{
|
samples_per_second = rate;
|
||||||
samples_per_second = rate;
|
safepoint.sample_counts.clear();
|
||||||
safepoint.sample_counts.clear();
|
clear_samples();
|
||||||
clear_samples();
|
samples.reserve(10 * rate);
|
||||||
samples.reserve(10*rate);
|
sample_callstacks.reserve(100 * rate);
|
||||||
sample_callstacks.reserve(100*rate);
|
atomic::store(&sampling_profiler_p, true);
|
||||||
atomic::store(&sampling_profiler_p, true);
|
start_sampling_profiler_timer();
|
||||||
start_sampling_profiler_timer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::end_sampling_profiler()
|
void factor_vm::end_sampling_profiler() {
|
||||||
{
|
atomic::store(&sampling_profiler_p, false);
|
||||||
atomic::store(&sampling_profiler_p, false);
|
end_sampling_profiler_timer();
|
||||||
end_sampling_profiler_timer();
|
record_sample(false);
|
||||||
record_sample(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::primitive_sampling_profiler()
|
void factor_vm::primitive_sampling_profiler() {
|
||||||
{
|
set_sampling_profiler(to_fixnum(ctx->pop()));
|
||||||
set_sampling_profiler(to_fixnum(ctx->pop()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void factor_vm::primitive_get_samples()
|
void factor_vm::primitive_get_samples() {
|
||||||
{
|
if (atomic::load(&sampling_profiler_p) || samples.empty()) {
|
||||||
if (atomic::load(&sampling_profiler_p) || samples.empty()) {
|
ctx->push(false_object);
|
||||||
ctx->push(false_object);
|
} else {
|
||||||
} else {
|
data_root<array> samples_array(allot_array(samples.size(), false_object),
|
||||||
data_root<array> samples_array(allot_array(samples.size(), false_object),this);
|
this);
|
||||||
std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
|
std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
|
||||||
cell to_i = 0;
|
cell to_i = 0;
|
||||||
|
|
||||||
for (; from_iter != samples.end(); ++from_iter, ++to_i)
|
for (; from_iter != samples.end(); ++from_iter, ++to_i) {
|
||||||
{
|
data_root<array> sample(allot_array(7, false_object), this);
|
||||||
data_root<array> sample(allot_array(7, false_object),this);
|
|
||||||
|
|
||||||
set_array_nth(sample.untagged(),0,tag_fixnum(from_iter->counts.sample_count));
|
set_array_nth(sample.untagged(), 0,
|
||||||
set_array_nth(sample.untagged(),1,tag_fixnum(from_iter->counts.gc_sample_count));
|
tag_fixnum(from_iter->counts.sample_count));
|
||||||
set_array_nth(sample.untagged(),2,tag_fixnum(from_iter->counts.jit_sample_count));
|
set_array_nth(sample.untagged(), 1,
|
||||||
set_array_nth(sample.untagged(),3,tag_fixnum(from_iter->counts.foreign_sample_count));
|
tag_fixnum(from_iter->counts.gc_sample_count));
|
||||||
set_array_nth(sample.untagged(),4,tag_fixnum(from_iter->counts.foreign_thread_sample_count));
|
set_array_nth(sample.untagged(), 2,
|
||||||
|
tag_fixnum(from_iter->counts.jit_sample_count));
|
||||||
|
set_array_nth(sample.untagged(), 3,
|
||||||
|
tag_fixnum(from_iter->counts.foreign_sample_count));
|
||||||
|
set_array_nth(sample.untagged(), 4,
|
||||||
|
tag_fixnum(from_iter->counts.foreign_thread_sample_count));
|
||||||
|
|
||||||
set_array_nth(sample.untagged(),5,from_iter->thread);
|
set_array_nth(sample.untagged(), 5, from_iter->thread);
|
||||||
|
|
||||||
cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
|
cell callstack_size =
|
||||||
data_root<array> callstack(allot_array(callstack_size,false_object),this);
|
from_iter->callstack_end - from_iter->callstack_begin;
|
||||||
|
data_root<array> callstack(allot_array(callstack_size, false_object),
|
||||||
|
this);
|
||||||
|
|
||||||
std::vector<cell>::const_iterator
|
std::vector<cell>::const_iterator callstacks_begin =
|
||||||
callstacks_begin = sample_callstacks.begin(),
|
sample_callstacks.begin(),
|
||||||
c_from_iter = callstacks_begin + from_iter->callstack_begin,
|
c_from_iter =
|
||||||
c_from_iter_end = callstacks_begin + from_iter->callstack_end;
|
callstacks_begin +
|
||||||
cell c_to_i = 0;
|
from_iter->callstack_begin,
|
||||||
|
c_from_iter_end =
|
||||||
|
callstacks_begin +
|
||||||
|
from_iter->callstack_end;
|
||||||
|
cell c_to_i = 0;
|
||||||
|
|
||||||
for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
|
for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
|
||||||
set_array_nth(callstack.untagged(),c_to_i,*c_from_iter);
|
set_array_nth(callstack.untagged(), c_to_i, *c_from_iter);
|
||||||
|
|
||||||
set_array_nth(sample.untagged(),6,callstack.value());
|
set_array_nth(sample.untagged(), 6, callstack.value());
|
||||||
|
|
||||||
set_array_nth(samples_array.untagged(),to_i,sample.value());
|
set_array_nth(samples_array.untagged(), to_i, sample.value());
|
||||||
}
|
}
|
||||||
ctx->push(samples_array.value());
|
ctx->push(samples_array.value());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void factor_vm::primitive_clear_samples()
|
void factor_vm::primitive_clear_samples() { clear_samples(); }
|
||||||
{
|
|
||||||
clear_samples();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,64 +1,54 @@
|
||||||
namespace factor
|
namespace factor {
|
||||||
{
|
|
||||||
|
|
||||||
struct profiling_sample_count
|
struct profiling_sample_count {
|
||||||
{
|
// Number of samples taken before the safepoint that recorded the sample
|
||||||
// Number of samples taken before the safepoint that recorded the sample
|
fixnum sample_count;
|
||||||
fixnum sample_count;
|
// Number of samples taken during GC
|
||||||
// Number of samples taken during GC
|
fixnum gc_sample_count;
|
||||||
fixnum gc_sample_count;
|
// Number of samples taken during unoptimized compiler
|
||||||
// Number of samples taken during unoptimized compiler
|
fixnum jit_sample_count;
|
||||||
fixnum jit_sample_count;
|
// Number of samples taken during foreign code execution
|
||||||
// Number of samples taken during foreign code execution
|
fixnum foreign_sample_count;
|
||||||
fixnum foreign_sample_count;
|
// Number of samples taken during code execution in non-Factor threads
|
||||||
// Number of samples taken during code execution in non-Factor threads
|
fixnum foreign_thread_sample_count;
|
||||||
fixnum foreign_thread_sample_count;
|
|
||||||
|
|
||||||
profiling_sample_count() :
|
profiling_sample_count()
|
||||||
sample_count(0),
|
: sample_count(0),
|
||||||
gc_sample_count(0),
|
gc_sample_count(0),
|
||||||
jit_sample_count(0),
|
jit_sample_count(0),
|
||||||
foreign_sample_count(0),
|
foreign_sample_count(0),
|
||||||
foreign_thread_sample_count(0) {}
|
foreign_thread_sample_count(0) {}
|
||||||
|
|
||||||
profiling_sample_count(fixnum sample_count,
|
profiling_sample_count(fixnum sample_count, fixnum gc_sample_count,
|
||||||
fixnum gc_sample_count,
|
fixnum jit_sample_count, fixnum foreign_sample_count,
|
||||||
fixnum jit_sample_count,
|
fixnum foreign_thread_sample_count)
|
||||||
fixnum foreign_sample_count,
|
: sample_count(sample_count),
|
||||||
fixnum foreign_thread_sample_count) :
|
gc_sample_count(gc_sample_count),
|
||||||
sample_count(sample_count),
|
jit_sample_count(jit_sample_count),
|
||||||
gc_sample_count(gc_sample_count),
|
foreign_sample_count(foreign_sample_count),
|
||||||
jit_sample_count(jit_sample_count),
|
foreign_thread_sample_count(foreign_thread_sample_count) {}
|
||||||
foreign_sample_count(foreign_sample_count),
|
|
||||||
foreign_thread_sample_count(foreign_thread_sample_count) {}
|
|
||||||
|
|
||||||
bool empty() const
|
bool empty() const {
|
||||||
{
|
return sample_count + gc_sample_count + jit_sample_count +
|
||||||
return sample_count
|
foreign_sample_count + foreign_thread_sample_count ==
|
||||||
+ gc_sample_count
|
0;
|
||||||
+ jit_sample_count
|
}
|
||||||
+ foreign_sample_count
|
|
||||||
+ foreign_thread_sample_count == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
profiling_sample_count record_counts() volatile;
|
profiling_sample_count record_counts() volatile;
|
||||||
void clear() volatile;
|
void clear() volatile;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct profiling_sample
|
struct profiling_sample {
|
||||||
{
|
// Sample counts
|
||||||
// Sample counts
|
profiling_sample_count counts;
|
||||||
profiling_sample_count counts;
|
// Active thread during sample
|
||||||
// Active thread during sample
|
cell thread;
|
||||||
cell thread;
|
/* The callstack at safepoint time. Indexes to the beginning and ending
|
||||||
/* The callstack at safepoint time. Indexes to the beginning and ending
|
code_block entries in the vm sample_callstacks array. */
|
||||||
code_block entries in the vm sample_callstacks array. */
|
cell callstack_begin, callstack_end;
|
||||||
cell callstack_begin, callstack_end;
|
|
||||||
|
|
||||||
profiling_sample(factor_vm *vm,
|
profiling_sample(factor_vm* vm, bool prolog_p,
|
||||||
bool prolog_p,
|
profiling_sample_count const& counts, cell thread);
|
||||||
profiling_sample_count const &counts,
|
|
||||||
cell thread);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue