CocoaHeterogeneousCpuInfo.cpp (1631B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 6 7 #include "Hal.h" 8 #include "HalTypes.h" 9 #include <sys/types.h> 10 #include <sys/sysctl.h> 11 12 namespace mozilla::hal_impl { 13 14 mozilla::Maybe<hal::HeterogeneousCpuInfo> CreateHeterogeneousCpuInfo() { 15 #ifdef __aarch64__ 16 // As of now on Apple Silicon the number of *.logicalcpu_max is the same as 17 // *.physicalcpu_max. 18 size_t len = sizeof(uint32_t); 19 uint32_t pCores = 0; 20 if (sysctlbyname("hw.perflevel0.logicalcpu_max", &pCores, &len, nullptr, 0)) { 21 return Nothing(); 22 } 23 24 len = sizeof(uint32_t); 25 uint32_t eCores = 0; 26 if (sysctlbyname("hw.perflevel1.logicalcpu_max", &eCores, &len, nullptr, 0)) { 27 return Nothing(); 28 } 29 30 hal::HeterogeneousCpuInfo info; 31 info.mTotalNumCpus = pCores + eCores; 32 33 // The API has currently a limit how many cpu cores it can tell about. 34 for (uint32_t i = 0; i < hal::HeterogeneousCpuInfo::MAX_CPUS; ++i) { 35 if (pCores) { 36 --pCores; 37 info.mBigCpus[i] = true; 38 } else if (eCores) { 39 --eCores; 40 info.mLittleCpus[i] = true; 41 } else { 42 break; 43 } 44 } 45 46 return Some(info); 47 #else 48 return Nothing(); 49 #endif 50 } 51 52 const Maybe<hal::HeterogeneousCpuInfo>& GetHeterogeneousCpuInfo() { 53 static const Maybe<hal::HeterogeneousCpuInfo> cpuInfo = 54 CreateHeterogeneousCpuInfo(); 55 return cpuInfo; 56 } 57 58 } // namespace mozilla::hal_impl