Bug 1444976 - Decide how many threads to use in the threadpool using the number of threads the machine can run. DO NOT LAND draft
authorPaul Adenot <paul@paul.cx>
Thu, 15 Mar 2018 14:39:07 +0100
changeset 770066 8930776bdae783e491d60fed3bef767827e067cc
parent 770065 f9af0e1376e098d6210eadd8913c440dc30845b9
child 770492 1826d8084197404261fd0acfe271723c1d42cd56
push id103306
push userpaul@paul.cx
push dateTue, 20 Mar 2018 17:27:46 +0000
bugs1444976
milestone60.0a1
Bug 1444976 - Decide how many threads to use in the threadpool using the number of threads the machine can run. DO NOT LAND MozReview-Commit-ID: LlLCKjeJ6Ez
dom/media/VideoUtils.cpp
--- a/dom/media/VideoUtils.cpp
+++ b/dom/media/VideoUtils.cpp
@@ -1,14 +1,16 @@
 /* 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 "VideoUtils.h"
 
+#include <unistd.h>
+
 #include "ImageContainer.h"
 #include "MediaContainerType.h"
 #include "MediaPrefs.h"
 #include "MediaResource.h"
 #include "TimeUnits.h"
 #include "VorbisUtils.h"
 #include "mozilla/Base64.h"
 #include "mozilla/SharedThreadPool.h"
@@ -211,17 +213,33 @@ already_AddRefed<SharedThreadPool> GetMe
       break;
     default:
       MOZ_FALLTHROUGH_ASSERT("Unexpected MediaThreadType");
     case MediaThreadType::PLAYBACK:
       name = "MediaPlayback";
       break;
   }
 
-  static const uint32_t kMediaThreadPoolDefaultCount = 4;
+
+    static uint32_t ncpus = 0;
+
+    if (ncpus == 0) {
+#if defined(_SC_NPROCESSORS_CONF)
+        long n = sysconf(_SC_NPROCESSORS_CONF);
+        ncpus = (n > 0) ? uint32_t(n) : 1;
+#elif defined(_SC_NPROCESSORS_ONLN)
+        long n = sysconf(_SC_NPROCESSORS_ONLN);
+        ncpus = (n > 0) ? uint32_t(n) : 1;
+#else
+        ncpus = 1;
+#endif
+    }
+
+  // - 1 for main thread - 1 for audio thread
+  static const uint32_t kMediaThreadPoolDefaultCount = ncpus - 2;
   return SharedThreadPool::
     Get(nsDependentCString(name), kMediaThreadPoolDefaultCount);
 }
 
 bool
 ExtractVPXCodecDetails(const nsAString& aCodec,
                        uint8_t& aProfile,
                        uint8_t& aLevel,