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