• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_INNER_H
17 #define RELIABILITY_WATCHDOG_INNER_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <memory>
22 #include <mutex>
23 #include <queue>
24 #include <set>
25 #include <string>
26 #include <thread>
27 
28 #include "watchdog_task.h"
29 #include "c/ffrt_watchdog.h"
30 #include "singleton.h"
31 
32 namespace OHOS {
33 namespace HiviewDFX {
34 class WatchdogInner : public Singleton<WatchdogInner> {
35     DECLARE_SINGLETON(WatchdogInner);
36 public:
37     static const int XCOLLIE_CALLBACK_HISTORY_MAX = 5;
38     static const int XCOLLIE_CALLBACK_TIMEWIN_MAX = 60;
39     std::map<int64_t, int> taskIdCnt;
40     int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler, uint64_t interval);
41     int AddThread(const std::string &name, std::shared_ptr<AppExecFwk::EventHandler> handler,
42         TimeOutCallback timeOutCallback, uint64_t interval);
43     void RunOneShotTask(const std::string& name, Task&& task, uint64_t delay);
44     void RunPeriodicalTask(const std::string& name, Task&& task, uint64_t interval, uint64_t delay);
45     int64_t RunXCollieTask(const std::string& name, uint64_t timeout, XCollieCallback func, void *arg,
46         unsigned int flag);
47     void RemoveXCollieTask(int64_t id);
48     void StopWatchdog();
49     bool IsCallbackLimit(unsigned int flag);
50     void IpcCheck();
51     void InitFfrtWatchdog();
52     static void FfrtCallback(uint64_t taskId, const char *taskInfo, uint32_t delayedTaskCount);
53     static void SendFfrtEvent(const std::string &msg, const std::string &eventName, const char *taskInfo);
54     std::string currentScene_;
55 
56 private:
57     bool Start();
58     bool Stop();
59     bool SendMsgToHungtask(const std::string& msg);
60     bool KickWatchdog();
61     bool IsTaskExistLocked(const std::string& name);
62     bool IsExceedMaxTaskLocked();
63     int64_t InsertWatchdogTaskLocked(const std::string& name, WatchdogTask&& task);
64     uint64_t FetchNextTask(uint64_t now, WatchdogTask& task);
65     void ReInsertTaskIfNeed(WatchdogTask& task);
66     void CreateWatchdogThreadIfNeed();
67 
68     static const unsigned int MAX_WATCH_NUM = 128; // 128: max handler thread
69     std::priority_queue<WatchdogTask> checkerQueue_; // protected by lock_
70     std::unique_ptr<std::thread> threadLoop_;
71     std::mutex lock_;
72     std::condition_variable condition_;
73     std::atomic_bool isNeedStop_ = false;
74     std::once_flag flag_;
75     std::set<std::string> taskNameSet_;
76     std::shared_ptr<AppExecFwk::EventHandler> binderCheckHander_;
77     int cntCallback_;
78     time_t timeCallback_;
79 };
80 } // end of namespace HiviewDFX
81 } // end of namespace OHOS
82 #endif
83