diff --git a/services/timer/include/timer_manager.h b/services/timer/include/timer_manager.h index 7f0f722c17f58c579640c20e0040094c8875f822..e8915cd05058018090309f4d40e480630a2d17b3 100644 --- a/services/timer/include/timer_manager.h +++ b/services/timer/include/timer_manager.h @@ -170,6 +170,7 @@ private: std::map delayedTimers_; // idle timer std::shared_ptr mPendingIdleUntil_; + std::array lastSetTime_ = {0}; bool adjustPolicy_ = false; uint32_t adjustInterval_ = 0; int64_t timerOutOfRangeTimes_ = 0; diff --git a/services/timer/include/timer_manager_interface.h b/services/timer/include/timer_manager_interface.h index 79a3544b3493691455a903f051118dbbae2b7112..512ef5f73e78f95ce32716874c7be7d70bd1d44b 100644 --- a/services/timer/include/timer_manager_interface.h +++ b/services/timer/include/timer_manager_interface.h @@ -54,7 +54,8 @@ public: RTC_WAKEUP = 0, RTC = 1, ELAPSED_REALTIME_WAKEUP = 2, - ELAPSED_REALTIME = 3 + ELAPSED_REALTIME = 3, + TIMER_TYPE_BUTT }; virtual int32_t CreateTimer(TimerPara ¶s, diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 9804ce7f27a8a3d740e95cdabbcc57fdef9ddfca..deaed375de8b59a069528f8303024f4a378d88d4 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -858,7 +858,6 @@ bool TimerManager::TriggerTimersLocked(std::vector> & // needs to acquire the lock `mutex_` before calling this method void TimerManager::RescheduleKernelTimerLocked() { - auto nextNonWakeup = std::chrono::steady_clock::time_point::min(); auto bootTime = GetBootTimeNs(); if (!alarmBatches_.empty()) { auto firstWakeup = FindFirstWakeupBatchLocked(); @@ -867,16 +866,21 @@ void TimerManager::RescheduleKernelTimerLocked() #ifdef POWER_MANAGER_ENABLE HandleRunningLock(firstWakeup); #endif - SetLocked(ELAPSED_REALTIME_WAKEUP, firstWakeup->GetStart().time_since_epoch(), bootTime); + auto setTimePoint = firstWakeup->GetStart().time_since_epoch(); + if (setTimePoint < bootTime.time_since_epoch() || + setTimePoint.count() != lastSetTime_[ELAPSED_REALTIME_WAKEUP]) { + SetLocked(ELAPSED_REALTIME_WAKEUP, setTimePoint, bootTime); + lastSetTime_[ELAPSED_REALTIME_WAKEUP] = setTimePoint.count(); + } } if (firstBatch != firstWakeup) { - nextNonWakeup = firstBatch->GetStart(); + auto setTimePoint = firstBatch->GetStart().time_since_epoch(); + if (setTimePoint < bootTime.time_since_epoch() || setTimePoint.count() != lastSetTime_[ELAPSED_REALTIME]) { + SetLocked(ELAPSED_REALTIME, setTimePoint, bootTime); + lastSetTime_[ELAPSED_REALTIME] = setTimePoint.count(); + } } } - - if (nextNonWakeup != std::chrono::steady_clock::time_point::min()) { - SetLocked(ELAPSED_REALTIME, nextNonWakeup.time_since_epoch(), bootTime); - } } // needs to acquire the lock `mutex_` before calling this method @@ -1409,10 +1413,13 @@ void TimerManager::HandleRepeatTimer( uint64_t count = 1 + static_cast( duration_cast(nowElapsed - timer->whenElapsed) / timer->repeatInterval); auto delta = count * timer->repeatInterval; - auto nextElapsed = timer->whenElapsed + delta; + steady_clock::time_point nextElapsed = timer->whenElapsed + delta; + steady_clock::time_point nextMaxElapsed = (timer->windowLength == milliseconds::zero()) ? + nextElapsed : + MaxTriggerTime(nowElapsed, nextElapsed, timer->repeatInterval); SetHandlerLocked(timer->name, timer->id, timer->type, timer->when + delta, nextElapsed, timer->windowLength, - MaxTriggerTime(nowElapsed, nextElapsed, timer->repeatInterval), timer->repeatInterval, timer->callback, - timer->wantAgent, timer->flags, timer->autoRestore, timer->uid, timer->pid, timer->bundleName); + nextMaxElapsed, timer->repeatInterval, timer->callback, timer->wantAgent, timer->flags, timer->autoRestore, + timer->uid, timer->pid, timer->bundleName); } else { TimerProxy::GetInstance().RemoveUidTimerMap(timer); TimerProxy::GetInstance().RemovePidTimerMap(timer);