]> granicus.if.org Git - postgresql/blob - src/backend/storage/lmgr/s_lock.c
Improve spinlock code for recent x86 processors: insert a PAUSE
[postgresql] / src / backend / storage / lmgr / s_lock.c
1 /*-------------------------------------------------------------------------
2  *
3  * s_lock.c
4  *         Hardware-dependent implementation of spinlocks.
5  *
6  *
7  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  *        $PostgreSQL: pgsql/src/backend/storage/lmgr/s_lock.c,v 1.23 2003/12/27 20:58:58 tgl Exp $
13  *
14  *-------------------------------------------------------------------------
15  */
16 #include "postgres.h"
17
18 #include <sys/time.h>
19 #include <unistd.h>
20
21 #include "storage/s_lock.h"
22
23
24 /*
25  * s_lock_stuck() - complain about a stuck spinlock
26  */
27 static void
28 s_lock_stuck(volatile slock_t *lock, const char *file, int line)
29 {
30 #if defined(S_LOCK_TEST)
31         fprintf(stderr,
32                         "\nStuck spinlock (%p) detected at %s:%d.\n",
33                         lock, file, line);
34         exit(1);
35 #else
36         elog(PANIC, "stuck spinlock (%p) detected at %s:%d",
37                  lock, file, line);
38 #endif
39 }
40
41
42 /*
43  * s_lock(lock) - platform-independent portion of waiting for a spinlock.
44  */
45 void
46 s_lock(volatile slock_t *lock, const char *file, int line)
47 {
48         /*
49          * We loop tightly for awhile, then delay using select() and try
50          * again. Preferably, "awhile" should be a small multiple of the
51          * maximum time we expect a spinlock to be held.  100 iterations seems
52          * about right.  In most multi-CPU scenarios, the spinlock is probably
53          * held by a process on another CPU and will be released before we
54          * finish 100 iterations.  However, on a uniprocessor, the tight loop
55          * is just a waste of cycles, so don't iterate thousands of times.
56          *
57          * Once we do decide to block, we use randomly increasing select()
58          * delays. The first delay is 10 msec, then the delay randomly
59          * increases to about one second, after which we reset to 10 msec and
60          * start again.  The idea here is that in the presence of heavy
61          * contention we need to increase the delay, else the spinlock holder
62          * may never get to run and release the lock.  (Consider situation
63          * where spinlock holder has been nice'd down in priority by the
64          * scheduler --- it will not get scheduled until all would-be
65          * acquirers are sleeping, so if we always use a 10-msec sleep, there
66          * is a real possibility of starvation.)  But we can't just clamp the
67          * delay to an upper bound, else it would take a long time to make a
68          * reasonable number of tries.
69          *
70          * We time out and declare error after NUM_DELAYS delays (thus, exactly
71          * that many tries).  With the given settings, this will usually take
72          * 3 or so minutes.  It seems better to fix the total number of tries
73          * (and thus the probability of unintended failure) than to fix the
74          * total time spent.
75          *
76          * The select() delays are measured in centiseconds (0.01 sec) because 10
77          * msec is a common resolution limit at the OS level.
78          */
79 #define SPINS_PER_DELAY         100
80 #define NUM_DELAYS                      1000
81 #define MIN_DELAY_CSEC          1
82 #define MAX_DELAY_CSEC          100
83
84         int                     spins = 0;
85         int                     delays = 0;
86         int                     cur_delay = MIN_DELAY_CSEC;
87         struct timeval delay;
88
89         while (TAS(lock))
90         {
91                 /* CPU-specific delay each time through the loop */
92                 SPIN_DELAY();
93
94                 /* Block the process every SPINS_PER_DELAY tries */
95                 if (++spins > SPINS_PER_DELAY)
96                 {
97                         if (++delays > NUM_DELAYS)
98                                 s_lock_stuck(lock, file, line);
99
100                         delay.tv_sec = cur_delay / 100;
101                         delay.tv_usec = (cur_delay % 100) * 10000;
102                         (void) select(0, NULL, NULL, NULL, &delay);
103
104 #if defined(S_LOCK_TEST)
105                         fprintf(stdout, "*");
106                         fflush(stdout);
107 #endif
108
109                         /* increase delay by a random fraction between 1X and 2X */
110                         cur_delay += (int) (cur_delay *
111                           (((double) random()) / ((double) MAX_RANDOM_VALUE)) + 0.5);
112                         /* wrap back to minimum delay when max is exceeded */
113                         if (cur_delay > MAX_DELAY_CSEC)
114                                 cur_delay = MIN_DELAY_CSEC;
115
116                         spins = 0;
117                 }
118         }
119 }
120
121 /*
122  * Various TAS implementations that cannot live in s_lock.h as no inline
123  * definition exists (yet).
124  * In the future, get rid of tas.[cso] and fold it into this file.
125  *
126  * If you change something here, you will likely need to modify s_lock.h too,
127  * because the definitions for these are split between this file and s_lock.h.
128  */
129
130
131 #ifdef HAVE_SPINLOCKS   /* skip spinlocks if requested */
132
133
134 #if defined(__GNUC__)
135
136 /*
137  * All the gcc flavors that are not inlined
138  */
139
140
141 #if defined(__m68k__)
142 static void
143 tas_dummy()                                             /* really means: extern int tas(slock_t
144                                                                  * **lock); */
145 {
146         __asm__         __volatile__(
147                                                                                  "\
148 .global         _tas                            \n\
149 _tas:                                                   \n\
150                         movel   sp@(0x4),a0     \n\
151                         tas     a0@                     \n\
152                         beq     _success        \n\
153                         moveq   #-128,d0        \n\
154                         rts                                     \n\
155 _success:                                               \n\
156                         moveq   #0,d0           \n\
157                         rts                                     \n\
158 ");
159 }
160 #endif   /* __m68k__ */
161
162
163 #if defined(__mips__) && !defined(__sgi)
164 static void
165 tas_dummy()
166 {
167         __asm__         __volatile__(
168                                                                                  "\
169 .global tas                                             \n\
170 tas:                                                    \n\
171                         .frame  $sp, 0, $31     \n\
172                         .set push               \n\
173                         .set mips2              \n\
174                         ll              $14, 0($4)      \n\
175                         or              $15, $14, 1     \n\
176                         sc              $15, 0($4)      \n\
177                         .set pop                        \n\
178                         beq             $15, 0, fail\n\
179                         bne             $14, 0, fail\n\
180                         li              $2, 0           \n\
181                         .livereg 0x2000FF0E,0x00000FFF  \n\
182                         j               $31                     \n\
183 fail:                                                   \n\
184                         li              $2, 1           \n\
185                         j       $31                     \n\
186 ");
187 }
188 #endif   /* __mips__ && !__sgi */
189
190
191 #else                                                   /* not __GNUC__ */
192
193 /*
194  * All non gcc
195  */
196
197
198 #if defined(sun3)
199 static void
200 tas_dummy()                                             /* really means: extern int tas(slock_t
201                                                                  * *lock); */
202 {
203         asm("LLA0:");
204         asm("   .data");
205         asm("   .text");
206         asm("|#PROC# 04");
207         asm("   .globl  _tas");
208         asm("_tas:");
209         asm("|#PROLOGUE# 1");
210         asm("   movel   sp@(0x4),a0");
211         asm("   tas a0@");
212         asm("   beq LLA1");
213         asm("   moveq   #-128,d0");
214         asm("   rts");
215         asm("LLA1:");
216         asm("   moveq   #0,d0");
217         asm("   rts");
218         asm("   .data");
219 }
220 #endif   /* sun3 */
221
222
223 #if defined(__sparc__) || defined(__sparc)
224 /*
225  * sparc machines not using gcc
226  */
227 static void
228 tas_dummy()                                             /* really means: extern int tas(slock_t
229                                                                  * *lock); */
230 {
231         asm(".seg \"data\"");
232         asm(".seg \"text\"");
233         asm("_tas:");
234
235         /*
236          * Sparc atomic test and set (sparc calls it "atomic load-store")
237          */
238         asm("ldstub [%r8], %r8");
239         asm("retl");
240         asm("nop");
241 }
242 #endif   /* __sparc || __sparc__ */
243
244
245 #endif   /* not __GNUC__ */
246
247 #endif /* HAVE_SPINLOCKS */
248
249
250
251 /*****************************************************************************/
252 #if defined(S_LOCK_TEST)
253
254 /*
255  * test program for verifying a port's spinlock support.
256  */
257
258 volatile slock_t test_lock;
259
260 int
261 main()
262 {
263         srandom((unsigned int) time(NULL));
264
265         S_INIT_LOCK(&test_lock);
266
267         if (!S_LOCK_FREE(&test_lock))
268         {
269                 printf("S_LOCK_TEST: failed, lock not initialized\n");
270                 return 1;
271         }
272
273         S_LOCK(&test_lock);
274
275         if (S_LOCK_FREE(&test_lock))
276         {
277                 printf("S_LOCK_TEST: failed, lock not locked\n");
278                 return 1;
279         }
280
281         S_UNLOCK(&test_lock);
282
283         if (!S_LOCK_FREE(&test_lock))
284         {
285                 printf("S_LOCK_TEST: failed, lock not unlocked\n");
286                 return 1;
287         }
288
289         S_LOCK(&test_lock);
290
291         if (S_LOCK_FREE(&test_lock))
292         {
293                 printf("S_LOCK_TEST: failed, lock not re-locked\n");
294                 return 1;
295         }
296
297         printf("S_LOCK_TEST: this will print %d stars and then\n", NUM_DELAYS);
298         printf("             exit with a 'stuck spinlock' message\n");
299         printf("             if S_LOCK() and TAS() are working.\n");
300         fflush(stdout);
301
302         s_lock(&test_lock, __FILE__, __LINE__);
303
304         printf("S_LOCK_TEST: failed, lock not locked\n");
305         return 1;
306 }
307
308 #endif   /* S_LOCK_TEST */