diff options
-rw-r--r-- | libperfmgr/HintManager.cc | 10 | ||||
-rw-r--r-- | libperfmgr/NodeLooperThread.cc | 5 | ||||
-rw-r--r-- | libperfmgr/include/perfmgr/HintManager.h | 5 | ||||
-rw-r--r-- | libperfmgr/include/perfmgr/NodeLooperThread.h | 4 | ||||
-rw-r--r-- | libperfmgr/tests/HintManagerTest.cc | 11 | ||||
-rw-r--r-- | libperfmgr/tests/NodeLooperThreadTest.cc | 5 |
6 files changed, 34 insertions, 6 deletions
diff --git a/libperfmgr/HintManager.cc b/libperfmgr/HintManager.cc index dd103533..13060b01 100644 --- a/libperfmgr/HintManager.cc +++ b/libperfmgr/HintManager.cc @@ -106,8 +106,12 @@ void HintManager::DumpToFd(int fd) { fsync(fd); } +bool HintManager::Start() { + return nm_->Start(); +} + std::unique_ptr<HintManager> HintManager::GetFromJSON( - const std::string& config_path) { + const std::string& config_path, bool start) { std::string json_doc; if (!android::base::ReadFileToString(config_path, &json_doc)) { @@ -133,6 +137,10 @@ std::unique_ptr<HintManager> HintManager::GetFromJSON( std::make_unique<HintManager>(std::move(nm), actions); LOG(INFO) << "Initialized HintManager from JSON config: " << config_path; + + if (start) { + hm->Start(); + } return hm; } diff --git a/libperfmgr/NodeLooperThread.cc b/libperfmgr/NodeLooperThread.cc index eb5c9b43..9bb10c0b 100644 --- a/libperfmgr/NodeLooperThread.cc +++ b/libperfmgr/NodeLooperThread.cc @@ -120,13 +120,14 @@ bool NodeLooperThread::threadLoop() { return true; } -void NodeLooperThread::onFirstRef() { +bool NodeLooperThread::Start() { auto ret = this->run("NodeLooperThread", PRIORITY_HIGHEST); if (ret != NO_ERROR) { - LOG(ERROR) << "NodeLooperThread start fail"; + LOG(ERROR) << "NodeLooperThread start failed: " << ret; } else { LOG(INFO) << "NodeLooperThread started"; } + return ret == NO_ERROR; } void NodeLooperThread::Stop() { diff --git a/libperfmgr/include/perfmgr/HintManager.h b/libperfmgr/include/perfmgr/HintManager.h index 487e5bd9..bfb96fde 100644 --- a/libperfmgr/include/perfmgr/HintManager.h +++ b/libperfmgr/include/perfmgr/HintManager.h @@ -65,7 +65,7 @@ class HintManager { // Static method to construct HintManager from the JSON config file. static std::unique_ptr<HintManager> GetFromJSON( - const std::string& config_path); + const std::string& config_path, bool start = true); // Return available hints managed by HintManager std::vector<std::string> GetHints() const; @@ -73,6 +73,9 @@ class HintManager { // Dump internal status to fd void DumpToFd(int fd); + // Start thread loop + bool Start(); + protected: static std::vector<std::unique_ptr<Node>> ParseNodes( const std::string& json_doc); diff --git a/libperfmgr/include/perfmgr/NodeLooperThread.h b/libperfmgr/include/perfmgr/NodeLooperThread.h index b04d2a40..981e64f2 100644 --- a/libperfmgr/include/perfmgr/NodeLooperThread.h +++ b/libperfmgr/include/perfmgr/NodeLooperThread.h @@ -76,11 +76,13 @@ class NodeLooperThread : public ::android::Thread { // Dump all nodes to fd void DumpToFd(int fd); + // Return true when successfully started the looper thread + bool Start(); + private: NodeLooperThread(NodeLooperThread const&) = delete; void operator=(NodeLooperThread const&) = delete; bool threadLoop() override; - void onFirstRef() override; static constexpr auto kMaxUpdatePeriod = std::chrono::milliseconds::max(); diff --git a/libperfmgr/tests/HintManagerTest.cc b/libperfmgr/tests/HintManagerTest.cc index 4aa6469c..3323a27b 100644 --- a/libperfmgr/tests/HintManagerTest.cc +++ b/libperfmgr/tests/HintManagerTest.cc @@ -204,6 +204,7 @@ static inline void _VerifyPathValue(const std::string& path, // Test GetHints TEST_F(HintManagerTest, GetHintsTest) { HintManager hm(nm_, actions_); + EXPECT_TRUE(hm.Start()); std::vector<std::string> hints = hm.GetHints(); EXPECT_TRUE(hm.IsRunning()); EXPECT_EQ(2u, hints.size()); @@ -214,6 +215,7 @@ TEST_F(HintManagerTest, GetHintsTest) { // Test initialization of default values TEST_F(HintManagerTest, HintInitDefaultTest) { HintManager hm(nm_, actions_); + EXPECT_TRUE(hm.Start()); std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS); EXPECT_TRUE(hm.IsRunning()); _VerifyPathValue(files_[0]->path, ""); @@ -232,6 +234,7 @@ TEST_F(HintManagerTest, HintSupportedTest) { // Test hint/cancel/expire with dummy actions TEST_F(HintManagerTest, HintTest) { HintManager hm(nm_, actions_); + EXPECT_TRUE(hm.Start()); EXPECT_TRUE(hm.IsRunning()); EXPECT_TRUE(hm.DoHint("INTERACTION")); std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS); @@ -494,8 +497,14 @@ TEST_F(HintManagerTest, GetFromJSONTest) { TemporaryFile json_file; ASSERT_TRUE(android::base::WriteStringToFile(json_doc_, json_file.path)) << strerror(errno); - std::unique_ptr<HintManager> hm = HintManager::GetFromJSON(json_file.path); + std::unique_ptr<HintManager> hm = HintManager::GetFromJSON(json_file.path, false); EXPECT_NE(nullptr, hm.get()); + EXPECT_FALSE(hm->IsRunning()); + EXPECT_TRUE(hm->Start()); + EXPECT_TRUE(hm->IsRunning()); + hm = HintManager::GetFromJSON(json_file.path); + EXPECT_NE(nullptr, hm.get()); + EXPECT_TRUE(hm->IsRunning()); std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS); EXPECT_TRUE(hm->IsRunning()); // Initial default value on Node0 diff --git a/libperfmgr/tests/NodeLooperThreadTest.cc b/libperfmgr/tests/NodeLooperThreadTest.cc index fd634d89..5683d117 100644 --- a/libperfmgr/tests/NodeLooperThreadTest.cc +++ b/libperfmgr/tests/NodeLooperThreadTest.cc @@ -64,6 +64,7 @@ static inline void _VerifyPathValue(const std::string& path, // Test default value init TEST_F(NodeLooperThreadTest, InitRunTest) { sp<NodeLooperThread> th = new NodeLooperThread(std::move(nodes_)); + EXPECT_TRUE(th->Start()); std::this_thread::sleep_for(kSLEEP_TOLERANCE_MS); EXPECT_TRUE(th->isRunning()); _VerifyPathValue(files_[0]->path, ""); @@ -75,6 +76,7 @@ TEST_F(NodeLooperThreadTest, InitRunTest) { // Test add request TEST_F(NodeLooperThreadTest, AddRequest) { sp<NodeLooperThread> th = new NodeLooperThread(std::move(nodes_)); + EXPECT_TRUE(th->Start()); EXPECT_TRUE(th->isRunning()); // Dummy LAUNCH boost actions: // Node0, value0, 200ms @@ -97,6 +99,7 @@ TEST_F(NodeLooperThreadTest, AddRequest) { // Test request to override expire time TEST_F(NodeLooperThreadTest, AddRequestOverride) { sp<NodeLooperThread> th = new NodeLooperThread(std::move(nodes_)); + EXPECT_TRUE(th->Start()); EXPECT_TRUE(th->isRunning()); // Dummy LAUNCH boost actions: // Node0, value0, 200ms @@ -128,6 +131,7 @@ TEST_F(NodeLooperThreadTest, AddRequestOverride) { // Test cancel request TEST_F(NodeLooperThreadTest, CancelRequest) { sp<NodeLooperThread> th = new NodeLooperThread(std::move(nodes_)); + EXPECT_TRUE(th->Start()); EXPECT_TRUE(th->isRunning()); // Dummy LAUNCH boost actions: // Node0, value0, forever @@ -148,6 +152,7 @@ TEST_F(NodeLooperThreadTest, CancelRequest) { // Test multiple request TEST_F(NodeLooperThreadTest, MultipleRequest) { sp<NodeLooperThread> th = new NodeLooperThread(std::move(nodes_)); + EXPECT_TRUE(th->Start()); EXPECT_TRUE(th->isRunning()); // Dummy LAUNCH boost actions: // Node0, value1, 800ms |