blob: e38b7fea9741f8186815c42e99c84705ef81ce09 [file] [log] [blame]
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001/*
2 * functions about threads.
3 *
4 * Copyright (C) 2017 Christopher Fauet - cfaulet@haproxy.com
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau149ab772019-01-26 14:27:06 +010013#define _GNU_SOURCE
Christopher Faulet339fff82017-10-19 11:59:15 +020014#include <unistd.h>
Willy Tarreau0ccd3222018-07-30 10:34:35 +020015#include <stdlib.h>
Christopher Faulet339fff82017-10-19 11:59:15 +020016
Willy Tarreauaa992762021-10-06 23:33:20 +020017#include <signal.h>
18#include <unistd.h>
19#ifdef _POSIX_PRIORITY_SCHEDULING
20#include <sched.h>
21#endif
22
Willy Tarreau5e03dfa2021-10-06 22:53:51 +020023#ifdef USE_THREAD
24# include <pthread.h>
25#endif
26
Willy Tarreau149ab772019-01-26 14:27:06 +010027#ifdef USE_CPU_AFFINITY
Willy Tarreaud10385a2021-10-06 22:22:40 +020028# include <sched.h>
29# if defined(__FreeBSD__) || defined(__DragonFly__)
30# include <sys/param.h>
31# ifdef __FreeBSD__
32# include <sys/cpuset.h>
33# endif
34# include <pthread_np.h>
35# endif
36# ifdef __APPLE__
37# include <mach/mach_types.h>
38# include <mach/thread_act.h>
39# include <mach/thread_policy.h>
40# endif
41# include <haproxy/cpuset.h>
Willy Tarreau149ab772019-01-26 14:27:06 +010042#endif
43
Willy Tarreau6be78492020-06-05 00:00:29 +020044#include <haproxy/cfgparse.h>
Willy Tarreau55542642021-10-08 09:33:24 +020045#include <haproxy/clock.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/fd.h>
47#include <haproxy/global.h>
Willy Tarreau11bd6f72021-05-08 20:33:02 +020048#include <haproxy/log.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020049#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020050#include <haproxy/tools.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020051
Willy Tarreauf9662842021-09-13 18:11:26 +020052struct tgroup_info ha_tgroup_info[MAX_TGROUPS] = { };
53THREAD_LOCAL const struct tgroup_info *tg = &ha_tgroup_info[0];
54
David Carliera92c5ce2019-09-13 05:03:12 +010055struct thread_info ha_thread_info[MAX_THREADS] = { };
Willy Tarreau60363422021-10-01 16:29:27 +020056THREAD_LOCAL const struct thread_info *ti = &ha_thread_info[0];
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020057
Willy Tarreau03f9b352022-06-27 16:02:24 +020058struct tgroup_ctx ha_tgroup_ctx[MAX_TGROUPS] = { };
59THREAD_LOCAL struct tgroup_ctx *tg_ctx = &ha_tgroup_ctx[0];
60
Willy Tarreau1a9c9222021-10-01 11:30:33 +020061struct thread_ctx ha_thread_ctx[MAX_THREADS] = { };
62THREAD_LOCAL struct thread_ctx *th_ctx = &ha_thread_ctx[0];
63
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020064#ifdef USE_THREAD
65
Willy Tarreau56c3b8b2021-04-10 17:28:18 +020066volatile unsigned long all_threads_mask __read_mostly = 1; // nbthread 1 assumed by default
Willy Tarreaucce203a2022-06-24 15:55:11 +020067volatile unsigned long all_tgroups_mask __read_mostly = 1; // nbtgroup 1 assumed by default
Willy Tarreau598cf3f2022-07-01 15:08:37 +020068volatile unsigned int rdv_requests = 0; // total number of threads requesting RDV
69volatile unsigned int isolated_thread = ~0; // ID of the isolated thread, or ~0 when none
Willy Tarreaub90935c2021-09-30 08:00:11 +020070THREAD_LOCAL unsigned int tgid = 1; // thread ID starts at 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020071THREAD_LOCAL unsigned int tid = 0;
72THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
Willy Tarreau149ab772019-01-26 14:27:06 +010073int thread_cpus_enabled_at_boot = 1;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +020074static pthread_t ha_pthread[MAX_THREADS] = { };
Willy Tarreau0c026f42018-08-01 19:12:20 +020075
Willy Tarreau60b639c2018-08-02 10:16:17 +020076/* Marks the thread as harmless until the last thread using the rendez-vous
Willy Tarreau598cf3f2022-07-01 15:08:37 +020077 * point quits. Given that we can wait for a long time, sched_yield() is
Christopher Fauleta9a9e9a2021-03-25 14:11:36 +010078 * used when available to offer the CPU resources to competing threads if
79 * needed.
Willy Tarreau60b639c2018-08-02 10:16:17 +020080 */
81void thread_harmless_till_end()
82{
Willy Tarreau03f9b352022-06-27 16:02:24 +020083 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +020084 while (_HA_ATOMIC_LOAD(&rdv_requests) != 0) {
Willy Tarreau286363b2021-08-04 10:33:57 +020085 ha_thread_relax();
86 }
Willy Tarreau60b639c2018-08-02 10:16:17 +020087}
88
89/* Isolates the current thread : request the ability to work while all other
Willy Tarreauf519cfa2021-08-04 11:22:07 +020090 * threads are harmless, as defined by thread_harmless_now() (i.e. they're not
91 * going to touch any visible memory area). Only returns once all of them are
Willy Tarreau03f9b352022-06-27 16:02:24 +020092 * harmless, with the current thread's bit in &tg_ctx->threads_harmless cleared.
Willy Tarreauf519cfa2021-08-04 11:22:07 +020093 * Needs to be completed using thread_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +020094 */
95void thread_isolate()
96{
Willy Tarreau598cf3f2022-07-01 15:08:37 +020097 uint tgrp, thr;
Willy Tarreau60b639c2018-08-02 10:16:17 +020098
Willy Tarreau03f9b352022-06-27 16:02:24 +020099 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Olivier Houchardb23a61f2019-03-08 18:51:17 +0100100 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200101 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200102
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200103 /* wait for all threads to become harmless. They cannot change their
104 * mind once seen thanks to rdv_requests above, unless they pass in
105 * front of us.
106 */
Willy Tarreau60b639c2018-08-02 10:16:17 +0200107 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200108 for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) {
109 while ((_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless) &
110 ha_tgroup_info[tgrp].threads_enabled) != ha_tgroup_info[tgrp].threads_enabled)
111 ha_thread_relax();
112 }
Willy Tarreau60b639c2018-08-02 10:16:17 +0200113
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200114 /* Now we've seen all threads marked harmless, we can try to run
115 * by competing with other threads to win the race of the isolated
116 * thread. It eventually converges since winners will enventually
117 * relax their request and go back to wait for this to be over.
118 * Competing on this only after seeing all threads harmless limits
119 * the write contention.
120 */
121 thr = _HA_ATOMIC_LOAD(&isolated_thread);
122 if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
123 break; // we won!
Willy Tarreau38171da2019-05-17 16:33:13 +0200124 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +0200125 }
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200126
127 /* the thread is no longer harmless as it runs */
128 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
129
130 /* the thread is isolated until it calls thread_release() which will
131 * 1) reset isolated_thread to ~0;
132 * 2) decrement rdv_requests.
Willy Tarreau60b639c2018-08-02 10:16:17 +0200133 */
134}
135
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200136/* Isolates the current thread : request the ability to work while all other
137 * threads are idle, as defined by thread_idle_now(). It only returns once
138 * all of them are both harmless and idle, with the current thread's bit in
Willy Tarreau03f9b352022-06-27 16:02:24 +0200139 * &tg_ctx->threads_harmless and idle_mask cleared. Needs to be completed using
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200140 * thread_release(). By doing so the thread also engages in being safe against
141 * any actions that other threads might be about to start under the same
142 * conditions. This specifically targets destruction of any internal structure,
143 * which implies that the current thread may not hold references to any object.
144 *
145 * Note that a concurrent thread_isolate() will usually win against
146 * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to
147 * get back to the poller or any other fully idle location, that will
148 * ultimately release this one.
149 */
150void thread_isolate_full()
151{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200152 uint tgrp, thr;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200153
Willy Tarreau03f9b352022-06-27 16:02:24 +0200154 _HA_ATOMIC_OR(&tg_ctx->threads_idle, ti->ltid_bit);
155 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200156 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200157 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200158
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200159 /* wait for all threads to become harmless. They cannot change their
160 * mind once seen thanks to rdv_requests above, unless they pass in
161 * front of us.
162 */
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200163 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200164 for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) {
165 while ((_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless) &
166 _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_idle) &
167 ha_tgroup_info[tgrp].threads_enabled) != ha_tgroup_info[tgrp].threads_enabled)
168 ha_thread_relax();
169 }
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200170
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200171 /* Now we've seen all threads marked harmless and idle, we can
172 * try to run by competing with other threads to win the race
173 * of the isolated thread. It eventually converges since winners
174 * will enventually relax their request and go back to wait for
175 * this to be over. Competing on this only after seeing all
176 * threads harmless+idle limits the write contention.
177 */
178 thr = _HA_ATOMIC_LOAD(&isolated_thread);
179 if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
180 break; // we won!
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200181 ha_thread_relax();
182 }
183
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200184 /* we're not idle nor harmless anymore at this point. Other threads
185 * waiting on this condition will need to wait until out next pass to
186 * the poller, or our next call to thread_isolate_full().
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200187 */
Willy Tarreau03f9b352022-06-27 16:02:24 +0200188 _HA_ATOMIC_AND(&tg_ctx->threads_idle, ~ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200189 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200190}
191
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200192/* Cancels the effect of thread_isolate() by resetting the ID of the isolated
193 * thread and decrementing the number of RDV requesters. This immediately allows
194 * other threads to expect to be executed, though they will first have to wait
195 * for this thread to become harmless again (possibly by reaching the poller
196 * again).
Willy Tarreau60b639c2018-08-02 10:16:17 +0200197 */
198void thread_release()
199{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200200 HA_ATOMIC_STORE(&isolated_thread, ~0U);
201 HA_ATOMIC_DEC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200202}
Christopher Faulet339fff82017-10-19 11:59:15 +0200203
Willy Tarreaud10385a2021-10-06 22:22:40 +0200204/* Sets up threads, signals and masks, and starts threads 2 and above.
205 * Does nothing when threads are disabled.
206 */
207void setup_extra_threads(void *(*handler)(void *))
208{
209 sigset_t blocked_sig, old_sig;
210 int i;
211
212 /* ensure the signals will be blocked in every thread */
213 sigfillset(&blocked_sig);
214 sigdelset(&blocked_sig, SIGPROF);
215 sigdelset(&blocked_sig, SIGBUS);
216 sigdelset(&blocked_sig, SIGFPE);
217 sigdelset(&blocked_sig, SIGILL);
218 sigdelset(&blocked_sig, SIGSEGV);
219 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
220
221 /* Create nbthread-1 thread. The first thread is the current process */
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200222 ha_pthread[0] = pthread_self();
Willy Tarreaud10385a2021-10-06 22:22:40 +0200223 for (i = 1; i < global.nbthread; i++)
Willy Tarreau43ab05b2021-09-28 09:43:11 +0200224 pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200225}
226
227/* waits for all threads to terminate. Does nothing when threads are
228 * disabled.
229 */
230void wait_for_threads_completion()
231{
232 int i;
233
234 /* Wait the end of other threads */
235 for (i = 1; i < global.nbthread; i++)
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200236 pthread_join(ha_pthread[i], NULL);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200237
238#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
239 show_lock_stats();
240#endif
241}
242
243/* Tries to set the current thread's CPU affinity according to the cpu_map */
244void set_thread_cpu_affinity()
245{
246#if defined(USE_CPU_AFFINITY)
247 /* no affinity setting for the master process */
248 if (master)
249 return;
250
251 /* Now the CPU affinity for all threads */
Willy Tarreau5b093412022-07-08 09:38:30 +0200252 if (ha_cpuset_count(&cpu_map[tgid - 1].proc))
253 ha_cpuset_and(&cpu_map[tgid - 1].thread[ti->ltid], &cpu_map[tgid - 1].proc);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200254
Willy Tarreau5b093412022-07-08 09:38:30 +0200255 if (ha_cpuset_count(&cpu_map[tgid - 1].thread[ti->ltid])) {/* only do this if the thread has a THREAD map */
Willy Tarreaud10385a2021-10-06 22:22:40 +0200256# if defined(__APPLE__)
257 /* Note: this API is limited to the first 32/64 CPUs */
Willy Tarreau5b093412022-07-08 09:38:30 +0200258 unsigned long set = cpu_map[tgid - 1].thread[ti->ltid].cpuset;
Willy Tarreaud10385a2021-10-06 22:22:40 +0200259 int j;
260
261 while ((j = ffsl(set)) > 0) {
262 thread_affinity_policy_data_t cpu_set = { j - 1 };
263 thread_port_t mthread;
264
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200265 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200266 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
267 set &= ~(1UL << (j - 1));
268 }
269# else
Willy Tarreau5b093412022-07-08 09:38:30 +0200270 struct hap_cpuset *set = &cpu_map[tgid - 1].thread[ti->ltid];
Willy Tarreaud10385a2021-10-06 22:22:40 +0200271
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200272 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200273# endif
274 }
275#endif /* USE_CPU_AFFINITY */
276}
277
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200278/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
279 * since POSIX took great care of not specifying its representation, making it
280 * hard to export for post-mortem analysis. For this reason we copy it into a
281 * union and will use the smallest scalar type at least as large as its size,
282 * which will keep endianness and alignment for all regular sizes. As a last
283 * resort we end up with a long long ligned to the first bytes in memory, which
284 * will be endian-dependent if pthread_t is larger than a long long (not seen
285 * yet).
286 */
287unsigned long long ha_get_pthread_id(unsigned int thr)
288{
289 union {
290 pthread_t t;
291 unsigned long long ll;
292 unsigned int i;
293 unsigned short s;
294 unsigned char c;
295 } u = { 0 };
296
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200297 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200298
299 if (sizeof(u.t) <= sizeof(u.c))
300 return u.c;
301 else if (sizeof(u.t) <= sizeof(u.s))
302 return u.s;
303 else if (sizeof(u.t) <= sizeof(u.i))
304 return u.i;
305 return u.ll;
306}
307
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200308/* send signal <sig> to thread <thr> */
309void ha_tkill(unsigned int thr, int sig)
310{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200311 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200312}
313
314/* send signal <sig> to all threads. The calling thread is signaled last in
315 * order to allow all threads to synchronize in the handler.
316 */
317void ha_tkillall(int sig)
318{
319 unsigned int thr;
320
321 for (thr = 0; thr < global.nbthread; thr++) {
Willy Tarreauf15c75a2022-07-15 08:27:56 +0200322 if (!(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit))
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200323 continue;
324 if (thr == tid)
325 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200326 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200327 }
328 raise(sig);
329}
330
Willy Tarreauaa992762021-10-06 23:33:20 +0200331void ha_thread_relax(void)
332{
333#ifdef _POSIX_PRIORITY_SCHEDULING
334 sched_yield();
335#else
336 pl_cpu_relax();
337#endif
338}
339
Willy Tarreau3d184982020-10-18 10:20:59 +0200340/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100341void ha_spin_init(HA_SPINLOCK_T *l)
342{
343 HA_SPIN_INIT(l);
344}
345
Willy Tarreau3d184982020-10-18 10:20:59 +0200346/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100347void ha_rwlock_init(HA_RWLOCK_T *l)
348{
349 HA_RWLOCK_INIT(l);
350}
351
Willy Tarreau149ab772019-01-26 14:27:06 +0100352/* returns the number of CPUs the current process is enabled to run on */
353static int thread_cpus_enabled()
354{
355 int ret = 1;
356
357#ifdef USE_CPU_AFFINITY
358#if defined(__linux__) && defined(CPU_COUNT)
359 cpu_set_t mask;
360
361 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
362 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200363#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
364 cpuset_t cpuset;
365 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
366 sizeof(cpuset), &cpuset) == 0)
367 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000368#elif defined(__APPLE__)
369 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100370#endif
371#endif
372 ret = MAX(ret, 1);
373 ret = MIN(ret, MAX_THREADS);
374 return ret;
375}
376
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200377/* Returns 1 if the cpu set is currently restricted for the process else 0.
378 * Currently only implemented for the Linux platform.
379 */
380int thread_cpu_mask_forced()
381{
382#if defined(__linux__)
383 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
384 return cpus_avail != thread_cpus_enabled();
385#else
386 return 0;
387#endif
388}
389
Willy Tarreau407ef892021-10-05 18:39:27 +0200390/* Below come the lock-debugging functions */
391
392#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
393
394struct lock_stat lock_stats[LOCK_LABELS];
395
396/* this is only used below */
397static const char *lock_label(enum lock_label label)
398{
399 switch (label) {
400 case TASK_RQ_LOCK: return "TASK_RQ";
401 case TASK_WQ_LOCK: return "TASK_WQ";
402 case LISTENER_LOCK: return "LISTENER";
403 case PROXY_LOCK: return "PROXY";
404 case SERVER_LOCK: return "SERVER";
405 case LBPRM_LOCK: return "LBPRM";
406 case SIGNALS_LOCK: return "SIGNALS";
407 case STK_TABLE_LOCK: return "STK_TABLE";
408 case STK_SESS_LOCK: return "STK_SESS";
409 case APPLETS_LOCK: return "APPLETS";
410 case PEER_LOCK: return "PEER";
411 case SHCTX_LOCK: return "SHCTX";
412 case SSL_LOCK: return "SSL";
413 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
414 case PATREF_LOCK: return "PATREF";
415 case PATEXP_LOCK: return "PATEXP";
416 case VARS_LOCK: return "VARS";
417 case COMP_POOL_LOCK: return "COMP_POOL";
418 case LUA_LOCK: return "LUA";
419 case NOTIF_LOCK: return "NOTIF";
420 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
421 case DNS_LOCK: return "DNS";
422 case PID_LIST_LOCK: return "PID_LIST";
423 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
424 case PIPES_LOCK: return "PIPES";
425 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
426 case AUTH_LOCK: return "AUTH";
427 case LOGSRV_LOCK: return "LOGSRV";
428 case DICT_LOCK: return "DICT";
429 case PROTO_LOCK: return "PROTO";
430 case QUEUE_LOCK: return "QUEUE";
431 case CKCH_LOCK: return "CKCH";
432 case SNI_LOCK: return "SNI";
433 case SSL_SERVER_LOCK: return "SSL_SERVER";
434 case SFT_LOCK: return "SFT";
435 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
436 case QUIC_LOCK: return "QUIC";
437 case OTHER_LOCK: return "OTHER";
438 case DEBUG1_LOCK: return "DEBUG1";
439 case DEBUG2_LOCK: return "DEBUG2";
440 case DEBUG3_LOCK: return "DEBUG3";
441 case DEBUG4_LOCK: return "DEBUG4";
442 case DEBUG5_LOCK: return "DEBUG5";
443 case LOCK_LABELS: break; /* keep compiler happy */
444 };
445 /* only way to come here is consecutive to an internal bug */
446 abort();
447}
448
449void show_lock_stats()
450{
451 int lbl;
452
453 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
454 if (!lock_stats[lbl].num_write_locked &&
455 !lock_stats[lbl].num_seek_locked &&
456 !lock_stats[lbl].num_read_locked) {
457 fprintf(stderr,
458 "Stats about Lock %s: not used\n",
459 lock_label(lbl));
460 continue;
461 }
462
463 fprintf(stderr,
464 "Stats about Lock %s: \n",
465 lock_label(lbl));
466
467 if (lock_stats[lbl].num_write_locked)
468 fprintf(stderr,
469 "\t # write lock : %lu\n"
470 "\t # write unlock: %lu (%ld)\n"
471 "\t # wait time for write : %.3f msec\n"
472 "\t # wait time for write/lock: %.3f nsec\n",
473 lock_stats[lbl].num_write_locked,
474 lock_stats[lbl].num_write_unlocked,
475 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
476 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
477 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
478
479 if (lock_stats[lbl].num_seek_locked)
480 fprintf(stderr,
481 "\t # seek lock : %lu\n"
482 "\t # seek unlock : %lu (%ld)\n"
483 "\t # wait time for seek : %.3f msec\n"
484 "\t # wait time for seek/lock : %.3f nsec\n",
485 lock_stats[lbl].num_seek_locked,
486 lock_stats[lbl].num_seek_unlocked,
487 lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked,
488 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
489 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
490
491 if (lock_stats[lbl].num_read_locked)
492 fprintf(stderr,
493 "\t # read lock : %lu\n"
494 "\t # read unlock : %lu (%ld)\n"
495 "\t # wait time for read : %.3f msec\n"
496 "\t # wait time for read/lock : %.3f nsec\n",
497 lock_stats[lbl].num_read_locked,
498 lock_stats[lbl].num_read_unlocked,
499 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
500 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
501 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
502 }
503}
504
Willy Tarreau407ef892021-10-05 18:39:27 +0200505void __ha_rwlock_init(struct ha_rwlock *l)
506{
507 memset(l, 0, sizeof(struct ha_rwlock));
508 __RWLOCK_INIT(&l->lock);
509}
510
511void __ha_rwlock_destroy(struct ha_rwlock *l)
512{
513 __RWLOCK_DESTROY(&l->lock);
514 memset(l, 0, sizeof(struct ha_rwlock));
515}
516
517
518void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
519 const char *func, const char *file, int line)
520{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200521 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
522 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200523 uint64_t start_time;
524
Willy Tarreau7aa41192022-07-15 17:53:10 +0200525 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200526 abort();
527
Willy Tarreau7aa41192022-07-15 17:53:10 +0200528 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200529
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200530 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200531 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200532 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200533
534 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
535
Willy Tarreau7aa41192022-07-15 17:53:10 +0200536 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200537 l->info.last_location.function = func;
538 l->info.last_location.file = file;
539 l->info.last_location.line = line;
540
Willy Tarreau7aa41192022-07-15 17:53:10 +0200541 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200542}
543
544int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
545 const char *func, const char *file, int line)
546{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200547 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
548 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200549 uint64_t start_time;
550 int r;
551
Willy Tarreau7aa41192022-07-15 17:53:10 +0200552 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200553 abort();
554
555 /* We set waiting writer because trywrlock could wait for readers to quit */
Willy Tarreau7aa41192022-07-15 17:53:10 +0200556 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200557
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200558 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200559 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200560 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200561 if (unlikely(r)) {
Willy Tarreau7aa41192022-07-15 17:53:10 +0200562 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200563 return r;
564 }
565 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
566
Willy Tarreau7aa41192022-07-15 17:53:10 +0200567 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200568 l->info.last_location.function = func;
569 l->info.last_location.file = file;
570 l->info.last_location.line = line;
571
Willy Tarreau7aa41192022-07-15 17:53:10 +0200572 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200573
574 return 0;
575}
576
577void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
578 const char *func, const char *file, int line)
579{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200580 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
581 struct ha_rwlock_state *st = &l->info.st[tgid-1];
582
583 if (unlikely(!(st->cur_writer & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200584 /* the thread is not owning the lock for write */
585 abort();
586 }
587
Willy Tarreau7aa41192022-07-15 17:53:10 +0200588 st->cur_writer = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200589 l->info.last_location.function = func;
590 l->info.last_location.file = file;
591 l->info.last_location.line = line;
592
593 __RWLOCK_WRUNLOCK(&l->lock);
594
595 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
596}
597
598void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
599{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200600 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
601 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200602 uint64_t start_time;
603
Willy Tarreau7aa41192022-07-15 17:53:10 +0200604 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200605 abort();
606
Willy Tarreau7aa41192022-07-15 17:53:10 +0200607 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200608
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200609 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200610 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200611 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200612 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
613
Willy Tarreau7aa41192022-07-15 17:53:10 +0200614 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200615
Willy Tarreau7aa41192022-07-15 17:53:10 +0200616 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200617}
618
619int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
620{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200621 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
622 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200623 int r;
624
Willy Tarreau7aa41192022-07-15 17:53:10 +0200625 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200626 abort();
627
628 /* try read should never wait */
629 r = __RWLOCK_TRYRDLOCK(&l->lock);
630 if (unlikely(r))
631 return r;
632 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
633
Willy Tarreau7aa41192022-07-15 17:53:10 +0200634 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200635
636 return 0;
637}
638
639void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
640{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200641 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
642 struct ha_rwlock_state *st = &l->info.st[tgid-1];
643
644 if (unlikely(!(st->cur_readers & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200645 /* the thread is not owning the lock for read */
646 abort();
647 }
648
Willy Tarreau7aa41192022-07-15 17:53:10 +0200649 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200650
651 __RWLOCK_RDUNLOCK(&l->lock);
652
653 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
654}
655
656void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
657 const char *func, const char *file, int line)
658{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200659 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
660 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200661 uint64_t start_time;
662
Willy Tarreau7aa41192022-07-15 17:53:10 +0200663 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200664 abort();
665
Willy Tarreau7aa41192022-07-15 17:53:10 +0200666 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200667 abort();
668
Willy Tarreau7aa41192022-07-15 17:53:10 +0200669 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200670
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200671 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200672 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200673 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200674
675 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
676
Willy Tarreau7aa41192022-07-15 17:53:10 +0200677 HA_ATOMIC_OR(&st->cur_readers, tbit);
678 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200679 l->info.last_location.function = func;
680 l->info.last_location.file = file;
681 l->info.last_location.line = line;
682
Willy Tarreau7aa41192022-07-15 17:53:10 +0200683 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200684}
685
686void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
687 const char *func, const char *file, int line)
688{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200689 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
690 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200691 uint64_t start_time;
692
Willy Tarreau7aa41192022-07-15 17:53:10 +0200693 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200694 abort();
695
Willy Tarreau7aa41192022-07-15 17:53:10 +0200696 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200697 abort();
698
Willy Tarreau7aa41192022-07-15 17:53:10 +0200699 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200700
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200701 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200702 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200703 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200704
705 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
706
Willy Tarreau7aa41192022-07-15 17:53:10 +0200707 HA_ATOMIC_OR(&st->cur_seeker, tbit);
708 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200709 l->info.last_location.function = func;
710 l->info.last_location.file = file;
711 l->info.last_location.line = line;
712
Willy Tarreau7aa41192022-07-15 17:53:10 +0200713 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200714}
715
716void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
717 const char *func, const char *file, int line)
718{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200719 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
720 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200721 uint64_t start_time;
722
Willy Tarreau7aa41192022-07-15 17:53:10 +0200723 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200724 abort();
725
Willy Tarreau7aa41192022-07-15 17:53:10 +0200726 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200727
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200728 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200729 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200730 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200731
732 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
733
Willy Tarreau7aa41192022-07-15 17:53:10 +0200734 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200735 l->info.last_location.function = func;
736 l->info.last_location.file = file;
737 l->info.last_location.line = line;
738
Willy Tarreau7aa41192022-07-15 17:53:10 +0200739 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200740}
741
742void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
743 const char *func, const char *file, int line)
744{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200745 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
746 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200747 uint64_t start_time;
748
Willy Tarreau7aa41192022-07-15 17:53:10 +0200749 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200750 abort();
751
Willy Tarreau7aa41192022-07-15 17:53:10 +0200752 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200753 abort();
754
Willy Tarreau7aa41192022-07-15 17:53:10 +0200755 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200756
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200757 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200758 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200759 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200760
761 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
762
Willy Tarreau7aa41192022-07-15 17:53:10 +0200763 HA_ATOMIC_OR(&st->cur_writer, tbit);
764 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200765 l->info.last_location.function = func;
766 l->info.last_location.file = file;
767 l->info.last_location.line = line;
768
Willy Tarreau7aa41192022-07-15 17:53:10 +0200769 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200770}
771
772void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
773 const char *func, const char *file, int line)
774{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200775 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
776 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200777 uint64_t start_time;
778
Willy Tarreau7aa41192022-07-15 17:53:10 +0200779 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200780 abort();
781
Willy Tarreau7aa41192022-07-15 17:53:10 +0200782 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200783 abort();
784
Willy Tarreau7aa41192022-07-15 17:53:10 +0200785 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200786
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200787 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200788 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200789 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200790
791 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
792
Willy Tarreau7aa41192022-07-15 17:53:10 +0200793 HA_ATOMIC_OR(&st->cur_readers, tbit);
794 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200795 l->info.last_location.function = func;
796 l->info.last_location.file = file;
797 l->info.last_location.line = line;
798
Willy Tarreau7aa41192022-07-15 17:53:10 +0200799 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200800}
801
802void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
803 const char *func, const char *file, int line)
804{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200805 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
806 struct ha_rwlock_state *st = &l->info.st[tgid-1];
807 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200808 abort();
809
Willy Tarreau7aa41192022-07-15 17:53:10 +0200810 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200811 l->info.last_location.function = func;
812 l->info.last_location.file = file;
813 l->info.last_location.line = line;
814
815 __RWLOCK_SKUNLOCK(&l->lock);
816
817 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
818}
819
820int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
821 const char *func, const char *file, int line)
822{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200823 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
824 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200825 uint64_t start_time;
826 int r;
827
Willy Tarreau7aa41192022-07-15 17:53:10 +0200828 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200829 abort();
830
Willy Tarreau7aa41192022-07-15 17:53:10 +0200831 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200832
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200833 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200834 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200835 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200836
837 if (likely(!r)) {
838 /* got the lock ! */
839 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200840 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200841 l->info.last_location.function = func;
842 l->info.last_location.file = file;
843 l->info.last_location.line = line;
844 }
845
Willy Tarreau7aa41192022-07-15 17:53:10 +0200846 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200847 return r;
848}
849
850int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
851 const char *func, const char *file, int line)
852{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200853 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
854 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200855 uint64_t start_time;
856 int r;
857
Willy Tarreau7aa41192022-07-15 17:53:10 +0200858 if ((st->cur_writer | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200859 abort();
860
Willy Tarreau7aa41192022-07-15 17:53:10 +0200861 if (!(st->cur_readers & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200862 abort();
863
Willy Tarreau7aa41192022-07-15 17:53:10 +0200864 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200865
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200866 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200867 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200868 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200869
870 if (likely(!r)) {
871 /* got the lock ! */
872 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200873 HA_ATOMIC_OR(&st->cur_seeker, tbit);
874 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200875 l->info.last_location.function = func;
876 l->info.last_location.file = file;
877 l->info.last_location.line = line;
878 }
879
Willy Tarreau7aa41192022-07-15 17:53:10 +0200880 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200881 return r;
882}
883
884void __spin_init(struct ha_spinlock *l)
885{
886 memset(l, 0, sizeof(struct ha_spinlock));
887 __SPIN_INIT(&l->lock);
888}
889
890void __spin_destroy(struct ha_spinlock *l)
891{
892 __SPIN_DESTROY(&l->lock);
893 memset(l, 0, sizeof(struct ha_spinlock));
894}
895
896void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
897 const char *func, const char *file, int line)
898{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200899 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
900 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200901 uint64_t start_time;
902
Willy Tarreau7aa41192022-07-15 17:53:10 +0200903 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200904 /* the thread is already owning the lock */
905 abort();
906 }
907
Willy Tarreau7aa41192022-07-15 17:53:10 +0200908 HA_ATOMIC_OR(&st->waiters, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200909
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200910 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200911 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200912 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200913
914 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
915
916
Willy Tarreau7aa41192022-07-15 17:53:10 +0200917 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200918 l->info.last_location.function = func;
919 l->info.last_location.file = file;
920 l->info.last_location.line = line;
921
Willy Tarreau7aa41192022-07-15 17:53:10 +0200922 HA_ATOMIC_AND(&st->waiters, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200923}
924
925int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
926 const char *func, const char *file, int line)
927{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200928 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
929 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200930 int r;
931
Willy Tarreau7aa41192022-07-15 17:53:10 +0200932 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200933 /* the thread is already owning the lock */
934 abort();
935 }
936
937 /* try read should never wait */
938 r = __SPIN_TRYLOCK(&l->lock);
939 if (unlikely(r))
940 return r;
941 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
942
Willy Tarreau7aa41192022-07-15 17:53:10 +0200943 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200944 l->info.last_location.function = func;
945 l->info.last_location.file = file;
946 l->info.last_location.line = line;
947
948 return 0;
949}
950
951void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
952 const char *func, const char *file, int line)
953{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200954 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
955 struct ha_spinlock_state *st = &l->info.st[tgid-1];
956
957 if (unlikely(!(st->owner & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200958 /* the thread is not owning the lock */
959 abort();
960 }
961
Willy Tarreau7aa41192022-07-15 17:53:10 +0200962 st->owner = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200963 l->info.last_location.function = func;
964 l->info.last_location.file = file;
965 l->info.last_location.line = line;
966
967 __SPIN_UNLOCK(&l->lock);
968 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
969}
970
971#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
972
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200973/* Depending on the platform and how libpthread was built, pthread_exit() may
974 * involve some code in libgcc_s that would be loaded on exit for the first
975 * time, causing aborts if the process is chrooted. It's harmless bit very
976 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
977 * needed, so what we do here is that during early boot we create a dummy
978 * thread that immediately exits. This will lead to libgcc_s being loaded
979 * during boot on the platforms where it's required.
980 */
981static void *dummy_thread_function(void *data)
982{
983 pthread_exit(NULL);
984 return NULL;
985}
986
987static inline void preload_libgcc_s(void)
988{
989 pthread_t dummy_thread;
990 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
991 pthread_join(dummy_thread, NULL);
992}
993
Willy Tarreau3f567e42020-05-28 15:29:19 +0200994static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200995{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100996 char *ptr = NULL;
997
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200998 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +0200999
Willy Tarreau149ab772019-01-26 14:27:06 +01001000 thread_cpus_enabled_at_boot = thread_cpus_enabled();
1001
1002 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
1003 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001004 hap_register_build_opts(ptr, 1);
1005
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001006#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
1007 memset(lock_stats, 0, sizeof(lock_stats));
1008#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001009}
Willy Tarreau8ead1d02022-04-25 19:23:17 +02001010INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001011
Willy Tarreau8459f252018-12-15 16:48:14 +01001012#else
1013
Willy Tarreauaa992762021-10-06 23:33:20 +02001014/* send signal <sig> to thread <thr> (send to process in fact) */
1015void ha_tkill(unsigned int thr, int sig)
1016{
1017 raise(sig);
1018}
1019
1020/* send signal <sig> to all threads (send to process in fact) */
1021void ha_tkillall(int sig)
1022{
1023 raise(sig);
1024}
1025
1026void ha_thread_relax(void)
1027{
1028#ifdef _POSIX_PRIORITY_SCHEDULING
1029 sched_yield();
1030#endif
1031}
1032
Willy Tarreau8459f252018-12-15 16:48:14 +01001033REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
1034
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001035#endif // USE_THREAD
1036
1037
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001038/* scans the configured thread mapping and establishes the final one. Returns <0
1039 * on failure, >=0 on success.
1040 */
1041int thread_map_to_groups()
1042{
1043 int t, g, ut, ug;
1044 int q, r;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001045 ulong m __maybe_unused;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001046
1047 ut = ug = 0; // unassigned threads & groups
1048
1049 for (t = 0; t < global.nbthread; t++) {
1050 if (!ha_thread_info[t].tg)
1051 ut++;
1052 }
1053
1054 for (g = 0; g < global.nbtgroups; g++) {
1055 if (!ha_tgroup_info[g].count)
1056 ug++;
Willy Tarreau60fe4a92022-06-28 17:48:07 +02001057 ha_tgroup_info[g].tgid_bit = 1UL << g;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001058 }
1059
1060 if (ug > ut) {
1061 ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut);
1062 return -1;
1063 }
1064
1065 /* look for first unassigned thread */
1066 for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++)
1067 ;
1068
1069 /* assign threads to empty groups */
1070 for (g = 0; ug && ut; ) {
1071 /* due to sparse thread assignment we can end up with more threads
1072 * per group on last assigned groups than former ones, so we must
1073 * always try to pack the maximum remaining ones together first.
1074 */
1075 q = ut / ug;
1076 r = ut % ug;
1077 if ((q + !!r) > MAX_THREADS_PER_GROUP) {
1078 ha_alert("Too many remaining unassigned threads (%d) for thread groups (%d). Please increase thread-groups or make sure to keep thread numbers contiguous\n", ug, ut);
1079 return -1;
1080 }
1081
1082 /* thread <t> is the next unassigned one. Let's look for next
1083 * unassigned group, we know there are some left
1084 */
1085 while (ut >= ug && ha_tgroup_info[g].count)
1086 g++;
1087
1088 /* group g is unassigned, try to fill it with consecutive threads */
1089 while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r &&
1090 (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) {
1091
1092 if (!ha_tgroup_info[g].count) {
1093 /* assign new group */
1094 ha_tgroup_info[g].base = t;
1095 ug--;
1096 }
1097
1098 ha_tgroup_info[g].count++;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001099 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001100 ha_thread_info[t].tg = &ha_tgroup_info[g];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001101 ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g];
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001102
1103 ut--;
1104 /* switch to next unassigned thread */
1105 while (++t < global.nbthread && ha_thread_info[t].tg)
1106 ;
1107 }
1108 }
1109
1110 if (ut) {
1111 ha_alert("Remaining unassigned threads found (%d) because all groups are in use. Please increase 'thread-groups', reduce 'nbthreads' or remove or extend 'thread-group' enumerations.\n", ut);
1112 return -1;
1113 }
1114
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001115 for (t = 0; t < global.nbthread; t++) {
1116 ha_thread_info[t].tid = t;
1117 ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base;
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001118 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1119 }
1120
Willy Tarreaucce203a2022-06-24 15:55:11 +02001121 m = 0;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001122 for (g = 0; g < global.nbtgroups; g++) {
1123 ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count);
Willy Tarreaucce203a2022-06-24 15:55:11 +02001124 if (!ha_tgroup_info[g].count)
1125 continue;
1126 m |= 1UL << g;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001127
1128 }
1129
Willy Tarreaucce203a2022-06-24 15:55:11 +02001130#ifdef USE_THREAD
1131 all_tgroups_mask = m;
1132#endif
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001133 return 0;
1134}
1135
Willy Tarreau6018c022022-06-28 08:27:43 +02001136/* converts a configuration thread num or group+mask to a global group+mask
1137 * depending on the configured thread group id. This is essentially for use
1138 * with the "thread" directive on "bind" lines, where "thread 4-6" might be
1139 * turned to "2/1-3". It cannot be used before the thread mapping above was
1140 * completed and the thread group number configured. Possible options:
Willy Tarreau627def92021-09-29 18:59:47 +02001141 * - igid == 0: imask represents global IDs. We have to check that all
1142 * configured threads in the mask belong to the same group. If imask is zero
1143 * it means everything, so for now we only support this with a single group.
Willy Tarreau6018c022022-06-28 08:27:43 +02001144 * - igid > 0, imask = 0: convert global values to local values for this thread
1145 * - igid > 0, imask > 0: convert global values to local values
1146 * Note that the output mask is always local to the group.
Willy Tarreau627def92021-09-29 18:59:47 +02001147 *
1148 * Returns <0 on failure, >=0 on success.
1149 */
1150int thread_resolve_group_mask(uint igid, ulong imask, uint *ogid, ulong *omask, char **err)
1151{
1152 ulong mask;
1153 uint t;
1154
1155 if (igid == 0) {
1156 /* unspecified group, IDs are global */
1157 if (!imask) {
1158 /* all threads of all groups */
1159 if (global.nbtgroups > 1) {
1160 memprintf(err, "'thread' directive spans multiple groups");
1161 return -1;
1162 }
Willy Tarreau627def92021-09-29 18:59:47 +02001163 *ogid = 1; // first and only group
Willy Tarreau6018c022022-06-28 08:27:43 +02001164 *omask = ha_tgroup_info[0].threads_enabled;
Willy Tarreau627def92021-09-29 18:59:47 +02001165 return 0;
1166 } else {
1167 /* some global threads */
Willy Tarreau627def92021-09-29 18:59:47 +02001168 for (t = 0; t < global.nbthread; t++) {
1169 if (imask & (1UL << t)) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001170 if (ha_thread_info[t].tgid != igid) {
Willy Tarreau627def92021-09-29 18:59:47 +02001171 if (!igid)
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001172 igid = ha_thread_info[t].tgid;
Willy Tarreau627def92021-09-29 18:59:47 +02001173 else {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001174 memprintf(err, "'thread' directive spans multiple groups (at least %u and %u)", igid, ha_thread_info[t].tgid);
Willy Tarreau627def92021-09-29 18:59:47 +02001175 return -1;
1176 }
1177 }
1178 }
1179 }
1180
1181 if (!igid) {
1182 memprintf(err, "'thread' directive contains threads that belong to no group");
1183 return -1;
1184 }
1185
1186 /* we have a valid group, convert this to global thread IDs */
1187 *ogid = igid;
Willy Tarreau6018c022022-06-28 08:27:43 +02001188 imask = imask >> ha_tgroup_info[igid - 1].base;
1189 imask &= ha_tgroup_info[igid - 1].threads_enabled;
1190 *omask = imask;
Willy Tarreau627def92021-09-29 18:59:47 +02001191 return 0;
1192 }
1193 } else {
1194 /* group was specified */
1195 if (igid > global.nbtgroups) {
1196 memprintf(err, "'thread' directive references non-existing thread group %u", igid);
1197 return -1;
1198 }
1199
1200 if (!imask) {
1201 /* all threads of this groups. Let's make a mask from their count and base. */
1202 *ogid = igid;
Willy Tarreau6018c022022-06-28 08:27:43 +02001203 *omask = nbits(ha_tgroup_info[igid - 1].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001204 return 0;
1205 } else {
1206 /* some local threads. Keep only existing ones for this group */
1207
Willy Tarreau6018c022022-06-28 08:27:43 +02001208 mask = nbits(ha_tgroup_info[igid - 1].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001209
1210 if (!(mask & imask)) {
1211 /* no intersection between the thread group's
1212 * threads and the bind line's.
1213 */
1214#ifdef THREAD_AUTO_ADJUST_GROUPS
1215 unsigned long new_mask = 0;
1216
1217 while (imask) {
1218 new_mask |= imask & mask;
1219 imask >>= ha_tgroup_info[igid - 1].count;
1220 }
1221 imask = new_mask;
1222#else
1223 memprintf(err, "'thread' directive only references threads not belonging to the group");
1224 return -1;
1225#endif
1226 }
1227
Willy Tarreau6018c022022-06-28 08:27:43 +02001228 *omask = mask & imask;
Willy Tarreau627def92021-09-29 18:59:47 +02001229 *ogid = igid;
1230 return 0;
1231 }
1232 }
1233}
1234
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001235/* Parse the "nbthread" global directive, which takes an integer argument that
1236 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001237 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001238static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
1239 const struct proxy *defpx, const char *file, int line,
1240 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001241{
1242 long nbthread;
1243 char *errptr;
1244
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001245 if (too_many_args(1, args, err, NULL))
1246 return -1;
1247
1248 nbthread = strtol(args[1], &errptr, 10);
1249 if (!*args[1] || *errptr) {
1250 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1251 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001252 }
1253
1254#ifndef USE_THREAD
1255 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001256 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]);
1257 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001258 }
1259#else
1260 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001261 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1262 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001263 }
1264
Willy Tarreaufc647362019-02-02 17:05:03 +01001265 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001266#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001267
1268 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001269 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1270 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001271
1272 global.nbthread = nbthread;
1273 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001274}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001275
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001276/* Parse the "thread-group" global directive, which takes an integer argument
1277 * that designates a thread group, and a list of threads to put into that group.
1278 */
1279static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx,
1280 const struct proxy *defpx, const char *file, int line,
1281 char **err)
1282{
1283 char *errptr;
1284 long tnum, tend, tgroup;
1285 int arg, tot;
1286
1287 tgroup = strtol(args[1], &errptr, 10);
1288 if (!*args[1] || *errptr) {
1289 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1290 return -1;
1291 }
1292
1293 if (tgroup < 1 || tgroup > MAX_TGROUPS) {
1294 memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup);
1295 return -1;
1296 }
1297
1298 /* look for a preliminary definition of any thread pointing to this
1299 * group, and remove them.
1300 */
1301 if (ha_tgroup_info[tgroup-1].count) {
1302 ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n",
1303 file, line, args[0], tgroup);
1304
1305 for (tnum = ha_tgroup_info[tgroup-1].base;
1306 tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count;
1307 tnum++) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001308 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001309 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001310 ha_thread_info[tnum-1].tgid = 0;
Willy Tarreau03f9b352022-06-27 16:02:24 +02001311 ha_thread_info[tnum-1].tg_ctx = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001312 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001313 }
1314 ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0;
1315 }
1316
1317 tot = 0;
1318 for (arg = 2; args[arg] && *args[arg]; arg++) {
1319 tend = tnum = strtol(args[arg], &errptr, 10);
1320
1321 if (*errptr == '-')
1322 tend = strtol(errptr + 1, &errptr, 10);
1323
1324 if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) {
1325 memprintf(err, "'%s %ld' passed an unparsable or invalid thread number '%s' (valid range is 1 to %d)", args[0], tgroup, args[arg], MAX_THREADS);
1326 return -1;
1327 }
1328
1329 for(; tnum <= tend; tnum++) {
1330 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
1331 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n",
1332 file, line, args[0], tgroup, tnum);
1333 } else if (ha_thread_info[tnum-1].tg) {
1334 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n",
1335 file, line, args[0], tgroup, tnum,
1336 (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1));
1337 }
1338
1339 if (!ha_tgroup_info[tgroup-1].count) {
1340 ha_tgroup_info[tgroup-1].base = tnum-1;
1341 ha_tgroup_info[tgroup-1].count = 1;
1342 }
1343 else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) {
1344 ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base;
1345 }
1346 else if (tnum < ha_tgroup_info[tgroup-1].base) {
1347 ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1;
1348 ha_tgroup_info[tgroup-1].base = tnum - 1;
1349 }
1350
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001351 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001352 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001353 ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1];
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001354 tot++;
1355 }
1356 }
1357
1358 if (ha_tgroup_info[tgroup-1].count > tot) {
1359 memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup);
1360 return -1;
1361 }
1362
1363 if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) {
1364 memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP);
1365 return -1;
1366 }
1367
1368 return 0;
1369}
1370
Willy Tarreauc33b9692021-09-22 12:07:23 +02001371/* Parse the "thread-groups" global directive, which takes an integer argument
1372 * that contains the desired number of thread groups.
1373 */
1374static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx,
1375 const struct proxy *defpx, const char *file, int line,
1376 char **err)
1377{
1378 long nbtgroups;
1379 char *errptr;
1380
1381 if (too_many_args(1, args, err, NULL))
1382 return -1;
1383
1384 nbtgroups = strtol(args[1], &errptr, 10);
1385 if (!*args[1] || *errptr) {
1386 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1387 return -1;
1388 }
1389
1390#ifndef USE_THREAD
1391 if (nbtgroups != 1) {
1392 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]);
1393 return -1;
1394 }
1395#else
1396 if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) {
1397 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups);
1398 return -1;
1399 }
1400#endif
1401
1402 HA_DIAG_WARNING_COND(global.nbtgroups,
1403 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1404 file, line, args[0]);
1405
1406 global.nbtgroups = nbtgroups;
1407 return 0;
1408}
1409
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001410/* config keyword parsers */
1411static struct cfg_kw_list cfg_kws = {ILH, {
1412 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001413 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001414 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001415 { 0, NULL, NULL }
1416}};
1417
1418INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);