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