blob: be17df30c8d8015aaef1dcf0f7fcaab2e59f069d [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
17#include <common/config.h>
18#include <common/debug.h>
19#include <common/hathreads.h>
20#include <common/initcall.h>
21#include <common/standard.h>
22
23#include <types/global.h>
24
25#include <proto/cli.h>
26#include <proto/fd.h>
27#include <proto/stream_interface.h>
28#include <proto/task.h>
29
30/* Dumps to the trash some known information for the desired thread, and
31 * optionally extra info for the current thread.
32 */
33void ha_thread_dump(int thr)
34{
35 unsigned long thr_bit = 1UL << thr;
36
37 chunk_reset(&trash);
38 chunk_appendf(&trash,
39 "%c Thread %-2u: act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n"
40 " fdcache=%d prof=%d",
41 (thr == tid) ? '*' : ' ', thr + 1,
42 !!(active_tasks_mask & thr_bit),
43 !!(global_tasks_mask & thr_bit),
44 !eb_is_empty(&task_per_thread[thr].timers),
45 !eb_is_empty(&task_per_thread[thr].rqueue),
46 !LIST_ISEMPTY(&task_per_thread[thr].task_list),
47 task_per_thread[thr].task_list_size,
48 task_per_thread[thr].rqueue_size,
49 !!(fd_cache_mask & thr_bit),
50 !!(task_profiling_mask & thr_bit));
51
52#ifdef USE_THREAD
53 chunk_appendf(&trash,
54 " harmless=%d wantrdv=%d",
55 !!(threads_harmless_mask & thr_bit),
56 !!(threads_want_rdv_mask & thr_bit));
57#endif
58
59 chunk_appendf(&trash, "\n");
60
61 /* this is the end of what we can dump from outside the thread */
62
63 if (thr != tid)
64 return;
65
66 chunk_appendf(&trash, " curr_task=");
67 ha_task_dump(curr_task, " ");
68}
69
70
71/* dumps into the trash some information related to task <task> (which may
72 * either be a task or a tasklet, and prepend each line except the first one
73 * with <pfx>. The trash is only appended and the first output starts by the
74 * pointer itself.
75 */
76void ha_task_dump(const struct task *task, const char *pfx)
77{
Willy Tarreau14a1ab72019-05-17 10:34:25 +020078 if (!task) {
Willy Tarreau231ec392019-05-17 10:39:47 +020079 chunk_appendf(&trash, "0\n");
80 return;
81 }
82
Willy Tarreau4e2b6462019-05-16 17:44:30 +020083 chunk_appendf(&trash,
84 "%p (%s) calls=%u last=%llu%s\n",
Willy Tarreau14a1ab72019-05-17 10:34:25 +020085 task, TASK_IS_TASKLET(task) ? "tasklet" : "task",
86 task->calls,
87 task->call_date ? (unsigned long long)(now_mono_time() - task->call_date) : 0,
88 task->call_date ? " ns ago" : "");
Willy Tarreau4e2b6462019-05-16 17:44:30 +020089
90 chunk_appendf(&trash, "%s"
91 " fct=%p (%s) ctx=%p\n",
92 pfx,
Willy Tarreau14a1ab72019-05-17 10:34:25 +020093 task->process,
94 task->process == process_stream ? "process_stream" :
95 task->process == task_run_applet ? "task_run_applet" :
96 task->process == si_cs_io_cb ? "si_cs_io_cb" :
Willy Tarreau4e2b6462019-05-16 17:44:30 +020097 "?",
Willy Tarreau14a1ab72019-05-17 10:34:25 +020098 task->context);
Willy Tarreau4e2b6462019-05-16 17:44:30 +020099}
100
101
102/* This function dumps all profiling settings. It returns 0 if the output
103 * buffer is full and it needs to be called again, otherwise non-zero.
104 */
105static int cli_io_handler_show_threads(struct appctx *appctx)
106{
107 struct stream_interface *si = appctx->owner;
108 int thr;
109
110 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
111 return 1;
112
113 if (appctx->st0)
114 thr = appctx->st1;
115 else
116 thr = 0;
117
118 while (thr < global.nbthread) {
119 ha_thread_dump(thr);
120
121 if (ci_putchk(si_ic(si), &trash) == -1) {
122 /* failed, try again */
123 si_rx_room_blk(si);
124 appctx->st1 = thr;
125 return 0;
126 }
127 thr++;
128 }
129 return 1;
130}
131
132/* register cli keywords */
133static struct cli_kw_list cli_kws = {{ },{
134 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
135 {{},}
136}};
137
138INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);