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 | 20db911 | 2019-05-17 14:14:35 +0200 | [diff] [blame] | 87 | 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 99 | |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 100 | chunk_appendf(buf, "%s" |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 101 | " fct=%p (%s) ctx=%p\n", |
| 102 | pfx, |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 103 | 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 107 | "?", |
Willy Tarreau | 14a1ab7 | 2019-05-17 10:34:25 +0200 | [diff] [blame] | 108 | task->context); |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 111 | /* 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 | */ |
| 114 | static 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 Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 127 | chunk_reset(&trash); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 128 | ha_thread_dump_all_to_trash(); |
Willy Tarreau | 5cf64dd | 2019-05-17 10:36:08 +0200 | [diff] [blame] | 129 | |
| 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 Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 136 | return 1; |
| 137 | } |
| 138 | |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 139 | #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 | */ |
| 144 | void 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 */ |
| 158 | static volatile unsigned long threads_to_dump; |
| 159 | |
| 160 | /* ID of the thread requesting the dump */ |
| 161 | static 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 | */ |
| 167 | struct buffer *thread_dump_buffer = NULL; |
| 168 | |
| 169 | void 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 Tarreau | 522cfbc | 2019-05-03 10:16:39 +0200 | [diff] [blame^] | 187 | pthread_kill(thread_info[thr].pthread, DEBUGSIG); |
Willy Tarreau | c7091d8 | 2019-05-17 10:08:49 +0200 | [diff] [blame] | 188 | } |
| 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 */ |
| 195 | void 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 | |
| 233 | static 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 | |
| 244 | static 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 | |
| 256 | REGISTER_POST_CHECK(init_debug); |
| 257 | REGISTER_PER_THREAD_INIT(init_debug_per_thread); |
| 258 | |
| 259 | #endif /* USE_THREAD_DUMP */ |
| 260 | |
Willy Tarreau | 4e2b646 | 2019-05-16 17:44:30 +0200 | [diff] [blame] | 261 | /* register cli keywords */ |
| 262 | static 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 | |
| 267 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |