1 /* 2 * Copyright (C) 2021 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 <cinttypes> 19 #include <mutex> 20 #include <vector> 21 #include <map> 22 #include <random> 23 #include <thread> 24 #include <atomic> 25 #include <chrono> 26 #include <functional> 27 #include "batch.h" 28 #include "timer_handler.h" 29 #include "want_agent_helper.h" 30 31 namespace OHOS { 32 namespace MiscServices { 33 class TimerManager : public ITimerManager { 34 public: 35 static std::shared_ptr<TimerManager> Create(); 36 int32_t CreateTimer(TimerPara ¶s, 37 std::function<void (const uint64_t)> callback, 38 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent, 39 int uid, 40 uint64_t &timerId) override; 41 int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override; 42 int32_t StopTimer(uint64_t timerId) override; 43 int32_t DestroyTimer(uint64_t timerId) override; 44 bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override; 45 bool ResetAllProxy() override; 46 bool ShowtimerEntryMap(int fd); 47 bool ShowTimerEntryById(int fd, uint64_t timerId); 48 bool ShowTimerTriggerById(int fd, uint64_t timerId); 49 bool ShowIdleTimerInfo(int fd); 50 ~TimerManager() override; 51 void HandleRSSDeath(); 52 53 private: 54 explicit TimerManager(std::shared_ptr<TimerHandler> impl); 55 void TimerLooper(); 56 57 void SetHandler(uint64_t id, 58 int type, 59 uint64_t triggerAtTime, 60 uint64_t windowLength, 61 uint64_t interval, 62 int flag, 63 std::function<void (const uint64_t)> callback, 64 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent, 65 int uid); 66 void SetHandlerLocked(uint64_t id, 67 int type, 68 std::chrono::milliseconds when, 69 std::chrono::steady_clock::time_point whenElapsed, 70 std::chrono::milliseconds windowLength, 71 std::chrono::steady_clock::time_point maxWhen, 72 std::chrono::milliseconds interval, 73 std::function<void (const uint64_t)> callback, 74 const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> &wantAgent, 75 uint32_t flags, 76 bool doValidate, 77 uint64_t callingUid); 78 void RemoveHandler(uint64_t id); 79 void RemoveLocked(uint64_t id); 80 void ReBatchAllTimers(); 81 void ReBatchAllTimersLocked(bool doValidate); 82 void ReAddTimerLocked(std::shared_ptr<TimerInfo> timer, 83 std::chrono::steady_clock::time_point nowElapsed, 84 bool doValidate); 85 void SetHandlerLocked(std::shared_ptr<TimerInfo> alarm, bool rebatching, bool doValidate, 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 bool TriggerTimersLocked(std::vector<std::shared_ptr<TimerInfo>> &triggerList, 90 std::chrono::steady_clock::time_point nowElapsed); 91 void RescheduleKernelTimerLocked(); 92 void DeliverTimersLocked(const std::vector<std::shared_ptr<TimerInfo>> &triggerList, 93 std::chrono::steady_clock::time_point nowElapsed); 94 std::shared_ptr<Batch> FindFirstWakeupBatchLocked(); 95 void SetLocked(int type, std::chrono::nanoseconds when); 96 std::chrono::steady_clock::time_point ConvertToElapsed(std::chrono::milliseconds when, int type); 97 std::chrono::steady_clock::time_point GetBootTimeNs(); 98 void CallbackAlarmIfNeed(const std::shared_ptr<TimerInfo> &alarm); 99 int32_t StopTimerInner(uint64_t timerNumber, bool needDestroy); 100 void RemoveProxy(uint64_t timerNumber, int32_t uid); 101 void NotifyWantAgent(const std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> &wantAgent); 102 bool CheckAllowWhileIdle(uint32_t flag); 103 bool AdjustDeliveryTimeBasedOnDeviceIdle(const std::shared_ptr<TimerInfo> &alarm); 104 bool AdjustTimersBasedOnDeviceIdle(); 105 106 std::map<uint64_t, std::shared_ptr<TimerEntry>> timerEntryMap_; 107 std::default_random_engine random_; 108 std::atomic_bool runFlag_; 109 std::shared_ptr<TimerHandler> handler_; 110 std::unique_ptr<std::thread> alarmThread_; 111 std::vector<std::shared_ptr<Batch>> alarmBatches_; 112 std::mutex mutex_; 113 std::mutex entryMapMutex_; 114 std::mutex showTimerMutex_; 115 std::chrono::system_clock::time_point lastTimeChangeClockTime_; 116 std::chrono::steady_clock::time_point lastTimeChangeRealtime_; 117 118 // proxy uid 119 std::mutex proxyMutex_; 120 std::set<int32_t> proxyUids_; 121 std::map<int32_t, std::vector<std::shared_ptr<TimerInfo>>> proxyMap_; 122 123 std::vector<std::shared_ptr<TimerInfo>> pendingDelayTimers_; 124 // map<timerId, original trigger time> for delayed timers 125 std::map<uint64_t, std::chrono::steady_clock::time_point> delayedTimers_; 126 // idle timer 127 std::shared_ptr<TimerInfo> mPendingIdleUntil_; 128 std::mutex idleTimerMutex_; 129 }; // timer_manager 130 } // MiscServices 131 } // OHOS 132 133 #endif