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 | #include <fcntl.h> |
| 17 | |
Willy Tarreau | aa99276 | 2021-10-06 23:33:20 +0200 | [diff] [blame] | 18 | #include <signal.h> |
| 19 | #include <unistd.h> |
| 20 | #ifdef _POSIX_PRIORITY_SCHEDULING |
| 21 | #include <sched.h> |
| 22 | #endif |
| 23 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 24 | #ifdef USE_THREAD |
| 25 | # include <pthread.h> |
| 26 | #endif |
| 27 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 28 | #ifdef USE_CPU_AFFINITY |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 29 | # include <sched.h> |
| 30 | # if defined(__FreeBSD__) || defined(__DragonFly__) |
| 31 | # include <sys/param.h> |
| 32 | # ifdef __FreeBSD__ |
| 33 | # include <sys/cpuset.h> |
| 34 | # endif |
| 35 | # include <pthread_np.h> |
| 36 | # endif |
| 37 | # ifdef __APPLE__ |
| 38 | # include <mach/mach_types.h> |
| 39 | # include <mach/thread_act.h> |
| 40 | # include <mach/thread_policy.h> |
| 41 | # endif |
| 42 | # include <haproxy/cpuset.h> |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 43 | #endif |
| 44 | |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 45 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 46 | #include <haproxy/clock.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 47 | #include <haproxy/fd.h> |
| 48 | #include <haproxy/global.h> |
Willy Tarreau | 11bd6f7 | 2021-05-08 20:33:02 +0200 | [diff] [blame] | 49 | #include <haproxy/log.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 50 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 51 | #include <haproxy/tools.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 52 | |
Willy Tarreau | f966284 | 2021-09-13 18:11:26 +0200 | [diff] [blame] | 53 | struct tgroup_info ha_tgroup_info[MAX_TGROUPS] = { }; |
| 54 | THREAD_LOCAL const struct tgroup_info *tg = &ha_tgroup_info[0]; |
| 55 | |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 56 | struct thread_info ha_thread_info[MAX_THREADS] = { }; |
Willy Tarreau | 6036342 | 2021-10-01 16:29:27 +0200 | [diff] [blame] | 57 | THREAD_LOCAL const struct thread_info *ti = &ha_thread_info[0]; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 58 | |
Willy Tarreau | 1a9c922 | 2021-10-01 11:30:33 +0200 | [diff] [blame] | 59 | struct thread_ctx ha_thread_ctx[MAX_THREADS] = { }; |
| 60 | THREAD_LOCAL struct thread_ctx *th_ctx = &ha_thread_ctx[0]; |
| 61 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 62 | #ifdef USE_THREAD |
| 63 | |
Willy Tarreau | 56c3b8b | 2021-04-10 17:28:18 +0200 | [diff] [blame] | 64 | volatile unsigned long threads_want_rdv_mask __read_mostly = 0; |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 65 | volatile unsigned long threads_harmless_mask = 0; |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 66 | volatile unsigned long threads_idle_mask = 0; |
Willy Tarreau | 9a1f573 | 2019-06-09 12:20:02 +0200 | [diff] [blame] | 67 | volatile unsigned long threads_sync_mask = 0; |
Willy Tarreau | 56c3b8b | 2021-04-10 17:28:18 +0200 | [diff] [blame] | 68 | volatile unsigned long all_threads_mask __read_mostly = 1; // nbthread 1 assumed by default |
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; |
| 71 | THREAD_LOCAL unsigned long tid_bit = (1UL << 0); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 72 | int thread_cpus_enabled_at_boot = 1; |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 73 | static pthread_t ha_pthread[MAX_THREADS] = { }; |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 74 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 75 | /* Marks the thread as harmless until the last thread using the rendez-vous |
Christopher Faulet | a9a9e9a | 2021-03-25 14:11:36 +0100 | [diff] [blame] | 76 | * point quits, excluding the current one. Thus an isolated thread may be safely |
| 77 | * marked as harmless. Given that we can wait for a long time, sched_yield() is |
| 78 | * used when available to offer the CPU resources to competing threads if |
| 79 | * needed. |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 80 | */ |
| 81 | void thread_harmless_till_end() |
| 82 | { |
Willy Tarreau | 286363b | 2021-08-04 10:33:57 +0200 | [diff] [blame] | 83 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 84 | while (threads_want_rdv_mask & all_threads_mask & ~tid_bit) { |
| 85 | ha_thread_relax(); |
| 86 | } |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* Isolates the current thread : request the ability to work while all other |
Willy Tarreau | f519cfa | 2021-08-04 11:22:07 +0200 | [diff] [blame] | 90 | * threads are harmless, as defined by thread_harmless_now() (i.e. they're not |
| 91 | * going to touch any visible memory area). Only returns once all of them are |
| 92 | * harmless, with the current thread's bit in threads_harmless_mask cleared. |
| 93 | * Needs to be completed using thread_release(). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 94 | */ |
| 95 | void thread_isolate() |
| 96 | { |
| 97 | unsigned long old; |
| 98 | |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 99 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 100 | __ha_barrier_atomic_store(); |
| 101 | _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 102 | |
| 103 | /* wait for all threads to become harmless */ |
| 104 | old = threads_harmless_mask; |
| 105 | while (1) { |
| 106 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 107 | old = threads_harmless_mask; |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 108 | else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 109 | break; |
| 110 | |
Willy Tarreau | 38171da | 2019-05-17 16:33:13 +0200 | [diff] [blame] | 111 | ha_thread_relax(); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 112 | } |
| 113 | /* one thread gets released at a time here, with its harmess bit off. |
| 114 | * The loss of this bit makes the other one continue to spin while the |
| 115 | * thread is working alone. |
| 116 | */ |
| 117 | } |
| 118 | |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 119 | /* Isolates the current thread : request the ability to work while all other |
| 120 | * threads are idle, as defined by thread_idle_now(). It only returns once |
| 121 | * all of them are both harmless and idle, with the current thread's bit in |
| 122 | * threads_harmless_mask and idle_mask cleared. Needs to be completed using |
| 123 | * thread_release(). By doing so the thread also engages in being safe against |
| 124 | * any actions that other threads might be about to start under the same |
| 125 | * conditions. This specifically targets destruction of any internal structure, |
| 126 | * which implies that the current thread may not hold references to any object. |
| 127 | * |
| 128 | * Note that a concurrent thread_isolate() will usually win against |
| 129 | * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to |
| 130 | * get back to the poller or any other fully idle location, that will |
| 131 | * ultimately release this one. |
| 132 | */ |
| 133 | void thread_isolate_full() |
| 134 | { |
| 135 | unsigned long old; |
| 136 | |
| 137 | _HA_ATOMIC_OR(&threads_idle_mask, tid_bit); |
| 138 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 139 | __ha_barrier_atomic_store(); |
| 140 | _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
| 141 | |
| 142 | /* wait for all threads to become harmless */ |
| 143 | old = threads_harmless_mask; |
| 144 | while (1) { |
| 145 | unsigned long idle = _HA_ATOMIC_LOAD(&threads_idle_mask); |
| 146 | |
| 147 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 148 | old = _HA_ATOMIC_LOAD(&threads_harmless_mask); |
| 149 | else if ((idle & all_threads_mask) == all_threads_mask && |
| 150 | _HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
| 151 | break; |
| 152 | |
| 153 | ha_thread_relax(); |
| 154 | } |
| 155 | |
| 156 | /* we're not idle anymore at this point. Other threads waiting on this |
| 157 | * condition will need to wait until out next pass to the poller, or |
| 158 | * our next call to thread_isolate_full(). |
| 159 | */ |
| 160 | _HA_ATOMIC_AND(&threads_idle_mask, ~tid_bit); |
| 161 | } |
| 162 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 163 | /* Cancels the effect of thread_isolate() by releasing the current thread's bit |
Willy Tarreau | f519cfa | 2021-08-04 11:22:07 +0200 | [diff] [blame] | 164 | * in threads_want_rdv_mask. This immediately allows other threads to expect be |
| 165 | * executed, though they will first have to wait for this thread to become |
| 166 | * harmless again (possibly by reaching the poller again). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 167 | */ |
| 168 | void thread_release() |
| 169 | { |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 170 | _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 171 | } |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 172 | |
Willy Tarreau | 9a1f573 | 2019-06-09 12:20:02 +0200 | [diff] [blame] | 173 | /* Cancels the effect of thread_isolate() by releasing the current thread's bit |
| 174 | * in threads_want_rdv_mask and by marking this thread as harmless until the |
| 175 | * last worker finishes. The difference with thread_release() is that this one |
| 176 | * will not leave the function before others are notified to do the same, so it |
| 177 | * guarantees that the current thread will not pass through a subsequent call |
| 178 | * to thread_isolate() before others finish. |
| 179 | */ |
| 180 | void thread_sync_release() |
| 181 | { |
| 182 | _HA_ATOMIC_OR(&threads_sync_mask, tid_bit); |
| 183 | __ha_barrier_atomic_store(); |
| 184 | _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
| 185 | |
| 186 | while (threads_want_rdv_mask & all_threads_mask) { |
| 187 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 188 | while (threads_want_rdv_mask & all_threads_mask) |
| 189 | ha_thread_relax(); |
| 190 | HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit); |
| 191 | } |
| 192 | |
| 193 | /* the current thread is not harmless anymore, thread_isolate() |
| 194 | * is forced to wait till all waiters finish. |
| 195 | */ |
| 196 | _HA_ATOMIC_AND(&threads_sync_mask, ~tid_bit); |
| 197 | while (threads_sync_mask & all_threads_mask) |
| 198 | ha_thread_relax(); |
| 199 | } |
| 200 | |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 201 | /* Sets up threads, signals and masks, and starts threads 2 and above. |
| 202 | * Does nothing when threads are disabled. |
| 203 | */ |
| 204 | void setup_extra_threads(void *(*handler)(void *)) |
| 205 | { |
| 206 | sigset_t blocked_sig, old_sig; |
| 207 | int i; |
| 208 | |
| 209 | /* ensure the signals will be blocked in every thread */ |
| 210 | sigfillset(&blocked_sig); |
| 211 | sigdelset(&blocked_sig, SIGPROF); |
| 212 | sigdelset(&blocked_sig, SIGBUS); |
| 213 | sigdelset(&blocked_sig, SIGFPE); |
| 214 | sigdelset(&blocked_sig, SIGILL); |
| 215 | sigdelset(&blocked_sig, SIGSEGV); |
| 216 | pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig); |
| 217 | |
| 218 | /* Create nbthread-1 thread. The first thread is the current process */ |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 219 | ha_pthread[0] = pthread_self(); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 220 | for (i = 1; i < global.nbthread; i++) |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 221 | pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* waits for all threads to terminate. Does nothing when threads are |
| 225 | * disabled. |
| 226 | */ |
| 227 | void wait_for_threads_completion() |
| 228 | { |
| 229 | int i; |
| 230 | |
| 231 | /* Wait the end of other threads */ |
| 232 | for (i = 1; i < global.nbthread; i++) |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 233 | pthread_join(ha_pthread[i], NULL); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 234 | |
| 235 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 236 | show_lock_stats(); |
| 237 | #endif |
| 238 | } |
| 239 | |
| 240 | /* Tries to set the current thread's CPU affinity according to the cpu_map */ |
| 241 | void set_thread_cpu_affinity() |
| 242 | { |
| 243 | #if defined(USE_CPU_AFFINITY) |
| 244 | /* no affinity setting for the master process */ |
| 245 | if (master) |
| 246 | return; |
| 247 | |
| 248 | /* Now the CPU affinity for all threads */ |
| 249 | if (ha_cpuset_count(&cpu_map.proc)) |
| 250 | ha_cpuset_and(&cpu_map.thread[tid], &cpu_map.proc); |
| 251 | |
| 252 | if (ha_cpuset_count(&cpu_map.thread[tid])) {/* only do this if the thread has a THREAD map */ |
| 253 | # if defined(__APPLE__) |
| 254 | /* Note: this API is limited to the first 32/64 CPUs */ |
| 255 | unsigned long set = cpu_map.thread[tid].cpuset; |
| 256 | int j; |
| 257 | |
| 258 | while ((j = ffsl(set)) > 0) { |
| 259 | thread_affinity_policy_data_t cpu_set = { j - 1 }; |
| 260 | thread_port_t mthread; |
| 261 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 262 | mthread = pthread_mach_thread_np(ha_pthread[tid]); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 263 | thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1); |
| 264 | set &= ~(1UL << (j - 1)); |
| 265 | } |
| 266 | # else |
| 267 | struct hap_cpuset *set = &cpu_map.thread[tid]; |
| 268 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 269 | pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 270 | # endif |
| 271 | } |
| 272 | #endif /* USE_CPU_AFFINITY */ |
| 273 | } |
| 274 | |
Willy Tarreau | 4eeb883 | 2021-10-06 22:44:28 +0200 | [diff] [blame] | 275 | /* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long |
| 276 | * since POSIX took great care of not specifying its representation, making it |
| 277 | * hard to export for post-mortem analysis. For this reason we copy it into a |
| 278 | * union and will use the smallest scalar type at least as large as its size, |
| 279 | * which will keep endianness and alignment for all regular sizes. As a last |
| 280 | * resort we end up with a long long ligned to the first bytes in memory, which |
| 281 | * will be endian-dependent if pthread_t is larger than a long long (not seen |
| 282 | * yet). |
| 283 | */ |
| 284 | unsigned long long ha_get_pthread_id(unsigned int thr) |
| 285 | { |
| 286 | union { |
| 287 | pthread_t t; |
| 288 | unsigned long long ll; |
| 289 | unsigned int i; |
| 290 | unsigned short s; |
| 291 | unsigned char c; |
| 292 | } u = { 0 }; |
| 293 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 294 | u.t = ha_pthread[thr]; |
Willy Tarreau | 4eeb883 | 2021-10-06 22:44:28 +0200 | [diff] [blame] | 295 | |
| 296 | if (sizeof(u.t) <= sizeof(u.c)) |
| 297 | return u.c; |
| 298 | else if (sizeof(u.t) <= sizeof(u.s)) |
| 299 | return u.s; |
| 300 | else if (sizeof(u.t) <= sizeof(u.i)) |
| 301 | return u.i; |
| 302 | return u.ll; |
| 303 | } |
| 304 | |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 305 | /* send signal <sig> to thread <thr> */ |
| 306 | void ha_tkill(unsigned int thr, int sig) |
| 307 | { |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 308 | pthread_kill(ha_pthread[thr], sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* send signal <sig> to all threads. The calling thread is signaled last in |
| 312 | * order to allow all threads to synchronize in the handler. |
| 313 | */ |
| 314 | void ha_tkillall(int sig) |
| 315 | { |
| 316 | unsigned int thr; |
| 317 | |
| 318 | for (thr = 0; thr < global.nbthread; thr++) { |
| 319 | if (!(all_threads_mask & (1UL << thr))) |
| 320 | continue; |
| 321 | if (thr == tid) |
| 322 | continue; |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame] | 323 | pthread_kill(ha_pthread[thr], sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 324 | } |
| 325 | raise(sig); |
| 326 | } |
| 327 | |
Willy Tarreau | aa99276 | 2021-10-06 23:33:20 +0200 | [diff] [blame] | 328 | void ha_thread_relax(void) |
| 329 | { |
| 330 | #ifdef _POSIX_PRIORITY_SCHEDULING |
| 331 | sched_yield(); |
| 332 | #else |
| 333 | pl_cpu_relax(); |
| 334 | #endif |
| 335 | } |
| 336 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 337 | /* 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] | 338 | void ha_spin_init(HA_SPINLOCK_T *l) |
| 339 | { |
| 340 | HA_SPIN_INIT(l); |
| 341 | } |
| 342 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 343 | /* 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] | 344 | void ha_rwlock_init(HA_RWLOCK_T *l) |
| 345 | { |
| 346 | HA_RWLOCK_INIT(l); |
| 347 | } |
| 348 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 349 | /* returns the number of CPUs the current process is enabled to run on */ |
| 350 | static int thread_cpus_enabled() |
| 351 | { |
| 352 | int ret = 1; |
| 353 | |
| 354 | #ifdef USE_CPU_AFFINITY |
| 355 | #if defined(__linux__) && defined(CPU_COUNT) |
| 356 | cpu_set_t mask; |
| 357 | |
| 358 | if (sched_getaffinity(0, sizeof(mask), &mask) == 0) |
| 359 | ret = CPU_COUNT(&mask); |
Olivier Houchard | 46453d3 | 2019-04-11 00:06:47 +0200 | [diff] [blame] | 360 | #elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY) |
| 361 | cpuset_t cpuset; |
| 362 | if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, |
| 363 | sizeof(cpuset), &cpuset) == 0) |
| 364 | ret = CPU_COUNT(&cpuset); |
David CARLIER | 6a90601 | 2021-01-15 08:09:56 +0000 | [diff] [blame] | 365 | #elif defined(__APPLE__) |
| 366 | ret = (int)sysconf(_SC_NPROCESSORS_ONLN); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 367 | #endif |
| 368 | #endif |
| 369 | ret = MAX(ret, 1); |
| 370 | ret = MIN(ret, MAX_THREADS); |
| 371 | return ret; |
| 372 | } |
| 373 | |
Amaury Denoyelle | 4c9efde | 2021-03-31 16:57:39 +0200 | [diff] [blame] | 374 | /* Returns 1 if the cpu set is currently restricted for the process else 0. |
| 375 | * Currently only implemented for the Linux platform. |
| 376 | */ |
| 377 | int thread_cpu_mask_forced() |
| 378 | { |
| 379 | #if defined(__linux__) |
| 380 | const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN); |
| 381 | return cpus_avail != thread_cpus_enabled(); |
| 382 | #else |
| 383 | return 0; |
| 384 | #endif |
| 385 | } |
| 386 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 387 | /* Below come the lock-debugging functions */ |
| 388 | |
| 389 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 390 | |
| 391 | struct lock_stat lock_stats[LOCK_LABELS]; |
| 392 | |
| 393 | /* this is only used below */ |
| 394 | static const char *lock_label(enum lock_label label) |
| 395 | { |
| 396 | switch (label) { |
| 397 | case TASK_RQ_LOCK: return "TASK_RQ"; |
| 398 | case TASK_WQ_LOCK: return "TASK_WQ"; |
| 399 | case LISTENER_LOCK: return "LISTENER"; |
| 400 | case PROXY_LOCK: return "PROXY"; |
| 401 | case SERVER_LOCK: return "SERVER"; |
| 402 | case LBPRM_LOCK: return "LBPRM"; |
| 403 | case SIGNALS_LOCK: return "SIGNALS"; |
| 404 | case STK_TABLE_LOCK: return "STK_TABLE"; |
| 405 | case STK_SESS_LOCK: return "STK_SESS"; |
| 406 | case APPLETS_LOCK: return "APPLETS"; |
| 407 | case PEER_LOCK: return "PEER"; |
| 408 | case SHCTX_LOCK: return "SHCTX"; |
| 409 | case SSL_LOCK: return "SSL"; |
| 410 | case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS"; |
| 411 | case PATREF_LOCK: return "PATREF"; |
| 412 | case PATEXP_LOCK: return "PATEXP"; |
| 413 | case VARS_LOCK: return "VARS"; |
| 414 | case COMP_POOL_LOCK: return "COMP_POOL"; |
| 415 | case LUA_LOCK: return "LUA"; |
| 416 | case NOTIF_LOCK: return "NOTIF"; |
| 417 | case SPOE_APPLET_LOCK: return "SPOE_APPLET"; |
| 418 | case DNS_LOCK: return "DNS"; |
| 419 | case PID_LIST_LOCK: return "PID_LIST"; |
| 420 | case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS"; |
| 421 | case PIPES_LOCK: return "PIPES"; |
| 422 | case TLSKEYS_REF_LOCK: return "TLSKEYS_REF"; |
| 423 | case AUTH_LOCK: return "AUTH"; |
| 424 | case LOGSRV_LOCK: return "LOGSRV"; |
| 425 | case DICT_LOCK: return "DICT"; |
| 426 | case PROTO_LOCK: return "PROTO"; |
| 427 | case QUEUE_LOCK: return "QUEUE"; |
| 428 | case CKCH_LOCK: return "CKCH"; |
| 429 | case SNI_LOCK: return "SNI"; |
| 430 | case SSL_SERVER_LOCK: return "SSL_SERVER"; |
| 431 | case SFT_LOCK: return "SFT"; |
| 432 | case IDLE_CONNS_LOCK: return "IDLE_CONNS"; |
| 433 | case QUIC_LOCK: return "QUIC"; |
| 434 | case OTHER_LOCK: return "OTHER"; |
| 435 | case DEBUG1_LOCK: return "DEBUG1"; |
| 436 | case DEBUG2_LOCK: return "DEBUG2"; |
| 437 | case DEBUG3_LOCK: return "DEBUG3"; |
| 438 | case DEBUG4_LOCK: return "DEBUG4"; |
| 439 | case DEBUG5_LOCK: return "DEBUG5"; |
| 440 | case LOCK_LABELS: break; /* keep compiler happy */ |
| 441 | }; |
| 442 | /* only way to come here is consecutive to an internal bug */ |
| 443 | abort(); |
| 444 | } |
| 445 | |
| 446 | void show_lock_stats() |
| 447 | { |
| 448 | int lbl; |
| 449 | |
| 450 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 451 | if (!lock_stats[lbl].num_write_locked && |
| 452 | !lock_stats[lbl].num_seek_locked && |
| 453 | !lock_stats[lbl].num_read_locked) { |
| 454 | fprintf(stderr, |
| 455 | "Stats about Lock %s: not used\n", |
| 456 | lock_label(lbl)); |
| 457 | continue; |
| 458 | } |
| 459 | |
| 460 | fprintf(stderr, |
| 461 | "Stats about Lock %s: \n", |
| 462 | lock_label(lbl)); |
| 463 | |
| 464 | if (lock_stats[lbl].num_write_locked) |
| 465 | fprintf(stderr, |
| 466 | "\t # write lock : %lu\n" |
| 467 | "\t # write unlock: %lu (%ld)\n" |
| 468 | "\t # wait time for write : %.3f msec\n" |
| 469 | "\t # wait time for write/lock: %.3f nsec\n", |
| 470 | lock_stats[lbl].num_write_locked, |
| 471 | lock_stats[lbl].num_write_unlocked, |
| 472 | lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked, |
| 473 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 474 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0); |
| 475 | |
| 476 | if (lock_stats[lbl].num_seek_locked) |
| 477 | fprintf(stderr, |
| 478 | "\t # seek lock : %lu\n" |
| 479 | "\t # seek unlock : %lu (%ld)\n" |
| 480 | "\t # wait time for seek : %.3f msec\n" |
| 481 | "\t # wait time for seek/lock : %.3f nsec\n", |
| 482 | lock_stats[lbl].num_seek_locked, |
| 483 | lock_stats[lbl].num_seek_unlocked, |
| 484 | lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked, |
| 485 | (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0, |
| 486 | lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0); |
| 487 | |
| 488 | if (lock_stats[lbl].num_read_locked) |
| 489 | fprintf(stderr, |
| 490 | "\t # read lock : %lu\n" |
| 491 | "\t # read unlock : %lu (%ld)\n" |
| 492 | "\t # wait time for read : %.3f msec\n" |
| 493 | "\t # wait time for read/lock : %.3f nsec\n", |
| 494 | lock_stats[lbl].num_read_locked, |
| 495 | lock_stats[lbl].num_read_unlocked, |
| 496 | lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked, |
| 497 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 498 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 499 | } |
| 500 | } |
| 501 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 502 | void __ha_rwlock_init(struct ha_rwlock *l) |
| 503 | { |
| 504 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 505 | __RWLOCK_INIT(&l->lock); |
| 506 | } |
| 507 | |
| 508 | void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 509 | { |
| 510 | __RWLOCK_DESTROY(&l->lock); |
| 511 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 512 | } |
| 513 | |
| 514 | |
| 515 | void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 516 | const char *func, const char *file, int line) |
| 517 | { |
| 518 | uint64_t start_time; |
| 519 | |
| 520 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 521 | abort(); |
| 522 | |
| 523 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 524 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 525 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 526 | __RWLOCK_WRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 527 | 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] | 528 | |
| 529 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 530 | |
| 531 | l->info.cur_writer = tid_bit; |
| 532 | l->info.last_location.function = func; |
| 533 | l->info.last_location.file = file; |
| 534 | l->info.last_location.line = line; |
| 535 | |
| 536 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 537 | } |
| 538 | |
| 539 | int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 540 | const char *func, const char *file, int line) |
| 541 | { |
| 542 | uint64_t start_time; |
| 543 | int r; |
| 544 | |
| 545 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 546 | abort(); |
| 547 | |
| 548 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
| 549 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 550 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 551 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 552 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 553 | 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] | 554 | if (unlikely(r)) { |
| 555 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 556 | return r; |
| 557 | } |
| 558 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 559 | |
| 560 | l->info.cur_writer = tid_bit; |
| 561 | l->info.last_location.function = func; |
| 562 | l->info.last_location.file = file; |
| 563 | l->info.last_location.line = line; |
| 564 | |
| 565 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 571 | const char *func, const char *file, int line) |
| 572 | { |
| 573 | if (unlikely(!(l->info.cur_writer & tid_bit))) { |
| 574 | /* the thread is not owning the lock for write */ |
| 575 | abort(); |
| 576 | } |
| 577 | |
| 578 | l->info.cur_writer = 0; |
| 579 | l->info.last_location.function = func; |
| 580 | l->info.last_location.file = file; |
| 581 | l->info.last_location.line = line; |
| 582 | |
| 583 | __RWLOCK_WRUNLOCK(&l->lock); |
| 584 | |
| 585 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 586 | } |
| 587 | |
| 588 | void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 589 | { |
| 590 | uint64_t start_time; |
| 591 | |
| 592 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 593 | abort(); |
| 594 | |
| 595 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 596 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 597 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 598 | __RWLOCK_RDLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 599 | 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] | 600 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 601 | |
| 602 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 603 | |
| 604 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 605 | } |
| 606 | |
| 607 | int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 608 | { |
| 609 | int r; |
| 610 | |
| 611 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 612 | abort(); |
| 613 | |
| 614 | /* try read should never wait */ |
| 615 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 616 | if (unlikely(r)) |
| 617 | return r; |
| 618 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 619 | |
| 620 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 626 | { |
| 627 | if (unlikely(!(l->info.cur_readers & tid_bit))) { |
| 628 | /* the thread is not owning the lock for read */ |
| 629 | abort(); |
| 630 | } |
| 631 | |
| 632 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 633 | |
| 634 | __RWLOCK_RDUNLOCK(&l->lock); |
| 635 | |
| 636 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked); |
| 637 | } |
| 638 | |
| 639 | void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l, |
| 640 | const char *func, const char *file, int line) |
| 641 | { |
| 642 | uint64_t start_time; |
| 643 | |
| 644 | if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit) |
| 645 | abort(); |
| 646 | |
| 647 | if (!(l->info.cur_writer & tid_bit)) |
| 648 | abort(); |
| 649 | |
| 650 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 651 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 652 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 653 | __RWLOCK_WRTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 654 | 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] | 655 | |
| 656 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 657 | |
| 658 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 659 | HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit); |
| 660 | l->info.last_location.function = func; |
| 661 | l->info.last_location.file = file; |
| 662 | l->info.last_location.line = line; |
| 663 | |
| 664 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 665 | } |
| 666 | |
| 667 | void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 668 | const char *func, const char *file, int line) |
| 669 | { |
| 670 | uint64_t start_time; |
| 671 | |
| 672 | if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit) |
| 673 | abort(); |
| 674 | |
| 675 | if (!(l->info.cur_writer & tid_bit)) |
| 676 | abort(); |
| 677 | |
| 678 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 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_WRTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 682 | 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] | 683 | |
| 684 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 685 | |
| 686 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 687 | HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit); |
| 688 | l->info.last_location.function = func; |
| 689 | l->info.last_location.file = file; |
| 690 | l->info.last_location.line = line; |
| 691 | |
| 692 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 693 | } |
| 694 | |
| 695 | void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l, |
| 696 | const char *func, const char *file, int line) |
| 697 | { |
| 698 | uint64_t start_time; |
| 699 | |
| 700 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 701 | abort(); |
| 702 | |
| 703 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 704 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 705 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 706 | __RWLOCK_SKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 707 | 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] | 708 | |
| 709 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 710 | |
| 711 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 712 | l->info.last_location.function = func; |
| 713 | l->info.last_location.file = file; |
| 714 | l->info.last_location.line = line; |
| 715 | |
| 716 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 717 | } |
| 718 | |
| 719 | void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l, |
| 720 | const char *func, const char *file, int line) |
| 721 | { |
| 722 | uint64_t start_time; |
| 723 | |
| 724 | if ((l->info.cur_readers | l->info.cur_writer) & tid_bit) |
| 725 | abort(); |
| 726 | |
| 727 | if (!(l->info.cur_seeker & tid_bit)) |
| 728 | abort(); |
| 729 | |
| 730 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 731 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 732 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 733 | __RWLOCK_SKTOWR(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 734 | 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] | 735 | |
| 736 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 737 | |
| 738 | HA_ATOMIC_OR(&l->info.cur_writer, tid_bit); |
| 739 | HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit); |
| 740 | l->info.last_location.function = func; |
| 741 | l->info.last_location.file = file; |
| 742 | l->info.last_location.line = line; |
| 743 | |
| 744 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 745 | } |
| 746 | |
| 747 | void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l, |
| 748 | const char *func, const char *file, int line) |
| 749 | { |
| 750 | uint64_t start_time; |
| 751 | |
| 752 | if ((l->info.cur_readers | l->info.cur_writer) & tid_bit) |
| 753 | abort(); |
| 754 | |
| 755 | if (!(l->info.cur_seeker & tid_bit)) |
| 756 | abort(); |
| 757 | |
| 758 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 759 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 760 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 761 | __RWLOCK_SKTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 762 | 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] | 763 | |
| 764 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 765 | |
| 766 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 767 | HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit); |
| 768 | l->info.last_location.function = func; |
| 769 | l->info.last_location.file = file; |
| 770 | l->info.last_location.line = line; |
| 771 | |
| 772 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 773 | } |
| 774 | |
| 775 | void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 776 | const char *func, const char *file, int line) |
| 777 | { |
| 778 | if (!(l->info.cur_seeker & tid_bit)) |
| 779 | abort(); |
| 780 | |
| 781 | HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit); |
| 782 | l->info.last_location.function = func; |
| 783 | l->info.last_location.file = file; |
| 784 | l->info.last_location.line = line; |
| 785 | |
| 786 | __RWLOCK_SKUNLOCK(&l->lock); |
| 787 | |
| 788 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked); |
| 789 | } |
| 790 | |
| 791 | int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l, |
| 792 | const char *func, const char *file, int line) |
| 793 | { |
| 794 | uint64_t start_time; |
| 795 | int r; |
| 796 | |
| 797 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 798 | abort(); |
| 799 | |
| 800 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 801 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 802 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 803 | r = __RWLOCK_TRYSKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 804 | 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] | 805 | |
| 806 | if (likely(!r)) { |
| 807 | /* got the lock ! */ |
| 808 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 809 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 810 | l->info.last_location.function = func; |
| 811 | l->info.last_location.file = file; |
| 812 | l->info.last_location.line = line; |
| 813 | } |
| 814 | |
| 815 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 816 | return r; |
| 817 | } |
| 818 | |
| 819 | int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 820 | const char *func, const char *file, int line) |
| 821 | { |
| 822 | uint64_t start_time; |
| 823 | int r; |
| 824 | |
| 825 | if ((l->info.cur_writer | l->info.cur_seeker) & tid_bit) |
| 826 | abort(); |
| 827 | |
| 828 | if (!(l->info.cur_readers & tid_bit)) |
| 829 | abort(); |
| 830 | |
| 831 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 832 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 833 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 834 | r = __RWLOCK_TRYRDTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 835 | 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] | 836 | |
| 837 | if (likely(!r)) { |
| 838 | /* got the lock ! */ |
| 839 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 840 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 841 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 842 | l->info.last_location.function = func; |
| 843 | l->info.last_location.file = file; |
| 844 | l->info.last_location.line = line; |
| 845 | } |
| 846 | |
| 847 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 848 | return r; |
| 849 | } |
| 850 | |
| 851 | void __spin_init(struct ha_spinlock *l) |
| 852 | { |
| 853 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 854 | __SPIN_INIT(&l->lock); |
| 855 | } |
| 856 | |
| 857 | void __spin_destroy(struct ha_spinlock *l) |
| 858 | { |
| 859 | __SPIN_DESTROY(&l->lock); |
| 860 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 861 | } |
| 862 | |
| 863 | void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 864 | const char *func, const char *file, int line) |
| 865 | { |
| 866 | uint64_t start_time; |
| 867 | |
| 868 | if (unlikely(l->info.owner & tid_bit)) { |
| 869 | /* the thread is already owning the lock */ |
| 870 | abort(); |
| 871 | } |
| 872 | |
| 873 | HA_ATOMIC_OR(&l->info.waiters, tid_bit); |
| 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 | __SPIN_LOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 877 | 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] | 878 | |
| 879 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 880 | |
| 881 | |
| 882 | l->info.owner = tid_bit; |
| 883 | l->info.last_location.function = func; |
| 884 | l->info.last_location.file = file; |
| 885 | l->info.last_location.line = line; |
| 886 | |
| 887 | HA_ATOMIC_AND(&l->info.waiters, ~tid_bit); |
| 888 | } |
| 889 | |
| 890 | int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 891 | const char *func, const char *file, int line) |
| 892 | { |
| 893 | int r; |
| 894 | |
| 895 | if (unlikely(l->info.owner & tid_bit)) { |
| 896 | /* the thread is already owning the lock */ |
| 897 | abort(); |
| 898 | } |
| 899 | |
| 900 | /* try read should never wait */ |
| 901 | r = __SPIN_TRYLOCK(&l->lock); |
| 902 | if (unlikely(r)) |
| 903 | return r; |
| 904 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 905 | |
| 906 | l->info.owner = tid_bit; |
| 907 | l->info.last_location.function = func; |
| 908 | l->info.last_location.file = file; |
| 909 | l->info.last_location.line = line; |
| 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 915 | const char *func, const char *file, int line) |
| 916 | { |
| 917 | if (unlikely(!(l->info.owner & tid_bit))) { |
| 918 | /* the thread is not owning the lock */ |
| 919 | abort(); |
| 920 | } |
| 921 | |
| 922 | l->info.owner = 0; |
| 923 | l->info.last_location.function = func; |
| 924 | l->info.last_location.file = file; |
| 925 | l->info.last_location.line = line; |
| 926 | |
| 927 | __SPIN_UNLOCK(&l->lock); |
| 928 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 929 | } |
| 930 | |
| 931 | #endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 932 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 933 | /* Depending on the platform and how libpthread was built, pthread_exit() may |
| 934 | * involve some code in libgcc_s that would be loaded on exit for the first |
| 935 | * time, causing aborts if the process is chrooted. It's harmless bit very |
| 936 | * dirty. There isn't much we can do to make sure libgcc_s is loaded only if |
| 937 | * needed, so what we do here is that during early boot we create a dummy |
| 938 | * thread that immediately exits. This will lead to libgcc_s being loaded |
| 939 | * during boot on the platforms where it's required. |
| 940 | */ |
| 941 | static void *dummy_thread_function(void *data) |
| 942 | { |
| 943 | pthread_exit(NULL); |
| 944 | return NULL; |
| 945 | } |
| 946 | |
| 947 | static inline void preload_libgcc_s(void) |
| 948 | { |
| 949 | pthread_t dummy_thread; |
| 950 | pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL); |
| 951 | pthread_join(dummy_thread, NULL); |
| 952 | } |
| 953 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 954 | __attribute__((constructor)) |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 955 | static void __thread_init(void) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 956 | { |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 957 | char *ptr = NULL; |
| 958 | |
| 959 | if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) { |
| 960 | ha_alert("MAX_THREADS value must be between 1 and %d inclusive; " |
| 961 | "HAProxy was built with value %d, please fix it and rebuild.\n", |
| 962 | LONGBITS, MAX_THREADS); |
| 963 | exit(1); |
| 964 | } |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 965 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 966 | preload_libgcc_s(); |
Willy Tarreau | 77b9822 | 2020-09-02 08:04:35 +0200 | [diff] [blame] | 967 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 968 | thread_cpus_enabled_at_boot = thread_cpus_enabled(); |
| 969 | |
| 970 | memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).", |
| 971 | MAX_THREADS, thread_cpus_enabled_at_boot); |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 972 | hap_register_build_opts(ptr, 1); |
| 973 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 974 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 975 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 976 | #endif |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 977 | } |
| 978 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 979 | #else |
| 980 | |
Willy Tarreau | aa99276 | 2021-10-06 23:33:20 +0200 | [diff] [blame] | 981 | /* send signal <sig> to thread <thr> (send to process in fact) */ |
| 982 | void ha_tkill(unsigned int thr, int sig) |
| 983 | { |
| 984 | raise(sig); |
| 985 | } |
| 986 | |
| 987 | /* send signal <sig> to all threads (send to process in fact) */ |
| 988 | void ha_tkillall(int sig) |
| 989 | { |
| 990 | raise(sig); |
| 991 | } |
| 992 | |
| 993 | void ha_thread_relax(void) |
| 994 | { |
| 995 | #ifdef _POSIX_PRIORITY_SCHEDULING |
| 996 | sched_yield(); |
| 997 | #endif |
| 998 | } |
| 999 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 1000 | REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set)."); |
| 1001 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1002 | #endif // USE_THREAD |
| 1003 | |
| 1004 | |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1005 | /* scans the configured thread mapping and establishes the final one. Returns <0 |
| 1006 | * on failure, >=0 on success. |
| 1007 | */ |
| 1008 | int thread_map_to_groups() |
| 1009 | { |
| 1010 | int t, g, ut, ug; |
| 1011 | int q, r; |
| 1012 | |
| 1013 | ut = ug = 0; // unassigned threads & groups |
| 1014 | |
| 1015 | for (t = 0; t < global.nbthread; t++) { |
| 1016 | if (!ha_thread_info[t].tg) |
| 1017 | ut++; |
| 1018 | } |
| 1019 | |
| 1020 | for (g = 0; g < global.nbtgroups; g++) { |
| 1021 | if (!ha_tgroup_info[g].count) |
| 1022 | ug++; |
Willy Tarreau | 6eee85f | 2021-09-28 08:50:02 +0200 | [diff] [blame] | 1023 | ha_tgroup_info[g].tgid = g + 1; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | if (ug > ut) { |
| 1027 | ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut); |
| 1028 | return -1; |
| 1029 | } |
| 1030 | |
| 1031 | /* look for first unassigned thread */ |
| 1032 | for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++) |
| 1033 | ; |
| 1034 | |
| 1035 | /* assign threads to empty groups */ |
| 1036 | for (g = 0; ug && ut; ) { |
| 1037 | /* due to sparse thread assignment we can end up with more threads |
| 1038 | * per group on last assigned groups than former ones, so we must |
| 1039 | * always try to pack the maximum remaining ones together first. |
| 1040 | */ |
| 1041 | q = ut / ug; |
| 1042 | r = ut % ug; |
| 1043 | if ((q + !!r) > MAX_THREADS_PER_GROUP) { |
| 1044 | 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); |
| 1045 | return -1; |
| 1046 | } |
| 1047 | |
| 1048 | /* thread <t> is the next unassigned one. Let's look for next |
| 1049 | * unassigned group, we know there are some left |
| 1050 | */ |
| 1051 | while (ut >= ug && ha_tgroup_info[g].count) |
| 1052 | g++; |
| 1053 | |
| 1054 | /* group g is unassigned, try to fill it with consecutive threads */ |
| 1055 | while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r && |
| 1056 | (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) { |
| 1057 | |
| 1058 | if (!ha_tgroup_info[g].count) { |
| 1059 | /* assign new group */ |
| 1060 | ha_tgroup_info[g].base = t; |
| 1061 | ug--; |
| 1062 | } |
| 1063 | |
| 1064 | ha_tgroup_info[g].count++; |
| 1065 | ha_thread_info[t].tg = &ha_tgroup_info[g]; |
| 1066 | |
| 1067 | ut--; |
| 1068 | /* switch to next unassigned thread */ |
| 1069 | while (++t < global.nbthread && ha_thread_info[t].tg) |
| 1070 | ; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | if (ut) { |
| 1075 | 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); |
| 1076 | return -1; |
| 1077 | } |
| 1078 | |
Willy Tarreau | cc7a11e | 2021-09-28 08:53:11 +0200 | [diff] [blame] | 1079 | for (t = 0; t < global.nbthread; t++) { |
| 1080 | ha_thread_info[t].tid = t; |
| 1081 | ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base; |
| 1082 | |
| 1083 | ha_thread_info[t].tid_bit = 1UL << ha_thread_info[t].tid; |
| 1084 | ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid; |
| 1085 | } |
| 1086 | |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1087 | return 0; |
| 1088 | } |
| 1089 | |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1090 | /* converts a configuration thread group+mask to a global group+mask depending on |
| 1091 | * the configured thread group id. This is essentially for use with the "thread" |
| 1092 | * directive on "bind" lines, where "thread 2/1-3" might be turned to "4-6" for |
| 1093 | * the global ID. It cannot be used before the thread mapping above was completed |
| 1094 | * and the thread group number configured. Possible options: |
| 1095 | * - igid == 0: imask represents global IDs. We have to check that all |
| 1096 | * configured threads in the mask belong to the same group. If imask is zero |
| 1097 | * it means everything, so for now we only support this with a single group. |
| 1098 | * - igid > 0, imask = 0: convert local values to global values for this thread |
| 1099 | * - igid > 0, imask > 0: convert local values to global values |
| 1100 | * |
| 1101 | * Returns <0 on failure, >=0 on success. |
| 1102 | */ |
| 1103 | int thread_resolve_group_mask(uint igid, ulong imask, uint *ogid, ulong *omask, char **err) |
| 1104 | { |
| 1105 | ulong mask; |
| 1106 | uint t; |
| 1107 | |
| 1108 | if (igid == 0) { |
| 1109 | /* unspecified group, IDs are global */ |
| 1110 | if (!imask) { |
| 1111 | /* all threads of all groups */ |
| 1112 | if (global.nbtgroups > 1) { |
| 1113 | memprintf(err, "'thread' directive spans multiple groups"); |
| 1114 | return -1; |
| 1115 | } |
| 1116 | mask = 0; |
| 1117 | *ogid = 1; // first and only group |
| 1118 | *omask = all_threads_mask; |
| 1119 | return 0; |
| 1120 | } else { |
| 1121 | /* some global threads */ |
| 1122 | imask &= all_threads_mask; |
| 1123 | for (t = 0; t < global.nbthread; t++) { |
| 1124 | if (imask & (1UL << t)) { |
| 1125 | if (ha_thread_info[t].tg->tgid != igid) { |
| 1126 | if (!igid) |
| 1127 | igid = ha_thread_info[t].tg->tgid; |
| 1128 | else { |
| 1129 | memprintf(err, "'thread' directive spans multiple groups (at least %u and %u)", igid, ha_thread_info[t].tg->tgid); |
| 1130 | return -1; |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | if (!igid) { |
| 1137 | memprintf(err, "'thread' directive contains threads that belong to no group"); |
| 1138 | return -1; |
| 1139 | } |
| 1140 | |
| 1141 | /* we have a valid group, convert this to global thread IDs */ |
| 1142 | *ogid = igid; |
| 1143 | *omask = imask << ha_tgroup_info[igid - 1].base; |
| 1144 | return 0; |
| 1145 | } |
| 1146 | } else { |
| 1147 | /* group was specified */ |
| 1148 | if (igid > global.nbtgroups) { |
| 1149 | memprintf(err, "'thread' directive references non-existing thread group %u", igid); |
| 1150 | return -1; |
| 1151 | } |
| 1152 | |
| 1153 | if (!imask) { |
| 1154 | /* all threads of this groups. Let's make a mask from their count and base. */ |
| 1155 | *ogid = igid; |
| 1156 | mask = 1UL << (ha_tgroup_info[igid - 1].count - 1); |
| 1157 | mask |= mask - 1; |
| 1158 | *omask = mask << ha_tgroup_info[igid - 1].base; |
| 1159 | return 0; |
| 1160 | } else { |
| 1161 | /* some local threads. Keep only existing ones for this group */ |
| 1162 | |
| 1163 | mask = 1UL << (ha_tgroup_info[igid - 1].count - 1); |
| 1164 | mask |= mask - 1; |
| 1165 | |
| 1166 | if (!(mask & imask)) { |
| 1167 | /* no intersection between the thread group's |
| 1168 | * threads and the bind line's. |
| 1169 | */ |
| 1170 | #ifdef THREAD_AUTO_ADJUST_GROUPS |
| 1171 | unsigned long new_mask = 0; |
| 1172 | |
| 1173 | while (imask) { |
| 1174 | new_mask |= imask & mask; |
| 1175 | imask >>= ha_tgroup_info[igid - 1].count; |
| 1176 | } |
| 1177 | imask = new_mask; |
| 1178 | #else |
| 1179 | memprintf(err, "'thread' directive only references threads not belonging to the group"); |
| 1180 | return -1; |
| 1181 | #endif |
| 1182 | } |
| 1183 | |
| 1184 | mask &= imask; |
| 1185 | *omask = mask << ha_tgroup_info[igid - 1].base; |
| 1186 | *ogid = igid; |
| 1187 | return 0; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1192 | /* Parse the "nbthread" global directive, which takes an integer argument that |
| 1193 | * contains the desired number of threads. |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1194 | */ |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1195 | static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx, |
| 1196 | const struct proxy *defpx, const char *file, int line, |
| 1197 | char **err) |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1198 | { |
| 1199 | long nbthread; |
| 1200 | char *errptr; |
| 1201 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1202 | if (too_many_args(1, args, err, NULL)) |
| 1203 | return -1; |
| 1204 | |
| 1205 | nbthread = strtol(args[1], &errptr, 10); |
| 1206 | if (!*args[1] || *errptr) { |
| 1207 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1208 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | #ifndef USE_THREAD |
| 1212 | if (nbthread != 1) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1213 | 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]); |
| 1214 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1215 | } |
| 1216 | #else |
| 1217 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1218 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread); |
| 1219 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1220 | } |
| 1221 | |
Willy Tarreau | fc64736 | 2019-02-02 17:05:03 +0100 | [diff] [blame] | 1222 | all_threads_mask = nbits(nbthread); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1223 | #endif |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1224 | |
| 1225 | HA_DIAG_WARNING_COND(global.nbthread, |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1226 | "parsing [%s:%d] : '%s' is already defined and will be overridden.\n", |
| 1227 | file, line, args[0]); |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1228 | |
| 1229 | global.nbthread = nbthread; |
| 1230 | return 0; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1231 | } |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1232 | |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1233 | /* Parse the "thread-group" global directive, which takes an integer argument |
| 1234 | * that designates a thread group, and a list of threads to put into that group. |
| 1235 | */ |
| 1236 | static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx, |
| 1237 | const struct proxy *defpx, const char *file, int line, |
| 1238 | char **err) |
| 1239 | { |
| 1240 | char *errptr; |
| 1241 | long tnum, tend, tgroup; |
| 1242 | int arg, tot; |
| 1243 | |
| 1244 | tgroup = strtol(args[1], &errptr, 10); |
| 1245 | if (!*args[1] || *errptr) { |
| 1246 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1247 | return -1; |
| 1248 | } |
| 1249 | |
| 1250 | if (tgroup < 1 || tgroup > MAX_TGROUPS) { |
| 1251 | memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup); |
| 1252 | return -1; |
| 1253 | } |
| 1254 | |
| 1255 | /* look for a preliminary definition of any thread pointing to this |
| 1256 | * group, and remove them. |
| 1257 | */ |
| 1258 | if (ha_tgroup_info[tgroup-1].count) { |
| 1259 | ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n", |
| 1260 | file, line, args[0], tgroup); |
| 1261 | |
| 1262 | for (tnum = ha_tgroup_info[tgroup-1].base; |
| 1263 | tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count; |
| 1264 | tnum++) { |
| 1265 | if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) |
| 1266 | ha_thread_info[tnum-1].tg = NULL; |
| 1267 | } |
| 1268 | ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0; |
| 1269 | } |
| 1270 | |
| 1271 | tot = 0; |
| 1272 | for (arg = 2; args[arg] && *args[arg]; arg++) { |
| 1273 | tend = tnum = strtol(args[arg], &errptr, 10); |
| 1274 | |
| 1275 | if (*errptr == '-') |
| 1276 | tend = strtol(errptr + 1, &errptr, 10); |
| 1277 | |
| 1278 | if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) { |
| 1279 | 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); |
| 1280 | return -1; |
| 1281 | } |
| 1282 | |
| 1283 | for(; tnum <= tend; tnum++) { |
| 1284 | if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) { |
| 1285 | ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n", |
| 1286 | file, line, args[0], tgroup, tnum); |
| 1287 | } else if (ha_thread_info[tnum-1].tg) { |
| 1288 | ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n", |
| 1289 | file, line, args[0], tgroup, tnum, |
| 1290 | (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1)); |
| 1291 | } |
| 1292 | |
| 1293 | if (!ha_tgroup_info[tgroup-1].count) { |
| 1294 | ha_tgroup_info[tgroup-1].base = tnum-1; |
| 1295 | ha_tgroup_info[tgroup-1].count = 1; |
| 1296 | } |
| 1297 | else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) { |
| 1298 | ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base; |
| 1299 | } |
| 1300 | else if (tnum < ha_tgroup_info[tgroup-1].base) { |
| 1301 | ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1; |
| 1302 | ha_tgroup_info[tgroup-1].base = tnum - 1; |
| 1303 | } |
| 1304 | |
| 1305 | ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1]; |
| 1306 | tot++; |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | if (ha_tgroup_info[tgroup-1].count > tot) { |
| 1311 | memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup); |
| 1312 | return -1; |
| 1313 | } |
| 1314 | |
| 1315 | if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) { |
| 1316 | memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP); |
| 1317 | return -1; |
| 1318 | } |
| 1319 | |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1323 | /* Parse the "thread-groups" global directive, which takes an integer argument |
| 1324 | * that contains the desired number of thread groups. |
| 1325 | */ |
| 1326 | static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx, |
| 1327 | const struct proxy *defpx, const char *file, int line, |
| 1328 | char **err) |
| 1329 | { |
| 1330 | long nbtgroups; |
| 1331 | char *errptr; |
| 1332 | |
| 1333 | if (too_many_args(1, args, err, NULL)) |
| 1334 | return -1; |
| 1335 | |
| 1336 | nbtgroups = strtol(args[1], &errptr, 10); |
| 1337 | if (!*args[1] || *errptr) { |
| 1338 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1339 | return -1; |
| 1340 | } |
| 1341 | |
| 1342 | #ifndef USE_THREAD |
| 1343 | if (nbtgroups != 1) { |
| 1344 | 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]); |
| 1345 | return -1; |
| 1346 | } |
| 1347 | #else |
| 1348 | if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) { |
| 1349 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups); |
| 1350 | return -1; |
| 1351 | } |
| 1352 | #endif |
| 1353 | |
| 1354 | HA_DIAG_WARNING_COND(global.nbtgroups, |
| 1355 | "parsing [%s:%d] : '%s' is already defined and will be overridden.\n", |
| 1356 | file, line, args[0]); |
| 1357 | |
| 1358 | global.nbtgroups = nbtgroups; |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1362 | /* config keyword parsers */ |
| 1363 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1364 | { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 }, |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1365 | { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 }, |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1366 | { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 }, |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1367 | { 0, NULL, NULL } |
| 1368 | }}; |
| 1369 | |
| 1370 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |