power-libperfmgr: ADPF: clear ResetUclampGrp flag when app's paused

Set the profile on resume/pause

Bug: 245675204
Bug: 275687673
Test: adb shell dumpsys android.hardware.power.IPower/default
Change-Id: Icadf526a2f262e77d7adb2184f61b3ccba44c601
diff --git a/power-libperfmgr/aidl/PowerHintSession.cpp b/power-libperfmgr/aidl/PowerHintSession.cpp
index 81f2f90..993007b 100644
--- a/power-libperfmgr/aidl/PowerHintSession.cpp
+++ b/power-libperfmgr/aidl/PowerHintSession.cpp
@@ -202,7 +202,7 @@
 void PowerHintSession::dumpToStream(std::ostream &stream) {
     stream << "ID.Min.Act.Timeout(" << mIdString;
     stream << ", " << mDescriptor->current_min;
-    stream << ", " << mDescriptor->is_active;
+    stream << ", " << mDescriptor->is_active.load();
     stream << ", " << isTimeout() << ")";
 }
 
@@ -220,6 +220,7 @@
         traceSessionVal("active", mDescriptor->is_active.load());
     }
     updateUniveralBoostMode();
+    PowerSessionManager::getInstance()->removeThreadsFromPowerSession(this);
     return ndk::ScopedAStatus::ok();
 }
 
@@ -231,6 +232,7 @@
     if (mDescriptor->is_active.load())
         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
     mDescriptor->is_active.store(true);
+    PowerSessionManager::getInstance()->addThreadsFromPowerSession(this);
     // resume boost
     setSessionUclampMin(mDescriptor->current_min);
     if (ATRACE_ENABLED()) {
diff --git a/power-libperfmgr/aidl/PowerSessionManager.cpp b/power-libperfmgr/aidl/PowerSessionManager.cpp
index d697c6a..e66ff5f 100644
--- a/power-libperfmgr/aidl/PowerSessionManager.cpp
+++ b/power-libperfmgr/aidl/PowerSessionManager.cpp
@@ -104,23 +104,23 @@
 }
 
 void PowerSessionManager::addPowerSession(PowerHintSession *session) {
-    addThreadsFromPowerSession(session);
-    {
-        std::lock_guard<std::mutex> guard(mLock);
-        mSessions.insert(session);
-    }
+    std::lock_guard<std::mutex> guard(mLock);
+    mSessions.insert(session);
+    addThreadsFromPowerSessionLocked(session);
 }
 
 void PowerSessionManager::removePowerSession(PowerHintSession *session) {
-    removeThreadsFromPowerSession(session);
-    {
-        std::lock_guard<std::mutex> guard(mLock);
-        mSessions.erase(session);
-    }
+    std::lock_guard<std::mutex> guard(mLock);
+    mSessions.erase(session);
+    removeThreadsFromPowerSessionLocked(session);
 }
 
 void PowerSessionManager::addThreadsFromPowerSession(PowerHintSession *session) {
     std::lock_guard<std::mutex> guard(mLock);
+    addThreadsFromPowerSessionLocked(session);
+}
+
+void PowerSessionManager::addThreadsFromPowerSessionLocked(PowerHintSession *session) {
     for (auto t : session->getTidList()) {
         if (mTidSessionListMap[t].empty()) {
             if (!SetTaskProfiles(t, {"ResetUclampGrp"})) {
@@ -133,9 +133,13 @@
 
 void PowerSessionManager::removeThreadsFromPowerSession(PowerHintSession *session) {
     std::lock_guard<std::mutex> guard(mLock);
+    removeThreadsFromPowerSessionLocked(session);
+}
+
+void PowerSessionManager::removeThreadsFromPowerSessionLocked(PowerHintSession *session) {
     for (auto t : session->getTidList()) {
-        mTidSessionListMap[t].erase(session);
-        if (mTidSessionListMap[t].empty()) {
+        size_t cnt = mTidSessionListMap[t].erase(session);
+        if (cnt != 0 && mTidSessionListMap[t].empty()) {
             if (!SetTaskProfiles(t, {"NoResetUclampGrp"})) {
                 ALOGW("Failed to set NoResetUclampGrp task profile for tid:%d", t);
             }
diff --git a/power-libperfmgr/aidl/PowerSessionManager.h b/power-libperfmgr/aidl/PowerSessionManager.h
index 1c2733a..7c74039 100644
--- a/power-libperfmgr/aidl/PowerSessionManager.h
+++ b/power-libperfmgr/aidl/PowerSessionManager.h
@@ -51,7 +51,9 @@
     void addPowerSession(PowerHintSession *session);
     void removePowerSession(PowerHintSession *session);
     void addThreadsFromPowerSession(PowerHintSession *session);
+    void addThreadsFromPowerSessionLocked(PowerHintSession *session);
     void removeThreadsFromPowerSession(PowerHintSession *session);
+    void removeThreadsFromPowerSessionLocked(PowerHintSession *session);
     void setUclampMin(PowerHintSession *session, int min);
     void setUclampMinLocked(PowerHintSession *session, int min);
     void handleMessage(const Message &message) override;