Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/common/hathreads.h |
| 3 | * definitions, macros and inline functions about threads. |
| 4 | * |
| 5 | * Copyright (C) 2017 Christopher Fauet - cfaulet@haproxy.com |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _COMMON_HATHREADS_H |
| 23 | #define _COMMON_HATHREADS_H |
| 24 | |
| 25 | #include <common/config.h> |
| 26 | |
| 27 | #define MAX_THREADS_MASK ((unsigned long)-1) |
| 28 | extern THREAD_LOCAL unsigned int tid; /* The thread id */ |
Christopher Faulet | e9a896e | 2017-11-14 10:16:04 +0100 | [diff] [blame] | 29 | extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 30 | |
| 31 | #ifndef USE_THREAD |
| 32 | |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 33 | #define MAX_THREADS 1 |
| 34 | |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 35 | #define __decl_hathreads(decl) |
| 36 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 37 | #define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);}) |
| 38 | #define HA_ATOMIC_ADD(val, i) ({*(val) += (i);}) |
| 39 | #define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);}) |
| 40 | #define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);}) |
| 41 | #define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);}) |
| 42 | #define HA_ATOMIC_XCHG(val, new) \ |
| 43 | ({ \ |
| 44 | typeof(*(val)) __old = *(val); \ |
| 45 | *(val) = new; \ |
| 46 | __old; \ |
| 47 | }) |
| 48 | #define HA_ATOMIC_STORE(val, new) ({*(val) = new;}) |
| 49 | #define HA_ATOMIC_UPDATE_MAX(val, new) \ |
| 50 | ({ \ |
| 51 | typeof(*(val)) __new = (new); \ |
| 52 | \ |
| 53 | if (*(val) < __new) \ |
| 54 | *(val) = __new; \ |
| 55 | *(val); \ |
| 56 | }) |
| 57 | |
| 58 | #define HA_ATOMIC_UPDATE_MIN(val, new) \ |
| 59 | ({ \ |
| 60 | typeof(*(val)) __new = (new); \ |
| 61 | \ |
| 62 | if (*(val) > __new) \ |
| 63 | *(val) = __new; \ |
| 64 | *(val); \ |
| 65 | }) |
| 66 | |
Willy Tarreau | b29dc95 | 2017-10-31 18:00:20 +0100 | [diff] [blame] | 67 | #define HA_BARRIER() do { } while (0) |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 68 | |
| 69 | #define THREAD_SYNC_INIT(m) do { /* do nothing */ } while(0) |
| 70 | #define THREAD_SYNC_ENABLE() do { /* do nothing */ } while(0) |
| 71 | #define THREAD_WANT_SYNC() do { /* do nothing */ } while(0) |
| 72 | #define THREAD_ENTER_SYNC() do { /* do nothing */ } while(0) |
| 73 | #define THREAD_EXIT_SYNC() do { /* do nothing */ } while(0) |
| 74 | #define THREAD_NO_SYNC() ({ 0; }) |
| 75 | #define THREAD_NEED_SYNC() ({ 1; }) |
| 76 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 77 | #define HA_SPIN_INIT(l) do { /* do nothing */ } while(0) |
| 78 | #define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0) |
| 79 | #define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0) |
| 80 | #define HA_SPIN_TRYLOCK(lbl, l) ({ 0; }) |
| 81 | #define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 82 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 83 | #define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0) |
| 84 | #define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0) |
| 85 | #define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0) |
| 86 | #define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; }) |
| 87 | #define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0) |
| 88 | #define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0) |
| 89 | #define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; }) |
| 90 | #define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 91 | |
| 92 | #else /* USE_THREAD */ |
| 93 | |
| 94 | #include <stdio.h> |
| 95 | #include <stdlib.h> |
| 96 | #include <string.h> |
| 97 | #include <pthread.h> |
| 98 | #include <import/plock.h> |
| 99 | |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 100 | #define MAX_THREADS LONGBITS |
| 101 | |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 102 | #define __decl_hathreads(decl) decl |
| 103 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 104 | /* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to |
| 105 | * have a header file regrouping all functions dealing with threads. */ |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 106 | |
David Carlier | ec5e845 | 2018-01-11 14:20:43 +0000 | [diff] [blame] | 107 | #if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__) |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 108 | /* gcc < 4.7 */ |
| 109 | |
| 110 | #define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i) |
| 111 | #define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i) |
| 112 | #define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags) |
| 113 | #define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags) |
| 114 | |
| 115 | /* the CAS is a bit complicated. The older API doesn't support returning the |
| 116 | * value and the swap's result at the same time. So here we take what looks |
| 117 | * like the safest route, consisting in using the boolean version guaranteeing |
| 118 | * that the operation was performed or not, and we snoop a previous value. If |
| 119 | * the compare succeeds, we return. If it fails, we return the previous value, |
| 120 | * but only if it differs from the expected one. If it's the same it's a race |
| 121 | * thus we try again to avoid confusing a possibly sensitive caller. |
| 122 | */ |
| 123 | #define HA_ATOMIC_CAS(val, old, new) \ |
| 124 | ({ \ |
| 125 | typeof((val)) __val = (val); \ |
| 126 | typeof((old)) __oldp = (old); \ |
| 127 | typeof(*(old)) __oldv; \ |
| 128 | typeof((new)) __new = (new); \ |
| 129 | int __ret; \ |
| 130 | do { \ |
| 131 | __oldv = *__val; \ |
| 132 | __ret = __sync_bool_compare_and_swap(__val, *__oldp, __new); \ |
| 133 | } while (!__ret && *__oldp == __oldv); \ |
| 134 | if (!__ret) \ |
| 135 | *__oldp = __oldv; \ |
| 136 | __ret; \ |
| 137 | }) |
| 138 | |
| 139 | #define HA_ATOMIC_XCHG(val, new) \ |
| 140 | ({ \ |
| 141 | typeof((val)) __val = (val); \ |
| 142 | typeof(*(val)) __old; \ |
| 143 | typeof((new)) __new = (new); \ |
| 144 | do { __old = *__val; \ |
| 145 | } while (!__sync_bool_compare_and_swap(__val, __old, __new)); \ |
| 146 | __old; \ |
| 147 | }) |
| 148 | #define HA_ATOMIC_STORE(val, new) \ |
| 149 | ({ \ |
| 150 | typeof((val)) __val = (val); \ |
| 151 | typeof(*(val)) __old; \ |
| 152 | typeof((new)) __new = (new); \ |
| 153 | do { __old = *__val; \ |
| 154 | } while (!__sync_bool_compare_and_swap(__val, __old, __new)); \ |
| 155 | }) |
| 156 | #else |
| 157 | /* gcc >= 4.7 */ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 158 | #define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0) |
| 159 | #define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0) |
| 160 | #define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0) |
| 161 | #define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0) |
| 162 | #define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0) |
| 163 | #define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0) |
| 164 | #define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0) |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 165 | #endif |
| 166 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 167 | #define HA_ATOMIC_UPDATE_MAX(val, new) \ |
| 168 | ({ \ |
| 169 | typeof(*(val)) __old = *(val); \ |
| 170 | typeof(*(val)) __new = (new); \ |
| 171 | \ |
| 172 | while (__old < __new && !HA_ATOMIC_CAS(val, &__old, __new)); \ |
| 173 | (*val); \ |
| 174 | }) |
| 175 | #define HA_ATOMIC_UPDATE_MIN(val, new) \ |
| 176 | ({ \ |
| 177 | typeof((*val)) __old = *(val); \ |
| 178 | typeof((*val)) __new = (new); \ |
| 179 | \ |
| 180 | while (__old > __new && !HA_ATOMIC_CAS(val, &__old, __new)); \ |
| 181 | (*val); \ |
| 182 | }) |
| 183 | |
Willy Tarreau | b29dc95 | 2017-10-31 18:00:20 +0100 | [diff] [blame] | 184 | #define HA_BARRIER() pl_barrier() |
| 185 | |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 186 | #define THREAD_SYNC_INIT(m) thread_sync_init(m) |
| 187 | #define THREAD_SYNC_ENABLE() thread_sync_enable() |
| 188 | #define THREAD_WANT_SYNC() thread_want_sync() |
| 189 | #define THREAD_ENTER_SYNC() thread_enter_sync() |
| 190 | #define THREAD_EXIT_SYNC() thread_exit_sync() |
| 191 | #define THREAD_NO_SYNC() thread_no_sync() |
| 192 | #define THREAD_NEED_SYNC() thread_need_sync() |
| 193 | |
| 194 | int thread_sync_init(unsigned long mask); |
| 195 | void thread_sync_enable(void); |
| 196 | void thread_want_sync(void); |
| 197 | void thread_enter_sync(void); |
| 198 | void thread_exit_sync(void); |
| 199 | int thread_no_sync(void); |
| 200 | int thread_need_sync(void); |
| 201 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 202 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 203 | |
| 204 | enum lock_label { |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 205 | THREAD_SYNC_LOCK = 0, |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 206 | FDCACHE_LOCK, |
| 207 | FD_LOCK, |
| 208 | POLL_LOCK, |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 209 | TASK_RQ_LOCK, |
| 210 | TASK_WQ_LOCK, |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 211 | POOL_LOCK, |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 212 | LISTENER_LOCK, |
| 213 | LISTENER_QUEUE_LOCK, |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 214 | PROXY_LOCK, |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 215 | SERVER_LOCK, |
Christopher Faulet | 5d42e09 | 2017-10-16 12:00:40 +0200 | [diff] [blame] | 216 | UPDATED_SERVERS_LOCK, |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 217 | LBPRM_LOCK, |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 218 | SIGNALS_LOCK, |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 219 | STK_TABLE_LOCK, |
| 220 | STK_SESS_LOCK, |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 221 | APPLETS_LOCK, |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 222 | PEER_LOCK, |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 223 | BUF_WQ_LOCK, |
Emeric Brun | 6b35e9b | 2017-06-30 16:23:45 +0200 | [diff] [blame] | 224 | STRMS_LOCK, |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 225 | SSL_LOCK, |
| 226 | SSL_GEN_CERTS_LOCK, |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 227 | PATREF_LOCK, |
| 228 | PATEXP_LOCK, |
| 229 | PATLRU_LOCK, |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 230 | VARS_LOCK, |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 231 | COMP_POOL_LOCK, |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 232 | LUA_LOCK, |
Thierry FOURNIER | 738a6d7 | 2017-07-17 00:14:07 +0200 | [diff] [blame] | 233 | NOTIF_LOCK, |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 234 | SPOE_APPLET_LOCK, |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 235 | DNS_LOCK, |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 236 | PID_LIST_LOCK, |
Christopher Faulet | c2a89a6 | 2017-10-23 15:54:24 +0200 | [diff] [blame] | 237 | EMAIL_ALERTS_LOCK, |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 238 | PIPES_LOCK, |
Willy Tarreau | 1605c7a | 2018-01-23 19:01:49 +0100 | [diff] [blame] | 239 | START_LOCK, |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 240 | LOCK_LABELS |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 241 | }; |
| 242 | struct lock_stat { |
| 243 | uint64_t nsec_wait_for_write; |
| 244 | uint64_t nsec_wait_for_read; |
| 245 | uint64_t num_write_locked; |
| 246 | uint64_t num_write_unlocked; |
| 247 | uint64_t num_read_locked; |
| 248 | uint64_t num_read_unlocked; |
| 249 | }; |
| 250 | |
| 251 | extern struct lock_stat lock_stats[LOCK_LABELS]; |
| 252 | |
| 253 | #define __HA_SPINLOCK_T unsigned long |
| 254 | |
| 255 | #define __SPIN_INIT(l) ({ (*l) = 0; }) |
| 256 | #define __SPIN_DESTROY(l) ({ (*l) = 0; }) |
Willy Tarreau | 88ac59b | 2017-11-06 01:03:26 +0100 | [diff] [blame] | 257 | #define __SPIN_LOCK(l) pl_take_s(l) |
| 258 | #define __SPIN_TRYLOCK(l) !pl_try_s(l) |
| 259 | #define __SPIN_UNLOCK(l) pl_drop_s(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 260 | |
| 261 | #define __HA_RWLOCK_T unsigned long |
| 262 | |
| 263 | #define __RWLOCK_INIT(l) ({ (*l) = 0; }) |
| 264 | #define __RWLOCK_DESTROY(l) ({ (*l) = 0; }) |
| 265 | #define __RWLOCK_WRLOCK(l) pl_take_w(l) |
| 266 | #define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l) |
| 267 | #define __RWLOCK_WRUNLOCK(l) pl_drop_w(l) |
| 268 | #define __RWLOCK_RDLOCK(l) pl_take_r(l) |
| 269 | #define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l) |
| 270 | #define __RWLOCK_RDUNLOCK(l) pl_drop_r(l) |
| 271 | |
| 272 | #define HA_SPINLOCK_T struct ha_spinlock |
| 273 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 274 | #define HA_SPIN_INIT(l) __spin_init(l) |
| 275 | #define HA_SPIN_DESTROY(l) __spin_destroy(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 276 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 277 | #define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__) |
| 278 | #define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__) |
| 279 | #define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 280 | |
| 281 | #define HA_RWLOCK_T struct ha_rwlock |
| 282 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 283 | #define HA_RWLOCK_INIT(l) __ha_rwlock_init((l)) |
| 284 | #define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l)) |
| 285 | #define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__) |
| 286 | #define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__) |
| 287 | #define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__) |
| 288 | #define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l) |
| 289 | #define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l) |
| 290 | #define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 291 | |
| 292 | struct ha_spinlock { |
| 293 | __HA_SPINLOCK_T lock; |
| 294 | struct { |
| 295 | unsigned long owner; /* a bit is set to 1 << tid for the lock owner */ |
| 296 | unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */ |
| 297 | struct { |
| 298 | const char *function; |
| 299 | const char *file; |
| 300 | int line; |
| 301 | } last_location; /* location of the last owner */ |
| 302 | } info; |
| 303 | }; |
| 304 | |
| 305 | struct ha_rwlock { |
| 306 | __HA_RWLOCK_T lock; |
| 307 | struct { |
| 308 | unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */ |
| 309 | unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */ |
| 310 | unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */ |
| 311 | unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */ |
| 312 | struct { |
| 313 | const char *function; |
| 314 | const char *file; |
| 315 | int line; |
| 316 | } last_location; /* location of the last write owner */ |
| 317 | } info; |
| 318 | }; |
| 319 | |
| 320 | static inline void show_lock_stats() |
| 321 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 322 | const char *labels[LOCK_LABELS] = {"THREAD_SYNC", "FDTAB", "FDCACHE", "FD", "POLL", |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 323 | "TASK_RQ", "TASK_WQ", "POOL", |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 324 | "LISTENER", "LISTENER_QUEUE", "PROXY", "SERVER", |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 325 | "UPDATED_SERVERS", "LBPRM", "SIGNALS", "STK_TABLE", "STK_SESS", |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 326 | "APPLETS", "PEER", "BUF_WQ", "STREAMS", "SSL", "SSL_GEN_CERTS", |
Thierry FOURNIER | 738a6d7 | 2017-07-17 00:14:07 +0200 | [diff] [blame] | 327 | "PATREF", "PATEXP", "PATLRU", "VARS", "COMP_POOL", "LUA", |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 328 | "NOTIF", "SPOE_APPLET", "DNS", "PID_LIST", "EMAIL_ALERTS", |
| 329 | "PIPES" }; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 330 | int lbl; |
| 331 | |
| 332 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 333 | fprintf(stderr, |
| 334 | "Stats about Lock %s: \n" |
| 335 | "\t # write lock : %lu\n" |
| 336 | "\t # write unlock: %lu (%ld)\n" |
| 337 | "\t # wait time for write : %.3f msec\n" |
| 338 | "\t # wait time for write/lock: %.3f nsec\n" |
| 339 | "\t # read lock : %lu\n" |
| 340 | "\t # read unlock : %lu (%ld)\n" |
| 341 | "\t # wait time for read : %.3f msec\n" |
| 342 | "\t # wait time for read/lock : %.3f nsec\n", |
| 343 | labels[lbl], |
| 344 | lock_stats[lbl].num_write_locked, |
| 345 | lock_stats[lbl].num_write_unlocked, |
| 346 | lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked, |
| 347 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 348 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0, |
| 349 | lock_stats[lbl].num_read_locked, |
| 350 | lock_stats[lbl].num_read_unlocked, |
| 351 | lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked, |
| 352 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 353 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* Following functions are used to collect some stats about locks. We wrap |
| 358 | * pthread functions to known how much time we wait in a lock. */ |
| 359 | |
| 360 | static uint64_t nsec_now(void) { |
| 361 | struct timespec ts; |
| 362 | |
| 363 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 364 | return ((uint64_t) ts.tv_sec * 1000000000ULL + |
| 365 | (uint64_t) ts.tv_nsec); |
| 366 | } |
| 367 | |
| 368 | static inline void __ha_rwlock_init(struct ha_rwlock *l) |
| 369 | { |
| 370 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 371 | __RWLOCK_INIT(&l->lock); |
| 372 | } |
| 373 | |
| 374 | static inline void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 375 | { |
| 376 | __RWLOCK_DESTROY(&l->lock); |
| 377 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 382 | const char *func, const char *file, int line) |
| 383 | { |
| 384 | uint64_t start_time; |
| 385 | |
| 386 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 387 | /* the thread is already owning the lock for write */ |
| 388 | abort(); |
| 389 | } |
| 390 | |
| 391 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 392 | /* the thread is already owning the lock for read */ |
| 393 | abort(); |
| 394 | } |
| 395 | |
| 396 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 397 | |
| 398 | start_time = nsec_now(); |
| 399 | __RWLOCK_WRLOCK(&l->lock); |
| 400 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 401 | |
| 402 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 403 | |
| 404 | l->info.cur_writer = tid_bit; |
| 405 | l->info.last_location.function = func; |
| 406 | l->info.last_location.file = file; |
| 407 | l->info.last_location.line = line; |
| 408 | |
| 409 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 410 | } |
| 411 | |
| 412 | static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 413 | const char *func, const char *file, int line) |
| 414 | { |
| 415 | uint64_t start_time; |
| 416 | int r; |
| 417 | |
| 418 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 419 | /* the thread is already owning the lock for write */ |
| 420 | abort(); |
| 421 | } |
| 422 | |
| 423 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 424 | /* the thread is already owning the lock for read */ |
| 425 | abort(); |
| 426 | } |
| 427 | |
| 428 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
| 429 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 430 | |
| 431 | start_time = nsec_now(); |
| 432 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
| 433 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 434 | if (unlikely(r)) { |
| 435 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 436 | return r; |
| 437 | } |
| 438 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 439 | |
| 440 | l->info.cur_writer = tid_bit; |
| 441 | l->info.last_location.function = func; |
| 442 | l->info.last_location.file = file; |
| 443 | l->info.last_location.line = line; |
| 444 | |
| 445 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 451 | const char *func, const char *file, int line) |
| 452 | { |
| 453 | if (unlikely(!(l->info.cur_writer & tid_bit))) { |
| 454 | /* the thread is not owning the lock for write */ |
| 455 | abort(); |
| 456 | } |
| 457 | |
| 458 | l->info.cur_writer = 0; |
| 459 | l->info.last_location.function = func; |
| 460 | l->info.last_location.file = file; |
| 461 | l->info.last_location.line = line; |
| 462 | |
| 463 | __RWLOCK_WRUNLOCK(&l->lock); |
| 464 | |
| 465 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1); |
| 466 | } |
| 467 | |
| 468 | static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 469 | { |
| 470 | uint64_t start_time; |
| 471 | |
| 472 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 473 | /* the thread is already owning the lock for write */ |
| 474 | abort(); |
| 475 | } |
| 476 | |
| 477 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 478 | /* the thread is already owning the lock for read */ |
| 479 | abort(); |
| 480 | } |
| 481 | |
| 482 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 483 | |
| 484 | start_time = nsec_now(); |
| 485 | __RWLOCK_RDLOCK(&l->lock); |
| 486 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time)); |
| 487 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1); |
| 488 | |
| 489 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 490 | |
| 491 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 492 | } |
| 493 | |
| 494 | static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 495 | { |
| 496 | int r; |
| 497 | |
| 498 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 499 | /* the thread is already owning the lock for write */ |
| 500 | abort(); |
| 501 | } |
| 502 | |
| 503 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 504 | /* the thread is already owning the lock for read */ |
| 505 | abort(); |
| 506 | } |
| 507 | |
| 508 | /* try read should never wait */ |
| 509 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 510 | if (unlikely(r)) |
| 511 | return r; |
| 512 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1); |
| 513 | |
| 514 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 520 | { |
| 521 | if (unlikely(!(l->info.cur_readers & tid_bit))) { |
| 522 | /* the thread is not owning the lock for read */ |
| 523 | abort(); |
| 524 | } |
| 525 | |
| 526 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 527 | |
| 528 | __RWLOCK_RDUNLOCK(&l->lock); |
| 529 | |
| 530 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1); |
| 531 | } |
| 532 | |
| 533 | static inline void __spin_init(struct ha_spinlock *l) |
| 534 | { |
| 535 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 536 | __SPIN_INIT(&l->lock); |
| 537 | } |
| 538 | |
| 539 | static inline void __spin_destroy(struct ha_spinlock *l) |
| 540 | { |
| 541 | __SPIN_DESTROY(&l->lock); |
| 542 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 543 | } |
| 544 | |
| 545 | static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 546 | const char *func, const char *file, int line) |
| 547 | { |
| 548 | uint64_t start_time; |
| 549 | |
| 550 | if (unlikely(l->info.owner & tid_bit)) { |
| 551 | /* the thread is already owning the lock */ |
| 552 | abort(); |
| 553 | } |
| 554 | |
| 555 | HA_ATOMIC_OR(&l->info.waiters, tid_bit); |
| 556 | |
| 557 | start_time = nsec_now(); |
| 558 | __SPIN_LOCK(&l->lock); |
| 559 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 560 | |
| 561 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 562 | |
| 563 | |
| 564 | l->info.owner = tid_bit; |
| 565 | l->info.last_location.function = func; |
| 566 | l->info.last_location.file = file; |
| 567 | l->info.last_location.line = line; |
| 568 | |
| 569 | HA_ATOMIC_AND(&l->info.waiters, ~tid_bit); |
| 570 | } |
| 571 | |
| 572 | static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 573 | const char *func, const char *file, int line) |
| 574 | { |
| 575 | int r; |
| 576 | |
| 577 | if (unlikely(l->info.owner & tid_bit)) { |
| 578 | /* the thread is already owning the lock */ |
| 579 | abort(); |
| 580 | } |
| 581 | |
| 582 | /* try read should never wait */ |
| 583 | r = __SPIN_TRYLOCK(&l->lock); |
| 584 | if (unlikely(r)) |
| 585 | return r; |
| 586 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 587 | |
| 588 | l->info.owner = tid_bit; |
| 589 | l->info.last_location.function = func; |
| 590 | l->info.last_location.file = file; |
| 591 | l->info.last_location.line = line; |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 597 | const char *func, const char *file, int line) |
| 598 | { |
| 599 | if (unlikely(!(l->info.owner & tid_bit))) { |
| 600 | /* the thread is not owning the lock */ |
| 601 | abort(); |
| 602 | } |
| 603 | |
| 604 | l->info.owner = 0; |
| 605 | l->info.last_location.function = func; |
| 606 | l->info.last_location.file = file; |
| 607 | l->info.last_location.line = line; |
| 608 | |
Willy Tarreau | 7c2a2ad | 2017-11-02 16:26:02 +0100 | [diff] [blame] | 609 | __SPIN_UNLOCK(&l->lock); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 610 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1); |
| 611 | } |
| 612 | |
| 613 | #else /* DEBUG_THREAD */ |
| 614 | |
| 615 | #define HA_SPINLOCK_T unsigned long |
| 616 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 617 | #define HA_SPIN_INIT(l) ({ (*l) = 0; }) |
| 618 | #define HA_SPIN_DESTROY(l) ({ (*l) = 0; }) |
| 619 | #define HA_SPIN_LOCK(lbl, l) pl_take_s(l) |
| 620 | #define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l) |
| 621 | #define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 622 | |
| 623 | #define HA_RWLOCK_T unsigned long |
| 624 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 625 | #define HA_RWLOCK_INIT(l) ({ (*l) = 0; }) |
| 626 | #define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; }) |
| 627 | #define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l) |
| 628 | #define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l) |
| 629 | #define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l) |
| 630 | #define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l) |
| 631 | #define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l) |
| 632 | #define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 633 | |
| 634 | #endif /* DEBUG_THREAD */ |
| 635 | |
| 636 | #endif /* USE_THREAD */ |
| 637 | |
| 638 | #endif /* _COMMON_HATHREADS_H */ |