6 profiling_sample_count profiling_sample_count::record_counts() volatile
8 FACTOR_MEMORY_BARRIER();
9 profiling_sample_count returned(
13 foreign_thread_sample_count);
14 FACTOR_ATOMIC_SUB(&sample_count, returned.sample_count);
15 FACTOR_ATOMIC_SUB(&gc_sample_count, returned.gc_sample_count);
16 FACTOR_ATOMIC_SUB(&foreign_sample_count, returned.foreign_sample_count);
17 FACTOR_ATOMIC_SUB(&foreign_thread_sample_count, returned.foreign_thread_sample_count);
21 void profiling_sample_count::clear() volatile
25 foreign_sample_count = 0;
26 foreign_thread_sample_count = 0;
27 FACTOR_MEMORY_BARRIER();
30 profiling_sample::profiling_sample(factor_vm *vm,
31 profiling_sample_count const &counts,
37 vm->record_callstack_sample(&callstack_begin, &callstack_end);
40 void factor_vm::record_sample()
42 profiling_sample_count counts = safepoint_sample_counts.record_counts();
44 samples.push_back(profiling_sample(this, counts, ctx));
47 void factor_vm::record_callstack_sample(cell *begin, cell *end)
49 *begin = sample_callstacks.size();
50 stack_frame *frame = ctx->bottom_frame();
52 while (frame >= ctx->callstack_top) {
53 sample_callstacks.push_back(frame_code(frame));
54 frame = frame_successor(frame);
57 *end = sample_callstacks.size();
60 void factor_vm::set_sampling_profiler(bool sampling_p)
62 if (sampling_p == sampling_profiler_p)
66 start_sampling_profiler();
68 end_sampling_profiler();
71 void factor_vm::clear_samples()
73 // Swapping into temporaries releases the vector's allocated storage,
74 // whereas clear() would leave the allocation as-is
75 std::vector<profiling_sample> sample_graveyard;
76 std::vector<code_block*> sample_callstack_graveyard;
77 samples.swap(sample_graveyard);
78 sample_callstacks.swap(sample_callstack_graveyard);
81 void factor_vm::start_sampling_profiler()
83 safepoint_sample_counts.clear();
85 samples.reserve(10*FACTOR_PROFILE_SAMPLES_PER_SECOND);
86 sample_callstacks.reserve(100*FACTOR_PROFILE_SAMPLES_PER_SECOND);
87 sampling_profiler_p = true;
88 start_sampling_profiler_timer();
91 void factor_vm::end_sampling_profiler()
93 sampling_profiler_p = false;
94 FACTOR_MEMORY_BARRIER();
95 end_sampling_profiler_timer();
99 void factor_vm::primitive_sampling_profiler()
101 set_sampling_profiler(to_boolean(ctx->pop()));
104 void factor_vm::primitive_get_samples()
106 if (sampling_profiler_p || samples.empty()) {
107 ctx->push(false_object);
109 data_root<array> samples_array(allot_array(samples.size(), false_object),this);
110 std::vector<profiling_sample>::const_iterator from_iter = samples.begin();
113 for (; from_iter != samples.end(); ++from_iter, ++to_i)
115 data_root<array> sample(allot_array(6, false_object),this);
117 set_array_nth(sample.untagged(),0,from_unsigned_cell(from_iter->counts.sample_count));
118 set_array_nth(sample.untagged(),1,from_unsigned_cell(from_iter->counts.gc_sample_count));
119 set_array_nth(sample.untagged(),2,from_unsigned_cell(from_iter->counts.foreign_sample_count));
120 set_array_nth(sample.untagged(),3,from_unsigned_cell(from_iter->counts.foreign_thread_sample_count));
121 set_array_nth(sample.untagged(),4,allot_alien((void*)from_iter->ctx));
123 cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
124 data_root<array> callstack(allot_array(callstack_size,false_object),this);
126 std::vector<code_block*>::const_iterator
127 callstacks_begin = sample_callstacks.begin(),
128 c_from_iter = callstacks_begin + from_iter->callstack_begin,
129 c_from_iter_end = callstacks_begin + from_iter->callstack_end;
132 for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
133 set_array_nth(callstack.untagged(),c_to_i,(*c_from_iter)->owner);
135 set_array_nth(sample.untagged(),5,callstack.value());
137 set_array_nth(samples_array.untagged(),to_i,sample.value());
139 ctx->push(samples_array.value());
143 void factor_vm::primitive_clear_samples()