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"; |
| 435 | case OTHER_LOCK: return "OTHER"; |
| 436 | case DEBUG1_LOCK: return "DEBUG1"; |
| 437 | case DEBUG2_LOCK: return "DEBUG2"; |
| 438 | case DEBUG3_LOCK: return "DEBUG3"; |
| 439 | case DEBUG4_LOCK: return "DEBUG4"; |
| 440 | case DEBUG5_LOCK: return "DEBUG5"; |
| 441 | case LOCK_LABELS: break; /* keep compiler happy */ |
| 442 | }; |
| 443 | /* only way to come here is consecutive to an internal bug */ |
| 444 | abort(); |
| 445 | } |
| 446 | |
| 447 | void show_lock_stats() |
| 448 | { |
| 449 | int lbl; |
| 450 | |
| 451 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 452 | if (!lock_stats[lbl].num_write_locked && |
| 453 | !lock_stats[lbl].num_seek_locked && |
| 454 | !lock_stats[lbl].num_read_locked) { |
| 455 | fprintf(stderr, |
| 456 | "Stats about Lock %s: not used\n", |
| 457 | lock_label(lbl)); |
| 458 | continue; |
| 459 | } |
| 460 | |
| 461 | fprintf(stderr, |
| 462 | "Stats about Lock %s: \n", |
| 463 | lock_label(lbl)); |
| 464 | |
| 465 | if (lock_stats[lbl].num_write_locked) |
| 466 | fprintf(stderr, |
| 467 | "\t # write lock : %lu\n" |
| 468 | "\t # write unlock: %lu (%ld)\n" |
| 469 | "\t # wait time for write : %.3f msec\n" |
| 470 | "\t # wait time for write/lock: %.3f nsec\n", |
| 471 | lock_stats[lbl].num_write_locked, |
| 472 | lock_stats[lbl].num_write_unlocked, |
| 473 | lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked, |
| 474 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 475 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0); |
| 476 | |
| 477 | if (lock_stats[lbl].num_seek_locked) |
| 478 | fprintf(stderr, |
| 479 | "\t # seek lock : %lu\n" |
| 480 | "\t # seek unlock : %lu (%ld)\n" |
| 481 | "\t # wait time for seek : %.3f msec\n" |
| 482 | "\t # wait time for seek/lock : %.3f nsec\n", |
| 483 | lock_stats[lbl].num_seek_locked, |
| 484 | lock_stats[lbl].num_seek_unlocked, |
| 485 | lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked, |
| 486 | (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0, |
| 487 | lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0); |
| 488 | |
| 489 | if (lock_stats[lbl].num_read_locked) |
| 490 | fprintf(stderr, |
| 491 | "\t # read lock : %lu\n" |
| 492 | "\t # read unlock : %lu (%ld)\n" |
| 493 | "\t # wait time for read : %.3f msec\n" |
| 494 | "\t # wait time for read/lock : %.3f nsec\n", |
| 495 | lock_stats[lbl].num_read_locked, |
| 496 | lock_stats[lbl].num_read_unlocked, |
| 497 | lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked, |
| 498 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 499 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 500 | } |
| 501 | } |
| 502 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 503 | void __ha_rwlock_init(struct ha_rwlock *l) |
| 504 | { |
| 505 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 506 | __RWLOCK_INIT(&l->lock); |
| 507 | } |
| 508 | |
| 509 | void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 510 | { |
| 511 | __RWLOCK_DESTROY(&l->lock); |
| 512 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 517 | const char *func, const char *file, int line) |
| 518 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 519 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 520 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 521 | uint64_t start_time; |
| 522 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 523 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 524 | abort(); |
| 525 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 526 | HA_ATOMIC_OR(&st->wait_writers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 527 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 528 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 529 | __RWLOCK_WRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 530 | 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] | 531 | |
| 532 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 533 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 534 | st->cur_writer = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 535 | l->info.last_location.function = func; |
| 536 | l->info.last_location.file = file; |
| 537 | l->info.last_location.line = line; |
| 538 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 539 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 543 | const char *func, const char *file, int line) |
| 544 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 545 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 546 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 547 | uint64_t start_time; |
| 548 | int r; |
| 549 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 550 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 551 | abort(); |
| 552 | |
| 553 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 554 | HA_ATOMIC_OR(&st->wait_writers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 555 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 556 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 557 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 558 | 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] | 559 | if (unlikely(r)) { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 560 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 561 | return r; |
| 562 | } |
| 563 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 564 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 565 | st->cur_writer = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 566 | l->info.last_location.function = func; |
| 567 | l->info.last_location.file = file; |
| 568 | l->info.last_location.line = line; |
| 569 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 570 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 576 | const char *func, const char *file, int line) |
| 577 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 578 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 579 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
| 580 | |
| 581 | if (unlikely(!(st->cur_writer & tbit))) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 582 | /* the thread is not owning the lock for write */ |
| 583 | abort(); |
| 584 | } |
| 585 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 586 | st->cur_writer = 0; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 587 | l->info.last_location.function = func; |
| 588 | l->info.last_location.file = file; |
| 589 | l->info.last_location.line = line; |
| 590 | |
| 591 | __RWLOCK_WRUNLOCK(&l->lock); |
| 592 | |
| 593 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 594 | } |
| 595 | |
| 596 | void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 597 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 598 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 599 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 600 | uint64_t start_time; |
| 601 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 602 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 603 | abort(); |
| 604 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 605 | HA_ATOMIC_OR(&st->wait_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 606 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 607 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 608 | __RWLOCK_RDLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 609 | 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] | 610 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 611 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 612 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 613 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 614 | HA_ATOMIC_AND(&st->wait_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 618 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 619 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 620 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 621 | int r; |
| 622 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 623 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 624 | abort(); |
| 625 | |
| 626 | /* try read should never wait */ |
| 627 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 628 | if (unlikely(r)) |
| 629 | return r; |
| 630 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 631 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 632 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 638 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 639 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 640 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
| 641 | |
| 642 | if (unlikely(!(st->cur_readers & tbit))) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 643 | /* the thread is not owning the lock for read */ |
| 644 | abort(); |
| 645 | } |
| 646 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 647 | HA_ATOMIC_AND(&st->cur_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 648 | |
| 649 | __RWLOCK_RDUNLOCK(&l->lock); |
| 650 | |
| 651 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked); |
| 652 | } |
| 653 | |
| 654 | void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l, |
| 655 | const char *func, const char *file, int line) |
| 656 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 657 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 658 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 659 | uint64_t start_time; |
| 660 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 661 | if ((st->cur_readers | st->cur_seeker) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 662 | abort(); |
| 663 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 664 | if (!(st->cur_writer & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 665 | abort(); |
| 666 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 667 | HA_ATOMIC_OR(&st->wait_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 668 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 669 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 670 | __RWLOCK_WRTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 671 | 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] | 672 | |
| 673 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 674 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 675 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
| 676 | HA_ATOMIC_AND(&st->cur_writer, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 677 | l->info.last_location.function = func; |
| 678 | l->info.last_location.file = file; |
| 679 | l->info.last_location.line = line; |
| 680 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 681 | HA_ATOMIC_AND(&st->wait_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 685 | const char *func, const char *file, int line) |
| 686 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 687 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 688 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 689 | uint64_t start_time; |
| 690 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 691 | if ((st->cur_readers | st->cur_seeker) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 692 | abort(); |
| 693 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 694 | if (!(st->cur_writer & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 695 | abort(); |
| 696 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 697 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 698 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 699 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 700 | __RWLOCK_WRTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 701 | 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] | 702 | |
| 703 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 704 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 705 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
| 706 | HA_ATOMIC_AND(&st->cur_writer, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 707 | l->info.last_location.function = func; |
| 708 | l->info.last_location.file = file; |
| 709 | l->info.last_location.line = line; |
| 710 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 711 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l, |
| 715 | const char *func, const char *file, int line) |
| 716 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 717 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 718 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 719 | uint64_t start_time; |
| 720 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 721 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 722 | abort(); |
| 723 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 724 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 725 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 726 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 727 | __RWLOCK_SKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 728 | 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] | 729 | |
| 730 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 731 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 732 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 733 | l->info.last_location.function = func; |
| 734 | l->info.last_location.file = file; |
| 735 | l->info.last_location.line = line; |
| 736 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 737 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l, |
| 741 | const char *func, const char *file, int line) |
| 742 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 743 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 744 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 745 | uint64_t start_time; |
| 746 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 747 | if ((st->cur_readers | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 748 | abort(); |
| 749 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 750 | if (!(st->cur_seeker & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 751 | abort(); |
| 752 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 753 | HA_ATOMIC_OR(&st->wait_writers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 754 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 755 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 756 | __RWLOCK_SKTOWR(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 757 | 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] | 758 | |
| 759 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 760 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 761 | HA_ATOMIC_OR(&st->cur_writer, tbit); |
| 762 | HA_ATOMIC_AND(&st->cur_seeker, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 763 | l->info.last_location.function = func; |
| 764 | l->info.last_location.file = file; |
| 765 | l->info.last_location.line = line; |
| 766 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 767 | HA_ATOMIC_AND(&st->wait_writers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l, |
| 771 | const char *func, const char *file, int line) |
| 772 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 773 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 774 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 775 | uint64_t start_time; |
| 776 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 777 | if ((st->cur_readers | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 778 | abort(); |
| 779 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 780 | if (!(st->cur_seeker & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 781 | abort(); |
| 782 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 783 | HA_ATOMIC_OR(&st->wait_readers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 784 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 785 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 786 | __RWLOCK_SKTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 787 | 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] | 788 | |
| 789 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 790 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 791 | HA_ATOMIC_OR(&st->cur_readers, tbit); |
| 792 | HA_ATOMIC_AND(&st->cur_seeker, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 793 | l->info.last_location.function = func; |
| 794 | l->info.last_location.file = file; |
| 795 | l->info.last_location.line = line; |
| 796 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 797 | HA_ATOMIC_AND(&st->wait_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 801 | const char *func, const char *file, int line) |
| 802 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 803 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 804 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
| 805 | if (!(st->cur_seeker & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 806 | abort(); |
| 807 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 808 | HA_ATOMIC_AND(&st->cur_seeker, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 809 | l->info.last_location.function = func; |
| 810 | l->info.last_location.file = file; |
| 811 | l->info.last_location.line = line; |
| 812 | |
| 813 | __RWLOCK_SKUNLOCK(&l->lock); |
| 814 | |
| 815 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked); |
| 816 | } |
| 817 | |
| 818 | int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l, |
| 819 | const char *func, const char *file, int line) |
| 820 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 821 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 822 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 823 | uint64_t start_time; |
| 824 | int r; |
| 825 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 826 | if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 827 | abort(); |
| 828 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 829 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 830 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 831 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 832 | r = __RWLOCK_TRYSKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 833 | 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] | 834 | |
| 835 | if (likely(!r)) { |
| 836 | /* got the lock ! */ |
| 837 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 838 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 839 | l->info.last_location.function = func; |
| 840 | l->info.last_location.file = file; |
| 841 | l->info.last_location.line = line; |
| 842 | } |
| 843 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 844 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 845 | return r; |
| 846 | } |
| 847 | |
| 848 | int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 849 | const char *func, const char *file, int line) |
| 850 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 851 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 852 | struct ha_rwlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 853 | uint64_t start_time; |
| 854 | int r; |
| 855 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 856 | if ((st->cur_writer | st->cur_seeker) & tbit) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 857 | abort(); |
| 858 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 859 | if (!(st->cur_readers & tbit)) |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 860 | abort(); |
| 861 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 862 | HA_ATOMIC_OR(&st->wait_seekers, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 863 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 864 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 865 | r = __RWLOCK_TRYRDTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 866 | 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] | 867 | |
| 868 | if (likely(!r)) { |
| 869 | /* got the lock ! */ |
| 870 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 871 | HA_ATOMIC_OR(&st->cur_seeker, tbit); |
| 872 | HA_ATOMIC_AND(&st->cur_readers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 873 | l->info.last_location.function = func; |
| 874 | l->info.last_location.file = file; |
| 875 | l->info.last_location.line = line; |
| 876 | } |
| 877 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 878 | HA_ATOMIC_AND(&st->wait_seekers, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 879 | return r; |
| 880 | } |
| 881 | |
| 882 | void __spin_init(struct ha_spinlock *l) |
| 883 | { |
| 884 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 885 | __SPIN_INIT(&l->lock); |
| 886 | } |
| 887 | |
| 888 | void __spin_destroy(struct ha_spinlock *l) |
| 889 | { |
| 890 | __SPIN_DESTROY(&l->lock); |
| 891 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 892 | } |
| 893 | |
| 894 | void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 895 | const char *func, const char *file, int line) |
| 896 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 897 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 898 | struct ha_spinlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 899 | uint64_t start_time; |
| 900 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 901 | if (unlikely(st->owner & tbit)) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 902 | /* the thread is already owning the lock */ |
| 903 | abort(); |
| 904 | } |
| 905 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 906 | HA_ATOMIC_OR(&st->waiters, tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 907 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 908 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 909 | __SPIN_LOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 910 | 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] | 911 | |
| 912 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 913 | |
| 914 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 915 | st->owner = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 916 | l->info.last_location.function = func; |
| 917 | l->info.last_location.file = file; |
| 918 | l->info.last_location.line = line; |
| 919 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 920 | HA_ATOMIC_AND(&st->waiters, ~tbit); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 924 | const char *func, const char *file, int line) |
| 925 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 926 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 927 | struct ha_spinlock_state *st = &l->info.st[tgid-1]; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 928 | int r; |
| 929 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 930 | if (unlikely(st->owner & tbit)) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 931 | /* the thread is already owning the lock */ |
| 932 | abort(); |
| 933 | } |
| 934 | |
| 935 | /* try read should never wait */ |
| 936 | r = __SPIN_TRYLOCK(&l->lock); |
| 937 | if (unlikely(r)) |
| 938 | return r; |
| 939 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 940 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 941 | st->owner = tbit; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 942 | l->info.last_location.function = func; |
| 943 | l->info.last_location.file = file; |
| 944 | l->info.last_location.line = line; |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 950 | const char *func, const char *file, int line) |
| 951 | { |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 952 | ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1; |
| 953 | struct ha_spinlock_state *st = &l->info.st[tgid-1]; |
| 954 | |
| 955 | if (unlikely(!(st->owner & tbit))) { |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 956 | /* the thread is not owning the lock */ |
| 957 | abort(); |
| 958 | } |
| 959 | |
Willy Tarreau | 7aa4119 | 2022-07-15 17:53:10 +0200 | [diff] [blame] | 960 | st->owner = 0; |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 961 | l->info.last_location.function = func; |
| 962 | l->info.last_location.file = file; |
| 963 | l->info.last_location.line = line; |
| 964 | |
| 965 | __SPIN_UNLOCK(&l->lock); |
| 966 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 967 | } |
| 968 | |
| 969 | #endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 970 | |
Willy Tarreau | 87aff02 | 2022-07-10 10:58:57 +0200 | [diff] [blame] | 971 | |
| 972 | #if defined(USE_PTHREAD_EMULATION) |
| 973 | |
| 974 | /* pthread rwlock emulation using plocks (to avoid expensive futexes). |
| 975 | * these are a direct mapping on Progressive Locks, with the exception that |
| 976 | * since there's a common unlock operation in pthreads, we need to know if |
| 977 | * we need to unlock for reads or writes, so we set the topmost bit to 1 when |
| 978 | * a write lock is acquired to indicate that a write unlock needs to be |
| 979 | * performed. It's not a problem since this bit will never be used given that |
| 980 | * haproxy won't support as many threads as the plocks. |
| 981 | * |
| 982 | * The storage is the pthread_rwlock_t cast as an ulong |
| 983 | */ |
| 984 | |
| 985 | int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr) |
| 986 | { |
| 987 | ulong *lock = (ulong *)rwlock; |
| 988 | |
| 989 | *lock = 0; |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) |
| 994 | { |
| 995 | ulong *lock = (ulong *)rwlock; |
| 996 | |
| 997 | *lock = 0; |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) |
| 1002 | { |
| 1003 | pl_lorw_rdlock((unsigned long *)rwlock); |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock) |
| 1008 | { |
| 1009 | return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_SHR_BASE); |
| 1010 | } |
| 1011 | |
| 1012 | int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime) |
| 1013 | { |
| 1014 | return pthread_rwlock_tryrdlock(rwlock); |
| 1015 | } |
| 1016 | |
| 1017 | int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) |
| 1018 | { |
| 1019 | pl_lorw_wrlock((unsigned long *)rwlock); |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock) |
| 1024 | { |
| 1025 | return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_EXC_BASE); |
| 1026 | } |
| 1027 | |
| 1028 | int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime) |
| 1029 | { |
| 1030 | return pthread_rwlock_trywrlock(rwlock); |
| 1031 | } |
| 1032 | |
| 1033 | int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) |
| 1034 | { |
| 1035 | pl_lorw_unlock((unsigned long *)rwlock); |
| 1036 | return 0; |
| 1037 | } |
| 1038 | #endif // defined(USE_PTHREAD_EMULATION) |
| 1039 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 1040 | /* Depending on the platform and how libpthread was built, pthread_exit() may |
| 1041 | * involve some code in libgcc_s that would be loaded on exit for the first |
| 1042 | * time, causing aborts if the process is chrooted. It's harmless bit very |
| 1043 | * dirty. There isn't much we can do to make sure libgcc_s is loaded only if |
| 1044 | * needed, so what we do here is that during early boot we create a dummy |
| 1045 | * thread that immediately exits. This will lead to libgcc_s being loaded |
| 1046 | * during boot on the platforms where it's required. |
| 1047 | */ |
| 1048 | static void *dummy_thread_function(void *data) |
| 1049 | { |
| 1050 | pthread_exit(NULL); |
| 1051 | return NULL; |
| 1052 | } |
| 1053 | |
| 1054 | static inline void preload_libgcc_s(void) |
| 1055 | { |
| 1056 | pthread_t dummy_thread; |
| 1057 | pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL); |
| 1058 | pthread_join(dummy_thread, NULL); |
| 1059 | } |
| 1060 | |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 1061 | static void __thread_init(void) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1062 | { |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 1063 | char *ptr = NULL; |
| 1064 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 1065 | preload_libgcc_s(); |
Willy Tarreau | 77b9822 | 2020-09-02 08:04:35 +0200 | [diff] [blame] | 1066 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 1067 | thread_cpus_enabled_at_boot = thread_cpus_enabled(); |
| 1068 | |
Willy Tarreau | c80bdb2 | 2022-08-06 16:44:55 +0200 | [diff] [blame] | 1069 | memprintf(&ptr, "Built with multi-threading support (MAX_TGROUPS=%d, MAX_THREADS=%d, default=%d).", |
| 1070 | MAX_TGROUPS, MAX_THREADS, thread_cpus_enabled_at_boot); |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 1071 | hap_register_build_opts(ptr, 1); |
| 1072 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1073 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 1074 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 1075 | #endif |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1076 | } |
Willy Tarreau | 8ead1d0 | 2022-04-25 19:23:17 +0200 | [diff] [blame] | 1077 | INITCALL0(STG_PREPARE, __thread_init); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1078 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 1079 | #else |
| 1080 | |
Willy Tarreau | aa99276 | 2021-10-06 23:33:20 +0200 | [diff] [blame] | 1081 | /* send signal <sig> to thread <thr> (send to process in fact) */ |
| 1082 | void ha_tkill(unsigned int thr, int sig) |
| 1083 | { |
| 1084 | raise(sig); |
| 1085 | } |
| 1086 | |
| 1087 | /* send signal <sig> to all threads (send to process in fact) */ |
| 1088 | void ha_tkillall(int sig) |
| 1089 | { |
| 1090 | raise(sig); |
| 1091 | } |
| 1092 | |
| 1093 | void ha_thread_relax(void) |
| 1094 | { |
| 1095 | #ifdef _POSIX_PRIORITY_SCHEDULING |
| 1096 | sched_yield(); |
| 1097 | #endif |
| 1098 | } |
| 1099 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 1100 | REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set)."); |
| 1101 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1102 | #endif // USE_THREAD |
| 1103 | |
| 1104 | |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1105 | /* scans the configured thread mapping and establishes the final one. Returns <0 |
| 1106 | * on failure, >=0 on success. |
| 1107 | */ |
| 1108 | int thread_map_to_groups() |
| 1109 | { |
| 1110 | int t, g, ut, ug; |
| 1111 | int q, r; |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1112 | ulong m __maybe_unused; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1113 | |
| 1114 | ut = ug = 0; // unassigned threads & groups |
| 1115 | |
| 1116 | for (t = 0; t < global.nbthread; t++) { |
| 1117 | if (!ha_thread_info[t].tg) |
| 1118 | ut++; |
| 1119 | } |
| 1120 | |
| 1121 | for (g = 0; g < global.nbtgroups; g++) { |
| 1122 | if (!ha_tgroup_info[g].count) |
| 1123 | ug++; |
Willy Tarreau | 60fe4a9 | 2022-06-28 17:48:07 +0200 | [diff] [blame] | 1124 | ha_tgroup_info[g].tgid_bit = 1UL << g; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | if (ug > ut) { |
| 1128 | ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut); |
| 1129 | return -1; |
| 1130 | } |
| 1131 | |
| 1132 | /* look for first unassigned thread */ |
| 1133 | for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++) |
| 1134 | ; |
| 1135 | |
| 1136 | /* assign threads to empty groups */ |
| 1137 | for (g = 0; ug && ut; ) { |
| 1138 | /* due to sparse thread assignment we can end up with more threads |
| 1139 | * per group on last assigned groups than former ones, so we must |
| 1140 | * always try to pack the maximum remaining ones together first. |
| 1141 | */ |
| 1142 | q = ut / ug; |
| 1143 | r = ut % ug; |
| 1144 | if ((q + !!r) > MAX_THREADS_PER_GROUP) { |
| 1145 | 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); |
| 1146 | return -1; |
| 1147 | } |
| 1148 | |
| 1149 | /* thread <t> is the next unassigned one. Let's look for next |
| 1150 | * unassigned group, we know there are some left |
| 1151 | */ |
| 1152 | while (ut >= ug && ha_tgroup_info[g].count) |
| 1153 | g++; |
| 1154 | |
| 1155 | /* group g is unassigned, try to fill it with consecutive threads */ |
| 1156 | while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r && |
| 1157 | (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) { |
| 1158 | |
| 1159 | if (!ha_tgroup_info[g].count) { |
| 1160 | /* assign new group */ |
| 1161 | ha_tgroup_info[g].base = t; |
| 1162 | ug--; |
| 1163 | } |
| 1164 | |
| 1165 | ha_tgroup_info[g].count++; |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1166 | ha_thread_info[t].tgid = g + 1; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1167 | ha_thread_info[t].tg = &ha_tgroup_info[g]; |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 1168 | ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g]; |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1169 | |
| 1170 | ut--; |
| 1171 | /* switch to next unassigned thread */ |
| 1172 | while (++t < global.nbthread && ha_thread_info[t].tg) |
| 1173 | ; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | if (ut) { |
| 1178 | 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); |
| 1179 | return -1; |
| 1180 | } |
| 1181 | |
Willy Tarreau | cc7a11e | 2021-09-28 08:53:11 +0200 | [diff] [blame] | 1182 | for (t = 0; t < global.nbthread; t++) { |
| 1183 | ha_thread_info[t].tid = t; |
| 1184 | ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base; |
Willy Tarreau | cc7a11e | 2021-09-28 08:53:11 +0200 | [diff] [blame] | 1185 | ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid; |
| 1186 | } |
| 1187 | |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1188 | m = 0; |
Willy Tarreau | 377e37a | 2022-06-24 15:18:49 +0200 | [diff] [blame] | 1189 | for (g = 0; g < global.nbtgroups; g++) { |
| 1190 | ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count); |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1191 | if (!ha_tgroup_info[g].count) |
| 1192 | continue; |
| 1193 | m |= 1UL << g; |
Willy Tarreau | 377e37a | 2022-06-24 15:18:49 +0200 | [diff] [blame] | 1194 | |
| 1195 | } |
| 1196 | |
Willy Tarreau | cce203a | 2022-06-24 15:55:11 +0200 | [diff] [blame] | 1197 | #ifdef USE_THREAD |
| 1198 | all_tgroups_mask = m; |
| 1199 | #endif |
Willy Tarreau | e6806eb | 2021-09-27 10:10:26 +0200 | [diff] [blame] | 1200 | return 0; |
| 1201 | } |
| 1202 | |
Willy Tarreau | 6018c02 | 2022-06-28 08:27:43 +0200 | [diff] [blame] | 1203 | /* converts a configuration thread num or group+mask to a global group+mask |
| 1204 | * depending on the configured thread group id. This is essentially for use |
| 1205 | * with the "thread" directive on "bind" lines, where "thread 4-6" might be |
| 1206 | * turned to "2/1-3". It cannot be used before the thread mapping above was |
| 1207 | * completed and the thread group number configured. Possible options: |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1208 | * - igid == 0: imask represents global IDs. We have to check that all |
| 1209 | * configured threads in the mask belong to the same group. If imask is zero |
| 1210 | * 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] | 1211 | * - igid > 0, imask = 0: convert global values to local values for this thread |
| 1212 | * - igid > 0, imask > 0: convert global values to local values |
| 1213 | * Note that the output mask is always local to the group. |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1214 | * |
| 1215 | * Returns <0 on failure, >=0 on success. |
| 1216 | */ |
| 1217 | int thread_resolve_group_mask(uint igid, ulong imask, uint *ogid, ulong *omask, char **err) |
| 1218 | { |
| 1219 | ulong mask; |
| 1220 | uint t; |
| 1221 | |
| 1222 | if (igid == 0) { |
| 1223 | /* unspecified group, IDs are global */ |
| 1224 | if (!imask) { |
| 1225 | /* all threads of all groups */ |
| 1226 | if (global.nbtgroups > 1) { |
| 1227 | memprintf(err, "'thread' directive spans multiple groups"); |
| 1228 | return -1; |
| 1229 | } |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1230 | *ogid = 1; // first and only group |
Willy Tarreau | 6018c02 | 2022-06-28 08:27:43 +0200 | [diff] [blame] | 1231 | *omask = ha_tgroup_info[0].threads_enabled; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1232 | return 0; |
| 1233 | } else { |
| 1234 | /* some global threads */ |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1235 | for (t = 0; t < global.nbthread; t++) { |
| 1236 | if (imask & (1UL << t)) { |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1237 | if (ha_thread_info[t].tgid != igid) { |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1238 | if (!igid) |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1239 | igid = ha_thread_info[t].tgid; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1240 | else { |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1241 | 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] | 1242 | return -1; |
| 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | if (!igid) { |
| 1249 | memprintf(err, "'thread' directive contains threads that belong to no group"); |
| 1250 | return -1; |
| 1251 | } |
| 1252 | |
| 1253 | /* we have a valid group, convert this to global thread IDs */ |
| 1254 | *ogid = igid; |
Willy Tarreau | 6018c02 | 2022-06-28 08:27:43 +0200 | [diff] [blame] | 1255 | imask = imask >> ha_tgroup_info[igid - 1].base; |
| 1256 | imask &= ha_tgroup_info[igid - 1].threads_enabled; |
| 1257 | *omask = imask; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1258 | return 0; |
| 1259 | } |
| 1260 | } else { |
| 1261 | /* group was specified */ |
| 1262 | if (igid > global.nbtgroups) { |
| 1263 | memprintf(err, "'thread' directive references non-existing thread group %u", igid); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
| 1267 | if (!imask) { |
| 1268 | /* all threads of this groups. Let's make a mask from their count and base. */ |
| 1269 | *ogid = igid; |
Willy Tarreau | 6018c02 | 2022-06-28 08:27:43 +0200 | [diff] [blame] | 1270 | *omask = nbits(ha_tgroup_info[igid - 1].count); |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1271 | return 0; |
| 1272 | } else { |
| 1273 | /* some local threads. Keep only existing ones for this group */ |
| 1274 | |
Willy Tarreau | 6018c02 | 2022-06-28 08:27:43 +0200 | [diff] [blame] | 1275 | mask = nbits(ha_tgroup_info[igid - 1].count); |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1276 | |
| 1277 | if (!(mask & imask)) { |
| 1278 | /* no intersection between the thread group's |
| 1279 | * threads and the bind line's. |
| 1280 | */ |
| 1281 | #ifdef THREAD_AUTO_ADJUST_GROUPS |
| 1282 | unsigned long new_mask = 0; |
| 1283 | |
| 1284 | while (imask) { |
| 1285 | new_mask |= imask & mask; |
| 1286 | imask >>= ha_tgroup_info[igid - 1].count; |
| 1287 | } |
| 1288 | imask = new_mask; |
| 1289 | #else |
| 1290 | memprintf(err, "'thread' directive only references threads not belonging to the group"); |
| 1291 | return -1; |
| 1292 | #endif |
| 1293 | } |
| 1294 | |
Willy Tarreau | 6018c02 | 2022-06-28 08:27:43 +0200 | [diff] [blame] | 1295 | *omask = mask & imask; |
Willy Tarreau | 627def9 | 2021-09-29 18:59:47 +0200 | [diff] [blame] | 1296 | *ogid = igid; |
| 1297 | return 0; |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1302 | /* Parse the "nbthread" global directive, which takes an integer argument that |
| 1303 | * contains the desired number of threads. |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1304 | */ |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1305 | static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx, |
| 1306 | const struct proxy *defpx, const char *file, int line, |
| 1307 | char **err) |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1308 | { |
| 1309 | long nbthread; |
| 1310 | char *errptr; |
| 1311 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1312 | if (too_many_args(1, args, err, NULL)) |
| 1313 | return -1; |
| 1314 | |
| 1315 | nbthread = strtol(args[1], &errptr, 10); |
| 1316 | if (!*args[1] || *errptr) { |
| 1317 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1318 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | #ifndef USE_THREAD |
| 1322 | if (nbthread != 1) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1323 | 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]); |
| 1324 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1325 | } |
| 1326 | #else |
| 1327 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1328 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread); |
| 1329 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1330 | } |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1331 | #endif |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1332 | |
| 1333 | HA_DIAG_WARNING_COND(global.nbthread, |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1334 | "parsing [%s:%d] : '%s' is already defined and will be overridden.\n", |
| 1335 | file, line, args[0]); |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1336 | |
| 1337 | global.nbthread = nbthread; |
| 1338 | return 0; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1339 | } |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1340 | |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1341 | /* Parse the "thread-group" global directive, which takes an integer argument |
| 1342 | * that designates a thread group, and a list of threads to put into that group. |
| 1343 | */ |
| 1344 | static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx, |
| 1345 | const struct proxy *defpx, const char *file, int line, |
| 1346 | char **err) |
| 1347 | { |
| 1348 | char *errptr; |
| 1349 | long tnum, tend, tgroup; |
| 1350 | int arg, tot; |
| 1351 | |
| 1352 | tgroup = strtol(args[1], &errptr, 10); |
| 1353 | if (!*args[1] || *errptr) { |
| 1354 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1355 | return -1; |
| 1356 | } |
| 1357 | |
| 1358 | if (tgroup < 1 || tgroup > MAX_TGROUPS) { |
| 1359 | memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup); |
| 1360 | return -1; |
| 1361 | } |
| 1362 | |
| 1363 | /* look for a preliminary definition of any thread pointing to this |
| 1364 | * group, and remove them. |
| 1365 | */ |
| 1366 | if (ha_tgroup_info[tgroup-1].count) { |
| 1367 | ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n", |
| 1368 | file, line, args[0], tgroup); |
| 1369 | |
| 1370 | for (tnum = ha_tgroup_info[tgroup-1].base; |
| 1371 | tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count; |
| 1372 | tnum++) { |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1373 | if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) { |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1374 | ha_thread_info[tnum-1].tg = NULL; |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1375 | ha_thread_info[tnum-1].tgid = 0; |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 1376 | ha_thread_info[tnum-1].tg_ctx = NULL; |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1377 | } |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1378 | } |
| 1379 | ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0; |
| 1380 | } |
| 1381 | |
| 1382 | tot = 0; |
| 1383 | for (arg = 2; args[arg] && *args[arg]; arg++) { |
| 1384 | tend = tnum = strtol(args[arg], &errptr, 10); |
| 1385 | |
| 1386 | if (*errptr == '-') |
| 1387 | tend = strtol(errptr + 1, &errptr, 10); |
| 1388 | |
| 1389 | if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) { |
| 1390 | 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); |
| 1391 | return -1; |
| 1392 | } |
| 1393 | |
| 1394 | for(; tnum <= tend; tnum++) { |
| 1395 | if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) { |
| 1396 | ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n", |
| 1397 | file, line, args[0], tgroup, tnum); |
| 1398 | } else if (ha_thread_info[tnum-1].tg) { |
| 1399 | ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n", |
| 1400 | file, line, args[0], tgroup, tnum, |
| 1401 | (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1)); |
| 1402 | } |
| 1403 | |
| 1404 | if (!ha_tgroup_info[tgroup-1].count) { |
| 1405 | ha_tgroup_info[tgroup-1].base = tnum-1; |
| 1406 | ha_tgroup_info[tgroup-1].count = 1; |
| 1407 | } |
| 1408 | else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) { |
| 1409 | ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base; |
| 1410 | } |
| 1411 | else if (tnum < ha_tgroup_info[tgroup-1].base) { |
| 1412 | ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1; |
| 1413 | ha_tgroup_info[tgroup-1].base = tnum - 1; |
| 1414 | } |
| 1415 | |
Willy Tarreau | 66ad98a | 2022-06-28 10:49:57 +0200 | [diff] [blame] | 1416 | ha_thread_info[tnum-1].tgid = tgroup; |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1417 | ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1]; |
Willy Tarreau | 03f9b35 | 2022-06-27 16:02:24 +0200 | [diff] [blame] | 1418 | ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1]; |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1419 | tot++; |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | if (ha_tgroup_info[tgroup-1].count > tot) { |
| 1424 | memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup); |
| 1425 | return -1; |
| 1426 | } |
| 1427 | |
| 1428 | if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) { |
| 1429 | memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP); |
| 1430 | return -1; |
| 1431 | } |
| 1432 | |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1436 | /* Parse the "thread-groups" global directive, which takes an integer argument |
| 1437 | * that contains the desired number of thread groups. |
| 1438 | */ |
| 1439 | static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx, |
| 1440 | const struct proxy *defpx, const char *file, int line, |
| 1441 | char **err) |
| 1442 | { |
| 1443 | long nbtgroups; |
| 1444 | char *errptr; |
| 1445 | |
| 1446 | if (too_many_args(1, args, err, NULL)) |
| 1447 | return -1; |
| 1448 | |
| 1449 | nbtgroups = strtol(args[1], &errptr, 10); |
| 1450 | if (!*args[1] || *errptr) { |
| 1451 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 1452 | return -1; |
| 1453 | } |
| 1454 | |
| 1455 | #ifndef USE_THREAD |
| 1456 | if (nbtgroups != 1) { |
| 1457 | 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]); |
| 1458 | return -1; |
| 1459 | } |
| 1460 | #else |
| 1461 | if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) { |
| 1462 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups); |
| 1463 | return -1; |
| 1464 | } |
| 1465 | #endif |
| 1466 | |
| 1467 | HA_DIAG_WARNING_COND(global.nbtgroups, |
| 1468 | "parsing [%s:%d] : '%s' is already defined and will be overridden.\n", |
| 1469 | file, line, args[0]); |
| 1470 | |
| 1471 | global.nbtgroups = nbtgroups; |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1475 | /* config keyword parsers */ |
| 1476 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1477 | { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 }, |
Willy Tarreau | d04bc3a | 2021-09-27 13:55:10 +0200 | [diff] [blame] | 1478 | { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 }, |
Willy Tarreau | c33b969 | 2021-09-22 12:07:23 +0200 | [diff] [blame] | 1479 | { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 }, |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1480 | { 0, NULL, NULL } |
| 1481 | }}; |
| 1482 | |
| 1483 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |