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