blob: f8bd65723b5ddf2f5c5bbb8b55d1c9651bc6e69e [file] [log] [blame]
Willy Tarreau2bfefdb2019-05-03 13:52:18 +02001/*
2 * Thread lockup detection
3 *
4 * Copyright 2000-2019 Willy Tarreau <willy@haproxy.org>.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <signal.h>
13#include <time.h>
14
15#include <common/config.h>
16#include <common/debug.h>
17#include <common/hathreads.h>
18#include <common/initcall.h>
19#include <common/standard.h>
20#include <types/global.h>
Olivier Houchardde01ea92020-03-18 13:07:19 +010021#include <types/signal.h>
Willy Tarreau2bfefdb2019-05-03 13:52:18 +020022#include <proto/log.h>
23
24
25/*
26 * It relies on timer_create() and timer_settime() which are only available in
27 * this case.
28 */
Willy Tarreaue58114e2020-03-04 10:53:07 +010029#if defined(USE_RT) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
Willy Tarreau2bfefdb2019-05-03 13:52:18 +020030
Willy Tarreau2bfefdb2019-05-03 13:52:18 +020031/* Setup (or ping) the watchdog timer for thread <thr>. Returns non-zero on
32 * success, zero on failure. It interrupts once per second of CPU time. It
33 * happens that timers based on the CPU time are not automatically re-armed
34 * so we only use the value and leave the interval unset.
35 */
36int wdt_ping(int thr)
37{
38 struct itimerspec its;
39
40 its.it_value.tv_sec = 1; its.it_value.tv_nsec = 0;
41 its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0;
David Carliera92c5ce2019-09-13 05:03:12 +010042 return timer_settime(ha_thread_info[thr].wd_timer, 0, &its, NULL) == 0;
Willy Tarreau2bfefdb2019-05-03 13:52:18 +020043}
44
45/* This is the WDTSIG signal handler */
46void wdt_handler(int sig, siginfo_t *si, void *arg)
47{
48 unsigned long long n, p;
49 int thr;
50
51 switch (si->si_code) {
52 case SI_TIMER:
53 /* A thread's timer fired, the thread ID is in si_int. We have
54 * no guarantee that the thread handling this signal is in any
55 * way related to the one triggering it, so we need to retrieve
56 * the thread number from there. Note: this thread might
57 * continue to execute in parallel.
58 */
Willy Tarreau02255b22019-05-23 08:36:29 +020059 thr = si->si_value.sival_int;
Willy Tarreau2bfefdb2019-05-03 13:52:18 +020060
61 /* cannot happen unless an unknown timer tries to play with our
62 * nerves. Let's die for now if this happens.
63 */
64 if (thr < 0 || thr >= global.nbthread)
65 break;
66
David Carliera92c5ce2019-09-13 05:03:12 +010067 p = ha_thread_info[thr].prev_cpu_time;
68 n = now_cpu_time_thread(&ha_thread_info[thr]);
Willy Tarreau2bfefdb2019-05-03 13:52:18 +020069
70 /* not yet reached the deadline of 1 sec */
71 if (n - p < 1000000000UL)
72 goto update_and_leave;
73
Willy Tarreaua37cb182019-07-31 19:20:39 +020074 if ((threads_harmless_mask|sleeping_thread_mask|threads_to_dump) & (1UL << thr)) {
Willy Tarreau2bfefdb2019-05-03 13:52:18 +020075 /* This thread is currently doing exactly nothing
76 * waiting in the poll loop (unlikely but possible),
77 * waiting for all other threads to join the rendez-vous
78 * point (common), or waiting for another thread to
79 * finish an isolated operation (unlikely but possible).
80 */
81 goto update_and_leave;
82 }
83
84 /* So the thread indeed appears locked up. In order to be
85 * certain that we're not witnessing an exceptional spike of
86 * CPU usage due to a configuration issue (like running tens
87 * of thousands of tasks in a single loop), we'll check if the
88 * scheduler is still alive by setting the TI_FL_STUCK flag
89 * that the scheduler clears when switching to the next task.
90 * If it's already set, then it's our second call with no
91 * progress and the thread is dead.
92 */
David Carliera92c5ce2019-09-13 05:03:12 +010093 if (!(ha_thread_info[thr].flags & TI_FL_STUCK)) {
94 _HA_ATOMIC_OR(&ha_thread_info[thr].flags, TI_FL_STUCK);
Willy Tarreau2bfefdb2019-05-03 13:52:18 +020095 goto update_and_leave;
96 }
97
98 /* No doubt now, there's no hop to recover, die loudly! */
99 break;
Willy Tarreau06278152020-03-10 09:26:17 +0100100#ifdef USE_THREAD
Willy Tarreau2bfefdb2019-05-03 13:52:18 +0200101 case SI_TKILL:
102 /* we got a pthread_kill, stop on it */
103 thr = tid;
104 break;
Willy Tarreau06278152020-03-10 09:26:17 +0100105#endif
Willy Tarreau2bfefdb2019-05-03 13:52:18 +0200106 default:
107 /* unhandled other conditions */
108 return;
109 }
110
111 /* By default we terminate. If we're not on the victim thread, better
112 * bounce the signal there so that we produce a cleaner stack trace
113 * with the other thread interrupted exactly where it was running and
114 * the current one not involved in this.
115 */
Willy Tarreaue58114e2020-03-04 10:53:07 +0100116#ifdef USE_THREAD
Willy Tarreau2bfefdb2019-05-03 13:52:18 +0200117 if (thr != tid)
David Carliera92c5ce2019-09-13 05:03:12 +0100118 pthread_kill(ha_thread_info[thr].pthread, sig);
Willy Tarreau2bfefdb2019-05-03 13:52:18 +0200119 else
Willy Tarreaue58114e2020-03-04 10:53:07 +0100120#endif
Willy Tarreau2bfefdb2019-05-03 13:52:18 +0200121 ha_panic();
122 return;
123
124 update_and_leave:
125 wdt_ping(thr);
126}
127
128int init_wdt_per_thread()
129{
Willy Tarreauc1563e52020-02-26 14:03:05 +0100130 struct sigevent sev = { };
Willy Tarreau2bfefdb2019-05-03 13:52:18 +0200131 sigset_t set;
132
133 /* unblock the WDTSIG signal we intend to use */
134 sigemptyset(&set);
135 sigaddset(&set, WDTSIG);
136 ha_sigmask(SIG_UNBLOCK, &set, NULL);
137
138 /* this timer will signal WDTSIG when it fires, with tid in the si_int
139 * field (important since any thread will receive the signal).
140 */
141 sev.sigev_notify = SIGEV_SIGNAL;
142 sev.sigev_signo = WDTSIG;
143 sev.sigev_value.sival_int = tid;
Willy Tarreaud6f19662020-03-04 10:48:18 +0100144 if (timer_create(ti->clock_id, &sev, &ti->wd_timer) == -1 &&
145 timer_create(CLOCK_REALTIME, &sev, &ti->wd_timer) == -1)
Willy Tarreau2bfefdb2019-05-03 13:52:18 +0200146 goto fail1;
147
148 if (!wdt_ping(tid))
149 goto fail2;
150
151 return 1;
152
153 fail2:
154 timer_delete(ti->wd_timer);
155 fail1:
156 ti->wd_timer = TIMER_INVALID;
157 ha_warning("Failed to setup watchdog timer for thread %u, disabling lockup detection.\n", tid);
Willy Tarreau7259fa22020-03-04 10:46:13 +0100158 return 1;
Willy Tarreau2bfefdb2019-05-03 13:52:18 +0200159}
160
161void deinit_wdt_per_thread()
162{
163 if (ti->wd_timer != TIMER_INVALID)
164 timer_delete(ti->wd_timer);
165}
166
167/* registers the watchdog signal handler and returns 0. This sets up the signal
168 * handler for WDTSIG, so it must be called once per process.
169 */
170int init_wdt()
171{
172 struct sigaction sa;
173
174 sa.sa_handler = NULL;
175 sa.sa_sigaction = wdt_handler;
176 sigemptyset(&sa.sa_mask);
177 sa.sa_flags = SA_SIGINFO;
178 sigaction(WDTSIG, &sa, NULL);
179 return 0;
180}
181
182REGISTER_POST_CHECK(init_wdt);
183REGISTER_PER_THREAD_INIT(init_wdt_per_thread);
184REGISTER_PER_THREAD_DEINIT(deinit_wdt_per_thread);
185#endif