Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 13 | #define _GNU_SOURCE |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 14 | #include <unistd.h> |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 15 | #include <stdlib.h> |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 16 | #include <fcntl.h> |
| 17 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 18 | #ifdef USE_CPU_AFFINITY |
| 19 | #include <sched.h> |
| 20 | #endif |
| 21 | |
Olivier Houchard | 46453d3 | 2019-04-11 00:06:47 +0200 | [diff] [blame] | 22 | #ifdef __FreeBSD__ |
| 23 | #include <sys/cpuset.h> |
| 24 | #endif |
| 25 | |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 26 | #include <haproxy/cfgparse.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 27 | #include <haproxy/fd.h> |
| 28 | #include <haproxy/global.h> |
Willy Tarreau | 11bd6f7 | 2021-05-08 20:33:02 +0200 | [diff] [blame] | 29 | #include <haproxy/log.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 30 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 31 | #include <haproxy/tools.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 32 | |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 33 | struct thread_info ha_thread_info[MAX_THREADS] = { }; |
| 34 | THREAD_LOCAL struct thread_info *ti = &ha_thread_info[0]; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 35 | |
| 36 | #ifdef USE_THREAD |
| 37 | |
Willy Tarreau | 56c3b8b | 2021-04-10 17:28:18 +0200 | [diff] [blame] | 38 | volatile unsigned long threads_want_rdv_mask __read_mostly = 0; |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 39 | volatile unsigned long threads_harmless_mask = 0; |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame^] | 40 | volatile unsigned long threads_idle_mask = 0; |
Willy Tarreau | 9a1f573 | 2019-06-09 12:20:02 +0200 | [diff] [blame] | 41 | volatile unsigned long threads_sync_mask = 0; |
Willy Tarreau | 56c3b8b | 2021-04-10 17:28:18 +0200 | [diff] [blame] | 42 | volatile unsigned long all_threads_mask __read_mostly = 1; // nbthread 1 assumed by default |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 43 | THREAD_LOCAL unsigned int tid = 0; |
| 44 | THREAD_LOCAL unsigned long tid_bit = (1UL << 0); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 45 | int thread_cpus_enabled_at_boot = 1; |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 46 | |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 47 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 48 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 49 | struct lock_stat lock_stats[LOCK_LABELS]; |
| 50 | #endif |
| 51 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 52 | /* Marks the thread as harmless until the last thread using the rendez-vous |
Christopher Faulet | a9a9e9a | 2021-03-25 14:11:36 +0100 | [diff] [blame] | 53 | * point quits, excluding the current one. Thus an isolated thread may be safely |
| 54 | * marked as harmless. Given that we can wait for a long time, sched_yield() is |
| 55 | * used when available to offer the CPU resources to competing threads if |
| 56 | * needed. |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 57 | */ |
| 58 | void thread_harmless_till_end() |
| 59 | { |
Willy Tarreau | 286363b | 2021-08-04 10:33:57 +0200 | [diff] [blame] | 60 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 61 | while (threads_want_rdv_mask & all_threads_mask & ~tid_bit) { |
| 62 | ha_thread_relax(); |
| 63 | } |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* Isolates the current thread : request the ability to work while all other |
Willy Tarreau | f519cfa | 2021-08-04 11:22:07 +0200 | [diff] [blame] | 67 | * threads are harmless, as defined by thread_harmless_now() (i.e. they're not |
| 68 | * going to touch any visible memory area). Only returns once all of them are |
| 69 | * harmless, with the current thread's bit in threads_harmless_mask cleared. |
| 70 | * Needs to be completed using thread_release(). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 71 | */ |
| 72 | void thread_isolate() |
| 73 | { |
| 74 | unsigned long old; |
| 75 | |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 76 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 77 | __ha_barrier_atomic_store(); |
| 78 | _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 79 | |
| 80 | /* wait for all threads to become harmless */ |
| 81 | old = threads_harmless_mask; |
| 82 | while (1) { |
| 83 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 84 | old = threads_harmless_mask; |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 85 | else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 86 | break; |
| 87 | |
Willy Tarreau | 38171da | 2019-05-17 16:33:13 +0200 | [diff] [blame] | 88 | ha_thread_relax(); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame^] | 96 | /* Isolates the current thread : request the ability to work while all other |
| 97 | * threads are idle, as defined by thread_idle_now(). It only returns once |
| 98 | * all of them are both harmless and idle, with the current thread's bit in |
| 99 | * threads_harmless_mask and idle_mask cleared. Needs to be completed using |
| 100 | * thread_release(). By doing so the thread also engages in being safe against |
| 101 | * any actions that other threads might be about to start under the same |
| 102 | * conditions. This specifically targets destruction of any internal structure, |
| 103 | * which implies that the current thread may not hold references to any object. |
| 104 | * |
| 105 | * Note that a concurrent thread_isolate() will usually win against |
| 106 | * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to |
| 107 | * get back to the poller or any other fully idle location, that will |
| 108 | * ultimately release this one. |
| 109 | */ |
| 110 | void thread_isolate_full() |
| 111 | { |
| 112 | unsigned long old; |
| 113 | |
| 114 | _HA_ATOMIC_OR(&threads_idle_mask, tid_bit); |
| 115 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 116 | __ha_barrier_atomic_store(); |
| 117 | _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
| 118 | |
| 119 | /* wait for all threads to become harmless */ |
| 120 | old = threads_harmless_mask; |
| 121 | while (1) { |
| 122 | unsigned long idle = _HA_ATOMIC_LOAD(&threads_idle_mask); |
| 123 | |
| 124 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 125 | old = _HA_ATOMIC_LOAD(&threads_harmless_mask); |
| 126 | else if ((idle & all_threads_mask) == all_threads_mask && |
| 127 | _HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
| 128 | break; |
| 129 | |
| 130 | ha_thread_relax(); |
| 131 | } |
| 132 | |
| 133 | /* we're not idle anymore at this point. Other threads waiting on this |
| 134 | * condition will need to wait until out next pass to the poller, or |
| 135 | * our next call to thread_isolate_full(). |
| 136 | */ |
| 137 | _HA_ATOMIC_AND(&threads_idle_mask, ~tid_bit); |
| 138 | } |
| 139 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 140 | /* Cancels the effect of thread_isolate() by releasing the current thread's bit |
Willy Tarreau | f519cfa | 2021-08-04 11:22:07 +0200 | [diff] [blame] | 141 | * in threads_want_rdv_mask. This immediately allows other threads to expect be |
| 142 | * executed, though they will first have to wait for this thread to become |
| 143 | * harmless again (possibly by reaching the poller again). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 144 | */ |
| 145 | void thread_release() |
| 146 | { |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 147 | _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 148 | } |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 149 | |
Willy Tarreau | 9a1f573 | 2019-06-09 12:20:02 +0200 | [diff] [blame] | 150 | /* Cancels the effect of thread_isolate() by releasing the current thread's bit |
| 151 | * in threads_want_rdv_mask and by marking this thread as harmless until the |
| 152 | * last worker finishes. The difference with thread_release() is that this one |
| 153 | * will not leave the function before others are notified to do the same, so it |
| 154 | * guarantees that the current thread will not pass through a subsequent call |
| 155 | * to thread_isolate() before others finish. |
| 156 | */ |
| 157 | void thread_sync_release() |
| 158 | { |
| 159 | _HA_ATOMIC_OR(&threads_sync_mask, tid_bit); |
| 160 | __ha_barrier_atomic_store(); |
| 161 | _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
| 162 | |
| 163 | while (threads_want_rdv_mask & all_threads_mask) { |
| 164 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 165 | while (threads_want_rdv_mask & all_threads_mask) |
| 166 | ha_thread_relax(); |
| 167 | HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit); |
| 168 | } |
| 169 | |
| 170 | /* the current thread is not harmless anymore, thread_isolate() |
| 171 | * is forced to wait till all waiters finish. |
| 172 | */ |
| 173 | _HA_ATOMIC_AND(&threads_sync_mask, ~tid_bit); |
| 174 | while (threads_sync_mask & all_threads_mask) |
| 175 | ha_thread_relax(); |
| 176 | } |
| 177 | |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 178 | /* send signal <sig> to thread <thr> */ |
| 179 | void ha_tkill(unsigned int thr, int sig) |
| 180 | { |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 181 | pthread_kill(ha_thread_info[thr].pthread, sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* send signal <sig> to all threads. The calling thread is signaled last in |
| 185 | * order to allow all threads to synchronize in the handler. |
| 186 | */ |
| 187 | void ha_tkillall(int sig) |
| 188 | { |
| 189 | unsigned int thr; |
| 190 | |
| 191 | for (thr = 0; thr < global.nbthread; thr++) { |
| 192 | if (!(all_threads_mask & (1UL << thr))) |
| 193 | continue; |
| 194 | if (thr == tid) |
| 195 | continue; |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 196 | pthread_kill(ha_thread_info[thr].pthread, sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 197 | } |
| 198 | raise(sig); |
| 199 | } |
| 200 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 201 | /* these calls are used as callbacks at init time when debugging is on */ |
Willy Tarreau | a8ae77d | 2018-11-25 19:28:23 +0100 | [diff] [blame] | 202 | void ha_spin_init(HA_SPINLOCK_T *l) |
| 203 | { |
| 204 | HA_SPIN_INIT(l); |
| 205 | } |
| 206 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 207 | /* these calls are used as callbacks at init time when debugging is on */ |
Willy Tarreau | a8ae77d | 2018-11-25 19:28:23 +0100 | [diff] [blame] | 208 | void ha_rwlock_init(HA_RWLOCK_T *l) |
| 209 | { |
| 210 | HA_RWLOCK_INIT(l); |
| 211 | } |
| 212 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 213 | /* returns the number of CPUs the current process is enabled to run on */ |
| 214 | static int thread_cpus_enabled() |
| 215 | { |
| 216 | int ret = 1; |
| 217 | |
| 218 | #ifdef USE_CPU_AFFINITY |
| 219 | #if defined(__linux__) && defined(CPU_COUNT) |
| 220 | cpu_set_t mask; |
| 221 | |
| 222 | if (sched_getaffinity(0, sizeof(mask), &mask) == 0) |
| 223 | ret = CPU_COUNT(&mask); |
Olivier Houchard | 46453d3 | 2019-04-11 00:06:47 +0200 | [diff] [blame] | 224 | #elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY) |
| 225 | cpuset_t cpuset; |
| 226 | if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, |
| 227 | sizeof(cpuset), &cpuset) == 0) |
| 228 | ret = CPU_COUNT(&cpuset); |
David CARLIER | 6a90601 | 2021-01-15 08:09:56 +0000 | [diff] [blame] | 229 | #elif defined(__APPLE__) |
| 230 | ret = (int)sysconf(_SC_NPROCESSORS_ONLN); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 231 | #endif |
| 232 | #endif |
| 233 | ret = MAX(ret, 1); |
| 234 | ret = MIN(ret, MAX_THREADS); |
| 235 | return ret; |
| 236 | } |
| 237 | |
Amaury Denoyelle | 4c9efde | 2021-03-31 16:57:39 +0200 | [diff] [blame] | 238 | /* Returns 1 if the cpu set is currently restricted for the process else 0. |
| 239 | * Currently only implemented for the Linux platform. |
| 240 | */ |
| 241 | int thread_cpu_mask_forced() |
| 242 | { |
| 243 | #if defined(__linux__) |
| 244 | const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN); |
| 245 | return cpus_avail != thread_cpus_enabled(); |
| 246 | #else |
| 247 | return 0; |
| 248 | #endif |
| 249 | } |
| 250 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 251 | /* Depending on the platform and how libpthread was built, pthread_exit() may |
| 252 | * involve some code in libgcc_s that would be loaded on exit for the first |
| 253 | * time, causing aborts if the process is chrooted. It's harmless bit very |
| 254 | * dirty. There isn't much we can do to make sure libgcc_s is loaded only if |
| 255 | * needed, so what we do here is that during early boot we create a dummy |
| 256 | * thread that immediately exits. This will lead to libgcc_s being loaded |
| 257 | * during boot on the platforms where it's required. |
| 258 | */ |
| 259 | static void *dummy_thread_function(void *data) |
| 260 | { |
| 261 | pthread_exit(NULL); |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | static inline void preload_libgcc_s(void) |
| 266 | { |
| 267 | pthread_t dummy_thread; |
| 268 | pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL); |
| 269 | pthread_join(dummy_thread, NULL); |
| 270 | } |
| 271 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 272 | __attribute__((constructor)) |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 273 | static void __thread_init(void) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 274 | { |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 275 | char *ptr = NULL; |
| 276 | |
| 277 | if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) { |
| 278 | ha_alert("MAX_THREADS value must be between 1 and %d inclusive; " |
| 279 | "HAProxy was built with value %d, please fix it and rebuild.\n", |
| 280 | LONGBITS, MAX_THREADS); |
| 281 | exit(1); |
| 282 | } |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 283 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 284 | preload_libgcc_s(); |
Willy Tarreau | 77b9822 | 2020-09-02 08:04:35 +0200 | [diff] [blame] | 285 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 286 | thread_cpus_enabled_at_boot = thread_cpus_enabled(); |
| 287 | |
| 288 | memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).", |
| 289 | MAX_THREADS, thread_cpus_enabled_at_boot); |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 290 | hap_register_build_opts(ptr, 1); |
| 291 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 292 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 293 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 294 | #endif |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 297 | #else |
| 298 | |
| 299 | REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set)."); |
| 300 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 301 | #endif // USE_THREAD |
| 302 | |
| 303 | |
| 304 | /* Parse the number of threads in argument <arg>, returns it and adjusts a few |
| 305 | * internal variables accordingly, or fails and returns zero with an error |
| 306 | * reason in <errmsg>. May be called multiple times while parsing. |
| 307 | */ |
| 308 | int parse_nbthread(const char *arg, char **err) |
| 309 | { |
| 310 | long nbthread; |
| 311 | char *errptr; |
| 312 | |
| 313 | nbthread = strtol(arg, &errptr, 10); |
| 314 | if (!*arg || *errptr) { |
| 315 | memprintf(err, "passed a missing or unparsable integer value in '%s'", arg); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | #ifndef USE_THREAD |
| 320 | if (nbthread != 1) { |
| 321 | memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD"); |
| 322 | return 0; |
| 323 | } |
| 324 | #else |
| 325 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
| 326 | memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread); |
| 327 | return 0; |
| 328 | } |
| 329 | |
Willy Tarreau | fc64736 | 2019-02-02 17:05:03 +0100 | [diff] [blame] | 330 | all_threads_mask = nbits(nbthread); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 331 | #endif |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 332 | return nbthread; |
| 333 | } |