From: Björn Lindqvist Date: Sat, 24 Jun 2017 23:15:40 +0000 (+0200) Subject: VM: merging the profiling_sample and profiling_sample_count classes X-Git-Tag: 0.98~805 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=abbf8f8f1f0231364499d5e4a0c9fae1cf970a7a VM: merging the profiling_sample and profiling_sample_count classes This refactoring makes the code a bit simpler. --- diff --git a/vm/safepoints.cpp b/vm/safepoints.cpp index bdb9cd8eb5..c23baf3f30 100644 --- a/vm/safepoints.cpp +++ b/vm/safepoints.cpp @@ -15,21 +15,22 @@ void factor_vm::enqueue_samples(cell samples, if (!atomic::load(&sampling_profiler_p)) return; - atomic::fetch_add(&sample_counts.sample_count, samples); + atomic::fetch_add(¤t_sample.sample_count, samples); if (foreign_thread_p) - atomic::fetch_add(&sample_counts.foreign_thread_sample_count, samples); + atomic::fetch_add(¤t_sample.foreign_thread_sample_count, samples); else { if (atomic::load(¤t_gc_p)) - atomic::fetch_add(&sample_counts.gc_sample_count, samples); + atomic::fetch_add(¤t_sample.gc_sample_count, samples); if (atomic::load(¤t_jit_count) > 0) - atomic::fetch_add(&sample_counts.jit_sample_count, samples); + atomic::fetch_add(¤t_sample.jit_sample_count, samples); if (!code->seg->in_segment_p(pc)) - atomic::fetch_add(&sample_counts.foreign_sample_count, samples); + atomic::fetch_add(¤t_sample.foreign_sample_count, samples); } code->set_safepoint_guard(true); } +// Allocates memory (record_sample) void factor_vm::handle_safepoint(cell pc) { code->set_safepoint_guard(false); faulting_p = false; diff --git a/vm/sampling_profiler.cpp b/vm/sampling_profiler.cpp index 962cdc4f56..30680b73f0 100644 --- a/vm/sampling_profiler.cpp +++ b/vm/sampling_profiler.cpp @@ -36,11 +36,11 @@ void factor_vm::growarr_add(array *growarr_, cell elt_) { set_array_nth(growarr.untagged(), 0, tag_fixnum(count + 1)); } -profiling_sample_count profiling_sample_count::record_counts() volatile { +profiling_sample profiling_sample::record_counts() volatile { atomic::fence(); - profiling_sample_count returned(sample_count, gc_sample_count, - jit_sample_count, foreign_sample_count, - foreign_thread_sample_count); + 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); @@ -50,7 +50,7 @@ profiling_sample_count profiling_sample_count::record_counts() volatile { return returned; } -void profiling_sample_count::clear() volatile { +void profiling_sample::clear_counts() volatile { sample_count = 0; gc_sample_count = 0; jit_sample_count = 0; @@ -59,16 +59,10 @@ void profiling_sample_count::clear() volatile { atomic::fence(); } -profiling_sample::profiling_sample(profiling_sample_count const& counts, cell thread, - cell callstack_begin, cell callstack_end) - : counts(counts), thread(thread), - callstack_begin(callstack_begin), - callstack_end(callstack_end) { } - // Allocates memory (sample_callstacks2->add) void factor_vm::record_sample(bool prolog_p) { - profiling_sample_count counts = sample_counts.record_counts(); - if (counts.empty()) { + profiling_sample result = current_sample.record_counts(); + if (result.empty()) { return; } // Appends the callstack, which is just a sequence of quotation or @@ -89,8 +83,10 @@ void factor_vm::record_sample(bool prolog_p) { cell end = growarr_capacity(callstacks.untagged()); // Add the sample. - cell thread = special_objects[OBJ_CURRENT_THREAD]; - samples.push_back(profiling_sample(counts, thread, begin, end)); + result.thread = special_objects[OBJ_CURRENT_THREAD]; + result.callstack_begin = begin; + result.callstack_end = end; + samples.push_back(result); } void factor_vm::set_sampling_profiler(fixnum rate) { @@ -104,7 +100,7 @@ void factor_vm::set_sampling_profiler(fixnum rate) { void factor_vm::start_sampling_profiler(fixnum rate) { special_objects[OBJ_SAMPLE_CALLSTACKS] = tag(allot_growarr()); samples_per_second = rate; - sample_counts.clear(); + current_sample.clear_counts(); // Release the memory consumed by collecting samples. samples.clear(); samples.shrink_to_fit(); @@ -141,15 +137,15 @@ void factor_vm::primitive_get_samples() { data_root sample(allot_array(7, false_object), this); set_array_nth(sample.untagged(), 0, - tag_fixnum(from_iter->counts.sample_count)); + tag_fixnum(from_iter->sample_count)); set_array_nth(sample.untagged(), 1, - tag_fixnum(from_iter->counts.gc_sample_count)); + tag_fixnum(from_iter->gc_sample_count)); set_array_nth(sample.untagged(), 2, - tag_fixnum(from_iter->counts.jit_sample_count)); + tag_fixnum(from_iter->jit_sample_count)); set_array_nth(sample.untagged(), 3, - tag_fixnum(from_iter->counts.foreign_sample_count)); + tag_fixnum(from_iter->foreign_sample_count)); set_array_nth(sample.untagged(), 4, - tag_fixnum(from_iter->counts.foreign_thread_sample_count)); + tag_fixnum(from_iter->foreign_thread_sample_count)); set_array_nth(sample.untagged(), 5, from_iter->thread); diff --git a/vm/sampling_profiler.hpp b/vm/sampling_profiler.hpp index 7d6bde6607..161adc37e0 100644 --- a/vm/sampling_profiler.hpp +++ b/vm/sampling_profiler.hpp @@ -1,6 +1,12 @@ namespace factor { -struct profiling_sample_count { +struct profiling_sample { + // Active thread during sample + cell thread; + // The callstack at safepoint time. Indexes to the beginning and ending + // word entries in the vm sample_callstacks array. + cell callstack_begin, callstack_end; + // Number of samples taken before the safepoint that recorded the sample fixnum sample_count; // Number of samples taken during GC @@ -12,36 +18,25 @@ struct profiling_sample_count { // Number of samples taken during code execution in non-Factor threads fixnum foreign_thread_sample_count; - profiling_sample_count(fixnum sample_count, fixnum gc_sample_count, - fixnum jit_sample_count, fixnum foreign_sample_count, - fixnum foreign_thread_sample_count) - : sample_count(sample_count), + profiling_sample(fixnum sample_count, fixnum gc_sample_count, + fixnum jit_sample_count, fixnum foreign_sample_count, + fixnum foreign_thread_sample_count) + : thread(0), + callstack_begin(0), + callstack_end(0), + sample_count(sample_count), gc_sample_count(gc_sample_count), jit_sample_count(jit_sample_count), foreign_sample_count(foreign_sample_count), foreign_thread_sample_count(foreign_thread_sample_count) {} + profiling_sample record_counts() volatile; + void clear_counts() volatile; bool empty() const { return sample_count + gc_sample_count + jit_sample_count + foreign_sample_count + foreign_thread_sample_count == 0; } - - profiling_sample_count record_counts() volatile; - void clear() volatile; -}; - -struct profiling_sample { - // Sample counts - profiling_sample_count counts; - // Active thread during sample - cell thread; - // The callstack at safepoint time. Indexes to the beginning and ending - // code_block entries in the vm sample_callstacks array. - cell callstack_begin, callstack_end; - - profiling_sample(profiling_sample_count const& counts, cell thread, - cell callstack_begin, cell callstack_end); }; } diff --git a/vm/vm.cpp b/vm/vm.cpp index 130bdda7c9..0d2d65a715 100644 --- a/vm/vm.cpp +++ b/vm/vm.cpp @@ -16,7 +16,7 @@ factor_vm::factor_vm(THREADHANDLE thread) sampling_profiler_p(false), signal_pipe_input(0), signal_pipe_output(0), - sample_counts(0, 0, 0, 0, 0), + current_sample(0, 0, 0, 0, 0), gc_off(false), data(NULL), code(NULL), callbacks(NULL), current_gc(NULL), diff --git a/vm/vm.hpp b/vm/vm.hpp index cab80caa29..be6acf1ae8 100644 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -86,7 +86,7 @@ struct factor_vm { // State kept by the sampling profiler std::vector samples; - volatile profiling_sample_count sample_counts; + volatile profiling_sample current_sample; // GC is off during heap walking bool gc_off;