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