RM6785: power-libperfmgr: Optimize the wakeup performance and fix unstable boost

Optimize boost:
A more efficient way is to trigger the wakeup boost through mTidSessionListMap, so the time
   complexity reduce from O(n^3) to O(n^2).

The original code path:
  PSM:wakeSessions() contains a loop that is O(n),
  inside the loop, it calls to PHS::wakeup() which call to
  PSM::setUclampMinLocked(), and
  PSM::setUclampMinLocked() contains two loops O(n^2)

The new code path:
  PSM::wakeSessions() directly checks all the ADPF tasks O(n) and
  get the wakeup boost value O(n), then directly set the uclamp.min
  The time complexity lower to O(n^2).

Fix unstable boost:
The original flow is to find max boost and wake it up from stale state
one by one. But the previous woken ones would not be counted when the later ones finding their max boost value.
The new flow boost all the tasks first, then wake up all those sessions.

Bug: 235510337
Test: Manually playing UIBench -> Transitions -> ActivityTransition
Change-Id: I995673b74401e198eb72188134ba1ebc134f971c
diff --git a/aidl/power-libperfmgr/PowerHintSession.cpp b/aidl/power-libperfmgr/PowerHintSession.cpp
index 5c7532e..8de1021 100644
--- a/aidl/power-libperfmgr/PowerHintSession.cpp
+++ b/aidl/power-libperfmgr/PowerHintSession.cpp
@@ -416,14 +416,13 @@
         ATRACE_NAME(tag.c_str());
     }
     std::shared_ptr<AdpfConfig> adpfConfig = HintManager::GetInstance()->GetAdpfProfile();
-    int min = std::max(mDescriptor->current_min, static_cast<int>(adpfConfig->mUclampMinInit));
-    mDescriptor->current_min = min;
-    PowerSessionManager::getInstance()->setUclampMinLocked(this, min);
+    mDescriptor->current_min =
+            std::max(mDescriptor->current_min, static_cast<int>(adpfConfig->mUclampMinInit));
 
     if (ATRACE_ENABLED()) {
         const std::string idstr = getIdString();
         std::string sz = StringPrintf("adpf.%s-min", idstr.c_str());
-        ATRACE_INT(sz.c_str(), min);
+        ATRACE_INT(sz.c_str(), mDescriptor->current_min);
     }
 }
 
diff --git a/aidl/power-libperfmgr/PowerSessionManager.cpp b/aidl/power-libperfmgr/PowerSessionManager.cpp
index 888118d..ddd9f75 100644
--- a/aidl/power-libperfmgr/PowerSessionManager.cpp
+++ b/aidl/power-libperfmgr/PowerSessionManager.cpp
@@ -23,6 +23,7 @@
 namespace impl {
 namespace pixel {
 
+using ::android::perfmgr::AdpfConfig;
 using ::android::perfmgr::HintManager;
 
 namespace {
@@ -93,7 +94,28 @@
 
 void PowerSessionManager::wakeSessions() {
     std::lock_guard<std::mutex> guard(mLock);
-    for (PowerHintSession *s : mSessions) {
+    std::shared_ptr<AdpfConfig> adpfConfig = HintManager::GetInstance()->GetAdpfProfile();
+    std::unordered_set<PowerHintSession *> wakeupList;
+    const int wakeupBoostValue = static_cast<int>(adpfConfig->mUclampMinInit);
+    for (auto &it : mTidSessionListMap) {
+        int tid = it.first;
+        int maxboost = -1;
+        // Find the max boost value among all the sessions that include the same TID.
+        for (PowerHintSession *s : it.second) {
+            if (!s->isActive())
+                continue;
+            // all active sessions need to be awakened.
+            wakeupList.insert(s);
+            if (s->isTimeout()) {
+                maxboost = std::max(maxboost, s->getUclampMin());
+            }
+        }
+        // Found the max boost and actally set to the task.
+        if (maxboost != -1) {
+            set_uclamp_min(tid, std::max(maxboost, wakeupBoostValue));
+        }
+    }
+    for (PowerHintSession *s : wakeupList) {
         s->wakeup();
     }
 }