Bug 1013004 - Fix support for sampling intervals > 1 second. r=BenWa, a=lmandel
authorJim Chen <nchen@mozilla.com>
Tue, 19 Aug 2014 15:34:49 -0400
changeset 208352 61980c2f6177
parent 208351 50590d1557c4
child 208353 4f18903bc230
push id3836
push userryanvm@gmail.com
push date2014-08-20 23:01 +0000
treeherdermozilla-beta@f5d4b16203aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersBenWa, lmandel
bugs1013004
milestone32.0
Bug 1013004 - Fix support for sampling intervals > 1 second. r=BenWa, a=lmandel
tools/profiler/platform-linux.cc
--- a/tools/profiler/platform-linux.cc
+++ b/tools/profiler/platform-linux.cc
@@ -625,24 +625,31 @@ void TickSample::PopulateContext(void* a
   MOZ_ASSERT(aContext);
   ucontext_t* pContext = reinterpret_cast<ucontext_t*>(aContext);
   if (!getcontext(pContext)) {
     context = pContext;
     SetSampleContext(this, aContext);
   }
 }
 
-// WARNING: Works with values up to 1 second
 void OS::SleepMicro(int microseconds)
 {
+  if (MOZ_UNLIKELY(microseconds >= 1000000)) {
+    // Use usleep for larger intervals, because the nanosleep
+    // code below only supports intervals < 1 second.
+    MOZ_ALWAYS_TRUE(!::usleep(microseconds));
+    return;
+  }
+
   struct timespec ts;
   ts.tv_sec  = 0;
   ts.tv_nsec = microseconds * 1000UL;
 
-  while (true) {
-    // in the case of interrupt we keep waiting
-    // nanosleep puts the remaining to back into ts
-    if (!nanosleep(&ts, &ts) || errno != EINTR) {
-      return;
-    }
+  int rv = ::nanosleep(&ts, &ts);
+
+  while (rv != 0 && errno == EINTR) {
+    // Keep waiting in case of interrupt.
+    // nanosleep puts the remaining time back into ts.
+    rv = ::nanosleep(&ts, &ts);
   }
+
+  MOZ_ASSERT(!rv, "nanosleep call failed");
 }
-