blob: 1fafeab0bffd4daa61feb720cb171f0983f67fe5 [file] [log] [blame]
Willy Tarreau609aad92018-11-22 08:31:09 +01001/*
2 * activity measurement functions.
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
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
Willy Tarreaub2551052020-06-09 09:07:15 +020013#include <haproxy/activity-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreaue8d006a2022-05-05 14:19:00 +020015#include <haproxy/applet.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020016#include <haproxy/cfgparse.h>
Willy Tarreau55542642021-10-08 09:33:24 +020017#include <haproxy/clock.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020018#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020019#include <haproxy/cli.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/freq_ctr.h>
Willy Tarreauf9607f82022-11-25 15:32:38 +010021#include <haproxy/listener.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020022#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020023#include <haproxy/stconn.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020024#include <haproxy/tools.h>
Willy Tarreau75c62c22018-11-22 11:02:09 +010025
Willy Tarreaue8d006a2022-05-05 14:19:00 +020026/* CLI context for the "show profiling" command */
27struct show_prof_ctx {
28 int dump_step; /* 0,1,2,4,5,6; see cli_iohandler_show_profiling() */
29 int linenum; /* next line to be dumped (starts at 0) */
30 int maxcnt; /* max line count per step (0=not set) */
Willy Tarreaue86bc352022-09-08 16:38:10 +020031 int by_what; /* 0=sort by usage, 1=sort by address, 2=sort by time */
Willy Tarreaudc89b182022-09-08 16:05:57 +020032 int aggr; /* 0=dump raw, 1=aggregate on callee */
Willy Tarreaue8d006a2022-05-05 14:19:00 +020033};
34
Willy Tarreauf9607f82022-11-25 15:32:38 +010035/* CLI context for the "show activity" command */
36struct show_activity_ctx {
37 int thr; /* thread ID to show or -1 for all */
Willy Tarreau6ed0b982023-05-03 15:52:19 +020038 int line; /* line number being dumped */
Willy Tarreau8b3e39e2023-05-03 16:18:30 +020039 int col; /* columnline being dumped, 0 to nbt+1 */
Willy Tarreauf9607f82022-11-25 15:32:38 +010040};
41
Willy Tarreauf93c7be2021-05-05 17:07:09 +020042#if defined(DEBUG_MEM_STATS)
43/* these ones are macros in bug.h when DEBUG_MEM_STATS is set, and will
44 * prevent the new ones from being redefined.
45 */
46#undef calloc
47#undef malloc
48#undef realloc
49#endif
Willy Tarreau75c62c22018-11-22 11:02:09 +010050
51/* bit field of profiling options. Beware, may be modified at runtime! */
Willy Tarreauef7380f2021-05-05 16:28:31 +020052unsigned int profiling __read_mostly = HA_PROF_TASKS_AOFF;
Willy Tarreau609aad92018-11-22 08:31:09 +010053
54/* One struct per thread containing all collected measurements */
55struct activity activity[MAX_THREADS] __attribute__((aligned(64))) = { };
56
Willy Tarreaua3423872022-09-07 18:49:55 +020057/* One struct per function pointer hash entry (SCHED_ACT_HASH_BUCKETS values, 0=collision) */
58struct sched_activity sched_activity[SCHED_ACT_HASH_BUCKETS] __attribute__((aligned(64))) = { };
Willy Tarreau609aad92018-11-22 08:31:09 +010059
Willy Tarreaudb87fc72021-05-05 16:50:40 +020060
Willy Tarreaue15615c2021-08-28 12:04:25 +020061#ifdef USE_MEMORY_PROFILING
Willy Tarreau616491b2021-05-11 09:26:23 +020062
63static const char *const memprof_methods[MEMPROF_METH_METHODS] = {
Willy Tarreaufacfad22022-08-17 09:12:53 +020064 "unknown", "malloc", "calloc", "realloc", "free", "p_alloc", "p_free",
Willy Tarreau616491b2021-05-11 09:26:23 +020065};
66
Willy Tarreaudb87fc72021-05-05 16:50:40 +020067/* last one is for hash collisions ("others") and has no caller address */
68struct memprof_stats memprof_stats[MEMPROF_HASH_BUCKETS + 1] = { };
69
Willy Tarreauf93c7be2021-05-05 17:07:09 +020070/* used to detect recursive calls */
71static THREAD_LOCAL int in_memprof = 0;
72
Willy Tarreauf93c7be2021-05-05 17:07:09 +020073/* These ones are used by glibc and will be called early. They are in charge of
74 * initializing the handlers with the original functions.
75 */
76static void *memprof_malloc_initial_handler(size_t size);
77static void *memprof_calloc_initial_handler(size_t nmemb, size_t size);
78static void *memprof_realloc_initial_handler(void *ptr, size_t size);
79static void memprof_free_initial_handler(void *ptr);
80
81/* Fallback handlers for the main alloc/free functions. They are preset to
82 * the initializer in order to save a test in the functions's critical path.
83 */
84static void *(*memprof_malloc_handler)(size_t size) = memprof_malloc_initial_handler;
85static void *(*memprof_calloc_handler)(size_t nmemb, size_t size) = memprof_calloc_initial_handler;
86static void *(*memprof_realloc_handler)(void *ptr, size_t size) = memprof_realloc_initial_handler;
87static void (*memprof_free_handler)(void *ptr) = memprof_free_initial_handler;
88
89/* Used to force to die if it's not possible to retrieve the allocation
90 * functions. We cannot even use stdio in this case.
91 */
92static __attribute__((noreturn)) void memprof_die(const char *msg)
93{
94 DISGUISE(write(2, msg, strlen(msg)));
95 exit(1);
96}
97
98/* Resolve original allocation functions and initialize all handlers.
99 * This must be called very early at boot, before the very first malloc()
100 * call, and is not thread-safe! It's not even possible to use stdio there.
101 * Worse, we have to account for the risk of reentrance from dlsym() when
102 * it tries to prepare its error messages. Here its ahndled by in_memprof
103 * that makes allocators return NULL. dlsym() handles it gracefully. An
Ilya Shipitsin3df59892021-05-10 12:50:00 +0500104 * alternate approach consists in calling aligned_alloc() from these places
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200105 * but that would mean not being able to intercept it later if considered
106 * useful to do so.
107 */
108static void memprof_init()
109{
110 in_memprof++;
111 memprof_malloc_handler = get_sym_next_addr("malloc");
112 if (!memprof_malloc_handler)
113 memprof_die("FATAL: malloc() function not found.\n");
114
115 memprof_calloc_handler = get_sym_next_addr("calloc");
116 if (!memprof_calloc_handler)
117 memprof_die("FATAL: calloc() function not found.\n");
118
119 memprof_realloc_handler = get_sym_next_addr("realloc");
120 if (!memprof_realloc_handler)
121 memprof_die("FATAL: realloc() function not found.\n");
122
123 memprof_free_handler = get_sym_next_addr("free");
124 if (!memprof_free_handler)
125 memprof_die("FATAL: free() function not found.\n");
126 in_memprof--;
127}
128
129/* the initial handlers will initialize all regular handlers and will call the
130 * one they correspond to. A single one of these functions will typically be
131 * called, though it's unknown which one (as any might be called before main).
132 */
133static void *memprof_malloc_initial_handler(size_t size)
134{
135 if (in_memprof) {
136 /* it's likely that dlsym() needs malloc(), let's fail */
137 return NULL;
138 }
139
140 memprof_init();
141 return memprof_malloc_handler(size);
142}
143
144static void *memprof_calloc_initial_handler(size_t nmemb, size_t size)
145{
146 if (in_memprof) {
147 /* it's likely that dlsym() needs calloc(), let's fail */
148 return NULL;
149 }
150 memprof_init();
151 return memprof_calloc_handler(nmemb, size);
152}
153
154static void *memprof_realloc_initial_handler(void *ptr, size_t size)
155{
156 if (in_memprof) {
157 /* it's likely that dlsym() needs realloc(), let's fail */
158 return NULL;
159 }
160
161 memprof_init();
162 return memprof_realloc_handler(ptr, size);
163}
164
165static void memprof_free_initial_handler(void *ptr)
166{
167 memprof_init();
168 memprof_free_handler(ptr);
169}
170
171/* Assign a bin for the memprof_stats to the return address. May perform a few
172 * attempts before finding the right one, but always succeeds (in the worst
173 * case, returns a default bin). The caller address is atomically set except
174 * for the default one which is never set.
175 */
Willy Tarreau219afa22022-08-17 08:53:36 +0200176struct memprof_stats *memprof_get_bin(const void *ra, enum memprof_method meth)
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200177{
178 int retries = 16; // up to 16 consecutive entries may be tested.
Willy Tarreau4a753282021-05-09 23:18:50 +0200179 const void *old;
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200180 unsigned int bin;
181
Willy Tarreau245d32f2022-09-07 11:20:01 +0200182 bin = ptr_hash(ra, MEMPROF_HASH_BITS);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200183 for (; memprof_stats[bin].caller != ra; bin = (bin + 1) & (MEMPROF_HASH_BUCKETS - 1)) {
184 if (!--retries) {
185 bin = MEMPROF_HASH_BUCKETS;
186 break;
187 }
188
189 old = NULL;
190 if (!memprof_stats[bin].caller &&
Willy Tarreau616491b2021-05-11 09:26:23 +0200191 HA_ATOMIC_CAS(&memprof_stats[bin].caller, &old, ra)) {
192 memprof_stats[bin].method = meth;
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200193 break;
Willy Tarreau616491b2021-05-11 09:26:23 +0200194 }
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200195 }
196 return &memprof_stats[bin];
197}
198
199/* This is the new global malloc() function. It must optimize for the normal
200 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
201 * It must remain simple to guarantee the lack of reentrance. stdio is not
202 * possible there even for debugging. The reported size is the really allocated
203 * one as returned by malloc_usable_size(), because this will allow it to be
204 * compared to the one before realloc() or free(). This is a GNU and jemalloc
205 * extension but other systems may also store this size in ptr[-1].
206 */
207void *malloc(size_t size)
208{
209 struct memprof_stats *bin;
210 void *ret;
211
212 if (likely(!(profiling & HA_PROF_MEMORY)))
213 return memprof_malloc_handler(size);
214
215 ret = memprof_malloc_handler(size);
Willy Tarreau1de51eb2021-10-22 16:33:53 +0200216 size = malloc_usable_size(ret) + sizeof(void *);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200217
Willy Tarreau616491b2021-05-11 09:26:23 +0200218 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_MALLOC);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200219 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
220 _HA_ATOMIC_ADD(&bin->alloc_tot, size);
221 return ret;
222}
223
224/* This is the new global calloc() function. It must optimize for the normal
225 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
226 * It must remain simple to guarantee the lack of reentrance. stdio is not
227 * possible there even for debugging. The reported size is the really allocated
228 * one as returned by malloc_usable_size(), because this will allow it to be
229 * compared to the one before realloc() or free(). This is a GNU and jemalloc
230 * extension but other systems may also store this size in ptr[-1].
231 */
232void *calloc(size_t nmemb, size_t size)
233{
234 struct memprof_stats *bin;
235 void *ret;
236
237 if (likely(!(profiling & HA_PROF_MEMORY)))
238 return memprof_calloc_handler(nmemb, size);
239
240 ret = memprof_calloc_handler(nmemb, size);
Willy Tarreau1de51eb2021-10-22 16:33:53 +0200241 size = malloc_usable_size(ret) + sizeof(void *);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200242
Willy Tarreau616491b2021-05-11 09:26:23 +0200243 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_CALLOC);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200244 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
245 _HA_ATOMIC_ADD(&bin->alloc_tot, size);
246 return ret;
247}
248
249/* This is the new global realloc() function. It must optimize for the normal
250 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
251 * It must remain simple to guarantee the lack of reentrance. stdio is not
252 * possible there even for debugging. The reported size is the really allocated
253 * one as returned by malloc_usable_size(), because this will allow it to be
254 * compared to the one before realloc() or free(). This is a GNU and jemalloc
255 * extension but other systems may also store this size in ptr[-1].
256 * Depending on the old vs new size, it's considered as an allocation or a free
257 * (or neither if the size remains the same).
258 */
259void *realloc(void *ptr, size_t size)
260{
261 struct memprof_stats *bin;
262 size_t size_before;
263 void *ret;
264
265 if (likely(!(profiling & HA_PROF_MEMORY)))
266 return memprof_realloc_handler(ptr, size);
267
268 size_before = malloc_usable_size(ptr);
269 ret = memprof_realloc_handler(ptr, size);
Willy Tarreau2639e2e2021-05-07 08:01:35 +0200270 size = malloc_usable_size(ret);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200271
Willy Tarreau1de51eb2021-10-22 16:33:53 +0200272 /* only count the extra link for new allocations */
273 if (!ptr)
274 size += sizeof(void *);
275
Willy Tarreau616491b2021-05-11 09:26:23 +0200276 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_REALLOC);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200277 if (size > size_before) {
278 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
Willy Tarreau79acefa2021-05-11 09:12:56 +0200279 _HA_ATOMIC_ADD(&bin->alloc_tot, size - size_before);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200280 } else if (size < size_before) {
281 _HA_ATOMIC_ADD(&bin->free_calls, 1);
Willy Tarreau79acefa2021-05-11 09:12:56 +0200282 _HA_ATOMIC_ADD(&bin->free_tot, size_before - size);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200283 }
284 return ret;
285}
286
287/* This is the new global free() function. It must optimize for the normal
288 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
289 * It must remain simple to guarantee the lack of reentrance. stdio is not
290 * possible there even for debugging. The reported size is the really allocated
291 * one as returned by malloc_usable_size(), because this will allow it to be
292 * compared to the one before realloc() or free(). This is a GNU and jemalloc
293 * extension but other systems may also store this size in ptr[-1]. Since
294 * free() is often called on NULL pointers to collect garbage at the end of
295 * many functions or during config parsing, as a special case free(NULL)
296 * doesn't update any stats.
297 */
298void free(void *ptr)
299{
300 struct memprof_stats *bin;
301 size_t size_before;
302
303 if (likely(!(profiling & HA_PROF_MEMORY) || !ptr)) {
304 memprof_free_handler(ptr);
305 return;
306 }
307
Willy Tarreau1de51eb2021-10-22 16:33:53 +0200308 size_before = malloc_usable_size(ptr) + sizeof(void *);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200309 memprof_free_handler(ptr);
310
Willy Tarreau616491b2021-05-11 09:26:23 +0200311 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_FREE);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200312 _HA_ATOMIC_ADD(&bin->free_calls, 1);
313 _HA_ATOMIC_ADD(&bin->free_tot, size_before);
314}
315
Willy Tarreaudb87fc72021-05-05 16:50:40 +0200316#endif // USE_MEMORY_PROFILING
317
Willy Tarreau609aad92018-11-22 08:31:09 +0100318/* Updates the current thread's statistics about stolen CPU time. The unit for
319 * <stolen> is half-milliseconds.
320 */
321void report_stolen_time(uint64_t stolen)
322{
323 activity[tid].cpust_total += stolen;
324 update_freq_ctr(&activity[tid].cpust_1s, stolen);
325 update_freq_ctr_period(&activity[tid].cpust_15s, 15000, stolen);
326}
Willy Tarreau75c62c22018-11-22 11:02:09 +0100327
Willy Tarreau20adfde2021-10-08 11:34:46 +0200328/* Update avg_loop value for the current thread and possibly decide to enable
329 * task-level profiling on the current thread based on its average run time.
330 * The <run_time> argument is the number of microseconds elapsed since the
331 * last time poll() returned.
Willy Tarreaue0650222021-10-06 16:22:09 +0200332 */
Willy Tarreau20adfde2021-10-08 11:34:46 +0200333void activity_count_runtime(uint32_t run_time)
Willy Tarreaue0650222021-10-06 16:22:09 +0200334{
Willy Tarreaue0650222021-10-06 16:22:09 +0200335 uint32_t up, down;
336
337 /* 1 millisecond per loop on average over last 1024 iterations is
338 * enough to turn on profiling.
339 */
340 up = 1000;
341 down = up * 99 / 100;
342
Willy Tarreaue0650222021-10-06 16:22:09 +0200343 run_time = swrate_add(&activity[tid].avg_loop_us, TIME_STATS_SAMPLES, run_time);
344
345 /* In automatic mode, reaching the "up" threshold on average switches
346 * profiling to "on" when automatic, and going back below the "down"
347 * threshold switches to off. The forced modes don't check the load.
348 */
Willy Tarreaubdcd3252022-06-22 09:19:46 +0200349 if (!(_HA_ATOMIC_LOAD(&th_ctx->flags) & TH_FL_TASK_PROFILING)) {
Willy Tarreaue0650222021-10-06 16:22:09 +0200350 if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_ON ||
351 ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AON &&
352 swrate_avg(run_time, TIME_STATS_SAMPLES) >= up)))
Willy Tarreau680ed5f2022-06-13 15:59:39 +0200353 _HA_ATOMIC_OR(&th_ctx->flags, TH_FL_TASK_PROFILING);
Willy Tarreaue0650222021-10-06 16:22:09 +0200354 } else {
355 if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_OFF ||
356 ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AOFF &&
357 swrate_avg(run_time, TIME_STATS_SAMPLES) <= down)))
Willy Tarreau680ed5f2022-06-13 15:59:39 +0200358 _HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_TASK_PROFILING);
Willy Tarreaue0650222021-10-06 16:22:09 +0200359 }
360}
361
Willy Tarreauca3afc22021-05-05 18:33:19 +0200362#ifdef USE_MEMORY_PROFILING
363/* config parser for global "profiling.memory", accepts "on" or "off" */
364static int cfg_parse_prof_memory(char **args, int section_type, struct proxy *curpx,
365 const struct proxy *defpx, const char *file, int line,
366 char **err)
367{
368 if (too_many_args(1, args, err, NULL))
369 return -1;
370
371 if (strcmp(args[1], "on") == 0)
372 profiling |= HA_PROF_MEMORY;
373 else if (strcmp(args[1], "off") == 0)
374 profiling &= ~HA_PROF_MEMORY;
375 else {
376 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
377 return -1;
378 }
379 return 0;
380}
381#endif // USE_MEMORY_PROFILING
382
Willy Tarreau75c62c22018-11-22 11:02:09 +0100383/* config parser for global "profiling.tasks", accepts "on" or "off" */
384static int cfg_parse_prof_tasks(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100385 const struct proxy *defpx, const char *file, int line,
Willy Tarreau75c62c22018-11-22 11:02:09 +0100386 char **err)
387{
388 if (too_many_args(1, args, err, NULL))
389 return -1;
390
391 if (strcmp(args[1], "on") == 0)
Willy Tarreaud2d33482019-04-25 17:09:07 +0200392 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON;
393 else if (strcmp(args[1], "auto") == 0)
Willy Tarreauaa622b82021-01-28 21:44:22 +0100394 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100395 else if (strcmp(args[1], "off") == 0)
Willy Tarreaud2d33482019-04-25 17:09:07 +0200396 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100397 else {
Willy Tarreaud2d33482019-04-25 17:09:07 +0200398 memprintf(err, "'%s' expects either 'on', 'auto', or 'off' but got '%s'.", args[0], args[1]);
Willy Tarreau75c62c22018-11-22 11:02:09 +0100399 return -1;
400 }
401 return 0;
402}
403
404/* parse a "set profiling" command. It always returns 1. */
405static int cli_parse_set_profiling(char **args, char *payload, struct appctx *appctx, void *private)
406{
Willy Tarreau75c62c22018-11-22 11:02:09 +0100407 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
408 return 1;
409
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200410 if (strcmp(args[2], "memory") == 0) {
Willy Tarreaudb87fc72021-05-05 16:50:40 +0200411#ifdef USE_MEMORY_PROFILING
412 if (strcmp(args[3], "on") == 0) {
413 unsigned int old = profiling;
414 int i;
415
416 while (!_HA_ATOMIC_CAS(&profiling, &old, old | HA_PROF_MEMORY))
417 ;
418
419 /* also flush current profiling stats */
420 for (i = 0; i < sizeof(memprof_stats) / sizeof(memprof_stats[0]); i++) {
421 HA_ATOMIC_STORE(&memprof_stats[i].alloc_calls, 0);
422 HA_ATOMIC_STORE(&memprof_stats[i].free_calls, 0);
423 HA_ATOMIC_STORE(&memprof_stats[i].alloc_tot, 0);
424 HA_ATOMIC_STORE(&memprof_stats[i].free_tot, 0);
425 HA_ATOMIC_STORE(&memprof_stats[i].caller, NULL);
426 }
427 }
428 else if (strcmp(args[3], "off") == 0) {
429 unsigned int old = profiling;
430
431 while (!_HA_ATOMIC_CAS(&profiling, &old, old & ~HA_PROF_MEMORY))
432 ;
433 }
434 else
435 return cli_err(appctx, "Expects either 'on' or 'off'.\n");
436 return 1;
437#else
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200438 return cli_err(appctx, "Memory profiling not compiled in.\n");
Willy Tarreaudb87fc72021-05-05 16:50:40 +0200439#endif
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200440 }
441
Willy Tarreau9d008692019-08-09 11:21:01 +0200442 if (strcmp(args[2], "tasks") != 0)
Ilya Shipitsin3df59892021-05-10 12:50:00 +0500443 return cli_err(appctx, "Expects either 'tasks' or 'memory'.\n");
Willy Tarreau75c62c22018-11-22 11:02:09 +0100444
Willy Tarreaud2d33482019-04-25 17:09:07 +0200445 if (strcmp(args[3], "on") == 0) {
446 unsigned int old = profiling;
Willy Tarreaucfa71012021-01-29 11:56:21 +0100447 int i;
448
Willy Tarreaud2d33482019-04-25 17:09:07 +0200449 while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON))
450 ;
Willy Tarreaucfa71012021-01-29 11:56:21 +0100451 /* also flush current profiling stats */
Willy Tarreaua3423872022-09-07 18:49:55 +0200452 for (i = 0; i < SCHED_ACT_HASH_BUCKETS; i++) {
Willy Tarreaucfa71012021-01-29 11:56:21 +0100453 HA_ATOMIC_STORE(&sched_activity[i].calls, 0);
454 HA_ATOMIC_STORE(&sched_activity[i].cpu_time, 0);
455 HA_ATOMIC_STORE(&sched_activity[i].lat_time, 0);
456 HA_ATOMIC_STORE(&sched_activity[i].func, NULL);
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200457 HA_ATOMIC_STORE(&sched_activity[i].caller, NULL);
Willy Tarreaucfa71012021-01-29 11:56:21 +0100458 }
Willy Tarreaud2d33482019-04-25 17:09:07 +0200459 }
460 else if (strcmp(args[3], "auto") == 0) {
461 unsigned int old = profiling;
Willy Tarreauaa622b82021-01-28 21:44:22 +0100462 unsigned int new;
463
464 do {
465 if ((old & HA_PROF_TASKS_MASK) >= HA_PROF_TASKS_AON)
466 new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AON;
467 else
468 new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF;
469 } while (!_HA_ATOMIC_CAS(&profiling, &old, new));
Willy Tarreaud2d33482019-04-25 17:09:07 +0200470 }
471 else if (strcmp(args[3], "off") == 0) {
472 unsigned int old = profiling;
473 while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF))
474 ;
475 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200476 else
477 return cli_err(appctx, "Expects 'on', 'auto', or 'off'.\n");
478
Willy Tarreau75c62c22018-11-22 11:02:09 +0100479 return 1;
480}
481
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200482static int cmp_sched_activity_calls(const void *a, const void *b)
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100483{
484 const struct sched_activity *l = (const struct sched_activity *)a;
485 const struct sched_activity *r = (const struct sched_activity *)b;
486
487 if (l->calls > r->calls)
488 return -1;
489 else if (l->calls < r->calls)
490 return 1;
491 else
492 return 0;
493}
494
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200495/* sort by address first, then by call count */
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200496static int cmp_sched_activity_addr(const void *a, const void *b)
497{
498 const struct sched_activity *l = (const struct sched_activity *)a;
499 const struct sched_activity *r = (const struct sched_activity *)b;
500
501 if (l->func > r->func)
502 return -1;
503 else if (l->func < r->func)
504 return 1;
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200505 else if (l->calls > r->calls)
506 return -1;
507 else if (l->calls < r->calls)
508 return 1;
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200509 else
510 return 0;
511}
512
Willy Tarreaue86bc352022-09-08 16:38:10 +0200513/* sort by cpu time first, then by inverse call count (to spot highest offenders) */
514static int cmp_sched_activity_cpu(const void *a, const void *b)
515{
516 const struct sched_activity *l = (const struct sched_activity *)a;
517 const struct sched_activity *r = (const struct sched_activity *)b;
518
519 if (l->cpu_time > r->cpu_time)
520 return -1;
521 else if (l->cpu_time < r->cpu_time)
522 return 1;
523 else if (l->calls < r->calls)
524 return -1;
525 else if (l->calls > r->calls)
526 return 1;
527 else
528 return 0;
529}
530
Willy Tarreaue15615c2021-08-28 12:04:25 +0200531#ifdef USE_MEMORY_PROFILING
Willy Tarreau993d44d2021-05-05 18:07:02 +0200532/* used by qsort below */
533static int cmp_memprof_stats(const void *a, const void *b)
534{
535 const struct memprof_stats *l = (const struct memprof_stats *)a;
536 const struct memprof_stats *r = (const struct memprof_stats *)b;
537
538 if (l->alloc_tot + l->free_tot > r->alloc_tot + r->free_tot)
539 return -1;
540 else if (l->alloc_tot + l->free_tot < r->alloc_tot + r->free_tot)
541 return 1;
542 else
543 return 0;
544}
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200545
546static int cmp_memprof_addr(const void *a, const void *b)
547{
548 const struct memprof_stats *l = (const struct memprof_stats *)a;
549 const struct memprof_stats *r = (const struct memprof_stats *)b;
550
551 if (l->caller > r->caller)
552 return -1;
553 else if (l->caller < r->caller)
554 return 1;
555 else
556 return 0;
557}
Willy Tarreau993d44d2021-05-05 18:07:02 +0200558#endif // USE_MEMORY_PROFILING
559
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200560/* Computes the index of function pointer <func> and caller <caller> for use
561 * with sched_activity[] or any other similar array passed in <array>, and
562 * returns a pointer to the entry after having atomically assigned it to this
563 * function pointer and caller combination. Note that in case of collision,
564 * the first entry is returned instead ("other").
Willy Tarreaua26be372021-10-06 16:26:33 +0200565 */
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200566struct sched_activity *sched_activity_entry(struct sched_activity *array, const void *func, const void *caller)
Willy Tarreaua26be372021-10-06 16:26:33 +0200567{
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200568 uint32_t hash = ptr2_hash(func, caller, SCHED_ACT_HASH_BITS);
Willy Tarreaua26be372021-10-06 16:26:33 +0200569 struct sched_activity *ret;
Willy Tarreau64435aa2022-09-07 18:54:30 +0200570 const void *old;
571 int tries = 16;
Willy Tarreaua26be372021-10-06 16:26:33 +0200572
Willy Tarreau64435aa2022-09-07 18:54:30 +0200573 for (tries = 16; tries > 0; tries--, hash++) {
574 ret = &array[hash];
Willy Tarreaua26be372021-10-06 16:26:33 +0200575
Willy Tarreau64435aa2022-09-07 18:54:30 +0200576 while (1) {
577 if (likely(ret->func)) {
578 if (likely(ret->func == func && ret->caller == caller))
579 return ret;
580 break;
581 }
Willy Tarreaua26be372021-10-06 16:26:33 +0200582
Willy Tarreau64435aa2022-09-07 18:54:30 +0200583 /* try to create the new entry. Func is sufficient to
584 * reserve the node.
585 */
586 old = NULL;
587 if (HA_ATOMIC_CAS(&ret->func, &old, func)) {
588 ret->caller = caller;
589 return ret;
590 }
591 /* changed in parallel, check again */
592 }
593 }
Willy Tarreaua26be372021-10-06 16:26:33 +0200594
595 return array;
596}
597
Willy Tarreau75c62c22018-11-22 11:02:09 +0100598/* This function dumps all profiling settings. It returns 0 if the output
599 * buffer is full and it needs to be called again, otherwise non-zero.
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200600 * It dumps some parts depending on the following states from show_prof_ctx:
601 * dump_step:
Willy Tarreau637d85a2021-05-05 17:33:27 +0200602 * 0, 4: dump status, then jump to 1 if 0
603 * 1, 5: dump tasks, then jump to 2 if 1
604 * 2, 6: dump memory, then stop
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200605 * linenum:
Willy Tarreau637d85a2021-05-05 17:33:27 +0200606 * restart line for each step (starts at zero)
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200607 * maxcnt:
Willy Tarreau637d85a2021-05-05 17:33:27 +0200608 * may contain a configured max line count for each step (0=not set)
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200609 * byaddr:
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200610 * 0: sort by usage
611 * 1: sort by address
Willy Tarreau75c62c22018-11-22 11:02:09 +0100612 */
613static int cli_io_handler_show_profiling(struct appctx *appctx)
614{
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200615 struct show_prof_ctx *ctx = appctx->svcctx;
Willy Tarreaua3423872022-09-07 18:49:55 +0200616 struct sched_activity tmp_activity[SCHED_ACT_HASH_BUCKETS] __attribute__((aligned(64)));
Willy Tarreaue15615c2021-08-28 12:04:25 +0200617#ifdef USE_MEMORY_PROFILING
Willy Tarreau993d44d2021-05-05 18:07:02 +0200618 struct memprof_stats tmp_memstats[MEMPROF_HASH_BUCKETS + 1];
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200619 unsigned long long tot_alloc_calls, tot_free_calls;
620 unsigned long long tot_alloc_bytes, tot_free_bytes;
Willy Tarreau993d44d2021-05-05 18:07:02 +0200621#endif
Willy Tarreauc12b3212022-05-27 11:08:15 +0200622 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100623 struct buffer *name_buffer = get_trash_chunk();
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200624 const struct ha_caller *caller;
Willy Tarreaud2d33482019-04-25 17:09:07 +0200625 const char *str;
Willy Tarreau637d85a2021-05-05 17:33:27 +0200626 int max_lines;
Willy Tarreaudc89b182022-09-08 16:05:57 +0200627 int i, j, max;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100628
Christopher Faulet87633c32023-04-03 18:32:50 +0200629 /* FIXME: Don't watch the other side ! */
Christopher Faulet208c7122023-04-13 16:16:15 +0200630 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreau75c62c22018-11-22 11:02:09 +0100631 return 1;
632
633 chunk_reset(&trash);
634
Willy Tarreaud2d33482019-04-25 17:09:07 +0200635 switch (profiling & HA_PROF_TASKS_MASK) {
Willy Tarreauaa622b82021-01-28 21:44:22 +0100636 case HA_PROF_TASKS_AOFF: str="auto-off"; break;
637 case HA_PROF_TASKS_AON: str="auto-on"; break;
Willy Tarreaud2d33482019-04-25 17:09:07 +0200638 case HA_PROF_TASKS_ON: str="on"; break;
639 default: str="off"; break;
640 }
641
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200642 if ((ctx->dump_step & 3) != 0)
Willy Tarreau637d85a2021-05-05 17:33:27 +0200643 goto skip_status;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100644
Willy Tarreaud2d33482019-04-25 17:09:07 +0200645 chunk_printf(&trash,
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200646 "Per-task CPU profiling : %-8s # set profiling tasks {on|auto|off}\n"
647 "Memory usage profiling : %-8s # set profiling memory {on|off}\n",
648 str, (profiling & HA_PROF_MEMORY) ? "on" : "off");
Willy Tarreau75c62c22018-11-22 11:02:09 +0100649
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200650 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau637d85a2021-05-05 17:33:27 +0200651 /* failed, try again */
Willy Tarreau637d85a2021-05-05 17:33:27 +0200652 return 0;
653 }
654
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200655 ctx->linenum = 0; // reset first line to dump
656 if ((ctx->dump_step & 4) == 0)
657 ctx->dump_step++; // next step
Willy Tarreau637d85a2021-05-05 17:33:27 +0200658
659 skip_status:
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200660 if ((ctx->dump_step & 3) != 1)
Willy Tarreau637d85a2021-05-05 17:33:27 +0200661 goto skip_tasks;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100662
Willy Tarreau637d85a2021-05-05 17:33:27 +0200663 memcpy(tmp_activity, sched_activity, sizeof(tmp_activity));
Willy Tarreaudc89b182022-09-08 16:05:57 +0200664 /* for addr sort and for callee aggregation we have to first sort by address */
Willy Tarreaue86bc352022-09-08 16:38:10 +0200665 if (ctx->aggr || ctx->by_what == 1) // sort by addr
Willy Tarreaudc89b182022-09-08 16:05:57 +0200666 qsort(tmp_activity, SCHED_ACT_HASH_BUCKETS, sizeof(tmp_activity[0]), cmp_sched_activity_addr);
667
668 if (ctx->aggr) {
669 /* merge entries for the same callee and reset their count */
670 for (i = j = 0; i < SCHED_ACT_HASH_BUCKETS; i = j) {
671 for (j = i + 1; j < SCHED_ACT_HASH_BUCKETS && tmp_activity[j].func == tmp_activity[i].func; j++) {
672 tmp_activity[i].calls += tmp_activity[j].calls;
673 tmp_activity[i].cpu_time += tmp_activity[j].cpu_time;
674 tmp_activity[i].lat_time += tmp_activity[j].lat_time;
675 tmp_activity[j].calls = 0;
676 }
677 }
678 }
679
Willy Tarreaue86bc352022-09-08 16:38:10 +0200680 if (!ctx->by_what) // sort by usage
Willy Tarreaua3423872022-09-07 18:49:55 +0200681 qsort(tmp_activity, SCHED_ACT_HASH_BUCKETS, sizeof(tmp_activity[0]), cmp_sched_activity_calls);
Willy Tarreaue86bc352022-09-08 16:38:10 +0200682 else if (ctx->by_what == 2) // by cpu_tot
683 qsort(tmp_activity, SCHED_ACT_HASH_BUCKETS, sizeof(tmp_activity[0]), cmp_sched_activity_cpu);
Willy Tarreau637d85a2021-05-05 17:33:27 +0200684
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200685 if (!ctx->linenum)
Willy Tarreau637d85a2021-05-05 17:33:27 +0200686 chunk_appendf(&trash, "Tasks activity:\n"
687 " function calls cpu_tot cpu_avg lat_tot lat_avg\n");
688
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200689 max_lines = ctx->maxcnt;
Willy Tarreau637d85a2021-05-05 17:33:27 +0200690 if (!max_lines)
Willy Tarreaua3423872022-09-07 18:49:55 +0200691 max_lines = SCHED_ACT_HASH_BUCKETS;
Willy Tarreau637d85a2021-05-05 17:33:27 +0200692
Willy Tarreaudc89b182022-09-08 16:05:57 +0200693 for (i = ctx->linenum; i < max_lines; i++) {
694 if (!tmp_activity[i].calls)
695 continue; // skip aggregated or empty entries
696
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200697 ctx->linenum = i;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100698 chunk_reset(name_buffer);
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200699 caller = HA_ATOMIC_LOAD(&tmp_activity[i].caller);
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100700
701 if (!tmp_activity[i].func)
702 chunk_printf(name_buffer, "other");
703 else
704 resolve_sym_name(name_buffer, "", tmp_activity[i].func);
705
706 /* reserve 35 chars for name+' '+#calls, knowing that longer names
707 * are often used for less often called functions.
708 */
709 max = 35 - name_buffer->data;
710 if (max < 1)
711 max = 1;
712 chunk_appendf(&trash, " %s%*llu", name_buffer->area, max, (unsigned long long)tmp_activity[i].calls);
713
714 print_time_short(&trash, " ", tmp_activity[i].cpu_time, "");
715 print_time_short(&trash, " ", tmp_activity[i].cpu_time / tmp_activity[i].calls, "");
716 print_time_short(&trash, " ", tmp_activity[i].lat_time, "");
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200717 print_time_short(&trash, " ", tmp_activity[i].lat_time / tmp_activity[i].calls, "");
718
Willy Tarreaudc89b182022-09-08 16:05:57 +0200719 if (caller && !ctx->aggr && caller->what <= WAKEUP_TYPE_APPCTX_WAKEUP)
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200720 chunk_appendf(&trash, " <- %s@%s:%d %s",
721 caller->func, caller->file, caller->line,
722 task_wakeup_type_str(caller->what));
723
724 b_putchr(&trash, '\n');
Willy Tarreau637d85a2021-05-05 17:33:27 +0200725
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200726 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau637d85a2021-05-05 17:33:27 +0200727 /* failed, try again */
Willy Tarreau637d85a2021-05-05 17:33:27 +0200728 return 0;
729 }
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100730 }
731
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200732 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau75c62c22018-11-22 11:02:09 +0100733 /* failed, try again */
Willy Tarreau75c62c22018-11-22 11:02:09 +0100734 return 0;
735 }
Willy Tarreau637d85a2021-05-05 17:33:27 +0200736
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200737 ctx->linenum = 0; // reset first line to dump
738 if ((ctx->dump_step & 4) == 0)
739 ctx->dump_step++; // next step
Willy Tarreau637d85a2021-05-05 17:33:27 +0200740
741 skip_tasks:
742
Willy Tarreaue15615c2021-08-28 12:04:25 +0200743#ifdef USE_MEMORY_PROFILING
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200744 if ((ctx->dump_step & 3) != 2)
Willy Tarreau993d44d2021-05-05 18:07:02 +0200745 goto skip_mem;
746
747 memcpy(tmp_memstats, memprof_stats, sizeof(tmp_memstats));
Willy Tarreaue86bc352022-09-08 16:38:10 +0200748 if (ctx->by_what)
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200749 qsort(tmp_memstats, MEMPROF_HASH_BUCKETS+1, sizeof(tmp_memstats[0]), cmp_memprof_addr);
750 else
751 qsort(tmp_memstats, MEMPROF_HASH_BUCKETS+1, sizeof(tmp_memstats[0]), cmp_memprof_stats);
Willy Tarreau993d44d2021-05-05 18:07:02 +0200752
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200753 if (!ctx->linenum)
Willy Tarreau993d44d2021-05-05 18:07:02 +0200754 chunk_appendf(&trash,
755 "Alloc/Free statistics by call place:\n"
Willy Tarreau616491b2021-05-11 09:26:23 +0200756 " Calls | Tot Bytes | Caller and method\n"
Willy Tarreau993d44d2021-05-05 18:07:02 +0200757 "<- alloc -> <- free ->|<-- alloc ---> <-- free ---->|\n");
758
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200759 max_lines = ctx->maxcnt;
Willy Tarreau993d44d2021-05-05 18:07:02 +0200760 if (!max_lines)
761 max_lines = MEMPROF_HASH_BUCKETS + 1;
762
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200763 for (i = ctx->linenum; i < max_lines; i++) {
Willy Tarreau993d44d2021-05-05 18:07:02 +0200764 struct memprof_stats *entry = &tmp_memstats[i];
765
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200766 ctx->linenum = i;
Willy Tarreau993d44d2021-05-05 18:07:02 +0200767 if (!entry->alloc_calls && !entry->free_calls)
768 continue;
769 chunk_appendf(&trash, "%11llu %11llu %14llu %14llu| %16p ",
770 entry->alloc_calls, entry->free_calls,
771 entry->alloc_tot, entry->free_tot,
772 entry->caller);
773
774 if (entry->caller)
775 resolve_sym_name(&trash, NULL, entry->caller);
776 else
777 chunk_appendf(&trash, "[other]");
778
Willy Tarreau8cce4d72021-10-22 16:26:12 +0200779 chunk_appendf(&trash," %s(%lld)", memprof_methods[entry->method],
Willy Tarreau616491b2021-05-11 09:26:23 +0200780 (long long)(entry->alloc_tot - entry->free_tot) / (long long)(entry->alloc_calls + entry->free_calls));
Willy Tarreau993d44d2021-05-05 18:07:02 +0200781
Willy Tarreau8cce4d72021-10-22 16:26:12 +0200782 if (entry->alloc_tot && entry->free_tot) {
783 /* that's a realloc, show the total diff to help spot leaks */
784 chunk_appendf(&trash," [delta=%lld]", (long long)(entry->alloc_tot - entry->free_tot));
785 }
786
Willy Tarreau42b180d2022-08-17 09:35:16 +0200787 if (entry->info) {
788 /* that's a pool name */
789 const struct pool_head *pool = entry->info;
790 chunk_appendf(&trash," [pool=%s]", pool->name);
791 }
792
Willy Tarreau8cce4d72021-10-22 16:26:12 +0200793 chunk_appendf(&trash, "\n");
794
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200795 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau993d44d2021-05-05 18:07:02 +0200796 return 0;
Willy Tarreau993d44d2021-05-05 18:07:02 +0200797 }
798
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200799 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau993d44d2021-05-05 18:07:02 +0200800 return 0;
Willy Tarreau993d44d2021-05-05 18:07:02 +0200801
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200802 tot_alloc_calls = tot_free_calls = tot_alloc_bytes = tot_free_bytes = 0;
803 for (i = 0; i < max_lines; i++) {
804 tot_alloc_calls += tmp_memstats[i].alloc_calls;
805 tot_free_calls += tmp_memstats[i].free_calls;
806 tot_alloc_bytes += tmp_memstats[i].alloc_tot;
807 tot_free_bytes += tmp_memstats[i].free_tot;
808 }
809
810 chunk_appendf(&trash,
811 "-----------------------|-----------------------------|\n"
812 "%11llu %11llu %14llu %14llu| <- Total; Delta_calls=%lld; Delta_bytes=%lld\n",
813 tot_alloc_calls, tot_free_calls,
814 tot_alloc_bytes, tot_free_bytes,
815 tot_alloc_calls - tot_free_calls,
816 tot_alloc_bytes - tot_free_bytes);
817
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200818 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200819 return 0;
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200820
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200821 ctx->linenum = 0; // reset first line to dump
822 if ((ctx->dump_step & 4) == 0)
823 ctx->dump_step++; // next step
Willy Tarreau993d44d2021-05-05 18:07:02 +0200824
825 skip_mem:
826#endif // USE_MEMORY_PROFILING
827
Willy Tarreau75c62c22018-11-22 11:02:09 +0100828 return 1;
829}
830
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200831/* parse a "show profiling" command. It returns 1 on failure, 0 if it starts to dump.
832 * - cli.i0 is set to the first state (0=all, 4=status, 5=tasks, 6=memory)
833 * - cli.o1 is set to 1 if the output must be sorted by addr instead of usage
834 * - cli.o0 is set to the number of lines of output
835 */
Willy Tarreau42712cb2021-05-05 17:48:13 +0200836static int cli_parse_show_profiling(char **args, char *payload, struct appctx *appctx, void *private)
837{
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200838 struct show_prof_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200839 int arg;
840
Willy Tarreau42712cb2021-05-05 17:48:13 +0200841 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
842 return 1;
843
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200844 for (arg = 2; *args[arg]; arg++) {
845 if (strcmp(args[arg], "all") == 0) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200846 ctx->dump_step = 0; // will cycle through 0,1,2; default
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200847 }
848 else if (strcmp(args[arg], "status") == 0) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200849 ctx->dump_step = 4; // will visit status only
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200850 }
851 else if (strcmp(args[arg], "tasks") == 0) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200852 ctx->dump_step = 5; // will visit tasks only
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200853 }
854 else if (strcmp(args[arg], "memory") == 0) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200855 ctx->dump_step = 6; // will visit memory only
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200856 }
857 else if (strcmp(args[arg], "byaddr") == 0) {
Willy Tarreaue86bc352022-09-08 16:38:10 +0200858 ctx->by_what = 1; // sort output by address instead of usage
859 }
860 else if (strcmp(args[arg], "bytime") == 0) {
861 ctx->by_what = 2; // sort output by total time instead of usage
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200862 }
Willy Tarreaudc89b182022-09-08 16:05:57 +0200863 else if (strcmp(args[arg], "aggr") == 0) {
864 ctx->aggr = 1; // aggregate output by callee
865 }
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200866 else if (isdigit((unsigned char)*args[arg])) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200867 ctx->maxcnt = atoi(args[arg]); // number of entries to dump
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200868 }
869 else
Willy Tarreaue86bc352022-09-08 16:38:10 +0200870 return cli_err(appctx, "Expects either 'all', 'status', 'tasks', 'memory', 'byaddr', 'bytime', 'aggr' or a max number of output lines.\n");
Willy Tarreau42712cb2021-05-05 17:48:13 +0200871 }
872 return 0;
873}
874
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100875/* This function scans all threads' run queues and collects statistics about
876 * running tasks. It returns 0 if the output buffer is full and it needs to be
877 * called again, otherwise non-zero.
878 */
879static int cli_io_handler_show_tasks(struct appctx *appctx)
880{
Willy Tarreaua3423872022-09-07 18:49:55 +0200881 struct sched_activity tmp_activity[SCHED_ACT_HASH_BUCKETS] __attribute__((aligned(64)));
Willy Tarreauc12b3212022-05-27 11:08:15 +0200882 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100883 struct buffer *name_buffer = get_trash_chunk();
884 struct sched_activity *entry;
885 const struct tasklet *tl;
886 const struct task *t;
887 uint64_t now_ns, lat;
Willy Tarreau319d1362022-06-16 16:28:01 +0200888 struct eb32_node *rqnode;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100889 uint64_t tot_calls;
890 int thr, queue;
891 int i, max;
892
Christopher Faulet87633c32023-04-03 18:32:50 +0200893 /* FIXME: Don't watch the other side ! */
Christopher Faulet208c7122023-04-13 16:16:15 +0200894 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100895 return 1;
896
897 /* It's not possible to scan queues in small chunks and yield in the
898 * middle of the dump and come back again. So what we're doing instead
899 * is to freeze all threads and inspect their queues at once as fast as
900 * possible, using a sched_activity array to collect metrics with
901 * limited collision, then we'll report statistics only. The tasks'
902 * #calls will reflect the number of occurrences, and the lat_time will
Willy Tarreau75f72332021-01-29 15:04:16 +0100903 * reflect the latency when set. We prefer to take the time before
904 * calling thread_isolate() so that the wait time doesn't impact the
905 * measurement accuracy. However this requires to take care of negative
906 * times since tasks might be queued after we retrieve it.
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100907 */
908
909 now_ns = now_mono_time();
910 memset(tmp_activity, 0, sizeof(tmp_activity));
911
912 thread_isolate();
913
914 /* 1. global run queue */
915
916#ifdef USE_THREAD
Willy Tarreau6f780382022-06-16 15:30:50 +0200917 for (thr = 0; thr < global.nbthread; thr++) {
918 /* task run queue */
Willy Tarreau319d1362022-06-16 16:28:01 +0200919 rqnode = eb32_first(&ha_thread_ctx[thr].rqueue_shared);
Willy Tarreau6f780382022-06-16 15:30:50 +0200920 while (rqnode) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200921 t = eb32_entry(rqnode, struct task, rq);
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200922 entry = sched_activity_entry(tmp_activity, t->process, NULL);
Willy Tarreau04e50b32022-09-07 14:49:50 +0200923 if (t->wake_date) {
924 lat = now_ns - t->wake_date;
Willy Tarreau6f780382022-06-16 15:30:50 +0200925 if ((int64_t)lat > 0)
926 entry->lat_time += lat;
927 }
928 entry->calls++;
Willy Tarreau319d1362022-06-16 16:28:01 +0200929 rqnode = eb32_next(rqnode);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100930 }
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100931 }
932#endif
933 /* 2. all threads's local run queues */
934 for (thr = 0; thr < global.nbthread; thr++) {
935 /* task run queue */
Willy Tarreau319d1362022-06-16 16:28:01 +0200936 rqnode = eb32_first(&ha_thread_ctx[thr].rqueue);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100937 while (rqnode) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200938 t = eb32_entry(rqnode, struct task, rq);
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200939 entry = sched_activity_entry(tmp_activity, t->process, NULL);
Willy Tarreau04e50b32022-09-07 14:49:50 +0200940 if (t->wake_date) {
941 lat = now_ns - t->wake_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100942 if ((int64_t)lat > 0)
943 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100944 }
945 entry->calls++;
Willy Tarreau319d1362022-06-16 16:28:01 +0200946 rqnode = eb32_next(rqnode);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100947 }
948
949 /* shared tasklet list */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200950 list_for_each_entry(tl, mt_list_to_list(&ha_thread_ctx[thr].shared_tasklet_list), list) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100951 t = (const struct task *)tl;
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200952 entry = sched_activity_entry(tmp_activity, t->process, NULL);
Willy Tarreau04e50b32022-09-07 14:49:50 +0200953 if (!TASK_IS_TASKLET(t) && t->wake_date) {
954 lat = now_ns - t->wake_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100955 if ((int64_t)lat > 0)
956 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100957 }
958 entry->calls++;
959 }
960
961 /* classful tasklets */
962 for (queue = 0; queue < TL_CLASSES; queue++) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200963 list_for_each_entry(tl, &ha_thread_ctx[thr].tasklets[queue], list) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100964 t = (const struct task *)tl;
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200965 entry = sched_activity_entry(tmp_activity, t->process, NULL);
Willy Tarreau04e50b32022-09-07 14:49:50 +0200966 if (!TASK_IS_TASKLET(t) && t->wake_date) {
967 lat = now_ns - t->wake_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100968 if ((int64_t)lat > 0)
969 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100970 }
971 entry->calls++;
972 }
973 }
974 }
975
976 /* hopefully we're done */
977 thread_release();
978
979 chunk_reset(&trash);
980
981 tot_calls = 0;
Willy Tarreaua3423872022-09-07 18:49:55 +0200982 for (i = 0; i < SCHED_ACT_HASH_BUCKETS; i++)
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100983 tot_calls += tmp_activity[i].calls;
984
Willy Tarreaua3423872022-09-07 18:49:55 +0200985 qsort(tmp_activity, SCHED_ACT_HASH_BUCKETS, sizeof(tmp_activity[0]), cmp_sched_activity_calls);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100986
987 chunk_appendf(&trash, "Running tasks: %d (%d threads)\n"
988 " function places %% lat_tot lat_avg\n",
989 (int)tot_calls, global.nbthread);
990
Willy Tarreaua3423872022-09-07 18:49:55 +0200991 for (i = 0; i < SCHED_ACT_HASH_BUCKETS && tmp_activity[i].calls; i++) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100992 chunk_reset(name_buffer);
993
994 if (!tmp_activity[i].func)
995 chunk_printf(name_buffer, "other");
996 else
997 resolve_sym_name(name_buffer, "", tmp_activity[i].func);
998
999 /* reserve 35 chars for name+' '+#calls, knowing that longer names
1000 * are often used for less often called functions.
1001 */
1002 max = 35 - name_buffer->data;
1003 if (max < 1)
1004 max = 1;
1005 chunk_appendf(&trash, " %s%*llu %3d.%1d",
1006 name_buffer->area, max, (unsigned long long)tmp_activity[i].calls,
1007 (int)(100ULL * tmp_activity[i].calls / tot_calls),
1008 (int)((1000ULL * tmp_activity[i].calls / tot_calls)%10));
1009 print_time_short(&trash, " ", tmp_activity[i].lat_time, "");
1010 print_time_short(&trash, " ", tmp_activity[i].lat_time / tmp_activity[i].calls, "\n");
1011 }
1012
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001013 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +01001014 /* failed, try again */
Willy Tarreau7eff06e2021-01-29 11:32:55 +01001015 return 0;
1016 }
1017 return 1;
1018}
1019
Willy Tarreauf9607f82022-11-25 15:32:38 +01001020/* This function dumps some activity counters used by developers and support to
1021 * rule out some hypothesis during bug reports. It returns 0 if the output
1022 * buffer is full and it needs to be called again, otherwise non-zero. It dumps
1023 * everything at once in the buffer and is not designed to do it in multiple
1024 * passes.
1025 */
1026static int cli_io_handler_show_activity(struct appctx *appctx)
1027{
1028 struct stconn *sc = appctx_sc(appctx);
1029 struct show_activity_ctx *actctx = appctx->svcctx;
1030 int tgt = actctx->thr; // target thread, -1 for all, 0 for total only
Willy Tarreaub68d3082023-04-27 14:47:34 +02001031 uint up_sec, up_usec;
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001032 int base_line;
Willy Tarreaub68d3082023-04-27 14:47:34 +02001033 ullong up;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001034
Christopher Faulet87633c32023-04-03 18:32:50 +02001035 /* FIXME: Don't watch the other side ! */
Christopher Faulet208c7122023-04-13 16:16:15 +02001036 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreauf9607f82022-11-25 15:32:38 +01001037 return 1;
1038
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001039 /* this macro is used below to dump values. The thread number is "thr",
1040 * and runs from 0 to nbt-1 when values are printed using the formula.
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001041 * We normally try to dmup integral lines in order to keep counters
1042 * consistent. If we fail once on a line, we'll detect it next time
1043 * because we'll have committed actctx->col=1 thanks to the header
1044 * always being dumped individually. We'll be called again thanks to
1045 * the header being present, leaving some data in the buffer. In this
1046 * case once we restart we'll proceed one column at a time to make sure
1047 * we don't overflow the buffer again.
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001048 */
Willy Tarreau5ddf9be2023-05-03 14:28:35 +02001049#undef SHOW_VAL
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001050#define SHOW_VAL(header, x, formula) \
Willy Tarreauf9607f82022-11-25 15:32:38 +01001051 do { \
1052 unsigned int _v[MAX_THREADS]; \
1053 unsigned int _tot; \
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001054 const int _nbt = global.nbthread; \
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001055 int restarted = actctx->col > 0; \
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001056 int thr; \
1057 _tot = thr = 0; \
Willy Tarreauf9607f82022-11-25 15:32:38 +01001058 do { \
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001059 _tot += _v[thr] = (x); \
1060 } while (++thr < _nbt); \
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001061 for (thr = actctx->col - 2; thr <= _nbt; thr++) { \
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001062 if (thr == -2) { \
1063 /* line header */ \
1064 chunk_appendf(&trash, "%s", header); \
1065 } \
1066 else if (thr == -1) { \
1067 /* aggregate value only for multi-thread: all & 0 */ \
1068 if (_nbt > 1 && tgt <= 0) \
1069 chunk_appendf(&trash, " %u%s", \
1070 (formula), \
1071 (tgt < 0) ? \
1072 " [" : ""); \
1073 } \
1074 else if (thr < _nbt) { \
1075 /* individual value only for all or exact value */ \
1076 if (tgt == -1 || tgt == thr+1) \
1077 chunk_appendf(&trash, " %u", \
1078 _v[thr]); \
1079 } \
1080 else /* thr == _nbt */ { \
1081 chunk_appendf(&trash, "%s\n", \
1082 (_nbt > 1 && tgt < 0) ? \
1083 " ]" : ""); \
1084 } \
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001085 if (thr == -2 || restarted) { \
1086 /* failed once, emit one column at a time */\
1087 if (applet_putchk(appctx, &trash) == -1) \
1088 break; /* main loop handles it */ \
1089 chunk_reset(&trash); \
1090 actctx->col = thr + 3; \
1091 } \
Willy Tarreauf9607f82022-11-25 15:32:38 +01001092 } \
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001093 if (applet_putchk(appctx, &trash) == -1) \
1094 break; /* main loop will handle it */ \
1095 /* OK dump done for this line */ \
1096 chunk_reset(&trash); \
1097 if (thr > _nbt) \
1098 actctx->col = 0; \
Willy Tarreauf9607f82022-11-25 15:32:38 +01001099 } while (0)
1100
Willy Tarreauf9607f82022-11-25 15:32:38 +01001101 /* retrieve uptime */
Willy Tarreauc05d30e2023-04-28 14:50:29 +02001102 up = now_ns - start_time_ns;
Willy Tarreaub68d3082023-04-27 14:47:34 +02001103 up_sec = ns_to_sec(up);
1104 up_usec = (up / 1000U) % 1000000U;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001105
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001106 /* iterate over all dump lines. It happily skips over holes so it's
1107 * not a problem not to have an exact match, we just need to have
1108 * stable and consistent lines during a dump.
1109 */
1110 base_line = __LINE__;
1111 do {
1112 chunk_reset(&trash);
Willy Tarreaua465b212023-05-03 14:51:05 +02001113
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001114 switch (actctx->line + base_line) {
1115 case __LINE__: chunk_appendf(&trash, "thread_id: %u (%u..%u)\n", tid + 1, 1, global.nbthread); break;
1116 case __LINE__: chunk_appendf(&trash, "date_now: %lu.%06lu\n", (ulong)date.tv_sec, (ulong)date.tv_usec); break;
1117 case __LINE__: chunk_appendf(&trash, "uptime_now: %u.%06u\n", up_sec, up_usec); break;
1118 case __LINE__: SHOW_VAL("ctxsw:", activity[thr].ctxsw, _tot); break;
1119 case __LINE__: SHOW_VAL("tasksw:", activity[thr].tasksw, _tot); break;
1120 case __LINE__: SHOW_VAL("empty_rq:", activity[thr].empty_rq, _tot); break;
1121 case __LINE__: SHOW_VAL("long_rq:", activity[thr].long_rq, _tot); break;
1122 case __LINE__: SHOW_VAL("loops:", activity[thr].loops, _tot); break;
1123 case __LINE__: SHOW_VAL("wake_tasks:", activity[thr].wake_tasks, _tot); break;
1124 case __LINE__: SHOW_VAL("wake_signal:", activity[thr].wake_signal, _tot); break;
1125 case __LINE__: SHOW_VAL("poll_io:", activity[thr].poll_io, _tot); break;
1126 case __LINE__: SHOW_VAL("poll_exp:", activity[thr].poll_exp, _tot); break;
1127 case __LINE__: SHOW_VAL("poll_drop_fd:", activity[thr].poll_drop_fd, _tot); break;
1128 case __LINE__: SHOW_VAL("poll_skip_fd:", activity[thr].poll_skip_fd, _tot); break;
1129 case __LINE__: SHOW_VAL("conn_dead:", activity[thr].conn_dead, _tot); break;
1130 case __LINE__: SHOW_VAL("stream_calls:", activity[thr].stream_calls, _tot); break;
1131 case __LINE__: SHOW_VAL("pool_fail:", activity[thr].pool_fail, _tot); break;
1132 case __LINE__: SHOW_VAL("buf_wait:", activity[thr].buf_wait, _tot); break;
1133 case __LINE__: SHOW_VAL("cpust_ms_tot:", activity[thr].cpust_total / 2, _tot); break;
1134 case __LINE__: SHOW_VAL("cpust_ms_1s:", read_freq_ctr(&activity[thr].cpust_1s) / 2, _tot); break;
1135 case __LINE__: SHOW_VAL("cpust_ms_15s:", read_freq_ctr_period(&activity[thr].cpust_15s, 15000) / 2, _tot); break;
1136 case __LINE__: SHOW_VAL("avg_cpu_pct:", (100 - ha_thread_ctx[thr].idle_pct), (_tot + _nbt/2) / _nbt); break;
1137 case __LINE__: SHOW_VAL("avg_loop_us:", swrate_avg(activity[thr].avg_loop_us, TIME_STATS_SAMPLES), (_tot + _nbt/2) / _nbt); break;
1138 case __LINE__: SHOW_VAL("accepted:", activity[thr].accepted, _tot); break;
1139 case __LINE__: SHOW_VAL("accq_pushed:", activity[thr].accq_pushed, _tot); break;
1140 case __LINE__: SHOW_VAL("accq_full:", activity[thr].accq_full, _tot); break;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001141#ifdef USE_THREAD
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001142 case __LINE__: SHOW_VAL("accq_ring:", accept_queue_ring_len(&accept_queue_rings[thr]), _tot); break;
1143 case __LINE__: SHOW_VAL("fd_takeover:", activity[thr].fd_takeover, _tot); break;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001144#endif
1145
1146#if defined(DEBUG_DEV)
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001147 /* keep these ones at the end */
1148 case __LINE__: SHOW_VAL("ctr0:", activity[thr].ctr0, _tot); break;
1149 case __LINE__: SHOW_VAL("ctr1:", activity[thr].ctr1, _tot); break;
1150 case __LINE__: SHOW_VAL("ctr2:", activity[thr].ctr2, _tot); break;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001151#endif
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001152 }
1153#undef SHOW_VAL
Willy Tarreauf9607f82022-11-25 15:32:38 +01001154
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001155 /* try to dump what was possibly not dumped yet */
1156
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001157 if (applet_putchk(appctx, &trash) == -1) {
1158 /* buffer full, retry later */
1159 return 0;
1160 }
1161 /* line was dumped, let's commit it */
1162 actctx->line++;
1163 } while (actctx->line + base_line < __LINE__);
Willy Tarreauf9607f82022-11-25 15:32:38 +01001164
Willy Tarreauf9607f82022-11-25 15:32:38 +01001165 /* dump complete */
1166 return 1;
1167}
1168
1169/* parse a "show activity" CLI request. Returns 0 if it needs to continue, 1 if it
1170 * wants to stop here. It sets a show_activity_ctx context where, if a specific
1171 * thread is requested, it puts the thread number into ->thr otherwise sets it to
1172 * -1.
1173 */
1174static int cli_parse_show_activity(char **args, char *payload, struct appctx *appctx, void *private)
1175{
1176 struct show_activity_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1177
1178 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1179 return 1;
1180
1181 ctx->thr = -1; // show all by default
1182 if (*args[2])
1183 ctx->thr = atoi(args[2]);
1184
1185 if (ctx->thr < -1 || ctx->thr > global.nbthread)
1186 return cli_err(appctx, "Thread ID number must be between -1 and nbthread\n");
1187
1188 return 0;
1189}
1190
Willy Tarreau75c62c22018-11-22 11:02:09 +01001191/* config keyword parsers */
1192static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreauca3afc22021-05-05 18:33:19 +02001193#ifdef USE_MEMORY_PROFILING
1194 { CFG_GLOBAL, "profiling.memory", cfg_parse_prof_memory },
1195#endif
Willy Tarreau75c62c22018-11-22 11:02:09 +01001196 { CFG_GLOBAL, "profiling.tasks", cfg_parse_prof_tasks },
1197 { 0, NULL, NULL }
1198}};
1199
Willy Tarreau0108d902018-11-25 19:14:37 +01001200INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1201
Willy Tarreau75c62c22018-11-22 11:02:09 +01001202/* register cli keywords */
1203static struct cli_kw_list cli_kws = {{ },{
Daniel Corbett67b3cef2021-05-10 14:08:40 -04001204 { { "set", "profiling", NULL }, "set profiling <what> {auto|on|off} : enable/disable resource profiling (tasks,memory)", cli_parse_set_profiling, NULL },
Willy Tarreauf9607f82022-11-25 15:32:38 +01001205 { { "show", "activity", NULL }, "show activity [-1|0|thread_num] : show per-thread activity stats (for support/developers)", cli_parse_show_activity, cli_io_handler_show_activity, NULL },
Willy Tarreaudc89b182022-09-08 16:05:57 +02001206 { { "show", "profiling", NULL }, "show profiling [<what>|<#lines>|<opts>]*: show profiling state (all,status,tasks,memory)", cli_parse_show_profiling, cli_io_handler_show_profiling, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02001207 { { "show", "tasks", NULL }, "show tasks : show running tasks", NULL, cli_io_handler_show_tasks, NULL },
Willy Tarreau75c62c22018-11-22 11:02:09 +01001208 {{},}
1209}};
1210
Willy Tarreau0108d902018-11-25 19:14:37 +01001211INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);