blob: 0858acad87b263c0c643b93fcd4c7de1ecd6a7ff [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#include <fcntl.h>
17
Willy Tarreau5e03dfa2021-10-06 22:53:51 +020018#ifdef USE_THREAD
19# include <pthread.h>
20#endif
21
Willy Tarreau149ab772019-01-26 14:27:06 +010022#ifdef USE_CPU_AFFINITY
Willy Tarreaud10385a2021-10-06 22:22:40 +020023# include <sched.h>
24# if defined(__FreeBSD__) || defined(__DragonFly__)
25# include <sys/param.h>
26# ifdef __FreeBSD__
27# include <sys/cpuset.h>
28# endif
29# include <pthread_np.h>
30# endif
31# ifdef __APPLE__
32# include <mach/mach_types.h>
33# include <mach/thread_act.h>
34# include <mach/thread_policy.h>
35# endif
36# include <haproxy/cpuset.h>
Willy Tarreau149ab772019-01-26 14:27:06 +010037#endif
38
Willy Tarreau6be78492020-06-05 00:00:29 +020039#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/fd.h>
41#include <haproxy/global.h>
Willy Tarreau11bd6f72021-05-08 20:33:02 +020042#include <haproxy/log.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020043#include <haproxy/thread.h>
Willy Tarreaudced3eb2021-10-05 18:48:23 +020044#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020045#include <haproxy/tools.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020046
David Carliera92c5ce2019-09-13 05:03:12 +010047struct thread_info ha_thread_info[MAX_THREADS] = { };
48THREAD_LOCAL struct thread_info *ti = &ha_thread_info[0];
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020049
50#ifdef USE_THREAD
51
Willy Tarreau56c3b8b2021-04-10 17:28:18 +020052volatile unsigned long threads_want_rdv_mask __read_mostly = 0;
Willy Tarreau60b639c2018-08-02 10:16:17 +020053volatile unsigned long threads_harmless_mask = 0;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +020054volatile unsigned long threads_idle_mask = 0;
Willy Tarreau9a1f5732019-06-09 12:20:02 +020055volatile unsigned long threads_sync_mask = 0;
Willy Tarreau56c3b8b2021-04-10 17:28:18 +020056volatile unsigned long all_threads_mask __read_mostly = 1; // nbthread 1 assumed by default
Willy Tarreau0c026f42018-08-01 19:12:20 +020057THREAD_LOCAL unsigned int tid = 0;
58THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
Willy Tarreau149ab772019-01-26 14:27:06 +010059int thread_cpus_enabled_at_boot = 1;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +020060static pthread_t ha_pthread[MAX_THREADS] = { };
Willy Tarreau0c026f42018-08-01 19:12:20 +020061
Willy Tarreau60b639c2018-08-02 10:16:17 +020062/* Marks the thread as harmless until the last thread using the rendez-vous
Christopher Fauleta9a9e9a2021-03-25 14:11:36 +010063 * point quits, excluding the current one. Thus an isolated thread may be safely
64 * marked as harmless. Given that we can wait for a long time, sched_yield() is
65 * used when available to offer the CPU resources to competing threads if
66 * needed.
Willy Tarreau60b639c2018-08-02 10:16:17 +020067 */
68void thread_harmless_till_end()
69{
Willy Tarreau286363b2021-08-04 10:33:57 +020070 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
71 while (threads_want_rdv_mask & all_threads_mask & ~tid_bit) {
72 ha_thread_relax();
73 }
Willy Tarreau60b639c2018-08-02 10:16:17 +020074}
75
76/* Isolates the current thread : request the ability to work while all other
Willy Tarreauf519cfa2021-08-04 11:22:07 +020077 * threads are harmless, as defined by thread_harmless_now() (i.e. they're not
78 * going to touch any visible memory area). Only returns once all of them are
79 * harmless, with the current thread's bit in threads_harmless_mask cleared.
80 * Needs to be completed using thread_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +020081 */
82void thread_isolate()
83{
84 unsigned long old;
85
Olivier Houchardb23a61f2019-03-08 18:51:17 +010086 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
87 __ha_barrier_atomic_store();
88 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +020089
90 /* wait for all threads to become harmless */
91 old = threads_harmless_mask;
92 while (1) {
93 if (unlikely((old & all_threads_mask) != all_threads_mask))
94 old = threads_harmless_mask;
Olivier Houchardb23a61f2019-03-08 18:51:17 +010095 else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
Willy Tarreau60b639c2018-08-02 10:16:17 +020096 break;
97
Willy Tarreau38171da2019-05-17 16:33:13 +020098 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +020099 }
100 /* one thread gets released at a time here, with its harmess bit off.
101 * The loss of this bit makes the other one continue to spin while the
102 * thread is working alone.
103 */
104}
105
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200106/* Isolates the current thread : request the ability to work while all other
107 * threads are idle, as defined by thread_idle_now(). It only returns once
108 * all of them are both harmless and idle, with the current thread's bit in
109 * threads_harmless_mask and idle_mask cleared. Needs to be completed using
110 * thread_release(). By doing so the thread also engages in being safe against
111 * any actions that other threads might be about to start under the same
112 * conditions. This specifically targets destruction of any internal structure,
113 * which implies that the current thread may not hold references to any object.
114 *
115 * Note that a concurrent thread_isolate() will usually win against
116 * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to
117 * get back to the poller or any other fully idle location, that will
118 * ultimately release this one.
119 */
120void thread_isolate_full()
121{
122 unsigned long old;
123
124 _HA_ATOMIC_OR(&threads_idle_mask, tid_bit);
125 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
126 __ha_barrier_atomic_store();
127 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
128
129 /* wait for all threads to become harmless */
130 old = threads_harmless_mask;
131 while (1) {
132 unsigned long idle = _HA_ATOMIC_LOAD(&threads_idle_mask);
133
134 if (unlikely((old & all_threads_mask) != all_threads_mask))
135 old = _HA_ATOMIC_LOAD(&threads_harmless_mask);
136 else if ((idle & all_threads_mask) == all_threads_mask &&
137 _HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
138 break;
139
140 ha_thread_relax();
141 }
142
143 /* we're not idle anymore at this point. Other threads waiting on this
144 * condition will need to wait until out next pass to the poller, or
145 * our next call to thread_isolate_full().
146 */
147 _HA_ATOMIC_AND(&threads_idle_mask, ~tid_bit);
148}
149
Willy Tarreau60b639c2018-08-02 10:16:17 +0200150/* Cancels the effect of thread_isolate() by releasing the current thread's bit
Willy Tarreauf519cfa2021-08-04 11:22:07 +0200151 * in threads_want_rdv_mask. This immediately allows other threads to expect be
152 * executed, though they will first have to wait for this thread to become
153 * harmless again (possibly by reaching the poller again).
Willy Tarreau60b639c2018-08-02 10:16:17 +0200154 */
155void thread_release()
156{
Olivier Houchardb23a61f2019-03-08 18:51:17 +0100157 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200158}
Christopher Faulet339fff82017-10-19 11:59:15 +0200159
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200160/* Cancels the effect of thread_isolate() by releasing the current thread's bit
161 * in threads_want_rdv_mask and by marking this thread as harmless until the
162 * last worker finishes. The difference with thread_release() is that this one
163 * will not leave the function before others are notified to do the same, so it
164 * guarantees that the current thread will not pass through a subsequent call
165 * to thread_isolate() before others finish.
166 */
167void thread_sync_release()
168{
169 _HA_ATOMIC_OR(&threads_sync_mask, tid_bit);
170 __ha_barrier_atomic_store();
171 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
172
173 while (threads_want_rdv_mask & all_threads_mask) {
174 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
175 while (threads_want_rdv_mask & all_threads_mask)
176 ha_thread_relax();
177 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
178 }
179
180 /* the current thread is not harmless anymore, thread_isolate()
181 * is forced to wait till all waiters finish.
182 */
183 _HA_ATOMIC_AND(&threads_sync_mask, ~tid_bit);
184 while (threads_sync_mask & all_threads_mask)
185 ha_thread_relax();
186}
187
Willy Tarreaud10385a2021-10-06 22:22:40 +0200188/* Sets up threads, signals and masks, and starts threads 2 and above.
189 * Does nothing when threads are disabled.
190 */
191void setup_extra_threads(void *(*handler)(void *))
192{
193 sigset_t blocked_sig, old_sig;
194 int i;
195
196 /* ensure the signals will be blocked in every thread */
197 sigfillset(&blocked_sig);
198 sigdelset(&blocked_sig, SIGPROF);
199 sigdelset(&blocked_sig, SIGBUS);
200 sigdelset(&blocked_sig, SIGFPE);
201 sigdelset(&blocked_sig, SIGILL);
202 sigdelset(&blocked_sig, SIGSEGV);
203 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
204
205 /* Create nbthread-1 thread. The first thread is the current process */
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200206 ha_pthread[0] = pthread_self();
Willy Tarreaud10385a2021-10-06 22:22:40 +0200207 for (i = 1; i < global.nbthread; i++)
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200208 pthread_create(&ha_pthread[i], NULL, handler, (void *)(long)i);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200209}
210
211/* waits for all threads to terminate. Does nothing when threads are
212 * disabled.
213 */
214void wait_for_threads_completion()
215{
216 int i;
217
218 /* Wait the end of other threads */
219 for (i = 1; i < global.nbthread; i++)
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200220 pthread_join(ha_pthread[i], NULL);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200221
222#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
223 show_lock_stats();
224#endif
225}
226
227/* Tries to set the current thread's CPU affinity according to the cpu_map */
228void set_thread_cpu_affinity()
229{
230#if defined(USE_CPU_AFFINITY)
231 /* no affinity setting for the master process */
232 if (master)
233 return;
234
235 /* Now the CPU affinity for all threads */
236 if (ha_cpuset_count(&cpu_map.proc))
237 ha_cpuset_and(&cpu_map.thread[tid], &cpu_map.proc);
238
239 if (ha_cpuset_count(&cpu_map.thread[tid])) {/* only do this if the thread has a THREAD map */
240# if defined(__APPLE__)
241 /* Note: this API is limited to the first 32/64 CPUs */
242 unsigned long set = cpu_map.thread[tid].cpuset;
243 int j;
244
245 while ((j = ffsl(set)) > 0) {
246 thread_affinity_policy_data_t cpu_set = { j - 1 };
247 thread_port_t mthread;
248
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200249 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200250 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
251 set &= ~(1UL << (j - 1));
252 }
253# else
254 struct hap_cpuset *set = &cpu_map.thread[tid];
255
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200256 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200257# endif
258 }
259#endif /* USE_CPU_AFFINITY */
260}
261
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200262/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
263 * since POSIX took great care of not specifying its representation, making it
264 * hard to export for post-mortem analysis. For this reason we copy it into a
265 * union and will use the smallest scalar type at least as large as its size,
266 * which will keep endianness and alignment for all regular sizes. As a last
267 * resort we end up with a long long ligned to the first bytes in memory, which
268 * will be endian-dependent if pthread_t is larger than a long long (not seen
269 * yet).
270 */
271unsigned long long ha_get_pthread_id(unsigned int thr)
272{
273 union {
274 pthread_t t;
275 unsigned long long ll;
276 unsigned int i;
277 unsigned short s;
278 unsigned char c;
279 } u = { 0 };
280
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200281 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200282
283 if (sizeof(u.t) <= sizeof(u.c))
284 return u.c;
285 else if (sizeof(u.t) <= sizeof(u.s))
286 return u.s;
287 else if (sizeof(u.t) <= sizeof(u.i))
288 return u.i;
289 return u.ll;
290}
291
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200292/* send signal <sig> to thread <thr> */
293void ha_tkill(unsigned int thr, int sig)
294{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200295 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200296}
297
298/* send signal <sig> to all threads. The calling thread is signaled last in
299 * order to allow all threads to synchronize in the handler.
300 */
301void ha_tkillall(int sig)
302{
303 unsigned int thr;
304
305 for (thr = 0; thr < global.nbthread; thr++) {
306 if (!(all_threads_mask & (1UL << thr)))
307 continue;
308 if (thr == tid)
309 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200310 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200311 }
312 raise(sig);
313}
314
Willy Tarreau3d184982020-10-18 10:20:59 +0200315/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100316void ha_spin_init(HA_SPINLOCK_T *l)
317{
318 HA_SPIN_INIT(l);
319}
320
Willy Tarreau3d184982020-10-18 10:20:59 +0200321/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100322void ha_rwlock_init(HA_RWLOCK_T *l)
323{
324 HA_RWLOCK_INIT(l);
325}
326
Willy Tarreau149ab772019-01-26 14:27:06 +0100327/* returns the number of CPUs the current process is enabled to run on */
328static int thread_cpus_enabled()
329{
330 int ret = 1;
331
332#ifdef USE_CPU_AFFINITY
333#if defined(__linux__) && defined(CPU_COUNT)
334 cpu_set_t mask;
335
336 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
337 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200338#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
339 cpuset_t cpuset;
340 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
341 sizeof(cpuset), &cpuset) == 0)
342 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000343#elif defined(__APPLE__)
344 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100345#endif
346#endif
347 ret = MAX(ret, 1);
348 ret = MIN(ret, MAX_THREADS);
349 return ret;
350}
351
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200352/* Returns 1 if the cpu set is currently restricted for the process else 0.
353 * Currently only implemented for the Linux platform.
354 */
355int thread_cpu_mask_forced()
356{
357#if defined(__linux__)
358 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
359 return cpus_avail != thread_cpus_enabled();
360#else
361 return 0;
362#endif
363}
364
Willy Tarreau407ef892021-10-05 18:39:27 +0200365/* Below come the lock-debugging functions */
366
367#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
368
369struct lock_stat lock_stats[LOCK_LABELS];
370
371/* this is only used below */
372static const char *lock_label(enum lock_label label)
373{
374 switch (label) {
375 case TASK_RQ_LOCK: return "TASK_RQ";
376 case TASK_WQ_LOCK: return "TASK_WQ";
377 case LISTENER_LOCK: return "LISTENER";
378 case PROXY_LOCK: return "PROXY";
379 case SERVER_LOCK: return "SERVER";
380 case LBPRM_LOCK: return "LBPRM";
381 case SIGNALS_LOCK: return "SIGNALS";
382 case STK_TABLE_LOCK: return "STK_TABLE";
383 case STK_SESS_LOCK: return "STK_SESS";
384 case APPLETS_LOCK: return "APPLETS";
385 case PEER_LOCK: return "PEER";
386 case SHCTX_LOCK: return "SHCTX";
387 case SSL_LOCK: return "SSL";
388 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
389 case PATREF_LOCK: return "PATREF";
390 case PATEXP_LOCK: return "PATEXP";
391 case VARS_LOCK: return "VARS";
392 case COMP_POOL_LOCK: return "COMP_POOL";
393 case LUA_LOCK: return "LUA";
394 case NOTIF_LOCK: return "NOTIF";
395 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
396 case DNS_LOCK: return "DNS";
397 case PID_LIST_LOCK: return "PID_LIST";
398 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
399 case PIPES_LOCK: return "PIPES";
400 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
401 case AUTH_LOCK: return "AUTH";
402 case LOGSRV_LOCK: return "LOGSRV";
403 case DICT_LOCK: return "DICT";
404 case PROTO_LOCK: return "PROTO";
405 case QUEUE_LOCK: return "QUEUE";
406 case CKCH_LOCK: return "CKCH";
407 case SNI_LOCK: return "SNI";
408 case SSL_SERVER_LOCK: return "SSL_SERVER";
409 case SFT_LOCK: return "SFT";
410 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
411 case QUIC_LOCK: return "QUIC";
412 case OTHER_LOCK: return "OTHER";
413 case DEBUG1_LOCK: return "DEBUG1";
414 case DEBUG2_LOCK: return "DEBUG2";
415 case DEBUG3_LOCK: return "DEBUG3";
416 case DEBUG4_LOCK: return "DEBUG4";
417 case DEBUG5_LOCK: return "DEBUG5";
418 case LOCK_LABELS: break; /* keep compiler happy */
419 };
420 /* only way to come here is consecutive to an internal bug */
421 abort();
422}
423
424void show_lock_stats()
425{
426 int lbl;
427
428 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
429 if (!lock_stats[lbl].num_write_locked &&
430 !lock_stats[lbl].num_seek_locked &&
431 !lock_stats[lbl].num_read_locked) {
432 fprintf(stderr,
433 "Stats about Lock %s: not used\n",
434 lock_label(lbl));
435 continue;
436 }
437
438 fprintf(stderr,
439 "Stats about Lock %s: \n",
440 lock_label(lbl));
441
442 if (lock_stats[lbl].num_write_locked)
443 fprintf(stderr,
444 "\t # write lock : %lu\n"
445 "\t # write unlock: %lu (%ld)\n"
446 "\t # wait time for write : %.3f msec\n"
447 "\t # wait time for write/lock: %.3f nsec\n",
448 lock_stats[lbl].num_write_locked,
449 lock_stats[lbl].num_write_unlocked,
450 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
451 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
452 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
453
454 if (lock_stats[lbl].num_seek_locked)
455 fprintf(stderr,
456 "\t # seek lock : %lu\n"
457 "\t # seek unlock : %lu (%ld)\n"
458 "\t # wait time for seek : %.3f msec\n"
459 "\t # wait time for seek/lock : %.3f nsec\n",
460 lock_stats[lbl].num_seek_locked,
461 lock_stats[lbl].num_seek_unlocked,
462 lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked,
463 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
464 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
465
466 if (lock_stats[lbl].num_read_locked)
467 fprintf(stderr,
468 "\t # read lock : %lu\n"
469 "\t # read unlock : %lu (%ld)\n"
470 "\t # wait time for read : %.3f msec\n"
471 "\t # wait time for read/lock : %.3f nsec\n",
472 lock_stats[lbl].num_read_locked,
473 lock_stats[lbl].num_read_unlocked,
474 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
475 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
476 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
477 }
478}
479
Willy Tarreau407ef892021-10-05 18:39:27 +0200480void __ha_rwlock_init(struct ha_rwlock *l)
481{
482 memset(l, 0, sizeof(struct ha_rwlock));
483 __RWLOCK_INIT(&l->lock);
484}
485
486void __ha_rwlock_destroy(struct ha_rwlock *l)
487{
488 __RWLOCK_DESTROY(&l->lock);
489 memset(l, 0, sizeof(struct ha_rwlock));
490}
491
492
493void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
494 const char *func, const char *file, int line)
495{
496 uint64_t start_time;
497
498 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
499 abort();
500
501 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
502
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200503 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200504 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200505 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200506
507 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
508
509 l->info.cur_writer = tid_bit;
510 l->info.last_location.function = func;
511 l->info.last_location.file = file;
512 l->info.last_location.line = line;
513
514 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
515}
516
517int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
518 const char *func, const char *file, int line)
519{
520 uint64_t start_time;
521 int r;
522
523 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
524 abort();
525
526 /* We set waiting writer because trywrlock could wait for readers to quit */
527 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
528
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200529 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200530 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200531 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200532 if (unlikely(r)) {
533 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
534 return r;
535 }
536 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
537
538 l->info.cur_writer = tid_bit;
539 l->info.last_location.function = func;
540 l->info.last_location.file = file;
541 l->info.last_location.line = line;
542
543 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
544
545 return 0;
546}
547
548void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
549 const char *func, const char *file, int line)
550{
551 if (unlikely(!(l->info.cur_writer & tid_bit))) {
552 /* the thread is not owning the lock for write */
553 abort();
554 }
555
556 l->info.cur_writer = 0;
557 l->info.last_location.function = func;
558 l->info.last_location.file = file;
559 l->info.last_location.line = line;
560
561 __RWLOCK_WRUNLOCK(&l->lock);
562
563 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
564}
565
566void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
567{
568 uint64_t start_time;
569
570 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
571 abort();
572
573 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
574
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200575 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200576 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200577 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200578 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
579
580 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
581
582 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
583}
584
585int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
586{
587 int r;
588
589 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
590 abort();
591
592 /* try read should never wait */
593 r = __RWLOCK_TRYRDLOCK(&l->lock);
594 if (unlikely(r))
595 return r;
596 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
597
598 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
599
600 return 0;
601}
602
603void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
604{
605 if (unlikely(!(l->info.cur_readers & tid_bit))) {
606 /* the thread is not owning the lock for read */
607 abort();
608 }
609
610 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
611
612 __RWLOCK_RDUNLOCK(&l->lock);
613
614 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
615}
616
617void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
618 const char *func, const char *file, int line)
619{
620 uint64_t start_time;
621
622 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
623 abort();
624
625 if (!(l->info.cur_writer & tid_bit))
626 abort();
627
628 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
629
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200630 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200631 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200632 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200633
634 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
635
636 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
637 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
638 l->info.last_location.function = func;
639 l->info.last_location.file = file;
640 l->info.last_location.line = line;
641
642 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
643}
644
645void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
646 const char *func, const char *file, int line)
647{
648 uint64_t start_time;
649
650 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
651 abort();
652
653 if (!(l->info.cur_writer & tid_bit))
654 abort();
655
656 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
657
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200658 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200659 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200660 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200661
662 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
663
664 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
665 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
666 l->info.last_location.function = func;
667 l->info.last_location.file = file;
668 l->info.last_location.line = line;
669
670 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
671}
672
673void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
674 const char *func, const char *file, int line)
675{
676 uint64_t start_time;
677
678 if ((l->info.cur_readers | l->info.cur_seeker | 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_SKLOCK(&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 l->info.last_location.function = func;
691 l->info.last_location.file = file;
692 l->info.last_location.line = line;
693
694 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
695}
696
697void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
698 const char *func, const char *file, int line)
699{
700 uint64_t start_time;
701
702 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
703 abort();
704
705 if (!(l->info.cur_seeker & tid_bit))
706 abort();
707
708 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
709
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200710 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200711 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200712 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200713
714 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
715
716 HA_ATOMIC_OR(&l->info.cur_writer, tid_bit);
717 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
718 l->info.last_location.function = func;
719 l->info.last_location.file = file;
720 l->info.last_location.line = line;
721
722 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
723}
724
725void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
726 const char *func, const char *file, int line)
727{
728 uint64_t start_time;
729
730 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
731 abort();
732
733 if (!(l->info.cur_seeker & tid_bit))
734 abort();
735
736 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
737
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200738 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200739 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200740 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200741
742 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
743
744 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
745 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
746 l->info.last_location.function = func;
747 l->info.last_location.file = file;
748 l->info.last_location.line = line;
749
750 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
751}
752
753void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
754 const char *func, const char *file, int line)
755{
756 if (!(l->info.cur_seeker & tid_bit))
757 abort();
758
759 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
760 l->info.last_location.function = func;
761 l->info.last_location.file = file;
762 l->info.last_location.line = line;
763
764 __RWLOCK_SKUNLOCK(&l->lock);
765
766 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
767}
768
769int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
770 const char *func, const char *file, int line)
771{
772 uint64_t start_time;
773 int r;
774
775 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
776 abort();
777
778 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
779
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200780 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200781 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200782 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200783
784 if (likely(!r)) {
785 /* got the lock ! */
786 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
787 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
788 l->info.last_location.function = func;
789 l->info.last_location.file = file;
790 l->info.last_location.line = line;
791 }
792
793 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
794 return r;
795}
796
797int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
798 const char *func, const char *file, int line)
799{
800 uint64_t start_time;
801 int r;
802
803 if ((l->info.cur_writer | l->info.cur_seeker) & tid_bit)
804 abort();
805
806 if (!(l->info.cur_readers & tid_bit))
807 abort();
808
809 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
810
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200811 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200812 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200813 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200814
815 if (likely(!r)) {
816 /* got the lock ! */
817 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
818 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
819 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
820 l->info.last_location.function = func;
821 l->info.last_location.file = file;
822 l->info.last_location.line = line;
823 }
824
825 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
826 return r;
827}
828
829void __spin_init(struct ha_spinlock *l)
830{
831 memset(l, 0, sizeof(struct ha_spinlock));
832 __SPIN_INIT(&l->lock);
833}
834
835void __spin_destroy(struct ha_spinlock *l)
836{
837 __SPIN_DESTROY(&l->lock);
838 memset(l, 0, sizeof(struct ha_spinlock));
839}
840
841void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
842 const char *func, const char *file, int line)
843{
844 uint64_t start_time;
845
846 if (unlikely(l->info.owner & tid_bit)) {
847 /* the thread is already owning the lock */
848 abort();
849 }
850
851 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
852
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200853 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200854 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200855 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200856
857 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
858
859
860 l->info.owner = tid_bit;
861 l->info.last_location.function = func;
862 l->info.last_location.file = file;
863 l->info.last_location.line = line;
864
865 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
866}
867
868int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
869 const char *func, const char *file, int line)
870{
871 int r;
872
873 if (unlikely(l->info.owner & tid_bit)) {
874 /* the thread is already owning the lock */
875 abort();
876 }
877
878 /* try read should never wait */
879 r = __SPIN_TRYLOCK(&l->lock);
880 if (unlikely(r))
881 return r;
882 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
883
884 l->info.owner = tid_bit;
885 l->info.last_location.function = func;
886 l->info.last_location.file = file;
887 l->info.last_location.line = line;
888
889 return 0;
890}
891
892void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
893 const char *func, const char *file, int line)
894{
895 if (unlikely(!(l->info.owner & tid_bit))) {
896 /* the thread is not owning the lock */
897 abort();
898 }
899
900 l->info.owner = 0;
901 l->info.last_location.function = func;
902 l->info.last_location.file = file;
903 l->info.last_location.line = line;
904
905 __SPIN_UNLOCK(&l->lock);
906 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
907}
908
909#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
910
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200911/* Depending on the platform and how libpthread was built, pthread_exit() may
912 * involve some code in libgcc_s that would be loaded on exit for the first
913 * time, causing aborts if the process is chrooted. It's harmless bit very
914 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
915 * needed, so what we do here is that during early boot we create a dummy
916 * thread that immediately exits. This will lead to libgcc_s being loaded
917 * during boot on the platforms where it's required.
918 */
919static void *dummy_thread_function(void *data)
920{
921 pthread_exit(NULL);
922 return NULL;
923}
924
925static inline void preload_libgcc_s(void)
926{
927 pthread_t dummy_thread;
928 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
929 pthread_join(dummy_thread, NULL);
930}
931
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200932__attribute__((constructor))
Willy Tarreau3f567e42020-05-28 15:29:19 +0200933static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200934{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100935 char *ptr = NULL;
936
937 if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) {
938 ha_alert("MAX_THREADS value must be between 1 and %d inclusive; "
939 "HAProxy was built with value %d, please fix it and rebuild.\n",
940 LONGBITS, MAX_THREADS);
941 exit(1);
942 }
Willy Tarreau149ab772019-01-26 14:27:06 +0100943
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200944 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +0200945
Willy Tarreau149ab772019-01-26 14:27:06 +0100946 thread_cpus_enabled_at_boot = thread_cpus_enabled();
947
948 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
949 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100950 hap_register_build_opts(ptr, 1);
951
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200952#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
953 memset(lock_stats, 0, sizeof(lock_stats));
954#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200955}
956
Willy Tarreau8459f252018-12-15 16:48:14 +0100957#else
958
959REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
960
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200961#endif // USE_THREAD
962
963
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200964/* Parse the "nbthread" global directive, which takes an integer argument that
965 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200966 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200967static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
968 const struct proxy *defpx, const char *file, int line,
969 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200970{
971 long nbthread;
972 char *errptr;
973
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200974 if (too_many_args(1, args, err, NULL))
975 return -1;
976
977 nbthread = strtol(args[1], &errptr, 10);
978 if (!*args[1] || *errptr) {
979 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
980 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200981 }
982
983#ifndef USE_THREAD
984 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200985 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]);
986 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200987 }
988#else
989 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200990 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
991 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200992 }
993
Willy Tarreaufc647362019-02-02 17:05:03 +0100994 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200995#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200996
997 HA_DIAG_WARNING_COND(global.nbthread,
998 "parsing [%s:%d] : nbthread is already defined and will be overridden.\n",
999 file, line);
1000
1001 global.nbthread = nbthread;
1002 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001003}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001004
1005/* config keyword parsers */
1006static struct cfg_kw_list cfg_kws = {ILH, {
1007 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
1008 { 0, NULL, NULL }
1009}};
1010
1011INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);