PostgreSQL Source Code  git master
s_lock.c
Go to the documentation of this file.
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 "port/atomics.h"
54 #include "storage/s_lock.h"
55 
56 #define MIN_SPINS_PER_DELAY 10
57 #define MAX_SPINS_PER_DELAY 1000
58 #define NUM_DELAYS 1000
59 #define MIN_DELAY_USEC 1000L
60 #define MAX_DELAY_USEC 1000000L
61 
62 
64 
66 
67 
68 /*
69  * s_lock_stuck() - complain about a stuck spinlock
70  */
71 static void
72 s_lock_stuck(const char *file, int line, const char *func)
73 {
74  if (!func)
75  func = "(unknown)";
76 #if defined(S_LOCK_TEST)
77  fprintf(stderr,
78  "\nStuck spinlock detected at %s, %s:%d.\n",
79  func, file, line);
80  exit(1);
81 #else
82  elog(PANIC, "stuck spinlock detected at %s, %s:%d",
83  func, file, line);
84 #endif
85 }
86 
87 /*
88  * s_lock(lock) - platform-independent portion of waiting for a spinlock.
89  */
90 int
91 s_lock(volatile slock_t *lock, const char *file, int line, const char *func)
92 {
93  SpinDelayStatus delayStatus;
94 
95  init_spin_delay(&delayStatus, file, line, func);
96 
97  while (TAS_SPIN(lock))
98  {
99  perform_spin_delay(&delayStatus);
100  }
101 
102  finish_spin_delay(&delayStatus);
103 
104  return delayStatus.delays;
105 }
106 
107 #ifdef USE_DEFAULT_S_UNLOCK
108 void
109 s_unlock(volatile slock_t *lock)
110 {
111 #ifdef TAS_ACTIVE_WORD
112  /* HP's PA-RISC */
113  *TAS_ACTIVE_WORD(lock) = -1;
114 #else
115  *lock = 0;
116 #endif
117 }
118 #endif
119 
120 /*
121  * Wait while spinning on a contended spinlock.
122  */
123 void
125 {
126  /* CPU-specific delay each time through the loop */
127  SPIN_DELAY();
128 
129  /* Block the process every spins_per_delay tries */
130  if (++(status->spins) >= spins_per_delay)
131  {
132  if (++(status->delays) > NUM_DELAYS)
133  s_lock_stuck(status->file, status->line, status->func);
134 
135  if (status->cur_delay == 0) /* first time to delay? */
136  status->cur_delay = MIN_DELAY_USEC;
137 
138  pg_usleep(status->cur_delay);
139 
140 #if defined(S_LOCK_TEST)
141  fprintf(stdout, "*");
142  fflush(stdout);
143 #endif
144 
145  /* increase delay by a random fraction between 1X and 2X */
146  status->cur_delay += (int) (status->cur_delay *
147  ((double) random() / (double) MAX_RANDOM_VALUE) + 0.5);
148  /* wrap back to minimum delay when max is exceeded */
149  if (status->cur_delay > MAX_DELAY_USEC)
150  status->cur_delay = MIN_DELAY_USEC;
151 
152  status->spins = 0;
153  }
154 }
155 
156 /*
157  * After acquiring a spinlock, update estimates about how long to loop.
158  *
159  * If we were able to acquire the lock without delaying, it's a good
160  * indication we are in a multiprocessor. If we had to delay, it's a sign
161  * (but not a sure thing) that we are in a uniprocessor. Hence, we
162  * decrement spins_per_delay slowly when we had to delay, and increase it
163  * rapidly when we didn't. It's expected that spins_per_delay will
164  * converge to the minimum value on a uniprocessor and to the maximum
165  * value on a multiprocessor.
166  *
167  * Note: spins_per_delay is local within our current process. We want to
168  * average these observations across multiple backends, since it's
169  * relatively rare for this function to even get entered, and so a single
170  * backend might not live long enough to converge on a good value. That
171  * is handled by the two routines below.
172  */
173 void
175 {
176  if (status->cur_delay == 0)
177  {
178  /* we never had to delay */
181  }
182  else
183  {
186  }
187 }
188 
189 /*
190  * Set local copy of spins_per_delay during backend startup.
191  *
192  * NB: this has to be pretty fast as it is called while holding a spinlock
193  */
194 void
195 set_spins_per_delay(int shared_spins_per_delay)
196 {
197  spins_per_delay = shared_spins_per_delay;
198 }
199 
200 /*
201  * Update shared estimate of spins_per_delay during backend exit.
202  *
203  * NB: this has to be pretty fast as it is called while holding a spinlock
204  */
205 int
206 update_spins_per_delay(int shared_spins_per_delay)
207 {
208  /*
209  * We use an exponential moving average with a relatively slow adaption
210  * rate, so that noise in any one backend's result won't affect the shared
211  * value too much. As long as both inputs are within the allowed range,
212  * the result must be too, so we need not worry about clamping the result.
213  *
214  * We deliberately truncate rather than rounding; this is so that single
215  * adjustments inside a backend can affect the shared estimate (see the
216  * asymmetric adjustment rules above).
217  */
218  return (shared_spins_per_delay * 15 + spins_per_delay) / 16;
219 }
220 
221 
222 /*
223  * Various TAS implementations that cannot live in s_lock.h as no inline
224  * definition exists (yet).
225  * In the future, get rid of tas.[cso] and fold it into this file.
226  *
227  * If you change something here, you will likely need to modify s_lock.h too,
228  * because the definitions for these are split between this file and s_lock.h.
229  */
230 
231 
232 #ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */
233 
234 
235 #if defined(__GNUC__)
236 
237 /*
238  * All the gcc flavors that are not inlined
239  */
240 
241 
242 /*
243  * Note: all the if-tests here probably ought to be testing gcc version
244  * rather than platform, but I don't have adequate info to know what to
245  * write. Ideally we'd flush all this in favor of the inline version.
246  */
247 #if defined(__m68k__) && !defined(__linux__)
248 /* really means: extern int tas(slock_t* **lock); */
249 static void
250 tas_dummy()
251 {
252  __asm__ __volatile__(
253 #if (defined(__NetBSD__) || defined(__OpenBSD__)) && defined(__ELF__)
254 /* no underscore for label and % for registers */
255  "\
256 .global tas \n\
257 tas: \n\
258  movel %sp@(0x4),%a0 \n\
259  tas %a0@ \n\
260  beq _success \n\
261  moveq #-128,%d0 \n\
262  rts \n\
263 _success: \n\
264  moveq #0,%d0 \n\
265  rts \n"
266 #else
267  "\
268 .global _tas \n\
269 _tas: \n\
270  movel sp@(0x4),a0 \n\
271  tas a0@ \n\
272  beq _success \n\
273  moveq #-128,d0 \n\
274  rts \n\
275 _success: \n\
276  moveq #0,d0 \n\
277  rts \n"
278 #endif /* (__NetBSD__ || __OpenBSD__) && __ELF__ */
279  );
280 }
281 #endif /* __m68k__ && !__linux__ */
282 #endif /* not __GNUC__ */
283 #endif /* HAVE_SPINLOCKS */
284 
285 
286 
287 /*****************************************************************************/
288 #if defined(S_LOCK_TEST)
289 
290 /*
291  * test program for verifying a port's spinlock support.
292  */
293 
294 struct test_lock_struct
295 {
296  char pad1;
297  slock_t lock;
298  char pad2;
299 };
300 
301 volatile struct test_lock_struct test_lock;
302 
303 int
304 main()
305 {
306  srandom((unsigned int) time(NULL));
307 
308  test_lock.pad1 = test_lock.pad2 = 0x44;
309 
310  S_INIT_LOCK(&test_lock.lock);
311 
312  if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
313  {
314  printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
315  return 1;
316  }
317 
318  if (!S_LOCK_FREE(&test_lock.lock))
319  {
320  printf("S_LOCK_TEST: failed, lock not initialized\n");
321  return 1;
322  }
323 
324  S_LOCK(&test_lock.lock);
325 
326  if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
327  {
328  printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
329  return 1;
330  }
331 
332  if (S_LOCK_FREE(&test_lock.lock))
333  {
334  printf("S_LOCK_TEST: failed, lock not locked\n");
335  return 1;
336  }
337 
338  S_UNLOCK(&test_lock.lock);
339 
340  if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
341  {
342  printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
343  return 1;
344  }
345 
346  if (!S_LOCK_FREE(&test_lock.lock))
347  {
348  printf("S_LOCK_TEST: failed, lock not unlocked\n");
349  return 1;
350  }
351 
352  S_LOCK(&test_lock.lock);
353 
354  if (test_lock.pad1 != 0x44 || test_lock.pad2 != 0x44)
355  {
356  printf("S_LOCK_TEST: failed, declared datatype is wrong size\n");
357  return 1;
358  }
359 
360  if (S_LOCK_FREE(&test_lock.lock))
361  {
362  printf("S_LOCK_TEST: failed, lock not re-locked\n");
363  return 1;
364  }
365 
366  printf("S_LOCK_TEST: this will print %d stars and then\n", NUM_DELAYS);
367  printf(" exit with a 'stuck spinlock' message\n");
368  printf(" if S_LOCK() and TAS() are working.\n");
369  fflush(stdout);
370 
371  s_lock(&test_lock.lock, __FILE__, __LINE__);
372 
373  printf("S_LOCK_TEST: failed, lock not locked\n");
374  return 1;
375 }
376 
377 #endif /* S_LOCK_TEST */
#define NUM_DELAYS
Definition: s_lock.c:58
int slock_t
Definition: s_lock.h:934
#define S_INIT_LOCK(lock)
Definition: s_lock.h:943
#define SPIN_DELAY()
Definition: s_lock.h:990
void set_spins_per_delay(int shared_spins_per_delay)
Definition: s_lock.c:195
long random(void)
Definition: random.c:22
#define S_LOCK_FREE(lock)
Definition: s_lock.h:941
#define Min(x, y)
Definition: c.h:911
const char * func
Definition: s_lock.h:1028
#define MAX_DELAY_USEC
Definition: s_lock.c:60
#define printf(...)
Definition: port.h:198
static void s_lock_stuck(const char *file, int line, const char *func)
Definition: s_lock.c:72
#define fprintf
Definition: port.h:196
#define PANIC
Definition: elog.h:53
int main(int argc, char **argv)
Definition: oid2name.c:586
static void init_spin_delay(SpinDelayStatus *status, const char *file, int line, const char *func)
Definition: s_lock.h:1032
#define MIN_DELAY_USEC
Definition: s_lock.c:59
void pg_usleep(long microsec)
Definition: signal.c:53
void finish_spin_delay(SpinDelayStatus *status)
Definition: s_lock.c:174
#define MAX_SPINS_PER_DELAY
Definition: s_lock.c:57
#define MAX_RANDOM_VALUE
static int spins_per_delay
Definition: s_lock.c:65
int update_spins_per_delay(int shared_spins_per_delay)
Definition: s_lock.c:206
const char * file
Definition: s_lock.h:1026
#define TAS_SPIN(lock)
Definition: s_lock.h:1001
#define Max(x, y)
Definition: c.h:905
#define S_UNLOCK(lock)
Definition: s_lock.h:942
slock_t dummy_spinlock
Definition: s_lock.c:63
#define elog(elevel,...)
Definition: elog.h:228
void srandom(unsigned int seed)
Definition: srandom.c:22
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:226
#define S_LOCK(lock)
Definition: s_lock.h:955
void perform_spin_delay(SpinDelayStatus *status)
Definition: s_lock.c:124
int s_lock(volatile slock_t *lock, const char *file, int line, const char *func)
Definition: s_lock.c:91
#define DEFAULT_SPINS_PER_DELAY
Definition: s_lock.h:1012
#define MIN_SPINS_PER_DELAY
Definition: s_lock.c:56