2 * Copyright (c) 2005 Jeffrey M. Hsu. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Jeffrey M. Hsu. and Matthew Dillon
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * The spinlock code utilizes two counters to form a virtual FIFO, allowing
34 * a spinlock to allocate a slot and then only issue memory read operations
35 * until it is handed the lock (if it is not the next owner for the lock).
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/types.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
47 #include <machine/atomic.h>
48 #include <machine/cpu.h>
49 #include <machine/cpufunc.h>
50 #include <machine/specialreg.h>
51 #include <machine/clock.h>
52 #include <sys/spinlock.h>
53 #include <sys/spinlock2.h>
56 struct spinlock pmap_spin = SPINLOCK_INITIALIZER(pmap_spin);
60 struct indefinite_info {
68 #if !defined(KTR_SPIN_CONTENTION)
69 #define KTR_SPIN_CONTENTION KTR_ALL
71 #define SPIN_STRING "spin=%p type=%c"
72 #define SPIN_ARG_SIZE (sizeof(void *) + sizeof(int))
74 KTR_INFO_MASTER(spin);
76 KTR_INFO(KTR_SPIN_CONTENTION, spin, beg, 0, SPIN_STRING, SPIN_ARG_SIZE);
77 KTR_INFO(KTR_SPIN_CONTENTION, spin, end, 1, SPIN_STRING, SPIN_ARG_SIZE);
80 #define logspin(name, spin, type) \
81 KTR_LOG(spin_ ## name, spin, type)
84 static int spin_lock_test_mode;
87 static int64_t spinlocks_contested1;
88 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested1, CTLFLAG_RD,
89 &spinlocks_contested1, 0,
90 "Spinlock contention count due to collisions with exclusive lock holders");
92 static int64_t spinlocks_contested2;
93 SYSCTL_QUAD(_debug, OID_AUTO, spinlocks_contested2, CTLFLAG_RD,
94 &spinlocks_contested2, 0,
95 "Serious spinlock contention count");
97 #ifdef DEBUG_LOCKS_LATENCY
99 static long spinlocks_add_latency;
100 SYSCTL_LONG(_debug, OID_AUTO, spinlocks_add_latency, CTLFLAG_RW,
101 &spinlocks_add_latency, 0,
102 "Add spinlock latency");
108 * We need a fairly large pool to avoid contention on large SMP systems,
109 * particularly multi-chip systems.
111 /*#define SPINLOCK_NUM_POOL 8101*/
112 #define SPINLOCK_NUM_POOL 8192
113 #define SPINLOCK_NUM_POOL_MASK (SPINLOCK_NUM_POOL - 1)
115 static __cachealign struct {
116 struct spinlock spin;
117 char filler[32 - sizeof(struct spinlock)];
118 } pool_spinlocks[SPINLOCK_NUM_POOL];
120 static int spin_indefinite_check(struct spinlock *spin,
121 struct indefinite_info *info);
124 * We contested due to another exclusive lock holder. We lose.
126 * We have to unwind the attempt and may acquire the spinlock
127 * anyway while doing so. countb was incremented on our behalf.
130 spin_trylock_contested(struct spinlock *spin)
132 globaldata_t gd = mycpu;
134 /*++spinlocks_contested1;*/
135 --gd->gd_spinlocks_wr;
136 --gd->gd_curthread->td_critcount;
141 * The spin_lock() inline was unable to acquire the lock.
143 * atomic_swap_int() is the absolute fastest spinlock instruction, at
144 * least on multi-socket systems. All instructions seem to be about
145 * the same on single-socket multi-core systems. However, atomic_swap_int()
146 * does not result in an even distribution of successful acquisitions.
148 * Another problem we have is that (at least on the 48-core opteron we test
149 * with) having all 48 cores contesting the same spin lock reduces
150 * performance to around 600,000 ops/sec, verses millions when fewer cores
151 * are going after the same lock.
153 * Backoff algorithms can create even worse starvation problems, and don't
154 * really improve performance when a lot of cores are contending.
156 * Our solution is to allow the data cache to lazy-update by reading it
157 * non-atomically and only attempting to acquire the lock if the lazy read
158 * looks good. This effectively limits cache bus bandwidth. A cpu_pause()
159 * (for intel/amd anyhow) is not strictly needed as cache bus resource use
160 * is governed by the lazy update.
162 * WARNING!!!! Performance matters here, by a huge margin.
164 * 48-core test with pre-read / -j 48 no-modules kernel compile
165 * with fanned-out inactive and active queues came in at 55 seconds.
167 * 48-core test with pre-read / -j 48 no-modules kernel compile
168 * came in at 75 seconds. Without pre-read it came in at 170 seconds.
170 * 4-core test with pre-read / -j 48 no-modules kernel compile
171 * came in at 83 seconds. Without pre-read it came in at 83 seconds
172 * as well (no difference).
175 spin_lock_contested(struct spinlock *spin)
177 struct indefinite_info info = { 0, 0 };
180 #ifdef DEBUG_LOCKS_LATENCY
182 for (j = spinlocks_add_latency; j > 0; --j)
189 /*logspin(beg, spin, 'w');*/
192 * NOTE: Reading spin->counta prior to the swap is extremely
193 * important on multi-chip/many-core boxes. On 48-core
194 * this one change improves fully concurrent all-cores
195 * compiles by 100% or better.
197 * I can't emphasize enough how important the pre-read is in
198 * preventing hw cache bus armageddon on multi-chip systems.
199 * And on single-chip/multi-core systems it just doesn't hurt.
201 if (spin->counta == 0 && atomic_swap_int(&spin->counta, 1) == 0)
203 if ((++i & 0x7F) == 0x7F) {
205 if (spin_indefinite_check(spin, &info))
209 /*logspin(end, spin, 'w');*/
213 _spin_pool_hash(void *ptr)
217 i = ((int)(uintptr_t) ptr >> 5) ^ ((int)(uintptr_t)ptr >> 12);
218 i &= SPINLOCK_NUM_POOL_MASK;
223 _spin_pool_lock(void *chan)
227 sp = &pool_spinlocks[_spin_pool_hash(chan)].spin;
232 _spin_pool_unlock(void *chan)
236 sp = &pool_spinlocks[_spin_pool_hash(chan)].spin;
243 spin_indefinite_check(struct spinlock *spin, struct indefinite_info *info)
247 cpu_spinlock_contested();
249 count = sys_cputimer->count();
250 if (info->secs == 0) {
253 } else if (count - info->base > sys_cputimer->freq) {
254 kprintf("spin_lock: %p, indefinite wait (%d secs)!\n",
260 #if defined(INVARIANTS)
261 if (spin_lock_test_mode) {
266 #if defined(INVARIANTS)
267 if (info->secs == 11)
270 if (info->secs == 60)
271 panic("spin_lock: %p, indefinite wait!", spin);
277 * If INVARIANTS is enabled various spinlock timing tests can be run
278 * by setting debug.spin_lock_test:
280 * 1 Test the indefinite wait code
281 * 2 Time the best-case exclusive lock overhead (spin_test_count)
282 * 3 Time the best-case shared lock overhead (spin_test_count)
287 static int spin_test_count = 10000000;
288 SYSCTL_INT(_debug, OID_AUTO, spin_test_count, CTLFLAG_RW, &spin_test_count, 0,
289 "Number of iterations to use for spinlock wait code test");
292 sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS)
294 struct spinlock spin;
299 if ((error = priv_check(curthread, PRIV_ROOT)) != 0)
301 if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0)
305 * Indefinite wait test
309 spin_lock(&spin); /* force an indefinite wait */
310 spin_lock_test_mode = 1;
312 spin_unlock(&spin); /* Clean up the spinlock count */
314 spin_lock_test_mode = 0;
318 * Time best-case exclusive spinlocks
321 globaldata_t gd = mycpu;
324 for (i = spin_test_count; i > 0; --i) {
325 spin_lock_quick(gd, &spin);
326 spin_unlock_quick(gd, &spin);
333 SYSCTL_PROC(_debug, KERN_PROC_ALL, spin_lock_test, CTLFLAG_RW|CTLTYPE_INT,
334 0, 0, sysctl_spin_lock_test, "I", "Test spinlock wait code");
336 #endif /* INVARIANTS */