blob: 44cbc2e89e63db7716410512ad8202fa48214b0c [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
Willy Tarreau8fc70732023-05-27 13:45:01 +0200103 * front of us. For this reason we proceed in 4 steps:
104 * 1) wait for all threads to declare themselves harmless
105 * 2) try to grab the isolated_thread exclusivity
106 * 3) verify again that all threads are harmless, since another one
107 * that was isolating between 1 and 2 could have dropped its
108 * harmless state there.
109 * 4) drop harmless flag (which also has the benefit of leaving
110 * all other threads wait on reads instead of writes.
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200111 */
Willy Tarreau60b639c2018-08-02 10:16:17 +0200112 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200113 for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) {
Willy Tarreaub2f38c12023-01-19 19:14:18 +0100114 do {
115 ulong te = _HA_ATOMIC_LOAD(&ha_tgroup_info[tgrp].threads_enabled);
116 ulong th = _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless);
117
118 if ((th & te) == te)
119 break;
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200120 ha_thread_relax();
Willy Tarreaub2f38c12023-01-19 19:14:18 +0100121 } while (1);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200122 }
Willy Tarreau60b639c2018-08-02 10:16:17 +0200123
Willy Tarreau8fc70732023-05-27 13:45:01 +0200124 /* all other ones are harmless. isolated_thread will contain
125 * ~0U if no other one competes, !=tid if another one got it,
126 * tid if the current thread already grabbed it on the previous
127 * round.
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200128 */
129 thr = _HA_ATOMIC_LOAD(&isolated_thread);
Willy Tarreau8fc70732023-05-27 13:45:01 +0200130 if (thr == tid)
131 break; // we won and we're certain everyone is harmless
132
133 /* try to win the race against others */
134 if (thr != ~0U || !_HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
135 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +0200136 }
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200137
138 /* the thread is no longer harmless as it runs */
139 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
140
141 /* the thread is isolated until it calls thread_release() which will
142 * 1) reset isolated_thread to ~0;
143 * 2) decrement rdv_requests.
Willy Tarreau60b639c2018-08-02 10:16:17 +0200144 */
145}
146
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200147/* Isolates the current thread : request the ability to work while all other
148 * threads are idle, as defined by thread_idle_now(). It only returns once
149 * all of them are both harmless and idle, with the current thread's bit in
Willy Tarreau03f9b352022-06-27 16:02:24 +0200150 * &tg_ctx->threads_harmless and idle_mask cleared. Needs to be completed using
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200151 * thread_release(). By doing so the thread also engages in being safe against
152 * any actions that other threads might be about to start under the same
153 * conditions. This specifically targets destruction of any internal structure,
154 * which implies that the current thread may not hold references to any object.
155 *
156 * Note that a concurrent thread_isolate() will usually win against
157 * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to
158 * get back to the poller or any other fully idle location, that will
159 * ultimately release this one.
160 */
161void thread_isolate_full()
162{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200163 uint tgrp, thr;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200164
Willy Tarreau03f9b352022-06-27 16:02:24 +0200165 _HA_ATOMIC_OR(&tg_ctx->threads_idle, ti->ltid_bit);
166 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200167 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200168 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200169
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200170 /* wait for all threads to become harmless. They cannot change their
171 * mind once seen thanks to rdv_requests above, unless they pass in
Willy Tarreau8fc70732023-05-27 13:45:01 +0200172 * front of us. For this reason we proceed in 4 steps:
173 * 1) wait for all threads to declare themselves harmless
174 * 2) try to grab the isolated_thread exclusivity
175 * 3) verify again that all threads are harmless, since another one
176 * that was isolating between 1 and 2 could have dropped its
177 * harmless state there.
178 * 4) drop harmless flag (which also has the benefit of leaving
179 * all other threads wait on reads instead of writes.
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200180 */
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200181 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200182 for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) {
Willy Tarreaub2f38c12023-01-19 19:14:18 +0100183 do {
184 ulong te = _HA_ATOMIC_LOAD(&ha_tgroup_info[tgrp].threads_enabled);
185 ulong th = _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless);
186 ulong id = _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_idle);
187
188 if ((th & id & te) == te)
189 break;
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200190 ha_thread_relax();
Willy Tarreaub2f38c12023-01-19 19:14:18 +0100191 } while (1);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200192 }
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200193
Willy Tarreau8fc70732023-05-27 13:45:01 +0200194 /* all other ones are harmless and idle. isolated_thread will
195 * contain ~0U if no other one competes, !=tid if another one
196 * got it, tid if the current thread already grabbed it on the
197 * previous round.
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200198 */
199 thr = _HA_ATOMIC_LOAD(&isolated_thread);
Willy Tarreau8fc70732023-05-27 13:45:01 +0200200 if (thr == tid)
201 break; // we won and we're certain everyone is harmless
202
203 if (thr != ~0U || !_HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
204 ha_thread_relax();
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200205 }
206
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200207 /* we're not idle nor harmless anymore at this point. Other threads
208 * waiting on this condition will need to wait until out next pass to
209 * the poller, or our next call to thread_isolate_full().
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200210 */
Willy Tarreau03f9b352022-06-27 16:02:24 +0200211 _HA_ATOMIC_AND(&tg_ctx->threads_idle, ~ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200212 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
Willy Tarreau8fc70732023-05-27 13:45:01 +0200213
214 /* the thread is isolated until it calls thread_release() which will
215 * 1) reset isolated_thread to ~0;
216 * 2) decrement rdv_requests.
217 */
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200218}
219
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200220/* Cancels the effect of thread_isolate() by resetting the ID of the isolated
221 * thread and decrementing the number of RDV requesters. This immediately allows
222 * other threads to expect to be executed, though they will first have to wait
223 * for this thread to become harmless again (possibly by reaching the poller
224 * again).
Willy Tarreau60b639c2018-08-02 10:16:17 +0200225 */
226void thread_release()
227{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200228 HA_ATOMIC_STORE(&isolated_thread, ~0U);
229 HA_ATOMIC_DEC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200230}
Christopher Faulet339fff82017-10-19 11:59:15 +0200231
Willy Tarreaud10385a2021-10-06 22:22:40 +0200232/* Sets up threads, signals and masks, and starts threads 2 and above.
233 * Does nothing when threads are disabled.
234 */
235void setup_extra_threads(void *(*handler)(void *))
236{
237 sigset_t blocked_sig, old_sig;
238 int i;
239
240 /* ensure the signals will be blocked in every thread */
241 sigfillset(&blocked_sig);
242 sigdelset(&blocked_sig, SIGPROF);
243 sigdelset(&blocked_sig, SIGBUS);
244 sigdelset(&blocked_sig, SIGFPE);
245 sigdelset(&blocked_sig, SIGILL);
246 sigdelset(&blocked_sig, SIGSEGV);
247 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
248
249 /* Create nbthread-1 thread. The first thread is the current process */
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200250 ha_pthread[0] = pthread_self();
Willy Tarreaud10385a2021-10-06 22:22:40 +0200251 for (i = 1; i < global.nbthread; i++)
Willy Tarreau43ab05b2021-09-28 09:43:11 +0200252 pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200253}
254
255/* waits for all threads to terminate. Does nothing when threads are
256 * disabled.
257 */
258void wait_for_threads_completion()
259{
260 int i;
261
262 /* Wait the end of other threads */
263 for (i = 1; i < global.nbthread; i++)
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200264 pthread_join(ha_pthread[i], NULL);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200265
266#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
267 show_lock_stats();
268#endif
269}
270
271/* Tries to set the current thread's CPU affinity according to the cpu_map */
272void set_thread_cpu_affinity()
273{
274#if defined(USE_CPU_AFFINITY)
275 /* no affinity setting for the master process */
276 if (master)
277 return;
278
279 /* Now the CPU affinity for all threads */
Willy Tarreau5b093412022-07-08 09:38:30 +0200280 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 +0200281# if defined(__APPLE__)
282 /* Note: this API is limited to the first 32/64 CPUs */
Willy Tarreau5b093412022-07-08 09:38:30 +0200283 unsigned long set = cpu_map[tgid - 1].thread[ti->ltid].cpuset;
Willy Tarreaud10385a2021-10-06 22:22:40 +0200284 int j;
285
286 while ((j = ffsl(set)) > 0) {
287 thread_affinity_policy_data_t cpu_set = { j - 1 };
288 thread_port_t mthread;
289
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200290 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200291 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
292 set &= ~(1UL << (j - 1));
293 }
294# else
Willy Tarreau5b093412022-07-08 09:38:30 +0200295 struct hap_cpuset *set = &cpu_map[tgid - 1].thread[ti->ltid];
Willy Tarreaud10385a2021-10-06 22:22:40 +0200296
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200297 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200298# endif
299 }
300#endif /* USE_CPU_AFFINITY */
301}
302
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200303/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
304 * since POSIX took great care of not specifying its representation, making it
305 * hard to export for post-mortem analysis. For this reason we copy it into a
306 * union and will use the smallest scalar type at least as large as its size,
307 * which will keep endianness and alignment for all regular sizes. As a last
308 * resort we end up with a long long ligned to the first bytes in memory, which
309 * will be endian-dependent if pthread_t is larger than a long long (not seen
310 * yet).
311 */
312unsigned long long ha_get_pthread_id(unsigned int thr)
313{
314 union {
315 pthread_t t;
316 unsigned long long ll;
317 unsigned int i;
318 unsigned short s;
319 unsigned char c;
320 } u = { 0 };
321
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200322 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200323
324 if (sizeof(u.t) <= sizeof(u.c))
325 return u.c;
326 else if (sizeof(u.t) <= sizeof(u.s))
327 return u.s;
328 else if (sizeof(u.t) <= sizeof(u.i))
329 return u.i;
330 return u.ll;
331}
332
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200333/* send signal <sig> to thread <thr> */
334void ha_tkill(unsigned int thr, int sig)
335{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200336 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200337}
338
339/* send signal <sig> to all threads. The calling thread is signaled last in
340 * order to allow all threads to synchronize in the handler.
341 */
342void ha_tkillall(int sig)
343{
344 unsigned int thr;
345
346 for (thr = 0; thr < global.nbthread; thr++) {
Willy Tarreauf15c75a2022-07-15 08:27:56 +0200347 if (!(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit))
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200348 continue;
349 if (thr == tid)
350 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200351 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200352 }
353 raise(sig);
354}
355
Willy Tarreauaa992762021-10-06 23:33:20 +0200356void ha_thread_relax(void)
357{
358#ifdef _POSIX_PRIORITY_SCHEDULING
359 sched_yield();
360#else
361 pl_cpu_relax();
362#endif
363}
364
Willy Tarreau3d184982020-10-18 10:20:59 +0200365/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100366void ha_spin_init(HA_SPINLOCK_T *l)
367{
368 HA_SPIN_INIT(l);
369}
370
Willy Tarreau3d184982020-10-18 10:20:59 +0200371/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100372void ha_rwlock_init(HA_RWLOCK_T *l)
373{
374 HA_RWLOCK_INIT(l);
375}
376
Willy Tarreauf5b63272023-03-09 10:12:06 +0100377/* returns the number of CPUs the current process is enabled to run on,
378 * regardless of any MAX_THREADS limitation.
379 */
Willy Tarreau149ab772019-01-26 14:27:06 +0100380static int thread_cpus_enabled()
381{
382 int ret = 1;
383
384#ifdef USE_CPU_AFFINITY
385#if defined(__linux__) && defined(CPU_COUNT)
386 cpu_set_t mask;
387
388 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
389 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200390#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
391 cpuset_t cpuset;
392 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
393 sizeof(cpuset), &cpuset) == 0)
394 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000395#elif defined(__APPLE__)
396 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100397#endif
398#endif
399 ret = MAX(ret, 1);
Willy Tarreau149ab772019-01-26 14:27:06 +0100400 return ret;
401}
402
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200403/* Returns 1 if the cpu set is currently restricted for the process else 0.
404 * Currently only implemented for the Linux platform.
405 */
406int thread_cpu_mask_forced()
407{
408#if defined(__linux__)
409 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
410 return cpus_avail != thread_cpus_enabled();
411#else
412 return 0;
413#endif
414}
415
Willy Tarreau407ef892021-10-05 18:39:27 +0200416/* Below come the lock-debugging functions */
417
418#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
419
420struct lock_stat lock_stats[LOCK_LABELS];
421
422/* this is only used below */
423static const char *lock_label(enum lock_label label)
424{
425 switch (label) {
426 case TASK_RQ_LOCK: return "TASK_RQ";
427 case TASK_WQ_LOCK: return "TASK_WQ";
428 case LISTENER_LOCK: return "LISTENER";
429 case PROXY_LOCK: return "PROXY";
430 case SERVER_LOCK: return "SERVER";
431 case LBPRM_LOCK: return "LBPRM";
432 case SIGNALS_LOCK: return "SIGNALS";
433 case STK_TABLE_LOCK: return "STK_TABLE";
434 case STK_SESS_LOCK: return "STK_SESS";
435 case APPLETS_LOCK: return "APPLETS";
436 case PEER_LOCK: return "PEER";
437 case SHCTX_LOCK: return "SHCTX";
438 case SSL_LOCK: return "SSL";
439 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
440 case PATREF_LOCK: return "PATREF";
441 case PATEXP_LOCK: return "PATEXP";
442 case VARS_LOCK: return "VARS";
443 case COMP_POOL_LOCK: return "COMP_POOL";
444 case LUA_LOCK: return "LUA";
445 case NOTIF_LOCK: return "NOTIF";
446 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
447 case DNS_LOCK: return "DNS";
448 case PID_LIST_LOCK: return "PID_LIST";
449 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
450 case PIPES_LOCK: return "PIPES";
451 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
452 case AUTH_LOCK: return "AUTH";
453 case LOGSRV_LOCK: return "LOGSRV";
454 case DICT_LOCK: return "DICT";
455 case PROTO_LOCK: return "PROTO";
456 case QUEUE_LOCK: return "QUEUE";
457 case CKCH_LOCK: return "CKCH";
458 case SNI_LOCK: return "SNI";
459 case SSL_SERVER_LOCK: return "SSL_SERVER";
460 case SFT_LOCK: return "SFT";
461 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
Remi Tricot-Le Breton2b963642022-12-20 11:11:02 +0100462 case OCSP_LOCK: return "OCSP";
Amaury Denoyellee83f9372023-04-18 11:10:54 +0200463 case QC_CID_LOCK: return "QC_CID";
Willy Tarreau407ef892021-10-05 18:39:27 +0200464 case OTHER_LOCK: return "OTHER";
465 case DEBUG1_LOCK: return "DEBUG1";
466 case DEBUG2_LOCK: return "DEBUG2";
467 case DEBUG3_LOCK: return "DEBUG3";
468 case DEBUG4_LOCK: return "DEBUG4";
469 case DEBUG5_LOCK: return "DEBUG5";
470 case LOCK_LABELS: break; /* keep compiler happy */
471 };
472 /* only way to come here is consecutive to an internal bug */
473 abort();
474}
475
476void show_lock_stats()
477{
478 int lbl;
479
480 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
481 if (!lock_stats[lbl].num_write_locked &&
482 !lock_stats[lbl].num_seek_locked &&
483 !lock_stats[lbl].num_read_locked) {
484 fprintf(stderr,
485 "Stats about Lock %s: not used\n",
486 lock_label(lbl));
487 continue;
488 }
489
490 fprintf(stderr,
491 "Stats about Lock %s: \n",
492 lock_label(lbl));
493
494 if (lock_stats[lbl].num_write_locked)
495 fprintf(stderr,
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100496 "\t # write lock : %llu\n"
497 "\t # write unlock: %llu (%lld)\n"
Willy Tarreau407ef892021-10-05 18:39:27 +0200498 "\t # wait time for write : %.3f msec\n"
499 "\t # wait time for write/lock: %.3f nsec\n",
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100500 (ullong)lock_stats[lbl].num_write_locked,
501 (ullong)lock_stats[lbl].num_write_unlocked,
502 (llong)(lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked),
Willy Tarreau407ef892021-10-05 18:39:27 +0200503 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
504 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
505
506 if (lock_stats[lbl].num_seek_locked)
507 fprintf(stderr,
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100508 "\t # seek lock : %llu\n"
509 "\t # seek unlock : %llu (%lld)\n"
Willy Tarreau407ef892021-10-05 18:39:27 +0200510 "\t # wait time for seek : %.3f msec\n"
511 "\t # wait time for seek/lock : %.3f nsec\n",
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100512 (ullong)lock_stats[lbl].num_seek_locked,
513 (ullong)lock_stats[lbl].num_seek_unlocked,
514 (llong)(lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked),
Willy Tarreau407ef892021-10-05 18:39:27 +0200515 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
516 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
517
518 if (lock_stats[lbl].num_read_locked)
519 fprintf(stderr,
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100520 "\t # read lock : %llu\n"
521 "\t # read unlock : %llu (%lld)\n"
Willy Tarreau407ef892021-10-05 18:39:27 +0200522 "\t # wait time for read : %.3f msec\n"
523 "\t # wait time for read/lock : %.3f nsec\n",
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100524 (ullong)lock_stats[lbl].num_read_locked,
525 (ullong)lock_stats[lbl].num_read_unlocked,
526 (llong)(lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked),
Willy Tarreau407ef892021-10-05 18:39:27 +0200527 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
528 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
529 }
530}
531
Willy Tarreau407ef892021-10-05 18:39:27 +0200532void __ha_rwlock_init(struct ha_rwlock *l)
533{
534 memset(l, 0, sizeof(struct ha_rwlock));
535 __RWLOCK_INIT(&l->lock);
536}
537
538void __ha_rwlock_destroy(struct ha_rwlock *l)
539{
540 __RWLOCK_DESTROY(&l->lock);
541 memset(l, 0, sizeof(struct ha_rwlock));
542}
543
544
545void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
546 const char *func, const char *file, int line)
547{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200548 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
549 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200550 uint64_t start_time;
551
Willy Tarreau7aa41192022-07-15 17:53:10 +0200552 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200553 abort();
554
Willy Tarreau7aa41192022-07-15 17:53:10 +0200555 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200556
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200557 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200558 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200559 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200560
561 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
562
Willy Tarreau7aa41192022-07-15 17:53:10 +0200563 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200564 l->info.last_location.function = func;
565 l->info.last_location.file = file;
566 l->info.last_location.line = line;
567
Willy Tarreau7aa41192022-07-15 17:53:10 +0200568 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200569}
570
571int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
572 const char *func, const char *file, int line)
573{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200574 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
575 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200576 uint64_t start_time;
577 int r;
578
Willy Tarreau7aa41192022-07-15 17:53:10 +0200579 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200580 abort();
581
582 /* We set waiting writer because trywrlock could wait for readers to quit */
Willy Tarreau7aa41192022-07-15 17:53:10 +0200583 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200584
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200585 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200586 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200587 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200588 if (unlikely(r)) {
Willy Tarreau7aa41192022-07-15 17:53:10 +0200589 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200590 return r;
591 }
592 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
593
Willy Tarreau7aa41192022-07-15 17:53:10 +0200594 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200595 l->info.last_location.function = func;
596 l->info.last_location.file = file;
597 l->info.last_location.line = line;
598
Willy Tarreau7aa41192022-07-15 17:53:10 +0200599 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200600
601 return 0;
602}
603
604void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
605 const char *func, const char *file, int line)
606{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200607 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
608 struct ha_rwlock_state *st = &l->info.st[tgid-1];
609
610 if (unlikely(!(st->cur_writer & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200611 /* the thread is not owning the lock for write */
612 abort();
613 }
614
Willy Tarreau7aa41192022-07-15 17:53:10 +0200615 st->cur_writer = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200616 l->info.last_location.function = func;
617 l->info.last_location.file = file;
618 l->info.last_location.line = line;
619
620 __RWLOCK_WRUNLOCK(&l->lock);
621
622 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
623}
624
625void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
626{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200627 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
628 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200629 uint64_t start_time;
630
Willy Tarreau7aa41192022-07-15 17:53:10 +0200631 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200632 abort();
633
Willy Tarreau7aa41192022-07-15 17:53:10 +0200634 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200635
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200636 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200637 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200638 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200639 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
640
Willy Tarreau7aa41192022-07-15 17:53:10 +0200641 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200642
Willy Tarreau7aa41192022-07-15 17:53:10 +0200643 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200644}
645
646int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
647{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200648 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
649 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200650 int r;
651
Willy Tarreau7aa41192022-07-15 17:53:10 +0200652 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200653 abort();
654
655 /* try read should never wait */
656 r = __RWLOCK_TRYRDLOCK(&l->lock);
657 if (unlikely(r))
658 return r;
659 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
660
Willy Tarreau7aa41192022-07-15 17:53:10 +0200661 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200662
663 return 0;
664}
665
666void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
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];
670
671 if (unlikely(!(st->cur_readers & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200672 /* the thread is not owning the lock for read */
673 abort();
674 }
675
Willy Tarreau7aa41192022-07-15 17:53:10 +0200676 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200677
678 __RWLOCK_RDUNLOCK(&l->lock);
679
680 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
681}
682
683void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
684 const char *func, const char *file, int line)
685{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200686 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
687 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200688 uint64_t start_time;
689
Willy Tarreau7aa41192022-07-15 17:53:10 +0200690 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200691 abort();
692
Willy Tarreau7aa41192022-07-15 17:53:10 +0200693 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200694 abort();
695
Willy Tarreau7aa41192022-07-15 17:53:10 +0200696 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200697
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200698 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200699 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200700 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200701
702 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
703
Willy Tarreau7aa41192022-07-15 17:53:10 +0200704 HA_ATOMIC_OR(&st->cur_readers, tbit);
705 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200706 l->info.last_location.function = func;
707 l->info.last_location.file = file;
708 l->info.last_location.line = line;
709
Willy Tarreau7aa41192022-07-15 17:53:10 +0200710 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200711}
712
713void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
714 const char *func, const char *file, int line)
715{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200716 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
717 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200718 uint64_t start_time;
719
Willy Tarreau7aa41192022-07-15 17:53:10 +0200720 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200721 abort();
722
Willy Tarreau7aa41192022-07-15 17:53:10 +0200723 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200724 abort();
725
Willy Tarreau7aa41192022-07-15 17:53:10 +0200726 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200727
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200728 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200729 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200730 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200731
732 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
733
Willy Tarreau7aa41192022-07-15 17:53:10 +0200734 HA_ATOMIC_OR(&st->cur_seeker, tbit);
735 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200736 l->info.last_location.function = func;
737 l->info.last_location.file = file;
738 l->info.last_location.line = line;
739
Willy Tarreau7aa41192022-07-15 17:53:10 +0200740 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200741}
742
743void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
744 const char *func, const char *file, int line)
745{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200746 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
747 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200748 uint64_t start_time;
749
Willy Tarreau7aa41192022-07-15 17:53:10 +0200750 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200751 abort();
752
Willy Tarreau7aa41192022-07-15 17:53:10 +0200753 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200754
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200755 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200756 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200757 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200758
759 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
760
Willy Tarreau7aa41192022-07-15 17:53:10 +0200761 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200762 l->info.last_location.function = func;
763 l->info.last_location.file = file;
764 l->info.last_location.line = line;
765
Willy Tarreau7aa41192022-07-15 17:53:10 +0200766 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200767}
768
769void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
770 const char *func, const char *file, int line)
771{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200772 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
773 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200774 uint64_t start_time;
775
Willy Tarreau7aa41192022-07-15 17:53:10 +0200776 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200777 abort();
778
Willy Tarreau7aa41192022-07-15 17:53:10 +0200779 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200780 abort();
781
Willy Tarreau7aa41192022-07-15 17:53:10 +0200782 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200783
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200784 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200785 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200786 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200787
788 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
789
Willy Tarreau7aa41192022-07-15 17:53:10 +0200790 HA_ATOMIC_OR(&st->cur_writer, tbit);
791 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200792 l->info.last_location.function = func;
793 l->info.last_location.file = file;
794 l->info.last_location.line = line;
795
Willy Tarreau7aa41192022-07-15 17:53:10 +0200796 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200797}
798
799void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
800 const char *func, const char *file, int line)
801{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200802 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
803 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200804 uint64_t start_time;
805
Willy Tarreau7aa41192022-07-15 17:53:10 +0200806 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200807 abort();
808
Willy Tarreau7aa41192022-07-15 17:53:10 +0200809 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200810 abort();
811
Willy Tarreau7aa41192022-07-15 17:53:10 +0200812 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200813
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200814 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200815 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200816 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200817
818 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
819
Willy Tarreau7aa41192022-07-15 17:53:10 +0200820 HA_ATOMIC_OR(&st->cur_readers, tbit);
821 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200822 l->info.last_location.function = func;
823 l->info.last_location.file = file;
824 l->info.last_location.line = line;
825
Willy Tarreau7aa41192022-07-15 17:53:10 +0200826 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200827}
828
829void __ha_rwlock_skunlock(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];
834 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200835 abort();
836
Willy Tarreau7aa41192022-07-15 17:53:10 +0200837 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200838 l->info.last_location.function = func;
839 l->info.last_location.file = file;
840 l->info.last_location.line = line;
841
842 __RWLOCK_SKUNLOCK(&l->lock);
843
844 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
845}
846
847int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
848 const char *func, const char *file, int line)
849{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200850 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
851 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200852 uint64_t start_time;
853 int r;
854
Willy Tarreau7aa41192022-07-15 17:53:10 +0200855 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200856 abort();
857
Willy Tarreau7aa41192022-07-15 17:53:10 +0200858 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200859
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200860 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200861 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200862 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200863
864 if (likely(!r)) {
865 /* got the lock ! */
866 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200867 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200868 l->info.last_location.function = func;
869 l->info.last_location.file = file;
870 l->info.last_location.line = line;
871 }
872
Willy Tarreau7aa41192022-07-15 17:53:10 +0200873 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200874 return r;
875}
876
877int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
878 const char *func, const char *file, int line)
879{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200880 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
881 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200882 uint64_t start_time;
883 int r;
884
Willy Tarreau7aa41192022-07-15 17:53:10 +0200885 if ((st->cur_writer | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200886 abort();
887
Willy Tarreau7aa41192022-07-15 17:53:10 +0200888 if (!(st->cur_readers & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200889 abort();
890
Willy Tarreau7aa41192022-07-15 17:53:10 +0200891 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200892
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200893 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200894 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200895 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200896
897 if (likely(!r)) {
898 /* got the lock ! */
899 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200900 HA_ATOMIC_OR(&st->cur_seeker, tbit);
901 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200902 l->info.last_location.function = func;
903 l->info.last_location.file = file;
904 l->info.last_location.line = line;
905 }
906
Willy Tarreau7aa41192022-07-15 17:53:10 +0200907 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200908 return r;
909}
910
911void __spin_init(struct ha_spinlock *l)
912{
913 memset(l, 0, sizeof(struct ha_spinlock));
914 __SPIN_INIT(&l->lock);
915}
916
917void __spin_destroy(struct ha_spinlock *l)
918{
919 __SPIN_DESTROY(&l->lock);
920 memset(l, 0, sizeof(struct ha_spinlock));
921}
922
923void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
924 const char *func, const char *file, int line)
925{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200926 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
927 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200928 uint64_t start_time;
929
Willy Tarreau7aa41192022-07-15 17:53:10 +0200930 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200931 /* the thread is already owning the lock */
932 abort();
933 }
934
Willy Tarreau7aa41192022-07-15 17:53:10 +0200935 HA_ATOMIC_OR(&st->waiters, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200936
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200937 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200938 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200939 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200940
941 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
942
943
Willy Tarreau7aa41192022-07-15 17:53:10 +0200944 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200945 l->info.last_location.function = func;
946 l->info.last_location.file = file;
947 l->info.last_location.line = line;
948
Willy Tarreau7aa41192022-07-15 17:53:10 +0200949 HA_ATOMIC_AND(&st->waiters, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200950}
951
952int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
953 const char *func, const char *file, int line)
954{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200955 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
956 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200957 int r;
958
Willy Tarreau7aa41192022-07-15 17:53:10 +0200959 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200960 /* the thread is already owning the lock */
961 abort();
962 }
963
964 /* try read should never wait */
965 r = __SPIN_TRYLOCK(&l->lock);
966 if (unlikely(r))
967 return r;
968 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
969
Willy Tarreau7aa41192022-07-15 17:53:10 +0200970 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200971 l->info.last_location.function = func;
972 l->info.last_location.file = file;
973 l->info.last_location.line = line;
974
975 return 0;
976}
977
978void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
979 const char *func, const char *file, int line)
980{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200981 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
982 struct ha_spinlock_state *st = &l->info.st[tgid-1];
983
984 if (unlikely(!(st->owner & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200985 /* the thread is not owning the lock */
986 abort();
987 }
988
Willy Tarreau7aa41192022-07-15 17:53:10 +0200989 st->owner = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200990 l->info.last_location.function = func;
991 l->info.last_location.file = file;
992 l->info.last_location.line = line;
993
994 __SPIN_UNLOCK(&l->lock);
995 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
996}
997
998#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
999
Willy Tarreau87aff022022-07-10 10:58:57 +02001000
1001#if defined(USE_PTHREAD_EMULATION)
1002
1003/* pthread rwlock emulation using plocks (to avoid expensive futexes).
1004 * these are a direct mapping on Progressive Locks, with the exception that
1005 * since there's a common unlock operation in pthreads, we need to know if
1006 * we need to unlock for reads or writes, so we set the topmost bit to 1 when
1007 * a write lock is acquired to indicate that a write unlock needs to be
1008 * performed. It's not a problem since this bit will never be used given that
1009 * haproxy won't support as many threads as the plocks.
1010 *
1011 * The storage is the pthread_rwlock_t cast as an ulong
1012 */
1013
1014int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr)
1015{
1016 ulong *lock = (ulong *)rwlock;
1017
1018 *lock = 0;
1019 return 0;
1020}
1021
1022int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
1023{
1024 ulong *lock = (ulong *)rwlock;
1025
1026 *lock = 0;
1027 return 0;
1028}
1029
1030int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
1031{
1032 pl_lorw_rdlock((unsigned long *)rwlock);
1033 return 0;
1034}
1035
1036int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
1037{
1038 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_SHR_BASE);
1039}
1040
1041int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1042{
1043 return pthread_rwlock_tryrdlock(rwlock);
1044}
1045
1046int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
1047{
1048 pl_lorw_wrlock((unsigned long *)rwlock);
1049 return 0;
1050}
1051
1052int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
1053{
1054 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_EXC_BASE);
1055}
1056
1057int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1058{
1059 return pthread_rwlock_trywrlock(rwlock);
1060}
1061
1062int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
1063{
1064 pl_lorw_unlock((unsigned long *)rwlock);
1065 return 0;
1066}
1067#endif // defined(USE_PTHREAD_EMULATION)
1068
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001069/* Depending on the platform and how libpthread was built, pthread_exit() may
1070 * involve some code in libgcc_s that would be loaded on exit for the first
1071 * time, causing aborts if the process is chrooted. It's harmless bit very
1072 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
1073 * needed, so what we do here is that during early boot we create a dummy
1074 * thread that immediately exits. This will lead to libgcc_s being loaded
1075 * during boot on the platforms where it's required.
1076 */
1077static void *dummy_thread_function(void *data)
1078{
1079 pthread_exit(NULL);
1080 return NULL;
1081}
1082
1083static inline void preload_libgcc_s(void)
1084{
1085 pthread_t dummy_thread;
eaglegaief667b12023-05-26 16:44:34 +08001086 if (pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL) == 0)
1087 pthread_join(dummy_thread, NULL);
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001088}
1089
Willy Tarreau3f567e42020-05-28 15:29:19 +02001090static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001091{
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001092 char *ptr = NULL;
1093
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001094 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +02001095
Willy Tarreau149ab772019-01-26 14:27:06 +01001096 thread_cpus_enabled_at_boot = thread_cpus_enabled();
Willy Tarreauf5b63272023-03-09 10:12:06 +01001097 thread_cpus_enabled_at_boot = MIN(thread_cpus_enabled_at_boot, MAX_THREADS);
Willy Tarreau149ab772019-01-26 14:27:06 +01001098
Willy Tarreauc80bdb22022-08-06 16:44:55 +02001099 memprintf(&ptr, "Built with multi-threading support (MAX_TGROUPS=%d, MAX_THREADS=%d, default=%d).",
1100 MAX_TGROUPS, MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001101 hap_register_build_opts(ptr, 1);
1102
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001103#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
1104 memset(lock_stats, 0, sizeof(lock_stats));
1105#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001106}
Willy Tarreau8ead1d02022-04-25 19:23:17 +02001107INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001108
Willy Tarreau8459f252018-12-15 16:48:14 +01001109#else
1110
Willy Tarreauaa992762021-10-06 23:33:20 +02001111/* send signal <sig> to thread <thr> (send to process in fact) */
1112void ha_tkill(unsigned int thr, int sig)
1113{
1114 raise(sig);
1115}
1116
1117/* send signal <sig> to all threads (send to process in fact) */
1118void ha_tkillall(int sig)
1119{
1120 raise(sig);
1121}
1122
1123void ha_thread_relax(void)
1124{
1125#ifdef _POSIX_PRIORITY_SCHEDULING
1126 sched_yield();
1127#endif
1128}
1129
Willy Tarreau8459f252018-12-15 16:48:14 +01001130REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
1131
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001132#endif // USE_THREAD
1133
1134
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001135/* scans the configured thread mapping and establishes the final one. Returns <0
1136 * on failure, >=0 on success.
1137 */
1138int thread_map_to_groups()
1139{
1140 int t, g, ut, ug;
1141 int q, r;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001142 ulong m __maybe_unused;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001143
1144 ut = ug = 0; // unassigned threads & groups
1145
1146 for (t = 0; t < global.nbthread; t++) {
1147 if (!ha_thread_info[t].tg)
1148 ut++;
1149 }
1150
1151 for (g = 0; g < global.nbtgroups; g++) {
1152 if (!ha_tgroup_info[g].count)
1153 ug++;
Willy Tarreau60fe4a92022-06-28 17:48:07 +02001154 ha_tgroup_info[g].tgid_bit = 1UL << g;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001155 }
1156
1157 if (ug > ut) {
1158 ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut);
1159 return -1;
1160 }
1161
1162 /* look for first unassigned thread */
1163 for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++)
1164 ;
1165
1166 /* assign threads to empty groups */
1167 for (g = 0; ug && ut; ) {
1168 /* due to sparse thread assignment we can end up with more threads
1169 * per group on last assigned groups than former ones, so we must
1170 * always try to pack the maximum remaining ones together first.
1171 */
1172 q = ut / ug;
1173 r = ut % ug;
1174 if ((q + !!r) > MAX_THREADS_PER_GROUP) {
Willy Tarreaucf0d0ee2023-03-09 11:39:51 +01001175 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 +02001176 return -1;
1177 }
1178
1179 /* thread <t> is the next unassigned one. Let's look for next
1180 * unassigned group, we know there are some left
1181 */
1182 while (ut >= ug && ha_tgroup_info[g].count)
1183 g++;
1184
1185 /* group g is unassigned, try to fill it with consecutive threads */
1186 while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r &&
1187 (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) {
1188
1189 if (!ha_tgroup_info[g].count) {
1190 /* assign new group */
1191 ha_tgroup_info[g].base = t;
1192 ug--;
1193 }
1194
1195 ha_tgroup_info[g].count++;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001196 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001197 ha_thread_info[t].tg = &ha_tgroup_info[g];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001198 ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g];
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001199
1200 ut--;
1201 /* switch to next unassigned thread */
1202 while (++t < global.nbthread && ha_thread_info[t].tg)
1203 ;
1204 }
1205 }
1206
1207 if (ut) {
1208 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);
1209 return -1;
1210 }
1211
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001212 for (t = 0; t < global.nbthread; t++) {
1213 ha_thread_info[t].tid = t;
1214 ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base;
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001215 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1216 }
1217
Willy Tarreaucce203a2022-06-24 15:55:11 +02001218 m = 0;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001219 for (g = 0; g < global.nbtgroups; g++) {
1220 ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count);
Aurelien DARRAGON739281b2023-01-27 15:13:28 +01001221 /* for now, additional threads are not started, so we should
1222 * consider them as harmless and idle.
1223 * This will get automatically updated when such threads are
1224 * started in run_thread_poll_loop()
1225 * Without this, thread_isolate() and thread_isolate_full()
1226 * will fail to work as long as secondary threads did not enter
1227 * the polling loop at least once.
1228 */
1229 ha_tgroup_ctx[g].threads_harmless = ha_tgroup_info[g].threads_enabled;
1230 ha_tgroup_ctx[g].threads_idle = ha_tgroup_info[g].threads_enabled;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001231 if (!ha_tgroup_info[g].count)
1232 continue;
1233 m |= 1UL << g;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001234
1235 }
1236
Willy Tarreaucce203a2022-06-24 15:55:11 +02001237#ifdef USE_THREAD
1238 all_tgroups_mask = m;
1239#endif
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001240 return 0;
1241}
1242
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001243/* Converts a configuration thread set based on either absolute or relative
1244 * thread numbers into a global group+mask. This is essentially for use with
1245 * the "thread" directive on "bind" lines, where "thread 4-6,10-12" might be
1246 * turned to "2/1-3,4/1-3". It cannot be used before the thread mapping above
1247 * was completed and the thread group numbers configured. The thread_set is
1248 * replaced by the resolved group-based one. It is possible to force a single
1249 * default group for unspecified sets instead of enabling all groups by passing
Willy Tarreauf2988e12023-02-02 17:01:10 +01001250 * this group's non-zero value to defgrp.
Willy Tarreau627def92021-09-29 18:59:47 +02001251 *
1252 * Returns <0 on failure, >=0 on success.
1253 */
Willy Tarreauf2988e12023-02-02 17:01:10 +01001254int thread_resolve_group_mask(struct thread_set *ts, int defgrp, char **err)
Willy Tarreau627def92021-09-29 18:59:47 +02001255{
Willy Tarreau15c84282023-02-04 10:49:01 +01001256 struct thread_set new_ts = { };
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001257 ulong mask, imask;
1258 uint g;
Willy Tarreau627def92021-09-29 18:59:47 +02001259
Willy Tarreau97da9422023-03-01 11:24:29 +01001260 if (!ts->grps) {
Willy Tarreau627def92021-09-29 18:59:47 +02001261 /* unspecified group, IDs are global */
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001262 if (thread_set_is_empty(ts)) {
1263 /* all threads of all groups, unless defgrp is set and
1264 * we then set it as the only group.
1265 */
1266 for (g = defgrp ? defgrp-1 : 0; g < (defgrp ? defgrp : global.nbtgroups); g++) {
1267 new_ts.rel[g] = ha_tgroup_info[g].threads_enabled;
Willy Tarreau97da9422023-03-01 11:24:29 +01001268 if (new_ts.rel[g])
1269 new_ts.grps |= 1UL << g;
Willy Tarreau627def92021-09-29 18:59:47 +02001270 }
Willy Tarreau627def92021-09-29 18:59:47 +02001271 } else {
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001272 /* some absolute threads are set, we must remap them to
1273 * relative ones. Each group cannot have more than
1274 * LONGBITS threads, thus it spans at most two absolute
1275 * blocks.
1276 */
1277 for (g = 0; g < global.nbtgroups; g++) {
1278 uint block = ha_tgroup_info[g].base / LONGBITS;
1279 uint base = ha_tgroup_info[g].base % LONGBITS;
Willy Tarreau627def92021-09-29 18:59:47 +02001280
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001281 mask = ts->abs[block] >> base;
Willy Tarreau1b536a12023-03-22 10:28:50 +01001282 if (base &&
1283 (block + 1) < sizeof(ts->abs) / sizeof(ts->abs[0]) &&
1284 ha_tgroup_info[g].count > (LONGBITS - base))
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001285 mask |= ts->abs[block + 1] << (LONGBITS - base);
1286 mask &= nbits(ha_tgroup_info[g].count);
1287 mask &= ha_tgroup_info[g].threads_enabled;
Willy Tarreau627def92021-09-29 18:59:47 +02001288
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001289 /* now the mask exactly matches the threads to be enabled
1290 * in this group.
1291 */
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001292 new_ts.rel[g] |= mask;
Willy Tarreau97da9422023-03-01 11:24:29 +01001293 if (new_ts.rel[g])
1294 new_ts.grps |= 1UL << g;
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001295 }
Willy Tarreau627def92021-09-29 18:59:47 +02001296 }
1297 } else {
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001298 /* groups were specified */
1299 for (g = 0; g < MAX_TGROUPS; g++) {
1300 imask = ts->rel[g];
1301 if (!imask)
1302 continue;
Willy Tarreau627def92021-09-29 18:59:47 +02001303
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001304 if (g >= global.nbtgroups) {
1305 memprintf(err, "'thread' directive references non-existing thread group %u", g+1);
1306 return -1;
1307 }
Willy Tarreau627def92021-09-29 18:59:47 +02001308
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001309 /* some relative threads are set. Keep only existing ones for this group */
1310 mask = nbits(ha_tgroup_info[g].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001311
1312 if (!(mask & imask)) {
1313 /* no intersection between the thread group's
1314 * threads and the bind line's.
1315 */
1316#ifdef THREAD_AUTO_ADJUST_GROUPS
1317 unsigned long new_mask = 0;
1318
1319 while (imask) {
1320 new_mask |= imask & mask;
Willy Tarreaubef43df2023-01-31 19:27:48 +01001321 imask >>= ha_tgroup_info[g].count;
Willy Tarreau627def92021-09-29 18:59:47 +02001322 }
1323 imask = new_mask;
1324#else
Willy Tarreaubef43df2023-01-31 19:27:48 +01001325 memprintf(err, "'thread' directive only references threads not belonging to group %u", g+1);
Willy Tarreau627def92021-09-29 18:59:47 +02001326 return -1;
1327#endif
1328 }
1329
Willy Tarreaubef43df2023-01-31 19:27:48 +01001330 new_ts.rel[g] = imask & mask;
Willy Tarreau97da9422023-03-01 11:24:29 +01001331 if (new_ts.rel[g])
1332 new_ts.grps |= 1UL << g;
Willy Tarreau627def92021-09-29 18:59:47 +02001333 }
1334 }
Willy Tarreaubef43df2023-01-31 19:27:48 +01001335
1336 /* update the thread_set */
Willy Tarreau7b8aac42023-02-27 11:27:38 +01001337 if (!thread_set_nth_group(&new_ts, 0)) {
Willy Tarreaubef43df2023-01-31 19:27:48 +01001338 memprintf(err, "'thread' directive only references non-existing threads");
1339 return -1;
1340 }
1341
1342 *ts = new_ts;
Willy Tarreaubef43df2023-01-31 19:27:48 +01001343 return 0;
1344}
1345
1346/* Parse a string representing a thread set in one of the following forms:
1347 *
1348 * - { "all" | "odd" | "even" | <abs_num> [ "-" <abs_num> ] }[,...]
1349 * => these are (lists of) absolute thread numbers
1350 *
1351 * - <tgnum> "/" { "all" | "odd" | "even" | <rel_num> [ "-" <rel_num> ][,...]
1352 * => these are (lists of) per-group relative thread numbers. All numbers
1353 * must be lower than or equal to LONGBITS. When multiple list elements
1354 * are provided, each of them must contain the thread group number.
1355 *
1356 * Minimum value for a thread or group number is always 1. Maximum value for an
1357 * absolute thread number is MAX_THREADS, maximum value for a relative thread
1358 * number is MAX_THREADS_PER_GROUP, an maximum value for a thread group is
1359 * MAX_TGROUPS. "all", "even" and "odd" will be bound by MAX_THREADS and/or
1360 * MAX_THREADS_PER_GROUP in any case. In ranges, a missing digit before "-"
1361 * is implicitly 1, and a missing digit after "-" is implicitly the highest of
1362 * its class. As such "-" is equivalent to "all", allowing to build strings
1363 * such as "${MIN}-${MAX}" where both MIN and MAX are optional.
1364 *
1365 * It is not valid to mix absolute and relative numbers. As such:
1366 * - all valid (all absolute threads)
1367 * - 12-19,24-31 valid (abs threads 12 to 19 and 24 to 31)
1368 * - 1/all valid (all 32 or 64 threads of group 1)
1369 * - 1/1-4,1/8-10,2/1 valid
1370 * - 1/1-4,8-10 invalid (mixes relatve "1/1-4" with absolute "8-10")
1371 * - 1-4,8-10,2/1 invalid (mixes absolute "1-4,8-10" with relative "2/1")
1372 * - 1/odd-4 invalid (mixes range with boundary)
1373 *
1374 * The target thread set is *completed* with supported threads, which means
1375 * that it's the caller's responsibility for pre-initializing it. If the target
1376 * thread set is NULL, it's not updated and the function only verifies that the
1377 * input parses.
1378 *
1379 * On success, it returns 0. otherwise it returns non-zero with an error
1380 * message in <err>.
1381 */
1382int parse_thread_set(const char *arg, struct thread_set *ts, char **err)
1383{
1384 const char *set;
1385 const char *sep;
1386 int v, min, max, tg;
1387 int is_rel;
1388
1389 /* search for the first delimiter (',', '-' or '/') to decide whether
1390 * we're facing an absolute or relative form. The relative form always
1391 * starts with a number followed by a slash.
1392 */
1393 for (sep = arg; isdigit((uchar)*sep); sep++)
1394 ;
1395
1396 is_rel = (/*sep > arg &&*/ *sep == '/'); /* relative form */
1397
1398 /* from there we have to cut the thread spec around commas */
1399
1400 set = arg;
1401 tg = 0;
1402 while (*set) {
1403 /* note: we can't use strtol() here because "-3" would parse as
1404 * (-3) while we want to stop before the "-", so we find the
1405 * separator ourselves and rely on atoi() whose value we may
1406 * ignore depending where the separator is.
1407 */
1408 for (sep = set; isdigit((uchar)*sep); sep++)
1409 ;
1410
1411 if (sep != set && *sep && *sep != '/' && *sep != '-' && *sep != ',') {
1412 memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set);
1413 return -1;
1414 }
1415
1416 v = (sep != set) ? atoi(set) : 0;
1417
1418 /* Now we know that the string is made of an optional series of digits
1419 * optionally followed by one of the delimiters above, or that it
1420 * starts with a different character.
1421 */
1422
1423 /* first, let's search for the thread group (digits before '/') */
1424
1425 if (tg || !is_rel) {
1426 /* thread group already specified or not expected if absolute spec */
1427 if (*sep == '/') {
1428 if (tg)
1429 memprintf(err, "redundant thread group specification '%s' for group %d", set, tg);
1430 else
1431 memprintf(err, "group-relative thread specification '%s' is not permitted after a absolute thread range.", set);
1432 return -1;
1433 }
1434 } else {
1435 /* this is a group-relative spec, first field is the group number */
1436 if (sep == set && *sep == '/') {
1437 memprintf(err, "thread group number expected before '%s'.", set);
1438 return -1;
1439 }
1440
1441 if (*sep != '/') {
1442 memprintf(err, "absolute thread specification '%s' is not permitted after a group-relative thread range.", set);
1443 return -1;
1444 }
1445
1446 if (v < 1 || v > MAX_TGROUPS) {
1447 memprintf(err, "invalid thread group number '%d', permitted range is 1..%d in '%s'.", v, MAX_TGROUPS, set);
1448 return -1;
1449 }
1450
1451 tg = v;
1452
1453 /* skip group number and go on with set,sep,v as if
1454 * there was no group number.
1455 */
1456 set = sep + 1;
1457 continue;
1458 }
1459
1460 /* Now 'set' starts at the min thread number, whose value is in v if any,
1461 * and preset the max to it, unless the range is filled at once via "all"
1462 * (stored as 1:0), "odd" (stored as) 1:-1, or "even" (stored as 1:-2).
1463 * 'sep' points to the next non-digit which may be set itself e.g. for
1464 * "all" etc or "-xx".
1465 */
1466
1467 if (!*set) {
1468 /* empty set sets no restriction */
1469 min = 1;
1470 max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS;
1471 }
1472 else {
1473 if (sep != set && *sep && *sep != '-' && *sep != ',') {
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02001474 // Only delimiters are permitted around digits.
Willy Tarreaubef43df2023-01-31 19:27:48 +01001475 memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set);
1476 return -1;
1477 }
1478
1479 /* for non-digits, find next delim */
1480 for (; *sep && *sep != '-' && *sep != ','; sep++)
1481 ;
1482
1483 min = max = 1;
1484 if (sep != set) {
1485 /* non-empty first thread */
1486 if (isteq(ist2(set, sep-set), ist("all")))
1487 max = 0;
1488 else if (isteq(ist2(set, sep-set), ist("odd")))
1489 max = -1;
1490 else if (isteq(ist2(set, sep-set), ist("even")))
1491 max = -2;
1492 else if (v)
1493 min = max = v;
1494 else
1495 max = min = 0; // throw an error below
1496 }
1497
1498 if (min < 1 || min > MAX_THREADS || (is_rel && min > MAX_THREADS_PER_GROUP)) {
1499 memprintf(err, "invalid first thread number '%s', permitted range is 1..%d, or 'all', 'odd', 'even'.",
1500 set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS);
1501 return -1;
1502 }
1503
1504 /* is this a range ? */
1505 if (*sep == '-') {
1506 if (min != max) {
1507 memprintf(err, "extraneous range after 'all', 'odd' or 'even': '%s'.", set);
1508 return -1;
1509 }
1510
1511 /* this is a seemingly valid range, there may be another number */
1512 for (set = ++sep; isdigit((uchar)*sep); sep++)
1513 ;
1514 v = atoi(set);
1515
1516 if (sep == set) { // no digit: to the max
1517 max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS;
1518 if (*sep && *sep != ',')
1519 max = 0; // throw an error below
1520 } else
1521 max = v;
1522
1523 if (max < 1 || max > MAX_THREADS || (is_rel && max > MAX_THREADS_PER_GROUP)) {
1524 memprintf(err, "invalid last thread number '%s', permitted range is 1..%d.",
1525 set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS);
1526 return -1;
1527 }
1528 }
1529
1530 /* here sep points to the first non-digit after the thread spec,
1531 * must be a valid delimiter.
1532 */
1533 if (*sep && *sep != ',') {
1534 memprintf(err, "invalid character '%c' after thread set specification: '%s'.", *sep, set);
1535 return -1;
1536 }
1537 }
1538
1539 /* store values */
1540 if (ts) {
1541 if (is_rel) {
1542 /* group-relative thread numbers */
Willy Tarreau97da9422023-03-01 11:24:29 +01001543 ts->grps |= 1UL << (tg - 1);
Willy Tarreaubef43df2023-01-31 19:27:48 +01001544
1545 if (max >= min) {
1546 for (v = min; v <= max; v++)
Willy Tarreauf91ab7a2023-02-06 18:01:50 +01001547 ts->rel[tg - 1] |= 1UL << (v - 1);
Willy Tarreaubef43df2023-01-31 19:27:48 +01001548 } else {
1549 memset(&ts->rel[tg - 1],
1550 (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */,
1551 sizeof(ts->rel[tg - 1]));
1552 }
1553 } else {
1554 /* absolute thread numbers */
1555 if (max >= min) {
1556 for (v = min; v <= max; v++)
Willy Tarreauf91ab7a2023-02-06 18:01:50 +01001557 ts->abs[(v - 1) / LONGBITS] |= 1UL << ((v - 1) % LONGBITS);
Willy Tarreaubef43df2023-01-31 19:27:48 +01001558 } else {
1559 memset(&ts->abs,
1560 (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */,
1561 sizeof(ts->abs));
1562 }
1563 }
1564 }
1565
1566 set = *sep ? sep + 1 : sep;
1567 tg = 0;
1568 }
1569 return 0;
Willy Tarreau627def92021-09-29 18:59:47 +02001570}
1571
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001572/* Parse the "nbthread" global directive, which takes an integer argument that
1573 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001574 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001575static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
1576 const struct proxy *defpx, const char *file, int line,
1577 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001578{
1579 long nbthread;
1580 char *errptr;
1581
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001582 if (too_many_args(1, args, err, NULL))
1583 return -1;
1584
Christopher Faulet55343342022-11-18 15:52:58 +01001585 if (non_global_section_parsed == 1) {
1586 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]);
1587 return -1;
1588 }
1589
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001590 nbthread = strtol(args[1], &errptr, 10);
1591 if (!*args[1] || *errptr) {
1592 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1593 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001594 }
1595
1596#ifndef USE_THREAD
1597 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001598 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]);
1599 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001600 }
1601#else
1602 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001603 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1604 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001605 }
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001606#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001607
1608 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001609 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1610 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001611
1612 global.nbthread = nbthread;
1613 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001614}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001615
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001616/* Parse the "thread-group" global directive, which takes an integer argument
1617 * that designates a thread group, and a list of threads to put into that group.
1618 */
1619static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx,
1620 const struct proxy *defpx, const char *file, int line,
1621 char **err)
1622{
1623 char *errptr;
1624 long tnum, tend, tgroup;
1625 int arg, tot;
1626
Christopher Faulet55343342022-11-18 15:52:58 +01001627 if (non_global_section_parsed == 1) {
1628 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]);
1629 return -1;
1630 }
1631
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001632 tgroup = strtol(args[1], &errptr, 10);
1633 if (!*args[1] || *errptr) {
1634 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1635 return -1;
1636 }
1637
1638 if (tgroup < 1 || tgroup > MAX_TGROUPS) {
1639 memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup);
1640 return -1;
1641 }
1642
1643 /* look for a preliminary definition of any thread pointing to this
1644 * group, and remove them.
1645 */
1646 if (ha_tgroup_info[tgroup-1].count) {
1647 ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n",
1648 file, line, args[0], tgroup);
1649
1650 for (tnum = ha_tgroup_info[tgroup-1].base;
1651 tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count;
1652 tnum++) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001653 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001654 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001655 ha_thread_info[tnum-1].tgid = 0;
Willy Tarreau03f9b352022-06-27 16:02:24 +02001656 ha_thread_info[tnum-1].tg_ctx = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001657 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001658 }
1659 ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0;
1660 }
1661
1662 tot = 0;
1663 for (arg = 2; args[arg] && *args[arg]; arg++) {
1664 tend = tnum = strtol(args[arg], &errptr, 10);
1665
1666 if (*errptr == '-')
1667 tend = strtol(errptr + 1, &errptr, 10);
1668
1669 if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) {
1670 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);
1671 return -1;
1672 }
1673
1674 for(; tnum <= tend; tnum++) {
1675 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
1676 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n",
1677 file, line, args[0], tgroup, tnum);
1678 } else if (ha_thread_info[tnum-1].tg) {
1679 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n",
1680 file, line, args[0], tgroup, tnum,
1681 (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1));
1682 }
1683
1684 if (!ha_tgroup_info[tgroup-1].count) {
1685 ha_tgroup_info[tgroup-1].base = tnum-1;
1686 ha_tgroup_info[tgroup-1].count = 1;
1687 }
1688 else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) {
1689 ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base;
1690 }
1691 else if (tnum < ha_tgroup_info[tgroup-1].base) {
1692 ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1;
1693 ha_tgroup_info[tgroup-1].base = tnum - 1;
1694 }
1695
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001696 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001697 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001698 ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1];
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001699 tot++;
1700 }
1701 }
1702
1703 if (ha_tgroup_info[tgroup-1].count > tot) {
1704 memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup);
1705 return -1;
1706 }
1707
1708 if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) {
1709 memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP);
1710 return -1;
1711 }
1712
1713 return 0;
1714}
1715
Willy Tarreauc33b9692021-09-22 12:07:23 +02001716/* Parse the "thread-groups" global directive, which takes an integer argument
1717 * that contains the desired number of thread groups.
1718 */
1719static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx,
1720 const struct proxy *defpx, const char *file, int line,
1721 char **err)
1722{
1723 long nbtgroups;
1724 char *errptr;
1725
1726 if (too_many_args(1, args, err, NULL))
1727 return -1;
Christopher Faulet55343342022-11-18 15:52:58 +01001728
1729 if (non_global_section_parsed == 1) {
1730 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]);
1731 return -1;
1732 }
Willy Tarreauc33b9692021-09-22 12:07:23 +02001733
1734 nbtgroups = strtol(args[1], &errptr, 10);
1735 if (!*args[1] || *errptr) {
1736 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1737 return -1;
1738 }
1739
1740#ifndef USE_THREAD
1741 if (nbtgroups != 1) {
1742 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]);
1743 return -1;
1744 }
1745#else
1746 if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) {
1747 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups);
1748 return -1;
1749 }
1750#endif
1751
1752 HA_DIAG_WARNING_COND(global.nbtgroups,
1753 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1754 file, line, args[0]);
1755
1756 global.nbtgroups = nbtgroups;
1757 return 0;
1758}
1759
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001760/* config keyword parsers */
1761static struct cfg_kw_list cfg_kws = {ILH, {
1762 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001763 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001764 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001765 { 0, NULL, NULL }
1766}};
1767
1768INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);