Bug 1546544 - Reduce navigator.hardwareConcurrency to account for TCSM r=luke
authorHaik Aftandilian <haftandilian@mozilla.com>
Tue, 07 May 2019 22:16:28 +0000
changeset 534888 64ac5acae26bd63323e2f62890eae27380119bb3
parent 534887 cd1ccb74af7c84aaa8ea43851c6ac74fd00ec276
child 534889 e9e1b9c2cab6cfa713c6119c5b32b1a928faa4f9
push id2082
push userffxbld-merge
push dateMon, 01 Jul 2019 08:34:18 +0000
treeherdermozilla-release@2fb19d0466d2 [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