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 <map> 20 #include <string> 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 AppExecFwk { 31 class EventHandler; 32 } 33 34 namespace HiviewDFX { 35 class Watchdog : public Singleton<Watchdog> { 36 DECLARE_SINGLETON(Watchdog); 37 static const uint64_t WATCHDOG_TIMEVAL = 30000; 38 public: 39 40 /** 41 * Add handler to watchdog thread with customized check interval 42 * 43 * @param name, the name of handler check task 44 * @param handler, the handler to be checked periodically 45 * @param timeOutCallback, callback when timeout 46 * @param interval, the period in millisecond 47 * @return 0 if added 48 * 49 */ 50 int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler, 51 TimeOutCallback timeOutCallback = nullptr, uint64_t interval = WATCHDOG_TIMEVAL); 52 53 /** 54 * Add handler to watchdog thread with customized check interval 55 * 56 * @param name, the name of handler check task 57 * @param handler, the handler to be checked periodically 58 * @param interval, the period in millisecond 59 * @return 0 if added 60 * 61 */ 62 int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler, uint64_t interval); 63 64 /** 65 * Run a onshot task in shared watchdog thread, the submitted task should never be time consuming 66 * 67 * @param name, task name 68 * @param task, a short functiona 69 * @param delay, delay a few millisecond to run the task 70 * 71 */ 72 void RunOneShotTask(const std::string& name, Task&& task, uint64_t delay = 0); 73 74 /** 75 * Run a periodical task in shared watchdog thread 76 * 77 * @param name, task name 78 * @param task, a short functiona 79 * @param interval, the millisecond interval of the periodical task 80 * @param delay, delay a few millisecond to first run the task 81 * 82 */ 83 void RunPeriodicalTask(const std::string& name, Task&& task, uint64_t interval, uint64_t delay = 0); 84 85 /** 86 * stop watchdog thread and wait for cleanup synchronously 87 * 88 */ 89 void StopWatchdog(); 90 91 /** 92 * watchdog adapt ffrt and not influence ipc full, hungtask delete watchdog, merge xcollie and watchdog. 93 * 94 */ 95 void InitFfrtWatchdog(); 96 97 /** 98 * 99 * @brief Set bundle info. 100 * 101 */ 102 void SetBundleInfo(const std::string& bundleName, const std::string& bundleVersion); 103 104 /** 105 * 106 * @brief Set foreground. 107 * 108 */ 109 void SetForeground(const bool& isForeground); 110 111 /** 112 * 113 * @brief Get foreground. 114 * 115 */ 116 bool GetForeground(); 117 118 /** 119 * @brief Remove a periodical task by name. 120 * 121 * @param name, task name 122 */ 123 void RemovePeriodicalTask(const std::string& name); 124 125 /** 126 * @brief Remove the thread by name. 127 * 128 * @param name: task name 129 */ 130 void RemoveThread(const std::string& name); 131 132 /** 133 * @brief Init MainLooperWatcher in bussiness watchdog thread. 134 */ 135 void InitMainLooperWatcher(WatchdogBeginFunc* beginFunc, WatchdogEndFunc* endFunc); 136 137 /** 138 * @brief Set isAppDebug. 139 */ 140 void SetAppDebug(bool isAppDebug); 141 142 /** 143 * @brief Get isAppDebug. 144 */ 145 bool GetAppDebug(); 146 147 /** 148 * @brief Set sample jank params. 149 */ 150 int SetEventConfig(std::map<std::string, std::string> paramsMap); 151 152 /** 153 * @brief Set specified process name. 154 */ 155 void SetSpecifiedProcessName(const std::string& name); 156 157 /** 158 * @brief Set scroll param. 159 */ 160 void SetScrollState(bool isScroll); 161 162 /** 163 * @brief Start freeze stack sample. 164 */ 165 void StartSample(int duration, int interval, std::string& outFile); 166 }; 167 } // end of namespace HiviewDFX 168 } // end of namespace OHOS 169 #endif 170 171