blob: cc18b76d2f8a9f493f259bd2b4e48a0c504c948d [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 Tarreau56c3b8b2021-04-10 17:28:18 +020066volatile unsigned long all_threads_mask __read_mostly = 1; // nbthread 1 assumed by default
Willy Tarreaucce203a2022-06-24 15:55:11 +020067volatile unsigned long all_tgroups_mask __read_mostly = 1; // nbtgroup 1 assumed by default
Willy Tarreaub90935c2021-09-30 08:00:11 +020068THREAD_LOCAL unsigned int tgid = 1; // thread ID starts at 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020069THREAD_LOCAL unsigned int tid = 0;
70THREAD_LOCAL unsigned long tid_bit = (1UL << 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
Christopher Fauleta9a9e9a2021-03-25 14:11:36 +010075 * point quits, excluding the current one. Thus an isolated thread may be safely
76 * marked as harmless. Given that we can wait for a long time, sched_yield() is
77 * used when available to offer the CPU resources to competing threads if
78 * needed.
Willy Tarreau60b639c2018-08-02 10:16:17 +020079 */
80void thread_harmless_till_end()
81{
Willy Tarreau03f9b352022-06-27 16:02:24 +020082 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
83 while (_HA_ATOMIC_LOAD(&tg_ctx->threads_want_rdv) & tg->threads_enabled & ~ti->ltid_bit) {
Willy Tarreau286363b2021-08-04 10:33:57 +020084 ha_thread_relax();
85 }
Willy Tarreau60b639c2018-08-02 10:16:17 +020086}
87
88/* Isolates the current thread : request the ability to work while all other
Willy Tarreauf519cfa2021-08-04 11:22:07 +020089 * threads are harmless, as defined by thread_harmless_now() (i.e. they're not
90 * going to touch any visible memory area). Only returns once all of them are
Willy Tarreau03f9b352022-06-27 16:02:24 +020091 * harmless, with the current thread's bit in &tg_ctx->threads_harmless cleared.
Willy Tarreauf519cfa2021-08-04 11:22:07 +020092 * Needs to be completed using thread_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +020093 */
94void thread_isolate()
95{
96 unsigned long old;
97
Willy Tarreau03f9b352022-06-27 16:02:24 +020098 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Olivier Houchardb23a61f2019-03-08 18:51:17 +010099 __ha_barrier_atomic_store();
Willy Tarreau03f9b352022-06-27 16:02:24 +0200100 _HA_ATOMIC_OR(&tg_ctx->threads_want_rdv, ti->ltid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200101
102 /* wait for all threads to become harmless */
Willy Tarreau03f9b352022-06-27 16:02:24 +0200103 old = _HA_ATOMIC_LOAD(&tg_ctx->threads_harmless);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200104 while (1) {
Willy Tarreau03f9b352022-06-27 16:02:24 +0200105 if (unlikely((old & tg->threads_enabled) != tg->threads_enabled))
106 old = _HA_ATOMIC_LOAD(&tg_ctx->threads_harmless);
107 else if (_HA_ATOMIC_CAS(&tg_ctx->threads_harmless, &old, old & ~ti->ltid_bit))
Willy Tarreau60b639c2018-08-02 10:16:17 +0200108 break;
109
Willy Tarreau38171da2019-05-17 16:33:13 +0200110 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +0200111 }
112 /* one thread gets released at a time here, with its harmess bit off.
113 * The loss of this bit makes the other one continue to spin while the
114 * thread is working alone.
115 */
116}
117
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200118/* Isolates the current thread : request the ability to work while all other
119 * threads are idle, as defined by thread_idle_now(). It only returns once
120 * all of them are both harmless and idle, with the current thread's bit in
Willy Tarreau03f9b352022-06-27 16:02:24 +0200121 * &tg_ctx->threads_harmless and idle_mask cleared. Needs to be completed using
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200122 * thread_release(). By doing so the thread also engages in being safe against
123 * any actions that other threads might be about to start under the same
124 * conditions. This specifically targets destruction of any internal structure,
125 * which implies that the current thread may not hold references to any object.
126 *
127 * Note that a concurrent thread_isolate() will usually win against
128 * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to
129 * get back to the poller or any other fully idle location, that will
130 * ultimately release this one.
131 */
132void thread_isolate_full()
133{
134 unsigned long old;
135
Willy Tarreau03f9b352022-06-27 16:02:24 +0200136 _HA_ATOMIC_OR(&tg_ctx->threads_idle, ti->ltid_bit);
137 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200138 __ha_barrier_atomic_store();
Willy Tarreau03f9b352022-06-27 16:02:24 +0200139 _HA_ATOMIC_OR(&tg_ctx->threads_want_rdv, ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200140
141 /* wait for all threads to become harmless */
Willy Tarreau03f9b352022-06-27 16:02:24 +0200142 old = _HA_ATOMIC_LOAD(&tg_ctx->threads_harmless);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200143 while (1) {
Willy Tarreau03f9b352022-06-27 16:02:24 +0200144 unsigned long idle = _HA_ATOMIC_LOAD(&tg_ctx->threads_idle);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200145
Willy Tarreau03f9b352022-06-27 16:02:24 +0200146 if (unlikely((old & tg->threads_enabled) != tg->threads_enabled))
147 old = _HA_ATOMIC_LOAD(&tg_ctx->threads_harmless);
148 else if ((idle & tg->threads_enabled) == tg->threads_enabled &&
149 _HA_ATOMIC_CAS(&tg_ctx->threads_harmless, &old, old & ~ti->ltid_bit))
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200150 break;
151
152 ha_thread_relax();
153 }
154
155 /* we're not idle anymore at this point. Other threads waiting on this
156 * condition will need to wait until out next pass to the poller, or
157 * our next call to thread_isolate_full().
158 */
Willy Tarreau03f9b352022-06-27 16:02:24 +0200159 _HA_ATOMIC_AND(&tg_ctx->threads_idle, ~ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200160}
161
Willy Tarreau60b639c2018-08-02 10:16:17 +0200162/* Cancels the effect of thread_isolate() by releasing the current thread's bit
Willy Tarreau03f9b352022-06-27 16:02:24 +0200163 * in &tg_ctx->threads_want_rdv. This immediately allows other threads to expect be
Willy Tarreauf519cfa2021-08-04 11:22:07 +0200164 * executed, though they will first have to wait for this thread to become
165 * harmless again (possibly by reaching the poller again).
Willy Tarreau60b639c2018-08-02 10:16:17 +0200166 */
167void thread_release()
168{
Willy Tarreau03f9b352022-06-27 16:02:24 +0200169 _HA_ATOMIC_AND(&tg_ctx->threads_want_rdv, ~ti->ltid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200170}
Christopher Faulet339fff82017-10-19 11:59:15 +0200171
Willy Tarreaud10385a2021-10-06 22:22:40 +0200172/* Sets up threads, signals and masks, and starts threads 2 and above.
173 * Does nothing when threads are disabled.
174 */
175void setup_extra_threads(void *(*handler)(void *))
176{
177 sigset_t blocked_sig, old_sig;
178 int i;
179
180 /* ensure the signals will be blocked in every thread */
181 sigfillset(&blocked_sig);
182 sigdelset(&blocked_sig, SIGPROF);
183 sigdelset(&blocked_sig, SIGBUS);
184 sigdelset(&blocked_sig, SIGFPE);
185 sigdelset(&blocked_sig, SIGILL);
186 sigdelset(&blocked_sig, SIGSEGV);
187 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
188
189 /* Create nbthread-1 thread. The first thread is the current process */
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200190 ha_pthread[0] = pthread_self();
Willy Tarreaud10385a2021-10-06 22:22:40 +0200191 for (i = 1; i < global.nbthread; i++)
Willy Tarreau43ab05b2021-09-28 09:43:11 +0200192 pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200193}
194
195/* waits for all threads to terminate. Does nothing when threads are
196 * disabled.
197 */
198void wait_for_threads_completion()
199{
200 int i;
201
202 /* Wait the end of other threads */
203 for (i = 1; i < global.nbthread; i++)
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200204 pthread_join(ha_pthread[i], NULL);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200205
206#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
207 show_lock_stats();
208#endif
209}
210
211/* Tries to set the current thread's CPU affinity according to the cpu_map */
212void set_thread_cpu_affinity()
213{
214#if defined(USE_CPU_AFFINITY)
215 /* no affinity setting for the master process */
216 if (master)
217 return;
218
219 /* Now the CPU affinity for all threads */
220 if (ha_cpuset_count(&cpu_map.proc))
221 ha_cpuset_and(&cpu_map.thread[tid], &cpu_map.proc);
222
223 if (ha_cpuset_count(&cpu_map.thread[tid])) {/* only do this if the thread has a THREAD map */
224# if defined(__APPLE__)
225 /* Note: this API is limited to the first 32/64 CPUs */
226 unsigned long set = cpu_map.thread[tid].cpuset;
227 int j;
228
229 while ((j = ffsl(set)) > 0) {
230 thread_affinity_policy_data_t cpu_set = { j - 1 };
231 thread_port_t mthread;
232
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200233 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200234 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
235 set &= ~(1UL << (j - 1));
236 }
237# else
238 struct hap_cpuset *set = &cpu_map.thread[tid];
239
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200240 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200241# endif
242 }
243#endif /* USE_CPU_AFFINITY */
244}
245
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200246/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
247 * since POSIX took great care of not specifying its representation, making it
248 * hard to export for post-mortem analysis. For this reason we copy it into a
249 * union and will use the smallest scalar type at least as large as its size,
250 * which will keep endianness and alignment for all regular sizes. As a last
251 * resort we end up with a long long ligned to the first bytes in memory, which
252 * will be endian-dependent if pthread_t is larger than a long long (not seen
253 * yet).
254 */
255unsigned long long ha_get_pthread_id(unsigned int thr)
256{
257 union {
258 pthread_t t;
259 unsigned long long ll;
260 unsigned int i;
261 unsigned short s;
262 unsigned char c;
263 } u = { 0 };
264
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200265 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200266
267 if (sizeof(u.t) <= sizeof(u.c))
268 return u.c;
269 else if (sizeof(u.t) <= sizeof(u.s))
270 return u.s;
271 else if (sizeof(u.t) <= sizeof(u.i))
272 return u.i;
273 return u.ll;
274}
275
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200276/* send signal <sig> to thread <thr> */
277void ha_tkill(unsigned int thr, int sig)
278{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200279 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200280}
281
282/* send signal <sig> to all threads. The calling thread is signaled last in
283 * order to allow all threads to synchronize in the handler.
284 */
285void ha_tkillall(int sig)
286{
287 unsigned int thr;
288
289 for (thr = 0; thr < global.nbthread; thr++) {
Willy Tarreauc6cf64b2022-06-27 16:23:44 +0200290 if (!(tg->threads_enabled & ha_thread_info[thr].ltid_bit))
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200291 continue;
292 if (thr == tid)
293 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200294 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200295 }
296 raise(sig);
297}
298
Willy Tarreauaa992762021-10-06 23:33:20 +0200299void ha_thread_relax(void)
300{
301#ifdef _POSIX_PRIORITY_SCHEDULING
302 sched_yield();
303#else
304 pl_cpu_relax();
305#endif
306}
307
Willy Tarreau3d184982020-10-18 10:20:59 +0200308/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100309void ha_spin_init(HA_SPINLOCK_T *l)
310{
311 HA_SPIN_INIT(l);
312}
313
Willy Tarreau3d184982020-10-18 10:20:59 +0200314/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100315void ha_rwlock_init(HA_RWLOCK_T *l)
316{
317 HA_RWLOCK_INIT(l);
318}
319
Willy Tarreau149ab772019-01-26 14:27:06 +0100320/* returns the number of CPUs the current process is enabled to run on */
321static int thread_cpus_enabled()
322{
323 int ret = 1;
324
325#ifdef USE_CPU_AFFINITY
326#if defined(__linux__) && defined(CPU_COUNT)
327 cpu_set_t mask;
328
329 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
330 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200331#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
332 cpuset_t cpuset;
333 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
334 sizeof(cpuset), &cpuset) == 0)
335 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000336#elif defined(__APPLE__)
337 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100338#endif
339#endif
340 ret = MAX(ret, 1);
341 ret = MIN(ret, MAX_THREADS);
342 return ret;
343}
344
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200345/* Returns 1 if the cpu set is currently restricted for the process else 0.
346 * Currently only implemented for the Linux platform.
347 */
348int thread_cpu_mask_forced()
349{
350#if defined(__linux__)
351 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
352 return cpus_avail != thread_cpus_enabled();
353#else
354 return 0;
355#endif
356}
357
Willy Tarreau407ef892021-10-05 18:39:27 +0200358/* Below come the lock-debugging functions */
359
360#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
361
362struct lock_stat lock_stats[LOCK_LABELS];
363
364/* this is only used below */
365static const char *lock_label(enum lock_label label)
366{
367 switch (label) {
368 case TASK_RQ_LOCK: return "TASK_RQ";
369 case TASK_WQ_LOCK: return "TASK_WQ";
370 case LISTENER_LOCK: return "LISTENER";
371 case PROXY_LOCK: return "PROXY";
372 case SERVER_LOCK: return "SERVER";
373 case LBPRM_LOCK: return "LBPRM";
374 case SIGNALS_LOCK: return "SIGNALS";
375 case STK_TABLE_LOCK: return "STK_TABLE";
376 case STK_SESS_LOCK: return "STK_SESS";
377 case APPLETS_LOCK: return "APPLETS";
378 case PEER_LOCK: return "PEER";
379 case SHCTX_LOCK: return "SHCTX";
380 case SSL_LOCK: return "SSL";
381 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
382 case PATREF_LOCK: return "PATREF";
383 case PATEXP_LOCK: return "PATEXP";
384 case VARS_LOCK: return "VARS";
385 case COMP_POOL_LOCK: return "COMP_POOL";
386 case LUA_LOCK: return "LUA";
387 case NOTIF_LOCK: return "NOTIF";
388 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
389 case DNS_LOCK: return "DNS";
390 case PID_LIST_LOCK: return "PID_LIST";
391 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
392 case PIPES_LOCK: return "PIPES";
393 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
394 case AUTH_LOCK: return "AUTH";
395 case LOGSRV_LOCK: return "LOGSRV";
396 case DICT_LOCK: return "DICT";
397 case PROTO_LOCK: return "PROTO";
398 case QUEUE_LOCK: return "QUEUE";
399 case CKCH_LOCK: return "CKCH";
400 case SNI_LOCK: return "SNI";
401 case SSL_SERVER_LOCK: return "SSL_SERVER";
402 case SFT_LOCK: return "SFT";
403 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
404 case QUIC_LOCK: return "QUIC";
405 case OTHER_LOCK: return "OTHER";
406 case DEBUG1_LOCK: return "DEBUG1";
407 case DEBUG2_LOCK: return "DEBUG2";
408 case DEBUG3_LOCK: return "DEBUG3";
409 case DEBUG4_LOCK: return "DEBUG4";
410 case DEBUG5_LOCK: return "DEBUG5";
411 case LOCK_LABELS: break; /* keep compiler happy */
412 };
413 /* only way to come here is consecutive to an internal bug */
414 abort();
415}
416
417void show_lock_stats()
418{
419 int lbl;
420
421 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
422 if (!lock_stats[lbl].num_write_locked &&
423 !lock_stats[lbl].num_seek_locked &&
424 !lock_stats[lbl].num_read_locked) {
425 fprintf(stderr,
426 "Stats about Lock %s: not used\n",
427 lock_label(lbl));
428 continue;
429 }
430
431 fprintf(stderr,
432 "Stats about Lock %s: \n",
433 lock_label(lbl));
434
435 if (lock_stats[lbl].num_write_locked)
436 fprintf(stderr,
437 "\t # write lock : %lu\n"
438 "\t # write unlock: %lu (%ld)\n"
439 "\t # wait time for write : %.3f msec\n"
440 "\t # wait time for write/lock: %.3f nsec\n",
441 lock_stats[lbl].num_write_locked,
442 lock_stats[lbl].num_write_unlocked,
443 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
444 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
445 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
446
447 if (lock_stats[lbl].num_seek_locked)
448 fprintf(stderr,
449 "\t # seek lock : %lu\n"
450 "\t # seek unlock : %lu (%ld)\n"
451 "\t # wait time for seek : %.3f msec\n"
452 "\t # wait time for seek/lock : %.3f nsec\n",
453 lock_stats[lbl].num_seek_locked,
454 lock_stats[lbl].num_seek_unlocked,
455 lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked,
456 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
457 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
458
459 if (lock_stats[lbl].num_read_locked)
460 fprintf(stderr,
461 "\t # read lock : %lu\n"
462 "\t # read unlock : %lu (%ld)\n"
463 "\t # wait time for read : %.3f msec\n"
464 "\t # wait time for read/lock : %.3f nsec\n",
465 lock_stats[lbl].num_read_locked,
466 lock_stats[lbl].num_read_unlocked,
467 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
468 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
469 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
470 }
471}
472
Willy Tarreau407ef892021-10-05 18:39:27 +0200473void __ha_rwlock_init(struct ha_rwlock *l)
474{
475 memset(l, 0, sizeof(struct ha_rwlock));
476 __RWLOCK_INIT(&l->lock);
477}
478
479void __ha_rwlock_destroy(struct ha_rwlock *l)
480{
481 __RWLOCK_DESTROY(&l->lock);
482 memset(l, 0, sizeof(struct ha_rwlock));
483}
484
485
486void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
487 const char *func, const char *file, int line)
488{
489 uint64_t start_time;
490
491 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
492 abort();
493
494 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
495
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200496 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200497 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200498 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200499
500 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
501
502 l->info.cur_writer = tid_bit;
503 l->info.last_location.function = func;
504 l->info.last_location.file = file;
505 l->info.last_location.line = line;
506
507 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
508}
509
510int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
511 const char *func, const char *file, int line)
512{
513 uint64_t start_time;
514 int r;
515
516 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
517 abort();
518
519 /* We set waiting writer because trywrlock could wait for readers to quit */
520 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
521
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200522 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200523 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200524 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200525 if (unlikely(r)) {
526 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
527 return r;
528 }
529 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
530
531 l->info.cur_writer = tid_bit;
532 l->info.last_location.function = func;
533 l->info.last_location.file = file;
534 l->info.last_location.line = line;
535
536 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
537
538 return 0;
539}
540
541void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
542 const char *func, const char *file, int line)
543{
544 if (unlikely(!(l->info.cur_writer & tid_bit))) {
545 /* the thread is not owning the lock for write */
546 abort();
547 }
548
549 l->info.cur_writer = 0;
550 l->info.last_location.function = func;
551 l->info.last_location.file = file;
552 l->info.last_location.line = line;
553
554 __RWLOCK_WRUNLOCK(&l->lock);
555
556 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
557}
558
559void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
560{
561 uint64_t start_time;
562
563 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
564 abort();
565
566 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
567
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200568 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200569 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200570 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200571 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
572
573 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
574
575 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
576}
577
578int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
579{
580 int r;
581
582 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
583 abort();
584
585 /* try read should never wait */
586 r = __RWLOCK_TRYRDLOCK(&l->lock);
587 if (unlikely(r))
588 return r;
589 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
590
591 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
592
593 return 0;
594}
595
596void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
597{
598 if (unlikely(!(l->info.cur_readers & tid_bit))) {
599 /* the thread is not owning the lock for read */
600 abort();
601 }
602
603 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
604
605 __RWLOCK_RDUNLOCK(&l->lock);
606
607 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
608}
609
610void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
611 const char *func, const char *file, int line)
612{
613 uint64_t start_time;
614
615 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
616 abort();
617
618 if (!(l->info.cur_writer & tid_bit))
619 abort();
620
621 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
622
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200623 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200624 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200625 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200626
627 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
628
629 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
630 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
631 l->info.last_location.function = func;
632 l->info.last_location.file = file;
633 l->info.last_location.line = line;
634
635 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
636}
637
638void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
639 const char *func, const char *file, int line)
640{
641 uint64_t start_time;
642
643 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
644 abort();
645
646 if (!(l->info.cur_writer & tid_bit))
647 abort();
648
649 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
650
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200651 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200652 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200653 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200654
655 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
656
657 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
658 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
659 l->info.last_location.function = func;
660 l->info.last_location.file = file;
661 l->info.last_location.line = line;
662
663 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
664}
665
666void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
667 const char *func, const char *file, int line)
668{
669 uint64_t start_time;
670
671 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
672 abort();
673
674 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
675
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200676 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200677 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200678 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200679
680 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
681
682 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
683 l->info.last_location.function = func;
684 l->info.last_location.file = file;
685 l->info.last_location.line = line;
686
687 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
688}
689
690void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
691 const char *func, const char *file, int line)
692{
693 uint64_t start_time;
694
695 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
696 abort();
697
698 if (!(l->info.cur_seeker & tid_bit))
699 abort();
700
701 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
702
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200703 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200704 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200705 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200706
707 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
708
709 HA_ATOMIC_OR(&l->info.cur_writer, tid_bit);
710 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
711 l->info.last_location.function = func;
712 l->info.last_location.file = file;
713 l->info.last_location.line = line;
714
715 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
716}
717
718void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
719 const char *func, const char *file, int line)
720{
721 uint64_t start_time;
722
723 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
724 abort();
725
726 if (!(l->info.cur_seeker & tid_bit))
727 abort();
728
729 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
730
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200731 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200732 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200733 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200734
735 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
736
737 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
738 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
739 l->info.last_location.function = func;
740 l->info.last_location.file = file;
741 l->info.last_location.line = line;
742
743 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
744}
745
746void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
747 const char *func, const char *file, int line)
748{
749 if (!(l->info.cur_seeker & tid_bit))
750 abort();
751
752 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
753 l->info.last_location.function = func;
754 l->info.last_location.file = file;
755 l->info.last_location.line = line;
756
757 __RWLOCK_SKUNLOCK(&l->lock);
758
759 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
760}
761
762int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
763 const char *func, const char *file, int line)
764{
765 uint64_t start_time;
766 int r;
767
768 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
769 abort();
770
771 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
772
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200773 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200774 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200775 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200776
777 if (likely(!r)) {
778 /* got the lock ! */
779 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
780 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
781 l->info.last_location.function = func;
782 l->info.last_location.file = file;
783 l->info.last_location.line = line;
784 }
785
786 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
787 return r;
788}
789
790int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
791 const char *func, const char *file, int line)
792{
793 uint64_t start_time;
794 int r;
795
796 if ((l->info.cur_writer | l->info.cur_seeker) & tid_bit)
797 abort();
798
799 if (!(l->info.cur_readers & tid_bit))
800 abort();
801
802 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
803
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200804 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200805 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200806 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200807
808 if (likely(!r)) {
809 /* got the lock ! */
810 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
811 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
812 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
813 l->info.last_location.function = func;
814 l->info.last_location.file = file;
815 l->info.last_location.line = line;
816 }
817
818 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
819 return r;
820}
821
822void __spin_init(struct ha_spinlock *l)
823{
824 memset(l, 0, sizeof(struct ha_spinlock));
825 __SPIN_INIT(&l->lock);
826}
827
828void __spin_destroy(struct ha_spinlock *l)
829{
830 __SPIN_DESTROY(&l->lock);
831 memset(l, 0, sizeof(struct ha_spinlock));
832}
833
834void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
835 const char *func, const char *file, int line)
836{
837 uint64_t start_time;
838
839 if (unlikely(l->info.owner & tid_bit)) {
840 /* the thread is already owning the lock */
841 abort();
842 }
843
844 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
845
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200846 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200847 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200848 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200849
850 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
851
852
853 l->info.owner = tid_bit;
854 l->info.last_location.function = func;
855 l->info.last_location.file = file;
856 l->info.last_location.line = line;
857
858 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
859}
860
861int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
862 const char *func, const char *file, int line)
863{
864 int r;
865
866 if (unlikely(l->info.owner & tid_bit)) {
867 /* the thread is already owning the lock */
868 abort();
869 }
870
871 /* try read should never wait */
872 r = __SPIN_TRYLOCK(&l->lock);
873 if (unlikely(r))
874 return r;
875 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
876
877 l->info.owner = tid_bit;
878 l->info.last_location.function = func;
879 l->info.last_location.file = file;
880 l->info.last_location.line = line;
881
882 return 0;
883}
884
885void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
886 const char *func, const char *file, int line)
887{
888 if (unlikely(!(l->info.owner & tid_bit))) {
889 /* the thread is not owning the lock */
890 abort();
891 }
892
893 l->info.owner = 0;
894 l->info.last_location.function = func;
895 l->info.last_location.file = file;
896 l->info.last_location.line = line;
897
898 __SPIN_UNLOCK(&l->lock);
899 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
900}
901
902#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
903
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200904/* Depending on the platform and how libpthread was built, pthread_exit() may
905 * involve some code in libgcc_s that would be loaded on exit for the first
906 * time, causing aborts if the process is chrooted. It's harmless bit very
907 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
908 * needed, so what we do here is that during early boot we create a dummy
909 * thread that immediately exits. This will lead to libgcc_s being loaded
910 * during boot on the platforms where it's required.
911 */
912static void *dummy_thread_function(void *data)
913{
914 pthread_exit(NULL);
915 return NULL;
916}
917
918static inline void preload_libgcc_s(void)
919{
920 pthread_t dummy_thread;
921 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
922 pthread_join(dummy_thread, NULL);
923}
924
Willy Tarreau3f567e42020-05-28 15:29:19 +0200925static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200926{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100927 char *ptr = NULL;
928
929 if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) {
930 ha_alert("MAX_THREADS value must be between 1 and %d inclusive; "
931 "HAProxy was built with value %d, please fix it and rebuild.\n",
932 LONGBITS, MAX_THREADS);
933 exit(1);
934 }
Willy Tarreau149ab772019-01-26 14:27:06 +0100935
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200936 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +0200937
Willy Tarreau149ab772019-01-26 14:27:06 +0100938 thread_cpus_enabled_at_boot = thread_cpus_enabled();
939
940 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
941 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100942 hap_register_build_opts(ptr, 1);
943
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200944#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
945 memset(lock_stats, 0, sizeof(lock_stats));
946#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200947}
Willy Tarreau8ead1d02022-04-25 19:23:17 +0200948INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200949
Willy Tarreau8459f252018-12-15 16:48:14 +0100950#else
951
Willy Tarreauaa992762021-10-06 23:33:20 +0200952/* send signal <sig> to thread <thr> (send to process in fact) */
953void ha_tkill(unsigned int thr, int sig)
954{
955 raise(sig);
956}
957
958/* send signal <sig> to all threads (send to process in fact) */
959void ha_tkillall(int sig)
960{
961 raise(sig);
962}
963
964void ha_thread_relax(void)
965{
966#ifdef _POSIX_PRIORITY_SCHEDULING
967 sched_yield();
968#endif
969}
970
Willy Tarreau8459f252018-12-15 16:48:14 +0100971REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
972
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200973#endif // USE_THREAD
974
975
Willy Tarreaue6806eb2021-09-27 10:10:26 +0200976/* scans the configured thread mapping and establishes the final one. Returns <0
977 * on failure, >=0 on success.
978 */
979int thread_map_to_groups()
980{
981 int t, g, ut, ug;
982 int q, r;
Willy Tarreaucce203a2022-06-24 15:55:11 +0200983 ulong m __maybe_unused;
Willy Tarreaue6806eb2021-09-27 10:10:26 +0200984
985 ut = ug = 0; // unassigned threads & groups
986
987 for (t = 0; t < global.nbthread; t++) {
988 if (!ha_thread_info[t].tg)
989 ut++;
990 }
991
992 for (g = 0; g < global.nbtgroups; g++) {
993 if (!ha_tgroup_info[g].count)
994 ug++;
Willy Tarreau60fe4a92022-06-28 17:48:07 +0200995 ha_tgroup_info[g].tgid_bit = 1UL << g;
Willy Tarreaue6806eb2021-09-27 10:10:26 +0200996 }
997
998 if (ug > ut) {
999 ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut);
1000 return -1;
1001 }
1002
1003 /* look for first unassigned thread */
1004 for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++)
1005 ;
1006
1007 /* assign threads to empty groups */
1008 for (g = 0; ug && ut; ) {
1009 /* due to sparse thread assignment we can end up with more threads
1010 * per group on last assigned groups than former ones, so we must
1011 * always try to pack the maximum remaining ones together first.
1012 */
1013 q = ut / ug;
1014 r = ut % ug;
1015 if ((q + !!r) > MAX_THREADS_PER_GROUP) {
1016 ha_alert("Too many remaining unassigned threads (%d) for thread groups (%d). Please increase thread-groups or make sure to keep thread numbers contiguous\n", ug, ut);
1017 return -1;
1018 }
1019
1020 /* thread <t> is the next unassigned one. Let's look for next
1021 * unassigned group, we know there are some left
1022 */
1023 while (ut >= ug && ha_tgroup_info[g].count)
1024 g++;
1025
1026 /* group g is unassigned, try to fill it with consecutive threads */
1027 while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r &&
1028 (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) {
1029
1030 if (!ha_tgroup_info[g].count) {
1031 /* assign new group */
1032 ha_tgroup_info[g].base = t;
1033 ug--;
1034 }
1035
1036 ha_tgroup_info[g].count++;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001037 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001038 ha_thread_info[t].tg = &ha_tgroup_info[g];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001039 ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g];
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001040
1041 ut--;
1042 /* switch to next unassigned thread */
1043 while (++t < global.nbthread && ha_thread_info[t].tg)
1044 ;
1045 }
1046 }
1047
1048 if (ut) {
1049 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);
1050 return -1;
1051 }
1052
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001053 for (t = 0; t < global.nbthread; t++) {
1054 ha_thread_info[t].tid = t;
1055 ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base;
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001056 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1057 }
1058
Willy Tarreaucce203a2022-06-24 15:55:11 +02001059 m = 0;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001060 for (g = 0; g < global.nbtgroups; g++) {
1061 ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count);
Willy Tarreaucce203a2022-06-24 15:55:11 +02001062 if (!ha_tgroup_info[g].count)
1063 continue;
1064 m |= 1UL << g;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001065
1066 }
1067
Willy Tarreaucce203a2022-06-24 15:55:11 +02001068#ifdef USE_THREAD
1069 all_tgroups_mask = m;
1070#endif
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001071 return 0;
1072}
1073
Willy Tarreau627def92021-09-29 18:59:47 +02001074/* converts a configuration thread group+mask to a global group+mask depending on
1075 * the configured thread group id. This is essentially for use with the "thread"
1076 * directive on "bind" lines, where "thread 2/1-3" might be turned to "4-6" for
1077 * the global ID. It cannot be used before the thread mapping above was completed
1078 * and the thread group number configured. Possible options:
1079 * - igid == 0: imask represents global IDs. We have to check that all
1080 * configured threads in the mask belong to the same group. If imask is zero
1081 * it means everything, so for now we only support this with a single group.
1082 * - igid > 0, imask = 0: convert local values to global values for this thread
1083 * - igid > 0, imask > 0: convert local values to global values
1084 *
1085 * Returns <0 on failure, >=0 on success.
1086 */
1087int thread_resolve_group_mask(uint igid, ulong imask, uint *ogid, ulong *omask, char **err)
1088{
1089 ulong mask;
1090 uint t;
1091
1092 if (igid == 0) {
1093 /* unspecified group, IDs are global */
1094 if (!imask) {
1095 /* all threads of all groups */
1096 if (global.nbtgroups > 1) {
1097 memprintf(err, "'thread' directive spans multiple groups");
1098 return -1;
1099 }
1100 mask = 0;
1101 *ogid = 1; // first and only group
1102 *omask = all_threads_mask;
1103 return 0;
1104 } else {
1105 /* some global threads */
1106 imask &= all_threads_mask;
1107 for (t = 0; t < global.nbthread; t++) {
1108 if (imask & (1UL << t)) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001109 if (ha_thread_info[t].tgid != igid) {
Willy Tarreau627def92021-09-29 18:59:47 +02001110 if (!igid)
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001111 igid = ha_thread_info[t].tgid;
Willy Tarreau627def92021-09-29 18:59:47 +02001112 else {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001113 memprintf(err, "'thread' directive spans multiple groups (at least %u and %u)", igid, ha_thread_info[t].tgid);
Willy Tarreau627def92021-09-29 18:59:47 +02001114 return -1;
1115 }
1116 }
1117 }
1118 }
1119
1120 if (!igid) {
1121 memprintf(err, "'thread' directive contains threads that belong to no group");
1122 return -1;
1123 }
1124
1125 /* we have a valid group, convert this to global thread IDs */
1126 *ogid = igid;
1127 *omask = imask << ha_tgroup_info[igid - 1].base;
1128 return 0;
1129 }
1130 } else {
1131 /* group was specified */
1132 if (igid > global.nbtgroups) {
1133 memprintf(err, "'thread' directive references non-existing thread group %u", igid);
1134 return -1;
1135 }
1136
1137 if (!imask) {
1138 /* all threads of this groups. Let's make a mask from their count and base. */
1139 *ogid = igid;
1140 mask = 1UL << (ha_tgroup_info[igid - 1].count - 1);
1141 mask |= mask - 1;
1142 *omask = mask << ha_tgroup_info[igid - 1].base;
1143 return 0;
1144 } else {
1145 /* some local threads. Keep only existing ones for this group */
1146
1147 mask = 1UL << (ha_tgroup_info[igid - 1].count - 1);
1148 mask |= mask - 1;
1149
1150 if (!(mask & imask)) {
1151 /* no intersection between the thread group's
1152 * threads and the bind line's.
1153 */
1154#ifdef THREAD_AUTO_ADJUST_GROUPS
1155 unsigned long new_mask = 0;
1156
1157 while (imask) {
1158 new_mask |= imask & mask;
1159 imask >>= ha_tgroup_info[igid - 1].count;
1160 }
1161 imask = new_mask;
1162#else
1163 memprintf(err, "'thread' directive only references threads not belonging to the group");
1164 return -1;
1165#endif
1166 }
1167
1168 mask &= imask;
1169 *omask = mask << ha_tgroup_info[igid - 1].base;
1170 *ogid = igid;
1171 return 0;
1172 }
1173 }
1174}
1175
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001176/* Parse the "nbthread" global directive, which takes an integer argument that
1177 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001178 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001179static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
1180 const struct proxy *defpx, const char *file, int line,
1181 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001182{
1183 long nbthread;
1184 char *errptr;
1185
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001186 if (too_many_args(1, args, err, NULL))
1187 return -1;
1188
1189 nbthread = strtol(args[1], &errptr, 10);
1190 if (!*args[1] || *errptr) {
1191 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1192 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001193 }
1194
1195#ifndef USE_THREAD
1196 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001197 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]);
1198 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001199 }
1200#else
1201 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001202 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1203 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001204 }
1205
Willy Tarreaufc647362019-02-02 17:05:03 +01001206 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001207#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001208
1209 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001210 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1211 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001212
1213 global.nbthread = nbthread;
1214 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001215}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001216
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001217/* Parse the "thread-group" global directive, which takes an integer argument
1218 * that designates a thread group, and a list of threads to put into that group.
1219 */
1220static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx,
1221 const struct proxy *defpx, const char *file, int line,
1222 char **err)
1223{
1224 char *errptr;
1225 long tnum, tend, tgroup;
1226 int arg, tot;
1227
1228 tgroup = strtol(args[1], &errptr, 10);
1229 if (!*args[1] || *errptr) {
1230 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1231 return -1;
1232 }
1233
1234 if (tgroup < 1 || tgroup > MAX_TGROUPS) {
1235 memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup);
1236 return -1;
1237 }
1238
1239 /* look for a preliminary definition of any thread pointing to this
1240 * group, and remove them.
1241 */
1242 if (ha_tgroup_info[tgroup-1].count) {
1243 ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n",
1244 file, line, args[0], tgroup);
1245
1246 for (tnum = ha_tgroup_info[tgroup-1].base;
1247 tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count;
1248 tnum++) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001249 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001250 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001251 ha_thread_info[tnum-1].tgid = 0;
Willy Tarreau03f9b352022-06-27 16:02:24 +02001252 ha_thread_info[tnum-1].tg_ctx = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001253 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001254 }
1255 ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0;
1256 }
1257
1258 tot = 0;
1259 for (arg = 2; args[arg] && *args[arg]; arg++) {
1260 tend = tnum = strtol(args[arg], &errptr, 10);
1261
1262 if (*errptr == '-')
1263 tend = strtol(errptr + 1, &errptr, 10);
1264
1265 if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) {
1266 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);
1267 return -1;
1268 }
1269
1270 for(; tnum <= tend; tnum++) {
1271 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
1272 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n",
1273 file, line, args[0], tgroup, tnum);
1274 } else if (ha_thread_info[tnum-1].tg) {
1275 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n",
1276 file, line, args[0], tgroup, tnum,
1277 (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1));
1278 }
1279
1280 if (!ha_tgroup_info[tgroup-1].count) {
1281 ha_tgroup_info[tgroup-1].base = tnum-1;
1282 ha_tgroup_info[tgroup-1].count = 1;
1283 }
1284 else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) {
1285 ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base;
1286 }
1287 else if (tnum < ha_tgroup_info[tgroup-1].base) {
1288 ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1;
1289 ha_tgroup_info[tgroup-1].base = tnum - 1;
1290 }
1291
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001292 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001293 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001294 ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1];
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001295 tot++;
1296 }
1297 }
1298
1299 if (ha_tgroup_info[tgroup-1].count > tot) {
1300 memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup);
1301 return -1;
1302 }
1303
1304 if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) {
1305 memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP);
1306 return -1;
1307 }
1308
1309 return 0;
1310}
1311
Willy Tarreauc33b9692021-09-22 12:07:23 +02001312/* Parse the "thread-groups" global directive, which takes an integer argument
1313 * that contains the desired number of thread groups.
1314 */
1315static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx,
1316 const struct proxy *defpx, const char *file, int line,
1317 char **err)
1318{
1319 long nbtgroups;
1320 char *errptr;
1321
1322 if (too_many_args(1, args, err, NULL))
1323 return -1;
1324
1325 nbtgroups = strtol(args[1], &errptr, 10);
1326 if (!*args[1] || *errptr) {
1327 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1328 return -1;
1329 }
1330
1331#ifndef USE_THREAD
1332 if (nbtgroups != 1) {
1333 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]);
1334 return -1;
1335 }
1336#else
1337 if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) {
1338 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups);
1339 return -1;
1340 }
1341#endif
1342
1343 HA_DIAG_WARNING_COND(global.nbtgroups,
1344 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1345 file, line, args[0]);
1346
1347 global.nbtgroups = nbtgroups;
1348 return 0;
1349}
1350
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001351/* config keyword parsers */
1352static struct cfg_kw_list cfg_kws = {ILH, {
1353 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001354 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001355 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001356 { 0, NULL, NULL }
1357}};
1358
1359INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);