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/. */
11 #include "mozilla/BitSet.h"
12 #include "mozilla/CheckedInt.h"
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");
25 if (cpuCount
.value() > HeterogeneousCpuInfo::MAX_CPUS
) {
26 HAL_ERR("HeterogeneousCpuInfo: Too many cpus");
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());
41 HAL_ERR("HeterogeneousCpuInfo: Failed to open file %s",
42 freq_filename
.str().c_str());
47 HAL_ERR("HeterogeneousCpuInfo: Failed to parse file %s",
48 freq_filename
.str().c_str());
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;
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());
79 const Maybe
<HeterogeneousCpuInfo
>& GetHeterogeneousCpuInfo() {
80 static const Maybe
<HeterogeneousCpuInfo
> cpuInfo
=
81 CreateHeterogeneousCpuInfo();
85 } // namespace mozilla::hal_impl