Bug 1443932 - part 1 - add nsIThreadManager::kThreadPoolStackSize; r=erahm
authorNathan Froyd <froydnj@mozilla.com>
Wed, 07 Mar 2018 20:33:17 -0500
changeset 462133 e98924c2ee353146521b663c3f413e0d7ab50169
parent 462132 dd3beebf0ce3dda4f359907379aaa372449b022d
child 462134 18336fd064bd62d260ee771b82e7cc2e73059c0b
push id1683
push usersfraser@mozilla.com
push dateThu, 26 Apr 2018 16:43:40 +0000
treeherdermozilla-release@5af6cb21869d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1443932
milestone60.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 1443932 - part 1 - add nsIThreadManager::kThreadPoolStackSize; r=erahm A lot of our thread pools use the default stack size for the platform they're on, which can be rather large (8MB, usually, on Linux and OS X) and is probably too much for the typical thread in the thread pool regardless. SharedThreadPool already has some logic for selecting a reasonable stack size for worker threads; let's move that logic to nsIThreadManager so that logic (and constant) can be shared more broadly. (That we already have a couple of instances of SharedThreadPool usage solely for this constant suggests that it is a concept that should be available in a more central location.)
dom/media/FileBlockCache.cpp
dom/media/doctor/DDMediaLogs.cpp
xpcom/threads/SharedThreadPool.cpp
xpcom/threads/SharedThreadPool.h
xpcom/threads/nsIThreadManager.idl
--- a/dom/media/FileBlockCache.cpp
+++ b/dom/media/FileBlockCache.cpp
@@ -2,21 +2,21 @@
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "FileBlockCache.h"
 #include "MediaCache.h"
 #include "MediaPrefs.h"
-#include "mozilla/SharedThreadPool.h"
 #include "VideoUtils.h"
 #include "prio.h"
 #include <algorithm>
 #include "nsAnonymousTemporaryFile.h"
