Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 1 | /* |
| 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 Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 13 | #include <haproxy/activity-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 14 | #include <haproxy/api.h> |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 15 | #include <haproxy/applet.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 16 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 17 | #include <haproxy/clock.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 18 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 19 | #include <haproxy/cli.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 21 | #include <haproxy/sc_strm.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 22 | #include <haproxy/stconn.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 23 | #include <haproxy/tools.h> |
Willy Tarreau | a26be37 | 2021-10-06 16:26:33 +0200 | [diff] [blame] | 24 | #include <haproxy/xxhash.h> |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 25 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 26 | /* CLI context for the "show profiling" command */ |
| 27 | struct 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) */ |
| 31 | int by_addr; /* 0=sort by usage, 1=sort by address */ |
| 32 | }; |
| 33 | |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 34 | #if defined(DEBUG_MEM_STATS) |
| 35 | /* these ones are macros in bug.h when DEBUG_MEM_STATS is set, and will |
| 36 | * prevent the new ones from being redefined. |
| 37 | */ |
| 38 | #undef calloc |
| 39 | #undef malloc |
| 40 | #undef realloc |
| 41 | #endif |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 42 | |
| 43 | /* bit field of profiling options. Beware, may be modified at runtime! */ |
Willy Tarreau | ef7380f | 2021-05-05 16:28:31 +0200 | [diff] [blame] | 44 | unsigned int profiling __read_mostly = HA_PROF_TASKS_AOFF; |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 45 | |
| 46 | /* One struct per thread containing all collected measurements */ |
| 47 | struct activity activity[MAX_THREADS] __attribute__((aligned(64))) = { }; |
| 48 | |
Willy Tarreau | 3fb6a7b | 2021-01-28 19:19:26 +0100 | [diff] [blame] | 49 | /* One struct per function pointer hash entry (256 values, 0=collision) */ |
| 50 | struct sched_activity sched_activity[256] __attribute__((aligned(64))) = { }; |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 51 | |
Willy Tarreau | db87fc7 | 2021-05-05 16:50:40 +0200 | [diff] [blame] | 52 | |
Willy Tarreau | e15615c | 2021-08-28 12:04:25 +0200 | [diff] [blame] | 53 | #ifdef USE_MEMORY_PROFILING |
Willy Tarreau | db87fc7 | 2021-05-05 16:50:40 +0200 | [diff] [blame] | 54 | /* determine the number of buckets to store stats */ |
| 55 | #define MEMPROF_HASH_BITS 10 |
| 56 | #define MEMPROF_HASH_BUCKETS (1U << MEMPROF_HASH_BITS) |
| 57 | |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 58 | enum memprof_method { |
| 59 | MEMPROF_METH_UNKNOWN = 0, |
| 60 | MEMPROF_METH_MALLOC, |
| 61 | MEMPROF_METH_CALLOC, |
| 62 | MEMPROF_METH_REALLOC, |
| 63 | MEMPROF_METH_FREE, |
| 64 | MEMPROF_METH_METHODS /* count, must be last */ |
| 65 | }; |
| 66 | |
| 67 | static const char *const memprof_methods[MEMPROF_METH_METHODS] = { |
| 68 | "unknown", "malloc", "calloc", "realloc", "free", |
| 69 | }; |
| 70 | |
Willy Tarreau | db87fc7 | 2021-05-05 16:50:40 +0200 | [diff] [blame] | 71 | /* stats: |
| 72 | * - malloc increases alloc |
| 73 | * - free increases free (if non null) |
| 74 | * - realloc increases either depending on the size change. |
| 75 | * when the real size is known (malloc_usable_size()), it's used in free_tot |
| 76 | * and alloc_tot, otherwise the requested size is reported in alloc_tot and |
| 77 | * zero in free_tot. |
| 78 | */ |
| 79 | struct memprof_stats { |
| 80 | const void *caller; |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 81 | enum memprof_method method; |
| 82 | /* 4-7 bytes hole here */ |
Willy Tarreau | db87fc7 | 2021-05-05 16:50:40 +0200 | [diff] [blame] | 83 | unsigned long long alloc_calls; |
| 84 | unsigned long long free_calls; |
| 85 | unsigned long long alloc_tot; |
| 86 | unsigned long long free_tot; |
| 87 | }; |
| 88 | |
| 89 | /* last one is for hash collisions ("others") and has no caller address */ |
| 90 | struct memprof_stats memprof_stats[MEMPROF_HASH_BUCKETS + 1] = { }; |
| 91 | |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 92 | /* used to detect recursive calls */ |
| 93 | static THREAD_LOCAL int in_memprof = 0; |
| 94 | |
| 95 | /* perform a pointer hash by scrambling its bits and retrieving the most |
| 96 | * mixed ones (topmost ones in 32-bit, middle ones in 64-bit). |
| 97 | */ |
| 98 | static unsigned int memprof_hash_ptr(const void *p) |
| 99 | { |
| 100 | unsigned long long x = (unsigned long)p; |
| 101 | |
| 102 | x = 0xcbda9653U * x; |
| 103 | if (sizeof(long) == 4) |
| 104 | x >>= 32; |
| 105 | else |
| 106 | x >>= 33 - MEMPROF_HASH_BITS / 2; |
| 107 | return x & (MEMPROF_HASH_BUCKETS - 1); |
| 108 | } |
| 109 | |
| 110 | /* These ones are used by glibc and will be called early. They are in charge of |
| 111 | * initializing the handlers with the original functions. |
| 112 | */ |
| 113 | static void *memprof_malloc_initial_handler(size_t size); |
| 114 | static void *memprof_calloc_initial_handler(size_t nmemb, size_t size); |
| 115 | static void *memprof_realloc_initial_handler(void *ptr, size_t size); |
| 116 | static void memprof_free_initial_handler(void *ptr); |
| 117 | |
| 118 | /* Fallback handlers for the main alloc/free functions. They are preset to |
| 119 | * the initializer in order to save a test in the functions's critical path. |
| 120 | */ |
| 121 | static void *(*memprof_malloc_handler)(size_t size) = memprof_malloc_initial_handler; |
| 122 | static void *(*memprof_calloc_handler)(size_t nmemb, size_t size) = memprof_calloc_initial_handler; |
| 123 | static void *(*memprof_realloc_handler)(void *ptr, size_t size) = memprof_realloc_initial_handler; |
| 124 | static void (*memprof_free_handler)(void *ptr) = memprof_free_initial_handler; |
| 125 | |
| 126 | /* Used to force to die if it's not possible to retrieve the allocation |
| 127 | * functions. We cannot even use stdio in this case. |
| 128 | */ |
| 129 | static __attribute__((noreturn)) void memprof_die(const char *msg) |
| 130 | { |
| 131 | DISGUISE(write(2, msg, strlen(msg))); |
| 132 | exit(1); |
| 133 | } |
| 134 | |
| 135 | /* Resolve original allocation functions and initialize all handlers. |
| 136 | * This must be called very early at boot, before the very first malloc() |
| 137 | * call, and is not thread-safe! It's not even possible to use stdio there. |
| 138 | * Worse, we have to account for the risk of reentrance from dlsym() when |
| 139 | * it tries to prepare its error messages. Here its ahndled by in_memprof |
| 140 | * that makes allocators return NULL. dlsym() handles it gracefully. An |
Ilya Shipitsin | 3df5989 | 2021-05-10 12:50:00 +0500 | [diff] [blame] | 141 | * alternate approach consists in calling aligned_alloc() from these places |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 142 | * but that would mean not being able to intercept it later if considered |
| 143 | * useful to do so. |
| 144 | */ |
| 145 | static void memprof_init() |
| 146 | { |
| 147 | in_memprof++; |
| 148 | memprof_malloc_handler = get_sym_next_addr("malloc"); |
| 149 | if (!memprof_malloc_handler) |
| 150 | memprof_die("FATAL: malloc() function not found.\n"); |
| 151 | |
| 152 | memprof_calloc_handler = get_sym_next_addr("calloc"); |
| 153 | if (!memprof_calloc_handler) |
| 154 | memprof_die("FATAL: calloc() function not found.\n"); |
| 155 | |
| 156 | memprof_realloc_handler = get_sym_next_addr("realloc"); |
| 157 | if (!memprof_realloc_handler) |
| 158 | memprof_die("FATAL: realloc() function not found.\n"); |
| 159 | |
| 160 | memprof_free_handler = get_sym_next_addr("free"); |
| 161 | if (!memprof_free_handler) |
| 162 | memprof_die("FATAL: free() function not found.\n"); |
| 163 | in_memprof--; |
| 164 | } |
| 165 | |
| 166 | /* the initial handlers will initialize all regular handlers and will call the |
| 167 | * one they correspond to. A single one of these functions will typically be |
| 168 | * called, though it's unknown which one (as any might be called before main). |
| 169 | */ |
| 170 | static void *memprof_malloc_initial_handler(size_t size) |
| 171 | { |
| 172 | if (in_memprof) { |
| 173 | /* it's likely that dlsym() needs malloc(), let's fail */ |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | memprof_init(); |
| 178 | return memprof_malloc_handler(size); |
| 179 | } |
| 180 | |
| 181 | static void *memprof_calloc_initial_handler(size_t nmemb, size_t size) |
| 182 | { |
| 183 | if (in_memprof) { |
| 184 | /* it's likely that dlsym() needs calloc(), let's fail */ |
| 185 | return NULL; |
| 186 | } |
| 187 | memprof_init(); |
| 188 | return memprof_calloc_handler(nmemb, size); |
| 189 | } |
| 190 | |
| 191 | static void *memprof_realloc_initial_handler(void *ptr, size_t size) |
| 192 | { |
| 193 | if (in_memprof) { |
| 194 | /* it's likely that dlsym() needs realloc(), let's fail */ |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | memprof_init(); |
| 199 | return memprof_realloc_handler(ptr, size); |
| 200 | } |
| 201 | |
| 202 | static void memprof_free_initial_handler(void *ptr) |
| 203 | { |
| 204 | memprof_init(); |
| 205 | memprof_free_handler(ptr); |
| 206 | } |
| 207 | |
| 208 | /* Assign a bin for the memprof_stats to the return address. May perform a few |
| 209 | * attempts before finding the right one, but always succeeds (in the worst |
| 210 | * case, returns a default bin). The caller address is atomically set except |
| 211 | * for the default one which is never set. |
| 212 | */ |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 213 | static struct memprof_stats *memprof_get_bin(const void *ra, enum memprof_method meth) |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 214 | { |
| 215 | int retries = 16; // up to 16 consecutive entries may be tested. |
Willy Tarreau | 4a75328 | 2021-05-09 23:18:50 +0200 | [diff] [blame] | 216 | const void *old; |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 217 | unsigned int bin; |
| 218 | |
| 219 | bin = memprof_hash_ptr(ra); |
| 220 | for (; memprof_stats[bin].caller != ra; bin = (bin + 1) & (MEMPROF_HASH_BUCKETS - 1)) { |
| 221 | if (!--retries) { |
| 222 | bin = MEMPROF_HASH_BUCKETS; |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | old = NULL; |
| 227 | if (!memprof_stats[bin].caller && |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 228 | HA_ATOMIC_CAS(&memprof_stats[bin].caller, &old, ra)) { |
| 229 | memprof_stats[bin].method = meth; |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 230 | break; |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 231 | } |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 232 | } |
| 233 | return &memprof_stats[bin]; |
| 234 | } |
| 235 | |
| 236 | /* This is the new global malloc() function. It must optimize for the normal |
| 237 | * case (i.e. profiling disabled) hence the first test to permit a direct jump. |
| 238 | * It must remain simple to guarantee the lack of reentrance. stdio is not |
| 239 | * possible there even for debugging. The reported size is the really allocated |
| 240 | * one as returned by malloc_usable_size(), because this will allow it to be |
| 241 | * compared to the one before realloc() or free(). This is a GNU and jemalloc |
| 242 | * extension but other systems may also store this size in ptr[-1]. |
| 243 | */ |
| 244 | void *malloc(size_t size) |
| 245 | { |
| 246 | struct memprof_stats *bin; |
| 247 | void *ret; |
| 248 | |
| 249 | if (likely(!(profiling & HA_PROF_MEMORY))) |
| 250 | return memprof_malloc_handler(size); |
| 251 | |
| 252 | ret = memprof_malloc_handler(size); |
Willy Tarreau | 1de51eb | 2021-10-22 16:33:53 +0200 | [diff] [blame] | 253 | size = malloc_usable_size(ret) + sizeof(void *); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 254 | |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 255 | bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_MALLOC); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 256 | _HA_ATOMIC_ADD(&bin->alloc_calls, 1); |
| 257 | _HA_ATOMIC_ADD(&bin->alloc_tot, size); |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | /* This is the new global calloc() function. It must optimize for the normal |
| 262 | * case (i.e. profiling disabled) hence the first test to permit a direct jump. |
| 263 | * It must remain simple to guarantee the lack of reentrance. stdio is not |
| 264 | * possible there even for debugging. The reported size is the really allocated |
| 265 | * one as returned by malloc_usable_size(), because this will allow it to be |
| 266 | * compared to the one before realloc() or free(). This is a GNU and jemalloc |
| 267 | * extension but other systems may also store this size in ptr[-1]. |
| 268 | */ |
| 269 | void *calloc(size_t nmemb, size_t size) |
| 270 | { |
| 271 | struct memprof_stats *bin; |
| 272 | void *ret; |
| 273 | |
| 274 | if (likely(!(profiling & HA_PROF_MEMORY))) |
| 275 | return memprof_calloc_handler(nmemb, size); |
| 276 | |
| 277 | ret = memprof_calloc_handler(nmemb, size); |
Willy Tarreau | 1de51eb | 2021-10-22 16:33:53 +0200 | [diff] [blame] | 278 | size = malloc_usable_size(ret) + sizeof(void *); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 279 | |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 280 | bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_CALLOC); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 281 | _HA_ATOMIC_ADD(&bin->alloc_calls, 1); |
| 282 | _HA_ATOMIC_ADD(&bin->alloc_tot, size); |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | /* This is the new global realloc() function. It must optimize for the normal |
| 287 | * case (i.e. profiling disabled) hence the first test to permit a direct jump. |
| 288 | * It must remain simple to guarantee the lack of reentrance. stdio is not |
| 289 | * possible there even for debugging. The reported size is the really allocated |
| 290 | * one as returned by malloc_usable_size(), because this will allow it to be |
| 291 | * compared to the one before realloc() or free(). This is a GNU and jemalloc |
| 292 | * extension but other systems may also store this size in ptr[-1]. |
| 293 | * Depending on the old vs new size, it's considered as an allocation or a free |
| 294 | * (or neither if the size remains the same). |
| 295 | */ |
| 296 | void *realloc(void *ptr, size_t size) |
| 297 | { |
| 298 | struct memprof_stats *bin; |
| 299 | size_t size_before; |
| 300 | void *ret; |
| 301 | |
| 302 | if (likely(!(profiling & HA_PROF_MEMORY))) |
| 303 | return memprof_realloc_handler(ptr, size); |
| 304 | |
| 305 | size_before = malloc_usable_size(ptr); |
| 306 | ret = memprof_realloc_handler(ptr, size); |
Willy Tarreau | 2639e2e | 2021-05-07 08:01:35 +0200 | [diff] [blame] | 307 | size = malloc_usable_size(ret); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 308 | |
Willy Tarreau | 1de51eb | 2021-10-22 16:33:53 +0200 | [diff] [blame] | 309 | /* only count the extra link for new allocations */ |
| 310 | if (!ptr) |
| 311 | size += sizeof(void *); |
| 312 | |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 313 | bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_REALLOC); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 314 | if (size > size_before) { |
| 315 | _HA_ATOMIC_ADD(&bin->alloc_calls, 1); |
Willy Tarreau | 79acefa | 2021-05-11 09:12:56 +0200 | [diff] [blame] | 316 | _HA_ATOMIC_ADD(&bin->alloc_tot, size - size_before); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 317 | } else if (size < size_before) { |
| 318 | _HA_ATOMIC_ADD(&bin->free_calls, 1); |
Willy Tarreau | 79acefa | 2021-05-11 09:12:56 +0200 | [diff] [blame] | 319 | _HA_ATOMIC_ADD(&bin->free_tot, size_before - size); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 320 | } |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | /* This is the new global free() function. It must optimize for the normal |
| 325 | * case (i.e. profiling disabled) hence the first test to permit a direct jump. |
| 326 | * It must remain simple to guarantee the lack of reentrance. stdio is not |
| 327 | * possible there even for debugging. The reported size is the really allocated |
| 328 | * one as returned by malloc_usable_size(), because this will allow it to be |
| 329 | * compared to the one before realloc() or free(). This is a GNU and jemalloc |
| 330 | * extension but other systems may also store this size in ptr[-1]. Since |
| 331 | * free() is often called on NULL pointers to collect garbage at the end of |
| 332 | * many functions or during config parsing, as a special case free(NULL) |
| 333 | * doesn't update any stats. |
| 334 | */ |
| 335 | void free(void *ptr) |
| 336 | { |
| 337 | struct memprof_stats *bin; |
| 338 | size_t size_before; |
| 339 | |
| 340 | if (likely(!(profiling & HA_PROF_MEMORY) || !ptr)) { |
| 341 | memprof_free_handler(ptr); |
| 342 | return; |
| 343 | } |
| 344 | |
Willy Tarreau | 1de51eb | 2021-10-22 16:33:53 +0200 | [diff] [blame] | 345 | size_before = malloc_usable_size(ptr) + sizeof(void *); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 346 | memprof_free_handler(ptr); |
| 347 | |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 348 | bin = memprof_get_bin(__builtin_return_address(0), MEMPROF_METH_FREE); |
Willy Tarreau | f93c7be | 2021-05-05 17:07:09 +0200 | [diff] [blame] | 349 | _HA_ATOMIC_ADD(&bin->free_calls, 1); |
| 350 | _HA_ATOMIC_ADD(&bin->free_tot, size_before); |
| 351 | } |
| 352 | |
Willy Tarreau | db87fc7 | 2021-05-05 16:50:40 +0200 | [diff] [blame] | 353 | #endif // USE_MEMORY_PROFILING |
| 354 | |
Willy Tarreau | 609aad9 | 2018-11-22 08:31:09 +0100 | [diff] [blame] | 355 | /* Updates the current thread's statistics about stolen CPU time. The unit for |
| 356 | * <stolen> is half-milliseconds. |
| 357 | */ |
| 358 | void report_stolen_time(uint64_t stolen) |
| 359 | { |
| 360 | activity[tid].cpust_total += stolen; |
| 361 | update_freq_ctr(&activity[tid].cpust_1s, stolen); |
| 362 | update_freq_ctr_period(&activity[tid].cpust_15s, 15000, stolen); |
| 363 | } |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 364 | |
Willy Tarreau | 20adfde | 2021-10-08 11:34:46 +0200 | [diff] [blame] | 365 | /* Update avg_loop value for the current thread and possibly decide to enable |
| 366 | * task-level profiling on the current thread based on its average run time. |
| 367 | * The <run_time> argument is the number of microseconds elapsed since the |
| 368 | * last time poll() returned. |
Willy Tarreau | e065022 | 2021-10-06 16:22:09 +0200 | [diff] [blame] | 369 | */ |
Willy Tarreau | 20adfde | 2021-10-08 11:34:46 +0200 | [diff] [blame] | 370 | void activity_count_runtime(uint32_t run_time) |
Willy Tarreau | e065022 | 2021-10-06 16:22:09 +0200 | [diff] [blame] | 371 | { |
Willy Tarreau | e065022 | 2021-10-06 16:22:09 +0200 | [diff] [blame] | 372 | uint32_t up, down; |
| 373 | |
| 374 | /* 1 millisecond per loop on average over last 1024 iterations is |
| 375 | * enough to turn on profiling. |
| 376 | */ |
| 377 | up = 1000; |
| 378 | down = up * 99 / 100; |
| 379 | |
Willy Tarreau | e065022 | 2021-10-06 16:22:09 +0200 | [diff] [blame] | 380 | run_time = swrate_add(&activity[tid].avg_loop_us, TIME_STATS_SAMPLES, run_time); |
| 381 | |
| 382 | /* In automatic mode, reaching the "up" threshold on average switches |
| 383 | * profiling to "on" when automatic, and going back below the "down" |
| 384 | * threshold switches to off. The forced modes don't check the load. |
| 385 | */ |
Willy Tarreau | 680ed5f | 2022-06-13 15:59:39 +0200 | [diff] [blame] | 386 | if (!(th_ctx->flags & TH_FL_TASK_PROFILING)) { |
Willy Tarreau | e065022 | 2021-10-06 16:22:09 +0200 | [diff] [blame] | 387 | if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_ON || |
| 388 | ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AON && |
| 389 | swrate_avg(run_time, TIME_STATS_SAMPLES) >= up))) |
Willy Tarreau | 680ed5f | 2022-06-13 15:59:39 +0200 | [diff] [blame] | 390 | _HA_ATOMIC_OR(&th_ctx->flags, TH_FL_TASK_PROFILING); |
Willy Tarreau | e065022 | 2021-10-06 16:22:09 +0200 | [diff] [blame] | 391 | } else { |
| 392 | if (unlikely((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_OFF || |
| 393 | ((profiling & HA_PROF_TASKS_MASK) == HA_PROF_TASKS_AOFF && |
| 394 | swrate_avg(run_time, TIME_STATS_SAMPLES) <= down))) |
Willy Tarreau | 680ed5f | 2022-06-13 15:59:39 +0200 | [diff] [blame] | 395 | _HA_ATOMIC_AND(&th_ctx->flags, ~TH_FL_TASK_PROFILING); |
Willy Tarreau | e065022 | 2021-10-06 16:22:09 +0200 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Willy Tarreau | ca3afc2 | 2021-05-05 18:33:19 +0200 | [diff] [blame] | 399 | #ifdef USE_MEMORY_PROFILING |
| 400 | /* config parser for global "profiling.memory", accepts "on" or "off" */ |
| 401 | static int cfg_parse_prof_memory(char **args, int section_type, struct proxy *curpx, |
| 402 | const struct proxy *defpx, const char *file, int line, |
| 403 | char **err) |
| 404 | { |
| 405 | if (too_many_args(1, args, err, NULL)) |
| 406 | return -1; |
| 407 | |
| 408 | if (strcmp(args[1], "on") == 0) |
| 409 | profiling |= HA_PROF_MEMORY; |
| 410 | else if (strcmp(args[1], "off") == 0) |
| 411 | profiling &= ~HA_PROF_MEMORY; |
| 412 | else { |
| 413 | memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]); |
| 414 | return -1; |
| 415 | } |
| 416 | return 0; |
| 417 | } |
| 418 | #endif // USE_MEMORY_PROFILING |
| 419 | |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 420 | /* config parser for global "profiling.tasks", accepts "on" or "off" */ |
| 421 | static int cfg_parse_prof_tasks(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 422 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 423 | char **err) |
| 424 | { |
| 425 | if (too_many_args(1, args, err, NULL)) |
| 426 | return -1; |
| 427 | |
| 428 | if (strcmp(args[1], "on") == 0) |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 429 | profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON; |
| 430 | else if (strcmp(args[1], "auto") == 0) |
Willy Tarreau | aa622b8 | 2021-01-28 21:44:22 +0100 | [diff] [blame] | 431 | profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF; |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 432 | else if (strcmp(args[1], "off") == 0) |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 433 | profiling = (profiling & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF; |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 434 | else { |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 435 | memprintf(err, "'%s' expects either 'on', 'auto', or 'off' but got '%s'.", args[0], args[1]); |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 436 | return -1; |
| 437 | } |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /* parse a "set profiling" command. It always returns 1. */ |
| 442 | static int cli_parse_set_profiling(char **args, char *payload, struct appctx *appctx, void *private) |
| 443 | { |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 444 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 445 | return 1; |
| 446 | |
Willy Tarreau | 00dd44f | 2021-05-05 16:44:23 +0200 | [diff] [blame] | 447 | if (strcmp(args[2], "memory") == 0) { |
Willy Tarreau | db87fc7 | 2021-05-05 16:50:40 +0200 | [diff] [blame] | 448 | #ifdef USE_MEMORY_PROFILING |
| 449 | if (strcmp(args[3], "on") == 0) { |
| 450 | unsigned int old = profiling; |
| 451 | int i; |
| 452 | |
| 453 | while (!_HA_ATOMIC_CAS(&profiling, &old, old | HA_PROF_MEMORY)) |
| 454 | ; |
| 455 | |
| 456 | /* also flush current profiling stats */ |
| 457 | for (i = 0; i < sizeof(memprof_stats) / sizeof(memprof_stats[0]); i++) { |
| 458 | HA_ATOMIC_STORE(&memprof_stats[i].alloc_calls, 0); |
| 459 | HA_ATOMIC_STORE(&memprof_stats[i].free_calls, 0); |
| 460 | HA_ATOMIC_STORE(&memprof_stats[i].alloc_tot, 0); |
| 461 | HA_ATOMIC_STORE(&memprof_stats[i].free_tot, 0); |
| 462 | HA_ATOMIC_STORE(&memprof_stats[i].caller, NULL); |
| 463 | } |
| 464 | } |
| 465 | else if (strcmp(args[3], "off") == 0) { |
| 466 | unsigned int old = profiling; |
| 467 | |
| 468 | while (!_HA_ATOMIC_CAS(&profiling, &old, old & ~HA_PROF_MEMORY)) |
| 469 | ; |
| 470 | } |
| 471 | else |
| 472 | return cli_err(appctx, "Expects either 'on' or 'off'.\n"); |
| 473 | return 1; |
| 474 | #else |
Willy Tarreau | 00dd44f | 2021-05-05 16:44:23 +0200 | [diff] [blame] | 475 | return cli_err(appctx, "Memory profiling not compiled in.\n"); |
Willy Tarreau | db87fc7 | 2021-05-05 16:50:40 +0200 | [diff] [blame] | 476 | #endif |
Willy Tarreau | 00dd44f | 2021-05-05 16:44:23 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 479 | if (strcmp(args[2], "tasks") != 0) |
Ilya Shipitsin | 3df5989 | 2021-05-10 12:50:00 +0500 | [diff] [blame] | 480 | return cli_err(appctx, "Expects either 'tasks' or 'memory'.\n"); |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 481 | |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 482 | if (strcmp(args[3], "on") == 0) { |
| 483 | unsigned int old = profiling; |
Willy Tarreau | cfa7101 | 2021-01-29 11:56:21 +0100 | [diff] [blame] | 484 | int i; |
| 485 | |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 486 | while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_ON)) |
| 487 | ; |
Willy Tarreau | cfa7101 | 2021-01-29 11:56:21 +0100 | [diff] [blame] | 488 | /* also flush current profiling stats */ |
| 489 | for (i = 0; i < 256; i++) { |
| 490 | HA_ATOMIC_STORE(&sched_activity[i].calls, 0); |
| 491 | HA_ATOMIC_STORE(&sched_activity[i].cpu_time, 0); |
| 492 | HA_ATOMIC_STORE(&sched_activity[i].lat_time, 0); |
| 493 | HA_ATOMIC_STORE(&sched_activity[i].func, NULL); |
| 494 | } |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 495 | } |
| 496 | else if (strcmp(args[3], "auto") == 0) { |
| 497 | unsigned int old = profiling; |
Willy Tarreau | aa622b8 | 2021-01-28 21:44:22 +0100 | [diff] [blame] | 498 | unsigned int new; |
| 499 | |
| 500 | do { |
| 501 | if ((old & HA_PROF_TASKS_MASK) >= HA_PROF_TASKS_AON) |
| 502 | new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AON; |
| 503 | else |
| 504 | new = (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_AOFF; |
| 505 | } while (!_HA_ATOMIC_CAS(&profiling, &old, new)); |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 506 | } |
| 507 | else if (strcmp(args[3], "off") == 0) { |
| 508 | unsigned int old = profiling; |
| 509 | while (!_HA_ATOMIC_CAS(&profiling, &old, (old & ~HA_PROF_TASKS_MASK) | HA_PROF_TASKS_OFF)) |
| 510 | ; |
| 511 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 512 | else |
| 513 | return cli_err(appctx, "Expects 'on', 'auto', or 'off'.\n"); |
| 514 | |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 515 | return 1; |
| 516 | } |
| 517 | |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 518 | static int cmp_sched_activity_calls(const void *a, const void *b) |
Willy Tarreau | 1bd67e9 | 2021-01-29 00:07:40 +0100 | [diff] [blame] | 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->calls > r->calls) |
| 524 | return -1; |
| 525 | else if (l->calls < r->calls) |
| 526 | return 1; |
| 527 | else |
| 528 | return 0; |
| 529 | } |
| 530 | |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 531 | static int cmp_sched_activity_addr(const void *a, const void *b) |
| 532 | { |
| 533 | const struct sched_activity *l = (const struct sched_activity *)a; |
| 534 | const struct sched_activity *r = (const struct sched_activity *)b; |
| 535 | |
| 536 | if (l->func > r->func) |
| 537 | return -1; |
| 538 | else if (l->func < r->func) |
| 539 | return 1; |
| 540 | else |
| 541 | return 0; |
| 542 | } |
| 543 | |
Willy Tarreau | e15615c | 2021-08-28 12:04:25 +0200 | [diff] [blame] | 544 | #ifdef USE_MEMORY_PROFILING |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 545 | /* used by qsort below */ |
| 546 | static int cmp_memprof_stats(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->alloc_tot + l->free_tot > r->alloc_tot + r->free_tot) |
| 552 | return -1; |
| 553 | else if (l->alloc_tot + l->free_tot < r->alloc_tot + r->free_tot) |
| 554 | return 1; |
| 555 | else |
| 556 | return 0; |
| 557 | } |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 558 | |
| 559 | static int cmp_memprof_addr(const void *a, const void *b) |
| 560 | { |
| 561 | const struct memprof_stats *l = (const struct memprof_stats *)a; |
| 562 | const struct memprof_stats *r = (const struct memprof_stats *)b; |
| 563 | |
| 564 | if (l->caller > r->caller) |
| 565 | return -1; |
| 566 | else if (l->caller < r->caller) |
| 567 | return 1; |
| 568 | else |
| 569 | return 0; |
| 570 | } |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 571 | #endif // USE_MEMORY_PROFILING |
| 572 | |
Willy Tarreau | a26be37 | 2021-10-06 16:26:33 +0200 | [diff] [blame] | 573 | /* Computes the index of function pointer <func> for use with sched_activity[] |
| 574 | * or any other similar array passed in <array>, and returns a pointer to the |
| 575 | * entry after having atomically assigned it to this function pointer. Note |
| 576 | * that in case of collision, the first entry is returned instead ("other"). |
| 577 | */ |
| 578 | struct sched_activity *sched_activity_entry(struct sched_activity *array, const void *func) |
| 579 | { |
| 580 | uint64_t hash = XXH64_avalanche(XXH64_mergeRound((size_t)func, (size_t)func)); |
| 581 | struct sched_activity *ret; |
| 582 | const void *old = NULL; |
| 583 | |
| 584 | hash ^= (hash >> 32); |
| 585 | hash ^= (hash >> 16); |
| 586 | hash ^= (hash >> 8); |
| 587 | hash &= 0xff; |
| 588 | ret = &array[hash]; |
| 589 | |
| 590 | if (likely(ret->func == func)) |
| 591 | return ret; |
| 592 | |
| 593 | if (HA_ATOMIC_CAS(&ret->func, &old, func)) |
| 594 | return ret; |
| 595 | |
| 596 | return array; |
| 597 | } |
| 598 | |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 599 | /* This function dumps all profiling settings. It returns 0 if the output |
| 600 | * buffer is full and it needs to be called again, otherwise non-zero. |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 601 | * It dumps some parts depending on the following states from show_prof_ctx: |
| 602 | * dump_step: |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 603 | * 0, 4: dump status, then jump to 1 if 0 |
| 604 | * 1, 5: dump tasks, then jump to 2 if 1 |
| 605 | * 2, 6: dump memory, then stop |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 606 | * linenum: |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 607 | * restart line for each step (starts at zero) |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 608 | * maxcnt: |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 609 | * may contain a configured max line count for each step (0=not set) |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 610 | * byaddr: |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 611 | * 0: sort by usage |
| 612 | * 1: sort by address |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 613 | */ |
| 614 | static int cli_io_handler_show_profiling(struct appctx *appctx) |
| 615 | { |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 616 | struct show_prof_ctx *ctx = appctx->svcctx; |
Willy Tarreau | 1bd67e9 | 2021-01-29 00:07:40 +0100 | [diff] [blame] | 617 | struct sched_activity tmp_activity[256] __attribute__((aligned(64))); |
Willy Tarreau | e15615c | 2021-08-28 12:04:25 +0200 | [diff] [blame] | 618 | #ifdef USE_MEMORY_PROFILING |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 619 | struct memprof_stats tmp_memstats[MEMPROF_HASH_BUCKETS + 1]; |
Willy Tarreau | f5fb858 | 2021-05-11 14:06:24 +0200 | [diff] [blame] | 620 | unsigned long long tot_alloc_calls, tot_free_calls; |
| 621 | unsigned long long tot_alloc_bytes, tot_free_bytes; |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 622 | #endif |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 623 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 1bd67e9 | 2021-01-29 00:07:40 +0100 | [diff] [blame] | 624 | struct buffer *name_buffer = get_trash_chunk(); |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 625 | const char *str; |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 626 | int max_lines; |
Willy Tarreau | 1bd67e9 | 2021-01-29 00:07:40 +0100 | [diff] [blame] | 627 | int i, max; |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 628 | |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 629 | if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 630 | return 1; |
| 631 | |
| 632 | chunk_reset(&trash); |
| 633 | |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 634 | switch (profiling & HA_PROF_TASKS_MASK) { |
Willy Tarreau | aa622b8 | 2021-01-28 21:44:22 +0100 | [diff] [blame] | 635 | case HA_PROF_TASKS_AOFF: str="auto-off"; break; |
| 636 | case HA_PROF_TASKS_AON: str="auto-on"; break; |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 637 | case HA_PROF_TASKS_ON: str="on"; break; |
| 638 | default: str="off"; break; |
| 639 | } |
| 640 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 641 | if ((ctx->dump_step & 3) != 0) |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 642 | goto skip_status; |
Willy Tarreau | 1bd67e9 | 2021-01-29 00:07:40 +0100 | [diff] [blame] | 643 | |
Willy Tarreau | d2d3348 | 2019-04-25 17:09:07 +0200 | [diff] [blame] | 644 | chunk_printf(&trash, |
Willy Tarreau | 00dd44f | 2021-05-05 16:44:23 +0200 | [diff] [blame] | 645 | "Per-task CPU profiling : %-8s # set profiling tasks {on|auto|off}\n" |
| 646 | "Memory usage profiling : %-8s # set profiling memory {on|off}\n", |
| 647 | str, (profiling & HA_PROF_MEMORY) ? "on" : "off"); |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 648 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 649 | if (applet_putchk(appctx, &trash) == -1) { |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 650 | /* failed, try again */ |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 654 | ctx->linenum = 0; // reset first line to dump |
| 655 | if ((ctx->dump_step & 4) == 0) |
| 656 | ctx->dump_step++; // next step |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 657 | |
| 658 | skip_status: |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 659 | if ((ctx->dump_step & 3) != 1) |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 660 | goto skip_tasks; |
Willy Tarreau | 1bd67e9 | 2021-01-29 00:07:40 +0100 | [diff] [blame] | 661 | |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 662 | memcpy(tmp_activity, sched_activity, sizeof(tmp_activity)); |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 663 | if (ctx->by_addr) |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 664 | qsort(tmp_activity, 256, sizeof(tmp_activity[0]), cmp_sched_activity_addr); |
| 665 | else |
| 666 | qsort(tmp_activity, 256, sizeof(tmp_activity[0]), cmp_sched_activity_calls); |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 667 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 668 | if (!ctx->linenum) |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 669 | chunk_appendf(&trash, "Tasks activity:\n" |
| 670 | " function calls cpu_tot cpu_avg lat_tot lat_avg\n"); |
| 671 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 672 | max_lines = ctx->maxcnt; |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 673 | if (!max_lines) |
| 674 | max_lines = 256; |
| 675 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 676 | for (i = ctx->linenum; i < max_lines && tmp_activity[i].calls; i++) { |
| 677 | ctx->linenum = i; |
Willy Tarreau | 1bd67e9 | 2021-01-29 00:07:40 +0100 | [diff] [blame] | 678 | chunk_reset(name_buffer); |
| 679 | |
| 680 | if (!tmp_activity[i].func) |
| 681 | chunk_printf(name_buffer, "other"); |
| 682 | else |
| 683 | resolve_sym_name(name_buffer, "", tmp_activity[i].func); |
| 684 | |
| 685 | /* reserve 35 chars for name+' '+#calls, knowing that longer names |
| 686 | * are often used for less often called functions. |
| 687 | */ |
| 688 | max = 35 - name_buffer->data; |
| 689 | if (max < 1) |
| 690 | max = 1; |
| 691 | chunk_appendf(&trash, " %s%*llu", name_buffer->area, max, (unsigned long long)tmp_activity[i].calls); |
| 692 | |
| 693 | print_time_short(&trash, " ", tmp_activity[i].cpu_time, ""); |
| 694 | print_time_short(&trash, " ", tmp_activity[i].cpu_time / tmp_activity[i].calls, ""); |
| 695 | print_time_short(&trash, " ", tmp_activity[i].lat_time, ""); |
| 696 | print_time_short(&trash, " ", tmp_activity[i].lat_time / tmp_activity[i].calls, "\n"); |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 697 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 698 | if (applet_putchk(appctx, &trash) == -1) { |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 699 | /* failed, try again */ |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 700 | return 0; |
| 701 | } |
Willy Tarreau | 1bd67e9 | 2021-01-29 00:07:40 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 704 | if (applet_putchk(appctx, &trash) == -1) { |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 705 | /* failed, try again */ |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 706 | return 0; |
| 707 | } |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 708 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 709 | ctx->linenum = 0; // reset first line to dump |
| 710 | if ((ctx->dump_step & 4) == 0) |
| 711 | ctx->dump_step++; // next step |
Willy Tarreau | 637d85a | 2021-05-05 17:33:27 +0200 | [diff] [blame] | 712 | |
| 713 | skip_tasks: |
| 714 | |
Willy Tarreau | e15615c | 2021-08-28 12:04:25 +0200 | [diff] [blame] | 715 | #ifdef USE_MEMORY_PROFILING |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 716 | if ((ctx->dump_step & 3) != 2) |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 717 | goto skip_mem; |
| 718 | |
| 719 | memcpy(tmp_memstats, memprof_stats, sizeof(tmp_memstats)); |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 720 | if (ctx->by_addr) |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 721 | qsort(tmp_memstats, MEMPROF_HASH_BUCKETS+1, sizeof(tmp_memstats[0]), cmp_memprof_addr); |
| 722 | else |
| 723 | qsort(tmp_memstats, MEMPROF_HASH_BUCKETS+1, sizeof(tmp_memstats[0]), cmp_memprof_stats); |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 724 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 725 | if (!ctx->linenum) |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 726 | chunk_appendf(&trash, |
| 727 | "Alloc/Free statistics by call place:\n" |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 728 | " Calls | Tot Bytes | Caller and method\n" |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 729 | "<- alloc -> <- free ->|<-- alloc ---> <-- free ---->|\n"); |
| 730 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 731 | max_lines = ctx->maxcnt; |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 732 | if (!max_lines) |
| 733 | max_lines = MEMPROF_HASH_BUCKETS + 1; |
| 734 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 735 | for (i = ctx->linenum; i < max_lines; i++) { |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 736 | struct memprof_stats *entry = &tmp_memstats[i]; |
| 737 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 738 | ctx->linenum = i; |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 739 | if (!entry->alloc_calls && !entry->free_calls) |
| 740 | continue; |
| 741 | chunk_appendf(&trash, "%11llu %11llu %14llu %14llu| %16p ", |
| 742 | entry->alloc_calls, entry->free_calls, |
| 743 | entry->alloc_tot, entry->free_tot, |
| 744 | entry->caller); |
| 745 | |
| 746 | if (entry->caller) |
| 747 | resolve_sym_name(&trash, NULL, entry->caller); |
| 748 | else |
| 749 | chunk_appendf(&trash, "[other]"); |
| 750 | |
Willy Tarreau | 8cce4d7 | 2021-10-22 16:26:12 +0200 | [diff] [blame] | 751 | chunk_appendf(&trash," %s(%lld)", memprof_methods[entry->method], |
Willy Tarreau | 616491b | 2021-05-11 09:26:23 +0200 | [diff] [blame] | 752 | (long long)(entry->alloc_tot - entry->free_tot) / (long long)(entry->alloc_calls + entry->free_calls)); |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 753 | |
Willy Tarreau | 8cce4d7 | 2021-10-22 16:26:12 +0200 | [diff] [blame] | 754 | if (entry->alloc_tot && entry->free_tot) { |
| 755 | /* that's a realloc, show the total diff to help spot leaks */ |
| 756 | chunk_appendf(&trash," [delta=%lld]", (long long)(entry->alloc_tot - entry->free_tot)); |
| 757 | } |
| 758 | |
| 759 | chunk_appendf(&trash, "\n"); |
| 760 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 761 | if (applet_putchk(appctx, &trash) == -1) |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 762 | return 0; |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 763 | } |
| 764 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 765 | if (applet_putchk(appctx, &trash) == -1) |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 766 | return 0; |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 767 | |
Willy Tarreau | f5fb858 | 2021-05-11 14:06:24 +0200 | [diff] [blame] | 768 | tot_alloc_calls = tot_free_calls = tot_alloc_bytes = tot_free_bytes = 0; |
| 769 | for (i = 0; i < max_lines; i++) { |
| 770 | tot_alloc_calls += tmp_memstats[i].alloc_calls; |
| 771 | tot_free_calls += tmp_memstats[i].free_calls; |
| 772 | tot_alloc_bytes += tmp_memstats[i].alloc_tot; |
| 773 | tot_free_bytes += tmp_memstats[i].free_tot; |
| 774 | } |
| 775 | |
| 776 | chunk_appendf(&trash, |
| 777 | "-----------------------|-----------------------------|\n" |
| 778 | "%11llu %11llu %14llu %14llu| <- Total; Delta_calls=%lld; Delta_bytes=%lld\n", |
| 779 | tot_alloc_calls, tot_free_calls, |
| 780 | tot_alloc_bytes, tot_free_bytes, |
| 781 | tot_alloc_calls - tot_free_calls, |
| 782 | tot_alloc_bytes - tot_free_bytes); |
| 783 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 784 | if (applet_putchk(appctx, &trash) == -1) |
Willy Tarreau | f5fb858 | 2021-05-11 14:06:24 +0200 | [diff] [blame] | 785 | return 0; |
Willy Tarreau | f5fb858 | 2021-05-11 14:06:24 +0200 | [diff] [blame] | 786 | |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 787 | ctx->linenum = 0; // reset first line to dump |
| 788 | if ((ctx->dump_step & 4) == 0) |
| 789 | ctx->dump_step++; // next step |
Willy Tarreau | 993d44d | 2021-05-05 18:07:02 +0200 | [diff] [blame] | 790 | |
| 791 | skip_mem: |
| 792 | #endif // USE_MEMORY_PROFILING |
| 793 | |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 794 | return 1; |
| 795 | } |
| 796 | |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 797 | /* parse a "show profiling" command. It returns 1 on failure, 0 if it starts to dump. |
| 798 | * - cli.i0 is set to the first state (0=all, 4=status, 5=tasks, 6=memory) |
| 799 | * - cli.o1 is set to 1 if the output must be sorted by addr instead of usage |
| 800 | * - cli.o0 is set to the number of lines of output |
| 801 | */ |
Willy Tarreau | 42712cb | 2021-05-05 17:48:13 +0200 | [diff] [blame] | 802 | static int cli_parse_show_profiling(char **args, char *payload, struct appctx *appctx, void *private) |
| 803 | { |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 804 | struct show_prof_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 805 | int arg; |
| 806 | |
Willy Tarreau | 42712cb | 2021-05-05 17:48:13 +0200 | [diff] [blame] | 807 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 808 | return 1; |
| 809 | |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 810 | for (arg = 2; *args[arg]; arg++) { |
| 811 | if (strcmp(args[arg], "all") == 0) { |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 812 | ctx->dump_step = 0; // will cycle through 0,1,2; default |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 813 | } |
| 814 | else if (strcmp(args[arg], "status") == 0) { |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 815 | ctx->dump_step = 4; // will visit status only |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 816 | } |
| 817 | else if (strcmp(args[arg], "tasks") == 0) { |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 818 | ctx->dump_step = 5; // will visit tasks only |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 819 | } |
| 820 | else if (strcmp(args[arg], "memory") == 0) { |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 821 | ctx->dump_step = 6; // will visit memory only |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 822 | } |
| 823 | else if (strcmp(args[arg], "byaddr") == 0) { |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 824 | ctx->by_addr = 1; // sort output by address instead of usage |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 825 | } |
| 826 | else if (isdigit((unsigned char)*args[arg])) { |
Willy Tarreau | e8d006a | 2022-05-05 14:19:00 +0200 | [diff] [blame] | 827 | ctx->maxcnt = atoi(args[arg]); // number of entries to dump |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 828 | } |
| 829 | else |
| 830 | return cli_err(appctx, "Expects either 'all', 'status', 'tasks', 'memory', 'byaddr' or a max number of output lines.\n"); |
Willy Tarreau | 42712cb | 2021-05-05 17:48:13 +0200 | [diff] [blame] | 831 | } |
| 832 | return 0; |
| 833 | } |
| 834 | |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 835 | /* 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 | */ |
| 839 | static int cli_io_handler_show_tasks(struct appctx *appctx) |
| 840 | { |
| 841 | struct sched_activity tmp_activity[256] __attribute__((aligned(64))); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 842 | struct stconn *sc = appctx_sc(appctx); |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 843 | 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 | |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 853 | if (unlikely(sc_ic(sc)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 854 | 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 Tarreau | 75f7233 | 2021-01-29 15:04:16 +0100 | [diff] [blame] | 862 | * 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 Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 866 | */ |
| 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 |
Willy Tarreau | 6f78038 | 2022-06-16 15:30:50 +0200 | [diff] [blame^] | 876 | for (thr = 0; thr < global.nbthread; thr++) { |
| 877 | /* task run queue */ |
| 878 | rqnode = eb32sc_first(&ha_thread_ctx[thr].rqueue_shared, ~0UL); |
| 879 | while (rqnode) { |
| 880 | t = eb32sc_entry(rqnode, struct task, rq); |
| 881 | entry = sched_activity_entry(tmp_activity, t->process); |
| 882 | if (t->call_date) { |
| 883 | lat = now_ns - t->call_date; |
| 884 | if ((int64_t)lat > 0) |
| 885 | entry->lat_time += lat; |
| 886 | } |
| 887 | entry->calls++; |
| 888 | rqnode = eb32sc_next(rqnode, ~0UL); |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 889 | } |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 890 | } |
| 891 | #endif |
| 892 | /* 2. all threads's local run queues */ |
| 893 | for (thr = 0; thr < global.nbthread; thr++) { |
| 894 | /* task run queue */ |
Willy Tarreau | 1a9c922 | 2021-10-01 11:30:33 +0200 | [diff] [blame] | 895 | rqnode = eb32sc_first(&ha_thread_ctx[thr].rqueue, ~0UL); |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 896 | while (rqnode) { |
| 897 | t = eb32sc_entry(rqnode, struct task, rq); |
| 898 | entry = sched_activity_entry(tmp_activity, t->process); |
| 899 | if (t->call_date) { |
| 900 | lat = now_ns - t->call_date; |
Willy Tarreau | 75f7233 | 2021-01-29 15:04:16 +0100 | [diff] [blame] | 901 | if ((int64_t)lat > 0) |
| 902 | entry->lat_time += lat; |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 903 | } |
| 904 | entry->calls++; |
| 905 | rqnode = eb32sc_next(rqnode, ~0UL); |
| 906 | } |
| 907 | |
| 908 | /* shared tasklet list */ |
Willy Tarreau | 1a9c922 | 2021-10-01 11:30:33 +0200 | [diff] [blame] | 909 | list_for_each_entry(tl, mt_list_to_list(&ha_thread_ctx[thr].shared_tasklet_list), list) { |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 910 | t = (const struct task *)tl; |
| 911 | entry = sched_activity_entry(tmp_activity, t->process); |
| 912 | if (!TASK_IS_TASKLET(t) && t->call_date) { |
| 913 | lat = now_ns - t->call_date; |
Willy Tarreau | 75f7233 | 2021-01-29 15:04:16 +0100 | [diff] [blame] | 914 | if ((int64_t)lat > 0) |
| 915 | entry->lat_time += lat; |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 916 | } |
| 917 | entry->calls++; |
| 918 | } |
| 919 | |
| 920 | /* classful tasklets */ |
| 921 | for (queue = 0; queue < TL_CLASSES; queue++) { |
Willy Tarreau | 1a9c922 | 2021-10-01 11:30:33 +0200 | [diff] [blame] | 922 | list_for_each_entry(tl, &ha_thread_ctx[thr].tasklets[queue], list) { |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 923 | t = (const struct task *)tl; |
| 924 | entry = sched_activity_entry(tmp_activity, t->process); |
| 925 | if (!TASK_IS_TASKLET(t) && t->call_date) { |
| 926 | lat = now_ns - t->call_date; |
Willy Tarreau | 75f7233 | 2021-01-29 15:04:16 +0100 | [diff] [blame] | 927 | if ((int64_t)lat > 0) |
| 928 | entry->lat_time += lat; |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 929 | } |
| 930 | entry->calls++; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | /* hopefully we're done */ |
| 936 | thread_release(); |
| 937 | |
| 938 | chunk_reset(&trash); |
| 939 | |
| 940 | tot_calls = 0; |
| 941 | for (i = 0; i < 256; i++) |
| 942 | tot_calls += tmp_activity[i].calls; |
| 943 | |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 944 | qsort(tmp_activity, 256, sizeof(tmp_activity[0]), cmp_sched_activity_calls); |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 945 | |
| 946 | chunk_appendf(&trash, "Running tasks: %d (%d threads)\n" |
| 947 | " function places %% lat_tot lat_avg\n", |
| 948 | (int)tot_calls, global.nbthread); |
| 949 | |
| 950 | for (i = 0; i < 256 && tmp_activity[i].calls; i++) { |
| 951 | chunk_reset(name_buffer); |
| 952 | |
| 953 | if (!tmp_activity[i].func) |
| 954 | chunk_printf(name_buffer, "other"); |
| 955 | else |
| 956 | resolve_sym_name(name_buffer, "", tmp_activity[i].func); |
| 957 | |
| 958 | /* reserve 35 chars for name+' '+#calls, knowing that longer names |
| 959 | * are often used for less often called functions. |
| 960 | */ |
| 961 | max = 35 - name_buffer->data; |
| 962 | if (max < 1) |
| 963 | max = 1; |
| 964 | chunk_appendf(&trash, " %s%*llu %3d.%1d", |
| 965 | name_buffer->area, max, (unsigned long long)tmp_activity[i].calls, |
| 966 | (int)(100ULL * tmp_activity[i].calls / tot_calls), |
| 967 | (int)((1000ULL * tmp_activity[i].calls / tot_calls)%10)); |
| 968 | print_time_short(&trash, " ", tmp_activity[i].lat_time, ""); |
| 969 | print_time_short(&trash, " ", tmp_activity[i].lat_time / tmp_activity[i].calls, "\n"); |
| 970 | } |
| 971 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 972 | if (applet_putchk(appctx, &trash) == -1) { |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 973 | /* failed, try again */ |
Willy Tarreau | 7eff06e | 2021-01-29 11:32:55 +0100 | [diff] [blame] | 974 | return 0; |
| 975 | } |
| 976 | return 1; |
| 977 | } |
| 978 | |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 979 | /* config keyword parsers */ |
| 980 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | ca3afc2 | 2021-05-05 18:33:19 +0200 | [diff] [blame] | 981 | #ifdef USE_MEMORY_PROFILING |
| 982 | { CFG_GLOBAL, "profiling.memory", cfg_parse_prof_memory }, |
| 983 | #endif |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 984 | { CFG_GLOBAL, "profiling.tasks", cfg_parse_prof_tasks }, |
| 985 | { 0, NULL, NULL } |
| 986 | }}; |
| 987 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 988 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 989 | |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 990 | /* register cli keywords */ |
| 991 | static struct cli_kw_list cli_kws = {{ },{ |
Daniel Corbett | 67b3cef | 2021-05-10 14:08:40 -0400 | [diff] [blame] | 992 | { { "set", "profiling", NULL }, "set profiling <what> {auto|on|off} : enable/disable resource profiling (tasks,memory)", cli_parse_set_profiling, NULL }, |
Willy Tarreau | f1c8a38 | 2021-05-13 10:00:17 +0200 | [diff] [blame] | 993 | { { "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 Tarreau | b205bfd | 2021-05-07 11:38:37 +0200 | [diff] [blame] | 994 | { { "show", "tasks", NULL }, "show tasks : show running tasks", NULL, cli_io_handler_show_tasks, NULL }, |
Willy Tarreau | 75c62c2 | 2018-11-22 11:02:09 +0100 | [diff] [blame] | 995 | {{},} |
| 996 | }}; |
| 997 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 998 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |