diff --git a/power-libperfmgr/aidl/PowerHintSession.cpp b/power-libperfmgr/aidl/PowerHintSession.cpp index 564cbb22..84263d99 100644 --- a/power-libperfmgr/aidl/PowerHintSession.cpp +++ b/power-libperfmgr/aidl/PowerHintSession.cpp @@ -346,7 +346,6 @@ ndk::ScopedAStatus PowerHintSession::reportActualWorkDuration( mDescriptor->current_min + static_cast(output)); next_min = std::max(static_cast(adpfConfig->mUclampMinLow), next_min); setSessionUclampMin(next_min); - mStaleTimerHandler->updateTimer(getStaleTime()); mAdaptiveCpu->ReportWorkDurations(actualDurations, mDescriptor->duration); @@ -380,7 +379,12 @@ bool PowerHintSession::isActive() { bool PowerHintSession::isTimeout() { auto now = std::chrono::steady_clock::now(); - return now >= getStaleTime(); + time_point staleTime = + mLastUpdatedTime.load() + + nanoseconds(static_cast( + mDescriptor->duration.count() * + HintManager::GetInstance()->GetAdpfProfile()->mStaleTimeFactor)); + return now >= staleTime; } const std::vector &PowerHintSession::getTidList() const { @@ -399,31 +403,19 @@ void PowerHintSession::setStale() { } } -time_point PowerHintSession::getStaleTime() { - return mLastUpdatedTime.load() + - nanoseconds(static_cast( - mDescriptor->duration.count() * - HintManager::GetInstance()->GetAdpfProfile()->mStaleTimeFactor)); -} - void PowerHintSession::StaleTimerHandler::updateTimer() { - time_point staleTime = - std::chrono::steady_clock::now() + - nanoseconds(static_cast( - mSession->mDescriptor->duration.count() * - HintManager::GetInstance()->GetAdpfProfile()->mStaleTimeFactor)); - updateTimer(staleTime); -} - -void PowerHintSession::StaleTimerHandler::updateTimer(time_point staleTime) { - mStaleTime.store(staleTime); + auto now = std::chrono::steady_clock::now(); + nanoseconds staleDuration = std::chrono::nanoseconds( + static_cast(mSession->mDescriptor->duration.count() * + HintManager::GetInstance()->GetAdpfProfile()->mStaleTimeFactor)); + mStaleTime.store(now + staleDuration); + int64_t next = static_cast(staleDuration.count()); { std::lock_guard guard(mMessageLock); PowerHintMonitor::getInstance()->getLooper()->removeMessages(mSession->mStaleTimerHandler); - PowerHintMonitor::getInstance()->getLooper()->sendMessage(mSession->mStaleTimerHandler, - NULL); + PowerHintMonitor::getInstance()->getLooper()->sendMessageDelayed( + next, mSession->mStaleTimerHandler, NULL); } - mIsMonitoring.store(true); if (ATRACE_ENABLED()) { const std::string idstr = mSession->getIdString(); std::string sz = StringPrintf("adpf.%s-timer.stale", idstr.c_str()); @@ -446,12 +438,11 @@ void PowerHintSession::StaleTimerHandler::handleMessage(const Message &) { next, mSession->mStaleTimerHandler, NULL); } else { mSession->setStale(); - mIsMonitoring.store(false); } if (ATRACE_ENABLED()) { const std::string idstr = mSession->getIdString(); std::string sz = StringPrintf("adpf.%s-timer.stale", idstr.c_str()); - ATRACE_INT(sz.c_str(), mIsMonitoring ? 0 : 1); + ATRACE_INT(sz.c_str(), next > 0 ? 0 : 1); } } diff --git a/power-libperfmgr/aidl/PowerHintSession.h b/power-libperfmgr/aidl/PowerHintSession.h index 2002020b..aa6f8fc4 100644 --- a/power-libperfmgr/aidl/PowerHintSession.h +++ b/power-libperfmgr/aidl/PowerHintSession.h @@ -93,10 +93,8 @@ class PowerHintSession : public BnPowerHintSession { private: class StaleTimerHandler : public MessageHandler { public: - StaleTimerHandler(PowerHintSession *session) - : mSession(session), mIsMonitoring(false), mIsSessionDead(false) {} + StaleTimerHandler(PowerHintSession *session) : mSession(session), mIsSessionDead(false) {} void updateTimer(); - void updateTimer(time_point staleTime); void handleMessage(const Message &message) override; void setSessionDead(); @@ -105,7 +103,6 @@ class PowerHintSession : public BnPowerHintSession { std::mutex mStaleLock; std::mutex mMessageLock; std::atomic> mStaleTime; - std::atomic mIsMonitoring; bool mIsSessionDead; };