blob: 551f5e6d4f4fc58c4be1266bc73cea8182db7e68 [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 Tarreau598cf3f2022-07-01 15:08:37 +020068volatile unsigned int rdv_requests = 0; // total number of threads requesting RDV
69volatile unsigned int isolated_thread = ~0; // ID of the isolated thread, or ~0 when none
Willy Tarreaub90935c2021-09-30 08:00:11 +020070THREAD_LOCAL unsigned int tgid = 1; // thread ID starts at 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020071THREAD_LOCAL unsigned int tid = 0;
72THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
Willy Tarreau149ab772019-01-26 14:27:06 +010073int thread_cpus_enabled_at_boot = 1;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +020074static pthread_t ha_pthread[MAX_THREADS] = { };
Willy Tarreau0c026f42018-08-01 19:12:20 +020075
Willy Tarreau60b639c2018-08-02 10:16:17 +020076/* Marks the thread as harmless until the last thread using the rendez-vous
Willy Tarreau598cf3f2022-07-01 15:08:37 +020077 * point quits. Given that we can wait for a long time, sched_yield() is
Christopher Fauleta9a9e9a2021-03-25 14:11:36 +010078 * used when available to offer the CPU resources to competing threads if
79 * needed.
Willy Tarreau60b639c2018-08-02 10:16:17 +020080 */
81void thread_harmless_till_end()
82{
Willy Tarreau03f9b352022-06-27 16:02:24 +020083 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +020084 while (_HA_ATOMIC_LOAD(&rdv_requests) != 0) {
Willy Tarreau286363b2021-08-04 10:33:57 +020085 ha_thread_relax();
86 }
Willy Tarreau60b639c2018-08-02 10:16:17 +020087}
88
89/* Isolates the current thread : request the ability to work while all other
Willy Tarreauf519cfa2021-08-04 11:22:07 +020090 * threads are harmless, as defined by thread_harmless_now() (i.e. they're not
91 * going to touch any visible memory area). Only returns once all of them are
Willy Tarreau03f9b352022-06-27 16:02:24 +020092 * harmless, with the current thread's bit in &tg_ctx->threads_harmless cleared.
Willy Tarreauf519cfa2021-08-04 11:22:07 +020093 * Needs to be completed using thread_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +020094 */
95void thread_isolate()
96{
Willy Tarreau598cf3f2022-07-01 15:08:37 +020097 uint tgrp, thr;
Willy Tarreau60b639c2018-08-02 10:16:17 +020098
Willy Tarreau03f9b352022-06-27 16:02:24 +020099 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Olivier Houchardb23a61f2019-03-08 18:51:17 +0100100 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200101 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200102
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200103 /* wait for all threads to become harmless. They cannot change their
104 * mind once seen thanks to rdv_requests above, unless they pass in
105 * front of us.
106 */
Willy Tarreau60b639c2018-08-02 10:16:17 +0200107 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200108 for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) {
109 while ((_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless) &
110 ha_tgroup_info[tgrp].threads_enabled) != ha_tgroup_info[tgrp].threads_enabled)
111 ha_thread_relax();
112 }
Willy Tarreau60b639c2018-08-02 10:16:17 +0200113
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200114 /* Now we've seen all threads marked harmless, we can try to run
115 * by competing with other threads to win the race of the isolated
116 * thread. It eventually converges since winners will enventually
117 * relax their request and go back to wait for this to be over.
118 * Competing on this only after seeing all threads harmless limits
119 * the write contention.
120 */
121 thr = _HA_ATOMIC_LOAD(&isolated_thread);
122 if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
123 break; // we won!
Willy Tarreau38171da2019-05-17 16:33:13 +0200124 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +0200125 }
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200126
127 /* the thread is no longer harmless as it runs */
128 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
129
130 /* the thread is isolated until it calls thread_release() which will
131 * 1) reset isolated_thread to ~0;
132 * 2) decrement rdv_requests.
Willy Tarreau60b639c2018-08-02 10:16:17 +0200133 */
134}
135
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200136/* Isolates the current thread : request the ability to work while all other
137 * threads are idle, as defined by thread_idle_now(). It only returns once
138 * all of them are both harmless and idle, with the current thread's bit in
Willy Tarreau03f9b352022-06-27 16:02:24 +0200139 * &tg_ctx->threads_harmless and idle_mask cleared. Needs to be completed using
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200140 * thread_release(). By doing so the thread also engages in being safe against
141 * any actions that other threads might be about to start under the same
142 * conditions. This specifically targets destruction of any internal structure,
143 * which implies that the current thread may not hold references to any object.
144 *
145 * Note that a concurrent thread_isolate() will usually win against
146 * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to
147 * get back to the poller or any other fully idle location, that will
148 * ultimately release this one.
149 */
150void thread_isolate_full()
151{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200152 uint tgrp, thr;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200153
Willy Tarreau03f9b352022-06-27 16:02:24 +0200154 _HA_ATOMIC_OR(&tg_ctx->threads_idle, ti->ltid_bit);
155 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200156 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200157 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200158
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200159 /* wait for all threads to become harmless. They cannot change their
160 * mind once seen thanks to rdv_requests above, unless they pass in
161 * front of us.
162 */
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200163 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200164 for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) {
165 while ((_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless) &
166 _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_idle) &
167 ha_tgroup_info[tgrp].threads_enabled) != ha_tgroup_info[tgrp].threads_enabled)
168 ha_thread_relax();
169 }
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200170
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200171 /* Now we've seen all threads marked harmless and idle, we can
172 * try to run by competing with other threads to win the race
173 * of the isolated thread. It eventually converges since winners
174 * will enventually relax their request and go back to wait for
175 * this to be over. Competing on this only after seeing all
176 * threads harmless+idle limits the write contention.
177 */
178 thr = _HA_ATOMIC_LOAD(&isolated_thread);
179 if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
180 break; // we won!
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200181 ha_thread_relax();
182 }
183
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200184 /* we're not idle nor harmless anymore at this point. Other threads
185 * waiting on this condition will need to wait until out next pass to
186 * the poller, or our next call to thread_isolate_full().
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200187 */
Willy Tarreau03f9b352022-06-27 16:02:24 +0200188 _HA_ATOMIC_AND(&tg_ctx->threads_idle, ~ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200189 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200190}
191
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200192/* Cancels the effect of thread_isolate() by resetting the ID of the isolated
193 * thread and decrementing the number of RDV requesters. This immediately allows
194 * other threads to expect to be executed, though they will first have to wait
195 * for this thread to become harmless again (possibly by reaching the poller
196 * again).
Willy Tarreau60b639c2018-08-02 10:16:17 +0200197 */
198void thread_release()
199{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200200 HA_ATOMIC_STORE(&isolated_thread, ~0U);
201 HA_ATOMIC_DEC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200202}
Christopher Faulet339fff82017-10-19 11:59:15 +0200203
Willy Tarreaud10385a2021-10-06 22:22:40 +0200204/* Sets up threads, signals and masks, and starts threads 2 and above.
205 * Does nothing when threads are disabled.
206 */
207void setup_extra_threads(void *(*handler)(void *))
208{
209 sigset_t blocked_sig, old_sig;
210 int i;
211
212 /* ensure the signals will be blocked in every thread */
213 sigfillset(&blocked_sig);
214 sigdelset(&blocked_sig, SIGPROF);
215 sigdelset(&blocked_sig, SIGBUS);
216 sigdelset(&blocked_sig, SIGFPE);
217 sigdelset(&blocked_sig, SIGILL);
218 sigdelset(&blocked_sig, SIGSEGV);
219 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
220
221 /* Create nbthread-1 thread. The first thread is the current process */
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200222 ha_pthread[0] = pthread_self();
Willy Tarreaud10385a2021-10-06 22:22:40 +0200223 for (i = 1; i < global.nbthread; i++)
Willy Tarreau43ab05b2021-09-28 09:43:11 +0200224 pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200225}
226
227/* waits for all threads to terminate. Does nothing when threads are
228 * disabled.
229 */
230void wait_for_threads_completion()
231{
232 int i;
233
234 /* Wait the end of other threads */
235 for (i = 1; i < global.nbthread; i++)
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200236 pthread_join(ha_pthread[i], NULL);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200237
238#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
239 show_lock_stats();
240#endif
241}
242
243/* Tries to set the current thread's CPU affinity according to the cpu_map */
244void set_thread_cpu_affinity()
245{
246#if defined(USE_CPU_AFFINITY)
247 /* no affinity setting for the master process */
248 if (master)
249 return;
250
251 /* Now the CPU affinity for all threads */
252 if (ha_cpuset_count(&cpu_map.proc))
253 ha_cpuset_and(&cpu_map.thread[tid], &cpu_map.proc);
254
255 if (ha_cpuset_count(&cpu_map.thread[tid])) {/* only do this if the thread has a THREAD map */
256# if defined(__APPLE__)
257 /* Note: this API is limited to the first 32/64 CPUs */
258 unsigned long set = cpu_map.thread[tid].cpuset;
259 int j;
260
261 while ((j = ffsl(set)) > 0) {
262 thread_affinity_policy_data_t cpu_set = { j - 1 };
263 thread_port_t mthread;
264
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200265 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200266 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
267 set &= ~(1UL << (j - 1));
268 }
269# else
270 struct hap_cpuset *set = &cpu_map.thread[tid];
271
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200272 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200273# endif
274 }
275#endif /* USE_CPU_AFFINITY */
276}
277
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200278/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
279 * since POSIX took great care of not specifying its representation, making it
280 * hard to export for post-mortem analysis. For this reason we copy it into a
281 * union and will use the smallest scalar type at least as large as its size,
282 * which will keep endianness and alignment for all regular sizes. As a last
283 * resort we end up with a long long ligned to the first bytes in memory, which
284 * will be endian-dependent if pthread_t is larger than a long long (not seen
285 * yet).
286 */
287unsigned long long ha_get_pthread_id(unsigned int thr)
288{
289 union {
290 pthread_t t;
291 unsigned long long ll;
292 unsigned int i;
293 unsigned short s;
294 unsigned char c;
295 } u = { 0 };
296
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200297 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200298
299 if (sizeof(u.t) <= sizeof(u.c))
300 return u.c;
301 else if (sizeof(u.t) <= sizeof(u.s))
302 return u.s;
303 else if (sizeof(u.t) <= sizeof(u.i))
304 return u.i;
305 return u.ll;
306}
307
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200308/* send signal <sig> to thread <thr> */
309void ha_tkill(unsigned int thr, int sig)
310{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200311 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200312}
313
314/* send signal <sig> to all threads. The calling thread is signaled last in
315 * order to allow all threads to synchronize in the handler.
316 */
317void ha_tkillall(int sig)
318{
319 unsigned int thr;
320
321 for (thr = 0; thr < global.nbthread; thr++) {
Willy Tarreauf15c75a2022-07-15 08:27:56 +0200322 if (!(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit))
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200323 continue;
324 if (thr == tid)
325 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200326 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200327 }
328 raise(sig);
329}
330
Willy Tarreauaa992762021-10-06 23:33:20 +0200331void ha_thread_relax(void)
332{
333#ifdef _POSIX_PRIORITY_SCHEDULING
334 sched_yield();
335#else
336 pl_cpu_relax();
337#endif
338}
339
Willy Tarreau3d184982020-10-18 10:20:59 +0200340/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100341void ha_spin_init(HA_SPINLOCK_T *l)
342{
343 HA_SPIN_INIT(l);
344}
345
Willy Tarreau3d184982020-10-18 10:20:59 +0200346/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100347void ha_rwlock_init(HA_RWLOCK_T *l)
348{
349 HA_RWLOCK_INIT(l);
350}
351
Willy Tarreau149ab772019-01-26 14:27:06 +0100352/* returns the number of CPUs the current process is enabled to run on */
353static int thread_cpus_enabled()
354{
355 int ret = 1;
356
357#ifdef USE_CPU_AFFINITY
358#if defined(__linux__) && defined(CPU_COUNT)
359 cpu_set_t mask;
360
361 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
362 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200363#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
364 cpuset_t cpuset;
365 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
366 sizeof(cpuset), &cpuset) == 0)
367 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000368#elif defined(__APPLE__)
369 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100370#endif
371#endif
372 ret = MAX(ret, 1);
373 ret = MIN(ret, MAX_THREADS);
374 return ret;
375}
376
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200377/* Returns 1 if the cpu set is currently restricted for the process else 0.
378 * Currently only implemented for the Linux platform.
379 */
380int thread_cpu_mask_forced()
381{
382#if defined(__linux__)
383 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
384 return cpus_avail != thread_cpus_enabled();
385#else
386 return 0;
387#endif
388}
389
Willy Tarreau407ef892021-10-05 18:39:27 +0200390/* Below come the lock-debugging functions */
391
392#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
393
394struct lock_stat lock_stats[LOCK_LABELS];
395
396/* this is only used below */
397static const char *lock_label(enum lock_label label)
398{
399 switch (label) {
400 case TASK_RQ_LOCK: return "TASK_RQ";
401 case TASK_WQ_LOCK: return "TASK_WQ";
402 case LISTENER_LOCK: return "LISTENER";
403 case PROXY_LOCK: return "PROXY";
404 case SERVER_LOCK: return "SERVER";
405 case LBPRM_LOCK: return "LBPRM";
406 case SIGNALS_LOCK: return "SIGNALS";
407 case STK_TABLE_LOCK: return "STK_TABLE";
408 case STK_SESS_LOCK: return "STK_SESS";
409 case APPLETS_LOCK: return "APPLETS";
410 case PEER_LOCK: return "PEER";
411 case SHCTX_LOCK: return "SHCTX";
412 case SSL_LOCK: return "SSL";
413 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
414 case PATREF_LOCK: return "PATREF";
415 case PATEXP_LOCK: return "PATEXP";
416 case VARS_LOCK: return "VARS";
417 case COMP_POOL_LOCK: return "COMP_POOL";
418 case LUA_LOCK: return "LUA";
419 case NOTIF_LOCK: return "NOTIF";
420 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
421 case DNS_LOCK: return "DNS";
422 case PID_LIST_LOCK: return "PID_LIST";
423 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
424 case PIPES_LOCK: return "PIPES";
425 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
426 case AUTH_LOCK: return "AUTH";
427 case LOGSRV_LOCK: return "LOGSRV";
428 case DICT_LOCK: return "DICT";
429 case PROTO_LOCK: return "PROTO";
430 case QUEUE_LOCK: return "QUEUE";
431 case CKCH_LOCK: return "CKCH";
432 case SNI_LOCK: return "SNI";
433 case SSL_SERVER_LOCK: return "SSL_SERVER";
434 case SFT_LOCK: return "SFT";
435 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
436 case QUIC_LOCK: return "QUIC";
437 case OTHER_LOCK: return "OTHER";
438 case DEBUG1_LOCK: return "DEBUG1";
439 case DEBUG2_LOCK: return "DEBUG2";
440 case DEBUG3_LOCK: return "DEBUG3";
441 case DEBUG4_LOCK: return "DEBUG4";
442 case DEBUG5_LOCK: return "DEBUG5";
443 case LOCK_LABELS: break; /* keep compiler happy */
444 };
445 /* only way to come here is consecutive to an internal bug */
446 abort();
447}
448
449void show_lock_stats()
450{
451 int lbl;
452
453 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
454 if (!lock_stats[lbl].num_write_locked &&
455 !lock_stats[lbl].num_seek_locked &&
456 !lock_stats[lbl].num_read_locked) {
457 fprintf(stderr,
458 "Stats about Lock %s: not used\n",
459 lock_label(lbl));
460 continue;
461 }
462
463 fprintf(stderr,
464 "Stats about Lock %s: \n",
465 lock_label(lbl));
466
467 if (lock_stats[lbl].num_write_locked)
468 fprintf(stderr,
469 "\t # write lock : %lu\n"
470 "\t # write unlock: %lu (%ld)\n"
471 "\t # wait time for write : %.3f msec\n"
472 "\t # wait time for write/lock: %.3f nsec\n",
473 lock_stats[lbl].num_write_locked,
474 lock_stats[lbl].num_write_unlocked,
475 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
476 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
477 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
478
479 if (lock_stats[lbl].num_seek_locked)
480 fprintf(stderr,
481 "\t # seek lock : %lu\n"
482 "\t # seek unlock : %lu (%ld)\n"
483 "\t # wait time for seek : %.3f msec\n"
484 "\t # wait time for seek/lock : %.3f nsec\n",
485 lock_stats[lbl].num_seek_locked,
486 lock_stats[lbl].num_seek_unlocked,
487 lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked,
488 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
489 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
490
491 if (lock_stats[lbl].num_read_locked)
492 fprintf(stderr,
493 "\t # read lock : %lu\n"
494 "\t # read unlock : %lu (%ld)\n"
495 "\t # wait time for read : %.3f msec\n"
496 "\t # wait time for read/lock : %.3f nsec\n",
497 lock_stats[lbl].num_read_locked,
498 lock_stats[lbl].num_read_unlocked,
499 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
500 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
501 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
502 }
503}
504
Willy Tarreau407ef892021-10-05 18:39:27 +0200505void __ha_rwlock_init(struct ha_rwlock *l)
506{
507 memset(l, 0, sizeof(struct ha_rwlock));
508 __RWLOCK_INIT(&l->lock);
509}
510
511void __ha_rwlock_destroy(struct ha_rwlock *l)
512{
513 __RWLOCK_DESTROY(&l->lock);
514 memset(l, 0, sizeof(struct ha_rwlock));
515}
516
517
518void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
519 const char *func, const char *file, int line)
520{
521 uint64_t start_time;
522
523 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
524 abort();
525
526 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
527
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200528 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200529 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200530 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200531
532 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
533
534 l->info.cur_writer = tid_bit;
535 l->info.last_location.function = func;
536 l->info.last_location.file = file;
537 l->info.last_location.line = line;
538
539 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
540}
541
542int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
543 const char *func, const char *file, int line)
544{
545 uint64_t start_time;
546 int r;
547
548 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
549 abort();
550
551 /* We set waiting writer because trywrlock could wait for readers to quit */
552 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
553
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200554 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200555 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200556 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200557 if (unlikely(r)) {
558 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
559 return r;
560 }
561 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
562
563 l->info.cur_writer = tid_bit;
564 l->info.last_location.function = func;
565 l->info.last_location.file = file;
566 l->info.last_location.line = line;
567
568 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
569
570 return 0;
571}
572
573void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
574 const char *func, const char *file, int line)
575{
576 if (unlikely(!(l->info.cur_writer & tid_bit))) {
577 /* the thread is not owning the lock for write */
578 abort();
579 }
580
581 l->info.cur_writer = 0;
582 l->info.last_location.function = func;
583 l->info.last_location.file = file;
584 l->info.last_location.line = line;
585
586 __RWLOCK_WRUNLOCK(&l->lock);
587
588 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
589}
590
591void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
592{
593 uint64_t start_time;
594
595 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
596 abort();
597
598 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
599
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200600 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200601 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200602 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200603 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
604
605 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
606
607 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
608}
609
610int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
611{
612 int r;
613
614 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
615 abort();
616
617 /* try read should never wait */
618 r = __RWLOCK_TRYRDLOCK(&l->lock);
619 if (unlikely(r))
620 return r;
621 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
622
623 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
624
625 return 0;
626}
627
628void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
629{
630 if (unlikely(!(l->info.cur_readers & tid_bit))) {
631 /* the thread is not owning the lock for read */
632 abort();
633 }
634
635 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
636
637 __RWLOCK_RDUNLOCK(&l->lock);
638
639 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
640}
641
642void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
643 const char *func, const char *file, int line)
644{
645 uint64_t start_time;
646
647 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
648 abort();
649
650 if (!(l->info.cur_writer & tid_bit))
651 abort();
652
653 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
654
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200655 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200656 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200657 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200658
659 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
660
661 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
662 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
663 l->info.last_location.function = func;
664 l->info.last_location.file = file;
665 l->info.last_location.line = line;
666
667 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
668}
669
670void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
671 const char *func, const char *file, int line)
672{
673 uint64_t start_time;
674
675 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
676 abort();
677
678 if (!(l->info.cur_writer & tid_bit))
679 abort();
680
681 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
682
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200683 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200684 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200685 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200686
687 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
688
689 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
690 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
691 l->info.last_location.function = func;
692 l->info.last_location.file = file;
693 l->info.last_location.line = line;
694
695 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
696}
697
698void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
699 const char *func, const char *file, int line)
700{
701 uint64_t start_time;
702
703 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
704 abort();
705
706 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
707
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200708 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200709 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200710 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200711
712 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
713
714 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
715 l->info.last_location.function = func;
716 l->info.last_location.file = file;
717 l->info.last_location.line = line;
718
719 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
720}
721
722void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
723 const char *func, const char *file, int line)
724{
725 uint64_t start_time;
726
727 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
728 abort();
729
730 if (!(l->info.cur_seeker & tid_bit))
731 abort();
732
733 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
734
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200735 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200736 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200737 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200738
739 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
740
741 HA_ATOMIC_OR(&l->info.cur_writer, tid_bit);
742 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
743 l->info.last_location.function = func;
744 l->info.last_location.file = file;
745 l->info.last_location.line = line;
746
747 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
748}
749
750void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
751 const char *func, const char *file, int line)
752{
753 uint64_t start_time;
754
755 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
756 abort();
757
758 if (!(l->info.cur_seeker & tid_bit))
759 abort();
760
761 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
762
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200763 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200764 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200765 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200766
767 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
768
769 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
770 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
771 l->info.last_location.function = func;
772 l->info.last_location.file = file;
773 l->info.last_location.line = line;
774
775 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
776}
777
778void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
779 const char *func, const char *file, int line)
780{
781 if (!(l->info.cur_seeker & tid_bit))
782 abort();
783
784 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
785 l->info.last_location.function = func;
786 l->info.last_location.file = file;
787 l->info.last_location.line = line;
788
789 __RWLOCK_SKUNLOCK(&l->lock);
790
791 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
792}
793
794int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
795 const char *func, const char *file, int line)
796{
797 uint64_t start_time;
798 int r;
799
800 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
801 abort();
802
803 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
804
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200805 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200806 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200807 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200808
809 if (likely(!r)) {
810 /* got the lock ! */
811 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
812 HA_ATOMIC_OR(&l->info.cur_seeker, 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
822int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
823 const char *func, const char *file, int line)
824{
825 uint64_t start_time;
826 int r;
827
828 if ((l->info.cur_writer | l->info.cur_seeker) & tid_bit)
829 abort();
830
831 if (!(l->info.cur_readers & tid_bit))
832 abort();
833
834 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
835
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200836 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200837 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200838 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200839
840 if (likely(!r)) {
841 /* got the lock ! */
842 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
843 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
844 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
845 l->info.last_location.function = func;
846 l->info.last_location.file = file;
847 l->info.last_location.line = line;
848 }
849
850 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
851 return r;
852}
853
854void __spin_init(struct ha_spinlock *l)
855{
856 memset(l, 0, sizeof(struct ha_spinlock));
857 __SPIN_INIT(&l->lock);
858}
859
860void __spin_destroy(struct ha_spinlock *l)
861{
862 __SPIN_DESTROY(&l->lock);
863 memset(l, 0, sizeof(struct ha_spinlock));
864}
865
866void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
867 const char *func, const char *file, int line)
868{
869 uint64_t start_time;
870
871 if (unlikely(l->info.owner & tid_bit)) {
872 /* the thread is already owning the lock */
873 abort();
874 }
875
876 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
877
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200878 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200879 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200880 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200881
882 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
883
884
885 l->info.owner = tid_bit;
886 l->info.last_location.function = func;
887 l->info.last_location.file = file;
888 l->info.last_location.line = line;
889
890 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
891}
892
893int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
894 const char *func, const char *file, int line)
895{
896 int r;
897
898 if (unlikely(l->info.owner & tid_bit)) {
899 /* the thread is already owning the lock */
900 abort();
901 }
902
903 /* try read should never wait */
904 r = __SPIN_TRYLOCK(&l->lock);
905 if (unlikely(r))
906 return r;
907 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
908
909 l->info.owner = tid_bit;
910 l->info.last_location.function = func;
911 l->info.last_location.file = file;
912 l->info.last_location.line = line;
913
914 return 0;
915}
916
917void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
918 const char *func, const char *file, int line)
919{
920 if (unlikely(!(l->info.owner & tid_bit))) {
921 /* the thread is not owning the lock */
922 abort();
923 }
924
925 l->info.owner = 0;
926 l->info.last_location.function = func;
927 l->info.last_location.file = file;
928 l->info.last_location.line = line;
929
930 __SPIN_UNLOCK(&l->lock);
931 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
932}
933
934#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
935
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200936/* Depending on the platform and how libpthread was built, pthread_exit() may
937 * involve some code in libgcc_s that would be loaded on exit for the first
938 * time, causing aborts if the process is chrooted. It's harmless bit very
939 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
940 * needed, so what we do here is that during early boot we create a dummy
941 * thread that immediately exits. This will lead to libgcc_s being loaded
942 * during boot on the platforms where it's required.
943 */
944static void *dummy_thread_function(void *data)
945{
946 pthread_exit(NULL);
947 return NULL;
948}
949
950static inline void preload_libgcc_s(void)
951{
952 pthread_t dummy_thread;
953 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
954 pthread_join(dummy_thread, NULL);
955}
956
Willy Tarreau3f567e42020-05-28 15:29:19 +0200957static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200958{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100959 char *ptr = NULL;
960
961 if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) {
962 ha_alert("MAX_THREADS value must be between 1 and %d inclusive; "
963 "HAProxy was built with value %d, please fix it and rebuild.\n",
964 LONGBITS, MAX_THREADS);
965 exit(1);
966 }
Willy Tarreau149ab772019-01-26 14:27:06 +0100967
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200968 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +0200969
Willy Tarreau149ab772019-01-26 14:27:06 +0100970 thread_cpus_enabled_at_boot = thread_cpus_enabled();
971
972 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
973 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100974 hap_register_build_opts(ptr, 1);
975
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200976#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
977 memset(lock_stats, 0, sizeof(lock_stats));
978#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200979}
Willy Tarreau8ead1d02022-04-25 19:23:17 +0200980INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200981
Willy Tarreau8459f252018-12-15 16:48:14 +0100982#else
983
Willy Tarreauaa992762021-10-06 23:33:20 +0200984/* send signal <sig> to thread <thr> (send to process in fact) */
985void ha_tkill(unsigned int thr, int sig)
986{
987 raise(sig);
988}
989
990/* send signal <sig> to all threads (send to process in fact) */
991void ha_tkillall(int sig)
992{
993 raise(sig);
994}
995
996void ha_thread_relax(void)
997{
998#ifdef _POSIX_PRIORITY_SCHEDULING
999 sched_yield();
1000#endif
1001}
1002
Willy Tarreau8459f252018-12-15 16:48:14 +01001003REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
1004
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001005#endif // USE_THREAD
1006
1007
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001008/* scans the configured thread mapping and establishes the final one. Returns <0
1009 * on failure, >=0 on success.
1010 */
1011int thread_map_to_groups()
1012{
1013 int t, g, ut, ug;
1014 int q, r;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001015 ulong m __maybe_unused;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001016
1017 ut = ug = 0; // unassigned threads & groups
1018
1019 for (t = 0; t < global.nbthread; t++) {
1020 if (!ha_thread_info[t].tg)
1021 ut++;
1022 }
1023
1024 for (g = 0; g < global.nbtgroups; g++) {
1025 if (!ha_tgroup_info[g].count)
1026 ug++;
Willy Tarreau60fe4a92022-06-28 17:48:07 +02001027 ha_tgroup_info[g].tgid_bit = 1UL << g;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001028 }
1029
1030 if (ug > ut) {
1031 ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut);
1032 return -1;
1033 }
1034
1035 /* look for first unassigned thread */
1036 for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++)
1037 ;
1038
1039 /* assign threads to empty groups */
1040 for (g = 0; ug && ut; ) {
1041 /* due to sparse thread assignment we can end up with more threads
1042 * per group on last assigned groups than former ones, so we must
1043 * always try to pack the maximum remaining ones together first.
1044 */
1045 q = ut / ug;
1046 r = ut % ug;
1047 if ((q + !!r) > MAX_THREADS_PER_GROUP) {
1048 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);
1049 return -1;
1050 }
1051
1052 /* thread <t> is the next unassigned one. Let's look for next
1053 * unassigned group, we know there are some left
1054 */
1055 while (ut >= ug && ha_tgroup_info[g].count)
1056 g++;
1057
1058 /* group g is unassigned, try to fill it with consecutive threads */
1059 while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r &&
1060 (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) {
1061
1062 if (!ha_tgroup_info[g].count) {
1063 /* assign new group */
1064 ha_tgroup_info[g].base = t;
1065 ug--;
1066 }
1067
1068 ha_tgroup_info[g].count++;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001069 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001070 ha_thread_info[t].tg = &ha_tgroup_info[g];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001071 ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g];
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001072
1073 ut--;
1074 /* switch to next unassigned thread */
1075 while (++t < global.nbthread && ha_thread_info[t].tg)
1076 ;
1077 }
1078 }
1079
1080 if (ut) {
1081 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);
1082 return -1;
1083 }
1084
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001085 for (t = 0; t < global.nbthread; t++) {
1086 ha_thread_info[t].tid = t;
1087 ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base;
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001088 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1089 }
1090
Willy Tarreaucce203a2022-06-24 15:55:11 +02001091 m = 0;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001092 for (g = 0; g < global.nbtgroups; g++) {
1093 ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count);
Willy Tarreaucce203a2022-06-24 15:55:11 +02001094 if (!ha_tgroup_info[g].count)
1095 continue;
1096 m |= 1UL << g;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001097
1098 }
1099
Willy Tarreaucce203a2022-06-24 15:55:11 +02001100#ifdef USE_THREAD
1101 all_tgroups_mask = m;
1102#endif
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001103 return 0;
1104}
1105
Willy Tarreau627def92021-09-29 18:59:47 +02001106/* converts a configuration thread group+mask to a global group+mask depending on
1107 * the configured thread group id. This is essentially for use with the "thread"
1108 * directive on "bind" lines, where "thread 2/1-3" might be turned to "4-6" for
1109 * the global ID. It cannot be used before the thread mapping above was completed
1110 * and the thread group number configured. Possible options:
1111 * - igid == 0: imask represents global IDs. We have to check that all
1112 * configured threads in the mask belong to the same group. If imask is zero
1113 * it means everything, so for now we only support this with a single group.
1114 * - igid > 0, imask = 0: convert local values to global values for this thread
1115 * - igid > 0, imask > 0: convert local values to global values
1116 *
1117 * Returns <0 on failure, >=0 on success.
1118 */
1119int thread_resolve_group_mask(uint igid, ulong imask, uint *ogid, ulong *omask, char **err)
1120{
1121 ulong mask;
1122 uint t;
1123
1124 if (igid == 0) {
1125 /* unspecified group, IDs are global */
1126 if (!imask) {
1127 /* all threads of all groups */
1128 if (global.nbtgroups > 1) {
1129 memprintf(err, "'thread' directive spans multiple groups");
1130 return -1;
1131 }
1132 mask = 0;
1133 *ogid = 1; // first and only group
1134 *omask = all_threads_mask;
1135 return 0;
1136 } else {
1137 /* some global threads */
1138 imask &= all_threads_mask;
1139 for (t = 0; t < global.nbthread; t++) {
1140 if (imask & (1UL << t)) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001141 if (ha_thread_info[t].tgid != igid) {
Willy Tarreau627def92021-09-29 18:59:47 +02001142 if (!igid)
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001143 igid = ha_thread_info[t].tgid;
Willy Tarreau627def92021-09-29 18:59:47 +02001144 else {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001145 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 +02001146 return -1;
1147 }
1148 }
1149 }
1150 }
1151
1152 if (!igid) {
1153 memprintf(err, "'thread' directive contains threads that belong to no group");
1154 return -1;
1155 }
1156
1157 /* we have a valid group, convert this to global thread IDs */
1158 *ogid = igid;
Willy Tarreau9b0f0d12022-07-15 19:38:52 +02001159 *omask = imask & (ha_tgroup_info[igid - 1].threads_enabled << ha_tgroup_info[igid - 1].base);
Willy Tarreau627def92021-09-29 18:59:47 +02001160 return 0;
1161 }
1162 } else {
1163 /* group was specified */
1164 if (igid > global.nbtgroups) {
1165 memprintf(err, "'thread' directive references non-existing thread group %u", igid);
1166 return -1;
1167 }
1168
1169 if (!imask) {
1170 /* all threads of this groups. Let's make a mask from their count and base. */
1171 *ogid = igid;
1172 mask = 1UL << (ha_tgroup_info[igid - 1].count - 1);
1173 mask |= mask - 1;
1174 *omask = mask << ha_tgroup_info[igid - 1].base;
1175 return 0;
1176 } else {
1177 /* some local threads. Keep only existing ones for this group */
1178
1179 mask = 1UL << (ha_tgroup_info[igid - 1].count - 1);
1180 mask |= mask - 1;
1181
1182 if (!(mask & imask)) {
1183 /* no intersection between the thread group's
1184 * threads and the bind line's.
1185 */
1186#ifdef THREAD_AUTO_ADJUST_GROUPS
1187 unsigned long new_mask = 0;
1188
1189 while (imask) {
1190 new_mask |= imask & mask;
1191 imask >>= ha_tgroup_info[igid - 1].count;
1192 }
1193 imask = new_mask;
1194#else
1195 memprintf(err, "'thread' directive only references threads not belonging to the group");
1196 return -1;
1197#endif
1198 }
1199
1200 mask &= imask;
1201 *omask = mask << ha_tgroup_info[igid - 1].base;
1202 *ogid = igid;
1203 return 0;
1204 }
1205 }
1206}
1207
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001208/* Parse the "nbthread" global directive, which takes an integer argument that
1209 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001210 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001211static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
1212 const struct proxy *defpx, const char *file, int line,
1213 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001214{
1215 long nbthread;
1216 char *errptr;
1217
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001218 if (too_many_args(1, args, err, NULL))
1219 return -1;
1220
1221 nbthread = strtol(args[1], &errptr, 10);
1222 if (!*args[1] || *errptr) {
1223 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1224 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001225 }
1226
1227#ifndef USE_THREAD
1228 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001229 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]);
1230 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001231 }
1232#else
1233 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001234 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1235 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001236 }
1237
Willy Tarreaufc647362019-02-02 17:05:03 +01001238 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001239#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001240
1241 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001242 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1243 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001244
1245 global.nbthread = nbthread;
1246 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001247}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001248
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001249/* Parse the "thread-group" global directive, which takes an integer argument
1250 * that designates a thread group, and a list of threads to put into that group.
1251 */
1252static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx,
1253 const struct proxy *defpx, const char *file, int line,
1254 char **err)
1255{
1256 char *errptr;
1257 long tnum, tend, tgroup;
1258 int arg, tot;
1259
1260 tgroup = strtol(args[1], &errptr, 10);
1261 if (!*args[1] || *errptr) {
1262 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1263 return -1;
1264 }
1265
1266 if (tgroup < 1 || tgroup > MAX_TGROUPS) {
1267 memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup);
1268 return -1;
1269 }
1270
1271 /* look for a preliminary definition of any thread pointing to this
1272 * group, and remove them.
1273 */
1274 if (ha_tgroup_info[tgroup-1].count) {
1275 ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n",
1276 file, line, args[0], tgroup);
1277
1278 for (tnum = ha_tgroup_info[tgroup-1].base;
1279 tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count;
1280 tnum++) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001281 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001282 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001283 ha_thread_info[tnum-1].tgid = 0;
Willy Tarreau03f9b352022-06-27 16:02:24 +02001284 ha_thread_info[tnum-1].tg_ctx = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001285 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001286 }
1287 ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0;
1288 }
1289
1290 tot = 0;
1291 for (arg = 2; args[arg] && *args[arg]; arg++) {
1292 tend = tnum = strtol(args[arg], &errptr, 10);
1293
1294 if (*errptr == '-')
1295 tend = strtol(errptr + 1, &errptr, 10);
1296
1297 if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) {
1298 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);
1299 return -1;
1300 }
1301
1302 for(; tnum <= tend; tnum++) {
1303 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
1304 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n",
1305 file, line, args[0], tgroup, tnum);
1306 } else if (ha_thread_info[tnum-1].tg) {
1307 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n",
1308 file, line, args[0], tgroup, tnum,
1309 (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1));
1310 }
1311
1312 if (!ha_tgroup_info[tgroup-1].count) {
1313 ha_tgroup_info[tgroup-1].base = tnum-1;
1314 ha_tgroup_info[tgroup-1].count = 1;
1315 }
1316 else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) {
1317 ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base;
1318 }
1319 else if (tnum < ha_tgroup_info[tgroup-1].base) {
1320 ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1;
1321 ha_tgroup_info[tgroup-1].base = tnum - 1;
1322 }
1323
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001324 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001325 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001326 ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1];
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001327 tot++;
1328 }
1329 }
1330
1331 if (ha_tgroup_info[tgroup-1].count > tot) {
1332 memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup);
1333 return -1;
1334 }
1335
1336 if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) {
1337 memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP);
1338 return -1;
1339 }
1340
1341 return 0;
1342}
1343
Willy Tarreauc33b9692021-09-22 12:07:23 +02001344/* Parse the "thread-groups" global directive, which takes an integer argument
1345 * that contains the desired number of thread groups.
1346 */
1347static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx,
1348 const struct proxy *defpx, const char *file, int line,
1349 char **err)
1350{
1351 long nbtgroups;
1352 char *errptr;
1353
1354 if (too_many_args(1, args, err, NULL))
1355 return -1;
1356
1357 nbtgroups = strtol(args[1], &errptr, 10);
1358 if (!*args[1] || *errptr) {
1359 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1360 return -1;
1361 }
1362
1363#ifndef USE_THREAD
1364 if (nbtgroups != 1) {
1365 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]);
1366 return -1;
1367 }
1368#else
1369 if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) {
1370 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups);
1371 return -1;
1372 }
1373#endif
1374
1375 HA_DIAG_WARNING_COND(global.nbtgroups,
1376 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1377 file, line, args[0]);
1378
1379 global.nbtgroups = nbtgroups;
1380 return 0;
1381}
1382
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001383/* config keyword parsers */
1384static struct cfg_kw_list cfg_kws = {ILH, {
1385 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001386 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001387 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001388 { 0, NULL, NULL }
1389}};
1390
1391INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);