• 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 
28 #include "batch.h"
29 #include "timer_handler.h"
30 #include "want_agent_helper.h"
31 
32 #ifdef POWER_MANAGER_ENABLE
33 #include "power_mgr_client.h"
34 #endif
35 
36 namespace OHOS {
37 namespace MiscServices {
38 class TimerManager : public ITimerManager {
39 public:
40     static std::shared_ptr<TimerManager> Create();
41     int32_t CreateTimer(TimerPara &paras,
42                         std::function<void (const uint64_t)> callback,
43                         std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent,
44                         int uid,
45                         uint64_t &timerId) override;
46     int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override;
47     int32_t StopTimer(uint64_t timerId) override;
48     int32_t DestroyTimer(uint64_t timerId) override;
49     bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override;
50     bool ResetAllProxy() override;
51     bool ShowTimerEntryMap(int fd);
52     bool ShowTimerEntryById(int fd, uint64_t timerId);
53     bool ShowTimerTriggerById(int fd, uint64_t timerId);
54     bool ShowIdleTimerInfo(int fd);
55     ~TimerManager() override;
56     void HandleRSSDeath();
57 
58 private:
59     explicit TimerManager(std::shared_ptr<TimerHandler> impl);
60     void TimerLooper();
61 
62     void SetHandler(uint64_t id,
63                     int type,
64                     uint64_t triggerAtTime,
65                     uint64_t windowLength,
66                     uint64_t interval,
67                     int flag,
68                     std::function<void (const uint64_t)> callback,
69                     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent,
70                     int uid,
71                     const std::string &bundleName);
72     void SetHandlerLocked(uint64_t id,
73                           int type,
74                           std::chrono::milliseconds when,
75                           std::chrono::steady_clock::time_point whenElapsed,
76                           std::chrono::milliseconds windowLength,
77                           std::chrono::steady_clock::time_point maxWhen,
78                           std::chrono::milliseconds interval,
79                           std::function<void (const uint64_t)> callback,
80                           const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> &wantAgent,
81                           uint32_t flags,
82                           bool doValidate,
83                           uint64_t callingUid,
84                           const std::string &bundleName);
85     void RemoveHandler(uint64_t id);
86     void RemoveLocked(uint64_t id);
87     void ReBatchAllTimers();
88     void ReBatchAllTimersLocked(bool doValidate);
89     void ReAddTimerLocked(std::shared_ptr<TimerInfo> timer,
90                           std::chrono::steady_clock::time_point nowElapsed,
91                           bool doValidate);
92     void SetHandlerLocked(std::shared_ptr<TimerInfo> alarm, bool rebatching, bool doValidate, bool isRebatched);
93     void InsertAndBatchTimerLocked(std::shared_ptr<TimerInfo> alarm);
94     int64_t AttemptCoalesceLocked(std::chrono::steady_clock::time_point whenElapsed,
95                                   std::chrono::steady_clock::time_point maxWhen);
96     void TriggerIdleTimer();
97     void ProcTriggerTimer(std::shared_ptr<TimerInfo> &alarm,
98         std::vector<std::shared_ptr<TimerInfo>> &triggerList, const std::chrono::steady_clock::time_point &nowElapsed);
99     bool TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &triggerList,
100                              std::chrono::steady_clock::time_point nowElapsed);
101     void RescheduleKernelTimerLocked();
102     void DeliverTimersLocked(const std::vector<std::shared_ptr<TimerInfo>> &triggerList);
103     std::shared_ptr<Batch> FindFirstWakeupBatchLocked();
104     void SetLocked(int type, std::chrono::nanoseconds when);
105     std::chrono::steady_clock::time_point ConvertToElapsed(std::chrono::milliseconds when, int type);
106     std::chrono::steady_clock::time_point GetBootTimeNs();
107     int32_t StopTimerInner(uint64_t timerNumber, bool needDestroy);
108     void NotifyWantAgent(const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> &wantAgent);
109     bool CheckAllowWhileIdle(const std::shared_ptr<TimerInfo> &alarm);
110     bool AdjustDeliveryTimeBasedOnDeviceIdle(const std::shared_ptr<TimerInfo> &alarm);
111     bool AdjustTimersBasedOnDeviceIdle();
112     void HandleRepeatTimer(const std::shared_ptr<TimerInfo> &timer, std::chrono::steady_clock::time_point nowElapsed);
113     #ifdef POWER_MANAGER_ENABLE
114     void HandleRunningLock(const std::shared_ptr<Batch> &firstWakeup);
115     void AddRunningLock(long long holdLockTime);
116     #endif
117 
118     void UpdateTimersState(std::shared_ptr<TimerInfo> &alarm);
119 
120     std::map<uint64_t, std::shared_ptr<TimerEntry>> timerEntryMap_;
121     std::default_random_engine random_;
122     std::atomic_bool runFlag_;
123     std::shared_ptr<TimerHandler> handler_;
124     std::unique_ptr<std::thread> alarmThread_;
125     std::vector<std::shared_ptr<Batch>> alarmBatches_;
126     std::mutex mutex_;
127     std::mutex entryMapMutex_;
128     std::mutex showTimerMutex_;
129     std::chrono::system_clock::time_point lastTimeChangeClockTime_;
130     std::chrono::steady_clock::time_point lastTimeChangeRealtime_;
131 
132     std::vector<std::shared_ptr<TimerInfo>> pendingDelayTimers_;
133     // map<timerId, original trigger time> for delayed timers
134     std::map<uint64_t, std::chrono::steady_clock::time_point> delayedTimers_;
135     // idle timer
136     std::shared_ptr<TimerInfo> mPendingIdleUntil_;
137     std::mutex idleTimerMutex_;
138     #ifdef POWER_MANAGER_ENABLE
139     std::shared_ptr<PowerMgr::RunningLock> runningLock_;
140     int64_t lockExpiredTime_ = 0;
141     #endif
142 }; // timer_manager
143 } // MiscServices
144 } // OHOS
145 
146 #endif