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