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); WatchdogTask()38 WatchdogTask() 39 : name(""), 40 task(nullptr), 41 timeOutCallback(nullptr), 42 checker(nullptr), 43 checkInterval(0), 44 nextTickTime(0), 45 isOneshotTask(false), 46 watchdogTid(0) {}; ~WatchdogTask()47 ~WatchdogTask() {}; 48 49 bool operator<(const WatchdogTask &obj) const 50 { 51 // as we use std::priority_queue, the event with smaller target time will be in the top of the queue 52 return (this->nextTickTime > obj.nextTickTime); 53 } 54 55 void Run(uint64_t now); 56 void RunHandlerCheckerTask(); 57 void SendEvent(const std::string &msg, const std::string &eventName) const; 58 void SendXCollieEvent(const std::string &timerName, const std::string &keyMsg) const; 59 void DoCallback(); 60 61 int EvaluateCheckerState(); 62 std::string GetBlockDescription(uint64_t interval); 63 std::string name; 64 Task task; 65 TimeOutCallback timeOutCallback; 66 std::shared_ptr<HandlerChecker> checker; 67 int64_t id; 68 uint64_t timeout; 69 XCollieCallback func; 70 void *arg; 71 unsigned int flag; 72 uint64_t checkInterval; 73 uint64_t nextTickTime; 74 bool isTaskScheduled; 75 bool isOneshotTask; 76 pid_t watchdogTid; 77 }; 78 } // end of namespace HiviewDFX 79 } // end of namespace OHOS 80 #endif 81