blob: fce23c389b87d8294e632184cb5afbc0cce464be [file] [log] [blame]
Willy Tarreau4e2b6462019-05-16 17:44:30 +02001/*
2 * Process debugging functions.
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
13#include <signal.h>
14#include <time.h>
15#include <stdio.h>
16
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020017#include <common/buf.h>
Willy Tarreau4e2b6462019-05-16 17:44:30 +020018#include <common/config.h>
19#include <common/debug.h>
20#include <common/hathreads.h>
21#include <common/initcall.h>
22#include <common/standard.h>
23
24#include <types/global.h>
25
26#include <proto/cli.h>
27#include <proto/fd.h>
28#include <proto/stream_interface.h>
29#include <proto/task.h>
30
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020031/* Dumps to the buffer some known information for the desired thread, and
32 * optionally extra info for the current thread. The dump will be appended to
33 * the buffer, so the caller is responsible for preliminary initializing it.
34 * The calling thread ID needs to be passed in <calling_tid> to display a star
35 * in front of the calling thread's line (usually it's tid).
Willy Tarreau4e2b6462019-05-16 17:44:30 +020036 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020037void ha_thread_dump(struct buffer *buf, int thr, int calling_tid)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020038{
39 unsigned long thr_bit = 1UL << thr;
40
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020041 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020042 "%c Thread %-2u: act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
43 " fdcache=%d prof=%d",
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020044 (thr == calling_tid) ? '*' : ' ', thr + 1,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020045 !!(active_tasks_mask & thr_bit),
46 !!(global_tasks_mask & thr_bit),
47 !eb_is_empty(&task_per_thread[thr].timers),
48 !eb_is_empty(&task_per_thread[thr].rqueue),
49 !LIST_ISEMPTY(&task_per_thread[thr].task_list),
50 task_per_thread[thr].task_list_size,
51 task_per_thread[thr].rqueue_size,
52 !!(fd_cache_mask & thr_bit),
53 !!(task_profiling_mask & thr_bit));
54
55#ifdef USE_THREAD
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020056 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020057 " harmless=%d wantrdv=%d",
58 !!(threads_harmless_mask & thr_bit),
59 !!(threads_want_rdv_mask & thr_bit));
60#endif
61
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020062 chunk_appendf(buf, "\n");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020063
64 /* this is the end of what we can dump from outside the thread */
65
66 if (thr != tid)
67 return;
68
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020069 chunk_appendf(buf, " curr_task=");
70 ha_task_dump(buf, curr_task, " ");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020071}
72
73
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020074/* dumps into the buffer some information related to task <task> (which may
Willy Tarreau4e2b6462019-05-16 17:44:30 +020075 * either be a task or a tasklet, and prepend each line except the first one
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020076 * with <pfx>. The buffer is only appended and the first output starts by the
77 * pointer itself. The caller is responsible for making sure the task is not
78 * going to vanish during the dump.
Willy Tarreau4e2b6462019-05-16 17:44:30 +020079 */
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020080void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
Willy Tarreau4e2b6462019-05-16 17:44:30 +020081{
Willy Tarreau14a1ab72019-05-17 10:34:25 +020082 if (!task) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020083 chunk_appendf(buf, "0\n");
Willy Tarreau231ec392019-05-17 10:39:47 +020084 return;
85 }
86
Willy Tarreau20db9112019-05-17 14:14:35 +020087 if (TASK_IS_TASKLET(task))
88 chunk_appendf(buf,
89 "%p (tasklet) calls=%u\n",
90 task,
91 task->calls);
92 else
93 chunk_appendf(buf,
94 "%p (task) calls=%u last=%llu%s\n",
95 task,
96 task->calls,
97 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
98 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020099
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200100 chunk_appendf(buf, "%s"
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200101 " fct=%p (%s) ctx=%p\n",
102 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200103 task->process,
104 task->process == process_stream ? "process_stream" :
105 task->process == task_run_applet ? "task_run_applet" :
106 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200107 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200108 task->context);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200109}
110
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200111/* This function dumps all profiling settings. It returns 0 if the output
112 * buffer is full and it needs to be called again, otherwise non-zero.
113 */
114static int cli_io_handler_show_threads(struct appctx *appctx)
115{
116 struct stream_interface *si = appctx->owner;
117 int thr;
118
119 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
120 return 1;
121
122 if (appctx->st0)
123 thr = appctx->st1;
124 else
125 thr = 0;
126
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200127 chunk_reset(&trash);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200128 ha_thread_dump_all_to_trash();
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200129
130 if (ci_putchk(si_ic(si), &trash) == -1) {
131 /* failed, try again */
132 si_rx_room_blk(si);
133 appctx->st1 = thr;
134 return 0;
135 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200136 return 1;
137}
138
Willy Tarreauc7091d82019-05-17 10:08:49 +0200139#ifndef USE_THREAD_DUMP
140
141/* This function dumps all threads' state to the trash. This version is the
142 * most basic one, which doesn't inspect other threads.
143 */
144void ha_thread_dump_all_to_trash()
145{
146 unsigned int thr;
147
148 for (thr = 0; thr < global.nbthread; thr++)
149 ha_thread_dump(&trash, thr, tid);
150}
151
152#else /* below USE_THREAD_DUMP is set */
153
154/* The signal to trigger a debug dump on a thread is SIGPWR */
155#define DEBUGSIG SIGPWR
156
157/* mask of threads still having to dump, used to respect ordering */
158static volatile unsigned long threads_to_dump;
159
160/* ID of the thread requesting the dump */
161static unsigned int thread_dump_tid;
162
163/* points to the buffer where the dump functions should write. It must
164 * have already been initialized by the requester. Nothing is done if
165 * it's NULL.
166 */
167struct buffer *thread_dump_buffer = NULL;
168
169void ha_thread_dump_all_to_trash()
170{
171 __maybe_unused unsigned int thr;
172 unsigned long old;
173
174 while (1) {
175 old = 0;
176 if (HA_ATOMIC_CAS(&threads_to_dump, &old, all_threads_mask))
177 break;
178 ha_thread_relax();
179 }
180
181 thread_dump_buffer = &trash;
182 thread_dump_tid = tid;
183
184#ifdef USE_THREAD
185 for (thr = 0; thr < global.nbthread; thr++) {
186 if (thr != tid)
Willy Tarreau522cfbc2019-05-03 10:16:39 +0200187 pthread_kill(thread_info[thr].pthread, DEBUGSIG);
Willy Tarreauc7091d82019-05-17 10:08:49 +0200188 }
189#endif
190 /* dump ourselves last */
191 raise(DEBUGSIG);
192}
193
194/* handles DEBUGSIG to dump the state of the thread it's working on */
195void debug_handler(int sig, siginfo_t *si, void *arg)
196{
197 /* There are 4 phases in the dump process:
198 * 1- wait for our turn, i.e. when all lower bits are gone.
199 * 2- perform the action if our bit is set
200 * 3- remove our bit to let the next one go, unless we're
201 * the last one and have to put them all but ours
202 * 4- wait for zero and clear our bit if it's set
203 */
204
205 /* wait for all previous threads to finish first */
206 while (threads_to_dump & (tid_bit - 1))
207 ha_thread_relax();
208
209 /* dump if needed */
210 if (threads_to_dump & tid_bit) {
211 if (thread_dump_buffer)
212 ha_thread_dump(thread_dump_buffer, tid, thread_dump_tid);
213 if ((threads_to_dump & all_threads_mask) == tid_bit) {
214 /* last one */
215 HA_ATOMIC_STORE(&threads_to_dump, all_threads_mask & ~tid_bit);
216 thread_dump_buffer = NULL;
217 }
218 else
219 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
220 }
221
222 /* now wait for all others to finish dumping. The last one will set all
223 * bits again to broadcast the leaving condition.
224 */
225 while (threads_to_dump & all_threads_mask) {
226 if (threads_to_dump & tid_bit)
227 HA_ATOMIC_AND(&threads_to_dump, ~tid_bit);
228 else
229 ha_thread_relax();
230 }
231}
232
233static int init_debug_per_thread()
234{
235 sigset_t set;
236
237 /* unblock the DEBUGSIG signal we intend to use */
238 sigemptyset(&set);
239 sigaddset(&set, DEBUGSIG);
240 ha_sigmask(SIG_UNBLOCK, &set, NULL);
241 return 1;
242}
243
244static int init_debug()
245{
246 struct sigaction sa;
247
248 sa.sa_handler = NULL;
249 sa.sa_sigaction = debug_handler;
250 sigemptyset(&sa.sa_mask);
251 sa.sa_flags = SA_SIGINFO;
252 sigaction(DEBUGSIG, &sa, NULL);
253 return 0;
254}
255
256REGISTER_POST_CHECK(init_debug);
257REGISTER_PER_THREAD_INIT(init_debug_per_thread);
258
259#endif /* USE_THREAD_DUMP */
260
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200261/* register cli keywords */
262static struct cli_kw_list cli_kws = {{ },{
263 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
264 {{},}
265}};
266
267INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);