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