Bug 1320256 - register decode threads to Gecko profiler. r=cyu,tnikkel
authorShih-Chiang Chien <schien@mozilla.com>
Fri, 25 Nov 2016 15:00:10 +0800
changeset 324596 2281f43b043b0ac5b842b5bcae7fc0995737fb19
parent 324595 9d051cf1d568b06dada2fe2cda2b9844a5bc1b25
child 324597 53d1dbd5e9059e8fb7319c9a13ec43bcc41f6383
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerscyu, tnikkel
bugs1320256
milestone53.0a1
Bug 1320256 - register decode threads to Gecko profiler. r=cyu,tnikkel MozReview-Commit-ID: CftQ4LwzqmN
image/DecodePool.cpp
--- a/image/DecodePool.cpp
+++ b/image/DecodePool.cpp
@@ -7,16 +7,17 @@
 
 #include <algorithm>
 
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Monitor.h"
 #include "nsCOMPtr.h"
 #include "nsIObserverService.h"
 #include "nsIThreadPool.h"
+#include "nsPrintfCString.h"
 #include "nsThreadManager.h"
 #include "nsThreadUtils.h"
 #include "nsXPCOMCIDInternal.h"
 #include "prsystem.h"
 
 #include "gfxPrefs.h"
 
 #include "Decoder.h"
@@ -154,51 +155,75 @@ private:
   nsTArray<RefPtr<IDecodingTask>> mHighPriorityQueue;
   nsTArray<RefPtr<IDecodingTask>> mLowPriorityQueue;
   bool mShuttingDown;
 };
 
 class DecodePoolWorker : public Runnable
 {
 public:
-  explicit DecodePoolWorker(DecodePoolImpl* aImpl) : mImpl(aImpl) { }
+  explicit DecodePoolWorker(DecodePoolImpl* aImpl)
+    : mImpl(aImpl)
+    , mSerialNumber(++sNextSerialNumber)
+  { }
 
   NS_IMETHOD Run() override
   {
+#ifdef MOZ_ENABLE_PROFILER_SPS
+    char stackBaseGuess; // Need to be the first variable of main loop function.
+#endif // MOZ_ENABLE_PROFILER_SPS
+
     MOZ_ASSERT(!NS_IsMainThread());
 
     mImpl->InitCurrentThread();
 
     nsCOMPtr<nsIThread> thisThread;
     nsThreadManager::get().GetCurrentThread(getter_AddRefs(thisThread));
 
+#ifdef MOZ_ENABLE_PROFILER_SPS
+    {
+      const nsPrintfCString threadName("ImgDecoder#%lu", mSerialNumber);
+      profiler_register_thread(threadName.get(), &stackBaseGuess);
+    }
+#endif // MOZ_ENABLE_PROFILER_SPS
+
     do {
       Work work = mImpl->PopWork();
       switch (work.mType) {
         case Work::Type::TASK:
           work.mTask->Run();
           break;
 
         case Work::Type::SHUTDOWN:
           DecodePoolImpl::ShutdownThread(thisThread);
+
+#ifdef MOZ_ENABLE_PROFILER_SPS
+          profiler_unregister_thread();
+#endif // MOZ_ENABLE_PROFILER_SPS
+
           return NS_OK;
 
         default:
           MOZ_ASSERT_UNREACHABLE("Unknown work type");
       }
     } while (true);
 
     MOZ_ASSERT_UNREACHABLE("Exiting thread without Work::Type::SHUTDOWN");
     return NS_OK;
   }
 
 private:
+  static uint32_t sNextSerialNumber;
+
   RefPtr<DecodePoolImpl> mImpl;
+  uint32_t mSerialNumber;
 };
 
+uint32_t DecodePoolWorker::sNextSerialNumber = 0;
+
 /* static */ void
 DecodePool::Initialize()
 {
   MOZ_ASSERT(NS_IsMainThread());
   sNumCores = max<int32_t>(PR_GetNumberOfProcessors(), 1);
   DecodePool::Singleton();
 }