Bug 1013004 - Fix support for sampling intervals > 1 second. r=BenWa, a=sledru
authorJim Chen <nchen@mozilla.com>
Tue, 19 Aug 2014 15:34:49 -0400
changeset 216463 24a8aa6ad070115a187f88de35c5a479d78f35df
parent 216462 7b4b393ac0f1f3aaad9dfe86b6dd22da8bcdd2c5
child 216464 9be51ba78646a6a7643a5aaef8fb16d194619fa7
push id3857
push userraliiev@mozilla.com
push dateTue, 02 Sep 2014 16:39:23 +0000
treeherdermozilla-beta@5638b907b505 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersBenWa, sledru
bugs1013004
milestone33.0a2
Bug 1013004 - Fix support for sampling intervals > 1 second. r=BenWa, a=sledru
tools/profiler/platform-linux.cc
--- a/tools/profiler/platform-linux.cc
+++ b/tools/profiler/platform-linux.cc
@@ -628,24 +628,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");
 }
-