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