Flutter Engine
The Flutter Engine
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/platform/android/cpu_affinity.h"
6
7#include <pthread.h>
8#include <sys/resource.h>
9#include <sys/time.h>
10#include <unistd.h>
11#include <mutex>
12#include <optional>
13#include <thread>
14#include "flutter/fml/logging.h"
15
16namespace fml {
17
18/// The CPUSpeedTracker is initialized once the first time a thread affinity is
19/// requested.
20std::once_flag gCPUTrackerFlag;
22
23// For each CPU index provided, attempts to open the file
24// /sys/devices/system/cpu/cpu$NUM/cpufreq/cpuinfo_max_freq and parse a number
25// containing the CPU frequency.
26void InitCPUInfo(size_t cpu_count) {
27 std::vector<CpuIndexAndSpeed> cpu_speeds;
28
29 for (auto i = 0u; i < cpu_count; i++) {
30 auto path = "/sys/devices/system/cpu/cpu" + std::to_string(i) +
31 "/cpufreq/cpuinfo_max_freq";
32 auto speed = ReadIntFromFile(path);
33 if (speed.has_value()) {
34 cpu_speeds.push_back({.index = i, .speed = speed.value()});
35 }
36 }
37 gCPUTracker = new CPUSpeedTracker(cpu_speeds);
38}
39
41 // Populate CPU Info if uninitialized.
42 auto count = std::thread::hardware_concurrency();
43 std::call_once(gCPUTrackerFlag, [count]() { InitCPUInfo(count); });
44 if (gCPUTracker == nullptr || !gCPUTracker->IsValid()) {
45 return false;
46 }
47 return true;
48}
49
50std::optional<size_t> AndroidEfficiencyCoreCount() {
51 if (!SetUpCPUTracker()) {
52 return true;
53 }
55 FML_DCHECK(result > 0);
56 return result;
57}
58
60 if (!SetUpCPUTracker()) {
61 return true;
62 }
63
64 cpu_set_t set;
65 CPU_ZERO(&set);
66 for (const auto index : gCPUTracker->GetIndices(affinity)) {
67 CPU_SET(index, &set);
68 }
69 return sched_setaffinity(gettid(), sizeof(set), &set) == 0;
70}
71
72} // namespace fml
int count
Definition: FontMgrTest.cpp:50
A class that computes the correct CPU indices for a requested CPU affinity.
Definition: cpu_affinity.h:60
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
GAsyncResult * result
#define FML_DCHECK(condition)
Definition: logging.h:103
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
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 to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not set
Definition: switches.h:76
Definition: ascii_trie.cc:9
CpuAffinity
Definition: cpu_affinity.h:17
@ kEfficiency
Request CPU affinity for the efficiency cores.
std::optional< int64_t > ReadIntFromFile(const std::string &path)
Definition: cpu_affinity.cc:86
static CPUSpeedTracker * gCPUTracker
Definition: cpu_affinity.cc:21
std::optional< size_t > AndroidEfficiencyCoreCount()
Android specific implementation of EfficiencyCoreCount.
Definition: cpu_affinity.cc:50
bool AndroidRequestAffinity(CpuAffinity affinity)
Android specific implementation of RequestAffinity.
Definition: cpu_affinity.cc:59
std::once_flag gCPUTrackerFlag
Definition: cpu_affinity.cc:20
bool SetUpCPUTracker()
Definition: cpu_affinity.cc:40
void InitCPUInfo(size_t cpu_count)
Definition: cpu_affinity.cc:26
static SkString to_string(int n)
Definition: nanobench.cpp:119