1cf69771bSopenharmony_ci/* 2cf69771bSopenharmony_ci * Copyright (C) 2021-2023 Huawei Device Co., Ltd. 3cf69771bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cf69771bSopenharmony_ci * you may not use this file except in compliance with the License. 5cf69771bSopenharmony_ci * You may obtain a copy of the License at 6cf69771bSopenharmony_ci * 7cf69771bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cf69771bSopenharmony_ci * 9cf69771bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cf69771bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cf69771bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cf69771bSopenharmony_ci * See the License for the specific language governing permissions and 13cf69771bSopenharmony_ci * limitations under the License. 14cf69771bSopenharmony_ci */ 15cf69771bSopenharmony_ci#ifndef TIMER_MANAGER_H 16cf69771bSopenharmony_ci#define TIMER_MANAGER_H 17cf69771bSopenharmony_ci 18cf69771bSopenharmony_ci#include <atomic> 19cf69771bSopenharmony_ci#include <chrono> 20cf69771bSopenharmony_ci#include <cinttypes> 21cf69771bSopenharmony_ci#include <functional> 22cf69771bSopenharmony_ci#include <map> 23cf69771bSopenharmony_ci#include <mutex> 24cf69771bSopenharmony_ci#include <random> 25cf69771bSopenharmony_ci#include <thread> 26cf69771bSopenharmony_ci#include <vector> 27cf69771bSopenharmony_ci#include <unordered_set> 28cf69771bSopenharmony_ci#include <utility> 29cf69771bSopenharmony_ci 30cf69771bSopenharmony_ci#include "batch.h" 31cf69771bSopenharmony_ci#include "timer_handler.h" 32cf69771bSopenharmony_ci#include "want_agent_helper.h" 33cf69771bSopenharmony_ci#include "time_common.h" 34cf69771bSopenharmony_ci 35cf69771bSopenharmony_ci#ifdef POWER_MANAGER_ENABLE 36cf69771bSopenharmony_ci#include "completed_callback.h" 37cf69771bSopenharmony_ci#include "power_mgr_client.h" 38cf69771bSopenharmony_ci#endif 39cf69771bSopenharmony_ci 40cf69771bSopenharmony_cinamespace OHOS { 41cf69771bSopenharmony_cinamespace MiscServices { 42cf69771bSopenharmony_cistatic std::vector<std::string> NEED_RECOVER_ON_REBOOT = { "not_support" }; 43cf69771bSopenharmony_ci 44cf69771bSopenharmony_ciclass TimerManager : public ITimerManager { 45cf69771bSopenharmony_cipublic: 46cf69771bSopenharmony_ci int32_t CreateTimer(TimerPara ¶s, 47cf69771bSopenharmony_ci std::function<int32_t (const uint64_t)> callback, 48cf69771bSopenharmony_ci std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent, 49cf69771bSopenharmony_ci int uid, 50cf69771bSopenharmony_ci int pid, 51cf69771bSopenharmony_ci uint64_t &timerId, 52cf69771bSopenharmony_ci DatabaseType type) override; 53cf69771bSopenharmony_ci void ReCreateTimer(uint64_t timerId, std::shared_ptr<TimerEntry> timerInfo); 54cf69771bSopenharmony_ci int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override; 55cf69771bSopenharmony_ci int32_t StopTimer(uint64_t timerId) override; 56cf69771bSopenharmony_ci int32_t DestroyTimer(uint64_t timerId) override; 57cf69771bSopenharmony_ci bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override; 58cf69771bSopenharmony_ci bool ProxyTimer(std::set<int> pidList, bool isProxy, bool needRetrigger) override; 59cf69771bSopenharmony_ci bool AdjustTimer(bool isAdjust, uint32_t interval) override; 60cf69771bSopenharmony_ci void SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption) override; 61cf69771bSopenharmony_ci bool ResetAllProxy() override; 62cf69771bSopenharmony_ci bool ShowTimerEntryMap(int fd); 63cf69771bSopenharmony_ci bool ShowTimerEntryById(int fd, uint64_t timerId); 64cf69771bSopenharmony_ci bool ShowTimerTriggerById(int fd, uint64_t timerId); 65cf69771bSopenharmony_ci bool ShowIdleTimerInfo(int fd); 66cf69771bSopenharmony_ci void OnUserRemoved(int userId); 67cf69771bSopenharmony_ci ~TimerManager() override; 68cf69771bSopenharmony_ci void HandleRSSDeath(); 69cf69771bSopenharmony_ci static TimerManager* GetInstance(); 70cf69771bSopenharmony_ci 71cf69771bSopenharmony_ciprivate: 72cf69771bSopenharmony_ci explicit TimerManager(std::shared_ptr<TimerHandler> impl); 73cf69771bSopenharmony_ci void TimerLooper(); 74cf69771bSopenharmony_ci 75cf69771bSopenharmony_ci void SetHandler(uint64_t id, 76cf69771bSopenharmony_ci int type, 77cf69771bSopenharmony_ci uint64_t triggerAtTime, 78cf69771bSopenharmony_ci int64_t windowLength, 79cf69771bSopenharmony_ci uint64_t interval, 80cf69771bSopenharmony_ci int flag, 81cf69771bSopenharmony_ci std::function<int32_t (const uint64_t)> callback, 82cf69771bSopenharmony_ci std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent, 83cf69771bSopenharmony_ci int uid, 84cf69771bSopenharmony_ci int pid, 85cf69771bSopenharmony_ci const std::string &bundleName); 86cf69771bSopenharmony_ci void SetHandlerLocked(uint64_t id, 87cf69771bSopenharmony_ci int type, 88cf69771bSopenharmony_ci std::chrono::milliseconds when, 89cf69771bSopenharmony_ci std::chrono::steady_clock::time_point whenElapsed, 90cf69771bSopenharmony_ci std::chrono::milliseconds windowLength, 91cf69771bSopenharmony_ci std::chrono::steady_clock::time_point maxWhen, 92cf69771bSopenharmony_ci std::chrono::milliseconds interval, 93cf69771bSopenharmony_ci std::function<int32_t (const uint64_t)> callback, 94cf69771bSopenharmony_ci const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> &wantAgent, 95cf69771bSopenharmony_ci uint32_t flags, 96cf69771bSopenharmony_ci uint64_t callingUid, 97cf69771bSopenharmony_ci uint64_t callingPid, 98cf69771bSopenharmony_ci const std::string &bundleName); 99cf69771bSopenharmony_ci void RemoveHandler(uint64_t id); 100cf69771bSopenharmony_ci void RemoveLocked(uint64_t id, bool needReschedule); 101cf69771bSopenharmony_ci void ReBatchAllTimers(); 102cf69771bSopenharmony_ci void ReAddTimerLocked(std::shared_ptr<TimerInfo> timer, 103cf69771bSopenharmony_ci std::chrono::steady_clock::time_point nowElapsed); 104cf69771bSopenharmony_ci void ReCalcuOriWhenElapsed(std::shared_ptr<TimerInfo> timer, 105cf69771bSopenharmony_ci std::chrono::steady_clock::time_point nowElapsed); 106cf69771bSopenharmony_ci void SetHandlerLocked(std::shared_ptr<TimerInfo> alarm, bool rebatching, bool isRebatched); 107cf69771bSopenharmony_ci void InsertAndBatchTimerLocked(std::shared_ptr<TimerInfo> alarm); 108cf69771bSopenharmony_ci int64_t AttemptCoalesceLocked(std::chrono::steady_clock::time_point whenElapsed, 109cf69771bSopenharmony_ci std::chrono::steady_clock::time_point maxWhen); 110cf69771bSopenharmony_ci void TriggerIdleTimer(); 111cf69771bSopenharmony_ci bool ProcTriggerTimer(std::shared_ptr<TimerInfo> &alarm, 112cf69771bSopenharmony_ci const std::chrono::steady_clock::time_point &nowElapsed); 113cf69771bSopenharmony_ci bool TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &triggerList, 114cf69771bSopenharmony_ci std::chrono::steady_clock::time_point nowElapsed); 115cf69771bSopenharmony_ci void RescheduleKernelTimerLocked(); 116cf69771bSopenharmony_ci void DeliverTimersLocked(const std::vector<std::shared_ptr<TimerInfo>> &triggerList); 117cf69771bSopenharmony_ci void NotifyWantAgentRetry(std::shared_ptr<TimerInfo> timer); 118cf69771bSopenharmony_ci std::shared_ptr<Batch> FindFirstWakeupBatchLocked(); 119cf69771bSopenharmony_ci void SetLocked(int type, std::chrono::nanoseconds when, std::chrono::steady_clock::time_point bootTime); 120cf69771bSopenharmony_ci std::chrono::steady_clock::time_point ConvertToElapsed(std::chrono::milliseconds when, int type); 121cf69771bSopenharmony_ci std::chrono::steady_clock::time_point GetBootTimeNs(); 122cf69771bSopenharmony_ci int32_t StopTimerInner(uint64_t timerNumber, bool needDestroy); 123cf69771bSopenharmony_ci int32_t CheckUserIdForNotify(const std::shared_ptr<TimerInfo> &timer); 124cf69771bSopenharmony_ci bool NotifyWantAgent(const std::shared_ptr<TimerInfo> &timer); 125cf69771bSopenharmony_ci bool CheckAllowWhileIdle(const std::shared_ptr<TimerInfo> &alarm); 126cf69771bSopenharmony_ci bool AdjustDeliveryTimeBasedOnDeviceIdle(const std::shared_ptr<TimerInfo> &alarm); 127cf69771bSopenharmony_ci bool AdjustTimersBasedOnDeviceIdle(); 128cf69771bSopenharmony_ci void HandleRepeatTimer(const std::shared_ptr<TimerInfo> &timer, std::chrono::steady_clock::time_point nowElapsed); 129cf69771bSopenharmony_ci inline bool CheckNeedRecoverOnReboot(std::string bundleName, int type); 130cf69771bSopenharmony_ci #ifdef POWER_MANAGER_ENABLE 131cf69771bSopenharmony_ci void HandleRunningLock(const std::shared_ptr<Batch> &firstWakeup); 132cf69771bSopenharmony_ci void AddRunningLock(long long holdLockTime); 133cf69771bSopenharmony_ci void AddRunningLockRetry(long long holdLockTime); 134cf69771bSopenharmony_ci #endif 135cf69771bSopenharmony_ci 136cf69771bSopenharmony_ci void UpdateTimersState(std::shared_ptr<TimerInfo> &alarm); 137cf69771bSopenharmony_ci bool AdjustSingleTimer(std::shared_ptr<TimerInfo> timer); 138cf69771bSopenharmony_ci 139cf69771bSopenharmony_ci std::map<uint64_t, std::shared_ptr<TimerEntry>> timerEntryMap_; 140cf69771bSopenharmony_ci std::default_random_engine random_; 141cf69771bSopenharmony_ci std::atomic_bool runFlag_; 142cf69771bSopenharmony_ci std::shared_ptr<TimerHandler> handler_; 143cf69771bSopenharmony_ci std::unique_ptr<std::thread> alarmThread_; 144cf69771bSopenharmony_ci std::vector<std::shared_ptr<Batch>> alarmBatches_; 145cf69771bSopenharmony_ci std::mutex mutex_; 146cf69771bSopenharmony_ci std::mutex entryMapMutex_; 147cf69771bSopenharmony_ci std::chrono::system_clock::time_point lastTimeChangeClockTime_; 148cf69771bSopenharmony_ci std::chrono::steady_clock::time_point lastTimeChangeRealtime_; 149cf69771bSopenharmony_ci static std::mutex instanceLock_; 150cf69771bSopenharmony_ci static TimerManager* instance_; 151cf69771bSopenharmony_ci 152cf69771bSopenharmony_ci std::vector<std::shared_ptr<TimerInfo>> pendingDelayTimers_; 153cf69771bSopenharmony_ci // map<timerId, original trigger time> for delayed timers 154cf69771bSopenharmony_ci std::map<uint64_t, std::chrono::steady_clock::time_point> delayedTimers_; 155cf69771bSopenharmony_ci // idle timer 156cf69771bSopenharmony_ci std::shared_ptr<TimerInfo> mPendingIdleUntil_; 157cf69771bSopenharmony_ci bool adjustPolicy_ = false; 158cf69771bSopenharmony_ci uint32_t adjustInterval_ = 0; 159cf69771bSopenharmony_ci #ifdef POWER_MANAGER_ENABLE 160cf69771bSopenharmony_ci std::mutex runningLockMutex_; 161cf69771bSopenharmony_ci std::shared_ptr<PowerMgr::RunningLock> runningLock_; 162cf69771bSopenharmony_ci int64_t lockExpiredTime_ = 0; 163cf69771bSopenharmony_ci #endif 164cf69771bSopenharmony_ci}; // timer_manager 165cf69771bSopenharmony_ci} // MiscServices 166cf69771bSopenharmony_ci} // OHOS 167cf69771bSopenharmony_ci 168cf69771bSopenharmony_ci#endif