blob: c5f06038f317f5d76bc4049ca61b6f77eb9a9c30 [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 Tarreau1a9c9222021-10-01 11:30:33 +020058struct thread_ctx ha_thread_ctx[MAX_THREADS] = { };
59THREAD_LOCAL struct thread_ctx *th_ctx = &ha_thread_ctx[0];
60
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020061#ifdef USE_THREAD
62
Willy Tarreau56c3b8b2021-04-10 17:28:18 +020063volatile unsigned long threads_want_rdv_mask __read_mostly = 0;
Willy Tarreau60b639c2018-08-02 10:16:17 +020064volatile unsigned long threads_harmless_mask = 0;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +020065volatile unsigned long threads_idle_mask = 0;
Willy Tarreau9a1f5732019-06-09 12:20:02 +020066volatile unsigned long threads_sync_mask = 0;
Willy Tarreau56c3b8b2021-04-10 17:28:18 +020067volatile unsigned long all_threads_mask __read_mostly = 1; // nbthread 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 Tarreau286363b2021-08-04 10:33:57 +020082 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
83 while (threads_want_rdv_mask & all_threads_mask & ~tid_bit) {
84 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
91 * harmless, with the current thread's bit in threads_harmless_mask cleared.
92 * Needs to be completed using thread_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +020093 */
94void thread_isolate()
95{
96 unsigned long old;
97
Olivier Houchardb23a61f2019-03-08 18:51:17 +010098 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
99 __ha_barrier_atomic_store();
100 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200101
102 /* wait for all threads to become harmless */
103 old = threads_harmless_mask;
104 while (1) {
105 if (unlikely((old & all_threads_mask) != all_threads_mask))
106 old = threads_harmless_mask;
Olivier Houchardb23a61f2019-03-08 18:51:17 +0100107 else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_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
121 * threads_harmless_mask and idle_mask cleared. Needs to be completed using
122 * 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
136 _HA_ATOMIC_OR(&threads_idle_mask, tid_bit);
137 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
138 __ha_barrier_atomic_store();
139 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
140
141 /* wait for all threads to become harmless */
142 old = threads_harmless_mask;
143 while (1) {
144 unsigned long idle = _HA_ATOMIC_LOAD(&threads_idle_mask);
145
146 if (unlikely((old & all_threads_mask) != all_threads_mask))
147 old = _HA_ATOMIC_LOAD(&threads_harmless_mask);
148 else if ((idle & all_threads_mask) == all_threads_mask &&
149 _HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
150 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 */
159 _HA_ATOMIC_AND(&threads_idle_mask, ~tid_bit);
160}
161
Willy Tarreau60b639c2018-08-02 10:16:17 +0200162/* Cancels the effect of thread_isolate() by releasing the current thread's bit
Willy Tarreauf519cfa2021-08-04 11:22:07 +0200163 * in threads_want_rdv_mask. This immediately allows other threads to expect be
164 * 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{
Olivier Houchardb23a61f2019-03-08 18:51:17 +0100169 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200170}
Christopher Faulet339fff82017-10-19 11:59:15 +0200171
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200172/* Cancels the effect of thread_isolate() by releasing the current thread's bit
173 * in threads_want_rdv_mask and by marking this thread as harmless until the
174 * last worker finishes. The difference with thread_release() is that this one
175 * will not leave the function before others are notified to do the same, so it
176 * guarantees that the current thread will not pass through a subsequent call
177 * to thread_isolate() before others finish.
178 */
179void thread_sync_release()
180{
181 _HA_ATOMIC_OR(&threads_sync_mask, tid_bit);
182 __ha_barrier_atomic_store();
183 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
184
185 while (threads_want_rdv_mask & all_threads_mask) {
186 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
187 while (threads_want_rdv_mask & all_threads_mask)
188 ha_thread_relax();
189 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
190 }
191
192 /* the current thread is not harmless anymore, thread_isolate()
193 * is forced to wait till all waiters finish.
194 */
195 _HA_ATOMIC_AND(&threads_sync_mask, ~tid_bit);
196 while (threads_sync_mask & all_threads_mask)
197 ha_thread_relax();
198}
199
Willy Tarreaud10385a2021-10-06 22:22:40 +0200200/* Sets up threads, signals and masks, and starts threads 2 and above.
201 * Does nothing when threads are disabled.
202 */
203void setup_extra_threads(void *(*handler)(void *))
204{
205 sigset_t blocked_sig, old_sig;
206 int i;
207
208 /* ensure the signals will be blocked in every thread */
209 sigfillset(&blocked_sig);
210 sigdelset(&blocked_sig, SIGPROF);
211 sigdelset(&blocked_sig, SIGBUS);
212 sigdelset(&blocked_sig, SIGFPE);
213 sigdelset(&blocked_sig, SIGILL);
214 sigdelset(&blocked_sig, SIGSEGV);
215 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
216
217 /* Create nbthread-1 thread. The first thread is the current process */
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200218 ha_pthread[0] = pthread_self();
Willy Tarreaud10385a2021-10-06 22:22:40 +0200219 for (i = 1; i < global.nbthread; i++)
Willy Tarreau43ab05b2021-09-28 09:43:11 +0200220 pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200221}
222
223/* waits for all threads to terminate. Does nothing when threads are
224 * disabled.
225 */
226void wait_for_threads_completion()
227{
228 int i;
229
230 /* Wait the end of other threads */
231 for (i = 1; i < global.nbthread; i++)
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200232 pthread_join(ha_pthread[i], NULL);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200233
234#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
235 show_lock_stats();
236#endif
237}
238
239/* Tries to set the current thread's CPU affinity according to the cpu_map */
240void set_thread_cpu_affinity()
241{
242#if defined(USE_CPU_AFFINITY)
243 /* no affinity setting for the master process */
244 if (master)
245 return;
246
247 /* Now the CPU affinity for all threads */
248 if (ha_cpuset_count(&cpu_map.proc))
249 ha_cpuset_and(&cpu_map.thread[tid], &cpu_map.proc);
250
251 if (ha_cpuset_count(&cpu_map.thread[tid])) {/* only do this if the thread has a THREAD map */
252# if defined(__APPLE__)
253 /* Note: this API is limited to the first 32/64 CPUs */
254 unsigned long set = cpu_map.thread[tid].cpuset;
255 int j;
256
257 while ((j = ffsl(set)) > 0) {
258 thread_affinity_policy_data_t cpu_set = { j - 1 };
259 thread_port_t mthread;
260
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200261 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200262 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
263 set &= ~(1UL << (j - 1));
264 }
265# else
266 struct hap_cpuset *set = &cpu_map.thread[tid];
267
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200268 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200269# endif
270 }
271#endif /* USE_CPU_AFFINITY */
272}
273
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200274/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
275 * since POSIX took great care of not specifying its representation, making it
276 * hard to export for post-mortem analysis. For this reason we copy it into a
277 * union and will use the smallest scalar type at least as large as its size,
278 * which will keep endianness and alignment for all regular sizes. As a last
279 * resort we end up with a long long ligned to the first bytes in memory, which
280 * will be endian-dependent if pthread_t is larger than a long long (not seen
281 * yet).
282 */
283unsigned long long ha_get_pthread_id(unsigned int thr)
284{
285 union {
286 pthread_t t;
287 unsigned long long ll;
288 unsigned int i;
289 unsigned short s;
290 unsigned char c;
291 } u = { 0 };
292
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200293 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200294
295 if (sizeof(u.t) <= sizeof(u.c))
296 return u.c;
297 else if (sizeof(u.t) <= sizeof(u.s))
298 return u.s;
299 else if (sizeof(u.t) <= sizeof(u.i))
300 return u.i;
301 return u.ll;
302}
303
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200304/* send signal <sig> to thread <thr> */
305void ha_tkill(unsigned int thr, int sig)
306{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200307 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200308}
309
310/* send signal <sig> to all threads. The calling thread is signaled last in
311 * order to allow all threads to synchronize in the handler.
312 */
313void ha_tkillall(int sig)
314{
315 unsigned int thr;
316
317 for (thr = 0; thr < global.nbthread; thr++) {
318 if (!(all_threads_mask & (1UL << thr)))
319 continue;
320 if (thr == tid)
321 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200322 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200323 }
324 raise(sig);
325}
326
Willy Tarreauaa992762021-10-06 23:33:20 +0200327void ha_thread_relax(void)
328{
329#ifdef _POSIX_PRIORITY_SCHEDULING
330 sched_yield();
331#else
332 pl_cpu_relax();
333#endif
334}
335
Willy Tarreau3d184982020-10-18 10:20:59 +0200336/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100337void ha_spin_init(HA_SPINLOCK_T *l)
338{
339 HA_SPIN_INIT(l);
340}
341
Willy Tarreau3d184982020-10-18 10:20:59 +0200342/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100343void ha_rwlock_init(HA_RWLOCK_T *l)
344{
345 HA_RWLOCK_INIT(l);
346}
347
Willy Tarreau149ab772019-01-26 14:27:06 +0100348/* returns the number of CPUs the current process is enabled to run on */
349static int thread_cpus_enabled()
350{
351 int ret = 1;
352
353#ifdef USE_CPU_AFFINITY
354#if defined(__linux__) && defined(CPU_COUNT)
355 cpu_set_t mask;
356
357 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
358 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200359#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
360 cpuset_t cpuset;
361 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
362 sizeof(cpuset), &cpuset) == 0)
363 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000364#elif defined(__APPLE__)
365 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100366#endif
367#endif
368 ret = MAX(ret, 1);
369 ret = MIN(ret, MAX_THREADS);
370 return ret;
371}
372
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200373/* Returns 1 if the cpu set is currently restricted for the process else 0.
374 * Currently only implemented for the Linux platform.
375 */
376int thread_cpu_mask_forced()
377{
378#if defined(__linux__)
379 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
380 return cpus_avail != thread_cpus_enabled();
381#else
382 return 0;
383#endif
384}
385
Willy Tarreau407ef892021-10-05 18:39:27 +0200386/* Below come the lock-debugging functions */
387
388#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
389
390struct lock_stat lock_stats[LOCK_LABELS];
391
392/* this is only used below */
393static const char *lock_label(enum lock_label label)
394{
395 switch (label) {
396 case TASK_RQ_LOCK: return "TASK_RQ";
397 case TASK_WQ_LOCK: return "TASK_WQ";
398 case LISTENER_LOCK: return "LISTENER";
399 case PROXY_LOCK: return "PROXY";
400 case SERVER_LOCK: return "SERVER";
401 case LBPRM_LOCK: return "LBPRM";
402 case SIGNALS_LOCK: return "SIGNALS";
403 case STK_TABLE_LOCK: return "STK_TABLE";
404 case STK_SESS_LOCK: return "STK_SESS";
405 case APPLETS_LOCK: return "APPLETS";
406 case PEER_LOCK: return "PEER";
407 case SHCTX_LOCK: return "SHCTX";
408 case SSL_LOCK: return "SSL";
409 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
410 case PATREF_LOCK: return "PATREF";
411 case PATEXP_LOCK: return "PATEXP";
412 case VARS_LOCK: return "VARS";
413 case COMP_POOL_LOCK: return "COMP_POOL";
414 case LUA_LOCK: return "LUA";
415 case NOTIF_LOCK: return "NOTIF";
416 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
417 case DNS_LOCK: return "DNS";
418 case PID_LIST_LOCK: return "PID_LIST";
419 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
420 case PIPES_LOCK: return "PIPES";
421 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
422 case AUTH_LOCK: return "AUTH";
423 case LOGSRV_LOCK: return "LOGSRV";
424 case DICT_LOCK: return "DICT";
425 case PROTO_LOCK: return "PROTO";
426 case QUEUE_LOCK: return "QUEUE";
427 case CKCH_LOCK: return "CKCH";
428 case SNI_LOCK: return "SNI";
429 case SSL_SERVER_LOCK: return "SSL_SERVER";
430 case SFT_LOCK: return "SFT";
431 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
432 case QUIC_LOCK: return "QUIC";
433 case OTHER_LOCK: return "OTHER";
434 case DEBUG1_LOCK: return "DEBUG1";
435 case DEBUG2_LOCK: return "DEBUG2";
436 case DEBUG3_LOCK: return "DEBUG3";
437 case DEBUG4_LOCK: return "DEBUG4";
438 case DEBUG5_LOCK: return "DEBUG5";
439 case LOCK_LABELS: break; /* keep compiler happy */
440 };
441 /* only way to come here is consecutive to an internal bug */
442 abort();
443}
444
445void show_lock_stats()
446{
447 int lbl;
448
449 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
450 if (!lock_stats[lbl].num_write_locked &&
451 !lock_stats[lbl].num_seek_locked &&
452 !lock_stats[lbl].num_read_locked) {
453 fprintf(stderr,
454 "Stats about Lock %s: not used\n",
455 lock_label(lbl));
456 continue;
457 }
458
459 fprintf(stderr,
460 "Stats about Lock %s: \n",
461 lock_label(lbl));
462
463 if (lock_stats[lbl].num_write_locked)
464 fprintf(stderr,
465 "\t # write lock : %lu\n"
466 "\t # write unlock: %lu (%ld)\n"
467 "\t # wait time for write : %.3f msec\n"
468 "\t # wait time for write/lock: %.3f nsec\n",
469 lock_stats[lbl].num_write_locked,
470 lock_stats[lbl].num_write_unlocked,
471 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
472 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
473 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
474
475 if (lock_stats[lbl].num_seek_locked)
476 fprintf(stderr,
477 "\t # seek lock : %lu\n"
478 "\t # seek unlock : %lu (%ld)\n"
479 "\t # wait time for seek : %.3f msec\n"
480 "\t # wait time for seek/lock : %.3f nsec\n",
481 lock_stats[lbl].num_seek_locked,
482 lock_stats[lbl].num_seek_unlocked,
483 lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked,
484 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
485 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
486
487 if (lock_stats[lbl].num_read_locked)
488 fprintf(stderr,
489 "\t # read lock : %lu\n"
490 "\t # read unlock : %lu (%ld)\n"
491 "\t # wait time for read : %.3f msec\n"
492 "\t # wait time for read/lock : %.3f nsec\n",
493 lock_stats[lbl].num_read_locked,
494 lock_stats[lbl].num_read_unlocked,
495 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
496 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
497 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
498 }
499}
500
Willy Tarreau407ef892021-10-05 18:39:27 +0200501void __ha_rwlock_init(struct ha_rwlock *l)
502{
503 memset(l, 0, sizeof(struct ha_rwlock));
504 __RWLOCK_INIT(&l->lock);
505}
506
507void __ha_rwlock_destroy(struct ha_rwlock *l)
508{
509 __RWLOCK_DESTROY(&l->lock);
510 memset(l, 0, sizeof(struct ha_rwlock));
511}
512
513
514void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
515 const char *func, const char *file, int line)
516{
517 uint64_t start_time;
518
519 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
520 abort();
521
522 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
523
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200524 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200525 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200526 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200527
528 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
529
530 l->info.cur_writer = tid_bit;
531 l->info.last_location.function = func;
532 l->info.last_location.file = file;
533 l->info.last_location.line = line;
534
535 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
536}
537
538int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
539 const char *func, const char *file, int line)
540{
541 uint64_t start_time;
542 int r;
543
544 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
545 abort();
546
547 /* We set waiting writer because trywrlock could wait for readers to quit */
548 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
549
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200550 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200551 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200552 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200553 if (unlikely(r)) {
554 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
555 return r;
556 }
557 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
558
559 l->info.cur_writer = tid_bit;
560 l->info.last_location.function = func;
561 l->info.last_location.file = file;
562 l->info.last_location.line = line;
563
564 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
565
566 return 0;
567}
568
569void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
570 const char *func, const char *file, int line)
571{
572 if (unlikely(!(l->info.cur_writer & tid_bit))) {
573 /* the thread is not owning the lock for write */
574 abort();
575 }
576
577 l->info.cur_writer = 0;
578 l->info.last_location.function = func;
579 l->info.last_location.file = file;
580 l->info.last_location.line = line;
581
582 __RWLOCK_WRUNLOCK(&l->lock);
583
584 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
585}
586
587void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
588{
589 uint64_t start_time;
590
591 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
592 abort();
593
594 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
595
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200596 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200597 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200598 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200599 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
600
601 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
602
603 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
604}
605
606int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
607{
608 int r;
609
610 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
611 abort();
612
613 /* try read should never wait */
614 r = __RWLOCK_TRYRDLOCK(&l->lock);
615 if (unlikely(r))
616 return r;
617 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
618
619 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
620
621 return 0;
622}
623
624void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
625{
626 if (unlikely(!(l->info.cur_readers & tid_bit))) {
627 /* the thread is not owning the lock for read */
628 abort();
629 }
630
631 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
632
633 __RWLOCK_RDUNLOCK(&l->lock);
634
635 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
636}
637
638void __ha_rwlock_wrtord(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_readers, 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_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200653 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200654
655 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
656
657 HA_ATOMIC_OR(&l->info.cur_readers, 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_readers, ~tid_bit);
664}
665
666void __ha_rwlock_wrtosk(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) & tid_bit)
672 abort();
673
674 if (!(l->info.cur_writer & tid_bit))
675 abort();
676
677 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
678
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200679 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200680 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200681 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200682
683 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
684
685 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
686 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
687 l->info.last_location.function = func;
688 l->info.last_location.file = file;
689 l->info.last_location.line = line;
690
691 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
692}
693
694void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
695 const char *func, const char *file, int line)
696{
697 uint64_t start_time;
698
699 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
700 abort();
701
702 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
703
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200704 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200705 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200706 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200707
708 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
709
710 HA_ATOMIC_OR(&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_seekers, ~tid_bit);
716}
717
718void __ha_rwlock_sktowr(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_writers, 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_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200733 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200734
735 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
736
737 HA_ATOMIC_OR(&l->info.cur_writer, 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_writers, ~tid_bit);
744}
745
746void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
747 const char *func, const char *file, int line)
748{
749 uint64_t start_time;
750
751 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
752 abort();
753
754 if (!(l->info.cur_seeker & tid_bit))
755 abort();
756
757 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
758
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200759 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200760 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200761 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200762
763 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
764
765 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
766 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
767 l->info.last_location.function = func;
768 l->info.last_location.file = file;
769 l->info.last_location.line = line;
770
771 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
772}
773
774void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
775 const char *func, const char *file, int line)
776{
777 if (!(l->info.cur_seeker & tid_bit))
778 abort();
779
780 HA_ATOMIC_AND(&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 __RWLOCK_SKUNLOCK(&l->lock);
786
787 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
788}
789
790int __ha_rwlock_trysklock(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_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
797 abort();
798
799 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
800
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200801 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200802 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200803 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200804
805 if (likely(!r)) {
806 /* got the lock ! */
807 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
808 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
809 l->info.last_location.function = func;
810 l->info.last_location.file = file;
811 l->info.last_location.line = line;
812 }
813
814 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
815 return r;
816}
817
818int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
819 const char *func, const char *file, int line)
820{
821 uint64_t start_time;
822 int r;
823
824 if ((l->info.cur_writer | l->info.cur_seeker) & tid_bit)
825 abort();
826
827 if (!(l->info.cur_readers & tid_bit))
828 abort();
829
830 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
831
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200832 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200833 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200834 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200835
836 if (likely(!r)) {
837 /* got the lock ! */
838 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
839 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
840 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
841 l->info.last_location.function = func;
842 l->info.last_location.file = file;
843 l->info.last_location.line = line;
844 }
845
846 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
847 return r;
848}
849
850void __spin_init(struct ha_spinlock *l)
851{
852 memset(l, 0, sizeof(struct ha_spinlock));
853 __SPIN_INIT(&l->lock);
854}
855
856void __spin_destroy(struct ha_spinlock *l)
857{
858 __SPIN_DESTROY(&l->lock);
859 memset(l, 0, sizeof(struct ha_spinlock));
860}
861
862void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
863 const char *func, const char *file, int line)
864{
865 uint64_t start_time;
866
867 if (unlikely(l->info.owner & tid_bit)) {
868 /* the thread is already owning the lock */
869 abort();
870 }
871
872 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
873
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200874 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200875 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200876 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200877
878 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
879
880
881 l->info.owner = tid_bit;
882 l->info.last_location.function = func;
883 l->info.last_location.file = file;
884 l->info.last_location.line = line;
885
886 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
887}
888
889int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
890 const char *func, const char *file, int line)
891{
892 int r;
893
894 if (unlikely(l->info.owner & tid_bit)) {
895 /* the thread is already owning the lock */
896 abort();
897 }
898
899 /* try read should never wait */
900 r = __SPIN_TRYLOCK(&l->lock);
901 if (unlikely(r))
902 return r;
903 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
904
905 l->info.owner = tid_bit;
906 l->info.last_location.function = func;
907 l->info.last_location.file = file;
908 l->info.last_location.line = line;
909
910 return 0;
911}
912
913void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
914 const char *func, const char *file, int line)
915{
916 if (unlikely(!(l->info.owner & tid_bit))) {
917 /* the thread is not owning the lock */
918 abort();
919 }
920
921 l->info.owner = 0;
922 l->info.last_location.function = func;
923 l->info.last_location.file = file;
924 l->info.last_location.line = line;
925
926 __SPIN_UNLOCK(&l->lock);
927 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
928}
929
930#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
931
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200932/* Depending on the platform and how libpthread was built, pthread_exit() may
933 * involve some code in libgcc_s that would be loaded on exit for the first
934 * time, causing aborts if the process is chrooted. It's harmless bit very
935 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
936 * needed, so what we do here is that during early boot we create a dummy
937 * thread that immediately exits. This will lead to libgcc_s being loaded
938 * during boot on the platforms where it's required.
939 */
940static void *dummy_thread_function(void *data)
941{
942 pthread_exit(NULL);
943 return NULL;
944}
945
946static inline void preload_libgcc_s(void)
947{
948 pthread_t dummy_thread;
949 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
950 pthread_join(dummy_thread, NULL);
951}
952
Willy Tarreau3f567e42020-05-28 15:29:19 +0200953static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200954{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100955 char *ptr = NULL;
956
957 if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) {
958 ha_alert("MAX_THREADS value must be between 1 and %d inclusive; "
959 "HAProxy was built with value %d, please fix it and rebuild.\n",
960 LONGBITS, MAX_THREADS);
961 exit(1);
962 }
Willy Tarreau149ab772019-01-26 14:27:06 +0100963
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200964 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +0200965
Willy Tarreau149ab772019-01-26 14:27:06 +0100966 thread_cpus_enabled_at_boot = thread_cpus_enabled();
967
968 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
969 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100970 hap_register_build_opts(ptr, 1);
971
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200972#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
973 memset(lock_stats, 0, sizeof(lock_stats));
974#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200975}
Willy Tarreau8ead1d02022-04-25 19:23:17 +0200976INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200977
Willy Tarreau8459f252018-12-15 16:48:14 +0100978#else
979
Willy Tarreauaa992762021-10-06 23:33:20 +0200980/* send signal <sig> to thread <thr> (send to process in fact) */
981void ha_tkill(unsigned int thr, int sig)
982{
983 raise(sig);
984}
985
986/* send signal <sig> to all threads (send to process in fact) */
987void ha_tkillall(int sig)
988{
989 raise(sig);
990}
991
992void ha_thread_relax(void)
993{
994#ifdef _POSIX_PRIORITY_SCHEDULING
995 sched_yield();
996#endif
997}
998
Willy Tarreau8459f252018-12-15 16:48:14 +0100999REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
1000
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001001#endif // USE_THREAD
1002
1003
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001004/* scans the configured thread mapping and establishes the final one. Returns <0
1005 * on failure, >=0 on success.
1006 */
1007int thread_map_to_groups()
1008{
1009 int t, g, ut, ug;
1010 int q, r;
1011
1012 ut = ug = 0; // unassigned threads & groups
1013
1014 for (t = 0; t < global.nbthread; t++) {
1015 if (!ha_thread_info[t].tg)
1016 ut++;
1017 }
1018
1019 for (g = 0; g < global.nbtgroups; g++) {
1020 if (!ha_tgroup_info[g].count)
1021 ug++;
Willy Tarreau6eee85f2021-09-28 08:50:02 +02001022 ha_tgroup_info[g].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001023 }
1024
1025 if (ug > ut) {
1026 ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut);
1027 return -1;
1028 }
1029
1030 /* look for first unassigned thread */
1031 for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++)
1032 ;
1033
1034 /* assign threads to empty groups */
1035 for (g = 0; ug && ut; ) {
1036 /* due to sparse thread assignment we can end up with more threads
1037 * per group on last assigned groups than former ones, so we must
1038 * always try to pack the maximum remaining ones together first.
1039 */
1040 q = ut / ug;
1041 r = ut % ug;
1042 if ((q + !!r) > MAX_THREADS_PER_GROUP) {
1043 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);
1044 return -1;
1045 }
1046
1047 /* thread <t> is the next unassigned one. Let's look for next
1048 * unassigned group, we know there are some left
1049 */
1050 while (ut >= ug && ha_tgroup_info[g].count)
1051 g++;
1052
1053 /* group g is unassigned, try to fill it with consecutive threads */
1054 while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r &&
1055 (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) {
1056
1057 if (!ha_tgroup_info[g].count) {
1058 /* assign new group */
1059 ha_tgroup_info[g].base = t;
1060 ug--;
1061 }
1062
1063 ha_tgroup_info[g].count++;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001064 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001065 ha_thread_info[t].tg = &ha_tgroup_info[g];
1066
1067 ut--;
1068 /* switch to next unassigned thread */
1069 while (++t < global.nbthread && ha_thread_info[t].tg)
1070 ;
1071 }
1072 }
1073
1074 if (ut) {
1075 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);
1076 return -1;
1077 }
1078
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001079 for (t = 0; t < global.nbthread; t++) {
1080 ha_thread_info[t].tid = t;
1081 ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base;
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001082 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1083 }
1084
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001085 return 0;
1086}
1087
Willy Tarreau627def92021-09-29 18:59:47 +02001088/* converts a configuration thread group+mask to a global group+mask depending on
1089 * the configured thread group id. This is essentially for use with the "thread"
1090 * directive on "bind" lines, where "thread 2/1-3" might be turned to "4-6" for
1091 * the global ID. It cannot be used before the thread mapping above was completed
1092 * and the thread group number configured. Possible options:
1093 * - igid == 0: imask represents global IDs. We have to check that all
1094 * configured threads in the mask belong to the same group. If imask is zero
1095 * it means everything, so for now we only support this with a single group.
1096 * - igid > 0, imask = 0: convert local values to global values for this thread
1097 * - igid > 0, imask > 0: convert local values to global values
1098 *
1099 * Returns <0 on failure, >=0 on success.
1100 */
1101int thread_resolve_group_mask(uint igid, ulong imask, uint *ogid, ulong *omask, char **err)
1102{
1103 ulong mask;
1104 uint t;
1105
1106 if (igid == 0) {
1107 /* unspecified group, IDs are global */
1108 if (!imask) {
1109 /* all threads of all groups */
1110 if (global.nbtgroups > 1) {
1111 memprintf(err, "'thread' directive spans multiple groups");
1112 return -1;
1113 }
1114 mask = 0;
1115 *ogid = 1; // first and only group
1116 *omask = all_threads_mask;
1117 return 0;
1118 } else {
1119 /* some global threads */
1120 imask &= all_threads_mask;
1121 for (t = 0; t < global.nbthread; t++) {
1122 if (imask & (1UL << t)) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001123 if (ha_thread_info[t].tgid != igid) {
Willy Tarreau627def92021-09-29 18:59:47 +02001124 if (!igid)
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001125 igid = ha_thread_info[t].tgid;
Willy Tarreau627def92021-09-29 18:59:47 +02001126 else {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001127 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 +02001128 return -1;
1129 }
1130 }
1131 }
1132 }
1133
1134 if (!igid) {
1135 memprintf(err, "'thread' directive contains threads that belong to no group");
1136 return -1;
1137 }
1138
1139 /* we have a valid group, convert this to global thread IDs */
1140 *ogid = igid;
1141 *omask = imask << ha_tgroup_info[igid - 1].base;
1142 return 0;
1143 }
1144 } else {
1145 /* group was specified */
1146 if (igid > global.nbtgroups) {
1147 memprintf(err, "'thread' directive references non-existing thread group %u", igid);
1148 return -1;
1149 }
1150
1151 if (!imask) {
1152 /* all threads of this groups. Let's make a mask from their count and base. */
1153 *ogid = igid;
1154 mask = 1UL << (ha_tgroup_info[igid - 1].count - 1);
1155 mask |= mask - 1;
1156 *omask = mask << ha_tgroup_info[igid - 1].base;
1157 return 0;
1158 } else {
1159 /* some local threads. Keep only existing ones for this group */
1160
1161 mask = 1UL << (ha_tgroup_info[igid - 1].count - 1);
1162 mask |= mask - 1;
1163
1164 if (!(mask & imask)) {
1165 /* no intersection between the thread group's
1166 * threads and the bind line's.
1167 */
1168#ifdef THREAD_AUTO_ADJUST_GROUPS
1169 unsigned long new_mask = 0;
1170
1171 while (imask) {
1172 new_mask |= imask & mask;
1173 imask >>= ha_tgroup_info[igid - 1].count;
1174 }
1175 imask = new_mask;
1176#else
1177 memprintf(err, "'thread' directive only references threads not belonging to the group");
1178 return -1;
1179#endif
1180 }
1181
1182 mask &= imask;
1183 *omask = mask << ha_tgroup_info[igid - 1].base;
1184 *ogid = igid;
1185 return 0;
1186 }
1187 }
1188}
1189
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001190/* Parse the "nbthread" global directive, which takes an integer argument that
1191 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001192 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001193static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
1194 const struct proxy *defpx, const char *file, int line,
1195 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001196{
1197 long nbthread;
1198 char *errptr;
1199
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001200 if (too_many_args(1, args, err, NULL))
1201 return -1;
1202
1203 nbthread = strtol(args[1], &errptr, 10);
1204 if (!*args[1] || *errptr) {
1205 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1206 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001207 }
1208
1209#ifndef USE_THREAD
1210 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001211 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]);
1212 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001213 }
1214#else
1215 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001216 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1217 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001218 }
1219
Willy Tarreaufc647362019-02-02 17:05:03 +01001220 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001221#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001222
1223 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001224 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1225 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001226
1227 global.nbthread = nbthread;
1228 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001229}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001230
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001231/* Parse the "thread-group" global directive, which takes an integer argument
1232 * that designates a thread group, and a list of threads to put into that group.
1233 */
1234static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx,
1235 const struct proxy *defpx, const char *file, int line,
1236 char **err)
1237{
1238 char *errptr;
1239 long tnum, tend, tgroup;
1240 int arg, tot;
1241
1242 tgroup = strtol(args[1], &errptr, 10);
1243 if (!*args[1] || *errptr) {
1244 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1245 return -1;
1246 }
1247
1248 if (tgroup < 1 || tgroup > MAX_TGROUPS) {
1249 memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup);
1250 return -1;
1251 }
1252
1253 /* look for a preliminary definition of any thread pointing to this
1254 * group, and remove them.
1255 */
1256 if (ha_tgroup_info[tgroup-1].count) {
1257 ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n",
1258 file, line, args[0], tgroup);
1259
1260 for (tnum = ha_tgroup_info[tgroup-1].base;
1261 tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count;
1262 tnum++) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001263 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001264 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001265 ha_thread_info[tnum-1].tgid = 0;
1266 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001267 }
1268 ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0;
1269 }
1270
1271 tot = 0;
1272 for (arg = 2; args[arg] && *args[arg]; arg++) {
1273 tend = tnum = strtol(args[arg], &errptr, 10);
1274
1275 if (*errptr == '-')
1276 tend = strtol(errptr + 1, &errptr, 10);
1277
1278 if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) {
1279 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);
1280 return -1;
1281 }
1282
1283 for(; tnum <= tend; tnum++) {
1284 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
1285 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n",
1286 file, line, args[0], tgroup, tnum);
1287 } else if (ha_thread_info[tnum-1].tg) {
1288 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n",
1289 file, line, args[0], tgroup, tnum,
1290 (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1));
1291 }
1292
1293 if (!ha_tgroup_info[tgroup-1].count) {
1294 ha_tgroup_info[tgroup-1].base = tnum-1;
1295 ha_tgroup_info[tgroup-1].count = 1;
1296 }
1297 else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) {
1298 ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base;
1299 }
1300 else if (tnum < ha_tgroup_info[tgroup-1].base) {
1301 ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1;
1302 ha_tgroup_info[tgroup-1].base = tnum - 1;
1303 }
1304
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001305 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001306 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
1307 tot++;
1308 }
1309 }
1310
1311 if (ha_tgroup_info[tgroup-1].count > tot) {
1312 memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup);
1313 return -1;
1314 }
1315
1316 if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) {
1317 memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP);
1318 return -1;
1319 }
1320
1321 return 0;
1322}
1323
Willy Tarreauc33b9692021-09-22 12:07:23 +02001324/* Parse the "thread-groups" global directive, which takes an integer argument
1325 * that contains the desired number of thread groups.
1326 */
1327static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx,
1328 const struct proxy *defpx, const char *file, int line,
1329 char **err)
1330{
1331 long nbtgroups;
1332 char *errptr;
1333
1334 if (too_many_args(1, args, err, NULL))
1335 return -1;
1336
1337 nbtgroups = strtol(args[1], &errptr, 10);
1338 if (!*args[1] || *errptr) {
1339 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1340 return -1;
1341 }
1342
1343#ifndef USE_THREAD
1344 if (nbtgroups != 1) {
1345 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]);
1346 return -1;
1347 }
1348#else
1349 if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) {
1350 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups);
1351 return -1;
1352 }
1353#endif
1354
1355 HA_DIAG_WARNING_COND(global.nbtgroups,
1356 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1357 file, line, args[0]);
1358
1359 global.nbtgroups = nbtgroups;
1360 return 0;
1361}
1362
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001363/* config keyword parsers */
1364static struct cfg_kw_list cfg_kws = {ILH, {
1365 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001366 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001367 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001368 { 0, NULL, NULL }
1369}};
1370
1371INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);