power: add PowerHintSession for ADPF implementation

Adapted from PoC from ag/13100800

Added more ATRACE for further tuning and debug

Test: APPPID=$(adb shell pidof com.prefabulated.touchlatency); watch -n
      1 adb shell grep uclamp /proc/${APPPID}/sched
Test: atest VtsHalPowerTargetTest
Bug: 177492680
Change-Id: I6bfd61b21dc1cde04f6ba9ae8d3533cd263ad814
Signed-off-by: Wei Wang <wvw@google.com>
diff --git a/aidl/power-libperfmgr/Android.mk b/aidl/power-libperfmgr/Android.mk
index 292701f..25d960f 100644
--- a/aidl/power-libperfmgr/Android.mk
+++ b/aidl/power-libperfmgr/Android.mk
@@ -24,7 +24,8 @@
 LOCAL_MODULE_RELATIVE_PATH := hw
 
 LOCAL_SHARED_LIBRARIES := \
-    android.hardware.power-V1-ndk_platform \
+    android.hardware.power-V2-ndk_platform \
+    libadpf-pixel \
     libbase \
     libbinder_ndk \
     libcutils \
diff --git a/aidl/power-libperfmgr/InteractionHandler.cpp b/aidl/power-libperfmgr/InteractionHandler.cpp
index 64dbce4..dd135b0 100644
--- a/aidl/power-libperfmgr/InteractionHandler.cpp
+++ b/aidl/power-libperfmgr/InteractionHandler.cpp
@@ -15,7 +15,7 @@
  */
 
 #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
-#define LOG_TAG "android.hardware.power-service.mediatek-libperfmgr"
+#define LOG_TAG "powerhal-libperfmgr"
 
 #include <array>
 #include <memory>
diff --git a/aidl/power-libperfmgr/Power.cpp b/aidl/power-libperfmgr/Power.cpp
index ca2e3a1..78cdb4c 100644
--- a/aidl/power-libperfmgr/Power.cpp
+++ b/aidl/power-libperfmgr/Power.cpp
@@ -15,7 +15,7 @@
  */
 
 #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
-#define LOG_TAG "android.hardware.power-service.mediatek-libperfmgr"
+#define LOG_TAG "powerhal-libperfmgr"
 
 #include "Power.h"
 
@@ -26,10 +26,13 @@
 #include <android-base/properties.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
+#include <cutils/properties.h>
 
 #include <utils/Log.h>
 #include <utils/Trace.h>
 
