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
13namespace 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.
18enum 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.
24
25 /// @brief Request CPU affinity for the efficiency cores.
27
28 /// @brief Request affinity for all non-performance cores.
30
31 /// @brief Request affinity for all non-efficiency cores.
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.
41std::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
51bool RequestAffinity(CpuAffinity affinity);
52
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.
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.
90std::optional<int64_t> ReadIntFromFile(const std::string& path);
91
92} // namespace fml
93
94#endif // FLUTTER_FML_CPU_AFFINITY_H_
A class that computes the correct CPU indices for a requested CPU affinity.
const std::vector< size_t > & GetIndices(CpuAffinity affinity) const
Return the set of CPU indices for the requested CPU affinity.
bool IsValid() const
The class is valid if it has more than one CPU index and a distinct set of efficiency or performance ...
CpuAffinity
@ kNotEfficiency
Request affinity for all non-efficiency cores.
@ 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)
bool RequestAffinity(CpuAffinity affinity)
Request the given affinity for the current thread.
std::optional< size_t > EfficiencyCoreCount()
Request count of efficiency cores.
std::shared_ptr< const fml::Mapping > data