blob: a7c3b3a75b973fba0673b4f61ae280a00037bcec [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 Tarreau5cf64dd2019-05-17 10:36:08 +020087 chunk_appendf(buf,
Willy Tarreau4e2b6462019-05-16 17:44:30 +020088 "%p (%s) calls=%u last=%llu%s\n",
Willy Tarreau14a1ab72019-05-17 10:34:25 +020089 task, TASK_IS_TASKLET(task) ? "tasklet" : "task",
90 task->calls,
91 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
92 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020093
Willy Tarreau5cf64dd2019-05-17 10:36:08 +020094 chunk_appendf(buf, "%s"
Willy Tarreau4e2b6462019-05-16 17:44:30 +020095 " fct=%p (%s) ctx=%p\n",
96 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +020097 task->process,
98 task->process == process_stream ? "process_stream" :
99 task->process == task_run_applet ? "task_run_applet" :
100 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200101 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +0200102 task->context);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200103}
104
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200105/* This function dumps all profiling settings. It returns 0 if the output
106 * buffer is full and it needs to be called again, otherwise non-zero.
107 */
108static int cli_io_handler_show_threads(struct appctx *appctx)
109{
110 struct stream_interface *si = appctx->owner;
111 int thr;
112
113 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
114 return 1;
115
116 if (appctx->st0)
117 thr = appctx->st1;
118 else
119 thr = 0;
120
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200121 chunk_reset(&trash);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200122 while (thr < global.nbthread) {
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200123 ha_thread_dump(&trash, thr, tid);
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200124 thr++;
125 }
Willy Tarreau5cf64dd2019-05-17 10:36:08 +0200126
127 if (ci_putchk(si_ic(si), &trash) == -1) {
128 /* failed, try again */
129 si_rx_room_blk(si);
130 appctx->st1 = thr;
131 return 0;
132 }
Willy Tarreau4e2b6462019-05-16 17:44:30 +0200133 return 1;
134}
135
136/* register cli keywords */
137static struct cli_kw_list cli_kws = {{ },{
138 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
139 {{},}
140}};
141
142INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);