Skip to content
Snippets Groups Projects
Commit 938068b3 authored by Jamie Nicol's avatar Jamie Nicol
Browse files

Bug 1850573 - Add HeterogeneousCpuInfo to HAL with Android implementation....

Bug 1850573 - Add HeterogeneousCpuInfo to HAL with Android implementation. r=geckoview-reviewers,owlish

This adds a function GetHeterogeneousCpuInfo() to HAL, which
classifies the processor cores in to "big", "medium", and
"little". This is currently only implemented on Android, where it
works by parsing the maximum CPU frequency from sysfs.

When all CPUs have the same frequency they are all classified as
"big", and when there are only 2 different frequencies they are
classified as "big" and "little". All CPUs with a frequency in-between
the lowest and highest are classified as "medium".

This information can be used to count the number of each cores in each
category, eg for determining appropriate thread pool sizes. Or to
determine the indices of cores in a certain category, eg for setting
CPU affinity for certain threads.

Differential Revision: https://phabricator.services.mozilla.com/D188478
parent d03ae3ce
No related branches found
No related tags found
No related merge requests found
......@@ -423,6 +423,10 @@ UniquePtr<hal::PerformanceHintSession> CreatePerformanceHintSession(
return hal_impl::CreatePerformanceHintSession(aThreads, aTargetWorkDuration);
}
const Maybe<hal::HeterogeneousCpuInfo>& GetHeterogeneousCpuInfo() {
return hal_impl::GetHeterogeneousCpuInfo();
}
void Init() {
MOZ_ASSERT(!sInitialized);
......
......@@ -251,6 +251,15 @@ UniquePtr<hal::PerformanceHintSession> CreatePerformanceHintSession(
const nsTArray<PlatformThreadHandle>& aThreads,
mozilla::TimeDuration aTargetWorkDuration);
/**
* Returns information categorizing the CPUs on the system by performance class.
*
* Returns Nothing if we are unable to calculate the required information.
*
* See the definition of hal::HeterogeneousCpuInfo for more details.
*/
const Maybe<hal::HeterogeneousCpuInfo>& GetHeterogeneousCpuInfo();
} // namespace MOZ_HAL_NAMESPACE
} // namespace mozilla
......
......@@ -7,6 +7,7 @@
#define mozilla_hal_Types_h
#include "ipc/EnumSerializer.h"
#include "mozilla/BitSet.h"
#include "mozilla/Observer.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/UniquePtr.h"
......@@ -80,6 +81,29 @@ class PerformanceHintSession {
virtual void ReportActualWorkDuration(TimeDuration aDuration) = 0;
};
/**
* Categorizes the CPUs on the system in to big, medium, and little classes.
*
* A set bit in each bitset indicates that the CPU of that index belongs to that
* class. If the CPUs are fully homogeneous they are all categorized as big. If
* there are only 2 classes, they are categorized as either big or little.
* Finally, if there are >= 3 classes, the remainder will be categorized as
* medium.
*
* If there are more than MAX_CPUS present we are unable to represent this
* information.
*/
struct HeterogeneousCpuInfo {
// We use a max of 32 because currently this is only implemented 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.
static const size_t MAX_CPUS = 32;
size_t mTotalNumCpus;
mozilla::BitSet<MAX_CPUS> mLittleCpus;
mozilla::BitSet<MAX_CPUS> mMediumCpus;
mozilla::BitSet<MAX_CPUS> mBigCpus;
};
} // namespace hal
} // namespace mozilla
......
/* -*- 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 "HalLog.h"
#include "HalTypes.h"
#include "mozilla/BitSet.h"
#include "mozilla/CheckedInt.h"
#include "prsystem.h"
#include <fstream>
namespace mozilla::hal_impl {
mozilla::Maybe<HeterogeneousCpuInfo> CreateHeterogeneousCpuInfo() {
CheckedInt<size_t> cpuCount = PR_GetNumberOfProcessors();
if (!cpuCount.isValid()) {
HAL_ERR("HeterogeneousCpuInfo: Failed to query processor count");
return Nothing();
}
if (cpuCount.value() > HeterogeneousCpuInfo::MAX_CPUS) {
HAL_ERR("HeterogeneousCpuInfo: Too many cpus");
return Nothing();
}
std::vector<std::pair<int, int>> cpu_freqs;
cpu_freqs.reserve(cpuCount.value());
for (size_t i = 0; i < cpuCount.value(); i++) {
std::stringstream freq_filename;
// On all devices tested, even with isolated content processes we do have
// permission to read this file. If, however, this function stops working
// one day, then this may be a good place to start investigating.
freq_filename << "/sys/devices/system/cpu/cpu" << i
<< "/cpufreq/cpuinfo_max_freq";
std::ifstream ifs(freq_filename.str());
if (!ifs) {
HAL_ERR("HeterogeneousCpuInfo: Failed to open file %s",
freq_filename.str().c_str());
return Nothing();
}
int freq;
if (!(ifs >> freq)) {
HAL_ERR("HeterogeneousCpuInfo: Failed to parse file %s",
freq_filename.str().c_str());
return Nothing();
}
cpu_freqs.push_back(std::make_pair(i, freq));
}
std::sort(cpu_freqs.begin(), cpu_freqs.end(),
[](auto lhs, auto rhs) { return lhs.second < rhs.second; });
HeterogeneousCpuInfo info;
info.mTotalNumCpus = cpuCount.value();
int lowest_freq = cpu_freqs.front().second;
int highest_freq = cpu_freqs.back().second;
for (const auto& [cpu, freq] : cpu_freqs) {
if (freq == highest_freq) {
info.mBigCpus[cpu] = true;
} else if (freq == lowest_freq) {
info.mLittleCpus[cpu] = true;
} else {
info.mMediumCpus[cpu] = true;
}
}
HAL_LOG("HeterogeneousCpuInfo: little: %zu, med: %zu, big: %zu",
info.mLittleCpus.Count(), info.mMediumCpus.Count(),
info.mBigCpus.Count());
return Some(info);
}
const Maybe<HeterogeneousCpuInfo>& GetHeterogeneousCpuInfo() {
static const Maybe<HeterogeneousCpuInfo> cpuInfo =
CreateHeterogeneousCpuInfo();
return cpuInfo;
}
} // namespace mozilla::hal_impl
/* -*- 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"
namespace mozilla::hal_impl {
const Maybe<hal::HeterogeneousCpuInfo>& GetHeterogeneousCpuInfo() {
static Maybe<hal::HeterogeneousCpuInfo> cpuInfo = Nothing();
return cpuInfo;
}
} // namespace mozilla::hal_impl
......@@ -37,6 +37,7 @@ if CONFIG["MOZ_WIDGET_TOOLKIT"] == "android":
"/widget/android",
]
UNIFIED_SOURCES += [
"android/AndroidHeterogeneousCpuInfo.cpp",
"android/AndroidPerformanceHintManager.cpp",
"android/AndroidProcessPriority.cpp",
"android/AndroidSensor.cpp",
......@@ -105,6 +106,7 @@ 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",
]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment