xref: /freebsd/sys/kern/subr_lock.c (revision 8a16fb47303ef4bfc773b52a920e0b223f8e9c20)
183a81bcbSJohn Baldwin /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
483a81bcbSJohn Baldwin  * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
583a81bcbSJohn Baldwin  *
683a81bcbSJohn Baldwin  * Redistribution and use in source and binary forms, with or without
783a81bcbSJohn Baldwin  * modification, are permitted provided that the following conditions
883a81bcbSJohn Baldwin  * are met:
983a81bcbSJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
1083a81bcbSJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
1183a81bcbSJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
1283a81bcbSJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
1383a81bcbSJohn Baldwin  *    documentation and/or other materials provided with the distribution.
1483a81bcbSJohn Baldwin  *
1583a81bcbSJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1683a81bcbSJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1783a81bcbSJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1883a81bcbSJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1983a81bcbSJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2083a81bcbSJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2183a81bcbSJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2283a81bcbSJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2383a81bcbSJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2483a81bcbSJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2583a81bcbSJohn Baldwin  * SUCH DAMAGE.
2683a81bcbSJohn Baldwin  */
2783a81bcbSJohn Baldwin 
2883a81bcbSJohn Baldwin /*
2983a81bcbSJohn Baldwin  * This module holds the global variables and functions used to maintain
3083a81bcbSJohn Baldwin  * lock_object structures.
3183a81bcbSJohn Baldwin  */
3283a81bcbSJohn Baldwin 
3383a81bcbSJohn Baldwin #include <sys/cdefs.h>
3483a81bcbSJohn Baldwin __FBSDID("$FreeBSD$");
3583a81bcbSJohn Baldwin 
366ef970a9SJohn Baldwin #include "opt_ddb.h"
377c0435b9SKip Macy #include "opt_mprof.h"
386ef970a9SJohn Baldwin 
3983a81bcbSJohn Baldwin #include <sys/param.h>
4083a81bcbSJohn Baldwin #include <sys/systm.h>
41eea4f254SJeff Roberson #include <sys/kernel.h>
4283a81bcbSJohn Baldwin #include <sys/ktr.h>
43eac22dd4SMateusz Guzik #include <sys/limits.h>
4483a81bcbSJohn Baldwin #include <sys/lock.h>
457c0435b9SKip Macy #include <sys/lock_profile.h>
46eea4f254SJeff Roberson #include <sys/malloc.h>
472e6b8de4SJeff Roberson #include <sys/mutex.h>
48eea4f254SJeff Roberson #include <sys/pcpu.h>
49eea4f254SJeff Roberson #include <sys/proc.h>
50eea4f254SJeff Roberson #include <sys/sbuf.h>
512e6b8de4SJeff Roberson #include <sys/sched.h>
52eea4f254SJeff Roberson #include <sys/smp.h>
53eea4f254SJeff Roberson #include <sys/sysctl.h>
5483a81bcbSJohn Baldwin 
5583a81bcbSJohn Baldwin #ifdef DDB
5683a81bcbSJohn Baldwin #include <ddb/ddb.h>
5783a81bcbSJohn Baldwin #endif
5883a81bcbSJohn Baldwin 
59eea4f254SJeff Roberson #include <machine/cpufunc.h>
60eea4f254SJeff Roberson 
616a467cc5SMateusz Guzik /*
626a467cc5SMateusz Guzik  * Uncomment to validate that spin argument to acquire/release routines matches
636a467cc5SMateusz Guzik  * the flag in the lock
646a467cc5SMateusz Guzik  */
656a467cc5SMateusz Guzik //#define	LOCK_PROFILING_DEBUG_SPIN
666a467cc5SMateusz Guzik 
6783a81bcbSJohn Baldwin CTASSERT(LOCK_CLASS_MAX == 15);
6883a81bcbSJohn Baldwin 
6983a81bcbSJohn Baldwin struct lock_class *lock_classes[LOCK_CLASS_MAX + 1] = {
7083a81bcbSJohn Baldwin 	&lock_class_mtx_spin,
7183a81bcbSJohn Baldwin 	&lock_class_mtx_sleep,
7283a81bcbSJohn Baldwin 	&lock_class_sx,
73f53d15feSStephan Uphoff 	&lock_class_rm,
74cd32bd7aSJohn Baldwin 	&lock_class_rm_sleepable,
753f08bd8bSJohn Baldwin 	&lock_class_rw,
7661bd5e21SKip Macy 	&lock_class_lockmgr,
7783a81bcbSJohn Baldwin };
7883a81bcbSJohn Baldwin 
7983a81bcbSJohn Baldwin void
8083a81bcbSJohn Baldwin lock_init(struct lock_object *lock, struct lock_class *class, const char *name,
8183a81bcbSJohn Baldwin     const char *type, int flags)
8283a81bcbSJohn Baldwin {
8383a81bcbSJohn Baldwin 	int i;
8483a81bcbSJohn Baldwin 
8583a81bcbSJohn Baldwin 	/* Check for double-init and zero object. */
86fd07ddcfSDmitry Chagin 	KASSERT(flags & LO_NEW || !lock_initialized(lock),
87fd07ddcfSDmitry Chagin 	    ("lock \"%s\" %p already initialized", name, lock));
8883a81bcbSJohn Baldwin 
8983a81bcbSJohn Baldwin 	/* Look up lock class to find its index. */
9083a81bcbSJohn Baldwin 	for (i = 0; i < LOCK_CLASS_MAX; i++)
9183a81bcbSJohn Baldwin 		if (lock_classes[i] == class) {
9283a81bcbSJohn Baldwin 			lock->lo_flags = i << LO_CLASSSHIFT;
9383a81bcbSJohn Baldwin 			break;
9483a81bcbSJohn Baldwin 		}
9583a81bcbSJohn Baldwin 	KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class));
9683a81bcbSJohn Baldwin 
9783a81bcbSJohn Baldwin 	/* Initialize the lock object. */
9883a81bcbSJohn Baldwin 	lock->lo_name = name;
9983a81bcbSJohn Baldwin 	lock->lo_flags |= flags | LO_INITIALIZED;
10083a81bcbSJohn Baldwin 	LOCK_LOG_INIT(lock, 0);
10190356491SAttilio Rao 	WITNESS_INIT(lock, (type != NULL) ? type : name);
10283a81bcbSJohn Baldwin }
10383a81bcbSJohn Baldwin 
10483a81bcbSJohn Baldwin void
10583a81bcbSJohn Baldwin lock_destroy(struct lock_object *lock)
10683a81bcbSJohn Baldwin {
10783a81bcbSJohn Baldwin 
1083a6cdc4eSJohn-Mark Gurney 	KASSERT(lock_initialized(lock), ("lock %p is not initialized", lock));
10983a81bcbSJohn Baldwin 	WITNESS_DESTROY(lock);
11083a81bcbSJohn Baldwin 	LOCK_LOG_DESTROY(lock, 0);
11183a81bcbSJohn Baldwin 	lock->lo_flags &= ~LO_INITIALIZED;
11283a81bcbSJohn Baldwin }
11383a81bcbSJohn Baldwin 
1147029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, lock, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1157029da5cSPawel Biernacki     "lock debugging");
1167029da5cSPawel Biernacki static SYSCTL_NODE(_debug_lock, OID_AUTO, delay,
1177029da5cSPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1188e5a3e9aSMateusz Guzik     "lock delay");
1198e5a3e9aSMateusz Guzik 
1201ada9041SMateusz Guzik void
1211ada9041SMateusz Guzik lock_delay(struct lock_delay_arg *la)
1221ada9041SMateusz Guzik {
1231ada9041SMateusz Guzik 	struct lock_delay_config *lc = la->config;
124*8a16fb47SJonathan T. Looney 	u_int i;
1251ada9041SMateusz Guzik 
1263c798b2bSMateusz Guzik 	for (i = la->delay; i > 0; i--)
1271ada9041SMateusz Guzik 		cpu_spinwait();
1288e5a3e9aSMateusz Guzik 	la->spin_cnt += la->delay;
1297f6157f7SEdward Tomasz Napierala 
1307f6157f7SEdward Tomasz Napierala 	la->delay <<= 1;
131*8a16fb47SJonathan T. Looney 	if (__predict_false(la->delay > (u_int)lc->max))
1327f6157f7SEdward Tomasz Napierala 		la->delay = lc->max;
1338e5a3e9aSMateusz Guzik }
1348e5a3e9aSMateusz Guzik 
1358e5a3e9aSMateusz Guzik static u_int
1368e5a3e9aSMateusz Guzik lock_roundup_2(u_int val)
1378e5a3e9aSMateusz Guzik {
1388e5a3e9aSMateusz Guzik 	u_int res;
1398e5a3e9aSMateusz Guzik 
1408e5a3e9aSMateusz Guzik 	for (res = 1; res <= val; res <<= 1)
1418e5a3e9aSMateusz Guzik 		continue;
1428e5a3e9aSMateusz Guzik 
1438e5a3e9aSMateusz Guzik 	return (res);
1448e5a3e9aSMateusz Guzik }
1458e5a3e9aSMateusz Guzik 
1468e5a3e9aSMateusz Guzik void
1478e5a3e9aSMateusz Guzik lock_delay_default_init(struct lock_delay_config *lc)
1488e5a3e9aSMateusz Guzik {
1498e5a3e9aSMateusz Guzik 
150a045941bSMateusz Guzik 	lc->base = 1;
151*8a16fb47SJonathan T. Looney 	lc->max = min(lock_roundup_2(mp_ncpus) * 256, SHRT_MAX);
1521ada9041SMateusz Guzik }
1531ada9041SMateusz Guzik 
1542e77cad1SMateusz Guzik struct lock_delay_config __read_frequently locks_delay;
1552e77cad1SMateusz Guzik u_short __read_frequently locks_delay_retries;
1562e77cad1SMateusz Guzik u_short __read_frequently locks_delay_loops;
1572e77cad1SMateusz Guzik 
1582e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_base, CTLFLAG_RW, &locks_delay.base,
1592e77cad1SMateusz Guzik     0, "");
1602e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_max, CTLFLAG_RW, &locks_delay.max,
1612e77cad1SMateusz Guzik     0, "");
1622e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_retries, CTLFLAG_RW, &locks_delay_retries,
1632e77cad1SMateusz Guzik     0, "");
1642e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_loops, CTLFLAG_RW, &locks_delay_loops,
1652e77cad1SMateusz Guzik     0, "");
1662e77cad1SMateusz Guzik 
1672e77cad1SMateusz Guzik static void
1682e77cad1SMateusz Guzik locks_delay_init(void *arg __unused)
1692e77cad1SMateusz Guzik {
1702e77cad1SMateusz Guzik 
1712e77cad1SMateusz Guzik 	lock_delay_default_init(&locks_delay);
1722e77cad1SMateusz Guzik 	locks_delay_retries = 10;
1732e77cad1SMateusz Guzik 	locks_delay_loops = max(10000, locks_delay.max);
1742e77cad1SMateusz Guzik }
1752e77cad1SMateusz Guzik LOCK_DELAY_SYSINIT(locks_delay_init);
1762e77cad1SMateusz Guzik 
17783a81bcbSJohn Baldwin #ifdef DDB
17883a81bcbSJohn Baldwin DB_SHOW_COMMAND(lock, db_show_lock)
17983a81bcbSJohn Baldwin {
18083a81bcbSJohn Baldwin 	struct lock_object *lock;
18183a81bcbSJohn Baldwin 	struct lock_class *class;
18283a81bcbSJohn Baldwin 
18383a81bcbSJohn Baldwin 	if (!have_addr)
18483a81bcbSJohn Baldwin 		return;
18583a81bcbSJohn Baldwin 	lock = (struct lock_object *)addr;
18683a81bcbSJohn Baldwin 	if (LO_CLASSINDEX(lock) > LOCK_CLASS_MAX) {
18783a81bcbSJohn Baldwin 		db_printf("Unknown lock class: %d\n", LO_CLASSINDEX(lock));
18883a81bcbSJohn Baldwin 		return;
18983a81bcbSJohn Baldwin 	}
19083a81bcbSJohn Baldwin 	class = LOCK_CLASS(lock);
19183a81bcbSJohn Baldwin 	db_printf(" class: %s\n", class->lc_name);
19283a81bcbSJohn Baldwin 	db_printf(" name: %s\n", lock->lo_name);
19383a81bcbSJohn Baldwin 	class->lc_ddb_show(lock);
19483a81bcbSJohn Baldwin }
19583a81bcbSJohn Baldwin #endif
1967c0435b9SKip Macy 
1977c0435b9SKip Macy #ifdef LOCK_PROFILING
198eea4f254SJeff Roberson 
199eea4f254SJeff Roberson /*
200eea4f254SJeff Roberson  * One object per-thread for each lock the thread owns.  Tracks individual
201eea4f254SJeff Roberson  * lock instances.
202eea4f254SJeff Roberson  */
203eea4f254SJeff Roberson struct lock_profile_object {
204eea4f254SJeff Roberson 	LIST_ENTRY(lock_profile_object) lpo_link;
205eea4f254SJeff Roberson 	struct lock_object *lpo_obj;
206eea4f254SJeff Roberson 	const char	*lpo_file;
207eea4f254SJeff Roberson 	int		lpo_line;
208eea4f254SJeff Roberson 	uint16_t	lpo_ref;
209eea4f254SJeff Roberson 	uint16_t	lpo_cnt;
21060ae52f7SEd Schouten 	uint64_t	lpo_acqtime;
21160ae52f7SEd Schouten 	uint64_t	lpo_waittime;
212eea4f254SJeff Roberson 	u_int		lpo_contest_locking;
213eea4f254SJeff Roberson };
214eea4f254SJeff Roberson 
215eea4f254SJeff Roberson /*
216eea4f254SJeff Roberson  * One lock_prof for each (file, line, lock object) triple.
217eea4f254SJeff Roberson  */
218eea4f254SJeff Roberson struct lock_prof {
219eea4f254SJeff Roberson 	SLIST_ENTRY(lock_prof) link;
2200c66dc67SJeff Roberson 	struct lock_class *class;
221eea4f254SJeff Roberson 	const char	*file;
222eea4f254SJeff Roberson 	const char	*name;
223eea4f254SJeff Roberson 	int		line;
224eea4f254SJeff Roberson 	int		ticks;
225947265b6SKip Macy 	uintmax_t	cnt_wait_max;
226eea4f254SJeff Roberson 	uintmax_t	cnt_max;
227eea4f254SJeff Roberson 	uintmax_t	cnt_tot;
228eea4f254SJeff Roberson 	uintmax_t	cnt_wait;
229eea4f254SJeff Roberson 	uintmax_t	cnt_cur;
230eea4f254SJeff Roberson 	uintmax_t	cnt_contest_locking;
231eea4f254SJeff Roberson };
232eea4f254SJeff Roberson 
233eea4f254SJeff Roberson SLIST_HEAD(lphead, lock_prof);
234eea4f254SJeff Roberson 
235eea4f254SJeff Roberson #define	LPROF_HASH_SIZE		4096
236eea4f254SJeff Roberson #define	LPROF_HASH_MASK		(LPROF_HASH_SIZE - 1)
237eea4f254SJeff Roberson #define	LPROF_CACHE_SIZE	4096
238eea4f254SJeff Roberson 
239eea4f254SJeff Roberson /*
240eea4f254SJeff Roberson  * Array of objects and profs for each type of object for each cpu.  Spinlocks
241b1ce21c6SRebecca Cran  * are handled separately because a thread may be preempted and acquire a
242eea4f254SJeff Roberson  * spinlock while in the lock profiling code of a non-spinlock.  In this way
243eea4f254SJeff Roberson  * we only need a critical section to protect the per-cpu lists.
244eea4f254SJeff Roberson  */
245eea4f254SJeff Roberson struct lock_prof_type {
246eea4f254SJeff Roberson 	struct lphead		lpt_lpalloc;
247eea4f254SJeff Roberson 	struct lpohead		lpt_lpoalloc;
248eea4f254SJeff Roberson 	struct lphead		lpt_hash[LPROF_HASH_SIZE];
249eea4f254SJeff Roberson 	struct lock_prof	lpt_prof[LPROF_CACHE_SIZE];
250eea4f254SJeff Roberson 	struct lock_profile_object lpt_objs[LPROF_CACHE_SIZE];
251eea4f254SJeff Roberson };
252eea4f254SJeff Roberson 
253eea4f254SJeff Roberson struct lock_prof_cpu {
254eea4f254SJeff Roberson 	struct lock_prof_type	lpc_types[2]; /* One for spin one for other. */
255eea4f254SJeff Roberson };
256eea4f254SJeff Roberson 
257d2be3ef0SMateusz Guzik DPCPU_DEFINE_STATIC(struct lock_prof_cpu, lp);
258d2be3ef0SMateusz Guzik #define	LP_CPU_SELF	(DPCPU_PTR(lp))
259d2be3ef0SMateusz Guzik #define	LP_CPU(cpu)	(DPCPU_ID_PTR((cpu), lp))
260eea4f254SJeff Roberson 
26129051116SMateusz Guzik volatile int __read_mostly lock_prof_enable;
262a0842e69SMateusz Guzik int __read_mostly lock_contested_only;
2632e6b8de4SJeff Roberson static volatile int lock_prof_resetting;
264eea4f254SJeff Roberson 
2654e657159SMatthew D Fleming #define LPROF_SBUF_SIZE		256
266eea4f254SJeff Roberson 
267eea4f254SJeff Roberson static int lock_prof_rejected;
268eea4f254SJeff Roberson static int lock_prof_skipspin;
269eea4f254SJeff Roberson 
270eea4f254SJeff Roberson #ifndef USE_CPU_NANOSECONDS
27160ae52f7SEd Schouten uint64_t
272eea4f254SJeff Roberson nanoseconds(void)
2737c0435b9SKip Macy {
274eea4f254SJeff Roberson 	struct bintime bt;
27560ae52f7SEd Schouten 	uint64_t ns;
2767c0435b9SKip Macy 
277eea4f254SJeff Roberson 	binuptime(&bt);
278eea4f254SJeff Roberson 	/* From bintime2timespec */
27960ae52f7SEd Schouten 	ns = bt.sec * (uint64_t)1000000000;
280eea4f254SJeff Roberson 	ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
281eea4f254SJeff Roberson 	return (ns);
282eea4f254SJeff Roberson }
283eea4f254SJeff Roberson #endif
284fe68a916SKip Macy 
285eea4f254SJeff Roberson static void
286eea4f254SJeff Roberson lock_prof_init_type(struct lock_prof_type *type)
287eea4f254SJeff Roberson {
288eea4f254SJeff Roberson 	int i;
289fe68a916SKip Macy 
290eea4f254SJeff Roberson 	SLIST_INIT(&type->lpt_lpalloc);
291eea4f254SJeff Roberson 	LIST_INIT(&type->lpt_lpoalloc);
292eea4f254SJeff Roberson 	for (i = 0; i < LPROF_CACHE_SIZE; i++) {
293eea4f254SJeff Roberson 		SLIST_INSERT_HEAD(&type->lpt_lpalloc, &type->lpt_prof[i],
294eea4f254SJeff Roberson 		    link);
295eea4f254SJeff Roberson 		LIST_INSERT_HEAD(&type->lpt_lpoalloc, &type->lpt_objs[i],
296eea4f254SJeff Roberson 		    lpo_link);
297eea4f254SJeff Roberson 	}
298eea4f254SJeff Roberson }
299eea4f254SJeff Roberson 
300eea4f254SJeff Roberson static void
301eea4f254SJeff Roberson lock_prof_init(void *arg)
302eea4f254SJeff Roberson {
303eea4f254SJeff Roberson 	int cpu;
304eea4f254SJeff Roberson 
305cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
306d2be3ef0SMateusz Guzik 		lock_prof_init_type(&LP_CPU(cpu)->lpc_types[0]);
307d2be3ef0SMateusz Guzik 		lock_prof_init_type(&LP_CPU(cpu)->lpc_types[1]);
308eea4f254SJeff Roberson 	}
309eea4f254SJeff Roberson }
310eea4f254SJeff Roberson SYSINIT(lockprof, SI_SUB_SMP, SI_ORDER_ANY, lock_prof_init, NULL);
311eea4f254SJeff Roberson 
3122e6b8de4SJeff Roberson static void
3132e6b8de4SJeff Roberson lock_prof_reset_wait(void)
3142e6b8de4SJeff Roberson {
3152e6b8de4SJeff Roberson 
3162e6b8de4SJeff Roberson 	/*
31728d91af3SJeff Roberson 	 * Spin relinquishing our cpu so that quiesce_all_cpus may
31828d91af3SJeff Roberson 	 * complete.
3192e6b8de4SJeff Roberson 	 */
3202e6b8de4SJeff Roberson 	while (lock_prof_resetting)
3212e6b8de4SJeff Roberson 		sched_relinquish(curthread);
3222e6b8de4SJeff Roberson }
3232e6b8de4SJeff Roberson 
324eea4f254SJeff Roberson static void
325eea4f254SJeff Roberson lock_prof_reset(void)
326eea4f254SJeff Roberson {
327eea4f254SJeff Roberson 	struct lock_prof_cpu *lpc;
328eea4f254SJeff Roberson 	int enabled, i, cpu;
329eea4f254SJeff Roberson 
3302e6b8de4SJeff Roberson 	/*
3312e6b8de4SJeff Roberson 	 * We not only race with acquiring and releasing locks but also
3322e6b8de4SJeff Roberson 	 * thread exit.  To be certain that threads exit without valid head
3332e6b8de4SJeff Roberson 	 * pointers they must see resetting set before enabled is cleared.
3342e6b8de4SJeff Roberson 	 * Otherwise a lock may not be removed from a per-thread list due
3352e6b8de4SJeff Roberson 	 * to disabled being set but not wait for reset() to remove it below.
3362e6b8de4SJeff Roberson 	 */
3372e6b8de4SJeff Roberson 	atomic_store_rel_int(&lock_prof_resetting, 1);
338eea4f254SJeff Roberson 	enabled = lock_prof_enable;
339eea4f254SJeff Roberson 	lock_prof_enable = 0;
3403ac2ac2eSMateusz Guzik 	/*
3413ac2ac2eSMateusz Guzik 	 * This both publishes lock_prof_enable as disabled and makes sure
3423ac2ac2eSMateusz Guzik 	 * everyone else reads it if they are not far enough. We wait for the
3433ac2ac2eSMateusz Guzik 	 * rest down below.
3443ac2ac2eSMateusz Guzik 	 */
3453ac2ac2eSMateusz Guzik 	cpus_fence_seq_cst();
3463ac2ac2eSMateusz Guzik 	quiesce_all_critical();
3472e6b8de4SJeff Roberson 	/*
3482e6b8de4SJeff Roberson 	 * Some objects may have migrated between CPUs.  Clear all links
3492e6b8de4SJeff Roberson 	 * before we zero the structures.  Some items may still be linked
3502e6b8de4SJeff Roberson 	 * into per-thread lists as well.
3512e6b8de4SJeff Roberson 	 */
352cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
353d2be3ef0SMateusz Guzik 		lpc = LP_CPU(cpu);
354eea4f254SJeff Roberson 		for (i = 0; i < LPROF_CACHE_SIZE; i++) {
355eea4f254SJeff Roberson 			LIST_REMOVE(&lpc->lpc_types[0].lpt_objs[i], lpo_link);
356eea4f254SJeff Roberson 			LIST_REMOVE(&lpc->lpc_types[1].lpt_objs[i], lpo_link);
357eea4f254SJeff Roberson 		}
3582e6b8de4SJeff Roberson 	}
359cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
360d2be3ef0SMateusz Guzik 		lpc = LP_CPU(cpu);
361eea4f254SJeff Roberson 		bzero(lpc, sizeof(*lpc));
362eea4f254SJeff Roberson 		lock_prof_init_type(&lpc->lpc_types[0]);
363eea4f254SJeff Roberson 		lock_prof_init_type(&lpc->lpc_types[1]);
364eea4f254SJeff Roberson 	}
3653ac2ac2eSMateusz Guzik 	/*
3663ac2ac2eSMateusz Guzik 	 * Paired with the fence from cpus_fence_seq_cst()
3673ac2ac2eSMateusz Guzik 	 */
3682e6b8de4SJeff Roberson 	atomic_store_rel_int(&lock_prof_resetting, 0);
369eea4f254SJeff Roberson 	lock_prof_enable = enabled;
370eea4f254SJeff Roberson }
371eea4f254SJeff Roberson 
372eea4f254SJeff Roberson static void
373eea4f254SJeff Roberson lock_prof_output(struct lock_prof *lp, struct sbuf *sb)
374eea4f254SJeff Roberson {
375eea4f254SJeff Roberson 	const char *p;
376eea4f254SJeff Roberson 
377eea4f254SJeff Roberson 	for (p = lp->file; p != NULL && strncmp(p, "../", 3) == 0; p += 3);
378eea4f254SJeff Roberson 	sbuf_printf(sb,
379947265b6SKip Macy 	    "%8ju %9ju %11ju %11ju %11ju %6ju %6ju %2ju %6ju %s:%d (%s:%s)\n",
380947265b6SKip Macy 	    lp->cnt_max / 1000, lp->cnt_wait_max / 1000, lp->cnt_tot / 1000,
381eea4f254SJeff Roberson 	    lp->cnt_wait / 1000, lp->cnt_cur,
382eea4f254SJeff Roberson 	    lp->cnt_cur == 0 ? (uintmax_t)0 :
383eea4f254SJeff Roberson 	    lp->cnt_tot / (lp->cnt_cur * 1000),
384eea4f254SJeff Roberson 	    lp->cnt_cur == 0 ? (uintmax_t)0 :
385eea4f254SJeff Roberson 	    lp->cnt_wait / (lp->cnt_cur * 1000),
386eea4f254SJeff Roberson 	    (uintmax_t)0, lp->cnt_contest_locking,
3870c66dc67SJeff Roberson 	    p, lp->line, lp->class->lc_name, lp->name);
388eea4f254SJeff Roberson }
389eea4f254SJeff Roberson 
390eea4f254SJeff Roberson static void
391eea4f254SJeff Roberson lock_prof_sum(struct lock_prof *match, struct lock_prof *dst, int hash,
392eea4f254SJeff Roberson     int spin, int t)
393eea4f254SJeff Roberson {
394eea4f254SJeff Roberson 	struct lock_prof_type *type;
395eea4f254SJeff Roberson 	struct lock_prof *l;
396eea4f254SJeff Roberson 	int cpu;
397eea4f254SJeff Roberson 
398eea4f254SJeff Roberson 	dst->file = match->file;
399eea4f254SJeff Roberson 	dst->line = match->line;
4000c66dc67SJeff Roberson 	dst->class = match->class;
401eea4f254SJeff Roberson 	dst->name = match->name;
402eea4f254SJeff Roberson 
403cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
404d2be3ef0SMateusz Guzik 		type = &LP_CPU(cpu)->lpc_types[spin];
405eea4f254SJeff Roberson 		SLIST_FOREACH(l, &type->lpt_hash[hash], link) {
406eea4f254SJeff Roberson 			if (l->ticks == t)
407eea4f254SJeff Roberson 				continue;
408eea4f254SJeff Roberson 			if (l->file != match->file || l->line != match->line ||
4090c66dc67SJeff Roberson 			    l->name != match->name)
410eea4f254SJeff Roberson 				continue;
411eea4f254SJeff Roberson 			l->ticks = t;
412eea4f254SJeff Roberson 			if (l->cnt_max > dst->cnt_max)
413eea4f254SJeff Roberson 				dst->cnt_max = l->cnt_max;
414947265b6SKip Macy 			if (l->cnt_wait_max > dst->cnt_wait_max)
415947265b6SKip Macy 				dst->cnt_wait_max = l->cnt_wait_max;
416eea4f254SJeff Roberson 			dst->cnt_tot += l->cnt_tot;
417eea4f254SJeff Roberson 			dst->cnt_wait += l->cnt_wait;
418eea4f254SJeff Roberson 			dst->cnt_cur += l->cnt_cur;
419eea4f254SJeff Roberson 			dst->cnt_contest_locking += l->cnt_contest_locking;
420eea4f254SJeff Roberson 		}
421eea4f254SJeff Roberson 	}
422eea4f254SJeff Roberson }
423eea4f254SJeff Roberson 
424eea4f254SJeff Roberson static void
425eea4f254SJeff Roberson lock_prof_type_stats(struct lock_prof_type *type, struct sbuf *sb, int spin,
426eea4f254SJeff Roberson     int t)
427eea4f254SJeff Roberson {
428eea4f254SJeff Roberson 	struct lock_prof *l;
429eea4f254SJeff Roberson 	int i;
430eea4f254SJeff Roberson 
431eea4f254SJeff Roberson 	for (i = 0; i < LPROF_HASH_SIZE; ++i) {
432eea4f254SJeff Roberson 		SLIST_FOREACH(l, &type->lpt_hash[i], link) {
433eea4f254SJeff Roberson 			struct lock_prof lp = {};
434eea4f254SJeff Roberson 
435eea4f254SJeff Roberson 			if (l->ticks == t)
436eea4f254SJeff Roberson 				continue;
437eea4f254SJeff Roberson 			lock_prof_sum(l, &lp, i, spin, t);
438eea4f254SJeff Roberson 			lock_prof_output(&lp, sb);
439eea4f254SJeff Roberson 		}
440eea4f254SJeff Roberson 	}
441eea4f254SJeff Roberson }
442eea4f254SJeff Roberson 
443eea4f254SJeff Roberson static int
444eea4f254SJeff Roberson dump_lock_prof_stats(SYSCTL_HANDLER_ARGS)
445eea4f254SJeff Roberson {
446eea4f254SJeff Roberson 	struct sbuf *sb;
447eea4f254SJeff Roberson 	int error, cpu, t;
4480c66dc67SJeff Roberson 	int enabled;
449eea4f254SJeff Roberson 
45000f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
45100f0e671SMatthew D Fleming 	if (error != 0)
45200f0e671SMatthew D Fleming 		return (error);
4534e657159SMatthew D Fleming 	sb = sbuf_new_for_sysctl(NULL, NULL, LPROF_SBUF_SIZE, req);
454947265b6SKip Macy 	sbuf_printf(sb, "\n%8s %9s %11s %11s %11s %6s %6s %2s %6s %s\n",
455947265b6SKip Macy 	    "max", "wait_max", "total", "wait_total", "count", "avg", "wait_avg", "cnt_hold", "cnt_lock", "name");
4560c66dc67SJeff Roberson 	enabled = lock_prof_enable;
4570c66dc67SJeff Roberson 	lock_prof_enable = 0;
4583ac2ac2eSMateusz Guzik 	/*
4593ac2ac2eSMateusz Guzik 	 * See the comment in lock_prof_reset
4603ac2ac2eSMateusz Guzik 	 */
4613ac2ac2eSMateusz Guzik 	cpus_fence_seq_cst();
4623ac2ac2eSMateusz Guzik 	quiesce_all_critical();
463eea4f254SJeff Roberson 	t = ticks;
464cbba2cb3SMateusz Guzik 	CPU_FOREACH(cpu) {
465d2be3ef0SMateusz Guzik 		lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[0], sb, 0, t);
466d2be3ef0SMateusz Guzik 		lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[1], sb, 1, t);
467eea4f254SJeff Roberson 	}
4683ac2ac2eSMateusz Guzik 	atomic_thread_fence_rel();
4690c66dc67SJeff Roberson 	lock_prof_enable = enabled;
470eea4f254SJeff Roberson 
4714e657159SMatthew D Fleming 	error = sbuf_finish(sb);
4724e657159SMatthew D Fleming 	/* Output a trailing NUL. */
4734e657159SMatthew D Fleming 	if (error == 0)
4744e657159SMatthew D Fleming 		error = SYSCTL_OUT(req, "", 1);
475eea4f254SJeff Roberson 	sbuf_delete(sb);
476eea4f254SJeff Roberson 	return (error);
477eea4f254SJeff Roberson }
478eea4f254SJeff Roberson 
479eea4f254SJeff Roberson static int
480eea4f254SJeff Roberson enable_lock_prof(SYSCTL_HANDLER_ARGS)
481eea4f254SJeff Roberson {
482eea4f254SJeff Roberson 	int error, v;
483eea4f254SJeff Roberson 
484eea4f254SJeff Roberson 	v = lock_prof_enable;
485eea4f254SJeff Roberson 	error = sysctl_handle_int(oidp, &v, v, req);
486eea4f254SJeff Roberson 	if (error)
487eea4f254SJeff Roberson 		return (error);
488eea4f254SJeff Roberson 	if (req->newptr == NULL)
489eea4f254SJeff Roberson 		return (error);
490eea4f254SJeff Roberson 	if (v == lock_prof_enable)
491eea4f254SJeff Roberson 		return (0);
492eea4f254SJeff Roberson 	if (v == 1)
493eea4f254SJeff Roberson 		lock_prof_reset();
494eea4f254SJeff Roberson 	lock_prof_enable = !!v;
495eea4f254SJeff Roberson 
496eea4f254SJeff Roberson 	return (0);
497eea4f254SJeff Roberson }
498eea4f254SJeff Roberson 
499eea4f254SJeff Roberson static int
500eea4f254SJeff Roberson reset_lock_prof_stats(SYSCTL_HANDLER_ARGS)
501eea4f254SJeff Roberson {
502eea4f254SJeff Roberson 	int error, v;
503eea4f254SJeff Roberson 
504eea4f254SJeff Roberson 	v = 0;
505eea4f254SJeff Roberson 	error = sysctl_handle_int(oidp, &v, 0, req);
506eea4f254SJeff Roberson 	if (error)
507eea4f254SJeff Roberson 		return (error);
508eea4f254SJeff Roberson 	if (req->newptr == NULL)
509eea4f254SJeff Roberson 		return (error);
510eea4f254SJeff Roberson 	if (v == 0)
511eea4f254SJeff Roberson 		return (0);
512eea4f254SJeff Roberson 	lock_prof_reset();
513eea4f254SJeff Roberson 
514eea4f254SJeff Roberson 	return (0);
515eea4f254SJeff Roberson }
516eea4f254SJeff Roberson 
517eea4f254SJeff Roberson static struct lock_prof *
518eea4f254SJeff Roberson lock_profile_lookup(struct lock_object *lo, int spin, const char *file,
519eea4f254SJeff Roberson     int line)
520eea4f254SJeff Roberson {
521eea4f254SJeff Roberson 	const char *unknown = "(unknown)";
522eea4f254SJeff Roberson 	struct lock_prof_type *type;
523eea4f254SJeff Roberson 	struct lock_prof *lp;
524eea4f254SJeff Roberson 	struct lphead *head;
525eea4f254SJeff Roberson 	const char *p;
526eea4f254SJeff Roberson 	u_int hash;
527eea4f254SJeff Roberson 
528eea4f254SJeff Roberson 	p = file;
529eea4f254SJeff Roberson 	if (p == NULL || *p == '\0')
530eea4f254SJeff Roberson 		p = unknown;
531eea4f254SJeff Roberson 	hash = (uintptr_t)lo->lo_name * 31 + (uintptr_t)p * 31 + line;
532eea4f254SJeff Roberson 	hash &= LPROF_HASH_MASK;
533d2be3ef0SMateusz Guzik 	type = &LP_CPU_SELF->lpc_types[spin];
534eea4f254SJeff Roberson 	head = &type->lpt_hash[hash];
535eea4f254SJeff Roberson 	SLIST_FOREACH(lp, head, link) {
536eea4f254SJeff Roberson 		if (lp->line == line && lp->file == p &&
537eea4f254SJeff Roberson 		    lp->name == lo->lo_name)
538eea4f254SJeff Roberson 			return (lp);
539eea4f254SJeff Roberson 	}
540eea4f254SJeff Roberson 	lp = SLIST_FIRST(&type->lpt_lpalloc);
541eea4f254SJeff Roberson 	if (lp == NULL) {
542eea4f254SJeff Roberson 		lock_prof_rejected++;
543eea4f254SJeff Roberson 		return (lp);
544eea4f254SJeff Roberson 	}
545eea4f254SJeff Roberson 	SLIST_REMOVE_HEAD(&type->lpt_lpalloc, link);
546eea4f254SJeff Roberson 	lp->file = p;
547eea4f254SJeff Roberson 	lp->line = line;
5480c66dc67SJeff Roberson 	lp->class = LOCK_CLASS(lo);
549eea4f254SJeff Roberson 	lp->name = lo->lo_name;
550eea4f254SJeff Roberson 	SLIST_INSERT_HEAD(&type->lpt_hash[hash], lp, link);
551eea4f254SJeff Roberson 	return (lp);
552eea4f254SJeff Roberson }
553eea4f254SJeff Roberson 
554eea4f254SJeff Roberson static struct lock_profile_object *
555eea4f254SJeff Roberson lock_profile_object_lookup(struct lock_object *lo, int spin, const char *file,
556eea4f254SJeff Roberson     int line)
557eea4f254SJeff Roberson {
558eea4f254SJeff Roberson 	struct lock_profile_object *l;
559eea4f254SJeff Roberson 	struct lock_prof_type *type;
560eea4f254SJeff Roberson 	struct lpohead *head;
561eea4f254SJeff Roberson 
562eea4f254SJeff Roberson 	head = &curthread->td_lprof[spin];
563eea4f254SJeff Roberson 	LIST_FOREACH(l, head, lpo_link)
564eea4f254SJeff Roberson 		if (l->lpo_obj == lo && l->lpo_file == file &&
565eea4f254SJeff Roberson 		    l->lpo_line == line)
566eea4f254SJeff Roberson 			return (l);
567d2be3ef0SMateusz Guzik 	type = &LP_CPU_SELF->lpc_types[spin];
568eea4f254SJeff Roberson 	l = LIST_FIRST(&type->lpt_lpoalloc);
569eea4f254SJeff Roberson 	if (l == NULL) {
570eea4f254SJeff Roberson 		lock_prof_rejected++;
571eea4f254SJeff Roberson 		return (NULL);
572eea4f254SJeff Roberson 	}
573eea4f254SJeff Roberson 	LIST_REMOVE(l, lpo_link);
574eea4f254SJeff Roberson 	l->lpo_obj = lo;
575eea4f254SJeff Roberson 	l->lpo_file = file;
576eea4f254SJeff Roberson 	l->lpo_line = line;
577eea4f254SJeff Roberson 	l->lpo_cnt = 0;
578eea4f254SJeff Roberson 	LIST_INSERT_HEAD(head, l, lpo_link);
579eea4f254SJeff Roberson 
580eea4f254SJeff Roberson 	return (l);
581eea4f254SJeff Roberson }
582eea4f254SJeff Roberson 
583eea4f254SJeff Roberson void
5846a467cc5SMateusz Guzik lock_profile_obtain_lock_success(struct lock_object *lo, bool spin,
5856a467cc5SMateusz Guzik     int contested, uint64_t waittime, const char *file, int line)
586eea4f254SJeff Roberson {
587eea4f254SJeff Roberson 	struct lock_profile_object *l;
5886a467cc5SMateusz Guzik 
5896a467cc5SMateusz Guzik #ifdef LOCK_PROFILING_DEBUG_SPIN
5906a467cc5SMateusz Guzik 	bool is_spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK);
5916a467cc5SMateusz Guzik 	if ((spin && !is_spin) || (!spin && is_spin))
5926a467cc5SMateusz Guzik 		printf("%s: lock %s spin mismatch (arg %d, flag %d)\n", __func__,
5936a467cc5SMateusz Guzik 		    lo->lo_name, spin, is_spin);
5946a467cc5SMateusz Guzik #endif
595eea4f254SJeff Roberson 
596eea4f254SJeff Roberson 	/* don't reset the timer when/if recursing */
597eea4f254SJeff Roberson 	if (!lock_prof_enable || (lo->lo_flags & LO_NOPROFILE))
598eea4f254SJeff Roberson 		return;
599a0842e69SMateusz Guzik 	if (lock_contested_only && !contested)
600a0842e69SMateusz Guzik 		return;
601eea4f254SJeff Roberson 	if (spin && lock_prof_skipspin == 1)
602eea4f254SJeff Roberson 		return;
603e2ab16b1SMateusz Guzik 
604e2ab16b1SMateusz Guzik 	if (SCHEDULER_STOPPED())
605e2ab16b1SMateusz Guzik 		return;
606e2ab16b1SMateusz Guzik 
6072e6b8de4SJeff Roberson 	critical_enter();
6082e6b8de4SJeff Roberson 	/* Recheck enabled now that we're in a critical section. */
6092e6b8de4SJeff Roberson 	if (lock_prof_enable == 0)
6102e6b8de4SJeff Roberson 		goto out;
611eea4f254SJeff Roberson 	l = lock_profile_object_lookup(lo, spin, file, line);
612eea4f254SJeff Roberson 	if (l == NULL)
6132e6b8de4SJeff Roberson 		goto out;
614eea4f254SJeff Roberson 	l->lpo_cnt++;
615eea4f254SJeff Roberson 	if (++l->lpo_ref > 1)
6162e6b8de4SJeff Roberson 		goto out;
617eea4f254SJeff Roberson 	l->lpo_contest_locking = contested;
6187c0435b9SKip Macy 	l->lpo_acqtime = nanoseconds();
619aa077979SKip Macy 	if (waittime && (l->lpo_acqtime > waittime))
6207c0435b9SKip Macy 		l->lpo_waittime = l->lpo_acqtime - waittime;
621aa077979SKip Macy 	else
622aa077979SKip Macy 		l->lpo_waittime = 0;
6232e6b8de4SJeff Roberson out:
6243ac2ac2eSMateusz Guzik 	/*
6253ac2ac2eSMateusz Guzik 	 * Paired with cpus_fence_seq_cst().
6263ac2ac2eSMateusz Guzik 	 */
6273ac2ac2eSMateusz Guzik 	atomic_thread_fence_rel();
6282e6b8de4SJeff Roberson 	critical_exit();
6292e6b8de4SJeff Roberson }
6302e6b8de4SJeff Roberson 
6312e6b8de4SJeff Roberson void
6322e6b8de4SJeff Roberson lock_profile_thread_exit(struct thread *td)
6332e6b8de4SJeff Roberson {
6342e6b8de4SJeff Roberson #ifdef INVARIANTS
6352e6b8de4SJeff Roberson 	struct lock_profile_object *l;
6362e6b8de4SJeff Roberson 
6372e6b8de4SJeff Roberson 	MPASS(curthread->td_critnest == 0);
6382e6b8de4SJeff Roberson #endif
6392e6b8de4SJeff Roberson 	/*
6402e6b8de4SJeff Roberson 	 * If lock profiling was disabled we have to wait for reset to
6412e6b8de4SJeff Roberson 	 * clear our pointers before we can exit safely.
6422e6b8de4SJeff Roberson 	 */
6432e6b8de4SJeff Roberson 	lock_prof_reset_wait();
6442e6b8de4SJeff Roberson #ifdef INVARIANTS
6452e6b8de4SJeff Roberson 	LIST_FOREACH(l, &td->td_lprof[0], lpo_link)
6462e6b8de4SJeff Roberson 		printf("thread still holds lock acquired at %s:%d\n",
6472e6b8de4SJeff Roberson 		    l->lpo_file, l->lpo_line);
6482e6b8de4SJeff Roberson 	LIST_FOREACH(l, &td->td_lprof[1], lpo_link)
6492e6b8de4SJeff Roberson 		printf("thread still holds lock acquired at %s:%d\n",
6502e6b8de4SJeff Roberson 		    l->lpo_file, l->lpo_line);
6512e6b8de4SJeff Roberson #endif
6522e6b8de4SJeff Roberson 	MPASS(LIST_FIRST(&td->td_lprof[0]) == NULL);
6532e6b8de4SJeff Roberson 	MPASS(LIST_FIRST(&td->td_lprof[1]) == NULL);
6547c0435b9SKip Macy }
6557c0435b9SKip Macy 
656eea4f254SJeff Roberson void
6576a467cc5SMateusz Guzik lock_profile_release_lock(struct lock_object *lo, bool spin)
6587c0435b9SKip Macy {
659eea4f254SJeff Roberson 	struct lock_profile_object *l;
660eea4f254SJeff Roberson 	struct lock_prof_type *type;
661eea4f254SJeff Roberson 	struct lock_prof *lp;
66260ae52f7SEd Schouten 	uint64_t curtime, holdtime;
663eea4f254SJeff Roberson 	struct lpohead *head;
6646a467cc5SMateusz Guzik 
6656a467cc5SMateusz Guzik #ifdef LOCK_PROFILING_DEBUG_SPIN
6666a467cc5SMateusz Guzik 	bool is_spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK);
6676a467cc5SMateusz Guzik 	if ((spin && !is_spin) || (!spin && is_spin))
6686a467cc5SMateusz Guzik 		printf("%s: lock %s spin mismatch (arg %d, flag %d)\n", __func__,
6696a467cc5SMateusz Guzik 		    lo->lo_name, spin, is_spin);
6706a467cc5SMateusz Guzik #endif
6717c0435b9SKip Macy 
6722e6b8de4SJeff Roberson 	if (lo->lo_flags & LO_NOPROFILE)
6737c0435b9SKip Macy 		return;
674eea4f254SJeff Roberson 	head = &curthread->td_lprof[spin];
6752e6b8de4SJeff Roberson 	if (LIST_FIRST(head) == NULL)
6762e6b8de4SJeff Roberson 		return;
677e2ab16b1SMateusz Guzik 	if (SCHEDULER_STOPPED())
678e2ab16b1SMateusz Guzik 		return;
679eea4f254SJeff Roberson 	critical_enter();
6802e6b8de4SJeff Roberson 	/* Recheck enabled now that we're in a critical section. */
6812e6b8de4SJeff Roberson 	if (lock_prof_enable == 0 && lock_prof_resetting == 1)
6822e6b8de4SJeff Roberson 		goto out;
6832e6b8de4SJeff Roberson 	/*
6842e6b8de4SJeff Roberson 	 * If lock profiling is not enabled we still want to remove the
6852e6b8de4SJeff Roberson 	 * lpo from our queue.
6862e6b8de4SJeff Roberson 	 */
687eea4f254SJeff Roberson 	LIST_FOREACH(l, head, lpo_link)
688eea4f254SJeff Roberson 		if (l->lpo_obj == lo)
6897c0435b9SKip Macy 			break;
690eea4f254SJeff Roberson 	if (l == NULL)
691eea4f254SJeff Roberson 		goto out;
692eea4f254SJeff Roberson 	if (--l->lpo_ref > 0)
693eea4f254SJeff Roberson 		goto out;
694eea4f254SJeff Roberson 	lp = lock_profile_lookup(lo, spin, l->lpo_file, l->lpo_line);
695eea4f254SJeff Roberson 	if (lp == NULL)
696eea4f254SJeff Roberson 		goto release;
697e7154e7eSAndriy Gapon 	curtime = nanoseconds();
698e7154e7eSAndriy Gapon 	if (curtime < l->lpo_acqtime)
699eea4f254SJeff Roberson 		goto release;
700e7154e7eSAndriy Gapon 	holdtime = curtime - l->lpo_acqtime;
701e7154e7eSAndriy Gapon 
7027c0435b9SKip Macy 	/*
70383b72e3eSKip Macy 	 * Record if the lock has been held longer now than ever
7047c0435b9SKip Macy 	 * before.
7057c0435b9SKip Macy 	 */
706eea4f254SJeff Roberson 	if (holdtime > lp->cnt_max)
707eea4f254SJeff Roberson 		lp->cnt_max = holdtime;
708947265b6SKip Macy 	if (l->lpo_waittime > lp->cnt_wait_max)
709947265b6SKip Macy 		lp->cnt_wait_max = l->lpo_waittime;
710eea4f254SJeff Roberson 	lp->cnt_tot += holdtime;
711eea4f254SJeff Roberson 	lp->cnt_wait += l->lpo_waittime;
712eea4f254SJeff Roberson 	lp->cnt_contest_locking += l->lpo_contest_locking;
713eea4f254SJeff Roberson 	lp->cnt_cur += l->lpo_cnt;
714eea4f254SJeff Roberson release:
715eea4f254SJeff Roberson 	LIST_REMOVE(l, lpo_link);
716d2be3ef0SMateusz Guzik 	type = &LP_CPU_SELF->lpc_types[spin];
717eea4f254SJeff Roberson 	LIST_INSERT_HEAD(&type->lpt_lpoalloc, l, lpo_link);
718eea4f254SJeff Roberson out:
7193ac2ac2eSMateusz Guzik 	/*
7203ac2ac2eSMateusz Guzik 	 * Paired with cpus_fence_seq_cst().
7213ac2ac2eSMateusz Guzik 	 */
7223ac2ac2eSMateusz Guzik 	atomic_thread_fence_rel();
723eea4f254SJeff Roberson 	critical_exit();
724eea4f254SJeff Roberson }
7257c0435b9SKip Macy 
7267029da5cSPawel Biernacki static SYSCTL_NODE(_debug_lock, OID_AUTO, prof,
7277029da5cSPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
7286472ac3dSEd Schouten     "lock profiling");
729eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipspin, CTLFLAG_RW,
730eea4f254SJeff Roberson     &lock_prof_skipspin, 0, "Skip profiling on spinlocks.");
731eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, rejected, CTLFLAG_RD,
732eea4f254SJeff Roberson     &lock_prof_rejected, 0, "Number of rejected profiling records");
733a0842e69SMateusz Guzik SYSCTL_INT(_debug_lock_prof, OID_AUTO, contested_only, CTLFLAG_RW,
734a0842e69SMateusz Guzik     &lock_contested_only, 0, "Only profile contested acquires");
7357029da5cSPawel Biernacki SYSCTL_PROC(_debug_lock_prof, OID_AUTO, stats,
7367029da5cSPawel Biernacki     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
7377029da5cSPawel Biernacki     dump_lock_prof_stats, "A",
7387029da5cSPawel Biernacki     "Lock profiling statistics");
7397029da5cSPawel Biernacki SYSCTL_PROC(_debug_lock_prof, OID_AUTO, reset,
7407029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
7417029da5cSPawel Biernacki     reset_lock_prof_stats, "I",
7427029da5cSPawel Biernacki     "Reset lock profiling statistics");
7437029da5cSPawel Biernacki SYSCTL_PROC(_debug_lock_prof, OID_AUTO, enable,
7447029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
7457029da5cSPawel Biernacki     enable_lock_prof, "I",
7467029da5cSPawel Biernacki     "Enable lock profiling");
747eea4f254SJeff Roberson 
7487c0435b9SKip Macy #endif
749