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