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