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