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