1 /* 2 * Copyright (c) 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_TASK_H 17 #define RELIABILITY_WATCHDOG_TASK_H 18 19 #include <functional> 20 #include <string> 21 #include <sys/types.h> 22 23 #include "event_handler.h" 24 #include "handler_checker.h" 25 26 using Task = std::function<void()>; 27 using TimeOutCallback = std::function<void(const std::string &name, int waitState)>; 28 using XCollieCallback = std::function<void (void *)>; 29 namespace OHOS { 30 namespace HiviewDFX { 31 class WatchdogTask { 32 static int64_t curId; 33 public: 34 WatchdogTask(std::string name, std::shared_ptr<AppExecFwk::EventHandler> handler, 35 TimeOutCallback timeOutCallback, uint64_t interval); 36 WatchdogTask(std::string name, Task&& task, uint64_t delay, uint64_t interval, bool isOneshot); 37 WatchdogTask(std::string name, unsigned int timeout, XCollieCallback func, void *arg, unsigned int flag); 38 WatchdogTask(std::string name, unsigned int timeLimit, int countLimit); WatchdogTask()39 WatchdogTask() 40 : name(""), 41 task(nullptr), 42 timeOutCallback(nullptr), 43 checker(nullptr), 44 id(0), 45 timeout(0), 46 func(nullptr), 47 arg(nullptr), 48 flag(0), 49 checkInterval(0), 50 nextTickTime(0), 51 isTaskScheduled(false), 52 isOneshotTask(false), 53 watchdogTid(0), 54 timeLimit(0), 55 countLimit(0) {}; ~WatchdogTask()56 ~WatchdogTask() {}; 57 operator <(const WatchdogTask &obj) const58 bool operator<(const WatchdogTask &obj) const 59 { 60 // as we use std::priority_queue, the event with smaller target time will be in the top of the queue 61 return (this->nextTickTime > obj.nextTickTime); 62 } 63 64 void Run(uint64_t now); 65 void RunHandlerCheckerTask(); 66 void SendEvent(const std::string &msg, const std::string &eventName); 67 void SendXCollieEvent(const std::string &timerName, const std::string &keyMsg) const; 68 void DoCallback(); 69 void TimerCountTask(); 70 71 int EvaluateCheckerState(); 72 std::string GetBlockDescription(uint64_t interval); 73 std::string name; 74 std::string message; 75 Task task; 76 TimeOutCallback timeOutCallback; 77 std::shared_ptr<HandlerChecker> checker; 78 int64_t id; 79 uint64_t timeout; 80 XCollieCallback func; 81 void *arg; 82 unsigned int flag; 83 uint64_t checkInterval; 84 uint64_t nextTickTime; 85 bool isTaskScheduled; 86 bool isOneshotTask; 87 pid_t watchdogTid; 88 uint64_t timeLimit; 89 int countLimit; 90 std::vector<uint64_t> triggerTimes; 91 92 private: 93 bool IsMemHookOn(); 94 }; 95 } // end of namespace HiviewDFX 96 } // end of namespace OHOS 97 #endif 98