Debian Codegraph @

libperfmgr/tests/FileNodeTest.cc

raw
1 /*
2 * Copyright (C) 2017 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 specic language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <algorithm>
18 #include <thread>
19
20 #include <android-base/file.h>
21 #include <android-base/stringprintf.h>
22
23 #include <gtest/gtest.h>
24
25 #include "perfmgr/FileNode.h"
26
27 namespace android {
28 namespace perfmgr {
29
30 using namespace std::chrono_literals;
31
32 constexpr double kTIMING_TOLERANCE_MS = std::chrono::milliseconds(25).count();
33 constexpr auto kSLEEP_TOLERANCE_MS = 2ms;
34
35 static inline void _VerifyPathValue(const std::string& path,
36 const std::string& value) {
37 std::string s;
38 EXPECT_TRUE(android::base::ReadFileToString(path, &s)) << strerror(errno);
39 EXPECT_EQ(value, s);
40 }
41
42 // Test init with no default value
43 TEST(FileNodeTest, NoInitDefaultTest) {
44 TemporaryFile tf;
45 FileNode t("t", tf.path, {{"value0"}, {"value1"}, {"value2"}}, 1, false);
46 _VerifyPathValue(tf.path, "");
47 }
48
49 // Test init with default value
50 TEST(FileNodeTest, InitDefaultTest) {
51 TemporaryFile tf;
52 FileNode t("t", tf.path, {{"value0"}, {"value1"}, {"value2"}}, 1, true);
53 _VerifyPathValue(tf.path, "value1");
54 TemporaryFile tf2;
55 FileNode t2("t2", tf2.path, {{"value0"}, {"value1"}, {"value2"}}, 0, true);
56 _VerifyPathValue(tf2.path, "value0");
57 }
58
59 // Test DumpToFd
60 TEST(FileNodeTest, DumpToFdTest) {
61 TemporaryFile tf;
62 FileNode t("test_dump", tf.path, {{"value0"}, {"value1"}, {"value2"}}, 1,
63 true);
64 TemporaryFile dumptf;
65 t.DumpToFd(dumptf.fd);
66 fsync(dumptf.fd);
67 std::string buf(
68 android::base::StringPrintf("test_dump\t%s\t1\tvalue1\n", tf.path));
69 _VerifyPathValue(dumptf.path, buf);
70 }
71
72 // Test GetValueIndex
73 TEST(FileNodeTest, GetValueIndexTest) {
74 TemporaryFile tf;
75 FileNode t("t", tf.path, {{"value0"}, {"value1"}, {"value2"}}, 1, false);
76 std::size_t index = 0;
77 EXPECT_TRUE(t.GetValueIndex("value2", &index));
78 EXPECT_EQ(2u, index);
79 index = 1234;
80 EXPECT_FALSE(t.GetValueIndex("NON_EXIST", &index));
81 EXPECT_EQ(1234u, index);
82 }
83
84 // Test GetValues
85 TEST(FileNodeTest, GetValuesTest) {
86 TemporaryFile tf;
87 FileNode t("t", tf.path, {{"value0"}, {"value1"}, {"value2"}}, 1, false);
88 std::vector values = t.GetValues();
89 EXPECT_EQ(3u, values.size());
90 EXPECT_EQ("value0", values[0]);
91 EXPECT_EQ("value1", values[1]);
92 EXPECT_EQ("value2", values[2]);
93 }
94
95 // Test get more properties
96 TEST(FileNodeTest, GetPropertiesTest) {
97 std::string test_name = "TESTREQ_1";
98 std::string test_path = "TEST_PATH";
99 FileNode t(test_name, test_path, {}, 0, false, true);
100 EXPECT_EQ(test_name, t.GetName());
101 EXPECT_EQ(test_path, t.GetPath());
102 EXPECT_EQ(0u, t.GetValues().size());
103 EXPECT_EQ(0u, t.GetDefaultIndex());
104 EXPECT_FALSE(t.GetResetOnInit());
105 EXPECT_TRUE(t.GetHoldFd());
106 }
107
108 // Test add request fail and retry
109 TEST(FileNodeTest, AddRequestTestFail) {
110 FileNode t("t", "/sys/android/nonexist_node_test",
111 {{"value0"}, {"value1"}, {"value2"}}, 2, true);
112 auto start = std::chrono::steady_clock::now();
113 EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 200ms));
114 std::chrono::milliseconds expire_time = t.Update(true);
115 // Add request @ value1
116 EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
117 kTIMING_TOLERANCE_MS);
118 // Add request @ value0 higher prio than value1
119 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 2000ms));
120 expire_time = t.Update(true);
121 // Retry in 500 ms
122 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
123 kTIMING_TOLERANCE_MS);
124 }
125
126 // Test add request
127 TEST(FileNodeTest, AddRequestTest) {
128 TemporaryFile tf;
129 FileNode t("t", tf.path, {{"value0"}, {"value1"}, {"value2"}}, 2, true);
130 auto start = std::chrono::steady_clock::now();
131 EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 500ms));
132 std::chrono::milliseconds expire_time = t.Update(true);
133 // Add request @ value1
134 _VerifyPathValue(tf.path, "value1");
135 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
136 kTIMING_TOLERANCE_MS);
137 // Add request @ value0 higher prio than value1
138 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 200ms));
139 expire_time = t.Update(true);
140 _VerifyPathValue(tf.path, "value0");
141 EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
142 kTIMING_TOLERANCE_MS);
143 // Let high prio request timeout, now only request @ value1 active
144 std::this_thread::sleep_for(expire_time + kSLEEP_TOLERANCE_MS);
145 expire_time = t.Update(true);
146 _VerifyPathValue(tf.path, "value1");
147 EXPECT_NEAR(std::chrono::milliseconds(300).count(), expire_time.count(),
148 kTIMING_TOLERANCE_MS);
149 // Let all requests timeout, now default value2
150 std::this_thread::sleep_for(expire_time + kSLEEP_TOLERANCE_MS);
151 expire_time = t.Update(true);
152 _VerifyPathValue(tf.path, "value2");
153 EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
154 }
155
156 // Test remove request
157 TEST(FileNodeTest, RemoveRequestTest) {
158 TemporaryFile tf;
159 FileNode t("t", tf.path, {{"value0"}, {"value1"}, {"value2"}}, 2, true);
160 auto start = std::chrono::steady_clock::now();
161 EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 500ms));
162 std::chrono::milliseconds expire_time = t.Update(true);
163 // Add request @ value1
164 _VerifyPathValue(tf.path, "value1");
165 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
166 kTIMING_TOLERANCE_MS);
167 // Add request @ value0 higher prio than value1
168 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 200ms));
169 expire_time = t.Update(true);
170 _VerifyPathValue(tf.path, "value0");
171 EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
172 kTIMING_TOLERANCE_MS);
173 // Remove high prio request, now only request @ value1 active
174 t.RemoveRequest("LAUNCH");
175 expire_time = t.Update(true);
176 _VerifyPathValue(tf.path, "value1");
177 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
178 kTIMING_TOLERANCE_MS);
179 // Remove request, now default value2
180 t.RemoveRequest("INTERACTION");
181 expire_time = t.Update(true);
182 _VerifyPathValue(tf.path, "value2");
183 EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
184 }
185
186 // Test add request with holding fd
187 TEST(FileNodeTest, AddRequestTestHoldFdOverride) {
188 TemporaryFile tf;
189 FileNode t("t", tf.path, {{"value0"}, {"value1"}, {"value2"}}, 2, true,
190 true);
191 EXPECT_TRUE(t.GetHoldFd());
192 auto start = std::chrono::steady_clock::now();
193 EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 500ms));
194 std::chrono::milliseconds expire_time = t.Update(true);
195 // Add request @ value1
196 _VerifyPathValue(tf.path, "value1");
197 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
198 kTIMING_TOLERANCE_MS);
199 // Add request @ value0 higher prio than value1
200 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 200ms));
201 expire_time = t.Update(true);
202 _VerifyPathValue(tf.path, "value0");
203 EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
204 kTIMING_TOLERANCE_MS);
205 // Add request @ value0 shorter
206 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 100ms));
207 expire_time = t.Update(true);
208 _VerifyPathValue(tf.path, "value0");
209 EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
210 kTIMING_TOLERANCE_MS);
211 // Add request @ value0 longer
212 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 300ms));
213 expire_time = t.Update(true);
214 _VerifyPathValue(tf.path, "value0");
215 EXPECT_NEAR(std::chrono::milliseconds(300).count(), expire_time.count(),
216 kTIMING_TOLERANCE_MS);
217 // Remove high prio request, now only request @ value1 active
218 t.RemoveRequest("LAUNCH");
219 expire_time = t.Update(true);
220 _VerifyPathValue(tf.path, "value1");
221 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
222 kTIMING_TOLERANCE_MS);
223 // Remove request, now default value2
224 t.RemoveRequest("INTERACTION");
225 expire_time = t.Update(true);
226 _VerifyPathValue(tf.path, "value2");
227 EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
228 }
229
230 } // namespace perfmgr
231 } // namespace android