Add support for createHintSessionWithConfig

This patch adds support for the new "createHintSessionWithConfig" call
which supports session tagging and session IDs, and updates the
performance_hint NDK implementation to use it.

Bug: 330553312
Bug: 315894228
Test: atest PerformanceHintNativeTestCases
Test: atest HintManagerServiceTest
Change-Id: Idc1c16be6bb53983cc3ec102596b9613935f7301
diff --git a/native/android/performance_hint.cpp b/native/android/performance_hint.cpp
index 882afca..fbb35e2 100644
--- a/native/android/performance_hint.cpp
+++ b/native/android/performance_hint.cpp
@@ -59,7 +59,8 @@
     ~APerformanceHintManager() = default;
 
     APerformanceHintSession* createSession(const int32_t* threadIds, size_t size,
-                                           int64_t initialTargetWorkDurationNanos);
+                                           int64_t initialTargetWorkDurationNanos,
+                                           hal::SessionTag tag = hal::SessionTag::OTHER);
     int64_t getPreferredRateNanos() const;
 
 private:
@@ -84,7 +85,8 @@
 public:
     APerformanceHintSession(std::shared_ptr<IHintManager> hintManager,
                             std::shared_ptr<IHintSession> session, int64_t preferredRateNanos,
-                            int64_t targetDurationNanos);
+                            int64_t targetDurationNanos,
+                            std::optional<hal::SessionConfig> sessionConfig);
     APerformanceHintSession() = delete;
     ~APerformanceHintSession();
 
@@ -116,9 +118,10 @@
     // Cached samples
     std::vector<hal::WorkDuration> mActualWorkDurations;
     std::string mSessionName;
-    static int32_t sIDCounter;
+    static int64_t sIDCounter;
     // The most recent set of thread IDs
     std::vector<int32_t> mLastThreadIDs;
+    std::optional<hal::SessionConfig> mSessionConfig;
     // Tracing helpers
     void traceThreads(std::vector<int32_t>& tids);
     void tracePowerEfficient(bool powerEfficient);
@@ -129,7 +132,8 @@
 
 static std::shared_ptr<IHintManager>* gIHintManagerForTesting = nullptr;
 static APerformanceHintManager* gHintManagerForTesting = nullptr;
-int32_t APerformanceHintSession::sIDCounter = 0;
+// Start above the int32 range so we don't collide with config sessions
+int64_t APerformanceHintSession::sIDCounter = INT32_MAX;
 
 // ===================================== APerformanceHintManager implementation
 APerformanceHintManager::APerformanceHintManager(std::shared_ptr<IHintManager> manager,
@@ -174,16 +178,20 @@
 }
 
 APerformanceHintSession* APerformanceHintManager::createSession(
-        const int32_t* threadIds, size_t size, int64_t initialTargetWorkDurationNanos) {
+        const int32_t* threadIds, size_t size, int64_t initialTargetWorkDurationNanos,
+        hal::SessionTag tag) {
     std::vector<int32_t> tids(threadIds, threadIds + size);
     std::shared_ptr<IHintSession> session;
-    ndk::ScopedAStatus ret =
-            mHintManager->createHintSession(mToken, tids, initialTargetWorkDurationNanos, &session);
+    ndk::ScopedAStatus ret;
+    std::optional<hal::SessionConfig> sessionConfig;
+    ret = mHintManager->createHintSessionWithConfig(mToken, tids, initialTargetWorkDurationNanos,
+                                                    tag, &sessionConfig, &session);
+
     if (!ret.isOk() || !session) {
         return nullptr;
     }
     auto out = new APerformanceHintSession(mHintManager, std::move(session), mPreferredRateNanos,
-                                           initialTargetWorkDurationNanos);
+                                           initialTargetWorkDurationNanos, sessionConfig);
     out->traceThreads(tids);
     out->traceTargetDuration(initialTargetWorkDurationNanos);
     out->tracePowerEfficient(false);
@@ -199,19 +207,23 @@
 APerformanceHintSession::APerformanceHintSession(std::shared_ptr<IHintManager> hintManager,
                                                  std::shared_ptr<IHintSession> session,
                                                  int64_t preferredRateNanos,
-                                                 int64_t targetDurationNanos)
+                                                 int64_t targetDurationNanos,
+                                                 std::optional<hal::SessionConfig> sessionConfig)
       : mHintManager(hintManager),
         mHintSession(std::move(session)),
         mPreferredRateNanos(preferredRateNanos),
         mTargetDurationNanos(targetDurationNanos),
         mFirstTargetMetTimestamp(0),
