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
This commit is contained in:
Haik Aftandilian 2019-05-06 06:09:11 +00:00
parent cd1c25bb79
commit a2d7f3a328
4 changed files with 35 additions and 2 deletions

View File

@ -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
}

View File

@ -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'

View File

@ -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;
}

View File

@ -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<nsMacUtilsImpl::TCSMStatus> sTCSMStatus;
static bool IsTCSMAvailable();
static nsresult EnableTCSM();
#if defined(DEBUG)
static bool IsTCSMEnabled();