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 | f5b6327 | 2023-03-09 10:12:06 +0100 | [diff] [blame] | 360 | /* returns the number of CPUs the current process is enabled to run on, |
| 361 | * regardless of any MAX_THREADS limitation. |
| 362 | */ |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 363 | static int thread_cpus_enabled() |
| 364 | { |
| 365 | int ret = 1; |
| 366 | |
| 367 | #ifdef USE_CPU_AFFINITY |
| 368 | #if defined(__linux__) && defined(CPU_COUNT) |
| 369 | cpu_set_t mask; |
| 370 | |
| 371 | if (sched_getaffinity(0, sizeof(mask), &mask) == 0) |
| 372 | ret = CPU_COUNT(&mask); |
Olivier Houchard | 46453d3 | 2019-04-11 00:06:47 +0200 | [diff] [blame] | 373 | #elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY) |
| 374 | cpuset_t cpuset; |
| 375 | if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, |
| 376 | sizeof(cpuset), &cpuset) == 0) |
| 377 | ret = CPU_COUNT(&cpuset); |
David CARLIER | 6a90601 | 2021-01-15 08:09:56 +0000 | [diff] [blame] | 378 | #elif defined(__APPLE__) |
| 379 | ret = (int)sysconf(_SC_NPROCESSORS_ONLN); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 380 | #endif |
| 381 | #endif |
| 382 | ret = MAX(ret, 1); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 383 | return ret; |
| 384 | } |
| 385 | |
Amaury Denoyelle | 4c9efde | 2021-03-31 16:57:39 +0200 | [diff] [blame] | 386 | /* Returns 1 if the cpu set is currently restricted for the process else 0. |
| 387 | * Currently only implemented for the Linux platform. |
| 388 | */ |
| 389 | int thread_cpu_mask_forced() |
| 390 | { |
| 391 | #if defined(__linux__) |
| 392 | const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN); |
| 393 | return cpus_avail != thread_cpus_enabled(); |
| 394 | #else |
| 395 | return 0; |
| 396 | #endif |
| 397 | } |
| 398 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 399 | /* Below come the lock-debugging functions */ |
| 400 | |
| 401 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 402 | |
| 403 | struct lock_stat lock_stats[LOCK_LABELS]; |
| 404 | |
| 405 | /* this is only used below */ |
| 406 | static const char *lock_label(enum lock_label label) |
| 407 | { |
| 408 | switch (label) { |
| 409 | case TASK_RQ_LOCK: return "TASK_RQ"; |
| 410 | case TASK_WQ_LOCK: return "TASK_WQ"; |
| 411 | case LISTENER_LOCK: return "LISTENER"; |
| 412 | case PROXY_LOCK: return "PROXY"; |
| 413 | case SERVER_LOCK: return "SERVER"; |
| 414 | case LBPRM_LOCK: return "LBPRM"; |
| 415 | case SIGNALS_LOCK: return "SIGNALS"; |
| 416 | case STK_TABLE_LOCK: return "STK_TABLE"; |
| 417 | case STK_SESS_LOCK: return "STK_SESS"; |
| 418 | case APPLETS_LOCK: return "APPLETS"; |
| 419 | case PEER_LOCK: return "PEER"; |
| 420 | case SHCTX_LOCK: return "SHCTX"; |
| 421 | case SSL_LOCK: return "SSL"; |
| 422 | case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS"; |
| 423 | case PATREF_LOCK: return "PATREF"; |
| 424 | case PATEXP_LOCK: return "PATEXP"; |
| 425 | case VARS_LOCK: return "VARS"; |
| 426 | case COMP_POOL_LOCK: return "COMP_POOL"; |
| 427 | case LUA_LOCK: return "LUA"; |
| 428 | case NOTIF_LOCK: return "NOTIF"; |
| 429 | case SPOE_APPLET_LOCK: return "SPOE_APPLET"; |
| 430 | case DNS_LOCK: return "DNS"; |
| 431 | case PID_LIST_LOCK: return "PID_LIST"; |
| 432 | case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS"; |
| 433 | case PIPES_LOCK: return "PIPES"; |
| 434 | case TLSKEYS_REF_LOCK: return "TLSKEYS_REF"; |
| 435 | case AUTH_LOCK: return "AUTH"; |
| 436 | case LOGSRV_LOCK: return "LOGSRV"; |
| 437 | case DICT_LOCK: return "DICT"; |
| 438 | case PROTO_LOCK: return "PROTO"; |
| 439 | case QUEUE_LOCK: return "QUEUE"; |
| 440 | case CKCH_LOCK: return "CKCH"; |
| 441 | case SNI_LOCK: return "SNI"; |
| 442 | case SSL_SERVER_LOCK: return "SSL_SERVER"; |
| 443 | case SFT_LOCK: return "SFT"; |
| 444 | case IDLE_CONNS_LOCK: return "IDLE_CONNS"; |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 445 | case OCSP_LOCK: return "OCSP"; |
Amaury Denoyelle | e83f937 | 2023-04-18 11:10:54 +0200 | [diff] [blame] | 446 | case QC_CID_LOCK: return "QC_CID"; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 447 | case OTHER_LOCK: return "OTHER"; |
| 448 | case DEBUG1_LOCK: return "DEBUG1"; |
| 449 | case DEBUG2_LOCK: return "DEBUG2"; |
| 450 | case DEBUG3_LOCK: return "DEBUG3"; |
| 451 | case DEBUG4_LOCK: return "DEBUG4"; |
| 452 | case DEBUG5_LOCK: return "DEBUG5"; |
| 453 | case LOCK_LABELS: break; /* keep compiler happy */ |
| 454 | }; |
| 455 | /* only way to come here is consecutive to an internal bug */ |
| 456 | abort(); |
| 457 | } |
| 458 | |
| 459 | void show_lock_stats() |
| 460 | { |
| 461 | int lbl; |
| 462 | |
| 463 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 464 | if (!lock_stats[lbl].num_write_locked && |
| 465 | !lock_stats[lbl].num_seek_locked && |
| 466 | !lock_stats[lbl].num_read_locked) { |
| 467 | fprintf(stderr, |
| 468 | "Stats about Lock %s: not used\n", |
| 469 | lock_label(lbl)); |
| 470 | continue; |
| 471 | } |
| 472 | |
| 473 | fprintf(stderr, |
| 474 | "Stats about Lock %s: \n", |
| 475 | lock_label(lbl)); |
| 476 | |
| 477 | if (lock_stats[lbl].num_write_locked) |
| 478 | fprintf(stderr, |
Frédéric Lécaille | 83540ed | 2023-02-24 09:47:07 +0100 | [diff] [blame] | 479 | "\t # write lock : %llu\n" |
| 480 | "\t # write unlock: %llu (%lld)\n" |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 481 | "\t # wait time for write : %.3f msec\n" |
| 482 | "\t # wait time for write/lock: %.3f nsec\n", |
Frédéric Lécaille | 83540ed | 2023-02-24 09:47:07 +0100 | [diff] [blame] | 483 | (ullong)lock_stats[lbl].num_write_locked, |
| 484 | (ullong)lock_stats[lbl].num_write_unlocked, |
| 485 | (llong)(lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked), |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 486 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 487 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0); |
| 488 | |
| 489 | if (lock_stats[lbl].num_seek_locked) |
| 490 | fprintf(stderr, |
Frédéric Lécaille | 83540ed | 2023-02-24 09:47:07 +0100 | [diff] [blame] | 491 | "\t # seek lock : %llu\n" |
| 492 | "\t # seek unlock : %llu (%lld)\n" |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 493 | "\t # wait time for seek : %.3f msec\n" |
| 494 | "\t # wait time for seek/lock : %.3f nsec\n", |
Frédéric Lécaille | 83540ed | 2023-02-24 09:47:07 +0100 | [diff] [blame] | 495 | (ullong)lock_stats[lbl].num_seek_locked, |
| 496 | (ullong)lock_stats[lbl].num_seek_unlocked, |
| 497 | (llong)(lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked), |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 498 | (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0, |
| 499 | lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0); |
| 500 | |
| 501 | if (lock_stats[lbl].num_read_locked) |
| 502 | fprintf(stderr, |
Frédéric Lécaille | 83540ed | 2023-02-24 09:47:07 +0100 | [diff] [blame] | 503 | "\t # read lock : %llu\n" |
| 504 | "\t # read unlock : %llu (%lld)\n" |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 505 | "\t # wait time for read : %.3f msec\n" |
| 506 | "\t # wait time for read/lock : %.3f nsec\n", |
Frédéric Lécaille | 83540ed | 2023-02-24 09:47:07 +0100 | [diff] [blame] | 507 | (ullong)lock_stats[lbl].num_read_locked, |
| 508 | (ullong)lock_stats[lbl].num_read_unlocked, |
| 509 | (llong)(lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked), |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 510 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 511 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 512 | } |
| 513 | } |
| 514 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 515 | void __ha_rwlock_init(struct ha_rwlock *l) |
| 516 | { |
| 517 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 518 | __RWLOCK_INIT(&l->lock); |
| 519 | } |
| 520 | |
| 521 | void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 522 | { |
| 523 | __RWLOCK_DESTROY(&l->lock); |
| 524 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 525 | } |
| 526 | |
| 527 | |
| 528 | void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 529 | const char *func, const char *file, int line) |
| 530 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 531 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 532 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 533 | uint64_t start_time; |
| 534 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 535 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 536 | abort(); |
| 537 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 538 | HA_ATOMIC_OR(&st->wait_writers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 539 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 540 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 541 | __RWLOCK_WRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 542 | 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] | 543 | |
| 544 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 545 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 546 | st->cur_writer = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 547 | l->info.last_location.function = func; |
| 548 | l->info.last_location.file = file; |
| 549 | l->info.last_location.line = line; |
| 550 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 551 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 555 | const char *func, const char *file, int line) |
| 556 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 557 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 558 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 559 | uint64_t start_time; |
| 560 | int r; |
| 561 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 562 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 563 | abort(); |
| 564 | |
| 565 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 566 | HA_ATOMIC_OR(&st->wait_writers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 567 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 568 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 569 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 570 | 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] | 571 | if (unlikely(r)) { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 572 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 573 | return r; |
| 574 | } |
| 575 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 576 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 577 | st->cur_writer = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 578 | l->info.last_location.function = func; |
| 579 | l->info.last_location.file = file; |
| 580 | l->info.last_location.line = line; |
| 581 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 582 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 588 | const char *func, const char *file, int line) |
| 589 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 590 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 591 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
| 592 | |
| 593 | if (unlikely(!(st->cur_writer & tbit))) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 594 | /* the thread is not owning the lock for write */ |
| 595 | abort(); |
| 596 | } |
| 597 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 598 | st->cur_writer = 0; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 599 | l->info.last_location.function = func; |
| 600 | l->info.last_location.file = file; |
| 601 | l->info.last_location.line = line; |
| 602 | |
| 603 | __RWLOCK_WRUNLOCK(&l->lock); |
| 604 | |
| 605 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 606 | } |
| 607 | |
| 608 | void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 609 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 610 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 611 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 612 | uint64_t start_time; |
| 613 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 614 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 615 | abort(); |
| 616 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 617 | HA_ATOMIC_OR(&st->wait_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 618 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 619 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 620 | __RWLOCK_RDLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 621 | 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] | 622 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 623 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 624 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 625 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 626 | HA_ATOMIC_AND(&st->wait_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 630 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 631 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 632 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 633 | int r; |
| 634 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 635 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 636 | abort(); |
| 637 | |
| 638 | /* try read should never wait */ |
| 639 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 640 | if (unlikely(r)) |
| 641 | return r; |
| 642 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 643 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 644 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 650 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 651 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 652 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
| 653 | |
| 654 | if (unlikely(!(st->cur_readers & tbit))) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 655 | /* the thread is not owning the lock for read */ |
| 656 | abort(); |
| 657 | } |
| 658 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 659 | HA_ATOMIC_AND(&st->cur_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 660 | |
| 661 | __RWLOCK_RDUNLOCK(&l->lock); |
| 662 | |
| 663 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked); |
| 664 | } |
| 665 | |
| 666 | void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l, |
| 667 | const char *func, const char *file, int line) |
| 668 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 669 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 670 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 671 | uint64_t start_time; |
| 672 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 673 | if ((st->cur_readers | st->cur_seeker) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 674 | abort(); |
| 675 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 676 | if (!(st->cur_writer & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 677 | abort(); |
| 678 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 679 | HA_ATOMIC_OR(&st->wait_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 680 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 681 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 682 | __RWLOCK_WRTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 683 | 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] | 684 | |
| 685 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 686 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 687 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
| 688 | HA_ATOMIC_AND(&st->cur_writer, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 689 | l->info.last_location.function = func; |
| 690 | l->info.last_location.file = file; |
| 691 | l->info.last_location.line = line; |
| 692 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 693 | HA_ATOMIC_AND(&st->wait_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 697 | const char *func, const char *file, int line) |
| 698 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 699 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 700 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 701 | uint64_t start_time; |
| 702 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 703 | if ((st->cur_readers | st->cur_seeker) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 704 | abort(); |
| 705 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 706 | if (!(st->cur_writer & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 707 | abort(); |
| 708 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 709 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 710 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 711 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 712 | __RWLOCK_WRTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 713 | 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] | 714 | |
| 715 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 716 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 717 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
| 718 | HA_ATOMIC_AND(&st->cur_writer, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 719 | l->info.last_location.function = func; |
| 720 | l->info.last_location.file = file; |
| 721 | l->info.last_location.line = line; |
| 722 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 723 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l, |
| 727 | const char *func, const char *file, int line) |
| 728 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 729 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 730 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 731 | uint64_t start_time; |
| 732 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 733 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 734 | abort(); |
| 735 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 736 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 737 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 738 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 739 | __RWLOCK_SKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 740 | 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] | 741 | |
| 742 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 743 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 744 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 745 | l->info.last_location.function = func; |
| 746 | l->info.last_location.file = file; |
| 747 | l->info.last_location.line = line; |
| 748 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 749 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l, |
| 753 | const char *func, const char *file, int line) |
| 754 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 755 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 756 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 757 | uint64_t start_time; |
| 758 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 759 | if ((st->cur_readers | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 760 | abort(); |
| 761 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 762 | if (!(st->cur_seeker & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 763 | abort(); |
| 764 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 765 | HA_ATOMIC_OR(&st->wait_writers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 766 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 767 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 768 | __RWLOCK_SKTOWR(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 769 | 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] | 770 | |
| 771 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 772 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 773 | HA_ATOMIC_OR(&st->cur_writer, tbit); |
| 774 | HA_ATOMIC_AND(&st->cur_seeker, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 775 | l->info.last_location.function = func; |
| 776 | l->info.last_location.file = file; |
| 777 | l->info.last_location.line = line; |
| 778 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 779 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l, |
| 783 | const char *func, const char *file, int line) |
| 784 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 785 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 786 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 787 | uint64_t start_time; |
| 788 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 789 | if ((st->cur_readers | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 790 | abort(); |
| 791 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 792 | if (!(st->cur_seeker & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 793 | abort(); |
| 794 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 795 | HA_ATOMIC_OR(&st->wait_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 796 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 797 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 798 | __RWLOCK_SKTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 799 | 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] | 800 | |
| 801 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 802 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 803 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
| 804 | HA_ATOMIC_AND(&st->cur_seeker, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 805 | l->info.last_location.function = func; |
| 806 | l->info.last_location.file = file; |
| 807 | l->info.last_location.line = line; |
| 808 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 809 | HA_ATOMIC_AND(&st->wait_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 813 | const char *func, const char *file, int line) |
| 814 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 815 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 816 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
| 817 | if (!(st->cur_seeker & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 818 | abort(); |
| 819 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 820 | HA_ATOMIC_AND(&st->cur_seeker, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 821 | l->info.last_location.function = func; |
| 822 | l->info.last_location.file = file; |
| 823 | l->info.last_location.line = line; |
| 824 | |
| 825 | __RWLOCK_SKUNLOCK(&l->lock); |
| 826 | |
| 827 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked); |
| 828 | } |
| 829 | |
| 830 | int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l, |
| 831 | const char *func, const char *file, int line) |
| 832 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 833 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 834 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 835 | uint64_t start_time; |
| 836 | int r; |
| 837 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 838 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 839 | abort(); |
| 840 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 841 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 842 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 843 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 844 | r = __RWLOCK_TRYSKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 845 | 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] | 846 | |
| 847 | if (likely(!r)) { |
| 848 | /* got the lock ! */ |
| 849 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 850 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 851 | l->info.last_location.function = func; |
| 852 | l->info.last_location.file = file; |
| 853 | l->info.last_location.line = line; |
| 854 | } |
| 855 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 856 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 857 | return r; |
| 858 | } |
| 859 | |
| 860 | int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 861 | const char *func, const char *file, int line) |
| 862 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 863 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 864 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 865 | uint64_t start_time; |
| 866 | int r; |
| 867 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 868 | if ((st->cur_writer | st->cur_seeker) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 869 | abort(); |
| 870 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 871 | if (!(st->cur_readers & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 872 | abort(); |
| 873 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 874 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 875 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 876 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 877 | r = __RWLOCK_TRYRDTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 878 | 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] | 879 | |
| 880 | if (likely(!r)) { |
| 881 | /* got the lock ! */ |
| 882 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 883 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
| 884 | HA_ATOMIC_AND(&st->cur_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 885 | l->info.last_location.function = func; |
| 886 | l->info.last_location.file = file; |
| 887 | l->info.last_location.line = line; |
| 888 | } |
| 889 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 890 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 891 | return r; |
| 892 | } |
| 893 | |
| 894 | void __spin_init(struct ha_spinlock *l) |
| 895 | { |
| 896 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 897 | __SPIN_INIT(&l->lock); |
| 898 | } |
| 899 | |
| 900 | void __spin_destroy(struct ha_spinlock *l) |
| 901 | { |
| 902 | __SPIN_DESTROY(&l->lock); |
| 903 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 904 | } |
| 905 | |
| 906 | void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 907 | const char *func, const char *file, int line) |
| 908 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 909 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 910 | struct ha_spinlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 911 | uint64_t start_time; |
| 912 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 913 | if (unlikely(st->owner & tbit)) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 914 | /* the thread is already owning the lock */ |
| 915 | abort(); |
| 916 | } |
| 917 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 918 | HA_ATOMIC_OR(&st->waiters, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 919 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 920 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 921 | __SPIN_LOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 922 | 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] | 923 | |
| 924 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 925 | |
| 926 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 927 | st->owner = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 928 | l->info.last_location.function = func; |
| 929 | l->info.last_location.file = file; |
| 930 | l->info.last_location.line = line; |
| 931 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 932 | HA_ATOMIC_AND(&st->waiters, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 936 | const char *func, const char *file, int line) |
| 937 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 938 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 939 | struct ha_spinlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 940 | int r; |
| 941 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 942 | if (unlikely(st->owner & tbit)) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 943 | /* the thread is already owning the lock */ |
| 944 | abort(); |
| 945 | } |
| 946 | |
| 947 | /* try read should never wait */ |
| 948 | r = __SPIN_TRYLOCK(&l->lock); |
| 949 | if (unlikely(r)) |
| 950 | return r; |
| 951 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 952 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 953 | st->owner = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 954 | l->info.last_location.function = func; |
| 955 | l->info.last_location.file = file; |
| 956 | l->info.last_location.line = line; |
| 957 | |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 962 | const char *func, const char *file, int line) |
| 963 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 964 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 965 | struct ha_spinlock_state *st = &l->info.st[tgid-1]; |
| 966 | |
| 967 | if (unlikely(!(st->owner & tbit))) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 968 | /* the thread is not owning the lock */ |
| 969 | abort(); |
| 970 | } |
| 971 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 972 | st->owner = 0; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 973 | l->info.last_location.function = func; |
| 974 | l->info.last_location.file = file; |
| 975 | l->info.last_location.line = line; |
| 976 | |
| 977 | __SPIN_UNLOCK(&l->lock); |
| 978 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 979 | } |
| 980 | |
| 981 | #endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 982 | |
Willy Tarreau | 87aff02 | 2022-07-10 10:58:57 +0200 | [diff] [blame] | 983 | |
| 984 | #if defined(USE_PTHREAD_EMULATION) |
| 985 | |
| 986 | /* pthread rwlock emulation using plocks (to avoid expensive futexes). |
| 987 | * these are a direct mapping on Progressive Locks, with the exception that |
| 988 | * since there's a common unlock operation in pthreads, we need to know if |
| 989 | * we need to unlock for reads or writes, so we set the topmost bit to 1 when |
| 990 | * a write lock is acquired to indicate that a write unlock needs to be |
| 991 | * performed. It's not a problem since this bit will never be used given that |
| 992 | * haproxy won't support as many threads as the plocks. |
| 993 | * |
| 994 | * The storage is the pthread_rwlock_t cast as an ulong |
| 995 | */ |
| 996 | |
| 997 | int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr) |
| 998 | { |
| 999 | ulong *lock = (ulong *)rwlock; |
| 1000 | |
| 1001 | *lock = 0; |
| 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) |
| 1006 | { |
| 1007 | ulong *lock = (ulong *)rwlock; |
| 1008 | |
| 1009 | *lock = 0; |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) |
| 1014 | { |
| 1015 | pl_lorw_rdlock((unsigned long *)rwlock); |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock) |
| 1020 | { |
| 1021 | return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_SHR_BASE); |
| 1022 | } |
| 1023 | |
| 1024 | int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime) |
| 1025 | { |
| 1026 | return pthread_rwlock_tryrdlock(rwlock); |
| 1027 | } |
| 1028 | |
| 1029 | int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) |
| 1030 | { |
| 1031 | pl_lorw_wrlock((unsigned long *)rwlock); |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock) |
| 1036 | { |
| 1037 | return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_EXC_BASE); |
| 1038 | } |
| 1039 | |
| 1040 | int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime) |
| 1041 | { |
| 1042 | return pthread_rwlock_trywrlock(rwlock); |
| 1043 | } |
| 1044 | |
| 1045 | int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) |
| 1046 | { |
| 1047 | pl_lorw_unlock((unsigned long *)rwlock); |
| 1048 | return 0; |
| 1049 | } |
| 1050 | #endif // defined(USE_PTHREAD_EMULATION) |
| 1051 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 1052 | /* Depending on the platform and how libpthread was built, pthread_exit() may |
| 1053 | * involve some code in libgcc_s that would be loaded on exit for the first |
| 1054 | * time, causing aborts if the process is chrooted. It's harmless bit very |
| 1055 | * dirty. There isn't much we can do to make sure libgcc_s is loaded only if |
| 1056 | * needed, so what we do here is that during early boot we create a dummy |
| 1057 | * thread that immediately exits. This will lead to libgcc_s being loaded |
| 1058 | * during boot on the platforms where it's required. |
| 1059 | */ |
| 1060 | static void *dummy_thread_function(void *data) |
| 1061 | { |
| 1062 | pthread_exit(NULL); |
| 1063 | return NULL; |
| 1064 | } |
| 1065 | |
| 1066 | static inline void preload_libgcc_s(void) |
| 1067 | { |
| 1068 | pthread_t dummy_thread; |
| 1069 | pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL); |
| 1070 | pthread_join(dummy_thread, NULL); |
| 1071 | } |
| 1072 | |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 1073 | static void __thread_init(void) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1074 | { |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 1075 | char *ptr = NULL; |
| 1076 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 1077 | preload_libgcc_s(); |
Willy Tarreau | 77b9822 | 2020-09-02 08:04:35 +0200 | [diff] [blame] | 1078 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 1079 | thread_cpus_enabled_at_boot = thread_cpus_enabled(); |
Willy Tarreau | f5b6327 | 2023-03-09 10:12:06 +0100 | [diff] [blame] | 1080 | thread_cpus_enabled_at_boot = MIN(thread_cpus_enabled_at_boot, MAX_THREADS); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 1081 | |
Willy Tarreau | c80bdb2 | 2022-08-06 16:44:55 +0200 | [diff] [blame] | 1082 | memprintf(&ptr, "Built with multi-threading support (MAX_TGROUPS=%d, MAX_THREADS=%d, default=%d).", |
| 1083 | MAX_TGROUPS, MAX_THREADS, thread_cpus_enabled_at_boot); |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 1084 | hap_register_build_opts(ptr, 1); |
| 1085 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1086 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 1087 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 1088 | #endif |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1089 | } |
Willy Tarreau | 8ead1d0 | 2022-04-25 19:23:17 +0200 | [diff] [blame] | 1090 | INITCALL0(STG_PREPARE, __thread_init); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1091 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 1092 | #else |
| 1093 | |
Willy Tarreau | aa99276 | 2021-10-06 23:33:20 +0200 | [diff] [blame] | 1094 | /* send signal <sig> to thread <thr> (send to process in fact) */ |
| 1095 | void ha_tkill(unsigned int thr, int sig) |
| 1096 | { |
| 1097 | raise(sig); |
| 1098 | } |
| 1099 | |
| 1100 | /* send signal <sig> to all threads (send to process in fact) */ |
| 1101 | void ha_tkillall(int sig) |
| 1102 | { |
| 1103 | raise(sig); |
| 1104 | } |
| 1105 | |
| 1106 | void ha_thread_relax(void) |
| 1107 | { |
| 1108 | #ifdef _POSIX_PRIORITY_SCHEDULING |
| 1109 | sched_yield(); |
| 1110 | #endif |
| 1111 | } |
| 1112 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 1113 | REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set)."); |
| 1114 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1115 | #endif // USE_THREAD |
| 1116 | |
| 1117 | |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1118 | /* scans the configured thread mapping and establishes the final one. Returns <0 |
| 1119 | * on failure, >=0 on success. |
| 1120 | */ |
| 1121 | int thread_map_to_groups() |
| 1122 | { |
| 1123 | int t, g, ut, ug; |
| 1124 | int q, r; |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1125 | ulong m __maybe_unused; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1126 | |
| 1127 | ut = ug = 0; // unassigned threads & groups |
| 1128 | |
| 1129 | for (t = 0; t < global.nbthread; t++) { |
| 1130 | if (!ha_thread_info[t].tg) |
| 1131 | ut++; |
| 1132 | } |
| 1133 | |
| 1134 | for (g = 0; g < global.nbtgroups; g++) { |
| 1135 | if (!ha_tgroup_info[g].count) |
| 1136 | ug++; |
Willy Tarreau | 60fe4a9 | 2022-06-28 17:48:07 +0200 | [diff] [blame] | 1137 | ha_tgroup_info[g].tgid_bit = 1UL << g; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | if (ug > ut) { |
| 1141 | ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut); |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
| 1145 | /* look for first unassigned thread */ |
| 1146 | for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++) |
| 1147 | ; |
| 1148 | |
| 1149 | /* assign threads to empty groups */ |
| 1150 | for (g = 0; ug && ut; ) { |
| 1151 | /* due to sparse thread assignment we can end up with more threads |
| 1152 | * per group on last assigned groups than former ones, so we must |
| 1153 | * always try to pack the maximum remaining ones together first. |
| 1154 | */ |
| 1155 | q = ut / ug; |
| 1156 | r = ut % ug; |
| 1157 | if ((q + !!r) > MAX_THREADS_PER_GROUP) { |
Willy Tarreau | cf0d0ee | 2023-03-09 11:39:51 +0100 | [diff] [blame] | 1158 | 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", ut, ug); |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1159 | return -1; |
| 1160 | } |
| 1161 | |
| 1162 | /* thread <t> is the next unassigned one. Let's look for next |
| 1163 | * unassigned group, we know there are some left |
| 1164 | */ |
| 1165 | while (ut >= ug && ha_tgroup_info[g].count) |
| 1166 | g++; |
| 1167 | |
| 1168 | /* group g is unassigned, try to fill it with consecutive threads */ |
| 1169 | while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r && |
| 1170 | (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) { |
| 1171 | |
| 1172 | if (!ha_tgroup_info[g].count) { |
| 1173 | /* assign new group */ |
| 1174 | ha_tgroup_info[g].base = t; |
| 1175 | ug--; |
| 1176 | } |
| 1177 | |
| 1178 | ha_tgroup_info[g].count++; |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1179 | ha_thread_info[t].tgid = g + 1; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1180 | ha_thread_info[t].tg = &ha_tgroup_info[g]; |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 1181 | ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g]; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1182 | |
| 1183 | ut--; |
| 1184 | /* switch to next unassigned thread */ |
| 1185 | while (++t < global.nbthread && ha_thread_info[t].tg) |
| 1186 | ; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | if (ut) { |
| 1191 | 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); |
| 1192 | return -1; |
| 1193 | } |
| 1194 | |
Willy Tarreau | cc7a11e | 2021-09-28 08:53:11 +0200 | [diff] [blame] | 1195 | for (t = 0; t < global.nbthread; t++) { |
| 1196 | ha_thread_info[t].tid = t; |
| 1197 | ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base; |
Willy Tarreau | cc7a11e | 2021-09-28 08:53:11 +0200 | [diff] [blame] | 1198 | ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid; |
| 1199 | } |
| 1200 | |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1201 | m = 0; |
Willy Tarreau | 377e37a | 2022-06-24 15:18:49 +0200 | [diff] [blame] | 1202 | for (g = 0; g < global.nbtgroups; g++) { |
| 1203 | ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count); |
Aurelien DARRAGON | 739281b | 2023-01-27 15:13:28 +0100 | [diff] [blame] | 1204 | /* for now, additional threads are not started, so we should |
| 1205 | * consider them as harmless and idle. |
| 1206 | * This will get automatically updated when such threads are |
| 1207 | * started in run_thread_poll_loop() |
| 1208 | * Without this, thread_isolate() and thread_isolate_full() |
| 1209 | * will fail to work as long as secondary threads did not enter |
| 1210 | * the polling loop at least once. |
| 1211 | */ |
| 1212 | ha_tgroup_ctx[g].threads_harmless = ha_tgroup_info[g].threads_enabled; |
| 1213 | ha_tgroup_ctx[g].threads_idle = ha_tgroup_info[g].threads_enabled; |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1214 | if (!ha_tgroup_info[g].count) |
| 1215 | continue; |
| 1216 | m |= 1UL << g; |
Willy Tarreau | 377e37a | 2022-06-24 15:18:49 +0200 | [diff] [blame] | 1217 | |
| 1218 | } |
| 1219 | |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1220 | #ifdef USE_THREAD |
| 1221 | all_tgroups_mask = m; |
| 1222 | #endif |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1223 | return 0; |
| 1224 | } |
| 1225 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1226 | /* Converts a configuration thread set based on either absolute or relative |
| 1227 | * thread numbers into a global group+mask. This is essentially for use with |
| 1228 | * the "thread" directive on "bind" lines, where "thread 4-6,10-12" might be |
| 1229 | * turned to "2/1-3,4/1-3". It cannot be used before the thread mapping above |
| 1230 | * was completed and the thread group numbers configured. The thread_set is |
| 1231 | * replaced by the resolved group-based one. It is possible to force a single |
| 1232 | * default group for unspecified sets instead of enabling all groups by passing |
Willy Tarreau | f2988e1 | 2023-02-02 17:01:10 +0100 | [diff] [blame] | 1233 | * this group's non-zero value to defgrp. |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1234 | * |
| 1235 | * Returns <0 on failure, >=0 on success. |
| 1236 | */ |
Willy Tarreau | f2988e1 | 2023-02-02 17:01:10 +0100 | [diff] [blame] | 1237 | 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] | 1238 | { |
Willy Tarreau | 15c8428 | 2023-02-04 10:49:01 +0100 | [diff] [blame] | 1239 | struct thread_set new_ts = { }; |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1240 | ulong mask, imask; |
| 1241 | uint g; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1242 | |
Willy Tarreau | 97da942 | 2023-03-01 11:24:29 +0100 | [diff] [blame] | 1243 | if (!ts->grps) { |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1244 | /* unspecified group, IDs are global */ |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1245 | if (thread_set_is_empty(ts)) { |
| 1246 | /* all threads of all groups, unless defgrp is set and |
| 1247 | * we then set it as the only group. |
| 1248 | */ |
| 1249 | for (g = defgrp ? defgrp-1 : 0; g < (defgrp ? defgrp : global.nbtgroups); g++) { |
| 1250 | new_ts.rel[g] = ha_tgroup_info[g].threads_enabled; |
Willy Tarreau | 97da942 | 2023-03-01 11:24:29 +0100 | [diff] [blame] | 1251 | if (new_ts.rel[g]) |
| 1252 | new_ts.grps |= 1UL << g; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1253 | } |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1254 | } else { |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1255 | /* some absolute threads are set, we must remap them to |
| 1256 | * relative ones. Each group cannot have more than |
| 1257 | * LONGBITS threads, thus it spans at most two absolute |
| 1258 | * blocks. |
| 1259 | */ |
| 1260 | for (g = 0; g < global.nbtgroups; g++) { |
| 1261 | uint block = ha_tgroup_info[g].base / LONGBITS; |
| 1262 | uint base = ha_tgroup_info[g].base % LONGBITS; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1263 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1264 | mask = ts->abs[block] >> base; |
Willy Tarreau | 1b536a1 | 2023-03-22 10:28:50 +0100 | [diff] [blame] | 1265 | if (base && |
| 1266 | (block + 1) < sizeof(ts->abs) / sizeof(ts->abs[0]) && |
| 1267 | ha_tgroup_info[g].count > (LONGBITS - base)) |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1268 | mask |= ts->abs[block + 1] << (LONGBITS - base); |
| 1269 | mask &= nbits(ha_tgroup_info[g].count); |
| 1270 | mask &= ha_tgroup_info[g].threads_enabled; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1271 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1272 | /* now the mask exactly matches the threads to be enabled |
| 1273 | * in this group. |
| 1274 | */ |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1275 | new_ts.rel[g] |= mask; |
Willy Tarreau | 97da942 | 2023-03-01 11:24:29 +0100 | [diff] [blame] | 1276 | if (new_ts.rel[g]) |
| 1277 | new_ts.grps |= 1UL << g; |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1278 | } |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1279 | } |
| 1280 | } else { |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1281 | /* groups were specified */ |
| 1282 | for (g = 0; g < MAX_TGROUPS; g++) { |
| 1283 | imask = ts->rel[g]; |
| 1284 | if (!imask) |
| 1285 | continue; |
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 | if (g >= global.nbtgroups) { |
| 1288 | memprintf(err, "'thread' directive references non-existing thread group %u", g+1); |
| 1289 | return -1; |
| 1290 | } |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1291 | |
Willy Tarreau | f0de8ca | 2023-01-31 19:31:27 +0100 | [diff] [blame] | 1292 | /* some relative threads are set. Keep only existing ones for this group */ |
| 1293 | mask = nbits(ha_tgroup_info[g].count); |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1294 | |
| 1295 | if (!(mask & imask)) { |
| 1296 | /* no intersection between the thread group's |
| 1297 | * threads and the bind line's. |
| 1298 | */ |
| 1299 | #ifdef THREAD_AUTO_ADJUST_GROUPS |
| 1300 | unsigned long new_mask = 0; |
| 1301 | |
| 1302 | while (imask) { |
| 1303 | new_mask |= imask & mask; |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1304 | imask >>= ha_tgroup_info[g].count; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1305 | } |
| 1306 | imask = new_mask; |
| 1307 | #else |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1308 | 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] | 1309 | return -1; |
| 1310 | #endif |
| 1311 | } |
| 1312 | |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1313 | new_ts.rel[g] = imask & mask; |
Willy Tarreau | 97da942 | 2023-03-01 11:24:29 +0100 | [diff] [blame] | 1314 | if (new_ts.rel[g]) |
| 1315 | new_ts.grps |= 1UL << g; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1316 | } |
| 1317 | } |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1318 | |
| 1319 | /* update the thread_set */ |
Willy Tarreau | 7b8aac4 | 2023-02-27 11:27:38 +0100 | [diff] [blame] | 1320 | if (!thread_set_nth_group(&new_ts, 0)) { |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1321 | memprintf(err, "'thread' directive only references non-existing threads"); |
| 1322 | return -1; |
| 1323 | } |
| 1324 | |
| 1325 | *ts = new_ts; |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1326 | return 0; |
| 1327 | } |
| 1328 | |
| 1329 | /* Parse a string representing a thread set in one of the following forms: |
| 1330 | * |
| 1331 | * - { "all" | "odd" | "even" | <abs_num> [ "-" <abs_num> ] }[,...] |
| 1332 | * => these are (lists of) absolute thread numbers |
| 1333 | * |
| 1334 | * - <tgnum> "/" { "all" | "odd" | "even" | <rel_num> [ "-" <rel_num> ][,...] |
| 1335 | * => these are (lists of) per-group relative thread numbers. All numbers |
| 1336 | * must be lower than or equal to LONGBITS. When multiple list elements |
| 1337 | * are provided, each of them must contain the thread group number. |
| 1338 | * |
| 1339 | * Minimum value for a thread or group number is always 1. Maximum value for an |
| 1340 | * absolute thread number is MAX_THREADS, maximum value for a relative thread |
| 1341 | * number is MAX_THREADS_PER_GROUP, an maximum value for a thread group is |
| 1342 | * MAX_TGROUPS. "all", "even" and "odd" will be bound by MAX_THREADS and/or |
| 1343 | * MAX_THREADS_PER_GROUP in any case. In ranges, a missing digit before "-" |
| 1344 | * is implicitly 1, and a missing digit after "-" is implicitly the highest of |
| 1345 | * its class. As such "-" is equivalent to "all", allowing to build strings |
| 1346 | * such as "${MIN}-${MAX}" where both MIN and MAX are optional. |
| 1347 | * |
| 1348 | * It is not valid to mix absolute and relative numbers. As such: |
| 1349 | * - all valid (all absolute threads) |
| 1350 | * - 12-19,24-31 valid (abs threads 12 to 19 and 24 to 31) |
| 1351 | * - 1/all valid (all 32 or 64 threads of group 1) |
| 1352 | * - 1/1-4,1/8-10,2/1 valid |
| 1353 | * - 1/1-4,8-10 invalid (mixes relatve "1/1-4" with absolute "8-10") |
| 1354 | * - 1-4,8-10,2/1 invalid (mixes absolute "1-4,8-10" with relative "2/1") |
| 1355 | * - 1/odd-4 invalid (mixes range with boundary) |
| 1356 | * |
| 1357 | * The target thread set is *completed* with supported threads, which means |
| 1358 | * that it's the caller's responsibility for pre-initializing it. If the target |
| 1359 | * thread set is NULL, it's not updated and the function only verifies that the |
| 1360 | * input parses. |
| 1361 | * |
| 1362 | * On success, it returns 0. otherwise it returns non-zero with an error |
| 1363 | * message in <err>. |
| 1364 | */ |
| 1365 | int parse_thread_set(const char *arg, struct thread_set *ts, char **err) |
| 1366 | { |
| 1367 | const char *set; |
| 1368 | const char *sep; |
| 1369 | int v, min, max, tg; |
| 1370 | int is_rel; |
| 1371 | |
| 1372 | /* search for the first delimiter (',', '-' or '/') to decide whether |
| 1373 | * we're facing an absolute or relative form. The relative form always |
| 1374 | * starts with a number followed by a slash. |
| 1375 | */ |
| 1376 | for (sep = arg; isdigit((uchar)*sep); sep++) |
| 1377 | ; |
| 1378 | |
| 1379 | is_rel = (/*sep > arg &&*/ *sep == '/'); /* relative form */ |
| 1380 | |
| 1381 | /* from there we have to cut the thread spec around commas */ |
| 1382 | |
| 1383 | set = arg; |
| 1384 | tg = 0; |
| 1385 | while (*set) { |
| 1386 | /* note: we can't use strtol() here because "-3" would parse as |
| 1387 | * (-3) while we want to stop before the "-", so we find the |
| 1388 | * separator ourselves and rely on atoi() whose value we may |
| 1389 | * ignore depending where the separator is. |
| 1390 | */ |
| 1391 | for (sep = set; isdigit((uchar)*sep); sep++) |
| 1392 | ; |
| 1393 | |
| 1394 | if (sep != set && *sep && *sep != '/' && *sep != '-' && *sep != ',') { |
| 1395 | memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set); |
| 1396 | return -1; |
| 1397 | } |
| 1398 | |
| 1399 | v = (sep != set) ? atoi(set) : 0; |
| 1400 | |
| 1401 | /* Now we know that the string is made of an optional series of digits |
| 1402 | * optionally followed by one of the delimiters above, or that it |
| 1403 | * starts with a different character. |
| 1404 | */ |
| 1405 | |
| 1406 | /* first, let's search for the thread group (digits before '/') */ |
| 1407 | |
| 1408 | if (tg || !is_rel) { |
| 1409 | /* thread group already specified or not expected if absolute spec */ |
| 1410 | if (*sep == '/') { |
| 1411 | if (tg) |
| 1412 | memprintf(err, "redundant thread group specification '%s' for group %d", set, tg); |
| 1413 | else |
| 1414 | memprintf(err, "group-relative thread specification '%s' is not permitted after a absolute thread range.", set); |
| 1415 | return -1; |
| 1416 | } |
| 1417 | } else { |
| 1418 | /* this is a group-relative spec, first field is the group number */ |
| 1419 | if (sep == set && *sep == '/') { |
| 1420 | memprintf(err, "thread group number expected before '%s'.", set); |
| 1421 | return -1; |
| 1422 | } |
| 1423 | |
| 1424 | if (*sep != '/') { |
| 1425 | memprintf(err, "absolute thread specification '%s' is not permitted after a group-relative thread range.", set); |
| 1426 | return -1; |
| 1427 | } |
| 1428 | |
| 1429 | if (v < 1 || v > MAX_TGROUPS) { |
| 1430 | memprintf(err, "invalid thread group number '%d', permitted range is 1..%d in '%s'.", v, MAX_TGROUPS, set); |
| 1431 | return -1; |
| 1432 | } |
| 1433 | |
| 1434 | tg = v; |
| 1435 | |
| 1436 | /* skip group number and go on with set,sep,v as if |
| 1437 | * there was no group number. |
| 1438 | */ |
| 1439 | set = sep + 1; |
| 1440 | continue; |
| 1441 | } |
| 1442 | |
| 1443 | /* Now 'set' starts at the min thread number, whose value is in v if any, |
| 1444 | * and preset the max to it, unless the range is filled at once via "all" |
| 1445 | * (stored as 1:0), "odd" (stored as) 1:-1, or "even" (stored as 1:-2). |
| 1446 | * 'sep' points to the next non-digit which may be set itself e.g. for |
| 1447 | * "all" etc or "-xx". |
| 1448 | */ |
| 1449 | |
| 1450 | if (!*set) { |
| 1451 | /* empty set sets no restriction */ |
| 1452 | min = 1; |
| 1453 | max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS; |
| 1454 | } |
| 1455 | else { |
| 1456 | if (sep != set && *sep && *sep != '-' && *sep != ',') { |
Ilya Shipitsin | 07be66d | 2023-04-01 12:26:42 +0200 | [diff] [blame] | 1457 | // Only delimiters are permitted around digits. |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1458 | memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set); |
| 1459 | return -1; |
| 1460 | } |
| 1461 | |
| 1462 | /* for non-digits, find next delim */ |
| 1463 | for (; *sep && *sep != '-' && *sep != ','; sep++) |
| 1464 | ; |
| 1465 | |
| 1466 | min = max = 1; |
| 1467 | if (sep != set) { |
| 1468 | /* non-empty first thread */ |
| 1469 | if (isteq(ist2(set, sep-set), ist("all"))) |
| 1470 | max = 0; |
| 1471 | else if (isteq(ist2(set, sep-set), ist("odd"))) |
| 1472 | max = -1; |
| 1473 | else if (isteq(ist2(set, sep-set), ist("even"))) |
| 1474 | max = -2; |
| 1475 | else if (v) |
| 1476 | min = max = v; |
| 1477 | else |
| 1478 | max = min = 0; // throw an error below |
| 1479 | } |
| 1480 | |
| 1481 | if (min < 1 || min > MAX_THREADS || (is_rel && min > MAX_THREADS_PER_GROUP)) { |
| 1482 | memprintf(err, "invalid first thread number '%s', permitted range is 1..%d, or 'all', 'odd', 'even'.", |
| 1483 | set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS); |
| 1484 | return -1; |
| 1485 | } |
| 1486 | |
| 1487 | /* is this a range ? */ |
| 1488 | if (*sep == '-') { |
| 1489 | if (min != max) { |
| 1490 | memprintf(err, "extraneous range after 'all', 'odd' or 'even': '%s'.", set); |
| 1491 | return -1; |
| 1492 | } |
| 1493 | |
| 1494 | /* this is a seemingly valid range, there may be another number */ |
| 1495 | for (set = ++sep; isdigit((uchar)*sep); sep++) |
| 1496 | ; |
| 1497 | v = atoi(set); |
| 1498 | |
| 1499 | if (sep == set) { // no digit: to the max |
| 1500 | max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS; |
| 1501 | if (*sep && *sep != ',') |
| 1502 | max = 0; // throw an error below |
| 1503 | } else |
| 1504 | max = v; |
| 1505 | |
| 1506 | if (max < 1 || max > MAX_THREADS || (is_rel && max > MAX_THREADS_PER_GROUP)) { |
| 1507 | memprintf(err, "invalid last thread number '%s', permitted range is 1..%d.", |
| 1508 | set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS); |
| 1509 | return -1; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | /* here sep points to the first non-digit after the thread spec, |
| 1514 | * must be a valid delimiter. |
| 1515 | */ |
| 1516 | if (*sep && *sep != ',') { |
| 1517 | memprintf(err, "invalid character '%c' after thread set specification: '%s'.", *sep, set); |
| 1518 | return -1; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | /* store values */ |
| 1523 | if (ts) { |
| 1524 | if (is_rel) { |
| 1525 | /* group-relative thread numbers */ |
Willy Tarreau | 97da942 | 2023-03-01 11:24:29 +0100 | [diff] [blame] | 1526 | ts->grps |= 1UL << (tg - 1); |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1527 | |
| 1528 | if (max >= min) { |
| 1529 | for (v = min; v <= max; v++) |
Willy Tarreau | f91ab7a | 2023-02-06 18:01:50 +0100 | [diff] [blame] | 1530 | ts->rel[tg - 1] |= 1UL << (v - 1); |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1531 | } else { |
| 1532 | memset(&ts->rel[tg - 1], |
| 1533 | (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */, |
| 1534 | sizeof(ts->rel[tg - 1])); |
| 1535 | } |
| 1536 | } else { |
| 1537 | /* absolute thread numbers */ |
| 1538 | if (max >= min) { |
| 1539 | for (v = min; v <= max; v++) |
Willy Tarreau | f91ab7a | 2023-02-06 18:01:50 +0100 | [diff] [blame] | 1540 | ts->abs[(v - 1) / LONGBITS] |= 1UL << ((v - 1) % LONGBITS); |
Willy Tarreau | bef43df | 2023-01-31 19:27:48 +0100 | [diff] [blame] | 1541 | } else { |
| 1542 | memset(&ts->abs, |
| 1543 | (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */, |
| 1544 | sizeof(ts->abs)); |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | set = *sep ? sep + 1 : sep; |
| 1550 | tg = 0; |
| 1551 | } |
| 1552 | return 0; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1553 | } |
| 1554 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1555 | /* Parse the "nbthread" global directive, which takes an integer argument that |
| 1556 | * contains the desired number of threads. |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1557 | */ |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1558 | static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx, |
| 1559 | const struct proxy *defpx, const char *file, int line, |
| 1560 | char **err) |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1561 | { |
| 1562 | long nbthread; |
| 1563 | char *errptr; |
| 1564 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1565 | if (too_many_args(1, args, err, NULL)) |
| 1566 | return -1; |
| 1567 | |
Christopher Faulet | 5534334 | 2022-11-18 15:52:58 +0100 | [diff] [blame] | 1568 | if (non_global_section_parsed == 1) { |
| 1569 | 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]); |
| 1570 | return -1; |
| 1571 | } |
| 1572 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1573 | nbthread = strtol(args[1], &errptr, 10); |
| 1574 | if (!*args[1] || *errptr) { |
| 1575 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1576 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | #ifndef USE_THREAD |
| 1580 | if (nbthread != 1) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1581 | 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]); |
| 1582 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1583 | } |
| 1584 | #else |
| 1585 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1586 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread); |
| 1587 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1588 | } |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1589 | #endif |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1590 | |
| 1591 | HA_DIAG_WARNING_COND(global.nbthread, |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1592 | "parsing [%s:%d] : '%s' is already defined and will be overridden.\n", |
| 1593 | file, line, args[0]); |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1594 | |
| 1595 | global.nbthread = nbthread; |
| 1596 | return 0; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1597 | } |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1598 | |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1599 | /* Parse the "thread-group" global directive, which takes an integer argument |
| 1600 | * that designates a thread group, and a list of threads to put into that group. |
| 1601 | */ |
| 1602 | static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx, |
| 1603 | const struct proxy *defpx, const char *file, int line, |
| 1604 | char **err) |
| 1605 | { |
| 1606 | char *errptr; |
| 1607 | long tnum, tend, tgroup; |
| 1608 | int arg, tot; |
| 1609 | |
Christopher Faulet | 5534334 | 2022-11-18 15:52:58 +0100 | [diff] [blame] | 1610 | if (non_global_section_parsed == 1) { |
| 1611 | 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]); |
| 1612 | return -1; |
| 1613 | } |
| 1614 | |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1615 | tgroup = strtol(args[1], &errptr, 10); |
| 1616 | if (!*args[1] || *errptr) { |
| 1617 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1618 | return -1; |
| 1619 | } |
| 1620 | |
| 1621 | if (tgroup < 1 || tgroup > MAX_TGROUPS) { |
| 1622 | memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup); |
| 1623 | return -1; |
| 1624 | } |
| 1625 | |
| 1626 | /* look for a preliminary definition of any thread pointing to this |
| 1627 | * group, and remove them. |
| 1628 | */ |
| 1629 | if (ha_tgroup_info[tgroup-1].count) { |
| 1630 | ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n", |
| 1631 | file, line, args[0], tgroup); |
| 1632 | |
| 1633 | for (tnum = ha_tgroup_info[tgroup-1].base; |
| 1634 | tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count; |
| 1635 | tnum++) { |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1636 | if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) { |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1637 | ha_thread_info[tnum-1].tg = NULL; |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1638 | ha_thread_info[tnum-1].tgid = 0; |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 1639 | ha_thread_info[tnum-1].tg_ctx = NULL; |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1640 | } |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1641 | } |
| 1642 | ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0; |
| 1643 | } |
| 1644 | |
| 1645 | tot = 0; |
| 1646 | for (arg = 2; args[arg] && *args[arg]; arg++) { |
| 1647 | tend = tnum = strtol(args[arg], &errptr, 10); |
| 1648 | |
| 1649 | if (*errptr == '-') |
| 1650 | tend = strtol(errptr + 1, &errptr, 10); |
| 1651 | |
| 1652 | if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) { |
| 1653 | 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); |
| 1654 | return -1; |
| 1655 | } |
| 1656 | |
| 1657 | for(; tnum <= tend; tnum++) { |
| 1658 | if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) { |
| 1659 | ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n", |
| 1660 | file, line, args[0], tgroup, tnum); |
| 1661 | } else if (ha_thread_info[tnum-1].tg) { |
| 1662 | ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n", |
| 1663 | file, line, args[0], tgroup, tnum, |
| 1664 | (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1)); |
| 1665 | } |
| 1666 | |
| 1667 | if (!ha_tgroup_info[tgroup-1].count) { |
| 1668 | ha_tgroup_info[tgroup-1].base = tnum-1; |
| 1669 | ha_tgroup_info[tgroup-1].count = 1; |
| 1670 | } |
| 1671 | else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) { |
| 1672 | ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base; |
| 1673 | } |
| 1674 | else if (tnum < ha_tgroup_info[tgroup-1].base) { |
| 1675 | ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1; |
| 1676 | ha_tgroup_info[tgroup-1].base = tnum - 1; |
| 1677 | } |
| 1678 | |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1679 | ha_thread_info[tnum-1].tgid = tgroup; |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1680 | ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1]; |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 1681 | ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1]; |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1682 | tot++; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | if (ha_tgroup_info[tgroup-1].count > tot) { |
| 1687 | memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup); |
| 1688 | return -1; |
| 1689 | } |
| 1690 | |
| 1691 | if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) { |
| 1692 | memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP); |
| 1693 | return -1; |
| 1694 | } |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1699 | /* Parse the "thread-groups" global directive, which takes an integer argument |
| 1700 | * that contains the desired number of thread groups. |
| 1701 | */ |
| 1702 | static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx, |
| 1703 | const struct proxy *defpx, const char *file, int line, |
| 1704 | char **err) |
| 1705 | { |
| 1706 | long nbtgroups; |
| 1707 | char *errptr; |
| 1708 | |
| 1709 | if (too_many_args(1, args, err, NULL)) |
| 1710 | return -1; |
Christopher Faulet | 5534334 | 2022-11-18 15:52:58 +0100 | [diff] [blame] | 1711 | |
| 1712 | if (non_global_section_parsed == 1) { |
| 1713 | 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]); |
| 1714 | return -1; |
| 1715 | } |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1716 | |
| 1717 | nbtgroups = strtol(args[1], &errptr, 10); |
| 1718 | if (!*args[1] || *errptr) { |
| 1719 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1720 | return -1; |
| 1721 | } |
| 1722 | |
| 1723 | #ifndef USE_THREAD |
| 1724 | if (nbtgroups != 1) { |
| 1725 | 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]); |
| 1726 | return -1; |
| 1727 | } |
| 1728 | #else |
| 1729 | if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) { |
| 1730 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups); |
| 1731 | return -1; |
| 1732 | } |
| 1733 | #endif |
| 1734 | |
| 1735 | HA_DIAG_WARNING_COND(global.nbtgroups, |
| 1736 | "parsing [%s:%d] : '%s' is already defined and will be overridden.\n", |
| 1737 | file, line, args[0]); |
| 1738 | |
| 1739 | global.nbtgroups = nbtgroups; |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1743 | /* config keyword parsers */ |
| 1744 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1745 | { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 }, |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1746 | { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 }, |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1747 | { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 }, |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1748 | { 0, NULL, NULL } |
| 1749 | }}; |
| 1750 | |
| 1751 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |