blob: b4512b6e96420bc38a4b883075c6dec6b59e8df1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
Itay Marom
Cisco Systems, Inc.
*/
/*
Copyright (c) 2015-2015 Cisco Systems, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __TREX_WATCHDOG_H__
#define __TREX_WATCHDOG_H__
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <assert.h>
#include "mbuf.h"
#include "os_time.h"
/**
* every thread creates its own monitor from its own memory
*
* @author imarom (19-Jun-16)
*/
class TrexMonitor {
friend class TrexWatchDog;
public:
/**
* create a monitor
*
* @author imarom (31-May-16)
*
* @param name
* @param timeout
*
* @return int
*/
void create(const std::string &name, double timeout_sec);
/**
* disable the monitor for 'time_sec'
* by default it will disable it for a long period of time
* (forever)
*
*/
void disable(dsec_t time_sec = 1e9) {
set_timeout(time_sec);
}
/**
* re-enable a monitor after it was disabled
*
*/
void enable() {
set_timeout(m_base_timeout_sec);
}
/**
* not thread safe
* call from current thread only
*/
void io_begin() {
/**
* holds a ref cnt
* a thread might start many IO operations
*/
m_io_ref_cnt++;
set_timeout(IO_TIMEOUT_SEC);
}
/**
* not thread safe
* call from current thread only
*/
void io_end() {
assert(m_io_ref_cnt > 0);
m_io_ref_cnt--;
if (m_io_ref_cnt == 0) {
set_timeout(m_base_timeout_sec);
}
}
/**
* tickle the monitor - this should be called from the thread
* to avoid the watchdog from detecting a stuck thread
*
* @author imarom (19-Jun-16)
*/
void tickle() {
/* to avoid useless writes - first check */
if (!m_tickled) {
m_tickled = true;
}
}
const std::string &get_name() const {
return m_name;
}
/* return how much time has passed since last tickle */
dsec_t get_interval(dsec_t now) const {
return (now - m_ts);
}
dsec_t get_timeout_sec() const {
return m_timeout_sec;
}
private:
/**
* called by the watchdog to reset the monitor for a new round
*
*/
void reset(dsec_t now) {
m_tickled = false;
m_ts = now;
}
pthread_t get_tid() const {
return m_tid;
}
volatile bool is_tickled() const {
return m_tickled;
}
bool is_expired(dsec_t now) const {
return ( get_interval(now) > m_timeout_sec );
}
void set_timeout(double timeout_sec) {
/* before changing timeout we MUST tickle and memory fence o.w the main thread might crash */
tickle();
asm volatile("mfence" ::: "memory");
m_timeout_sec = timeout_sec;
}
/* write fields are first */
volatile bool m_tickled;
int m_handle;
dsec_t m_ts;
double m_timeout_sec;
double m_base_timeout_sec;
pthread_t m_tid;
std::string m_name;
uint32_t m_io_ref_cnt;
static const int IO_TIMEOUT_SEC = 30;
} __rte_cache_aligned;
/**
* a watchdog is a list of registered monitors
*
* @author imarom (19-Jun-16)
*/
class TrexWatchDog {
public:
/**
* singleton entry
*
* @author imarom (19-Jun-16)
*
* @return TrexWatchDog&
*/
static TrexWatchDog& getInstance() {
static TrexWatchDog instance;
return instance;
}
class IOFunction;
void init(bool enable);
/**
* get monitor of current thread if registered
* (NULL if not registered)
*
*/
TrexMonitor * get_current_monitor();
/**
* add a monitor to the watchdog
* from now on this monitor will be watched
*
* @author imarom (19-Jun-16)
*
* @param monitor - a pointer to the object
*
*/
void register_monitor(TrexMonitor *monitor);
/**
* start the watchdog
*
*/
void start();
/**
* stop the watchdog
*
*/
void stop();
private:
TrexWatchDog() {
m_thread = NULL;
m_enable = false;
m_active = false;
m_mon_count = 0;
}
void register_signal();
void _main();
static const int MAX_MONITORS = 100;
TrexMonitor *m_monitors[MAX_MONITORS];
volatile int m_mon_count;
std::mutex m_lock;
bool m_enable;
volatile bool m_active;
std::thread *m_thread;
static bool g_signal_init;
};
class TrexWatchDog::IOFunction {
public:
static void io_begin() {
TrexMonitor * cur_monitor = TrexWatchDog::getInstance().get_current_monitor();
if (cur_monitor != NULL) {
cur_monitor->io_begin();
}
}
static void io_end() {
TrexMonitor * cur_monitor = TrexWatchDog::getInstance().get_current_monitor();
if (cur_monitor != NULL) {
cur_monitor->io_end();
}
}
IOFunction() {
IOFunction::io_begin();
}
~IOFunction() {
IOFunction::io_end();
}
};
#endif /* __TREX_WATCHDOG_H__ */
|