blob: 489c841645a5bb988da0c0eaac0099c16861903e [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++) {
Willy Tarreaub2f38c12023-01-19 19:14:18 +0100107 do {
108 ulong te = _HA_ATOMIC_LOAD(&ha_tgroup_info[tgrp].threads_enabled);
109 ulong th = _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless);
110
111 if ((th & te) == te)
112 break;
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200113 ha_thread_relax();
Willy Tarreaub2f38c12023-01-19 19:14:18 +0100114 } while (1);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200115 }
Willy Tarreau60b639c2018-08-02 10:16:17 +0200116
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200117 /* Now we've seen all threads marked harmless, we can try to run
118 * by competing with other threads to win the race of the isolated
119 * thread. It eventually converges since winners will enventually
120 * relax their request and go back to wait for this to be over.
121 * Competing on this only after seeing all threads harmless limits
122 * the write contention.
123 */
124 thr = _HA_ATOMIC_LOAD(&isolated_thread);
125 if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
126 break; // we won!
Willy Tarreau38171da2019-05-17 16:33:13 +0200127 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +0200128 }
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200129
130 /* the thread is no longer harmless as it runs */
131 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
132
133 /* the thread is isolated until it calls thread_release() which will
134 * 1) reset isolated_thread to ~0;
135 * 2) decrement rdv_requests.
Willy Tarreau60b639c2018-08-02 10:16:17 +0200136 */
137}
138
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200139/* Isolates the current thread : request the ability to work while all other
140 * threads are idle, as defined by thread_idle_now(). It only returns once
141 * all of them are both harmless and idle, with the current thread's bit in
Willy Tarreau03f9b352022-06-27 16:02:24 +0200142 * &tg_ctx->threads_harmless and idle_mask cleared. Needs to be completed using
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200143 * thread_release(). By doing so the thread also engages in being safe against
144 * any actions that other threads might be about to start under the same
145 * conditions. This specifically targets destruction of any internal structure,
146 * which implies that the current thread may not hold references to any object.
147 *
148 * Note that a concurrent thread_isolate() will usually win against
149 * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to
150 * get back to the poller or any other fully idle location, that will
151 * ultimately release this one.
152 */
153void thread_isolate_full()
154{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200155 uint tgrp, thr;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200156
Willy Tarreau03f9b352022-06-27 16:02:24 +0200157 _HA_ATOMIC_OR(&tg_ctx->threads_idle, ti->ltid_bit);
158 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200159 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200160 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200161
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200162 /* wait for all threads to become harmless. They cannot change their
163 * mind once seen thanks to rdv_requests above, unless they pass in
164 * front of us.
165 */
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200166 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200167 for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) {
Willy Tarreaub2f38c12023-01-19 19:14:18 +0100168 do {
169 ulong te = _HA_ATOMIC_LOAD(&ha_tgroup_info[tgrp].threads_enabled);
170 ulong th = _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless);
171 ulong id = _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_idle);
172
173 if ((th & id & te) == te)
174 break;
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200175 ha_thread_relax();
Willy Tarreaub2f38c12023-01-19 19:14:18 +0100176 } while (1);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200177 }
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200178
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200179 /* Now we've seen all threads marked harmless and idle, we can
180 * try to run by competing with other threads to win the race
181 * of the isolated thread. It eventually converges since winners
182 * will enventually relax their request and go back to wait for
183 * this to be over. Competing on this only after seeing all
184 * threads harmless+idle limits the write contention.
185 */
186 thr = _HA_ATOMIC_LOAD(&isolated_thread);
187 if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
188 break; // we won!
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200189 ha_thread_relax();
190 }
191
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200192 /* we're not idle nor harmless anymore at this point. Other threads
193 * waiting on this condition will need to wait until out next pass to
194 * the poller, or our next call to thread_isolate_full().
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200195 */
Willy Tarreau03f9b352022-06-27 16:02:24 +0200196 _HA_ATOMIC_AND(&tg_ctx->threads_idle, ~ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200197 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200198}
199
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200200/* Cancels the effect of thread_isolate() by resetting the ID of the isolated
201 * thread and decrementing the number of RDV requesters. This immediately allows
202 * other threads to expect to be executed, though they will first have to wait
203 * for this thread to become harmless again (possibly by reaching the poller
204 * again).
Willy Tarreau60b639c2018-08-02 10:16:17 +0200205 */
206void thread_release()
207{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200208 HA_ATOMIC_STORE(&isolated_thread, ~0U);
209 HA_ATOMIC_DEC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200210}
Christopher Faulet339fff82017-10-19 11:59:15 +0200211
Willy Tarreaud10385a2021-10-06 22:22:40 +0200212/* Sets up threads, signals and masks, and starts threads 2 and above.
213 * Does nothing when threads are disabled.
214 */
215void setup_extra_threads(void *(*handler)(void *))
216{
217 sigset_t blocked_sig, old_sig;
218 int i;
219
220 /* ensure the signals will be blocked in every thread */
221 sigfillset(&blocked_sig);
222 sigdelset(&blocked_sig, SIGPROF);
223 sigdelset(&blocked_sig, SIGBUS);
224 sigdelset(&blocked_sig, SIGFPE);
225 sigdelset(&blocked_sig, SIGILL);
226 sigdelset(&blocked_sig, SIGSEGV);
227 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
228
229 /* Create nbthread-1 thread. The first thread is the current process */
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200230 ha_pthread[0] = pthread_self();
Willy Tarreaud10385a2021-10-06 22:22:40 +0200231 for (i = 1; i < global.nbthread; i++)
Willy Tarreau43ab05b2021-09-28 09:43:11 +0200232 pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200233}
234
235/* waits for all threads to terminate. Does nothing when threads are
236 * disabled.
237 */
238void wait_for_threads_completion()
239{
240 int i;
241
242 /* Wait the end of other threads */
243 for (i = 1; i < global.nbthread; i++)
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200244 pthread_join(ha_pthread[i], NULL);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200245
246#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
247 show_lock_stats();
248#endif
249}
250
251/* Tries to set the current thread's CPU affinity according to the cpu_map */
252void set_thread_cpu_affinity()
253{
254#if defined(USE_CPU_AFFINITY)
255 /* no affinity setting for the master process */
256 if (master)
257 return;
258
259 /* Now the CPU affinity for all threads */
Willy Tarreau5b093412022-07-08 09:38:30 +0200260 if (ha_cpuset_count(&cpu_map[tgid - 1].proc))
261 ha_cpuset_and(&cpu_map[tgid - 1].thread[ti->ltid], &cpu_map[tgid - 1].proc);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200262
Willy Tarreau5b093412022-07-08 09:38:30 +0200263 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 +0200264# if defined(__APPLE__)
265 /* Note: this API is limited to the first 32/64 CPUs */
Willy Tarreau5b093412022-07-08 09:38:30 +0200266 unsigned long set = cpu_map[tgid - 1].thread[ti->ltid].cpuset;
Willy Tarreaud10385a2021-10-06 22:22:40 +0200267 int j;
268
269 while ((j = ffsl(set)) > 0) {
270 thread_affinity_policy_data_t cpu_set = { j - 1 };
271 thread_port_t mthread;
272
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200273 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200274 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
275 set &= ~(1UL << (j - 1));
276 }
277# else
Willy Tarreau5b093412022-07-08 09:38:30 +0200278 struct hap_cpuset *set = &cpu_map[tgid - 1].thread[ti->ltid];
Willy Tarreaud10385a2021-10-06 22:22:40 +0200279
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200280 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200281# endif
282 }
283#endif /* USE_CPU_AFFINITY */
284}
285
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200286/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
287 * since POSIX took great care of not specifying its representation, making it
288 * hard to export for post-mortem analysis. For this reason we copy it into a
289 * union and will use the smallest scalar type at least as large as its size,
290 * which will keep endianness and alignment for all regular sizes. As a last
291 * resort we end up with a long long ligned to the first bytes in memory, which
292 * will be endian-dependent if pthread_t is larger than a long long (not seen
293 * yet).
294 */
295unsigned long long ha_get_pthread_id(unsigned int thr)
296{
297 union {
298 pthread_t t;
299 unsigned long long ll;
300 unsigned int i;
301 unsigned short s;
302 unsigned char c;
303 } u = { 0 };
304
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200305 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200306
307 if (sizeof(u.t) <= sizeof(u.c))
308 return u.c;
309 else if (sizeof(u.t) <= sizeof(u.s))
310 return u.s;
311 else if (sizeof(u.t) <= sizeof(u.i))
312 return u.i;
313 return u.ll;
314}
315
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200316/* send signal <sig> to thread <thr> */
317void ha_tkill(unsigned int thr, int sig)
318{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200319 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200320}
321
322/* send signal <sig> to all threads. The calling thread is signaled last in
323 * order to allow all threads to synchronize in the handler.
324 */
325void ha_tkillall(int sig)
326{
327 unsigned int thr;
328
329 for (thr = 0; thr < global.nbthread; thr++) {
Willy Tarreauf15c75a2022-07-15 08:27:56 +0200330 if (!(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit))
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200331 continue;
332 if (thr == tid)
333 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200334 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200335 }
336 raise(sig);
337}
338
Willy Tarreauaa992762021-10-06 23:33:20 +0200339void ha_thread_relax(void)
340{
341#ifdef _POSIX_PRIORITY_SCHEDULING
342 sched_yield();
343#else
344 pl_cpu_relax();
345#endif
346}
347
Willy Tarreau3d184982020-10-18 10:20:59 +0200348/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100349void ha_spin_init(HA_SPINLOCK_T *l)
350{
351 HA_SPIN_INIT(l);
352}
353
Willy Tarreau3d184982020-10-18 10:20:59 +0200354/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100355void ha_rwlock_init(HA_RWLOCK_T *l)
356{
357 HA_RWLOCK_INIT(l);
358}
359
Willy Tarreau149ab772019-01-26 14:27:06 +0100360/* returns the number of CPUs the current process is enabled to run on */
361static int thread_cpus_enabled()
362{
363 int ret = 1;
364
365#ifdef USE_CPU_AFFINITY
366#if defined(__linux__) && defined(CPU_COUNT)
367 cpu_set_t mask;
368
369 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
370 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200371#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
372 cpuset_t cpuset;
373 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
374 sizeof(cpuset), &cpuset) == 0)
375 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000376#elif defined(__APPLE__)
377 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100378#endif
379#endif
380 ret = MAX(ret, 1);
381 ret = MIN(ret, MAX_THREADS);
382 return ret;
383}
384
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200385/* Returns 1 if the cpu set is currently restricted for the process else 0.
386 * Currently only implemented for the Linux platform.
387 */
388int thread_cpu_mask_forced()
389{
390#if defined(__linux__)
391 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
392 return cpus_avail != thread_cpus_enabled();
393#else
394 return 0;
395#endif
396}
397
Willy Tarreau407ef892021-10-05 18:39:27 +0200398/* Below come the lock-debugging functions */
399
400#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
401
402struct lock_stat lock_stats[LOCK_LABELS];
403
404/* this is only used below */
405static const char *lock_label(enum lock_label label)
406{
407 switch (label) {
408 case TASK_RQ_LOCK: return "TASK_RQ";
409 case TASK_WQ_LOCK: return "TASK_WQ";
410 case LISTENER_LOCK: return "LISTENER";
411 case PROXY_LOCK: return "PROXY";
412 case SERVER_LOCK: return "SERVER";
413 case LBPRM_LOCK: return "LBPRM";
414 case SIGNALS_LOCK: return "SIGNALS";
415 case STK_TABLE_LOCK: return "STK_TABLE";
416 case STK_SESS_LOCK: return "STK_SESS";
417 case APPLETS_LOCK: return "APPLETS";
418 case PEER_LOCK: return "PEER";
419 case SHCTX_LOCK: return "SHCTX";
420 case SSL_LOCK: return "SSL";
421 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
422 case PATREF_LOCK: return "PATREF";
423 case PATEXP_LOCK: return "PATEXP";
424 case VARS_LOCK: return "VARS";
425 case COMP_POOL_LOCK: return "COMP_POOL";
426 case LUA_LOCK: return "LUA";
427 case NOTIF_LOCK: return "NOTIF";
428 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
429 case DNS_LOCK: return "DNS";
430 case PID_LIST_LOCK: return "PID_LIST";
431 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
432 case PIPES_LOCK: return "PIPES";
433 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
434 case AUTH_LOCK: return "AUTH";
435 case LOGSRV_LOCK: return "LOGSRV";
436 case DICT_LOCK: return "DICT";
437 case PROTO_LOCK: return "PROTO";
438 case QUEUE_LOCK: return "QUEUE";
439 case CKCH_LOCK: return "CKCH";
440 case SNI_LOCK: return "SNI";
441 case SSL_SERVER_LOCK: return "SSL_SERVER";
442 case SFT_LOCK: return "SFT";
443 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
444 case QUIC_LOCK: return "QUIC";
Remi Tricot-Le Breton2b963642022-12-20 11:11:02 +0100445 case OCSP_LOCK: return "OCSP";
Willy Tarreau407ef892021-10-05 18:39:27 +0200446 case OTHER_LOCK: return "OTHER";
447 case DEBUG1_LOCK: return "DEBUG1";
448 case DEBUG2_LOCK: return "DEBUG2";
449 case DEBUG3_LOCK: return "DEBUG3";
450 case DEBUG4_LOCK: return "DEBUG4";
451 case DEBUG5_LOCK: return "DEBUG5";
452 case LOCK_LABELS: break; /* keep compiler happy */
453 };
454 /* only way to come here is consecutive to an internal bug */
455 abort();
456}
457
458void show_lock_stats()
459{
460 int lbl;
461
462 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
463 if (!lock_stats[lbl].num_write_locked &&
464 !lock_stats[lbl].num_seek_locked &&
465 !lock_stats[lbl].num_read_locked) {
466 fprintf(stderr,
467 "Stats about Lock %s: not used\n",
468 lock_label(lbl));
469 continue;
470 }
471
472 fprintf(stderr,
473 "Stats about Lock %s: \n",
474 lock_label(lbl));
475
476 if (lock_stats[lbl].num_write_locked)
477 fprintf(stderr,
478 "\t # write lock : %lu\n"
479 "\t # write unlock: %lu (%ld)\n"
480 "\t # wait time for write : %.3f msec\n"
481 "\t # wait time for write/lock: %.3f nsec\n",
482 lock_stats[lbl].num_write_locked,
483 lock_stats[lbl].num_write_unlocked,
484 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
485 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
486 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
487
488 if (lock_stats[lbl].num_seek_locked)
489 fprintf(stderr,
490 "\t # seek lock : %lu\n"
491 "\t # seek unlock : %lu (%ld)\n"
492 "\t # wait time for seek : %.3f msec\n"
493 "\t # wait time for seek/lock : %.3f nsec\n",
494 lock_stats[lbl].num_seek_locked,
495 lock_stats[lbl].num_seek_unlocked,
496 lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked,
497 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
498 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
499
500 if (lock_stats[lbl].num_read_locked)
501 fprintf(stderr,
502 "\t # read lock : %lu\n"
503 "\t # read unlock : %lu (%ld)\n"
504 "\t # wait time for read : %.3f msec\n"
505 "\t # wait time for read/lock : %.3f nsec\n",
506 lock_stats[lbl].num_read_locked,
507 lock_stats[lbl].num_read_unlocked,
508 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
509 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
510 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
511 }
512}
513
Willy Tarreau407ef892021-10-05 18:39:27 +0200514void __ha_rwlock_init(struct ha_rwlock *l)
515{
516 memset(l, 0, sizeof(struct ha_rwlock));
517 __RWLOCK_INIT(&l->lock);
518}
519
520void __ha_rwlock_destroy(struct ha_rwlock *l)
521{
522 __RWLOCK_DESTROY(&l->lock);
523 memset(l, 0, sizeof(struct ha_rwlock));
524}
525
526
527void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
528 const char *func, const char *file, int line)
529{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200530 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
531 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200532 uint64_t start_time;
533
Willy Tarreau7aa41192022-07-15 17:53:10 +0200534 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200535 abort();
536
Willy Tarreau7aa41192022-07-15 17:53:10 +0200537 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200538
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200539 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200540 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200541 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200542
543 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
544
Willy Tarreau7aa41192022-07-15 17:53:10 +0200545 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200546 l->info.last_location.function = func;
547 l->info.last_location.file = file;
548 l->info.last_location.line = line;
549
Willy Tarreau7aa41192022-07-15 17:53:10 +0200550 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200551}
552
553int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
554 const char *func, const char *file, int line)
555{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200556 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
557 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200558 uint64_t start_time;
559 int r;
560
Willy Tarreau7aa41192022-07-15 17:53:10 +0200561 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200562 abort();
563
564 /* We set waiting writer because trywrlock could wait for readers to quit */
Willy Tarreau7aa41192022-07-15 17:53:10 +0200565 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200566
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200567 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200568 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200569 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200570 if (unlikely(r)) {
Willy Tarreau7aa41192022-07-15 17:53:10 +0200571 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200572 return r;
573 }
574 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
575
Willy Tarreau7aa41192022-07-15 17:53:10 +0200576 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200577 l->info.last_location.function = func;
578 l->info.last_location.file = file;
579 l->info.last_location.line = line;
580
Willy Tarreau7aa41192022-07-15 17:53:10 +0200581 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200582
583 return 0;
584}
585
586void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
587 const char *func, const char *file, int line)
588{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200589 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
590 struct ha_rwlock_state *st = &l->info.st[tgid-1];
591
592 if (unlikely(!(st->cur_writer & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200593 /* the thread is not owning the lock for write */
594 abort();
595 }
596
Willy Tarreau7aa41192022-07-15 17:53:10 +0200597 st->cur_writer = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200598 l->info.last_location.function = func;
599 l->info.last_location.file = file;
600 l->info.last_location.line = line;
601
602 __RWLOCK_WRUNLOCK(&l->lock);
603
604 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
605}
606
607void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
608{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200609 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
610 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200611 uint64_t start_time;
612
Willy Tarreau7aa41192022-07-15 17:53:10 +0200613 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200614 abort();
615
Willy Tarreau7aa41192022-07-15 17:53:10 +0200616 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200617
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200618 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200619 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200620 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200621 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
622
Willy Tarreau7aa41192022-07-15 17:53:10 +0200623 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200624
Willy Tarreau7aa41192022-07-15 17:53:10 +0200625 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200626}
627
628int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
629{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200630 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
631 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200632 int r;
633
Willy Tarreau7aa41192022-07-15 17:53:10 +0200634 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200635 abort();
636
637 /* try read should never wait */
638 r = __RWLOCK_TRYRDLOCK(&l->lock);
639 if (unlikely(r))
640 return r;
641 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
642
Willy Tarreau7aa41192022-07-15 17:53:10 +0200643 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200644
645 return 0;
646}
647
648void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
649{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200650 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
651 struct ha_rwlock_state *st = &l->info.st[tgid-1];
652
653 if (unlikely(!(st->cur_readers & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200654 /* the thread is not owning the lock for read */
655 abort();
656 }
657
Willy Tarreau7aa41192022-07-15 17:53:10 +0200658 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200659
660 __RWLOCK_RDUNLOCK(&l->lock);
661
662 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
663}
664
665void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
666 const char *func, const char *file, int line)
667{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200668 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
669 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200670 uint64_t start_time;
671
Willy Tarreau7aa41192022-07-15 17:53:10 +0200672 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200673 abort();
674
Willy Tarreau7aa41192022-07-15 17:53:10 +0200675 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200676 abort();
677
Willy Tarreau7aa41192022-07-15 17:53:10 +0200678 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200679
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200680 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200681 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200682 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200683
684 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
685
Willy Tarreau7aa41192022-07-15 17:53:10 +0200686 HA_ATOMIC_OR(&st->cur_readers, tbit);
687 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200688 l->info.last_location.function = func;
689 l->info.last_location.file = file;
690 l->info.last_location.line = line;
691
Willy Tarreau7aa41192022-07-15 17:53:10 +0200692 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200693}
694
695void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
696 const char *func, const char *file, int line)
697{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200698 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
699 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200700 uint64_t start_time;
701
Willy Tarreau7aa41192022-07-15 17:53:10 +0200702 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200703 abort();
704
Willy Tarreau7aa41192022-07-15 17:53:10 +0200705 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200706 abort();
707
Willy Tarreau7aa41192022-07-15 17:53:10 +0200708 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200709
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200710 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200711 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200712 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200713
714 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
715
Willy Tarreau7aa41192022-07-15 17:53:10 +0200716 HA_ATOMIC_OR(&st->cur_seeker, tbit);
717 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200718 l->info.last_location.function = func;
719 l->info.last_location.file = file;
720 l->info.last_location.line = line;
721
Willy Tarreau7aa41192022-07-15 17:53:10 +0200722 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200723}
724
725void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
726 const char *func, const char *file, int line)
727{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200728 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
729 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200730 uint64_t start_time;
731
Willy Tarreau7aa41192022-07-15 17:53:10 +0200732 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200733 abort();
734
Willy Tarreau7aa41192022-07-15 17:53:10 +0200735 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200736
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200737 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200738 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200739 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200740
741 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
742
Willy Tarreau7aa41192022-07-15 17:53:10 +0200743 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200744 l->info.last_location.function = func;
745 l->info.last_location.file = file;
746 l->info.last_location.line = line;
747
Willy Tarreau7aa41192022-07-15 17:53:10 +0200748 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200749}
750
751void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
752 const char *func, const char *file, int line)
753{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200754 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
755 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200756 uint64_t start_time;
757
Willy Tarreau7aa41192022-07-15 17:53:10 +0200758 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200759 abort();
760
Willy Tarreau7aa41192022-07-15 17:53:10 +0200761 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200762 abort();
763
Willy Tarreau7aa41192022-07-15 17:53:10 +0200764 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200765
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200766 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200767 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200768 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200769
770 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
771
Willy Tarreau7aa41192022-07-15 17:53:10 +0200772 HA_ATOMIC_OR(&st->cur_writer, tbit);
773 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200774 l->info.last_location.function = func;
775 l->info.last_location.file = file;
776 l->info.last_location.line = line;
777
Willy Tarreau7aa41192022-07-15 17:53:10 +0200778 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200779}
780
781void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
782 const char *func, const char *file, int line)
783{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200784 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
785 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200786 uint64_t start_time;
787
Willy Tarreau7aa41192022-07-15 17:53:10 +0200788 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200789 abort();
790
Willy Tarreau7aa41192022-07-15 17:53:10 +0200791 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200792 abort();
793
Willy Tarreau7aa41192022-07-15 17:53:10 +0200794 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200795
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200796 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200797 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200798 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200799
800 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
801
Willy Tarreau7aa41192022-07-15 17:53:10 +0200802 HA_ATOMIC_OR(&st->cur_readers, tbit);
803 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200804 l->info.last_location.function = func;
805 l->info.last_location.file = file;
806 l->info.last_location.line = line;
807
Willy Tarreau7aa41192022-07-15 17:53:10 +0200808 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200809}
810
811void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
812 const char *func, const char *file, int line)
813{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200814 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
815 struct ha_rwlock_state *st = &l->info.st[tgid-1];
816 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200817 abort();
818
Willy Tarreau7aa41192022-07-15 17:53:10 +0200819 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200820 l->info.last_location.function = func;
821 l->info.last_location.file = file;
822 l->info.last_location.line = line;
823
824 __RWLOCK_SKUNLOCK(&l->lock);
825
826 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
827}
828
829int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
830 const char *func, const char *file, int line)
831{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200832 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
833 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200834 uint64_t start_time;
835 int r;
836
Willy Tarreau7aa41192022-07-15 17:53:10 +0200837 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200838 abort();
839
Willy Tarreau7aa41192022-07-15 17:53:10 +0200840 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200841
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200842 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200843 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200844 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200845
846 if (likely(!r)) {
847 /* got the lock ! */
848 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200849 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200850 l->info.last_location.function = func;
851 l->info.last_location.file = file;
852 l->info.last_location.line = line;
853 }
854
Willy Tarreau7aa41192022-07-15 17:53:10 +0200855 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200856 return r;
857}
858
859int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
860 const char *func, const char *file, int line)
861{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200862 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
863 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200864 uint64_t start_time;
865 int r;
866
Willy Tarreau7aa41192022-07-15 17:53:10 +0200867 if ((st->cur_writer | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200868 abort();
869
Willy Tarreau7aa41192022-07-15 17:53:10 +0200870 if (!(st->cur_readers & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200871 abort();
872
Willy Tarreau7aa41192022-07-15 17:53:10 +0200873 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200874
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200875 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200876 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200877 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200878
879 if (likely(!r)) {
880 /* got the lock ! */
881 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200882 HA_ATOMIC_OR(&st->cur_seeker, tbit);
883 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200884 l->info.last_location.function = func;
885 l->info.last_location.file = file;
886 l->info.last_location.line = line;
887 }
888
Willy Tarreau7aa41192022-07-15 17:53:10 +0200889 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200890 return r;
891}
892
893void __spin_init(struct ha_spinlock *l)
894{
895 memset(l, 0, sizeof(struct ha_spinlock));
896 __SPIN_INIT(&l->lock);
897}
898
899void __spin_destroy(struct ha_spinlock *l)
900{
901 __SPIN_DESTROY(&l->lock);
902 memset(l, 0, sizeof(struct ha_spinlock));
903}
904
905void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
906 const char *func, const char *file, int line)
907{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200908 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
909 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200910 uint64_t start_time;
911
Willy Tarreau7aa41192022-07-15 17:53:10 +0200912 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200913 /* the thread is already owning the lock */
914 abort();
915 }
916
Willy Tarreau7aa41192022-07-15 17:53:10 +0200917 HA_ATOMIC_OR(&st->waiters, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200918
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200919 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200920 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200921 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200922
923 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
924
925
Willy Tarreau7aa41192022-07-15 17:53:10 +0200926 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200927 l->info.last_location.function = func;
928 l->info.last_location.file = file;
929 l->info.last_location.line = line;
930
Willy Tarreau7aa41192022-07-15 17:53:10 +0200931 HA_ATOMIC_AND(&st->waiters, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200932}
933
934int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
935 const char *func, const char *file, int line)
936{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200937 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
938 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200939 int r;
940
Willy Tarreau7aa41192022-07-15 17:53:10 +0200941 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200942 /* the thread is already owning the lock */
943 abort();
944 }
945
946 /* try read should never wait */
947 r = __SPIN_TRYLOCK(&l->lock);
948 if (unlikely(r))
949 return r;
950 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
951
Willy Tarreau7aa41192022-07-15 17:53:10 +0200952 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200953 l->info.last_location.function = func;
954 l->info.last_location.file = file;
955 l->info.last_location.line = line;
956
957 return 0;
958}
959
960void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
961 const char *func, const char *file, int line)
962{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200963 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
964 struct ha_spinlock_state *st = &l->info.st[tgid-1];
965
966 if (unlikely(!(st->owner & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200967 /* the thread is not owning the lock */
968 abort();
969 }
970
Willy Tarreau7aa41192022-07-15 17:53:10 +0200971 st->owner = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200972 l->info.last_location.function = func;
973 l->info.last_location.file = file;
974 l->info.last_location.line = line;
975
976 __SPIN_UNLOCK(&l->lock);
977 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
978}
979
980#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
981
Willy Tarreau87aff022022-07-10 10:58:57 +0200982
983#if defined(USE_PTHREAD_EMULATION)
984
985/* pthread rwlock emulation using plocks (to avoid expensive futexes).
986 * these are a direct mapping on Progressive Locks, with the exception that
987 * since there's a common unlock operation in pthreads, we need to know if
988 * we need to unlock for reads or writes, so we set the topmost bit to 1 when
989 * a write lock is acquired to indicate that a write unlock needs to be
990 * performed. It's not a problem since this bit will never be used given that
991 * haproxy won't support as many threads as the plocks.
992 *
993 * The storage is the pthread_rwlock_t cast as an ulong
994 */
995
996int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr)
997{
998 ulong *lock = (ulong *)rwlock;
999
1000 *lock = 0;
1001 return 0;
1002}
1003
1004int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
1005{
1006 ulong *lock = (ulong *)rwlock;
1007
1008 *lock = 0;
1009 return 0;
1010}
1011
1012int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
1013{
1014 pl_lorw_rdlock((unsigned long *)rwlock);
1015 return 0;
1016}
1017
1018int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
1019{
1020 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_SHR_BASE);
1021}
1022
1023int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1024{
1025 return pthread_rwlock_tryrdlock(rwlock);
1026}
1027
1028int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
1029{
1030 pl_lorw_wrlock((unsigned long *)rwlock);
1031 return 0;
1032}
1033
1034int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
1035{
1036 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_EXC_BASE);
1037}
1038
1039int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1040{
1041 return pthread_rwlock_trywrlock(rwlock);
1042}
1043
1044int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
1045{
1046 pl_lorw_unlock((unsigned long *)rwlock);
1047 return 0;
1048}
1049#endif // defined(USE_PTHREAD_EMULATION)
1050
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001051/* Depending on the platform and how libpthread was built, pthread_exit() may
1052 * involve some code in libgcc_s that would be loaded on exit for the first
1053 * time, causing aborts if the process is chrooted. It's harmless bit very
1054 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
1055 * needed, so what we do here is that during early boot we create a dummy
1056 * thread that immediately exits. This will lead to libgcc_s being loaded
1057 * during boot on the platforms where it's required.
1058 */
1059static void *dummy_thread_function(void *data)
1060{
1061 pthread_exit(NULL);
1062 return NULL;
1063}
1064
1065static inline void preload_libgcc_s(void)
1066{
1067 pthread_t dummy_thread;
1068 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
1069 pthread_join(dummy_thread, NULL);
1070}
1071
Willy Tarreau3f567e42020-05-28 15:29:19 +02001072static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001073{
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001074 char *ptr = NULL;
1075
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001076 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +02001077
Willy Tarreau149ab772019-01-26 14:27:06 +01001078 thread_cpus_enabled_at_boot = thread_cpus_enabled();
1079
Willy Tarreauc80bdb22022-08-06 16:44:55 +02001080 memprintf(&ptr, "Built with multi-threading support (MAX_TGROUPS=%d, MAX_THREADS=%d, default=%d).",
1081 MAX_TGROUPS, MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001082 hap_register_build_opts(ptr, 1);
1083
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001084#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
1085 memset(lock_stats, 0, sizeof(lock_stats));
1086#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001087}
Willy Tarreau8ead1d02022-04-25 19:23:17 +02001088INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001089
Willy Tarreau8459f252018-12-15 16:48:14 +01001090#else
1091
Willy Tarreauaa992762021-10-06 23:33:20 +02001092/* send signal <sig> to thread <thr> (send to process in fact) */
1093void ha_tkill(unsigned int thr, int sig)
1094{
1095 raise(sig);
1096}
1097
1098/* send signal <sig> to all threads (send to process in fact) */
1099void ha_tkillall(int sig)
1100{
1101 raise(sig);
1102}
1103
1104void ha_thread_relax(void)
1105{
1106#ifdef _POSIX_PRIORITY_SCHEDULING
1107 sched_yield();
1108#endif
1109}
1110
Willy Tarreau8459f252018-12-15 16:48:14 +01001111REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
1112
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001113#endif // USE_THREAD
1114
1115
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001116/* scans the configured thread mapping and establishes the final one. Returns <0
1117 * on failure, >=0 on success.
1118 */
1119int thread_map_to_groups()
1120{
1121 int t, g, ut, ug;
1122 int q, r;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001123 ulong m __maybe_unused;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001124
1125 ut = ug = 0; // unassigned threads & groups
1126
1127 for (t = 0; t < global.nbthread; t++) {
1128 if (!ha_thread_info[t].tg)
1129 ut++;
1130 }
1131
1132 for (g = 0; g < global.nbtgroups; g++) {
1133 if (!ha_tgroup_info[g].count)
1134 ug++;
Willy Tarreau60fe4a92022-06-28 17:48:07 +02001135 ha_tgroup_info[g].tgid_bit = 1UL << g;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001136 }
1137
1138 if (ug > ut) {
1139 ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut);
1140 return -1;
1141 }
1142
1143 /* look for first unassigned thread */
1144 for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++)
1145 ;
1146
1147 /* assign threads to empty groups */
1148 for (g = 0; ug && ut; ) {
1149 /* due to sparse thread assignment we can end up with more threads
1150 * per group on last assigned groups than former ones, so we must
1151 * always try to pack the maximum remaining ones together first.
1152 */
1153 q = ut / ug;
1154 r = ut % ug;
1155 if ((q + !!r) > MAX_THREADS_PER_GROUP) {
1156 ha_alert("Too many remaining unassigned threads (%d) for thread groups (%d). Please increase thread-groups or make sure to keep thread numbers contiguous\n", ug, ut);
1157 return -1;
1158 }
1159
1160 /* thread <t> is the next unassigned one. Let's look for next
1161 * unassigned group, we know there are some left
1162 */
1163 while (ut >= ug && ha_tgroup_info[g].count)
1164 g++;
1165
1166 /* group g is unassigned, try to fill it with consecutive threads */
1167 while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r &&
1168 (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) {
1169
1170 if (!ha_tgroup_info[g].count) {
1171 /* assign new group */
1172 ha_tgroup_info[g].base = t;
1173 ug--;
1174 }
1175
1176 ha_tgroup_info[g].count++;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001177 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001178 ha_thread_info[t].tg = &ha_tgroup_info[g];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001179 ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g];
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001180
1181 ut--;
1182 /* switch to next unassigned thread */
1183 while (++t < global.nbthread && ha_thread_info[t].tg)
1184 ;
1185 }
1186 }
1187
1188 if (ut) {
1189 ha_alert("Remaining unassigned threads found (%d) because all groups are in use. Please increase 'thread-groups', reduce 'nbthreads' or remove or extend 'thread-group' enumerations.\n", ut);
1190 return -1;
1191 }
1192
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001193 for (t = 0; t < global.nbthread; t++) {
1194 ha_thread_info[t].tid = t;
1195 ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base;
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001196 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1197 }
1198
Willy Tarreaucce203a2022-06-24 15:55:11 +02001199 m = 0;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001200 for (g = 0; g < global.nbtgroups; g++) {
1201 ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count);
Aurelien DARRAGON739281b2023-01-27 15:13:28 +01001202 /* for now, additional threads are not started, so we should
1203 * consider them as harmless and idle.
1204 * This will get automatically updated when such threads are
1205 * started in run_thread_poll_loop()
1206 * Without this, thread_isolate() and thread_isolate_full()
1207 * will fail to work as long as secondary threads did not enter
1208 * the polling loop at least once.
1209 */
1210 ha_tgroup_ctx[g].threads_harmless = ha_tgroup_info[g].threads_enabled;
1211 ha_tgroup_ctx[g].threads_idle = ha_tgroup_info[g].threads_enabled;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001212 if (!ha_tgroup_info[g].count)
1213 continue;
1214 m |= 1UL << g;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001215
1216 }
1217
Willy Tarreaucce203a2022-06-24 15:55:11 +02001218#ifdef USE_THREAD
1219 all_tgroups_mask = m;
1220#endif
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001221 return 0;
1222}
1223
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001224/* Converts a configuration thread set based on either absolute or relative
1225 * thread numbers into a global group+mask. This is essentially for use with
1226 * the "thread" directive on "bind" lines, where "thread 4-6,10-12" might be
1227 * turned to "2/1-3,4/1-3". It cannot be used before the thread mapping above
1228 * was completed and the thread group numbers configured. The thread_set is
1229 * replaced by the resolved group-based one. It is possible to force a single
1230 * default group for unspecified sets instead of enabling all groups by passing
Willy Tarreauf2988e12023-02-02 17:01:10 +01001231 * this group's non-zero value to defgrp.
Willy Tarreau627def92021-09-29 18:59:47 +02001232 *
1233 * Returns <0 on failure, >=0 on success.
1234 */
Willy Tarreauf2988e12023-02-02 17:01:10 +01001235int thread_resolve_group_mask(struct thread_set *ts, int defgrp, char **err)
Willy Tarreau627def92021-09-29 18:59:47 +02001236{
Willy Tarreau15c84282023-02-04 10:49:01 +01001237 struct thread_set new_ts = { };
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001238 ulong mask, imask;
1239 uint g;
Willy Tarreau627def92021-09-29 18:59:47 +02001240
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001241 if (!ts->nbgrp) {
Willy Tarreau627def92021-09-29 18:59:47 +02001242 /* unspecified group, IDs are global */
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001243 if (thread_set_is_empty(ts)) {
1244 /* all threads of all groups, unless defgrp is set and
1245 * we then set it as the only group.
1246 */
1247 for (g = defgrp ? defgrp-1 : 0; g < (defgrp ? defgrp : global.nbtgroups); g++) {
1248 new_ts.rel[g] = ha_tgroup_info[g].threads_enabled;
1249 new_ts.nbgrp++;
Willy Tarreau627def92021-09-29 18:59:47 +02001250 }
Willy Tarreau627def92021-09-29 18:59:47 +02001251 } else {
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001252 /* some absolute threads are set, we must remap them to
1253 * relative ones. Each group cannot have more than
1254 * LONGBITS threads, thus it spans at most two absolute
1255 * blocks.
1256 */
1257 for (g = 0; g < global.nbtgroups; g++) {
1258 uint block = ha_tgroup_info[g].base / LONGBITS;
1259 uint base = ha_tgroup_info[g].base % LONGBITS;
Willy Tarreau627def92021-09-29 18:59:47 +02001260
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001261 mask = ts->abs[block] >> base;
1262 if (base && ha_tgroup_info[g].count > (LONGBITS - base))
1263 mask |= ts->abs[block + 1] << (LONGBITS - base);
1264 mask &= nbits(ha_tgroup_info[g].count);
1265 mask &= ha_tgroup_info[g].threads_enabled;
Willy Tarreau627def92021-09-29 18:59:47 +02001266
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001267 /* now the mask exactly matches the threads to be enabled
1268 * in this group.
1269 */
1270 if (!new_ts.rel[g] && mask)
1271 new_ts.nbgrp++;
1272 new_ts.rel[g] |= mask;
1273 }
Willy Tarreau627def92021-09-29 18:59:47 +02001274 }
1275 } else {
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001276 /* groups were specified */
1277 for (g = 0; g < MAX_TGROUPS; g++) {
1278 imask = ts->rel[g];
1279 if (!imask)
1280 continue;
Willy Tarreau627def92021-09-29 18:59:47 +02001281
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001282 if (g >= global.nbtgroups) {
1283 memprintf(err, "'thread' directive references non-existing thread group %u", g+1);
1284 return -1;
1285 }
Willy Tarreau627def92021-09-29 18:59:47 +02001286
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001287 /* some relative threads are set. Keep only existing ones for this group */
1288 mask = nbits(ha_tgroup_info[g].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001289
1290 if (!(mask & imask)) {
1291 /* no intersection between the thread group's
1292 * threads and the bind line's.
1293 */
1294#ifdef THREAD_AUTO_ADJUST_GROUPS
1295 unsigned long new_mask = 0;
1296
1297 while (imask) {
1298 new_mask |= imask & mask;
Willy Tarreaubef43df2023-01-31 19:27:48 +01001299 imask >>= ha_tgroup_info[g].count;
Willy Tarreau627def92021-09-29 18:59:47 +02001300 }
1301 imask = new_mask;
1302#else
Willy Tarreaubef43df2023-01-31 19:27:48 +01001303 memprintf(err, "'thread' directive only references threads not belonging to group %u", g+1);
Willy Tarreau627def92021-09-29 18:59:47 +02001304 return -1;
1305#endif
1306 }
1307
Willy Tarreaubef43df2023-01-31 19:27:48 +01001308 new_ts.rel[g] = imask & mask;
1309 new_ts.nbgrp++;
Willy Tarreau627def92021-09-29 18:59:47 +02001310 }
1311 }
Willy Tarreaubef43df2023-01-31 19:27:48 +01001312
1313 /* update the thread_set */
1314 if (!thread_set_first_group(&new_ts)) {
1315 memprintf(err, "'thread' directive only references non-existing threads");
1316 return -1;
1317 }
1318
1319 *ts = new_ts;
Willy Tarreaubef43df2023-01-31 19:27:48 +01001320 return 0;
1321}
1322
1323/* Parse a string representing a thread set in one of the following forms:
1324 *
1325 * - { "all" | "odd" | "even" | <abs_num> [ "-" <abs_num> ] }[,...]
1326 * => these are (lists of) absolute thread numbers
1327 *
1328 * - <tgnum> "/" { "all" | "odd" | "even" | <rel_num> [ "-" <rel_num> ][,...]
1329 * => these are (lists of) per-group relative thread numbers. All numbers
1330 * must be lower than or equal to LONGBITS. When multiple list elements
1331 * are provided, each of them must contain the thread group number.
1332 *
1333 * Minimum value for a thread or group number is always 1. Maximum value for an
1334 * absolute thread number is MAX_THREADS, maximum value for a relative thread
1335 * number is MAX_THREADS_PER_GROUP, an maximum value for a thread group is
1336 * MAX_TGROUPS. "all", "even" and "odd" will be bound by MAX_THREADS and/or
1337 * MAX_THREADS_PER_GROUP in any case. In ranges, a missing digit before "-"
1338 * is implicitly 1, and a missing digit after "-" is implicitly the highest of
1339 * its class. As such "-" is equivalent to "all", allowing to build strings
1340 * such as "${MIN}-${MAX}" where both MIN and MAX are optional.
1341 *
1342 * It is not valid to mix absolute and relative numbers. As such:
1343 * - all valid (all absolute threads)
1344 * - 12-19,24-31 valid (abs threads 12 to 19 and 24 to 31)
1345 * - 1/all valid (all 32 or 64 threads of group 1)
1346 * - 1/1-4,1/8-10,2/1 valid
1347 * - 1/1-4,8-10 invalid (mixes relatve "1/1-4" with absolute "8-10")
1348 * - 1-4,8-10,2/1 invalid (mixes absolute "1-4,8-10" with relative "2/1")
1349 * - 1/odd-4 invalid (mixes range with boundary)
1350 *
1351 * The target thread set is *completed* with supported threads, which means
1352 * that it's the caller's responsibility for pre-initializing it. If the target
1353 * thread set is NULL, it's not updated and the function only verifies that the
1354 * input parses.
1355 *
1356 * On success, it returns 0. otherwise it returns non-zero with an error
1357 * message in <err>.
1358 */
1359int parse_thread_set(const char *arg, struct thread_set *ts, char **err)
1360{
1361 const char *set;
1362 const char *sep;
1363 int v, min, max, tg;
1364 int is_rel;
1365
1366 /* search for the first delimiter (',', '-' or '/') to decide whether
1367 * we're facing an absolute or relative form. The relative form always
1368 * starts with a number followed by a slash.
1369 */
1370 for (sep = arg; isdigit((uchar)*sep); sep++)
1371 ;
1372
1373 is_rel = (/*sep > arg &&*/ *sep == '/'); /* relative form */
1374
1375 /* from there we have to cut the thread spec around commas */
1376
1377 set = arg;
1378 tg = 0;
1379 while (*set) {
1380 /* note: we can't use strtol() here because "-3" would parse as
1381 * (-3) while we want to stop before the "-", so we find the
1382 * separator ourselves and rely on atoi() whose value we may
1383 * ignore depending where the separator is.
1384 */
1385 for (sep = set; isdigit((uchar)*sep); sep++)
1386 ;
1387
1388 if (sep != set && *sep && *sep != '/' && *sep != '-' && *sep != ',') {
1389 memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set);
1390 return -1;
1391 }
1392
1393 v = (sep != set) ? atoi(set) : 0;
1394
1395 /* Now we know that the string is made of an optional series of digits
1396 * optionally followed by one of the delimiters above, or that it
1397 * starts with a different character.
1398 */
1399
1400 /* first, let's search for the thread group (digits before '/') */
1401
1402 if (tg || !is_rel) {
1403 /* thread group already specified or not expected if absolute spec */
1404 if (*sep == '/') {
1405 if (tg)
1406 memprintf(err, "redundant thread group specification '%s' for group %d", set, tg);
1407 else
1408 memprintf(err, "group-relative thread specification '%s' is not permitted after a absolute thread range.", set);
1409 return -1;
1410 }
1411 } else {
1412 /* this is a group-relative spec, first field is the group number */
1413 if (sep == set && *sep == '/') {
1414 memprintf(err, "thread group number expected before '%s'.", set);
1415 return -1;
1416 }
1417
1418 if (*sep != '/') {
1419 memprintf(err, "absolute thread specification '%s' is not permitted after a group-relative thread range.", set);
1420 return -1;
1421 }
1422
1423 if (v < 1 || v > MAX_TGROUPS) {
1424 memprintf(err, "invalid thread group number '%d', permitted range is 1..%d in '%s'.", v, MAX_TGROUPS, set);
1425 return -1;
1426 }
1427
1428 tg = v;
1429
1430 /* skip group number and go on with set,sep,v as if
1431 * there was no group number.
1432 */
1433 set = sep + 1;
1434 continue;
1435 }
1436
1437 /* Now 'set' starts at the min thread number, whose value is in v if any,
1438 * and preset the max to it, unless the range is filled at once via "all"
1439 * (stored as 1:0), "odd" (stored as) 1:-1, or "even" (stored as 1:-2).
1440 * 'sep' points to the next non-digit which may be set itself e.g. for
1441 * "all" etc or "-xx".
1442 */
1443
1444 if (!*set) {
1445 /* empty set sets no restriction */
1446 min = 1;
1447 max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS;
1448 }
1449 else {
1450 if (sep != set && *sep && *sep != '-' && *sep != ',') {
1451 // Only delimitors are permitted around digits.
1452 memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set);
1453 return -1;
1454 }
1455
1456 /* for non-digits, find next delim */
1457 for (; *sep && *sep != '-' && *sep != ','; sep++)
1458 ;
1459
1460 min = max = 1;
1461 if (sep != set) {
1462 /* non-empty first thread */
1463 if (isteq(ist2(set, sep-set), ist("all")))
1464 max = 0;
1465 else if (isteq(ist2(set, sep-set), ist("odd")))
1466 max = -1;
1467 else if (isteq(ist2(set, sep-set), ist("even")))
1468 max = -2;
1469 else if (v)
1470 min = max = v;
1471 else
1472 max = min = 0; // throw an error below
1473 }
1474
1475 if (min < 1 || min > MAX_THREADS || (is_rel && min > MAX_THREADS_PER_GROUP)) {
1476 memprintf(err, "invalid first thread number '%s', permitted range is 1..%d, or 'all', 'odd', 'even'.",
1477 set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS);
1478 return -1;
1479 }
1480
1481 /* is this a range ? */
1482 if (*sep == '-') {
1483 if (min != max) {
1484 memprintf(err, "extraneous range after 'all', 'odd' or 'even': '%s'.", set);
1485 return -1;
1486 }
1487
1488 /* this is a seemingly valid range, there may be another number */
1489 for (set = ++sep; isdigit((uchar)*sep); sep++)
1490 ;
1491 v = atoi(set);
1492
1493 if (sep == set) { // no digit: to the max
1494 max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS;
1495 if (*sep && *sep != ',')
1496 max = 0; // throw an error below
1497 } else
1498 max = v;
1499
1500 if (max < 1 || max > MAX_THREADS || (is_rel && max > MAX_THREADS_PER_GROUP)) {
1501 memprintf(err, "invalid last thread number '%s', permitted range is 1..%d.",
1502 set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS);
1503 return -1;
1504 }
1505 }
1506
1507 /* here sep points to the first non-digit after the thread spec,
1508 * must be a valid delimiter.
1509 */
1510 if (*sep && *sep != ',') {
1511 memprintf(err, "invalid character '%c' after thread set specification: '%s'.", *sep, set);
1512 return -1;
1513 }
1514 }
1515
1516 /* store values */
1517 if (ts) {
1518 if (is_rel) {
1519 /* group-relative thread numbers */
1520 if (!ts->rel[tg - 1])
1521 ts->nbgrp++;
1522
1523 if (max >= min) {
1524 for (v = min; v <= max; v++)
1525 ts->rel[tg - 1] |= 1UL << v;
1526 } else {
1527 memset(&ts->rel[tg - 1],
1528 (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */,
1529 sizeof(ts->rel[tg - 1]));
1530 }
1531 } else {
1532 /* absolute thread numbers */
1533 if (max >= min) {
1534 for (v = min; v <= max; v++)
1535 ts->abs[v / LONGBITS] |= 1UL << (v % LONGBITS);
1536 } else {
1537 memset(&ts->abs,
1538 (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */,
1539 sizeof(ts->abs));
1540 }
1541 }
1542 }
1543
1544 set = *sep ? sep + 1 : sep;
1545 tg = 0;
1546 }
1547 return 0;
Willy Tarreau627def92021-09-29 18:59:47 +02001548}
1549
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001550/* Parse the "nbthread" global directive, which takes an integer argument that
1551 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001552 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001553static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
1554 const struct proxy *defpx, const char *file, int line,
1555 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001556{
1557 long nbthread;
1558 char *errptr;
1559
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001560 if (too_many_args(1, args, err, NULL))
1561 return -1;
1562
Christopher Faulet55343342022-11-18 15:52:58 +01001563 if (non_global_section_parsed == 1) {
1564 memprintf(err, "'%s' not allowed if a non-global section was previously defined. This parameter must be declared in the first global section", args[0]);
1565 return -1;
1566 }
1567
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001568 nbthread = strtol(args[1], &errptr, 10);
1569 if (!*args[1] || *errptr) {
1570 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1571 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001572 }
1573
1574#ifndef USE_THREAD
1575 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001576 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]);
1577 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001578 }
1579#else
1580 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001581 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1582 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001583 }
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001584#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001585
1586 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001587 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1588 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001589
1590 global.nbthread = nbthread;
1591 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001592}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001593
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001594/* Parse the "thread-group" global directive, which takes an integer argument
1595 * that designates a thread group, and a list of threads to put into that group.
1596 */
1597static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx,
1598 const struct proxy *defpx, const char *file, int line,
1599 char **err)
1600{
1601 char *errptr;
1602 long tnum, tend, tgroup;
1603 int arg, tot;
1604
Christopher Faulet55343342022-11-18 15:52:58 +01001605 if (non_global_section_parsed == 1) {
1606 memprintf(err, "'%s' not allowed if a non-global section was previously defined. This parameter must be declared in the first global section", args[0]);
1607 return -1;
1608 }
1609
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001610 tgroup = strtol(args[1], &errptr, 10);
1611 if (!*args[1] || *errptr) {
1612 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1613 return -1;
1614 }
1615
1616 if (tgroup < 1 || tgroup > MAX_TGROUPS) {
1617 memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup);
1618 return -1;
1619 }
1620
1621 /* look for a preliminary definition of any thread pointing to this
1622 * group, and remove them.
1623 */
1624 if (ha_tgroup_info[tgroup-1].count) {
1625 ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n",
1626 file, line, args[0], tgroup);
1627
1628 for (tnum = ha_tgroup_info[tgroup-1].base;
1629 tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count;
1630 tnum++) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001631 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001632 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001633 ha_thread_info[tnum-1].tgid = 0;
Willy Tarreau03f9b352022-06-27 16:02:24 +02001634 ha_thread_info[tnum-1].tg_ctx = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001635 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001636 }
1637 ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0;
1638 }
1639
1640 tot = 0;
1641 for (arg = 2; args[arg] && *args[arg]; arg++) {
1642 tend = tnum = strtol(args[arg], &errptr, 10);
1643
1644 if (*errptr == '-')
1645 tend = strtol(errptr + 1, &errptr, 10);
1646
1647 if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) {
1648 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);
1649 return -1;
1650 }
1651
1652 for(; tnum <= tend; tnum++) {
1653 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
1654 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n",
1655 file, line, args[0], tgroup, tnum);
1656 } else if (ha_thread_info[tnum-1].tg) {
1657 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n",
1658 file, line, args[0], tgroup, tnum,
1659 (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1));
1660 }
1661
1662 if (!ha_tgroup_info[tgroup-1].count) {
1663 ha_tgroup_info[tgroup-1].base = tnum-1;
1664 ha_tgroup_info[tgroup-1].count = 1;
1665 }
1666 else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) {
1667 ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base;
1668 }
1669 else if (tnum < ha_tgroup_info[tgroup-1].base) {
1670 ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1;
1671 ha_tgroup_info[tgroup-1].base = tnum - 1;
1672 }
1673
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001674 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001675 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001676 ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1];
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001677 tot++;
1678 }
1679 }
1680
1681 if (ha_tgroup_info[tgroup-1].count > tot) {
1682 memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup);
1683 return -1;
1684 }
1685
1686 if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) {
1687 memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP);
1688 return -1;
1689 }
1690
1691 return 0;
1692}
1693
Willy Tarreauc33b9692021-09-22 12:07:23 +02001694/* Parse the "thread-groups" global directive, which takes an integer argument
1695 * that contains the desired number of thread groups.
1696 */
1697static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx,
1698 const struct proxy *defpx, const char *file, int line,
1699 char **err)
1700{
1701 long nbtgroups;
1702 char *errptr;
1703
1704 if (too_many_args(1, args, err, NULL))
1705 return -1;
Christopher Faulet55343342022-11-18 15:52:58 +01001706
1707 if (non_global_section_parsed == 1) {
1708 memprintf(err, "'%s' not allowed if a non-global section was previously defined. This parameter must be declared in the first global section", args[0]);
1709 return -1;
1710 }
Willy Tarreauc33b9692021-09-22 12:07:23 +02001711
1712 nbtgroups = strtol(args[1], &errptr, 10);
1713 if (!*args[1] || *errptr) {
1714 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1715 return -1;
1716 }
1717
1718#ifndef USE_THREAD
1719 if (nbtgroups != 1) {
1720 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]);
1721 return -1;
1722 }
1723#else
1724 if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) {
1725 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups);
1726 return -1;
1727 }
1728#endif
1729
1730 HA_DIAG_WARNING_COND(global.nbtgroups,
1731 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1732 file, line, args[0]);
1733
1734 global.nbtgroups = nbtgroups;
1735 return 0;
1736}
1737
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001738/* config keyword parsers */
1739static struct cfg_kw_list cfg_kws = {ILH, {
1740 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001741 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001742 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001743 { 0, NULL, NULL }
1744}};
1745
1746INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);