From 36addfaf7c2b0ed96329a1a6dfff2d45bef45efe Mon Sep 17 00:00:00 2001 From: Wei Wang Date: Thu, 18 Jul 2024 12:56:42 +0530 Subject: [PATCH] sm6375-common: power-libperfmgr: improve adpf logic Cache active state and reduce log spam Add value tracing into libperfmgr Use adaptive stale timeout based on rate limit Bug: 191331719 Bug: 191296994 Bug: 177493042 Test: boot Signed-off-by: Wei Wang Change-Id: I1c1484c9277209bf68bd287ceae83e2b37684c62 --- power-libperfmgr/Power.cpp | 16 ++--- power-libperfmgr/Power.h | 2 +- power-libperfmgr/PowerHintSession.cpp | 83 ++++++++++++------------ power-libperfmgr/PowerHintSession.h | 14 ++-- power-libperfmgr/PowerSessionManager.cpp | 32 +++++---- power-libperfmgr/PowerSessionManager.h | 10 ++- 6 files changed, 84 insertions(+), 73 deletions(-) diff --git a/power-libperfmgr/Power.cpp b/power-libperfmgr/Power.cpp index 187ec8b..903af0d 100644 --- a/power-libperfmgr/Power.cpp +++ b/power-libperfmgr/Power.cpp @@ -55,7 +55,8 @@ Power::Power(std::shared_ptr hm) : mHintManager(hm), mInteractionHandler(nullptr), mSustainedPerfModeOn(false), - mAdpfRate(::android::base::GetIntProperty(kPowerHalAdpfRateProp, kPowerHalAdpfRateDefault)) { + mAdpfRateNs( + ::android::base::GetIntProperty(kPowerHalAdpfRateProp, kPowerHalAdpfRateDefault)) { mInteractionHandler = std::make_unique(mHintManager); mInteractionHandler->Init(); @@ -81,7 +82,7 @@ Power::Power(std::shared_ptr hm) } // Now start to take powerhint - LOG(INFO) << "PowerHAL ready to take hints, Adpf update rate: " << mAdpfRate; + LOG(INFO) << "PowerHAL ready to take hints, Adpf update rate: " << mAdpfRateNs; } ndk::ScopedAStatus Power::setMode(Mode type, bool enabled) { @@ -200,7 +201,6 @@ binder_status_t Power::dump(int fd, const char **, uint32_t) { if (!::android::base::WriteStringToFd(buf, fd)) { PLOG(ERROR) << "Failed to dump state to fd"; } - // TODO(jimmyshiu@): dump weak_ptr of PowerHintSession fsync(fd); return STATUS_OK; } @@ -209,7 +209,7 @@ ndk::ScopedAStatus Power::createHintSession(int32_t tgid, int32_t uid, const std::vector &threadIds, int64_t durationNanos, std::shared_ptr *_aidl_return) { - if (mAdpfRate == -1) { + if (mAdpfRateNs <= 0) { *_aidl_return = nullptr; return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } @@ -218,15 +218,15 @@ ndk::ScopedAStatus Power::createHintSession(int32_t tgid, int32_t uid, *_aidl_return = nullptr; return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); } - std::shared_ptr session = - ndk::SharedRefBase::make(tgid, uid, threadIds, durationNanos); + std::shared_ptr session = ndk::SharedRefBase::make( + tgid, uid, threadIds, durationNanos, nanoseconds(mAdpfRateNs)); *_aidl_return = session; return ndk::ScopedAStatus::ok(); } ndk::ScopedAStatus Power::getHintSessionPreferredRate(int64_t *outNanoseconds) { - *outNanoseconds = mAdpfRate; - if (mAdpfRate == -1) { + *outNanoseconds = mAdpfRateNs; + if (mAdpfRateNs <= 0) { return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); } diff --git a/power-libperfmgr/Power.h b/power-libperfmgr/Power.h index 6f4a938..888ca90 100644 --- a/power-libperfmgr/Power.h +++ b/power-libperfmgr/Power.h @@ -55,7 +55,7 @@ class Power : public ::aidl::android::hardware::power::BnPower { std::shared_ptr mHintManager; std::unique_ptr mInteractionHandler; std::atomic mSustainedPerfModeOn; - const int64_t mAdpfRate; + const int64_t mAdpfRateNs; }; } // namespace pixel diff --git a/power-libperfmgr/PowerHintSession.cpp b/power-libperfmgr/PowerHintSession.cpp index be27209..e75829b 100644 --- a/power-libperfmgr/PowerHintSession.cpp +++ b/power-libperfmgr/PowerHintSession.cpp @@ -51,7 +51,7 @@ constexpr char kPowerHalAdpfPidInitialIntegral[] = "vendor.powerhal.adpf.pid.i_i constexpr char kPowerHalAdpfUclampEnable[] = "vendor.powerhal.adpf.uclamp"; constexpr char kPowerHalAdpfUclampCapRatio[] = "vendor.powerhal.adpf.uclamp.cap_ratio"; constexpr char kPowerHalAdpfUclampGranularity[] = "vendor.powerhal.adpf.uclamp.granularity"; -constexpr char kPowerHalAdpfStaleTimeout[] = "vendor.powerhal.adpf.stale_timeout_ms"; +constexpr char kPowerHalAdpfStaleTimeFactor[] = "vendor.powerhal.adpf.stale_timeout_factor"; constexpr char kPowerHalAdpfSamplingWindow[] = "vendor.powerhal.adpf.sampling_window"; namespace { @@ -83,8 +83,8 @@ static inline void TRACE_ADPF_PID(uintptr_t session_id, int32_t uid, int32_t tgi int64_t err, int64_t integral, int64_t previous, int64_t p, int64_t i, int64_t d, int32_t output) { if (ATRACE_ENABLED()) { - std::string idstr = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR, tgid, uid, - session_id & 0xffff); + const std::string idstr = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR, tgid, uid, + session_id & 0xffff); std::string sz = StringPrintf("%s-pid.count", idstr.c_str()); ATRACE_INT(sz.c_str(), count); sz = StringPrintf("%s-pid.err", idstr.c_str()); @@ -104,11 +104,11 @@ static inline void TRACE_ADPF_PID(uintptr_t session_id, int32_t uid, int32_t tgi } } -static int64_t ns_to_100us(int64_t ns) { +static inline int64_t ns_to_100us(int64_t ns) { return ns / 100000; } -double getDoubleProperty(const char *prop, double value) { +static double getDoubleProperty(const char *prop, double value) { std::string result = ::android::base::GetProperty(prop, std::to_string(value).c_str()); if (!::android::base::ParseDouble(result.c_str(), &value)) { ALOGE("PowerHintSession : failed to parse double in %s", prop); @@ -134,51 +134,53 @@ static const int sUclampCap = static_cast(getDoubleProperty(kPowerHalAdpfUclampCapRatio, 0.5) * 1024); static const uint32_t sUclampGranularity = ::android::base::GetUintProperty(kPowerHalAdpfUclampGranularity, 5); -static const int64_t sStaleTimeoutMs = - ::android::base::GetIntProperty(kPowerHalAdpfStaleTimeout, 3000); +static const int64_t sStaleTimeFactor = + ::android::base::GetIntProperty(kPowerHalAdpfStaleTimeFactor, 20); static const size_t sSamplingWindow = ::android::base::GetUintProperty(kPowerHalAdpfSamplingWindow, 1); } // namespace PowerHintSession::PowerHintSession(int32_t tgid, int32_t uid, const std::vector &threadIds, - int64_t durationNanos) { + int64_t durationNanos, const nanoseconds adpfRate) + : kAdpfRate(adpfRate) { mDescriptor = new AppHintDesc(tgid, uid, threadIds, sUclampCap); mDescriptor->duration = std::chrono::nanoseconds(durationNanos); - mStaleHandler = sp(new StaleHandler(this, sStaleTimeoutMs)); + mStaleHandler = sp(new StaleHandler(this)); mPowerManagerHandler = PowerSessionManager::getInstance(); if (ATRACE_ENABLED()) { - std::string sz = - StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-target", mDescriptor->tgid, - mDescriptor->uid, reinterpret_cast(this) & 0xffff); + const std::string idstr = getIdString(); + std::string sz = StringPrintf("%s-target", idstr.c_str()); ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count()); - sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid, - mDescriptor->uid, reinterpret_cast(this) & 0xffff); + sz = StringPrintf("%s-active", idstr.c_str()); ATRACE_INT(sz.c_str(), mDescriptor->is_active.load()); } PowerSessionManager::getInstance()->addPowerSession(this); - ALOGD("PowerHintSession created: %s", mDescriptor->toString().c_str()); + ALOGV("PowerHintSession created: %s", mDescriptor->toString().c_str()); } PowerHintSession::~PowerHintSession() { close(); - ALOGD("PowerHintSession deleted: %s", mDescriptor->toString().c_str()); + ALOGV("PowerHintSession deleted: %s", mDescriptor->toString().c_str()); if (ATRACE_ENABLED()) { - std::string sz = - StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-target", mDescriptor->tgid, - mDescriptor->uid, reinterpret_cast(this) & 0xffff); + const std::string idstr = getIdString(); + std::string sz = StringPrintf("%s-target", idstr.c_str()); ATRACE_INT(sz.c_str(), 0); - sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-actl_last", mDescriptor->tgid, - mDescriptor->uid, reinterpret_cast(this) & 0xffff); + sz = StringPrintf("%s-actl_last", idstr.c_str()); ATRACE_INT(sz.c_str(), 0); - sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid, - mDescriptor->uid, reinterpret_cast(this) & 0xffff); + sz = sz = StringPrintf("%s-active", idstr.c_str()); ATRACE_INT(sz.c_str(), 0); } delete mDescriptor; } +std::string PowerHintSession::getIdString() const { + std::string idstr = StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR, mDescriptor->tgid, + mDescriptor->uid, reinterpret_cast(this) & 0xffff); + return idstr; +} + void PowerHintSession::updateUniveralBoostMode() { PowerHintMonitor::getInstance()->getLooper()->sendMessage(mPowerManagerHandler, NULL); } @@ -194,9 +196,6 @@ int PowerHintSession::setUclamp(int32_t min, int32_t max) { StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-min", mDescriptor->tgid, mDescriptor->uid, reinterpret_cast(this) & 0xffff); ATRACE_INT(sz.c_str(), min); - sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-max", mDescriptor->tgid, - mDescriptor->uid, reinterpret_cast(this) & 0xffff); - ATRACE_INT(sz.c_str(), max); } for (const auto tid : mDescriptor->threadIds) { sched_attr attr = {}; @@ -223,7 +222,7 @@ ndk::ScopedAStatus PowerHintSession::pause() { mDescriptor->is_active.store(false); if (ATRACE_ENABLED()) { std::string sz = - StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid, + StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid, mDescriptor->uid, reinterpret_cast(this) & 0xffff); ATRACE_INT(sz.c_str(), mDescriptor->is_active.load()); } @@ -238,7 +237,7 @@ ndk::ScopedAStatus PowerHintSession::resume() { mDescriptor->integral_error = std::max(sPidIInit, mDescriptor->integral_error); if (ATRACE_ENABLED()) { std::string sz = - StringPrintf("%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid, + StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-active", mDescriptor->tgid, mDescriptor->uid, reinterpret_cast(this) & 0xffff); ATRACE_INT(sz.c_str(), mDescriptor->is_active.load()); } @@ -303,25 +302,15 @@ ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration( int64_t targetDurationNanos = (int64_t)mDescriptor->duration.count(); size_t length = actualDurations.size(); size_t start = sSamplingWindow == 0 || sSamplingWindow > length ? 0 : length - sSamplingWindow; - int64_t actualDurationNanos = 0; int64_t dt = ns_to_100us(targetDurationNanos); int64_t error = 0; int64_t derivative = 0; for (size_t i = start; i < length; i++) { - actualDurationNanos = actualDurations[i].durationNanos; + int64_t actualDurationNanos = actualDurations[i].durationNanos; if (std::abs(actualDurationNanos) > targetDurationNanos * 20) { ALOGW("The actual duration is way far from the target (%" PRId64 " >> %" PRId64 ")", actualDurationNanos, targetDurationNanos); } - if (ATRACE_ENABLED()) { - std::string sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-actl_last", - mDescriptor->tgid, mDescriptor->uid, - reinterpret_cast(this) & 0xffff); - ATRACE_INT(sz.c_str(), actualDurationNanos); - sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-target", mDescriptor->tgid, - mDescriptor->uid, reinterpret_cast(this) & 0xffff); - ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count()); - } // PID control algorithm error = ns_to_100us(actualDurationNanos - targetDurationNanos) + static_cast(sPidOffset); @@ -331,6 +320,19 @@ ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration( derivative = (error - mDescriptor->previous_error) / dt; mDescriptor->previous_error = error; } + if (ATRACE_ENABLED()) { + std::string sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-actl_last", + mDescriptor->tgid, mDescriptor->uid, + reinterpret_cast(this) & 0xffff); + ATRACE_INT(sz.c_str(), actualDurations[length - 1].durationNanos); + sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-target", mDescriptor->tgid, + mDescriptor->uid, reinterpret_cast(this) & 0xffff); + ATRACE_INT(sz.c_str(), (int64_t)mDescriptor->duration.count()); + sz = StringPrintf("adpf.%" PRId32 "-%" PRId32 "-%" PRIxPTR "-sample_size", + mDescriptor->tgid, mDescriptor->uid, + reinterpret_cast(this) & 0xffff); + ATRACE_INT(sz.c_str(), length); + } int64_t pOut = static_cast(sPidP * error); int64_t iOut = static_cast(sPidI * mDescriptor->integral_error); int64_t dOut = static_cast(sPidD * derivative); @@ -418,7 +420,8 @@ void PowerHintSession::StaleHandler::updateStaleTimer() { } time_point PowerHintSession::StaleHandler::getStaleTime() { - return mLastUpdatedTime.load() + kStaleTimeout; + return mLastUpdatedTime.load() + + std::chrono::duration_cast(mSession->kAdpfRate) * sStaleTimeFactor; } void PowerHintSession::StaleHandler::handleMessage(const Message &) { diff --git a/power-libperfmgr/PowerHintSession.h b/power-libperfmgr/PowerHintSession.h index 4a2e817..e850634 100644 --- a/power-libperfmgr/PowerHintSession.h +++ b/power-libperfmgr/PowerHintSession.h @@ -68,8 +68,8 @@ struct AppHintDesc { class PowerHintSession : public BnPowerHintSession { public: - PowerHintSession(int32_t tgid, int32_t uid, const std::vector &threadIds, - int64_t durationNanos); + explicit PowerHintSession(int32_t tgid, int32_t uid, const std::vector &threadIds, + int64_t durationNanos, nanoseconds adpfRate); ~PowerHintSession(); ndk::ScopedAStatus close() override; ndk::ScopedAStatus pause() override; @@ -83,17 +83,13 @@ class PowerHintSession : public BnPowerHintSession { private: class StaleHandler : public MessageHandler { public: - StaleHandler(PowerHintSession *session, int64_t timeout_ms) - : kStaleTimeout(timeout_ms), - mSession(session), - mIsMonitoringStale(false), - mLastUpdatedTime(steady_clock::now()) {} + StaleHandler(PowerHintSession *session) + : mSession(session), mIsMonitoringStale(false), mLastUpdatedTime(steady_clock::now()) {} void handleMessage(const Message &message) override; void updateStaleTimer(); time_point getStaleTime(); private: - const milliseconds kStaleTimeout; PowerHintSession *mSession; std::atomic mIsMonitoringStale; std::atomic> mLastUpdatedTime; @@ -104,10 +100,12 @@ class PowerHintSession : public BnPowerHintSession { void setStale(); void updateUniveralBoostMode(); int setUclamp(int32_t max, int32_t min); + std::string getIdString() const; AppHintDesc *mDescriptor = nullptr; sp mStaleHandler; sp mPowerManagerHandler; std::mutex mLock; + const nanoseconds kAdpfRate; }; } // namespace pixel diff --git a/power-libperfmgr/PowerSessionManager.cpp b/power-libperfmgr/PowerSessionManager.cpp index 79dea6e..ff853ff 100644 --- a/power-libperfmgr/PowerSessionManager.cpp +++ b/power-libperfmgr/PowerSessionManager.cpp @@ -37,7 +37,7 @@ void PowerSessionManager::setHintManager(std::shared_ptr const &hin } void PowerSessionManager::updateHintMode(const std::string &mode, bool enabled) { - ALOGD("PowerSessionManager::updateHintMode: mode: %s, enabled: %d", mode.c_str(), enabled); + ALOGV("PowerSessionManager::updateHintMode: mode: %s, enabled: %d", mode.c_str(), enabled); if (enabled && mode.compare(0, 8, "REFRESH_") == 0) { if (mode.compare("REFRESH_120FPS") == 0) { mDisplayRefreshRate = 120; @@ -63,19 +63,31 @@ void PowerSessionManager::removePowerSession(PowerHintSession *session) { mSessions.erase(session); } -bool PowerSessionManager::isAnySessionActive() { +std::optional PowerSessionManager::isAnySessionActive() { std::lock_guard guard(mLock); + bool active = false; for (PowerHintSession *s : mSessions) { // session active and not stale is actually active. if (s->isActive() && !s->isStale()) { - return true; + active = true; + break; } } - return false; + if (active == mActive) { + return std::nullopt; + } else { + mActive = active; + } + + return active; } void PowerSessionManager::handleMessage(const Message &) { - if (isAnySessionActive()) { + auto active = isAnySessionActive(); + if (!active.has_value()) { + return; + } + if (active.value()) { disableSystemTopAppBoost(); } else { enableSystemTopAppBoost(); @@ -84,20 +96,14 @@ void PowerSessionManager::handleMessage(const Message &) { void PowerSessionManager::enableSystemTopAppBoost() { if (mHintManager) { - ALOGD("PowerSessionManager::enableSystemTopAppBoost!!"); - if (ATRACE_ENABLED()) { - ATRACE_INT(kDisableBoostHintName.c_str(), 0); - } + ALOGV("PowerSessionManager::enableSystemTopAppBoost!!"); mHintManager->EndHint(kDisableBoostHintName); } } void PowerSessionManager::disableSystemTopAppBoost() { if (mHintManager) { - ALOGD("PowerSessionManager::disableSystemTopAppBoost!!"); - if (ATRACE_ENABLED()) { - ATRACE_INT(kDisableBoostHintName.c_str(), 1); - } + ALOGV("PowerSessionManager::disableSystemTopAppBoost!!"); mHintManager->DoHint(kDisableBoostHintName); } } diff --git a/power-libperfmgr/PowerSessionManager.h b/power-libperfmgr/PowerSessionManager.h index 006a565..a705b68 100644 --- a/power-libperfmgr/PowerSessionManager.h +++ b/power-libperfmgr/PowerSessionManager.h @@ -23,6 +23,7 @@ #include #include +#include #include namespace aidl { @@ -48,7 +49,7 @@ class PowerSessionManager : public MessageHandler { // monitoring session status void addPowerSession(PowerHintSession *session); void removePowerSession(PowerHintSession *session); - bool isAnySessionActive(); + void handleMessage(const Message &message) override; void setHintManager(std::shared_ptr const &hint_manager); @@ -59,19 +60,22 @@ class PowerSessionManager : public MessageHandler { } private: + std::optional isAnySessionActive(); void disableSystemTopAppBoost(); void enableSystemTopAppBoost(); const std::string kDisableBoostHintName; std::shared_ptr mHintManager; - std::unordered_set mSessions; + std::unordered_set mSessions; // protected by mLock std::mutex mLock; int mDisplayRefreshRate; + bool mActive; // protected by mLock // Singleton PowerSessionManager() : kDisableBoostHintName(::android::base::GetProperty(kPowerHalAdpfDisableTopAppBoost, "ADPF_DISABLE_TA_BOOST")), mHintManager(nullptr), - mDisplayRefreshRate(60) {} + mDisplayRefreshRate(60), + mActive(false) {} PowerSessionManager(PowerSessionManager const &) = delete; void operator=(PowerSessionManager const &) = delete; };