+#include "adpf/PowerHintSession.h"
+
 namespace aidl {
 namespace google {
 namespace hardware {
@@ -37,6 +40,8 @@
 namespace impl {
 namespace pixel {
 
+using ::aidl::google::hardware::power::impl::pixel::adpf::PowerHintSession;
+
 #ifdef MODE_EXT
 extern bool isDeviceSpecificModeSupported(Mode type, bool* _aidl_return);
 extern bool setDeviceSpecificMode(Mode type, bool enabled);
@@ -45,37 +50,40 @@
 constexpr char kPowerHalStateProp[] = "vendor.powerhal.state";
 constexpr char kPowerHalAudioProp[] = "vendor.powerhal.audio";
 constexpr char kPowerHalRenderingProp[] = "vendor.powerhal.rendering";
+constexpr char kPowerHalAdpfRateProp[] = "vendor.powerhal.adpf.rate";
+constexpr int64_t kPowerHalAdpfRateDefault = -1;
 
 Power::Power(std::shared_ptr<HintManager> hm)
     : mHintManager(hm),
       mInteractionHandler(nullptr),
-      mSustainedPerfModeOn(false) {
+      mSustainedPerfModeOn(false),
+      mAdpfRate(::android::base::GetIntProperty(kPowerHalAdpfRateProp, kPowerHalAdpfRateDefault)) {
     mInteractionHandler = std::make_unique<InteractionHandler>(mHintManager);
     mInteractionHandler->Init();
 
     std::string state = ::android::base::GetProperty(kPowerHalStateProp, "");
     if (state == "SUSTAINED_PERFORMANCE") {
-        ALOGI("Initialize with SUSTAINED_PERFORMANCE on");
+        LOG(INFO) << "Initialize with SUSTAINED_PERFORMANCE on";
         mHintManager->DoHint("SUSTAINED_PERFORMANCE");
         mSustainedPerfModeOn = true;
     } else {
-        ALOGI("Initialize PowerHAL");
+        LOG(INFO) << "Initialize PowerHAL";
     }
 
     state = ::android::base::GetProperty(kPowerHalAudioProp, "");
     if (state == "AUDIO_STREAMING_LOW_LATENCY") {
-        ALOGI("Initialize with AUDIO_LOW_LATENCY on");
+        LOG(INFO) << "Initialize with AUDIO_LOW_LATENCY on";
         mHintManager->DoHint(state);
     }
 
     state = ::android::base::GetProperty(kPowerHalRenderingProp, "");
     if (state == "EXPENSIVE_RENDERING") {
-        ALOGI("Initialize with EXPENSIVE_RENDERING on");
+        LOG(INFO) << "Initialize with EXPENSIVE_RENDERING on";
         mHintManager->DoHint("EXPENSIVE_RENDERING");
     }
 
     // Now start to take powerhint
-    ALOGI("PowerHAL ready to process hints");
+    LOG(INFO) << "PowerHAL ready to take hints, Adpf update rate: " << mAdpfRate;
 }
 
 ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) {
@@ -208,6 +216,34 @@
     return STATUS_OK;
 }
 
+ndk::ScopedAStatus Power::createHintSession(int32_t tgid, int32_t uid,
+                                            const std::vector<int32_t> &threadIds,
+                                            int64_t durationNanos,
+                                            std::shared_ptr<IPowerHintSession> *_aidl_return) {
+    if (mAdpfRate == -1) {
+        *_aidl_return = nullptr;
+        return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+    }
+    if (threadIds.size() == 0) {
+        LOG(ERROR) << "Error: threadIds.size() shouldn't be " << threadIds.size();
+        *_aidl_return = nullptr;
+        return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
+    }
+    std::shared_ptr<IPowerHintSession> session =
+            ndk::SharedRefBase::make<PowerHintSession>(tgid, uid, threadIds, durationNanos);
+    *_aidl_return = session;
+    return ndk::ScopedAStatus::ok();
+}
+
+ndk::ScopedAStatus Power::getHintSessionPreferredRate(int64_t *outNanoseconds) {
+    *outNanoseconds = mAdpfRate;
+    if (mAdpfRate == -1) {
+        return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
+    }
+
+    return ndk::ScopedAStatus::ok();
+}
+
 }  // namespace pixel
 }  // namespace impl
 }  // namespace power
diff --git a/aidl/power-libperfmgr/Power.h b/aidl/power-libperfmgr/Power.h
index 712d0f0..6f4a938 100644
--- a/aidl/power-libperfmgr/Power.h
+++ b/aidl/power-libperfmgr/Power.h
@@ -33,6 +33,7 @@
 namespace pixel {
 
 using ::aidl::android::hardware::power::Boost;
+using ::aidl::android::hardware::power::IPowerHintSession;
 using ::aidl::android::hardware::power::Mode;
 using ::android::perfmgr::HintManager;
 
@@ -43,12 +44,18 @@
     ndk::ScopedAStatus isModeSupported(Mode type, bool *_aidl_return) override;
     ndk::ScopedAStatus setBoost(Boost type, int32_t durationMs) override;
     ndk::ScopedAStatus isBoostSupported(Boost type, bool *_aidl_return) override;
+    ndk::ScopedAStatus createHintSession(int32_t tgid, int32_t uid,
+                                         const std::vector<int32_t> &threadIds,
+                                         int64_t durationNanos,
+                                         std::shared_ptr<IPowerHintSession> *_aidl_return) override;
+    ndk::ScopedAStatus getHintSessionPreferredRate(int64_t *outNanoseconds) override;
     binder_status_t dump(int fd, const char **args, uint32_t numArgs) override;
 
   private:
     std::shared_ptr<HintManager> mHintManager;
     std::unique_ptr<InteractionHandler> mInteractionHandler;
     std::atomic<bool> mSustainedPerfModeOn;
+    const int64_t mAdpfRate;
 };
 
 }  // namespace pixel
diff --git a/aidl/power-libperfmgr/android.hardware.power-service.mediatek.xml b/aidl/power-libperfmgr/android.hardware.power-service.mediatek.xml
index caf6ea2..9f56deb 100644
--- a/aidl/power-libperfmgr/android.hardware.power-service.mediatek.xml
+++ b/aidl/power-libperfmgr/android.hardware.power-service.mediatek.xml
@@ -1,6 +1,7 @@
 <manifest version="1.0" type="device">
     <hal format="aidl">
         <name>android.hardware.power</name>
+        <version>2</version>
         <fqname>IPower/default</fqname>
     </hal>
 </manifest>
diff --git a/aidl/power-libperfmgr/service.cpp b/aidl/power-libperfmgr/service.cpp
index 7771f8c..c2bcc44 100644
--- a/aidl/power-libperfmgr/service.cpp
+++ b/aidl/power-libperfmgr/service.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "android.hardware.power-service.mediatek-libperfmgr"
+#define LOG_TAG "powerhal-libperfmgr"
 
 #include <thread>