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/logging.h>
19 #include <android-base/properties.h>
20 #include <android-base/stringprintf.h>
21 #include <gtest/gtest.h>
22 
23 #include <algorithm>
24 #include <thread>
25 
26 #include "perfmgr/FileNode.h"
27 #include "perfmgr/HintManager.h"
28 #include "perfmgr/PropertyNode.h"
29 
30 namespace android {
31 namespace perfmgr {
32 
33 using std::literals::chrono_literals::operator""ms;
34 
35 constexpr auto kSLEEP_TOLERANCE_MS = 50ms;
36 
37 constexpr char kJSON_RAW[] = R"(
38 {
39     "Nodes": [
40         {
41             "Name": "CPUCluster0MinFreq",
42             "Path": "/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq",
43             "Values": [
44                 "1512000",
45                 "1134000",
46                 "384000"
47             ],
48             "DefaultIndex": 2,
49             "ResetOnInit": true
50         },
51         {
52             "Name": "CPUCluster1MinFreq",
53             "Path": "/sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq",
54             "Values": [
55                 "1512000",
56                 "1134000",
57                 "384000"
58             ],
59             "HoldFd": true
60         },
61         {
62             "Name": "ModeProperty",
63             "Path": "vendor.pwhal.mode",
64             "Values": [
65                 "HIGH",
66                 "LOW",
67                 "NONE"
68             ],
69             "Type": "Property"
70         }
71     ],
72     "Actions": [
73         {
74             "PowerHint": "INTERACTION",
75             "Node": "CPUCluster1MinFreq",
76             "Value": "1134000",
77             "Duration": 800
78         },
79         {
80             "PowerHint": "INTERACTION",
81             "Node": "ModeProperty",
82             "Value": "LOW",
83             "Duration": 800
84         },
85         {
86             "PowerHint": "LAUNCH",
87             "Node": "CPUCluster0MinFreq",
88             "Value": "1134000",
89             "Duration": 500
90         },
91         {
92             "PowerHint": "LAUNCH",
93             "Node": "ModeProperty",
94             "Value": "HIGH",
95             "Duration": 500
96         },
97         {
98             "PowerHint": "LAUNCH",
99             "Node": "CPUCluster1MinFreq",
100             "Value": "1512000",
101             "Duration": 2000
102         },
103         {
104             "PowerHint": "MASK_LAUNCH_MODE",
105             "Type": "MaskHint",
106             "Value": "LAUNCH"
107         },
108         {
109             "PowerHint": "END_LAUNCH_MODE",
110             "Type": "EndHint",
111             "Value": "LAUNCH"
112         },
113         {
114             "PowerHint": "DO_LAUNCH_MODE",
115             "Type": "DoHint",
116             "Value": "LAUNCH"
117         }
118     ]
119 }
120 )";
121 
122 class HintManagerTest : public ::testing::Test, public HintManager {
123   protected:
HintManagerTest()124     HintManagerTest() : HintManager(nullptr, std::unordered_map<std::string, Hint>{}) {
125         android::base::SetMinimumLogSeverity(android::base::VERBOSE);
126         prop_ = "vendor.pwhal.mode";
127     }
128 
SetUp()129     virtual void SetUp() {
130         // Set up 3 dummy nodes
131         std::unique_ptr<TemporaryFile> tf = std::make_unique<TemporaryFile>();
132         nodes_.emplace_back(new FileNode(
133             "n0", tf->path, {{"n0_value0"}, {"n0_value1"}, {"n0_value2"}}, 2,
134             false));
135         files_.emplace_back(std::move(tf));
136         tf = std::make_unique<TemporaryFile>();
137         nodes_.emplace_back(new FileNode(
138             "n1", tf->path, {{"n1_value0"}, {"n1_value1"}, {"n1_value2"}}, 2,
139             true));
140         files_.emplace_back(std::move(tf));
141         nodes_.emplace_back(new PropertyNode(
142             "n2", prop_, {{"n2_value0"}, {"n2_value1"}, {"n2_value2"}}, 2,
143             true));
144         nm_ = new NodeLooperThread(std::move(nodes_));
145         // Set up dummy actions
146         // "INTERACTION"
147         // Node0, value1, 800ms
148         // Node1, value1, forever
149         // Node2, value1, 800ms
150         // "LAUNCH"
151         // Node0, value0, forever
152         // Node1, value0, 400ms
153         // Node2, value0, 400ms
154         actions_["INTERACTION"].node_actions =
155                 std::vector<NodeAction>{{0, 1, 800ms}, {1, 1, 0ms}, {2, 1, 800ms}};
156         actions_["LAUNCH"].node_actions =
157                 std::vector<NodeAction>{{0, 0, 0ms}, {1, 0, 400ms}, {2, 0, 400ms}};
158 
159         // Prepare dummy files to replace the nodes' path in example json_doc
160         files_.emplace_back(std::make_unique<TemporaryFile>());
161         files_.emplace_back(std::make_unique<TemporaryFile>());
162         // replace file path
163         json_doc_ = kJSON_RAW;
164         std::string from =
165             "/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq";
166         size_t start_pos = json_doc_.find(from);
167         json_doc_.replace(start_pos, from.length(), files_[0 + 2]->path);
168         from = "/sys/devices/system/cpu/cpu4/cpufreq/scaling_min_freq";
169         start_pos = json_doc_.find(from);
170         json_doc_.replace(start_pos, from.length(), files_[1 + 2]->path);
171         EXPECT_TRUE(android::base::SetProperty(prop_, ""))
172             << "failed to clear property";
173     }
174 
TearDown()175     virtual void TearDown() {
176         actions_.clear();
177         nodes_.clear();
178         files_.clear();
179         nm_ = nullptr;
180     }
181     sp<NodeLooperThread> nm_;
182     std::unordered_map<std::string, Hint> actions_;
183     std::vector<std::unique_ptr<Node>> nodes_;
184     std::vector<std::unique_ptr<TemporaryFile>> files_;
185     std::string json_doc_;
186     std::string prop_;
187 };
188 
_VerifyPropertyValue(const std::string & path,const std::string & value)189 static inline void _VerifyPropertyValue(const std::string& path,
190                                         const std::string& value) {
191     std::string s = android::base::GetProperty(path, "");
192     EXPECT_EQ(value, s);
193 }
194 
_VerifyPathValue(const std::string & path,const std::string & value)195 static inline void _VerifyPathValue(const std::string& path,
196                                     const std::string& value) {
197     std::string s;
198     EXPECT_TRUE(android::base::ReadFileToString(path, &s)) << strerror(errno);
199     EXPECT_EQ(value, s);
200 }
201 
_VerifyStats(const HintStats & stats,uint32_t count,uint64_t duration_min,uint64_t duration_max)202 static inline void _VerifyStats(const HintStats &stats, uint32_t count, uint64_t duration_min,
203                                 uint64_t duration_max) {
204     EXPECT_EQ(stats.count, count);
205     EXPECT_GE(stats.duration_ms, duration_min);
206     EXPECT_LT(stats.duration_ms, duration_max);
207 }
208 
209 // Test GetHints
TEST_F(HintManagerTest,GetHintsTest)210 TEST_F(HintManagerTest, GetHintsTest) {
211     HintManager hm(nm_, actions_);
212     EXPECT_TRUE(hm.Start());
213     std::vector<std::string> hints = hm.GetHints();
214     EXPECT_TRUE(hm.IsRunning());
215     EXPECT_EQ(2u, hints.size());
216     EXPECT_NE(std::find(hints.begin(), hints.end(), "INTERACTION"), hints.end());
217     EXPECT_NE(std::find(hints.begin(), hints.end(), "LAUNCH"), hints.end());
218 }
219 
220 // Test GetHintStats
TEST_F(HintManagerTest,GetHintStatsTest)221 TEST_F(HintManagerTest, GetHintStatsTest) {
222     auto hm = std::make_unique<HintManager>(nm_, actions_);
223     EXPECT_TRUE(InitHintStatus(hm));
224     EXPECT_TRUE(hm->Start());
225     HintStats launch_stats(hm->GetHintStats("LAUNCH"));
226     EXPECT_EQ(0, launch_stats.count);
227     EXPECT_EQ(0, launch_stats.duration_ms);
228     HintStats interaction_stats(hm->GetHintStats("INTERACTION"));
229     EXPECT_EQ(0, interaction_stats.count);
230     EXPECT_EQ(0, interaction_stats.duration_ms);
231 }
232 
233 // Test initialization of default values
TEST_F(HintManagerTest,HintInitDefaultTest)234 TEST_F(HintManagerTest, HintInitDefaultTest) {
235     HintManager hm(nm_, actions_);
236     EXPECT_TRUE(hm.Start());
237     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
238     EXPECT_TRUE(hm.IsRunning());
239     _VerifyPathValue(files_[0]->path, "");
240     _VerifyPathValue(files_[1]->path, "n1_value2");
241     _VerifyPropertyValue(prop_, "n2_value2");
242 }
243 
244 // Test IsHintSupported
TEST_F(HintManagerTest,HintSupportedTest)245 TEST_F(HintManagerTest, HintSupportedTest) {
246     HintManager hm(nm_, actions_);
247     EXPECT_TRUE(hm.IsHintSupported("INTERACTION"));
248     EXPECT_TRUE(hm.IsHintSupported("LAUNCH"));
249     EXPECT_FALSE(hm.IsHintSupported("NO_SUCH_HINT"));
250 }
251 
252 // Test DumpToFd
TEST_F(HintManagerTest,DumpToFdTest)253 TEST_F(HintManagerTest, DumpToFdTest) {
254     auto hm = std::make_unique<HintManager>(nm_, actions_);
255     EXPECT_TRUE(InitHintStatus(hm));
256     TemporaryFile dumptf;
257     hm->DumpToFd(dumptf.fd);
258     fsync(dumptf.fd);
259     std::ostringstream dump_buf;
260     dump_buf << "========== Begin perfmgr nodes ==========\nNode Name\tNode "
261                 "Path\tCurrent Index\tCurrent Value\nn0\t"
262              << files_[0]->path << "\t2\t\nn1\t" << files_[1]->path
263              << "\t2\t\nn2\tvendor.pwhal.mode\t2\t\n==========  End perfmgr "
264                 "nodes  ==========\n========== Begin perfmgr stats ==========\n"
265                 "Hint Name\tCounts\tDuration\nINTERACTION\t0\t0\nLAUNCH\t0\t0\n"
266                 "==========  End perfmgr stats  ==========\n";
267     _VerifyPathValue(dumptf.path, dump_buf.str());
268     TemporaryFile dumptf_started;
269     EXPECT_TRUE(hm->Start());
270     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
271     EXPECT_TRUE(hm->IsRunning());
272     hm->DumpToFd(dumptf_started.fd);
273     fsync(dumptf_started.fd);
274     dump_buf.str("");
275     dump_buf.clear();
276     dump_buf << "========== Begin perfmgr nodes ==========\nNode Name\tNode "
277                 "Path\tCurrent Index\tCurrent Value\nn0\t"
278              << files_[0]->path << "\t2\t\nn1\t" << files_[1]->path
279              << "\t2\tn1_value2\nn2\tvendor.pwhal.mode\t2\tn2_value2\n========="
280                 "=  End perfmgr nodes  ==========\n========== Begin perfmgr "
281                 "stats ==========\nHint Name\tCounts\tDuration\nINTERACTION\t0\t"
282                 "0\nLAUNCH\t0\t0\n==========  End perfmgr stats  ==========\n";
283     _VerifyPathValue(dumptf_started.path, dump_buf.str());
284 }
285 
286 // Test hint/cancel/expire with dummy actions
TEST_F(HintManagerTest,HintTest)287 TEST_F(HintManagerTest, HintTest) {
288     auto hm = std::make_unique<HintManager>(nm_, actions_);
289     EXPECT_TRUE(InitHintStatus(hm));
290     EXPECT_TRUE(hm->Start());
291     EXPECT_TRUE(hm->IsRunning());
292     EXPECT_TRUE(hm->DoHint("INTERACTION"));
293     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
294     _VerifyPathValue(files_[0]->path, "n0_value1");
295     _VerifyPathValue(files_[1]->path, "n1_value1");
296     _VerifyPropertyValue(prop_, "n2_value1");
297     // this won't change the expire time of INTERACTION hint
298     EXPECT_TRUE(hm->DoHint("INTERACTION", 200ms));
299     // now place new hint
300     EXPECT_TRUE(hm->DoHint("LAUNCH"));
301     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
302     _VerifyPathValue(files_[0]->path, "n0_value0");
303     _VerifyPathValue(files_[1]->path, "n1_value0");
304     _VerifyPropertyValue(prop_, "n2_value0");
305     EXPECT_TRUE(hm->DoHint("LAUNCH", 500ms));
306     // "LAUNCH" node1 not expired
307     std::this_thread::sleep_for(400ms);
308     _VerifyPathValue(files_[0]->path, "n0_value0");
309     _VerifyPathValue(files_[1]->path, "n1_value0");
310     _VerifyPropertyValue(prop_, "n2_value0");
311     // "LAUNCH" node1 expired
312     std::this_thread::sleep_for(100ms + kSLEEP_TOLERANCE_MS);
313     _VerifyPathValue(files_[0]->path, "n0_value0");
314     _VerifyPathValue(files_[1]->path, "n1_value1");
315     _VerifyPropertyValue(prop_, "n2_value1");
316     EXPECT_TRUE(hm->EndHint("LAUNCH"));
317     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
318     // "LAUNCH" canceled
319     _VerifyPathValue(files_[0]->path, "n0_value1");
320     _VerifyPathValue(files_[1]->path, "n1_value1");
321     _VerifyPropertyValue(prop_, "n2_value1");
322     std::this_thread::sleep_for(200ms);
323     // "INTERACTION" node0 expired
324     _VerifyPathValue(files_[0]->path, "n0_value2");
325     _VerifyPathValue(files_[1]->path, "n1_value1");
326     _VerifyPropertyValue(prop_, "n2_value2");
327     EXPECT_TRUE(hm->EndHint("INTERACTION"));
328     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
329     // "INTERACTION" canceled
330     _VerifyPathValue(files_[0]->path, "n0_value2");
331     _VerifyPathValue(files_[1]->path, "n1_value2");
332     _VerifyPropertyValue(prop_, "n2_value2");
333 }
334 
335 // Test collecting stats with simple actions
TEST_F(HintManagerTest,HintStatsTest)336 TEST_F(HintManagerTest, HintStatsTest) {
337     auto hm = std::make_unique<HintManager>(nm_, actions_);
338     EXPECT_TRUE(InitHintStatus(hm));
339     EXPECT_TRUE(hm->Start());
340     EXPECT_TRUE(hm->IsRunning());
341     EXPECT_TRUE(hm->DoHint("INTERACTION"));
342     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
343     _VerifyPathValue(files_[0]->path, "n0_value1");
344     _VerifyPathValue(files_[1]->path, "n1_value1");
345     _VerifyPropertyValue(prop_, "n2_value1");
346     // now place "LAUNCH" hint with timeout of 500ms
347     EXPECT_TRUE(hm->DoHint("LAUNCH", 500ms));
348     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
349     _VerifyPathValue(files_[0]->path, "n0_value0");
350     _VerifyPathValue(files_[1]->path, "n1_value0");
351     _VerifyPropertyValue(prop_, "n2_value0");
352     // "LAUNCH" expired
353     std::this_thread::sleep_for(500ms + kSLEEP_TOLERANCE_MS);
354     _VerifyPathValue(files_[0]->path, "n0_value1");
355     _VerifyPathValue(files_[1]->path, "n1_value1");
356     _VerifyPropertyValue(prop_, "n2_value1");
357     HintStats launch_stats(hm->GetHintStats("LAUNCH"));
358     // Since duration is recorded at the next DoHint,
359     // duration should be 0.
360     _VerifyStats(launch_stats, 1, 0, 100);
361     std::this_thread::sleep_for(100ms + kSLEEP_TOLERANCE_MS);
362     EXPECT_TRUE(hm->EndHint("INTERACTION"));
363     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
364     // "INTERACTION" canceled
365     _VerifyPathValue(files_[0]->path, "n0_value2");
366     _VerifyPathValue(files_[1]->path, "n1_value2");
367     _VerifyPropertyValue(prop_, "n2_value2");
368     HintStats interaction_stats(hm->GetHintStats("INTERACTION"));
369     _VerifyStats(interaction_stats, 1, 800, 900);
370     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
371     // Second LAUNCH hint sent to get the first duration recorded.
372     EXPECT_TRUE(hm->DoHint("LAUNCH"));
373     launch_stats = hm->GetHintStats("LAUNCH");
374     _VerifyStats(launch_stats, 2, 500, 600);
375 }
376 
377 // Test parsing nodes
TEST_F(HintManagerTest,ParseNodesTest)378 TEST_F(HintManagerTest, ParseNodesTest) {
379     std::vector<std::unique_ptr<Node>> nodes =
380         HintManager::ParseNodes(json_doc_);
381     EXPECT_EQ(3u, nodes.size());
382     EXPECT_EQ("CPUCluster0MinFreq", nodes[0]->GetName());
383     EXPECT_EQ("CPUCluster1MinFreq", nodes[1]->GetName());
384     EXPECT_EQ(files_[0 + 2]->path, nodes[0]->GetPath());
385     EXPECT_EQ(files_[1 + 2]->path, nodes[1]->GetPath());
386     EXPECT_EQ("1512000", nodes[0]->GetValues()[0]);
387     EXPECT_EQ("1134000", nodes[0]->GetValues()[1]);
388     EXPECT_EQ("384000", nodes[0]->GetValues()[2]);
389     EXPECT_EQ("1512000", nodes[1]->GetValues()[0]);
390     EXPECT_EQ("1134000", nodes[1]->GetValues()[1]);
391     EXPECT_EQ("384000", nodes[1]->GetValues()[2]);
392     EXPECT_EQ(2u, nodes[0]->GetDefaultIndex());
393     EXPECT_EQ(2u, nodes[1]->GetDefaultIndex());
394     EXPECT_TRUE(nodes[0]->GetResetOnInit());
395     EXPECT_FALSE(nodes[1]->GetResetOnInit());
396     // no dynamic_cast intentionally in Android
397     EXPECT_FALSE(reinterpret_cast<FileNode*>(nodes[0].get())->GetHoldFd());
398     EXPECT_TRUE(reinterpret_cast<FileNode*>(nodes[1].get())->GetHoldFd());
399     EXPECT_EQ("ModeProperty", nodes[2]->GetName());
400     EXPECT_EQ(prop_, nodes[2]->GetPath());
401     EXPECT_EQ("HIGH", nodes[2]->GetValues()[0]);
402     EXPECT_EQ("LOW", nodes[2]->GetValues()[1]);
403     EXPECT_EQ("NONE", nodes[2]->GetValues()[2]);
404     EXPECT_EQ(2u, nodes[2]->GetDefaultIndex());
405     EXPECT_FALSE(nodes[2]->GetResetOnInit());
406 }
407 
408 // Test parsing nodes with duplicate name
TEST_F(HintManagerTest,ParseNodesDuplicateNameTest)409 TEST_F(HintManagerTest, ParseNodesDuplicateNameTest) {
410     std::string from = "CPUCluster0MinFreq";
411     size_t start_pos = json_doc_.find(from);
412     json_doc_.replace(start_pos, from.length(), "CPUCluster1MinFreq");
413     std::vector<std::unique_ptr<Node>> nodes =
414         HintManager::ParseNodes(json_doc_);
415     EXPECT_EQ(0u, nodes.size());
416 }
417 
TEST_F(HintManagerTest,ParsePropertyNodesDuplicatNameTest)418 TEST_F(HintManagerTest, ParsePropertyNodesDuplicatNameTest) {
419     std::string from = "ModeProperty";
420     size_t start_pos = json_doc_.find(from);
421     json_doc_.replace(start_pos, from.length(), "CPUCluster1MinFreq");
422     std::vector<std::unique_ptr<Node>> nodes =
423         HintManager::ParseNodes(json_doc_);
424     EXPECT_EQ(0u, nodes.size());
425 }
426 
427 // Test parsing nodes with duplicate path
TEST_F(HintManagerTest,ParseNodesDuplicatePathTest)428 TEST_F(HintManagerTest, ParseNodesDuplicatePathTest) {
429     std::string from = files_[0 + 2]->path;
430     size_t start_pos = json_doc_.find(from);
431     json_doc_.replace(start_pos, from.length(), files_[1 + 2]->path);
432     std::vector<std::unique_ptr<Node>> nodes =
433         HintManager::ParseNodes(json_doc_);
434     EXPECT_EQ(0u, nodes.size());
435 }
436 
437 // Test parsing file node with duplicate value
TEST_F(HintManagerTest,ParseFileNodesDuplicateValueTest)438 TEST_F(HintManagerTest, ParseFileNodesDuplicateValueTest) {
439     std::string from = "1512000";
440     size_t start_pos = json_doc_.find(from);
441     json_doc_.replace(start_pos, from.length(), "1134000");
442     std::vector<std::unique_ptr<Node>> nodes =
443         HintManager::ParseNodes(json_doc_);
444     EXPECT_EQ(0u, nodes.size());
445 }
446 
447 // Test parsing property node with duplicate value
TEST_F(HintManagerTest,ParsePropertyNodesDuplicateValueTest)448 TEST_F(HintManagerTest, ParsePropertyNodesDuplicateValueTest) {
449     std::string from = "HIGH";
450     size_t start_pos = json_doc_.find(from);
451     json_doc_.replace(start_pos, from.length(), "LOW");
452     std::vector<std::unique_ptr<Node>> nodes =
453         HintManager::ParseNodes(json_doc_);
454     EXPECT_EQ(0u, nodes.size());
455 }
456 
457 // Test parsing file node with empty value
TEST_F(HintManagerTest,ParseFileNodesEmptyValueTest)458 TEST_F(HintManagerTest, ParseFileNodesEmptyValueTest) {
459     std::string from = "384000";
460     size_t start_pos = json_doc_.find(from);
461     json_doc_.replace(start_pos, from.length(), "");
462     std::vector<std::unique_ptr<Node>> nodes =
463         HintManager::ParseNodes(json_doc_);
464     EXPECT_EQ(0u, nodes.size());
465 }
466 
467 // Test parsing property node with empty value
TEST_F(HintManagerTest,ParsePropertyNodesEmptyValueTest)468 TEST_F(HintManagerTest, ParsePropertyNodesEmptyValueTest) {
469     std::string from = "LOW";
470     size_t start_pos = json_doc_.find(from);
471     json_doc_.replace(start_pos, from.length(), "");
472     std::vector<std::unique_ptr<Node>> nodes =
473         HintManager::ParseNodes(json_doc_);
474     EXPECT_EQ(3u, nodes.size());
475     EXPECT_EQ("CPUCluster0MinFreq", nodes[0]->GetName());
476     EXPECT_EQ("CPUCluster1MinFreq", nodes[1]->GetName());
477     EXPECT_EQ(files_[0 + 2]->path, nodes[0]->GetPath());
478     EXPECT_EQ(files_[1 + 2]->path, nodes[1]->GetPath());
479     EXPECT_EQ("1512000", nodes[0]->GetValues()[0]);
480     EXPECT_EQ("1134000", nodes[0]->GetValues()[1]);
481     EXPECT_EQ("384000", nodes[0]->GetValues()[2]);
482     EXPECT_EQ("1512000", nodes[1]->GetValues()[0]);
483     EXPECT_EQ("1134000", nodes[1]->GetValues()[1]);
484     EXPECT_EQ("384000", nodes[1]->GetValues()[2]);
485     EXPECT_EQ(2u, nodes[0]->GetDefaultIndex());
486     EXPECT_EQ(2u, nodes[1]->GetDefaultIndex());
487     EXPECT_TRUE(nodes[0]->GetResetOnInit());
488     EXPECT_FALSE(nodes[1]->GetResetOnInit());
489     // no dynamic_cast intentionally in Android
490     EXPECT_FALSE(reinterpret_cast<FileNode*>(nodes[0].get())->GetHoldFd());
491     EXPECT_TRUE(reinterpret_cast<FileNode*>(nodes[1].get())->GetHoldFd());
492     EXPECT_EQ("ModeProperty", nodes[2]->GetName());
493     EXPECT_EQ(prop_, nodes[2]->GetPath());
494     EXPECT_EQ("HIGH", nodes[2]->GetValues()[0]);
495     EXPECT_EQ("", nodes[2]->GetValues()[1]);
496     EXPECT_EQ("NONE", nodes[2]->GetValues()[2]);
497     EXPECT_EQ(2u, nodes[2]->GetDefaultIndex());
498     EXPECT_FALSE(nodes[2]->GetResetOnInit());
499 }
500 
501 // Test parsing invalid json for nodes
TEST_F(HintManagerTest,ParseBadFileNodesTest)502 TEST_F(HintManagerTest, ParseBadFileNodesTest) {
503     std::vector<std::unique_ptr<Node>> nodes =
504         HintManager::ParseNodes("invalid json");
505     EXPECT_EQ(0u, nodes.size());
506     nodes = HintManager::ParseNodes(
507         "{\"devices\":{\"15\":[\"armeabi-v7a\"],\"16\":[\"armeabi-v7a\"],"
508         "\"26\":[\"armeabi-v7a\",\"arm64-v8a\",\"x86\",\"x86_64\"]}}");
509     EXPECT_EQ(0u, nodes.size());
510 }
511 
512 // Test parsing actions
TEST_F(HintManagerTest,ParseActionsTest)513 TEST_F(HintManagerTest, ParseActionsTest) {
514     std::vector<std::unique_ptr<Node>> nodes =
515         HintManager::ParseNodes(json_doc_);
516     std::unordered_map<std::string, Hint> actions = HintManager::ParseActions(json_doc_, nodes);
517     EXPECT_EQ(5u, actions.size());
518 
519     EXPECT_EQ(2u, actions["INTERACTION"].node_actions.size());
520     EXPECT_EQ(1u, actions["INTERACTION"].node_actions[0].node_index);
521     EXPECT_EQ(1u, actions["INTERACTION"].node_actions[0].value_index);
522     EXPECT_EQ(std::chrono::milliseconds(800).count(),
523               actions["INTERACTION"].node_actions[0].timeout_ms.count());
524 
525     EXPECT_EQ(2u, actions["INTERACTION"].node_actions[1].node_index);
526     EXPECT_EQ(1u, actions["INTERACTION"].node_actions[1].value_index);
527     EXPECT_EQ(std::chrono::milliseconds(800).count(),
528               actions["INTERACTION"].node_actions[1].timeout_ms.count());
529 
530     EXPECT_EQ(3u, actions["LAUNCH"].node_actions.size());
531 
532     EXPECT_EQ(0u, actions["LAUNCH"].node_actions[0].node_index);
533     EXPECT_EQ(1u, actions["LAUNCH"].node_actions[0].value_index);
534     EXPECT_EQ(std::chrono::milliseconds(500).count(),
535               actions["LAUNCH"].node_actions[0].timeout_ms.count());
536 
537     EXPECT_EQ(2u, actions["LAUNCH"].node_actions[1].node_index);
538     EXPECT_EQ(0u, actions["LAUNCH"].node_actions[1].value_index);
539     EXPECT_EQ(std::chrono::milliseconds(500).count(),
540               actions["LAUNCH"].node_actions[1].timeout_ms.count());
541 
542     EXPECT_EQ(1u, actions["LAUNCH"].node_actions[2].node_index);
543     EXPECT_EQ(0u, actions["LAUNCH"].node_actions[2].value_index);
544     EXPECT_EQ(std::chrono::milliseconds(2000).count(),
545               actions["LAUNCH"].node_actions[2].timeout_ms.count());
546 
547     EXPECT_EQ(1u, actions["MASK_LAUNCH_MODE"].hint_actions.size());
548     EXPECT_EQ(HintActionType::MaskHint, actions["MASK_LAUNCH_MODE"].hint_actions[0].type);
549     EXPECT_TRUE("LAUNCH" == actions["MASK_LAUNCH_MODE"].hint_actions[0].value);
550 
551     EXPECT_EQ(1u, actions["DO_LAUNCH_MODE"].hint_actions.size());
552     EXPECT_EQ(HintActionType::DoHint, actions["DO_LAUNCH_MODE"].hint_actions[0].type);
553     EXPECT_TRUE("LAUNCH" == actions["DO_LAUNCH_MODE"].hint_actions[0].value);
554 
555     EXPECT_EQ(1u, actions["END_LAUNCH_MODE"].hint_actions.size());
556     EXPECT_EQ(HintActionType::EndHint, actions["END_LAUNCH_MODE"].hint_actions[0].type);
557     EXPECT_TRUE("LAUNCH" == actions["END_LAUNCH_MODE"].hint_actions[0].value);
558 }
559 
560 // Test parsing actions with duplicate File node
TEST_F(HintManagerTest,ParseActionDuplicateFileNodeTest)561 TEST_F(HintManagerTest, ParseActionDuplicateFileNodeTest) {
562     std::string from = R"("Node": "CPUCluster0MinFreq")";
563     size_t start_pos = json_doc_.find(from);
564     json_doc_.replace(start_pos, from.length(), R"("Node": "CPUCluster1MinFreq")");
565     std::vector<std::unique_ptr<Node>> nodes =
566         HintManager::ParseNodes(json_doc_);
567     EXPECT_EQ(3u, nodes.size());
568     auto actions = HintManager::ParseActions(json_doc_, nodes);
569     EXPECT_EQ(0u, actions.size());
570 }
571 
572 // Test parsing actions with duplicate Property node
TEST_F(HintManagerTest,ParseActionDuplicatePropertyNodeTest)573 TEST_F(HintManagerTest, ParseActionDuplicatePropertyNodeTest) {
574     std::string from = R"("Node": "CPUCluster0MinFreq")";
575     size_t start_pos = json_doc_.find(from);
576     json_doc_.replace(start_pos, from.length(), R"("Node": "ModeProperty")");
577     auto nodes = HintManager::ParseNodes(json_doc_);
578     EXPECT_EQ(3u, nodes.size());
579     auto actions = HintManager::ParseActions(json_doc_, nodes);
580     EXPECT_EQ(0u, actions.size());
581 }
582 
583 // Test parsing invalid json for actions
TEST_F(HintManagerTest,ParseBadActionsTest)584 TEST_F(HintManagerTest, ParseBadActionsTest) {
585     std::vector<std::unique_ptr<Node>> nodes =
586         HintManager::ParseNodes(json_doc_);
587     auto actions = HintManager::ParseActions("invalid json", nodes);
588     EXPECT_EQ(0u, actions.size());
589     actions = HintManager::ParseActions(
590         "{\"devices\":{\"15\":[\"armeabi-v7a\"],\"16\":[\"armeabi-v7a\"],"
591         "\"26\":[\"armeabi-v7a\",\"arm64-v8a\",\"x86\",\"x86_64\"]}}",
592         nodes);
593     EXPECT_EQ(0u, actions.size());
594 }
595 
596 // Test hint/cancel/expire with json config
TEST_F(HintManagerTest,GetFromJSONTest)597 TEST_F(HintManagerTest, GetFromJSONTest) {
598     TemporaryFile json_file;
599     ASSERT_TRUE(android::base::WriteStringToFile(json_doc_, json_file.path))
600         << strerror(errno);
601     std::unique_ptr<HintManager> hm =
602         HintManager::GetFromJSON(json_file.path, false);
603     EXPECT_NE(nullptr, hm.get());
604     EXPECT_FALSE(hm->IsRunning());
605     EXPECT_TRUE(hm->Start());
606     EXPECT_TRUE(hm->IsRunning());
607     hm = HintManager::GetFromJSON(json_file.path);
608     EXPECT_NE(nullptr, hm.get());
609     EXPECT_TRUE(hm->IsRunning());
610     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
611     EXPECT_TRUE(hm->IsRunning());
612     // Initial default value on Node0
613     _VerifyPathValue(files_[0 + 2]->path, "384000");
614     _VerifyPathValue(files_[1 + 2]->path, "");
615     _VerifyPropertyValue(prop_, "");
616     // Do INTERACTION
617     EXPECT_TRUE(hm->DoHint("INTERACTION"));
618     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
619     _VerifyPathValue(files_[0 + 2]->path, "384000");
620     _VerifyPathValue(files_[1 + 2]->path, "1134000");
621     _VerifyPropertyValue(prop_, "LOW");
622     // Do LAUNCH
623     EXPECT_TRUE(hm->DoHint("LAUNCH"));
624     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
625     _VerifyPathValue(files_[0 + 2]->path, "1134000");
626     _VerifyPathValue(files_[1 + 2]->path, "1512000");
627     _VerifyPropertyValue(prop_, "HIGH");
628     std::this_thread::sleep_for(500ms);
629     // "LAUNCH" node0 expired
630     _VerifyPathValue(files_[0 + 2]->path, "384000");
631     _VerifyPathValue(files_[1 + 2]->path, "1512000");
632     _VerifyPropertyValue(prop_, "LOW");
633     EXPECT_TRUE(hm->EndHint("LAUNCH"));
634     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
635     // "LAUNCH" canceled
636     _VerifyPathValue(files_[0 + 2]->path, "384000");
637     _VerifyPathValue(files_[1 + 2]->path, "1134000");
638     _VerifyPropertyValue(prop_, "LOW");
639     std::this_thread::sleep_for(300ms);
640     // "INTERACTION" node1 expired
641     _VerifyPathValue(files_[0 + 2]->path, "384000");
642     _VerifyPathValue(files_[1 + 2]->path, "384000");
643     _VerifyPropertyValue(prop_, "NONE");
644 
645     // Mask LAUNCH and do LAUNCH
646     EXPECT_TRUE(hm->DoHint("MASK_LAUNCH_MODE"));
647     EXPECT_FALSE(hm->DoHint("LAUNCH"));  // should fail
648     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
649     _VerifyPathValue(files_[0 + 2]->path, "384000");
650     _VerifyPathValue(files_[1 + 2]->path, "384000");
651     _VerifyPropertyValue(prop_, "NONE");
652 
653     // UnMask LAUNCH and do LAUNCH
654     EXPECT_TRUE(hm->EndHint("MASK_LAUNCH_MODE"));
655     EXPECT_TRUE(hm->DoHint("LAUNCH"));
656     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
657     _VerifyPathValue(files_[0 + 2]->path, "1134000");
658     _VerifyPathValue(files_[1 + 2]->path, "1512000");
659     _VerifyPropertyValue(prop_, "HIGH");
660     // END_LAUNCH_MODE should deactivate LAUNCH
661     EXPECT_TRUE(hm->DoHint("END_LAUNCH_MODE"));
662     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
663     _VerifyPathValue(files_[0 + 2]->path, "384000");
664     _VerifyPathValue(files_[1 + 2]->path, "384000");
665     _VerifyPropertyValue(prop_, "NONE");
666     EXPECT_TRUE(hm->EndHint("END_LAUNCH_MODE"));
667 
668     // DO_LAUNCH_MODE should activate LAUNCH
669     EXPECT_TRUE(hm->DoHint("DO_LAUNCH_MODE"));
670     std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS);
671     _VerifyPathValue(files_[0 + 2]->path, "1134000");
672     _VerifyPathValue(files_[1 + 2]->path, "1512000");
673     _VerifyPropertyValue(prop_, "HIGH");
674 }
675 
676 }  // namespace perfmgr
677 }  // namespace android
678