Bug 999071 - Don't notify observers in profiler when used by Android ANR reporter. r=BenWa, a=sledru
authorJim Chen <nchen@mozilla.com>
Mon, 28 Apr 2014 22:20:51 -0400
changeset 200103 95b027dc3cf99e0a989842629f249dfa624293a6
parent 200102 57afb46280b3c23e90ff85d2549ab45d38dd87a6
child 200104 36532bdbbc5c36b756af3a74790c77aa8b8490f8
push id486
push userasasaki@mozilla.com
push dateMon, 14 Jul 2014 18:39:42 +0000
treeherdermozilla-release@d33428174ff1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersBenWa, sledru
bugs999071
milestone31.0a2
Bug 999071 - Don't notify observers in profiler when used by Android ANR reporter. r=BenWa, a=sledru
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_;