• 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     #ifdef SET_AUTO_REBOOT_ENABLE
72     std::vector<std::string> powerOnApps_;
73     #endif
74 
75 private:
76     explicit TimerManager(std::shared_ptr<TimerHandler> impl);
77     void TimerLooper();
78 
79     void SetHandlerLocked(std::shared_ptr<TimerInfo> timer);
80     void RemoveHandler(uint64_t id);
81     void RemoveLocked(uint64_t id, bool needReschedule);
82     void ReBatchAllTimers();
83     void ReAddTimerLocked(std::shared_ptr<TimerInfo> timer,
84                           std::chrono::steady_clock::time_point nowElapsed);
85     void SetHandlerLocked(std::shared_ptr<TimerInfo> alarm, bool rebatching, bool isRebatched);
86     void InsertAndBatchTimerLocked(std::shared_ptr<TimerInfo> alarm);
87     int64_t AttemptCoalesceLocked(std::chrono::steady_clock::time_point whenElapsed,
88                                   std::chrono::steady_clock::time_point maxWhen);
89     void TriggerIdleTimer();
90     bool ProcTriggerTimer(std::shared_ptr<TimerInfo> &alarm,
91                           const std::chrono::steady_clock::time_point &nowElapsed);
92     bool TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &triggerList,
93                              std::chrono::steady_clock::time_point nowElapsed);
94     void RescheduleKernelTimerLocked();
95     void DeliverTimersLocked(const std::vector<std::shared_ptr<TimerInfo>> &triggerList);
96     void NotifyWantAgentRetry(std::shared_ptr<TimerInfo> timer);
97     std::shared_ptr<Batch> FindFirstWakeupBatchLocked();
98     void SetLocked(int type, std::chrono::nanoseconds when, std::chrono::steady_clock::time_point bootTime);
99     int32_t StopTimerInner(uint64_t timerNumber, bool needDestroy);
100     int32_t StopTimerInnerLocked(bool needDestroy, uint64_t timerNumber, bool &needRecover);
101     void UpdateOrDeleteDatabase(bool needDestroy, uint64_t timerNumber, bool needRecover);
102     #ifdef MULTI_ACCOUNT_ENABLE
103     int32_t CheckUserIdForNotify(const std::shared_ptr<TimerInfo> &timer);
104     #endif
105     bool NotifyWantAgent(const std::shared_ptr<TimerInfo> &timer);
106     bool CheckAllowWhileIdle(const std::shared_ptr<TimerInfo> &alarm);
107     bool AdjustDeliveryTimeBasedOnDeviceIdle(const std::shared_ptr<TimerInfo> &alarm);
108     bool AdjustTimersBasedOnDeviceIdle();
109     void HandleRepeatTimer(const std::shared_ptr<TimerInfo> &timer, std::chrono::steady_clock::time_point nowElapsed);
110     inline bool CheckNeedRecoverOnReboot(std::string bundleName, int type, bool autoRestore);
111     #ifdef POWER_MANAGER_ENABLE
112     void HandleRunningLock(const std::shared_ptr<Batch> &firstWakeup);
113     void AddRunningLock(long long holdLockTime);
114     void AddRunningLockRetry(long long holdLockTime);
115     #endif
116 
117     void UpdateTimersState(std::shared_ptr<TimerInfo> &alarm, bool needRetrigger);
118     bool AdjustSingleTimer(std::shared_ptr<TimerInfo> timer);
119     bool AdjustSingleTimerLocked(std::shared_ptr<TimerInfo> timer);
120     void IncreaseTimerCount(int uid);
121     void DecreaseTimerCount(int uid);
122     void CheckTimerCount();
123     void ShowTimerCountByUid(int count);
124     void AddTimerName(int uid, std::string name, uint64_t timerId);
125     void DeleteTimerName(int uid, std::string name, uint64_t timerId);
126     #ifdef SET_AUTO_REBOOT_ENABLE
127     bool IsPowerOnTimer(std::shared_ptr<TimerInfo> timerInfo);
128     void DeleteTimerFromPowerOnTimerListById(uint64_t timerId);
129     void ReschedulePowerOnTimerLocked();
130     #endif
131 
132     std::map<uint64_t, std::shared_ptr<TimerEntry>> timerEntryMap_;
133     // vector<uid, count>
134     std::vector<std::pair<int32_t, int32_t>> timerCount_;
135     // <uid, <name, timerid>>
136     std::map<int32_t, std::map<std::string, uint64_t>> timerNameMap_;
137     std::default_random_engine random_;
138     std::atomic_bool runFlag_;
139     std::shared_ptr<TimerHandler> handler_;
140     std::unique_ptr<std::thread> alarmThread_;
141     std::vector<std::shared_ptr<Batch>> alarmBatches_;
142     std::mutex mutex_;
143     std::mutex entryMapMutex_;
144     std::mutex timerMapMutex_;
145     std::chrono::system_clock::time_point lastTimeChangeClockTime_;
146     std::chrono::steady_clock::time_point lastTimeChangeRealtime_;
147     static std::mutex instanceLock_;
148     static TimerManager* instance_;
149 
150     std::vector<std::shared_ptr<TimerInfo>> pendingDelayTimers_;
151     // map<timerId, original trigger time> for delayed timers
152     std::map<uint64_t, std::chrono::steady_clock::time_point> delayedTimers_;
153     // idle timer
154     std::shared_ptr<TimerInfo> mPendingIdleUntil_;
155     std::array<int64_t, TIMER_TYPE_BUTT> lastSetTime_ = {0};
156     bool adjustPolicy_ = false;
157     uint32_t adjustInterval_ = 0;
158     uint32_t adjustDelta_ = 0;
159     int64_t timerOutOfRangeTimes_ = 0;
160     std::chrono::steady_clock::time_point lastTimerOutOfRangeTime_;
161     #ifdef SET_AUTO_REBOOT_ENABLE
162     std::vector<std::shared_ptr<TimerInfo>> powerOnTriggerTimerList_;
163     #endif
164     #ifdef POWER_MANAGER_ENABLE
165     std::mutex runningLockMutex_;
166     std::shared_ptr<PowerMgr::RunningLock> runningLock_;
167     int64_t lockExpiredTime_ = 0;
168     #endif
169 }; // timer_manager
170 } // MiscServices
171 } // OHOS
172 
173 #endif