| 1 | /*------------------------------------------------------------------------- |
| 2 | * |
| 3 | * s_lock.c |
| 4 | * Hardware-dependent implementation of spinlocks. |
| 5 | * |
| 6 | * When waiting for a contended spinlock we loop tightly for awhile, then |
| 7 | * delay using pg_usleep() and try again. Preferably, "awhile" should be a |
| 8 | * small multiple of the maximum time we expect a spinlock to be held. 100 |
| 9 | * iterations seems about right as an initial guess. However, on a |
| 10 | * uniprocessor the loop is a waste of cycles, while in a multi-CPU scenario |
| 11 | * it's usually better to spin a bit longer than to call the kernel, so we try |
| 12 | * to adapt the spin loop count depending on whether we seem to be in a |
| 13 | * uniprocessor or multiprocessor. |
| 14 | * |
| 15 | * Note: you might think MIN_SPINS_PER_DELAY should be just 1, but you'd |
| 16 | * be wrong; there are platforms where that can result in a "stuck |
| 17 | * spinlock" failure. This has been seen particularly on Alphas; it seems |
| 18 | * that the first TAS after returning from kernel space will always fail |
| 19 | * on that hardware. |
| 20 | * |
| 21 | * Once we do decide to block, we use randomly increasing pg_usleep() |
| 22 | * delays. The first delay is 1 msec, then the delay randomly increases to |
| 23 | * about one second, after which we reset to 1 msec and start again. The |
| 24 | * idea here is that in the presence of heavy contention we need to |
| 25 | * increase the delay, else the spinlock holder may never get to run and |
| 26 | * release the lock. (Consider situation where spinlock holder has been |
| 27 | * nice'd down in priority by the scheduler --- it will not get scheduled |
| 28 | * until all would-be acquirers are sleeping, so if we always use a 1-msec |
| 29 | * sleep, there is a real possibility of starvation.) But we can't just |
| 30 | * clamp the delay to an upper bound, else it would take a long time to |
| 31 | * make a reasonable number of tries. |
| 32 | * |
| 33 | * We time out and declare error after NUM_DELAYS delays (thus, exactly |
| 34 | * that many tries). With the given settings, this will usually take 2 or |
| 35 | * so minutes. It seems better to fix the total number of tries (and thus |
| 36 | * the probability of unintended failure) than to fix the total time |
| 37 | * spent. |
| 38 | * |
| 39 | * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group |
| 40 | * Portions Copyright (c) 1994, Regents of the University of California |
| 41 | * |
| 42 | * |
| 43 | * IDENTIFICATION |
| 44 | * src/backend/storage/lmgr/s_lock.c |
| 45 | * |
| 46 | *------------------------------------------------------------------------- |
| 47 | */ |
| 48 | #include "postgres.h" |
| 49 | |
| 50 | #include <time.h> |
| 51 | #include <unistd.h> |
| 52 | |
| 53 | #include "storage/s_lock.h" |
| 54 | #include "port/atomics.h" |
| 55 | |
| 56 | |
| 57 | #define MIN_SPINS_PER_DELAY 10 |
| 58 | #define MAX_SPINS_PER_DELAY 1000 |
| 59 | #define NUM_DELAYS 1000 |
| 60 | #define MIN_DELAY_USEC 1000L |
| 61 | #define MAX_DELAY_USEC 1000000L |
| 62 | |
| 63 | |
| 64 | slock_t dummy_spinlock; |
| 65 | |
| 66 | static int spins_per_delay = DEFAULT_SPINS_PER_DELAY; |
| 67 | |
| 68 | |
| 69 | /* |
| 70 | * s_lock_stuck() - complain about a stuck spinlock |
| 71 | */ |
| 72 | static void |
| 73 | s_lock_stuck(const char *file, int line, const char *func) |
| 74 | { |
| 75 | if (!func) |
| 76 | func = "(unknown)" ; |
| 77 | #if defined(S_LOCK_TEST) |
| 78 | fprintf(stderr, |
| 79 | "\nStuck spinlock detected at %s, %s:%d.\n" , |
| 80 | func, file, line); |
| 81 | exit(1); |
| 82 | #else |
| 83 | elog(PANIC, "stuck spinlock detected at %s, %s:%d" , |
| 84 | func, file, line); |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * s_lock(lock) - platform-independent portion of waiting for a spinlock. |
| 90 | */ |
| 91 | int |
| 92 | s_lock(volatile slock_t *lock, const char *file, int line, const char *func) |
| 93 | { |
| 94 | SpinDelayStatus delayStatus; |
| 95 | |
| 96 | init_spin_delay(&delayStatus, file, line, func); |
| 97 | |
| 98 | while (TAS_SPIN(lock)) |
| 99 | { |
| 100 | perform_spin_delay(&delayStatus); |
| 101 | } |
| 102 | |
| 103 | finish_spin_delay(&delayStatus); |
| 104 | |
| 105 | return delayStatus.delays; |
| 106 | } |
| 107 | |
| 108 | #ifdef USE_DEFAULT_S_UNLOCK |
| 109 | void |
| 110 | s_unlock(volatile slock_t *lock) |
| 111 | { |
| 112 | #ifdef TAS_ACTIVE_WORD |
| 113 | /* HP's PA-RISC */ |
| 114 | *TAS_ACTIVE_WORD(lock) = -1; |
| 115 | #else |
| 116 | *lock = 0; |
| 117 | #endif |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | /* |
| 122 | * Wait while spinning on a contended spinlock. |
| 123 | */ |
| 124 | void |
| 125 | perform_spin_delay(SpinDelayStatus *status) |
| 126 | { |
| 127 | /* CPU-specific delay each time through the loop */ |
| 128 | SPIN_DELAY(); |
| 129 | |
| 130 | /* Block the process every spins_per_delay tries */ |
| 131 | if (++(status->spins) >= spins_per_delay) |
| 132 | { |
| 133 | if (++(status->delays) > NUM_DELAYS) |
| 134 | s_lock_stuck(status->file, status->line, status->func); |
| 135 | |
| 136 | if (status->cur_delay == 0) /* first time to delay? */ |
| 137 | status->cur_delay = MIN_DELAY_USEC; |
| 138 | |
| 139 | pg_usleep(status->cur_delay); |
| 140 | |
| 141 | #if defined(S_LOCK_TEST) |
| 142 | fprintf(stdout, "*" ); |
| 143 | fflush(stdout); |
| 144 | #endif |
| 145 | |
| 146 | /* increase delay by a random fraction between 1X and 2X */ |
| 147 | status->cur_delay += (int) (status->cur_delay * |
| 148 | ((double) random() / (double) MAX_RANDOM_VALUE) + 0.5); |
| 149 | /* wrap back to minimum delay when max is exceeded */ |
| 150 | if (status->cur_delay > MAX_DELAY_USEC) |
| 151 | status->cur_delay = MIN_DELAY_USEC; |
| 152 | |
| 153 | status->spins = 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * After acquiring a spinlock, update estimates about how long to loop. |
| 159 | * |
| 160 | * If we were able to acquire the lock without delaying, it's a good |
| 161 | * indication we are in a multiprocessor. If we had to delay, it's a sign |
| 162 | * (but not a sure thing) that we are in a uniprocessor. Hence, we |
| 163 | * decrement spins_per_delay slowly when we had to delay, and increase it |
| 164 | * rapidly when we didn't. It's expected that spins_per_delay will |
| 165 | * converge to the minimum value on a uniprocessor and to the maximum |
| 166 | * value on a multiprocessor. |
| 167 | * |
| 168 | * Note: spins_per_delay is local within our current process. We want to |
| 169 | * average these observations across multiple backends, since it's |
| 170 | * relatively rare for this function to even get entered, and so a single |
| 171 | * backend might not live long enough to converge on a good value. That |
| 172 | * is handled by the two routines below. |
| 173 | */ |
| 174 | void |
| 175 | finish_spin_delay(SpinDelayStatus *status) |
| 176 | { |
| 177 | if (status->cur_delay == 0) |
| 178 | { |
| 179 | /* we never had to delay */ |
| 180 | if (spins_per_delay < MAX_SPINS_PER_DELAY) |
| 181 | spins_per_delay = Min(spins_per_delay + 100, MAX_SPINS_PER_DELAY); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | if (spins_per_delay > MIN_SPINS_PER_DELAY) |
| 186 | spins_per_delay = Max(spins_per_delay - 1, MIN_SPINS_PER_DELAY); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Set local copy of spins_per_delay during backend startup. |
| 192 | * |
| 193 | * NB: this has to be pretty fast as it is called while holding a spinlock |
| 194 | */ |
| 195 | void |
| 196 | set_spins_per_delay(int shared_spins_per_delay) |
| 197 | { |
| 198 | spins_per_delay = shared_spins_per_delay; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Update shared estimate of spins_per_delay during backend exit. |
| 203 | * |
| 204 | * NB: this has to be pretty fast as it is called while holding a spinlock |
| 205 | */ |
| 206 | int |
| 207 | update_spins_per_delay(int shared_spins_per_delay) |
| 208 | { |
| 209 | /* |
| 210 | * We use an exponential moving average with a relatively slow adaption |
| 211 | * rate, so that noise in any one backend's result won't affect the shared |
| 212 | * value too much. As long as both inputs are within the allowed range, |
| 213 | * the result must be too, so we need not worry about clamping the result. |
| 214 | * |
| 215 | * We deliberately truncate rather than rounding; this is so that single |
| 216 | * adjustments inside a backend can affect the shared estimate (see the |
| 217 | * asymmetric adjustment rules above). |
| 218 | */ |
| 219 | return (shared_spins_per_delay * 15 + spins_per_delay) / 16; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /* |
| 224 | * Various TAS implementations that cannot live in s_lock.h as no inline |
| 225 | * definition exists (yet). |
| 226 | * In the future, get rid of tas.[cso] and fold it into this file. |
| 227 | * |
| 228 | * If you change something here, you will likely need to modify s_lock.h too, |
| 229 | * because the definitions for these are split between this file and s_lock.h. |
| 230 | */ |
| 231 | |
| 232 | |
| 233 | #ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */ |
| 234 | |
| 235 | |
| 236 | #if defined(__GNUC__) |
| 237 | |
| 238 | /* |
| 239 | * All the gcc flavors that are not inlined |
| 240 | */ |
| 241 | |
| 242 | |
| 243 | /* |
| 244 | * Note: all the if-tests here probably ought to be testing gcc version |
| 245 | * rather than platform, but I don't have adequate info to know what to |
| 246 | * write. Ideally we'd flush all this in favor of the inline version. |
| 247 | */ |
| 248 | #if defined(__m68k__) && !defined(__linux__) |
| 249 | /* really means: extern int tas(slock_t* **lock); */ |
| 250 | static void |
| 251 | tas_dummy() |
| 252 | { |
| 253 | __asm__ __volatile__( |
| 254 | #if (defined(__NetBSD__) || defined(__OpenBSD__)) && defined(__ELF__) |
| 255 | /* no underscore for label and % for registers */ |
| 256 | "\ |
| 257 | .global tas \n\ |
| 258 | tas: \n\ |
| 259 | movel %sp@(0x4),%a0 \n\ |
| 260 | tas %a0@ \n\ |
| 261 | beq _success \n\ |
| 262 | moveq #-128,%d0 \n\ |
| 263 | rts \n\ |
| 264 | _success: \n\ |
| 265 | moveq #0,%d0 \n\ |
| 266 | rts \n" |
| 267 | #else |
| 268 | "\ |
| 269 | .global _tas \n\ |
| 270 | _tas: \n\ |
| 271 | movel sp@(0x4),a0 \n\ |
| 272 | tas a0@ \n\ |
| 273 | beq _success \n\ |
| 274 | moveq #-128,d0 \n\ |
| 275 | rts \n\ |
| 276 | _success: \n\ |
| 277 | moveq #0,d0 \n\ |
| 278 | rts \n" |
| 279 | #endif /* (__NetBSD__ || __OpenBSD__) && __ELF__ */ |
| 280 | ); |
| 281 | } |
| 282 | #endif /* __m68k__ && !__linux__ */ |
| 283 | #endif /* not __GNUC__ */ |
| 284 | #endif /* HAVE_SPINLOCKS */ |
| 285 | |
| 286 | |
| 287 | |
| 288 | /*****************************************************************************/ |
| 289 | #if defined(S_LOCK_TEST) |
| 290 | |
| 291 | /* |
| 292 | * test program for verifying a port's spinlock support. |
| 293 | */ |
| 294 | |
| 295 | struct test_lock_struct |
| 296 | { |
| 297 | char pad1; |
| 298 | slock_t lock; |
| 299 | char pad2; |
| 300 | }; |
| 301 | |
| 302 | volatile struct test_lock_struct test_lock; |
| 303 | |
| 304 | int |
| 305 | main() |
| 306 | { |
| 307 | srandom((unsigned int) time(NULL)); |
| 308 | |
| 309 | test_lock.pad1 = test_lock.pad2 = 0x44; |
| 310 | |
| 311 | S_INIT_LOCK(&test_lock.lock); |
| 312 | |
| 313 | if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44) |
| 314 | { |
| 315 | printf("S_LOCK_TEST: failed, declared datatype is wrong size\n" ); |
| 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | if (!S_LOCK_FREE(&test_lock.lock)) |
| 320 | { |
| 321 | printf("S_LOCK_TEST: failed, lock not initialized\n" ); |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | S_LOCK(&test_lock.lock); |
| 326 | |
| 327 | if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44) |
| 328 | { |
| 329 | printf("S_LOCK_TEST: failed, declared datatype is wrong size\n" ); |
| 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | if (S_LOCK_FREE(&test_lock.lock)) |
| 334 | { |
| 335 | printf("S_LOCK_TEST: failed, lock not locked\n" ); |
| 336 | return 1; |
| 337 | } |
| 338 | |
| 339 | S_UNLOCK(&test_lock.lock); |
| 340 | |
| 341 | if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44) |
| 342 | { |
| 343 | printf("S_LOCK_TEST: failed, declared datatype is wrong size\n" ); |
| 344 | return 1; |
| 345 | } |
| 346 | |
| 347 | if (!S_LOCK_FREE(&test_lock.lock)) |
| 348 | { |
| 349 | printf("S_LOCK_TEST: failed, lock not unlocked\n" ); |
| 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | S_LOCK(&test_lock.lock); |
| 354 | |
| 355 | if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44) |
| 356 | { |
| 357 | printf("S_LOCK_TEST: failed, declared datatype is wrong size\n" ); |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | if (S_LOCK_FREE(&test_lock.lock)) |
| 362 | { |
| 363 | printf("S_LOCK_TEST: failed, lock not re-locked\n" ); |
| 364 | return 1; |
| 365 | } |
| 366 | |
| 367 | printf("S_LOCK_TEST: this will print %d stars and then\n" , NUM_DELAYS); |
| 368 | printf(" exit with a 'stuck spinlock' message\n" ); |
| 369 | printf(" if S_LOCK() and TAS() are working.\n" ); |
| 370 | fflush(stdout); |
| 371 | |
| 372 | s_lock(&test_lock.lock, __FILE__, __LINE__); |
| 373 | |
| 374 | printf("S_LOCK_TEST: failed, lock not locked\n" ); |
| 375 | return 1; |
| 376 | } |
| 377 | |
| 378 | #endif /* S_LOCK_TEST */ |
| 379 | |