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 | 0493149 | 2017-11-03 23:39:25 +0100 | [diff] [blame] | 26 | #include <common/cfgparse.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 27 | #include <common/hathreads.h> |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 28 | #include <common/standard.h> |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 29 | #include <types/global.h> |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 30 | #include <proto/fd.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | 5a6e224 | 2019-05-20 18:56:48 +0200 | [diff] [blame^] | 32 | struct thread_info thread_info[MAX_THREADS]; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 33 | |
| 34 | #ifdef USE_THREAD |
| 35 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 36 | volatile unsigned long threads_want_rdv_mask = 0; |
| 37 | volatile unsigned long threads_harmless_mask = 0; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 38 | volatile unsigned long all_threads_mask = 1; // nbthread 1 assumed by default |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 39 | THREAD_LOCAL unsigned int tid = 0; |
| 40 | THREAD_LOCAL unsigned long tid_bit = (1UL << 0); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 41 | int thread_cpus_enabled_at_boot = 1; |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 42 | |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 43 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 44 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 45 | struct lock_stat lock_stats[LOCK_LABELS]; |
| 46 | #endif |
| 47 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 48 | /* Marks the thread as harmless until the last thread using the rendez-vous |
| 49 | * point quits. Given that we can wait for a long time, sched_yield() is used |
| 50 | * when available to offer the CPU resources to competing threads if needed. |
| 51 | */ |
| 52 | void thread_harmless_till_end() |
| 53 | { |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 54 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 55 | while (threads_want_rdv_mask & all_threads_mask) { |
Willy Tarreau | 38171da | 2019-05-17 16:33:13 +0200 | [diff] [blame] | 56 | ha_thread_relax(); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | /* Isolates the current thread : request the ability to work while all other |
| 61 | * threads are harmless. Only returns once all of them are harmless, with the |
| 62 | * current thread's bit in threads_harmless_mask cleared. Needs to be completed |
| 63 | * using thread_release(). |
| 64 | */ |
| 65 | void thread_isolate() |
| 66 | { |
| 67 | unsigned long old; |
| 68 | |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 69 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 70 | __ha_barrier_atomic_store(); |
| 71 | _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 72 | |
| 73 | /* wait for all threads to become harmless */ |
| 74 | old = threads_harmless_mask; |
| 75 | while (1) { |
| 76 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 77 | old = threads_harmless_mask; |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 78 | else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 79 | break; |
| 80 | |
Willy Tarreau | 38171da | 2019-05-17 16:33:13 +0200 | [diff] [blame] | 81 | ha_thread_relax(); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 82 | } |
| 83 | /* one thread gets released at a time here, with its harmess bit off. |
| 84 | * The loss of this bit makes the other one continue to spin while the |
| 85 | * thread is working alone. |
| 86 | */ |
| 87 | } |
| 88 | |
| 89 | /* Cancels the effect of thread_isolate() by releasing the current thread's bit |
| 90 | * in threads_want_rdv_mask and by marking this thread as harmless until the |
| 91 | * last worker finishes. |
| 92 | */ |
| 93 | void thread_release() |
| 94 | { |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 95 | _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
Willy Tarreau | a9c0252 | 2018-10-16 16:11:56 +0200 | [diff] [blame] | 96 | thread_harmless_end(); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 97 | } |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 98 | |
Willy Tarreau | a8ae77d | 2018-11-25 19:28:23 +0100 | [diff] [blame] | 99 | /* these calls are used as callbacks at init time */ |
| 100 | void ha_spin_init(HA_SPINLOCK_T *l) |
| 101 | { |
| 102 | HA_SPIN_INIT(l); |
| 103 | } |
| 104 | |
| 105 | /* these calls are used as callbacks at init time */ |
| 106 | void ha_rwlock_init(HA_RWLOCK_T *l) |
| 107 | { |
| 108 | HA_RWLOCK_INIT(l); |
| 109 | } |
| 110 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 111 | /* returns the number of CPUs the current process is enabled to run on */ |
| 112 | static int thread_cpus_enabled() |
| 113 | { |
| 114 | int ret = 1; |
| 115 | |
| 116 | #ifdef USE_CPU_AFFINITY |
| 117 | #if defined(__linux__) && defined(CPU_COUNT) |
| 118 | cpu_set_t mask; |
| 119 | |
| 120 | if (sched_getaffinity(0, sizeof(mask), &mask) == 0) |
| 121 | ret = CPU_COUNT(&mask); |
Olivier Houchard | 46453d3 | 2019-04-11 00:06:47 +0200 | [diff] [blame] | 122 | #elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY) |
| 123 | cpuset_t cpuset; |
| 124 | if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, |
| 125 | sizeof(cpuset), &cpuset) == 0) |
| 126 | ret = CPU_COUNT(&cpuset); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 127 | #endif |
| 128 | #endif |
| 129 | ret = MAX(ret, 1); |
| 130 | ret = MIN(ret, MAX_THREADS); |
| 131 | return ret; |
| 132 | } |
| 133 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 134 | __attribute__((constructor)) |
| 135 | static void __hathreads_init(void) |
| 136 | { |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 137 | char *ptr = NULL; |
| 138 | |
| 139 | if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) { |
| 140 | ha_alert("MAX_THREADS value must be between 1 and %d inclusive; " |
| 141 | "HAProxy was built with value %d, please fix it and rebuild.\n", |
| 142 | LONGBITS, MAX_THREADS); |
| 143 | exit(1); |
| 144 | } |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 145 | |
| 146 | thread_cpus_enabled_at_boot = thread_cpus_enabled(); |
| 147 | |
| 148 | memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).", |
| 149 | MAX_THREADS, thread_cpus_enabled_at_boot); |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 150 | hap_register_build_opts(ptr, 1); |
| 151 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 152 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 153 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 154 | #endif |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 157 | #else |
| 158 | |
| 159 | REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set)."); |
| 160 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 161 | #endif // USE_THREAD |
| 162 | |
| 163 | |
| 164 | /* Parse the number of threads in argument <arg>, returns it and adjusts a few |
| 165 | * internal variables accordingly, or fails and returns zero with an error |
| 166 | * reason in <errmsg>. May be called multiple times while parsing. |
| 167 | */ |
| 168 | int parse_nbthread(const char *arg, char **err) |
| 169 | { |
| 170 | long nbthread; |
| 171 | char *errptr; |
| 172 | |
| 173 | nbthread = strtol(arg, &errptr, 10); |
| 174 | if (!*arg || *errptr) { |
| 175 | memprintf(err, "passed a missing or unparsable integer value in '%s'", arg); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | #ifndef USE_THREAD |
| 180 | if (nbthread != 1) { |
| 181 | memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD"); |
| 182 | return 0; |
| 183 | } |
| 184 | #else |
| 185 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
| 186 | memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread); |
| 187 | return 0; |
| 188 | } |
| 189 | |
Willy Tarreau | fc64736 | 2019-02-02 17:05:03 +0100 | [diff] [blame] | 190 | all_threads_mask = nbits(nbthread); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 191 | #endif |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 192 | return nbthread; |
| 193 | } |