Bug 999071 - Don't notify observers in profiler when used by Android ANR reporter; r=BenWa
authorJim Chen <nchen@mozilla.com>
Mon, 28 Apr 2014 22:20:51 -0400
changeset 181052 b4d1efedb68f041639da29de80403a728eeafb3c
parent 181051 07196a58650f09d38bfb34ad56588a9878a48259
child 181053 77ed9758681b101e3a889a7712a1d083013c9360
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersBenWa
bugs999071
milestone32.0a1
Bug 999071 - Don't notify observers in profiler when used by Android ANR reporter; r=BenWa
tools/profiler/TableTicker.cpp
tools/profiler/platform.cpp
tools/profiler/platform.h
--- a/tools/profiler/TableTicker.cpp
+++ b/tools/profiler/TableTicker.cpp
@@ -278,40 +278,41 @@ void TableTicker::StreamJSObject(JSStrea
             continue;
 
           MutexAutoLock lock(*sRegisteredThreads->at(i)->Profile()->GetMutex());
 
           sRegisteredThreads->at(i)->Profile()->StreamJSObject(b);
         }
       }
 
-      // Send a event asking any subprocesses (plugins) to
-      // give us their information
-      SubprocessClosure closure(&b);
-      nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
-      if (os) {
-        nsRefPtr<ProfileSaveEvent> pse = new ProfileSaveEvent(SubProcessCallback, &closure);
-        os->NotifyObservers(pse, "profiler-subprocess", nullptr);
+      if (Sampler::CanNotifyObservers()) {
+        // Send a event asking any subprocesses (plugins) to
+        // give us their information
+        SubprocessClosure closure(&b);
+        nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
+        if (os) {
+          nsRefPtr<ProfileSaveEvent> pse = new ProfileSaveEvent(SubProcessCallback, &closure);
+          os->NotifyObservers(pse, "profiler-subprocess", nullptr);
+        }
       }
 
   #if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
       if (ProfileJava()) {
         mozilla::widget::android::GeckoJavaSampler::PauseJavaProfiling();
 
         BuildJavaThreadJSObject(b);
 
         mozilla::widget::android::GeckoJavaSampler::UnpauseJavaProfiling();
       }
   #endif
 
       SetPaused(false);
     b.EndArray();
 
   b.EndObject();
-
 }
 
 // END SaveProfileTask et al
 ////////////////////////////////////////////////////////////////////////
 
 static
 void addDynamicTag(ThreadProfile &aProfile, char aTagName, const char *aStr)
 {
--- a/tools/profiler/platform.cpp
+++ b/tools/profiler/platform.cpp
@@ -693,19 +693,21 @@ void mozilla_sampler_start(int aProfileE
       sInterposeObserver = new mozilla::ProfilerIOInterposeObserver();
     }
     mozilla::IOInterposer::Register(mozilla::IOInterposeObserver::OpAll,
                                     sInterposeObserver);
   }
 
   sIsProfiling = true;
 
-  nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
-  if (os)
-    os->NotifyObservers(nullptr, "profiler-started", nullptr);
+  if (Sampler::CanNotifyObservers()) {
+    nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
+    if (os)
+      os->NotifyObservers(nullptr, "profiler-started", nullptr);
+  }
 
   LOG("END   mozilla_sampler_start");
 }
 
 void mozilla_sampler_stop()
 {
   LOG("BEGIN mozilla_sampler_stop");
 
@@ -744,19 +746,21 @@ void mozilla_sampler_stop()
   }
 
   mozilla::IOInterposer::Unregister(mozilla::IOInterposeObserver::OpAll,
                                     sInterposeObserver);
   sInterposeObserver = nullptr;
 
   sIsProfiling = false;
 
-  nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
-  if (os)
-    os->NotifyObservers(nullptr, "profiler-stopped", nullptr);
+  if (Sampler::CanNotifyObservers()) {
+    nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
+    if (os)
+      os->NotifyObservers(nullptr, "profiler-stopped", nullptr);
+  }
 
   LOG("END   mozilla_sampler_stop");
 }
 
 bool mozilla_sampler_is_paused() {
   if (Sampler::GetActiveSampler()) {
     return Sampler::GetActiveSampler()->IsPaused();
   } else {
--- a/tools/profiler/platform.h
+++ b/tools/profiler/platform.h
@@ -39,16 +39,17 @@
 #include <pthread.h>
 #endif
 
 #include <stdint.h>
 #include <math.h>
 #include "mozilla/unused.h"
 #include "mozilla/TimeStamp.h"
 #include "mozilla/Mutex.h"
+#include "MainThreadUtils.h"
 #include "PlatformMacros.h"
 #include "v8-support.h"
 #include <vector>
 
 #ifdef XP_WIN
 #include <windows.h>
 #endif
 
@@ -348,16 +349,27 @@ class Sampler {
   static void Startup();
   // Should only be called on shutdown
   static void Shutdown();
 
   static TableTicker* GetActiveSampler() { return sActiveSampler; }
   static void SetActiveSampler(TableTicker* sampler) { sActiveSampler = sampler; }
 
   static mozilla::Mutex* sRegisteredThreadsMutex;
+
+  static bool CanNotifyObservers() {
+#if defined(SPS_OS_android) && !defined(MOZ_WIDGET_GONK)
+    // Android ANR reporter uses the profiler off the main thread
+    return NS_IsMainThread();
+#else
+    MOZ_ASSERT(NS_IsMainThread());
+    return true;
+#endif
+  }
+
  protected:
   static std::vector<ThreadInfo*>* sRegisteredThreads;
   static TableTicker* sActiveSampler;
 
  private:
   void SetActive(bool value) { NoBarrier_Store(&active_, value); }
 
   const double interval_;