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), 56 bootTimeStart(0), 57 monoTimeStart(0) {}; ~WatchdogTask()58 ~WatchdogTask() {}; 59 60 bool operator<(const WatchdogTask &obj) const 61 { 62 // as we use std::priority_queue, the event with smaller target time will be in the top of the queue 63 return (this->nextTickTime > obj.nextTickTime); 64 } 65 66 void Run(uint64_t now); 67 void RunHandlerCheckerTask(); 68 void SendEvent(const std::string &msg, const std::string &eventName); 69 void SendXCollieEvent(const std::string &timerName, const std::string &keyMsg) const; 70 void DoCallback(); 71 void TimerCountTask(); 72 73 int EvaluateCheckerState(); 74 std::string GetBlockDescription(uint64_t interval); 75 std::string name; 76 std::string message; 77 Task task; 78 TimeOutCallback timeOutCallback; 79 std::shared_ptr<HandlerChecker> checker; 80 int64_t id; 81 uint64_t timeout; 82 XCollieCallback func; 83 void *arg; 84 unsigned int flag; 85 uint64_t checkInterval; 86 uint64_t nextTickTime; 87 bool isTaskScheduled; 88 bool isOneshotTask; 89 pid_t watchdogTid; 90 uint64_t timeLimit; 91 int countLimit; 92 std::vector<uint64_t> triggerTimes; 93 uint64_t bootTimeStart; 94 uint64_t monoTimeStart; 95 96 private: 97 bool IsMemHookOn(); 98 }; 99 } // end of namespace HiviewDFX 100 } // end of namespace OHOS 101 #endif 102