Bug 1874454 - Implement GetHeterogeneousCpuInfo for Apple Silicon Mac, r=haik,mstange

Differential Revision: https://phabricator.services.mozilla.com/D198486
This commit is contained in:
Olli Pettay 2024-01-15 12:23:33 +00:00
parent 5e50b178a8
commit d69e4a8d4d
4 changed files with 69 additions and 2 deletions

View file

@ -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<MAX_CPUS> mLittleCpus;

View file

@ -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 <sys/types.h>
#include <sys/sysctl.h>
#include "mozilla/BitSet.h"
namespace mozilla::hal_impl {
mozilla::Maybe<hal::HeterogeneousCpuInfo> 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<hal::HeterogeneousCpuInfo>& GetHeterogeneousCpuInfo() {
static const Maybe<hal::HeterogeneousCpuInfo> cpuInfo =
CreateHeterogeneousCpuInfo();
return cpuInfo;
}
} // namespace mozilla::hal_impl

View file

@ -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",
]

View file

@ -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")