blob: 510a6f51adb789accc1be5c4c83680b9e97f2053 [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].proc))
281 ha_cpuset_and(&cpu_map[tgid - 1].thread[ti->ltid], &cpu_map[tgid - 1].proc);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200282
Willy Tarreau5b093412022-07-08 09:38:30 +0200283 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 +0200284# if defined(__APPLE__)
285 /* Note: this API is limited to the first 32/64 CPUs */
Willy Tarreau5b093412022-07-08 09:38:30 +0200286 unsigned long set = cpu_map[tgid - 1].thread[ti->ltid].cpuset;
Willy Tarreaud10385a2021-10-06 22:22:40 +0200287 int j;
288
289 while ((j = ffsl(set)) > 0) {
290 thread_affinity_policy_data_t cpu_set = { j - 1 };
291 thread_port_t mthread;
292
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200293 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200294 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
295 set &= ~(1UL << (j - 1));
296 }
297# else
Willy Tarreau5b093412022-07-08 09:38:30 +0200298 struct hap_cpuset *set = &cpu_map[tgid - 1].thread[ti->ltid];
Willy Tarreaud10385a2021-10-06 22:22:40 +0200299
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200300 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200301# endif
302 }
303#endif /* USE_CPU_AFFINITY */
304}
305
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200306/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
307 * since POSIX took great care of not specifying its representation, making it
308 * hard to export for post-mortem analysis. For this reason we copy it into a
309 * union and will use the smallest scalar type at least as large as its size,
310 * which will keep endianness and alignment for all regular sizes. As a last
311 * resort we end up with a long long ligned to the first bytes in memory, which
312 * will be endian-dependent if pthread_t is larger than a long long (not seen
313 * yet).
314 */
315unsigned long long ha_get_pthread_id(unsigned int thr)
316{
317 union {
318 pthread_t t;
319 unsigned long long ll;
320 unsigned int i;
321 unsigned short s;
322 unsigned char c;
323 } u = { 0 };
324
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200325 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200326
327 if (sizeof(u.t) <= sizeof(u.c))
328 return u.c;
329 else if (sizeof(u.t) <= sizeof(u.s))
330 return u.s;
331 else if (sizeof(u.t) <= sizeof(u.i))
332 return u.i;
333 return u.ll;
334}
335
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200336/* send signal <sig> to thread <thr> */
337void ha_tkill(unsigned int thr, int sig)
338{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200339 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200340}
341
342/* send signal <sig> to all threads. The calling thread is signaled last in
343 * order to allow all threads to synchronize in the handler.
344 */
345void ha_tkillall(int sig)
346{
347 unsigned int thr;
348
349 for (thr = 0; thr < global.nbthread; thr++) {
Willy Tarreauf15c75a2022-07-15 08:27:56 +0200350 if (!(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit))
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200351 continue;
352 if (thr == tid)
353 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200354 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200355 }
356 raise(sig);
357}
358
Willy Tarreauaa992762021-10-06 23:33:20 +0200359void ha_thread_relax(void)
360{
361#ifdef _POSIX_PRIORITY_SCHEDULING
362 sched_yield();
363#else
364 pl_cpu_relax();
365#endif
366}
367
Willy Tarreau3d184982020-10-18 10:20:59 +0200368/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100369void ha_spin_init(HA_SPINLOCK_T *l)
370{
371 HA_SPIN_INIT(l);
372}
373
Willy Tarreau3d184982020-10-18 10:20:59 +0200374/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100375void ha_rwlock_init(HA_RWLOCK_T *l)
376{
377 HA_RWLOCK_INIT(l);
378}
379
Willy Tarreauf5b63272023-03-09 10:12:06 +0100380/* returns the number of CPUs the current process is enabled to run on,
381 * regardless of any MAX_THREADS limitation.
382 */
Willy Tarreau149ab772019-01-26 14:27:06 +0100383static int thread_cpus_enabled()
384{
385 int ret = 1;
386
387#ifdef USE_CPU_AFFINITY
388#if defined(__linux__) && defined(CPU_COUNT)
389 cpu_set_t mask;
390
391 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
392 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200393#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
394 cpuset_t cpuset;
395 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
396 sizeof(cpuset), &cpuset) == 0)
397 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000398#elif defined(__APPLE__)
399 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100400#endif
401#endif
402 ret = MAX(ret, 1);
Willy Tarreau149ab772019-01-26 14:27:06 +0100403 return ret;
404}
405
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200406/* Returns 1 if the cpu set is currently restricted for the process else 0.
407 * Currently only implemented for the Linux platform.
408 */
409int thread_cpu_mask_forced()
410{
411#if defined(__linux__)
412 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
413 return cpus_avail != thread_cpus_enabled();
414#else
415 return 0;
416#endif
417}
418
Willy Tarreau407ef892021-10-05 18:39:27 +0200419/* Below come the lock-debugging functions */
420
421#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
422
423struct lock_stat lock_stats[LOCK_LABELS];
424
425/* this is only used below */
426static const char *lock_label(enum lock_label label)
427{
428 switch (label) {
429 case TASK_RQ_LOCK: return "TASK_RQ";
430 case TASK_WQ_LOCK: return "TASK_WQ";
431 case LISTENER_LOCK: return "LISTENER";
432 case PROXY_LOCK: return "PROXY";
433 case SERVER_LOCK: return "SERVER";
434 case LBPRM_LOCK: return "LBPRM";
435 case SIGNALS_LOCK: return "SIGNALS";
436 case STK_TABLE_LOCK: return "STK_TABLE";
437 case STK_SESS_LOCK: return "STK_SESS";
438 case APPLETS_LOCK: return "APPLETS";
439 case PEER_LOCK: return "PEER";
440 case SHCTX_LOCK: return "SHCTX";
441 case SSL_LOCK: return "SSL";
442 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
443 case PATREF_LOCK: return "PATREF";
444 case PATEXP_LOCK: return "PATEXP";
445 case VARS_LOCK: return "VARS";
446 case COMP_POOL_LOCK: return "COMP_POOL";
447 case LUA_LOCK: return "LUA";
448 case NOTIF_LOCK: return "NOTIF";
449 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
450 case DNS_LOCK: return "DNS";
451 case PID_LIST_LOCK: return "PID_LIST";
452 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
453 case PIPES_LOCK: return "PIPES";
454 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
455 case AUTH_LOCK: return "AUTH";
456 case LOGSRV_LOCK: return "LOGSRV";
457 case DICT_LOCK: return "DICT";
458 case PROTO_LOCK: return "PROTO";
459 case QUEUE_LOCK: return "QUEUE";
460 case CKCH_LOCK: return "CKCH";
461 case SNI_LOCK: return "SNI";
462 case SSL_SERVER_LOCK: return "SSL_SERVER";
463 case SFT_LOCK: return "SFT";
464 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
Remi Tricot-Le Breton2b963642022-12-20 11:11:02 +0100465 case OCSP_LOCK: return "OCSP";
Amaury Denoyellee83f9372023-04-18 11:10:54 +0200466 case QC_CID_LOCK: return "QC_CID";
Willy Tarreau407ef892021-10-05 18:39:27 +0200467 case OTHER_LOCK: return "OTHER";
468 case DEBUG1_LOCK: return "DEBUG1";
469 case DEBUG2_LOCK: return "DEBUG2";
470 case DEBUG3_LOCK: return "DEBUG3";
471 case DEBUG4_LOCK: return "DEBUG4";
472 case DEBUG5_LOCK: return "DEBUG5";
473 case LOCK_LABELS: break; /* keep compiler happy */
474 };
475 /* only way to come here is consecutive to an internal bug */
476 abort();
477}
478
479void show_lock_stats()
480{
481 int lbl;
482
483 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
484 if (!lock_stats[lbl].num_write_locked &&
485 !lock_stats[lbl].num_seek_locked &&
486 !lock_stats[lbl].num_read_locked) {
487 fprintf(stderr,
488 "Stats about Lock %s: not used\n",
489 lock_label(lbl));
490 continue;
491 }
492
493 fprintf(stderr,
494 "Stats about Lock %s: \n",
495 lock_label(lbl));
496
497 if (lock_stats[lbl].num_write_locked)
498 fprintf(stderr,
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100499 "\t # write lock : %llu\n"
500 "\t # write unlock: %llu (%lld)\n"
Willy Tarreau407ef892021-10-05 18:39:27 +0200501 "\t # wait time for write : %.3f msec\n"
502 "\t # wait time for write/lock: %.3f nsec\n",
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100503 (ullong)lock_stats[lbl].num_write_locked,
504 (ullong)lock_stats[lbl].num_write_unlocked,
505 (llong)(lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked),
Willy Tarreau407ef892021-10-05 18:39:27 +0200506 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
507 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
508
509 if (lock_stats[lbl].num_seek_locked)
510 fprintf(stderr,
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100511 "\t # seek lock : %llu\n"
512 "\t # seek unlock : %llu (%lld)\n"
Willy Tarreau407ef892021-10-05 18:39:27 +0200513 "\t # wait time for seek : %.3f msec\n"
514 "\t # wait time for seek/lock : %.3f nsec\n",
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100515 (ullong)lock_stats[lbl].num_seek_locked,
516 (ullong)lock_stats[lbl].num_seek_unlocked,
517 (llong)(lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked),
Willy Tarreau407ef892021-10-05 18:39:27 +0200518 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
519 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
520
521 if (lock_stats[lbl].num_read_locked)
522 fprintf(stderr,
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100523 "\t # read lock : %llu\n"
524 "\t # read unlock : %llu (%lld)\n"
Willy Tarreau407ef892021-10-05 18:39:27 +0200525 "\t # wait time for read : %.3f msec\n"
526 "\t # wait time for read/lock : %.3f nsec\n",
Frédéric Lécaille83540ed2023-02-24 09:47:07 +0100527 (ullong)lock_stats[lbl].num_read_locked,
528 (ullong)lock_stats[lbl].num_read_unlocked,
529 (llong)(lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked),
Willy Tarreau407ef892021-10-05 18:39:27 +0200530 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
531 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
532 }
533}
534
Willy Tarreau407ef892021-10-05 18:39:27 +0200535void __ha_rwlock_init(struct ha_rwlock *l)
536{
537 memset(l, 0, sizeof(struct ha_rwlock));
538 __RWLOCK_INIT(&l->lock);
539}
540
541void __ha_rwlock_destroy(struct ha_rwlock *l)
542{
543 __RWLOCK_DESTROY(&l->lock);
544 memset(l, 0, sizeof(struct ha_rwlock));
545}
546
547
548void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
549 const char *func, const char *file, int line)
550{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200551 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
552 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200553 uint64_t start_time;
554
Willy Tarreau7aa41192022-07-15 17:53:10 +0200555 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200556 abort();
557
Willy Tarreau7aa41192022-07-15 17:53:10 +0200558 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200559
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200560 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200561 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200562 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200563
564 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
565
Willy Tarreau7aa41192022-07-15 17:53:10 +0200566 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200567 l->info.last_location.function = func;
568 l->info.last_location.file = file;
569 l->info.last_location.line = line;
570
Willy Tarreau7aa41192022-07-15 17:53:10 +0200571 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200572}
573
574int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
575 const char *func, const char *file, int line)
576{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200577 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
578 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200579 uint64_t start_time;
580 int r;
581
Willy Tarreau7aa41192022-07-15 17:53:10 +0200582 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200583 abort();
584
585 /* We set waiting writer because trywrlock could wait for readers to quit */
Willy Tarreau7aa41192022-07-15 17:53:10 +0200586 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200587
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200588 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200589 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200590 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200591 if (unlikely(r)) {
Willy Tarreau7aa41192022-07-15 17:53:10 +0200592 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200593 return r;
594 }
595 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
596
Willy Tarreau7aa41192022-07-15 17:53:10 +0200597 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200598 l->info.last_location.function = func;
599 l->info.last_location.file = file;
600 l->info.last_location.line = line;
601
Willy Tarreau7aa41192022-07-15 17:53:10 +0200602 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200603
604 return 0;
605}
606
607void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
608 const char *func, const char *file, int line)
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];
612
613 if (unlikely(!(st->cur_writer & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200614 /* the thread is not owning the lock for write */
615 abort();
616 }
617
Willy Tarreau7aa41192022-07-15 17:53:10 +0200618 st->cur_writer = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200619 l->info.last_location.function = func;
620 l->info.last_location.file = file;
621 l->info.last_location.line = line;
622
623 __RWLOCK_WRUNLOCK(&l->lock);
624
625 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
626}
627
628void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
629{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200630 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
631 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200632 uint64_t start_time;
633
Willy Tarreau7aa41192022-07-15 17:53:10 +0200634 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200635 abort();
636
Willy Tarreau7aa41192022-07-15 17:53:10 +0200637 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200638
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200639 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200640 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200641 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200642 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
Willy Tarreau7aa41192022-07-15 17:53:10 +0200646 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200647}
648
649int __ha_rwlock_tryrdlock(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];
Willy Tarreau407ef892021-10-05 18:39:27 +0200653 int r;
654
Willy Tarreau7aa41192022-07-15 17:53:10 +0200655 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200656 abort();
657
658 /* try read should never wait */
659 r = __RWLOCK_TRYRDLOCK(&l->lock);
660 if (unlikely(r))
661 return r;
662 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
663
Willy Tarreau7aa41192022-07-15 17:53:10 +0200664 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200665
666 return 0;
667}
668
669void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
670{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200671 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
672 struct ha_rwlock_state *st = &l->info.st[tgid-1];
673
674 if (unlikely(!(st->cur_readers & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200675 /* the thread is not owning the lock for read */
676 abort();
677 }
678
Willy Tarreau7aa41192022-07-15 17:53:10 +0200679 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200680
681 __RWLOCK_RDUNLOCK(&l->lock);
682
683 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
684}
685
686void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
687 const char *func, const char *file, int line)
688{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200689 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
690 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200691 uint64_t start_time;
692
Willy Tarreau7aa41192022-07-15 17:53:10 +0200693 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200694 abort();
695
Willy Tarreau7aa41192022-07-15 17:53:10 +0200696 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200697 abort();
698
Willy Tarreau7aa41192022-07-15 17:53:10 +0200699 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200700
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200701 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200702 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200703 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200704
705 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
706
Willy Tarreau7aa41192022-07-15 17:53:10 +0200707 HA_ATOMIC_OR(&st->cur_readers, tbit);
708 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200709 l->info.last_location.function = func;
710 l->info.last_location.file = file;
711 l->info.last_location.line = line;
712
Willy Tarreau7aa41192022-07-15 17:53:10 +0200713 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200714}
715
716void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
717 const char *func, const char *file, int line)
718{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200719 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
720 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200721 uint64_t start_time;
722
Willy Tarreau7aa41192022-07-15 17:53:10 +0200723 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200724 abort();
725
Willy Tarreau7aa41192022-07-15 17:53:10 +0200726 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200727 abort();
728
Willy Tarreau7aa41192022-07-15 17:53:10 +0200729 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200730
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200731 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200732 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200733 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200734
735 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
736
Willy Tarreau7aa41192022-07-15 17:53:10 +0200737 HA_ATOMIC_OR(&st->cur_seeker, tbit);
738 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200739 l->info.last_location.function = func;
740 l->info.last_location.file = file;
741 l->info.last_location.line = line;
742
Willy Tarreau7aa41192022-07-15 17:53:10 +0200743 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200744}
745
746void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
747 const char *func, const char *file, int line)
748{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200749 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
750 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200751 uint64_t start_time;
752
Willy Tarreau7aa41192022-07-15 17:53:10 +0200753 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200754 abort();
755
Willy Tarreau7aa41192022-07-15 17:53:10 +0200756 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200757
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200758 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200759 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200760 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200761
762 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
763
Willy Tarreau7aa41192022-07-15 17:53:10 +0200764 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200765 l->info.last_location.function = func;
766 l->info.last_location.file = file;
767 l->info.last_location.line = line;
768
Willy Tarreau7aa41192022-07-15 17:53:10 +0200769 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200770}
771
772void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
773 const char *func, const char *file, int line)
774{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200775 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
776 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200777 uint64_t start_time;
778
Willy Tarreau7aa41192022-07-15 17:53:10 +0200779 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200780 abort();
781
Willy Tarreau7aa41192022-07-15 17:53:10 +0200782 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200783 abort();
784
Willy Tarreau7aa41192022-07-15 17:53:10 +0200785 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200786
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200787 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200788 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200789 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200790
791 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
792
Willy Tarreau7aa41192022-07-15 17:53:10 +0200793 HA_ATOMIC_OR(&st->cur_writer, tbit);
794 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200795 l->info.last_location.function = func;
796 l->info.last_location.file = file;
797 l->info.last_location.line = line;
798
Willy Tarreau7aa41192022-07-15 17:53:10 +0200799 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200800}
801
802void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
803 const char *func, const char *file, int line)
804{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200805 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
806 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200807 uint64_t start_time;
808
Willy Tarreau7aa41192022-07-15 17:53:10 +0200809 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200810 abort();
811
Willy Tarreau7aa41192022-07-15 17:53:10 +0200812 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200813 abort();
814
Willy Tarreau7aa41192022-07-15 17:53:10 +0200815 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200816
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200817 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200818 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200819 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200820
821 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
822
Willy Tarreau7aa41192022-07-15 17:53:10 +0200823 HA_ATOMIC_OR(&st->cur_readers, tbit);
824 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200825 l->info.last_location.function = func;
826 l->info.last_location.file = file;
827 l->info.last_location.line = line;
828
Willy Tarreau7aa41192022-07-15 17:53:10 +0200829 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200830}
831
832void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
833 const char *func, const char *file, int line)
834{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200835 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
836 struct ha_rwlock_state *st = &l->info.st[tgid-1];
837 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200838 abort();
839
Willy Tarreau7aa41192022-07-15 17:53:10 +0200840 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200841 l->info.last_location.function = func;
842 l->info.last_location.file = file;
843 l->info.last_location.line = line;
844
845 __RWLOCK_SKUNLOCK(&l->lock);
846
847 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
848}
849
850int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
851 const char *func, const char *file, int line)
852{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200853 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
854 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200855 uint64_t start_time;
856 int r;
857
Willy Tarreau7aa41192022-07-15 17:53:10 +0200858 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200859 abort();
860
Willy Tarreau7aa41192022-07-15 17:53:10 +0200861 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200862
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200863 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200864 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200865 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200866
867 if (likely(!r)) {
868 /* got the lock ! */
869 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200870 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200871 l->info.last_location.function = func;
872 l->info.last_location.file = file;
873 l->info.last_location.line = line;
874 }
875
Willy Tarreau7aa41192022-07-15 17:53:10 +0200876 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200877 return r;
878}
879
880int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
881 const char *func, const char *file, int line)
882{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200883 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
884 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200885 uint64_t start_time;
886 int r;
887
Willy Tarreau7aa41192022-07-15 17:53:10 +0200888 if ((st->cur_writer | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200889 abort();
890
Willy Tarreau7aa41192022-07-15 17:53:10 +0200891 if (!(st->cur_readers & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200892 abort();
893
Willy Tarreau7aa41192022-07-15 17:53:10 +0200894 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200895
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200896 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200897 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200898 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200899
900 if (likely(!r)) {
901 /* got the lock ! */
902 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200903 HA_ATOMIC_OR(&st->cur_seeker, tbit);
904 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200905 l->info.last_location.function = func;
906 l->info.last_location.file = file;
907 l->info.last_location.line = line;
908 }
909
Willy Tarreau7aa41192022-07-15 17:53:10 +0200910 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200911 return r;
912}
913
914void __spin_init(struct ha_spinlock *l)
915{
916 memset(l, 0, sizeof(struct ha_spinlock));
917 __SPIN_INIT(&l->lock);
918}
919
920void __spin_destroy(struct ha_spinlock *l)
921{
922 __SPIN_DESTROY(&l->lock);
923 memset(l, 0, sizeof(struct ha_spinlock));
924}
925
926void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
927 const char *func, const char *file, int line)
928{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200929 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
930 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200931 uint64_t start_time;
932
Willy Tarreau7aa41192022-07-15 17:53:10 +0200933 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200934 /* the thread is already owning the lock */
935 abort();
936 }
937
Willy Tarreau7aa41192022-07-15 17:53:10 +0200938 HA_ATOMIC_OR(&st->waiters, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200939
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200940 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200941 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200942 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200943
944 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
945
946
Willy Tarreau7aa41192022-07-15 17:53:10 +0200947 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200948 l->info.last_location.function = func;
949 l->info.last_location.file = file;
950 l->info.last_location.line = line;
951
Willy Tarreau7aa41192022-07-15 17:53:10 +0200952 HA_ATOMIC_AND(&st->waiters, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200953}
954
955int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
956 const char *func, const char *file, int line)
957{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200958 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
959 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200960 int r;
961
Willy Tarreau7aa41192022-07-15 17:53:10 +0200962 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200963 /* the thread is already owning the lock */
964 abort();
965 }
966
967 /* try read should never wait */
968 r = __SPIN_TRYLOCK(&l->lock);
969 if (unlikely(r))
970 return r;
971 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
972
Willy Tarreau7aa41192022-07-15 17:53:10 +0200973 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200974 l->info.last_location.function = func;
975 l->info.last_location.file = file;
976 l->info.last_location.line = line;
977
978 return 0;
979}
980
981void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
982 const char *func, const char *file, int line)
983{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200984 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
985 struct ha_spinlock_state *st = &l->info.st[tgid-1];
986
987 if (unlikely(!(st->owner & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200988 /* the thread is not owning the lock */
989 abort();
990 }
991
Willy Tarreau7aa41192022-07-15 17:53:10 +0200992 st->owner = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200993 l->info.last_location.function = func;
994 l->info.last_location.file = file;
995 l->info.last_location.line = line;
996
997 __SPIN_UNLOCK(&l->lock);
998 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
999}
1000
1001#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
1002
Willy Tarreau87aff022022-07-10 10:58:57 +02001003
1004#if defined(USE_PTHREAD_EMULATION)
1005
1006/* pthread rwlock emulation using plocks (to avoid expensive futexes).
1007 * these are a direct mapping on Progressive Locks, with the exception that
1008 * since there's a common unlock operation in pthreads, we need to know if
1009 * we need to unlock for reads or writes, so we set the topmost bit to 1 when
1010 * a write lock is acquired to indicate that a write unlock needs to be
1011 * performed. It's not a problem since this bit will never be used given that
1012 * haproxy won't support as many threads as the plocks.
1013 *
1014 * The storage is the pthread_rwlock_t cast as an ulong
1015 */
1016
1017int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr)
1018{
1019 ulong *lock = (ulong *)rwlock;
1020
1021 *lock = 0;
1022 return 0;
1023}
1024
1025int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
1026{
1027 ulong *lock = (ulong *)rwlock;
1028
1029 *lock = 0;
1030 return 0;
1031}
1032
1033int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
1034{
1035 pl_lorw_rdlock((unsigned long *)rwlock);
1036 return 0;
1037}
1038
1039int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock)
1040{
1041 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_SHR_BASE);
1042}
1043
1044int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1045{
1046 return pthread_rwlock_tryrdlock(rwlock);
1047}
1048
1049int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
1050{
1051 pl_lorw_wrlock((unsigned long *)rwlock);
1052 return 0;
1053}
1054
1055int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock)
1056{
1057 return !!pl_cmpxchg((unsigned long *)rwlock, 0, PLOCK_LORW_EXC_BASE);
1058}
1059
1060int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock, const struct timespec *restrict abstime)
1061{
1062 return pthread_rwlock_trywrlock(rwlock);
1063}
1064
1065int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
1066{
1067 pl_lorw_unlock((unsigned long *)rwlock);
1068 return 0;
1069}
1070#endif // defined(USE_PTHREAD_EMULATION)
1071
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001072/* Depending on the platform and how libpthread was built, pthread_exit() may
1073 * involve some code in libgcc_s that would be loaded on exit for the first
1074 * time, causing aborts if the process is chrooted. It's harmless bit very
1075 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
1076 * needed, so what we do here is that during early boot we create a dummy
1077 * thread that immediately exits. This will lead to libgcc_s being loaded
1078 * during boot on the platforms where it's required.
1079 */
1080static void *dummy_thread_function(void *data)
1081{
1082 pthread_exit(NULL);
1083 return NULL;
1084}
1085
1086static inline void preload_libgcc_s(void)
1087{
1088 pthread_t dummy_thread;
eaglegaief667b12023-05-26 16:44:34 +08001089 if (pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL) == 0)
1090 pthread_join(dummy_thread, NULL);
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001091}
1092
Willy Tarreau3f567e42020-05-28 15:29:19 +02001093static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001094{
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001095 char *ptr = NULL;
1096
Willy Tarreauf734ebf2020-09-09 17:07:54 +02001097 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +02001098
Willy Tarreau149ab772019-01-26 14:27:06 +01001099 thread_cpus_enabled_at_boot = thread_cpus_enabled();
Willy Tarreauf5b63272023-03-09 10:12:06 +01001100 thread_cpus_enabled_at_boot = MIN(thread_cpus_enabled_at_boot, MAX_THREADS);
Willy Tarreau149ab772019-01-26 14:27:06 +01001101
Willy Tarreauc80bdb22022-08-06 16:44:55 +02001102 memprintf(&ptr, "Built with multi-threading support (MAX_TGROUPS=%d, MAX_THREADS=%d, default=%d).",
1103 MAX_TGROUPS, MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001104 hap_register_build_opts(ptr, 1);
1105
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001106#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
1107 memset(lock_stats, 0, sizeof(lock_stats));
1108#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001109}
Willy Tarreau8ead1d02022-04-25 19:23:17 +02001110INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001111
Willy Tarreau8459f252018-12-15 16:48:14 +01001112#else
1113
Willy Tarreauaa992762021-10-06 23:33:20 +02001114/* send signal <sig> to thread <thr> (send to process in fact) */
1115void ha_tkill(unsigned int thr, int sig)
1116{
1117 raise(sig);
1118}
1119
1120/* send signal <sig> to all threads (send to process in fact) */
1121void ha_tkillall(int sig)
1122{
1123 raise(sig);
1124}
1125
1126void ha_thread_relax(void)
1127{
1128#ifdef _POSIX_PRIORITY_SCHEDULING
1129 sched_yield();
1130#endif
1131}
1132
Willy Tarreau8459f252018-12-15 16:48:14 +01001133REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
1134
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001135#endif // USE_THREAD
1136
1137
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001138/* scans the configured thread mapping and establishes the final one. Returns <0
1139 * on failure, >=0 on success.
1140 */
1141int thread_map_to_groups()
1142{
1143 int t, g, ut, ug;
1144 int q, r;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001145 ulong m __maybe_unused;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001146
1147 ut = ug = 0; // unassigned threads & groups
1148
1149 for (t = 0; t < global.nbthread; t++) {
1150 if (!ha_thread_info[t].tg)
1151 ut++;
1152 }
1153
1154 for (g = 0; g < global.nbtgroups; g++) {
1155 if (!ha_tgroup_info[g].count)
1156 ug++;
Willy Tarreau60fe4a92022-06-28 17:48:07 +02001157 ha_tgroup_info[g].tgid_bit = 1UL << g;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001158 }
1159
1160 if (ug > ut) {
1161 ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut);
1162 return -1;
1163 }
1164
1165 /* look for first unassigned thread */
1166 for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++)
1167 ;
1168
1169 /* assign threads to empty groups */
1170 for (g = 0; ug && ut; ) {
1171 /* due to sparse thread assignment we can end up with more threads
1172 * per group on last assigned groups than former ones, so we must
1173 * always try to pack the maximum remaining ones together first.
1174 */
1175 q = ut / ug;
1176 r = ut % ug;
1177 if ((q + !!r) > MAX_THREADS_PER_GROUP) {
Willy Tarreaucf0d0ee2023-03-09 11:39:51 +01001178 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 +02001179 return -1;
1180 }
1181
1182 /* thread <t> is the next unassigned one. Let's look for next
1183 * unassigned group, we know there are some left
1184 */
1185 while (ut >= ug && ha_tgroup_info[g].count)
1186 g++;
1187
1188 /* group g is unassigned, try to fill it with consecutive threads */
1189 while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r &&
1190 (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) {
1191
1192 if (!ha_tgroup_info[g].count) {
1193 /* assign new group */
1194 ha_tgroup_info[g].base = t;
1195 ug--;
1196 }
1197
1198 ha_tgroup_info[g].count++;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001199 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001200 ha_thread_info[t].tg = &ha_tgroup_info[g];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001201 ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g];
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001202
1203 ut--;
1204 /* switch to next unassigned thread */
1205 while (++t < global.nbthread && ha_thread_info[t].tg)
1206 ;
1207 }
1208 }
1209
1210 if (ut) {
1211 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);
1212 return -1;
1213 }
1214
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001215 for (t = 0; t < global.nbthread; t++) {
1216 ha_thread_info[t].tid = t;
1217 ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base;
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001218 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1219 }
1220
Willy Tarreaucce203a2022-06-24 15:55:11 +02001221 m = 0;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001222 for (g = 0; g < global.nbtgroups; g++) {
1223 ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count);
Aurelien DARRAGON739281b2023-01-27 15:13:28 +01001224 /* for now, additional threads are not started, so we should
1225 * consider them as harmless and idle.
1226 * This will get automatically updated when such threads are
1227 * started in run_thread_poll_loop()
1228 * Without this, thread_isolate() and thread_isolate_full()
1229 * will fail to work as long as secondary threads did not enter
1230 * the polling loop at least once.
1231 */
1232 ha_tgroup_ctx[g].threads_harmless = ha_tgroup_info[g].threads_enabled;
1233 ha_tgroup_ctx[g].threads_idle = ha_tgroup_info[g].threads_enabled;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001234 if (!ha_tgroup_info[g].count)
1235 continue;
1236 m |= 1UL << g;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001237
1238 }
1239
Willy Tarreaucce203a2022-06-24 15:55:11 +02001240#ifdef USE_THREAD
1241 all_tgroups_mask = m;
1242#endif
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001243 return 0;
1244}
1245
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001246/* Converts a configuration thread set based on either absolute or relative
1247 * thread numbers into a global group+mask. This is essentially for use with
1248 * the "thread" directive on "bind" lines, where "thread 4-6,10-12" might be
1249 * turned to "2/1-3,4/1-3". It cannot be used before the thread mapping above
1250 * was completed and the thread group numbers configured. The thread_set is
1251 * replaced by the resolved group-based one. It is possible to force a single
1252 * default group for unspecified sets instead of enabling all groups by passing
Willy Tarreauf2988e12023-02-02 17:01:10 +01001253 * this group's non-zero value to defgrp.
Willy Tarreau627def92021-09-29 18:59:47 +02001254 *
1255 * Returns <0 on failure, >=0 on success.
1256 */
Willy Tarreauf2988e12023-02-02 17:01:10 +01001257int thread_resolve_group_mask(struct thread_set *ts, int defgrp, char **err)
Willy Tarreau627def92021-09-29 18:59:47 +02001258{
Willy Tarreau15c84282023-02-04 10:49:01 +01001259 struct thread_set new_ts = { };
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001260 ulong mask, imask;
1261 uint g;
Willy Tarreau627def92021-09-29 18:59:47 +02001262
Willy Tarreau97da9422023-03-01 11:24:29 +01001263 if (!ts->grps) {
Willy Tarreau627def92021-09-29 18:59:47 +02001264 /* unspecified group, IDs are global */
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001265 if (thread_set_is_empty(ts)) {
1266 /* all threads of all groups, unless defgrp is set and
1267 * we then set it as the only group.
1268 */
1269 for (g = defgrp ? defgrp-1 : 0; g < (defgrp ? defgrp : global.nbtgroups); g++) {
1270 new_ts.rel[g] = ha_tgroup_info[g].threads_enabled;
Willy Tarreau97da9422023-03-01 11:24:29 +01001271 if (new_ts.rel[g])
1272 new_ts.grps |= 1UL << g;
Willy Tarreau627def92021-09-29 18:59:47 +02001273 }
Willy Tarreau627def92021-09-29 18:59:47 +02001274 } else {
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001275 /* some absolute threads are set, we must remap them to
1276 * relative ones. Each group cannot have more than
1277 * LONGBITS threads, thus it spans at most two absolute
1278 * blocks.
1279 */
1280 for (g = 0; g < global.nbtgroups; g++) {
1281 uint block = ha_tgroup_info[g].base / LONGBITS;
1282 uint base = ha_tgroup_info[g].base % LONGBITS;
Willy Tarreau627def92021-09-29 18:59:47 +02001283
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001284 mask = ts->abs[block] >> base;
Willy Tarreau1b536a12023-03-22 10:28:50 +01001285 if (base &&
1286 (block + 1) < sizeof(ts->abs) / sizeof(ts->abs[0]) &&
1287 ha_tgroup_info[g].count > (LONGBITS - base))
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001288 mask |= ts->abs[block + 1] << (LONGBITS - base);
1289 mask &= nbits(ha_tgroup_info[g].count);
1290 mask &= ha_tgroup_info[g].threads_enabled;
Willy Tarreau627def92021-09-29 18:59:47 +02001291
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001292 /* now the mask exactly matches the threads to be enabled
1293 * in this group.
1294 */
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001295 new_ts.rel[g] |= mask;
Willy Tarreau97da9422023-03-01 11:24:29 +01001296 if (new_ts.rel[g])
1297 new_ts.grps |= 1UL << g;
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001298 }
Willy Tarreau627def92021-09-29 18:59:47 +02001299 }
1300 } else {
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001301 /* groups were specified */
1302 for (g = 0; g < MAX_TGROUPS; g++) {
1303 imask = ts->rel[g];
1304 if (!imask)
1305 continue;
Willy Tarreau627def92021-09-29 18:59:47 +02001306
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001307 if (g >= global.nbtgroups) {
1308 memprintf(err, "'thread' directive references non-existing thread group %u", g+1);
1309 return -1;
1310 }
Willy Tarreau627def92021-09-29 18:59:47 +02001311
Willy Tarreauf0de8ca2023-01-31 19:31:27 +01001312 /* some relative threads are set. Keep only existing ones for this group */
1313 mask = nbits(ha_tgroup_info[g].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001314
1315 if (!(mask & imask)) {
1316 /* no intersection between the thread group's
1317 * threads and the bind line's.
1318 */
1319#ifdef THREAD_AUTO_ADJUST_GROUPS
1320 unsigned long new_mask = 0;
1321
1322 while (imask) {
1323 new_mask |= imask & mask;
Willy Tarreaubef43df2023-01-31 19:27:48 +01001324 imask >>= ha_tgroup_info[g].count;
Willy Tarreau627def92021-09-29 18:59:47 +02001325 }
1326 imask = new_mask;
1327#else
Willy Tarreaubef43df2023-01-31 19:27:48 +01001328 memprintf(err, "'thread' directive only references threads not belonging to group %u", g+1);
Willy Tarreau627def92021-09-29 18:59:47 +02001329 return -1;
1330#endif
1331 }
1332
Willy Tarreaubef43df2023-01-31 19:27:48 +01001333 new_ts.rel[g] = imask & mask;
Willy Tarreau97da9422023-03-01 11:24:29 +01001334 if (new_ts.rel[g])
1335 new_ts.grps |= 1UL << g;
Willy Tarreau627def92021-09-29 18:59:47 +02001336 }
1337 }
Willy Tarreaubef43df2023-01-31 19:27:48 +01001338
1339 /* update the thread_set */
Willy Tarreau7b8aac42023-02-27 11:27:38 +01001340 if (!thread_set_nth_group(&new_ts, 0)) {
Willy Tarreaubef43df2023-01-31 19:27:48 +01001341 memprintf(err, "'thread' directive only references non-existing threads");
1342 return -1;
1343 }
1344
1345 *ts = new_ts;
Willy Tarreaubef43df2023-01-31 19:27:48 +01001346 return 0;
1347}
1348
1349/* Parse a string representing a thread set in one of the following forms:
1350 *
1351 * - { "all" | "odd" | "even" | <abs_num> [ "-" <abs_num> ] }[,...]
1352 * => these are (lists of) absolute thread numbers
1353 *
1354 * - <tgnum> "/" { "all" | "odd" | "even" | <rel_num> [ "-" <rel_num> ][,...]
1355 * => these are (lists of) per-group relative thread numbers. All numbers
1356 * must be lower than or equal to LONGBITS. When multiple list elements
1357 * are provided, each of them must contain the thread group number.
1358 *
1359 * Minimum value for a thread or group number is always 1. Maximum value for an
1360 * absolute thread number is MAX_THREADS, maximum value for a relative thread
1361 * number is MAX_THREADS_PER_GROUP, an maximum value for a thread group is
1362 * MAX_TGROUPS. "all", "even" and "odd" will be bound by MAX_THREADS and/or
1363 * MAX_THREADS_PER_GROUP in any case. In ranges, a missing digit before "-"
1364 * is implicitly 1, and a missing digit after "-" is implicitly the highest of
1365 * its class. As such "-" is equivalent to "all", allowing to build strings
1366 * such as "${MIN}-${MAX}" where both MIN and MAX are optional.
1367 *
1368 * It is not valid to mix absolute and relative numbers. As such:
1369 * - all valid (all absolute threads)
1370 * - 12-19,24-31 valid (abs threads 12 to 19 and 24 to 31)
1371 * - 1/all valid (all 32 or 64 threads of group 1)
1372 * - 1/1-4,1/8-10,2/1 valid
1373 * - 1/1-4,8-10 invalid (mixes relatve "1/1-4" with absolute "8-10")
1374 * - 1-4,8-10,2/1 invalid (mixes absolute "1-4,8-10" with relative "2/1")
1375 * - 1/odd-4 invalid (mixes range with boundary)
1376 *
1377 * The target thread set is *completed* with supported threads, which means
1378 * that it's the caller's responsibility for pre-initializing it. If the target
1379 * thread set is NULL, it's not updated and the function only verifies that the
1380 * input parses.
1381 *
1382 * On success, it returns 0. otherwise it returns non-zero with an error
1383 * message in <err>.
1384 */
1385int parse_thread_set(const char *arg, struct thread_set *ts, char **err)
1386{
1387 const char *set;
1388 const char *sep;
1389 int v, min, max, tg;
1390 int is_rel;
1391
1392 /* search for the first delimiter (',', '-' or '/') to decide whether
1393 * we're facing an absolute or relative form. The relative form always
1394 * starts with a number followed by a slash.
1395 */
1396 for (sep = arg; isdigit((uchar)*sep); sep++)
1397 ;
1398
1399 is_rel = (/*sep > arg &&*/ *sep == '/'); /* relative form */
1400
1401 /* from there we have to cut the thread spec around commas */
1402
1403 set = arg;
1404 tg = 0;
1405 while (*set) {
1406 /* note: we can't use strtol() here because "-3" would parse as
1407 * (-3) while we want to stop before the "-", so we find the
1408 * separator ourselves and rely on atoi() whose value we may
1409 * ignore depending where the separator is.
1410 */
1411 for (sep = set; isdigit((uchar)*sep); sep++)
1412 ;
1413
1414 if (sep != set && *sep && *sep != '/' && *sep != '-' && *sep != ',') {
1415 memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set);
1416 return -1;
1417 }
1418
1419 v = (sep != set) ? atoi(set) : 0;
1420
1421 /* Now we know that the string is made of an optional series of digits
1422 * optionally followed by one of the delimiters above, or that it
1423 * starts with a different character.
1424 */
1425
1426 /* first, let's search for the thread group (digits before '/') */
1427
1428 if (tg || !is_rel) {
1429 /* thread group already specified or not expected if absolute spec */
1430 if (*sep == '/') {
1431 if (tg)
1432 memprintf(err, "redundant thread group specification '%s' for group %d", set, tg);
1433 else
1434 memprintf(err, "group-relative thread specification '%s' is not permitted after a absolute thread range.", set);
1435 return -1;
1436 }
1437 } else {
1438 /* this is a group-relative spec, first field is the group number */
1439 if (sep == set && *sep == '/') {
1440 memprintf(err, "thread group number expected before '%s'.", set);
1441 return -1;
1442 }
1443
1444 if (*sep != '/') {
1445 memprintf(err, "absolute thread specification '%s' is not permitted after a group-relative thread range.", set);
1446 return -1;
1447 }
1448
1449 if (v < 1 || v > MAX_TGROUPS) {
1450 memprintf(err, "invalid thread group number '%d', permitted range is 1..%d in '%s'.", v, MAX_TGROUPS, set);
1451 return -1;
1452 }
1453
1454 tg = v;
1455
1456 /* skip group number and go on with set,sep,v as if
1457 * there was no group number.
1458 */
1459 set = sep + 1;
1460 continue;
1461 }
1462
1463 /* Now 'set' starts at the min thread number, whose value is in v if any,
1464 * and preset the max to it, unless the range is filled at once via "all"
1465 * (stored as 1:0), "odd" (stored as) 1:-1, or "even" (stored as 1:-2).
1466 * 'sep' points to the next non-digit which may be set itself e.g. for
1467 * "all" etc or "-xx".
1468 */
1469
1470 if (!*set) {
1471 /* empty set sets no restriction */
1472 min = 1;
1473 max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS;
1474 }
1475 else {
1476 if (sep != set && *sep && *sep != '-' && *sep != ',') {
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02001477 // Only delimiters are permitted around digits.
Willy Tarreaubef43df2023-01-31 19:27:48 +01001478 memprintf(err, "invalid character '%c' in thread set specification: '%s'.", *sep, set);
1479 return -1;
1480 }
1481
1482 /* for non-digits, find next delim */
1483 for (; *sep && *sep != '-' && *sep != ','; sep++)
1484 ;
1485
1486 min = max = 1;
1487 if (sep != set) {
1488 /* non-empty first thread */
1489 if (isteq(ist2(set, sep-set), ist("all")))
1490 max = 0;
1491 else if (isteq(ist2(set, sep-set), ist("odd")))
1492 max = -1;
1493 else if (isteq(ist2(set, sep-set), ist("even")))
1494 max = -2;
1495 else if (v)
1496 min = max = v;
1497 else
1498 max = min = 0; // throw an error below
1499 }
1500
1501 if (min < 1 || min > MAX_THREADS || (is_rel && min > MAX_THREADS_PER_GROUP)) {
1502 memprintf(err, "invalid first thread number '%s', permitted range is 1..%d, or 'all', 'odd', 'even'.",
1503 set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS);
1504 return -1;
1505 }
1506
1507 /* is this a range ? */
1508 if (*sep == '-') {
1509 if (min != max) {
1510 memprintf(err, "extraneous range after 'all', 'odd' or 'even': '%s'.", set);
1511 return -1;
1512 }
1513
1514 /* this is a seemingly valid range, there may be another number */
1515 for (set = ++sep; isdigit((uchar)*sep); sep++)
1516 ;
1517 v = atoi(set);
1518
1519 if (sep == set) { // no digit: to the max
1520 max = is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS;
1521 if (*sep && *sep != ',')
1522 max = 0; // throw an error below
1523 } else
1524 max = v;
1525
1526 if (max < 1 || max > MAX_THREADS || (is_rel && max > MAX_THREADS_PER_GROUP)) {
1527 memprintf(err, "invalid last thread number '%s', permitted range is 1..%d.",
1528 set, is_rel ? MAX_THREADS_PER_GROUP : MAX_THREADS);
1529 return -1;
1530 }
1531 }
1532
1533 /* here sep points to the first non-digit after the thread spec,
1534 * must be a valid delimiter.
1535 */
1536 if (*sep && *sep != ',') {
1537 memprintf(err, "invalid character '%c' after thread set specification: '%s'.", *sep, set);
1538 return -1;
1539 }
1540 }
1541
1542 /* store values */
1543 if (ts) {
1544 if (is_rel) {
1545 /* group-relative thread numbers */
Willy Tarreau97da9422023-03-01 11:24:29 +01001546 ts->grps |= 1UL << (tg - 1);
Willy Tarreaubef43df2023-01-31 19:27:48 +01001547
1548 if (max >= min) {
1549 for (v = min; v <= max; v++)
Willy Tarreauf91ab7a2023-02-06 18:01:50 +01001550 ts->rel[tg - 1] |= 1UL << (v - 1);
Willy Tarreaubef43df2023-01-31 19:27:48 +01001551 } else {
1552 memset(&ts->rel[tg - 1],
1553 (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */,
1554 sizeof(ts->rel[tg - 1]));
1555 }
1556 } else {
1557 /* absolute thread numbers */
1558 if (max >= min) {
1559 for (v = min; v <= max; v++)
Willy Tarreauf91ab7a2023-02-06 18:01:50 +01001560 ts->abs[(v - 1) / LONGBITS] |= 1UL << ((v - 1) % LONGBITS);
Willy Tarreaubef43df2023-01-31 19:27:48 +01001561 } else {
1562 memset(&ts->abs,
1563 (max == 0) ? 0xff /* all */ : (max == -1) ? 0x55 /* odd */: 0xaa /* even */,
1564 sizeof(ts->abs));
1565 }
1566 }
1567 }
1568
1569 set = *sep ? sep + 1 : sep;
1570 tg = 0;
1571 }
1572 return 0;
Willy Tarreau627def92021-09-29 18:59:47 +02001573}
1574
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001575/* Parse the "nbthread" global directive, which takes an integer argument that
1576 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001577 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001578static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
1579 const struct proxy *defpx, const char *file, int line,
1580 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001581{
1582 long nbthread;
1583 char *errptr;
1584
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001585 if (too_many_args(1, args, err, NULL))
1586 return -1;
1587
Christopher Faulet55343342022-11-18 15:52:58 +01001588 if (non_global_section_parsed == 1) {
1589 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]);
1590 return -1;
1591 }
1592
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001593 nbthread = strtol(args[1], &errptr, 10);
1594 if (!*args[1] || *errptr) {
1595 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1596 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001597 }
1598
1599#ifndef USE_THREAD
1600 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001601 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]);
1602 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001603 }
1604#else
1605 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001606 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1607 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001608 }
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001609#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001610
1611 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001612 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1613 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001614
1615 global.nbthread = nbthread;
1616 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001617}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001618
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001619/* Parse the "thread-group" global directive, which takes an integer argument
1620 * that designates a thread group, and a list of threads to put into that group.
1621 */
1622static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx,
1623 const struct proxy *defpx, const char *file, int line,
1624 char **err)
1625{
1626 char *errptr;
1627 long tnum, tend, tgroup;
1628 int arg, tot;
1629
Christopher Faulet55343342022-11-18 15:52:58 +01001630 if (non_global_section_parsed == 1) {
1631 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]);
1632 return -1;
1633 }
1634
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001635 tgroup = strtol(args[1], &errptr, 10);
1636 if (!*args[1] || *errptr) {
1637 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1638 return -1;
1639 }
1640
1641 if (tgroup < 1 || tgroup > MAX_TGROUPS) {
1642 memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup);
1643 return -1;
1644 }
1645
1646 /* look for a preliminary definition of any thread pointing to this
1647 * group, and remove them.
1648 */
1649 if (ha_tgroup_info[tgroup-1].count) {
1650 ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n",
1651 file, line, args[0], tgroup);
1652
1653 for (tnum = ha_tgroup_info[tgroup-1].base;
1654 tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count;
1655 tnum++) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001656 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001657 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001658 ha_thread_info[tnum-1].tgid = 0;
Willy Tarreau03f9b352022-06-27 16:02:24 +02001659 ha_thread_info[tnum-1].tg_ctx = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001660 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001661 }
1662 ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0;
1663 }
1664
1665 tot = 0;
1666 for (arg = 2; args[arg] && *args[arg]; arg++) {
1667 tend = tnum = strtol(args[arg], &errptr, 10);
1668
1669 if (*errptr == '-')
1670 tend = strtol(errptr + 1, &errptr, 10);
1671
1672 if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) {
1673 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);
1674 return -1;
1675 }
1676
1677 for(; tnum <= tend; tnum++) {
1678 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
1679 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n",
1680 file, line, args[0], tgroup, tnum);
1681 } else if (ha_thread_info[tnum-1].tg) {
1682 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n",
1683 file, line, args[0], tgroup, tnum,
1684 (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1));
1685 }
1686
1687 if (!ha_tgroup_info[tgroup-1].count) {
1688 ha_tgroup_info[tgroup-1].base = tnum-1;
1689 ha_tgroup_info[tgroup-1].count = 1;
1690 }
1691 else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) {
1692 ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base;
1693 }
1694 else if (tnum < ha_tgroup_info[tgroup-1].base) {
1695 ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1;
1696 ha_tgroup_info[tgroup-1].base = tnum - 1;
1697 }
1698
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001699 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001700 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001701 ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1];
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001702 tot++;
1703 }
1704 }
1705
1706 if (ha_tgroup_info[tgroup-1].count > tot) {
1707 memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup);
1708 return -1;
1709 }
1710
1711 if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) {
1712 memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP);
1713 return -1;
1714 }
1715
1716 return 0;
1717}
1718
Willy Tarreauc33b9692021-09-22 12:07:23 +02001719/* Parse the "thread-groups" global directive, which takes an integer argument
1720 * that contains the desired number of thread groups.
1721 */
1722static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx,
1723 const struct proxy *defpx, const char *file, int line,
1724 char **err)
1725{
1726 long nbtgroups;
1727 char *errptr;
1728
1729 if (too_many_args(1, args, err, NULL))
1730 return -1;
Christopher Faulet55343342022-11-18 15:52:58 +01001731
1732 if (non_global_section_parsed == 1) {
1733 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]);
1734 return -1;
1735 }
Willy Tarreauc33b9692021-09-22 12:07:23 +02001736
1737 nbtgroups = strtol(args[1], &errptr, 10);
1738 if (!*args[1] || *errptr) {
1739 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1740 return -1;
1741 }
1742
1743#ifndef USE_THREAD
1744 if (nbtgroups != 1) {
1745 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]);
1746 return -1;
1747 }
1748#else
1749 if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) {
1750 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups);
1751 return -1;
1752 }
1753#endif
1754
1755 HA_DIAG_WARNING_COND(global.nbtgroups,
1756 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1757 file, line, args[0]);
1758
1759 global.nbtgroups = nbtgroups;
1760 return 0;
1761}
1762
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001763/* config keyword parsers */
1764static struct cfg_kw_list cfg_kws = {ILH, {
1765 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001766 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001767 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001768 { 0, NULL, NULL }
1769}};
1770
1771INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);