Bug 1546544 - Reduce navigator.hardwareConcurrency to account for TCSM r=luke
☠☠ backed out by 18ae393f5416 ☠ ☠
authorHaik Aftandilian <haftandilian@mozilla.com>
Mon, 06 May 2019 06:09:11 +0000
changeset 472772 d4b67960c0f921396353762c413b0bd2ef234cb0
parent 472771 fda45db5f8e981524e7538b7b2e259fa7828424d
child 472773 0258584daaa638523318b5b6fb10fe24e63a05d5
push id35978
push usershindli@mozilla.com
push dateTue, 07 May 2019 09:44:39 +0000
treeherdermozilla-central@7aee5a30dd15 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs1546544
milestone68.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 1546544 - Reduce navigator.hardwareConcurrency to account for TCSM r=luke Differential Revision: https://phabricator.services.mozilla.com/D29437
dom/workers/RuntimeService.cpp
dom/workers/moz.build
xpcom/base/nsMacUtilsImpl.cpp
xpcom/base/nsMacUtilsImpl.h
--- a/dom/workers/RuntimeService.cpp
+++ b/dom/workers/RuntimeService.cpp
@@ -66,16 +66,20 @@
 #include "nsNetUtil.h"
 #include "nsServiceManagerUtils.h"
 #include "nsThreadUtils.h"
 #include "nsXPCOM.h"
 #include "nsXPCOMPrivate.h"
 #include "OSFileConstants.h"
 #include "xpcpublic.h"
 
+#if defined(XP_MACOSX)
+# include "nsMacUtilsImpl.h"
+#endif
+
 #include "Principal.h"
 #include "WorkerDebuggerManager.h"
 #include "WorkerError.h"
 #include "WorkerLoadInfo.h"
 #include "WorkerPrivate.h"
 #include "WorkerRunnable.h"
 #include "WorkerScope.h"
 #include "WorkerThread.h"
@@ -2145,17 +2149,27 @@ uint32_t RuntimeService::ClampedHardware
 
   // This needs to be atomic, because multiple workers, and even mainthread,
   // could race to initialize it at once.
   static Atomic<uint32_t> clampedHardwareConcurrency;
 
   // No need to loop here: if compareExchange fails, that just means that some
   // other worker has initialized numberOfProcessors, so we're good to go.
   if (!clampedHardwareConcurrency) {
-    int32_t numberOfProcessors = PR_GetNumberOfProcessors();
+    int32_t numberOfProcessors = 0;
+#if defined(XP_MACOSX)
+    if (nsMacUtilsImpl::IsTCSMAvailable()) {
+      // On failure, zero is returned from GetPhysicalCPUCount()
+      // and we fallback to PR_GetNumberOfProcessors below.
+      numberOfProcessors = nsMacUtilsImpl::GetPhysicalCPUCount();
+    }
+#endif
+    if (numberOfProcessors == 0) {
+      numberOfProcessors = PR_GetNumberOfProcessors();
+    }
     if (numberOfProcessors <= 0) {
       numberOfProcessors = 1;  // Must be one there somewhere
     }
     uint32_t clampedValue =
         std::min(uint32_t(numberOfProcessors), gMaxHardwareConcurrency);
     Unused << clampedHardwareConcurrency.compareExchange(0, clampedValue);
   }
 
--- a/dom/workers/moz.build
+++ b/dom/workers/moz.build
@@ -75,16 +75,22 @@ LOCAL_INCLUDES += [
     '/dom/bindings',
     '/dom/system',
     '/js/xpconnect/loader',
     '/netwerk/base',
     '/xpcom/build',
     '/xpcom/threads',
 ]
 
+if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa':
+    LOCAL_INCLUDES += [
+        '/xpcom/base',
+    ]
+
+
 include('/ipc/chromium/chromium-config.mozbuild')
 
 FINAL_LIBRARY = 'xul'
 
 MOCHITEST_MANIFESTS += [
     'test/mochitest.ini',
 ]
 
--- a/xpcom/base/nsMacUtilsImpl.cpp
+++ b/xpcom/base/nsMacUtilsImpl.cpp
@@ -298,8 +298,20 @@ void nsMacUtilsImpl::EnableTCSMIfAvailab
 /* static */
 bool nsMacUtilsImpl::IsTCSMEnabled() {
   uint32_t oldVal = 0;
   size_t oldValSize = sizeof(oldVal);
   int rv = sysctlbyname("kern.tcsm_enable", &oldVal, &oldValSize, NULL, 0);
   return (rv == 0) && (oldVal != 0);
 }
 #endif
+
+// Returns 0 on error.
+/* static */
+uint32_t nsMacUtilsImpl::GetPhysicalCPUCount() {
+  uint32_t oldVal = 0;
+  size_t oldValSize = sizeof(oldVal);
+  int rv = sysctlbyname("hw.physicalcpu_max", &oldVal, &oldValSize, NULL, 0);
+  if (rv == -1) {
+    return 0;
+  }
+  return oldVal;
+}
--- a/xpcom/base/nsMacUtilsImpl.h
+++ b/xpcom/base/nsMacUtilsImpl.h
@@ -31,16 +31,18 @@ class nsMacUtilsImpl final : public nsIM
 #  ifdef DEBUG
   static nsresult GetBloatLogDir(nsCString& aDirectoryPath);
   static nsresult GetDirectoryPath(const char* aPath,
                                    nsCString& aDirectoryPath);
 #  endif /* DEBUG */
 #endif   /* MOZ_SANDBOX */
 
   static void EnableTCSMIfAvailable();
+  static bool IsTCSMAvailable();
+  static uint32_t GetPhysicalCPUCount();
 
  private:
   ~nsMacUtilsImpl() {}
 
   nsresult GetArchString(nsAString& aArchString);
 
   // A string containing a "-" delimited list of architectures
   // in our binary.
@@ -55,17 +57,16 @@ class nsMacUtilsImpl final : public nsIM
 
   enum TCSMStatus {
     TCSM_Unknown = 0,
     TCSM_Available,
     TCSM_Unavailable
   };
   static mozilla::Atomic<nsMacUtilsImpl::TCSMStatus> sTCSMStatus;
 
-  static bool IsTCSMAvailable();
   static nsresult EnableTCSM();
 #if defined(DEBUG)
   static bool IsTCSMEnabled();
 #endif
 };
 
 // Global singleton service
 // 697BD3FD-43E5-41CE-AD5E-C339175C0818