blob: bd00f04712c268605e6babef4494d1909ba2380a [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
Valentine Krasnobaevad6c9af92024-05-28 17:06:24 +0200779 if ((tmp_memstats[i].method != MEMPROF_METH_P_ALLOC) &&
780 (tmp_memstats[i].method != MEMPROF_METH_MALLOC) &&
781 (tmp_memstats[i].method != MEMPROF_METH_CALLOC)) {
782 chunk_appendf(&trash," %s(%lld)", memprof_methods[entry->method],
783 (long long)(entry->alloc_tot - entry->free_tot) / (long long)(entry->alloc_calls + entry->free_calls));
784 } else
785 chunk_appendf(&trash," %s(%lld)", memprof_methods[entry->method],
786 (long long)(entry->alloc_tot) / (long long)(entry->alloc_calls));
Willy Tarreau993d44d2021-05-05 18:07:02 +0200787
Willy Tarreau8cce4d72021-10-22 16:26:12 +0200788 if (entry->alloc_tot && entry->free_tot) {
789 /* that's a realloc, show the total diff to help spot leaks */
790 chunk_appendf(&trash," [delta=%lld]", (long long)(entry->alloc_tot - entry->free_tot));
791 }
792
Willy Tarreau42b180d2022-08-17 09:35:16 +0200793 if (entry->info) {
794 /* that's a pool name */
795 const struct pool_head *pool = entry->info;
796 chunk_appendf(&trash," [pool=%s]", pool->name);
797 }
798
Willy Tarreau8cce4d72021-10-22 16:26:12 +0200799 chunk_appendf(&trash, "\n");
800
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200801 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau993d44d2021-05-05 18:07:02 +0200802 return 0;
Willy Tarreau993d44d2021-05-05 18:07:02 +0200803 }
804
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200805 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau993d44d2021-05-05 18:07:02 +0200806 return 0;
Willy Tarreau993d44d2021-05-05 18:07:02 +0200807
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200808 tot_alloc_calls = tot_free_calls = tot_alloc_bytes = tot_free_bytes = 0;
809 for (i = 0; i < max_lines; i++) {
810 tot_alloc_calls += tmp_memstats[i].alloc_calls;
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200811 tot_alloc_bytes += tmp_memstats[i].alloc_tot;
Valentine Krasnobaevad6c9af92024-05-28 17:06:24 +0200812 if ((tmp_memstats[i].method != MEMPROF_METH_P_ALLOC) &&
813 (tmp_memstats[i].method != MEMPROF_METH_MALLOC) &&
814 (tmp_memstats[i].method != MEMPROF_METH_CALLOC)) {
815 tot_free_calls += tmp_memstats[i].free_calls;
816 tot_free_bytes += tmp_memstats[i].free_tot;
817 }
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200818 }
819
820 chunk_appendf(&trash,
821 "-----------------------|-----------------------------|\n"
822 "%11llu %11llu %14llu %14llu| <- Total; Delta_calls=%lld; Delta_bytes=%lld\n",
823 tot_alloc_calls, tot_free_calls,
824 tot_alloc_bytes, tot_free_bytes,
825 tot_alloc_calls - tot_free_calls,
826 tot_alloc_bytes - tot_free_bytes);
827
Willy Tarreaud0a06d52022-05-18 15:07:19 +0200828 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200829 return 0;
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200830
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200831 ctx->linenum = 0; // reset first line to dump
832 if ((ctx->dump_step & 4) == 0)
833 ctx->dump_step++; // next step
Willy Tarreau993d44d2021-05-05 18:07:02 +0200834
835 skip_mem:
836#endif // USE_MEMORY_PROFILING
837
Willy Tarreau75c62c22018-11-22 11:02:09 +0100838 return 1;
839}
840
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200841/* parse a "show profiling" command. It returns 1 on failure, 0 if it starts to dump.
842 * - cli.i0 is set to the first state (0=all, 4=status, 5=tasks, 6=memory)
843 * - cli.o1 is set to 1 if the output must be sorted by addr instead of usage
844 * - cli.o0 is set to the number of lines of output
845 */
Willy Tarreau42712cb2021-05-05 17:48:13 +0200846static int cli_parse_show_profiling(char **args, char *payload, struct appctx *appctx, void *private)
847{
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200848 struct show_prof_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200849 int arg;
850
Willy Tarreau42712cb2021-05-05 17:48:13 +0200851 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
852 return 1;
853
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200854 for (arg = 2; *args[arg]; arg++) {
855 if (strcmp(args[arg], "all") == 0) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200856 ctx->dump_step = 0; // will cycle through 0,1,2; default
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200857 }
858 else if (strcmp(args[arg], "status") == 0) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200859 ctx->dump_step = 4; // will visit status only
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200860 }
861 else if (strcmp(args[arg], "tasks") == 0) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200862 ctx->dump_step = 5; // will visit tasks only
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200863 }
864 else if (strcmp(args[arg], "memory") == 0) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200865 ctx->dump_step = 6; // will visit memory only
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200866 }
867 else if (strcmp(args[arg], "byaddr") == 0) {
Willy Tarreaue86bc352022-09-08 16:38:10 +0200868 ctx->by_what = 1; // sort output by address instead of usage
869 }
870 else if (strcmp(args[arg], "bytime") == 0) {
871 ctx->by_what = 2; // sort output by total time instead of usage
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200872 }
Willy Tarreaudc89b182022-09-08 16:05:57 +0200873 else if (strcmp(args[arg], "aggr") == 0) {
874 ctx->aggr = 1; // aggregate output by callee
875 }
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200876 else if (isdigit((unsigned char)*args[arg])) {
Willy Tarreaue8d006a2022-05-05 14:19:00 +0200877 ctx->maxcnt = atoi(args[arg]); // number of entries to dump
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200878 }
879 else
Willy Tarreaue86bc352022-09-08 16:38:10 +0200880 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 +0200881 }
882 return 0;
883}
884
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100885/* This function scans all threads' run queues and collects statistics about
886 * running tasks. It returns 0 if the output buffer is full and it needs to be
887 * called again, otherwise non-zero.
888 */
889static int cli_io_handler_show_tasks(struct appctx *appctx)
890{
Willy Tarreaua3423872022-09-07 18:49:55 +0200891 struct sched_activity tmp_activity[SCHED_ACT_HASH_BUCKETS] __attribute__((aligned(64)));
Willy Tarreauc12b3212022-05-27 11:08:15 +0200892 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100893 struct buffer *name_buffer = get_trash_chunk();
894 struct sched_activity *entry;
895 const struct tasklet *tl;
896 const struct task *t;
897 uint64_t now_ns, lat;
Willy Tarreau319d1362022-06-16 16:28:01 +0200898 struct eb32_node *rqnode;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100899 uint64_t tot_calls;
900 int thr, queue;
901 int i, max;
902
Christopher Faulet87633c32023-04-03 18:32:50 +0200903 /* FIXME: Don't watch the other side ! */
Christopher Faulet208c7122023-04-13 16:16:15 +0200904 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100905 return 1;
906
907 /* It's not possible to scan queues in small chunks and yield in the
908 * middle of the dump and come back again. So what we're doing instead
909 * is to freeze all threads and inspect their queues at once as fast as
910 * possible, using a sched_activity array to collect metrics with
911 * limited collision, then we'll report statistics only. The tasks'
912 * #calls will reflect the number of occurrences, and the lat_time will
Willy Tarreau75f72332021-01-29 15:04:16 +0100913 * reflect the latency when set. We prefer to take the time before
914 * calling thread_isolate() so that the wait time doesn't impact the
915 * measurement accuracy. However this requires to take care of negative
916 * times since tasks might be queued after we retrieve it.
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100917 */
918
919 now_ns = now_mono_time();
920 memset(tmp_activity, 0, sizeof(tmp_activity));
921
922 thread_isolate();
923
924 /* 1. global run queue */
925
926#ifdef USE_THREAD
Willy Tarreau6f780382022-06-16 15:30:50 +0200927 for (thr = 0; thr < global.nbthread; thr++) {
928 /* task run queue */
Willy Tarreau319d1362022-06-16 16:28:01 +0200929 rqnode = eb32_first(&ha_thread_ctx[thr].rqueue_shared);
Willy Tarreau6f780382022-06-16 15:30:50 +0200930 while (rqnode) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200931 t = eb32_entry(rqnode, struct task, rq);
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200932 entry = sched_activity_entry(tmp_activity, t->process, NULL);
Willy Tarreau04e50b32022-09-07 14:49:50 +0200933 if (t->wake_date) {
934 lat = now_ns - t->wake_date;
Willy Tarreau6f780382022-06-16 15:30:50 +0200935 if ((int64_t)lat > 0)
936 entry->lat_time += lat;
937 }
938 entry->calls++;
Willy Tarreau319d1362022-06-16 16:28:01 +0200939 rqnode = eb32_next(rqnode);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100940 }
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100941 }
942#endif
943 /* 2. all threads's local run queues */
944 for (thr = 0; thr < global.nbthread; thr++) {
945 /* task run queue */
Willy Tarreau319d1362022-06-16 16:28:01 +0200946 rqnode = eb32_first(&ha_thread_ctx[thr].rqueue);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100947 while (rqnode) {
Willy Tarreau319d1362022-06-16 16:28:01 +0200948 t = eb32_entry(rqnode, struct task, rq);
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200949 entry = sched_activity_entry(tmp_activity, t->process, NULL);
Willy Tarreau04e50b32022-09-07 14:49:50 +0200950 if (t->wake_date) {
951 lat = now_ns - t->wake_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100952 if ((int64_t)lat > 0)
953 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100954 }
955 entry->calls++;
Willy Tarreau319d1362022-06-16 16:28:01 +0200956 rqnode = eb32_next(rqnode);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100957 }
958
959 /* shared tasklet list */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200960 list_for_each_entry(tl, mt_list_to_list(&ha_thread_ctx[thr].shared_tasklet_list), list) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100961 t = (const struct task *)tl;
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200962 entry = sched_activity_entry(tmp_activity, t->process, NULL);
Willy Tarreau04e50b32022-09-07 14:49:50 +0200963 if (!TASK_IS_TASKLET(t) && t->wake_date) {
964 lat = now_ns - t->wake_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100965 if ((int64_t)lat > 0)
966 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100967 }
968 entry->calls++;
969 }
970
971 /* classful tasklets */
972 for (queue = 0; queue < TL_CLASSES; queue++) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200973 list_for_each_entry(tl, &ha_thread_ctx[thr].tasklets[queue], list) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100974 t = (const struct task *)tl;
Willy Tarreau3d4cdb12022-09-07 18:37:47 +0200975 entry = sched_activity_entry(tmp_activity, t->process, NULL);
Willy Tarreau04e50b32022-09-07 14:49:50 +0200976 if (!TASK_IS_TASKLET(t) && t->wake_date) {
977 lat = now_ns - t->wake_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100978 if ((int64_t)lat > 0)
979 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100980 }
981 entry->calls++;
982 }
983 }
984 }
985
986 /* hopefully we're done */
987 thread_release();
988
989 chunk_reset(&trash);
990
991 tot_calls = 0;
Willy Tarreaua3423872022-09-07 18:49:55 +0200992 for (i = 0; i < SCHED_ACT_HASH_BUCKETS; i++)
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100993 tot_calls += tmp_activity[i].calls;
994
Willy Tarreaua3423872022-09-07 18:49:55 +0200995 qsort(tmp_activity, SCHED_ACT_HASH_BUCKETS, sizeof(tmp_activity[0]), cmp_sched_activity_calls);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100996
997 chunk_appendf(&trash, "Running tasks: %d (%d threads)\n"
998 " function places %% lat_tot lat_avg\n",
999 (int)tot_calls, global.nbthread);
1000
Willy Tarreaua3423872022-09-07 18:49:55 +02001001 for (i = 0; i < SCHED_ACT_HASH_BUCKETS && tmp_activity[i].calls; i++) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +01001002 chunk_reset(name_buffer);
1003
1004 if (!tmp_activity[i].func)
1005 chunk_printf(name_buffer, "other");
1006 else
1007 resolve_sym_name(name_buffer, "", tmp_activity[i].func);
1008
1009 /* reserve 35 chars for name+' '+#calls, knowing that longer names
1010 * are often used for less often called functions.
1011 */
1012 max = 35 - name_buffer->data;
1013 if (max < 1)
1014 max = 1;
1015 chunk_appendf(&trash, " %s%*llu %3d.%1d",
1016 name_buffer->area, max, (unsigned long long)tmp_activity[i].calls,
1017 (int)(100ULL * tmp_activity[i].calls / tot_calls),
1018 (int)((1000ULL * tmp_activity[i].calls / tot_calls)%10));
1019 print_time_short(&trash, " ", tmp_activity[i].lat_time, "");
1020 print_time_short(&trash, " ", tmp_activity[i].lat_time / tmp_activity[i].calls, "\n");
1021 }
1022
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001023 if (applet_putchk(appctx, &trash) == -1) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +01001024 /* failed, try again */
Willy Tarreau7eff06e2021-01-29 11:32:55 +01001025 return 0;
1026 }
1027 return 1;
1028}
1029
Willy Tarreauf9607f82022-11-25 15:32:38 +01001030/* This function dumps some activity counters used by developers and support to
1031 * rule out some hypothesis during bug reports. It returns 0 if the output
1032 * buffer is full and it needs to be called again, otherwise non-zero. It dumps
1033 * everything at once in the buffer and is not designed to do it in multiple
1034 * passes.
1035 */
1036static int cli_io_handler_show_activity(struct appctx *appctx)
1037{
1038 struct stconn *sc = appctx_sc(appctx);
1039 struct show_activity_ctx *actctx = appctx->svcctx;
1040 int tgt = actctx->thr; // target thread, -1 for all, 0 for total only
Willy Tarreaub68d3082023-04-27 14:47:34 +02001041 uint up_sec, up_usec;
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001042 int base_line;
Willy Tarreaub68d3082023-04-27 14:47:34 +02001043 ullong up;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001044
Christopher Faulet87633c32023-04-03 18:32:50 +02001045 /* FIXME: Don't watch the other side ! */
Christopher Faulet208c7122023-04-13 16:16:15 +02001046 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreauf9607f82022-11-25 15:32:38 +01001047 return 1;
1048
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001049 /* this macro is used below to dump values. The thread number is "thr",
1050 * and runs from 0 to nbt-1 when values are printed using the formula.
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001051 * We normally try to dmup integral lines in order to keep counters
1052 * consistent. If we fail once on a line, we'll detect it next time
1053 * because we'll have committed actctx->col=1 thanks to the header
1054 * always being dumped individually. We'll be called again thanks to
1055 * the header being present, leaving some data in the buffer. In this
1056 * case once we restart we'll proceed one column at a time to make sure
1057 * we don't overflow the buffer again.
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001058 */
Willy Tarreau5ddf9be2023-05-03 14:28:35 +02001059#undef SHOW_VAL
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001060#define SHOW_VAL(header, x, formula) \
Willy Tarreauf9607f82022-11-25 15:32:38 +01001061 do { \
1062 unsigned int _v[MAX_THREADS]; \
1063 unsigned int _tot; \
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001064 const int _nbt = global.nbthread; \
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001065 int restarted = actctx->col > 0; \
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001066 int thr; \
1067 _tot = thr = 0; \
Willy Tarreauf9607f82022-11-25 15:32:38 +01001068 do { \
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001069 _tot += _v[thr] = (x); \
1070 } while (++thr < _nbt); \
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001071 for (thr = actctx->col - 2; thr <= _nbt; thr++) { \
Willy Tarreau8ee0d112023-05-03 15:25:04 +02001072 if (thr == -2) { \
1073 /* line header */ \
1074 chunk_appendf(&trash, "%s", header); \
1075 } \
1076 else if (thr == -1) { \
1077 /* aggregate value only for multi-thread: all & 0 */ \
1078 if (_nbt > 1 && tgt <= 0) \
1079 chunk_appendf(&trash, " %u%s", \
1080 (formula), \
1081 (tgt < 0) ? \
1082 " [" : ""); \
1083 } \
1084 else if (thr < _nbt) { \
1085 /* individual value only for all or exact value */ \
1086 if (tgt == -1 || tgt == thr+1) \
1087 chunk_appendf(&trash, " %u", \
1088 _v[thr]); \
1089 } \
1090 else /* thr == _nbt */ { \
1091 chunk_appendf(&trash, "%s\n", \
1092 (_nbt > 1 && tgt < 0) ? \
1093 " ]" : ""); \
1094 } \
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001095 if (thr == -2 || restarted) { \
1096 /* failed once, emit one column at a time */\
1097 if (applet_putchk(appctx, &trash) == -1) \
1098 break; /* main loop handles it */ \
1099 chunk_reset(&trash); \
1100 actctx->col = thr + 3; \
1101 } \
Willy Tarreauf9607f82022-11-25 15:32:38 +01001102 } \
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001103 if (applet_putchk(appctx, &trash) == -1) \
1104 break; /* main loop will handle it */ \
1105 /* OK dump done for this line */ \
1106 chunk_reset(&trash); \
1107 if (thr > _nbt) \
1108 actctx->col = 0; \
Willy Tarreauf9607f82022-11-25 15:32:38 +01001109 } while (0)
1110
Willy Tarreauf9607f82022-11-25 15:32:38 +01001111 /* retrieve uptime */
Willy Tarreauc05d30e2023-04-28 14:50:29 +02001112 up = now_ns - start_time_ns;
Willy Tarreaub68d3082023-04-27 14:47:34 +02001113 up_sec = ns_to_sec(up);
1114 up_usec = (up / 1000U) % 1000000U;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001115
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001116 /* iterate over all dump lines. It happily skips over holes so it's
1117 * not a problem not to have an exact match, we just need to have
1118 * stable and consistent lines during a dump.
1119 */
1120 base_line = __LINE__;
1121 do {
1122 chunk_reset(&trash);
Willy Tarreaua465b212023-05-03 14:51:05 +02001123
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001124 switch (actctx->line + base_line) {
1125 case __LINE__: chunk_appendf(&trash, "thread_id: %u (%u..%u)\n", tid + 1, 1, global.nbthread); break;
1126 case __LINE__: chunk_appendf(&trash, "date_now: %lu.%06lu\n", (ulong)date.tv_sec, (ulong)date.tv_usec); break;
1127 case __LINE__: chunk_appendf(&trash, "uptime_now: %u.%06u\n", up_sec, up_usec); break;
1128 case __LINE__: SHOW_VAL("ctxsw:", activity[thr].ctxsw, _tot); break;
1129 case __LINE__: SHOW_VAL("tasksw:", activity[thr].tasksw, _tot); break;
1130 case __LINE__: SHOW_VAL("empty_rq:", activity[thr].empty_rq, _tot); break;
1131 case __LINE__: SHOW_VAL("long_rq:", activity[thr].long_rq, _tot); break;
1132 case __LINE__: SHOW_VAL("loops:", activity[thr].loops, _tot); break;
1133 case __LINE__: SHOW_VAL("wake_tasks:", activity[thr].wake_tasks, _tot); break;
1134 case __LINE__: SHOW_VAL("wake_signal:", activity[thr].wake_signal, _tot); break;
1135 case __LINE__: SHOW_VAL("poll_io:", activity[thr].poll_io, _tot); break;
1136 case __LINE__: SHOW_VAL("poll_exp:", activity[thr].poll_exp, _tot); break;
1137 case __LINE__: SHOW_VAL("poll_drop_fd:", activity[thr].poll_drop_fd, _tot); break;
1138 case __LINE__: SHOW_VAL("poll_skip_fd:", activity[thr].poll_skip_fd, _tot); break;
1139 case __LINE__: SHOW_VAL("conn_dead:", activity[thr].conn_dead, _tot); break;
1140 case __LINE__: SHOW_VAL("stream_calls:", activity[thr].stream_calls, _tot); break;
1141 case __LINE__: SHOW_VAL("pool_fail:", activity[thr].pool_fail, _tot); break;
1142 case __LINE__: SHOW_VAL("buf_wait:", activity[thr].buf_wait, _tot); break;
1143 case __LINE__: SHOW_VAL("cpust_ms_tot:", activity[thr].cpust_total / 2, _tot); break;
1144 case __LINE__: SHOW_VAL("cpust_ms_1s:", read_freq_ctr(&activity[thr].cpust_1s) / 2, _tot); break;
1145 case __LINE__: SHOW_VAL("cpust_ms_15s:", read_freq_ctr_period(&activity[thr].cpust_15s, 15000) / 2, _tot); break;
1146 case __LINE__: SHOW_VAL("avg_cpu_pct:", (100 - ha_thread_ctx[thr].idle_pct), (_tot + _nbt/2) / _nbt); break;
1147 case __LINE__: SHOW_VAL("avg_loop_us:", swrate_avg(activity[thr].avg_loop_us, TIME_STATS_SAMPLES), (_tot + _nbt/2) / _nbt); break;
1148 case __LINE__: SHOW_VAL("accepted:", activity[thr].accepted, _tot); break;
1149 case __LINE__: SHOW_VAL("accq_pushed:", activity[thr].accq_pushed, _tot); break;
1150 case __LINE__: SHOW_VAL("accq_full:", activity[thr].accq_full, _tot); break;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001151#ifdef USE_THREAD
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001152 case __LINE__: SHOW_VAL("accq_ring:", accept_queue_ring_len(&accept_queue_rings[thr]), _tot); break;
1153 case __LINE__: SHOW_VAL("fd_takeover:", activity[thr].fd_takeover, _tot); break;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001154#endif
1155
1156#if defined(DEBUG_DEV)
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001157 /* keep these ones at the end */
1158 case __LINE__: SHOW_VAL("ctr0:", activity[thr].ctr0, _tot); break;
1159 case __LINE__: SHOW_VAL("ctr1:", activity[thr].ctr1, _tot); break;
1160 case __LINE__: SHOW_VAL("ctr2:", activity[thr].ctr2, _tot); break;
Willy Tarreauf9607f82022-11-25 15:32:38 +01001161#endif
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001162 }
1163#undef SHOW_VAL
Willy Tarreauf9607f82022-11-25 15:32:38 +01001164
Willy Tarreau8b3e39e2023-05-03 16:18:30 +02001165 /* try to dump what was possibly not dumped yet */
1166
Willy Tarreau6ed0b982023-05-03 15:52:19 +02001167 if (applet_putchk(appctx, &trash) == -1) {
1168 /* buffer full, retry later */
1169 return 0;
1170 }
1171 /* line was dumped, let's commit it */
1172 actctx->line++;
1173 } while (actctx->line + base_line < __LINE__);
Willy Tarreauf9607f82022-11-25 15:32:38 +01001174
Willy Tarreauf9607f82022-11-25 15:32:38 +01001175 /* dump complete */
1176 return 1;
1177}
1178
1179/* parse a "show activity" CLI request. Returns 0 if it needs to continue, 1 if it
1180 * wants to stop here. It sets a show_activity_ctx context where, if a specific
1181 * thread is requested, it puts the thread number into ->thr otherwise sets it to
1182 * -1.
1183 */
1184static int cli_parse_show_activity(char **args, char *payload, struct appctx *appctx, void *private)
1185{
1186 struct show_activity_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
1187
1188 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1189 return 1;
1190
1191 ctx->thr = -1; // show all by default
1192 if (*args[2])
1193 ctx->thr = atoi(args[2]);
1194
1195 if (ctx->thr < -1 || ctx->thr > global.nbthread)
1196 return cli_err(appctx, "Thread ID number must be between -1 and nbthread\n");
1197
1198 return 0;
1199}
1200
Willy Tarreau75c62c22018-11-22 11:02:09 +01001201/* config keyword parsers */
1202static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreauca3afc22021-05-05 18:33:19 +02001203#ifdef USE_MEMORY_PROFILING
1204 { CFG_GLOBAL, "profiling.memory", cfg_parse_prof_memory },
1205#endif
Willy Tarreau75c62c22018-11-22 11:02:09 +01001206 { CFG_GLOBAL, "profiling.tasks", cfg_parse_prof_tasks },
1207 { 0, NULL, NULL }
1208}};
1209
Willy Tarreau0108d902018-11-25 19:14:37 +01001210INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1211
Willy Tarreau75c62c22018-11-22 11:02:09 +01001212/* register cli keywords */
1213static struct cli_kw_list cli_kws = {{ },{
Daniel Corbett67b3cef2021-05-10 14:08:40 -04001214 { { "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 +01001215 { { "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 +02001216 { { "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 +02001217 { { "show", "tasks", NULL }, "show tasks : show running tasks", NULL, cli_io_handler_show_tasks, NULL },
Willy Tarreau75c62c22018-11-22 11:02:09 +01001218 {{},}
1219}};
1220
Willy Tarreau0108d902018-11-25 19:14:37 +01001221INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);