summaryrefslogtreecommitdiffstats
path: root/src/trex_watchdog.cpp
blob: d38809fcbee1870c89b8cb80e64ecc8b3d551a2d (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 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.
*/

#include "trex_watchdog.h"
#include "trex_exception.h"

#include <assert.h>
#include <unistd.h>
#include <sstream>

#include <sys/ptrace.h>
#include <execinfo.h>
#include <cxxabi.h>
#include <dlfcn.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <iostream>
#include  <stdexcept>

static TrexWatchDog::monitor_st *global_monitor;

const char *get_exe_name();

std::string exec(const char* cmd) {
    char buffer[128];
    std::string result = "";
    std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    while (!feof(pipe.get())) {
        if (fgets(buffer, 128, pipe.get()) != NULL) {
            result += buffer;
        }
    }
    return result;
}

// This function produces a stack backtrace with demangled function & method names.
__attribute__((noinline))
std::string Backtrace(int skip = 1)
{
    void *callstack[128];
    const int nMaxFrames = sizeof(callstack) / sizeof(callstack[0]);
    char buf[1024];
    int nFrames = backtrace(callstack, nMaxFrames);
    char **symbols = backtrace_symbols(callstack, nFrames);

    std::ostringstream trace_buf;
    for (int i = skip; i < nFrames; i++) {

        Dl_info info;
        if (dladdr(callstack[i], &info) && info.dli_sname) {
            char *demangled = NULL;
            int status = -1;
            if (info.dli_sname[0] == '_')
                demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status);
            snprintf(buf, sizeof(buf), "%-3d %*p %s + %zd\n",
                     i, int(2 + sizeof(void*) * 2), callstack[i],
                     status == 0 ? demangled :
                     info.dli_sname == 0 ? symbols[i] : info.dli_sname,
                     (char *)callstack[i] - (char *)info.dli_saddr);
            free(demangled);
        } else {
            snprintf(buf, sizeof(buf), "%-3d %*p %s\n",
                     i, int(2 + sizeof(void*) * 2), callstack[i], symbols[i]);
        }
        trace_buf << buf;
    }
    free(symbols);
    if (nFrames == nMaxFrames)
        trace_buf << "[truncated]\n";

    /* add the addr2line info */
    std::stringstream addr2line;

    addr2line << "/usr/bin/addr2line -e " << get_exe_name() << " ";
    for (int i = skip; i < nFrames; i++) {
        addr2line << callstack[i] << " ";
    }

    trace_buf << "\n\n*** addr2line information follows ***\n\n";
    try {
        trace_buf << exec(addr2line.str().c_str());
    } catch (std::runtime_error &e) {
        trace_buf << "\n" << e.what();
    }

    return trace_buf.str();
}

__attribute__((noinline))
static void _callstack_signal_handler(int signr, siginfo_t *info, void *secret) {
    std::stringstream ss;

    double now = now_sec();

    ss << "WATCHDOG: task '" << global_monitor->name << "' has not responded for more than " << (now - global_monitor->ts) << " seconds - timeout is " << global_monitor->timeout_sec << " seconds";

    std::string backtrace = Backtrace();
    ss << "\n\n*** traceback follows ***\n\n" << backtrace << "\n";

    throw std::runtime_error(ss.str());
}

void TrexWatchDog::mark_pending_monitor(int count) {
    std::unique_lock<std::mutex> lock(m_lock);
    m_pending += count;
    lock.unlock();
}

void TrexWatchDog::block_on_pending(int max_block_time_ms) {

    int timeout_msec = max_block_time_ms;

    std::unique_lock<std::mutex> lock(m_lock);

    while (m_pending > 0) {

        lock.unlock();
        delay(1);
        lock.lock();

        timeout_msec -= 1;
        if (timeout_msec == 0) {
            throw TrexException("WATCHDOG: block on pending monitors timed out");
        }
    }

    /* lock will be released */
}

/**
 * register a monitor 
 * must be called from the relevant thread 
 *  
 * this function is thread safe 
 * 
 * @author imarom (01-Jun-16)
 * 
 * @param name 
 * @param timeout_sec 
 * 
 * @return int 
 */
int TrexWatchDog::register_monitor(const std::string &name, double timeout_sec) {
    monitor_st monitor;

    /* cannot add monitors while active */
    assert(m_active == false);

    monitor.tid          = pthread_self();
    monitor.name         = name;
    monitor.timeout_sec  = timeout_sec;
    monitor.tickled      = true;
    monitor.ts           = 0;

    /* critical section start */
    std::unique_lock<std::mutex> lock(m_lock);

    /* make sure no double register */
    for (auto &m : m_monitors) {
        if (m.tid == pthread_self()) {
            std::stringstream ss;
            ss << "WATCHDOG: double register detected\n\n" << Backtrace();
            throw TrexException(ss.str());
        }
    }

    monitor.handle = m_monitors.size();
    m_monitors.push_back(monitor);

    assert(m_pending > 0);
    m_pending--;

    /* critical section end */
    lock.unlock();

    return monitor.handle;
}

/**
 * thread safe function
 * 
 */
void TrexWatchDog::tickle(int handle) {

    /* ignore ticks if not active */
    if (!m_active) {
        return;
    }

    assert(handle < m_monitors.size());

    /* not nesscary but write gets cache invalidate for nothing */
    if (m_monitors[handle].tickled) {
        return;
    }

    m_monitors[handle].tickled = true;
}

void TrexWatchDog::register_signal() {

    /* do this once */
    if (g_signal_init) {
        return;
    }

    /* register a handler on SIG ALARM */
    struct sigaction sa;
    memset (&sa, '\0', sizeof(sa));

    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = _callstack_signal_handler;

    int rc = sigaction(SIGALRM , &sa, NULL);
    assert(rc == 0);

    g_signal_init = true;
}

void TrexWatchDog::start() {

    block_on_pending();

    /* no pending monitors */
    assert(m_pending == 0);

    /* under GDB - disable the watchdog */
    if (ptrace(PTRACE_TRACEME, 0, NULL, 0) == -1) {
        printf("\n\n*** GDB detected - disabling watchdog... ***\n\n");
        return;
    }

    register_signal();
   
    m_active = true;
    m_thread = new std::thread(&TrexWatchDog::_main, this);
    if (!m_thread) {
        throw TrexException("unable to create watchdog thread");
    }
}

void TrexWatchDog::stop() {
    m_active = false;

    if (m_thread) {
        m_thread->join();
        delete m_thread;
        m_thread = NULL;
    }

    m_monitors.clear();
}



/**
 * main loop
 * 
 */
void TrexWatchDog::_main() {

    /* reset all the monitors */
    for (auto &monitor : m_monitors) {
        monitor.tickled = true;
    }

    /* start main loop */
    while (m_active) {

        dsec_t now = now_sec();

        for (auto &monitor : m_monitors) {
            /* if its own - turn it off and write down the time */
            if (monitor.tickled) {
                monitor.tickled = false;
                monitor.ts      = now;
                continue;
            }

            /* the bit is off - check the time first */
            if ( (now - monitor.ts) > monitor.timeout_sec ) {
                global_monitor = &monitor;

                pthread_kill(monitor.tid, SIGALRM);

                /* nothing to do more... the other thread will terminate, but if not - we terminate */
                sleep(5);
                printf("\n\n*** WATCHDOG violation detected on task '%s' which have failed to response to the signal ***\n\n", monitor.name.c_str());
                exit(1);
            }

        }

        /* the internal clock - 250 ms */
        delay(250);
    }
}

bool TrexWatchDog::g_signal_init = false;