1 /* 2 * Copyright (c) 2021-2022 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 16 #ifndef RELIABILITY_WATCHDOG_INNER_H 17 #define RELIABILITY_WATCHDOG_INNER_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <memory> 22 #include <mutex> 23 #include <queue> 24 #include <set> 25 #include <string> 26 #include <thread> 27 28 #include "watchdog_task.h" 29 #include "c/ffrt_watchdog.h" 30 #include "singleton.h" 31 32 namespace OHOS { 33 namespace HiviewDFX { 34 class WatchdogInner : public Singleton<WatchdogInner> { 35 DECLARE_SINGLETON(WatchdogInner); 36 public: 37 static const int XCOLLIE_CALLBACK_HISTORY_MAX = 5; 38 static const int XCOLLIE_CALLBACK_TIMEWIN_MAX = 60; 39 std::map<int64_t, int> taskIdCnt; 40 int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler, uint64_t interval); 41 int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler, 42 TimeOutCallback timeOutCallback, uint64_t interval); 43 void RunOneShotTask(const std::string& name, Task&& task, uint64_t delay); 44 void RunPeriodicalTask(const std::string& name, Task&& task, uint64_t interval, uint64_t delay); 45 int64_t RunXCollieTask(const std::string& name, uint64_t timeout, XCollieCallback func, void *arg, 46 unsigned int flag); 47 void RemoveXCollieTask(int64_t id); 48 void StopWatchdog(); 49 bool IsCallbackLimit(unsigned int flag); 50 void IpcCheck(); 51 void InitFfrtWatchdog(); 52 static void WriteStringToFile(uint32_t pid, const char *str); 53 static void FfrtCallback(uint64_t taskId, const char *taskInfo, uint32_t delayedTaskCount); 54 static void SendFfrtEvent(const std::string &msg, const std::string &eventName, const char *taskInfo); 55 static void LeftTimeExitProcess(const std::string &description); 56 std::string currentScene_; 57 58 private: 59 bool Start(); 60 bool Stop(); 61 bool SendMsgToHungtask(const std::string& msg); 62 bool KickWatchdog(); 63 bool IsTaskExistLocked(const std::string& name); 64 bool IsExceedMaxTaskLocked(); 65 int64_t InsertWatchdogTaskLocked(const std::string& name, WatchdogTask&& task); 66 uint64_t FetchNextTask(uint64_t now, WatchdogTask& task); 67 void ReInsertTaskIfNeed(WatchdogTask& task); 68 void CreateWatchdogThreadIfNeed(); 69 70 static const unsigned int MAX_WATCH_NUM = 128; // 128: max handler thread 71 std::priority_queue<WatchdogTask> checkerQueue_; // protected by lock_ 72 std::unique_ptr<std::thread> threadLoop_; 73 std::mutex lock_; 74 static std::mutex lockFfrt_; 75 std::condition_variable condition_; 76 std::atomic_bool isNeedStop_ = false; 77 std::once_flag flag_; 78 std::set<std::string> taskNameSet_; 79 std::shared_ptr<AppExecFwk::EventHandler> binderCheckHander_; 80 int cntCallback_; 81 time_t timeCallback_; 82 bool isHmos = false; 83 }; 84 } // end of namespace HiviewDFX 85 } // end of namespace OHOS 86 #endif 87