Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * functions about threads. |
| 3 | * |
| 4 | * Copyright (C) 2017 Christopher Fauet - cfaulet@haproxy.com |
| 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 | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 13 | #define _GNU_SOURCE |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 14 | #include <unistd.h> |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 15 | #include <stdlib.h> |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 16 | |
Willy Tarreau | aa99276 | 2021-10-06 23:33:20 +0200 | [diff] [blame] | 17 | #include <signal.h> |
| 18 | #include <unistd.h> |
| 19 | #ifdef _POSIX_PRIORITY_SCHEDULING |
| 20 | #include <sched.h> |
| 21 | #endif |
| 22 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 23 | #ifdef USE_THREAD |
| 24 | # include <pthread.h> |
| 25 | #endif |
| 26 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 27 | #ifdef USE_CPU_AFFINITY |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 28 | # include <sched.h> |
| 29 | # if defined(__FreeBSD__) || defined(__DragonFly__) |
| 30 | # include <sys/param.h> |
| 31 | # ifdef __FreeBSD__ |
| 32 | # include <sys/cpuset.h> |
| 33 | # endif |
| 34 | # include <pthread_np.h> |
| 35 | # endif |
| 36 | # ifdef __APPLE__ |
| 37 | # include <mach/mach_types.h> |
| 38 | # include <mach/thread_act.h> |
| 39 | # include <mach/thread_policy.h> |
| 40 | # endif |
| 41 | # include <haproxy/cpuset.h> |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 42 | #endif |
| 43 | |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 44 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 45 | #include <haproxy/clock.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 46 | #include <haproxy/fd.h> |
| 47 | #include <haproxy/global.h> |
Willy Tarreau | 11bd6f7 | 2021-05-08 20:33:02 +0200 | [diff] [blame] | 48 | #include <haproxy/log.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 49 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 50 | #include <haproxy/tools.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 51 | |
Willy Tarreau | f966284 | 2021-09-13 18:11:26 +0200 | [diff] [blame] | 52 | struct tgroup_info ha_tgroup_info[MAX_TGROUPS] = { }; |
| 53 | THREAD_LOCAL const struct tgroup_info *tg = &ha_tgroup_info[0]; |
| 54 | |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 55 | struct thread_info ha_thread_info[MAX_THREADS] = { }; |
Willy Tarreau | 6036342 | 2021-10-01 16:29:27 +0200 | [diff] [blame] | 56 | THREAD_LOCAL const struct thread_info *ti = &ha_thread_info[0]; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 57 | |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 58 | struct tgroup_ctx ha_tgroup_ctx[MAX_TGROUPS] = { }; |
| 59 | THREAD_LOCAL struct tgroup_ctx *tg_ctx = &ha_tgroup_ctx[0]; |
| 60 | |
Willy Tarreau | 1a9c922 | 2021-10-01 11:30:33 +0200 | [diff] [blame] | 61 | struct thread_ctx ha_thread_ctx[MAX_THREADS] = { }; |
| 62 | THREAD_LOCAL struct thread_ctx *th_ctx = &ha_thread_ctx[0]; |
| 63 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 64 | #ifdef USE_THREAD |
| 65 | |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 66 | volatile unsigned long all_tgroups_mask __read_mostly = 1; // nbtgroup 1 assumed by default |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 67 | volatile unsigned int rdv_requests = 0; // total number of threads requesting RDV |
| 68 | volatile unsigned int isolated_thread = ~0; // ID of the isolated thread, or ~0 when none |
Willy Tarreau | b90935c | 2021-09-30 08:00:11 +0200 | [diff] [blame] | 69 | THREAD_LOCAL unsigned int tgid = 1; // thread ID starts at 1 |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 70 | THREAD_LOCAL unsigned int tid = 0; |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 71 | int thread_cpus_enabled_at_boot = 1; |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 72 | static pthread_t ha_pthread[MAX_THREADS] = { }; |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 73 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 74 | /* Marks the thread as harmless until the last thread using the rendez-vous |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 75 | * point quits. Given that we can wait for a long time, sched_yield() is |
Christopher Faulet | a9a9e9a | 2021-03-25 14:11:36 +0100 | [diff] [blame] | 76 | * used when available to offer the CPU resources to competing threads if |
| 77 | * needed. |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 78 | */ |
| 79 | void thread_harmless_till_end() |
| 80 | { |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 81 | _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit); |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 82 | while (_HA_ATOMIC_LOAD(&rdv_requests) != 0) { |
Willy Tarreau | 286363b | 2021-08-04 10:33:57 +0200 | [diff] [blame] | 83 | ha_thread_relax(); |
| 84 | } |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Isolates the current thread : request the ability to work while all other |
Willy Tarreau | f519cfa | 2021-08-04 11:22:07 +0200 | [diff] [blame] | 88 | * threads are harmless, as defined by thread_harmless_now() (i.e. they're not |
| 89 | * going to touch any visible memory area). Only returns once all of them are |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 90 | * harmless, with the current thread's bit in &tg_ctx->threads_harmless cleared. |
Willy Tarreau | f519cfa | 2021-08-04 11:22:07 +0200 | [diff] [blame] | 91 | * Needs to be completed using thread_release(). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 92 | */ |
| 93 | void thread_isolate() |
| 94 | { |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 95 | uint tgrp, thr; |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 96 | |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 97 | _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit); |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 98 | __ha_barrier_atomic_store(); |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 99 | _HA_ATOMIC_INC(&rdv_requests); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 100 | |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 101 | /* wait for all threads to become harmless. They cannot change their |
| 102 | * mind once seen thanks to rdv_requests above, unless they pass in |
| 103 | * front of us. |
| 104 | */ |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 105 | while (1) { |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 106 | for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) { |
Willy Tarreau | b2f38c1 | 2023-01-19 19:14:18 +0100 | [diff] [blame] | 107 | do { |
| 108 | ulong te = _HA_ATOMIC_LOAD(&ha_tgroup_info[tgrp].threads_enabled); |
| 109 | ulong th = _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless); |
| 110 | |
| 111 | if ((th & te) == te) |
| 112 | break; |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 113 | ha_thread_relax(); |
Willy Tarreau | b2f38c1 | 2023-01-19 19:14:18 +0100 | [diff] [blame] | 114 | } while (1); |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 115 | } |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 116 | |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 117 | /* Now we've seen all threads marked harmless, we can try to run |
| 118 | * by competing with other threads to win the race of the isolated |
| 119 | * thread. It eventually converges since winners will enventually |
| 120 | * relax their request and go back to wait for this to be over. |
| 121 | * Competing on this only after seeing all threads harmless limits |
| 122 | * the write contention. |
| 123 | */ |
| 124 | thr = _HA_ATOMIC_LOAD(&isolated_thread); |
| 125 | if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid)) |
| 126 | break; // we won! |
Willy Tarreau | 38171da | 2019-05-17 16:33:13 +0200 | [diff] [blame] | 127 | ha_thread_relax(); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 128 | } |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 129 | |
| 130 | /* the thread is no longer harmless as it runs */ |
| 131 | _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit); |
| 132 | |
| 133 | /* the thread is isolated until it calls thread_release() which will |
| 134 | * 1) reset isolated_thread to ~0; |
| 135 | * 2) decrement rdv_requests. |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 136 | */ |
| 137 | } |
| 138 | |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 139 | /* Isolates the current thread : request the ability to work while all other |
| 140 | * threads are idle, as defined by thread_idle_now(). It only returns once |
| 141 | * all of them are both harmless and idle, with the current thread's bit in |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 142 | * &tg_ctx->threads_harmless and idle_mask cleared. Needs to be completed using |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 143 | * thread_release(). By doing so the thread also engages in being safe against |
| 144 | * any actions that other threads might be about to start under the same |
| 145 | * conditions. This specifically targets destruction of any internal structure, |
| 146 | * which implies that the current thread may not hold references to any object. |
| 147 | * |
| 148 | * Note that a concurrent thread_isolate() will usually win against |
| 149 | * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to |
| 150 | * get back to the poller or any other fully idle location, that will |
| 151 | * ultimately release this one. |
| 152 | */ |
| 153 | void thread_isolate_full() |
| 154 | { |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 155 | uint tgrp, thr; |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 156 | |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 157 | _HA_ATOMIC_OR(&tg_ctx->threads_idle, ti->ltid_bit); |
| 158 | _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit); |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 159 | __ha_barrier_atomic_store(); |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 160 | _HA_ATOMIC_INC(&rdv_requests); |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 162 | /* wait for all threads to become harmless. They cannot change their |
| 163 | * mind once seen thanks to rdv_requests above, unless they pass in |
| 164 | * front of us. |
| 165 | */ |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 166 | while (1) { |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 167 | for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) { |
Willy Tarreau | b2f38c1 | 2023-01-19 19:14:18 +0100 | [diff] [blame] | 168 | do { |
| 169 | ulong te = _HA_ATOMIC_LOAD(&ha_tgroup_info[tgrp].threads_enabled); |
| 170 | ulong th = _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless); |
| 171 | ulong id = _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_idle); |
| 172 | |
| 173 | if ((th & id & te) == te) |
| 174 | break; |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 175 | ha_thread_relax(); |
Willy Tarreau | b2f38c1 | 2023-01-19 19:14:18 +0100 | [diff] [blame] | 176 | } while (1); |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 177 | } |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 178 | |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 179 | /* Now we've seen all threads marked harmless and idle, we can |
| 180 | * try to run by competing with other threads to win the race |
| 181 | * of the isolated thread. It eventually converges since winners |
| 182 | * will enventually relax their request and go back to wait for |
| 183 | * this to be over. Competing on this only after seeing all |
| 184 | * threads harmless+idle limits the write contention. |
| 185 | */ |
| 186 | thr = _HA_ATOMIC_LOAD(&isolated_thread); |
| 187 | if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid)) |
| 188 | break; // we won! |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 189 | ha_thread_relax(); |
| 190 | } |
| 191 | |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 192 | /* we're not idle nor harmless anymore at this point. Other threads |
| 193 | * waiting on this condition will need to wait until out next pass to |
| 194 | * the poller, or our next call to thread_isolate_full(). |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 195 | */ |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 196 | _HA_ATOMIC_AND(&tg_ctx->threads_idle, ~ti->ltid_bit); |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 197 | _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit); |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 200 | /* Cancels the effect of thread_isolate() by resetting the ID of the isolated |
| 201 | * thread and decrementing the number of RDV requesters. This immediately allows |
| 202 | * other threads to expect to be executed, though they will first have to wait |
| 203 | * for this thread to become harmless again (possibly by reaching the poller |
| 204 | * again). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 205 | */ |
| 206 | void thread_release() |
| 207 | { |
Willy Tarreau | 598cf3f | 2022-07-01 15:08:37 +0200 | [diff] [blame] | 208 | HA_ATOMIC_STORE(&isolated_thread, ~0U); |
| 209 | HA_ATOMIC_DEC(&rdv_requests); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 210 | } |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 211 | |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 212 | /* Sets up threads, signals and masks, and starts threads 2 and above. |
| 213 | * Does nothing when threads are disabled. |
| 214 | */ |
| 215 | void setup_extra_threads(void *(*handler)(void *)) |
| 216 | { |
| 217 | sigset_t blocked_sig, old_sig; |
| 218 | int i; |
| 219 | |
| 220 | /* ensure the signals will be blocked in every thread */ |
| 221 | sigfillset(&blocked_sig); |
| 222 | sigdelset(&blocked_sig, SIGPROF); |
| 223 | sigdelset(&blocked_sig, SIGBUS); |
| 224 | sigdelset(&blocked_sig, SIGFPE); |
| 225 | sigdelset(&blocked_sig, SIGILL); |
| 226 | sigdelset(&blocked_sig, SIGSEGV); |
| 227 | pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig); |
| 228 | |
| 229 | /* Create nbthread-1 thread. The first thread is the current process */ |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 230 | ha_pthread[0] = pthread_self(); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 231 | for (i = 1; i < global.nbthread; i++) |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 232 | pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* waits for all threads to terminate. Does nothing when threads are |
| 236 | * disabled. |
| 237 | */ |
| 238 | void wait_for_threads_completion() |
| 239 | { |
| 240 | int i; |
| 241 | |
| 242 | /* Wait the end of other threads */ |
| 243 | for (i = 1; i < global.nbthread; i++) |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 244 | pthread_join(ha_pthread[i], NULL); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 245 | |
| 246 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 247 | show_lock_stats(); |
| 248 | #endif |
| 249 | } |
| 250 | |
| 251 | /* Tries to set the current thread's CPU affinity according to the cpu_map */ |
| 252 | void set_thread_cpu_affinity() |
| 253 | { |
| 254 | #if defined(USE_CPU_AFFINITY) |
| 255 | /* no affinity setting for the master process */ |
| 256 | if (master) |
| 257 | return; |
| 258 | |
| 259 | /* Now the CPU affinity for all threads */ |
Willy Tarreau | 5b09341 | 2022-07-08 09:38:30 +0200 | [diff] [blame] | 260 | if (ha_cpuset_count(&cpu_map[tgid - 1].proc)) |
| 261 | ha_cpuset_and(&cpu_map[tgid - 1].thread[ti->ltid], &cpu_map[tgid - 1].proc); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | 5b09341 | 2022-07-08 09:38:30 +0200 | [diff] [blame] | 263 | if (ha_cpuset_count(&cpu_map[tgid - 1].thread[ti->ltid])) {/* only do this if the thread has a THREAD map */ |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 264 | # if defined(__APPLE__) |
| 265 | /* Note: this API is limited to the first 32/64 CPUs */ |
Willy Tarreau | 5b09341 | 2022-07-08 09:38:30 +0200 | [diff] [blame] | 266 | unsigned long set = cpu_map[tgid - 1].thread[ti->ltid].cpuset; |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 267 | int j; |
| 268 | |
| 269 | while ((j = ffsl(set)) > 0) { |
| 270 | thread_affinity_policy_data_t cpu_set = { j - 1 }; |
| 271 | thread_port_t mthread; |
| 272 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 273 | mthread = pthread_mach_thread_np(ha_pthread[tid]); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 274 | thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1); |
| 275 | set &= ~(1UL << (j - 1)); |
| 276 | } |
| 277 | # else |
Willy Tarreau | 5b09341 | 2022-07-08 09:38:30 +0200 | [diff] [blame] | 278 | struct hap_cpuset *set = &cpu_map[tgid - 1].thread[ti->ltid]; |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 279 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 280 | pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 281 | # endif |
| 282 | } |
| 283 | #endif /* USE_CPU_AFFINITY */ |
| 284 | } |
| 285 | |
Willy Tarreau | 4eeb883 | 2021-10-06 22:44:28 +0200 | [diff] [blame] | 286 | /* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long |
| 287 | * since POSIX took great care of not specifying its representation, making it |
| 288 | * hard to export for post-mortem analysis. For this reason we copy it into a |
| 289 | * union and will use the smallest scalar type at least as large as its size, |
| 290 | * which will keep endianness and alignment for all regular sizes. As a last |
| 291 | * resort we end up with a long long ligned to the first bytes in memory, which |
| 292 | * will be endian-dependent if pthread_t is larger than a long long (not seen |
| 293 | * yet). |
| 294 | */ |
| 295 | unsigned long long ha_get_pthread_id(unsigned int thr) |
| 296 | { |
| 297 | union { |
| 298 | pthread_t t; |
| 299 | unsigned long long ll; |
| 300 | unsigned int i; |
| 301 | unsigned short s; |
| 302 | unsigned char c; |
| 303 | } u = { 0 }; |
| 304 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 305 | u.t = ha_pthread[thr]; |
Willy Tarreau | 4eeb883 | 2021-10-06 22:44:28 +0200 | [diff] [blame] | 306 | |
| 307 | if (sizeof(u.t) <= sizeof(u.c)) |
| 308 | return u.c; |
| 309 | else if (sizeof(u.t) <= sizeof(u.s)) |
| 310 | return u.s; |
| 311 | else if (sizeof(u.t) <= sizeof(u.i)) |
| 312 | return u.i; |
| 313 | return u.ll; |
| 314 | } |
| 315 | |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 316 | /* send signal <sig> to thread <thr> */ |
| 317 | void ha_tkill(unsigned int thr, int sig) |
| 318 | { |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 319 | pthread_kill(ha_pthread[thr], sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* send signal <sig> to all threads. The calling thread is signaled last in |
| 323 | * order to allow all threads to synchronize in the handler. |
| 324 | */ |
| 325 | void ha_tkillall(int sig) |
| 326 | { |
| 327 | unsigned int thr; |
| 328 | |
| 329 | for (thr = 0; thr < global.nbthread; thr++) { |
Willy Tarreau | f15c75a | 2022-07-15 08:27:56 +0200 | [diff] [blame] | 330 | if (!(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit)) |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 331 | continue; |
| 332 | if (thr == tid) |
| 333 | continue; |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 334 | pthread_kill(ha_pthread[thr], sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 335 | } |
| 336 | raise(sig); |
| 337 | } |
| 338 | |
Willy Tarreau | aa99276 | 2021-10-06 23:33:20 +0200 | [diff] [blame] | 339 | void ha_thread_relax(void) |
| 340 | { |
| 341 | #ifdef _POSIX_PRIORITY_SCHEDULING |
| 342 | sched_yield(); |
| 343 | #else |
| 344 | pl_cpu_relax(); |
| 345 | #endif |
| 346 | } |
| 347 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 348 | /* these calls are used as callbacks at init time when debugging is on */ |
Willy Tarreau | a8ae77d | 2018-11-25 19:28:23 +0100 | [diff] [blame] | 349 | void ha_spin_init(HA_SPINLOCK_T *l) |
| 350 | { |
| 351 | HA_SPIN_INIT(l); |
| 352 | } |
| 353 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 354 | /* these calls are used as callbacks at init time when debugging is on */ |
Willy Tarreau | a8ae77d | 2018-11-25 19:28:23 +0100 | [diff] [blame] | 355 | void ha_rwlock_init(HA_RWLOCK_T *l) |
| 356 | { |
| 357 | HA_RWLOCK_INIT(l); |
| 358 | } |
| 359 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 360 | /* returns the number of CPUs the current process is enabled to run on */ |
| 361 | static int thread_cpus_enabled() |
| 362 | { |
| 363 | int ret = 1; |
| 364 | |
| 365 | #ifdef USE_CPU_AFFINITY |
| 366 | #if defined(__linux__) && defined(CPU_COUNT) |
| 367 | cpu_set_t mask; |
| 368 | |
| 369 | if (sched_getaffinity(0, sizeof(mask), &mask) == 0) |
| 370 | ret = CPU_COUNT(&mask); |
Olivier Houchard | 46453d3 | 2019-04-11 00:06:47 +0200 | [diff] [blame] | 371 | #elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY) |
| 372 | cpuset_t cpuset; |
| 373 | if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, |
| 374 | sizeof(cpuset), &cpuset) == 0) |
| 375 | ret = CPU_COUNT(&cpuset); |
David CARLIER | 6a90601 | 2021-01-15 08:09:56 +0000 | [diff] [blame] | 376 | #elif defined(__APPLE__) |
| 377 | ret = (int)sysconf(_SC_NPROCESSORS_ONLN); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 378 | #endif |
| 379 | #endif |
| 380 | ret = MAX(ret, 1); |
| 381 | ret = MIN(ret, MAX_THREADS); |
| 382 | return ret; |
| 383 | } |
| 384 | |
Amaury Denoyelle | 4c9efde | 2021-03-31 16:57:39 +0200 | [diff] [blame] | 385 | /* Returns 1 if the cpu set is currently restricted for the process else 0. |
| 386 | * Currently only implemented for the Linux platform. |
| 387 | */ |
| 388 | int thread_cpu_mask_forced() |
| 389 | { |
| 390 | #if defined(__linux__) |
| 391 | const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN); |
| 392 | return cpus_avail != thread_cpus_enabled(); |
| 393 | #else |
| 394 | return 0; |
| 395 | #endif |
| 396 | } |
| 397 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 398 | /* Below come the lock-debugging functions */ |
| 399 | |
| 400 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 401 | |
| 402 | struct lock_stat lock_stats[LOCK_LABELS]; |
| 403 | |
| 404 | /* this is only used below */ |
| 405 | static const char *lock_label(enum lock_label label) |
| 406 | { |
| 407 | switch (label) { |
| 408 | case TASK_RQ_LOCK: return "TASK_RQ"; |
| 409 | case TASK_WQ_LOCK: return "TASK_WQ"; |
| 410 | case LISTENER_LOCK: return "LISTENER"; |
| 411 | case PROXY_LOCK: return "PROXY"; |
| 412 | case SERVER_LOCK: return "SERVER"; |
| 413 | case LBPRM_LOCK: return "LBPRM"; |
| 414 | case SIGNALS_LOCK: return "SIGNALS"; |
| 415 | case STK_TABLE_LOCK: return "STK_TABLE"; |
| 416 | case STK_SESS_LOCK: return "STK_SESS"; |
| 417 | case APPLETS_LOCK: return "APPLETS"; |
| 418 | case PEER_LOCK: return "PEER"; |
| 419 | case SHCTX_LOCK: return "SHCTX"; |
| 420 | case SSL_LOCK: return "SSL"; |
| 421 | case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS"; |
| 422 | case PATREF_LOCK: return "PATREF"; |
| 423 | case PATEXP_LOCK: return "PATEXP"; |
| 424 | case VARS_LOCK: return "VARS"; |
| 425 | case COMP_POOL_LOCK: return "COMP_POOL"; |
| 426 | case LUA_LOCK: return "LUA"; |
| 427 | case NOTIF_LOCK: return "NOTIF"; |
| 428 | case SPOE_APPLET_LOCK: return "SPOE_APPLET"; |
| 429 | case DNS_LOCK: return "DNS"; |
| 430 | case PID_LIST_LOCK: return "PID_LIST"; |
| 431 | case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS"; |
| 432 | case PIPES_LOCK: return "PIPES"; |
| 433 | case TLSKEYS_REF_LOCK: return "TLSKEYS_REF"; |
| 434 | case AUTH_LOCK: return "AUTH"; |
| 435 | case LOGSRV_LOCK: return "LOGSRV"; |
| 436 | case DICT_LOCK: return "DICT"; |
| 437 | case PROTO_LOCK: return "PROTO"; |
| 438 | case QUEUE_LOCK: return "QUEUE"; |
| 439 | case CKCH_LOCK: return "CKCH"; |
| 440 | case SNI_LOCK: return "SNI"; |
| 441 | case SSL_SERVER_LOCK: return "SSL_SERVER"; |
| 442 | case SFT_LOCK: return "SFT"; |
| 443 | case IDLE_CONNS_LOCK: return "IDLE_CONNS"; |
| 444 | case QUIC_LOCK: return "QUIC"; |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 445 | case OCSP_LOCK: return "OCSP"; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 446 | case OTHER_LOCK: return "OTHER"; |
| 447 | case DEBUG1_LOCK: return "DEBUG1"; |
| 448 | case DEBUG2_LOCK: return "DEBUG2"; |
| 449 | case DEBUG3_LOCK: return "DEBUG3"; |
| 450 | case DEBUG4_LOCK: return "DEBUG4"; |
| 451 | case DEBUG5_LOCK: return "DEBUG5"; |
| 452 | case LOCK_LABELS: break; /* keep compiler happy */ |
| 453 | }; |
| 454 | /* only way to come here is consecutive to an internal bug */ |
| 455 | abort(); |
| 456 | } |
| 457 | |
| 458 | void show_lock_stats() |
| 459 | { |
| 460 | int lbl; |
| 461 | |
| 462 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 463 | if (!lock_stats[lbl].num_write_locked && |
| 464 | !lock_stats[lbl].num_seek_locked && |
| 465 | !lock_stats[lbl].num_read_locked) { |
| 466 | fprintf(stderr, |
| 467 | "Stats about Lock %s: not used\n", |
| 468 | lock_label(lbl)); |
| 469 | continue; |
| 470 | } |
| 471 | |
| 472 | fprintf(stderr, |
| 473 | "Stats about Lock %s: \n", |
| 474 | lock_label(lbl)); |
| 475 | |
| 476 | if (lock_stats[lbl].num_write_locked) |
| 477 | fprintf(stderr, |
| 478 | "\t # write lock : %lu\n" |
| 479 | "\t # write unlock: %lu (%ld)\n" |
| 480 | "\t # wait time for write : %.3f msec\n" |
| 481 | "\t # wait time for write/lock: %.3f nsec\n", |
| 482 | lock_stats[lbl].num_write_locked, |
| 483 | lock_stats[lbl].num_write_unlocked, |
| 484 | lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked, |
| 485 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 486 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0); |
| 487 | |
| 488 | if (lock_stats[lbl].num_seek_locked) |
| 489 | fprintf(stderr, |
| 490 | "\t # seek lock : %lu\n" |
| 491 | "\t # seek unlock : %lu (%ld)\n" |
| 492 | "\t # wait time for seek : %.3f msec\n" |
| 493 | "\t # wait time for seek/lock : %.3f nsec\n", |
| 494 | lock_stats[lbl].num_seek_locked, |
| 495 | lock_stats[lbl].num_seek_unlocked, |
| 496 | lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked, |
| 497 | (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0, |
| 498 | lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0); |
| 499 | |
| 500 | if (lock_stats[lbl].num_read_locked) |
| 501 | fprintf(stderr, |
| 502 | "\t # read lock : %lu\n" |
| 503 | "\t # read unlock : %lu (%ld)\n" |
| 504 | "\t # wait time for read : %.3f msec\n" |
| 505 | "\t # wait time for read/lock : %.3f nsec\n", |
| 506 | lock_stats[lbl].num_read_locked, |
| 507 | lock_stats[lbl].num_read_unlocked, |
| 508 | lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked, |
| 509 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 510 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 511 | } |
| 512 | } |
| 513 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 514 | void __ha_rwlock_init(struct ha_rwlock *l) |
| 515 | { |
| 516 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 517 | __RWLOCK_INIT(&l->lock); |
| 518 | } |
| 519 | |
| 520 | void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 521 | { |
| 522 | __RWLOCK_DESTROY(&l->lock); |
| 523 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 524 | } |
| 525 | |
| 526 | |
| 527 | void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 528 | const char *func, const char *file, int line) |
| 529 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 530 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 531 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 532 | uint64_t start_time; |
| 533 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 534 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 535 | abort(); |
| 536 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 537 | HA_ATOMIC_OR(&st->wait_writers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 538 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 539 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 540 | __RWLOCK_WRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 541 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 542 | |
| 543 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 544 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 545 | st->cur_writer = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 546 | l->info.last_location.function = func; |
| 547 | l->info.last_location.file = file; |
| 548 | l->info.last_location.line = line; |
| 549 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 550 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 554 | const char *func, const char *file, int line) |
| 555 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 556 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 557 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 558 | uint64_t start_time; |
| 559 | int r; |
| 560 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 561 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 562 | abort(); |
| 563 | |
| 564 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 565 | HA_ATOMIC_OR(&st->wait_writers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 566 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 567 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 568 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 569 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 570 | if (unlikely(r)) { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 571 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 572 | return r; |
| 573 | } |
| 574 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 575 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 576 | st->cur_writer = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 577 | l->info.last_location.function = func; |
| 578 | l->info.last_location.file = file; |
| 579 | l->info.last_location.line = line; |
| 580 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 581 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 587 | const char *func, const char *file, int line) |
| 588 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 589 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 590 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
| 591 | |
| 592 | if (unlikely(!(st->cur_writer & tbit))) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 593 | /* the thread is not owning the lock for write */ |
| 594 | abort(); |
| 595 | } |
| 596 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 597 | st->cur_writer = 0; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 598 | l->info.last_location.function = func; |
| 599 | l->info.last_location.file = file; |
| 600 | l->info.last_location.line = line; |
| 601 | |
| 602 | __RWLOCK_WRUNLOCK(&l->lock); |
| 603 | |
| 604 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 605 | } |
| 606 | |
| 607 | void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 608 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 609 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 610 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 611 | uint64_t start_time; |
| 612 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 613 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 614 | abort(); |
| 615 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 616 | HA_ATOMIC_OR(&st->wait_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 617 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 618 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 619 | __RWLOCK_RDLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 620 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 621 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 622 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 623 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 624 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 625 | HA_ATOMIC_AND(&st->wait_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 629 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 630 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 631 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 632 | int r; |
| 633 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 634 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 635 | abort(); |
| 636 | |
| 637 | /* try read should never wait */ |
| 638 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 639 | if (unlikely(r)) |
| 640 | return r; |
| 641 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 642 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 643 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 649 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 650 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 651 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
| 652 | |
| 653 | if (unlikely(!(st->cur_readers & tbit))) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 654 | /* the thread is not owning the lock for read */ |
| 655 | abort(); |
| 656 | } |
| 657 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 658 | HA_ATOMIC_AND(&st->cur_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 659 | |
| 660 | __RWLOCK_RDUNLOCK(&l->lock); |
| 661 | |
| 662 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked); |
| 663 | } |
| 664 | |
| 665 | void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l, |
| 666 | const char *func, const char *file, int line) |
| 667 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 668 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 669 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 670 | uint64_t start_time; |
| 671 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 672 | if ((st->cur_readers | st->cur_seeker) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 673 | abort(); |
| 674 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 675 | if (!(st->cur_writer & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 676 | abort(); |
| 677 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 678 | HA_ATOMIC_OR(&st->wait_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 679 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 680 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 681 | __RWLOCK_WRTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 682 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 683 | |
| 684 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 685 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 686 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
| 687 | HA_ATOMIC_AND(&st->cur_writer, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 688 | l->info.last_location.function = func; |
| 689 | l->info.last_location.file = file; |
| 690 | l->info.last_location.line = line; |
| 691 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 692 | HA_ATOMIC_AND(&st->wait_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 696 | const char *func, const char *file, int line) |
| 697 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 698 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 699 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 700 | uint64_t start_time; |
| 701 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 702 | if ((st->cur_readers | st->cur_seeker) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 703 | abort(); |
| 704 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 705 | if (!(st->cur_writer & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 706 | abort(); |
| 707 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 708 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 709 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 710 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 711 | __RWLOCK_WRTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 712 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 713 | |
| 714 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 715 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 716 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
| 717 | HA_ATOMIC_AND(&st->cur_writer, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 718 | l->info.last_location.function = func; |
| 719 | l->info.last_location.file = file; |
| 720 | l->info.last_location.line = line; |
| 721 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 722 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l, |
| 726 | const char *func, const char *file, int line) |
| 727 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 728 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 729 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 730 | uint64_t start_time; |
| 731 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 732 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 733 | abort(); |
| 734 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 735 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 736 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 737 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 738 | __RWLOCK_SKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 739 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 740 | |
| 741 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 742 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 743 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 744 | l->info.last_location.function = func; |
| 745 | l->info.last_location.file = file; |
| 746 | l->info.last_location.line = line; |
| 747 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 748 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l, |
| 752 | const char *func, const char *file, int line) |
| 753 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 754 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 755 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 756 | uint64_t start_time; |
| 757 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 758 | if ((st->cur_readers | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 759 | abort(); |
| 760 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 761 | if (!(st->cur_seeker & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 762 | abort(); |
| 763 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 764 | HA_ATOMIC_OR(&st->wait_writers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 765 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 766 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 767 | __RWLOCK_SKTOWR(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 768 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 769 | |
| 770 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 771 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 772 | HA_ATOMIC_OR(&st->cur_writer, tbit); |
| 773 | HA_ATOMIC_AND(&st->cur_seeker, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 774 | l->info.last_location.function = func; |
| 775 | l->info.last_location.file = file; |
| 776 | l->info.last_location.line = line; |
| 777 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 778 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l, |
| 782 | const char *func, const char *file, int line) |
| 783 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 784 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 785 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 786 | uint64_t start_time; |
| 787 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 788 | if ((st->cur_readers | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 789 | abort(); |
| 790 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 791 | if (!(st->cur_seeker & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 792 | abort(); |
| 793 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 794 | HA_ATOMIC_OR(&st->wait_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 795 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 796 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 797 | __RWLOCK_SKTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 798 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 799 | |
| 800 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 801 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 802 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
| 803 | HA_ATOMIC_AND(&st->cur_seeker, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 804 | l->info.last_location.function = func; |
| 805 | l->info.last_location.file = file; |
| 806 | l->info.last_location.line = line; |
| 807 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 808 | HA_ATOMIC_AND(&st->wait_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 812 | const char *func, const char *file, int line) |
| 813 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 814 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 815 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
| 816 | if (!(st->cur_seeker & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 817 | abort(); |
| 818 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 819 | HA_ATOMIC_AND(&st->cur_seeker, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 820 | l->info.last_location.function = func; |
| 821 | l->info.last_location.file = file; |
| 822 | l->info.last_location.line = line; |
| 823 | |
| 824 | __RWLOCK_SKUNLOCK(&l->lock); |
| 825 | |
| 826 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked); |
| 827 | } |
| 828 | |
| 829 | int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l, |
| 830 | const char *func, const char *file, int line) |
| 831 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 832 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 833 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 834 | uint64_t start_time; |
| 835 | int r; |
| 836 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 837 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 838 | abort(); |
| 839 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 840 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 841 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 842 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 843 | r = __RWLOCK_TRYSKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 844 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 845 | |
| 846 | if (likely(!r)) { |
| 847 | /* got the lock ! */ |
| 848 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 849 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 850 | l->info.last_location.function = func; |
| 851 | l->info.last_location.file = file; |
| 852 | l->info.last_location.line = line; |
| 853 | } |
| 854 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 855 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 856 | return r; |
| 857 | } |
| 858 | |
| 859 | int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 860 | const char *func, const char *file, int line) |
| 861 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 862 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 863 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 864 | uint64_t start_time; |
| 865 | int r; |
| 866 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 867 | if ((st->cur_writer | st->cur_seeker) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 868 | abort(); |
| 869 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 870 | if (!(st->cur_readers & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 871 | abort(); |
| 872 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 873 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 874 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 875 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 876 | r = __RWLOCK_TRYRDTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 877 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 878 | |
| 879 | if (likely(!r)) { |
| 880 | /* got the lock ! */ |
| 881 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 882 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
| 883 | HA_ATOMIC_AND(&st->cur_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 884 | l->info.last_location.function = func; |
| 885 | l->info.last_location.file = file; |
| 886 | l->info.last_location.line = line; |
| 887 | } |
| 888 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 889 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 890 | return r; |
| 891 | } |
| 892 | |
| 893 | void __spin_init(struct ha_spinlock *l) |
| 894 | { |
| 895 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 896 | __SPIN_INIT(&l->lock); |
| 897 | } |
| 898 | |
| 899 | void __spin_destroy(struct ha_spinlock *l) |
| 900 | { |
| 901 | __SPIN_DESTROY(&l->lock); |
| 902 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 903 | } |
| 904 | |
| 905 | void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 906 | const char *func, const char *file, int line) |
| 907 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 908 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 909 | struct ha_spinlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 910 | uint64_t start_time; |
| 911 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 912 | if (unlikely(st->owner & tbit)) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 913 | /* the thread is already owning the lock */ |
| 914 | abort(); |
| 915 | } |
| 916 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 917 | HA_ATOMIC_OR(&st->waiters, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 918 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 919 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 920 | __SPIN_LOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 921 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 922 | |
| 923 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 924 | |
| 925 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 926 | st->owner = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 927 | l->info.last_location.function = func; |
| 928 | l->info.last_location.file = file; |
| 929 | l->info.last_location.line = line; |
| 930 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 931 | HA_ATOMIC_AND(&st->waiters, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 935 | const char *func, const char *file, int line) |
| 936 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 937 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 938 | struct ha_spinlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 939 | int r; |
| 940 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 941 | if (unlikely(st->owner & tbit)) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 942 | /* the thread is already owning the lock */ |
| 943 | abort(); |
| 944 | } |
| 945 | |
| 946 | /* try read should never wait */ |
| 947 | r = __SPIN_TRYLOCK(&l->lock); |
| 948 | if (unlikely(r)) |
| 949 | return r; |
| 950 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 951 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 952 | st->owner = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 953 | l->info.last_location.function = func; |
| 954 | l->info.last_location.file = file; |
| 955 | l->info.last_location.line = line; |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 961 | const char *func, const char *file, int line) |
| 962 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 963 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 964 | struct ha_spinlock_state *st = &l->info.st[tgid-1]; |
| 965 | |
| 966 | if (unlikely(!(st->owner & tbit))) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 967 | /* the thread is not owning the lock */ |
| 968 | abort(); |
| 969 | } |
| 970 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 971 | st->owner = 0; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 972 | l->info.last_location.function = func; |
| 973 | l->info.last_location.file = file; |
| 974 | l->info.last_location.line = line; |
| 975 | |
| 976 | __SPIN_UNLOCK(&l->lock); |
| 977 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 978 | } |
| 979 | |
| 980 | #endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 981 | |
Willy Tarreau | 87aff02 | 2022-07-10 10:58:57 +0200 | [diff] [blame] | 982 | |
| 983 | #if defined(USE_PTHREAD_EMULATION) |
| 984 | |
| 985 | /* pthread rwlock emulation using plocks (to avoid expensive futexes). |
| 986 | * these are a direct mapping on Progressive Locks, with the exception that |
| 987 | * since there's a common unlock operation in pthreads, we need to know if |
| 988 | * we need to unlock for reads or writes, so we set the topmost bit to 1 when |
| 989 | * a write lock is acquired to indicate that a write unlock needs to be |
| 990 | * performed. It's not a problem since this bit will never be used given that |
| 991 | * haproxy won't support as many threads as the plocks. |
| 992 | * |
| 993 | * The storage is the pthread_rwlock_t cast as an ulong |
| 994 | */ |
| 995 | |
| 996 | int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr) |
| 997 | { |
| 998 | ulong *lock = (ulong *)rwlock; |
| 999 | |
| 1000 | *lock = 0; |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) |
| 1005 | { |
| 1006 | ulong *lock = (ulong *)rwlock; |
| 1007 | |
| 1008 | *lock = 0; |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) |
| 1013 | { |
| 1014 | pl_lorw_rdlock((unsigned long *)rwlock); |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock) |
| 1019 | { |
| 1020 | return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_SHR_BASE); |
| 1021 | } |
| 1022 | |
| 1023 | int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime) |
| 1024 | { |
| 1025 | return pthread_rwlock_tryrdlock(rwlock); |
| 1026 | } |
| 1027 | |
| 1028 | int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) |
| 1029 | { |
| 1030 | pl_lorw_wrlock((unsigned long *)rwlock); |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock) |
| 1035 | { |
| 1036 | return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_EXC_BASE); |
| 1037 | } |
| 1038 | |
| 1039 | int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime) |
| 1040 | { |
| 1041 | return pthread_rwlock_trywrlock(rwlock); |
| 1042 | } |
| 1043 | |
| 1044 | int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) |
| 1045 | { |
| 1046 | pl_lorw_unlock((unsigned long *)rwlock); |
| 1047 | return 0; |
| 1048 | } |
| 1049 | #endif // defined(USE_PTHREAD_EMULATION) |
| 1050 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 1051 | /* Depending on the platform and how libpthread was built, pthread_exit() may |
| 1052 | * involve some code in libgcc_s that would be loaded on exit for the first |
| 1053 | * time, causing aborts if the process is chrooted. It's harmless bit very |
| 1054 | * dirty. There isn't much we can do to make sure libgcc_s is loaded only if |
| 1055 | * needed, so what we do here is that during early boot we create a dummy |
| 1056 | * thread that immediately exits. This will lead to libgcc_s being loaded |
| 1057 | * during boot on the platforms where it's required. |
| 1058 | */ |
| 1059 | static void *dummy_thread_function(void *data) |
| 1060 | { |
| 1061 | pthread_exit(NULL); |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | |
| 1065 | static inline void preload_libgcc_s(void) |
| 1066 | { |
| 1067 | pthread_t dummy_thread; |
| 1068 | pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL); |
| 1069 | pthread_join(dummy_thread, NULL); |
| 1070 | } |
| 1071 | |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 1072 | static void __thread_init(void) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1073 | { |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 1074 | char *ptr = NULL; |
| 1075 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 1076 | preload_libgcc_s(); |
Willy Tarreau | 77b9822 | 2020-09-02 08:04:35 +0200 | [diff] [blame] | 1077 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 1078 | thread_cpus_enabled_at_boot = thread_cpus_enabled(); |
| 1079 | |
Willy Tarreau | c80bdb2 | 2022-08-06 16:44:55 +0200 | [diff] [blame] | 1080 | memprintf(&ptr, "Built with multi-threading support (MAX_TGROUPS=%d, MAX_THREADS=%d, default=%d).", |
| 1081 | MAX_TGROUPS, MAX_THREADS, thread_cpus_enabled_at_boot); |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 1082 | hap_register_build_opts(ptr, 1); |
| 1083 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1084 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 1085 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 1086 | #endif |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1087 | } |
Willy Tarreau | 8ead1d0 | 2022-04-25 19:23:17 +0200 | [diff] [blame] | 1088 | INITCALL0(STG_PREPARE, __thread_init); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1089 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 1090 | #else |
| 1091 | |
Willy Tarreau | aa99276 | 2021-10-06 23:33:20 +0200 | [diff] [blame] | 1092 | /* send signal <sig> to thread <thr> (send to process in fact) */ |
| 1093 | void ha_tkill(unsigned int thr, int sig) |
| 1094 | { |
| 1095 | raise(sig); |
| 1096 | } |
| 1097 | |
| 1098 | /* send signal <sig> to all threads (send to process in fact) */ |
| 1099 | void ha_tkillall(int sig) |
| 1100 | { |
| 1101 | raise(sig); |
| 1102 | } |
| 1103 | |
| 1104 | void ha_thread_relax(void) |
| 1105 | { |
| 1106 | #ifdef _POSIX_PRIORITY_SCHEDULING |
| 1107 | sched_yield(); |
| 1108 | #endif |
| 1109 | } |
| 1110 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 1111 | REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set)."); |
| 1112 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1113 | #endif // USE_THREAD |
| 1114 | |
| 1115 | |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1116 | /* scans the configured thread mapping and establishes the final one. Returns <0 |
| 1117 | * on failure, >=0 on success. |
| 1118 | */ |
| 1119 | int thread_map_to_groups() |
| 1120 | { |
| 1121 | int t, g, ut, ug; |
| 1122 | int q, r; |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1123 | ulong m __maybe_unused; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1124 | |
| 1125 | ut = ug = 0; // unassigned threads & groups |
| 1126 | |
| 1127 | for (t = 0; t < global.nbthread; t++) { |
| 1128 | if (!ha_thread_info[t].tg) |
| 1129 | ut++; |
| 1130 | } |
| 1131 | |
| 1132 | for (g = 0; g < global.nbtgroups; g++) { |
| 1133 | if (!ha_tgroup_info[g].count) |
| 1134 | ug++; |
Willy Tarreau | 60fe4a9 | 2022-06-28 17:48:07 +0200 | [diff] [blame] | 1135 | ha_tgroup_info[g].tgid_bit = 1UL << g; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | if (ug > ut) { |
| 1139 | ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut); |
| 1140 | return -1; |
| 1141 | } |
| 1142 | |
| 1143 | /* look for first unassigned thread */ |
| 1144 | for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++) |
| 1145 | ; |
| 1146 | |
| 1147 | /* assign threads to empty groups */ |
| 1148 | for (g = 0; ug && ut; ) { |
| 1149 | /* due to sparse thread assignment we can end up with more threads |
| 1150 | * per group on last assigned groups than former ones, so we must |
| 1151 | * always try to pack the maximum remaining ones together first. |
| 1152 | */ |
| 1153 | q = ut / ug; |
| 1154 | r = ut % ug; |
| 1155 | if ((q + !!r) > MAX_THREADS_PER_GROUP) { |
| 1156 | ha_alert("Too many remaining unassigned threads (%d) for thread groups (%d). Please increase thread-groups or make sure to keep thread numbers contiguous\n", ug, ut); |
| 1157 | return -1; |
| 1158 | } |
| 1159 | |
| 1160 | /* thread <t> is the next unassigned one. Let's look for next |
| 1161 | * unassigned group, we know there are some left |
| 1162 | */ |
| 1163 | while (ut >= ug && ha_tgroup_info[g].count) |
| 1164 | g++; |
| 1165 | |
| 1166 | /* group g is unassigned, try to fill it with consecutive threads */ |
| 1167 | while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r && |
| 1168 | (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) { |
| 1169 | |
| 1170 | if (!ha_tgroup_info[g].count) { |
| 1171 | /* assign new group */ |
| 1172 | ha_tgroup_info[g].base = t; |
| 1173 | ug--; |
| 1174 | } |
| 1175 | |
| 1176 | ha_tgroup_info[g].count++; |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1177 | ha_thread_info[t].tgid = g + 1; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1178 | ha_thread_info[t].tg = &ha_tgroup_info[g]; |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 1179 | ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g]; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1180 | |
| 1181 | ut--; |
| 1182 | /* switch to next unassigned thread */ |
| 1183 | while (++t < global.nbthread && ha_thread_info[t].tg) |
| 1184 | ; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | if (ut) { |
| 1189 | ha_alert("Remaining unassigned threads found (%d) because all groups are in use. Please increase 'thread-groups', reduce 'nbthreads' or remove or extend 'thread-group' enumerations.\n", ut); |
| 1190 | return -1; |
| 1191 | } |
| 1192 | |
Willy Tarreau | cc7a11e | 2021-09-28 08:53:11 +0200 | [diff] [blame] | 1193 | for (t = 0; t < global.nbthread; t++) { |
| 1194 | ha_thread_info[t].tid = t; |
| 1195 | ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base; |
Willy Tarreau | cc7a11e | 2021-09-28 08:53:11 +0200 | [diff] [blame] | 1196 | ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid; |
| 1197 | } |
| 1198 | |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1199 | m = 0; |
Willy Tarreau | 377e37a | 2022-06-24 15:18:49 +0200 | [diff] [blame] | 1200 | for (g = 0; g < global.nbtgroups; g++) { |
| 1201 | ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count); |
Aurelien DARRAGON | 739281b | 2023-01-27 15:13:28 +0100 | [diff] [blame] | 1202 | /* for now, additional threads are not started, so we should |
| 1203 | * consider them as harmless and idle. |
| 1204 | * This will get automatically updated when such threads are |
| 1205 | * started in run_thread_poll_loop() |
| 1206 | * Without this, thread_isolate() and thread_isolate_full() |
| 1207 | * will fail to work as long as secondary threads did not enter |
| 1208 | * the polling loop at least once. |
| 1209 | */ |
| 1210 | ha_tgroup_ctx[g].threads_harmless = ha_tgroup_info[g].threads_enabled; |
| 1211 | ha_tgroup_ctx[g].threads_idle = ha_tgroup_info[g].threads_enabled; |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1212 | if (!ha_tgroup_info[g].count) |
| 1213 | continue; |
| 1214 | m |= 1UL << g; |
Willy Tarreau | 377e37a | 2022-06-24 15:18:49 +0200 | [diff] [blame] | 1215 | |
| 1216 | } |
| 1217 | |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1218 | #ifdef USE_THREAD |
| 1219 | all_tgroups_mask = m; |
| 1220 | #endif |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1221 | return 0; |
| 1222 | } |
| 1223 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1224 | /* Converts a configuration thread set based on either absolute or relative |
| 1225 | * thread numbers into a global group+mask. This is essentially for use with |
| 1226 | * the "thread" directive on "bind" lines, where "thread 4-6,10-12" might be |
| 1227 | * turned to "2/1-3,4/1-3". It cannot be used before the thread mapping above |
| 1228 | * was completed and the thread group numbers configured. The thread_set is |
| 1229 | * replaced by the resolved group-based one. It is possible to force a single |
| 1230 | * default group for unspecified sets instead of enabling all groups by passing |
Willy Tarreau | f2988e1 | 2023-02-02 17:01:10 +0100 | [diff] [blame] | 1231 | * this group's non-zero value to defgrp. |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1232 | * |
| 1233 | * Returns <0 on failure, >=0 on success. |
| 1234 | */ |
Willy Tarreau | f2988e1 | 2023-02-02 17:01:10 +0100 | [diff] [blame] | 1235 | int thread_resolve_group_mask(struct thread_set *ts, int defgrp, char **err) |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1236 | { |
Willy Tarreau | 15c8428 | 2023-02-04 10:49:01 +0100 | [diff] [blame^] | 1237 | struct thread_set new_ts = { }; |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1238 | ulong mask, imask; |
| 1239 | uint g; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1240 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1241 | if (!ts->nbgrp) { |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1242 | /* unspecified group, IDs are global */ |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1243 | if (thread_set_is_empty(ts)) { |
| 1244 | /* all threads of all groups, unless defgrp is set and |
| 1245 | * we then set it as the only group. |
| 1246 | */ |
| 1247 | for (g = defgrp ? defgrp-1 : 0; g < (defgrp ? defgrp : global.nbtgroups); g++) { |
| 1248 | new_ts.rel[g] = ha_tgroup_info[g].threads_enabled; |
| 1249 | new_ts.nbgrp++; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1250 | } |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1251 | } else { |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1252 | /* some absolute threads are set, we must remap them to |
| 1253 | * relative ones. Each group cannot have more than |
| 1254 | * LONGBITS threads, thus it spans at most two absolute |
| 1255 | * blocks. |
| 1256 | */ |
| 1257 | for (g = 0; g < global.nbtgroups; g++) { |
| 1258 | uint block = ha_tgroup_info[g].base / LONGBITS; |
| 1259 | uint base = ha_tgroup_info[g].base % LONGBITS; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1260 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1261 | mask = ts->abs[block] >> base; |
| 1262 | if (base && ha_tgroup_info[g].count > (LONGBITS - base)) |
| 1263 | mask |= ts->abs[block + 1] << (LONGBITS - base); |
| 1264 | mask &= nbits(ha_tgroup_info[g].count); |
| 1265 | mask &= ha_tgroup_info[g].threads_enabled; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1266 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1267 | /* now the mask exactly matches the threads to be enabled |
| 1268 | * in this group. |
| 1269 | */ |
| 1270 | if (!new_ts.rel[g] && mask) |
| 1271 | new_ts.nbgrp++; |
| 1272 | new_ts.rel[g] |= mask; |
| 1273 | } |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1274 | } |
| 1275 | } else { |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1276 | /* groups were specified */ |
| 1277 | for (g = 0; g < MAX_TGROUPS; g++) { |
| 1278 | imask = ts->rel[g]; |
| 1279 | if (!imask) |
| 1280 | continue; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1281 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1282 | if (g >= global.nbtgroups) { |
| 1283 | memprintf(err, "'thread' directive references non-existing thread group %u", g+1); |
| 1284 | return -1; |
| 1285 | } |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1286 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1287 | /* some relative threads are set. Keep only existing ones for this group */ |
| 1288 | mask = nbits(ha_tgroup_info[g].count); |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1289 | |
| 1290 | if (!(mask & imask)) { |
| 1291 | /* no intersection between the thread group's |
| 1292 | * threads and the bind line's. |
| 1293 | */ |
| 1294 | #ifdef THREAD_AUTO_ADJUST_GROUPS |
| 1295 | unsigned long new_mask = 0; |
| 1296 | |
| 1297 | while (imask) { |
| 1298 | new_mask |= imask & mask; |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1299 | imask >>= ha_tgroup_info[g].count; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1300 | } |
| 1301 | imask = new_mask; |
| 1302 | #else |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1303 | memprintf(err, "'thread' directive only references threads not belonging to group %u", g+1); |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1304 | return -1; |
| 1305 | #endif |
| 1306 | } |
| 1307 | |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1308 | new_ts.rel[g] = imask & mask; |
| 1309 | new_ts.nbgrp++; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1310 | } |
| 1311 | } |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1312 | |
| 1313 | /* update the thread_set */ |
| 1314 | if (!thread_set_first_group(&new_ts)) { |
| 1315 | memprintf(err, "'thread' directive only references non-existing threads"); |
| 1316 | return -1; |
| 1317 | } |
| 1318 | |
| 1319 | *ts = new_ts; |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | /* Parse a string representing a thread set in one of the following forms: |
| 1324 | * |
| 1325 | * - { "all" | "odd" | "even" | <abs_num> [ "-" <abs_num> ] }[,...] |
| 1326 | * => these are (lists of) absolute thread numbers |
| 1327 | * |
| 1328 | * - <tgnum> "/" { "all" | "odd" | "even" | <rel_num> [ "-" <rel_num> ][,...] |
| 1329 | * => these are (lists of) per-group relative thread numbers. All numbers |
| 1330 | * must be lower than or equal to LONGBITS. When multiple list elements |
| 1331 | * are provided, each of them must contain the thread group number. |
| 1332 | * |
| 1333 | * Minimum value for a thread or group number is always 1. Maximum value for an |
| 1334 | * absolute thread number is MAX_THREADS, maximum value for a relative thread |
| 1335 | * number is MAX_THREADS_PER_GROUP, an maximum value for a thread group is |
| 1336 | * MAX_TGROUPS. "all", "even" and "odd" will be bound by MAX_THREADS and/or |
| 1337 | * MAX_THREADS_PER_GROUP in any case. In ranges, a missing digit before "-" |
| 1338 | * is implicitly 1, and a missing digit after "-" is implicitly the highest of |
| 1339 | * its class. As such "-" is equivalent to "all", allowing to build strings |
| 1340 | * such as "${MIN}-${MAX}" where both MIN and MAX are optional. |
| 1341 | * |
| 1342 | * It is not valid to mix absolute and relative numbers. As such: |
| 1343 | * - all valid (all absolute threads) |
| 1344 | * - 12-19,24-31 valid (abs threads 12 to 19 and 24 to 31) |
| 1345 | * - 1/all valid (all 32 or 64 threads of group 1) |
| 1346 | * - 1/1-4,1/8-10,2/1 valid |
| 1347 | * - 1/1-4,8-10 invalid (mixes relatve "1/1-4" with absolute "8-10") |
| 1348 | * - 1-4,8-10,2/1 invalid (mixes absolute "1-4,8-10" with relative "2/1") |
| 1349 | * - 1/odd-4 invalid (mixes range with boundary) |
| 1350 | * |
| 1351 | * The target thread set is *completed* with supported threads, which means |
| 1352 | * that it's the caller's responsibility for pre-initializing it. If the target |
| 1353 | * thread set is NULL, it's not updated and the function only verifies that the |
| 1354 | * input parses. |
| 1355 | * |
| 1356 | * On success, it returns 0. otherwise it returns non-zero with an error |
| 1357 | * message in <err>. |
| 1358 | */ |
| 1359 | int parse_thread_set(const char *arg, struct thread_set *ts, char **err) |
| 1360 | { |
| 1361 | const char *set; |
| 1362 | const char *sep; |
| 1363 | int v, min, max, tg; |
| 1364 | int is_rel; |
| 1365 | |
| 1366 | /* search for the first delimiter (',', '-' or '/') to decide whether |
| 1367 | * we're facing an absolute or relative form. The relative form always |
| 1368 | * starts with a number followed by a slash. |
| 1369 | */ |
| 1370 | for (sep = arg; isdigit((uchar)*sep); sep++) |
| 1371 | ; |
| 1372 | |
| 1373 | is_rel = (/*sep > arg &&*/ *sep == '/'); /* relative form */ |
| 1374 | |
| 1375 | /* from there we have to cut the thread spec around commas */ |
| 1376 | |
| 1377 | set = arg; |
| 1378 | tg = 0; |
| 1379 | while (*set) { |
| 1380 | /* note: we can't use strtol() here because "-3" would parse as |
| 1381 | * (-3) while we want to stop before the "-", so we find the |
| 1382 | * separator ourselves and rely on atoi() whose value we may |
| 1383 | * ignore depending where the separator is. |
| 1384 | */ |
| 1385 | for (sep = set; isdigit((uchar)*sep); sep++) |
| 1386 | ; |
| 1387 | |
| 1388 | if (sep != set && *sep && *sep != '/' && *sep != '-' && *sep != ',') { |
| 1389 | memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set); |
| 1390 | return -1; |
| 1391 | } |
| 1392 | |
| 1393 | v = (sep != set) ? atoi(set) : 0; |
| 1394 | |
| 1395 | /* Now we know that the string is made of an optional series of digits |
| 1396 | * optionally followed by one of the delimiters above, or that it |
| 1397 | * starts with a different character. |
| 1398 | */ |
| 1399 | |
| 1400 | /* first, let's search for the thread group (digits before '/') */ |
| 1401 | |
| 1402 | if (tg || !is_rel) { |
| 1403 | /* thread group already specified or not expected if absolute spec */ |
| 1404 | if (*sep == '/') { |
| 1405 | if (tg) |
| 1406 | memprintf(err, "redundant thread group specification '%s' for group %d", set, tg); |
| 1407 | else |
| 1408 | memprintf(err, "group-relative thread specification '%s' is not permitted after a absolute thread range.", set); |
| 1409 | return -1; |
| 1410 | } |
| 1411 | } else { |
| 1412 | /* this is a group-relative spec, first field is the group number */ |
| 1413 | if (sep == set && *sep == '/') { |
| 1414 | memprintf(err, "thread group number expected before '%s'.", set); |
| 1415 | return -1; |
| 1416 | } |
| 1417 | |
| 1418 | if (*sep != '/') { |
| 1419 | memprintf(err, "absolute thread specification '%s' is not permitted after a group-relative thread range.", set); |
| 1420 | return -1; |
| 1421 | } |
| 1422 | |
| 1423 | if (v < 1 || v > MAX_TGROUPS) { |
| 1424 | memprintf(err, "invalid thread group number '%d', permitted range is 1..%d in '%s'.", v, MAX_TGROUPS, set); |
| 1425 | return -1; |
| 1426 | } |
| 1427 | |
| 1428 | tg = v; |
| 1429 | |
| 1430 | /* skip group number and go on with set,sep,v as if |
| 1431 | * there was no group number. |
| 1432 | */ |
| 1433 | set = sep + 1; |
| 1434 | continue; |
| 1435 | } |
| 1436 | |
| 1437 | /* Now 'set' starts at the min thread number, whose value is in v if any, |
| 1438 | * and preset the max to it, unless the range is filled at once via "all" |
| 1439 | * (stored as 1:0), "odd" (stored as) 1:-1, or "even" (stored as 1:-2). |
| 1440 | * 'sep' points to the next non-digit which may be set itself e.g. for |
| 1441 | * "all" etc or "-xx". |
| 1442 | */ |
| 1443 | |
| 1444 | if (!*set) { |
| 1445 | /* empty set sets no restriction */ |
| 1446 | min = 1; |
| 1447 | max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS; |
| 1448 | } |
| 1449 | else { |
| 1450 | if (sep != set && *sep && *sep != '-' && *sep != ',') { |
| 1451 | // Only delimitors are permitted around digits. |
| 1452 | memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set); |
| 1453 | return -1; |
| 1454 | } |
| 1455 | |
| 1456 | /* for non-digits, find next delim */ |
| 1457 | for (; *sep && *sep != '-' && *sep != ','; sep++) |
| 1458 | ; |
| 1459 | |
| 1460 | min = max = 1; |
| 1461 | if (sep != set) { |
| 1462 | /* non-empty first thread */ |
| 1463 | if (isteq(ist2(set, sep-set), ist("all"))) |
| 1464 | max = 0; |
| 1465 | else if (isteq(ist2(set, sep-set), ist("odd"))) |
| 1466 | max = -1; |
| 1467 | else if (isteq(ist2(set, sep-set), ist("even"))) |
| 1468 | max = -2; |
| 1469 | else if (v) |
| 1470 | min = max = v; |
| 1471 | else |
| 1472 | max = min = 0; // throw an error below |
| 1473 | } |
| 1474 | |
| 1475 | if (min < 1 || min > MAX_THREADS || (is_rel && min > MAX_THREADS_PER_GROUP)) { |
| 1476 | memprintf(err, "invalid first thread number '%s', permitted range is 1..%d, or 'all', 'odd', 'even'.", |
| 1477 | set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS); |
| 1478 | return -1; |
| 1479 | } |
| 1480 | |
| 1481 | /* is this a range ? */ |
| 1482 | if (*sep == '-') { |
| 1483 | if (min != max) { |
| 1484 | memprintf(err, "extraneous range after 'all', 'odd' or 'even': '%s'.", set); |
| 1485 | return -1; |
| 1486 | } |
| 1487 | |
| 1488 | /* this is a seemingly valid range, there may be another number */ |
| 1489 | for (set = ++sep; isdigit((uchar)*sep); sep++) |
| 1490 | ; |
| 1491 | v = atoi(set); |
| 1492 | |
| 1493 | if (sep == set) { // no digit: to the max |
| 1494 | max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS; |
| 1495 | if (*sep && *sep != ',') |
| 1496 | max = 0; // throw an error below |
| 1497 | } else |
| 1498 | max = v; |
| 1499 | |
| 1500 | if (max < 1 || max > MAX_THREADS || (is_rel && max > MAX_THREADS_PER_GROUP)) { |
| 1501 | memprintf(err, "invalid last thread number '%s', permitted range is 1..%d.", |
| 1502 | set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS); |
| 1503 | return -1; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | /* here sep points to the first non-digit after the thread spec, |
| 1508 | * must be a valid delimiter. |
| 1509 | */ |
| 1510 | if (*sep && *sep != ',') { |
| 1511 | memprintf(err, "invalid character '%c' after thread set specification: '%s'.", *sep, set); |
| 1512 | return -1; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | /* store values */ |
| 1517 | if (ts) { |
| 1518 | if (is_rel) { |
| 1519 | /* group-relative thread numbers */ |
| 1520 | if (!ts->rel[tg - 1]) |
| 1521 | ts->nbgrp++; |
| 1522 | |
| 1523 | if (max >= min) { |
| 1524 | for (v = min; v <= max; v++) |
| 1525 | ts->rel[tg - 1] |= 1UL << v; |
| 1526 | } else { |
| 1527 | memset(&ts->rel[tg - 1], |
| 1528 | (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */, |
| 1529 | sizeof(ts->rel[tg - 1])); |
| 1530 | } |
| 1531 | } else { |
| 1532 | /* absolute thread numbers */ |
| 1533 | if (max >= min) { |
| 1534 | for (v = min; v <= max; v++) |
| 1535 | ts->abs[v / LONGBITS] |= 1UL << (v % LONGBITS); |
| 1536 | } else { |
| 1537 | memset(&ts->abs, |
| 1538 | (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */, |
| 1539 | sizeof(ts->abs)); |
| 1540 | } |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | set = *sep ? sep + 1 : sep; |
| 1545 | tg = 0; |
| 1546 | } |
| 1547 | return 0; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1548 | } |
| 1549 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1550 | /* Parse the "nbthread" global directive, which takes an integer argument that |
| 1551 | * contains the desired number of threads. |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1552 | */ |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1553 | static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx, |
| 1554 | const struct proxy *defpx, const char *file, int line, |
| 1555 | char **err) |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1556 | { |
| 1557 | long nbthread; |
| 1558 | char *errptr; |
| 1559 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1560 | if (too_many_args(1, args, err, NULL)) |
| 1561 | return -1; |
| 1562 | |
Christopher Faulet | 5534334 | 2022-11-18 15:52:58 +0100 | [diff] [blame] | 1563 | if (non_global_section_parsed == 1) { |
| 1564 | memprintf(err, "'%s' not allowed if a non-global section was previously defined. This parameter must be declared in the first global section", args[0]); |
| 1565 | return -1; |
| 1566 | } |
| 1567 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1568 | nbthread = strtol(args[1], &errptr, 10); |
| 1569 | if (!*args[1] || *errptr) { |
| 1570 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1571 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | #ifndef USE_THREAD |
| 1575 | if (nbthread != 1) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1576 | memprintf(err, "'%s' specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD", args[0]); |
| 1577 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1578 | } |
| 1579 | #else |
| 1580 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1581 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread); |
| 1582 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1583 | } |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1584 | #endif |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1585 | |
| 1586 | HA_DIAG_WARNING_COND(global.nbthread, |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1587 | "parsing [%s:%d] : '%s' is already defined and will be overridden.\n", |
| 1588 | file, line, args[0]); |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1589 | |
| 1590 | global.nbthread = nbthread; |
| 1591 | return 0; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1592 | } |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1593 | |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1594 | /* Parse the "thread-group" global directive, which takes an integer argument |
| 1595 | * that designates a thread group, and a list of threads to put into that group. |
| 1596 | */ |
| 1597 | static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx, |
| 1598 | const struct proxy *defpx, const char *file, int line, |
| 1599 | char **err) |
| 1600 | { |
| 1601 | char *errptr; |
| 1602 | long tnum, tend, tgroup; |
| 1603 | int arg, tot; |
| 1604 | |
Christopher Faulet | 5534334 | 2022-11-18 15:52:58 +0100 | [diff] [blame] | 1605 | if (non_global_section_parsed == 1) { |
| 1606 | memprintf(err, "'%s' not allowed if a non-global section was previously defined. This parameter must be declared in the first global section", args[0]); |
| 1607 | return -1; |
| 1608 | } |
| 1609 | |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1610 | tgroup = strtol(args[1], &errptr, 10); |
| 1611 | if (!*args[1] || *errptr) { |
| 1612 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1613 | return -1; |
| 1614 | } |
| 1615 | |
| 1616 | if (tgroup < 1 || tgroup > MAX_TGROUPS) { |
| 1617 | memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup); |
| 1618 | return -1; |
| 1619 | } |
| 1620 | |
| 1621 | /* look for a preliminary definition of any thread pointing to this |
| 1622 | * group, and remove them. |
| 1623 | */ |
| 1624 | if (ha_tgroup_info[tgroup-1].count) { |
| 1625 | ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n", |
| 1626 | file, line, args[0], tgroup); |
| 1627 | |
| 1628 | for (tnum = ha_tgroup_info[tgroup-1].base; |
| 1629 | tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count; |
| 1630 | tnum++) { |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1631 | if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) { |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1632 | ha_thread_info[tnum-1].tg = NULL; |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1633 | ha_thread_info[tnum-1].tgid = 0; |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 1634 | ha_thread_info[tnum-1].tg_ctx = NULL; |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1635 | } |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1636 | } |
| 1637 | ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0; |
| 1638 | } |
| 1639 | |
| 1640 | tot = 0; |
| 1641 | for (arg = 2; args[arg] && *args[arg]; arg++) { |
| 1642 | tend = tnum = strtol(args[arg], &errptr, 10); |
| 1643 | |
| 1644 | if (*errptr == '-') |
| 1645 | tend = strtol(errptr + 1, &errptr, 10); |
| 1646 | |
| 1647 | if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) { |
| 1648 | memprintf(err, "'%s %ld' passed an unparsable or invalid thread number '%s' (valid range is 1 to %d)", args[0], tgroup, args[arg], MAX_THREADS); |
| 1649 | return -1; |
| 1650 | } |
| 1651 | |
| 1652 | for(; tnum <= tend; tnum++) { |
| 1653 | if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) { |
| 1654 | ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n", |
| 1655 | file, line, args[0], tgroup, tnum); |
| 1656 | } else if (ha_thread_info[tnum-1].tg) { |
| 1657 | ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n", |
| 1658 | file, line, args[0], tgroup, tnum, |
| 1659 | (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1)); |
| 1660 | } |
| 1661 | |
| 1662 | if (!ha_tgroup_info[tgroup-1].count) { |
| 1663 | ha_tgroup_info[tgroup-1].base = tnum-1; |
| 1664 | ha_tgroup_info[tgroup-1].count = 1; |
| 1665 | } |
| 1666 | else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) { |
| 1667 | ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base; |
| 1668 | } |
| 1669 | else if (tnum < ha_tgroup_info[tgroup-1].base) { |
| 1670 | ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1; |
| 1671 | ha_tgroup_info[tgroup-1].base = tnum - 1; |
| 1672 | } |
| 1673 | |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1674 | ha_thread_info[tnum-1].tgid = tgroup; |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1675 | ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1]; |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 1676 | ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1]; |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1677 | tot++; |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | if (ha_tgroup_info[tgroup-1].count > tot) { |
| 1682 | memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup); |
| 1683 | return -1; |
| 1684 | } |
| 1685 | |
| 1686 | if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) { |
| 1687 | memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP); |
| 1688 | return -1; |
| 1689 | } |
| 1690 | |
| 1691 | return 0; |
| 1692 | } |
| 1693 | |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1694 | /* Parse the "thread-groups" global directive, which takes an integer argument |
| 1695 | * that contains the desired number of thread groups. |
| 1696 | */ |
| 1697 | static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx, |
| 1698 | const struct proxy *defpx, const char *file, int line, |
| 1699 | char **err) |
| 1700 | { |
| 1701 | long nbtgroups; |
| 1702 | char *errptr; |
| 1703 | |
| 1704 | if (too_many_args(1, args, err, NULL)) |
| 1705 | return -1; |
Christopher Faulet | 5534334 | 2022-11-18 15:52:58 +0100 | [diff] [blame] | 1706 | |
| 1707 | if (non_global_section_parsed == 1) { |
| 1708 | memprintf(err, "'%s' not allowed if a non-global section was previously defined. This parameter must be declared in the first global section", args[0]); |
| 1709 | return -1; |
| 1710 | } |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1711 | |
| 1712 | nbtgroups = strtol(args[1], &errptr, 10); |
| 1713 | if (!*args[1] || *errptr) { |
| 1714 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1715 | return -1; |
| 1716 | } |
| 1717 | |
| 1718 | #ifndef USE_THREAD |
| 1719 | if (nbtgroups != 1) { |
| 1720 | memprintf(err, "'%s' specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD", args[0]); |
| 1721 | return -1; |
| 1722 | } |
| 1723 | #else |
| 1724 | if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) { |
| 1725 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups); |
| 1726 | return -1; |
| 1727 | } |
| 1728 | #endif |
| 1729 | |
| 1730 | HA_DIAG_WARNING_COND(global.nbtgroups, |
| 1731 | "parsing [%s:%d] : '%s' is already defined and will be overridden.\n", |
| 1732 | file, line, args[0]); |
| 1733 | |
| 1734 | global.nbtgroups = nbtgroups; |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1738 | /* config keyword parsers */ |
| 1739 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1740 | { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 }, |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1741 | { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 }, |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1742 | { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 }, |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1743 | { 0, NULL, NULL } |
| 1744 | }}; |
| 1745 | |
| 1746 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |