bug 606574 - Implement event loop instrumentation using native events, core implementation + GTK2 implementation. r=karlt,cjones
authorTed Mielczarek <ted.mielczarek@gmail.com>
Thu, 03 Mar 2011 11:20:02 -0500
changeset 68054 81ea8b39ed4efb4f682547347f554df7658f3248
parent 68053 2a343ee6acd3131d8e6f04afc346e069d74e101f
child 68055 b7bafc5210cb6a9e81e148aa9ab58a4007add1dc
push id19491
push usertmielczarek@mozilla.com
push dateWed, 13 Apr 2011 17:14:46 +0000
treeherdermozilla-central@2c5a545681cf [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskarlt, cjones
bugs606574
milestone6.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 606574 - Implement event loop instrumentation using native events, core implementation + GTK2 implementation. r=karlt,cjones
config/autoconf.mk.in
configure.in
toolkit/xre/EventTracer.cpp
toolkit/xre/EventTracer.h
toolkit/xre/Makefile.in
toolkit/xre/nsAppRunner.cpp
widget/public/Makefile.in
widget/public/WidgetTraceEvent.h
widget/src/gtk2/Makefile.in
widget/src/gtk2/WidgetTraceEvent.cpp
--- a/config/autoconf.mk.in
+++ b/config/autoconf.mk.in
@@ -711,16 +711,18 @@ ANDROID_PLATFORM  = @ANDROID_PLATFORM@
 ANDROID_SDK       = @ANDROID_SDK@
 ANDROID_PLATFORM_TOOLS = @ANDROID_PLATFORM_TOOLS@
 ANDROID_VERSION   = @ANDROID_VERSION@
 
 ANDROID_PACKAGE_NAME = @ANDROID_PACKAGE_NAME@
 
 JS_SHARED_LIBRARY = @JS_SHARED_LIBRARY@
 
+MOZ_INSTRUMENT_EVENT_LOOP = @MOZ_INSTRUMENT_EVENT_LOOP@
+
 # We only want to do the pymake sanity on Windows, other os's can cope
 ifeq (,$(filter-out WINNT WINCE,$(HOST_OS_ARCH)))
 # Ensure invariants between GNU Make and pymake
 # Checked here since we want the sane error in a file that
 # actually can be found regardless of path-style.
 ifeq (_:,$(.PYMAKE)_$(findstring :,$(srcdir)))
 $(error Windows-style srcdir being used with GNU make. Did you mean to run $(topsrcdir)/build/pymake/make.py instead? [see-also: https://developer.mozilla.org/en/Gmake_vs._Pymake])
 endif
--- a/configure.in
+++ b/configure.in
@@ -5215,16 +5215,17 @@ cairo-gtk2|cairo-gtk2-x11)
     AC_DEFINE(MOZ_X11)
     MOZ_X11=1
     USE_FC_FREETYPE=1
 
     TK_CFLAGS='$(MOZ_GTK2_CFLAGS)'
     TK_LIBS='$(MOZ_GTK2_LIBS)'
     AC_DEFINE(MOZ_WIDGET_GTK2)
     MOZ_PDF_PRINTING=1
+    MOZ_INSTRUMENT_EVENT_LOOP=1
     ;;
 
 cairo-gtk2-dfb)
     MOZ_WIDGET_TOOLKIT=gtk2
     MOZ_ENABLE_GTK2=1
     MOZ_WEBGL=
 
     AC_DEFINE(MOZ_DFB)
@@ -5297,16 +5298,20 @@ if test "$MOZ_PDF_PRINTING"; then
    PDF_SURFACE_FEATURE="#define CAIRO_HAS_PDF_SURFACE 1"
    AC_DEFINE(MOZ_PDF_PRINTING)
 fi
 
 if test "$MOZ_ENABLE_XREMOTE"; then
     AC_DEFINE(MOZ_ENABLE_XREMOTE)
 fi
 
+if test "$MOZ_INSTRUMENT_EVENT_LOOP"; then
+   AC_DEFINE(MOZ_INSTRUMENT_EVENT_LOOP)
+fi
+
 if test "$COMPILE_ENVIRONMENT"; then
   if test "$MOZ_ENABLE_GTK2"; then
     if test "$MOZ_X11"; then
       GDK_PACKAGES=gdk-x11-2.0
     elif test "$MOZ_DFB"; then
       PKG_CHECK_MODULES(MOZ_DFB, directfb >= 1.1.0)
       GDK_PACKAGES=directfb
     fi
@@ -9180,16 +9185,17 @@ AC_SUBST(MOZ_OGG)
 AC_SUBST(MOZ_ALSA_LIBS)
 AC_SUBST(VPX_AS)
 AC_SUBST(VPX_ASFLAGS)
 AC_SUBST(VPX_DASH_C_FLAG)
 AC_SUBST(VPX_AS_CONVERSION)
 AC_SUBST(VPX_ASM_SUFFIX)
 AC_SUBST(VPX_X86_ASM)
 AC_SUBST(VPX_ARM_ASM)
+AC_SUBST(MOZ_INSTRUMENT_EVENT_LOOP)
 AC_SUBST(LIBJPEG_TURBO_AS)
 AC_SUBST(LIBJPEG_TURBO_ASFLAGS)
 AC_SUBST(LIBJPEG_TURBO_X86_ASM)
 AC_SUBST(LIBJPEG_TURBO_X64_ASM)
 
 if test "$USING_HCC"; then
    CC='${topsrcdir}/build/hcc'
    CC="$CC '$_OLDCC'"
new file mode 100644
--- /dev/null
+++ b/toolkit/xre/EventTracer.cpp
@@ -0,0 +1,182 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ * The Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Ted Mielczarek <ted.mielczarek@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+/*
+ * Event loop instrumentation. This code attempts to measure the
+ * latency of the UI-thread event loop by firing native events at it from
+ * a background thread, and measuring how long it takes for them
+ * to be serviced. The measurement interval (kMeasureInterval, below)
+ * is also used as the upper bound of acceptable response time.
+ * When an event takes longer than that interval to be serviced,
+ * a sample will be written to the log.
+ *
+ * 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.
+ *
+ * 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:
+ *   MOZ_EVENT_TRACE stop <timestamp>
+ * will be output.
+ *
+ * When an event servicing time exceeds the threshold, a line of the form:
+ *   MOZ_EVENT_TRACE sample <timestamp> <duration>
+ * will be output, where <duration> is the number of milliseconds that
+ * it took for the event to be serviced.
+ */
+
+#include "EventTracer.h"
+
+#include <stdio.h>
+
+#include "mozilla/TimeStamp.h"
+#include "mozilla/WidgetTraceEvent.h"
+#include <prenv.h>
+#include <prinrval.h>
+#include <prthread.h>
+#include <prtime.h>
+
+using mozilla::TimeDuration;
+using mozilla::TimeStamp;
+using mozilla::FireAndWaitForTracerEvent;
+
+namespace {
+
+PRThread* sTracerThread = NULL;
+bool sExit = false;
+
+/*
+ * The tracer thread fires events at the native event loop roughly
+ * every kMeasureInterval. It will sleep to attempt not to send them
+ * more quickly, but if the response time is longer than kMeasureInterval
+ * 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)
+{
+  // This should be set to the maximum latency we'd like to allow
+  // for responsiveness.
+  const PRIntervalTime kMeasureInterval = PR_MillisecondsToInterval(50);
+
+  FILE* log = NULL;
+  char* envfile = PR_GetEnv("MOZ_INSTRUMENT_EVENT_LOOP_OUTPUT");
+  if (envfile) {
+    log = fopen(envfile, "w");
+  }
+  if (log == NULL)
+    log = stdout;
+
+  fprintf(log, "MOZ_EVENT_TRACE start %llu\n", PR_Now() / PR_USEC_PER_MSEC);
+
+  while (!sExit) {
+    TimeStamp start(TimeStamp::Now());
+    PRIntervalTime next_sleep = kMeasureInterval;
+
+    //TODO: only wait up to a maximum of kMeasureInterval, 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) {
+        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());
+      }
+      else {
+        // Don't sleep at all if this event took longer than the measure
+        // interval to deliver.
+        next_sleep = 0;
+      }
+    }
+
+    if (next_sleep != 0 && !sExit) {
+      PR_Sleep(next_sleep);
+    }
+  }
+
+  fprintf(log, "MOZ_EVENT_TRACE stop %llu\n", PR_Now() / PR_USEC_PER_MSEC);
+
+  if (log != stdout)
+    fclose(log);
+}
+
+} // namespace
+
+namespace mozilla {
+
+bool InitEventTracing()
+{
+  // Create a thread that will fire events back at the
+  // main thread to measure responsiveness.
+  NS_ABORT_IF_FALSE(!sTracerThread, "Event tracing already initialized!");
+  sTracerThread = PR_CreateThread(PR_USER_THREAD,
+                                  TracerThread,
+                                  NULL,
+                                  PR_PRIORITY_NORMAL,
+                                  PR_GLOBAL_THREAD,
+                                  PR_JOINABLE_THREAD,
+                                  0);
+  return sTracerThread != NULL;
+}
+
+void ShutdownEventTracing()
+{
+  sExit = true;
+  if (sTracerThread)
+    PR_JoinThread(sTracerThread);
+  sTracerThread = NULL;
+}
+
+}  // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/toolkit/xre/EventTracer.h
@@ -0,0 +1,54 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ * The Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Ted Mielczarek <ted.mielczarek@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef XRE_EVENTTRACER_H_
+#define XRE_EVENTTRACER_H_
+
+namespace mozilla {
+
+// Create a thread that will fire events back at the
+// main thread to measure responsiveness. Return true
+// if the thread was created successfully.
+bool InitEventTracing();
+
+// Signal the background thread to stop, and join it.
+// Must be called from the same thread that called InitEventTracing.
+void ShutdownEventTracing();
+
+}  // namespace mozilla
+
+#endif /* XRE_EVENTTRACER_H_ */
--- a/toolkit/xre/Makefile.in
+++ b/toolkit/xre/Makefile.in
@@ -61,16 +61,20 @@ CPPSRCS = \
   nsAppRunner.cpp \
   nsConsoleWriter.cpp \
   nsXREDirProvider.cpp \
   nsNativeAppSupportBase.cpp \
   nsAppData.cpp \
   nsSigHandlers.cpp \
   $(NULL)
 
+ifdef MOZ_INSTRUMENT_EVENT_LOOP
+CPPSRCS += EventTracer.cpp
+endif
+
 ifdef MOZ_SPLASHSCREEN
 ifeq ($(OS_ARCH),WINCE)
 CPPSRCS += nsSplashScreenWin.cpp
 else
 CPPSRCS += nsSplashScreenDummy.cpp
 endif
 endif
 
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -57,16 +57,20 @@
 #endif // MOZ_WIDGET_QT
 
 #include "mozilla/dom/ContentParent.h"
 using mozilla::dom::ContentParent;
 
 #include "nsAppRunner.h"
 #include "nsUpdateDriver.h"
 
+#ifdef MOZ_INSTRUMENT_EVENT_LOOP
+#include "EventTracer.h"
+#endif
+
 #ifdef XP_MACOSX
 #include "MacLaunchHelper.h"
 #include "MacApplicationDelegate.h"
 #include "MacAutoreleasePool.h"
 #endif
 
 #ifdef XP_OS2
 #include "private/pprthred.h"
@@ -3739,16 +3743,23 @@ XRE_main(int argc, char* argv[], const n
           if (remoteService)
             remoteService->Startup(gAppData->name,
                                    PromiseFlatCString(profileName).get());
 #endif /* MOZ_ENABLE_XREMOTE */
 
           nativeApp->Enable();
         }
 
+#ifdef MOZ_INSTRUMENT_EVENT_LOOP
+        bool event_tracing_running = false;
+        if (PR_GetEnv("MOZ_INSTRUMENT_EVENT_LOOP")) {
+          event_tracing_running = mozilla::InitEventTracing();
+        }
+#endif /* MOZ_INSTRUMENT_EVENT_LOOP */
+
         NS_TIME_FUNCTION_MARK("Next: Run");
 
         NS_TIME_FUNCTION_MARK("appStartup->Run");
 
         MOZ_SPLASHSCREEN_UPDATE(90);
         {
           NS_TIMELINE_ENTER("appStartup->Run");
           rv = appStartup->Run();
@@ -3758,16 +3769,21 @@ XRE_main(int argc, char* argv[], const n
             gLogConsoleErrors = PR_TRUE;
           }
         }
 
         NS_TIME_FUNCTION_MARK("Next: Finish");
 
         NS_TIME_FUNCTION_MARK("appStartup->Run done");
 
+#ifdef MOZ_INSTRUMENT_EVENT_LOOP
+        if (event_tracing_running)
+          mozilla::ShutdownEventTracing();
+#endif
+
         // Check for an application initiated restart.  This is one that
         // corresponds to nsIAppStartup.quit(eRestart)
         if (rv == NS_SUCCESS_RESTART_APP)
           appInitiatedRestart = PR_TRUE;
 
         if (!shuttingDown) {
 #ifdef MOZ_ENABLE_XREMOTE
           // shut down the x remote proxy window
--- a/widget/public/Makefile.in
+++ b/widget/public/Makefile.in
@@ -41,22 +41,27 @@ srcdir		= @srcdir@
 VPATH		= @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 MODULE		= widget
 XPIDL_MODULE	= widget
 GRE_MODULE	= 1
 
-EXPORTS_NAMESPACES = IPC
+EXPORTS_NAMESPACES = IPC mozilla
 
 EXPORTS_IPC = \
 		nsGUIEventIPC.h \
 		$(NULL)
 
+ifdef MOZ_INSTRUMENT_EVENT_LOOP
+EXPORTS_mozilla = \
+  WidgetTraceEvent.h
+endif
+
 EXPORTS		= \
 		nsIWidget.h \
 		nsGUIEvent.h \
 		nsEvent.h \
 		nsNativeWidget.h \
 		nsWidgetInitData.h \
 		nsWidgetsCID.h \
 		nsILookAndFeel.h \
new file mode 100644
--- /dev/null
+++ b/widget/public/WidgetTraceEvent.h
@@ -0,0 +1,50 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ * The Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Ted Mielczarek <ted.mielczarek@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#ifndef WIDGET_PUBLIC_WIDGETTRACEEVENT_H_
+#define WIDGET_PUBLIC_WIDGETTRACEEVENT_H_
+
+namespace mozilla {
+
+// Fire a tracer event at the UI-thread event loop, and block until
+// the event is processed. This should only be called by
+// a thread that's not the UI thread.
+bool FireAndWaitForTracerEvent();
+
+}
+
+#endif  // WIDGET_PUBLIC_WIDGETTRACEEVENT_H_
--- a/widget/src/gtk2/Makefile.in
+++ b/widget/src/gtk2/Makefile.in
@@ -57,33 +57,34 @@ NATIVE_THEME_SUPPORT = 1
 CSRCS		= \
 		mozcontainer.c \
 		$(NULL)
 
 ifdef ACCESSIBILITY
 CSRCS += maiRedundantObjectFactory.c
 endif
 
-CPPSRCS		= \
-		nsWindow.cpp \
-		nsAppShell.cpp \
-		nsWidgetFactory.cpp \
-		nsToolkit.cpp \
-		nsBidiKeyboard.cpp \
-		nsLookAndFeel.cpp \
-		nsGtkKeyUtils.cpp \
-		nsFilePicker.cpp \
-		nsSound.cpp \
-		nsNativeKeyBindings.cpp \
-		nsScreenGtk.cpp \
-		nsScreenManagerGtk.cpp \
-		nsImageToPixbuf.cpp \
-		nsAccessibilityHelper.cpp \
-		nsGtkIMModule.cpp \
-		$(NULL)
+CPPSRCS = \
+  nsWindow.cpp \
+  nsAppShell.cpp \
+  nsWidgetFactory.cpp \
+  nsToolkit.cpp \
+  nsBidiKeyboard.cpp \
+  nsLookAndFeel.cpp \
+  nsGtkKeyUtils.cpp \
+  nsFilePicker.cpp \
+  nsSound.cpp \
+  nsNativeKeyBindings.cpp \
+  nsScreenGtk.cpp \
+  nsScreenManagerGtk.cpp \
+  nsImageToPixbuf.cpp \
+  nsAccessibilityHelper.cpp \
+  nsGtkIMModule.cpp \
+  WidgetTraceEvent.cpp \
+  $(NULL)
 
 ifdef MOZ_X11
 CPPSRCS         += nsIdleServiceGTK.cpp
 endif
 
 ifdef NS_PRINTING
 CPPSRCS		+= \
 		nsDeviceContextSpecG.cpp \
new file mode 100644
--- /dev/null
+++ b/widget/src/gtk2/WidgetTraceEvent.cpp
@@ -0,0 +1,86 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is
+ * The Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2011
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *   Ted Mielczarek <ted.mielczarek@gmail.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+#include "mozilla/WidgetTraceEvent.h"
+
+#include <glib.h>
+#include <mozilla/CondVar.h>
+#include <mozilla/Mutex.h>
+#include <stdio.h>
+
+using mozilla::CondVar;
+using mozilla::Mutex;
+using mozilla::MutexAutoLock;
+
+namespace {
+
+Mutex sMutex("Event tracer thread mutex");
+CondVar sCondVar(sMutex, "Event tracer thread condvar");
+bool sTracerProcessed = false;
+
+// This function is called from the main (UI) thread.
+gboolean TracerCallback(gpointer data)
+{
+  MutexAutoLock lock(sMutex);
+  NS_ABORT_IF_FALSE(!sTracerProcessed, "Tracer synchronization state is wrong");
+  sTracerProcessed = true;
+  sCondVar.Notify();
+  return FALSE;
+}
+
+} // namespace
+
+namespace mozilla {
+
+// This function is called from the background tracer thread.
+bool FireAndWaitForTracerEvent()
+{
+  // Send a default-priority idle event through the
+  // event loop, and wait for it to finish.
+  MutexAutoLock lock(sMutex);
+  NS_ABORT_IF_FALSE(!sTracerProcessed, "Tracer synchronization state is wrong");
+  g_idle_add_full(G_PRIORITY_DEFAULT,
+                  TracerCallback,
+                  NULL,
+                  NULL);
+  while (!sTracerProcessed)
+    sCondVar.Wait();
+  sTracerProcessed = false;
+  return true;
+}
+
+}  // namespace mozilla