blob: 54f91d6238c17d2686680f6ec95cff9270d166dd [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{
78 chunk_appendf(&trash,
79 "%p (%s) calls=%u last=%llu%s\n",
80 curr_task, TASK_IS_TASKLET(curr_task) ? "tasklet" : "task",
81 curr_task->calls,
82 curr_task->call_date ? (unsigned long long)(now_mono_time() - curr_task->call_date) : 0,
83 curr_task->call_date ? " ns ago" : "");
84
85 chunk_appendf(&trash, "%s"
86 " fct=%p (%s) ctx=%p\n",
87 pfx,
88 curr_task->process,
89 curr_task->process == process_stream ? "process_stream" :
90 curr_task->process == task_run_applet ? "task_run_applet" :
91 curr_task->process == si_cs_io_cb ? "si_cs_io_cb" :
92 "?",
93 curr_task->context);
94}
95
96
97/* This function dumps all profiling settings. It returns 0 if the output
98 * buffer is full and it needs to be called again, otherwise non-zero.
99 */
100static int cli_io_handler_show_threads(struct appctx *appctx)
101{
102 struct stream_interface *si = appctx->owner;
103 int thr;
104
105 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
106 return 1;
107
108 if (appctx->st0)
109 thr = appctx->st1;
110 else
111 thr = 0;
112
113 while (thr < global.nbthread) {
114 ha_thread_dump(thr);
115
116 if (ci_putchk(si_ic(si), &trash) == -1) {
117 /* failed, try again */
118 si_rx_room_blk(si);
119 appctx->st1 = thr;
120 return 0;
121 }
122 thr++;
123 }
124 return 1;
125}
126
127/* register cli keywords */
128static struct cli_kw_list cli_kws = {{ },{
129 { { "show", "threads", NULL }, "show threads : show some threads debugging information", NULL, cli_io_handler_show_threads, NULL },
130 {{},}
131}};
132
133INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);