Skip to content

Commit

Permalink
Add simple method profiler (non-sampling, each method call creates on…
Browse files Browse the repository at this point in the history
…e sample)
  • Loading branch information
tmm1 committed Nov 12, 2010
1 parent 73c8e8b commit b5e879a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README
Expand Up @@ -33,6 +33,10 @@ google-perftools for ruby code

$ CPUPROFILE_OBJECTS=1 ruby my_app.rb

Profile method calls:

$ CPUPROFILE_METHODS=1 ruby my_app.rb


=== Reporting

Expand Down
39 changes: 38 additions & 1 deletion ext/perftools.c
Expand Up @@ -213,6 +213,8 @@ static VALUE Isend;

static VALUE objprofiler_setup();
static VALUE objprofiler_teardown();
static VALUE methprofiler_setup();
static VALUE methprofiler_teardown();

/* CpuProfiler */

Expand All @@ -236,6 +238,7 @@ cpuprofiler_stop(VALUE self)

bProfilerRunning = Qfalse;
objprofiler_teardown();
methprofiler_teardown();
ProfilerStop();
ProfilerFlush();

Expand All @@ -252,6 +255,8 @@ cpuprofiler_start(VALUE self, VALUE filename)

if (getenv("CPUPROFILE_OBJECTS"))
objprofiler_setup();
else if (getenv("CPUPROFILE_METHODS"))
methprofiler_setup();

if (ProfilerStart(RSTRING_PTR(filename))) {
bProfilerRunning = Qtrue;
Expand Down Expand Up @@ -285,6 +290,7 @@ cpuprofiler_gc_mark()
#include <sys/mman.h>

static VALUE bObjProfilerRunning;
static VALUE bMethProfilerRunning;
#define NUM_ORIG_BYTES 2

struct {
Expand Down Expand Up @@ -332,6 +338,35 @@ uc_get_ip(ucontext_t *uc) {
return (char**)&uc->program_counter;
}

static void
event_handler(rb_event_t event, NODE *node, VALUE self, ID id, VALUE klass) {
ProfilerRecord(0, NULL, NULL);
}

static VALUE
methprofiler_setup()
{
if (bMethProfilerRunning)
return Qtrue;

rb_add_event_hook(event_handler, RUBY_EVENT_CALL);

bMethProfilerRunning = Qtrue;
return Qtrue;
}

static VALUE
methprofiler_teardown()
{
if (!bMethProfilerRunning)
return Qfalse;

rb_remove_event_hook(event_handler);

bMethProfilerRunning = Qfalse;
return Qtrue;
}

static void
trap_handler(int sig, siginfo_t *info, void *data) {
int i;
Expand Down Expand Up @@ -423,7 +458,7 @@ Init_perftools()
I__send__ = rb_intern("__send__");
Isend = rb_intern("send");

bObjProfilerRunning = bProfilerRunning = Qfalse;
bMethProfilerRunning = bObjProfilerRunning = bProfilerRunning = Qfalse;

rb_define_singleton_method(cCpuProfiler, "running?", cpuprofiler_running_p, 0);
rb_define_singleton_method(cCpuProfiler, "start", cpuprofiler_start, 1);
Expand All @@ -437,6 +472,8 @@ Init_perftools()

if (getenv("CPUPROFILE_OBJECTS")) { // want to profile objects
objprofiler_setup();
} else if (getenv("CPUPROFILE_METHODS")) {
methprofiler_setup();
}

rb_set_end_proc(profiler_at_exit, 0); // make sure to cleanup before the VM shuts down
Expand Down
10 changes: 5 additions & 5 deletions patches/perftools-objects.patch
Expand Up @@ -12,7 +12,7 @@ index 619b980..2ecf2c0 100644
}

void ProfileHandler::StartTimer() {
+ if (getenv("CPUPROFILE_OBJECTS")) return;
+ if (getenv("CPUPROFILE_OBJECTS") || getenv("CPUPROFILE_METHODS")) return;
+
struct itimerval timer;
timer.it_interval.tv_sec = 0;
Expand All @@ -21,15 +21,15 @@ index 619b980..2ecf2c0 100644
}

void ProfileHandler::StopTimer() {
+ if (getenv("CPUPROFILE_OBJECTS")) return;
+ if (getenv("CPUPROFILE_OBJECTS") || getenv("CPUPROFILE_METHODS")) return;
+
struct itimerval timer;
memset(&timer, 0, sizeof timer);
setitimer(realtime_ ? ITIMER_REAL : ITIMER_PROF, &timer, 0);
}

bool ProfileHandler::IsTimerRunning() {
+ if (getenv("CPUPROFILE_OBJECTS")) return false;
+ if (getenv("CPUPROFILE_OBJECTS") || getenv("CPUPROFILE_METHODS")) return false;
+
struct itimerval current_timer;
RAW_CHECK(0 == getitimer(realtime_ ? ITIMER_REAL : ITIMER_PROF, &current_timer), "getitimer");
Expand All @@ -38,7 +38,7 @@ index 619b980..2ecf2c0 100644
}

void ProfileHandler::EnableHandler() {
+ if (getenv("CPUPROFILE_OBJECTS")) return;
+ if (getenv("CPUPROFILE_OBJECTS") || getenv("CPUPROFILE_METHODS")) return;
+
struct sigaction sa;
sa.sa_sigaction = SignalHandler;
Expand All @@ -47,7 +47,7 @@ index 619b980..2ecf2c0 100644
}

void ProfileHandler::DisableHandler() {
+ if (getenv("CPUPROFILE_OBJECTS")) return;
+ if (getenv("CPUPROFILE_OBJECTS") || getenv("CPUPROFILE_METHODS")) return;
+
struct sigaction sa;
sa.sa_handler = SIG_IGN;
Expand Down

0 comments on commit b5e879a

Please sign in to comment.