/*-------------------------------------------------------------------------
*
* s_lock.c
- * Spinlock support routines
+ * Hardware-dependent implementation of spinlocks.
*
- * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
+ * When waiting for a contended spinlock we loop tightly for awhile, then
+ * delay using pg_usleep() and try again. Preferably, "awhile" should be a
+ * small multiple of the maximum time we expect a spinlock to be held. 100
+ * iterations seems about right as an initial guess. However, on a
+ * uniprocessor the loop is a waste of cycles, while in a multi-CPU scenario
+ * it's usually better to spin a bit longer than to call the kernel, so we try
+ * to adapt the spin loop count depending on whether we seem to be in a
+ * uniprocessor or multiprocessor.
+ *
+ * Note: you might think MIN_SPINS_PER_DELAY should be just 1, but you'd
+ * be wrong; there are platforms where that can result in a "stuck
+ * spinlock" failure. This has been seen particularly on Alphas; it seems
+ * that the first TAS after returning from kernel space will always fail
+ * on that hardware.
+ *
+ * Once we do decide to block, we use randomly increasing pg_usleep()
+ * delays. The first delay is 1 msec, then the delay randomly increases to
+ * about one second, after which we reset to 1 msec and start again. The
+ * idea here is that in the presence of heavy contention we need to
+ * increase the delay, else the spinlock holder may never get to run and
+ * release the lock. (Consider situation where spinlock holder has been
+ * nice'd down in priority by the scheduler --- it will not get scheduled
+ * until all would-be acquirers are sleeping, so if we always use a 1-msec
+ * sleep, there is a real possibility of starvation.) But we can't just
+ * clamp the delay to an upper bound, else it would take a long time to
+ * make a reasonable number of tries.
+ *
+ * We time out and declare error after NUM_DELAYS delays (thus, exactly
+ * that many tries). With the given settings, this will usually take 2 or
+ * so minutes. It seems better to fix the total number of tries (and thus
+ * the probability of unintended failure) than to fix the total time
+ * spent.
+ *
+ * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/s_lock.c,v 1.1 2001/09/27 19:10:02 tgl Exp $
+ * src/backend/storage/lmgr/s_lock.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
-#include <sys/time.h>
+#include <time.h>
#include <unistd.h>
-#include "miscadmin.h"
#include "storage/s_lock.h"
+#include "storage/barrier.h"
-/*----------
- * Each time we busy spin we select the next element of this array as the
- * number of microseconds to wait. This accomplishes pseudo random back-off.
- *
- * Note that on most platforms, specified values will be rounded up to the
- * next multiple of a clock tick, which is often ten milliseconds (10000).
- * So, we are being way overoptimistic to assume that these different values
- * are really different, other than the last. But there are a few platforms
- * with better-than-usual timekeeping, and on these we will get pretty good
- * pseudo-random behavior.
- *
- * Total time to cycle through all 20 entries will be at least 100 msec,
- * more commonly (10 msec resolution) 220 msec, and on some platforms
- * as much as 420 msec (when the remainder of the current tick cycle is
- * ignored in deciding when to time out, as on FreeBSD and older Linuxen).
- * We use the 100msec figure to figure max_spins, so actual timeouts may
- * be as much as four times the nominal value, but will never be less.
- *----------
- */
-#define S_NSPINCYCLE 20
-
-int s_spincycle[S_NSPINCYCLE] =
-{1, 10, 100, 1000,
- 10000, 1000, 1000, 1000,
- 10000, 1000, 1000, 10000,
- 1000, 1000, 10000, 1000,
- 10000, 1000, 10000, 30000
-};
+#define MIN_SPINS_PER_DELAY 10
+#define MAX_SPINS_PER_DELAY 1000
+#define NUM_DELAYS 1000
+#define MIN_DELAY_USEC 1000L
+#define MAX_DELAY_USEC 1000000L
+
-#define AVG_SPINCYCLE 5000 /* average entry in microsec: 100ms / 20 */
+slock_t dummy_spinlock;
-#define DEFAULT_TIMEOUT (100*1000000) /* default timeout: 100 sec */
+static int spins_per_delay = DEFAULT_SPINS_PER_DELAY;
/*
* s_lock_stuck() - complain about a stuck spinlock
*/
static void
-s_lock_stuck(volatile slock_t *lock, const char *file, const int line)
+s_lock_stuck(const char *file, int line, const char *func)
{
+ if (!func)
+ func = "(unknown)";
+#if defined(S_LOCK_TEST)
fprintf(stderr,
- "\nFATAL: s_lock(%p) at %s:%d, stuck spinlock. Aborting.\n",
- lock, file, line);
- fprintf(stdout,
- "\nFATAL: s_lock(%p) at %s:%d, stuck spinlock. Aborting.\n",
- lock, file, line);
- abort();
+ "\nStuck spinlock detected at %s, %s:%d.\n",
+ func, file, line);
+ exit(1);
+#else
+ elog(PANIC, "stuck spinlock detected at %s, %s:%d",
+ func, file, line);
+#endif
+}
+
+/*
+ * s_lock(lock) - platform-independent portion of waiting for a spinlock.
+ */
+int
+s_lock(volatile slock_t *lock, const char *file, int line, const char *func)
+{
+ SpinDelayStatus delayStatus = init_spin_delay(file, line, func);
+
+ while (TAS_SPIN(lock))
+ {
+ perform_spin_delay(&delayStatus);
+ }
+
+ finish_spin_delay(&delayStatus);
+
+ return delayStatus.delays;
}
+#ifdef USE_DEFAULT_S_UNLOCK
+void
+s_unlock(volatile slock_t *lock)
+{
+#ifdef TAS_ACTIVE_WORD
+ /* HP's PA-RISC */
+ *TAS_ACTIVE_WORD(lock) = -1;
+#else
+ *lock = 0;
+#endif
+}
+#endif
/*
- * s_lock_sleep() - sleep a pseudo-random amount of time, check for timeout
- *
- * The 'timeout' is given in microsec, or may be 0 for "infinity". Note that
- * this will be a lower bound (a fairly loose lower bound, on most platforms).
- *
- * 'microsec' is the number of microsec to delay per loop. Normally
- * 'microsec' is 0, specifying to use the next s_spincycle[] value.
- * Some callers may pass a nonzero interval, specifying to use exactly that
- * delay value rather than a pseudo-random delay.
+ * Wait while spinning on a contended spinlock.
*/
void
-s_lock_sleep(unsigned spins, int timeout, int microsec,
- volatile slock_t *lock,
- const char *file, const int line)
+perform_spin_delay(SpinDelayStatus *status)
{
- struct timeval delay;
+ /* CPU-specific delay each time through the loop */
+ SPIN_DELAY();
- if (microsec > 0)
+ /* Block the process every spins_per_delay tries */
+ if (++(status->spins) >= spins_per_delay)
{
- delay.tv_sec = microsec / 1000000;
- delay.tv_usec = microsec % 1000000;
+ if (++(status->delays) > NUM_DELAYS)
+ s_lock_stuck(status->file, status->line, status->func);
+
+ if (status->cur_delay == 0) /* first time to delay? */
+ status->cur_delay = MIN_DELAY_USEC;
+
+ pg_usleep(status->cur_delay);
+
+#if defined(S_LOCK_TEST)
+ fprintf(stdout, "*");
+ fflush(stdout);
+#endif
+
+ /* increase delay by a random fraction between 1X and 2X */
+ status->cur_delay += (int) (status->cur_delay *
+ ((double) random() / (double) MAX_RANDOM_VALUE) + 0.5);
+ /* wrap back to minimum delay when max is exceeded */
+ if (status->cur_delay > MAX_DELAY_USEC)
+ status->cur_delay = MIN_DELAY_USEC;
+
+ status->spins = 0;
}
- else
+}
+
+/*
+ * After acquiring a spinlock, update estimates about how long to loop.
+ *
+ * If we were able to acquire the lock without delaying, it's a good
+ * indication we are in a multiprocessor. If we had to delay, it's a sign
+ * (but not a sure thing) that we are in a uniprocessor. Hence, we
+ * decrement spins_per_delay slowly when we had to delay, and increase it
+ * rapidly when we didn't. It's expected that spins_per_delay will
+ * converge to the minimum value on a uniprocessor and to the maximum
+ * value on a multiprocessor.
+ *
+ * Note: spins_per_delay is local within our current process. We want to
+ * average these observations across multiple backends, since it's
+ * relatively rare for this function to even get entered, and so a single
+ * backend might not live long enough to converge on a good value. That
+ * is handled by the two routines below.
+ */
+void
+finish_spin_delay(SpinDelayStatus *status)
+{
+ if (status->cur_delay == 0)
{
- delay.tv_sec = 0;
- delay.tv_usec = s_spincycle[spins % S_NSPINCYCLE];
- microsec = AVG_SPINCYCLE; /* use average to figure timeout */
+ /* we never had to delay */
+ if (spins_per_delay < MAX_SPINS_PER_DELAY)
+ spins_per_delay = Min(spins_per_delay + 100, MAX_SPINS_PER_DELAY);
}
-
- if (timeout > 0)
+ else
{
- unsigned max_spins = timeout / microsec;
-
- if (spins > max_spins)
- s_lock_stuck(lock, file, line);
+ if (spins_per_delay > MIN_SPINS_PER_DELAY)
+ spins_per_delay = Max(spins_per_delay - 1, MIN_SPINS_PER_DELAY);
}
-
- (void) select(0, NULL, NULL, NULL, &delay);
}
-
/*
- * s_lock(lock) - take a spinlock with backoff
+ * Set local copy of spins_per_delay during backend startup.
+ *
+ * NB: this has to be pretty fast as it is called while holding a spinlock
*/
void
-s_lock(volatile slock_t *lock, const char *file, const int line)
+set_spins_per_delay(int shared_spins_per_delay)
{
- unsigned spins = 0;
+ spins_per_delay = shared_spins_per_delay;
+}
+/*
+ * Update shared estimate of spins_per_delay during backend exit.
+ *
+ * NB: this has to be pretty fast as it is called while holding a spinlock
+ */
+int
+update_spins_per_delay(int shared_spins_per_delay)
+{
/*
- * If you are thinking of changing this code, be careful. This same
- * loop logic is used in other places that call TAS() directly.
+ * We use an exponential moving average with a relatively slow adaption
+ * rate, so that noise in any one backend's result won't affect the shared
+ * value too much. As long as both inputs are within the allowed range,
+ * the result must be too, so we need not worry about clamping the result.
*
- * While waiting for a lock, we check for cancel/die interrupts (which is
- * a no-op if we are inside a critical section). The interrupt check
- * can be omitted in places that know they are inside a critical
- * section. Note that an interrupt must NOT be accepted after
- * acquiring the lock.
+ * We deliberately truncate rather than rounding; this is so that single
+ * adjustments inside a backend can affect the shared estimate (see the
+ * asymmetric adjustment rules above).
*/
- while (TAS(lock))
- {
- s_lock_sleep(spins++, DEFAULT_TIMEOUT, 0, lock, file, line);
- CHECK_FOR_INTERRUPTS();
- }
+ return (shared_spins_per_delay * 15 + spins_per_delay) / 16;
}
+
/*
* Various TAS implementations that cannot live in s_lock.h as no inline
* definition exists (yet).
* In the future, get rid of tas.[cso] and fold it into this file.
+ *
+ * If you change something here, you will likely need to modify s_lock.h too,
+ * because the definitions for these are split between this file and s_lock.h.
*/
+#ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */
+
+
#if defined(__GNUC__)
-/*************************************************************************
+
+/*
* All the gcc flavors that are not inlined
*/
-#if defined(__m68k__)
+/*
+ * Note: all the if-tests here probably ought to be testing gcc version
+ * rather than platform, but I don't have adequate info to know what to
+ * write. Ideally we'd flush all this in favor of the inline version.
+ */
+#if defined(__m68k__) && !defined(__linux__)
+/* really means: extern int tas(slock_t* **lock); */
static void
-tas_dummy() /* really means: extern int tas(slock_t
- * **lock); */
+tas_dummy()
{
__asm__ __volatile__(
+#if defined(__NetBSD__) && defined(__ELF__)
+/* no underscore for label and % for registers */
+ "\
+.global tas \n\
+tas: \n\
+ movel %sp@(0x4),%a0 \n\
+ tas %a0@ \n\
+ beq _success \n\
+ moveq #-128,%d0 \n\
+ rts \n\
+_success: \n\
+ moveq #0,%d0 \n\
+ rts \n"
+#else
"\
.global _tas \n\
_tas: \n\
rts \n\
_success: \n\
moveq #0,d0 \n\
- rts \n\
-");
+ rts \n"
+#endif /* __NetBSD__ && __ELF__ */
+ );
}
+#endif /* __m68k__ && !__linux__ */
+#endif /* not __GNUC__ */
+#endif /* HAVE_SPINLOCKS */
-#endif /* __m68k__ */
-#if defined(__APPLE__) && defined(__ppc__)
-/* used in darwin. */
-/* We key off __APPLE__ here because this function differs from
- * the LinuxPPC implementation only in compiler syntax.
- */
-static void
-tas_dummy()
-{
- __asm__ __volatile__(
- "\
- .globl tas \n\
- .globl _tas \n\
-_tas: \n\
-tas: \n\
- lwarx r5,0,r3 \n\
- cmpwi r5,0 \n\
- bne fail \n\
- addi r5,r5,1 \n\
- stwcx. r5,0,r3 \n\
- beq success \n\
-fail: li r3,1 \n\
- blr \n\
-success: \n\
- li r3,0 \n\
- blr \n\
-");
-}
-
-#endif /* __APPLE__ && __ppc__ */
-
-#if defined(__powerpc__)
-/* Note: need a nice gcc constrained asm version so it can be inlined */
-static void
-tas_dummy()
-{
- __asm__ __volatile__(
- "\
-.global tas \n\
-tas: \n\
- lwarx 5,0,3 \n\
- cmpwi 5,0 \n\
- bne fail \n\
- addi 5,5,1 \n\
- stwcx. 5,0,3 \n\
- beq success \n\
-fail: li 3,1 \n\
- blr \n\
-success: \n\
- li 3,0 \n\
- blr \n\
-");
-}
-
-#endif /* __powerpc__ */
-
-#if defined(__mips__) && !defined(__sgi)
-static void
-tas_dummy()
-{
- __asm__ __volatile__(
- "\
-.global tas \n\
-tas: \n\
- .frame $sp, 0, $31 \n\
- ll $14, 0($4) \n\
- or $15, $14, 1 \n\
- sc $15, 0($4) \n\
- beq $15, 0, fail\n\
- bne $14, 0, fail\n\
- li $2, 0 \n\
- .livereg 0x2000FF0E,0x00000FFF \n\
- j $31 \n\
-fail: \n\
- li $2, 1 \n\
- j $31 \n\
-");
-}
-#endif /* __mips__ && !__sgi */
+/*****************************************************************************/
+#if defined(S_LOCK_TEST)
-#else /* not __GNUC__ */
-/***************************************************************************
- * All non gcc
+/*
+ * test program for verifying a port's spinlock support.
*/
-
-
-#if defined(sun3)
-static void
-tas_dummy() /* really means: extern int tas(slock_t
- * *lock); */
+struct test_lock_struct
{
- asm("LLA0:");
- asm(" .data");
- asm(" .text");
- asm("|#PROC# 04");
- asm(" .globl _tas");
- asm("_tas:");
- asm("|#PROLOGUE# 1");
- asm(" movel sp@(0x4),a0");
- asm(" tas a0@");
- asm(" beq LLA1");
- asm(" moveq #-128,d0");
- asm(" rts");
- asm("LLA1:");
- asm(" moveq #0,d0");
- asm(" rts");
- asm(" .data");
-}
-
-#endif /* sun3 */
-
+ char pad1;
+ slock_t lock;
+ char pad2;
+};
+volatile struct test_lock_struct test_lock;
-#if defined(NEED_SPARC_TAS_ASM)
-/*
- * sparc machines not using gcc
- */
-static void
-tas_dummy() /* really means: extern int tas(slock_t
- * *lock); */
+int
+main()
{
- asm(".seg \"data\"");
- asm(".seg \"text\"");
- asm("_tas:");
-
- /*
- * Sparc atomic test and set (sparc calls it "atomic load-store")
- */
- asm("ldstub [%r8], %r8");
- asm("retl");
- asm("nop");
-}
-
-#endif /* NEED_SPARC_TAS_ASM */
-
+ srandom((unsigned int) time(NULL));
+ test_lock.pad1 = test_lock.pad2 = 0x44;
+ S_INIT_LOCK(&test_lock.lock);
-#if defined(NEED_I386_TAS_ASM)
-/* non gcc i386 based things */
-#endif /* NEED_I386_TAS_ASM */
-
-
+ if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
+ {
+ printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
+ return 1;
+ }
-#endif /* not __GNUC__ */
+ if (!S_LOCK_FREE(&test_lock.lock))
+ {
+ printf("S_LOCK_TEST: failed, lock not initialized\n");
+ return 1;
+ }
+ S_LOCK(&test_lock.lock);
+ if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
+ {
+ printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
+ return 1;
+ }
+ if (S_LOCK_FREE(&test_lock.lock))
+ {
+ printf("S_LOCK_TEST: failed, lock not locked\n");
+ return 1;
+ }
-/*****************************************************************************/
-#if defined(S_LOCK_TEST)
+ S_UNLOCK(&test_lock.lock);
-/*
- * test program for verifying a port.
- */
+ if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
+ {
+ printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
+ return 1;
+ }
-volatile slock_t test_lock;
+ if (!S_LOCK_FREE(&test_lock.lock))
+ {
+ printf("S_LOCK_TEST: failed, lock not unlocked\n");
+ return 1;
+ }
-void
-main()
-{
- S_INIT_LOCK(&test_lock);
+ S_LOCK(&test_lock.lock);
- if (!S_LOCK_FREE(&test_lock))
+ if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
{
- printf("S_LOCK_TEST: failed, lock not initialized.\n");
- exit(1);
+ printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
+ return 1;
}
- S_LOCK(&test_lock);
-
- if (S_LOCK_FREE(&test_lock))
+ if (S_LOCK_FREE(&test_lock.lock))
{
- printf("S_LOCK_TEST: failed, lock not locked\n");
- exit(2);
+ printf("S_LOCK_TEST: failed, lock not re-locked\n");
+ return 1;
}
- printf("S_LOCK_TEST: this will hang for a few minutes and then abort\n");
- printf(" with a 'stuck spinlock' message if S_LOCK()\n");
- printf(" and TAS() are working.\n");
- s_lock(&test_lock, __FILE__, __LINE__);
+ printf("S_LOCK_TEST: this will print %d stars and then\n", NUM_DELAYS);
+ printf(" exit with a 'stuck spinlock' message\n");
+ printf(" if S_LOCK() and TAS() are working.\n");
+ fflush(stdout);
- printf("S_LOCK_TEST: failed, lock not locked~\n");
- exit(3);
+ s_lock(&test_lock.lock, __FILE__, __LINE__);
+ printf("S_LOCK_TEST: failed, lock not locked\n");
+ return 1;
}
-#endif /* S_LOCK_TEST */
+#endif /* S_LOCK_TEST */