<commit_before>#include "master.hpp"

namespace factor
{

profiling_sample_count profiling_sample_count::record_counts() volatile
{
	atomic::fence();
	profiling_sample_count returned(
		sample_count,
		gc_sample_count,
		foreign_sample_count,
		foreign_thread_sample_count);
	atomic::subtract(&sample_count, returned.sample_count);
	atomic::subtract(&gc_sample_count, returned.gc_sample_count);
	atomic::subtract(&foreign_sample_count, returned.foreign_sample_count);
	atomic::subtract(&foreign_thread_sample_count, returned.foreign_thread_sample_count);
	return returned;
}

void profiling_sample_count::clear() volatile
{
	sample_count = 0;
	gc_sample_count = 0;
	foreign_sample_count = 0;
	foreign_thread_sample_count = 0;
	atomic::fence();
}

profiling_sample::profiling_sample(factor_vm *vm,
	profiling_sample_count const &counts,
	context *ctx)
	:
	counts(counts),
	ctx(ctx)
{
	vm->record_callstack_sample(&callstack_begin, &callstack_end);
}

void factor_vm::record_sample()
{
	profiling_sample_count counts = safepoint_sample_counts.record_counts();
	if (!counts.empty())
		samples.push_back(profiling_sample(this, counts, ctx));
}

void factor_vm::record_callstack_sample(cell *begin, cell *end)
{
	*begin = sample_callstacks.size();
	stack_frame *frame = ctx->bottom_frame();

	while (frame >= ctx->callstack_top) {
		sample_callstacks.push_back(frame_code(frame));
		frame = frame_successor(frame);
	}

	*end = sample_callstacks.size();
}

void factor_vm::set_sampling_profiler(fixnum rate)
{
	bool sampling_p = !!rate;
	if (sampling_p == sampling_profiler_p)
		return;
	
	if (sampling_p)
		start_sampling_profiler(rate);
	else
		end_sampling_profiler();
}

void factor_vm::clear_samples()
{
	// Swapping into temporaries releases the vector's allocated storage,
	// whereas clear() would leave the allocation as-is
	std::vector<profiling_sample> sample_graveyard;
	std::vector<code_block*> sample_callstack_graveyard;
	samples.swap(sample_graveyard);
	sample_callstacks.swap(sample_callstack_graveyard);
}

void factor_vm::start_sampling_profiler(fixnum rate)
{
	samples_per_second = rate;
	safepoint_sample_counts.clear();
	clear_samples();
	samples.reserve(10*rate);
	sample_callstacks.reserve(100*rate);
	sampling_profiler_p = true;
	start_sampling_profiler_timer();
}

void factor_vm::end_sampling_profiler()
{
	sampling_profiler_p = false;
	atomic::fence();
	end_sampling_profiler_timer();
	record_sample();
}

void factor_vm::primitive_sampling_profiler()
{
	set_sampling_profiler(to_fixnum(ctx->pop()));
}

void factor_vm::primitive_get_samples()
{
	if (sampling_profiler_p || samples.empty()) {
		ctx->push(false_object);
	} else {
		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;

		for (; from_iter != samples.end(); ++from_iter, ++to_i)
		{
			data_root<array> sample(allot_array(6, false_object),this);

			set_array_nth(sample.untagged(),0,from_unsigned_cell(from_iter->counts.sample_count));
			set_array_nth(sample.untagged(),1,from_unsigned_cell(from_iter->counts.gc_sample_count));
			set_array_nth(sample.untagged(),2,from_unsigned_cell(from_iter->counts.foreign_sample_count));
			set_array_nth(sample.untagged(),3,from_unsigned_cell(from_iter->counts.foreign_thread_sample_count));
			set_array_nth(sample.untagged(),4,allot_alien((void*)from_iter->ctx));

			cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
			data_root<array> callstack(allot_array(callstack_size,false_object),this);

			std::vector<code_block*>::const_iterator
				callstacks_begin = sample_callstacks.begin(),
				c_from_iter = callstacks_begin + from_iter->callstack_begin,<commit_msg>vm: fix data roots in get-samples primitive<commit_after>#include "master.hpp"

namespace factor
{

profiling_sample_count profiling_sample_count::record_counts() volatile
{
	atomic::fence();
	profiling_sample_count returned(
		sample_count,
		gc_sample_count,
		foreign_sample_count,
		foreign_thread_sample_count);
	atomic::subtract(&sample_count, returned.sample_count);
	atomic::subtract(&gc_sample_count, returned.gc_sample_count);
	atomic::subtract(&foreign_sample_count, returned.foreign_sample_count);
	atomic::subtract(&foreign_thread_sample_count, returned.foreign_thread_sample_count);
	return returned;
}

void profiling_sample_count::clear() volatile
{
	sample_count = 0;
	gc_sample_count = 0;
	foreign_sample_count = 0;
	foreign_thread_sample_count = 0;
	atomic::fence();
}

profiling_sample::profiling_sample(factor_vm *vm,
	profiling_sample_count const &counts,
	context *ctx)
	:
	counts(counts),
	ctx(ctx)
{
	vm->record_callstack_sample(&callstack_begin, &callstack_end);
}

void factor_vm::record_sample()
{
	profiling_sample_count counts = safepoint_sample_counts.record_counts();
	if (!counts.empty())
		samples.push_back(profiling_sample(this, counts, ctx));
}

void factor_vm::record_callstack_sample(cell *begin, cell *end)
{
	*begin = sample_callstacks.size();
	stack_frame *frame = ctx->bottom_frame();

	while (frame >= ctx->callstack_top) {
		sample_callstacks.push_back(frame_code(frame));
		frame = frame_successor(frame);
	}

	*end = sample_callstacks.size();
}

void factor_vm::set_sampling_profiler(fixnum rate)
{
	bool sampling_p = !!rate;
	if (sampling_p == sampling_profiler_p)
		return;
	
	if (sampling_p)
		start_sampling_profiler(rate);
	else
		end_sampling_profiler();
}

void factor_vm::clear_samples()
{
	// Swapping into temporaries releases the vector's allocated storage,
	// whereas clear() would leave the allocation as-is
	std::vector<profiling_sample> sample_graveyard;
	std::vector<code_block*> sample_callstack_graveyard;
	samples.swap(sample_graveyard);
	sample_callstacks.swap(sample_callstack_graveyard);
}

void factor_vm::start_sampling_profiler(fixnum rate)
{
	samples_per_second = rate;
	safepoint_sample_counts.clear();
	clear_samples();
	samples.reserve(10*rate);
	sample_callstacks.reserve(100*rate);
	sampling_profiler_p = true;
	start_sampling_profiler_timer();
}

void factor_vm::end_sampling_profiler()
{
	sampling_profiler_p = false;
	atomic::fence();
	end_sampling_profiler_timer();
	record_sample();
}

void factor_vm::primitive_sampling_profiler()
{
	set_sampling_profiler(to_fixnum(ctx->pop()));
}

void factor_vm::primitive_get_samples()
{
	if (sampling_profiler_p || samples.empty()) {
		ctx->push(false_object);
	} else {
		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;

		for (; from_iter != samples.end(); ++from_iter, ++to_i)
		{
			data_root<array> sample(allot_array(6, false_object),this);

			set_array_nth(sample.untagged(),0,tag_fixnum(from_iter->counts.sample_count));
			set_array_nth(sample.untagged(),1,tag_fixnum(from_iter->counts.gc_sample_count));
			set_array_nth(sample.untagged(),2,tag_fixnum(from_iter->counts.foreign_sample_count));
			set_array_nth(sample.untagged(),3,tag_fixnum(from_iter->counts.foreign_thread_sample_count));

			cell ctx = allot_alien((void*)from_iter->ctx);
			set_array_nth(sample.untagged(),4,ctx);

			cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
			data_root<array> callstack(allot_array(callstack_size,false_object),this);

			std::vector<code_block*>::const_iterator
				callstacks_begin = sample_callstacks.begin(),
				c_from_iter = callstacks_begin + from_iter->callstack_begin,
{'fraction_non_alphanumeric': 0.12386787426744805, 'fraction_numerical': 0.004262120404901438, 'mean_word_length': 7.8549528301886795, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1845740', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2664, 'n_words': 360}