blob: c0b7bdbad1c7c43521786cd64c21b46b6359bbf9 [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 Tarreau149ab772019-01-26 14:27:06 +010018#ifdef USE_CPU_AFFINITY
Willy Tarreaud10385a2021-10-06 22:22:40 +020019# include <sched.h>
20# if defined(__FreeBSD__) || defined(__DragonFly__)
21# include <sys/param.h>
22# ifdef __FreeBSD__
23# include <sys/cpuset.h>
24# endif
25# include <pthread_np.h>
26# endif
27# ifdef __APPLE__
28# include <mach/mach_types.h>
29# include <mach/thread_act.h>
30# include <mach/thread_policy.h>
31# endif
32# include <haproxy/cpuset.h>
Willy Tarreau149ab772019-01-26 14:27:06 +010033#endif
34
Willy Tarreau6be78492020-06-05 00:00:29 +020035#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <haproxy/fd.h>
37#include <haproxy/global.h>
Willy Tarreau11bd6f72021-05-08 20:33:02 +020038#include <haproxy/log.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020039#include <haproxy/thread.h>
Willy Tarreaudced3eb2021-10-05 18:48:23 +020040#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020041#include <haproxy/tools.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020042
David Carliera92c5ce2019-09-13 05:03:12 +010043struct thread_info ha_thread_info[MAX_THREADS] = { };
44THREAD_LOCAL struct thread_info *ti = &ha_thread_info[0];
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020045
46#ifdef USE_THREAD
47
Willy Tarreau56c3b8b2021-04-10 17:28:18 +020048volatile unsigned long threads_want_rdv_mask __read_mostly = 0;
Willy Tarreau60b639c2018-08-02 10:16:17 +020049volatile unsigned long threads_harmless_mask = 0;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +020050volatile unsigned long threads_idle_mask = 0;
Willy Tarreau9a1f5732019-06-09 12:20:02 +020051volatile unsigned long threads_sync_mask = 0;
Willy Tarreau56c3b8b2021-04-10 17:28:18 +020052volatile unsigned long all_threads_mask __read_mostly = 1; // nbthread 1 assumed by default
Willy Tarreau0c026f42018-08-01 19:12:20 +020053THREAD_LOCAL unsigned int tid = 0;
54THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
Willy Tarreau149ab772019-01-26 14:27:06 +010055int thread_cpus_enabled_at_boot = 1;
Willy Tarreau0c026f42018-08-01 19:12:20 +020056
Willy Tarreau60b639c2018-08-02 10:16:17 +020057/* Marks the thread as harmless until the last thread using the rendez-vous
Christopher Fauleta9a9e9a2021-03-25 14:11:36 +010058 * point quits, excluding the current one. Thus an isolated thread may be safely
59 * marked as harmless. Given that we can wait for a long time, sched_yield() is
60 * used when available to offer the CPU resources to competing threads if
61 * needed.
Willy Tarreau60b639c2018-08-02 10:16:17 +020062 */
63void thread_harmless_till_end()
64{
Willy Tarreau286363b2021-08-04 10:33:57 +020065 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
66 while (threads_want_rdv_mask & all_threads_mask & ~tid_bit) {
67 ha_thread_relax();
68 }
Willy Tarreau60b639c2018-08-02 10:16:17 +020069}
70
71/* Isolates the current thread : request the ability to work while all other
Willy Tarreauf519cfa2021-08-04 11:22:07 +020072 * threads are harmless, as defined by thread_harmless_now() (i.e. they're not
73 * going to touch any visible memory area). Only returns once all of them are
74 * harmless, with the current thread's bit in threads_harmless_mask cleared.
75 * Needs to be completed using thread_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +020076 */
77void thread_isolate()
78{
79 unsigned long old;
80
Olivier Houchardb23a61f2019-03-08 18:51:17 +010081 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
82 __ha_barrier_atomic_store();
83 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +020084
85 /* wait for all threads to become harmless */
86 old = threads_harmless_mask;
87 while (1) {
88 if (unlikely((old & all_threads_mask) != all_threads_mask))
89 old = threads_harmless_mask;
Olivier Houchardb23a61f2019-03-08 18:51:17 +010090 else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
Willy Tarreau60b639c2018-08-02 10:16:17 +020091 break;
92
Willy Tarreau38171da2019-05-17 16:33:13 +020093 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +020094 }
95 /* one thread gets released at a time here, with its harmess bit off.
96 * The loss of this bit makes the other one continue to spin while the
97 * thread is working alone.
98 */
99}
100
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200101/* Isolates the current thread : request the ability to work while all other
102 * threads are idle, as defined by thread_idle_now(). It only returns once
103 * all of them are both harmless and idle, with the current thread's bit in
104 * threads_harmless_mask and idle_mask cleared. Needs to be completed using
105 * thread_release(). By doing so the thread also engages in being safe against
106 * any actions that other threads might be about to start under the same
107 * conditions. This specifically targets destruction of any internal structure,
108 * which implies that the current thread may not hold references to any object.
109 *
110 * Note that a concurrent thread_isolate() will usually win against
111 * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to
112 * get back to the poller or any other fully idle location, that will
113 * ultimately release this one.
114 */
115void thread_isolate_full()
116{
117 unsigned long old;
118
119 _HA_ATOMIC_OR(&threads_idle_mask, tid_bit);
120 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
121 __ha_barrier_atomic_store();
122 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
123
124 /* wait for all threads to become harmless */
125 old = threads_harmless_mask;
126 while (1) {
127 unsigned long idle = _HA_ATOMIC_LOAD(&threads_idle_mask);
128
129 if (unlikely((old & all_threads_mask) != all_threads_mask))
130 old = _HA_ATOMIC_LOAD(&threads_harmless_mask);
131 else if ((idle & all_threads_mask) == all_threads_mask &&
132 _HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
133 break;
134
135 ha_thread_relax();
136 }
137
138 /* we're not idle anymore at this point. Other threads waiting on this
139 * condition will need to wait until out next pass to the poller, or
140 * our next call to thread_isolate_full().
141 */
142 _HA_ATOMIC_AND(&threads_idle_mask, ~tid_bit);
143}
144
Willy Tarreau60b639c2018-08-02 10:16:17 +0200145/* Cancels the effect of thread_isolate() by releasing the current thread's bit
Willy Tarreauf519cfa2021-08-04 11:22:07 +0200146 * in threads_want_rdv_mask. This immediately allows other threads to expect be
147 * executed, though they will first have to wait for this thread to become
148 * harmless again (possibly by reaching the poller again).
Willy Tarreau60b639c2018-08-02 10:16:17 +0200149 */
150void thread_release()
151{
Olivier Houchardb23a61f2019-03-08 18:51:17 +0100152 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200153}
Christopher Faulet339fff82017-10-19 11:59:15 +0200154
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200155/* Cancels the effect of thread_isolate() by releasing the current thread's bit
156 * in threads_want_rdv_mask and by marking this thread as harmless until the
157 * last worker finishes. The difference with thread_release() is that this one
158 * will not leave the function before others are notified to do the same, so it
159 * guarantees that the current thread will not pass through a subsequent call
160 * to thread_isolate() before others finish.
161 */
162void thread_sync_release()
163{
164 _HA_ATOMIC_OR(&threads_sync_mask, tid_bit);
165 __ha_barrier_atomic_store();
166 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
167
168 while (threads_want_rdv_mask & all_threads_mask) {
169 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
170 while (threads_want_rdv_mask & all_threads_mask)
171 ha_thread_relax();
172 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
173 }
174
175 /* the current thread is not harmless anymore, thread_isolate()
176 * is forced to wait till all waiters finish.
177 */
178 _HA_ATOMIC_AND(&threads_sync_mask, ~tid_bit);
179 while (threads_sync_mask & all_threads_mask)
180 ha_thread_relax();
181}
182
Willy Tarreaud10385a2021-10-06 22:22:40 +0200183/* Sets up threads, signals and masks, and starts threads 2 and above.
184 * Does nothing when threads are disabled.
185 */
186void setup_extra_threads(void *(*handler)(void *))
187{
188 sigset_t blocked_sig, old_sig;
189 int i;
190
191 /* ensure the signals will be blocked in every thread */
192 sigfillset(&blocked_sig);
193 sigdelset(&blocked_sig, SIGPROF);
194 sigdelset(&blocked_sig, SIGBUS);
195 sigdelset(&blocked_sig, SIGFPE);
196 sigdelset(&blocked_sig, SIGILL);
197 sigdelset(&blocked_sig, SIGSEGV);
198 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
199
200 /* Create nbthread-1 thread. The first thread is the current process */
201 ha_thread_info[0].pthread = pthread_self();
202 for (i = 1; i < global.nbthread; i++)
203 pthread_create(&ha_thread_info[i].pthread, NULL, handler, (void *)(long)i);
204}
205
206/* waits for all threads to terminate. Does nothing when threads are
207 * disabled.
208 */
209void wait_for_threads_completion()
210{
211 int i;
212
213 /* Wait the end of other threads */
214 for (i = 1; i < global.nbthread; i++)
215 pthread_join(ha_thread_info[i].pthread, NULL);
216
217#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
218 show_lock_stats();
219#endif
220}
221
222/* Tries to set the current thread's CPU affinity according to the cpu_map */
223void set_thread_cpu_affinity()
224{
225#if defined(USE_CPU_AFFINITY)
226 /* no affinity setting for the master process */
227 if (master)
228 return;
229
230 /* Now the CPU affinity for all threads */
231 if (ha_cpuset_count(&cpu_map.proc))
232 ha_cpuset_and(&cpu_map.thread[tid], &cpu_map.proc);
233
234 if (ha_cpuset_count(&cpu_map.thread[tid])) {/* only do this if the thread has a THREAD map */
235# if defined(__APPLE__)
236 /* Note: this API is limited to the first 32/64 CPUs */
237 unsigned long set = cpu_map.thread[tid].cpuset;
238 int j;
239
240 while ((j = ffsl(set)) > 0) {
241 thread_affinity_policy_data_t cpu_set = { j - 1 };
242 thread_port_t mthread;
243
244 mthread = pthread_mach_thread_np(ha_thread_info[tid].pthread);
245 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
246 set &= ~(1UL << (j - 1));
247 }
248# else
249 struct hap_cpuset *set = &cpu_map.thread[tid];
250
251 pthread_setaffinity_np(ha_thread_info[tid].pthread, sizeof(set->cpuset), &set->cpuset);
252# endif
253 }
254#endif /* USE_CPU_AFFINITY */
255}
256
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200257/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
258 * since POSIX took great care of not specifying its representation, making it
259 * hard to export for post-mortem analysis. For this reason we copy it into a
260 * union and will use the smallest scalar type at least as large as its size,
261 * which will keep endianness and alignment for all regular sizes. As a last
262 * resort we end up with a long long ligned to the first bytes in memory, which
263 * will be endian-dependent if pthread_t is larger than a long long (not seen
264 * yet).
265 */
266unsigned long long ha_get_pthread_id(unsigned int thr)
267{
268 union {
269 pthread_t t;
270 unsigned long long ll;
271 unsigned int i;
272 unsigned short s;
273 unsigned char c;
274 } u = { 0 };
275
276 u.t = ha_thread_info[thr].pthread;
277
278 if (sizeof(u.t) <= sizeof(u.c))
279 return u.c;
280 else if (sizeof(u.t) <= sizeof(u.s))
281 return u.s;
282 else if (sizeof(u.t) <= sizeof(u.i))
283 return u.i;
284 return u.ll;
285}
286
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200287/* send signal <sig> to thread <thr> */
288void ha_tkill(unsigned int thr, int sig)
289{
David Carliera92c5ce2019-09-13 05:03:12 +0100290 pthread_kill(ha_thread_info[thr].pthread, sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200291}
292
293/* send signal <sig> to all threads. The calling thread is signaled last in
294 * order to allow all threads to synchronize in the handler.
295 */
296void ha_tkillall(int sig)
297{
298 unsigned int thr;
299
300 for (thr = 0; thr < global.nbthread; thr++) {
301 if (!(all_threads_mask & (1UL << thr)))
302 continue;
303 if (thr == tid)
304 continue;
David Carliera92c5ce2019-09-13 05:03:12 +0100305 pthread_kill(ha_thread_info[thr].pthread, sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200306 }
307 raise(sig);
308}
309
Willy Tarreau3d184982020-10-18 10:20:59 +0200310/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100311void ha_spin_init(HA_SPINLOCK_T *l)
312{
313 HA_SPIN_INIT(l);
314}
315
Willy Tarreau3d184982020-10-18 10:20:59 +0200316/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100317void ha_rwlock_init(HA_RWLOCK_T *l)
318{
319 HA_RWLOCK_INIT(l);
320}
321
Willy Tarreau149ab772019-01-26 14:27:06 +0100322/* returns the number of CPUs the current process is enabled to run on */
323static int thread_cpus_enabled()
324{
325 int ret = 1;
326
327#ifdef USE_CPU_AFFINITY
328#if defined(__linux__) && defined(CPU_COUNT)
329 cpu_set_t mask;
330
331 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
332 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200333#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
334 cpuset_t cpuset;
335 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
336 sizeof(cpuset), &cpuset) == 0)
337 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000338#elif defined(__APPLE__)
339 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100340#endif
341#endif
342 ret = MAX(ret, 1);
343 ret = MIN(ret, MAX_THREADS);
344 return ret;
345}
346
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200347/* Returns 1 if the cpu set is currently restricted for the process else 0.
348 * Currently only implemented for the Linux platform.
349 */
350int thread_cpu_mask_forced()
351{
352#if defined(__linux__)
353 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
354 return cpus_avail != thread_cpus_enabled();
355#else
356 return 0;
357#endif
358}
359
Willy Tarreau407ef892021-10-05 18:39:27 +0200360/* Below come the lock-debugging functions */
361
362#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
363
364struct lock_stat lock_stats[LOCK_LABELS];
365
366/* this is only used below */
367static const char *lock_label(enum lock_label label)
368{
369 switch (label) {
370 case TASK_RQ_LOCK: return "TASK_RQ";
371 case TASK_WQ_LOCK: return "TASK_WQ";
372 case LISTENER_LOCK: return "LISTENER";
373 case PROXY_LOCK: return "PROXY";
374 case SERVER_LOCK: return "SERVER";
375 case LBPRM_LOCK: return "LBPRM";
376 case SIGNALS_LOCK: return "SIGNALS";
377 case STK_TABLE_LOCK: return "STK_TABLE";
378 case STK_SESS_LOCK: return "STK_SESS";
379 case APPLETS_LOCK: return "APPLETS";
380 case PEER_LOCK: return "PEER";
381 case SHCTX_LOCK: return "SHCTX";
382 case SSL_LOCK: return "SSL";
383 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
384 case PATREF_LOCK: return "PATREF";
385 case PATEXP_LOCK: return "PATEXP";
386 case VARS_LOCK: return "VARS";
387 case COMP_POOL_LOCK: return "COMP_POOL";
388 case LUA_LOCK: return "LUA";
389 case NOTIF_LOCK: return "NOTIF";
390 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
391 case DNS_LOCK: return "DNS";
392 case PID_LIST_LOCK: return "PID_LIST";
393 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
394 case PIPES_LOCK: return "PIPES";
395 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
396 case AUTH_LOCK: return "AUTH";
397 case LOGSRV_LOCK: return "LOGSRV";
398 case DICT_LOCK: return "DICT";
399 case PROTO_LOCK: return "PROTO";
400 case QUEUE_LOCK: return "QUEUE";
401 case CKCH_LOCK: return "CKCH";
402 case SNI_LOCK: return "SNI";
403 case SSL_SERVER_LOCK: return "SSL_SERVER";
404 case SFT_LOCK: return "SFT";
405 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
406 case QUIC_LOCK: return "QUIC";
407 case OTHER_LOCK: return "OTHER";
408 case DEBUG1_LOCK: return "DEBUG1";
409 case DEBUG2_LOCK: return "DEBUG2";
410 case DEBUG3_LOCK: return "DEBUG3";
411 case DEBUG4_LOCK: return "DEBUG4";
412 case DEBUG5_LOCK: return "DEBUG5";
413 case LOCK_LABELS: break; /* keep compiler happy */
414 };
415 /* only way to come here is consecutive to an internal bug */
416 abort();
417}
418
419void show_lock_stats()
420{
421 int lbl;
422
423 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
424 if (!lock_stats[lbl].num_write_locked &&
425 !lock_stats[lbl].num_seek_locked &&
426 !lock_stats[lbl].num_read_locked) {
427 fprintf(stderr,
428 "Stats about Lock %s: not used\n",
429 lock_label(lbl));
430 continue;
431 }
432
433 fprintf(stderr,
434 "Stats about Lock %s: \n",
435 lock_label(lbl));
436
437 if (lock_stats[lbl].num_write_locked)
438 fprintf(stderr,
439 "\t # write lock : %lu\n"
440 "\t # write unlock: %lu (%ld)\n"
441 "\t # wait time for write : %.3f msec\n"
442 "\t # wait time for write/lock: %.3f nsec\n",
443 lock_stats[lbl].num_write_locked,
444 lock_stats[lbl].num_write_unlocked,
445 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
446 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
447 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
448
449 if (lock_stats[lbl].num_seek_locked)
450 fprintf(stderr,
451 "\t # seek lock : %lu\n"
452 "\t # seek unlock : %lu (%ld)\n"
453 "\t # wait time for seek : %.3f msec\n"
454 "\t # wait time for seek/lock : %.3f nsec\n",
455 lock_stats[lbl].num_seek_locked,
456 lock_stats[lbl].num_seek_unlocked,
457 lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked,
458 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
459 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
460
461 if (lock_stats[lbl].num_read_locked)
462 fprintf(stderr,
463 "\t # read lock : %lu\n"
464 "\t # read unlock : %lu (%ld)\n"
465 "\t # wait time for read : %.3f msec\n"
466 "\t # wait time for read/lock : %.3f nsec\n",
467 lock_stats[lbl].num_read_locked,
468 lock_stats[lbl].num_read_unlocked,
469 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
470 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
471 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
472 }
473}
474
Willy Tarreau407ef892021-10-05 18:39:27 +0200475void __ha_rwlock_init(struct ha_rwlock *l)
476{
477 memset(l, 0, sizeof(struct ha_rwlock));
478 __RWLOCK_INIT(&l->lock);
479}
480
481void __ha_rwlock_destroy(struct ha_rwlock *l)
482{
483 __RWLOCK_DESTROY(&l->lock);
484 memset(l, 0, sizeof(struct ha_rwlock));
485}
486
487
488void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
489 const char *func, const char *file, int line)
490{
491 uint64_t start_time;
492
493 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
494 abort();
495
496 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
497
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200498 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200499 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200500 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200501
502 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
503
504 l->info.cur_writer = tid_bit;
505 l->info.last_location.function = func;
506 l->info.last_location.file = file;
507 l->info.last_location.line = line;
508
509 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
510}
511
512int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
513 const char *func, const char *file, int line)
514{
515 uint64_t start_time;
516 int r;
517
518 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
519 abort();
520
521 /* We set waiting writer because trywrlock could wait for readers to quit */
522 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
523
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200524 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200525 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200526 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200527 if (unlikely(r)) {
528 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
529 return r;
530 }
531 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
532
533 l->info.cur_writer = tid_bit;
534 l->info.last_location.function = func;
535 l->info.last_location.file = file;
536 l->info.last_location.line = line;
537
538 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
539
540 return 0;
541}
542
543void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
544 const char *func, const char *file, int line)
545{
546 if (unlikely(!(l->info.cur_writer & tid_bit))) {
547 /* the thread is not owning the lock for write */
548 abort();
549 }
550
551 l->info.cur_writer = 0;
552 l->info.last_location.function = func;
553 l->info.last_location.file = file;
554 l->info.last_location.line = line;
555
556 __RWLOCK_WRUNLOCK(&l->lock);
557
558 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
559}
560
561void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
562{
563 uint64_t start_time;
564
565 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
566 abort();
567
568 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
569
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200570 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200571 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200572 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200573 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
574
575 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
576
577 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
578}
579
580int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
581{
582 int r;
583
584 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
585 abort();
586
587 /* try read should never wait */
588 r = __RWLOCK_TRYRDLOCK(&l->lock);
589 if (unlikely(r))
590 return r;
591 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
592
593 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
594
595 return 0;
596}
597
598void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
599{
600 if (unlikely(!(l->info.cur_readers & tid_bit))) {
601 /* the thread is not owning the lock for read */
602 abort();
603 }
604
605 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
606
607 __RWLOCK_RDUNLOCK(&l->lock);
608
609 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
610}
611
612void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
613 const char *func, const char *file, int line)
614{
615 uint64_t start_time;
616
617 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
618 abort();
619
620 if (!(l->info.cur_writer & tid_bit))
621 abort();
622
623 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
624
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200625 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200626 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200627 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200628
629 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
630
631 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
632 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
633 l->info.last_location.function = func;
634 l->info.last_location.file = file;
635 l->info.last_location.line = line;
636
637 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
638}
639
640void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
641 const char *func, const char *file, int line)
642{
643 uint64_t start_time;
644
645 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
646 abort();
647
648 if (!(l->info.cur_writer & tid_bit))
649 abort();
650
651 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
652
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200653 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200654 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200655 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200656
657 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
658
659 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
660 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
661 l->info.last_location.function = func;
662 l->info.last_location.file = file;
663 l->info.last_location.line = line;
664
665 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
666}
667
668void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
669 const char *func, const char *file, int line)
670{
671 uint64_t start_time;
672
673 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
674 abort();
675
676 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
677
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200678 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200679 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200680 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200681
682 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
683
684 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
685 l->info.last_location.function = func;
686 l->info.last_location.file = file;
687 l->info.last_location.line = line;
688
689 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
690}
691
692void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
693 const char *func, const char *file, int line)
694{
695 uint64_t start_time;
696
697 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
698 abort();
699
700 if (!(l->info.cur_seeker & tid_bit))
701 abort();
702
703 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
704
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200705 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200706 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200707 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200708
709 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
710
711 HA_ATOMIC_OR(&l->info.cur_writer, tid_bit);
712 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
713 l->info.last_location.function = func;
714 l->info.last_location.file = file;
715 l->info.last_location.line = line;
716
717 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
718}
719
720void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
721 const char *func, const char *file, int line)
722{
723 uint64_t start_time;
724
725 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
726 abort();
727
728 if (!(l->info.cur_seeker & tid_bit))
729 abort();
730
731 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
732
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200733 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200734 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200735 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200736
737 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
738
739 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
740 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
741 l->info.last_location.function = func;
742 l->info.last_location.file = file;
743 l->info.last_location.line = line;
744
745 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
746}
747
748void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
749 const char *func, const char *file, int line)
750{
751 if (!(l->info.cur_seeker & tid_bit))
752 abort();
753
754 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
755 l->info.last_location.function = func;
756 l->info.last_location.file = file;
757 l->info.last_location.line = line;
758
759 __RWLOCK_SKUNLOCK(&l->lock);
760
761 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
762}
763
764int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
765 const char *func, const char *file, int line)
766{
767 uint64_t start_time;
768 int r;
769
770 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
771 abort();
772
773 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
774
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200775 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200776 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200777 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200778
779 if (likely(!r)) {
780 /* got the lock ! */
781 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
782 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
783 l->info.last_location.function = func;
784 l->info.last_location.file = file;
785 l->info.last_location.line = line;
786 }
787
788 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
789 return r;
790}
791
792int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
793 const char *func, const char *file, int line)
794{
795 uint64_t start_time;
796 int r;
797
798 if ((l->info.cur_writer | l->info.cur_seeker) & tid_bit)
799 abort();
800
801 if (!(l->info.cur_readers & tid_bit))
802 abort();
803
804 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
805
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200806 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200807 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200808 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200809
810 if (likely(!r)) {
811 /* got the lock ! */
812 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
813 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
814 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
815 l->info.last_location.function = func;
816 l->info.last_location.file = file;
817 l->info.last_location.line = line;
818 }
819
820 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
821 return r;
822}
823
824void __spin_init(struct ha_spinlock *l)
825{
826 memset(l, 0, sizeof(struct ha_spinlock));
827 __SPIN_INIT(&l->lock);
828}
829
830void __spin_destroy(struct ha_spinlock *l)
831{
832 __SPIN_DESTROY(&l->lock);
833 memset(l, 0, sizeof(struct ha_spinlock));
834}
835
836void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
837 const char *func, const char *file, int line)
838{
839 uint64_t start_time;
840
841 if (unlikely(l->info.owner & tid_bit)) {
842 /* the thread is already owning the lock */
843 abort();
844 }
845
846 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
847
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200848 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200849 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200850 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200851
852 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
853
854
855 l->info.owner = tid_bit;
856 l->info.last_location.function = func;
857 l->info.last_location.file = file;
858 l->info.last_location.line = line;
859
860 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
861}
862
863int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
864 const char *func, const char *file, int line)
865{
866 int r;
867
868 if (unlikely(l->info.owner & tid_bit)) {
869 /* the thread is already owning the lock */
870 abort();
871 }
872
873 /* try read should never wait */
874 r = __SPIN_TRYLOCK(&l->lock);
875 if (unlikely(r))
876 return r;
877 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
878
879 l->info.owner = tid_bit;
880 l->info.last_location.function = func;
881 l->info.last_location.file = file;
882 l->info.last_location.line = line;
883
884 return 0;
885}
886
887void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
888 const char *func, const char *file, int line)
889{
890 if (unlikely(!(l->info.owner & tid_bit))) {
891 /* the thread is not owning the lock */
892 abort();
893 }
894
895 l->info.owner = 0;
896 l->info.last_location.function = func;
897 l->info.last_location.file = file;
898 l->info.last_location.line = line;
899
900 __SPIN_UNLOCK(&l->lock);
901 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
902}
903
904#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
905
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200906/* Depending on the platform and how libpthread was built, pthread_exit() may
907 * involve some code in libgcc_s that would be loaded on exit for the first
908 * time, causing aborts if the process is chrooted. It's harmless bit very
909 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
910 * needed, so what we do here is that during early boot we create a dummy
911 * thread that immediately exits. This will lead to libgcc_s being loaded
912 * during boot on the platforms where it's required.
913 */
914static void *dummy_thread_function(void *data)
915{
916 pthread_exit(NULL);
917 return NULL;
918}
919
920static inline void preload_libgcc_s(void)
921{
922 pthread_t dummy_thread;
923 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
924 pthread_join(dummy_thread, NULL);
925}
926
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200927__attribute__((constructor))
Willy Tarreau3f567e42020-05-28 15:29:19 +0200928static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200929{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100930 char *ptr = NULL;
931
932 if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) {
933 ha_alert("MAX_THREADS value must be between 1 and %d inclusive; "
934 "HAProxy was built with value %d, please fix it and rebuild.\n",
935 LONGBITS, MAX_THREADS);
936 exit(1);
937 }
Willy Tarreau149ab772019-01-26 14:27:06 +0100938
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200939 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +0200940
Willy Tarreau149ab772019-01-26 14:27:06 +0100941 thread_cpus_enabled_at_boot = thread_cpus_enabled();
942
943 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
944 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100945 hap_register_build_opts(ptr, 1);
946
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200947#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
948 memset(lock_stats, 0, sizeof(lock_stats));
949#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200950}
951
Willy Tarreau8459f252018-12-15 16:48:14 +0100952#else
953
954REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
955
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200956#endif // USE_THREAD
957
958
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200959/* Parse the "nbthread" global directive, which takes an integer argument that
960 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200961 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200962static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
963 const struct proxy *defpx, const char *file, int line,
964 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200965{
966 long nbthread;
967 char *errptr;
968
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200969 if (too_many_args(1, args, err, NULL))
970 return -1;
971
972 nbthread = strtol(args[1], &errptr, 10);
973 if (!*args[1] || *errptr) {
974 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
975 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200976 }
977
978#ifndef USE_THREAD
979 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200980 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]);
981 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200982 }
983#else
984 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200985 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
986 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200987 }
988
Willy Tarreaufc647362019-02-02 17:05:03 +0100989 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200990#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200991
992 HA_DIAG_WARNING_COND(global.nbthread,
993 "parsing [%s:%d] : nbthread is already defined and will be overridden.\n",
994 file, line);
995
996 global.nbthread = nbthread;
997 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200998}
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200999
1000/* config keyword parsers */
1001static struct cfg_kw_list cfg_kws = {ILH, {
1002 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
1003 { 0, NULL, NULL }
1004}};
1005
1006INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);