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 using IpcFullCallback = std::function<void (void *)>; 30 namespace OHOS { 31 namespace HiviewDFX { 32 class WatchdogTask { 33 static int64_t curId; 34 public: 35 WatchdogTask(std::string name, std::shared_ptr<AppExecFwk::EventHandler> handler, 36 TimeOutCallback timeOutCallback, uint64_t interval); 37 WatchdogTask(uint64_t interval, IpcFullCallback func, void *arg, unsigned int flag); 38 WatchdogTask(std::string name, Task&& task, uint64_t delay, uint64_t interval, bool isOneshot); 39 WatchdogTask(std::string name, unsigned int timeout, XCollieCallback func, void *arg, unsigned int flag); 40 WatchdogTask(std::string name, unsigned int timeLimit, int countLimit); WatchdogTask()41 WatchdogTask() 42 : name(""), 43 task(nullptr), 44 timeOutCallback(nullptr), 45 checker(nullptr), 46 id(0), 47 timeout(0), 48 func(nullptr), 49 arg(nullptr), 50 flag(0), 51 checkInterval(0), 52 nextTickTime(0), 53 isTaskScheduled(false), 54 isOneshotTask(false), 55 watchdogTid(0), 56 timeLimit(0), 57 countLimit(0), 58 bootTimeStart(0), 59 monoTimeStart(0), 60 reportCount(0) {}; ~WatchdogTask()61 ~WatchdogTask() {}; 62 63 bool operator<(const WatchdogTask &obj) const 64 { 65 // as we use std::priority_queue, the event with smaller target time will be in the top of the queue 66 return (this->nextTickTime > obj.nextTickTime); 67 } 68 69 void Run(uint64_t now); 70 void RunHandlerCheckerTask(); 71 void SendEvent(const std::string &msg, const std::string &eventName, const std::string& faultTimeStr); 72 void SendXCollieEvent(const std::string &timerName, const std::string &keyMsg, 73 const std::string& faultTimeStr) const; 74 void DoCallback(); 75 void TimerCountTask(); 76 void DumpKernelStack(struct HstackVal& val, int& ret) const; 77 #ifdef SUSPEND_CHECK_ENABLE 78 bool ShouldSkipCheckForSuspend(uint64_t &now, double &suspendStartTime, double &suspendEndTime); 79 #endif 80 int EvaluateCheckerState(); 81 std::string GetBlockDescription(uint64_t interval); 82 std::string name; 83 std::string message; 84 Task task; 85 TimeOutCallback timeOutCallback; 86 std::shared_ptr<HandlerChecker> checker; 87 int64_t id; 88 uint64_t timeout; 89 XCollieCallback func; 90 void *arg; 91 unsigned int flag; 92 uint64_t checkInterval; 93 uint64_t nextTickTime; 94 bool isTaskScheduled; 95 bool isOneshotTask; 96 pid_t watchdogTid; 97 uint64_t timeLimit; 98 int countLimit; 99 std::vector<uint64_t> triggerTimes; 100 uint64_t bootTimeStart; 101 uint64_t monoTimeStart; 102 int reportCount; 103 }; 104 } // end of namespace HiviewDFX 105 } // end of namespace OHOS 106 #endif 107