Flutter Engine
The Flutter Engine
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 <optional>
9#include <string>
10#include <vector>
11
12namespace fml {
13
14/// The CPU Affinity provides a hint to the operating system on which cores a
15/// particular thread should be scheduled on. The operating system may or may
16/// not honor these requests.
17enum class CpuAffinity {
18 /// @brief Request CPU affinity for the performance cores.
19 ///
20 /// Generally speaking, only the UI and Raster thread should
21 /// use this option.
23
24 /// @brief Request CPU affinity for the efficiency cores.
26
27 /// @brief Request affinity for all non-performance cores.
29};
30
31/// @brief Request count of efficiency cores.
32///
33/// Efficiency cores are defined as those with the lowest reported
34/// cpu_max_freq. If the CPU speed could not be determined, or if all
35/// cores have the same reported speed then this returns std::nullopt.
36/// That is, the result will never be 0.
37std::optional<size_t> EfficiencyCoreCount();
38
39/// @brief Request the given affinity for the current thread.
40///
41/// Returns true if successfull, or if it was a no-op. This function is
42/// only supported on Android devices.
43///
44/// Affinity requests are based on documented CPU speed. This speed data
45/// is parsed from cpuinfo_max_freq files, see also:
46/// https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt
47bool RequestAffinity(CpuAffinity affinity);
48
50 // The index of the given CPU.
51 size_t index;
52 // CPU speed in kHZ
53 int64_t speed;
54};
55
56/// @brief A class that computes the correct CPU indices for a requested CPU
57/// affinity.
58///
59/// @note This is visible for testing.
61 public:
62 explicit CPUSpeedTracker(std::vector<CpuIndexAndSpeed> data);
63
64 /// @brief The class is valid if it has more than one CPU index and a distinct
65 /// set of efficiency or performance CPUs.
66 ///
67 /// If all CPUs are the same speed this returns false, and all requests
68 /// to set affinity are ignored.
69 bool IsValid() const;
70
71 /// @brief Return the set of CPU indices for the requested CPU affinity.
72 ///
73 /// If the tracker is valid, this will always return a non-empty set.
74 const std::vector<size_t>& GetIndices(CpuAffinity affinity) const;
75
76 private:
77 bool valid_ = false;
78 std::vector<CpuIndexAndSpeed> cpu_speeds_;
79 std::vector<size_t> efficiency_;
80 std::vector<size_t> performance_;
81 std::vector<size_t> not_performance_;
82};
83
84/// @note Visible for testing.
85std::optional<int64_t> ReadIntFromFile(const std::string& path);
86
87} // namespace fml
88
89#endif // FLUTTER_FML_CPU_AFFINITY_H_
A class that computes the correct CPU indices for a requested CPU affinity.
Definition: cpu_affinity.h:60
CPUSpeedTracker(std::vector< CpuIndexAndSpeed > data)
Definition: cpu_affinity.cc:34
const std::vector< size_t > & GetIndices(CpuAffinity affinity) const
Return the set of CPU indices for the requested CPU affinity.
Definition: cpu_affinity.cc:71
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:67
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
Definition: ascii_trie.cc:9
CpuAffinity
Definition: cpu_affinity.h:17
@ kPerformance
Request CPU affinity for the performance cores.
@ kEfficiency
Request CPU affinity for the efficiency cores.
@ kNotPerformance
Request affinity for all non-performance cores.
std::optional< int64_t > ReadIntFromFile(const std::string &path)
Definition: cpu_affinity.cc:86
bool RequestAffinity(CpuAffinity affinity)
Request the given affinity for the current thread.
Definition: cpu_affinity.cc:26
std::optional< size_t > EfficiencyCoreCount()
Request count of efficiency cores.
Definition: cpu_affinity.cc:18
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63