1 /*-------------------------------------------------------------------------
4 * Hardware-dependent implementation of spinlocks.
7 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/backend/storage/lmgr/s_lock.c
14 *-------------------------------------------------------------------------
21 #include "storage/s_lock.h"
23 slock_t dummy_spinlock;
25 static int spins_per_delay = DEFAULT_SPINS_PER_DELAY;
29 * s_lock_stuck() - complain about a stuck spinlock
32 s_lock_stuck(volatile slock_t *lock, const char *file, int line)
34 #if defined(S_LOCK_TEST)
36 "\nStuck spinlock (%p) detected at %s:%d.\n",
40 elog(PANIC, "stuck spinlock (%p) detected at %s:%d",
47 * s_lock(lock) - platform-independent portion of waiting for a spinlock.
50 s_lock(volatile slock_t *lock, const char *file, int line)
53 * We loop tightly for awhile, then delay using pg_usleep() and try again.
54 * Preferably, "awhile" should be a small multiple of the maximum time we
55 * expect a spinlock to be held. 100 iterations seems about right as an
56 * initial guess. However, on a uniprocessor the loop is a waste of
57 * cycles, while in a multi-CPU scenario it's usually better to spin a bit
58 * longer than to call the kernel, so we try to adapt the spin loop count
59 * depending on whether we seem to be in a uniprocessor or multiprocessor.
61 * Note: you might think MIN_SPINS_PER_DELAY should be just 1, but you'd
62 * be wrong; there are platforms where that can result in a "stuck
63 * spinlock" failure. This has been seen particularly on Alphas; it seems
64 * that the first TAS after returning from kernel space will always fail
67 * Once we do decide to block, we use randomly increasing pg_usleep()
68 * delays. The first delay is 1 msec, then the delay randomly increases to
69 * about one second, after which we reset to 1 msec and start again. The
70 * idea here is that in the presence of heavy contention we need to
71 * increase the delay, else the spinlock holder may never get to run and
72 * release the lock. (Consider situation where spinlock holder has been
73 * nice'd down in priority by the scheduler --- it will not get scheduled
74 * until all would-be acquirers are sleeping, so if we always use a 1-msec
75 * sleep, there is a real possibility of starvation.) But we can't just
76 * clamp the delay to an upper bound, else it would take a long time to
77 * make a reasonable number of tries.
79 * We time out and declare error after NUM_DELAYS delays (thus, exactly
80 * that many tries). With the given settings, this will usually take 2 or
81 * so minutes. It seems better to fix the total number of tries (and thus
82 * the probability of unintended failure) than to fix the total time
85 * The pg_usleep() delays are measured in milliseconds because 1 msec is a
86 * common resolution limit at the OS level for newer platforms. On older
87 * platforms the resolution limit is usually 10 msec, in which case the
88 * total delay before timeout will be a bit more.
90 #define MIN_SPINS_PER_DELAY 10
91 #define MAX_SPINS_PER_DELAY 1000
92 #define NUM_DELAYS 1000
93 #define MIN_DELAY_MSEC 1
94 #define MAX_DELAY_MSEC 1000
100 while (TAS_SPIN(lock))
102 /* CPU-specific delay each time through the loop */
105 /* Block the process every spins_per_delay tries */
106 if (++spins >= spins_per_delay)
108 if (++delays > NUM_DELAYS)
109 s_lock_stuck(lock, file, line);
111 if (cur_delay == 0) /* first time to delay? */
112 cur_delay = MIN_DELAY_MSEC;
114 pg_usleep(cur_delay * 1000L);
116 #if defined(S_LOCK_TEST)
117 fprintf(stdout, "*");
121 /* increase delay by a random fraction between 1X and 2X */
122 cur_delay += (int) (cur_delay *
123 ((double) random() / (double) MAX_RANDOM_VALUE) + 0.5);
124 /* wrap back to minimum delay when max is exceeded */
125 if (cur_delay > MAX_DELAY_MSEC)
126 cur_delay = MIN_DELAY_MSEC;
133 * If we were able to acquire the lock without delaying, it's a good
134 * indication we are in a multiprocessor. If we had to delay, it's a sign
135 * (but not a sure thing) that we are in a uniprocessor. Hence, we
136 * decrement spins_per_delay slowly when we had to delay, and increase it
137 * rapidly when we didn't. It's expected that spins_per_delay will
138 * converge to the minimum value on a uniprocessor and to the maximum
139 * value on a multiprocessor.
141 * Note: spins_per_delay is local within our current process. We want to
142 * average these observations across multiple backends, since it's
143 * relatively rare for this function to even get entered, and so a single
144 * backend might not live long enough to converge on a good value. That
145 * is handled by the two routines below.
149 /* we never had to delay */
150 if (spins_per_delay < MAX_SPINS_PER_DELAY)
151 spins_per_delay = Min(spins_per_delay + 100, MAX_SPINS_PER_DELAY);
155 if (spins_per_delay > MIN_SPINS_PER_DELAY)
156 spins_per_delay = Max(spins_per_delay - 1, MIN_SPINS_PER_DELAY);
162 * Set local copy of spins_per_delay during backend startup.
164 * NB: this has to be pretty fast as it is called while holding a spinlock
167 set_spins_per_delay(int shared_spins_per_delay)
169 spins_per_delay = shared_spins_per_delay;
173 * Update shared estimate of spins_per_delay during backend exit.
175 * NB: this has to be pretty fast as it is called while holding a spinlock
178 update_spins_per_delay(int shared_spins_per_delay)
181 * We use an exponential moving average with a relatively slow adaption
182 * rate, so that noise in any one backend's result won't affect the shared
183 * value too much. As long as both inputs are within the allowed range,
184 * the result must be too, so we need not worry about clamping the result.
186 * We deliberately truncate rather than rounding; this is so that single
187 * adjustments inside a backend can affect the shared estimate (see the
188 * asymmetric adjustment rules above).
190 return (shared_spins_per_delay * 15 + spins_per_delay) / 16;
195 * Various TAS implementations that cannot live in s_lock.h as no inline
196 * definition exists (yet).
197 * In the future, get rid of tas.[cso] and fold it into this file.
199 * If you change something here, you will likely need to modify s_lock.h too,
200 * because the definitions for these are split between this file and s_lock.h.
204 #ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */
207 #if defined(__GNUC__)
210 * All the gcc flavors that are not inlined
215 * Note: all the if-tests here probably ought to be testing gcc version
216 * rather than platform, but I don't have adequate info to know what to
217 * write. Ideally we'd flush all this in favor of the inline version.
219 #if defined(__m68k__) && !defined(__linux__)
220 /* really means: extern int tas(slock_t* **lock); */
224 __asm__ __volatile__(
225 #if defined(__NetBSD__) && defined(__ELF__)
226 /* no underscore for label and % for registers */
230 movel %sp@(0x4),%a0 \n\
242 movel sp@(0x4),a0 \n\
250 #endif /* __NetBSD__ && __ELF__ */
253 #endif /* __m68k__ && !__linux__ */
254 #else /* not __GNUC__ */
263 tas_dummy() /* really means: extern int tas(slock_t
272 asm("|#PROLOGUE# 1");
273 asm(" movel sp@(0x4),a0");
276 asm(" moveq #-128,d0");
284 #endif /* not __GNUC__ */
285 #endif /* HAVE_SPINLOCKS */
289 /*****************************************************************************/
290 #if defined(S_LOCK_TEST)
293 * test program for verifying a port's spinlock support.
296 struct test_lock_struct
303 volatile struct test_lock_struct test_lock;
308 srandom((unsigned int) time(NULL));
310 test_lock.pad1 = test_lock.pad2 = 0x44;
312 S_INIT_LOCK(&test_lock.lock);
314 if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
316 printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
320 if (!S_LOCK_FREE(&test_lock.lock))
322 printf("S_LOCK_TEST: failed, lock not initialized\n");
326 S_LOCK(&test_lock.lock);
328 if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
330 printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
334 if (S_LOCK_FREE(&test_lock.lock))
336 printf("S_LOCK_TEST: failed, lock not locked\n");
340 S_UNLOCK(&test_lock.lock);
342 if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
344 printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
348 if (!S_LOCK_FREE(&test_lock.lock))
350 printf("S_LOCK_TEST: failed, lock not unlocked\n");
354 S_LOCK(&test_lock.lock);
356 if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
358 printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
362 if (S_LOCK_FREE(&test_lock.lock))
364 printf("S_LOCK_TEST: failed, lock not re-locked\n");
368 printf("S_LOCK_TEST: this will print %d stars and then\n", NUM_DELAYS);
369 printf(" exit with a 'stuck spinlock' message\n");
370 printf(" if S_LOCK() and TAS() are working.\n");
373 s_lock(&test_lock.lock, __FILE__, __LINE__);
375 printf("S_LOCK_TEST: failed, lock not locked\n");
379 #endif /* S_LOCK_TEST */