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_H 17 #define RELIABILITY_WATCHDOG_H 18 19 #include <string> 20 #include "event_handler.h" 21 #include "singleton.h" 22 23 using Task = std::function<void()>; 24 using TimeOutCallback = std::function<void(const std::string &name, int waitState)>; 25 26 typedef void (*WatchdogBeginFunc)(const char* eventName); 27 typedef void (*WatchdogEndFunc)(const char* eventName); 28 29 namespace OHOS { 30 namespace HiviewDFX { 31 class Watchdog : public Singleton<Watchdog> { 32 DECLARE_SINGLETON(Watchdog); 33 static const uint64_t WATCHDOG_TIMEVAL = 30000; 34 public: 35 36 /** 37 * Add handler to watchdog thread with customized check interval 38 * 39 * @param name, the name of handler check task 40 * @param handler, the handler to be checked periodically 41 * @param timeOutCallback, callback when timeout 42 * @param interval, the period in millisecond 43 * @return 0 if added 44 * 45 */ 46 int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler, 47 TimeOutCallback timeOutCallback = nullptr, uint64_t interval = WATCHDOG_TIMEVAL); 48 49 /** 50 * Add handler to watchdog thread with customized check interval 51 * 52 * @param name, the name of handler check task 53 * @param handler, the handler to be checked periodically 54 * @param interval, the period in millisecond 55 * @return 0 if added 56 * 57 */ 58 int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler, uint64_t interval); 59 60 /** 61 * Run a onshot task in shared watchdog thread, the submitted task should never be time consuming 62 * 63 * @param name, task name 64 * @param task, a short functiona 65 * @param delay, delay a few millisecond to run the task 66 * 67 */ 68 void RunOneShotTask(const std::string& name, Task&& task, uint64_t delay = 0); 69 70 /** 71 * Run a periodical task in shared watchdog thread 72 * 73 * @param name, task name 74 * @param task, a short functiona 75 * @param interval, the millisecond interval of the periodical task 76 * @param delay, delay a few millisecond to first run the task 77 * 78 */ 79 void RunPeriodicalTask(const std::string& name, Task&& task, uint64_t interval, uint64_t delay = 0); 80 81 /** 82 * stop watchdog thread and wait for cleanup synchronously 83 * 84 */ 85 void StopWatchdog(); 86 87 /** 88 * watchdog adapt ffrt and not influence ipc full, hungtask delete watchdog, merge xcollie and watchdog. 89 * 90 */ 91 void InitFfrtWatchdog(); 92 93 /** 94 * 95 * @brief Set bundle info. 96 * 97 */ 98 void SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion); 99 100 /** 101 * 102 * @brief Set foreground. 103 * 104 */ 105 void SetForeground(const bool& isForeground); 106 107 /** 108 * @brief Remove a periodical task by name. 109 * 110 * @param name, task name 111 */ 112 void RemovePeriodicalTask(const std::string& name); 113 114 /** 115 * @brief Remove the thread by name. 116 * 117 * @param name: task name 118 */ 119 void RemoveThread(const std::string& name); 120 121 /** 122 * @brief Init MainLooperWatcher in bussiness watchdog thread. 123 */ 124 void InitMainLooperWatcher(WatchdogBeginFunc* beginFunc, WatchdogEndFunc* endFunc); 125 126 /** 127 * @brief Set isAppDebug. 128 */ 129 void SetAppDebug(bool isAppDebug); 130 131 /** 132 * @brief Get isAppDebug. 133 */ 134 bool GetAppDebug(); 135 }; 136 } // end of namespace HiviewDFX 137 } // end of namespace OHOS 138 #endif 139 140