blob: b1b91aced620bb0930450c78eea2948a8759bf2c [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 Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreau55542642021-10-08 09:33:24 +020016#include <haproxy/clock.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020017#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020018#include <haproxy/cli.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/freq_ctr.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020020#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020021#include <haproxy/tools.h>
Willy Tarreaua26be372021-10-06 16:26:33 +020022#include <haproxy/xxhash.h>
Willy Tarreau75c62c22018-11-22 11:02:09 +010023
Willy Tarreauf93c7be2021-05-05 17:07:09 +020024#if defined(DEBUG_MEM_STATS)
25/* these ones are macros in bug.h when DEBUG_MEM_STATS is set, and will
26 * prevent the new ones from being redefined.
27 */
28#undef calloc
29#undef malloc
30#undef realloc
31#endif
Willy Tarreau75c62c22018-11-22 11:02:09 +010032
33/* bit field of profiling options. Beware, may be modified at runtime! */
Willy Tarreauef7380f2021-05-05 16:28:31 +020034unsigned int profiling __read_mostly = HA_PROF_TASKS_AOFF;
35unsigned long task_profiling_mask __read_mostly = 0;
Willy Tarreau609aad92018-11-22 08:31:09 +010036
37/* One struct per thread containing all collected measurements */
38struct activity activity[MAX_THREADS] __attribute__((aligned(64))) = { };
39
Willy Tarreau3fb6a7b2021-01-28 19:19:26 +010040/* One struct per function pointer hash entry (256 values, 0=collision) */
41struct sched_activity sched_activity[256] __attribute__((aligned(64))) = { };
Willy Tarreau609aad92018-11-22 08:31:09 +010042
Willy Tarreaudb87fc72021-05-05 16:50:40 +020043
Willy Tarreaue15615c2021-08-28 12:04:25 +020044#ifdef USE_MEMORY_PROFILING
Willy Tarreaudb87fc72021-05-05 16:50:40 +020045/* determine the number of buckets to store stats */
46#define MEMPROF_HASH_BITS 10
47#define MEMPROF_HASH_BUCKETS (1U << MEMPROF_HASH_BITS)
48
Willy Tarreau616491b2021-05-11 09:26:23 +020049enum memprof_method {
50 MEMPROF_METH_UNKNOWN = 0,
51 MEMPROF_METH_MALLOC,
52 MEMPROF_METH_CALLOC,
53 MEMPROF_METH_REALLOC,
54 MEMPROF_METH_FREE,
55 MEMPROF_METH_METHODS /* count, must be last */
56};
57
58static const char *const memprof_methods[MEMPROF_METH_METHODS] = {
59 "unknown", "malloc", "calloc", "realloc", "free",
60};
61
Willy Tarreaudb87fc72021-05-05 16:50:40 +020062/* stats:
63 * - malloc increases alloc
64 * - free increases free (if non null)
65 * - realloc increases either depending on the size change.
66 * when the real size is known (malloc_usable_size()), it's used in free_tot
67 * and alloc_tot, otherwise the requested size is reported in alloc_tot and
68 * zero in free_tot.
69 */
70struct memprof_stats {
71 const void *caller;
Willy Tarreau616491b2021-05-11 09:26:23 +020072 enum memprof_method method;
73 /* 4-7 bytes hole here */
Willy Tarreaudb87fc72021-05-05 16:50:40 +020074 unsigned long long alloc_calls;
75 unsigned long long free_calls;
76 unsigned long long alloc_tot;
77 unsigned long long free_tot;
78};
79
80/* last one is for hash collisions ("others") and has no caller address */
81struct memprof_stats memprof_stats[MEMPROF_HASH_BUCKETS + 1] = { };
82
Willy Tarreauf93c7be2021-05-05 17:07:09 +020083/* used to detect recursive calls */
84static THREAD_LOCAL int in_memprof = 0;
85
86/* perform a pointer hash by scrambling its bits and retrieving the most
87 * mixed ones (topmost ones in 32-bit, middle ones in 64-bit).
88 */
89static unsigned int memprof_hash_ptr(const void *p)
90{
91 unsigned long long x = (unsigned long)p;
92
93 x = 0xcbda9653U * x;
94 if (sizeof(long) == 4)
95 x >>= 32;
96 else
97 x >>= 33 - MEMPROF_HASH_BITS / 2;
98 return x & (MEMPROF_HASH_BUCKETS - 1);
99}
100
101/* These ones are used by glibc and will be called early. They are in charge of
102 * initializing the handlers with the original functions.
103 */
104static void *memprof_malloc_initial_handler(size_t size);
105static void *memprof_calloc_initial_handler(size_t nmemb, size_t size);
106static void *memprof_realloc_initial_handler(void *ptr, size_t size);
107static void memprof_free_initial_handler(void *ptr);
108
109/* Fallback handlers for the main alloc/free functions. They are preset to
110 * the initializer in order to save a test in the functions's critical path.
111 */
112static void *(*memprof_malloc_handler)(size_t size) = memprof_malloc_initial_handler;
113static void *(*memprof_calloc_handler)(size_t nmemb, size_t size) = memprof_calloc_initial_handler;
114static void *(*memprof_realloc_handler)(void *ptr, size_t size) = memprof_realloc_initial_handler;
115static void (*memprof_free_handler)(void *ptr) = memprof_free_initial_handler;
116
117/* Used to force to die if it's not possible to retrieve the allocation
118 * functions. We cannot even use stdio in this case.
119 */
120static __attribute__((noreturn)) void memprof_die(const char *msg)
121{
122 DISGUISE(write(2, msg, strlen(msg)));
123 exit(1);
124}
125
126/* Resolve original allocation functions and initialize all handlers.
127 * This must be called very early at boot, before the very first malloc()
128 * call, and is not thread-safe! It's not even possible to use stdio there.
129 * Worse, we have to account for the risk of reentrance from dlsym() when
130 * it tries to prepare its error messages. Here its ahndled by in_memprof
131 * that makes allocators return NULL. dlsym() handles it gracefully. An
Ilya Shipitsin3df59892021-05-10 12:50:00 +0500132 * alternate approach consists in calling aligned_alloc() from these places
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200133 * but that would mean not being able to intercept it later if considered
134 * useful to do so.
135 */
136static void memprof_init()
137{
138 in_memprof++;
139 memprof_malloc_handler = get_sym_next_addr("malloc");
140 if (!memprof_malloc_handler)
141 memprof_die("FATAL: malloc() function not found.\n");
142
143 memprof_calloc_handler = get_sym_next_addr("calloc");
144 if (!memprof_calloc_handler)
145 memprof_die("FATAL: calloc() function not found.\n");
146
147 memprof_realloc_handler = get_sym_next_addr("realloc");
148 if (!memprof_realloc_handler)
149 memprof_die("FATAL: realloc() function not found.\n");
150
151 memprof_free_handler = get_sym_next_addr("free");
152 if (!memprof_free_handler)
153 memprof_die("FATAL: free() function not found.\n");
154 in_memprof--;
155}
156
157/* the initial handlers will initialize all regular handlers and will call the
158 * one they correspond to. A single one of these functions will typically be
159 * called, though it's unknown which one (as any might be called before main).
160 */
161static void *memprof_malloc_initial_handler(size_t size)
162{
163 if (in_memprof) {
164 /* it's likely that dlsym() needs malloc(), let's fail */
165 return NULL;
166 }
167
168 memprof_init();
169 return memprof_malloc_handler(size);
170}
171
172static void *memprof_calloc_initial_handler(size_t nmemb, size_t size)
173{
174 if (in_memprof) {
175 /* it's likely that dlsym() needs calloc(), let's fail */
176 return NULL;
177 }
178 memprof_init();
179 return memprof_calloc_handler(nmemb, size);
180}
181
182static void *memprof_realloc_initial_handler(void *ptr, size_t size)
183{
184 if (in_memprof) {
185 /* it's likely that dlsym() needs realloc(), let's fail */
186 return NULL;
187 }
188
189 memprof_init();
190 return memprof_realloc_handler(ptr, size);
191}
192
193static void memprof_free_initial_handler(void *ptr)
194{
195 memprof_init();
196 memprof_free_handler(ptr);
197}
198
199/* Assign a bin for the memprof_stats to the return address. May perform a few
200 * attempts before finding the right one, but always succeeds (in the worst
201 * case, returns a default bin). The caller address is atomically set except
202 * for the default one which is never set.
203 */
Willy Tarreau616491b2021-05-11 09:26:23 +0200204static struct memprof_stats *memprof_get_bin(const void *ra, enum memprof_method meth)
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200205{
206 int retries = 16; // up to 16 consecutive entries may be tested.
Willy Tarreau4a753282021-05-09 23:18:50 +0200207 const void *old;
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200208 unsigned int bin;
209
210 bin = memprof_hash_ptr(ra);
211 for (; memprof_stats[bin].caller != ra; bin = (bin + 1) & (MEMPROF_HASH_BUCKETS - 1)) {
212 if (!--retries) {
213 bin = MEMPROF_HASH_BUCKETS;
214 break;
215 }
216
217 old = NULL;
218 if (!memprof_stats[bin].caller &&
Willy Tarreau616491b2021-05-11 09:26:23 +0200219 HA_ATOMIC_CAS(&memprof_stats[bin].caller, &old, ra)) {
220 memprof_stats[bin].method = meth;
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200221 break;
Willy Tarreau616491b2021-05-11 09:26:23 +0200222 }
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200223 }
224 return &memprof_stats[bin];
225}
226
227/* This is the new global malloc() function. It must optimize for the normal
228 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
229 * It must remain simple to guarantee the lack of reentrance. stdio is not
230 * possible there even for debugging. The reported size is the really allocated
231 * one as returned by malloc_usable_size(), because this will allow it to be
232 * compared to the one before realloc() or free(). This is a GNU and jemalloc
233 * extension but other systems may also store this size in ptr[-1].
234 */
235void *malloc(size_t size)
236{
237 struct memprof_stats *bin;
238 void *ret;
239
240 if (likely(!(profiling & HA_PROF_MEMORY)))
241 return memprof_malloc_handler(size);
242
243 ret = memprof_malloc_handler(size);
244 size = malloc_usable_size(ret);
245
Willy Tarreau616491b2021-05-11 09:26:23 +0200246 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_MALLOC);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200247 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
248 _HA_ATOMIC_ADD(&bin->alloc_tot, size);
249 return ret;
250}
251
252/* This is the new global calloc() function. It must optimize for the normal
253 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
254 * It must remain simple to guarantee the lack of reentrance. stdio is not
255 * possible there even for debugging. The reported size is the really allocated
256 * one as returned by malloc_usable_size(), because this will allow it to be
257 * compared to the one before realloc() or free(). This is a GNU and jemalloc
258 * extension but other systems may also store this size in ptr[-1].
259 */
260void *calloc(size_t nmemb, size_t size)
261{
262 struct memprof_stats *bin;
263 void *ret;
264
265 if (likely(!(profiling & HA_PROF_MEMORY)))
266 return memprof_calloc_handler(nmemb, size);
267
268 ret = memprof_calloc_handler(nmemb, size);
269 size = malloc_usable_size(ret);
270
Willy Tarreau616491b2021-05-11 09:26:23 +0200271 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_CALLOC);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200272 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
273 _HA_ATOMIC_ADD(&bin->alloc_tot, size);
274 return ret;
275}
276
277/* This is the new global realloc() function. It must optimize for the normal
278 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
279 * It must remain simple to guarantee the lack of reentrance. stdio is not
280 * possible there even for debugging. The reported size is the really allocated
281 * one as returned by malloc_usable_size(), because this will allow it to be
282 * compared to the one before realloc() or free(). This is a GNU and jemalloc
283 * extension but other systems may also store this size in ptr[-1].
284 * Depending on the old vs new size, it's considered as an allocation or a free
285 * (or neither if the size remains the same).
286 */
287void *realloc(void *ptr, size_t size)
288{
289 struct memprof_stats *bin;
290 size_t size_before;
291 void *ret;
292
293 if (likely(!(profiling & HA_PROF_MEMORY)))
294 return memprof_realloc_handler(ptr, size);
295
296 size_before = malloc_usable_size(ptr);
297 ret = memprof_realloc_handler(ptr, size);
Willy Tarreau2639e2e2021-05-07 08:01:35 +0200298 size = malloc_usable_size(ret);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200299
Willy Tarreau616491b2021-05-11 09:26:23 +0200300 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_REALLOC);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200301 if (size > size_before) {
302 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
Willy Tarreau79acefa2021-05-11 09:12:56 +0200303 _HA_ATOMIC_ADD(&bin->alloc_tot, size - size_before);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200304 } else if (size < size_before) {
305 _HA_ATOMIC_ADD(&bin->free_calls, 1);
Willy Tarreau79acefa2021-05-11 09:12:56 +0200306 _HA_ATOMIC_ADD(&bin->free_tot, size_before - size);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200307 }
308 return ret;
309}
310
311/* This is the new global free() function. It must optimize for the normal
312 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
313 * It must remain simple to guarantee the lack of reentrance. stdio is not
314 * possible there even for debugging. The reported size is the really allocated
315 * one as returned by malloc_usable_size(), because this will allow it to be
316 * compared to the one before realloc() or free(). This is a GNU and jemalloc
317 * extension but other systems may also store this size in ptr[-1]. Since
318 * free() is often called on NULL pointers to collect garbage at the end of
319 * many functions or during config parsing, as a special case free(NULL)
320 * doesn't update any stats.
321 */
322void free(void *ptr)
323{
324 struct memprof_stats *bin;
325 size_t size_before;
326
327 if (likely(!(profiling & HA_PROF_MEMORY) || !ptr)) {
328 memprof_free_handler(ptr);
329 return;
330 }
331
332 size_before = malloc_usable_size(ptr);
333 memprof_free_handler(ptr);
334
Willy Tarreau616491b2021-05-11 09:26:23 +0200335 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_FREE);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200336 _HA_ATOMIC_ADD(&bin->free_calls, 1);
337 _HA_ATOMIC_ADD(&bin->free_tot, size_before);
338}
339
Willy Tarreaudb87fc72021-05-05 16:50:40 +0200340#endif // USE_MEMORY_PROFILING
341
Willy Tarreau609aad92018-11-22 08:31:09 +0100342/* Updates the current thread's statistics about stolen CPU time. The unit for
343 * <stolen> is half-milliseconds.
344 */
345void report_stolen_time(uint64_t stolen)
346{
347 activity[tid].cpust_total += stolen;
348 update_freq_ctr(&activity[tid].cpust_1s, stolen);
349 update_freq_ctr_period(&activity[tid].cpust_15s, 15000, stolen);
350}
Willy Tarreau75c62c22018-11-22 11:02:09 +0100351
Willy Tarreau20adfde2021-10-08 11:34:46 +0200352/* Update avg_loop value for the current thread and possibly decide to enable
353 * task-level profiling on the current thread based on its average run time.
354 * The <run_time> argument is the number of microseconds elapsed since the
355 * last time poll() returned.
Willy Tarreaue0650222021-10-06 16:22:09 +0200356 */
Willy Tarreau20adfde2021-10-08 11:34:46 +0200357void activity_count_runtime(uint32_t run_time)
Willy Tarreaue0650222021-10-06 16:22:09 +0200358{
Willy Tarreaue0650222021-10-06 16:22:09 +0200359 uint32_t up, down;
360
361 /* 1 millisecond per loop on average over last 1024 iterations is
362 * enough to turn on profiling.
363 */
364 up = 1000;
365 down = up * 99 / 100;
366
Willy Tarreaue0650222021-10-06 16:22:09 +0200367 run_time = swrate_add(&activity[tid].avg_loop_us, TIME_STATS_SAMPLES, run_time);
368
369 /* In automatic mode, reaching the "up" threshold on average switches
370 * profiling to "on" when automatic, and going back below the "down"
371 * threshold switches to off. The forced modes don't check the load.
372 */
373 if (!(task_profiling_mask & tid_bit)) {
374 if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_ON ||
375 ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AON &&
376 swrate_avg(run_time, TIME_STATS_SAMPLES) >= up)))
377 _HA_ATOMIC_OR(&task_profiling_mask, tid_bit);
378 } else {
379 if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_OFF ||
380 ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AOFF &&
381 swrate_avg(run_time, TIME_STATS_SAMPLES) <= down)))
382 _HA_ATOMIC_AND(&task_profiling_mask, ~tid_bit);
383 }
384}
385
Willy Tarreauca3afc22021-05-05 18:33:19 +0200386#ifdef USE_MEMORY_PROFILING
387/* config parser for global "profiling.memory", accepts "on" or "off" */
388static int cfg_parse_prof_memory(char **args, int section_type, struct proxy *curpx,
389 const struct proxy *defpx, const char *file, int line,
390 char **err)
391{
392 if (too_many_args(1, args, err, NULL))
393 return -1;
394
395 if (strcmp(args[1], "on") == 0)
396 profiling |= HA_PROF_MEMORY;
397 else if (strcmp(args[1], "off") == 0)
398 profiling &= ~HA_PROF_MEMORY;
399 else {
400 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
401 return -1;
402 }
403 return 0;
404}
405#endif // USE_MEMORY_PROFILING
406
Willy Tarreau75c62c22018-11-22 11:02:09 +0100407/* config parser for global "profiling.tasks", accepts "on" or "off" */
408static int cfg_parse_prof_tasks(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100409 const struct proxy *defpx, const char *file, int line,
Willy Tarreau75c62c22018-11-22 11:02:09 +0100410 char **err)
411{
412 if (too_many_args(1, args, err, NULL))
413 return -1;
414
415 if (strcmp(args[1], "on") == 0)
Willy Tarreaud2d33482019-04-25 17:09:07 +0200416 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON;
417 else if (strcmp(args[1], "auto") == 0)
Willy Tarreauaa622b82021-01-28 21:44:22 +0100418 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100419 else if (strcmp(args[1], "off") == 0)
Willy Tarreaud2d33482019-04-25 17:09:07 +0200420 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100421 else {
Willy Tarreaud2d33482019-04-25 17:09:07 +0200422 memprintf(err, "'%s' expects either 'on', 'auto', or 'off' but got '%s'.", args[0], args[1]);
Willy Tarreau75c62c22018-11-22 11:02:09 +0100423 return -1;
424 }
425 return 0;
426}
427
428/* parse a "set profiling" command. It always returns 1. */
429static int cli_parse_set_profiling(char **args, char *payload, struct appctx *appctx, void *private)
430{
Willy Tarreau75c62c22018-11-22 11:02:09 +0100431 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
432 return 1;
433
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200434 if (strcmp(args[2], "memory") == 0) {
Willy Tarreaudb87fc72021-05-05 16:50:40 +0200435#ifdef USE_MEMORY_PROFILING
436 if (strcmp(args[3], "on") == 0) {
437 unsigned int old = profiling;
438 int i;
439
440 while (!_HA_ATOMIC_CAS(&profiling, &old, old | HA_PROF_MEMORY))
441 ;
442
443 /* also flush current profiling stats */
444 for (i = 0; i < sizeof(memprof_stats) / sizeof(memprof_stats[0]); i++) {
445 HA_ATOMIC_STORE(&memprof_stats[i].alloc_calls, 0);
446 HA_ATOMIC_STORE(&memprof_stats[i].free_calls, 0);
447 HA_ATOMIC_STORE(&memprof_stats[i].alloc_tot, 0);
448 HA_ATOMIC_STORE(&memprof_stats[i].free_tot, 0);
449 HA_ATOMIC_STORE(&memprof_stats[i].caller, NULL);
450 }
451 }
452 else if (strcmp(args[3], "off") == 0) {
453 unsigned int old = profiling;
454
455 while (!_HA_ATOMIC_CAS(&profiling, &old, old & ~HA_PROF_MEMORY))
456 ;
457 }
458 else
459 return cli_err(appctx, "Expects either 'on' or 'off'.\n");
460 return 1;
461#else
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200462 return cli_err(appctx, "Memory profiling not compiled in.\n");
Willy Tarreaudb87fc72021-05-05 16:50:40 +0200463#endif
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200464 }
465
Willy Tarreau9d008692019-08-09 11:21:01 +0200466 if (strcmp(args[2], "tasks") != 0)
Ilya Shipitsin3df59892021-05-10 12:50:00 +0500467 return cli_err(appctx, "Expects either 'tasks' or 'memory'.\n");
Willy Tarreau75c62c22018-11-22 11:02:09 +0100468
Willy Tarreaud2d33482019-04-25 17:09:07 +0200469 if (strcmp(args[3], "on") == 0) {
470 unsigned int old = profiling;
Willy Tarreaucfa71012021-01-29 11:56:21 +0100471 int i;
472
Willy Tarreaud2d33482019-04-25 17:09:07 +0200473 while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON))
474 ;
Willy Tarreaucfa71012021-01-29 11:56:21 +0100475 /* also flush current profiling stats */
476 for (i = 0; i < 256; i++) {
477 HA_ATOMIC_STORE(&sched_activity[i].calls, 0);
478 HA_ATOMIC_STORE(&sched_activity[i].cpu_time, 0);
479 HA_ATOMIC_STORE(&sched_activity[i].lat_time, 0);
480 HA_ATOMIC_STORE(&sched_activity[i].func, NULL);
481 }
Willy Tarreaud2d33482019-04-25 17:09:07 +0200482 }
483 else if (strcmp(args[3], "auto") == 0) {
484 unsigned int old = profiling;
Willy Tarreauaa622b82021-01-28 21:44:22 +0100485 unsigned int new;
486
487 do {
488 if ((old & HA_PROF_TASKS_MASK) >= HA_PROF_TASKS_AON)
489 new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AON;
490 else
491 new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF;
492 } while (!_HA_ATOMIC_CAS(&profiling, &old, new));
Willy Tarreaud2d33482019-04-25 17:09:07 +0200493 }
494 else if (strcmp(args[3], "off") == 0) {
495 unsigned int old = profiling;
496 while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF))
497 ;
498 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200499 else
500 return cli_err(appctx, "Expects 'on', 'auto', or 'off'.\n");
501
Willy Tarreau75c62c22018-11-22 11:02:09 +0100502 return 1;
503}
504
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200505static int cmp_sched_activity_calls(const void *a, const void *b)
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100506{
507 const struct sched_activity *l = (const struct sched_activity *)a;
508 const struct sched_activity *r = (const struct sched_activity *)b;
509
510 if (l->calls > r->calls)
511 return -1;
512 else if (l->calls < r->calls)
513 return 1;
514 else
515 return 0;
516}
517
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200518static int cmp_sched_activity_addr(const void *a, const void *b)
519{
520 const struct sched_activity *l = (const struct sched_activity *)a;
521 const struct sched_activity *r = (const struct sched_activity *)b;
522
523 if (l->func > r->func)
524 return -1;
525 else if (l->func < r->func)
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 Tarreaua26be372021-10-06 16:26:33 +0200560/* Computes the index of function pointer <func> for use with sched_activity[]
561 * or any other similar array passed in <array>, and returns a pointer to the
562 * entry after having atomically assigned it to this function pointer. Note
563 * that in case of collision, the first entry is returned instead ("other").
564 */
565struct sched_activity *sched_activity_entry(struct sched_activity *array, const void *func)
566{
567 uint64_t hash = XXH64_avalanche(XXH64_mergeRound((size_t)func, (size_t)func));
568 struct sched_activity *ret;
569 const void *old = NULL;
570
571 hash ^= (hash >> 32);
572 hash ^= (hash >> 16);
573 hash ^= (hash >> 8);
574 hash &= 0xff;
575 ret = &array[hash];
576
577 if (likely(ret->func == func))
578 return ret;
579
580 if (HA_ATOMIC_CAS(&ret->func, &old, func))
581 return ret;
582
583 return array;
584}
585
Willy Tarreau75c62c22018-11-22 11:02:09 +0100586/* This function dumps all profiling settings. It returns 0 if the output
587 * buffer is full and it needs to be called again, otherwise non-zero.
Willy Tarreau637d85a2021-05-05 17:33:27 +0200588 * It dumps some parts depending on the following states:
589 * ctx.cli.i0:
590 * 0, 4: dump status, then jump to 1 if 0
591 * 1, 5: dump tasks, then jump to 2 if 1
592 * 2, 6: dump memory, then stop
593 * ctx.cli.i1:
594 * restart line for each step (starts at zero)
595 * ctx.cli.o0:
596 * may contain a configured max line count for each step (0=not set)
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200597 * ctx.cli.o1:
598 * 0: sort by usage
599 * 1: sort by address
Willy Tarreau75c62c22018-11-22 11:02:09 +0100600 */
601static int cli_io_handler_show_profiling(struct appctx *appctx)
602{
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100603 struct sched_activity tmp_activity[256] __attribute__((aligned(64)));
Willy Tarreaue15615c2021-08-28 12:04:25 +0200604#ifdef USE_MEMORY_PROFILING
Willy Tarreau993d44d2021-05-05 18:07:02 +0200605 struct memprof_stats tmp_memstats[MEMPROF_HASH_BUCKETS + 1];
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200606 unsigned long long tot_alloc_calls, tot_free_calls;
607 unsigned long long tot_alloc_bytes, tot_free_bytes;
Willy Tarreau993d44d2021-05-05 18:07:02 +0200608#endif
Willy Tarreau75c62c22018-11-22 11:02:09 +0100609 struct stream_interface *si = appctx->owner;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100610 struct buffer *name_buffer = get_trash_chunk();
Willy Tarreaud2d33482019-04-25 17:09:07 +0200611 const char *str;
Willy Tarreau637d85a2021-05-05 17:33:27 +0200612 int max_lines;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100613 int i, max;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100614
615 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
616 return 1;
617
618 chunk_reset(&trash);
619
Willy Tarreaud2d33482019-04-25 17:09:07 +0200620 switch (profiling & HA_PROF_TASKS_MASK) {
Willy Tarreauaa622b82021-01-28 21:44:22 +0100621 case HA_PROF_TASKS_AOFF: str="auto-off"; break;
622 case HA_PROF_TASKS_AON: str="auto-on"; break;
Willy Tarreaud2d33482019-04-25 17:09:07 +0200623 case HA_PROF_TASKS_ON: str="on"; break;
624 default: str="off"; break;
625 }
626
Willy Tarreau637d85a2021-05-05 17:33:27 +0200627 if ((appctx->ctx.cli.i0 & 3) != 0)
628 goto skip_status;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100629
Willy Tarreaud2d33482019-04-25 17:09:07 +0200630 chunk_printf(&trash,
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200631 "Per-task CPU profiling : %-8s # set profiling tasks {on|auto|off}\n"
632 "Memory usage profiling : %-8s # set profiling memory {on|off}\n",
633 str, (profiling & HA_PROF_MEMORY) ? "on" : "off");
Willy Tarreau75c62c22018-11-22 11:02:09 +0100634
Willy Tarreau637d85a2021-05-05 17:33:27 +0200635 if (ci_putchk(si_ic(si), &trash) == -1) {
636 /* failed, try again */
637 si_rx_room_blk(si);
638 return 0;
639 }
640
641 appctx->ctx.cli.i1 = 0; // reset first line to dump
642 if ((appctx->ctx.cli.i0 & 4) == 0)
643 appctx->ctx.cli.i0++; // next step
644
645 skip_status:
646 if ((appctx->ctx.cli.i0 & 3) != 1)
647 goto skip_tasks;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100648
Willy Tarreau637d85a2021-05-05 17:33:27 +0200649 memcpy(tmp_activity, sched_activity, sizeof(tmp_activity));
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200650 if (appctx->ctx.cli.o1)
651 qsort(tmp_activity, 256, sizeof(tmp_activity[0]), cmp_sched_activity_addr);
652 else
653 qsort(tmp_activity, 256, sizeof(tmp_activity[0]), cmp_sched_activity_calls);
Willy Tarreau637d85a2021-05-05 17:33:27 +0200654
655 if (!appctx->ctx.cli.i1)
656 chunk_appendf(&trash, "Tasks activity:\n"
657 " function calls cpu_tot cpu_avg lat_tot lat_avg\n");
658
659 max_lines = appctx->ctx.cli.o0;
660 if (!max_lines)
661 max_lines = 256;
662
663 for (i = appctx->ctx.cli.i1; i < max_lines && tmp_activity[i].calls; i++) {
664 appctx->ctx.cli.i1 = i;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100665 chunk_reset(name_buffer);
666
667 if (!tmp_activity[i].func)
668 chunk_printf(name_buffer, "other");
669 else
670 resolve_sym_name(name_buffer, "", tmp_activity[i].func);
671
672 /* reserve 35 chars for name+' '+#calls, knowing that longer names
673 * are often used for less often called functions.
674 */
675 max = 35 - name_buffer->data;
676 if (max < 1)
677 max = 1;
678 chunk_appendf(&trash, " %s%*llu", name_buffer->area, max, (unsigned long long)tmp_activity[i].calls);
679
680 print_time_short(&trash, " ", tmp_activity[i].cpu_time, "");
681 print_time_short(&trash, " ", tmp_activity[i].cpu_time / tmp_activity[i].calls, "");
682 print_time_short(&trash, " ", tmp_activity[i].lat_time, "");
683 print_time_short(&trash, " ", tmp_activity[i].lat_time / tmp_activity[i].calls, "\n");
Willy Tarreau637d85a2021-05-05 17:33:27 +0200684
685 if (ci_putchk(si_ic(si), &trash) == -1) {
686 /* failed, try again */
687 si_rx_room_blk(si);
688 return 0;
689 }
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100690 }
691
Willy Tarreau75c62c22018-11-22 11:02:09 +0100692 if (ci_putchk(si_ic(si), &trash) == -1) {
693 /* failed, try again */
694 si_rx_room_blk(si);
695 return 0;
696 }
Willy Tarreau637d85a2021-05-05 17:33:27 +0200697
698 appctx->ctx.cli.i1 = 0; // reset first line to dump
699 if ((appctx->ctx.cli.i0 & 4) == 0)
700 appctx->ctx.cli.i0++; // next step
701
702 skip_tasks:
703
Willy Tarreaue15615c2021-08-28 12:04:25 +0200704#ifdef USE_MEMORY_PROFILING
Willy Tarreau993d44d2021-05-05 18:07:02 +0200705 if ((appctx->ctx.cli.i0 & 3) != 2)
706 goto skip_mem;
707
708 memcpy(tmp_memstats, memprof_stats, sizeof(tmp_memstats));
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200709 if (appctx->ctx.cli.o1)
710 qsort(tmp_memstats, MEMPROF_HASH_BUCKETS+1, sizeof(tmp_memstats[0]), cmp_memprof_addr);
711 else
712 qsort(tmp_memstats, MEMPROF_HASH_BUCKETS+1, sizeof(tmp_memstats[0]), cmp_memprof_stats);
Willy Tarreau993d44d2021-05-05 18:07:02 +0200713
714 if (!appctx->ctx.cli.i1)
715 chunk_appendf(&trash,
716 "Alloc/Free statistics by call place:\n"
Willy Tarreau616491b2021-05-11 09:26:23 +0200717 " Calls | Tot Bytes | Caller and method\n"
Willy Tarreau993d44d2021-05-05 18:07:02 +0200718 "<- alloc -> <- free ->|<-- alloc ---> <-- free ---->|\n");
719
720 max_lines = appctx->ctx.cli.o0;
721 if (!max_lines)
722 max_lines = MEMPROF_HASH_BUCKETS + 1;
723
724 for (i = appctx->ctx.cli.i1; i < max_lines; i++) {
725 struct memprof_stats *entry = &tmp_memstats[i];
726
727 appctx->ctx.cli.i1 = i;
728 if (!entry->alloc_calls && !entry->free_calls)
729 continue;
730 chunk_appendf(&trash, "%11llu %11llu %14llu %14llu| %16p ",
731 entry->alloc_calls, entry->free_calls,
732 entry->alloc_tot, entry->free_tot,
733 entry->caller);
734
735 if (entry->caller)
736 resolve_sym_name(&trash, NULL, entry->caller);
737 else
738 chunk_appendf(&trash, "[other]");
739
Willy Tarreau616491b2021-05-11 09:26:23 +0200740 chunk_appendf(&trash," %s(%lld)\n", memprof_methods[entry->method],
741 (long long)(entry->alloc_tot - entry->free_tot) / (long long)(entry->alloc_calls + entry->free_calls));
Willy Tarreau993d44d2021-05-05 18:07:02 +0200742
743 if (ci_putchk(si_ic(si), &trash) == -1) {
744 si_rx_room_blk(si);
745 return 0;
746 }
747 }
748
749 if (ci_putchk(si_ic(si), &trash) == -1) {
750 si_rx_room_blk(si);
751 return 0;
752 }
753
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200754 tot_alloc_calls = tot_free_calls = tot_alloc_bytes = tot_free_bytes = 0;
755 for (i = 0; i < max_lines; i++) {
756 tot_alloc_calls += tmp_memstats[i].alloc_calls;
757 tot_free_calls += tmp_memstats[i].free_calls;
758 tot_alloc_bytes += tmp_memstats[i].alloc_tot;
759 tot_free_bytes += tmp_memstats[i].free_tot;
760 }
761
762 chunk_appendf(&trash,
763 "-----------------------|-----------------------------|\n"
764 "%11llu %11llu %14llu %14llu| <- Total; Delta_calls=%lld; Delta_bytes=%lld\n",
765 tot_alloc_calls, tot_free_calls,
766 tot_alloc_bytes, tot_free_bytes,
767 tot_alloc_calls - tot_free_calls,
768 tot_alloc_bytes - tot_free_bytes);
769
770 if (ci_putchk(si_ic(si), &trash) == -1) {
771 si_rx_room_blk(si);
772 return 0;
773 }
774
Willy Tarreau993d44d2021-05-05 18:07:02 +0200775 appctx->ctx.cli.i1 = 0; // reset first line to dump
776 if ((appctx->ctx.cli.i0 & 4) == 0)
777 appctx->ctx.cli.i0++; // next step
778
779 skip_mem:
780#endif // USE_MEMORY_PROFILING
781
Willy Tarreau75c62c22018-11-22 11:02:09 +0100782 return 1;
783}
784
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200785/* parse a "show profiling" command. It returns 1 on failure, 0 if it starts to dump.
786 * - cli.i0 is set to the first state (0=all, 4=status, 5=tasks, 6=memory)
787 * - cli.o1 is set to 1 if the output must be sorted by addr instead of usage
788 * - cli.o0 is set to the number of lines of output
789 */
Willy Tarreau42712cb2021-05-05 17:48:13 +0200790static int cli_parse_show_profiling(char **args, char *payload, struct appctx *appctx, void *private)
791{
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200792 int arg;
793
Willy Tarreau42712cb2021-05-05 17:48:13 +0200794 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
795 return 1;
796
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200797 for (arg = 2; *args[arg]; arg++) {
798 if (strcmp(args[arg], "all") == 0) {
799 appctx->ctx.cli.i0 = 0; // will cycle through 0,1,2; default
800 }
801 else if (strcmp(args[arg], "status") == 0) {
802 appctx->ctx.cli.i0 = 4; // will visit status only
803 }
804 else if (strcmp(args[arg], "tasks") == 0) {
805 appctx->ctx.cli.i0 = 5; // will visit tasks only
806 }
807 else if (strcmp(args[arg], "memory") == 0) {
808 appctx->ctx.cli.i0 = 6; // will visit memory only
809 }
810 else if (strcmp(args[arg], "byaddr") == 0) {
811 appctx->ctx.cli.o1 = 1; // sort output by address instead of usage
812 }
813 else if (isdigit((unsigned char)*args[arg])) {
814 appctx->ctx.cli.o0 = atoi(args[arg]); // number of entries to dump
815 }
816 else
817 return cli_err(appctx, "Expects either 'all', 'status', 'tasks', 'memory', 'byaddr' or a max number of output lines.\n");
Willy Tarreau42712cb2021-05-05 17:48:13 +0200818 }
819 return 0;
820}
821
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100822/* This function scans all threads' run queues and collects statistics about
823 * running tasks. It returns 0 if the output buffer is full and it needs to be
824 * called again, otherwise non-zero.
825 */
826static int cli_io_handler_show_tasks(struct appctx *appctx)
827{
828 struct sched_activity tmp_activity[256] __attribute__((aligned(64)));
829 struct stream_interface *si = appctx->owner;
830 struct buffer *name_buffer = get_trash_chunk();
831 struct sched_activity *entry;
832 const struct tasklet *tl;
833 const struct task *t;
834 uint64_t now_ns, lat;
835 struct eb32sc_node *rqnode;
836 uint64_t tot_calls;
837 int thr, queue;
838 int i, max;
839
840 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
841 return 1;
842
843 /* It's not possible to scan queues in small chunks and yield in the
844 * middle of the dump and come back again. So what we're doing instead
845 * is to freeze all threads and inspect their queues at once as fast as
846 * possible, using a sched_activity array to collect metrics with
847 * limited collision, then we'll report statistics only. The tasks'
848 * #calls will reflect the number of occurrences, and the lat_time will
Willy Tarreau75f72332021-01-29 15:04:16 +0100849 * reflect the latency when set. We prefer to take the time before
850 * calling thread_isolate() so that the wait time doesn't impact the
851 * measurement accuracy. However this requires to take care of negative
852 * times since tasks might be queued after we retrieve it.
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100853 */
854
855 now_ns = now_mono_time();
856 memset(tmp_activity, 0, sizeof(tmp_activity));
857
858 thread_isolate();
859
860 /* 1. global run queue */
861
862#ifdef USE_THREAD
863 rqnode = eb32sc_first(&rqueue, ~0UL);
864 while (rqnode) {
865 t = eb32sc_entry(rqnode, struct task, rq);
866 entry = sched_activity_entry(tmp_activity, t->process);
867 if (t->call_date) {
868 lat = now_ns - t->call_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100869 if ((int64_t)lat > 0)
870 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100871 }
872 entry->calls++;
873 rqnode = eb32sc_next(rqnode, ~0UL);
874 }
875#endif
876 /* 2. all threads's local run queues */
877 for (thr = 0; thr < global.nbthread; thr++) {
878 /* task run queue */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200879 rqnode = eb32sc_first(&ha_thread_ctx[thr].rqueue, ~0UL);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100880 while (rqnode) {
881 t = eb32sc_entry(rqnode, struct task, rq);
882 entry = sched_activity_entry(tmp_activity, t->process);
883 if (t->call_date) {
884 lat = now_ns - t->call_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100885 if ((int64_t)lat > 0)
886 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100887 }
888 entry->calls++;
889 rqnode = eb32sc_next(rqnode, ~0UL);
890 }
891
892 /* shared tasklet list */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200893 list_for_each_entry(tl, mt_list_to_list(&ha_thread_ctx[thr].shared_tasklet_list), list) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100894 t = (const struct task *)tl;
895 entry = sched_activity_entry(tmp_activity, t->process);
896 if (!TASK_IS_TASKLET(t) && t->call_date) {
897 lat = now_ns - t->call_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100898 if ((int64_t)lat > 0)
899 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100900 }
901 entry->calls++;
902 }
903
904 /* classful tasklets */
905 for (queue = 0; queue < TL_CLASSES; queue++) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200906 list_for_each_entry(tl, &ha_thread_ctx[thr].tasklets[queue], list) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100907 t = (const struct task *)tl;
908 entry = sched_activity_entry(tmp_activity, t->process);
909 if (!TASK_IS_TASKLET(t) && t->call_date) {
910 lat = now_ns - t->call_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100911 if ((int64_t)lat > 0)
912 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100913 }
914 entry->calls++;
915 }
916 }
917 }
918
919 /* hopefully we're done */
920 thread_release();
921
922 chunk_reset(&trash);
923
924 tot_calls = 0;
925 for (i = 0; i < 256; i++)
926 tot_calls += tmp_activity[i].calls;
927
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200928 qsort(tmp_activity, 256, sizeof(tmp_activity[0]), cmp_sched_activity_calls);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100929
930 chunk_appendf(&trash, "Running tasks: %d (%d threads)\n"
931 " function places %% lat_tot lat_avg\n",
932 (int)tot_calls, global.nbthread);
933
934 for (i = 0; i < 256 && tmp_activity[i].calls; i++) {
935 chunk_reset(name_buffer);
936
937 if (!tmp_activity[i].func)
938 chunk_printf(name_buffer, "other");
939 else
940 resolve_sym_name(name_buffer, "", tmp_activity[i].func);
941
942 /* reserve 35 chars for name+' '+#calls, knowing that longer names
943 * are often used for less often called functions.
944 */
945 max = 35 - name_buffer->data;
946 if (max < 1)
947 max = 1;
948 chunk_appendf(&trash, " %s%*llu %3d.%1d",
949 name_buffer->area, max, (unsigned long long)tmp_activity[i].calls,
950 (int)(100ULL * tmp_activity[i].calls / tot_calls),
951 (int)((1000ULL * tmp_activity[i].calls / tot_calls)%10));
952 print_time_short(&trash, " ", tmp_activity[i].lat_time, "");
953 print_time_short(&trash, " ", tmp_activity[i].lat_time / tmp_activity[i].calls, "\n");
954 }
955
956 if (ci_putchk(si_ic(si), &trash) == -1) {
957 /* failed, try again */
958 si_rx_room_blk(si);
959 return 0;
960 }
961 return 1;
962}
963
Willy Tarreau75c62c22018-11-22 11:02:09 +0100964/* config keyword parsers */
965static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreauca3afc22021-05-05 18:33:19 +0200966#ifdef USE_MEMORY_PROFILING
967 { CFG_GLOBAL, "profiling.memory", cfg_parse_prof_memory },
968#endif
Willy Tarreau75c62c22018-11-22 11:02:09 +0100969 { CFG_GLOBAL, "profiling.tasks", cfg_parse_prof_tasks },
970 { 0, NULL, NULL }
971}};
972
Willy Tarreau0108d902018-11-25 19:14:37 +0100973INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
974
Willy Tarreau75c62c22018-11-22 11:02:09 +0100975/* register cli keywords */
976static struct cli_kw_list cli_kws = {{ },{
Daniel Corbett67b3cef2021-05-10 14:08:40 -0400977 { { "set", "profiling", NULL }, "set profiling <what> {auto|on|off} : enable/disable resource profiling (tasks,memory)", cli_parse_set_profiling, NULL },
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200978 { { "show", "profiling", NULL }, "show profiling [<what>|<#lines>|byaddr]*: show profiling state (all,status,tasks,memory)", cli_parse_show_profiling, cli_io_handler_show_profiling, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +0200979 { { "show", "tasks", NULL }, "show tasks : show running tasks", NULL, cli_io_handler_show_tasks, NULL },
Willy Tarreau75c62c22018-11-22 11:02:09 +0100980 {{},}
981}};
982
Willy Tarreau0108d902018-11-25 19:14:37 +0100983INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);