Bug 1877642 - Disable browser_fullscreen-tab-close-race.js on apple_silicon !debug...
[gecko.git] / hal / android / AndroidHeterogeneousCpuInfo.cpp
blobe51ebce260d0803e9bf2fc765f50b8e79623a6a0
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/. */
7 #include "Hal.h"
8 #include "HalLog.h"
9 #include "HalTypes.h"
11 #include "mozilla/BitSet.h"
12 #include "mozilla/CheckedInt.h"
13 #include "prsystem.h"
14 #include <fstream>
16 namespace mozilla::hal_impl {
18 mozilla::Maybe<HeterogeneousCpuInfo> CreateHeterogeneousCpuInfo() {
19 CheckedInt<size_t> cpuCount = PR_GetNumberOfProcessors();
20 if (!cpuCount.isValid()) {
21 HAL_ERR("HeterogeneousCpuInfo: Failed to query processor count");
22 return Nothing();
25 if (cpuCount.value() > HeterogeneousCpuInfo::MAX_CPUS) {
26 HAL_ERR("HeterogeneousCpuInfo: Too many cpus");
27 return Nothing();
30 std::vector<std::pair<int, int>> cpu_freqs;
31 cpu_freqs.reserve(cpuCount.value());
32 for (size_t i = 0; i < cpuCount.value(); i++) {
33 std::stringstream freq_filename;
34 // On all devices tested, even with isolated content processes we do have
35 // permission to read this file. If, however, this function stops working
36 // one day, then this may be a good place to start investigating.
37 freq_filename << "/sys/devices/system/cpu/cpu" << i
38 << "/cpufreq/cpuinfo_max_freq";
39 std::ifstream ifs(freq_filename.str());
40 if (!ifs) {
41 HAL_ERR("HeterogeneousCpuInfo: Failed to open file %s",
42 freq_filename.str().c_str());
43 return Nothing();
45 int freq;
46 if (!(ifs >> freq)) {
47 HAL_ERR("HeterogeneousCpuInfo: Failed to parse file %s",
48 freq_filename.str().c_str());
49 return Nothing();
51 cpu_freqs.push_back(std::make_pair(i, freq));
54 std::sort(cpu_freqs.begin(), cpu_freqs.end(),
55 [](auto lhs, auto rhs) { return lhs.second < rhs.second; });
57 HeterogeneousCpuInfo info;
58 info.mTotalNumCpus = cpuCount.value();
60 int lowest_freq = cpu_freqs.front().second;
61 int highest_freq = cpu_freqs.back().second;
62 for (const auto& [cpu, freq] : cpu_freqs) {
63 if (freq == highest_freq) {
64 info.mBigCpus[cpu] = true;
65 } else if (freq == lowest_freq) {
66 info.mLittleCpus[cpu] = true;
67 } else {
68 info.mMediumCpus[cpu] = true;
72 HAL_LOG("HeterogeneousCpuInfo: little: %zu, med: %zu, big: %zu",
73 info.mLittleCpus.Count(), info.mMediumCpus.Count(),
74 info.mBigCpus.Count());
76 return Some(info);
79 const Maybe<HeterogeneousCpuInfo>& GetHeterogeneousCpuInfo() {
80 static const Maybe<HeterogeneousCpuInfo> cpuInfo =
81 CreateHeterogeneousCpuInfo();
82 return cpuInfo;
85 } // namespace mozilla::hal_impl