libperfmgr: Add lock to HintManager

Test: libperfmgr_test
Bug: 62041945

(cherry picked from commit de306991da412967382d320fd243fc071e10cb41)

Change-Id: I4446bdf5838b23004526bb3e6aa5aca6e7a8ba86
diff --git a/libperfmgr/HintManager.cc b/libperfmgr/HintManager.cc
index d0a1520..9d70e23 100644
--- a/libperfmgr/HintManager.cc
+++ b/libperfmgr/HintManager.cc
@@ -40,6 +40,7 @@
 }
 
 bool HintManager::DoHint(const std::string& hint_type) {
+    std::lock_guard<std::mutex> lk(lock_);
     LOG(VERBOSE) << "Do Powerhint: " << hint_type;
     return ValidateHint(hint_type)
                ? nm_->Request(actions_[hint_type], hint_type)
@@ -48,6 +49,7 @@
 
 bool HintManager::DoHint(const std::string& hint_type,
                          std::chrono::milliseconds timeout_ms_override) {
+    std::lock_guard<std::mutex> lk(lock_);
     LOG(VERBOSE) << "Do Powerhint: " << hint_type << " for "
                  << timeout_ms_override.count() << "ms";
     if (!ValidateHint(hint_type)) {
@@ -61,6 +63,7 @@
 }
 
 bool HintManager::EndHint(const std::string& hint_type) {
+    std::lock_guard<std::mutex> lk(lock_);
     LOG(VERBOSE) << "End Powerhint: " << hint_type;
     return ValidateHint(hint_type) ? nm_->Cancel(actions_[hint_type], hint_type)
                                    : false;
@@ -229,7 +232,7 @@
     std::size_t total_parsed = 0;
 
     std::map<std::string, std::size_t> nodes_index;
-    for (uint32_t i = 0; i <= nodes.size(); ++i) {
+    for (std::size_t i = 0; i < nodes.size(); ++i) {
         nodes_index[nodes[i]->GetName()] = i;
     }
 
@@ -275,6 +278,8 @@
             return actions_parsed;
         }
         LOG(VERBOSE) << "Action[" << i << "]'s ValueIndex: " << value_index;
+        LOG(VERBOSE) << "Action[" << i << "]'s Node Value: "
+                     << nodes[node_index]->GetValues()[value_index];
 
         Json::UInt64 duration = 0;
         if (actions[i]["Duration"].empty() ||
diff --git a/libperfmgr/RequestGroup.cc b/libperfmgr/RequestGroup.cc
index 275f34a..f2cb7aa 100644
--- a/libperfmgr/RequestGroup.cc
+++ b/libperfmgr/RequestGroup.cc
@@ -35,7 +35,7 @@
     return request_map_.erase(hint_type);
 }
 
-const std::string RequestGroup::GetRequestValue() const {
+std::string RequestGroup::GetRequestValue() const {
     return request_value_;
 }
 
diff --git a/libperfmgr/include/perfmgr/HintManager.h b/libperfmgr/include/perfmgr/HintManager.h
index 4e7f78d..65a8e77 100644
--- a/libperfmgr/include/perfmgr/HintManager.h
+++ b/libperfmgr/include/perfmgr/HintManager.h
@@ -18,6 +18,7 @@
 #define ANDROID_LIBPERFMGR_HINTMANAGER_H_
 
 #include <cstddef>
+#include <mutex>
 #include <string>
 #include <vector>
 
@@ -75,6 +76,7 @@
 
     sp<NodeLooperThread> nm_;
     std::map<std::string, std::vector<NodeAction>> actions_;
+    std::mutex lock_;
 };
 
 }  // namespace perfmgr
diff --git a/libperfmgr/include/perfmgr/RequestGroup.h b/libperfmgr/include/perfmgr/RequestGroup.h
index 935e162..93550aa 100644
--- a/libperfmgr/include/perfmgr/RequestGroup.h
+++ b/libperfmgr/include/perfmgr/RequestGroup.h
@@ -44,7 +44,7 @@
     // request_map_ is empty.
     bool GetExpireTime(std::chrono::milliseconds* expire_time);
     // Return the request value.
-    const std::string GetRequestValue() const;
+    std::string GetRequestValue() const;
     // Return true for adding request, false for extending expire time of
     // existing active request on given hint_type.
     bool AddRequest(const std::string& hint_type, ReqTime end_time);
diff --git a/libperfmgr/tests/HintManagerTest.cc b/libperfmgr/tests/HintManagerTest.cc
index 56f23f9..ff90128 100644
--- a/libperfmgr/tests/HintManagerTest.cc
+++ b/libperfmgr/tests/HintManagerTest.cc
@@ -18,6 +18,7 @@
 #include <thread>
 
 #include <android-base/file.h>
+#include <android-base/logging.h>
 #include <android-base/test_utils.h>
 
 #include <gtest/gtest.h>
@@ -94,7 +95,9 @@
   protected:
     HintManagerTest()
         : HintManager(nullptr,
-                      std::map<std::string, std::vector<NodeAction>>{}) {}
+                      std::map<std::string, std::vector<NodeAction>>{}) {
+        android::base::SetMinimumLogSeverity(android::base::VERBOSE);
+    }
 
     virtual void SetUp() {
         // Set up dummy nodes