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