• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <random>
19 #include <thread>
20 #include <cinttypes>
21 
22 #include "batch.h"
23 #include "timer_handler.h"
24 
25 #ifdef POWER_MANAGER_ENABLE
26 #include "completed_callback.h"
27 #include "power_mgr_client.h"
28 #endif
29 
30 #ifdef MULTI_ACCOUNT_ENABLE
31 #include <utility>
32 #endif
33 
34 namespace OHOS {
35 namespace MiscServices {
36 static std::vector<std::string> NEED_RECOVER_ON_REBOOT = { "not_support" };
37 
38 class TimerManager : public ITimerManager {
39 public:
40     int32_t CreateTimer(TimerPara &paras,
41                         std::function<int32_t (const uint64_t)> callback,
42                         std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent,
43                         int uid,
44                         int pid,
45                         uint64_t &timerId,
46                         DatabaseType type) override;
47     void ReCreateTimer(uint64_t timerId, std::shared_ptr<TimerEntry> timerInfo);
48     int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override;
49     int32_t StopTimer(uint64_t timerId) override;
50     int32_t DestroyTimer(uint64_t timerId) override;
51     bool ProxyTimer(int32_t uid, std::set<int> pidList, bool isProxy, bool needRetrigger) override;
52     bool AdjustTimer(bool isAdjust, uint32_t interval, uint32_t delta) override;
53     void SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption) override;
54     bool ResetAllProxy() override;
55     #ifndef RDB_ENABLE
56     int32_t StartTimerGroup(std::vector<std::pair<uint64_t, uint64_t>> timerVec, std::string tableName);
57     #endif
58     #ifdef HIDUMPER_ENABLE
59     bool ShowTimerEntryMap(int fd);
60     bool ShowTimerEntryById(int fd, uint64_t timerId);
61     bool ShowTimerTriggerById(int fd, uint64_t timerId);
62     bool ShowIdleTimerInfo(int fd);
63     #endif
64     #ifdef MULTI_ACCOUNT_ENABLE
65     void OnUserRemoved(int userId);
66     #endif
67     void OnPackageRemoved(int uid);
68     ~TimerManager() override;
69     void HandleRSSDeath();
70     static TimerManager* GetInstance();
71 
72 private:
73     explicit TimerManager(std::shared_ptr<TimerHandler> impl);
74     void TimerLooper();
75 
76     void SetHandler(std::string name,
77                     uint64_t id,
78                     int type,
79                     uint64_t triggerAtTime,
80                     int64_t windowLength,
81                     uint64_t interval,
82                     uint32_t flag,
83                     bool autoRestore,
84                     std::function<int32_t (const uint64_t)> callback,
85                     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent,
86                     int uid,
87                     int pid,
88                     const std::string &bundleName);
89     void SetHandlerLocked(std::string name,
90                           uint64_t id,
91                           int type,
92                           std::chrono::milliseconds when,
93                           std::chrono::steady_clock::time_point whenElapsed,
94                           std::chrono::milliseconds windowLength,
95                           std::chrono::steady_clock::time_point maxWhen,
96                           std::chrono::milliseconds interval,
97                           std::function<int32_t (const uint64_t)> callback,
98                           const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> &wantAgent,
99                           uint32_t flags,
100                           bool autoRestore,
101                           uint64_t callingUid,
102                           uint64_t callingPid,
103                           const std::string &bundleName);
104     void RemoveHandler(uint64_t id);
105     void RemoveLocked(uint64_t id, bool needReschedule);
106     void ReBatchAllTimers();
107     void ReAddTimerLocked(std::shared_ptr<TimerInfo> timer,
108                           std::chrono::steady_clock::time_point nowElapsed);
109     void ReCalcuOriWhenElapsed(std::shared_ptr<TimerInfo> timer,
110                           std::chrono::steady_clock::time_point nowElapsed);
111     void SetHandlerLocked(std::shared_ptr<TimerInfo> alarm, bool rebatching, bool isRebatched);
112     void InsertAndBatchTimerLocked(std::shared_ptr<TimerInfo> alarm);
113     int64_t AttemptCoalesceLocked(std::chrono::steady_clock::time_point whenElapsed,
114                                   std::chrono::steady_clock::time_point maxWhen);
115     void TriggerIdleTimer();
116     bool ProcTriggerTimer(std::shared_ptr<TimerInfo> &alarm,
117                           const std::chrono::steady_clock::time_point &nowElapsed);
118     bool TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &triggerList,
119                              std::chrono::steady_clock::time_point nowElapsed);
120     void RescheduleKernelTimerLocked();
121     void DeliverTimersLocked(const std::vector<std::shared_ptr<TimerInfo>> &triggerList);
122     void NotifyWantAgentRetry(std::shared_ptr<TimerInfo> timer);
123     std::shared_ptr<Batch> FindFirstWakeupBatchLocked();
124     void SetLocked(int type, std::chrono::nanoseconds when, std::chrono::steady_clock::time_point bootTime);
125     std::chrono::steady_clock::time_point ConvertToElapsed(std::chrono::milliseconds when, int type);
126     std::chrono::steady_clock::time_point GetBootTimeNs();
127     int32_t StopTimerInner(uint64_t timerNumber, bool needDestroy);
128     int32_t StopTimerInnerLocked(bool needDestroy, uint64_t timerNumber, bool &needRecover);
129     void UpdateOrDeleteDatabase(bool needDestroy, uint64_t timerNumber, bool needRecover);
130     #ifdef MULTI_ACCOUNT_ENABLE
131     int32_t CheckUserIdForNotify(const std::shared_ptr<TimerInfo> &timer);
132     #endif
133     bool NotifyWantAgent(const std::shared_ptr<TimerInfo> &timer);
134     bool CheckAllowWhileIdle(const std::shared_ptr<TimerInfo> &alarm);
135     bool AdjustDeliveryTimeBasedOnDeviceIdle(const std::shared_ptr<TimerInfo> &alarm);
136     bool AdjustTimersBasedOnDeviceIdle();
137     void HandleRepeatTimer(const std::shared_ptr<TimerInfo> &timer, std::chrono::steady_clock::time_point nowElapsed);
138     inline bool CheckNeedRecoverOnReboot(std::string bundleName, int type, bool autoRestore);
139     #ifdef POWER_MANAGER_ENABLE
140     void HandleRunningLock(const std::shared_ptr<Batch> &firstWakeup);
141     void AddRunningLock(long long holdLockTime);
142     void AddRunningLockRetry(long long holdLockTime);
143     #endif
144 
145     void UpdateTimersState(std::shared_ptr<TimerInfo> &alarm, bool needRetrigger);
146     bool AdjustSingleTimer(std::shared_ptr<TimerInfo> timer);
147     bool AdjustSingleTimerLocked(std::shared_ptr<TimerInfo> timer);
148     void IncreaseTimerCount(int uid);
149     void DecreaseTimerCount(int uid);
150     void CheckTimerCount();
151     void ShowTimerCountByUid(int count);
152     void AddTimerName(int uid, std::string name, uint64_t timerId);
153     void DeleteTimerName(int uid, std::string name, uint64_t timerId);
154 
155     std::map<uint64_t, std::shared_ptr<TimerEntry>> timerEntryMap_;
156     // vector<uid, count>
157     std::vector<std::pair<int32_t, int32_t>> timerCount_;
158     // <uid, <name, timerid>>
159     std::map<int32_t, std::map<std::string, uint64_t>> timerNameMap_;
160     std::default_random_engine random_;
161     std::atomic_bool runFlag_;
162     std::shared_ptr<TimerHandler> handler_;
163     std::unique_ptr<std::thread> alarmThread_;
164     std::vector<std::shared_ptr<Batch>> alarmBatches_;
165     std::mutex mutex_;
166     std::mutex entryMapMutex_;
167     std::mutex timerMapMutex_;
168     std::chrono::system_clock::time_point lastTimeChangeClockTime_;
169     std::chrono::steady_clock::time_point lastTimeChangeRealtime_;
170     static std::mutex instanceLock_;
171     static TimerManager* instance_;
172 
173     std::vector<std::shared_ptr<TimerInfo>> pendingDelayTimers_;
174     // map<timerId, original trigger time> for delayed timers
175     std::map<uint64_t, std::chrono::steady_clock::time_point> delayedTimers_;
176     // idle timer
177     std::shared_ptr<TimerInfo> mPendingIdleUntil_;
178     std::array<int64_t, TIMER_TYPE_BUTT> lastSetTime_ = {0};
179     bool adjustPolicy_ = false;
180     uint32_t adjustInterval_ = 0;
181     uint32_t adjustDelta_ = 0;
182     int64_t timerOutOfRangeTimes_ = 0;
183     std::chrono::steady_clock::time_point lastTimerOutOfRangeTime_;
184     #ifdef POWER_MANAGER_ENABLE
185     std::mutex runningLockMutex_;
186     std::shared_ptr<PowerMgr::RunningLock> runningLock_;
187     int64_t lockExpiredTime_ = 0;
188     #endif
189 }; // timer_manager
190 } // MiscServices
191 } // OHOS
192 
193 #endif