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