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