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