bug 679427 - make event loop responsiveness threshold and interval configurable via environment variable. r=jlebar
authorTed Mielczarek <ted.mielczarek@gmail.com>
Fri, 09 Sep 2011 08:51:08 -0400
changeset 78697 cf18cd153c5e19d9beb340904eec54019546248b
parent 78696 f850d4a184c955003375648a659737e1359e186f
child 78698 959c1e6bdb11759dcb380ab96df71f13b2f7086d
push id78
push userclegnitto@mozilla.com
push dateFri, 16 Dec 2011 17:32:24 +0000
treeherdermozilla-release@79d24e644fdd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjlebar
bugs679427
milestone9.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 679427 - make event loop responsiveness threshold and interval configurable via environment variable. r=jlebar
toolkit/xre/EventTracer.cpp
--- a/toolkit/xre/EventTracer.cpp
+++ b/toolkit/xre/EventTracer.cpp
@@ -47,16 +47,28 @@
  * Usage:
  *
  * Set MOZ_INSTRUMENT_EVENT_LOOP=1 in the environment to enable
  * this instrumentation. Currently only the UI process is instrumented.
  *
  * Set MOZ_INSTRUMENT_EVENT_LOOP_OUTPUT in the environment to a
  * file path to contain the log output, the default is to log to stdout.
  *
+ * Set MOZ_INSTRUMENT_EVENT_LOOP_THRESHOLD in the environment to an
+ * integer number of milliseconds to change the threshold for reporting.
+ * The default is 20 milliseconds. Unresponsive periods shorter than this
+ * threshold will not be reported.
+ *
+ * Set MOZ_INSTRUMENT_EVENT_LOOP_INTERVAL in the environment to an
+ * integer number of milliseconds to change the maximum sampling frequency.
+ * This variable controls how often events will be sent to the main
+ * thread's event loop to sample responsiveness. The sampler will not
+ * send events twice within LOOP_INTERVAL milliseconds.
+ * The default is 10 milliseconds.
+ *
  * All logged output lines start with MOZ_EVENT_TRACE. All timestamps
  * output are milliseconds since the epoch (PRTime / 1000).
  *
  * On startup, a line of the form:
  *   MOZ_EVENT_TRACE start <timestamp>
  * will be output.
  *
  * On shutdown, a line of the form:
@@ -70,16 +82,17 @@
  */
 
 #include "EventTracer.h"
 
 #include <stdio.h>
 
 #include "mozilla/TimeStamp.h"
 #include "mozilla/WidgetTraceEvent.h"
+#include <limits.h>
 #include <prenv.h>
 #include <prinrval.h>
 #include <prthread.h>
 #include <prtime.h>
 
 using mozilla::TimeDuration;
 using mozilla::TimeStamp;
 using mozilla::FireAndWaitForTracerEvent;
@@ -96,41 +109,60 @@ bool sExit = false;
  * it will not send another event until the previous response is received.
  *
  * The output defaults to stdout, but can be redirected to a file by
  * settting the environment variable MOZ_INSTRUMENT_EVENT_LOOP_OUTPUT
  * to the name of a file to use.
  */
 void TracerThread(void *arg)
 {
+  // These are the defaults. They can be overridden by environment vars.
   // This should be set to the maximum latency we'd like to allow
   // for responsiveness.
-  const PRIntervalTime kMeasureInterval = PR_MillisecondsToInterval(50);
+  PRIntervalTime threshold = PR_MillisecondsToInterval(20);
+  // This is the sampling interval.
+  PRIntervalTime interval = PR_MillisecondsToInterval(10);
 
   FILE* log = NULL;
   char* envfile = PR_GetEnv("MOZ_INSTRUMENT_EVENT_LOOP_OUTPUT");
   if (envfile) {
     log = fopen(envfile, "w");
   }
   if (log == NULL)
     log = stdout;
 
+  char* thresholdenv = PR_GetEnv("MOZ_INSTRUMENT_EVENT_LOOP_THRESHOLD");
+  if (thresholdenv && *thresholdenv) {
+    int val = atoi(thresholdenv);
+    if (val != 0 && val != INT_MAX && val != INT_MIN) {
+      threshold = PR_MillisecondsToInterval(val);
+    }
+  }
+
+  char* intervalenv = PR_GetEnv("MOZ_INSTRUMENT_EVENT_LOOP_INTERVAL");
+  if (intervalenv && *intervalenv) {
+    int val = atoi(intervalenv);
+    if (val != 0 && val != INT_MAX && val != INT_MIN) {
+      interval = PR_MillisecondsToInterval(val);
+    }
+  }
+
   fprintf(log, "MOZ_EVENT_TRACE start %llu\n", PR_Now() / PR_USEC_PER_MSEC);
 
   while (!sExit) {
     TimeStamp start(TimeStamp::Now());
-    PRIntervalTime next_sleep = kMeasureInterval;
+    PRIntervalTime next_sleep = interval;
 
-    //TODO: only wait up to a maximum of kMeasureInterval, return
+    //TODO: only wait up to a maximum of interval; return
     // early if that threshold is exceeded and dump a stack trace
     // or do something else useful.
     if (FireAndWaitForTracerEvent()) {
       TimeDuration duration = TimeStamp::Now() - start;
-      // Only report samples that exceed our measurement interval.
-      if (duration.ToMilliseconds() > kMeasureInterval) {
+      // Only report samples that exceed our measurement threshold.
+      if (duration.ToMilliseconds() > threshold) {
         fprintf(log, "MOZ_EVENT_TRACE sample %llu %d\n",
                 PR_Now() / PR_USEC_PER_MSEC,
                 int(duration.ToSecondsSigDigits() * 1000));
       }
 
       if (next_sleep > duration.ToMilliseconds()) {
         next_sleep -= int(duration.ToMilliseconds());
       }