blob: e72b9c7dece6e4ed1c1fce2475c723132e57b03e [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>
Christopher Faulet908628c2022-03-25 16:43:49 +010019#include <haproxy/conn_stream.h>
20#include <haproxy/cs_utils.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/freq_ctr.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020022#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020023#include <haproxy/tools.h>
Willy Tarreaua26be372021-10-06 16:26:33 +020024#include <haproxy/xxhash.h>
Willy Tarreau75c62c22018-11-22 11:02:09 +010025
Willy Tarreauf93c7be2021-05-05 17:07:09 +020026#if defined(DEBUG_MEM_STATS)
27/* these ones are macros in bug.h when DEBUG_MEM_STATS is set, and will
28 * prevent the new ones from being redefined.
29 */
30#undef calloc
31#undef malloc
32#undef realloc
33#endif
Willy Tarreau75c62c22018-11-22 11:02:09 +010034
35/* bit field of profiling options. Beware, may be modified at runtime! */
Willy Tarreauef7380f2021-05-05 16:28:31 +020036unsigned int profiling __read_mostly = HA_PROF_TASKS_AOFF;
37unsigned long task_profiling_mask __read_mostly = 0;
Willy Tarreau609aad92018-11-22 08:31:09 +010038
39/* One struct per thread containing all collected measurements */
40struct activity activity[MAX_THREADS] __attribute__((aligned(64))) = { };
41
Willy Tarreau3fb6a7b2021-01-28 19:19:26 +010042/* One struct per function pointer hash entry (256 values, 0=collision) */
43struct sched_activity sched_activity[256] __attribute__((aligned(64))) = { };
Willy Tarreau609aad92018-11-22 08:31:09 +010044
Willy Tarreaudb87fc72021-05-05 16:50:40 +020045
Willy Tarreaue15615c2021-08-28 12:04:25 +020046#ifdef USE_MEMORY_PROFILING
Willy Tarreaudb87fc72021-05-05 16:50:40 +020047/* determine the number of buckets to store stats */
48#define MEMPROF_HASH_BITS 10
49#define MEMPROF_HASH_BUCKETS (1U << MEMPROF_HASH_BITS)
50
Willy Tarreau616491b2021-05-11 09:26:23 +020051enum memprof_method {
52 MEMPROF_METH_UNKNOWN = 0,
53 MEMPROF_METH_MALLOC,
54 MEMPROF_METH_CALLOC,
55 MEMPROF_METH_REALLOC,
56 MEMPROF_METH_FREE,
57 MEMPROF_METH_METHODS /* count, must be last */
58};
59
60static const char *const memprof_methods[MEMPROF_METH_METHODS] = {
61 "unknown", "malloc", "calloc", "realloc", "free",
62};
63
Willy Tarreaudb87fc72021-05-05 16:50:40 +020064/* stats:
65 * - malloc increases alloc
66 * - free increases free (if non null)
67 * - realloc increases either depending on the size change.
68 * when the real size is known (malloc_usable_size()), it's used in free_tot
69 * and alloc_tot, otherwise the requested size is reported in alloc_tot and
70 * zero in free_tot.
71 */
72struct memprof_stats {
73 const void *caller;
Willy Tarreau616491b2021-05-11 09:26:23 +020074 enum memprof_method method;
75 /* 4-7 bytes hole here */
Willy Tarreaudb87fc72021-05-05 16:50:40 +020076 unsigned long long alloc_calls;
77 unsigned long long free_calls;
78 unsigned long long alloc_tot;
79 unsigned long long free_tot;
80};
81
82/* last one is for hash collisions ("others") and has no caller address */
83struct memprof_stats memprof_stats[MEMPROF_HASH_BUCKETS + 1] = { };
84
Willy Tarreauf93c7be2021-05-05 17:07:09 +020085/* used to detect recursive calls */
86static THREAD_LOCAL int in_memprof = 0;
87
88/* perform a pointer hash by scrambling its bits and retrieving the most
89 * mixed ones (topmost ones in 32-bit, middle ones in 64-bit).
90 */
91static unsigned int memprof_hash_ptr(const void *p)
92{
93 unsigned long long x = (unsigned long)p;
94
95 x = 0xcbda9653U * x;
96 if (sizeof(long) == 4)
97 x >>= 32;
98 else
99 x >>= 33 - MEMPROF_HASH_BITS / 2;
100 return x & (MEMPROF_HASH_BUCKETS - 1);
101}
102
103/* These ones are used by glibc and will be called early. They are in charge of
104 * initializing the handlers with the original functions.
105 */
106static void *memprof_malloc_initial_handler(size_t size);
107static void *memprof_calloc_initial_handler(size_t nmemb, size_t size);
108static void *memprof_realloc_initial_handler(void *ptr, size_t size);
109static void memprof_free_initial_handler(void *ptr);
110
111/* Fallback handlers for the main alloc/free functions. They are preset to
112 * the initializer in order to save a test in the functions's critical path.
113 */
114static void *(*memprof_malloc_handler)(size_t size) = memprof_malloc_initial_handler;
115static void *(*memprof_calloc_handler)(size_t nmemb, size_t size) = memprof_calloc_initial_handler;
116static void *(*memprof_realloc_handler)(void *ptr, size_t size) = memprof_realloc_initial_handler;
117static void (*memprof_free_handler)(void *ptr) = memprof_free_initial_handler;
118
119/* Used to force to die if it's not possible to retrieve the allocation
120 * functions. We cannot even use stdio in this case.
121 */
122static __attribute__((noreturn)) void memprof_die(const char *msg)
123{
124 DISGUISE(write(2, msg, strlen(msg)));
125 exit(1);
126}
127
128/* Resolve original allocation functions and initialize all handlers.
129 * This must be called very early at boot, before the very first malloc()
130 * call, and is not thread-safe! It's not even possible to use stdio there.
131 * Worse, we have to account for the risk of reentrance from dlsym() when
132 * it tries to prepare its error messages. Here its ahndled by in_memprof
133 * that makes allocators return NULL. dlsym() handles it gracefully. An
Ilya Shipitsin3df59892021-05-10 12:50:00 +0500134 * alternate approach consists in calling aligned_alloc() from these places
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200135 * but that would mean not being able to intercept it later if considered
136 * useful to do so.
137 */
138static void memprof_init()
139{
140 in_memprof++;
141 memprof_malloc_handler = get_sym_next_addr("malloc");
142 if (!memprof_malloc_handler)
143 memprof_die("FATAL: malloc() function not found.\n");
144
145 memprof_calloc_handler = get_sym_next_addr("calloc");
146 if (!memprof_calloc_handler)
147 memprof_die("FATAL: calloc() function not found.\n");
148
149 memprof_realloc_handler = get_sym_next_addr("realloc");
150 if (!memprof_realloc_handler)
151 memprof_die("FATAL: realloc() function not found.\n");
152
153 memprof_free_handler = get_sym_next_addr("free");
154 if (!memprof_free_handler)
155 memprof_die("FATAL: free() function not found.\n");
156 in_memprof--;
157}
158
159/* the initial handlers will initialize all regular handlers and will call the
160 * one they correspond to. A single one of these functions will typically be
161 * called, though it's unknown which one (as any might be called before main).
162 */
163static void *memprof_malloc_initial_handler(size_t size)
164{
165 if (in_memprof) {
166 /* it's likely that dlsym() needs malloc(), let's fail */
167 return NULL;
168 }
169
170 memprof_init();
171 return memprof_malloc_handler(size);
172}
173
174static void *memprof_calloc_initial_handler(size_t nmemb, size_t size)
175{
176 if (in_memprof) {
177 /* it's likely that dlsym() needs calloc(), let's fail */
178 return NULL;
179 }
180 memprof_init();
181 return memprof_calloc_handler(nmemb, size);
182}
183
184static void *memprof_realloc_initial_handler(void *ptr, size_t size)
185{
186 if (in_memprof) {
187 /* it's likely that dlsym() needs realloc(), let's fail */
188 return NULL;
189 }
190
191 memprof_init();
192 return memprof_realloc_handler(ptr, size);
193}
194
195static void memprof_free_initial_handler(void *ptr)
196{
197 memprof_init();
198 memprof_free_handler(ptr);
199}
200
201/* Assign a bin for the memprof_stats to the return address. May perform a few
202 * attempts before finding the right one, but always succeeds (in the worst
203 * case, returns a default bin). The caller address is atomically set except
204 * for the default one which is never set.
205 */
Willy Tarreau616491b2021-05-11 09:26:23 +0200206static struct memprof_stats *memprof_get_bin(const void *ra, enum memprof_method meth)
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200207{
208 int retries = 16; // up to 16 consecutive entries may be tested.
Willy Tarreau4a753282021-05-09 23:18:50 +0200209 const void *old;
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200210 unsigned int bin;
211
212 bin = memprof_hash_ptr(ra);
213 for (; memprof_stats[bin].caller != ra; bin = (bin + 1) & (MEMPROF_HASH_BUCKETS - 1)) {
214 if (!--retries) {
215 bin = MEMPROF_HASH_BUCKETS;
216 break;
217 }
218
219 old = NULL;
220 if (!memprof_stats[bin].caller &&
Willy Tarreau616491b2021-05-11 09:26:23 +0200221 HA_ATOMIC_CAS(&memprof_stats[bin].caller, &old, ra)) {
222 memprof_stats[bin].method = meth;
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200223 break;
Willy Tarreau616491b2021-05-11 09:26:23 +0200224 }
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200225 }
226 return &memprof_stats[bin];
227}
228
229/* This is the new global malloc() function. It must optimize for the normal
230 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
231 * It must remain simple to guarantee the lack of reentrance. stdio is not
232 * possible there even for debugging. The reported size is the really allocated
233 * one as returned by malloc_usable_size(), because this will allow it to be
234 * compared to the one before realloc() or free(). This is a GNU and jemalloc
235 * extension but other systems may also store this size in ptr[-1].
236 */
237void *malloc(size_t size)
238{
239 struct memprof_stats *bin;
240 void *ret;
241
242 if (likely(!(profiling & HA_PROF_MEMORY)))
243 return memprof_malloc_handler(size);
244
245 ret = memprof_malloc_handler(size);
Willy Tarreau1de51eb2021-10-22 16:33:53 +0200246 size = malloc_usable_size(ret) + sizeof(void *);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200247
Willy Tarreau616491b2021-05-11 09:26:23 +0200248 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_MALLOC);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200249 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
250 _HA_ATOMIC_ADD(&bin->alloc_tot, size);
251 return ret;
252}
253
254/* This is the new global calloc() function. It must optimize for the normal
255 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
256 * It must remain simple to guarantee the lack of reentrance. stdio is not
257 * possible there even for debugging. The reported size is the really allocated
258 * one as returned by malloc_usable_size(), because this will allow it to be
259 * compared to the one before realloc() or free(). This is a GNU and jemalloc
260 * extension but other systems may also store this size in ptr[-1].
261 */
262void *calloc(size_t nmemb, size_t size)
263{
264 struct memprof_stats *bin;
265 void *ret;
266
267 if (likely(!(profiling & HA_PROF_MEMORY)))
268 return memprof_calloc_handler(nmemb, size);
269
270 ret = memprof_calloc_handler(nmemb, size);
Willy Tarreau1de51eb2021-10-22 16:33:53 +0200271 size = malloc_usable_size(ret) + sizeof(void *);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200272
Willy Tarreau616491b2021-05-11 09:26:23 +0200273 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_CALLOC);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200274 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
275 _HA_ATOMIC_ADD(&bin->alloc_tot, size);
276 return ret;
277}
278
279/* This is the new global realloc() function. It must optimize for the normal
280 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
281 * It must remain simple to guarantee the lack of reentrance. stdio is not
282 * possible there even for debugging. The reported size is the really allocated
283 * one as returned by malloc_usable_size(), because this will allow it to be
284 * compared to the one before realloc() or free(). This is a GNU and jemalloc
285 * extension but other systems may also store this size in ptr[-1].
286 * Depending on the old vs new size, it's considered as an allocation or a free
287 * (or neither if the size remains the same).
288 */
289void *realloc(void *ptr, size_t size)
290{
291 struct memprof_stats *bin;
292 size_t size_before;
293 void *ret;
294
295 if (likely(!(profiling & HA_PROF_MEMORY)))
296 return memprof_realloc_handler(ptr, size);
297
298 size_before = malloc_usable_size(ptr);
299 ret = memprof_realloc_handler(ptr, size);
Willy Tarreau2639e2e2021-05-07 08:01:35 +0200300 size = malloc_usable_size(ret);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200301
Willy Tarreau1de51eb2021-10-22 16:33:53 +0200302 /* only count the extra link for new allocations */
303 if (!ptr)
304 size += sizeof(void *);
305
Willy Tarreau616491b2021-05-11 09:26:23 +0200306 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_REALLOC);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200307 if (size > size_before) {
308 _HA_ATOMIC_ADD(&bin->alloc_calls, 1);
Willy Tarreau79acefa2021-05-11 09:12:56 +0200309 _HA_ATOMIC_ADD(&bin->alloc_tot, size - size_before);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200310 } else if (size < size_before) {
311 _HA_ATOMIC_ADD(&bin->free_calls, 1);
Willy Tarreau79acefa2021-05-11 09:12:56 +0200312 _HA_ATOMIC_ADD(&bin->free_tot, size_before - size);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200313 }
314 return ret;
315}
316
317/* This is the new global free() function. It must optimize for the normal
318 * case (i.e. profiling disabled) hence the first test to permit a direct jump.
319 * It must remain simple to guarantee the lack of reentrance. stdio is not
320 * possible there even for debugging. The reported size is the really allocated
321 * one as returned by malloc_usable_size(), because this will allow it to be
322 * compared to the one before realloc() or free(). This is a GNU and jemalloc
323 * extension but other systems may also store this size in ptr[-1]. Since
324 * free() is often called on NULL pointers to collect garbage at the end of
325 * many functions or during config parsing, as a special case free(NULL)
326 * doesn't update any stats.
327 */
328void free(void *ptr)
329{
330 struct memprof_stats *bin;
331 size_t size_before;
332
333 if (likely(!(profiling & HA_PROF_MEMORY) || !ptr)) {
334 memprof_free_handler(ptr);
335 return;
336 }
337
Willy Tarreau1de51eb2021-10-22 16:33:53 +0200338 size_before = malloc_usable_size(ptr) + sizeof(void *);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200339 memprof_free_handler(ptr);
340
Willy Tarreau616491b2021-05-11 09:26:23 +0200341 bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_FREE);
Willy Tarreauf93c7be2021-05-05 17:07:09 +0200342 _HA_ATOMIC_ADD(&bin->free_calls, 1);
343 _HA_ATOMIC_ADD(&bin->free_tot, size_before);
344}
345
Willy Tarreaudb87fc72021-05-05 16:50:40 +0200346#endif // USE_MEMORY_PROFILING
347
Willy Tarreau609aad92018-11-22 08:31:09 +0100348/* Updates the current thread's statistics about stolen CPU time. The unit for
349 * <stolen> is half-milliseconds.
350 */
351void report_stolen_time(uint64_t stolen)
352{
353 activity[tid].cpust_total += stolen;
354 update_freq_ctr(&activity[tid].cpust_1s, stolen);
355 update_freq_ctr_period(&activity[tid].cpust_15s, 15000, stolen);
356}
Willy Tarreau75c62c22018-11-22 11:02:09 +0100357
Willy Tarreau20adfde2021-10-08 11:34:46 +0200358/* Update avg_loop value for the current thread and possibly decide to enable
359 * task-level profiling on the current thread based on its average run time.
360 * The <run_time> argument is the number of microseconds elapsed since the
361 * last time poll() returned.
Willy Tarreaue0650222021-10-06 16:22:09 +0200362 */
Willy Tarreau20adfde2021-10-08 11:34:46 +0200363void activity_count_runtime(uint32_t run_time)
Willy Tarreaue0650222021-10-06 16:22:09 +0200364{
Willy Tarreaue0650222021-10-06 16:22:09 +0200365 uint32_t up, down;
366
367 /* 1 millisecond per loop on average over last 1024 iterations is
368 * enough to turn on profiling.
369 */
370 up = 1000;
371 down = up * 99 / 100;
372
Willy Tarreaue0650222021-10-06 16:22:09 +0200373 run_time = swrate_add(&activity[tid].avg_loop_us, TIME_STATS_SAMPLES, run_time);
374
375 /* In automatic mode, reaching the "up" threshold on average switches
376 * profiling to "on" when automatic, and going back below the "down"
377 * threshold switches to off. The forced modes don't check the load.
378 */
379 if (!(task_profiling_mask & tid_bit)) {
380 if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_ON ||
381 ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AON &&
382 swrate_avg(run_time, TIME_STATS_SAMPLES) >= up)))
383 _HA_ATOMIC_OR(&task_profiling_mask, tid_bit);
384 } else {
385 if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_OFF ||
386 ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AOFF &&
387 swrate_avg(run_time, TIME_STATS_SAMPLES) <= down)))
388 _HA_ATOMIC_AND(&task_profiling_mask, ~tid_bit);
389 }
390}
391
Willy Tarreauca3afc22021-05-05 18:33:19 +0200392#ifdef USE_MEMORY_PROFILING
393/* config parser for global "profiling.memory", accepts "on" or "off" */
394static int cfg_parse_prof_memory(char **args, int section_type, struct proxy *curpx,
395 const struct proxy *defpx, const char *file, int line,
396 char **err)
397{
398 if (too_many_args(1, args, err, NULL))
399 return -1;
400
401 if (strcmp(args[1], "on") == 0)
402 profiling |= HA_PROF_MEMORY;
403 else if (strcmp(args[1], "off") == 0)
404 profiling &= ~HA_PROF_MEMORY;
405 else {
406 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
407 return -1;
408 }
409 return 0;
410}
411#endif // USE_MEMORY_PROFILING
412
Willy Tarreau75c62c22018-11-22 11:02:09 +0100413/* config parser for global "profiling.tasks", accepts "on" or "off" */
414static int cfg_parse_prof_tasks(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100415 const struct proxy *defpx, const char *file, int line,
Willy Tarreau75c62c22018-11-22 11:02:09 +0100416 char **err)
417{
418 if (too_many_args(1, args, err, NULL))
419 return -1;
420
421 if (strcmp(args[1], "on") == 0)
Willy Tarreaud2d33482019-04-25 17:09:07 +0200422 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON;
423 else if (strcmp(args[1], "auto") == 0)
Willy Tarreauaa622b82021-01-28 21:44:22 +0100424 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100425 else if (strcmp(args[1], "off") == 0)
Willy Tarreaud2d33482019-04-25 17:09:07 +0200426 profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100427 else {
Willy Tarreaud2d33482019-04-25 17:09:07 +0200428 memprintf(err, "'%s' expects either 'on', 'auto', or 'off' but got '%s'.", args[0], args[1]);
Willy Tarreau75c62c22018-11-22 11:02:09 +0100429 return -1;
430 }
431 return 0;
432}
433
434/* parse a "set profiling" command. It always returns 1. */
435static int cli_parse_set_profiling(char **args, char *payload, struct appctx *appctx, void *private)
436{
Willy Tarreau75c62c22018-11-22 11:02:09 +0100437 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
438 return 1;
439
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200440 if (strcmp(args[2], "memory") == 0) {
Willy Tarreaudb87fc72021-05-05 16:50:40 +0200441#ifdef USE_MEMORY_PROFILING
442 if (strcmp(args[3], "on") == 0) {
443 unsigned int old = profiling;
444 int i;
445
446 while (!_HA_ATOMIC_CAS(&profiling, &old, old | HA_PROF_MEMORY))
447 ;
448
449 /* also flush current profiling stats */
450 for (i = 0; i < sizeof(memprof_stats) / sizeof(memprof_stats[0]); i++) {
451 HA_ATOMIC_STORE(&memprof_stats[i].alloc_calls, 0);
452 HA_ATOMIC_STORE(&memprof_stats[i].free_calls, 0);
453 HA_ATOMIC_STORE(&memprof_stats[i].alloc_tot, 0);
454 HA_ATOMIC_STORE(&memprof_stats[i].free_tot, 0);
455 HA_ATOMIC_STORE(&memprof_stats[i].caller, NULL);
456 }
457 }
458 else if (strcmp(args[3], "off") == 0) {
459 unsigned int old = profiling;
460
461 while (!_HA_ATOMIC_CAS(&profiling, &old, old & ~HA_PROF_MEMORY))
462 ;
463 }
464 else
465 return cli_err(appctx, "Expects either 'on' or 'off'.\n");
466 return 1;
467#else
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200468 return cli_err(appctx, "Memory profiling not compiled in.\n");
Willy Tarreaudb87fc72021-05-05 16:50:40 +0200469#endif
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200470 }
471
Willy Tarreau9d008692019-08-09 11:21:01 +0200472 if (strcmp(args[2], "tasks") != 0)
Ilya Shipitsin3df59892021-05-10 12:50:00 +0500473 return cli_err(appctx, "Expects either 'tasks' or 'memory'.\n");
Willy Tarreau75c62c22018-11-22 11:02:09 +0100474
Willy Tarreaud2d33482019-04-25 17:09:07 +0200475 if (strcmp(args[3], "on") == 0) {
476 unsigned int old = profiling;
Willy Tarreaucfa71012021-01-29 11:56:21 +0100477 int i;
478
Willy Tarreaud2d33482019-04-25 17:09:07 +0200479 while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON))
480 ;
Willy Tarreaucfa71012021-01-29 11:56:21 +0100481 /* also flush current profiling stats */
482 for (i = 0; i < 256; i++) {
483 HA_ATOMIC_STORE(&sched_activity[i].calls, 0);
484 HA_ATOMIC_STORE(&sched_activity[i].cpu_time, 0);
485 HA_ATOMIC_STORE(&sched_activity[i].lat_time, 0);
486 HA_ATOMIC_STORE(&sched_activity[i].func, NULL);
487 }
Willy Tarreaud2d33482019-04-25 17:09:07 +0200488 }
489 else if (strcmp(args[3], "auto") == 0) {
490 unsigned int old = profiling;
Willy Tarreauaa622b82021-01-28 21:44:22 +0100491 unsigned int new;
492
493 do {
494 if ((old & HA_PROF_TASKS_MASK) >= HA_PROF_TASKS_AON)
495 new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AON;
496 else
497 new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF;
498 } while (!_HA_ATOMIC_CAS(&profiling, &old, new));
Willy Tarreaud2d33482019-04-25 17:09:07 +0200499 }
500 else if (strcmp(args[3], "off") == 0) {
501 unsigned int old = profiling;
502 while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF))
503 ;
504 }
Willy Tarreau9d008692019-08-09 11:21:01 +0200505 else
506 return cli_err(appctx, "Expects 'on', 'auto', or 'off'.\n");
507
Willy Tarreau75c62c22018-11-22 11:02:09 +0100508 return 1;
509}
510
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200511static int cmp_sched_activity_calls(const void *a, const void *b)
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100512{
513 const struct sched_activity *l = (const struct sched_activity *)a;
514 const struct sched_activity *r = (const struct sched_activity *)b;
515
516 if (l->calls > r->calls)
517 return -1;
518 else if (l->calls < r->calls)
519 return 1;
520 else
521 return 0;
522}
523
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200524static int cmp_sched_activity_addr(const void *a, const void *b)
525{
526 const struct sched_activity *l = (const struct sched_activity *)a;
527 const struct sched_activity *r = (const struct sched_activity *)b;
528
529 if (l->func > r->func)
530 return -1;
531 else if (l->func < r->func)
532 return 1;
533 else
534 return 0;
535}
536
Willy Tarreaue15615c2021-08-28 12:04:25 +0200537#ifdef USE_MEMORY_PROFILING
Willy Tarreau993d44d2021-05-05 18:07:02 +0200538/* used by qsort below */
539static int cmp_memprof_stats(const void *a, const void *b)
540{
541 const struct memprof_stats *l = (const struct memprof_stats *)a;
542 const struct memprof_stats *r = (const struct memprof_stats *)b;
543
544 if (l->alloc_tot + l->free_tot > r->alloc_tot + r->free_tot)
545 return -1;
546 else if (l->alloc_tot + l->free_tot < r->alloc_tot + r->free_tot)
547 return 1;
548 else
549 return 0;
550}
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200551
552static int cmp_memprof_addr(const void *a, const void *b)
553{
554 const struct memprof_stats *l = (const struct memprof_stats *)a;
555 const struct memprof_stats *r = (const struct memprof_stats *)b;
556
557 if (l->caller > r->caller)
558 return -1;
559 else if (l->caller < r->caller)
560 return 1;
561 else
562 return 0;
563}
Willy Tarreau993d44d2021-05-05 18:07:02 +0200564#endif // USE_MEMORY_PROFILING
565
Willy Tarreaua26be372021-10-06 16:26:33 +0200566/* Computes the index of function pointer <func> for use with sched_activity[]
567 * or any other similar array passed in <array>, and returns a pointer to the
568 * entry after having atomically assigned it to this function pointer. Note
569 * that in case of collision, the first entry is returned instead ("other").
570 */
571struct sched_activity *sched_activity_entry(struct sched_activity *array, const void *func)
572{
573 uint64_t hash = XXH64_avalanche(XXH64_mergeRound((size_t)func, (size_t)func));
574 struct sched_activity *ret;
575 const void *old = NULL;
576
577 hash ^= (hash >> 32);
578 hash ^= (hash >> 16);
579 hash ^= (hash >> 8);
580 hash &= 0xff;
581 ret = &array[hash];
582
583 if (likely(ret->func == func))
584 return ret;
585
586 if (HA_ATOMIC_CAS(&ret->func, &old, func))
587 return ret;
588
589 return array;
590}
591
Willy Tarreau75c62c22018-11-22 11:02:09 +0100592/* This function dumps all profiling settings. It returns 0 if the output
593 * buffer is full and it needs to be called again, otherwise non-zero.
Willy Tarreau637d85a2021-05-05 17:33:27 +0200594 * It dumps some parts depending on the following states:
595 * ctx.cli.i0:
596 * 0, 4: dump status, then jump to 1 if 0
597 * 1, 5: dump tasks, then jump to 2 if 1
598 * 2, 6: dump memory, then stop
599 * ctx.cli.i1:
600 * restart line for each step (starts at zero)
601 * ctx.cli.o0:
602 * may contain a configured max line count for each step (0=not set)
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200603 * ctx.cli.o1:
604 * 0: sort by usage
605 * 1: sort by address
Willy Tarreau75c62c22018-11-22 11:02:09 +0100606 */
607static int cli_io_handler_show_profiling(struct appctx *appctx)
608{
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100609 struct sched_activity tmp_activity[256] __attribute__((aligned(64)));
Willy Tarreaue15615c2021-08-28 12:04:25 +0200610#ifdef USE_MEMORY_PROFILING
Willy Tarreau993d44d2021-05-05 18:07:02 +0200611 struct memprof_stats tmp_memstats[MEMPROF_HASH_BUCKETS + 1];
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200612 unsigned long long tot_alloc_calls, tot_free_calls;
613 unsigned long long tot_alloc_bytes, tot_free_bytes;
Willy Tarreau993d44d2021-05-05 18:07:02 +0200614#endif
Christopher Faulet908628c2022-03-25 16:43:49 +0100615 struct conn_stream *cs = appctx->owner;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100616 struct buffer *name_buffer = get_trash_chunk();
Willy Tarreaud2d33482019-04-25 17:09:07 +0200617 const char *str;
Willy Tarreau637d85a2021-05-05 17:33:27 +0200618 int max_lines;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100619 int i, max;
Willy Tarreau75c62c22018-11-22 11:02:09 +0100620
Christopher Faulet908628c2022-03-25 16:43:49 +0100621 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau75c62c22018-11-22 11:02:09 +0100622 return 1;
623
624 chunk_reset(&trash);
625
Willy Tarreaud2d33482019-04-25 17:09:07 +0200626 switch (profiling & HA_PROF_TASKS_MASK) {
Willy Tarreauaa622b82021-01-28 21:44:22 +0100627 case HA_PROF_TASKS_AOFF: str="auto-off"; break;
628 case HA_PROF_TASKS_AON: str="auto-on"; break;
Willy Tarreaud2d33482019-04-25 17:09:07 +0200629 case HA_PROF_TASKS_ON: str="on"; break;
630 default: str="off"; break;
631 }
632
Willy Tarreau637d85a2021-05-05 17:33:27 +0200633 if ((appctx->ctx.cli.i0 & 3) != 0)
634 goto skip_status;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100635
Willy Tarreaud2d33482019-04-25 17:09:07 +0200636 chunk_printf(&trash,
Willy Tarreau00dd44f2021-05-05 16:44:23 +0200637 "Per-task CPU profiling : %-8s # set profiling tasks {on|auto|off}\n"
638 "Memory usage profiling : %-8s # set profiling memory {on|off}\n",
639 str, (profiling & HA_PROF_MEMORY) ? "on" : "off");
Willy Tarreau75c62c22018-11-22 11:02:09 +0100640
Christopher Faulet908628c2022-03-25 16:43:49 +0100641 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Willy Tarreau637d85a2021-05-05 17:33:27 +0200642 /* failed, try again */
Christopher Faulet908628c2022-03-25 16:43:49 +0100643 si_rx_room_blk(cs->si);
Willy Tarreau637d85a2021-05-05 17:33:27 +0200644 return 0;
645 }
646
647 appctx->ctx.cli.i1 = 0; // reset first line to dump
648 if ((appctx->ctx.cli.i0 & 4) == 0)
649 appctx->ctx.cli.i0++; // next step
650
651 skip_status:
652 if ((appctx->ctx.cli.i0 & 3) != 1)
653 goto skip_tasks;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100654
Willy Tarreau637d85a2021-05-05 17:33:27 +0200655 memcpy(tmp_activity, sched_activity, sizeof(tmp_activity));
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200656 if (appctx->ctx.cli.o1)
657 qsort(tmp_activity, 256, sizeof(tmp_activity[0]), cmp_sched_activity_addr);
658 else
659 qsort(tmp_activity, 256, sizeof(tmp_activity[0]), cmp_sched_activity_calls);
Willy Tarreau637d85a2021-05-05 17:33:27 +0200660
661 if (!appctx->ctx.cli.i1)
662 chunk_appendf(&trash, "Tasks activity:\n"
663 " function calls cpu_tot cpu_avg lat_tot lat_avg\n");
664
665 max_lines = appctx->ctx.cli.o0;
666 if (!max_lines)
667 max_lines = 256;
668
669 for (i = appctx->ctx.cli.i1; i < max_lines && tmp_activity[i].calls; i++) {
670 appctx->ctx.cli.i1 = i;
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100671 chunk_reset(name_buffer);
672
673 if (!tmp_activity[i].func)
674 chunk_printf(name_buffer, "other");
675 else
676 resolve_sym_name(name_buffer, "", tmp_activity[i].func);
677
678 /* reserve 35 chars for name+' '+#calls, knowing that longer names
679 * are often used for less often called functions.
680 */
681 max = 35 - name_buffer->data;
682 if (max < 1)
683 max = 1;
684 chunk_appendf(&trash, " %s%*llu", name_buffer->area, max, (unsigned long long)tmp_activity[i].calls);
685
686 print_time_short(&trash, " ", tmp_activity[i].cpu_time, "");
687 print_time_short(&trash, " ", tmp_activity[i].cpu_time / tmp_activity[i].calls, "");
688 print_time_short(&trash, " ", tmp_activity[i].lat_time, "");
689 print_time_short(&trash, " ", tmp_activity[i].lat_time / tmp_activity[i].calls, "\n");
Willy Tarreau637d85a2021-05-05 17:33:27 +0200690
Christopher Faulet908628c2022-03-25 16:43:49 +0100691 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Willy Tarreau637d85a2021-05-05 17:33:27 +0200692 /* failed, try again */
Christopher Faulet908628c2022-03-25 16:43:49 +0100693 si_rx_room_blk(cs->si);
Willy Tarreau637d85a2021-05-05 17:33:27 +0200694 return 0;
695 }
Willy Tarreau1bd67e92021-01-29 00:07:40 +0100696 }
697
Christopher Faulet908628c2022-03-25 16:43:49 +0100698 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Willy Tarreau75c62c22018-11-22 11:02:09 +0100699 /* failed, try again */
Christopher Faulet908628c2022-03-25 16:43:49 +0100700 si_rx_room_blk(cs->si);
Willy Tarreau75c62c22018-11-22 11:02:09 +0100701 return 0;
702 }
Willy Tarreau637d85a2021-05-05 17:33:27 +0200703
704 appctx->ctx.cli.i1 = 0; // reset first line to dump
705 if ((appctx->ctx.cli.i0 & 4) == 0)
706 appctx->ctx.cli.i0++; // next step
707
708 skip_tasks:
709
Willy Tarreaue15615c2021-08-28 12:04:25 +0200710#ifdef USE_MEMORY_PROFILING
Willy Tarreau993d44d2021-05-05 18:07:02 +0200711 if ((appctx->ctx.cli.i0 & 3) != 2)
712 goto skip_mem;
713
714 memcpy(tmp_memstats, memprof_stats, sizeof(tmp_memstats));
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200715 if (appctx->ctx.cli.o1)
716 qsort(tmp_memstats, MEMPROF_HASH_BUCKETS+1, sizeof(tmp_memstats[0]), cmp_memprof_addr);
717 else
718 qsort(tmp_memstats, MEMPROF_HASH_BUCKETS+1, sizeof(tmp_memstats[0]), cmp_memprof_stats);
Willy Tarreau993d44d2021-05-05 18:07:02 +0200719
720 if (!appctx->ctx.cli.i1)
721 chunk_appendf(&trash,
722 "Alloc/Free statistics by call place:\n"
Willy Tarreau616491b2021-05-11 09:26:23 +0200723 " Calls | Tot Bytes | Caller and method\n"
Willy Tarreau993d44d2021-05-05 18:07:02 +0200724 "<- alloc -> <- free ->|<-- alloc ---> <-- free ---->|\n");
725
726 max_lines = appctx->ctx.cli.o0;
727 if (!max_lines)
728 max_lines = MEMPROF_HASH_BUCKETS + 1;
729
730 for (i = appctx->ctx.cli.i1; i < max_lines; i++) {
731 struct memprof_stats *entry = &tmp_memstats[i];
732
733 appctx->ctx.cli.i1 = i;
734 if (!entry->alloc_calls && !entry->free_calls)
735 continue;
736 chunk_appendf(&trash, "%11llu %11llu %14llu %14llu| %16p ",
737 entry->alloc_calls, entry->free_calls,
738 entry->alloc_tot, entry->free_tot,
739 entry->caller);
740
741 if (entry->caller)
742 resolve_sym_name(&trash, NULL, entry->caller);
743 else
744 chunk_appendf(&trash, "[other]");
745
Willy Tarreau8cce4d72021-10-22 16:26:12 +0200746 chunk_appendf(&trash," %s(%lld)", memprof_methods[entry->method],
Willy Tarreau616491b2021-05-11 09:26:23 +0200747 (long long)(entry->alloc_tot - entry->free_tot) / (long long)(entry->alloc_calls + entry->free_calls));
Willy Tarreau993d44d2021-05-05 18:07:02 +0200748
Willy Tarreau8cce4d72021-10-22 16:26:12 +0200749 if (entry->alloc_tot && entry->free_tot) {
750 /* that's a realloc, show the total diff to help spot leaks */
751 chunk_appendf(&trash," [delta=%lld]", (long long)(entry->alloc_tot - entry->free_tot));
752 }
753
754 chunk_appendf(&trash, "\n");
755
Christopher Faulet908628c2022-03-25 16:43:49 +0100756 if (ci_putchk(cs_ic(cs), &trash) == -1) {
757 si_rx_room_blk(cs->si);
Willy Tarreau993d44d2021-05-05 18:07:02 +0200758 return 0;
759 }
760 }
761
Christopher Faulet908628c2022-03-25 16:43:49 +0100762 if (ci_putchk(cs_ic(cs), &trash) == -1) {
763 si_rx_room_blk(cs->si);
Willy Tarreau993d44d2021-05-05 18:07:02 +0200764 return 0;
765 }
766
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200767 tot_alloc_calls = tot_free_calls = tot_alloc_bytes = tot_free_bytes = 0;
768 for (i = 0; i < max_lines; i++) {
769 tot_alloc_calls += tmp_memstats[i].alloc_calls;
770 tot_free_calls += tmp_memstats[i].free_calls;
771 tot_alloc_bytes += tmp_memstats[i].alloc_tot;
772 tot_free_bytes += tmp_memstats[i].free_tot;
773 }
774
775 chunk_appendf(&trash,
776 "-----------------------|-----------------------------|\n"
777 "%11llu %11llu %14llu %14llu| <- Total; Delta_calls=%lld; Delta_bytes=%lld\n",
778 tot_alloc_calls, tot_free_calls,
779 tot_alloc_bytes, tot_free_bytes,
780 tot_alloc_calls - tot_free_calls,
781 tot_alloc_bytes - tot_free_bytes);
782
Christopher Faulet908628c2022-03-25 16:43:49 +0100783 if (ci_putchk(cs_ic(cs), &trash) == -1) {
784 si_rx_room_blk(cs->si);
Willy Tarreauf5fb8582021-05-11 14:06:24 +0200785 return 0;
786 }
787
Willy Tarreau993d44d2021-05-05 18:07:02 +0200788 appctx->ctx.cli.i1 = 0; // reset first line to dump
789 if ((appctx->ctx.cli.i0 & 4) == 0)
790 appctx->ctx.cli.i0++; // next step
791
792 skip_mem:
793#endif // USE_MEMORY_PROFILING
794
Willy Tarreau75c62c22018-11-22 11:02:09 +0100795 return 1;
796}
797
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200798/* parse a "show profiling" command. It returns 1 on failure, 0 if it starts to dump.
799 * - cli.i0 is set to the first state (0=all, 4=status, 5=tasks, 6=memory)
800 * - cli.o1 is set to 1 if the output must be sorted by addr instead of usage
801 * - cli.o0 is set to the number of lines of output
802 */
Willy Tarreau42712cb2021-05-05 17:48:13 +0200803static int cli_parse_show_profiling(char **args, char *payload, struct appctx *appctx, void *private)
804{
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200805 int arg;
806
Willy Tarreau42712cb2021-05-05 17:48:13 +0200807 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
808 return 1;
809
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200810 for (arg = 2; *args[arg]; arg++) {
811 if (strcmp(args[arg], "all") == 0) {
812 appctx->ctx.cli.i0 = 0; // will cycle through 0,1,2; default
813 }
814 else if (strcmp(args[arg], "status") == 0) {
815 appctx->ctx.cli.i0 = 4; // will visit status only
816 }
817 else if (strcmp(args[arg], "tasks") == 0) {
818 appctx->ctx.cli.i0 = 5; // will visit tasks only
819 }
820 else if (strcmp(args[arg], "memory") == 0) {
821 appctx->ctx.cli.i0 = 6; // will visit memory only
822 }
823 else if (strcmp(args[arg], "byaddr") == 0) {
824 appctx->ctx.cli.o1 = 1; // sort output by address instead of usage
825 }
826 else if (isdigit((unsigned char)*args[arg])) {
827 appctx->ctx.cli.o0 = atoi(args[arg]); // number of entries to dump
828 }
829 else
830 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 +0200831 }
832 return 0;
833}
834
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100835/* This function scans all threads' run queues and collects statistics about
836 * running tasks. It returns 0 if the output buffer is full and it needs to be
837 * called again, otherwise non-zero.
838 */
839static int cli_io_handler_show_tasks(struct appctx *appctx)
840{
841 struct sched_activity tmp_activity[256] __attribute__((aligned(64)));
Christopher Faulet908628c2022-03-25 16:43:49 +0100842 struct conn_stream *cs = appctx->owner;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100843 struct buffer *name_buffer = get_trash_chunk();
844 struct sched_activity *entry;
845 const struct tasklet *tl;
846 const struct task *t;
847 uint64_t now_ns, lat;
848 struct eb32sc_node *rqnode;
849 uint64_t tot_calls;
850 int thr, queue;
851 int i, max;
852
Christopher Faulet908628c2022-03-25 16:43:49 +0100853 if (unlikely(cs_ic(cs)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100854 return 1;
855
856 /* It's not possible to scan queues in small chunks and yield in the
857 * middle of the dump and come back again. So what we're doing instead
858 * is to freeze all threads and inspect their queues at once as fast as
859 * possible, using a sched_activity array to collect metrics with
860 * limited collision, then we'll report statistics only. The tasks'
861 * #calls will reflect the number of occurrences, and the lat_time will
Willy Tarreau75f72332021-01-29 15:04:16 +0100862 * reflect the latency when set. We prefer to take the time before
863 * calling thread_isolate() so that the wait time doesn't impact the
864 * measurement accuracy. However this requires to take care of negative
865 * times since tasks might be queued after we retrieve it.
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100866 */
867
868 now_ns = now_mono_time();
869 memset(tmp_activity, 0, sizeof(tmp_activity));
870
871 thread_isolate();
872
873 /* 1. global run queue */
874
875#ifdef USE_THREAD
876 rqnode = eb32sc_first(&rqueue, ~0UL);
877 while (rqnode) {
878 t = eb32sc_entry(rqnode, struct task, rq);
879 entry = sched_activity_entry(tmp_activity, t->process);
880 if (t->call_date) {
881 lat = now_ns - t->call_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100882 if ((int64_t)lat > 0)
883 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100884 }
885 entry->calls++;
886 rqnode = eb32sc_next(rqnode, ~0UL);
887 }
888#endif
889 /* 2. all threads's local run queues */
890 for (thr = 0; thr < global.nbthread; thr++) {
891 /* task run queue */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200892 rqnode = eb32sc_first(&ha_thread_ctx[thr].rqueue, ~0UL);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100893 while (rqnode) {
894 t = eb32sc_entry(rqnode, struct task, rq);
895 entry = sched_activity_entry(tmp_activity, t->process);
896 if (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 rqnode = eb32sc_next(rqnode, ~0UL);
903 }
904
905 /* shared tasklet list */
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200906 list_for_each_entry(tl, mt_list_to_list(&ha_thread_ctx[thr].shared_tasklet_list), 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 /* classful tasklets */
918 for (queue = 0; queue < TL_CLASSES; queue++) {
Willy Tarreau1a9c9222021-10-01 11:30:33 +0200919 list_for_each_entry(tl, &ha_thread_ctx[thr].tasklets[queue], list) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100920 t = (const struct task *)tl;
921 entry = sched_activity_entry(tmp_activity, t->process);
922 if (!TASK_IS_TASKLET(t) && t->call_date) {
923 lat = now_ns - t->call_date;
Willy Tarreau75f72332021-01-29 15:04:16 +0100924 if ((int64_t)lat > 0)
925 entry->lat_time += lat;
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100926 }
927 entry->calls++;
928 }
929 }
930 }
931
932 /* hopefully we're done */
933 thread_release();
934
935 chunk_reset(&trash);
936
937 tot_calls = 0;
938 for (i = 0; i < 256; i++)
939 tot_calls += tmp_activity[i].calls;
940
Willy Tarreauf1c8a382021-05-13 10:00:17 +0200941 qsort(tmp_activity, 256, sizeof(tmp_activity[0]), cmp_sched_activity_calls);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100942
943 chunk_appendf(&trash, "Running tasks: %d (%d threads)\n"
944 " function places %% lat_tot lat_avg\n",
945 (int)tot_calls, global.nbthread);
946
947 for (i = 0; i < 256 && tmp_activity[i].calls; i++) {
948 chunk_reset(name_buffer);
949
950 if (!tmp_activity[i].func)
951 chunk_printf(name_buffer, "other");
952 else
953 resolve_sym_name(name_buffer, "", tmp_activity[i].func);
954
955 /* reserve 35 chars for name+' '+#calls, knowing that longer names
956 * are often used for less often called functions.
957 */
958 max = 35 - name_buffer->data;
959 if (max < 1)
960 max = 1;
961 chunk_appendf(&trash, " %s%*llu %3d.%1d",
962 name_buffer->area, max, (unsigned long long)tmp_activity[i].calls,
963 (int)(100ULL * tmp_activity[i].calls / tot_calls),
964 (int)((1000ULL * tmp_activity[i].calls / tot_calls)%10));
965 print_time_short(&trash, " ", tmp_activity[i].lat_time, "");
966 print_time_short(&trash, " ", tmp_activity[i].lat_time / tmp_activity[i].calls, "\n");
967 }
968
Christopher Faulet908628c2022-03-25 16:43:49 +0100969 if (ci_putchk(cs_ic(cs), &trash) == -1) {
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100970 /* failed, try again */
Christopher Faulet908628c2022-03-25 16:43:49 +0100971 si_rx_room_blk(cs->si);
Willy Tarreau7eff06e2021-01-29 11:32:55 +0100972 return 0;
973 }
974 return 1;
975}
976
Willy Tarreau75c62c22018-11-22 11:02:09 +0100977/* config keyword parsers */
978static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreauca3afc22021-05-05 18:33:19 +0200979#ifdef USE_MEMORY_PROFILING
980 { CFG_GLOBAL, "profiling.memory", cfg_parse_prof_memory },
981#endif
Willy Tarreau75c62c22018-11-22 11:02:09 +0100982 { CFG_GLOBAL, "profiling.tasks", cfg_parse_prof_tasks },
983 { 0, NULL, NULL }
984}};
985
Willy Tarreau0108d902018-11-25 19:14:37 +0100986INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
987
Willy Tarreau75c62c22018-11-22 11:02:09 +0100988/* register cli keywords */
989static struct cli_kw_list cli_kws = {{ },{
Daniel Corbett67b3cef2021-05-10 14:08:40 -0400990 { { "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 +0200991 { { "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 +0200992 { { "show", "tasks", NULL }, "show tasks : show running tasks", NULL, cli_io_handler_show_tasks, NULL },
Willy Tarreau75c62c22018-11-22 11:02:09 +0100993 {{},}
994}};
995
Willy Tarreau0108d902018-11-25 19:14:37 +0100996INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);