Skip to content

Commit

Permalink
Add CPUPROFILE_REALTIME env var to use ITIMER_REAL instead of ITIMER_…
Browse files Browse the repository at this point in the history
…PROF
  • Loading branch information
tmm1 committed Nov 4, 2009
1 parent 634cc2e commit 8f9b826
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ google-perftools for ruby code

$ CPUPROFILE=/tmp/my_app_profile RUBYOPT="-r`gem which perftools | tail -1`" ruby my_app.rb

Change the sampling interval (defaults to 100 times a second, valid range is 1-4000):

$ CPUPROFILE_FREQUENCY=500 ruby my_app.rb

Use walltime instead of cputime profiling:

$ CPUPROFILE_REALTIME=1 ruby my_app.rb


=== Reporting

Expand Down
3 changes: 2 additions & 1 deletion ext/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def sys(cmd)
['perftools-gc', true],
['perftools-osx', RUBY_PLATFORM =~ /darwin/],
['perftools-osx-106', RUBY_PLATFORM =~ /darwin10/],
['perftools-debug', true]
['perftools-debug', true],
['perftools-realtime', true]
].each do |patch, apply|
if apply
sys("patch -p1 < ../../../patches/#{patch}.patch")
Expand Down
68 changes: 68 additions & 0 deletions patches/perftools-realtime.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
diff --git a/src/profile-handler.cc b/src/profile-handler.cc
index 370d012..619b980 100644
--- a/src/profile-handler.cc
+++ b/src/profile-handler.cc
@@ -133,6 +133,9 @@ class ProfileHandler {
// SIGPROF interrupt frequency, read-only after construction.
int32 frequency_;

+ // use SIGALRM/ITIMER_REAL
+ bool realtime_;
+
// Counts the number of callbacks registered.
int32 callback_count_ GUARDED_BY(control_lock_);

@@ -241,6 +244,13 @@ ProfileHandler::ProfileHandler()
callback_count_(0),
timer_sharing_(TIMERS_UNTOUCHED) {
SpinLockHolder cl(&control_lock_);
+
+ const char* rt = getenv("CPUPROFILE_REALTIME");
+ if (rt != NULL)
+ realtime_ = true;
+ else
+ realtime_ = false;
+
// Get frequency of interrupts (if specified)
char junk;
const char* fr = getenv("CPUPROFILE_FREQUENCY");
@@ -396,18 +406,18 @@ void ProfileHandler::StartTimer() {
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 1000000 / frequency_;
timer.it_value = timer.it_interval;
- setitimer(ITIMER_PROF, &timer, 0);
+ setitimer(realtime_ ? ITIMER_REAL : ITIMER_PROF, &timer, 0);
}

void ProfileHandler::StopTimer() {
struct itimerval timer;
memset(&timer, 0, sizeof timer);
- setitimer(ITIMER_PROF, &timer, 0);
+ setitimer(realtime_ ? ITIMER_REAL : ITIMER_PROF, &timer, 0);
}

bool ProfileHandler::IsTimerRunning() {
struct itimerval current_timer;
- RAW_CHECK(0 == getitimer(ITIMER_PROF, &current_timer), "getitimer");
+ RAW_CHECK(0 == getitimer(realtime_ ? ITIMER_REAL : ITIMER_PROF, &current_timer), "getitimer");
return (current_timer.it_value.tv_sec != 0 ||
current_timer.it_value.tv_usec != 0);
}
@@ -417,7 +427,7 @@ void ProfileHandler::EnableHandler() {
sa.sa_sigaction = SignalHandler;
sa.sa_flags = SA_RESTART | SA_SIGINFO;
sigemptyset(&sa.sa_mask);
- RAW_CHECK(sigaction(SIGPROF, &sa, NULL) == 0, "sigprof (enable)");
+ RAW_CHECK(sigaction(realtime_ ? SIGALRM : SIGPROF, &sa, NULL) == 0, "sigprof (enable)");
}

void ProfileHandler::DisableHandler() {
@@ -425,7 +435,7 @@ void ProfileHandler::DisableHandler() {
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
- RAW_CHECK(sigaction(SIGPROF, &sa, NULL) == 0, "sigprof (disable)");
+ RAW_CHECK(sigaction(realtime_ ? SIGALRM : SIGPROF, &sa, NULL) == 0, "sigprof (disable)");
}

void ProfileHandler::SignalHandler(int sig, siginfo_t* sinfo, void* ucontext) {
5 changes: 3 additions & 2 deletions perftools.rb.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spec = Gem::Specification.new do |s|
s.name = 'perftools.rb'
s.version = '0.3.6'
s.date = '2009-10-26'
s.version = '0.3.7'
s.date = '2009-11-03'
s.rubyforge_project = 'perftools-rb'
s.summary = 'google-perftools for ruby code'
s.description = 'A sampling profiler for ruby code based on patches to google-perftools'
Expand All @@ -28,6 +28,7 @@ spec = Gem::Specification.new do |s|
"patches/perftools-osx-106.patch",
"patches/perftools-osx.patch",
"patches/perftools-pprof.patch",
"patches/perftools-realtime.patch",
"patches/perftools-notests.patch",
"patches/perftools.patch",
"perftools.rb.gemspec"
Expand Down

0 comments on commit 8f9b826

Please sign in to comment.