blob: cc983a26126d79590563f2c1e08628db7e6953ac [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 Tarreaucce203a2022-06-24 15:55:11 +020066volatile unsigned long all_tgroups_mask __read_mostly = 1; // nbtgroup 1 assumed by default
Willy Tarreau598cf3f2022-07-01 15:08:37 +020067volatile unsigned int rdv_requests = 0; // total number of threads requesting RDV
68volatile unsigned int isolated_thread = ~0; // ID of the isolated thread, or ~0 when none
Willy Tarreaub90935c2021-09-30 08:00:11 +020069THREAD_LOCAL unsigned int tgid = 1; // thread ID starts at 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020070THREAD_LOCAL unsigned int tid = 0;
Willy Tarreau149ab772019-01-26 14:27:06 +010071int thread_cpus_enabled_at_boot = 1;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +020072static pthread_t ha_pthread[MAX_THREADS] = { };
Willy Tarreau0c026f42018-08-01 19:12:20 +020073
Willy Tarreau60b639c2018-08-02 10:16:17 +020074/* Marks the thread as harmless until the last thread using the rendez-vous
Willy Tarreau598cf3f2022-07-01 15:08:37 +020075 * point quits. Given that we can wait for a long time, sched_yield() is
Christopher Fauleta9a9e9a2021-03-25 14:11:36 +010076 * used when available to offer the CPU resources to competing threads if
77 * needed.
Willy Tarreau60b639c2018-08-02 10:16:17 +020078 */
79void thread_harmless_till_end()
80{
Willy Tarreau03f9b352022-06-27 16:02:24 +020081 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +020082 while (_HA_ATOMIC_LOAD(&rdv_requests) != 0) {
Willy Tarreau286363b2021-08-04 10:33:57 +020083 ha_thread_relax();
84 }
Willy Tarreau60b639c2018-08-02 10:16:17 +020085}
86
87/* Isolates the current thread : request the ability to work while all other
Willy Tarreauf519cfa2021-08-04 11:22:07 +020088 * threads are harmless, as defined by thread_harmless_now() (i.e. they're not
89 * going to touch any visible memory area). Only returns once all of them are
Willy Tarreau03f9b352022-06-27 16:02:24 +020090 * harmless, with the current thread's bit in &tg_ctx->threads_harmless cleared.
Willy Tarreauf519cfa2021-08-04 11:22:07 +020091 * Needs to be completed using thread_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +020092 */
93void thread_isolate()
94{
Willy Tarreau598cf3f2022-07-01 15:08:37 +020095 uint tgrp, thr;
Willy Tarreau60b639c2018-08-02 10:16:17 +020096
Willy Tarreau03f9b352022-06-27 16:02:24 +020097 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Olivier Houchardb23a61f2019-03-08 18:51:17 +010098 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +020099 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200100
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200101 /* wait for all threads to become harmless. They cannot change their
102 * mind once seen thanks to rdv_requests above, unless they pass in
103 * front of us.
104 */
Willy Tarreau60b639c2018-08-02 10:16:17 +0200105 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200106 for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) {
107 while ((_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless) &
108 ha_tgroup_info[tgrp].threads_enabled) != ha_tgroup_info[tgrp].threads_enabled)
109 ha_thread_relax();
110 }
Willy Tarreau60b639c2018-08-02 10:16:17 +0200111
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200112 /* Now we've seen all threads marked harmless, we can try to run
113 * by competing with other threads to win the race of the isolated
114 * thread. It eventually converges since winners will enventually
115 * relax their request and go back to wait for this to be over.
116 * Competing on this only after seeing all threads harmless limits
117 * the write contention.
118 */
119 thr = _HA_ATOMIC_LOAD(&isolated_thread);
120 if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
121 break; // we won!
Willy Tarreau38171da2019-05-17 16:33:13 +0200122 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +0200123 }
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200124
125 /* the thread is no longer harmless as it runs */
126 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
127
128 /* the thread is isolated until it calls thread_release() which will
129 * 1) reset isolated_thread to ~0;
130 * 2) decrement rdv_requests.
Willy Tarreau60b639c2018-08-02 10:16:17 +0200131 */
132}
133
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200134/* Isolates the current thread : request the ability to work while all other
135 * threads are idle, as defined by thread_idle_now(). It only returns once
136 * all of them are both harmless and idle, with the current thread's bit in
Willy Tarreau03f9b352022-06-27 16:02:24 +0200137 * &tg_ctx->threads_harmless and idle_mask cleared. Needs to be completed using
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200138 * thread_release(). By doing so the thread also engages in being safe against
139 * any actions that other threads might be about to start under the same
140 * conditions. This specifically targets destruction of any internal structure,
141 * which implies that the current thread may not hold references to any object.
142 *
143 * Note that a concurrent thread_isolate() will usually win against
144 * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to
145 * get back to the poller or any other fully idle location, that will
146 * ultimately release this one.
147 */
148void thread_isolate_full()
149{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200150 uint tgrp, thr;
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200151
Willy Tarreau03f9b352022-06-27 16:02:24 +0200152 _HA_ATOMIC_OR(&tg_ctx->threads_idle, ti->ltid_bit);
153 _HA_ATOMIC_OR(&tg_ctx->threads_harmless, ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200154 __ha_barrier_atomic_store();
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200155 _HA_ATOMIC_INC(&rdv_requests);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200156
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200157 /* wait for all threads to become harmless. They cannot change their
158 * mind once seen thanks to rdv_requests above, unless they pass in
159 * front of us.
160 */
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200161 while (1) {
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200162 for (tgrp = 0; tgrp < global.nbtgroups; tgrp++) {
163 while ((_HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_harmless) &
164 _HA_ATOMIC_LOAD(&ha_tgroup_ctx[tgrp].threads_idle) &
165 ha_tgroup_info[tgrp].threads_enabled) != ha_tgroup_info[tgrp].threads_enabled)
166 ha_thread_relax();
167 }
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200168
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200169 /* Now we've seen all threads marked harmless and idle, we can
170 * try to run by competing with other threads to win the race
171 * of the isolated thread. It eventually converges since winners
172 * will enventually relax their request and go back to wait for
173 * this to be over. Competing on this only after seeing all
174 * threads harmless+idle limits the write contention.
175 */
176 thr = _HA_ATOMIC_LOAD(&isolated_thread);
177 if (thr == ~0U && _HA_ATOMIC_CAS(&isolated_thread, &thr, tid))
178 break; // we won!
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200179 ha_thread_relax();
180 }
181
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200182 /* we're not idle nor harmless anymore at this point. Other threads
183 * waiting on this condition will need to wait until out next pass to
184 * the poller, or our next call to thread_isolate_full().
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200185 */
Willy Tarreau03f9b352022-06-27 16:02:24 +0200186 _HA_ATOMIC_AND(&tg_ctx->threads_idle, ~ti->ltid_bit);
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200187 _HA_ATOMIC_AND(&tg_ctx->threads_harmless, ~ti->ltid_bit);
Willy Tarreau88d1c5d2021-08-04 11:44:17 +0200188}
189
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200190/* Cancels the effect of thread_isolate() by resetting the ID of the isolated
191 * thread and decrementing the number of RDV requesters. This immediately allows
192 * other threads to expect to be executed, though they will first have to wait
193 * for this thread to become harmless again (possibly by reaching the poller
194 * again).
Willy Tarreau60b639c2018-08-02 10:16:17 +0200195 */
196void thread_release()
197{
Willy Tarreau598cf3f2022-07-01 15:08:37 +0200198 HA_ATOMIC_STORE(&isolated_thread, ~0U);
199 HA_ATOMIC_DEC(&rdv_requests);
Willy Tarreau60b639c2018-08-02 10:16:17 +0200200}
Christopher Faulet339fff82017-10-19 11:59:15 +0200201
Willy Tarreaud10385a2021-10-06 22:22:40 +0200202/* Sets up threads, signals and masks, and starts threads 2 and above.
203 * Does nothing when threads are disabled.
204 */
205void setup_extra_threads(void *(*handler)(void *))
206{
207 sigset_t blocked_sig, old_sig;
208 int i;
209
210 /* ensure the signals will be blocked in every thread */
211 sigfillset(&blocked_sig);
212 sigdelset(&blocked_sig, SIGPROF);
213 sigdelset(&blocked_sig, SIGBUS);
214 sigdelset(&blocked_sig, SIGFPE);
215 sigdelset(&blocked_sig, SIGILL);
216 sigdelset(&blocked_sig, SIGSEGV);
217 pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig);
218
219 /* Create nbthread-1 thread. The first thread is the current process */
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200220 ha_pthread[0] = pthread_self();
Willy Tarreaud10385a2021-10-06 22:22:40 +0200221 for (i = 1; i < global.nbthread; i++)
Willy Tarreau43ab05b2021-09-28 09:43:11 +0200222 pthread_create(&ha_pthread[i], NULL, handler, &ha_thread_info[i]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200223}
224
225/* waits for all threads to terminate. Does nothing when threads are
226 * disabled.
227 */
228void wait_for_threads_completion()
229{
230 int i;
231
232 /* Wait the end of other threads */
233 for (i = 1; i < global.nbthread; i++)
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200234 pthread_join(ha_pthread[i], NULL);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200235
236#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
237 show_lock_stats();
238#endif
239}
240
241/* Tries to set the current thread's CPU affinity according to the cpu_map */
242void set_thread_cpu_affinity()
243{
244#if defined(USE_CPU_AFFINITY)
245 /* no affinity setting for the master process */
246 if (master)
247 return;
248
249 /* Now the CPU affinity for all threads */
Willy Tarreau5b093412022-07-08 09:38:30 +0200250 if (ha_cpuset_count(&cpu_map[tgid - 1].proc))
251 ha_cpuset_and(&cpu_map[tgid - 1].thread[ti->ltid], &cpu_map[tgid - 1].proc);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200252
Willy Tarreau5b093412022-07-08 09:38:30 +0200253 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 +0200254# if defined(__APPLE__)
255 /* Note: this API is limited to the first 32/64 CPUs */
Willy Tarreau5b093412022-07-08 09:38:30 +0200256 unsigned long set = cpu_map[tgid - 1].thread[ti->ltid].cpuset;
Willy Tarreaud10385a2021-10-06 22:22:40 +0200257 int j;
258
259 while ((j = ffsl(set)) > 0) {
260 thread_affinity_policy_data_t cpu_set = { j - 1 };
261 thread_port_t mthread;
262
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200263 mthread = pthread_mach_thread_np(ha_pthread[tid]);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200264 thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1);
265 set &= ~(1UL << (j - 1));
266 }
267# else
Willy Tarreau5b093412022-07-08 09:38:30 +0200268 struct hap_cpuset *set = &cpu_map[tgid - 1].thread[ti->ltid];
Willy Tarreaud10385a2021-10-06 22:22:40 +0200269
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200270 pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset);
Willy Tarreaud10385a2021-10-06 22:22:40 +0200271# endif
272 }
273#endif /* USE_CPU_AFFINITY */
274}
275
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200276/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
277 * since POSIX took great care of not specifying its representation, making it
278 * hard to export for post-mortem analysis. For this reason we copy it into a
279 * union and will use the smallest scalar type at least as large as its size,
280 * which will keep endianness and alignment for all regular sizes. As a last
281 * resort we end up with a long long ligned to the first bytes in memory, which
282 * will be endian-dependent if pthread_t is larger than a long long (not seen
283 * yet).
284 */
285unsigned long long ha_get_pthread_id(unsigned int thr)
286{
287 union {
288 pthread_t t;
289 unsigned long long ll;
290 unsigned int i;
291 unsigned short s;
292 unsigned char c;
293 } u = { 0 };
294
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200295 u.t = ha_pthread[thr];
Willy Tarreau4eeb8832021-10-06 22:44:28 +0200296
297 if (sizeof(u.t) <= sizeof(u.c))
298 return u.c;
299 else if (sizeof(u.t) <= sizeof(u.s))
300 return u.s;
301 else if (sizeof(u.t) <= sizeof(u.i))
302 return u.i;
303 return u.ll;
304}
305
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200306/* send signal <sig> to thread <thr> */
307void ha_tkill(unsigned int thr, int sig)
308{
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200309 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200310}
311
312/* send signal <sig> to all threads. The calling thread is signaled last in
313 * order to allow all threads to synchronize in the handler.
314 */
315void ha_tkillall(int sig)
316{
317 unsigned int thr;
318
319 for (thr = 0; thr < global.nbthread; thr++) {
Willy Tarreauf15c75a2022-07-15 08:27:56 +0200320 if (!(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit))
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200321 continue;
322 if (thr == tid)
323 continue;
Willy Tarreau5e03dfa2021-10-06 22:53:51 +0200324 pthread_kill(ha_pthread[thr], sig);
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200325 }
326 raise(sig);
327}
328
Willy Tarreauaa992762021-10-06 23:33:20 +0200329void ha_thread_relax(void)
330{
331#ifdef _POSIX_PRIORITY_SCHEDULING
332 sched_yield();
333#else
334 pl_cpu_relax();
335#endif
336}
337
Willy Tarreau3d184982020-10-18 10:20:59 +0200338/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100339void ha_spin_init(HA_SPINLOCK_T *l)
340{
341 HA_SPIN_INIT(l);
342}
343
Willy Tarreau3d184982020-10-18 10:20:59 +0200344/* these calls are used as callbacks at init time when debugging is on */
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100345void ha_rwlock_init(HA_RWLOCK_T *l)
346{
347 HA_RWLOCK_INIT(l);
348}
349
Willy Tarreau149ab772019-01-26 14:27:06 +0100350/* returns the number of CPUs the current process is enabled to run on */
351static int thread_cpus_enabled()
352{
353 int ret = 1;
354
355#ifdef USE_CPU_AFFINITY
356#if defined(__linux__) && defined(CPU_COUNT)
357 cpu_set_t mask;
358
359 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
360 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200361#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
362 cpuset_t cpuset;
363 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
364 sizeof(cpuset), &cpuset) == 0)
365 ret = CPU_COUNT(&cpuset);
David CARLIER6a906012021-01-15 08:09:56 +0000366#elif defined(__APPLE__)
367 ret = (int)sysconf(_SC_NPROCESSORS_ONLN);
Willy Tarreau149ab772019-01-26 14:27:06 +0100368#endif
369#endif
370 ret = MAX(ret, 1);
371 ret = MIN(ret, MAX_THREADS);
372 return ret;
373}
374
Amaury Denoyelle4c9efde2021-03-31 16:57:39 +0200375/* Returns 1 if the cpu set is currently restricted for the process else 0.
376 * Currently only implemented for the Linux platform.
377 */
378int thread_cpu_mask_forced()
379{
380#if defined(__linux__)
381 const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN);
382 return cpus_avail != thread_cpus_enabled();
383#else
384 return 0;
385#endif
386}
387
Willy Tarreau407ef892021-10-05 18:39:27 +0200388/* Below come the lock-debugging functions */
389
390#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
391
392struct lock_stat lock_stats[LOCK_LABELS];
393
394/* this is only used below */
395static const char *lock_label(enum lock_label label)
396{
397 switch (label) {
398 case TASK_RQ_LOCK: return "TASK_RQ";
399 case TASK_WQ_LOCK: return "TASK_WQ";
400 case LISTENER_LOCK: return "LISTENER";
401 case PROXY_LOCK: return "PROXY";
402 case SERVER_LOCK: return "SERVER";
403 case LBPRM_LOCK: return "LBPRM";
404 case SIGNALS_LOCK: return "SIGNALS";
405 case STK_TABLE_LOCK: return "STK_TABLE";
406 case STK_SESS_LOCK: return "STK_SESS";
407 case APPLETS_LOCK: return "APPLETS";
408 case PEER_LOCK: return "PEER";
409 case SHCTX_LOCK: return "SHCTX";
410 case SSL_LOCK: return "SSL";
411 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
412 case PATREF_LOCK: return "PATREF";
413 case PATEXP_LOCK: return "PATEXP";
414 case VARS_LOCK: return "VARS";
415 case COMP_POOL_LOCK: return "COMP_POOL";
416 case LUA_LOCK: return "LUA";
417 case NOTIF_LOCK: return "NOTIF";
418 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
419 case DNS_LOCK: return "DNS";
420 case PID_LIST_LOCK: return "PID_LIST";
421 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
422 case PIPES_LOCK: return "PIPES";
423 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
424 case AUTH_LOCK: return "AUTH";
425 case LOGSRV_LOCK: return "LOGSRV";
426 case DICT_LOCK: return "DICT";
427 case PROTO_LOCK: return "PROTO";
428 case QUEUE_LOCK: return "QUEUE";
429 case CKCH_LOCK: return "CKCH";
430 case SNI_LOCK: return "SNI";
431 case SSL_SERVER_LOCK: return "SSL_SERVER";
432 case SFT_LOCK: return "SFT";
433 case IDLE_CONNS_LOCK: return "IDLE_CONNS";
434 case QUIC_LOCK: return "QUIC";
435 case OTHER_LOCK: return "OTHER";
436 case DEBUG1_LOCK: return "DEBUG1";
437 case DEBUG2_LOCK: return "DEBUG2";
438 case DEBUG3_LOCK: return "DEBUG3";
439 case DEBUG4_LOCK: return "DEBUG4";
440 case DEBUG5_LOCK: return "DEBUG5";
441 case LOCK_LABELS: break; /* keep compiler happy */
442 };
443 /* only way to come here is consecutive to an internal bug */
444 abort();
445}
446
447void show_lock_stats()
448{
449 int lbl;
450
451 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
452 if (!lock_stats[lbl].num_write_locked &&
453 !lock_stats[lbl].num_seek_locked &&
454 !lock_stats[lbl].num_read_locked) {
455 fprintf(stderr,
456 "Stats about Lock %s: not used\n",
457 lock_label(lbl));
458 continue;
459 }
460
461 fprintf(stderr,
462 "Stats about Lock %s: \n",
463 lock_label(lbl));
464
465 if (lock_stats[lbl].num_write_locked)
466 fprintf(stderr,
467 "\t # write lock : %lu\n"
468 "\t # write unlock: %lu (%ld)\n"
469 "\t # wait time for write : %.3f msec\n"
470 "\t # wait time for write/lock: %.3f nsec\n",
471 lock_stats[lbl].num_write_locked,
472 lock_stats[lbl].num_write_unlocked,
473 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
474 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
475 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0);
476
477 if (lock_stats[lbl].num_seek_locked)
478 fprintf(stderr,
479 "\t # seek lock : %lu\n"
480 "\t # seek unlock : %lu (%ld)\n"
481 "\t # wait time for seek : %.3f msec\n"
482 "\t # wait time for seek/lock : %.3f nsec\n",
483 lock_stats[lbl].num_seek_locked,
484 lock_stats[lbl].num_seek_unlocked,
485 lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked,
486 (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0,
487 lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0);
488
489 if (lock_stats[lbl].num_read_locked)
490 fprintf(stderr,
491 "\t # read lock : %lu\n"
492 "\t # read unlock : %lu (%ld)\n"
493 "\t # wait time for read : %.3f msec\n"
494 "\t # wait time for read/lock : %.3f nsec\n",
495 lock_stats[lbl].num_read_locked,
496 lock_stats[lbl].num_read_unlocked,
497 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
498 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
499 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
500 }
501}
502
Willy Tarreau407ef892021-10-05 18:39:27 +0200503void __ha_rwlock_init(struct ha_rwlock *l)
504{
505 memset(l, 0, sizeof(struct ha_rwlock));
506 __RWLOCK_INIT(&l->lock);
507}
508
509void __ha_rwlock_destroy(struct ha_rwlock *l)
510{
511 __RWLOCK_DESTROY(&l->lock);
512 memset(l, 0, sizeof(struct ha_rwlock));
513}
514
515
516void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
517 const char *func, const char *file, int line)
518{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200519 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
520 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200521 uint64_t start_time;
522
Willy Tarreau7aa41192022-07-15 17:53:10 +0200523 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200524 abort();
525
Willy Tarreau7aa41192022-07-15 17:53:10 +0200526 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200527
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200528 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200529 __RWLOCK_WRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200530 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200531
532 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
533
Willy Tarreau7aa41192022-07-15 17:53:10 +0200534 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200535 l->info.last_location.function = func;
536 l->info.last_location.file = file;
537 l->info.last_location.line = line;
538
Willy Tarreau7aa41192022-07-15 17:53:10 +0200539 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200540}
541
542int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
543 const char *func, const char *file, int line)
544{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200545 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
546 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200547 uint64_t start_time;
548 int r;
549
Willy Tarreau7aa41192022-07-15 17:53:10 +0200550 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200551 abort();
552
553 /* We set waiting writer because trywrlock could wait for readers to quit */
Willy Tarreau7aa41192022-07-15 17:53:10 +0200554 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200555
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200556 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200557 r = __RWLOCK_TRYWRLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200558 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200559 if (unlikely(r)) {
Willy Tarreau7aa41192022-07-15 17:53:10 +0200560 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200561 return r;
562 }
563 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
564
Willy Tarreau7aa41192022-07-15 17:53:10 +0200565 st->cur_writer = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200566 l->info.last_location.function = func;
567 l->info.last_location.file = file;
568 l->info.last_location.line = line;
569
Willy Tarreau7aa41192022-07-15 17:53:10 +0200570 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200571
572 return 0;
573}
574
575void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
576 const char *func, const char *file, int line)
577{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200578 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
579 struct ha_rwlock_state *st = &l->info.st[tgid-1];
580
581 if (unlikely(!(st->cur_writer & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200582 /* the thread is not owning the lock for write */
583 abort();
584 }
585
Willy Tarreau7aa41192022-07-15 17:53:10 +0200586 st->cur_writer = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200587 l->info.last_location.function = func;
588 l->info.last_location.file = file;
589 l->info.last_location.line = line;
590
591 __RWLOCK_WRUNLOCK(&l->lock);
592
593 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
594}
595
596void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
597{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200598 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
599 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200600 uint64_t start_time;
601
Willy Tarreau7aa41192022-07-15 17:53:10 +0200602 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200603 abort();
604
Willy Tarreau7aa41192022-07-15 17:53:10 +0200605 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200606
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200607 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200608 __RWLOCK_RDLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200609 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200610 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
611
Willy Tarreau7aa41192022-07-15 17:53:10 +0200612 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200613
Willy Tarreau7aa41192022-07-15 17:53:10 +0200614 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200615}
616
617int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
618{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200619 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
620 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200621 int r;
622
Willy Tarreau7aa41192022-07-15 17:53:10 +0200623 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200624 abort();
625
626 /* try read should never wait */
627 r = __RWLOCK_TRYRDLOCK(&l->lock);
628 if (unlikely(r))
629 return r;
630 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
631
Willy Tarreau7aa41192022-07-15 17:53:10 +0200632 HA_ATOMIC_OR(&st->cur_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200633
634 return 0;
635}
636
637void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
638{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200639 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
640 struct ha_rwlock_state *st = &l->info.st[tgid-1];
641
642 if (unlikely(!(st->cur_readers & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200643 /* the thread is not owning the lock for read */
644 abort();
645 }
646
Willy Tarreau7aa41192022-07-15 17:53:10 +0200647 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200648
649 __RWLOCK_RDUNLOCK(&l->lock);
650
651 HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked);
652}
653
654void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l,
655 const char *func, const char *file, int line)
656{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200657 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
658 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200659 uint64_t start_time;
660
Willy Tarreau7aa41192022-07-15 17:53:10 +0200661 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200662 abort();
663
Willy Tarreau7aa41192022-07-15 17:53:10 +0200664 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200665 abort();
666
Willy Tarreau7aa41192022-07-15 17:53:10 +0200667 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200668
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200669 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200670 __RWLOCK_WRTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200671 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200672
673 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
674
Willy Tarreau7aa41192022-07-15 17:53:10 +0200675 HA_ATOMIC_OR(&st->cur_readers, tbit);
676 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200677 l->info.last_location.function = func;
678 l->info.last_location.file = file;
679 l->info.last_location.line = line;
680
Willy Tarreau7aa41192022-07-15 17:53:10 +0200681 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200682}
683
684void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l,
685 const char *func, const char *file, int line)
686{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200687 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
688 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200689 uint64_t start_time;
690
Willy Tarreau7aa41192022-07-15 17:53:10 +0200691 if ((st->cur_readers | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200692 abort();
693
Willy Tarreau7aa41192022-07-15 17:53:10 +0200694 if (!(st->cur_writer & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200695 abort();
696
Willy Tarreau7aa41192022-07-15 17:53:10 +0200697 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200698
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200699 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200700 __RWLOCK_WRTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200701 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200702
703 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
704
Willy Tarreau7aa41192022-07-15 17:53:10 +0200705 HA_ATOMIC_OR(&st->cur_seeker, tbit);
706 HA_ATOMIC_AND(&st->cur_writer, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200707 l->info.last_location.function = func;
708 l->info.last_location.file = file;
709 l->info.last_location.line = line;
710
Willy Tarreau7aa41192022-07-15 17:53:10 +0200711 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200712}
713
714void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l,
715 const char *func, const char *file, int line)
716{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200717 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
718 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200719 uint64_t start_time;
720
Willy Tarreau7aa41192022-07-15 17:53:10 +0200721 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200722 abort();
723
Willy Tarreau7aa41192022-07-15 17:53:10 +0200724 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200725
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200726 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200727 __RWLOCK_SKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200728 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200729
730 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
731
Willy Tarreau7aa41192022-07-15 17:53:10 +0200732 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200733 l->info.last_location.function = func;
734 l->info.last_location.file = file;
735 l->info.last_location.line = line;
736
Willy Tarreau7aa41192022-07-15 17:53:10 +0200737 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200738}
739
740void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l,
741 const char *func, const char *file, int line)
742{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200743 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
744 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200745 uint64_t start_time;
746
Willy Tarreau7aa41192022-07-15 17:53:10 +0200747 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200748 abort();
749
Willy Tarreau7aa41192022-07-15 17:53:10 +0200750 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200751 abort();
752
Willy Tarreau7aa41192022-07-15 17:53:10 +0200753 HA_ATOMIC_OR(&st->wait_writers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200754
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200755 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200756 __RWLOCK_SKTOWR(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200757 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200758
759 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
760
Willy Tarreau7aa41192022-07-15 17:53:10 +0200761 HA_ATOMIC_OR(&st->cur_writer, tbit);
762 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200763 l->info.last_location.function = func;
764 l->info.last_location.file = file;
765 l->info.last_location.line = line;
766
Willy Tarreau7aa41192022-07-15 17:53:10 +0200767 HA_ATOMIC_AND(&st->wait_writers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200768}
769
770void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l,
771 const char *func, const char *file, int line)
772{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200773 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
774 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200775 uint64_t start_time;
776
Willy Tarreau7aa41192022-07-15 17:53:10 +0200777 if ((st->cur_readers | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200778 abort();
779
Willy Tarreau7aa41192022-07-15 17:53:10 +0200780 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200781 abort();
782
Willy Tarreau7aa41192022-07-15 17:53:10 +0200783 HA_ATOMIC_OR(&st->wait_readers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200784
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200785 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200786 __RWLOCK_SKTORD(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200787 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200788
789 HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked);
790
Willy Tarreau7aa41192022-07-15 17:53:10 +0200791 HA_ATOMIC_OR(&st->cur_readers, tbit);
792 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200793 l->info.last_location.function = func;
794 l->info.last_location.file = file;
795 l->info.last_location.line = line;
796
Willy Tarreau7aa41192022-07-15 17:53:10 +0200797 HA_ATOMIC_AND(&st->wait_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200798}
799
800void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l,
801 const char *func, const char *file, int line)
802{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200803 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
804 struct ha_rwlock_state *st = &l->info.st[tgid-1];
805 if (!(st->cur_seeker & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200806 abort();
807
Willy Tarreau7aa41192022-07-15 17:53:10 +0200808 HA_ATOMIC_AND(&st->cur_seeker, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200809 l->info.last_location.function = func;
810 l->info.last_location.file = file;
811 l->info.last_location.line = line;
812
813 __RWLOCK_SKUNLOCK(&l->lock);
814
815 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked);
816}
817
818int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l,
819 const char *func, const char *file, int line)
820{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200821 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
822 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200823 uint64_t start_time;
824 int r;
825
Willy Tarreau7aa41192022-07-15 17:53:10 +0200826 if ((st->cur_readers | st->cur_seeker | st->cur_writer) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200827 abort();
828
Willy Tarreau7aa41192022-07-15 17:53:10 +0200829 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200830
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200831 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200832 r = __RWLOCK_TRYSKLOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200833 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200834
835 if (likely(!r)) {
836 /* got the lock ! */
837 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200838 HA_ATOMIC_OR(&st->cur_seeker, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200839 l->info.last_location.function = func;
840 l->info.last_location.file = file;
841 l->info.last_location.line = line;
842 }
843
Willy Tarreau7aa41192022-07-15 17:53:10 +0200844 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200845 return r;
846}
847
848int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l,
849 const char *func, const char *file, int line)
850{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200851 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
852 struct ha_rwlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200853 uint64_t start_time;
854 int r;
855
Willy Tarreau7aa41192022-07-15 17:53:10 +0200856 if ((st->cur_writer | st->cur_seeker) & tbit)
Willy Tarreau407ef892021-10-05 18:39:27 +0200857 abort();
858
Willy Tarreau7aa41192022-07-15 17:53:10 +0200859 if (!(st->cur_readers & tbit))
Willy Tarreau407ef892021-10-05 18:39:27 +0200860 abort();
861
Willy Tarreau7aa41192022-07-15 17:53:10 +0200862 HA_ATOMIC_OR(&st->wait_seekers, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200863
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200864 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200865 r = __RWLOCK_TRYRDTOSK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200866 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200867
868 if (likely(!r)) {
869 /* got the lock ! */
870 HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked);
Willy Tarreau7aa41192022-07-15 17:53:10 +0200871 HA_ATOMIC_OR(&st->cur_seeker, tbit);
872 HA_ATOMIC_AND(&st->cur_readers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200873 l->info.last_location.function = func;
874 l->info.last_location.file = file;
875 l->info.last_location.line = line;
876 }
877
Willy Tarreau7aa41192022-07-15 17:53:10 +0200878 HA_ATOMIC_AND(&st->wait_seekers, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200879 return r;
880}
881
882void __spin_init(struct ha_spinlock *l)
883{
884 memset(l, 0, sizeof(struct ha_spinlock));
885 __SPIN_INIT(&l->lock);
886}
887
888void __spin_destroy(struct ha_spinlock *l)
889{
890 __SPIN_DESTROY(&l->lock);
891 memset(l, 0, sizeof(struct ha_spinlock));
892}
893
894void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
895 const char *func, const char *file, int line)
896{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200897 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
898 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200899 uint64_t start_time;
900
Willy Tarreau7aa41192022-07-15 17:53:10 +0200901 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200902 /* the thread is already owning the lock */
903 abort();
904 }
905
Willy Tarreau7aa41192022-07-15 17:53:10 +0200906 HA_ATOMIC_OR(&st->waiters, tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200907
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200908 start_time = now_mono_time();
Willy Tarreau407ef892021-10-05 18:39:27 +0200909 __SPIN_LOCK(&l->lock);
Willy Tarreaudced3eb2021-10-05 18:48:23 +0200910 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time));
Willy Tarreau407ef892021-10-05 18:39:27 +0200911
912 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
913
914
Willy Tarreau7aa41192022-07-15 17:53:10 +0200915 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200916 l->info.last_location.function = func;
917 l->info.last_location.file = file;
918 l->info.last_location.line = line;
919
Willy Tarreau7aa41192022-07-15 17:53:10 +0200920 HA_ATOMIC_AND(&st->waiters, ~tbit);
Willy Tarreau407ef892021-10-05 18:39:27 +0200921}
922
923int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
924 const char *func, const char *file, int line)
925{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200926 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
927 struct ha_spinlock_state *st = &l->info.st[tgid-1];
Willy Tarreau407ef892021-10-05 18:39:27 +0200928 int r;
929
Willy Tarreau7aa41192022-07-15 17:53:10 +0200930 if (unlikely(st->owner & tbit)) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200931 /* the thread is already owning the lock */
932 abort();
933 }
934
935 /* try read should never wait */
936 r = __SPIN_TRYLOCK(&l->lock);
937 if (unlikely(r))
938 return r;
939 HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked);
940
Willy Tarreau7aa41192022-07-15 17:53:10 +0200941 st->owner = tbit;
Willy Tarreau407ef892021-10-05 18:39:27 +0200942 l->info.last_location.function = func;
943 l->info.last_location.file = file;
944 l->info.last_location.line = line;
945
946 return 0;
947}
948
949void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
950 const char *func, const char *file, int line)
951{
Willy Tarreau7aa41192022-07-15 17:53:10 +0200952 ulong tbit = (ti && ti->ltid_bit) ? ti->ltid_bit : 1;
953 struct ha_spinlock_state *st = &l->info.st[tgid-1];
954
955 if (unlikely(!(st->owner & tbit))) {
Willy Tarreau407ef892021-10-05 18:39:27 +0200956 /* the thread is not owning the lock */
957 abort();
958 }
959
Willy Tarreau7aa41192022-07-15 17:53:10 +0200960 st->owner = 0;
Willy Tarreau407ef892021-10-05 18:39:27 +0200961 l->info.last_location.function = func;
962 l->info.last_location.file = file;
963 l->info.last_location.line = line;
964
965 __SPIN_UNLOCK(&l->lock);
966 HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked);
967}
968
969#endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL)
970
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200971/* Depending on the platform and how libpthread was built, pthread_exit() may
972 * involve some code in libgcc_s that would be loaded on exit for the first
973 * time, causing aborts if the process is chrooted. It's harmless bit very
974 * dirty. There isn't much we can do to make sure libgcc_s is loaded only if
975 * needed, so what we do here is that during early boot we create a dummy
976 * thread that immediately exits. This will lead to libgcc_s being loaded
977 * during boot on the platforms where it's required.
978 */
979static void *dummy_thread_function(void *data)
980{
981 pthread_exit(NULL);
982 return NULL;
983}
984
985static inline void preload_libgcc_s(void)
986{
987 pthread_t dummy_thread;
988 pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL);
989 pthread_join(dummy_thread, NULL);
990}
991
Willy Tarreau3f567e42020-05-28 15:29:19 +0200992static void __thread_init(void)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200993{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100994 char *ptr = NULL;
995
Willy Tarreauf734ebf2020-09-09 17:07:54 +0200996 preload_libgcc_s();
Willy Tarreau77b98222020-09-02 08:04:35 +0200997
Willy Tarreau149ab772019-01-26 14:27:06 +0100998 thread_cpus_enabled_at_boot = thread_cpus_enabled();
999
1000 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
1001 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +01001002 hap_register_build_opts(ptr, 1);
1003
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001004#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
1005 memset(lock_stats, 0, sizeof(lock_stats));
1006#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001007}
Willy Tarreau8ead1d02022-04-25 19:23:17 +02001008INITCALL0(STG_PREPARE, __thread_init);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001009
Willy Tarreau8459f252018-12-15 16:48:14 +01001010#else
1011
Willy Tarreauaa992762021-10-06 23:33:20 +02001012/* send signal <sig> to thread <thr> (send to process in fact) */
1013void ha_tkill(unsigned int thr, int sig)
1014{
1015 raise(sig);
1016}
1017
1018/* send signal <sig> to all threads (send to process in fact) */
1019void ha_tkillall(int sig)
1020{
1021 raise(sig);
1022}
1023
1024void ha_thread_relax(void)
1025{
1026#ifdef _POSIX_PRIORITY_SCHEDULING
1027 sched_yield();
1028#endif
1029}
1030
Willy Tarreau8459f252018-12-15 16:48:14 +01001031REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
1032
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001033#endif // USE_THREAD
1034
1035
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001036/* scans the configured thread mapping and establishes the final one. Returns <0
1037 * on failure, >=0 on success.
1038 */
1039int thread_map_to_groups()
1040{
1041 int t, g, ut, ug;
1042 int q, r;
Willy Tarreaucce203a2022-06-24 15:55:11 +02001043 ulong m __maybe_unused;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001044
1045 ut = ug = 0; // unassigned threads & groups
1046
1047 for (t = 0; t < global.nbthread; t++) {
1048 if (!ha_thread_info[t].tg)
1049 ut++;
1050 }
1051
1052 for (g = 0; g < global.nbtgroups; g++) {
1053 if (!ha_tgroup_info[g].count)
1054 ug++;
Willy Tarreau60fe4a92022-06-28 17:48:07 +02001055 ha_tgroup_info[g].tgid_bit = 1UL << g;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001056 }
1057
1058 if (ug > ut) {
1059 ha_alert("More unassigned thread-groups (%d) than threads (%d). Please reduce thread-groups\n", ug, ut);
1060 return -1;
1061 }
1062
1063 /* look for first unassigned thread */
1064 for (t = 0; t < global.nbthread && ha_thread_info[t].tg; t++)
1065 ;
1066
1067 /* assign threads to empty groups */
1068 for (g = 0; ug && ut; ) {
1069 /* due to sparse thread assignment we can end up with more threads
1070 * per group on last assigned groups than former ones, so we must
1071 * always try to pack the maximum remaining ones together first.
1072 */
1073 q = ut / ug;
1074 r = ut % ug;
1075 if ((q + !!r) > MAX_THREADS_PER_GROUP) {
1076 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);
1077 return -1;
1078 }
1079
1080 /* thread <t> is the next unassigned one. Let's look for next
1081 * unassigned group, we know there are some left
1082 */
1083 while (ut >= ug && ha_tgroup_info[g].count)
1084 g++;
1085
1086 /* group g is unassigned, try to fill it with consecutive threads */
1087 while (ut && ut >= ug && ha_tgroup_info[g].count < q + !!r &&
1088 (!ha_tgroup_info[g].count || t == ha_tgroup_info[g].base + ha_tgroup_info[g].count)) {
1089
1090 if (!ha_tgroup_info[g].count) {
1091 /* assign new group */
1092 ha_tgroup_info[g].base = t;
1093 ug--;
1094 }
1095
1096 ha_tgroup_info[g].count++;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001097 ha_thread_info[t].tgid = g + 1;
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001098 ha_thread_info[t].tg = &ha_tgroup_info[g];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001099 ha_thread_info[t].tg_ctx = &ha_tgroup_ctx[g];
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001100
1101 ut--;
1102 /* switch to next unassigned thread */
1103 while (++t < global.nbthread && ha_thread_info[t].tg)
1104 ;
1105 }
1106 }
1107
1108 if (ut) {
1109 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);
1110 return -1;
1111 }
1112
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001113 for (t = 0; t < global.nbthread; t++) {
1114 ha_thread_info[t].tid = t;
1115 ha_thread_info[t].ltid = t - ha_thread_info[t].tg->base;
Willy Tarreaucc7a11e2021-09-28 08:53:11 +02001116 ha_thread_info[t].ltid_bit = 1UL << ha_thread_info[t].ltid;
1117 }
1118
Willy Tarreaucce203a2022-06-24 15:55:11 +02001119 m = 0;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001120 for (g = 0; g < global.nbtgroups; g++) {
1121 ha_tgroup_info[g].threads_enabled = nbits(ha_tgroup_info[g].count);
Willy Tarreaucce203a2022-06-24 15:55:11 +02001122 if (!ha_tgroup_info[g].count)
1123 continue;
1124 m |= 1UL << g;
Willy Tarreau377e37a2022-06-24 15:18:49 +02001125
1126 }
1127
Willy Tarreaucce203a2022-06-24 15:55:11 +02001128#ifdef USE_THREAD
1129 all_tgroups_mask = m;
1130#endif
Willy Tarreaue6806eb2021-09-27 10:10:26 +02001131 return 0;
1132}
1133
Willy Tarreau6018c022022-06-28 08:27:43 +02001134/* converts a configuration thread num or group+mask to a global group+mask
1135 * depending on the configured thread group id. This is essentially for use
1136 * with the "thread" directive on "bind" lines, where "thread 4-6" might be
1137 * turned to "2/1-3". It cannot be used before the thread mapping above was
1138 * completed and the thread group number configured. Possible options:
Willy Tarreau627def92021-09-29 18:59:47 +02001139 * - igid == 0: imask represents global IDs. We have to check that all
1140 * configured threads in the mask belong to the same group. If imask is zero
1141 * it means everything, so for now we only support this with a single group.
Willy Tarreau6018c022022-06-28 08:27:43 +02001142 * - igid > 0, imask = 0: convert global values to local values for this thread
1143 * - igid > 0, imask > 0: convert global values to local values
1144 * Note that the output mask is always local to the group.
Willy Tarreau627def92021-09-29 18:59:47 +02001145 *
1146 * Returns <0 on failure, >=0 on success.
1147 */
1148int thread_resolve_group_mask(uint igid, ulong imask, uint *ogid, ulong *omask, char **err)
1149{
1150 ulong mask;
1151 uint t;
1152
1153 if (igid == 0) {
1154 /* unspecified group, IDs are global */
1155 if (!imask) {
1156 /* all threads of all groups */
1157 if (global.nbtgroups > 1) {
1158 memprintf(err, "'thread' directive spans multiple groups");
1159 return -1;
1160 }
Willy Tarreau627def92021-09-29 18:59:47 +02001161 *ogid = 1; // first and only group
Willy Tarreau6018c022022-06-28 08:27:43 +02001162 *omask = ha_tgroup_info[0].threads_enabled;
Willy Tarreau627def92021-09-29 18:59:47 +02001163 return 0;
1164 } else {
1165 /* some global threads */
Willy Tarreau627def92021-09-29 18:59:47 +02001166 for (t = 0; t < global.nbthread; t++) {
1167 if (imask & (1UL << t)) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001168 if (ha_thread_info[t].tgid != igid) {
Willy Tarreau627def92021-09-29 18:59:47 +02001169 if (!igid)
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001170 igid = ha_thread_info[t].tgid;
Willy Tarreau627def92021-09-29 18:59:47 +02001171 else {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001172 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 +02001173 return -1;
1174 }
1175 }
1176 }
1177 }
1178
1179 if (!igid) {
1180 memprintf(err, "'thread' directive contains threads that belong to no group");
1181 return -1;
1182 }
1183
1184 /* we have a valid group, convert this to global thread IDs */
1185 *ogid = igid;
Willy Tarreau6018c022022-06-28 08:27:43 +02001186 imask = imask >> ha_tgroup_info[igid - 1].base;
1187 imask &= ha_tgroup_info[igid - 1].threads_enabled;
1188 *omask = imask;
Willy Tarreau627def92021-09-29 18:59:47 +02001189 return 0;
1190 }
1191 } else {
1192 /* group was specified */
1193 if (igid > global.nbtgroups) {
1194 memprintf(err, "'thread' directive references non-existing thread group %u", igid);
1195 return -1;
1196 }
1197
1198 if (!imask) {
1199 /* all threads of this groups. Let's make a mask from their count and base. */
1200 *ogid = igid;
Willy Tarreau6018c022022-06-28 08:27:43 +02001201 *omask = nbits(ha_tgroup_info[igid - 1].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001202 return 0;
1203 } else {
1204 /* some local threads. Keep only existing ones for this group */
1205
Willy Tarreau6018c022022-06-28 08:27:43 +02001206 mask = nbits(ha_tgroup_info[igid - 1].count);
Willy Tarreau627def92021-09-29 18:59:47 +02001207
1208 if (!(mask & imask)) {
1209 /* no intersection between the thread group's
1210 * threads and the bind line's.
1211 */
1212#ifdef THREAD_AUTO_ADJUST_GROUPS
1213 unsigned long new_mask = 0;
1214
1215 while (imask) {
1216 new_mask |= imask & mask;
1217 imask >>= ha_tgroup_info[igid - 1].count;
1218 }
1219 imask = new_mask;
1220#else
1221 memprintf(err, "'thread' directive only references threads not belonging to the group");
1222 return -1;
1223#endif
1224 }
1225
Willy Tarreau6018c022022-06-28 08:27:43 +02001226 *omask = mask & imask;
Willy Tarreau627def92021-09-29 18:59:47 +02001227 *ogid = igid;
1228 return 0;
1229 }
1230 }
1231}
1232
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001233/* Parse the "nbthread" global directive, which takes an integer argument that
1234 * contains the desired number of threads.
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001235 */
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001236static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx,
1237 const struct proxy *defpx, const char *file, int line,
1238 char **err)
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001239{
1240 long nbthread;
1241 char *errptr;
1242
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001243 if (too_many_args(1, args, err, NULL))
1244 return -1;
1245
1246 nbthread = strtol(args[1], &errptr, 10);
1247 if (!*args[1] || *errptr) {
1248 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1249 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001250 }
1251
1252#ifndef USE_THREAD
1253 if (nbthread != 1) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001254 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]);
1255 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001256 }
1257#else
1258 if (nbthread < 1 || nbthread > MAX_THREADS) {
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001259 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread);
1260 return -1;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001261 }
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001262#endif
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001263
1264 HA_DIAG_WARNING_COND(global.nbthread,
Willy Tarreauc33b9692021-09-22 12:07:23 +02001265 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1266 file, line, args[0]);
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001267
1268 global.nbthread = nbthread;
1269 return 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001270}
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001271
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001272/* Parse the "thread-group" global directive, which takes an integer argument
1273 * that designates a thread group, and a list of threads to put into that group.
1274 */
1275static int cfg_parse_thread_group(char **args, int section_type, struct proxy *curpx,
1276 const struct proxy *defpx, const char *file, int line,
1277 char **err)
1278{
1279 char *errptr;
1280 long tnum, tend, tgroup;
1281 int arg, tot;
1282
1283 tgroup = strtol(args[1], &errptr, 10);
1284 if (!*args[1] || *errptr) {
1285 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1286 return -1;
1287 }
1288
1289 if (tgroup < 1 || tgroup > MAX_TGROUPS) {
1290 memprintf(err, "'%s' thread-group number must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, tgroup);
1291 return -1;
1292 }
1293
1294 /* look for a preliminary definition of any thread pointing to this
1295 * group, and remove them.
1296 */
1297 if (ha_tgroup_info[tgroup-1].count) {
1298 ha_warning("parsing [%s:%d] : '%s %ld' was already defined and will be overridden.\n",
1299 file, line, args[0], tgroup);
1300
1301 for (tnum = ha_tgroup_info[tgroup-1].base;
1302 tnum < ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count;
1303 tnum++) {
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001304 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001305 ha_thread_info[tnum-1].tg = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001306 ha_thread_info[tnum-1].tgid = 0;
Willy Tarreau03f9b352022-06-27 16:02:24 +02001307 ha_thread_info[tnum-1].tg_ctx = NULL;
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001308 }
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001309 }
1310 ha_tgroup_info[tgroup-1].count = ha_tgroup_info[tgroup-1].base = 0;
1311 }
1312
1313 tot = 0;
1314 for (arg = 2; args[arg] && *args[arg]; arg++) {
1315 tend = tnum = strtol(args[arg], &errptr, 10);
1316
1317 if (*errptr == '-')
1318 tend = strtol(errptr + 1, &errptr, 10);
1319
1320 if (*errptr || tnum < 1 || tend < 1 || tnum > MAX_THREADS || tend > MAX_THREADS) {
1321 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);
1322 return -1;
1323 }
1324
1325 for(; tnum <= tend; tnum++) {
1326 if (ha_thread_info[tnum-1].tg == &ha_tgroup_info[tgroup-1]) {
1327 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld assigned more than once on the same line.\n",
1328 file, line, args[0], tgroup, tnum);
1329 } else if (ha_thread_info[tnum-1].tg) {
1330 ha_warning("parsing [%s:%d] : '%s %ld': thread %ld was previously assigned to thread group %ld and will be overridden.\n",
1331 file, line, args[0], tgroup, tnum,
1332 (long)(ha_thread_info[tnum-1].tg - &ha_tgroup_info[0] + 1));
1333 }
1334
1335 if (!ha_tgroup_info[tgroup-1].count) {
1336 ha_tgroup_info[tgroup-1].base = tnum-1;
1337 ha_tgroup_info[tgroup-1].count = 1;
1338 }
1339 else if (tnum >= ha_tgroup_info[tgroup-1].base + ha_tgroup_info[tgroup-1].count) {
1340 ha_tgroup_info[tgroup-1].count = tnum - ha_tgroup_info[tgroup-1].base;
1341 }
1342 else if (tnum < ha_tgroup_info[tgroup-1].base) {
1343 ha_tgroup_info[tgroup-1].count += ha_tgroup_info[tgroup-1].base - tnum-1;
1344 ha_tgroup_info[tgroup-1].base = tnum - 1;
1345 }
1346
Willy Tarreau66ad98a2022-06-28 10:49:57 +02001347 ha_thread_info[tnum-1].tgid = tgroup;
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001348 ha_thread_info[tnum-1].tg = &ha_tgroup_info[tgroup-1];
Willy Tarreau03f9b352022-06-27 16:02:24 +02001349 ha_thread_info[tnum-1].tg_ctx = &ha_tgroup_ctx[tgroup-1];
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001350 tot++;
1351 }
1352 }
1353
1354 if (ha_tgroup_info[tgroup-1].count > tot) {
1355 memprintf(err, "'%s %ld' assigned sparse threads, only contiguous supported", args[0], tgroup);
1356 return -1;
1357 }
1358
1359 if (ha_tgroup_info[tgroup-1].count > MAX_THREADS_PER_GROUP) {
1360 memprintf(err, "'%s %ld' assigned too many threads (%d, max=%d)", args[0], tgroup, tot, MAX_THREADS_PER_GROUP);
1361 return -1;
1362 }
1363
1364 return 0;
1365}
1366
Willy Tarreauc33b9692021-09-22 12:07:23 +02001367/* Parse the "thread-groups" global directive, which takes an integer argument
1368 * that contains the desired number of thread groups.
1369 */
1370static int cfg_parse_thread_groups(char **args, int section_type, struct proxy *curpx,
1371 const struct proxy *defpx, const char *file, int line,
1372 char **err)
1373{
1374 long nbtgroups;
1375 char *errptr;
1376
1377 if (too_many_args(1, args, err, NULL))
1378 return -1;
1379
1380 nbtgroups = strtol(args[1], &errptr, 10);
1381 if (!*args[1] || *errptr) {
1382 memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]);
1383 return -1;
1384 }
1385
1386#ifndef USE_THREAD
1387 if (nbtgroups != 1) {
1388 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]);
1389 return -1;
1390 }
1391#else
1392 if (nbtgroups < 1 || nbtgroups > MAX_TGROUPS) {
1393 memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_TGROUPS, nbtgroups);
1394 return -1;
1395 }
1396#endif
1397
1398 HA_DIAG_WARNING_COND(global.nbtgroups,
1399 "parsing [%s:%d] : '%s' is already defined and will be overridden.\n",
1400 file, line, args[0]);
1401
1402 global.nbtgroups = nbtgroups;
1403 return 0;
1404}
1405
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001406/* config keyword parsers */
1407static struct cfg_kw_list cfg_kws = {ILH, {
1408 { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 },
Willy Tarreaud04bc3a2021-09-27 13:55:10 +02001409 { CFG_GLOBAL, "thread-group", cfg_parse_thread_group, 0 },
Willy Tarreauc33b9692021-09-22 12:07:23 +02001410 { CFG_GLOBAL, "thread-groups", cfg_parse_thread_groups, 0 },
Willy Tarreau51ec03a2021-09-22 11:55:22 +02001411 { 0, NULL, NULL }
1412}};
1413
1414INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);