blob: 64ac476a35aadfcadef07ed30f901b65f1372498 [file] [log] [blame]
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001/*
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 Tarreau149ab772019-01-26 14:27:06 +010013#define _GNU_SOURCE
Christopher Faulet339fff82017-10-19 11:59:15 +020014#include <unistd.h>
Willy Tarreau0ccd3222018-07-30 10:34:35 +020015#include <stdlib.h>
Christopher Faulet339fff82017-10-19 11:59:15 +020016
Willy Tarreauaa992762021-10-06 23:33:20 +020017#include <signal.h>
18#include <unistd.h>
19#ifdef _POSIX_PRIORITY_SCHEDULING
20#include <sched.h>
21#endif
22
Willy Tarreau5e03dfa2021-10-06 22:53:51 +020023#ifdef USE_THREAD
24# include <pthread.h>
25#endif
26
Willy Tarreau149ab772019-01-26 14:27:06 +010027#ifdef USE_CPU_AFFINITY
Willy Tarreaud10385a2021-10-06 22:22:40 +020028# 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 Tarreau149ab772019-01-26 14:27:06 +010042#endif
43
Willy Tarreau6be78492020-06-05 00:00:29 +020044#include <haproxy/cfgparse.h>
Willy Tarreau55542642021-10-08 09:33:24 +020045#include <haproxy/clock.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/fd.h>
47#include <haproxy/global.h>
Willy Tarreau11bd6f72021-05-08 20:33:02 +020048#include <haproxy/log.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020049#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020050#include <haproxy/tools.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020051
Willy Tarreauf9662842021-09-13 18:11:26 +020052struct tgroup_info ha_tgroup_info[MAX_TGROUPS] = { };
53THREAD_LOCAL const struct tgroup_info *tg = &ha_tgroup_info[0];
54
David Carliera92c5ce2019-09-13 05:03:12 +010055struct thread_info ha_thread_info[MAX_THREADS] = { };
Willy Tarreau60363422021-10-01 16:29:27 +020056THREAD_LOCAL const struct thread_info *ti = &ha_thread_info[0];
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020057
Willy Tarreau03f9b352022-06-27 16:02:24 +020058struct tgroup_ctx ha_tgroup_ctx[MAX_TGROUPS] = { };
59THREAD_LOCAL struct tgroup_ctx *tg_ctx = &ha_tgroup_ctx[0];
60
Willy Tarreau1a9c9222021-10-01 11:30:33 +020061struct thread_ctx ha_thread_ctx[MAX_THREADS] = { };
62THREAD_LOCAL struct thread_ctx *th_ctx = &ha_thread_ctx[0];
63
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020064#ifdef USE_THREAD
65
Willy Tarreaucce203a2022-06-24 15:55:11 +020066volatile unsigned long all_tgroups_mask __read_mostly = 1; // nbtgroup 1 assumed by default
Willy Tarreau598cf3f2022-07-01 15:08:37 +020067volatile unsigned int rdv_requests = 0; // total number of threads requesting RDV
68volatile unsigned int isolated_thread = ~0; // ID of the isolated thread, or ~0 when none
Willy Tarreaub90935c2021-09-30 08:00:11 +020069THREAD_LOCAL unsigned int tgid = 1; // thread ID starts at 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020070THREAD_LOCAL unsigned int tid = 0;
Willy Tarreau149ab772019-01-26 14:27:06 +010071int thread_cpus_enabled_at_boot = 1;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +020072static pthread_t ha_pthread[MAX_THREADS] = { };
Willy Tarreau0c026f42018-08-01 19:12:20 +020073
Willy Tarreau60b639c2018-08-02 10:16:17 +020074/* Marks the thread as harmless until the last thread using the rendez-vous
Willy Tarreau598cf3f2022-07-01 15:08:37 +020075 * point quits. Given that we can wait for a long time, sched_yield() is
Christopher Fauleta9a9e9a2021-03-25 14:11:36 +010076 * used when available to offer the CPU resources to competing threads if
77 * needed.
Willy Tarreau60b639c2018-08-02 10:16:17 +020078 */
79void thread_harmless_till_end()
80{
Willy Tarreau03f9b352022-06-27 16:02:24 +020081 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +020082 while (_HA_ATOMIC_LOAD(&rdv_requests) != 0) {
Willy Tarreau286363b2021-08-04 10:33:57 +020083 ha_thread_relax();
84 }
Willy Tarreau60b639c2018-08-02 10:16:17 +020085}
86
87/* Isolates the current thread : request the ability to work while all other
Willy Tarreauf519cfa2021-08-04 11:22:07 +020088 * 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 Tarreau03f9b352022-06-27 16:02:24 +020090 * harmless, with the current thread's bit in &tg_ctx->threads_harmless cleared.
Willy Tarreauf519cfa2021-08-04 11:22:07 +020091 * Needs to be completed using thread_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +020092 */
93void thread_isolate()
94{
Willy Tarreau598cf3f2022-07-01 15:08:37 +020095 uint tgrp, thr;
Willy Tarreau60b639c2018-08-02 10:16:17 +020096
Willy Tarreau03f9b352022-06-27 16:02:24 +020097 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Olivier Houchardb23a61f2019-03-08 18:51:17 +010098 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +020099 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200100
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200101 /* 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 Tarreau60b639c2018-08-02 10:16:17 +0200105 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200106 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 Tarreau60b639c2018-08-02 10:16:17 +0200111
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200112 /* 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 Tarreau38171da2019-05-17 16:33:13 +0200122 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +0200123 }
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200124
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 Tarreau60b639c2018-08-02 10:16:17 +0200131 */
132}
133
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200134/* 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 Tarreau03f9b352022-06-27 16:02:24 +0200137 * &tg_ctx->threads_harmless and idle_mask cleared. Needs to be completed using
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200138 * 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 */
148void thread_isolate_full()
149{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200150 uint tgrp, thr;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200151
Willy Tarreau03f9b352022-06-27 16:02:24 +0200152 _HA_ATOMIC_OR(&tg_ctx->threads_idle, ti->ltid_bit);
153 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200154 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200155 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200156
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200157 /* 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 Tarreau88d1c5d2021-08-04 11:44:17 +0200161 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200162 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 Tarreau88d1c5d2021-08-04 11:44:17 +0200168
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200169 /* 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 Tarreau88d1c5d2021-08-04 11:44:17 +0200179 ha_thread_relax();
180 }
181
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200182 /* 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 Tarreau88d1c5d2021-08-04 11:44:17 +0200185 */
Willy Tarreau03f9b352022-06-27 16:02:24 +0200186 _HA_ATOMIC_AND(&tg_ctx->threads_idle, ~ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200187 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200188}
189
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200190/* 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 Tarreau60b639c2018-08-02 10:16:17 +0200195 */
196void thread_release()
197{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200198 HA_ATOMIC_STORE(&isolated_thread, ~0U);
199 HA_ATOMIC_DEC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200200}
Christopher Faulet339fff82017-10-19 11:59:15 +0200201
Willy Tarreaud10385a2021-10-06 22:22:40 +0200202/* Sets up threads, signals and masks, and starts threads 2 and above.
203 * Does nothing when threads are disabled.
204 */
205void 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 Tarreau5e03dfa2021-10-06 22:53:51 +0200220 ha_pthread[0] = pthread_self();
Willy Tarreaud10385a2021-10-06 22:22:40 +0200221 for (i = 1; i < global.nbthread; i++)
Willy Tarreau43ab05b2021-09-28 09:43:11 +0200222 pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200223}
224
225/* waits for all threads to terminate. Does nothing when threads are
226 * disabled.
227 */
228void 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 Tarreau5e03dfa2021-10-06 22:53:51 +0200234 pthread_join(ha_pthread[i], NULL);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200235
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 */
242void 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 Tarreau5b093412022-07-08 09:38:30 +0200250 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 Tarreaud10385a2021-10-06 22:22:40 +0200252
Willy Tarreau5b093412022-07-08 09:38:30 +0200253 if (ha_cpuset_count(&cpu_map[tgid - 1].thread[ti->ltid])) {/* only do this if the thread has a THREAD map */
Willy Tarreaud10385a2021-10-06 22:22:40 +0200254# if defined(__APPLE__)
255 /* Note: this API is limited to the first 32/64 CPUs */
Willy Tarreau5b093412022-07-08 09:38:30 +0200256 unsigned long set = cpu_map[tgid - 1].thread[ti->ltid].cpuset;
Willy Tarreaud10385a2021-10-06 22:22:40 +0200257 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 Tarreau5e03dfa2021-10-06 22:53:51 +0200263 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200264 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
265 set &= ~(1UL << (j - 1));
266 }
267# else
Willy Tarreau5b093412022-07-08 09:38:30 +0200268 struct hap_cpuset *set = &cpu_map[tgid - 1].thread[ti->ltid];
Willy Tarreaud10385a2021-10-06 22:22:40 +0200269
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200270 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200271# endif
272 }
273#endif /* USE_CPU_AFFINITY */
274}
275
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200276/* 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 */
285unsigned 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 Tarreau5e03dfa2021-10-06 22:53:51 +0200295 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200296
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 Tarreau2beaaf72019-05-22 08:43:34 +0200306/* send signal <sig> to thread <thr> */
307void ha_tkill(unsigned int thr, int sig)
308{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200309 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200310}
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 */
315void ha_tkillall(int sig)
316{
317 unsigned int thr;
318
319 for (thr = 0; thr < global.nbthread; thr++) {
Willy Tarreauf15c75a2022-07-15 08:27:56 +0200320 if (!(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit))
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200321 continue;
322 if (thr == tid)
323 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200324 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200325 }
326 raise(sig);
327}
328
Willy Tarreauaa992762021-10-06 23:33:20 +0200329void ha_thread_relax(void)
330{
331#ifdef _POSIX_PRIORITY_SCHEDULING
332 sched_yield();
333#else
334 pl_cpu_relax();
335#endif
336}
337
Willy Tarreau3d184982020-10-18 10:20:59 +0200338/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100339void ha_spin_init(HA_SPINLOCK_T *l)
340{
341 HA_SPIN_INIT(l);
342}
343
Willy Tarreau3d184982020-10-18 10:20:59 +0200344/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100345void ha_rwlock_init(HA_RWLOCK_T *l)
346{
347 HA_RWLOCK_INIT(l);
348}
349
Willy Tarreau149ab772019-01-26 14:27:06 +0100350/* returns the number of CPUs the current process is enabled to run on */
351static 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 Houchard46453d32019-04-11 00:06:47 +0200361#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 CARLIER6a906012021-01-15 08:09:56 +0000366#elif defined(__APPLE__)
367 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100368#endif
369#endif
370 ret = MAX(ret, 1);
371 ret = MIN(ret, MAX_THREADS);
372 return ret;
373}
374
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200375/* Returns 1 if the cpu set is currently restricted for the process else 0.
376 * Currently only implemented for the Linux platform.
377 */
378int 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 Tarreau407ef892021-10-05 18:39:27 +0200388/* Below come the lock-debugging functions */
389
390#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
391
392struct lock_stat lock_stats[LOCK_LABELS];
393
394/* this is only used below */
395static 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
447void 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 Tarreau407ef892021-10-05 18:39:27 +0200503void __ha_rwlock_init(struct ha_rwlock *l)
504{
505 memset(l, 0, sizeof(struct ha_rwlock));
506 __RWLOCK_INIT(&l->lock);
507}
508
509void __ha_rwlock_destroy(struct ha_rwlock *l)
510{
511 __RWLOCK_DESTROY(&l->lock);
512 memset(l, 0, sizeof(struct ha_rwlock));
513}
514
515
516void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
517 const char *func, const char *file, int line)
518{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200519 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
520 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200521 uint64_t start_time;
522
Willy Tarreau7aa41192022-07-15 17:53:10 +0200523 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200524 abort();
525
Willy Tarreau7aa41192022-07-15 17:53:10 +0200526 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200527
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200528 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200529 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200530 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200531
532 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
533
Willy Tarreau7aa41192022-07-15 17:53:10 +0200534 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200535 l->info.last_location.function = func;
536 l->info.last_location.file = file;
537 l->info.last_location.line = line;
538
Willy Tarreau7aa41192022-07-15 17:53:10 +0200539 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200540}
541
542int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
543 const char *func, const char *file, int line)
544{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200545 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
546 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200547 uint64_t start_time;
548 int r;
549
Willy Tarreau7aa41192022-07-15 17:53:10 +0200550 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200551 abort();
552
553 /* We set waiting writer because trywrlock could wait for readers to quit */
Willy Tarreau7aa41192022-07-15 17:53:10 +0200554 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200555
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200556 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200557 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200558 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200559 if (unlikely(r)) {
Willy Tarreau7aa41192022-07-15 17:53:10 +0200560 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200561 return r;
562 }
563 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
564
Willy Tarreau7aa41192022-07-15 17:53:10 +0200565 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200566 l->info.last_location.function = func;
567 l->info.last_location.file = file;
568 l->info.last_location.line = line;
569
Willy Tarreau7aa41192022-07-15 17:53:10 +0200570 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200571
572 return 0;
573}
574
575void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
576 const char *func, const char *file, int line)
577{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200578 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 Tarreau407ef892021-10-05 18:39:27 +0200582 /* the thread is not owning the lock for write */
583 abort();
584 }
585
Willy Tarreau7aa41192022-07-15 17:53:10 +0200586 st->cur_writer = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200587 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
596void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
597{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200598 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
599 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200600 uint64_t start_time;
601
Willy Tarreau7aa41192022-07-15 17:53:10 +0200602 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200603 abort();
604
Willy Tarreau7aa41192022-07-15 17:53:10 +0200605 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200606
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200607 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200608 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200609 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200610 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
611
Willy Tarreau7aa41192022-07-15 17:53:10 +0200612 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200613
Willy Tarreau7aa41192022-07-15 17:53:10 +0200614 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200615}
616
617int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
618{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200619 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
620 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200621 int r;
622
Willy Tarreau7aa41192022-07-15 17:53:10 +0200623 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200624 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 Tarreau7aa41192022-07-15 17:53:10 +0200632 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200633
634 return 0;
635}
636
637void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
638{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200639 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 Tarreau407ef892021-10-05 18:39:27 +0200643 /* the thread is not owning the lock for read */
644 abort();
645 }
646
Willy Tarreau7aa41192022-07-15 17:53:10 +0200647 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200648
649 __RWLOCK_RDUNLOCK(&l->lock);
650
651 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
652}
653
654void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
655 const char *func, const char *file, int line)
656{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200657 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
658 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200659 uint64_t start_time;
660
Willy Tarreau7aa41192022-07-15 17:53:10 +0200661 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200662 abort();
663
Willy Tarreau7aa41192022-07-15 17:53:10 +0200664 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200665 abort();
666
Willy Tarreau7aa41192022-07-15 17:53:10 +0200667 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200668
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200669 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200670 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200671 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200672
673 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
674
Willy Tarreau7aa41192022-07-15 17:53:10 +0200675 HA_ATOMIC_OR(&st->cur_readers, tbit);
676 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200677 l->info.last_location.function = func;
678 l->info.last_location.file = file;
679 l->info.last_location.line = line;
680
Willy Tarreau7aa41192022-07-15 17:53:10 +0200681 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200682}
683
684void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
685 const char *func, const char *file, int line)
686{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200687 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
688 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200689 uint64_t start_time;
690
Willy Tarreau7aa41192022-07-15 17:53:10 +0200691 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200692 abort();
693
Willy Tarreau7aa41192022-07-15 17:53:10 +0200694 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200695 abort();
696
Willy Tarreau7aa41192022-07-15 17:53:10 +0200697 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200698
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200699 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200700 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200701 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200702
703 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
704
Willy Tarreau7aa41192022-07-15 17:53:10 +0200705 HA_ATOMIC_OR(&st->cur_seeker, tbit);
706 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200707 l->info.last_location.function = func;
708 l->info.last_location.file = file;
709 l->info.last_location.line = line;
710
Willy Tarreau7aa41192022-07-15 17:53:10 +0200711 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200712}
713
714void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
715 const char *func, const char *file, int line)
716{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200717 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
718 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200719 uint64_t start_time;
720
Willy Tarreau7aa41192022-07-15 17:53:10 +0200721 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200722 abort();
723
Willy Tarreau7aa41192022-07-15 17:53:10 +0200724 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200725
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200726 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200727 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200728 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200729
730 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
731
Willy Tarreau7aa41192022-07-15 17:53:10 +0200732 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200733 l->info.last_location.function = func;
734 l->info.last_location.file = file;
735 l->info.last_location.line = line;
736
Willy Tarreau7aa41192022-07-15 17:53:10 +0200737 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200738}
739
740void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
741 const char *func, const char *file, int line)
742{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200743 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
744 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200745 uint64_t start_time;
746
Willy Tarreau7aa41192022-07-15 17:53:10 +0200747 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200748 abort();
749
Willy Tarreau7aa41192022-07-15 17:53:10 +0200750 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200751 abort();
752
Willy Tarreau7aa41192022-07-15 17:53:10 +0200753 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200754
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200755 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200756 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200757 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200758
759 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
760
Willy Tarreau7aa41192022-07-15 17:53:10 +0200761 HA_ATOMIC_OR(&st->cur_writer, tbit);
762 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200763 l->info.last_location.function = func;
764 l->info.last_location.file = file;
765 l->info.last_location.line = line;
766
Willy Tarreau7aa41192022-07-15 17:53:10 +0200767 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200768}
769
770void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
771 const char *func, const char *file, int line)
772{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200773 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
774 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200775 uint64_t start_time;
776
Willy Tarreau7aa41192022-07-15 17:53:10 +0200777 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200778 abort();
779
Willy Tarreau7aa41192022-07-15 17:53:10 +0200780 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200781 abort();
782
Willy Tarreau7aa41192022-07-15 17:53:10 +0200783 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200784
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200785 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200786 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200787 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200788
789 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
790
Willy Tarreau7aa41192022-07-15 17:53:10 +0200791 HA_ATOMIC_OR(&st->cur_readers, tbit);
792 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200793 l->info.last_location.function = func;
794 l->info.last_location.file = file;
795 l->info.last_location.line = line;
796
Willy Tarreau7aa41192022-07-15 17:53:10 +0200797 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200798}
799
800void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
801 const char *func, const char *file, int line)
802{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200803 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 Tarreau407ef892021-10-05 18:39:27 +0200806 abort();
807
Willy Tarreau7aa41192022-07-15 17:53:10 +0200808 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200809 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
818int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
819 const char *func, const char *file, int line)
820{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200821 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
822 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200823 uint64_t start_time;
824 int r;
825
Willy Tarreau7aa41192022-07-15 17:53:10 +0200826 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200827 abort();
828
Willy Tarreau7aa41192022-07-15 17:53:10 +0200829 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200830
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200831 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200832 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200833 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200834
835 if (likely(!r)) {
836 /* got the lock ! */
837 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200838 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200839 l->info.last_location.function = func;
840 l->info.last_location.file = file;
841 l->info.last_location.line = line;
842 }
843
Willy Tarreau7aa41192022-07-15 17:53:10 +0200844 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200845 return r;
846}
847
848int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
849 const char *func, const char *file, int line)
850{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200851 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
852 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200853 uint64_t start_time;
854 int r;
855
Willy Tarreau7aa41192022-07-15 17:53:10 +0200856 if ((st->cur_writer | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200857 abort();
858
Willy Tarreau7aa41192022-07-15 17:53:10 +0200859 if (!(st->cur_readers & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200860 abort();
861
Willy Tarreau7aa41192022-07-15 17:53:10 +0200862 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200863
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200864 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200865 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200866 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200867
868 if (likely(!r)) {
869 /* got the lock ! */
870 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200871 HA_ATOMIC_OR(&st->cur_seeker, tbit);
872 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200873 l->info.last_location.function = func;
874 l->info.last_location.file = file;
875 l->info.last_location.line = line;
876 }
877
Willy Tarreau7aa41192022-07-15 17:53:10 +0200878 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200879 return r;
880}
881
882void __spin_init(struct ha_spinlock *l)
883{
884 memset(l, 0, sizeof(struct ha_spinlock));
885 __SPIN_INIT(&l->lock);
886}
887
888void __spin_destroy(struct ha_spinlock *l)
889{
890 __SPIN_DESTROY(&l->lock);
891 memset(l, 0, sizeof(struct ha_spinlock));
892}
893
894void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
895 const char *func, const char *file, int line)
896{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200897 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
898 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200899 uint64_t start_time;
900
Willy Tarreau7aa41192022-07-15 17:53:10 +0200901 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200902 /* the thread is already owning the lock */
903 abort();
904 }
905
Willy Tarreau7aa41192022-07-15 17:53:10 +0200906 HA_ATOMIC_OR(&st->waiters, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200907
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200908 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200909 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200910 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200911
912 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
913
914
Willy Tarreau7aa41192022-07-15 17:53:10 +0200915 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200916 l->info.last_location.function = func;
917 l->info.last_location.file = file;
918 l->info.last_location.line = line;
919
Willy Tarreau7aa41192022-07-15 17:53:10 +0200920 HA_ATOMIC_AND(&st->waiters, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200921}
922
923int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
924 const char *func, const char *file, int line)
925{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200926 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
927 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200928 int r;
929
Willy Tarreau7aa41192022-07-15 17:53:10 +0200930 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200931 /* 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 Tarreau7aa41192022-07-15 17:53:10 +0200941 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200942 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
949void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
950 const char *func, const char *file, int line)
951{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200952 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 Tarreau407ef892021-10-05 18:39:27 +0200956 /* the thread is not owning the lock */
957 abort();
958 }
959
Willy Tarreau7aa41192022-07-15 17:53:10 +0200960 st->owner = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200961 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 Tarreau87aff022022-07-10 10:58:57 +0200971
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
985int 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
993int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
994{
995 ulong *lock = (ulong *)rwlock;
996
997 *lock = 0;
998 return 0;
999}
1000
1001int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
1002{
1003 pl_lorw_rdlock((unsigned long *)rwlock);
1004 return 0;
1005}
1006
1007int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
1008{
1009 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_SHR_BASE);
1010}
1011
1012int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1013{
1014 return pthread_rwlock_tryrdlock(rwlock);
1015}
1016
1017int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
1018{
1019 pl_lorw_wrlock((unsigned long *)rwlock);
1020 return 0;
1021}
1022
1023int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
1024{
1025 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_EXC_BASE);
1026}
1027
1028int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1029{
1030 return pthread_rwlock_trywrlock(rwlock);
1031}
1032
1033int 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 Tarreauf734ebf2020-09-09 17:07:54 +02001040/* 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 */
1048static void *dummy_thread_function(void *data)
1049{
1050 pthread_exit(NULL);
1051 return NULL;
1052}
1053
1054static 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 Tarreau3f567e42020-05-28 15:29:19 +02001061static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001062{
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001063 char *ptr = NULL;
1064
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001065 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +02001066
Willy Tarreau149ab772019-01-26 14:27:06 +01001067 thread_cpus_enabled_at_boot = thread_cpus_enabled();
1068
Willy Tarreauc80bdb22022-08-06 16:44:55 +02001069 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 Tarreauf5809cd2019-01-26 13:35:03 +01001071 hap_register_build_opts(ptr, 1);
1072
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001073#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
1074 memset(lock_stats, 0, sizeof(lock_stats));
1075#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001076}
Willy Tarreau8ead1d02022-04-25 19:23:17 +02001077INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001078
Willy Tarreau8459f252018-12-15 16:48:14 +01001079#else
1080
Willy Tarreauaa992762021-10-06 23:33:20 +02001081/* send signal <sig> to thread <thr> (send to process in fact) */
1082void 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) */
1088void ha_tkillall(int sig)
1089{
1090 raise(sig);
1091}
1092
1093void ha_thread_relax(void)
1094{
1095#ifdef _POSIX_PRIORITY_SCHEDULING
1096 sched_yield();
1097#endif
1098}
1099
Willy Tarreau8459f252018-12-15 16:48:14 +01001100REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
1101
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001102#endif // USE_THREAD
1103
1104
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001105/* scans the configured thread mapping and establishes the final one. Returns <0
1106 * on failure, >=0 on success.
1107 */
1108int thread_map_to_groups()
1109{
1110 int t, g, ut, ug;
1111 int q, r;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001112 ulong m __maybe_unused;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001113
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 Tarreau60fe4a92022-06-28 17:48:07 +02001124 ha_tgroup_info[g].tgid_bit = 1UL << g;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001125 }
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 Tarreau66ad98a2022-06-28 10:49:57 +02001166 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001167 ha_thread_info[t].tg = &ha_tgroup_info[g];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001168 ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g];
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001169
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 Tarreaucc7a11e2021-09-28 08:53:11 +02001182 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 Tarreaucc7a11e2021-09-28 08:53:11 +02001185 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1186 }
1187
Willy Tarreaucce203a2022-06-24 15:55:11 +02001188 m = 0;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001189 for (g = 0; g < global.nbtgroups; g++) {
1190 ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count);
Willy Tarreaucce203a2022-06-24 15:55:11 +02001191 if (!ha_tgroup_info[g].count)
1192 continue;
1193 m |= 1UL << g;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001194
1195 }
1196
Willy Tarreaucce203a2022-06-24 15:55:11 +02001197#ifdef USE_THREAD
1198 all_tgroups_mask = m;
1199#endif
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001200 return 0;
1201}
1202
Willy Tarreau6018c022022-06-28 08:27:43 +02001203/* 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 Tarreau627def92021-09-29 18:59:47 +02001208 * - 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 Tarreau6018c022022-06-28 08:27:43 +02001211 * - 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 Tarreau627def92021-09-29 18:59:47 +02001214 *
1215 * Returns <0 on failure, >=0 on success.
1216 */
1217int 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 Tarreau627def92021-09-29 18:59:47 +02001230 *ogid = 1; // first and only group
Willy Tarreau6018c022022-06-28 08:27:43 +02001231 *omask = ha_tgroup_info[0].threads_enabled;
Willy Tarreau627def92021-09-29 18:59:47 +02001232 return 0;
1233 } else {
1234 /* some global threads */
Willy Tarreau627def92021-09-29 18:59:47 +02001235 for (t = 0; t < global.nbthread; t++) {
1236 if (imask & (1UL << t)) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001237 if (ha_thread_info[t].tgid != igid) {
Willy Tarreau627def92021-09-29 18:59:47 +02001238 if (!igid)
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001239 igid = ha_thread_info[t].tgid;
Willy Tarreau627def92021-09-29 18:59:47 +02001240 else {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001241 memprintf(err, "'thread' directive spans multiple groups (at least %u and %u)", igid, ha_thread_info[t].tgid);
Willy Tarreau627def92021-09-29 18:59:47 +02001242 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 Tarreau6018c022022-06-28 08:27:43 +02001255 imask = imask >> ha_tgroup_info[igid - 1].base;
1256 imask &= ha_tgroup_info[igid - 1].threads_enabled;
1257 *omask = imask;
Willy Tarreau627def92021-09-29 18:59:47 +02001258 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 Tarreau6018c022022-06-28 08:27:43 +02001270 *omask = nbits(ha_tgroup_info[igid - 1].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001271 return 0;
1272 } else {
1273 /* some local threads. Keep only existing ones for this group */
1274
Willy Tarreau6018c022022-06-28 08:27:43 +02001275 mask = nbits(ha_tgroup_info[igid - 1].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001276
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 Tarreau6018c022022-06-28 08:27:43 +02001295 *omask = mask & imask;
Willy Tarreau627def92021-09-29 18:59:47 +02001296 *ogid = igid;
1297 return 0;
1298 }
1299 }
1300}
1301
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001302/* Parse the "nbthread" global directive, which takes an integer argument that
1303 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001304 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001305static 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 Tarreau0ccd3222018-07-30 10:34:35 +02001308{
1309 long nbthread;
1310 char *errptr;
1311
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001312 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 Tarreau0ccd3222018-07-30 10:34:35 +02001319 }
1320
1321#ifndef USE_THREAD
1322 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001323 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 Tarreau0ccd3222018-07-30 10:34:35 +02001325 }
1326#else
1327 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001328 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1329 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001330 }
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001331#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001332
1333 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001334 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1335 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001336
1337 global.nbthread = nbthread;
1338 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001339}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001340
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001341/* 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 */
1344static 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 Tarreau66ad98a2022-06-28 10:49:57 +02001373 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001374 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001375 ha_thread_info[tnum-1].tgid = 0;
Willy Tarreau03f9b352022-06-27 16:02:24 +02001376 ha_thread_info[tnum-1].tg_ctx = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001377 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001378 }
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 Tarreau66ad98a2022-06-28 10:49:57 +02001416 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001417 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001418 ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1];
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001419 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 Tarreauc33b9692021-09-22 12:07:23 +02001436/* Parse the "thread-groups" global directive, which takes an integer argument
1437 * that contains the desired number of thread groups.
1438 */
1439static 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 Tarreau51ec03a2021-09-22 11:55:22 +02001475/* config keyword parsers */
1476static struct cfg_kw_list cfg_kws = {ILH, {
1477 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001478 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001479 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001480 { 0, NULL, NULL }
1481}};
1482
1483INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);