blob: 686edca8e4cd4bd70288fca0a0574d844bd92dbf [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
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020032
33#ifdef USE_THREAD
34
Willy Tarreau60b639c2018-08-02 10:16:17 +020035volatile unsigned long threads_want_rdv_mask = 0;
36volatile unsigned long threads_harmless_mask = 0;
Willy Tarreau0ccd3222018-07-30 10:34:35 +020037volatile unsigned long all_threads_mask = 1; // nbthread 1 assumed by default
Willy Tarreau0c026f42018-08-01 19:12:20 +020038THREAD_LOCAL unsigned int tid = 0;
39THREAD_LOCAL unsigned long tid_bit = (1UL << 0);
Willy Tarreau149ab772019-01-26 14:27:06 +010040int thread_cpus_enabled_at_boot = 1;
Willy Tarreau0c026f42018-08-01 19:12:20 +020041
Christopher Faulet339fff82017-10-19 11:59:15 +020042
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020043#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
44struct lock_stat lock_stats[LOCK_LABELS];
45#endif
46
Willy Tarreau60b639c2018-08-02 10:16:17 +020047/* Marks the thread as harmless until the last thread using the rendez-vous
48 * point quits. Given that we can wait for a long time, sched_yield() is used
49 * when available to offer the CPU resources to competing threads if needed.
50 */
51void thread_harmless_till_end()
52{
Olivier Houchardb23a61f2019-03-08 18:51:17 +010053 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +020054 while (threads_want_rdv_mask & all_threads_mask) {
55#if _POSIX_PRIORITY_SCHEDULING
56 sched_yield();
57#else
58 pl_cpu_relax();
59#endif
60 }
61}
62
63/* Isolates the current thread : request the ability to work while all other
64 * threads are harmless. Only returns once all of them are harmless, with the
65 * current thread's bit in threads_harmless_mask cleared. Needs to be completed
66 * using thread_release().
67 */
68void thread_isolate()
69{
70 unsigned long old;
71
Olivier Houchardb23a61f2019-03-08 18:51:17 +010072 _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
73 __ha_barrier_atomic_store();
74 _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit);
Willy Tarreau60b639c2018-08-02 10:16:17 +020075
76 /* wait for all threads to become harmless */
77 old = threads_harmless_mask;
78 while (1) {
79 if (unlikely((old & all_threads_mask) != all_threads_mask))
80 old = threads_harmless_mask;
Olivier Houchardb23a61f2019-03-08 18:51:17 +010081 else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit))
Willy Tarreau60b639c2018-08-02 10:16:17 +020082 break;
83
84#if _POSIX_PRIORITY_SCHEDULING
85 sched_yield();
86#else
87 pl_cpu_relax();
88#endif
89 }
90 /* one thread gets released at a time here, with its harmess bit off.
91 * The loss of this bit makes the other one continue to spin while the
92 * thread is working alone.
93 */
94}
95
96/* Cancels the effect of thread_isolate() by releasing the current thread's bit
97 * in threads_want_rdv_mask and by marking this thread as harmless until the
98 * last worker finishes.
99 */
100void thread_release()
101{
Olivier Houchardb23a61f2019-03-08 18:51:17 +0100102 _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit);
Willy Tarreaua9c02522018-10-16 16:11:56 +0200103 thread_harmless_end();
Willy Tarreau60b639c2018-08-02 10:16:17 +0200104}
Christopher Faulet339fff82017-10-19 11:59:15 +0200105
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100106/* these calls are used as callbacks at init time */
107void ha_spin_init(HA_SPINLOCK_T *l)
108{
109 HA_SPIN_INIT(l);
110}
111
112/* these calls are used as callbacks at init time */
113void ha_rwlock_init(HA_RWLOCK_T *l)
114{
115 HA_RWLOCK_INIT(l);
116}
117
Willy Tarreau149ab772019-01-26 14:27:06 +0100118/* returns the number of CPUs the current process is enabled to run on */
119static int thread_cpus_enabled()
120{
121 int ret = 1;
122
123#ifdef USE_CPU_AFFINITY
124#if defined(__linux__) && defined(CPU_COUNT)
125 cpu_set_t mask;
126
127 if (sched_getaffinity(0, sizeof(mask), &mask) == 0)
128 ret = CPU_COUNT(&mask);
Olivier Houchard46453d32019-04-11 00:06:47 +0200129#elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY)
130 cpuset_t cpuset;
131 if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1,
132 sizeof(cpuset), &cpuset) == 0)
133 ret = CPU_COUNT(&cpuset);
Willy Tarreau149ab772019-01-26 14:27:06 +0100134#endif
135#endif
136 ret = MAX(ret, 1);
137 ret = MIN(ret, MAX_THREADS);
138 return ret;
139}
140
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200141__attribute__((constructor))
142static void __hathreads_init(void)
143{
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100144 char *ptr = NULL;
145
146 if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) {
147 ha_alert("MAX_THREADS value must be between 1 and %d inclusive; "
148 "HAProxy was built with value %d, please fix it and rebuild.\n",
149 LONGBITS, MAX_THREADS);
150 exit(1);
151 }
Willy Tarreau149ab772019-01-26 14:27:06 +0100152
153 thread_cpus_enabled_at_boot = thread_cpus_enabled();
154
155 memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).",
156 MAX_THREADS, thread_cpus_enabled_at_boot);
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100157 hap_register_build_opts(ptr, 1);
158
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200159#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
160 memset(lock_stats, 0, sizeof(lock_stats));
161#endif
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200162}
163
Willy Tarreau8459f252018-12-15 16:48:14 +0100164#else
165
166REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set).");
167
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200168#endif // USE_THREAD
169
170
171/* Parse the number of threads in argument <arg>, returns it and adjusts a few
172 * internal variables accordingly, or fails and returns zero with an error
173 * reason in <errmsg>. May be called multiple times while parsing.
174 */
175int parse_nbthread(const char *arg, char **err)
176{
177 long nbthread;
178 char *errptr;
179
180 nbthread = strtol(arg, &errptr, 10);
181 if (!*arg || *errptr) {
182 memprintf(err, "passed a missing or unparsable integer value in '%s'", arg);
183 return 0;
184 }
185
186#ifndef USE_THREAD
187 if (nbthread != 1) {
188 memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD");
189 return 0;
190 }
191#else
192 if (nbthread < 1 || nbthread > MAX_THREADS) {
193 memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread);
194 return 0;
195 }
196
Willy Tarreaufc647362019-02-02 17:05:03 +0100197 all_threads_mask = nbits(nbthread);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200198#endif
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200199 return nbthread;
200}