From 53f1fc4eeecd8919462250e0b457dd11c906b96f Mon Sep 17 00:00:00 2001 From: Jimmy Shiu Date: Thu, 18 Jul 2024 20:16:00 +0530 Subject: [PATCH] sm6375-common: power-libperfmgr: Make HintManager a singleton class HintManager is unique and widely used in many different components. This is for making it easiler to be reloaded for debugging. Bug: 172285365 Test: adb push \ out/target/product/raven/data/nativetest64/libperfmgr_test/libperfmgr_test \ /data/nativetest64/libperfmgr_test/libperfmgr_test && \ adb shell /data/nativetest64/libperfmgr_test/libperfmgr_test Change-Id: I3affdfe780073ebbc50fac7bfbdd1530ee9dc8c2 --- power-libperfmgr/InteractionHandler.cpp | 24 +++++++------- power-libperfmgr/InteractionHandler.h | 7 +--- power-libperfmgr/Power.cpp | 41 ++++++++++++------------ power-libperfmgr/Power.h | 9 ++---- power-libperfmgr/PowerExt.cpp | 25 ++++++++------- power-libperfmgr/PowerExt.h | 5 +-- power-libperfmgr/PowerSessionManager.cpp | 20 +++++------- power-libperfmgr/PowerSessionManager.h | 3 -- power-libperfmgr/service.cpp | 30 +++++------------ 9 files changed, 68 insertions(+), 96 deletions(-) diff --git a/power-libperfmgr/InteractionHandler.cpp b/power-libperfmgr/InteractionHandler.cpp index eb08855..a9f908b 100644 --- a/power-libperfmgr/InteractionHandler.cpp +++ b/power-libperfmgr/InteractionHandler.cpp @@ -17,20 +17,20 @@ #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL) #define LOG_TAG "powerhal-libperfmgr" -#include -#include +#include "InteractionHandler.h" +#include #include +#include #include #include #include #include - -#include #include #include -#include "InteractionHandler.h" +#include +#include #define MAX_LENGTH 64 @@ -79,10 +79,10 @@ static int FbIdleOpen(void) { } // namespace -InteractionHandler::InteractionHandler(std::shared_ptr const &hint_manager) - : mState(INTERACTION_STATE_UNINITIALIZED), - mDurationMs(0), - mHintManager(hint_manager) {} +using ::android::perfmgr::HintManager; + +InteractionHandler::InteractionHandler() + : mState(INTERACTION_STATE_UNINITIALIZED), mDurationMs(0) {} InteractionHandler::~InteractionHandler() { Exit(); @@ -130,14 +130,14 @@ void InteractionHandler::Exit() { void InteractionHandler::PerfLock() { ALOGV("%s: acquiring perf lock", __func__); - if (!mHintManager->DoHint("INTERACTION")) { + if (!HintManager::GetInstance()->DoHint("INTERACTION")) { ALOGE("%s: do hint INTERACTION failed", __func__); } } void InteractionHandler::PerfRel() { ALOGV("%s: releasing perf lock", __func__); - if (!mHintManager->EndHint("INTERACTION")) { + if (!HintManager::GetInstance()->EndHint("INTERACTION")) { ALOGE("%s: end hint INTERACTION failed", __func__); } } @@ -160,7 +160,7 @@ void InteractionHandler::Acquire(int32_t duration) { // 1) override property is set OR // 2) InteractionHandler not initialized if (!kDisplayIdleSupport || mState == INTERACTION_STATE_UNINITIALIZED) { - mHintManager->DoHint("INTERACTION", std::chrono::milliseconds(finalDuration)); + HintManager::GetInstance()->DoHint("INTERACTION", std::chrono::milliseconds(finalDuration)); return; } diff --git a/power-libperfmgr/InteractionHandler.h b/power-libperfmgr/InteractionHandler.h index 77ade88..c88f7d4 100644 --- a/power-libperfmgr/InteractionHandler.h +++ b/power-libperfmgr/InteractionHandler.h @@ -22,8 +22,6 @@ #include #include -#include - namespace aidl { namespace google { namespace hardware { @@ -31,8 +29,6 @@ namespace power { namespace impl { namespace pixel { -using ::android::perfmgr::HintManager; - enum InteractionState { INTERACTION_STATE_UNINITIALIZED, INTERACTION_STATE_IDLE, @@ -42,7 +38,7 @@ enum InteractionState { class InteractionHandler { public: - InteractionHandler(std::shared_ptr const &hint_manager); + InteractionHandler(); ~InteractionHandler(); bool Init(); void Exit(); @@ -65,7 +61,6 @@ class InteractionHandler { std::unique_ptr mThread; std::mutex mLock; std::condition_variable mCond; - std::shared_ptr mHintManager; }; } // namespace pixel diff --git a/power-libperfmgr/Power.cpp b/power-libperfmgr/Power.cpp index c430dcf..a90fc00 100644 --- a/power-libperfmgr/Power.cpp +++ b/power-libperfmgr/Power.cpp @@ -18,16 +18,16 @@ #include "Power.h" -#include - #include #include #include #include #include - +#include #include +#include + #include "PowerHintSession.h" #include "PowerSessionManager.h" @@ -41,6 +41,7 @@ namespace impl { namespace pixel { using ::aidl::google::hardware::power::impl::pixel::PowerHintSession; +using ::android::perfmgr::HintManager; constexpr char kPowerHalStateProp[] = "vendor.powerhal.state"; constexpr char kPowerHalAudioProp[] = "vendor.powerhal.audio"; @@ -48,19 +49,18 @@ constexpr char kPowerHalRenderingProp[] = "vendor.powerhal.rendering"; constexpr char kPowerHalAdpfRateProp[] = "vendor.powerhal.adpf.rate"; constexpr int64_t kPowerHalAdpfRateDefault = -1; -Power::Power(std::shared_ptr hm) - : mHintManager(hm), - mInteractionHandler(nullptr), +Power::Power() + : mInteractionHandler(nullptr), mSustainedPerfModeOn(false), mAdpfRateNs( ::android::base::GetIntProperty(kPowerHalAdpfRateProp, kPowerHalAdpfRateDefault)) { - mInteractionHandler = std::make_unique(mHintManager); + mInteractionHandler = std::make_unique(); mInteractionHandler->Init(); std::string state = ::android::base::GetProperty(kPowerHalStateProp, ""); if (state == "SUSTAINED_PERFORMANCE") { LOG(INFO) << "Initialize with SUSTAINED_PERFORMANCE on"; - mHintManager->DoHint("SUSTAINED_PERFORMANCE"); + HintManager::GetInstance()->DoHint("SUSTAINED_PERFORMANCE"); mSustainedPerfModeOn = true; } else { LOG(INFO) << "Initialize PowerHAL"; @@ -69,13 +69,13 @@ Power::Power(std::shared_ptr hm) state = ::android::base::GetProperty(kPowerHalAudioProp, ""); if (state == "AUDIO_STREAMING_LOW_LATENCY") { LOG(INFO) << "Initialize with AUDIO_LOW_LATENCY on"; - mHintManager->DoHint(state); + HintManager::GetInstance()->DoHint(state); } state = ::android::base::GetProperty(kPowerHalRenderingProp, ""); if (state == "EXPENSIVE_RENDERING") { LOG(INFO) << "Initialize with EXPENSIVE_RENDERING on"; - mHintManager->DoHint("EXPENSIVE_RENDERING"); + HintManager::GetInstance()->DoHint("EXPENSIVE_RENDERING"); } // Now start to take powerhint @@ -93,7 +93,7 @@ ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) { #endif case Mode::SUSTAINED_PERFORMANCE: if (enabled) { - mHintManager->DoHint("SUSTAINED_PERFORMANCE"); + HintManager::GetInstance()->DoHint("SUSTAINED_PERFORMANCE"); } mSustainedPerfModeOn = true; break; @@ -120,9 +120,9 @@ ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) { [[fallthrough]]; default: if (enabled) { - mHintManager->DoHint(toString(type)); + HintManager::GetInstance()->DoHint(toString(type)); } else { - mHintManager->EndHint(toString(type)); + HintManager::GetInstance()->EndHint(toString(type)); } break; } @@ -131,7 +131,7 @@ ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) { } ndk::ScopedAStatus Power::isModeSupported(Mode type, bool *_aidl_return) { - bool supported = mHintManager->IsHintSupported(toString(type)); + bool supported = HintManager::GetInstance()->IsHintSupported(toString(type)); #ifdef TAP_TO_WAKE_NODE if (type == Mode::DOUBLE_TAP_TO_WAKE) { supported = true; @@ -162,11 +162,12 @@ ndk::ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) { break; } if (durationMs > 0) { - mHintManager->DoHint(toString(type), std::chrono::milliseconds(durationMs)); + HintManager::GetInstance()->DoHint(toString(type), + std::chrono::milliseconds(durationMs)); } else if (durationMs == 0) { - mHintManager->DoHint(toString(type)); + HintManager::GetInstance()->DoHint(toString(type)); } else { - mHintManager->EndHint(toString(type)); + HintManager::GetInstance()->EndHint(toString(type)); } break; } @@ -175,7 +176,7 @@ ndk::ScopedAStatus Power::setBoost(Boost type, int32_t durationMs) { } ndk::ScopedAStatus Power::isBoostSupported(Boost type, bool *_aidl_return) { - bool supported = mHintManager->IsHintSupported(toString(type)); + bool supported = HintManager::GetInstance()->IsHintSupported(toString(type)); LOG(INFO) << "Power boost " << toString(type) << " isBoostSupported: " << supported; *_aidl_return = supported; return ndk::ScopedAStatus::ok(); @@ -189,10 +190,10 @@ binder_status_t Power::dump(int fd, const char **, uint32_t) { std::string buf(::android::base::StringPrintf( "HintManager Running: %s\n" "SustainedPerformanceMode: %s\n", - boolToString(mHintManager->IsRunning()), + boolToString(HintManager::GetInstance()->IsRunning()), boolToString(mSustainedPerfModeOn))); // Dump nodes through libperfmgr - mHintManager->DumpToFd(fd); + HintManager::GetInstance()->DumpToFd(fd); if (!::android::base::WriteStringToFd(buf, fd)) { PLOG(ERROR) << "Failed to dump state to fd"; } diff --git a/power-libperfmgr/Power.h b/power-libperfmgr/Power.h index 888ca90..46e6e08 100644 --- a/power-libperfmgr/Power.h +++ b/power-libperfmgr/Power.h @@ -16,13 +16,12 @@ #pragma once +#include + #include #include #include -#include -#include - #include "InteractionHandler.h" namespace aidl { @@ -35,11 +34,10 @@ namespace pixel { using ::aidl::android::hardware::power::Boost; using ::aidl::android::hardware::power::IPowerHintSession; using ::aidl::android::hardware::power::Mode; -using ::android::perfmgr::HintManager; class Power : public ::aidl::android::hardware::power::BnPower { public: - Power(std::shared_ptr hm); + Power(); ndk::ScopedAStatus setMode(Mode type, bool enabled) override; ndk::ScopedAStatus isModeSupported(Mode type, bool *_aidl_return) override; ndk::ScopedAStatus setBoost(Boost type, int32_t durationMs) override; @@ -52,7 +50,6 @@ class Power : public ::aidl::android::hardware::power::BnPower { binder_status_t dump(int fd, const char **args, uint32_t numArgs) override; private: - std::shared_ptr mHintManager; std::unique_ptr mInteractionHandler; std::atomic mSustainedPerfModeOn; const int64_t mAdpfRateNs; diff --git a/power-libperfmgr/PowerExt.cpp b/power-libperfmgr/PowerExt.cpp index a03bb0f..f407eb4 100644 --- a/power-libperfmgr/PowerExt.cpp +++ b/power-libperfmgr/PowerExt.cpp @@ -17,18 +17,19 @@ #define LOG_TAG "android.hardware.power-service.realme_sm6375.ext-libperfmgr" #include "PowerExt.h" -#include "PowerSessionManager.h" - -#include #include #include #include #include #include - +#include #include +#include + +#include "PowerSessionManager.h" + namespace aidl { namespace google { namespace hardware { @@ -36,13 +37,15 @@ namespace power { namespace impl { namespace pixel { +using ::android::perfmgr::HintManager; + ndk::ScopedAStatus PowerExt::setMode(const std::string &mode, bool enabled) { LOG(DEBUG) << "PowerExt setMode: " << mode << " to: " << enabled; if (enabled) { - mHintManager->DoHint(mode); + HintManager::GetInstance()->DoHint(mode); } else { - mHintManager->EndHint(mode); + HintManager::GetInstance()->EndHint(mode); } PowerSessionManager::getInstance()->updateHintMode(mode, enabled); @@ -50,7 +53,7 @@ ndk::ScopedAStatus PowerExt::setMode(const std::string &mode, bool enabled) { } ndk::ScopedAStatus PowerExt::isModeSupported(const std::string &mode, bool *_aidl_return) { - bool supported = mHintManager->IsHintSupported(mode); + bool supported = HintManager::GetInstance()->IsHintSupported(mode); LOG(INFO) << "PowerExt mode " << mode << " isModeSupported: " << supported; *_aidl_return = supported; return ndk::ScopedAStatus::ok(); @@ -60,18 +63,18 @@ ndk::ScopedAStatus PowerExt::setBoost(const std::string &boost, int32_t duration LOG(DEBUG) << "PowerExt setBoost: " << boost << " duration: " << durationMs; if (durationMs > 0) { - mHintManager->DoHint(boost, std::chrono::milliseconds(durationMs)); + HintManager::GetInstance()->DoHint(boost, std::chrono::milliseconds(durationMs)); } else if (durationMs == 0) { - mHintManager->DoHint(boost); + HintManager::GetInstance()->DoHint(boost); } else { - mHintManager->EndHint(boost); + HintManager::GetInstance()->EndHint(boost); } return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus PowerExt::isBoostSupported(const std::string &boost, bool *_aidl_return) { - bool supported = mHintManager->IsHintSupported(boost); + bool supported = HintManager::GetInstance()->IsHintSupported(boost); LOG(INFO) << "PowerExt boost " << boost << " isBoostSupported: " << supported; *_aidl_return = supported; return ndk::ScopedAStatus::ok(); diff --git a/power-libperfmgr/PowerExt.h b/power-libperfmgr/PowerExt.h index 885c6a0..bed12b5 100644 --- a/power-libperfmgr/PowerExt.h +++ b/power-libperfmgr/PowerExt.h @@ -30,18 +30,15 @@ namespace power { namespace impl { namespace pixel { -using ::android::perfmgr::HintManager; - class PowerExt : public ::aidl::google::hardware::power::extension::pixel::BnPowerExt { public: - PowerExt(std::shared_ptr hm) : mHintManager(hm) {} + PowerExt() {} ndk::ScopedAStatus setMode(const std::string &mode, bool enabled) override; ndk::ScopedAStatus isModeSupported(const std::string &mode, bool *_aidl_return) override; ndk::ScopedAStatus setBoost(const std::string &boost, int32_t durationMs) override; ndk::ScopedAStatus isBoostSupported(const std::string &boost, bool *_aidl_return) override; private: - std::shared_ptr mHintManager; }; } // namespace pixel diff --git a/power-libperfmgr/PowerSessionManager.cpp b/power-libperfmgr/PowerSessionManager.cpp index fc73c01..588ecee 100644 --- a/power-libperfmgr/PowerSessionManager.cpp +++ b/power-libperfmgr/PowerSessionManager.cpp @@ -17,12 +17,13 @@ #define LOG_TAG "powerhal-libperfmgr" #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL) +#include "PowerSessionManager.h" + #include +#include #include #include -#include "PowerSessionManager.h" - namespace aidl { namespace google { namespace hardware { @@ -30,12 +31,7 @@ namespace power { namespace impl { namespace pixel { -void PowerSessionManager::setHintManager(std::shared_ptr const &hint_manager) { - // Only initialize hintmanager instance if hint is supported. - if (hint_manager->IsHintSupported(kDisableBoostHintName)) { - mHintManager = hint_manager; - } -} +using ::android::perfmgr::HintManager; void PowerSessionManager::updateHintMode(const std::string &mode, bool enabled) { ALOGV("PowerSessionManager::updateHintMode: mode: %s, enabled: %d", mode.c_str(), enabled); @@ -124,16 +120,16 @@ void PowerSessionManager::handleMessage(const Message &) { } void PowerSessionManager::enableSystemTopAppBoost() { - if (mHintManager) { + if (HintManager::GetInstance()->IsHintSupported(kDisableBoostHintName)) { ALOGV("PowerSessionManager::enableSystemTopAppBoost!!"); - mHintManager->EndHint(kDisableBoostHintName); + HintManager::GetInstance()->EndHint(kDisableBoostHintName); } } void PowerSessionManager::disableSystemTopAppBoost() { - if (mHintManager) { + if (HintManager::GetInstance()->IsHintSupported(kDisableBoostHintName)) { ALOGV("PowerSessionManager::disableSystemTopAppBoost!!"); - mHintManager->DoHint(kDisableBoostHintName); + HintManager::GetInstance()->DoHint(kDisableBoostHintName); } } diff --git a/power-libperfmgr/PowerSessionManager.h b/power-libperfmgr/PowerSessionManager.h index 4b7c36d..acbdcc8 100644 --- a/power-libperfmgr/PowerSessionManager.h +++ b/power-libperfmgr/PowerSessionManager.h @@ -51,7 +51,6 @@ class PowerSessionManager : public MessageHandler { void removePowerSession(PowerHintSession *session); void handleMessage(const Message &message) override; - void setHintManager(std::shared_ptr const &hint_manager); // Singleton static sp getInstance() { @@ -64,7 +63,6 @@ class PowerSessionManager : public MessageHandler { void disableSystemTopAppBoost(); void enableSystemTopAppBoost(); const std::string kDisableBoostHintName; - std::shared_ptr mHintManager; std::unordered_set mSessions; // protected by mLock std::unordered_map mTidRefCountMap; // protected by mLock std::mutex mLock; @@ -74,7 +72,6 @@ class PowerSessionManager : public MessageHandler { PowerSessionManager() : kDisableBoostHintName(::android::base::GetProperty(kPowerHalAdpfDisableTopAppBoost, "ADPF_DISABLE_TA_BOOST")), - mHintManager(nullptr), mDisplayRefreshRate(60), mActive(false) {} PowerSessionManager(PowerSessionManager const &) = delete; diff --git a/power-libperfmgr/service.cpp b/power-libperfmgr/service.cpp index b144b43..8593f49 100644 --- a/power-libperfmgr/service.cpp +++ b/power-libperfmgr/service.cpp @@ -16,12 +16,13 @@ #define LOG_TAG "powerhal-libperfmgr" -#include - #include #include #include #include +#include + +#include #include "Power.h" #include "PowerExt.h" @@ -34,37 +35,23 @@ using aidl::google::hardware::power::impl::pixel::PowerSessionManager; using ::android::perfmgr::HintManager; constexpr std::string_view kPowerHalInitProp("vendor.powerhal.init"); -constexpr std::string_view kConfigDebugPathProperty("vendor.powerhal.config.debug"); -constexpr std::string_view kConfigProperty("vendor.powerhal.config"); -constexpr std::string_view kConfigDefaultFileName("powerhint.json"); int main() { - std::string config_path = "/vendor/etc/"; - if (android::base::GetBoolProperty(kConfigDebugPathProperty.data(), false)) { - config_path = "/data/vendor/etc/"; - LOG(WARNING) << "Power HAL AIDL Service for moto_sm6375 is using debug config from: " << config_path; - } - config_path.append( - android::base::GetProperty(kConfigProperty.data(), kConfigDefaultFileName.data())); - - LOG(INFO) << "Power HAL AIDL Service for moto_sm6375 with Extension is starting with config: " - << config_path; - // Parse config but do not start the looper - std::shared_ptr hm = HintManager::GetFromJSON(config_path, false); + std::shared_ptr hm = HintManager::GetInstance(); if (!hm) { - LOG(FATAL) << "Invalid config: " << config_path; + LOG(FATAL) << "HintManager Init failed"; } // single thread ABinderProcess_setThreadPoolMaxThreadCount(0); // core service - std::shared_ptr pw = ndk::SharedRefBase::make(hm); + std::shared_ptr pw = ndk::SharedRefBase::make(); ndk::SpAIBinder pwBinder = pw->asBinder(); // extension service - std::shared_ptr pwExt = ndk::SharedRefBase::make(hm); + std::shared_ptr pwExt = ndk::SharedRefBase::make(); // attach the extension to the same binder we will be registering CHECK(STATUS_OK == AIBinder_setExtension(pwBinder.get(), pwExt->asBinder().get())); @@ -76,12 +63,11 @@ int main() { if (::android::base::GetIntProperty("vendor.powerhal.adpf.rate", -1) != -1) { PowerHintMonitor::getInstance()->start(); - PowerSessionManager::getInstance()->setHintManager(hm); } std::thread initThread([&]() { ::android::base::WaitForProperty(kPowerHalInitProp.data(), "1"); - hm->Start(); + HintManager::GetInstance()->Start(); }); initThread.detach();