Skip to content

Commit b5879a3

Browse files
Revert: [Android] Let the raster thread use every core of a chip without little cores (flutter#195838)
Reverts: [[Android] Let the raster thread use every core of a chip without little cores](flutter#195071) Initiated by: @gaaclarke Reason for reverting: 1) we address the problem where the energy measurement shows the tradeoff Original PR Author: @Lopution Reviewed By: @gaaclarke The original PR description is provided below: `CPUSpeedTracker` counts the CPUs with the lowest `cpuinfo_max_freq` as efficiency cores, and the raster thread is set to the other cores (`kNotEfficiency`, renamed `kNotLittle` in this PR). On a chip without little cores, those are full-size cores. On the Snapdragon 8 Elite and 8 Elite Gen 5 (two prime and six performance Oryon cores) the raster thread only gets the two prime cores. They are mostly idle, so raster frames start at the cluster's lowest frequency. See flutter#195070 for measurements. This change also reads `/sys/devices/system/cpu/cpuN/cpu_capacity`, the kernel's rating of each CPU, scaled so that the fastest is 1024. When every CPU has a capacity and none is below half of the fastest, the chip has no little cores, and `kNotLittle` is every core. - Why half: little cores rate well below it. Upstream SM8650 device trees put the Cortex-A520 at about 37% of the X4 at their top frequencies. The SM8850's performance cores are at 762 of 1024. - Equal capacities, or a CPU without one, change nothing. A kernel without capacity data reports 1024 for every CPU. - Other affinities do not change. `kEfficiency` is still the slowest group, so background threads stay off the prime cores. - `kNotEfficiency` is renamed `kNotLittle`, since on these chips it includes the efficiency cores. Each `CpuAffinity` value's doc says which cores it covers, and that performance and efficiency there are speed groups, not the industry's core types. - Chips with little cores (Snapdragon 8 Gen 1–3, Tensor, Exynos 2400) should keep the current set. I have only read the capacities of the SM8850. - Other chips without little cores also get every core. Examples: Dimensity 9300/9400 (the slowest group is four Cortex-A720), Snapdragon 8s Gen 4, and Snapdragon 8 Elite Gen 6. On the 8 Elite Gen 6 the six performance cores report two top frequencies (4.03 and 3.74 GHz), so today three of them count as efficiency cores. I have not measured these chips. - On the Dimensity 9600 Pro (2 × C2-Ultra at 4.55 GHz, 3 × C2-Pro at 4.35 GHz, 3 × C2-Pro at 3.1 GHz), the 3.1 GHz cores should rate near half, so it may go either way. Either way the raster thread keeps at least the five cores it gets today. This keeps the current design and only widens it where the capacities are clear. A chip whose performance cores rate below half of its prime cores would still leave the raster thread on the prime cores. The more general fix would be to stop pinning the raster thread, as Android does not pin a native app's RenderThread. That needs measurements on the devices that flutter-team-archive/engine#45673 and flutter#153690 were about, so this change leaves it out. Tests: three new `CpuAffinity` tests in `fml_unittests`. `NoLittleCores` fails without this change. I ran the `CpuAffinity` tests against the changed files in a standalone build with clang++ and g++, not in a full engine build. On an SM8850 phone, setting the raster threads to every core from the app (the same result as this change) gave the numbers in flutter#195070. I have not tested a custom engine build on the device. Fixes flutter#195070 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 7a62b1f commit b5879a3

5 files changed

Lines changed: 22 additions & 129 deletions

File tree

‎engine/src/flutter/fml/cpu_affinity.cc‎

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
#include "flutter/fml/cpu_affinity.h"
66
#include "flutter/fml/build_config.h"
77

8-
#include <algorithm>
98
#include <cstdint>
109
#include <fstream>
11-
#include <limits>
1210
#include <optional>
1311
#include <string>
1412

@@ -34,29 +32,6 @@ bool RequestAffinity(CpuAffinity affinity) {
3432
#endif
3533
}
3634

37-
namespace {
38-
39-
// Whether the kernel's capacities rule out little cores: every CPU reports
40-
// one, and none is below half of the fastest. Equal capacities say nothing,
41-
// since a kernel without capacity data reports 1024 for every CPU.
42-
bool HasNoLittleCores(const std::vector<CpuIndexAndSpeed>& cpus) {
43-
if (cpus.empty()) {
44-
return false;
45-
}
46-
int64_t min_capacity = std::numeric_limits<int64_t>::max();
47-
int64_t max_capacity = std::numeric_limits<int64_t>::min();
48-
for (const auto& cpu : cpus) {
49-
if (!cpu.capacity.has_value()) {
50-
return false;
51-
}
52-
min_capacity = std::min(min_capacity, *cpu.capacity);
53-
max_capacity = std::max(max_capacity, *cpu.capacity);
54-
}
55-
return min_capacity != max_capacity && min_capacity * 2 >= max_capacity;
56-
}
57-
58-
} // namespace
59-
6035
CPUSpeedTracker::CPUSpeedTracker(std::vector<CpuIndexAndSpeed> data)
6136
: cpu_speeds_(std::move(data)) {
6237
std::optional<int64_t> max_speed = std::nullopt;
@@ -85,16 +60,7 @@ CPUSpeedTracker::CPUSpeedTracker(std::vector<CpuIndexAndSpeed> data)
8560
if (data.speed == min_speed_value) {
8661
efficiency_.push_back(data.index);
8762
} else {
88-
not_little_.push_back(data.index);
89-
}
90-
}
91-
// Without little cores, the efficiency cores are full-size ones: excluding
92-
// them would leave the performance cores alone, which may be as few as one
93-
// or two.
94-
if (HasNoLittleCores(cpu_speeds_)) {
95-
not_little_.clear();
96-
for (const auto& data : cpu_speeds_) {
97-
not_little_.push_back(data.index);
63+
not_efficiency_.push_back(data.index);
9864
}
9965
}
10066

@@ -114,8 +80,8 @@ const std::vector<size_t>& CPUSpeedTracker::GetIndices(
11480
return efficiency_;
11581
case CpuAffinity::kNotPerformance:
11682
return not_performance_;
117-
case CpuAffinity::kNotLittle:
118-
return not_little_;
83+
case CpuAffinity::kNotEfficiency:
84+
return not_efficiency_;
11985
}
12086
}
12187

‎engine/src/flutter/fml/cpu_affinity.h‎

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,21 @@ namespace fml {
1515
/// The CPU Affinity provides a hint to the operating system on which cores a
1616
/// particular thread should be scheduled on. The operating system may or may
1717
/// not honor these requests.
18-
///
19-
/// The performance and efficiency cores here are the groups of cores with the
20-
/// highest and the lowest cpuinfo_max_freq on the device. They are not the
21-
/// industry's core types: on a chip with two prime and six performance cores,
22-
/// the two prime cores are the performance cores here, and the six performance
23-
/// cores are the efficiency cores.
2418
enum class CpuAffinity {
25-
/// @brief Request affinity for the performance cores: those with the highest
26-
/// cpuinfo_max_freq.
19+
/// @brief Request CPU affinity for the performance cores.
20+
///
21+
/// Generally speaking, only the UI and Raster thread should
22+
/// use this option.
2723
kPerformance,
2824

29-
/// @brief Request affinity for the efficiency cores: those with the lowest
30-
/// cpuinfo_max_freq.
25+
/// @brief Request CPU affinity for the efficiency cores.
3126
kEfficiency,
3227

33-
/// @brief Request affinity for every core except the performance cores.
28+
/// @brief Request affinity for all non-performance cores.
3429
kNotPerformance,
3530

36-
/// @brief Request affinity for every core except the little cores.
37-
///
38-
/// The efficiency cores count as little cores unless the kernel's
39-
/// cpu_capacity values rule that out: every core reports one, they
40-
/// are not all equal, and none is below half of the highest. Then
41-
/// the device has no little cores, and this is every core, the
42-
/// efficiency cores included.
43-
kNotLittle,
31+
/// @brief Request affinity for all non-efficiency cores.
32+
kNotEfficiency,
4433
};
4534

4635
/// @brief Request count of efficiency cores.
@@ -66,11 +55,6 @@ struct CpuIndexAndSpeed {
6655
size_t index;
6756
// CPU speed in kHZ
6857
int64_t speed;
69-
// The kernel's capacity of the CPU, if known: its performance at its
70-
// highest frequency, scaled so that the fastest CPU is 1024. Parsed from
71-
// cpu_capacity files, see also:
72-
// https://www.kernel.org/doc/Documentation/devicetree/bindings/cpu/cpu-capacity.txt
73-
std::optional<int64_t> capacity = std::nullopt;
7458
};
7559

7660
/// @brief A class that computes the correct CPU indices for a requested CPU
@@ -99,7 +83,7 @@ class CPUSpeedTracker {
9983
std::vector<size_t> efficiency_;
10084
std::vector<size_t> performance_;
10185
std::vector<size_t> not_performance_;
102-
std::vector<size_t> not_little_;
86+
std::vector<size_t> not_efficiency_;
10387
};
10488

10589
/// @note Visible for testing.

‎engine/src/flutter/fml/cpu_affinity_unittests.cc‎

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,9 @@ TEST(CpuAffinity, NormalSlowMedFastCores) {
2929
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotPerformance).size(), 2u);
3030
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotPerformance)[0], 0u);
3131
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotPerformance)[1], 1u);
32-
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotLittle).size(), 2u);
33-
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotLittle)[0], 1u);
34-
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotLittle)[1], 2u);
35-
}
36-
37-
TEST(CpuAffinity, NoLittleCores) {
38-
// Two prime and six performance cores, as read on a Snapdragon 8 Elite
39-
// Gen 5: the slowest cores are three quarters of the fastest.
40-
std::vector<CpuIndexAndSpeed> speeds;
41-
speeds.reserve(8);
42-
for (size_t i = 0; i < 8; i++) {
43-
speeds.push_back({.index = i,
44-
.speed = i < 6 ? 3628800 : 4608000,
45-
.capacity = i < 6 ? 762 : 1024});
46-
}
47-
auto tracker = CPUSpeedTracker(speeds);
48-
49-
EXPECT_TRUE(tracker.IsValid());
50-
EXPECT_EQ(tracker.GetIndices(CpuAffinity::kNotLittle).size(), 8u);
51-
EXPECT_EQ(tracker.GetIndices(CpuAffinity::kEfficiency).size(), 6u);
52-
EXPECT_EQ(tracker.GetIndices(CpuAffinity::kPerformance).size(), 2u);
53-
}
54-
55-
TEST(CpuAffinity, LittleCoresByCapacity) {
56-
auto speeds = {
57-
CpuIndexAndSpeed{.index = 0, .speed = 2265600, .capacity = 381},
58-
CpuIndexAndSpeed{.index = 1, .speed = 3148800, .capacity = 980},
59-
CpuIndexAndSpeed{.index = 2, .speed = 3302400, .capacity = 1024}};
60-
auto tracker = CPUSpeedTracker(speeds);
61-
62-
EXPECT_TRUE(tracker.IsValid());
63-
// The size check guards the indexing below.
64-
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotLittle).size(), 2u);
65-
EXPECT_EQ(tracker.GetIndices(CpuAffinity::kNotLittle)[0], 1u);
66-
EXPECT_EQ(tracker.GetIndices(CpuAffinity::kNotLittle)[1], 2u);
67-
}
68-
69-
TEST(CpuAffinity, CapacitiesThatSayNothing) {
70-
// A kernel without capacity data reports 1024 for every CPU; a missing
71-
// capacity leaves the speeds to decide too.
72-
for (const auto& capacities :
73-
{std::vector<std::optional<int64_t>>{1024, 1024, 1024},
74-
std::vector<std::optional<int64_t>>{762, std::nullopt, 1024}}) {
75-
std::vector<CpuIndexAndSpeed> speeds;
76-
speeds.reserve(3);
77-
for (size_t i = 0; i < 3; i++) {
78-
speeds.push_back({.index = i,
79-
.speed = static_cast<int64_t>(i + 1),
80-
.capacity = capacities[i]});
81-
}
82-
auto tracker = CPUSpeedTracker(speeds);
83-
84-
EXPECT_TRUE(tracker.IsValid());
85-
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotLittle).size(), 2u);
86-
EXPECT_EQ(tracker.GetIndices(CpuAffinity::kNotLittle)[0], 1u);
87-
}
32+
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotEfficiency).size(), 2u);
33+
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotEfficiency)[0], 1u);
34+
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotEfficiency)[1], 2u);
8835
}
8936

