forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_affinity_unittests.cc
95 lines (73 loc) · 2.93 KB
/
cpu_affinity_unittests.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cpu_affinity.h"
#include "fml/file.h"
#include "fml/mapping.h"
#include "gtest/gtest.h"
#include "logging.h"
namespace fml {
namespace testing {
TEST(CpuAffinity, NonAndroidPlatformDefaults) {
ASSERT_FALSE(fml::EfficiencyCoreCount().has_value());
ASSERT_TRUE(fml::RequestAffinity(fml::CpuAffinity::kEfficiency));
}
TEST(CpuAffinity, NormalSlowMedFastCores) {
auto speeds = {CpuIndexAndSpeed{.index = 0, .speed = 1},
CpuIndexAndSpeed{.index = 1, .speed = 2},
CpuIndexAndSpeed{.index = 2, .speed = 3}};
auto tracker = CPUSpeedTracker(speeds);
ASSERT_TRUE(tracker.IsValid());
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kEfficiency)[0], 0u);
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kPerformance)[0], 2u);
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotPerformance).size(), 2u);
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotPerformance)[0], 0u);
ASSERT_EQ(tracker.GetIndices(CpuAffinity::kNotPerformance)[1], 1u);
}
TEST(CpuAffinity, NoCpuData) {
auto tracker = CPUSpeedTracker({});
ASSERT_FALSE(tracker.IsValid());
}
TEST(CpuAffinity, AllSameSpeed) {
auto speeds = {CpuIndexAndSpeed{.index = 0, .speed = 1},
CpuIndexAndSpeed{.index = 1, .speed = 1},
CpuIndexAndSpeed{.index = 2, .speed = 1}};
auto tracker = CPUSpeedTracker(speeds);
ASSERT_FALSE(tracker.IsValid());
}
TEST(CpuAffinity, SingleCore) {
auto speeds = {CpuIndexAndSpeed{.index = 0, .speed = 1}};
auto tracker = CPUSpeedTracker(speeds);
ASSERT_FALSE(tracker.IsValid());
}
TEST(CpuAffinity, FileParsing) {
fml::ScopedTemporaryDirectory base_dir;
ASSERT_TRUE(base_dir.fd().is_valid());
// Generate a fake CPU speed file
fml::DataMapping test_data(std::string("12345"));
ASSERT_TRUE(fml::WriteAtomically(base_dir.fd(), "test_file", test_data));
auto file = fml::OpenFileReadOnly(base_dir.fd(), "test_file");
ASSERT_TRUE(file.is_valid());
// Open file and parse speed.
auto result = ReadIntFromFile(base_dir.path() + "/test_file");
ASSERT_TRUE(result.has_value());
ASSERT_EQ(result.value_or(0), 12345);
}
TEST(CpuAffinity, FileParsingWithNonNumber) {
fml::ScopedTemporaryDirectory base_dir;
ASSERT_TRUE(base_dir.fd().is_valid());
// Generate a fake CPU speed file
fml::DataMapping test_data(std::string("whoa this isnt a number"));
ASSERT_TRUE(fml::WriteAtomically(base_dir.fd(), "test_file", test_data));
auto file = fml::OpenFileReadOnly(base_dir.fd(), "test_file");
ASSERT_TRUE(file.is_valid());
// Open file and parse speed.
auto result = ReadIntFromFile(base_dir.path() + "/test_file");
ASSERT_FALSE(result.has_value());
}
TEST(CpuAffinity, MissingFileParsing) {
auto result = ReadIntFromFile("/does_not_exist");
ASSERT_FALSE(result.has_value());
}
} // namespace testing
} // namespace fml