Skip to content

Commit

Permalink
Added profiler report interval.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed May 15, 2016
1 parent 7458cd0 commit 216c54a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
5 changes: 4 additions & 1 deletion library/rubinius/configuration.rb
Expand Up @@ -145,7 +145,10 @@

s.section "profiler" do |p|
p.vm_variable "target", "none",
"Location to send profiler output: path"
"Location to send profiler output: 'diagnostics', path"

p.vm_variable "interval", 10000,
"Report profiler results every N samples"

p.vm_variable "subprocess", false,
"Enable profiling in subprocesses created by fork"
Expand Down
13 changes: 9 additions & 4 deletions machine/shared_state.cpp
Expand Up @@ -37,6 +37,7 @@ namespace rubinius {
, metrics_(NULL)
, diagnostics_(NULL)
, profiler_path_()
, profiler_target_(eNone)
, profiler_enabled_(false)
, start_time_(get_current_time())
, method_count_(1)
Expand Down Expand Up @@ -171,10 +172,14 @@ namespace rubinius {
}

void SharedState::start_profiler(STATE) {
profiler_enabled_ =
!!config.system_profiler_target.value.compare("none");

if(profiler_enabled_) {
profiler_enabled_ = true;

if(!config.system_profiler_target.value.compare("none")) {
profiler_enabled_ = false;
} else if(!config.system_profiler_target.value.compare("diagnostics")) {
profiler_target_ = eDiagnostics;
} else {
profiler_target_ = ePath;
set_profiler_path();
}
}
Expand Down
13 changes: 13 additions & 0 deletions machine/shared_state.hpp
Expand Up @@ -80,6 +80,13 @@ namespace rubinius {
*/

class SharedState {
public:
enum ProfilerTarget {
eNone,
ePath,
eDiagnostics
};

private:
ThreadNexus* thread_nexus_;
InternalThreads* internal_threads_;
Expand All @@ -93,7 +100,9 @@ namespace rubinius {
CApiConstantHandleMap capi_constant_handle_map_;

std::string profiler_path_;
ProfilerTarget profiler_target_;
bool profiler_enabled_;

uint64_t start_time_;
uint64_t method_count_;
unsigned int class_count_;
Expand Down Expand Up @@ -242,6 +251,10 @@ namespace rubinius {
return profiler_enabled_;
}

ProfilerTarget profiler_target() {
return profiler_target_;
}

Environment* env() const {
return env_;
}
Expand Down
41 changes: 32 additions & 9 deletions machine/vm.cpp
Expand Up @@ -89,6 +89,7 @@ namespace rubinius {
, waiting_object_(this, cNil)
, profile_(this, nil<Tuple>())
, profile_sample_count_(0)
, profile_sample_interval_(shared.config.system_profiler_interval.value)
, max_profile_entries_(25)
, min_profile_sample_count_(0)
, start_time_(0)
Expand Down Expand Up @@ -286,6 +287,11 @@ namespace rubinius {
metrics().machine.profiles++;
profile_sample_count_++;

if(profile_sample_count_ > profile_sample_interval_) {
profile_sample_interval_ += state->shared().config.system_profiler_interval.value;
report_profile(state);
}

CompiledCode* code = state->vm()->call_frame()->compiled_code;
code->machine_code()->sample_count++;

Expand Down Expand Up @@ -317,16 +323,9 @@ namespace rubinius {
}
}

void VM::report_profile(STATE) {
if(!state->shared().profiler_enabled_p()) return;

Tuple* profile = profile_.get();
if(profile->nil_p()) return;

void VM::report_profile_file(STATE, Tuple* profile, double total_time) {
std::ofstream file;
file.open(state->shared().profiler_path());

double total_time = run_time();
file.open(state->shared().profiler_path(), std::fstream::out | std::fstream::app);

file << "Profile: thread: " << thread_id()
<< ", samples: " << profile_sample_count_
Expand Down Expand Up @@ -360,6 +359,30 @@ namespace rubinius {
file.close();
}

void VM::report_profile_diagnostics(STATE, Tuple* profile, double total_time) {

}

void VM::report_profile(STATE) {
if(!state->shared().profiler_enabled_p()) return;

Tuple* profile = profile_.get();
if(profile->nil_p()) return;

double total_time = run_time();

switch(state->shared().profiler_target()) {
case SharedState::ePath:
report_profile_file(state, profile, total_time);
break;
case SharedState::eDiagnostics:
report_profile_diagnostics(state, profile, total_time);
break;
default:
return;
}
}

static void suspend_thread() {
static int i = 0;
static int delay[] = {
Expand Down
3 changes: 3 additions & 0 deletions machine/vm.hpp
Expand Up @@ -146,6 +146,7 @@ namespace rubinius {

memory::TypedRoot<Tuple*> profile_;
uint64_t profile_sample_count_;
uint64_t profile_sample_interval_;
native_int max_profile_entries_;
native_int min_profile_sample_count_;

Expand Down Expand Up @@ -420,6 +421,8 @@ namespace rubinius {

void update_profile(STATE);
void report_profile(STATE);
void report_profile_file(STATE, Tuple* profile, double total_time);
void report_profile_diagnostics(STATE, Tuple* profile, double total_time);

#define RBX_PROFILE_MAX_SHIFT 0xf
#define RBX_PROFILE_MAX_INTERVAL 0x1fff
Expand Down

0 comments on commit 216c54a

Please sign in to comment.