From 4abe7f5fd600ce4cd12a369a65320b87d75af883 Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Tue, 5 Nov 2024 16:17:43 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[fix]=20=E4=BF=AE=E6=94=B9=E5=90=91?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E8=AE=BE=E7=BD=AE=E6=97=B6=E9=97=B4=E5=A4=9A?= =?UTF-8?q?=E6=AC=A1=E8=AE=BE=E7=BD=AE=E5=90=8C=E4=B8=80=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- services/timer/include/timer_manager.h | 1 + .../timer/include/timer_manager_interface.h | 3 ++- services/timer/src/timer_manager.cpp | 17 ++++++++++------- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/services/timer/include/timer_manager.h b/services/timer/include/timer_manager.h index 7f0f722c..e8915cd0 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 79a3544b..512ef5f7 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 9804ce7f..c08ee15c 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,20 @@ 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.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.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 -- Gitee From e05a64dfaa9dc4edc3deef4d5558dfcf23aec316 Mon Sep 17 00:00:00 2001 From: gaozhichao Date: Wed, 5 Mar 2025 14:40:08 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[fix]=20=E4=BF=AE=E6=94=B9=E5=BD=B1?= =?UTF-8?q?=E5=93=8Dtime=5Ftick=E6=AD=A3=E5=B8=B8=E5=8F=91=E9=80=81?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaozhichao --- services/timer/src/timer_manager.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index c08ee15c..deaed375 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -867,14 +867,15 @@ void TimerManager::RescheduleKernelTimerLocked() HandleRunningLock(firstWakeup); #endif auto setTimePoint = firstWakeup->GetStart().time_since_epoch(); - if (setTimePoint.count() != lastSetTime_[ELAPSED_REALTIME_WAKEUP]) { + 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) { auto setTimePoint = firstBatch->GetStart().time_since_epoch(); - if (setTimePoint.count() != lastSetTime_[ELAPSED_REALTIME]) { + if (setTimePoint < bootTime.time_since_epoch() || setTimePoint.count() != lastSetTime_[ELAPSED_REALTIME]) { SetLocked(ELAPSED_REALTIME, setTimePoint, bootTime); lastSetTime_[ELAPSED_REALTIME] = setTimePoint.count(); } @@ -1412,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); -- Gitee