blob: a6e76c39f226ca23a7fb2697f9a22bc615ac8e59 [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
19#include <sched.h>
20#endif
21
Olivier Houchard46453d32019-04-11 00:06:47 +020022#ifdef __FreeBSD__
23#include <sys/cpuset.h>
24#endif
25
Willy Tarreau6be78492020-06-05 00:00:29 +020026#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020027#include <haproxy/fd.h>
28#include <haproxy/global.h>
Willy Tarreau11bd6f72021-05-08 20:33:02 +020029#include <haproxy/log.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020030#include <haproxy/thread.h>
Willy Tarreaudced3eb2021-10-05 18:48:23 +020031#include <haproxy/time.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020032#include <haproxy/tools.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020033
David Carliera92c5ce2019-09-13 05:03:12 +010034struct thread_info ha_thread_info[MAX_THREADS] = { };
35THREAD_LOCAL struct thread_info *ti = &ha_thread_info[0];
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020036
37#ifdef USE_THREAD
38
Willy Tarreau56c3b8b2021-04-10 17:28:18 +020039volatile unsigned long threads_want_rdv_mask __read_mostly = 0;
Willy Tarreau60b639c2018-08-02 10:16:17 +020040volatile unsigned long threads_harmless_mask = 0;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +020041volatile unsigned long threads_idle_mask = 0;
Willy Tarreau9a1f5732019-06-09 12:20:02 +020042volatile unsigned long threads_sync_mask = 0;
Willy Tarreau56c3b8b2021-04-10 17:28:18 +020043volatile unsigned long all_threads_mask __read_mostly = 1; // nbthread 1 assumed by default
Willy Tarreau0c026f42018-08-01 19:12:20 +020044THREAD_LOCAL unsigned int tid = 0;
45THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
Willy Tarreau149ab772019-01-26 14:27:06 +010046int thread_cpus_enabled_at_boot = 1;
Willy Tarreau0c026f42018-08-01 19:12:20 +020047
Willy Tarreau60b639c2018-08-02 10:16:17 +020048/* Marks the thread as harmless until the last thread using the rendez-vous
Christopher Fauleta9a9e9a2021-03-25 14:11:36 +010049 * point quits, excluding the current one. Thus an isolated thread may be safely
50 * marked as harmless. Given that we can wait for a long time, sched_yield() is
51 * used when available to offer the CPU resources to competing threads if
52 * needed.
Willy Tarreau60b639c2018-08-02 10:16:17 +020053 */
54void thread_harmless_till_end()
55{
Willy Tarreau286363b2021-08-04 10:33:57 +020056 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
57 while (threads_want_rdv_mask & all_threads_mask & ~tid_bit) {
58 ha_thread_relax();
59 }
Willy Tarreau60b639c2018-08-02 10:16:17 +020060}
61
62/* Isolates the current thread : request the ability to work while all other
Willy Tarreauf519cfa2021-08-04 11:22:07 +020063 * threads are harmless, as defined by thread_harmless_now() (i.e. they're not
64 * going to touch any visible memory area). Only returns once all of them are
65 * harmless, with the current thread's bit in threads_harmless_mask cleared.
66 * Needs to be completed using thread_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +020067 */
68void thread_isolate()
69{
70 unsigned long old;
71
Olivier Houchardb23a61f2019-03-08 18:51:17 +010072 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
73 __ha_barrier_atomic_store();
74 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +020075
76 /* wait for all threads to become harmless */
77 old = threads_harmless_mask;
78 while (1) {
79 if (unlikely((old & all_threads_mask) != all_threads_mask))
80 old = threads_harmless_mask;
Olivier Houchardb23a61f2019-03-08 18:51:17 +010081 else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
Willy Tarreau60b639c2018-08-02 10:16:17 +020082 break;
83
Willy Tarreau38171da2019-05-17 16:33:13 +020084 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +020085 }
86 /* one thread gets released at a time here, with its harmess bit off.
87 * The loss of this bit makes the other one continue to spin while the
88 * thread is working alone.
89 */
90}
91
Willy Tarreau88d1c5d2021-08-04 11:44:17 +020092/* Isolates the current thread : request the ability to work while all other
93 * threads are idle, as defined by thread_idle_now(). It only returns once
94 * all of them are both harmless and idle, with the current thread's bit in
95 * threads_harmless_mask and idle_mask cleared. Needs to be completed using
96 * thread_release(). By doing so the thread also engages in being safe against
97 * any actions that other threads might be about to start under the same
98 * conditions. This specifically targets destruction of any internal structure,
99 * which implies that the current thread may not hold references to any object.
100 *
101 * Note that a concurrent thread_isolate() will usually win against
102 * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to
103 * get back to the poller or any other fully idle location, that will
104 * ultimately release this one.
105 */
106void thread_isolate_full()
107{
108 unsigned long old;
109
110 _HA_ATOMIC_OR(&threads_idle_mask, tid_bit);
111 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
112 __ha_barrier_atomic_store();
113 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
114
115 /* wait for all threads to become harmless */
116 old = threads_harmless_mask;
117 while (1) {
118 unsigned long idle = _HA_ATOMIC_LOAD(&threads_idle_mask);
119
120 if (unlikely((old & all_threads_mask) != all_threads_mask))
121 old = _HA_ATOMIC_LOAD(&threads_harmless_mask);
122 else if ((idle & all_threads_mask) == all_threads_mask &&
123 _HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
124 break;
125
126 ha_thread_relax();
127 }
128
129 /* we're not idle anymore at this point. Other threads waiting on this
130 * condition will need to wait until out next pass to the poller, or
131 * our next call to thread_isolate_full().
132 */
133 _HA_ATOMIC_AND(&threads_idle_mask, ~tid_bit);
134}
135
Willy Tarreau60b639c2018-08-02 10:16:17 +0200136/* Cancels the effect of thread_isolate() by releasing the current thread's bit
Willy Tarreauf519cfa2021-08-04 11:22:07 +0200137 * in threads_want_rdv_mask. This immediately allows other threads to expect be
138 * executed, though they will first have to wait for this thread to become
139 * harmless again (possibly by reaching the poller again).
Willy Tarreau60b639c2018-08-02 10:16:17 +0200140 */
141void thread_release()
142{
Olivier Houchardb23a61f2019-03-08 18:51:17 +0100143 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200144}
Christopher Faulet339fff82017-10-19 11:59:15 +0200145
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200146/* Cancels the effect of thread_isolate() by releasing the current thread's bit
147 * in threads_want_rdv_mask and by marking this thread as harmless until the
148 * last worker finishes. The difference with thread_release() is that this one
149 * will not leave the function before others are notified to do the same, so it
150 * guarantees that the current thread will not pass through a subsequent call
151 * to thread_isolate() before others finish.
152 */
153void thread_sync_release()
154{
155 _HA_ATOMIC_OR(&threads_sync_mask, tid_bit);
156 __ha_barrier_atomic_store();
157 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
158
159 while (threads_want_rdv_mask & all_threads_mask) {
160 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
161 while (threads_want_rdv_mask & all_threads_mask)
162 ha_thread_relax();
163 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
164 }
165
166 /* the current thread is not harmless anymore, thread_isolate()
167 * is forced to wait till all waiters finish.
168 */
169 _HA_ATOMIC_AND(&threads_sync_mask, ~tid_bit);
170 while (threads_sync_mask & all_threads_mask)
171 ha_thread_relax();
172}
173
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200174/* send signal <sig> to thread <thr> */
175void ha_tkill(unsigned int thr, int sig)
176{
David Carliera92c5ce2019-09-13 05:03:12 +0100177 pthread_kill(ha_thread_info[thr].pthread, sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200178}
179
180/* send signal <sig> to all threads. The calling thread is signaled last in
181 * order to allow all threads to synchronize in the handler.
182 */
183void ha_tkillall(int sig)
184{
185 unsigned int thr;
186
187 for (thr = 0; thr < global.nbthread; thr++) {
188 if (!(all_threads_mask & (1UL << thr)))
189 continue;
190 if (thr == tid)
191 continue;
David Carliera92c5ce2019-09-13 05:03:12 +0100192 pthread_kill(ha_thread_info[thr].pthread, sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200193 }
194 raise(sig);
195}
196
Willy Tarreau3d184982020-10-18 10:20:59 +0200197/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100198void ha_spin_init(HA_SPINLOCK_T *l)
199{
200 HA_SPIN_INIT(l);
201}
202
Willy Tarreau3d184982020-10-18 10:20:59 +0200203/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100204void ha_rwlock_init(HA_RWLOCK_T *l)
205{
206 HA_RWLOCK_INIT(l);
207}
208
Willy Tarreau149ab772019-01-26 14:27:06 +0100209/* returns the number of CPUs the current process is enabled to run on */
210static int thread_cpus_enabled()
211{
212 int ret = 1;
213
214#ifdef USE_CPU_AFFINITY
215#if defined(__linux__) && defined(CPU_COUNT)
216 cpu_set_t mask;
217
218 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
219 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200220#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
221 cpuset_t cpuset;
222 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
223 sizeof(cpuset), &cpuset) == 0)
224 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000225#elif defined(__APPLE__)
226 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100227#endif
228#endif
229 ret = MAX(ret, 1);
230 ret = MIN(ret, MAX_THREADS);
231 return ret;
232}
233
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200234/* Returns 1 if the cpu set is currently restricted for the process else 0.
235 * Currently only implemented for the Linux platform.
236 */
237int thread_cpu_mask_forced()
238{
239#if defined(__linux__)
240 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
241 return cpus_avail != thread_cpus_enabled();
242#else
243 return 0;
244#endif
245}
246
Willy Tarreau407ef892021-10-05 18:39:27 +0200247/* Below come the lock-debugging functions */
248
249#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
250
251struct lock_stat lock_stats[LOCK_LABELS];
252
253/* this is only used below */
254static const char *lock_label(enum lock_label label)
255{
256 switch (label) {
257 case TASK_RQ_LOCK: return "TASK_RQ";
258 case TASK_WQ_LOCK: return "TASK_WQ";
259 case LISTENER_LOCK: return "LISTENER";
260 case PROXY_LOCK: return "PROXY";
261 case SERVER_LOCK: return "SERVER";
262 case LBPRM_LOCK: return "LBPRM";
263 case SIGNALS_LOCK: return "SIGNALS";
264 case STK_TABLE_LOCK: return "STK_TABLE";
265 case STK_SESS_LOCK: return "STK_SESS";
266 case APPLETS_LOCK: return "APPLETS";
267 case PEER_LOCK: return "PEER";
268 case SHCTX_LOCK: return "SHCTX";
269 case SSL_LOCK: return "SSL";
270 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
271 case PATREF_LOCK: return "PATREF";
272 case PATEXP_LOCK: return "PATEXP";
273 case VARS_LOCK: return "VARS";
274 case COMP_POOL_LOCK: return "COMP_POOL";
275 case LUA_LOCK: return "LUA";
276 case NOTIF_LOCK: return "NOTIF";
277 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
278 case DNS_LOCK: return "DNS";
279 case PID_LIST_LOCK: return "PID_LIST";
280 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
281 case PIPES_LOCK: return "PIPES";
282 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
283 case AUTH_LOCK: return "AUTH";
284 case LOGSRV_LOCK: return "LOGSRV";
285 case DICT_LOCK: return "DICT";
286 case PROTO_LOCK: return "PROTO";
287 case QUEUE_LOCK: return "QUEUE";
288 case CKCH_LOCK: return "CKCH";
289 case SNI_LOCK: return "SNI";
290 case SSL_SERVER_LOCK: return "SSL_SERVER";
291 case SFT_LOCK: return "SFT";
292 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
293 case QUIC_LOCK: return "QUIC";
294 case OTHER_LOCK: return "OTHER";
295 case DEBUG1_LOCK: return "DEBUG1";
296 case DEBUG2_LOCK: return "DEBUG2";
297 case DEBUG3_LOCK: return "DEBUG3";
298 case DEBUG4_LOCK: return "DEBUG4";
299 case DEBUG5_LOCK: return "DEBUG5";
300 case LOCK_LABELS: break; /* keep compiler happy */
301 };
302 /* only way to come here is consecutive to an internal bug */
303 abort();
304}
305
306void show_lock_stats()
307{
308 int lbl;
309
310 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
311 if (!lock_stats[lbl].num_write_locked &&
312 !lock_stats[lbl].num_seek_locked &&
313 !lock_stats[lbl].num_read_locked) {
314 fprintf(stderr,
315 "Stats about Lock %s: not used\n",
316 lock_label(lbl));
317 continue;
318 }
319
320 fprintf(stderr,
321 "Stats about Lock %s: \n",
322 lock_label(lbl));
323
324 if (lock_stats[lbl].num_write_locked)
325 fprintf(stderr,
326 "\t # write lock : %lu\n"
327 "\t # write unlock: %lu (%ld)\n"
328 "\t # wait time for write : %.3f msec\n"
329 "\t # wait time for write/lock: %.3f nsec\n",
330 lock_stats[lbl].num_write_locked,
331 lock_stats[lbl].num_write_unlocked,
332 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
333 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
334 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
335
336 if (lock_stats[lbl].num_seek_locked)
337 fprintf(stderr,
338 "\t # seek lock : %lu\n"
339 "\t # seek unlock : %lu (%ld)\n"
340 "\t # wait time for seek : %.3f msec\n"
341 "\t # wait time for seek/lock : %.3f nsec\n",
342 lock_stats[lbl].num_seek_locked,
343 lock_stats[lbl].num_seek_unlocked,
344 lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked,
345 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
346 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
347
348 if (lock_stats[lbl].num_read_locked)
349 fprintf(stderr,
350 "\t # read lock : %lu\n"
351 "\t # read unlock : %lu (%ld)\n"
352 "\t # wait time for read : %.3f msec\n"
353 "\t # wait time for read/lock : %.3f nsec\n",
354 lock_stats[lbl].num_read_locked,
355 lock_stats[lbl].num_read_unlocked,
356 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
357 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
358 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
359 }
360}
361
Willy Tarreau407ef892021-10-05 18:39:27 +0200362void __ha_rwlock_init(struct ha_rwlock *l)
363{
364 memset(l, 0, sizeof(struct ha_rwlock));
365 __RWLOCK_INIT(&l->lock);
366}
367
368void __ha_rwlock_destroy(struct ha_rwlock *l)
369{
370 __RWLOCK_DESTROY(&l->lock);
371 memset(l, 0, sizeof(struct ha_rwlock));
372}
373
374
375void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
376 const char *func, const char *file, int line)
377{
378 uint64_t start_time;
379
380 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
381 abort();
382
383 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
384
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200385 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200386 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200387 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200388
389 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
390
391 l->info.cur_writer = tid_bit;
392 l->info.last_location.function = func;
393 l->info.last_location.file = file;
394 l->info.last_location.line = line;
395
396 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
397}
398
399int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
400 const char *func, const char *file, int line)
401{
402 uint64_t start_time;
403 int r;
404
405 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
406 abort();
407
408 /* We set waiting writer because trywrlock could wait for readers to quit */
409 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
410
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200411 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200412 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200413 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200414 if (unlikely(r)) {
415 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
416 return r;
417 }
418 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
419
420 l->info.cur_writer = tid_bit;
421 l->info.last_location.function = func;
422 l->info.last_location.file = file;
423 l->info.last_location.line = line;
424
425 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
426
427 return 0;
428}
429
430void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
431 const char *func, const char *file, int line)
432{
433 if (unlikely(!(l->info.cur_writer & tid_bit))) {
434 /* the thread is not owning the lock for write */
435 abort();
436 }
437
438 l->info.cur_writer = 0;
439 l->info.last_location.function = func;
440 l->info.last_location.file = file;
441 l->info.last_location.line = line;
442
443 __RWLOCK_WRUNLOCK(&l->lock);
444
445 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
446}
447
448void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
449{
450 uint64_t start_time;
451
452 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
453 abort();
454
455 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
456
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200457 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200458 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200459 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200460 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
461
462 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
463
464 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
465}
466
467int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
468{
469 int r;
470
471 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
472 abort();
473
474 /* try read should never wait */
475 r = __RWLOCK_TRYRDLOCK(&l->lock);
476 if (unlikely(r))
477 return r;
478 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
479
480 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
481
482 return 0;
483}
484
485void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
486{
487 if (unlikely(!(l->info.cur_readers & tid_bit))) {
488 /* the thread is not owning the lock for read */
489 abort();
490 }
491
492 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
493
494 __RWLOCK_RDUNLOCK(&l->lock);
495
496 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
497}
498
499void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
500 const char *func, const char *file, int line)
501{
502 uint64_t start_time;
503
504 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
505 abort();
506
507 if (!(l->info.cur_writer & tid_bit))
508 abort();
509
510 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
511
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200512 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200513 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200514 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200515
516 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
517
518 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
519 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
520 l->info.last_location.function = func;
521 l->info.last_location.file = file;
522 l->info.last_location.line = line;
523
524 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
525}
526
527void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
528 const char *func, const char *file, int line)
529{
530 uint64_t start_time;
531
532 if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit)
533 abort();
534
535 if (!(l->info.cur_writer & tid_bit))
536 abort();
537
538 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
539
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200540 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200541 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200542 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200543
544 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
545
546 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
547 HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit);
548 l->info.last_location.function = func;
549 l->info.last_location.file = file;
550 l->info.last_location.line = line;
551
552 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
553}
554
555void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
556 const char *func, const char *file, int line)
557{
558 uint64_t start_time;
559
560 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
561 abort();
562
563 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
564
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200565 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200566 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200567 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200568
569 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
570
571 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
572 l->info.last_location.function = func;
573 l->info.last_location.file = file;
574 l->info.last_location.line = line;
575
576 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
577}
578
579void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
580 const char *func, const char *file, int line)
581{
582 uint64_t start_time;
583
584 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
585 abort();
586
587 if (!(l->info.cur_seeker & tid_bit))
588 abort();
589
590 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
591
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200592 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200593 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200594 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200595
596 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
597
598 HA_ATOMIC_OR(&l->info.cur_writer, tid_bit);
599 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
600 l->info.last_location.function = func;
601 l->info.last_location.file = file;
602 l->info.last_location.line = line;
603
604 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
605}
606
607void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
608 const char *func, const char *file, int line)
609{
610 uint64_t start_time;
611
612 if ((l->info.cur_readers | l->info.cur_writer) & tid_bit)
613 abort();
614
615 if (!(l->info.cur_seeker & tid_bit))
616 abort();
617
618 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
619
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200620 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200621 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200622 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200623
624 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
625
626 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
627 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
628 l->info.last_location.function = func;
629 l->info.last_location.file = file;
630 l->info.last_location.line = line;
631
632 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
633}
634
635void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
636 const char *func, const char *file, int line)
637{
638 if (!(l->info.cur_seeker & tid_bit))
639 abort();
640
641 HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit);
642 l->info.last_location.function = func;
643 l->info.last_location.file = file;
644 l->info.last_location.line = line;
645
646 __RWLOCK_SKUNLOCK(&l->lock);
647
648 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
649}
650
651int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
652 const char *func, const char *file, int line)
653{
654 uint64_t start_time;
655 int r;
656
657 if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit)
658 abort();
659
660 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
661
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200662 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200663 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200664 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200665
666 if (likely(!r)) {
667 /* got the lock ! */
668 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
669 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
670 l->info.last_location.function = func;
671 l->info.last_location.file = file;
672 l->info.last_location.line = line;
673 }
674
675 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
676 return r;
677}
678
679int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
680 const char *func, const char *file, int line)
681{
682 uint64_t start_time;
683 int r;
684
685 if ((l->info.cur_writer | l->info.cur_seeker) & tid_bit)
686 abort();
687
688 if (!(l->info.cur_readers & tid_bit))
689 abort();
690
691 HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit);
692
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200693 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200694 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200695 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200696
697 if (likely(!r)) {
698 /* got the lock ! */
699 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
700 HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit);
701 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
702 l->info.last_location.function = func;
703 l->info.last_location.file = file;
704 l->info.last_location.line = line;
705 }
706
707 HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit);
708 return r;
709}
710
711void __spin_init(struct ha_spinlock *l)
712{
713 memset(l, 0, sizeof(struct ha_spinlock));
714 __SPIN_INIT(&l->lock);
715}
716
717void __spin_destroy(struct ha_spinlock *l)
718{
719 __SPIN_DESTROY(&l->lock);
720 memset(l, 0, sizeof(struct ha_spinlock));
721}
722
723void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
724 const char *func, const char *file, int line)
725{
726 uint64_t start_time;
727
728 if (unlikely(l->info.owner & tid_bit)) {
729 /* the thread is already owning the lock */
730 abort();
731 }
732
733 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
734
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200735 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200736 __SPIN_LOCK(&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
742 l->info.owner = 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.waiters, ~tid_bit);
748}
749
750int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
751 const char *func, const char *file, int line)
752{
753 int r;
754
755 if (unlikely(l->info.owner & tid_bit)) {
756 /* the thread is already owning the lock */
757 abort();
758 }
759
760 /* try read should never wait */
761 r = __SPIN_TRYLOCK(&l->lock);
762 if (unlikely(r))
763 return r;
764 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
765
766 l->info.owner = tid_bit;
767 l->info.last_location.function = func;
768 l->info.last_location.file = file;
769 l->info.last_location.line = line;
770
771 return 0;
772}
773
774void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
775 const char *func, const char *file, int line)
776{
777 if (unlikely(!(l->info.owner & tid_bit))) {
778 /* the thread is not owning the lock */
779 abort();
780 }
781
782 l->info.owner = 0;
783 l->info.last_location.function = func;
784 l->info.last_location.file = file;
785 l->info.last_location.line = line;
786
787 __SPIN_UNLOCK(&l->lock);
788 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
789}
790
791#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
792
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200793/* Depending on the platform and how libpthread was built, pthread_exit() may
794 * involve some code in libgcc_s that would be loaded on exit for the first
795 * time, causing aborts if the process is chrooted. It's harmless bit very
796 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
797 * needed, so what we do here is that during early boot we create a dummy
798 * thread that immediately exits. This will lead to libgcc_s being loaded
799 * during boot on the platforms where it's required.
800 */
801static void *dummy_thread_function(void *data)
802{
803 pthread_exit(NULL);
804 return NULL;
805}
806
807static inline void preload_libgcc_s(void)
808{
809 pthread_t dummy_thread;
810 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
811 pthread_join(dummy_thread, NULL);
812}
813
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200814__attribute__((constructor))
Willy Tarreau3f567e42020-05-28 15:29:19 +0200815static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200816{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100817 char *ptr = NULL;
818
819 if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) {
820 ha_alert("MAX_THREADS value must be between 1 and %d inclusive; "
821 "HAProxy was built with value %d, please fix it and rebuild.\n",
822 LONGBITS, MAX_THREADS);
823 exit(1);
824 }
Willy Tarreau149ab772019-01-26 14:27:06 +0100825
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200826 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +0200827
Willy Tarreau149ab772019-01-26 14:27:06 +0100828 thread_cpus_enabled_at_boot = thread_cpus_enabled();
829
830 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
831 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100832 hap_register_build_opts(ptr, 1);
833
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200834#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
835 memset(lock_stats, 0, sizeof(lock_stats));
836#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200837}
838
Willy Tarreau8459f252018-12-15 16:48:14 +0100839#else
840
841REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
842
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200843#endif // USE_THREAD
844
845
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200846/* Parse the "nbthread" global directive, which takes an integer argument that
847 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200848 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200849static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
850 const struct proxy *defpx, const char *file, int line,
851 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200852{
853 long nbthread;
854 char *errptr;
855
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200856 if (too_many_args(1, args, err, NULL))
857 return -1;
858
859 nbthread = strtol(args[1], &errptr, 10);
860 if (!*args[1] || *errptr) {
861 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
862 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200863 }
864
865#ifndef USE_THREAD
866 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200867 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]);
868 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200869 }
870#else
871 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200872 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
873 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200874 }
875
Willy Tarreaufc647362019-02-02 17:05:03 +0100876 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200877#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200878
879 HA_DIAG_WARNING_COND(global.nbthread,
880 "parsing [%s:%d] : nbthread is already defined and will be overridden.\n",
881 file, line);
882
883 global.nbthread = nbthread;
884 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200885}
Willy Tarreau51ec03a2021-09-22 11:55:22 +0200886
887/* config keyword parsers */
888static struct cfg_kw_list cfg_kws = {ILH, {
889 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
890 { 0, NULL, NULL }
891}};
892
893INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);