• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 __H_UV_STATUS_H__
17 #define __H_UV_STATUS_H__
18 
19 #include <cinttypes>
20 #include <string>
21 #include <sys/time.h>
22 #include <map>
23 #include <uv.h>
24 
25 using std::string;
26 
27 namespace Hdc {
28 
29 #define NS_PER_MS 1000000
30 #define MS_PER_SEC 1000
31 #define LOOP_MONITOR_PERIOD (3 * MS_PER_SEC)
32 #define LOOP_HUNG_TIMEOUT (MS_PER_SEC / 2)
33 
34 class LoopStatus {
35 public:
36     LoopStatus(uv_loop_t *loop, const string &loopName);
37     ~LoopStatus();
38 private:
39     bool Busy(void) const;
40 public:
41     void HandleStart(const uv_loop_t *loop, const string &handle);
42     void HandleEnd(const uv_loop_t *loop);
43     void Display(const string &info, bool all = true) const;
44     void HungCheck(int64_t timeout) const;
UnUsedForUpdater(void)45     void UnUsedForUpdater(void) const {}
46 public:
47     void StartReportTimer(void);
48     static void ReportTimerProc(uv_timer_t *req);
49 private:
50     uv_loop_t *mLoop;
51     const string mLoopName;
52     string mHandleName;
53     bool mBusyNow;
54     uint64_t mCallBackTime;
55     uv_timer_t mReportTimer;
56 };
57 
58 class CallStatGuard {
59 public:
CallStatGuard(LoopStatus & loopStatus,const uv_loop_t * loop,const string & handle)60     CallStatGuard(LoopStatus &loopStatus, const uv_loop_t *loop, const string &handle) : mCommitted(false),
61                                                                                          mLoop(loop),
62                                                                                          mLoopStatus(loopStatus)
63     {
64         mLoopStatus.HandleStart(loop, handle);
65     }
~CallStatGuard()66     ~CallStatGuard()
67     {
68         if (mCommitted) {
69             return;
70         }
71         mLoopStatus.HandleEnd(mLoop);
72     }
Commit(void)73     void Commit(void)
74     {
75         if (mCommitted) {
76             return;
77         }
78         mLoopStatus.HandleEnd(mLoop);
79         mCommitted = true;
80     }
81 private:
82     bool mCommitted;
83     const uv_loop_t *mLoop;
84     LoopStatus &mLoopStatus;
85 };
86 
87 void DispAllLoopStatus(const string &info);
88 #ifdef UPDATER_MODE
89 #define CALLSTAT_GUARD(ls, loop, funcname) (ls).UnUsedForUpdater()
90 #else
91 #define CALLSTAT_GUARD(ls, loop, funcname) CallStatGuard csg(ls, loop, funcname)
92 #endif
93 void StartLoopMonitor(void);
94 
95 } /* namespace Hdc  */
96 
97 #endif /* __H_UV_STATUS_H__ */
98