Flutter Engine
The 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 <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.
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
@ 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.