Flutter Engine
The 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
5#include "flutter/fml/cpu_affinity.h"
6#include "flutter/fml/build_config.h"
7
8#include <fstream>
9#include <optional>
10#include <string>
11
12#ifdef FML_OS_ANDROID
13#include "flutter/fml/platform/android/cpu_affinity.h"
14#endif // FML_OS_ANDROID
15
16namespace fml {
17
18std::optional<size_t> EfficiencyCoreCount() {
19#ifdef FML_OS_ANDROID
21#else
22 return std::nullopt;
23#endif
24}
25
27#ifdef FML_OS_ANDROID
28 return AndroidRequestAffinity(affinity);
29#else
30 return true;
31#endif
32}
33
34CPUSpeedTracker::CPUSpeedTracker(std::vector<CpuIndexAndSpeed> data)
35 : cpu_speeds_(std::move(data)) {
36 std::optional<int64_t> max_speed = std::nullopt;
37 std::optional<int64_t> min_speed = std::nullopt;
38 for (const auto& data : cpu_speeds_) {
39 if (!max_speed.has_value() || data.speed > max_speed.value()) {
40 max_speed = data.speed;
41 }
42 if (!min_speed.has_value() || data.speed < min_speed.value()) {
43 min_speed = data.speed;
44 }
45 }
46 if (!max_speed.has_value() || !min_speed.has_value() ||
47 min_speed.value() == max_speed.value()) {
48 return;
49 }
50 const int64_t max_speed_value = max_speed.value();
51 const int64_t min_speed_value = min_speed.value();
52
53 for (const auto& data : cpu_speeds_) {
54 if (data.speed == max_speed_value) {
55 performance_.push_back(data.index);
56 } else {
57 not_performance_.push_back(data.index);
58 }
59 if (data.speed == min_speed_value) {
60 efficiency_.push_back(data.index);
61 }
62 }
63
64 valid_ = true;
65}
66
68 return valid_;
69}
70
71const std::vector<size_t>& CPUSpeedTracker::GetIndices(
72 CpuAffinity affinity) const {
73 switch (affinity) {
75 return performance_;
77 return efficiency_;
79 return not_performance_;
80 }
81}
82
83// Get the size of the cpuinfo file by reading it until the end. This is
84// required because files under /proc do not always return a valid size
85// when using fseek(0, SEEK_END) + ftell(). Nor can they be mmap()-ed.
86std::optional<int64_t> ReadIntFromFile(const std::string& path) {
87 std::ifstream file;
88 file.open(path.c_str());
89
90 // Dont use stoi because if this data isnt a parseable number then it
91 // will abort, as we compile with exceptions disabled.
92 int64_t speed = 0;
93 file >> speed;
94 if (speed > 0) {
95 return speed;
96 }
97 return std::nullopt;
98}
99
100} // 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
@ 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:256