Flutter Engine
Loading...
Searching...
No Matches
cpu_affinity.h
Go to the documentation of this file.
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
#ifndef FLUTTER_FML_CPU_AFFINITY_H_
6
#define FLUTTER_FML_CPU_AFFINITY_H_
7
8
#include <cstdint>
9
#include <optional>
10
#include <string>
11
#include <vector>
12
13
namespace
fml
{
14
15
/// The CPU Affinity provides a hint to the operating system on which cores a
16
/// particular thread should be scheduled on. The operating system may or may
17
/// not honor these requests.
18
enum class
CpuAffinity
{
19
/// @brief Request CPU affinity for the performance cores.
20
///
21
/// Generally speaking, only the UI and Raster thread should
22
/// use this option.
23
kPerformance
,
24
25
/// @brief Request CPU affinity for the efficiency cores.
26
kEfficiency
,
27
28
/// @brief Request affinity for all non-performance cores.
29
kNotPerformance
,
30
31
/// @brief Request affinity for all non-efficiency cores.
32
kNotEfficiency
,
33
};
34
35
/// @brief Request count of efficiency cores.
36
///
37
/// Efficiency cores are defined as those with the lowest reported
38
/// cpu_max_freq. If the CPU speed could not be determined, or if all
39
/// cores have the same reported speed then this returns std::nullopt.
40
/// That is, the result will never be 0.
41
std::optional<size_t>
EfficiencyCoreCount
();
42
43
/// @brief Request the given affinity for the current thread.
44
///
45
/// Returns true if successfull, or if it was a no-op. This function is
46
/// only supported on Android devices.
47
///
48
/// Affinity requests are based on documented CPU speed. This speed data
49
/// is parsed from cpuinfo_max_freq files, see also:
50
/// https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt
51
bool
RequestAffinity
(
CpuAffinity
affinity);
52
53
struct
CpuIndexAndSpeed
{
54
// The index of the given CPU.
55
size_t
index
;
56
// CPU speed in kHZ
57
int64_t
speed
;
58
};
59
60
/// @brief A class that computes the correct CPU indices for a requested CPU
61
/// affinity.
62
///
63
/// @note This is visible for testing.
64
class
CPUSpeedTracker
{
65
public
:
66
explicit
CPUSpeedTracker
(std::vector<CpuIndexAndSpeed>
data
);
67
68
/// @brief The class is valid if it has more than one CPU index and a distinct
69
/// set of efficiency or performance CPUs.
70
///
71
/// If all CPUs are the same speed this returns false, and all requests
72
/// to set affinity are ignored.
73
bool
IsValid
()
const
;
74
75
/// @brief Return the set of CPU indices for the requested CPU affinity.
76
///
77
/// If the tracker is valid, this will always return a non-empty set.
78
const
std::vector<size_t>&
GetIndices
(
CpuAffinity
affinity)
const
;
79
80
private
:
81
bool
valid_ =
false
;
82
std::vector<CpuIndexAndSpeed> cpu_speeds_;
83
std::vector<size_t> efficiency_;
84
std::vector<size_t> performance_;
85
std::vector<size_t> not_performance_;
86
std::vector<size_t> not_efficiency_;
87
};
88
89
/// @note Visible for testing.
90
std::optional<int64_t>
ReadIntFromFile
(
const
std::string& path);
91
92
}
// namespace fml
93
94
#endif
// FLUTTER_FML_CPU_AFFINITY_H_
fml::CPUSpeedTracker
A class that computes the correct CPU indices for a requested CPU affinity.
Definition
cpu_affinity.h:64
fml::CPUSpeedTracker::GetIndices
const std::vector< size_t > & GetIndices(CpuAffinity affinity) const
Return the set of CPU indices for the requested CPU affinity.
Definition
cpu_affinity.cc:74
fml::CPUSpeedTracker::IsValid
bool IsValid() const
The class is valid if it has more than one CPU index and a distinct set of efficiency or performance ...
Definition
cpu_affinity.cc:70
fml
Definition
ascii_trie.cc:9
fml::CpuAffinity
CpuAffinity
Definition
cpu_affinity.h:18
fml::CpuAffinity::kNotEfficiency
@ kNotEfficiency
Request affinity for all non-efficiency cores.
fml::CpuAffinity::kPerformance
@ kPerformance
Request CPU affinity for the performance cores.
fml::CpuAffinity::kEfficiency
@ kEfficiency
Request CPU affinity for the efficiency cores.
fml::CpuAffinity::kNotPerformance
@ kNotPerformance
Request affinity for all non-performance cores.
fml::ReadIntFromFile
std::optional< int64_t > ReadIntFromFile(const std::string &path)
Definition
cpu_affinity.cc:91
fml::RequestAffinity
bool RequestAffinity(CpuAffinity affinity)
Request the given affinity for the current thread.
Definition
cpu_affinity.cc:27
fml::EfficiencyCoreCount
std::optional< size_t > EfficiencyCoreCount()
Request count of efficiency cores.
Definition
cpu_affinity.cc:19
fml::CpuIndexAndSpeed
Definition
cpu_affinity.h:53
fml::CpuIndexAndSpeed::speed
int64_t speed
Definition
cpu_affinity.h:57
fml::CpuIndexAndSpeed::index
size_t index
Definition
cpu_affinity.h:55
data
std::shared_ptr< const fml::Mapping > data
Definition
texture_gles.cc:68
fml
cpu_affinity.h
Generated on Thu Nov 6 2025 16:11:22 for Flutter Engine by
1.9.8