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