-        mLastTargetMetTimestamp(0) {
-    const std::vector<hal::SessionHint> sessionHintRange{ndk::enum_range<hal::SessionHint>()
-                                                                 .begin(),
-                                                         ndk::enum_range<hal::SessionHint>().end()};
-
-    mLastHintSentTimestamp = std::vector<int64_t>(sessionHintRange.size(), 0);
-    mSessionName = android::base::StringPrintf("ADPF Session %" PRId32, ++sIDCounter);
+        mLastTargetMetTimestamp(0),
+        mSessionConfig(sessionConfig) {
+    if (sessionConfig->id > INT32_MAX) {
+        ALOGE("Session ID too large, must fit 32-bit integer");
+    }
+    constexpr int numEnums =
+            ndk::enum_range<hal::SessionHint>().end() - ndk::enum_range<hal::SessionHint>().begin();
+    mLastHintSentTimestamp = std::vector<int64_t>(numEnums, 0);
+    int64_t traceId = sessionConfig.has_value() ? sessionConfig->id : ++sIDCounter;
+    mSessionName = android::base::StringPrintf("ADPF Session %" PRId64, traceId);
 }
 
 APerformanceHintSession::~APerformanceHintSession() {
diff --git a/native/android/tests/performance_hint/PerformanceHintNativeTest.cpp b/native/android/tests/performance_hint/PerformanceHintNativeTest.cpp
index bfbe34e..974e6e6 100644
--- a/native/android/tests/performance_hint/PerformanceHintNativeTest.cpp
+++ b/native/android/tests/performance_hint/PerformanceHintNativeTest.cpp
@@ -16,6 +16,8 @@
 
 #define LOG_TAG "PerformanceHintNativeTest"
 
+#include <aidl/android/hardware/power/SessionConfig.h>
+#include <aidl/android/hardware/power/SessionTag.h>
 #include <aidl/android/hardware/power/WorkDuration.h>
 #include <aidl/android/os/IHintManager.h>
 #include <android/binder_manager.h>
@@ -28,6 +30,8 @@
 #include <memory>
 #include <vector>
 
+using aidl::android::hardware::power::SessionConfig;
+using aidl::android::hardware::power::SessionTag;
 using aidl::android::hardware::power::WorkDuration;
 using aidl::android::os::IHintManager;
 using aidl::android::os::IHintSession;
@@ -39,8 +43,9 @@
 
 class MockIHintManager : public IHintManager {
 public:
-    MOCK_METHOD(ScopedAStatus, createHintSession,
+    MOCK_METHOD(ScopedAStatus, createHintSessionWithConfig,
                 (const SpAIBinder& token, const ::std::vector<int32_t>& tids, int64_t durationNanos,
+                 SessionTag tag, std::optional<SessionConfig>* config,
                  std::shared_ptr<IHintSession>* _aidl_return),
                 (override));
     MOCK_METHOD(ScopedAStatus, getHintSessionPreferredRate, (int64_t * _aidl_return), (override));
@@ -92,14 +97,18 @@
     APerformanceHintSession* createSession(APerformanceHintManager* manager,
                                            int64_t targetDuration = 56789L) {
         mMockSession = ndk::SharedRefBase::make<NiceMock<MockIHintSession>>();
-
+        int64_t sessionId = 123;
         std::vector<int32_t> tids;
         tids.push_back(1);
         tids.push_back(2);
 
-        ON_CALL(*mMockIHintManager, createHintSession(_, Eq(tids), Eq(targetDuration), _))
-                .WillByDefault(DoAll(SetArgPointee<3>(std::shared_ptr<IHintSession>(mMockSession)),
+        ON_CALL(*mMockIHintManager,
+                createHintSessionWithConfig(_, Eq(tids), Eq(targetDuration), _, _, _))
+                .WillByDefault(DoAll(SetArgPointee<4>(
+                                             std::make_optional<SessionConfig>({.id = sessionId})),
+                                     SetArgPointee<5>(std::shared_ptr<IHintSession>(mMockSession)),
                                      [] { return ScopedAStatus::ok(); }));
+
         ON_CALL(*mMockIHintManager, setHintSessionThreads(_, _)).WillByDefault([] {
             return ScopedAStatus::ok();
         });
@@ -115,7 +124,6 @@
         ON_CALL(*mMockSession, reportActualWorkDuration2(_)).WillByDefault([] {
             return ScopedAStatus::ok();
         });
-
         return APerformanceHint_createSession(manager, tids.data(), tids.size(), targetDuration);
     }
 
@@ -178,6 +186,14 @@
     APerformanceHint_closeSession(session);
 }
 
+TEST_F(PerformanceHintTest, TestUpdatedSessionCreation) {
+    EXPECT_CALL(*mMockIHintManager, createHintSessionWithConfig(_, _, _, _, _, _)).Times(1);
+    APerformanceHintManager* manager = createManager();
+    APerformanceHintSession* session = createSession(manager);
+    ASSERT_TRUE(session);
+    APerformanceHint_closeSession(session);
+}
+
 TEST_F(PerformanceHintTest, SetThreads) {
     APerformanceHintManager* manager = createManager();