blob: d7128252ed0ea4f258369163280bcf409eb9c429 [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 Tarreauf5b63272023-03-09 10:12:06 +0100360/* returns the number of CPUs the current process is enabled to run on,
361 * regardless of any MAX_THREADS limitation.
362 */
Willy Tarreau149ab772019-01-26 14:27:06 +0100363static int thread_cpus_enabled()
364{
365 int ret = 1;
366
367#ifdef USE_CPU_AFFINITY
368#if defined(__linux__) && defined(CPU_COUNT)
369 cpu_set_t mask;
370
371 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
372 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200373#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
374 cpuset_t cpuset;
375 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
376 sizeof(cpuset), &cpuset) == 0)
377 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000378#elif defined(__APPLE__)
379 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100380#endif
381#endif
382 ret = MAX(ret, 1);
Willy Tarreau149ab772019-01-26 14:27:06 +0100383 return ret;
384}
385
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200386/* Returns 1 if the cpu set is currently restricted for the process else 0.
387 * Currently only implemented for the Linux platform.
388 */
389int thread_cpu_mask_forced()
390{
391#if defined(__linux__)
392 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
393 return cpus_avail != thread_cpus_enabled();
394#else
395 return 0;
396#endif
397}
398
Willy Tarreau407ef892021-10-05 18:39:27 +0200399/* Below come the lock-debugging functions */
400
401#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
402
403struct lock_stat lock_stats[LOCK_LABELS];
404
405/* this is only used below */
406static const char *lock_label(enum lock_label label)
407{
408 switch (label) {
409 case TASK_RQ_LOCK: return "TASK_RQ";
410 case TASK_WQ_LOCK: return "TASK_WQ";
411 case LISTENER_LOCK: return "LISTENER";
412 case PROXY_LOCK: return "PROXY";
413 case SERVER_LOCK: return "SERVER";
414 case LBPRM_LOCK: return "LBPRM";
415 case SIGNALS_LOCK: return "SIGNALS";
416 case STK_TABLE_LOCK: return "STK_TABLE";
417 case STK_SESS_LOCK: return "STK_SESS";
418 case APPLETS_LOCK: return "APPLETS";
419 case PEER_LOCK: return "PEER";
420 case SHCTX_LOCK: return "SHCTX";
421 case SSL_LOCK: return "SSL";
422 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
423 case PATREF_LOCK: return "PATREF";
424 case PATEXP_LOCK: return "PATEXP";
425 case VARS_LOCK: return "VARS";
426 case COMP_POOL_LOCK: return "COMP_POOL";
427 case LUA_LOCK: return "LUA";
428 case NOTIF_LOCK: return "NOTIF";
429 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
430 case DNS_LOCK: return "DNS";
431 case PID_LIST_LOCK: return "PID_LIST";
432 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
433 case PIPES_LOCK: return "PIPES";
434 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
435 case AUTH_LOCK: return "AUTH";
436 case LOGSRV_LOCK: return "LOGSRV";
437 case DICT_LOCK: return "DICT";
438 case PROTO_LOCK: return "PROTO";
439 case QUEUE_LOCK: return "QUEUE";
440 case CKCH_LOCK: return "CKCH";
441 case SNI_LOCK: return "SNI";
442 case SSL_SERVER_LOCK: return "SSL_SERVER";
443 case SFT_LOCK: return "SFT";
444 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
Remi Tricot-Le Breton2b963642022-12-20 11:11:02 +0100445 case OCSP_LOCK: return "OCSP";
Amaury Denoyellee83f9372023-04-18 11:10:54 +0200446 case QC_CID_LOCK: return "QC_CID";
Willy Tarreau407ef892021-10-05 18:39:27 +0200447 case OTHER_LOCK: return "OTHER";
448 case DEBUG1_LOCK: return "DEBUG1";
449 case DEBUG2_LOCK: return "DEBUG2";
450 case DEBUG3_LOCK: return "DEBUG3";
451 case DEBUG4_LOCK: return "DEBUG4";
452 case DEBUG5_LOCK: return "DEBUG5";
453 case LOCK_LABELS: break; /* keep compiler happy */
454 };
455 /* only way to come here is consecutive to an internal bug */
456 abort();
457}
458
459void show_lock_stats()
460{
461 int lbl;
462
463 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
464 if (!lock_stats[lbl].num_write_locked &&
465 !lock_stats[lbl].num_seek_locked &&
466 !lock_stats[lbl].num_read_locked) {
467 fprintf(stderr,
468 "Stats about Lock %s: not used\n",
469 lock_label(lbl));
470 continue;
471 }
472
473 fprintf(stderr,
474 "Stats about Lock %s: \n",
475 lock_label(lbl));
476
477 if (lock_stats[lbl].num_write_locked)
478 fprintf(stderr,
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100479 "\t # write lock : %llu\n"
480 "\t # write unlock: %llu (%lld)\n"
Willy Tarreau407ef892021-10-05 18:39:27 +0200481 "\t # wait time for write : %.3f msec\n"
482 "\t # wait time for write/lock: %.3f nsec\n",
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100483 (ullong)lock_stats[lbl].num_write_locked,
484 (ullong)lock_stats[lbl].num_write_unlocked,
485 (llong)(lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked),
Willy Tarreau407ef892021-10-05 18:39:27 +0200486 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
487 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
488
489 if (lock_stats[lbl].num_seek_locked)
490 fprintf(stderr,
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100491 "\t # seek lock : %llu\n"
492 "\t # seek unlock : %llu (%lld)\n"
Willy Tarreau407ef892021-10-05 18:39:27 +0200493 "\t # wait time for seek : %.3f msec\n"
494 "\t # wait time for seek/lock : %.3f nsec\n",
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100495 (ullong)lock_stats[lbl].num_seek_locked,
496 (ullong)lock_stats[lbl].num_seek_unlocked,
497 (llong)(lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked),
Willy Tarreau407ef892021-10-05 18:39:27 +0200498 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
499 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
500
501 if (lock_stats[lbl].num_read_locked)
502 fprintf(stderr,
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100503 "\t # read lock : %llu\n"
504 "\t # read unlock : %llu (%lld)\n"
Willy Tarreau407ef892021-10-05 18:39:27 +0200505 "\t # wait time for read : %.3f msec\n"
506 "\t # wait time for read/lock : %.3f nsec\n",
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100507 (ullong)lock_stats[lbl].num_read_locked,
508 (ullong)lock_stats[lbl].num_read_unlocked,
509 (llong)(lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked),
Willy Tarreau407ef892021-10-05 18:39:27 +0200510 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
511 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
512 }
513}
514
Willy Tarreau407ef892021-10-05 18:39:27 +0200515void __ha_rwlock_init(struct ha_rwlock *l)
516{
517 memset(l, 0, sizeof(struct ha_rwlock));
518 __RWLOCK_INIT(&l->lock);
519}
520
521void __ha_rwlock_destroy(struct ha_rwlock *l)
522{
523 __RWLOCK_DESTROY(&l->lock);
524 memset(l, 0, sizeof(struct ha_rwlock));
525}
526
527
528void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
529 const char *func, const char *file, int line)
530{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200531 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
532 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200533 uint64_t start_time;
534
Willy Tarreau7aa41192022-07-15 17:53:10 +0200535 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200536 abort();
537
Willy Tarreau7aa41192022-07-15 17:53:10 +0200538 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200539
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200540 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200541 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200542 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200543
544 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
545
Willy Tarreau7aa41192022-07-15 17:53:10 +0200546 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200547 l->info.last_location.function = func;
548 l->info.last_location.file = file;
549 l->info.last_location.line = line;
550
Willy Tarreau7aa41192022-07-15 17:53:10 +0200551 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200552}
553
554int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
555 const char *func, const char *file, int line)
556{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200557 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
558 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200559 uint64_t start_time;
560 int r;
561
Willy Tarreau7aa41192022-07-15 17:53:10 +0200562 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200563 abort();
564
565 /* We set waiting writer because trywrlock could wait for readers to quit */
Willy Tarreau7aa41192022-07-15 17:53:10 +0200566 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200567
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200568 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200569 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200570 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200571 if (unlikely(r)) {
Willy Tarreau7aa41192022-07-15 17:53:10 +0200572 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200573 return r;
574 }
575 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
576
Willy Tarreau7aa41192022-07-15 17:53:10 +0200577 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200578 l->info.last_location.function = func;
579 l->info.last_location.file = file;
580 l->info.last_location.line = line;
581
Willy Tarreau7aa41192022-07-15 17:53:10 +0200582 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200583
584 return 0;
585}
586
587void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
588 const char *func, const char *file, int line)
589{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200590 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
591 struct ha_rwlock_state *st = &l->info.st[tgid-1];
592
593 if (unlikely(!(st->cur_writer & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200594 /* the thread is not owning the lock for write */
595 abort();
596 }
597
Willy Tarreau7aa41192022-07-15 17:53:10 +0200598 st->cur_writer = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200599 l->info.last_location.function = func;
600 l->info.last_location.file = file;
601 l->info.last_location.line = line;
602
603 __RWLOCK_WRUNLOCK(&l->lock);
604
605 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
606}
607
608void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
609{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200610 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
611 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200612 uint64_t start_time;
613
Willy Tarreau7aa41192022-07-15 17:53:10 +0200614 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200615 abort();
616
Willy Tarreau7aa41192022-07-15 17:53:10 +0200617 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200618
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200619 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200620 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200621 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200622 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
623
Willy Tarreau7aa41192022-07-15 17:53:10 +0200624 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200625
Willy Tarreau7aa41192022-07-15 17:53:10 +0200626 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200627}
628
629int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
630{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200631 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
632 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200633 int r;
634
Willy Tarreau7aa41192022-07-15 17:53:10 +0200635 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200636 abort();
637
638 /* try read should never wait */
639 r = __RWLOCK_TRYRDLOCK(&l->lock);
640 if (unlikely(r))
641 return r;
642 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
643
Willy Tarreau7aa41192022-07-15 17:53:10 +0200644 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200645
646 return 0;
647}
648
649void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
650{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200651 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
652 struct ha_rwlock_state *st = &l->info.st[tgid-1];
653
654 if (unlikely(!(st->cur_readers & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200655 /* the thread is not owning the lock for read */
656 abort();
657 }
658
Willy Tarreau7aa41192022-07-15 17:53:10 +0200659 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200660
661 __RWLOCK_RDUNLOCK(&l->lock);
662
663 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
664}
665
666void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
667 const char *func, const char *file, int line)
668{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200669 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
670 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200671 uint64_t start_time;
672
Willy Tarreau7aa41192022-07-15 17:53:10 +0200673 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200674 abort();
675
Willy Tarreau7aa41192022-07-15 17:53:10 +0200676 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200677 abort();
678
Willy Tarreau7aa41192022-07-15 17:53:10 +0200679 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200680
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200681 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200682 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200683 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200684
685 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
686
Willy Tarreau7aa41192022-07-15 17:53:10 +0200687 HA_ATOMIC_OR(&st->cur_readers, tbit);
688 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200689 l->info.last_location.function = func;
690 l->info.last_location.file = file;
691 l->info.last_location.line = line;
692
Willy Tarreau7aa41192022-07-15 17:53:10 +0200693 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200694}
695
696void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
697 const char *func, const char *file, int line)
698{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200699 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
700 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200701 uint64_t start_time;
702
Willy Tarreau7aa41192022-07-15 17:53:10 +0200703 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200704 abort();
705
Willy Tarreau7aa41192022-07-15 17:53:10 +0200706 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200707 abort();
708
Willy Tarreau7aa41192022-07-15 17:53:10 +0200709 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200710
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200711 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200712 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200713 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200714
715 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
716
Willy Tarreau7aa41192022-07-15 17:53:10 +0200717 HA_ATOMIC_OR(&st->cur_seeker, tbit);
718 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200719 l->info.last_location.function = func;
720 l->info.last_location.file = file;
721 l->info.last_location.line = line;
722
Willy Tarreau7aa41192022-07-15 17:53:10 +0200723 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200724}
725
726void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
727 const char *func, const char *file, int line)
728{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200729 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
730 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200731 uint64_t start_time;
732
Willy Tarreau7aa41192022-07-15 17:53:10 +0200733 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200734 abort();
735
Willy Tarreau7aa41192022-07-15 17:53:10 +0200736 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200737
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200738 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200739 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200740 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200741
742 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
743
Willy Tarreau7aa41192022-07-15 17:53:10 +0200744 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200745 l->info.last_location.function = func;
746 l->info.last_location.file = file;
747 l->info.last_location.line = line;
748
Willy Tarreau7aa41192022-07-15 17:53:10 +0200749 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200750}
751
752void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
753 const char *func, const char *file, int line)
754{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200755 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
756 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200757 uint64_t start_time;
758
Willy Tarreau7aa41192022-07-15 17:53:10 +0200759 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200760 abort();
761
Willy Tarreau7aa41192022-07-15 17:53:10 +0200762 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200763 abort();
764
Willy Tarreau7aa41192022-07-15 17:53:10 +0200765 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200766
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200767 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200768 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200769 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200770
771 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
772
Willy Tarreau7aa41192022-07-15 17:53:10 +0200773 HA_ATOMIC_OR(&st->cur_writer, tbit);
774 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200775 l->info.last_location.function = func;
776 l->info.last_location.file = file;
777 l->info.last_location.line = line;
778
Willy Tarreau7aa41192022-07-15 17:53:10 +0200779 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200780}
781
782void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
783 const char *func, const char *file, int line)
784{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200785 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
786 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200787 uint64_t start_time;
788
Willy Tarreau7aa41192022-07-15 17:53:10 +0200789 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200790 abort();
791
Willy Tarreau7aa41192022-07-15 17:53:10 +0200792 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200793 abort();
794
Willy Tarreau7aa41192022-07-15 17:53:10 +0200795 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200796
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200797 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200798 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200799 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200800
801 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
802
Willy Tarreau7aa41192022-07-15 17:53:10 +0200803 HA_ATOMIC_OR(&st->cur_readers, tbit);
804 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200805 l->info.last_location.function = func;
806 l->info.last_location.file = file;
807 l->info.last_location.line = line;
808
Willy Tarreau7aa41192022-07-15 17:53:10 +0200809 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200810}
811
812void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
813 const char *func, const char *file, int line)
814{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200815 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
816 struct ha_rwlock_state *st = &l->info.st[tgid-1];
817 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200818 abort();
819
Willy Tarreau7aa41192022-07-15 17:53:10 +0200820 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200821 l->info.last_location.function = func;
822 l->info.last_location.file = file;
823 l->info.last_location.line = line;
824
825 __RWLOCK_SKUNLOCK(&l->lock);
826
827 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
828}
829
830int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
831 const char *func, const char *file, int line)
832{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200833 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
834 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200835 uint64_t start_time;
836 int r;
837
Willy Tarreau7aa41192022-07-15 17:53:10 +0200838 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200839 abort();
840
Willy Tarreau7aa41192022-07-15 17:53:10 +0200841 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200842
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200843 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200844 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200845 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200846
847 if (likely(!r)) {
848 /* got the lock ! */
849 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200850 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200851 l->info.last_location.function = func;
852 l->info.last_location.file = file;
853 l->info.last_location.line = line;
854 }
855
Willy Tarreau7aa41192022-07-15 17:53:10 +0200856 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200857 return r;
858}
859
860int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
861 const char *func, const char *file, int line)
862{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200863 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
864 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200865 uint64_t start_time;
866 int r;
867
Willy Tarreau7aa41192022-07-15 17:53:10 +0200868 if ((st->cur_writer | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200869 abort();
870
Willy Tarreau7aa41192022-07-15 17:53:10 +0200871 if (!(st->cur_readers & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200872 abort();
873
Willy Tarreau7aa41192022-07-15 17:53:10 +0200874 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200875
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200876 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200877 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200878 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200879
880 if (likely(!r)) {
881 /* got the lock ! */
882 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200883 HA_ATOMIC_OR(&st->cur_seeker, tbit);
884 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200885 l->info.last_location.function = func;
886 l->info.last_location.file = file;
887 l->info.last_location.line = line;
888 }
889
Willy Tarreau7aa41192022-07-15 17:53:10 +0200890 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200891 return r;
892}
893
894void __spin_init(struct ha_spinlock *l)
895{
896 memset(l, 0, sizeof(struct ha_spinlock));
897 __SPIN_INIT(&l->lock);
898}
899
900void __spin_destroy(struct ha_spinlock *l)
901{
902 __SPIN_DESTROY(&l->lock);
903 memset(l, 0, sizeof(struct ha_spinlock));
904}
905
906void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
907 const char *func, const char *file, int line)
908{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200909 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
910 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200911 uint64_t start_time;
912
Willy Tarreau7aa41192022-07-15 17:53:10 +0200913 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200914 /* the thread is already owning the lock */
915 abort();
916 }
917
Willy Tarreau7aa41192022-07-15 17:53:10 +0200918 HA_ATOMIC_OR(&st->waiters, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200919
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200920 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200921 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200922 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200923
924 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
925
926
Willy Tarreau7aa41192022-07-15 17:53:10 +0200927 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200928 l->info.last_location.function = func;
929 l->info.last_location.file = file;
930 l->info.last_location.line = line;
931
Willy Tarreau7aa41192022-07-15 17:53:10 +0200932 HA_ATOMIC_AND(&st->waiters, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200933}
934
935int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
936 const char *func, const char *file, int line)
937{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200938 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
939 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200940 int r;
941
Willy Tarreau7aa41192022-07-15 17:53:10 +0200942 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200943 /* the thread is already owning the lock */
944 abort();
945 }
946
947 /* try read should never wait */
948 r = __SPIN_TRYLOCK(&l->lock);
949 if (unlikely(r))
950 return r;
951 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
952
Willy Tarreau7aa41192022-07-15 17:53:10 +0200953 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200954 l->info.last_location.function = func;
955 l->info.last_location.file = file;
956 l->info.last_location.line = line;
957
958 return 0;
959}
960
961void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
962 const char *func, const char *file, int line)
963{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200964 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
965 struct ha_spinlock_state *st = &l->info.st[tgid-1];
966
967 if (unlikely(!(st->owner & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200968 /* the thread is not owning the lock */
969 abort();
970 }
971
Willy Tarreau7aa41192022-07-15 17:53:10 +0200972 st->owner = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200973 l->info.last_location.function = func;
974 l->info.last_location.file = file;
975 l->info.last_location.line = line;
976
977 __SPIN_UNLOCK(&l->lock);
978 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
979}
980
981#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
982
Willy Tarreau87aff022022-07-10 10:58:57 +0200983
984#if defined(USE_PTHREAD_EMULATION)
985
986/* pthread rwlock emulation using plocks (to avoid expensive futexes).
987 * these are a direct mapping on Progressive Locks, with the exception that
988 * since there's a common unlock operation in pthreads, we need to know if
989 * we need to unlock for reads or writes, so we set the topmost bit to 1 when
990 * a write lock is acquired to indicate that a write unlock needs to be
991 * performed. It's not a problem since this bit will never be used given that
992 * haproxy won't support as many threads as the plocks.
993 *
994 * The storage is the pthread_rwlock_t cast as an ulong
995 */
996
997int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr)
998{
999 ulong *lock = (ulong *)rwlock;
1000
1001 *lock = 0;
1002 return 0;
1003}
1004
1005int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
1006{
1007 ulong *lock = (ulong *)rwlock;
1008
1009 *lock = 0;
1010 return 0;
1011}
1012
1013int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
1014{
1015 pl_lorw_rdlock((unsigned long *)rwlock);
1016 return 0;
1017}
1018
1019int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
1020{
1021 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_SHR_BASE);
1022}
1023
1024int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1025{
1026 return pthread_rwlock_tryrdlock(rwlock);
1027}
1028
1029int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
1030{
1031 pl_lorw_wrlock((unsigned long *)rwlock);
1032 return 0;
1033}
1034
1035int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
1036{
1037 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_EXC_BASE);
1038}
1039
1040int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1041{
1042 return pthread_rwlock_trywrlock(rwlock);
1043}
1044
1045int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
1046{
1047 pl_lorw_unlock((unsigned long *)rwlock);
1048 return 0;
1049}
1050#endif // defined(USE_PTHREAD_EMULATION)
1051
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001052/* Depending on the platform and how libpthread was built, pthread_exit() may
1053 * involve some code in libgcc_s that would be loaded on exit for the first
1054 * time, causing aborts if the process is chrooted. It's harmless bit very
1055 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
1056 * needed, so what we do here is that during early boot we create a dummy
1057 * thread that immediately exits. This will lead to libgcc_s being loaded
1058 * during boot on the platforms where it's required.
1059 */
1060static void *dummy_thread_function(void *data)
1061{
1062 pthread_exit(NULL);
1063 return NULL;
1064}
1065
1066static inline void preload_libgcc_s(void)
1067{
1068 pthread_t dummy_thread;
1069 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
1070 pthread_join(dummy_thread, NULL);
1071}
1072
Willy Tarreau3f567e42020-05-28 15:29:19 +02001073static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001074{
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001075 char *ptr = NULL;
1076
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001077 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +02001078
Willy Tarreau149ab772019-01-26 14:27:06 +01001079 thread_cpus_enabled_at_boot = thread_cpus_enabled();
Willy Tarreauf5b63272023-03-09 10:12:06 +01001080 thread_cpus_enabled_at_boot = MIN(thread_cpus_enabled_at_boot, MAX_THREADS);
Willy Tarreau149ab772019-01-26 14:27:06 +01001081
Willy Tarreauc80bdb22022-08-06 16:44:55 +02001082 memprintf(&ptr, "Built with multi-threading support (MAX_TGROUPS=%d, MAX_THREADS=%d, default=%d).",
1083 MAX_TGROUPS, MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001084 hap_register_build_opts(ptr, 1);
1085
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001086#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
1087 memset(lock_stats, 0, sizeof(lock_stats));
1088#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001089}
Willy Tarreau8ead1d02022-04-25 19:23:17 +02001090INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001091
Willy Tarreau8459f252018-12-15 16:48:14 +01001092#else
1093
Willy Tarreauaa992762021-10-06 23:33:20 +02001094/* send signal <sig> to thread <thr> (send to process in fact) */
1095void ha_tkill(unsigned int thr, int sig)
1096{
1097 raise(sig);
1098}
1099
1100/* send signal <sig> to all threads (send to process in fact) */
1101void ha_tkillall(int sig)
1102{
1103 raise(sig);
1104}
1105
1106void ha_thread_relax(void)
1107{
1108#ifdef _POSIX_PRIORITY_SCHEDULING
1109 sched_yield();
1110#endif
1111}
1112
Willy Tarreau8459f252018-12-15 16:48:14 +01001113REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
1114
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001115#endif // USE_THREAD
1116
1117
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001118/* scans the configured thread mapping and establishes the final one. Returns <0
1119 * on failure, >=0 on success.
1120 */
1121int thread_map_to_groups()
1122{
1123 int t, g, ut, ug;
1124 int q, r;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001125 ulong m __maybe_unused;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001126
1127 ut = ug = 0; // unassigned threads & groups
1128
1129 for (t = 0; t < global.nbthread; t++) {
1130 if (!ha_thread_info[t].tg)
1131 ut++;
1132 }
1133
1134 for (g = 0; g < global.nbtgroups; g++) {
1135 if (!ha_tgroup_info[g].count)
1136 ug++;
Willy Tarreau60fe4a92022-06-28 17:48:07 +02001137 ha_tgroup_info[g].tgid_bit = 1UL << g;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001138 }
1139
1140 if (ug > ut) {
1141 ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut);
1142 return -1;
1143 }
1144
1145 /* look for first unassigned thread */
1146 for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++)
1147 ;
1148
1149 /* assign threads to empty groups */
1150 for (g = 0; ug && ut; ) {
1151 /* due to sparse thread assignment we can end up with more threads
1152 * per group on last assigned groups than former ones, so we must
1153 * always try to pack the maximum remaining ones together first.
1154 */
1155 q = ut / ug;
1156 r = ut % ug;
1157 if ((q + !!r) > MAX_THREADS_PER_GROUP) {
Willy Tarreaucf0d0ee2023-03-09 11:39:51 +01001158 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", ut, ug);
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001159 return -1;
1160 }
1161
1162 /* thread <t> is the next unassigned one. Let's look for next
1163 * unassigned group, we know there are some left
1164 */
1165 while (ut >= ug && ha_tgroup_info[g].count)
1166 g++;
1167
1168 /* group g is unassigned, try to fill it with consecutive threads */
1169 while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r &&
1170 (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) {
1171
1172 if (!ha_tgroup_info[g].count) {
1173 /* assign new group */
1174 ha_tgroup_info[g].base = t;
1175 ug--;
1176 }
1177
1178 ha_tgroup_info[g].count++;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001179 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001180 ha_thread_info[t].tg = &ha_tgroup_info[g];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001181 ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g];
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001182
1183 ut--;
1184 /* switch to next unassigned thread */
1185 while (++t < global.nbthread && ha_thread_info[t].tg)
1186 ;
1187 }
1188 }
1189
1190 if (ut) {
1191 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);
1192 return -1;
1193 }
1194
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001195 for (t = 0; t < global.nbthread; t++) {
1196 ha_thread_info[t].tid = t;
1197 ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base;
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001198 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1199 }
1200
Willy Tarreaucce203a2022-06-24 15:55:11 +02001201 m = 0;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001202 for (g = 0; g < global.nbtgroups; g++) {
1203 ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count);
Aurelien DARRAGON739281b2023-01-27 15:13:28 +01001204 /* for now, additional threads are not started, so we should
1205 * consider them as harmless and idle.
1206 * This will get automatically updated when such threads are
1207 * started in run_thread_poll_loop()
1208 * Without this, thread_isolate() and thread_isolate_full()
1209 * will fail to work as long as secondary threads did not enter
1210 * the polling loop at least once.
1211 */
1212 ha_tgroup_ctx[g].threads_harmless = ha_tgroup_info[g].threads_enabled;
1213 ha_tgroup_ctx[g].threads_idle = ha_tgroup_info[g].threads_enabled;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001214 if (!ha_tgroup_info[g].count)
1215 continue;
1216 m |= 1UL << g;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001217
1218 }
1219
Willy Tarreaucce203a2022-06-24 15:55:11 +02001220#ifdef USE_THREAD
1221 all_tgroups_mask = m;
1222#endif
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001223 return 0;
1224}
1225
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001226/* Converts a configuration thread set based on either absolute or relative
1227 * thread numbers into a global group+mask. This is essentially for use with
1228 * the "thread" directive on "bind" lines, where "thread 4-6,10-12" might be
1229 * turned to "2/1-3,4/1-3". It cannot be used before the thread mapping above
1230 * was completed and the thread group numbers configured. The thread_set is
1231 * replaced by the resolved group-based one. It is possible to force a single
1232 * default group for unspecified sets instead of enabling all groups by passing
Willy Tarreauf2988e12023-02-02 17:01:10 +01001233 * this group's non-zero value to defgrp.
Willy Tarreau627def92021-09-29 18:59:47 +02001234 *
1235 * Returns <0 on failure, >=0 on success.
1236 */
Willy Tarreauf2988e12023-02-02 17:01:10 +01001237int thread_resolve_group_mask(struct thread_set *ts, int defgrp, char **err)
Willy Tarreau627def92021-09-29 18:59:47 +02001238{
Willy Tarreau15c84282023-02-04 10:49:01 +01001239 struct thread_set new_ts = { };
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001240 ulong mask, imask;
1241 uint g;
Willy Tarreau627def92021-09-29 18:59:47 +02001242
Willy Tarreau97da9422023-03-01 11:24:29 +01001243 if (!ts->grps) {
Willy Tarreau627def92021-09-29 18:59:47 +02001244 /* unspecified group, IDs are global */
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001245 if (thread_set_is_empty(ts)) {
1246 /* all threads of all groups, unless defgrp is set and
1247 * we then set it as the only group.
1248 */
1249 for (g = defgrp ? defgrp-1 : 0; g < (defgrp ? defgrp : global.nbtgroups); g++) {
1250 new_ts.rel[g] = ha_tgroup_info[g].threads_enabled;
Willy Tarreau97da9422023-03-01 11:24:29 +01001251 if (new_ts.rel[g])
1252 new_ts.grps |= 1UL << g;
Willy Tarreau627def92021-09-29 18:59:47 +02001253 }
Willy Tarreau627def92021-09-29 18:59:47 +02001254 } else {
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001255 /* some absolute threads are set, we must remap them to
1256 * relative ones. Each group cannot have more than
1257 * LONGBITS threads, thus it spans at most two absolute
1258 * blocks.
1259 */
1260 for (g = 0; g < global.nbtgroups; g++) {
1261 uint block = ha_tgroup_info[g].base / LONGBITS;
1262 uint base = ha_tgroup_info[g].base % LONGBITS;
Willy Tarreau627def92021-09-29 18:59:47 +02001263
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001264 mask = ts->abs[block] >> base;
Willy Tarreau1b536a12023-03-22 10:28:50 +01001265 if (base &&
1266 (block + 1) < sizeof(ts->abs) / sizeof(ts->abs[0]) &&
1267 ha_tgroup_info[g].count > (LONGBITS - base))
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001268 mask |= ts->abs[block + 1] << (LONGBITS - base);
1269 mask &= nbits(ha_tgroup_info[g].count);
1270 mask &= ha_tgroup_info[g].threads_enabled;
Willy Tarreau627def92021-09-29 18:59:47 +02001271
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001272 /* now the mask exactly matches the threads to be enabled
1273 * in this group.
1274 */
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001275 new_ts.rel[g] |= mask;
Willy Tarreau97da9422023-03-01 11:24:29 +01001276 if (new_ts.rel[g])
1277 new_ts.grps |= 1UL << g;
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001278 }
Willy Tarreau627def92021-09-29 18:59:47 +02001279 }
1280 } else {
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001281 /* groups were specified */
1282 for (g = 0; g < MAX_TGROUPS; g++) {
1283 imask = ts->rel[g];
1284 if (!imask)
1285 continue;
Willy Tarreau627def92021-09-29 18:59:47 +02001286
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001287 if (g >= global.nbtgroups) {
1288 memprintf(err, "'thread' directive references non-existing thread group %u", g+1);
1289 return -1;
1290 }
Willy Tarreau627def92021-09-29 18:59:47 +02001291
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001292 /* some relative threads are set. Keep only existing ones for this group */
1293 mask = nbits(ha_tgroup_info[g].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001294
1295 if (!(mask & imask)) {
1296 /* no intersection between the thread group's
1297 * threads and the bind line's.
1298 */
1299#ifdef THREAD_AUTO_ADJUST_GROUPS
1300 unsigned long new_mask = 0;
1301
1302 while (imask) {
1303 new_mask |= imask & mask;
Willy Tarreaubef43df2023-01-31 19:27:48 +01001304 imask >>= ha_tgroup_info[g].count;
Willy Tarreau627def92021-09-29 18:59:47 +02001305 }
1306 imask = new_mask;
1307#else
Willy Tarreaubef43df2023-01-31 19:27:48 +01001308 memprintf(err, "'thread' directive only references threads not belonging to group %u", g+1);
Willy Tarreau627def92021-09-29 18:59:47 +02001309 return -1;
1310#endif
1311 }
1312
Willy Tarreaubef43df2023-01-31 19:27:48 +01001313 new_ts.rel[g] = imask & mask;
Willy Tarreau97da9422023-03-01 11:24:29 +01001314 if (new_ts.rel[g])
1315 new_ts.grps |= 1UL << g;
Willy Tarreau627def92021-09-29 18:59:47 +02001316 }
1317 }
Willy Tarreaubef43df2023-01-31 19:27:48 +01001318
1319 /* update the thread_set */
Willy Tarreau7b8aac42023-02-27 11:27:38 +01001320 if (!thread_set_nth_group(&new_ts, 0)) {
Willy Tarreaubef43df2023-01-31 19:27:48 +01001321 memprintf(err, "'thread' directive only references non-existing threads");
1322 return -1;
1323 }
1324
1325 *ts = new_ts;
Willy Tarreaubef43df2023-01-31 19:27:48 +01001326 return 0;
1327}
1328
1329/* Parse a string representing a thread set in one of the following forms:
1330 *
1331 * - { "all" | "odd" | "even" | <abs_num> [ "-" <abs_num> ] }[,...]
1332 * => these are (lists of) absolute thread numbers
1333 *
1334 * - <tgnum> "/" { "all" | "odd" | "even" | <rel_num> [ "-" <rel_num> ][,...]
1335 * => these are (lists of) per-group relative thread numbers. All numbers
1336 * must be lower than or equal to LONGBITS. When multiple list elements
1337 * are provided, each of them must contain the thread group number.
1338 *
1339 * Minimum value for a thread or group number is always 1. Maximum value for an
1340 * absolute thread number is MAX_THREADS, maximum value for a relative thread
1341 * number is MAX_THREADS_PER_GROUP, an maximum value for a thread group is
1342 * MAX_TGROUPS. "all", "even" and "odd" will be bound by MAX_THREADS and/or
1343 * MAX_THREADS_PER_GROUP in any case. In ranges, a missing digit before "-"
1344 * is implicitly 1, and a missing digit after "-" is implicitly the highest of
1345 * its class. As such "-" is equivalent to "all", allowing to build strings
1346 * such as "${MIN}-${MAX}" where both MIN and MAX are optional.
1347 *
1348 * It is not valid to mix absolute and relative numbers. As such:
1349 * - all valid (all absolute threads)
1350 * - 12-19,24-31 valid (abs threads 12 to 19 and 24 to 31)
1351 * - 1/all valid (all 32 or 64 threads of group 1)
1352 * - 1/1-4,1/8-10,2/1 valid
1353 * - 1/1-4,8-10 invalid (mixes relatve "1/1-4" with absolute "8-10")
1354 * - 1-4,8-10,2/1 invalid (mixes absolute "1-4,8-10" with relative "2/1")
1355 * - 1/odd-4 invalid (mixes range with boundary)
1356 *
1357 * The target thread set is *completed* with supported threads, which means
1358 * that it's the caller's responsibility for pre-initializing it. If the target
1359 * thread set is NULL, it's not updated and the function only verifies that the
1360 * input parses.
1361 *
1362 * On success, it returns 0. otherwise it returns non-zero with an error
1363 * message in <err>.
1364 */
1365int parse_thread_set(const char *arg, struct thread_set *ts, char **err)
1366{
1367 const char *set;
1368 const char *sep;
1369 int v, min, max, tg;
1370 int is_rel;
1371
1372 /* search for the first delimiter (',', '-' or '/') to decide whether
1373 * we're facing an absolute or relative form. The relative form always
1374 * starts with a number followed by a slash.
1375 */
1376 for (sep = arg; isdigit((uchar)*sep); sep++)
1377 ;
1378
1379 is_rel = (/*sep > arg &&*/ *sep == '/'); /* relative form */
1380
1381 /* from there we have to cut the thread spec around commas */
1382
1383 set = arg;
1384 tg = 0;
1385 while (*set) {
1386 /* note: we can't use strtol() here because "-3" would parse as
1387 * (-3) while we want to stop before the "-", so we find the
1388 * separator ourselves and rely on atoi() whose value we may
1389 * ignore depending where the separator is.
1390 */
1391 for (sep = set; isdigit((uchar)*sep); sep++)
1392 ;
1393
1394 if (sep != set && *sep && *sep != '/' && *sep != '-' && *sep != ',') {
1395 memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set);
1396 return -1;
1397 }
1398
1399 v = (sep != set) ? atoi(set) : 0;
1400
1401 /* Now we know that the string is made of an optional series of digits
1402 * optionally followed by one of the delimiters above, or that it
1403 * starts with a different character.
1404 */
1405
1406 /* first, let's search for the thread group (digits before '/') */
1407
1408 if (tg || !is_rel) {
1409 /* thread group already specified or not expected if absolute spec */
1410 if (*sep == '/') {
1411 if (tg)
1412 memprintf(err, "redundant thread group specification '%s' for group %d", set, tg);
1413 else
1414 memprintf(err, "group-relative thread specification '%s' is not permitted after a absolute thread range.", set);
1415 return -1;
1416 }
1417 } else {
1418 /* this is a group-relative spec, first field is the group number */
1419 if (sep == set && *sep == '/') {
1420 memprintf(err, "thread group number expected before '%s'.", set);
1421 return -1;
1422 }
1423
1424 if (*sep != '/') {
1425 memprintf(err, "absolute thread specification '%s' is not permitted after a group-relative thread range.", set);
1426 return -1;
1427 }
1428
1429 if (v < 1 || v > MAX_TGROUPS) {
1430 memprintf(err, "invalid thread group number '%d', permitted range is 1..%d in '%s'.", v, MAX_TGROUPS, set);
1431 return -1;
1432 }
1433
1434 tg = v;
1435
1436 /* skip group number and go on with set,sep,v as if
1437 * there was no group number.
1438 */
1439 set = sep + 1;
1440 continue;
1441 }
1442
1443 /* Now 'set' starts at the min thread number, whose value is in v if any,
1444 * and preset the max to it, unless the range is filled at once via "all"
1445 * (stored as 1:0), "odd" (stored as) 1:-1, or "even" (stored as 1:-2).
1446 * 'sep' points to the next non-digit which may be set itself e.g. for
1447 * "all" etc or "-xx".
1448 */
1449
1450 if (!*set) {
1451 /* empty set sets no restriction */
1452 min = 1;
1453 max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS;
1454 }
1455 else {
1456 if (sep != set && *sep && *sep != '-' && *sep != ',') {
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02001457 // Only delimiters are permitted around digits.
Willy Tarreaubef43df2023-01-31 19:27:48 +01001458 memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set);
1459 return -1;
1460 }
1461
1462 /* for non-digits, find next delim */
1463 for (; *sep && *sep != '-' && *sep != ','; sep++)
1464 ;
1465
1466 min = max = 1;
1467 if (sep != set) {
1468 /* non-empty first thread */
1469 if (isteq(ist2(set, sep-set), ist("all")))
1470 max = 0;
1471 else if (isteq(ist2(set, sep-set), ist("odd")))
1472 max = -1;
1473 else if (isteq(ist2(set, sep-set), ist("even")))
1474 max = -2;
1475 else if (v)
1476 min = max = v;
1477 else
1478 max = min = 0; // throw an error below
1479 }
1480
1481 if (min < 1 || min > MAX_THREADS || (is_rel && min > MAX_THREADS_PER_GROUP)) {
1482 memprintf(err, "invalid first thread number '%s', permitted range is 1..%d, or 'all', 'odd', 'even'.",
1483 set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS);
1484 return -1;
1485 }
1486
1487 /* is this a range ? */
1488 if (*sep == '-') {
1489 if (min != max) {
1490 memprintf(err, "extraneous range after 'all', 'odd' or 'even': '%s'.", set);
1491 return -1;
1492 }
1493
1494 /* this is a seemingly valid range, there may be another number */
1495 for (set = ++sep; isdigit((uchar)*sep); sep++)
1496 ;
1497 v = atoi(set);
1498
1499 if (sep == set) { // no digit: to the max
1500 max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS;
1501 if (*sep && *sep != ',')
1502 max = 0; // throw an error below
1503 } else
1504 max = v;
1505
1506 if (max < 1 || max > MAX_THREADS || (is_rel && max > MAX_THREADS_PER_GROUP)) {
1507 memprintf(err, "invalid last thread number '%s', permitted range is 1..%d.",
1508 set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS);
1509 return -1;
1510 }
1511 }
1512
1513 /* here sep points to the first non-digit after the thread spec,
1514 * must be a valid delimiter.
1515 */
1516 if (*sep && *sep != ',') {
1517 memprintf(err, "invalid character '%c' after thread set specification: '%s'.", *sep, set);
1518 return -1;
1519 }
1520 }
1521
1522 /* store values */
1523 if (ts) {
1524 if (is_rel) {
1525 /* group-relative thread numbers */
Willy Tarreau97da9422023-03-01 11:24:29 +01001526 ts->grps |= 1UL << (tg - 1);
Willy Tarreaubef43df2023-01-31 19:27:48 +01001527
1528 if (max >= min) {
1529 for (v = min; v <= max; v++)
Willy Tarreauf91ab7a2023-02-06 18:01:50 +01001530 ts->rel[tg - 1] |= 1UL << (v - 1);
Willy Tarreaubef43df2023-01-31 19:27:48 +01001531 } else {
1532 memset(&ts->rel[tg - 1],
1533 (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */,
1534 sizeof(ts->rel[tg - 1]));
1535 }
1536 } else {
1537 /* absolute thread numbers */
1538 if (max >= min) {
1539 for (v = min; v <= max; v++)
Willy Tarreauf91ab7a2023-02-06 18:01:50 +01001540 ts->abs[(v - 1) / LONGBITS] |= 1UL << ((v - 1) % LONGBITS);
Willy Tarreaubef43df2023-01-31 19:27:48 +01001541 } else {
1542 memset(&ts->abs,
1543 (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */,
1544 sizeof(ts->abs));
1545 }
1546 }
1547 }
1548
1549 set = *sep ? sep + 1 : sep;
1550 tg = 0;
1551 }
1552 return 0;
Willy Tarreau627def92021-09-29 18:59:47 +02001553}
1554
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001555/* Parse the "nbthread" global directive, which takes an integer argument that
1556 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001557 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001558static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
1559 const struct proxy *defpx, const char *file, int line,
1560 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001561{
1562 long nbthread;
1563 char *errptr;
1564
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001565 if (too_many_args(1, args, err, NULL))
1566 return -1;
1567
Christopher Faulet55343342022-11-18 15:52:58 +01001568 if (non_global_section_parsed == 1) {
1569 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]);
1570 return -1;
1571 }
1572
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001573 nbthread = strtol(args[1], &errptr, 10);
1574 if (!*args[1] || *errptr) {
1575 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1576 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001577 }
1578
1579#ifndef USE_THREAD
1580 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001581 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]);
1582 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001583 }
1584#else
1585 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001586 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1587 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001588 }
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001589#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001590
1591 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001592 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1593 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001594
1595 global.nbthread = nbthread;
1596 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001597}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001598
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001599/* Parse the "thread-group" global directive, which takes an integer argument
1600 * that designates a thread group, and a list of threads to put into that group.
1601 */
1602static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx,
1603 const struct proxy *defpx, const char *file, int line,
1604 char **err)
1605{
1606 char *errptr;
1607 long tnum, tend, tgroup;
1608 int arg, tot;
1609
Christopher Faulet55343342022-11-18 15:52:58 +01001610 if (non_global_section_parsed == 1) {
1611 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]);
1612 return -1;
1613 }
1614
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001615 tgroup = strtol(args[1], &errptr, 10);
1616 if (!*args[1] || *errptr) {
1617 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1618 return -1;
1619 }
1620
1621 if (tgroup < 1 || tgroup > MAX_TGROUPS) {
1622 memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup);
1623 return -1;
1624 }
1625
1626 /* look for a preliminary definition of any thread pointing to this
1627 * group, and remove them.
1628 */
1629 if (ha_tgroup_info[tgroup-1].count) {
1630 ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n",
1631 file, line, args[0], tgroup);
1632
1633 for (tnum = ha_tgroup_info[tgroup-1].base;
1634 tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count;
1635 tnum++) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001636 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001637 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001638 ha_thread_info[tnum-1].tgid = 0;
Willy Tarreau03f9b352022-06-27 16:02:24 +02001639 ha_thread_info[tnum-1].tg_ctx = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001640 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001641 }
1642 ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0;
1643 }
1644
1645 tot = 0;
1646 for (arg = 2; args[arg] && *args[arg]; arg++) {
1647 tend = tnum = strtol(args[arg], &errptr, 10);
1648
1649 if (*errptr == '-')
1650 tend = strtol(errptr + 1, &errptr, 10);
1651
1652 if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) {
1653 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);
1654 return -1;
1655 }
1656
1657 for(; tnum <= tend; tnum++) {
1658 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
1659 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n",
1660 file, line, args[0], tgroup, tnum);
1661 } else if (ha_thread_info[tnum-1].tg) {
1662 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n",
1663 file, line, args[0], tgroup, tnum,
1664 (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1));
1665 }
1666
1667 if (!ha_tgroup_info[tgroup-1].count) {
1668 ha_tgroup_info[tgroup-1].base = tnum-1;
1669 ha_tgroup_info[tgroup-1].count = 1;
1670 }
1671 else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) {
1672 ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base;
1673 }
1674 else if (tnum < ha_tgroup_info[tgroup-1].base) {
1675 ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1;
1676 ha_tgroup_info[tgroup-1].base = tnum - 1;
1677 }
1678
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001679 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001680 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001681 ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1];
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001682 tot++;
1683 }
1684 }
1685
1686 if (ha_tgroup_info[tgroup-1].count > tot) {
1687 memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup);
1688 return -1;
1689 }
1690
1691 if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) {
1692 memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP);
1693 return -1;
1694 }
1695
1696 return 0;
1697}
1698
Willy Tarreauc33b9692021-09-22 12:07:23 +02001699/* Parse the "thread-groups" global directive, which takes an integer argument
1700 * that contains the desired number of thread groups.
1701 */
1702static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx,
1703 const struct proxy *defpx, const char *file, int line,
1704 char **err)
1705{
1706 long nbtgroups;
1707 char *errptr;
1708
1709 if (too_many_args(1, args, err, NULL))
1710 return -1;
Christopher Faulet55343342022-11-18 15:52:58 +01001711
1712 if (non_global_section_parsed == 1) {
1713 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]);
1714 return -1;
1715 }
Willy Tarreauc33b9692021-09-22 12:07:23 +02001716
1717 nbtgroups = strtol(args[1], &errptr, 10);
1718 if (!*args[1] || *errptr) {
1719 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1720 return -1;
1721 }
1722
1723#ifndef USE_THREAD
1724 if (nbtgroups != 1) {
1725 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]);
1726 return -1;
1727 }
1728#else
1729 if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) {
1730 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups);
1731 return -1;
1732 }
1733#endif
1734
1735 HA_DIAG_WARNING_COND(global.nbtgroups,
1736 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1737 file, line, args[0]);
1738
1739 global.nbtgroups = nbtgroups;
1740 return 0;
1741}
1742
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001743/* config keyword parsers */
1744static struct cfg_kw_list cfg_kws = {ILH, {
1745 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001746 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001747 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001748 { 0, NULL, NULL }
1749}};
1750
1751INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);