1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include "adaptivecpu/CpuFrequencyReader.h"
20 #include "mocks.h"
21
22 using testing::_;
23 using testing::ByMove;
24 using testing::Return;
25 using std::chrono_literals::operator""ms;
26
27 namespace aidl {
28 namespace google {
29 namespace hardware {
30 namespace power {
31 namespace impl {
32 namespace pixel {
33
operator <<(std::ostream & stream,const CpuPolicyAverageFrequency & frequency)34 std::ostream &operator<<(std::ostream &stream, const CpuPolicyAverageFrequency &frequency) {
35 return stream << "CpuPolicyAverageFrequency(" << frequency.policyId << ", "
36 << frequency.averageFrequencyHz << ")";
37 }
38
TEST(CpuFrequencyReaderTest,cpuPolicyIds)39 TEST(CpuFrequencyReaderTest, cpuPolicyIds) {
40 std::unique_ptr<MockFilesystem> filesystem = std::make_unique<MockFilesystem>();
41 EXPECT_CALL(*filesystem, ListDirectory("/sys/devices/system/cpu/cpufreq", _))
42 .WillOnce([](auto _path __attribute__((unused)), auto result) {
43 *result = std::vector<std::string>{"ignored1", "policy1", "ignored2",
44 "policy5", "policy10", "policybad"};
45 return true;
46 });
47 EXPECT_CALL(*filesystem, ReadFileStream(_, _))
48 .WillRepeatedly([](auto _path __attribute__((unused)), auto result) {
49 *result = std::make_unique<std::istringstream>("1 2\n3 4\n");
50 return true;
51 });
52
53 CpuFrequencyReader reader(std::move(filesystem));
54 EXPECT_TRUE(reader.Init());
55
56 std::map<uint32_t, std::map<uint64_t, std::chrono::milliseconds>> expected = {
57 {1, {{1, 20ms}, {3, 40ms}}}, {5, {{1, 20ms}, {3, 40ms}}}, {10, {{1, 20ms}, {3, 40ms}}}};
58 EXPECT_EQ(reader.GetPreviousCpuPolicyFrequencies(), expected);
59 }
60
TEST(CpuFrequencyReaderTest,getRecentCpuPolicyFrequencies)61 TEST(CpuFrequencyReaderTest, getRecentCpuPolicyFrequencies) {
62 std::unique_ptr<MockFilesystem> filesystem = std::make_unique<MockFilesystem>();
63 EXPECT_CALL(*filesystem, ListDirectory("/sys/devices/system/cpu/cpufreq", _))
64 .WillOnce([](auto _path __attribute__((unused)), auto result) {
65 *result = std::vector<std::string>{"policy1", "policy2"};
66 return true;
67 });
68 EXPECT_CALL(*filesystem,
69 ReadFileStream("/sys/devices/system/cpu/cpufreq/policy1/stats/time_in_state", _))
70 .Times(2)
71 .WillOnce([](auto _path __attribute__((unused)), auto result) {
72 *result = std::make_unique<std::istringstream>("1000 5\n2000 4");
73 return true;
74 })
75 .WillOnce([](auto _path __attribute__((unused)), auto result) {
76 *result = std::make_unique<std::istringstream>("1000 7\n2000 10");
77 return true;
78 });
79 EXPECT_CALL(*filesystem,
80 ReadFileStream("/sys/devices/system/cpu/cpufreq/policy2/stats/time_in_state", _))
81 .Times(2)
82 .WillOnce([](auto _path __attribute__((unused)), auto result) {
83 *result = std::make_unique<std::istringstream>("1500 1\n2500 23");
84 return true;
85 })
86 .WillOnce([](auto _path __attribute__((unused)), auto result) {
87 *result = std::make_unique<std::istringstream>("1500 5\n2500 23");
88 return true;
89 });
90
91 CpuFrequencyReader reader(std::move(filesystem));
92 EXPECT_TRUE(reader.Init());
93
94 std::vector<CpuPolicyAverageFrequency> actual;
95 EXPECT_TRUE(reader.GetRecentCpuPolicyFrequencies(&actual));
96 EXPECT_EQ(actual, std::vector<CpuPolicyAverageFrequency>({
97 {.policyId = 1, .averageFrequencyHz = 1750},
98 {.policyId = 2, .averageFrequencyHz = 1500},
99 }));
100 }
101
TEST(CpuFrequencyReaderTest,getRecentCpuPolicyFrequencies_frequenciesChange)102 TEST(CpuFrequencyReaderTest, getRecentCpuPolicyFrequencies_frequenciesChange) {
103 std::unique_ptr<MockFilesystem> filesystem = std::make_unique<MockFilesystem>();
104 EXPECT_CALL(*filesystem, ListDirectory("/sys/devices/system/cpu/cpufreq", _))
105 .WillOnce([](auto _path __attribute__((unused)), auto result) {
106 *result = std::vector<std::string>{"policy1"};
107 return true;
108 });
109 EXPECT_CALL(*filesystem,
110 ReadFileStream("/sys/devices/system/cpu/cpufreq/policy1/stats/time_in_state", _))
111 .Times(2)
112 .WillOnce([](auto _path __attribute__((unused)), auto result) {
113 *result = std::make_unique<std::istringstream>("1000 5\n2000 4");
114 return true;
115 })
116 .WillOnce([](auto _path __attribute__((unused)), auto result) {
117 *result = std::make_unique<std::istringstream>("1000 6\n2001 4");
118 return true;
119 });
120
121 CpuFrequencyReader reader(std::move(filesystem));
122 EXPECT_TRUE(reader.Init());
123
124 std::vector<CpuPolicyAverageFrequency> actual;
125 EXPECT_FALSE(reader.GetRecentCpuPolicyFrequencies(&actual));
126 }
127
TEST(CpuFrequencyReaderTest,getRecentCpuPolicyFrequencies_badFormat)128 TEST(CpuFrequencyReaderTest, getRecentCpuPolicyFrequencies_badFormat) {
129 std::unique_ptr<MockFilesystem> filesystem = std::make_unique<MockFilesystem>();
130 EXPECT_CALL(*filesystem, ListDirectory("/sys/devices/system/cpu/cpufreq", _))
131 .WillOnce([](auto _path __attribute__((unused)), auto result) {
132 *result = std::vector<std::string>{"policy1"};
133 return true;
134 });
135 EXPECT_CALL(*filesystem,
136 ReadFileStream("/sys/devices/system/cpu/cpufreq/policy1/stats/time_in_state", _))
137 .Times(2)
138 .WillOnce([](auto _path __attribute__((unused)), auto result) {
139 *result = std::make_unique<std::istringstream>("1000 1");
140 return true;
141 })
142 .WillOnce([](auto _path __attribute__((unused)), auto result) {
143 *result = std::make_unique<std::istringstream>("1000 2\nfoo");
144 return true;
145 });
146
147 CpuFrequencyReader reader(std::move(filesystem));
148 EXPECT_TRUE(reader.Init());
149
150 std::vector<CpuPolicyAverageFrequency> actual;
151 EXPECT_FALSE(reader.GetRecentCpuPolicyFrequencies(&actual));
152 }
153
154 } // namespace pixel
155 } // namespace impl
156 } // namespace power
157 } // namespace hardware
158 } // namespace google
159 } // namespace aidl
160