• 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 <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 
40 namespace OHOS {
41 namespace MiscServices {
42 static std::vector<std::string> NEED_RECOVER_ON_REBOOT = { "not_support" };
43 
44 class TimerManager : public ITimerManager {
45 public:
46     int32_t CreateTimer(TimerPara &paras,
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(int32_t uid, 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     ~TimerManager() override;
67     void HandleRSSDeath();
68     static TimerManager* GetInstance();
69 
70 private:
71     explicit TimerManager(std::shared_ptr<TimerHandler> impl);
72     void TimerLooper();
73 
74     void SetHandler(std::string name,
75                     uint64_t id,
76                     int type,
77                     uint64_t triggerAtTime,
78                     int64_t windowLength,
79                     uint64_t interval,
80                     int flag,
81                     bool autoRestore,
82                     std::function<int32_t (const uint64_t)> callback,
83                     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent,
84                     int uid,
85                     int pid,
86                     const std::string &bundleName);
87     void SetHandlerLocked(std::string name,
88                           uint64_t id,
89                           int type,
90                           std::chrono::milliseconds when,
91                           std::chrono::steady_clock::time_point whenElapsed,
92                           std::chrono::milliseconds windowLength,
93                           std::chrono::steady_clock::time_point maxWhen,
94                           std::chrono::milliseconds interval,
95                           std::function<int32_t (const uint64_t)> callback,
96                           const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> &wantAgent,
97                           uint32_t flags,
98                           bool autoRestore,
99                           uint64_t callingUid,
100                           uint64_t callingPid,
101                           const std::string &bundleName);
102     void RemoveHandler(uint64_t id);
103     void RemoveLocked(uint64_t id, bool needReschedule);
104     void ReBatchAllTimers();
105     void ReAddTimerLocked(std::shared_ptr<TimerInfo> timer,
106                           std::chrono::steady_clock::time_point nowElapsed);
107     void ReCalcuOriWhenElapsed(std::shared_ptr<TimerInfo> timer,
108                           std::chrono::steady_clock::time_point nowElapsed);
109     void SetHandlerLocked(std::shared_ptr<TimerInfo> alarm, bool rebatching, bool isRebatched);
110     void InsertAndBatchTimerLocked(std::shared_ptr<TimerInfo> alarm);
111     int64_t AttemptCoalesceLocked(std::chrono::steady_clock::time_point whenElapsed,
112                                   std::chrono::steady_clock::time_point maxWhen);
113     void TriggerIdleTimer();
114     bool ProcTriggerTimer(std::shared_ptr<TimerInfo> &alarm,
115                           const std::chrono::steady_clock::time_point &nowElapsed);
116     bool TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &triggerList,
117                              std::chrono::steady_clock::time_point nowElapsed);
118     void RescheduleKernelTimerLocked();
119     void DeliverTimersLocked(const std::vector<std::shared_ptr<TimerInfo>> &triggerList);
120     void NotifyWantAgentRetry(std::shared_ptr<TimerInfo> timer);
121     std::shared_ptr<Batch> FindFirstWakeupBatchLocked();
122     void SetLocked(int type, std::chrono::nanoseconds when, std::chrono::steady_clock::time_point bootTime);
123     std::chrono::steady_clock::time_point ConvertToElapsed(std::chrono::milliseconds when, int type);
124     std::chrono::steady_clock::time_point GetBootTimeNs();
125     int32_t StopTimerInner(uint64_t timerNumber, bool needDestroy);
126     int32_t StopTimerInnerLocked(bool needDestroy, uint64_t timerNumber, bool &needRecover);
127     void UpdateOrDeleteDatabase(bool needDestroy, uint64_t timerNumber, bool needRecover);
128     int32_t CheckUserIdForNotify(const std::shared_ptr<TimerInfo> &timer);
129     bool NotifyWantAgent(const std::shared_ptr<TimerInfo> &timer);
130     bool CheckAllowWhileIdle(const std::shared_ptr<TimerInfo> &alarm);
131     bool AdjustDeliveryTimeBasedOnDeviceIdle(const std::shared_ptr<TimerInfo> &alarm);
132     bool AdjustTimersBasedOnDeviceIdle();
133     void HandleRepeatTimer(const std::shared_ptr<TimerInfo> &timer, std::chrono::steady_clock::time_point nowElapsed);
134     inline bool CheckNeedRecoverOnReboot(std::string bundleName, int type, bool autoRestore);
135     #ifdef POWER_MANAGER_ENABLE
136     void HandleRunningLock(const std::shared_ptr<Batch> &firstWakeup);
137     void AddRunningLock(long long holdLockTime);
138     void AddRunningLockRetry(long long holdLockTime);
139     #endif
140 
141     void UpdateTimersState(std::shared_ptr<TimerInfo> &alarm);
142     bool AdjustSingleTimer(std::shared_ptr<TimerInfo> timer);
143     void IncreaseTimerCount(int uid);
144     void DecreaseTimerCount(int uid);
145     void CheckTimerCount();
146     void ShowTimerCountByUid(int count);
147     void AddTimerName(int uid, std::string name, uint64_t timerId);
148     void DeleteTimerName(int uid, std::string name, uint64_t timerId);
149 
150     std::map<uint64_t, std::shared_ptr<TimerEntry>> timerEntryMap_;
151     // vector<uid, count>
152     std::vector<std::pair<int32_t, int32_t>> timerCount_;
153     // <uid, <name, timerid>>
154     std::map<int32_t, std::map<std::string, uint64_t>> timerNameMap_;
155     std::default_random_engine random_;
156     std::atomic_bool runFlag_;
157     std::shared_ptr<TimerHandler> handler_;
158     std::unique_ptr<std::thread> alarmThread_;
159     std::vector<std::shared_ptr<Batch>> alarmBatches_;
160     std::mutex mutex_;
161     std::mutex entryMapMutex_;
162     std::mutex timerMapMutex_;
163     std::chrono::system_clock::time_point lastTimeChangeClockTime_;
164     std::chrono::steady_clock::time_point lastTimeChangeRealtime_;
165     static std::mutex instanceLock_;
166     static TimerManager* instance_;
167 
168     std::vector<std::shared_ptr<TimerInfo>> pendingDelayTimers_;
169     // map<timerId, original trigger time> for delayed timers
170     std::map<uint64_t, std::chrono::steady_clock::time_point> delayedTimers_;
171     // idle timer
172     std::shared_ptr<TimerInfo> mPendingIdleUntil_;
173     bool adjustPolicy_ = false;
174     uint32_t adjustInterval_ = 0;
175     int64_t timerOutOfRangeTimes_ = 0;
176     std::chrono::steady_clock::time_point lastTimerOutOfRangeTime_;
177     #ifdef POWER_MANAGER_ENABLE
178     std::mutex runningLockMutex_;
179     std::shared_ptr<PowerMgr::RunningLock> runningLock_;
180     int64_t lockExpiredTime_ = 0;
181     #endif
182 }; // timer_manager
183 } // MiscServices
184 } // OHOS
185 
186 #endif