Commit 171a7a1c authored by Olli Pettay's avatar Olli Pettay
Browse files

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

parent 6eeb2720
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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;
+58 −0
Original line number Diff line number Diff line
/* -*- 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 <sys/types.h>
#include <sys/sysctl.h>
#include "mozilla/BitSet.h"
#include "nsSystemInfo.h"

namespace mozilla::hal_impl {

mozilla::Maybe<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();
  }

  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 < 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<HeterogeneousCpuInfo>& GetHeterogeneousCpuInfo() {
  static const Maybe<HeterogeneousCpuInfo> cpuInfo =
      CreateHeterogeneousCpuInfo();
  return cpuInfo;
}

}  // namespace mozilla::hal_impl
+5 −1
Original line number Diff line number Diff line
@@ -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",
    ]
+2 −0
Original line number Diff line number Diff line
@@ -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")