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 | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 18 | #ifdef USE_THREAD |
| 19 | # include <pthread.h> |
| 20 | #endif |
| 21 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 22 | #ifdef USE_CPU_AFFINITY |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 23 | # include <sched.h> |
| 24 | # if defined(__FreeBSD__) || defined(__DragonFly__) |
| 25 | # include <sys/param.h> |
| 26 | # ifdef __FreeBSD__ |
| 27 | # include <sys/cpuset.h> |
| 28 | # endif |
| 29 | # include <pthread_np.h> |
| 30 | # endif |
| 31 | # ifdef __APPLE__ |
| 32 | # include <mach/mach_types.h> |
| 33 | # include <mach/thread_act.h> |
| 34 | # include <mach/thread_policy.h> |
| 35 | # endif |
| 36 | # include <haproxy/cpuset.h> |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 37 | #endif |
| 38 | |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 39 | #include <haproxy/cfgparse.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 40 | #include <haproxy/fd.h> |
| 41 | #include <haproxy/global.h> |
Willy Tarreau | 11bd6f7 | 2021-05-08 20:33:02 +0200 | [diff] [blame] | 42 | #include <haproxy/log.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 43 | #include <haproxy/thread.h> |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 44 | #include <haproxy/time.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 45 | #include <haproxy/tools.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 46 | |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 47 | struct thread_info ha_thread_info[MAX_THREADS] = { }; |
| 48 | THREAD_LOCAL struct thread_info *ti = &ha_thread_info[0]; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 49 | |
| 50 | #ifdef USE_THREAD |
| 51 | |
Willy Tarreau | 56c3b8b | 2021-04-10 17:28:18 +0200 | [diff] [blame] | 52 | volatile unsigned long threads_want_rdv_mask __read_mostly = 0; |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 53 | volatile unsigned long threads_harmless_mask = 0; |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 54 | volatile unsigned long threads_idle_mask = 0; |
Willy Tarreau | 9a1f573 | 2019-06-09 12:20:02 +0200 | [diff] [blame] | 55 | volatile unsigned long threads_sync_mask = 0; |
Willy Tarreau | 56c3b8b | 2021-04-10 17:28:18 +0200 | [diff] [blame] | 56 | 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] | 57 | THREAD_LOCAL unsigned int tid = 0; |
| 58 | THREAD_LOCAL unsigned long tid_bit = (1UL << 0); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 59 | int thread_cpus_enabled_at_boot = 1; |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 60 | static pthread_t ha_pthread[MAX_THREADS] = { }; |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 61 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 62 | /* 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] | 63 | * point quits, excluding the current one. Thus an isolated thread may be safely |
| 64 | * marked as harmless. Given that we can wait for a long time, sched_yield() is |
| 65 | * used when available to offer the CPU resources to competing threads if |
| 66 | * needed. |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 67 | */ |
| 68 | void thread_harmless_till_end() |
| 69 | { |
Willy Tarreau | 286363b | 2021-08-04 10:33:57 +0200 | [diff] [blame] | 70 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 71 | while (threads_want_rdv_mask & all_threads_mask & ~tid_bit) { |
| 72 | ha_thread_relax(); |
| 73 | } |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* Isolates the current thread : request the ability to work while all other |
Willy Tarreau | f519cfa | 2021-08-04 11:22:07 +0200 | [diff] [blame] | 77 | * threads are harmless, as defined by thread_harmless_now() (i.e. they're not |
| 78 | * going to touch any visible memory area). Only returns once all of them are |
| 79 | * harmless, with the current thread's bit in threads_harmless_mask cleared. |
| 80 | * Needs to be completed using thread_release(). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 81 | */ |
| 82 | void thread_isolate() |
| 83 | { |
| 84 | unsigned long old; |
| 85 | |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 86 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 87 | __ha_barrier_atomic_store(); |
| 88 | _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 89 | |
| 90 | /* wait for all threads to become harmless */ |
| 91 | old = threads_harmless_mask; |
| 92 | while (1) { |
| 93 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 94 | old = threads_harmless_mask; |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 95 | else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 96 | break; |
| 97 | |
Willy Tarreau | 38171da | 2019-05-17 16:33:13 +0200 | [diff] [blame] | 98 | ha_thread_relax(); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 99 | } |
| 100 | /* one thread gets released at a time here, with its harmess bit off. |
| 101 | * The loss of this bit makes the other one continue to spin while the |
| 102 | * thread is working alone. |
| 103 | */ |
| 104 | } |
| 105 | |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 106 | /* Isolates the current thread : request the ability to work while all other |
| 107 | * threads are idle, as defined by thread_idle_now(). It only returns once |
| 108 | * all of them are both harmless and idle, with the current thread's bit in |
| 109 | * threads_harmless_mask and idle_mask cleared. Needs to be completed using |
| 110 | * thread_release(). By doing so the thread also engages in being safe against |
| 111 | * any actions that other threads might be about to start under the same |
| 112 | * conditions. This specifically targets destruction of any internal structure, |
| 113 | * which implies that the current thread may not hold references to any object. |
| 114 | * |
| 115 | * Note that a concurrent thread_isolate() will usually win against |
| 116 | * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to |
| 117 | * get back to the poller or any other fully idle location, that will |
| 118 | * ultimately release this one. |
| 119 | */ |
| 120 | void thread_isolate_full() |
| 121 | { |
| 122 | unsigned long old; |
| 123 | |
| 124 | _HA_ATOMIC_OR(&threads_idle_mask, tid_bit); |
| 125 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 126 | __ha_barrier_atomic_store(); |
| 127 | _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
| 128 | |
| 129 | /* wait for all threads to become harmless */ |
| 130 | old = threads_harmless_mask; |
| 131 | while (1) { |
| 132 | unsigned long idle = _HA_ATOMIC_LOAD(&threads_idle_mask); |
| 133 | |
| 134 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 135 | old = _HA_ATOMIC_LOAD(&threads_harmless_mask); |
| 136 | else if ((idle & all_threads_mask) == all_threads_mask && |
| 137 | _HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
| 138 | break; |
| 139 | |
| 140 | ha_thread_relax(); |
| 141 | } |
| 142 | |
| 143 | /* we're not idle anymore at this point. Other threads waiting on this |
| 144 | * condition will need to wait until out next pass to the poller, or |
| 145 | * our next call to thread_isolate_full(). |
| 146 | */ |
| 147 | _HA_ATOMIC_AND(&threads_idle_mask, ~tid_bit); |
| 148 | } |
| 149 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 150 | /* 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] | 151 | * in threads_want_rdv_mask. This immediately allows other threads to expect be |
| 152 | * executed, though they will first have to wait for this thread to become |
| 153 | * harmless again (possibly by reaching the poller again). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 154 | */ |
| 155 | void thread_release() |
| 156 | { |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 157 | _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 158 | } |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 159 | |
Willy Tarreau | 9a1f573 | 2019-06-09 12:20:02 +0200 | [diff] [blame] | 160 | /* Cancels the effect of thread_isolate() by releasing the current thread's bit |
| 161 | * in threads_want_rdv_mask and by marking this thread as harmless until the |
| 162 | * last worker finishes. The difference with thread_release() is that this one |
| 163 | * will not leave the function before others are notified to do the same, so it |
| 164 | * guarantees that the current thread will not pass through a subsequent call |
| 165 | * to thread_isolate() before others finish. |
| 166 | */ |
| 167 | void thread_sync_release() |
| 168 | { |
| 169 | _HA_ATOMIC_OR(&threads_sync_mask, tid_bit); |
| 170 | __ha_barrier_atomic_store(); |
| 171 | _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
| 172 | |
| 173 | while (threads_want_rdv_mask & all_threads_mask) { |
| 174 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 175 | while (threads_want_rdv_mask & all_threads_mask) |
| 176 | ha_thread_relax(); |
| 177 | HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit); |
| 178 | } |
| 179 | |
| 180 | /* the current thread is not harmless anymore, thread_isolate() |
| 181 | * is forced to wait till all waiters finish. |
| 182 | */ |
| 183 | _HA_ATOMIC_AND(&threads_sync_mask, ~tid_bit); |
| 184 | while (threads_sync_mask & all_threads_mask) |
| 185 | ha_thread_relax(); |
| 186 | } |
| 187 | |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 188 | /* Sets up threads, signals and masks, and starts threads 2 and above. |
| 189 | * Does nothing when threads are disabled. |
| 190 | */ |
| 191 | void setup_extra_threads(void *(*handler)(void *)) |
| 192 | { |
| 193 | sigset_t blocked_sig, old_sig; |
| 194 | int i; |
| 195 | |
| 196 | /* ensure the signals will be blocked in every thread */ |
| 197 | sigfillset(&blocked_sig); |
| 198 | sigdelset(&blocked_sig, SIGPROF); |
| 199 | sigdelset(&blocked_sig, SIGBUS); |
| 200 | sigdelset(&blocked_sig, SIGFPE); |
| 201 | sigdelset(&blocked_sig, SIGILL); |
| 202 | sigdelset(&blocked_sig, SIGSEGV); |
| 203 | pthread_sigmask(SIG_SETMASK, &blocked_sig, &old_sig); |
| 204 | |
| 205 | /* Create nbthread-1 thread. The first thread is the current process */ |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 206 | ha_pthread[0] = pthread_self(); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 207 | for (i = 1; i < global.nbthread; i++) |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 208 | pthread_create(&ha_pthread[i], NULL, handler, (void *)(long)i); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* waits for all threads to terminate. Does nothing when threads are |
| 212 | * disabled. |
| 213 | */ |
| 214 | void wait_for_threads_completion() |
| 215 | { |
| 216 | int i; |
| 217 | |
| 218 | /* Wait the end of other threads */ |
| 219 | for (i = 1; i < global.nbthread; i++) |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 220 | pthread_join(ha_pthread[i], NULL); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 221 | |
| 222 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 223 | show_lock_stats(); |
| 224 | #endif |
| 225 | } |
| 226 | |
| 227 | /* Tries to set the current thread's CPU affinity according to the cpu_map */ |
| 228 | void set_thread_cpu_affinity() |
| 229 | { |
| 230 | #if defined(USE_CPU_AFFINITY) |
| 231 | /* no affinity setting for the master process */ |
| 232 | if (master) |
| 233 | return; |
| 234 | |
| 235 | /* Now the CPU affinity for all threads */ |
| 236 | if (ha_cpuset_count(&cpu_map.proc)) |
| 237 | ha_cpuset_and(&cpu_map.thread[tid], &cpu_map.proc); |
| 238 | |
| 239 | if (ha_cpuset_count(&cpu_map.thread[tid])) {/* only do this if the thread has a THREAD map */ |
| 240 | # if defined(__APPLE__) |
| 241 | /* Note: this API is limited to the first 32/64 CPUs */ |
| 242 | unsigned long set = cpu_map.thread[tid].cpuset; |
| 243 | int j; |
| 244 | |
| 245 | while ((j = ffsl(set)) > 0) { |
| 246 | thread_affinity_policy_data_t cpu_set = { j - 1 }; |
| 247 | thread_port_t mthread; |
| 248 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 249 | mthread = pthread_mach_thread_np(ha_pthread[tid]); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 250 | thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1); |
| 251 | set &= ~(1UL << (j - 1)); |
| 252 | } |
| 253 | # else |
| 254 | struct hap_cpuset *set = &cpu_map.thread[tid]; |
| 255 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 256 | pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset); |
Willy Tarreau | d10385a | 2021-10-06 22:22:40 +0200 | [diff] [blame] | 257 | # endif |
| 258 | } |
| 259 | #endif /* USE_CPU_AFFINITY */ |
| 260 | } |
| 261 | |
Willy Tarreau | 4eeb883 | 2021-10-06 22:44:28 +0200 | [diff] [blame] | 262 | /* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long |
| 263 | * since POSIX took great care of not specifying its representation, making it |
| 264 | * hard to export for post-mortem analysis. For this reason we copy it into a |
| 265 | * union and will use the smallest scalar type at least as large as its size, |
| 266 | * which will keep endianness and alignment for all regular sizes. As a last |
| 267 | * resort we end up with a long long ligned to the first bytes in memory, which |
| 268 | * will be endian-dependent if pthread_t is larger than a long long (not seen |
| 269 | * yet). |
| 270 | */ |
| 271 | unsigned long long ha_get_pthread_id(unsigned int thr) |
| 272 | { |
| 273 | union { |
| 274 | pthread_t t; |
| 275 | unsigned long long ll; |
| 276 | unsigned int i; |
| 277 | unsigned short s; |
| 278 | unsigned char c; |
| 279 | } u = { 0 }; |
| 280 | |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 281 | u.t = ha_pthread[thr]; |
Willy Tarreau | 4eeb883 | 2021-10-06 22:44:28 +0200 | [diff] [blame] | 282 | |
| 283 | if (sizeof(u.t) <= sizeof(u.c)) |
| 284 | return u.c; |
| 285 | else if (sizeof(u.t) <= sizeof(u.s)) |
| 286 | return u.s; |
| 287 | else if (sizeof(u.t) <= sizeof(u.i)) |
| 288 | return u.i; |
| 289 | return u.ll; |
| 290 | } |
| 291 | |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 292 | /* send signal <sig> to thread <thr> */ |
| 293 | void ha_tkill(unsigned int thr, int sig) |
| 294 | { |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 295 | pthread_kill(ha_pthread[thr], sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* send signal <sig> to all threads. The calling thread is signaled last in |
| 299 | * order to allow all threads to synchronize in the handler. |
| 300 | */ |
| 301 | void ha_tkillall(int sig) |
| 302 | { |
| 303 | unsigned int thr; |
| 304 | |
| 305 | for (thr = 0; thr < global.nbthread; thr++) { |
| 306 | if (!(all_threads_mask & (1UL << thr))) |
| 307 | continue; |
| 308 | if (thr == tid) |
| 309 | continue; |
Willy Tarreau | 5e03dfa | 2021-10-06 22:53:51 +0200 | [diff] [blame^] | 310 | pthread_kill(ha_pthread[thr], sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 311 | } |
| 312 | raise(sig); |
| 313 | } |
| 314 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 315 | /* 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] | 316 | void ha_spin_init(HA_SPINLOCK_T *l) |
| 317 | { |
| 318 | HA_SPIN_INIT(l); |
| 319 | } |
| 320 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 321 | /* 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] | 322 | void ha_rwlock_init(HA_RWLOCK_T *l) |
| 323 | { |
| 324 | HA_RWLOCK_INIT(l); |
| 325 | } |
| 326 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 327 | /* returns the number of CPUs the current process is enabled to run on */ |
| 328 | static int thread_cpus_enabled() |
| 329 | { |
| 330 | int ret = 1; |
| 331 | |
| 332 | #ifdef USE_CPU_AFFINITY |
| 333 | #if defined(__linux__) && defined(CPU_COUNT) |
| 334 | cpu_set_t mask; |
| 335 | |
| 336 | if (sched_getaffinity(0, sizeof(mask), &mask) == 0) |
| 337 | ret = CPU_COUNT(&mask); |
Olivier Houchard | 46453d3 | 2019-04-11 00:06:47 +0200 | [diff] [blame] | 338 | #elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY) |
| 339 | cpuset_t cpuset; |
| 340 | if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, |
| 341 | sizeof(cpuset), &cpuset) == 0) |
| 342 | ret = CPU_COUNT(&cpuset); |
David CARLIER | 6a90601 | 2021-01-15 08:09:56 +0000 | [diff] [blame] | 343 | #elif defined(__APPLE__) |
| 344 | ret = (int)sysconf(_SC_NPROCESSORS_ONLN); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 345 | #endif |
| 346 | #endif |
| 347 | ret = MAX(ret, 1); |
| 348 | ret = MIN(ret, MAX_THREADS); |
| 349 | return ret; |
| 350 | } |
| 351 | |
Amaury Denoyelle | 4c9efde | 2021-03-31 16:57:39 +0200 | [diff] [blame] | 352 | /* Returns 1 if the cpu set is currently restricted for the process else 0. |
| 353 | * Currently only implemented for the Linux platform. |
| 354 | */ |
| 355 | int thread_cpu_mask_forced() |
| 356 | { |
| 357 | #if defined(__linux__) |
| 358 | const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN); |
| 359 | return cpus_avail != thread_cpus_enabled(); |
| 360 | #else |
| 361 | return 0; |
| 362 | #endif |
| 363 | } |
| 364 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 365 | /* Below come the lock-debugging functions */ |
| 366 | |
| 367 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 368 | |
| 369 | struct lock_stat lock_stats[LOCK_LABELS]; |
| 370 | |
| 371 | /* this is only used below */ |
| 372 | static const char *lock_label(enum lock_label label) |
| 373 | { |
| 374 | switch (label) { |
| 375 | case TASK_RQ_LOCK: return "TASK_RQ"; |
| 376 | case TASK_WQ_LOCK: return "TASK_WQ"; |
| 377 | case LISTENER_LOCK: return "LISTENER"; |
| 378 | case PROXY_LOCK: return "PROXY"; |
| 379 | case SERVER_LOCK: return "SERVER"; |
| 380 | case LBPRM_LOCK: return "LBPRM"; |
| 381 | case SIGNALS_LOCK: return "SIGNALS"; |
| 382 | case STK_TABLE_LOCK: return "STK_TABLE"; |
| 383 | case STK_SESS_LOCK: return "STK_SESS"; |
| 384 | case APPLETS_LOCK: return "APPLETS"; |
| 385 | case PEER_LOCK: return "PEER"; |
| 386 | case SHCTX_LOCK: return "SHCTX"; |
| 387 | case SSL_LOCK: return "SSL"; |
| 388 | case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS"; |
| 389 | case PATREF_LOCK: return "PATREF"; |
| 390 | case PATEXP_LOCK: return "PATEXP"; |
| 391 | case VARS_LOCK: return "VARS"; |
| 392 | case COMP_POOL_LOCK: return "COMP_POOL"; |
| 393 | case LUA_LOCK: return "LUA"; |
| 394 | case NOTIF_LOCK: return "NOTIF"; |
| 395 | case SPOE_APPLET_LOCK: return "SPOE_APPLET"; |
| 396 | case DNS_LOCK: return "DNS"; |
| 397 | case PID_LIST_LOCK: return "PID_LIST"; |
| 398 | case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS"; |
| 399 | case PIPES_LOCK: return "PIPES"; |
| 400 | case TLSKEYS_REF_LOCK: return "TLSKEYS_REF"; |
| 401 | case AUTH_LOCK: return "AUTH"; |
| 402 | case LOGSRV_LOCK: return "LOGSRV"; |
| 403 | case DICT_LOCK: return "DICT"; |
| 404 | case PROTO_LOCK: return "PROTO"; |
| 405 | case QUEUE_LOCK: return "QUEUE"; |
| 406 | case CKCH_LOCK: return "CKCH"; |
| 407 | case SNI_LOCK: return "SNI"; |
| 408 | case SSL_SERVER_LOCK: return "SSL_SERVER"; |
| 409 | case SFT_LOCK: return "SFT"; |
| 410 | case IDLE_CONNS_LOCK: return "IDLE_CONNS"; |
| 411 | case QUIC_LOCK: return "QUIC"; |
| 412 | case OTHER_LOCK: return "OTHER"; |
| 413 | case DEBUG1_LOCK: return "DEBUG1"; |
| 414 | case DEBUG2_LOCK: return "DEBUG2"; |
| 415 | case DEBUG3_LOCK: return "DEBUG3"; |
| 416 | case DEBUG4_LOCK: return "DEBUG4"; |
| 417 | case DEBUG5_LOCK: return "DEBUG5"; |
| 418 | case LOCK_LABELS: break; /* keep compiler happy */ |
| 419 | }; |
| 420 | /* only way to come here is consecutive to an internal bug */ |
| 421 | abort(); |
| 422 | } |
| 423 | |
| 424 | void show_lock_stats() |
| 425 | { |
| 426 | int lbl; |
| 427 | |
| 428 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 429 | if (!lock_stats[lbl].num_write_locked && |
| 430 | !lock_stats[lbl].num_seek_locked && |
| 431 | !lock_stats[lbl].num_read_locked) { |
| 432 | fprintf(stderr, |
| 433 | "Stats about Lock %s: not used\n", |
| 434 | lock_label(lbl)); |
| 435 | continue; |
| 436 | } |
| 437 | |
| 438 | fprintf(stderr, |
| 439 | "Stats about Lock %s: \n", |
| 440 | lock_label(lbl)); |
| 441 | |
| 442 | if (lock_stats[lbl].num_write_locked) |
| 443 | fprintf(stderr, |
| 444 | "\t # write lock : %lu\n" |
| 445 | "\t # write unlock: %lu (%ld)\n" |
| 446 | "\t # wait time for write : %.3f msec\n" |
| 447 | "\t # wait time for write/lock: %.3f nsec\n", |
| 448 | lock_stats[lbl].num_write_locked, |
| 449 | lock_stats[lbl].num_write_unlocked, |
| 450 | lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked, |
| 451 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 452 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0); |
| 453 | |
| 454 | if (lock_stats[lbl].num_seek_locked) |
| 455 | fprintf(stderr, |
| 456 | "\t # seek lock : %lu\n" |
| 457 | "\t # seek unlock : %lu (%ld)\n" |
| 458 | "\t # wait time for seek : %.3f msec\n" |
| 459 | "\t # wait time for seek/lock : %.3f nsec\n", |
| 460 | lock_stats[lbl].num_seek_locked, |
| 461 | lock_stats[lbl].num_seek_unlocked, |
| 462 | lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked, |
| 463 | (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0, |
| 464 | lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0); |
| 465 | |
| 466 | if (lock_stats[lbl].num_read_locked) |
| 467 | fprintf(stderr, |
| 468 | "\t # read lock : %lu\n" |
| 469 | "\t # read unlock : %lu (%ld)\n" |
| 470 | "\t # wait time for read : %.3f msec\n" |
| 471 | "\t # wait time for read/lock : %.3f nsec\n", |
| 472 | lock_stats[lbl].num_read_locked, |
| 473 | lock_stats[lbl].num_read_unlocked, |
| 474 | lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked, |
| 475 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 476 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 477 | } |
| 478 | } |
| 479 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 480 | void __ha_rwlock_init(struct ha_rwlock *l) |
| 481 | { |
| 482 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 483 | __RWLOCK_INIT(&l->lock); |
| 484 | } |
| 485 | |
| 486 | void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 487 | { |
| 488 | __RWLOCK_DESTROY(&l->lock); |
| 489 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 494 | const char *func, const char *file, int line) |
| 495 | { |
| 496 | uint64_t start_time; |
| 497 | |
| 498 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 499 | abort(); |
| 500 | |
| 501 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 502 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 503 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 504 | __RWLOCK_WRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 505 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 506 | |
| 507 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 508 | |
| 509 | l->info.cur_writer = tid_bit; |
| 510 | l->info.last_location.function = func; |
| 511 | l->info.last_location.file = file; |
| 512 | l->info.last_location.line = line; |
| 513 | |
| 514 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 515 | } |
| 516 | |
| 517 | int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 518 | const char *func, const char *file, int line) |
| 519 | { |
| 520 | uint64_t start_time; |
| 521 | int r; |
| 522 | |
| 523 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 524 | abort(); |
| 525 | |
| 526 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
| 527 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 528 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 529 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 530 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 531 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 532 | if (unlikely(r)) { |
| 533 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 534 | return r; |
| 535 | } |
| 536 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 537 | |
| 538 | l->info.cur_writer = tid_bit; |
| 539 | l->info.last_location.function = func; |
| 540 | l->info.last_location.file = file; |
| 541 | l->info.last_location.line = line; |
| 542 | |
| 543 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 549 | const char *func, const char *file, int line) |
| 550 | { |
| 551 | if (unlikely(!(l->info.cur_writer & tid_bit))) { |
| 552 | /* the thread is not owning the lock for write */ |
| 553 | abort(); |
| 554 | } |
| 555 | |
| 556 | l->info.cur_writer = 0; |
| 557 | l->info.last_location.function = func; |
| 558 | l->info.last_location.file = file; |
| 559 | l->info.last_location.line = line; |
| 560 | |
| 561 | __RWLOCK_WRUNLOCK(&l->lock); |
| 562 | |
| 563 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 564 | } |
| 565 | |
| 566 | void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 567 | { |
| 568 | uint64_t start_time; |
| 569 | |
| 570 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 571 | abort(); |
| 572 | |
| 573 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 574 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 575 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 576 | __RWLOCK_RDLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 577 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 578 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 579 | |
| 580 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 581 | |
| 582 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 583 | } |
| 584 | |
| 585 | int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 586 | { |
| 587 | int r; |
| 588 | |
| 589 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 590 | abort(); |
| 591 | |
| 592 | /* try read should never wait */ |
| 593 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 594 | if (unlikely(r)) |
| 595 | return r; |
| 596 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 597 | |
| 598 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 604 | { |
| 605 | if (unlikely(!(l->info.cur_readers & tid_bit))) { |
| 606 | /* the thread is not owning the lock for read */ |
| 607 | abort(); |
| 608 | } |
| 609 | |
| 610 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 611 | |
| 612 | __RWLOCK_RDUNLOCK(&l->lock); |
| 613 | |
| 614 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked); |
| 615 | } |
| 616 | |
| 617 | void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l, |
| 618 | const char *func, const char *file, int line) |
| 619 | { |
| 620 | uint64_t start_time; |
| 621 | |
| 622 | if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit) |
| 623 | abort(); |
| 624 | |
| 625 | if (!(l->info.cur_writer & tid_bit)) |
| 626 | abort(); |
| 627 | |
| 628 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 629 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 630 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 631 | __RWLOCK_WRTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 632 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 633 | |
| 634 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 635 | |
| 636 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 637 | HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit); |
| 638 | l->info.last_location.function = func; |
| 639 | l->info.last_location.file = file; |
| 640 | l->info.last_location.line = line; |
| 641 | |
| 642 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 643 | } |
| 644 | |
| 645 | void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 646 | const char *func, const char *file, int line) |
| 647 | { |
| 648 | uint64_t start_time; |
| 649 | |
| 650 | if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit) |
| 651 | abort(); |
| 652 | |
| 653 | if (!(l->info.cur_writer & tid_bit)) |
| 654 | abort(); |
| 655 | |
| 656 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 657 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 658 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 659 | __RWLOCK_WRTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 660 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 661 | |
| 662 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 663 | |
| 664 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 665 | HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit); |
| 666 | l->info.last_location.function = func; |
| 667 | l->info.last_location.file = file; |
| 668 | l->info.last_location.line = line; |
| 669 | |
| 670 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 671 | } |
| 672 | |
| 673 | void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l, |
| 674 | const char *func, const char *file, int line) |
| 675 | { |
| 676 | uint64_t start_time; |
| 677 | |
| 678 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 679 | abort(); |
| 680 | |
| 681 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 682 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 683 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 684 | __RWLOCK_SKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 685 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 686 | |
| 687 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 688 | |
| 689 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 690 | l->info.last_location.function = func; |
| 691 | l->info.last_location.file = file; |
| 692 | l->info.last_location.line = line; |
| 693 | |
| 694 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 695 | } |
| 696 | |
| 697 | void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l, |
| 698 | const char *func, const char *file, int line) |
| 699 | { |
| 700 | uint64_t start_time; |
| 701 | |
| 702 | if ((l->info.cur_readers | l->info.cur_writer) & tid_bit) |
| 703 | abort(); |
| 704 | |
| 705 | if (!(l->info.cur_seeker & tid_bit)) |
| 706 | abort(); |
| 707 | |
| 708 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 709 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 710 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 711 | __RWLOCK_SKTOWR(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 712 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 713 | |
| 714 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 715 | |
| 716 | HA_ATOMIC_OR(&l->info.cur_writer, tid_bit); |
| 717 | HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit); |
| 718 | l->info.last_location.function = func; |
| 719 | l->info.last_location.file = file; |
| 720 | l->info.last_location.line = line; |
| 721 | |
| 722 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 723 | } |
| 724 | |
| 725 | void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l, |
| 726 | const char *func, const char *file, int line) |
| 727 | { |
| 728 | uint64_t start_time; |
| 729 | |
| 730 | if ((l->info.cur_readers | l->info.cur_writer) & tid_bit) |
| 731 | abort(); |
| 732 | |
| 733 | if (!(l->info.cur_seeker & tid_bit)) |
| 734 | abort(); |
| 735 | |
| 736 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 737 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 738 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 739 | __RWLOCK_SKTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 740 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 741 | |
| 742 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 743 | |
| 744 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 745 | HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit); |
| 746 | l->info.last_location.function = func; |
| 747 | l->info.last_location.file = file; |
| 748 | l->info.last_location.line = line; |
| 749 | |
| 750 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 751 | } |
| 752 | |
| 753 | void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 754 | const char *func, const char *file, int line) |
| 755 | { |
| 756 | if (!(l->info.cur_seeker & tid_bit)) |
| 757 | abort(); |
| 758 | |
| 759 | HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit); |
| 760 | l->info.last_location.function = func; |
| 761 | l->info.last_location.file = file; |
| 762 | l->info.last_location.line = line; |
| 763 | |
| 764 | __RWLOCK_SKUNLOCK(&l->lock); |
| 765 | |
| 766 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked); |
| 767 | } |
| 768 | |
| 769 | int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l, |
| 770 | const char *func, const char *file, int line) |
| 771 | { |
| 772 | uint64_t start_time; |
| 773 | int r; |
| 774 | |
| 775 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 776 | abort(); |
| 777 | |
| 778 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 779 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 780 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 781 | r = __RWLOCK_TRYSKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 782 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 783 | |
| 784 | if (likely(!r)) { |
| 785 | /* got the lock ! */ |
| 786 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 787 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 788 | l->info.last_location.function = func; |
| 789 | l->info.last_location.file = file; |
| 790 | l->info.last_location.line = line; |
| 791 | } |
| 792 | |
| 793 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 794 | return r; |
| 795 | } |
| 796 | |
| 797 | int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 798 | const char *func, const char *file, int line) |
| 799 | { |
| 800 | uint64_t start_time; |
| 801 | int r; |
| 802 | |
| 803 | if ((l->info.cur_writer | l->info.cur_seeker) & tid_bit) |
| 804 | abort(); |
| 805 | |
| 806 | if (!(l->info.cur_readers & tid_bit)) |
| 807 | abort(); |
| 808 | |
| 809 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 810 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 811 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 812 | r = __RWLOCK_TRYRDTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 813 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 814 | |
| 815 | if (likely(!r)) { |
| 816 | /* got the lock ! */ |
| 817 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 818 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 819 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 820 | l->info.last_location.function = func; |
| 821 | l->info.last_location.file = file; |
| 822 | l->info.last_location.line = line; |
| 823 | } |
| 824 | |
| 825 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 826 | return r; |
| 827 | } |
| 828 | |
| 829 | void __spin_init(struct ha_spinlock *l) |
| 830 | { |
| 831 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 832 | __SPIN_INIT(&l->lock); |
| 833 | } |
| 834 | |
| 835 | void __spin_destroy(struct ha_spinlock *l) |
| 836 | { |
| 837 | __SPIN_DESTROY(&l->lock); |
| 838 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 839 | } |
| 840 | |
| 841 | void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 842 | const char *func, const char *file, int line) |
| 843 | { |
| 844 | uint64_t start_time; |
| 845 | |
| 846 | if (unlikely(l->info.owner & tid_bit)) { |
| 847 | /* the thread is already owning the lock */ |
| 848 | abort(); |
| 849 | } |
| 850 | |
| 851 | HA_ATOMIC_OR(&l->info.waiters, tid_bit); |
| 852 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 853 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 854 | __SPIN_LOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame] | 855 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 856 | |
| 857 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 858 | |
| 859 | |
| 860 | l->info.owner = tid_bit; |
| 861 | l->info.last_location.function = func; |
| 862 | l->info.last_location.file = file; |
| 863 | l->info.last_location.line = line; |
| 864 | |
| 865 | HA_ATOMIC_AND(&l->info.waiters, ~tid_bit); |
| 866 | } |
| 867 | |
| 868 | int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 869 | const char *func, const char *file, int line) |
| 870 | { |
| 871 | int r; |
| 872 | |
| 873 | if (unlikely(l->info.owner & tid_bit)) { |
| 874 | /* the thread is already owning the lock */ |
| 875 | abort(); |
| 876 | } |
| 877 | |
| 878 | /* try read should never wait */ |
| 879 | r = __SPIN_TRYLOCK(&l->lock); |
| 880 | if (unlikely(r)) |
| 881 | return r; |
| 882 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 883 | |
| 884 | l->info.owner = tid_bit; |
| 885 | l->info.last_location.function = func; |
| 886 | l->info.last_location.file = file; |
| 887 | l->info.last_location.line = line; |
| 888 | |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 893 | const char *func, const char *file, int line) |
| 894 | { |
| 895 | if (unlikely(!(l->info.owner & tid_bit))) { |
| 896 | /* the thread is not owning the lock */ |
| 897 | abort(); |
| 898 | } |
| 899 | |
| 900 | l->info.owner = 0; |
| 901 | l->info.last_location.function = func; |
| 902 | l->info.last_location.file = file; |
| 903 | l->info.last_location.line = line; |
| 904 | |
| 905 | __SPIN_UNLOCK(&l->lock); |
| 906 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 907 | } |
| 908 | |
| 909 | #endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 910 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 911 | /* Depending on the platform and how libpthread was built, pthread_exit() may |
| 912 | * involve some code in libgcc_s that would be loaded on exit for the first |
| 913 | * time, causing aborts if the process is chrooted. It's harmless bit very |
| 914 | * dirty. There isn't much we can do to make sure libgcc_s is loaded only if |
| 915 | * needed, so what we do here is that during early boot we create a dummy |
| 916 | * thread that immediately exits. This will lead to libgcc_s being loaded |
| 917 | * during boot on the platforms where it's required. |
| 918 | */ |
| 919 | static void *dummy_thread_function(void *data) |
| 920 | { |
| 921 | pthread_exit(NULL); |
| 922 | return NULL; |
| 923 | } |
| 924 | |
| 925 | static inline void preload_libgcc_s(void) |
| 926 | { |
| 927 | pthread_t dummy_thread; |
| 928 | pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL); |
| 929 | pthread_join(dummy_thread, NULL); |
| 930 | } |
| 931 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 932 | __attribute__((constructor)) |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 933 | static void __thread_init(void) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 934 | { |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 935 | char *ptr = NULL; |
| 936 | |
| 937 | if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) { |
| 938 | ha_alert("MAX_THREADS value must be between 1 and %d inclusive; " |
| 939 | "HAProxy was built with value %d, please fix it and rebuild.\n", |
| 940 | LONGBITS, MAX_THREADS); |
| 941 | exit(1); |
| 942 | } |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 943 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 944 | preload_libgcc_s(); |
Willy Tarreau | 77b9822 | 2020-09-02 08:04:35 +0200 | [diff] [blame] | 945 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 946 | thread_cpus_enabled_at_boot = thread_cpus_enabled(); |
| 947 | |
| 948 | memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).", |
| 949 | MAX_THREADS, thread_cpus_enabled_at_boot); |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 950 | hap_register_build_opts(ptr, 1); |
| 951 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 952 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 953 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 954 | #endif |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 955 | } |
| 956 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 957 | #else |
| 958 | |
| 959 | REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set)."); |
| 960 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 961 | #endif // USE_THREAD |
| 962 | |
| 963 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 964 | /* Parse the "nbthread" global directive, which takes an integer argument that |
| 965 | * contains the desired number of threads. |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 966 | */ |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 967 | static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx, |
| 968 | const struct proxy *defpx, const char *file, int line, |
| 969 | char **err) |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 970 | { |
| 971 | long nbthread; |
| 972 | char *errptr; |
| 973 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 974 | if (too_many_args(1, args, err, NULL)) |
| 975 | return -1; |
| 976 | |
| 977 | nbthread = strtol(args[1], &errptr, 10); |
| 978 | if (!*args[1] || *errptr) { |
| 979 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 980 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | #ifndef USE_THREAD |
| 984 | if (nbthread != 1) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 985 | 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]); |
| 986 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 987 | } |
| 988 | #else |
| 989 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 990 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread); |
| 991 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Willy Tarreau | fc64736 | 2019-02-02 17:05:03 +0100 | [diff] [blame] | 994 | all_threads_mask = nbits(nbthread); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 995 | #endif |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 996 | |
| 997 | HA_DIAG_WARNING_COND(global.nbthread, |
| 998 | "parsing [%s:%d] : nbthread is already defined and will be overridden.\n", |
| 999 | file, line); |
| 1000 | |
| 1001 | global.nbthread = nbthread; |
| 1002 | return 0; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 1003 | } |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 1004 | |
| 1005 | /* config keyword parsers */ |
| 1006 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1007 | { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 }, |
| 1008 | { 0, NULL, NULL } |
| 1009 | }}; |
| 1010 | |
| 1011 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |