From d69e4a8d4def126202f73b636448bb1ad59bdb83 Mon Sep 17 00:00:00 2001 From: Olli Pettay Date: Mon, 15 Jan 2024 12:23:33 +0000 Subject: [PATCH] Bug 1874454 - Implement GetHeterogeneousCpuInfo for Apple Silicon Mac, r=haik,mstange Differential Revision: https://phabricator.services.mozilla.com/D198486 --- hal/HalTypes.h | 4 +- hal/cocoa/CocoaHeterogeneousCpuInfo.cpp | 59 +++++++++++++++++++++ hal/moz.build | 6 ++- security/sandbox/mac/SandboxPolicyContent.h | 2 + 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 hal/cocoa/CocoaHeterogeneousCpuInfo.cpp diff --git a/hal/HalTypes.h b/hal/HalTypes.h index 912e4c12bb5b..a04c018307b1 100644 --- a/hal/HalTypes.h +++ b/hal/HalTypes.h @@ -94,9 +94,11 @@ class PerformanceHintSession { * information. */ struct HeterogeneousCpuInfo { - // We use a max of 32 because currently this is only implemented for Android + // We use a max of 32 because this was initially implemented only for Android // where we are unlikely to need more CPUs than that, and it simplifies // dealing with cpu_set_t as CPU_SETSIZE is 32 on 32-bit Android. + // If there are more than 32 CPU cores, the implementation should try to fill + // first mBigCpus before adding anything to mMediumCpus or mLittleCpus. static const size_t MAX_CPUS = 32; size_t mTotalNumCpus; mozilla::BitSet mLittleCpus; diff --git a/hal/cocoa/CocoaHeterogeneousCpuInfo.cpp b/hal/cocoa/CocoaHeterogeneousCpuInfo.cpp new file mode 100644 index 000000000000..065a25b930f3 --- /dev/null +++ b/hal/cocoa/CocoaHeterogeneousCpuInfo.cpp @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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 https://mozilla.org/MPL/2.0/. */ + +#include "Hal.h" +#include "HalTypes.h" +#include +#include +#include "mozilla/BitSet.h" + +namespace mozilla::hal_impl { + +mozilla::Maybe CreateHeterogeneousCpuInfo() { +#ifdef __aarch64__ + // As of now on Apple Silicon the number of *.logicalcpu_max is the same as + // *.physicalcpu_max. + size_t len = sizeof(uint32_t); + uint32_t pCores = 0; + if (sysctlbyname("hw.perflevel0.logicalcpu_max", &pCores, &len, nullptr, 0)) { + return Nothing(); + } + + len = sizeof(uint32_t); + uint32_t eCores = 0; + if (sysctlbyname("hw.perflevel1.logicalcpu_max", &eCores, &len, nullptr, 0)) { + return Nothing(); + } + + hal::HeterogeneousCpuInfo info; + info.mTotalNumCpus = pCores + eCores; + + // The API has currently a limit how many cpu cores it can tell about. + for (uint32_t i = 0; i < hal::HeterogeneousCpuInfo::MAX_CPUS; ++i) { + if (pCores) { + --pCores; + info.mBigCpus[i] = true; + } else if (eCores) { + --eCores; + info.mLittleCpus[i] = true; + } else { + break; + } + } + + return Some(info); +#else + return Nothing(); +#endif +} + +const Maybe& GetHeterogeneousCpuInfo() { + static const Maybe cpuInfo = + CreateHeterogeneousCpuInfo(); + return cpuInfo; +} + +} // namespace mozilla::hal_impl diff --git a/hal/moz.build b/hal/moz.build index 6354771dbfc6..912a157372ea 100644 --- a/hal/moz.build +++ b/hal/moz.build @@ -48,6 +48,7 @@ if CONFIG["MOZ_WIDGET_TOOLKIT"] == "android": ] elif CONFIG["OS_TARGET"] == "Linux": UNIFIED_SOURCES += [ + "fallback/FallbackHeterogeneousCpuInfo.cpp", "fallback/FallbackScreenConfiguration.cpp", "fallback/FallbackSensor.cpp", "fallback/FallbackVibration.cpp", @@ -63,6 +64,7 @@ elif CONFIG["OS_TARGET"] == "Linux": ] elif CONFIG["OS_TARGET"] == "WINNT": UNIFIED_SOURCES += [ + "fallback/FallbackHeterogeneousCpuInfo.cpp", "fallback/FallbackVibration.cpp", "windows/WindowsProcessPriority.cpp", "windows/WindowsScreenConfiguration.cpp", @@ -75,12 +77,14 @@ elif CONFIG["OS_TARGET"] == "WINNT": elif CONFIG["MOZ_WIDGET_TOOLKIT"] == "cocoa": UNIFIED_SOURCES += [ "cocoa/CocoaBattery.cpp", + "cocoa/CocoaHeterogeneousCpuInfo.cpp", "fallback/FallbackProcessPriority.cpp", "fallback/FallbackScreenConfiguration.cpp", "fallback/FallbackVibration.cpp", ] elif CONFIG["OS_TARGET"] in ("OpenBSD", "NetBSD", "FreeBSD", "DragonFly"): UNIFIED_SOURCES += [ + "fallback/FallbackHeterogeneousCpuInfo.cpp", "fallback/FallbackProcessPriority.cpp", "fallback/FallbackScreenConfiguration.cpp", "fallback/FallbackSensor.cpp", @@ -97,6 +101,7 @@ elif CONFIG["OS_TARGET"] in ("OpenBSD", "NetBSD", "FreeBSD", "DragonFly"): else: UNIFIED_SOURCES += [ "fallback/FallbackBattery.cpp", + "fallback/FallbackHeterogeneousCpuInfo.cpp", "fallback/FallbackProcessPriority.cpp", "fallback/FallbackScreenConfiguration.cpp", "fallback/FallbackSensor.cpp", @@ -106,7 +111,6 @@ else: # Fallbacks for backends implemented on Android only. if CONFIG["MOZ_WIDGET_TOOLKIT"] != "android": UNIFIED_SOURCES += [ - "fallback/FallbackHeterogeneousCpuInfo.cpp", "fallback/FallbackNetwork.cpp", "fallback/FallbackPerformanceHintManager.cpp", ] diff --git a/security/sandbox/mac/SandboxPolicyContent.h b/security/sandbox/mac/SandboxPolicyContent.h index f40bc6eea59c..3f49f684d68c 100644 --- a/security/sandbox/mac/SandboxPolicyContent.h +++ b/security/sandbox/mac/SandboxPolicyContent.h @@ -113,6 +113,8 @@ static const char SandboxPolicyContent[] = R"SANDBOX_LITERAL( (sysctl-name "hw.pagesize_compat") (sysctl-name "hw.logicalcpu") (sysctl-name "hw.logicalcpu_max") + (sysctl-name "hw.perflevel0.logicalcpu_max") + (sysctl-name "hw.perflevel1.logicalcpu_max") (sysctl-name "hw.physicalcpu_max") (sysctl-name "hw.busfrequency_compat") (sysctl-name "hw.busfrequency_max")