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");
98 * We need a fairly large pool to avoid contention on large SMP systems,
99 * particularly multi-chip systems.
101 /*#define SPINLOCK_NUM_POOL 8101*/
102 #define SPINLOCK_NUM_POOL 8192
103 #define SPINLOCK_NUM_POOL_MASK (SPINLOCK_NUM_POOL - 1)
105 static __cachealign struct {
106 struct spinlock spin;
107 char filler[32 - sizeof(struct spinlock)];
108 } pool_spinlocks[SPINLOCK_NUM_POOL];
110 static int spin_indefinite_check(struct spinlock *spin,
111 struct indefinite_info *info);
114 * We contested due to another exclusive lock holder. We lose.
116 * We have to unwind the attempt and may acquire the spinlock
117 * anyway while doing so. countb was incremented on our behalf.
120 spin_trylock_contested(struct spinlock *spin)
122 globaldata_t gd = mycpu;
124 /*++spinlocks_contested1;*/
125 --gd->gd_spinlocks_wr;
126 --gd->gd_curthread->td_critcount;
131 * The spin_lock() inline was unable to acquire the lock.
133 * atomic_swap_int() is the absolute fastest spinlock instruction, at
134 * least on multi-socket systems. All instructions seem to be about
135 * the same on single-socket multi-core systems. However, atomic_swap_int()
136 * does not result in an even distribution of successful acquisitions.
138 * Another problem we have is that (at least on the 48-core opteron we test
139 * with) having all 48 cores contesting the same spin lock reduces
140 * performance to around 600,000 ops/sec, verses millions when fewer cores
141 * are going after the same lock.
143 * Backoff algorithms can create even worse starvation problems, and don't
144 * really improve performance when a lot of cores are contending.
146 * Our solution is to allow the data cache to lazy-update by reading it
147 * non-atomically and only attempting to acquire the lock if the lazy read
148 * looks good. This effectively limits cache bus bandwidth. A cpu_pause()
149 * (for intel/amd anyhow) is not strictly needed as cache bus resource use
150 * is governed by the lazy update.
152 * WARNING!!!! Performance matters here, by a huge margin. There are still
153 * a few bottlenecks in the kernel (e.g. the PQ_INACTIVE
154 * vm_page_queue) where things like parallel compiles hit up
155 * against full all-cores contention right here.
157 * 48-core test with pre-read / -j 48 no-modules kernel compile
158 * came in at 75 seconds. Without pre-read it came in at 170 seconds.
160 * 4-core test with pre-read / -j 48 no-modules kernel compile
161 * came in at 83 seconds. Without pre-read it came in at 83 seconds
162 * as well (no difference).
165 spin_lock_contested(struct spinlock *spin)
167 struct indefinite_info info = { 0, 0 };
173 /*logspin(beg, spin, 'w');*/
176 * NOTE: Reading spin->counta prior to the swap is extremely
177 * important on multi-chip/many-core boxes. On 48-core
178 * this one change improves fully concurrent all-cores
179 * compiles by 100% or better.
181 * I can't emphasize enough how important the pre-read is in
182 * preventing hw cache bus armageddon on multi-chip systems.
183 * And on single-chip/multi-core systems it just doesn't hurt.
185 if (spin->counta == 0 && atomic_swap_int(&spin->counta, 1) == 0)
187 if ((++i & 0x7F) == 0x7F) {
189 if (spin_indefinite_check(spin, &info))
193 /*logspin(end, spin, 'w');*/
197 _spin_pool_hash(void *ptr)
201 i = ((int)(uintptr_t) ptr >> 5) ^ ((int)(uintptr_t)ptr >> 12);
202 i &= SPINLOCK_NUM_POOL_MASK;
207 _spin_pool_lock(void *chan)
211 sp = &pool_spinlocks[_spin_pool_hash(chan)].spin;
216 _spin_pool_unlock(void *chan)
220 sp = &pool_spinlocks[_spin_pool_hash(chan)].spin;
227 spin_indefinite_check(struct spinlock *spin, struct indefinite_info *info)
231 cpu_spinlock_contested();
233 count = sys_cputimer->count();
234 if (info->secs == 0) {
237 } else if (count - info->base > sys_cputimer->freq) {
238 kprintf("spin_lock: %p, indefinite wait (%d secs)!\n",
244 #if defined(INVARIANTS)
245 if (spin_lock_test_mode) {
250 #if defined(INVARIANTS)
251 if (info->secs == 11)
254 if (info->secs == 60)
255 panic("spin_lock: %p, indefinite wait!\n", spin);
261 * If INVARIANTS is enabled various spinlock timing tests can be run
262 * by setting debug.spin_lock_test:
264 * 1 Test the indefinite wait code
265 * 2 Time the best-case exclusive lock overhead (spin_test_count)
266 * 3 Time the best-case shared lock overhead (spin_test_count)
271 static int spin_test_count = 10000000;
272 SYSCTL_INT(_debug, OID_AUTO, spin_test_count, CTLFLAG_RW, &spin_test_count, 0,
273 "Number of iterations to use for spinlock wait code test");
276 sysctl_spin_lock_test(SYSCTL_HANDLER_ARGS)
278 struct spinlock spin;
283 if ((error = priv_check(curthread, PRIV_ROOT)) != 0)
285 if ((error = SYSCTL_IN(req, &value, sizeof(value))) != 0)
289 * Indefinite wait test
293 spin_lock(&spin); /* force an indefinite wait */
294 spin_lock_test_mode = 1;
296 spin_unlock(&spin); /* Clean up the spinlock count */
298 spin_lock_test_mode = 0;
302 * Time best-case exclusive spinlocks
305 globaldata_t gd = mycpu;
308 for (i = spin_test_count; i > 0; --i) {
309 spin_lock_quick(gd, &spin);
310 spin_unlock_quick(gd, &spin);
317 SYSCTL_PROC(_debug, KERN_PROC_ALL, spin_lock_test, CTLFLAG_RW|CTLTYPE_INT,
318 0, 0, sysctl_spin_lock_test, "I", "Test spinlock wait code");
320 #endif /* INVARIANTS */