Flutter Engine
 
Loading...
Searching...
No Matches
cpu_affinity.cc
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
7
8#include <cstdint>
9#include <fstream>
10#include <optional>
11#include <string>
12
13#ifdef FML_OS_ANDROID
15#endif // FML_OS_ANDROID
16
17namespace fml {
18
19std::optional<size_t> EfficiencyCoreCount() {
20#ifdef FML_OS_ANDROID
22#else
23 return std::nullopt;
24#endif
25}
26
28#ifdef FML_OS_ANDROID
29 return AndroidRequestAffinity(affinity);
30#else
31 return true;
32#endif
33}
34
35CPUSpeedTracker::CPUSpeedTracker(std::vector<CpuIndexAndSpeed> data)
36 : cpu_speeds_(std::move(data)) {
37 std::optional<int64_t> max_speed = std::nullopt;
38 std::optional<int64_t> min_speed = std::nullopt;
39 for (const auto& data : cpu_speeds_) {
40 if (!max_speed.has_value() || data.speed > max_speed.value()) {
41 max_speed = data.speed;
42 }
43 if (!min_speed.has_value() || data.speed < min_speed.value()) {
44 min_speed = data.speed;
45 }
46 }
47 if (!max_speed.has_value() || !min_speed.has_value() ||
48 min_speed.value() == max_speed.value()) {
49 return;
50 }
51 const int64_t max_speed_value = max_speed.value();
52 const int64_t min_speed_value = min_speed.value();
53
54 for (const auto& data : cpu_speeds_) {
55 if (data.speed == max_speed_value) {
56 performance_.push_back(data.index);
57 } else {
58 not_performance_.push_back(data.index);
59 }
60 if (data.speed == min_speed_value) {
61 efficiency_.push_back(data.index);
62 } else {
63 not_efficiency_.push_back(data.index);
64 }
65 }
66
67 valid_ = true;
68}
69
71 return valid_;
72}
73
74const std::vector<size_t>& CPUSpeedTracker::GetIndices(
75 CpuAffinity affinity) const {
76 switch (affinity) {
78 return performance_;
80 return efficiency_;
82 return not_performance_;
84 return not_efficiency_;
85 }
86}
87
88// Get the size of the cpuinfo file by reading it until the end. This is
89// required because files under /proc do not always return a valid size
90// when using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed.
91std::optional<int64_t> ReadIntFromFile(const std::string& path) {
92 std::ifstream file;
93 file.open(path.c_str());
94
95 // Dont use stoi because if this data isnt a parseable number then it
96 // will abort, as we compile with exceptions disabled.
97 int64_t speed = 0;
98 file >> speed;
99 if (speed > 0) {
100 return speed;
101 }
102 return std::nullopt;
103}
104
105} // namespace fml
CPUSpeedTracker(std::vector< CpuIndexAndSpeed > data)
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)
std::optional< size_t > AndroidEfficiencyCoreCount()
Android specific implementation of EfficiencyCoreCount.
bool RequestAffinity(CpuAffinity affinity)
Request the given affinity for the current thread.
bool AndroidRequestAffinity(CpuAffinity affinity)
Android specific implementation of RequestAffinity.
std::optional< size_t > EfficiencyCoreCount()
Request count of efficiency cores.
Definition ref_ptr.h:261
std::shared_ptr< const fml::Mapping > data