Bug 881954 - Set the thread limit to the idle limit for WMFByteStream's thread pool. This stops the pool from constantly creating and destroying threads when under load, freeing up virtual address space and reducing our thread count. r=padenot
authorChris Pearce <cpearce@mozilla.com>
Fri, 14 Jun 2013 09:07:59 +1200
changeset 146484 3d4a1926278d0f03a412cfc92592f3df626cbfca
parent 146483 dc08e8496fc6c00f47307ebf8ca38cc95011053b
child 146485 ef9b20568ecda33718c89de3822e6f0526ee1696
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs881954
milestone24.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 881954 - Set the thread limit to the idle limit for WMFByteStream's thread pool. This stops the pool from constantly creating and destroying threads when under load, freeing up virtual address space and reducing our thread count. r=padenot
content/media/wmf/WMFByteStream.cpp
--- a/content/media/wmf/WMFByteStream.cpp
+++ b/content/media/wmf/WMFByteStream.cpp
@@ -23,16 +23,19 @@ namespace mozilla {
 
 #ifdef PR_LOGGING
 PRLogModuleInfo* gWMFByteStreamLog = nullptr;
 #define LOG(...) PR_LOG(gWMFByteStreamLog, PR_LOG_DEBUG, (__VA_ARGS__))
 #else
 #define LOG(...)
 #endif
 
+// Limit the number of threads that we use for IO.
+static const uint32_t NumWMFIoThreads = 4;
+
 // Thread pool listener which ensures that MSCOM is initialized and
 // deinitialized on the thread pool thread. We can call back into WMF
 // on this thread, so we need MSCOM working.
 class ThreadPoolListener MOZ_FINAL : public nsIThreadPoolListener {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSITHREADPOOLLISTENER
 };
@@ -138,16 +141,29 @@ WMFByteStream::Init()
     nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID, &rv);
     NS_ENSURE_SUCCESS(rv, rv);
 
     sThreadPool = pool;
     NS_ADDREF(sThreadPool);
 
     rv = sThreadPool->SetName(NS_LITERAL_CSTRING("WMFByteStream Async Read Pool"));
     NS_ENSURE_SUCCESS(rv, rv);
+    
+    // We limit the number of threads that we use for IO. Note that the thread
+    // limit is the same as the idle limit so that we're not constantly creating
+    // and destroying threads. When the thread pool threads shutdown they
+    // dispatch an event to the main thread to call nsIThread::Shutdown(),
+    // and if we're very busy that can take a while to run, and we end up with
+    // dozens of extra threads. Note that threads that are idle for 60 seconds
+    // are shutdown naturally.
+    rv = sThreadPool->SetThreadLimit(NumWMFIoThreads);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = sThreadPool->SetIdleThreadLimit(NumWMFIoThreads);
+    NS_ENSURE_SUCCESS(rv, rv);
 
     nsCOMPtr<nsIThreadPoolListener> listener = new ThreadPoolListener();
     rv = sThreadPool->SetListener(listener);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   sThreadPoolRefCnt++;
 
   // Store a ref to the thread pool, so that we keep the pool alive as long as