Flutter Engine
The 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}
33
34TEST(CpuAffinity, NoCpuData) {
35 auto tracker = CPUSpeedTracker({});
36
37 ASSERT_FALSE(tracker.IsValid());
38}
39
40TEST(CpuAffinity, AllSameSpeed) {
41 auto speeds = {CpuIndexAndSpeed{.index = 0, .speed = 1},
42 CpuIndexAndSpeed{.index = 1, .speed = 1},
43 CpuIndexAndSpeed{.index = 2, .speed = 1}};
44 auto tracker = CPUSpeedTracker(speeds);
45
46 ASSERT_FALSE(tracker.IsValid());
47}
48
49TEST(CpuAffinity, SingleCore) {
50 auto speeds = {CpuIndexAndSpeed{.index = 0, .speed = 1}};
51 auto tracker = CPUSpeedTracker(speeds);
52
53 ASSERT_FALSE(tracker.IsValid());
54}
55
56TEST(CpuAffinity, FileParsing) {
58 ASSERT_TRUE(base_dir.fd().is_valid());
59
60 // Generate a fake CPU speed file
61 fml::DataMapping test_data(std::string("12345"));
62 ASSERT_TRUE(fml::WriteAtomically(base_dir.fd(), "test_file", test_data));
63
64 auto file = fml::OpenFileReadOnly(base_dir.fd(), "test_file");
65 ASSERT_TRUE(file.is_valid());
66
67 // Open file and parse speed.
68 auto result = ReadIntFromFile(base_dir.path() + "/test_file");
69 ASSERT_TRUE(result.has_value());
70 ASSERT_EQ(result.value_or(0), 12345);
71}
72
73TEST(CpuAffinity, FileParsingWithNonNumber) {
75 ASSERT_TRUE(base_dir.fd().is_valid());
76
77 // Generate a fake CPU speed file
78 fml::DataMapping test_data(std::string("whoa this isnt a number"));
79 ASSERT_TRUE(fml::WriteAtomically(base_dir.fd(), "test_file", test_data));
80
81 auto file = fml::OpenFileReadOnly(base_dir.fd(), "test_file");
82 ASSERT_TRUE(file.is_valid());
83
84 // Open file and parse speed.
85 auto result = ReadIntFromFile(base_dir.path() + "/test_file");
86 ASSERT_FALSE(result.has_value());
87}
88
89TEST(CpuAffinity, MissingFileParsing) {
90 auto result = ReadIntFromFile("/does_not_exist");
91 ASSERT_FALSE(result.has_value());
92}
93
94} // namespace testing
95} // namespace fml
#define TEST(S, s, D, expected)
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
GAsyncResult * result
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.
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.