• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_XCOLLIE_INNER_H
17 #define RELIABILITY_XCOLLIE_INNER_H
18 
19 #include <time.h>
20 
21 #include <condition_variable>
22 #include <functional>
23 #include <map>
24 #include <memory>
25 #include <mutex>
26 #include <string>
27 #include <thread>
28 
29 #include "refbase.h"
30 #include "singleton.h"
31 
32 #include "timer_ring.h"
33 #include "xcollie_checker.h"
34 
35 namespace OHOS {
36 namespace HiviewDFX {
37 enum class CheckStatus {
38     COMPLETED = 0,
39     WAITING = 1,
40     WAITED_HALF = 2,
41 };
42 
43 static const std::string XCOLLIE_CHECKER_NAME = "XCollie";
44 static const std::string XCOLLIE_LOOP_NAME = "XCollieLoop";
45 
46 class XCollieInner : public Singleton<XCollieInner> {
47     DECLARE_SINGLETON(XCollieInner);
48 public:
49     static const int XCOLLIE_CALLBACK_HISTORY_MAX = 5;
50     static const int XCOLLIE_CALLBACK_TIMEWIN_MAX = 60;
51     static constexpr int STACK_INTERVAL = 30;
52     void RegisterXCollieChecker(const sptr<XCollieChecker> &checker, unsigned int type);
53     int SetTimer(const std::string &name, unsigned int timeout,
54         std::function<void (void *)> func, void *arg, unsigned int flag);
55     void CancelTimer(int id);
56     bool UpdateTimer(int id, unsigned int timeout);
57 
SetCheckerInterval(int interval)58     void SetCheckerInterval(int interval)
59     {
60         checkerInterval_ = interval;
61     }
SetRecoveryFlag(bool recovery)62     void SetRecoveryFlag(bool recovery)
63     {
64         recovery_ = recovery;
65     }
SetCheckStatus(CheckStatus status)66     void SetCheckStatus(CheckStatus status)
67     {
68         checkStatus_ = status;
69     }
70     std::string GetBlockdServiceName();
71     void UnRegisterXCollieChecker(const sptr<XCollieChecker> &checker);
72 
73 private:
74     void RunChecker();
75     bool Start(bool isWatchdog);
76     int StartCheckService();
77     std::string GetBlockServiceMsg(std::string &name) const;
78     bool GetThreadBlockResult();
79     void CheckResult();
80     void Stop();
81     void StopChecker();
82     void SendEvent(int tid, const std::string &timerName, const std::string &keyMsg) const;
83 
84     void DoTimerCallback(struct InputTimerPara *task);
85     bool IsCallbackLimit(unsigned int flag);
86     void DestroyTimer(const int id);
87 
88     std::unique_ptr<TimerRing> timerRing_;
89     mutable std::mutex lock_;
90     std::condition_variable condition_;
91 
92     /* monitor checker */
93     std::map<sptr<XCollieChecker>, unsigned int> xcollieCheckers_ = std::map<sptr<XCollieChecker>, unsigned int>();
94     sptr<XCollieChecker> threadChecker_; // current thread blocked service
95     sptr<XCollieChecker> lockChecker_; // current lock blocked service
96     std::unique_ptr<std::thread> threadLoop_; // watchdog thread
97     CheckStatus checkStatus_;
98     time_t startTime_;
99     bool lockCheckResult_;
100 
101     std::unique_ptr<std::thread> thread_;
102     volatile int exitThread_;
103 
104     unsigned int checkerInterval_;
105     bool recovery_;
106 
107     /* timer */
108     int cntCallback_;
109     time_t timeCallback_;
110 };
111 } // end of namespace HiviewDFX
112 } // end of namespace OHOS
113 #endif
114