+#include "nsIThreadManager.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/SystemGroup.h"
 #include "nsXULAppAPI.h"
 
 namespace mozilla {
 
 #undef LOG
 LazyLogModule gFileBlockCacheLog("FileBlockCache");
@@ -77,17 +77,17 @@ nsresult
 FileBlockCache::Init()
 {
   LOG("Init()");
   MutexAutoLock mon(mDataMutex);
   MOZ_ASSERT(!mThread);
   nsresult rv = NS_NewNamedThread("FileBlockCache",
                                   getter_AddRefs(mThread),
                                   nullptr,
-                                  SharedThreadPool::kStackSize);
+                                  nsIThreadManager::kThreadPoolStackSize);
   if (NS_FAILED(rv)) {
     return rv;
   }
 
   if (XRE_IsParentProcess()) {
     RefPtr<FileBlockCache> self = this;
     rv = mThread->Dispatch(
       NS_NewRunnableFunction("FileBlockCache::Init",
--- a/dom/media/doctor/DDMediaLogs.cpp
+++ b/dom/media/doctor/DDMediaLogs.cpp
@@ -2,29 +2,29 @@
 /* vim:set ts=2 sw=2 sts=2 et cindent: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "DDMediaLogs.h"
 
 #include "DDLogUtils.h"
+#include "nsIThreadManager.h"
 #include "mozilla/JSONWriter.h"
-#include "mozilla/SharedThreadPool.h"
 
 namespace mozilla {
 
 /* static */ DDMediaLogs::ConstructionResult
 DDMediaLogs::New()
 {
   nsCOMPtr<nsIThread> mThread;
   nsresult rv = NS_NewNamedThread("DDMediaLogs",
                                   getter_AddRefs(mThread),
                                   nullptr,
-                                  SharedThreadPool::kStackSize);
+                                  nsIThreadManager::kThreadPoolStackSize);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return { rv, nullptr };
   }
 
   return { rv, new DDMediaLogs(Move(mThread)) };
 }
 
 DDMediaLogs::DDMediaLogs(nsCOMPtr<nsIThread>&& aThread)
--- a/xpcom/threads/SharedThreadPool.cpp
+++ b/xpcom/threads/SharedThreadPool.cpp
@@ -9,16 +9,17 @@
 #include "mozilla/ReentrantMonitor.h"
 #include "mozilla/Services.h"
 #include "mozilla/StaticPtr.h"
 #include "nsDataHashtable.h"
 #include "nsXPCOMCIDInternal.h"
 #include "nsComponentManagerUtils.h"
 #include "nsIObserver.h"
 #include "nsIObserverService.h"
+#include "nsIThreadManager.h"
 #ifdef XP_WIN
 #include "ThreadPoolCOMListener.h"
 #endif
 
 namespace mozilla {
 
 // Created and destroyed on the main thread.
 static StaticAutoPtr<ReentrantMonitor> sMonitor;
@@ -215,17 +216,17 @@ CreateThreadPool(const nsCString& aName)
 {
   nsresult rv;
   nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID, &rv);
   NS_ENSURE_SUCCESS(rv, nullptr);
 
   rv = pool->SetName(aName);
   NS_ENSURE_SUCCESS(rv, nullptr);
 
-  rv = pool->SetThreadStackSize(SharedThreadPool::kStackSize);
+  rv = pool->SetThreadStackSize(nsIThreadManager::kThreadPoolStackSize);
   NS_ENSURE_SUCCESS(rv, nullptr);
 
 #ifdef XP_WIN
   // Ensure MSCOM is initialized on the thread pools threads.
   nsCOMPtr<nsIThreadPoolListener> listener = new MSCOMInitThreadPoolListener();
   rv = pool->SetListener(listener);
   NS_ENSURE_SUCCESS(rv, nullptr);
 #endif
--- a/xpcom/threads/SharedThreadPool.h
+++ b/xpcom/threads/SharedThreadPool.h
@@ -81,27 +81,16 @@ public:
 
   // Creates necessary statics. Called once at startup.
   static void InitStatics();
 
   // Spins the event loop until all thread pools are shutdown.
   // *Must* be called on the main thread.
   static void SpinUntilEmpty();
 
-#if defined(MOZ_ASAN)
-  // Use the system default in ASAN builds, because the default is assumed to be
-  // larger than the size we want to use and is hopefully sufficient for ASAN.
-  static const uint32_t kStackSize = nsIThreadManager::DEFAULT_STACK_SIZE;
-#elif defined(XP_WIN) || defined(XP_MACOSX) || defined(LINUX)
-  static const uint32_t kStackSize = (256 * 1024);
-#else
-  // All other platforms use their system defaults.
-  static const uint32_t kStackSize = nsIThreadManager::DEFAULT_STACK_SIZE;
-#endif
-
 private:
 
   // Returns whether there are no pools in existence at the moment.
   static bool IsEmpty();
 
   // Creates a singleton SharedThreadPool wrapper around aPool.
   // aName is the name of the aPool, and is used to lookup the
   // SharedThreadPool in the hash table of all created pools.
--- a/xpcom/threads/nsIThreadManager.idl
+++ b/xpcom/threads/nsIThreadManager.idl
@@ -28,16 +28,40 @@ interface nsINestedEventLoopCondition : 
 interface nsIThreadManager : nsISupports
 {
   /**
    * Default number of bytes reserved for a thread's stack, if no stack size
    * is specified in newThread(). 0 means use platform default.
    */
   const unsigned long DEFAULT_STACK_SIZE = 0;
 
+%{C++
+  /* DEFAULT_STACK_SIZE can be a little overzealous for many platforms.  On
+   * Linux and OS X, for instance, the default thread stack size is whatever
+   * getrlimit(RLIMIT_STACK) returns, which is often set at 8MB.  The
+   * default on Windows is 1MB, which is a little more reasonable.  But
+   * for thread pools, each individual thread often doesn't need that much
+   * stack space.
+   *
+   * We therefore have a separate setting for a reasonable stack size for
+   * a thread pool worker thread.
+   */
+#if defined(MOZ_ASAN) || defined(MOZ_TSAN)
+  // Use the system default in ASAN builds, because the default is assumed
+  // to be larger than the size we want to use and is hopefully sufficient
+  // for ASAN.
+  static const uint32_t kThreadPoolStackSize = DEFAULT_STACK_SIZE;
+#elif defined(XP_WIN) || defined(XP_MACOSX) || defined(LINUX)
+  static const uint32_t kThreadPoolStackSize = (256 * 1024);
+#else
+  // All other platforms use their system default.
+  static const uint32_t kThreadPoolStackSize = DEFAULT_STACK_SIZE;
+#endif
+%}
+
   /**
    * Create a new thread (a global, user PRThread).
    *
    * @param creationFlags
    *   Reserved for future use.  Pass 0.
    * @param stackSize
    *   Number of bytes to reserve for the thread's stack.
    *