Flutter Engine
 
Loading...
Searching...
No Matches
cpu_affinity_unittests.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 "cpu_affinity.h"
6
7#include "fml/file.h"
8#include "fml/mapping.h"
9#include "gtest/gtest.h"
10#include "logging.h"
11
12namespace fml {
13namespace testing {
14
15TEST(CpuAffinity, NonAndroidPlatformDefaults) {
16 ASSERT_FALSE(fml::EfficiencyCoreCount().has_value());
18}
19
20TEST(CpuAffinity, NormalSlowMedFastCores) {
21 auto speeds = {CpuIndexAndSpeed{.index = 0, .speed = 1},
22 CpuIndexAndSpeed{.index = 1, .speed = 2},
23 CpuIndexAndSpeed{.index = 2, .speed = 3}};
24 auto tracker = CPUSpeedTracker(speeds);
25
26 ASSERT_TRUE(tracker.IsValid());
27 ASSERT_EQ(tracker.GetIndices(CpuAffinity::kEfficiency)[0], 0u);
28 ASSERT_EQ(tracker.GetIndices(CpuAffinity::kPerformance)[0], 2u);
29 ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotPerformance).size(), 2u);
30 ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotPerformance)[0], 0u);
31 ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotPerformance)[1], 1u);
32 ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotEfficiency).size(), 2u);
33 ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotEfficiency)[0], 1u);
34 ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotEfficiency)[1], 2u);
35}
36
37TEST(CpuAffinity, NoCpuData) {
38 auto tracker = CPUSpeedTracker({});
39
40 ASSERT_FALSE(tracker.IsValid());
41}
42
43TEST(CpuAffinity, AllSameSpeed) {
44 auto speeds = {CpuIndexAndSpeed{.index = 0, .speed = 1},
45 CpuIndexAndSpeed{.index = 1, .speed = 1},
46 CpuIndexAndSpeed{.index = 2, .speed = 1}};
47 auto tracker = CPUSpeedTracker(speeds);
48
49 ASSERT_FALSE(tracker.IsValid());
50}
51
52TEST(CpuAffinity, SingleCore) {
53 auto speeds = {CpuIndexAndSpeed{.index = 0, .speed = 1}};
54 auto tracker = CPUSpeedTracker(speeds);
55
56 ASSERT_FALSE(tracker.IsValid());
57}
58
59TEST(CpuAffinity, FileParsing) {
61 ASSERT_TRUE(base_dir.fd().is_valid());
62
63 // Generate a fake CPU speed file
64 fml::DataMapping test_data(std::string("12345"));
65 ASSERT_TRUE(fml::WriteAtomically(base_dir.fd(), "test_file", test_data));
66
67 auto file = fml::OpenFileReadOnly(base_dir.fd(), "test_file");
68 ASSERT_TRUE(file.is_valid());
69
70 // Open file and parse speed.
71 auto result = ReadIntFromFile(base_dir.path() + "/test_file");
72 ASSERT_TRUE(result.has_value());
73 ASSERT_EQ(result.value_or(0), 12345);
74}
75
76TEST(CpuAffinity, FileParsingWithNonNumber) {
78 ASSERT_TRUE(base_dir.fd().is_valid());
79
80 // Generate a fake CPU speed file
81 fml::DataMapping test_data(std::string("whoa this isnt a number"));
82 ASSERT_TRUE(fml::WriteAtomically(base_dir.fd(), "test_file", test_data));
83
84 auto file = fml::OpenFileReadOnly(base_dir.fd(), "test_file");
85 ASSERT_TRUE(file.is_valid());
86
87 // Open file and parse speed.
88 auto result = ReadIntFromFile(base_dir.path() + "/test_file");
89 ASSERT_FALSE(result.has_value());
90}
91
92TEST(CpuAffinity, MissingFileParsing) {
93 auto result = ReadIntFromFile("/does_not_exist");
94 ASSERT_FALSE(result.has_value());
95}
96
97} // namespace testing
98} // namespace fml
A class that computes the correct CPU indices for a requested CPU affinity.
const UniqueFD & fd()
Definition file.h:147
const std::string & path() const
Definition file.h:146
bool is_valid() const
TEST(BacktraceTest, CanGatherBacktrace)
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.
fml::UniqueFD OpenFileReadOnly(const fml::UniqueFD &base_directory, const char *path)
Definition file.cc:92
bool WriteAtomically(const fml::UniqueFD &base_directory, const char *file_name, const Mapping &mapping)
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.