9037
TEST(CpuAffinity, NoCpuData) {

‎engine/src/flutter/fml/platform/android/cpu_affinity.cc‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@ static CPUSpeedTracker* gCPUTracker;
2222

2323
// For each CPU index provided, attempts to open the file
2424
// /sys/devices/system/cpu/cpu$NUM/cpufreq/cpuinfo_max_freq and parse a number
25-
// containing the CPU frequency, and
26-
// /sys/devices/system/cpu/cpu$NUM/cpu_capacity for the kernel's capacity of the
27-
// CPU.
25+
// containing the CPU frequency.
2826
void InitCPUInfo(size_t cpu_count) {
2927
std::vector<CpuIndexAndSpeed> cpu_speeds;
3028

3129
for (auto i = 0u; i < cpu_count; i++) {
32-
auto cpu = "/sys/devices/system/cpu/cpu" + std::to_string(i);
33-
auto speed = ReadIntFromFile(cpu + "/cpufreq/cpuinfo_max_freq");
30+
auto path = "/sys/devices/system/cpu/cpu" + std::to_string(i) +
31+
"/cpufreq/cpuinfo_max_freq";
32+
auto speed = ReadIntFromFile(path);
3433
if (speed.has_value()) {
35-
cpu_speeds.push_back(
36-
{.index = i,
37-
.speed = speed.value(),
38-
.capacity = ReadIntFromFile(cpu + "/cpu_capacity")});
34+
cpu_speeds.push_back({.index = i, .speed = speed.value()});
3935
}
4036
}
4137
gCPUTracker = new CPUSpeedTracker(cpu_speeds);

‎engine/src/flutter/shell/platform/android/android_shell_holder.cc‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ static void AndroidPlatformThreadConfigSetter(
4646
break;
4747
}
4848
case fml::Thread::ThreadPriority::kDisplay: {
49-
fml::RequestAffinity(fml::CpuAffinity::kNotLittle);
49+
fml::RequestAffinity(fml::CpuAffinity::kNotEfficiency);
5050
if (::setpriority(PRIO_PROCESS, 0, -1) != 0) {
5151
FML_LOG(ERROR) << "Failed to set UI task runner priority";
5252
}
5353
break;
5454
}
5555
case fml::Thread::ThreadPriority::kRaster: {
56-
fml::RequestAffinity(fml::CpuAffinity::kNotLittle);
56+
fml::RequestAffinity(fml::CpuAffinity::kNotEfficiency);
5757
// Android describes -8 as "most important display threads, for
5858
// compositing the screen and retrieving input events". Conservatively
5959
// set the raster thread to slightly lower priority than it.

0 commit comments

Comments
 (0)