Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 17 | #include <common/buf.h> |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 18 | #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 Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 31 | /* 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 36 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 37 | void ha_thread_dump(struct buffer *buf, int thr, int calling_tid) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 38 | { |
| 39 | unsigned long thr_bit = 1UL << thr; |
| 40 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 41 | chunk_appendf(buf, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 42 | "%c Thread %-2u: act=%d glob=%d wq=%d rq=%d tl=%d tlsz=%d rqsz=%d\n" |
| 43 | " fdcache=%d prof=%d", |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 44 | (thr == calling_tid) ? '*' : ' ', thr + 1, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 45 | !!(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 Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 56 | chunk_appendf(buf, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 57 | " harmless=%d wantrdv=%d", |
| 58 | !!(threads_harmless_mask & thr_bit), |
| 59 | !!(threads_want_rdv_mask & thr_bit)); |
| 60 | #endif |
| 61 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 62 | chunk_appendf(buf, "\n"); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 63 | |
| 64 | /* this is the end of what we can dump from outside the thread */ |
| 65 | |
| 66 | if (thr != tid) |
| 67 | return; |
| 68 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 69 | chunk_appendf(buf, " curr_task="); |
| 70 | ha_task_dump(buf, curr_task, " "); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 74 | /* dumps into the buffer some information related to task <task> (which may |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 75 | * either be a task or a tasklet, and prepend each line except the first one |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 76 | * 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 79 | */ |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 80 | void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx) |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 81 | { |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 82 | if (!task) { |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 83 | chunk_appendf(buf, "0\n"); |
Willy Tarreau | 231ec39 | 2019-05-17 10:39:47 +0200 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 87 | chunk_appendf(buf, |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 88 | "%p (%s) calls=%u last=%llu%s\n", |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 89 | 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 93 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 94 | chunk_appendf(buf, "%s" |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 95 | " fct=%p (%s) ctx=%p\n", |
| 96 | pfx, |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 97 | 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 101 | "?", |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 102 | task->context); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 105 | /* 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 | */ |
| 108 | static 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 Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 121 | chunk_reset(&trash); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 122 | while (thr < global.nbthread) { |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 123 | ha_thread_dump(&trash, thr, tid); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 124 | thr++; |
| 125 | } |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame^] | 126 | |
| 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | /* register cli keywords */ |
| 137 | static 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 | |
| 142 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |