blob: 006a0896d97116f637facc3fa6fd0766ea94e9ee [file] [log] [blame]
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001/*
2 * functions about threads.
3 *
4 * Copyright (C) 2017 Christopher Fauet - cfaulet@haproxy.com
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau149ab772019-01-26 14:27:06 +010013#define _GNU_SOURCE
Christopher Faulet339fff82017-10-19 11:59:15 +020014#include <unistd.h>
Willy Tarreau0ccd3222018-07-30 10:34:35 +020015#include <stdlib.h>
Christopher Faulet339fff82017-10-19 11:59:15 +020016#include <fcntl.h>
17
Willy Tarreau149ab772019-01-26 14:27:06 +010018#ifdef USE_CPU_AFFINITY
19#include <sched.h>
20#endif
21
Olivier Houchard46453d32019-04-11 00:06:47 +020022#ifdef __FreeBSD__
23#include <sys/cpuset.h>
24#endif
25
Willy Tarreau04931492017-11-03 23:39:25 +010026#include <common/cfgparse.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020027#include <common/hathreads.h>
Christopher Faulet339fff82017-10-19 11:59:15 +020028#include <common/standard.h>
Willy Tarreau80713382018-11-26 10:19:54 +010029#include <types/global.h>
Christopher Faulet339fff82017-10-19 11:59:15 +020030#include <proto/fd.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020031
Willy Tarreau05ed14c2019-05-22 06:42:27 +020032struct thread_info thread_info[MAX_THREADS] = { };
Willy Tarreau8323a372019-05-20 18:57:53 +020033THREAD_LOCAL struct thread_info *ti = &thread_info[0];
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020034
35#ifdef USE_THREAD
36
Willy Tarreau60b639c2018-08-02 10:16:17 +020037volatile unsigned long threads_want_rdv_mask = 0;
38volatile unsigned long threads_harmless_mask = 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +020039volatile unsigned long all_threads_mask = 1; // nbthread 1 assumed by default
Willy Tarreau0c026f42018-08-01 19:12:20 +020040THREAD_LOCAL unsigned int tid = 0;
41THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
Willy Tarreau149ab772019-01-26 14:27:06 +010042int thread_cpus_enabled_at_boot = 1;
Willy Tarreau0c026f42018-08-01 19:12:20 +020043
Christopher Faulet339fff82017-10-19 11:59:15 +020044
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020045#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
46struct lock_stat lock_stats[LOCK_LABELS];
47#endif
48
Willy Tarreau60b639c2018-08-02 10:16:17 +020049/* Marks the thread as harmless until the last thread using the rendez-vous
50 * point quits. Given that we can wait for a long time, sched_yield() is used
51 * when available to offer the CPU resources to competing threads if needed.
52 */
53void thread_harmless_till_end()
54{
Olivier Houchardb23a61f2019-03-08 18:51:17 +010055 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +020056 while (threads_want_rdv_mask & all_threads_mask) {
Willy Tarreau38171da2019-05-17 16:33:13 +020057 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +020058 }
59}
60
61/* Isolates the current thread : request the ability to work while all other
62 * threads are harmless. Only returns once all of them are harmless, with the
63 * current thread's bit in threads_harmless_mask cleared. Needs to be completed
64 * using thread_release().
65 */
66void thread_isolate()
67{
68 unsigned long old;
69
Olivier Houchardb23a61f2019-03-08 18:51:17 +010070 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
71 __ha_barrier_atomic_store();
72 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +020073
74 /* wait for all threads to become harmless */
75 old = threads_harmless_mask;
76 while (1) {
77 if (unlikely((old & all_threads_mask) != all_threads_mask))
78 old = threads_harmless_mask;
Olivier Houchardb23a61f2019-03-08 18:51:17 +010079 else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
Willy Tarreau60b639c2018-08-02 10:16:17 +020080 break;
81
Willy Tarreau38171da2019-05-17 16:33:13 +020082 ha_thread_relax();
Willy Tarreau60b639c2018-08-02 10:16:17 +020083 }
84 /* one thread gets released at a time here, with its harmess bit off.
85 * The loss of this bit makes the other one continue to spin while the
86 * thread is working alone.
87 */
88}
89
90/* Cancels the effect of thread_isolate() by releasing the current thread's bit
91 * in threads_want_rdv_mask and by marking this thread as harmless until the
92 * last worker finishes.
93 */
94void thread_release()
95{
Olivier Houchardb23a61f2019-03-08 18:51:17 +010096 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
Willy Tarreau31cba0d2019-06-09 08:44:19 +020097 while (threads_want_rdv_mask & all_threads_mask) {
98 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
99 while (threads_want_rdv_mask & all_threads_mask)
100 ha_thread_relax();
101 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
102 }
Willy Tarreau60b639c2018-08-02 10:16:17 +0200103}
Christopher Faulet339fff82017-10-19 11:59:15 +0200104
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200105/* send signal <sig> to thread <thr> */
106void ha_tkill(unsigned int thr, int sig)
107{
108 pthread_kill(thread_info[thr].pthread, sig);
109}
110
111/* send signal <sig> to all threads. The calling thread is signaled last in
112 * order to allow all threads to synchronize in the handler.
113 */
114void ha_tkillall(int sig)
115{
116 unsigned int thr;
117
118 for (thr = 0; thr < global.nbthread; thr++) {
119 if (!(all_threads_mask & (1UL << thr)))
120 continue;
121 if (thr == tid)
122 continue;
123 pthread_kill(thread_info[thr].pthread, sig);
124 }
125 raise(sig);
126}
127
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100128/* these calls are used as callbacks at init time */
129void ha_spin_init(HA_SPINLOCK_T *l)
130{
131 HA_SPIN_INIT(l);
132}
133
134/* these calls are used as callbacks at init time */
135void ha_rwlock_init(HA_RWLOCK_T *l)
136{
137 HA_RWLOCK_INIT(l);
138}
139
Willy Tarreau149ab772019-01-26 14:27:06 +0100140/* returns the number of CPUs the current process is enabled to run on */
141static int thread_cpus_enabled()
142{
143 int ret = 1;
144
145#ifdef USE_CPU_AFFINITY
146#if defined(__linux__) && defined(CPU_COUNT)
147 cpu_set_t mask;
148
149 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
150 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200151#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
152 cpuset_t cpuset;
153 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
154 sizeof(cpuset), &cpuset) == 0)
155 ret = CPU_COUNT(&cpuset);
Willy Tarreau149ab772019-01-26 14:27:06 +0100156#endif
157#endif
158 ret = MAX(ret, 1);
159 ret = MIN(ret, MAX_THREADS);
160 return ret;
161}
162
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200163__attribute__((constructor))
164static void __hathreads_init(void)
165{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100166 char *ptr = NULL;
167
168 if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) {
169 ha_alert("MAX_THREADS value must be between 1 and %d inclusive; "
170 "HAProxy was built with value %d, please fix it and rebuild.\n",
171 LONGBITS, MAX_THREADS);
172 exit(1);
173 }
Willy Tarreau149ab772019-01-26 14:27:06 +0100174
175 thread_cpus_enabled_at_boot = thread_cpus_enabled();
176
177 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
178 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100179 hap_register_build_opts(ptr, 1);
180
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200181#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
182 memset(lock_stats, 0, sizeof(lock_stats));
183#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200184}
185
Willy Tarreau8459f252018-12-15 16:48:14 +0100186#else
187
188REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
189
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200190#endif // USE_THREAD
191
192
193/* Parse the number of threads in argument <arg>, returns it and adjusts a few
194 * internal variables accordingly, or fails and returns zero with an error
195 * reason in <errmsg>. May be called multiple times while parsing.
196 */
197int parse_nbthread(const char *arg, char **err)
198{
199 long nbthread;
200 char *errptr;
201
202 nbthread = strtol(arg, &errptr, 10);
203 if (!*arg || *errptr) {
204 memprintf(err, "passed a missing or unparsable integer value in '%s'", arg);
205 return 0;
206 }
207
208#ifndef USE_THREAD
209 if (nbthread != 1) {
210 memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD");
211 return 0;
212 }
213#else
214 if (nbthread < 1 || nbthread > MAX_THREADS) {
215 memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread);
216 return 0;
217 }
218
Willy Tarreaufc647362019-02-02 17:05:03 +0100219 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200220#endif
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200221 return nbthread;
222}