From a2d7f3a3280ccd08a56f130f5856ca6b650ff767 Mon Sep 17 00:00:00 2001 From: Haik Aftandilian Date: Mon, 6 May 2019 06:09:11 +0000 Subject: [PATCH] Bug 1546544 - Reduce navigator.hardwareConcurrency to account for TCSM r=luke Differential Revision: https://phabricator.services.mozilla.com/D29437 --HG-- extra : moz-landing-system : lando --- dom/workers/RuntimeService.cpp | 16 +++++++++++++++- dom/workers/moz.build | 6 ++++++ xpcom/base/nsMacUtilsImpl.cpp | 12 ++++++++++++ xpcom/base/nsMacUtilsImpl.h | 3 ++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/dom/workers/RuntimeService.cpp b/dom/workers/RuntimeService.cpp index 07efe482561d..23dbc46c45ea 100644 --- a/dom/workers/RuntimeService.cpp +++ b/dom/workers/RuntimeService.cpp @@ -71,6 +71,10 @@ #include "OSFileConstants.h" #include "xpcpublic.h" +#if defined(XP_MACOSX) +# include "nsMacUtilsImpl.h" +#endif + #include "Principal.h" #include "WorkerDebuggerManager.h" #include "WorkerError.h" @@ -2150,7 +2154,17 @@ uint32_t RuntimeService::ClampedHardwareConcurrency() const { // 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 } diff --git a/dom/workers/moz.build b/dom/workers/moz.build index cf7727fc645f..c4b17fc67983 100644 --- a/dom/workers/moz.build +++ b/dom/workers/moz.build @@ -80,6 +80,12 @@ LOCAL_INCLUDES += [ '/xpcom/threads', ] +if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa': + LOCAL_INCLUDES += [ + '/xpcom/base', + ] + + include('/ipc/chromium/chromium-config.mozbuild') FINAL_LIBRARY = 'xul' diff --git a/xpcom/base/nsMacUtilsImpl.cpp b/xpcom/base/nsMacUtilsImpl.cpp index 11339131ba38..5aad7864b070 100644 --- a/xpcom/base/nsMacUtilsImpl.cpp +++ b/xpcom/base/nsMacUtilsImpl.cpp @@ -303,3 +303,15 @@ bool nsMacUtilsImpl::IsTCSMEnabled() { 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; +} diff --git a/xpcom/base/nsMacUtilsImpl.h b/xpcom/base/nsMacUtilsImpl.h index 761cf17a2599..ed3b4a412c3a 100644 --- a/xpcom/base/nsMacUtilsImpl.h +++ b/xpcom/base/nsMacUtilsImpl.h @@ -36,6 +36,8 @@ class nsMacUtilsImpl final : public nsIMacUtils { #endif /* MOZ_SANDBOX */ static void EnableTCSMIfAvailable(); + static bool IsTCSMAvailable(); + static uint32_t GetPhysicalCPUCount(); private: ~nsMacUtilsImpl() {} @@ -60,7 +62,6 @@ class nsMacUtilsImpl final : public nsIMacUtils { }; static mozilla::Atomic sTCSMStatus; - static bool IsTCSMAvailable(); static nsresult EnableTCSM(); #if defined(DEBUG) static bool IsTCSMEnabled(); -- 2.11.4.GIT