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 61*6a467cc5SMateusz Guzik /* 62*6a467cc5SMateusz Guzik * Uncomment to validate that spin argument to acquire/release routines matches 63*6a467cc5SMateusz Guzik * the flag in the lock 64*6a467cc5SMateusz Guzik */ 65*6a467cc5SMateusz Guzik //#define LOCK_PROFILING_DEBUG_SPIN 66*6a467cc5SMateusz Guzik 678e5a3e9aSMateusz Guzik SDT_PROVIDER_DEFINE(lock); 688e5a3e9aSMateusz Guzik SDT_PROBE_DEFINE1(lock, , , starvation, "u_int"); 698e5a3e9aSMateusz Guzik 7083a81bcbSJohn Baldwin CTASSERT(LOCK_CLASS_MAX == 15); 7183a81bcbSJohn Baldwin 7283a81bcbSJohn Baldwin struct lock_class *lock_classes[LOCK_CLASS_MAX + 1] = { 7383a81bcbSJohn Baldwin &lock_class_mtx_spin, 7483a81bcbSJohn Baldwin &lock_class_mtx_sleep, 7583a81bcbSJohn Baldwin &lock_class_sx, 76f53d15feSStephan Uphoff &lock_class_rm, 77cd32bd7aSJohn Baldwin &lock_class_rm_sleepable, 783f08bd8bSJohn Baldwin &lock_class_rw, 7961bd5e21SKip Macy &lock_class_lockmgr, 8083a81bcbSJohn Baldwin }; 8183a81bcbSJohn Baldwin 8283a81bcbSJohn Baldwin void 8383a81bcbSJohn Baldwin lock_init(struct lock_object *lock, struct lock_class *class, const char *name, 8483a81bcbSJohn Baldwin const char *type, int flags) 8583a81bcbSJohn Baldwin { 8683a81bcbSJohn Baldwin int i; 8783a81bcbSJohn Baldwin 8883a81bcbSJohn Baldwin /* Check for double-init and zero object. */ 89fd07ddcfSDmitry Chagin KASSERT(flags & LO_NEW || !lock_initialized(lock), 90fd07ddcfSDmitry Chagin ("lock \"%s\" %p already initialized", name, lock)); 9183a81bcbSJohn Baldwin 9283a81bcbSJohn Baldwin /* Look up lock class to find its index. */ 9383a81bcbSJohn Baldwin for (i = 0; i < LOCK_CLASS_MAX; i++) 9483a81bcbSJohn Baldwin if (lock_classes[i] == class) { 9583a81bcbSJohn Baldwin lock->lo_flags = i << LO_CLASSSHIFT; 9683a81bcbSJohn Baldwin break; 9783a81bcbSJohn Baldwin } 9883a81bcbSJohn Baldwin KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class)); 9983a81bcbSJohn Baldwin 10083a81bcbSJohn Baldwin /* Initialize the lock object. */ 10183a81bcbSJohn Baldwin lock->lo_name = name; 10283a81bcbSJohn Baldwin lock->lo_flags |= flags | LO_INITIALIZED; 10383a81bcbSJohn Baldwin LOCK_LOG_INIT(lock, 0); 10490356491SAttilio Rao WITNESS_INIT(lock, (type != NULL) ? type : name); 10583a81bcbSJohn Baldwin } 10683a81bcbSJohn Baldwin 10783a81bcbSJohn Baldwin void 10883a81bcbSJohn Baldwin lock_destroy(struct lock_object *lock) 10983a81bcbSJohn Baldwin { 11083a81bcbSJohn Baldwin 1113a6cdc4eSJohn-Mark Gurney KASSERT(lock_initialized(lock), ("lock %p is not initialized", lock)); 11283a81bcbSJohn Baldwin WITNESS_DESTROY(lock); 11383a81bcbSJohn Baldwin LOCK_LOG_DESTROY(lock, 0); 11483a81bcbSJohn Baldwin lock->lo_flags &= ~LO_INITIALIZED; 11583a81bcbSJohn Baldwin } 11683a81bcbSJohn Baldwin 1177029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, lock, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 1187029da5cSPawel Biernacki "lock debugging"); 1197029da5cSPawel Biernacki static SYSCTL_NODE(_debug_lock, OID_AUTO, delay, 1207029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 1218e5a3e9aSMateusz Guzik "lock delay"); 1228e5a3e9aSMateusz Guzik 1238e5a3e9aSMateusz Guzik static u_int __read_mostly starvation_limit = 131072; 1248e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_lock_delay, OID_AUTO, starvation_limit, CTLFLAG_RW, 1258e5a3e9aSMateusz Guzik &starvation_limit, 0, ""); 1268e5a3e9aSMateusz Guzik 1278e5a3e9aSMateusz Guzik static u_int __read_mostly restrict_starvation = 0; 1288e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_lock_delay, OID_AUTO, restrict_starvation, CTLFLAG_RW, 1298e5a3e9aSMateusz Guzik &restrict_starvation, 0, ""); 1308e5a3e9aSMateusz Guzik 1311ada9041SMateusz Guzik void 1321ada9041SMateusz Guzik lock_delay(struct lock_delay_arg *la) 1331ada9041SMateusz Guzik { 1341ada9041SMateusz Guzik struct lock_delay_config *lc = la->config; 1356b8dd26eSMateusz Guzik u_short i; 1361ada9041SMateusz Guzik 1373c798b2bSMateusz Guzik for (i = la->delay; i > 0; i--) 1381ada9041SMateusz Guzik cpu_spinwait(); 1398e5a3e9aSMateusz Guzik la->spin_cnt += la->delay; 1407f6157f7SEdward Tomasz Napierala 1417f6157f7SEdward Tomasz Napierala la->delay <<= 1; 1427f6157f7SEdward Tomasz Napierala if (__predict_false(la->delay > lc->max)) 1437f6157f7SEdward Tomasz Napierala la->delay = lc->max; 1447f6157f7SEdward Tomasz Napierala 1458e5a3e9aSMateusz Guzik if (__predict_false(la->spin_cnt > starvation_limit)) { 1468e5a3e9aSMateusz Guzik SDT_PROBE1(lock, , , starvation, la->delay); 1478e5a3e9aSMateusz Guzik if (restrict_starvation) 1488e5a3e9aSMateusz Guzik la->delay = lc->base; 1498e5a3e9aSMateusz Guzik } 1508e5a3e9aSMateusz Guzik } 1518e5a3e9aSMateusz Guzik 1528e5a3e9aSMateusz Guzik static u_int 1538e5a3e9aSMateusz Guzik lock_roundup_2(u_int val) 1548e5a3e9aSMateusz Guzik { 1558e5a3e9aSMateusz Guzik u_int res; 1568e5a3e9aSMateusz Guzik 1578e5a3e9aSMateusz Guzik for (res = 1; res <= val; res <<= 1) 1588e5a3e9aSMateusz Guzik continue; 1598e5a3e9aSMateusz Guzik 1608e5a3e9aSMateusz Guzik return (res); 1618e5a3e9aSMateusz Guzik } 1628e5a3e9aSMateusz Guzik 1638e5a3e9aSMateusz Guzik void 1648e5a3e9aSMateusz Guzik lock_delay_default_init(struct lock_delay_config *lc) 1658e5a3e9aSMateusz Guzik { 1668e5a3e9aSMateusz Guzik 167a045941bSMateusz Guzik lc->base = 1; 168a045941bSMateusz Guzik lc->max = lock_roundup_2(mp_ncpus) * 256; 169a045941bSMateusz Guzik if (lc->max > 32678) 170a045941bSMateusz Guzik lc->max = 32678; 1711ada9041SMateusz Guzik } 1721ada9041SMateusz Guzik 1732e77cad1SMateusz Guzik struct lock_delay_config __read_frequently locks_delay; 1742e77cad1SMateusz Guzik u_short __read_frequently locks_delay_retries; 1752e77cad1SMateusz Guzik u_short __read_frequently locks_delay_loops; 1762e77cad1SMateusz Guzik 1772e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_base, CTLFLAG_RW, &locks_delay.base, 1782e77cad1SMateusz Guzik 0, ""); 1792e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_max, CTLFLAG_RW, &locks_delay.max, 1802e77cad1SMateusz Guzik 0, ""); 1812e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_retries, CTLFLAG_RW, &locks_delay_retries, 1822e77cad1SMateusz Guzik 0, ""); 1832e77cad1SMateusz Guzik SYSCTL_U16(_debug_lock, OID_AUTO, delay_loops, CTLFLAG_RW, &locks_delay_loops, 1842e77cad1SMateusz Guzik 0, ""); 1852e77cad1SMateusz Guzik 1862e77cad1SMateusz Guzik static void 1872e77cad1SMateusz Guzik locks_delay_init(void *arg __unused) 1882e77cad1SMateusz Guzik { 1892e77cad1SMateusz Guzik 1902e77cad1SMateusz Guzik lock_delay_default_init(&locks_delay); 1912e77cad1SMateusz Guzik locks_delay_retries = 10; 1922e77cad1SMateusz Guzik locks_delay_loops = max(10000, locks_delay.max); 1932e77cad1SMateusz Guzik } 1942e77cad1SMateusz Guzik LOCK_DELAY_SYSINIT(locks_delay_init); 1952e77cad1SMateusz Guzik 19683a81bcbSJohn Baldwin #ifdef DDB 19783a81bcbSJohn Baldwin DB_SHOW_COMMAND(lock, db_show_lock) 19883a81bcbSJohn Baldwin { 19983a81bcbSJohn Baldwin struct lock_object *lock; 20083a81bcbSJohn Baldwin struct lock_class *class; 20183a81bcbSJohn Baldwin 20283a81bcbSJohn Baldwin if (!have_addr) 20383a81bcbSJohn Baldwin return; 20483a81bcbSJohn Baldwin lock = (struct lock_object *)addr; 20583a81bcbSJohn Baldwin if (LO_CLASSINDEX(lock) > LOCK_CLASS_MAX) { 20683a81bcbSJohn Baldwin db_printf("Unknown lock class: %d\n", LO_CLASSINDEX(lock)); 20783a81bcbSJohn Baldwin return; 20883a81bcbSJohn Baldwin } 20983a81bcbSJohn Baldwin class = LOCK_CLASS(lock); 21083a81bcbSJohn Baldwin db_printf(" class: %s\n", class->lc_name); 21183a81bcbSJohn Baldwin db_printf(" name: %s\n", lock->lo_name); 21283a81bcbSJohn Baldwin class->lc_ddb_show(lock); 21383a81bcbSJohn Baldwin } 21483a81bcbSJohn Baldwin #endif 2157c0435b9SKip Macy 2167c0435b9SKip Macy #ifdef LOCK_PROFILING 217eea4f254SJeff Roberson 218eea4f254SJeff Roberson /* 219eea4f254SJeff Roberson * One object per-thread for each lock the thread owns. Tracks individual 220eea4f254SJeff Roberson * lock instances. 221eea4f254SJeff Roberson */ 222eea4f254SJeff Roberson struct lock_profile_object { 223eea4f254SJeff Roberson LIST_ENTRY(lock_profile_object) lpo_link; 224eea4f254SJeff Roberson struct lock_object *lpo_obj; 225eea4f254SJeff Roberson const char *lpo_file; 226eea4f254SJeff Roberson int lpo_line; 227eea4f254SJeff Roberson uint16_t lpo_ref; 228eea4f254SJeff Roberson uint16_t lpo_cnt; 22960ae52f7SEd Schouten uint64_t lpo_acqtime; 23060ae52f7SEd Schouten uint64_t lpo_waittime; 231eea4f254SJeff Roberson u_int lpo_contest_locking; 232eea4f254SJeff Roberson }; 233eea4f254SJeff Roberson 234eea4f254SJeff Roberson /* 235eea4f254SJeff Roberson * One lock_prof for each (file, line, lock object) triple. 236eea4f254SJeff Roberson */ 237eea4f254SJeff Roberson struct lock_prof { 238eea4f254SJeff Roberson SLIST_ENTRY(lock_prof) link; 2390c66dc67SJeff Roberson struct lock_class *class; 240eea4f254SJeff Roberson const char *file; 241eea4f254SJeff Roberson const char *name; 242eea4f254SJeff Roberson int line; 243eea4f254SJeff Roberson int ticks; 244947265b6SKip Macy uintmax_t cnt_wait_max; 245eea4f254SJeff Roberson uintmax_t cnt_max; 246eea4f254SJeff Roberson uintmax_t cnt_tot; 247eea4f254SJeff Roberson uintmax_t cnt_wait; 248eea4f254SJeff Roberson uintmax_t cnt_cur; 249eea4f254SJeff Roberson uintmax_t cnt_contest_locking; 250eea4f254SJeff Roberson }; 251eea4f254SJeff Roberson 252eea4f254SJeff Roberson SLIST_HEAD(lphead, lock_prof); 253eea4f254SJeff Roberson 254eea4f254SJeff Roberson #define LPROF_HASH_SIZE 4096 255eea4f254SJeff Roberson #define LPROF_HASH_MASK (LPROF_HASH_SIZE - 1) 256eea4f254SJeff Roberson #define LPROF_CACHE_SIZE 4096 257eea4f254SJeff Roberson 258eea4f254SJeff Roberson /* 259eea4f254SJeff Roberson * Array of objects and profs for each type of object for each cpu. Spinlocks 260b1ce21c6SRebecca Cran * are handled separately because a thread may be preempted and acquire a 261eea4f254SJeff Roberson * spinlock while in the lock profiling code of a non-spinlock. In this way 262eea4f254SJeff Roberson * we only need a critical section to protect the per-cpu lists. 263eea4f254SJeff Roberson */ 264eea4f254SJeff Roberson struct lock_prof_type { 265eea4f254SJeff Roberson struct lphead lpt_lpalloc; 266eea4f254SJeff Roberson struct lpohead lpt_lpoalloc; 267eea4f254SJeff Roberson struct lphead lpt_hash[LPROF_HASH_SIZE]; 268eea4f254SJeff Roberson struct lock_prof lpt_prof[LPROF_CACHE_SIZE]; 269eea4f254SJeff Roberson struct lock_profile_object lpt_objs[LPROF_CACHE_SIZE]; 270eea4f254SJeff Roberson }; 271eea4f254SJeff Roberson 272eea4f254SJeff Roberson struct lock_prof_cpu { 273eea4f254SJeff Roberson struct lock_prof_type lpc_types[2]; /* One for spin one for other. */ 274eea4f254SJeff Roberson }; 275eea4f254SJeff Roberson 276d2be3ef0SMateusz Guzik DPCPU_DEFINE_STATIC(struct lock_prof_cpu, lp); 277d2be3ef0SMateusz Guzik #define LP_CPU_SELF (DPCPU_PTR(lp)) 278d2be3ef0SMateusz Guzik #define LP_CPU(cpu) (DPCPU_ID_PTR((cpu), lp)) 279eea4f254SJeff Roberson 28029051116SMateusz Guzik volatile int __read_mostly lock_prof_enable; 281a0842e69SMateusz Guzik int __read_mostly lock_contested_only; 2822e6b8de4SJeff Roberson static volatile int lock_prof_resetting; 283eea4f254SJeff Roberson 2844e657159SMatthew D Fleming #define LPROF_SBUF_SIZE 256 285eea4f254SJeff Roberson 286eea4f254SJeff Roberson static int lock_prof_rejected; 287eea4f254SJeff Roberson static int lock_prof_skipspin; 288eea4f254SJeff Roberson 289eea4f254SJeff Roberson #ifndef USE_CPU_NANOSECONDS 29060ae52f7SEd Schouten uint64_t 291eea4f254SJeff Roberson nanoseconds(void) 2927c0435b9SKip Macy { 293eea4f254SJeff Roberson struct bintime bt; 29460ae52f7SEd Schouten uint64_t ns; 2957c0435b9SKip Macy 296eea4f254SJeff Roberson binuptime(&bt); 297eea4f254SJeff Roberson /* From bintime2timespec */ 29860ae52f7SEd Schouten ns = bt.sec * (uint64_t)1000000000; 299eea4f254SJeff Roberson ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32; 300eea4f254SJeff Roberson return (ns); 301eea4f254SJeff Roberson } 302eea4f254SJeff Roberson #endif 303fe68a916SKip Macy 304eea4f254SJeff Roberson static void 305eea4f254SJeff Roberson lock_prof_init_type(struct lock_prof_type *type) 306eea4f254SJeff Roberson { 307eea4f254SJeff Roberson int i; 308fe68a916SKip Macy 309eea4f254SJeff Roberson SLIST_INIT(&type->lpt_lpalloc); 310eea4f254SJeff Roberson LIST_INIT(&type->lpt_lpoalloc); 311eea4f254SJeff Roberson for (i = 0; i < LPROF_CACHE_SIZE; i++) { 312eea4f254SJeff Roberson SLIST_INSERT_HEAD(&type->lpt_lpalloc, &type->lpt_prof[i], 313eea4f254SJeff Roberson link); 314eea4f254SJeff Roberson LIST_INSERT_HEAD(&type->lpt_lpoalloc, &type->lpt_objs[i], 315eea4f254SJeff Roberson lpo_link); 316eea4f254SJeff Roberson } 317eea4f254SJeff Roberson } 318eea4f254SJeff Roberson 319eea4f254SJeff Roberson static void 320eea4f254SJeff Roberson lock_prof_init(void *arg) 321eea4f254SJeff Roberson { 322eea4f254SJeff Roberson int cpu; 323eea4f254SJeff Roberson 324cbba2cb3SMateusz Guzik CPU_FOREACH(cpu) { 325d2be3ef0SMateusz Guzik lock_prof_init_type(&LP_CPU(cpu)->lpc_types[0]); 326d2be3ef0SMateusz Guzik lock_prof_init_type(&LP_CPU(cpu)->lpc_types[1]); 327eea4f254SJeff Roberson } 328eea4f254SJeff Roberson } 329eea4f254SJeff Roberson SYSINIT(lockprof, SI_SUB_SMP, SI_ORDER_ANY, lock_prof_init, NULL); 330eea4f254SJeff Roberson 3312e6b8de4SJeff Roberson static void 3322e6b8de4SJeff Roberson lock_prof_reset_wait(void) 3332e6b8de4SJeff Roberson { 3342e6b8de4SJeff Roberson 3352e6b8de4SJeff Roberson /* 33628d91af3SJeff Roberson * Spin relinquishing our cpu so that quiesce_all_cpus may 33728d91af3SJeff Roberson * complete. 3382e6b8de4SJeff Roberson */ 3392e6b8de4SJeff Roberson while (lock_prof_resetting) 3402e6b8de4SJeff Roberson sched_relinquish(curthread); 3412e6b8de4SJeff Roberson } 3422e6b8de4SJeff Roberson 343eea4f254SJeff Roberson static void 344eea4f254SJeff Roberson lock_prof_reset(void) 345eea4f254SJeff Roberson { 346eea4f254SJeff Roberson struct lock_prof_cpu *lpc; 347eea4f254SJeff Roberson int enabled, i, cpu; 348eea4f254SJeff Roberson 3492e6b8de4SJeff Roberson /* 3502e6b8de4SJeff Roberson * We not only race with acquiring and releasing locks but also 3512e6b8de4SJeff Roberson * thread exit. To be certain that threads exit without valid head 3522e6b8de4SJeff Roberson * pointers they must see resetting set before enabled is cleared. 3532e6b8de4SJeff Roberson * Otherwise a lock may not be removed from a per-thread list due 3542e6b8de4SJeff Roberson * to disabled being set but not wait for reset() to remove it below. 3552e6b8de4SJeff Roberson */ 3562e6b8de4SJeff Roberson atomic_store_rel_int(&lock_prof_resetting, 1); 357eea4f254SJeff Roberson enabled = lock_prof_enable; 358eea4f254SJeff Roberson lock_prof_enable = 0; 3593ac2ac2eSMateusz Guzik /* 3603ac2ac2eSMateusz Guzik * This both publishes lock_prof_enable as disabled and makes sure 3613ac2ac2eSMateusz Guzik * everyone else reads it if they are not far enough. We wait for the 3623ac2ac2eSMateusz Guzik * rest down below. 3633ac2ac2eSMateusz Guzik */ 3643ac2ac2eSMateusz Guzik cpus_fence_seq_cst(); 3653ac2ac2eSMateusz Guzik quiesce_all_critical(); 3662e6b8de4SJeff Roberson /* 3672e6b8de4SJeff Roberson * Some objects may have migrated between CPUs. Clear all links 3682e6b8de4SJeff Roberson * before we zero the structures. Some items may still be linked 3692e6b8de4SJeff Roberson * into per-thread lists as well. 3702e6b8de4SJeff Roberson */ 371cbba2cb3SMateusz Guzik CPU_FOREACH(cpu) { 372d2be3ef0SMateusz Guzik lpc = LP_CPU(cpu); 373eea4f254SJeff Roberson for (i = 0; i < LPROF_CACHE_SIZE; i++) { 374eea4f254SJeff Roberson LIST_REMOVE(&lpc->lpc_types[0].lpt_objs[i], lpo_link); 375eea4f254SJeff Roberson LIST_REMOVE(&lpc->lpc_types[1].lpt_objs[i], lpo_link); 376eea4f254SJeff Roberson } 3772e6b8de4SJeff Roberson } 378cbba2cb3SMateusz Guzik CPU_FOREACH(cpu) { 379d2be3ef0SMateusz Guzik lpc = LP_CPU(cpu); 380eea4f254SJeff Roberson bzero(lpc, sizeof(*lpc)); 381eea4f254SJeff Roberson lock_prof_init_type(&lpc->lpc_types[0]); 382eea4f254SJeff Roberson lock_prof_init_type(&lpc->lpc_types[1]); 383eea4f254SJeff Roberson } 3843ac2ac2eSMateusz Guzik /* 3853ac2ac2eSMateusz Guzik * Paired with the fence from cpus_fence_seq_cst() 3863ac2ac2eSMateusz Guzik */ 3872e6b8de4SJeff Roberson atomic_store_rel_int(&lock_prof_resetting, 0); 388eea4f254SJeff Roberson lock_prof_enable = enabled; 389eea4f254SJeff Roberson } 390eea4f254SJeff Roberson 391eea4f254SJeff Roberson static void 392eea4f254SJeff Roberson lock_prof_output(struct lock_prof *lp, struct sbuf *sb) 393eea4f254SJeff Roberson { 394eea4f254SJeff Roberson const char *p; 395eea4f254SJeff Roberson 396eea4f254SJeff Roberson for (p = lp->file; p != NULL && strncmp(p, "../", 3) == 0; p += 3); 397eea4f254SJeff Roberson sbuf_printf(sb, 398947265b6SKip Macy "%8ju %9ju %11ju %11ju %11ju %6ju %6ju %2ju %6ju %s:%d (%s:%s)\n", 399947265b6SKip Macy lp->cnt_max / 1000, lp->cnt_wait_max / 1000, lp->cnt_tot / 1000, 400eea4f254SJeff Roberson lp->cnt_wait / 1000, lp->cnt_cur, 401eea4f254SJeff Roberson lp->cnt_cur == 0 ? (uintmax_t)0 : 402eea4f254SJeff Roberson lp->cnt_tot / (lp->cnt_cur * 1000), 403eea4f254SJeff Roberson lp->cnt_cur == 0 ? (uintmax_t)0 : 404eea4f254SJeff Roberson lp->cnt_wait / (lp->cnt_cur * 1000), 405eea4f254SJeff Roberson (uintmax_t)0, lp->cnt_contest_locking, 4060c66dc67SJeff Roberson p, lp->line, lp->class->lc_name, lp->name); 407eea4f254SJeff Roberson } 408eea4f254SJeff Roberson 409eea4f254SJeff Roberson static void 410eea4f254SJeff Roberson lock_prof_sum(struct lock_prof *match, struct lock_prof *dst, int hash, 411eea4f254SJeff Roberson int spin, int t) 412eea4f254SJeff Roberson { 413eea4f254SJeff Roberson struct lock_prof_type *type; 414eea4f254SJeff Roberson struct lock_prof *l; 415eea4f254SJeff Roberson int cpu; 416eea4f254SJeff Roberson 417eea4f254SJeff Roberson dst->file = match->file; 418eea4f254SJeff Roberson dst->line = match->line; 4190c66dc67SJeff Roberson dst->class = match->class; 420eea4f254SJeff Roberson dst->name = match->name; 421eea4f254SJeff Roberson 422cbba2cb3SMateusz Guzik CPU_FOREACH(cpu) { 423d2be3ef0SMateusz Guzik type = &LP_CPU(cpu)->lpc_types[spin]; 424eea4f254SJeff Roberson SLIST_FOREACH(l, &type->lpt_hash[hash], link) { 425eea4f254SJeff Roberson if (l->ticks == t) 426eea4f254SJeff Roberson continue; 427eea4f254SJeff Roberson if (l->file != match->file || l->line != match->line || 4280c66dc67SJeff Roberson l->name != match->name) 429eea4f254SJeff Roberson continue; 430eea4f254SJeff Roberson l->ticks = t; 431eea4f254SJeff Roberson if (l->cnt_max > dst->cnt_max) 432eea4f254SJeff Roberson dst->cnt_max = l->cnt_max; 433947265b6SKip Macy if (l->cnt_wait_max > dst->cnt_wait_max) 434947265b6SKip Macy dst->cnt_wait_max = l->cnt_wait_max; 435eea4f254SJeff Roberson dst->cnt_tot += l->cnt_tot; 436eea4f254SJeff Roberson dst->cnt_wait += l->cnt_wait; 437eea4f254SJeff Roberson dst->cnt_cur += l->cnt_cur; 438eea4f254SJeff Roberson dst->cnt_contest_locking += l->cnt_contest_locking; 439eea4f254SJeff Roberson } 440eea4f254SJeff Roberson } 441eea4f254SJeff Roberson } 442eea4f254SJeff Roberson 443eea4f254SJeff Roberson static void 444eea4f254SJeff Roberson lock_prof_type_stats(struct lock_prof_type *type, struct sbuf *sb, int spin, 445eea4f254SJeff Roberson int t) 446eea4f254SJeff Roberson { 447eea4f254SJeff Roberson struct lock_prof *l; 448eea4f254SJeff Roberson int i; 449eea4f254SJeff Roberson 450eea4f254SJeff Roberson for (i = 0; i < LPROF_HASH_SIZE; ++i) { 451eea4f254SJeff Roberson SLIST_FOREACH(l, &type->lpt_hash[i], link) { 452eea4f254SJeff Roberson struct lock_prof lp = {}; 453eea4f254SJeff Roberson 454eea4f254SJeff Roberson if (l->ticks == t) 455eea4f254SJeff Roberson continue; 456eea4f254SJeff Roberson lock_prof_sum(l, &lp, i, spin, t); 457eea4f254SJeff Roberson lock_prof_output(&lp, sb); 458eea4f254SJeff Roberson } 459eea4f254SJeff Roberson } 460eea4f254SJeff Roberson } 461eea4f254SJeff Roberson 462eea4f254SJeff Roberson static int 463eea4f254SJeff Roberson dump_lock_prof_stats(SYSCTL_HANDLER_ARGS) 464eea4f254SJeff Roberson { 465eea4f254SJeff Roberson struct sbuf *sb; 466eea4f254SJeff Roberson int error, cpu, t; 4670c66dc67SJeff Roberson int enabled; 468eea4f254SJeff Roberson 46900f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 47000f0e671SMatthew D Fleming if (error != 0) 47100f0e671SMatthew D Fleming return (error); 4724e657159SMatthew D Fleming sb = sbuf_new_for_sysctl(NULL, NULL, LPROF_SBUF_SIZE, req); 473947265b6SKip Macy sbuf_printf(sb, "\n%8s %9s %11s %11s %11s %6s %6s %2s %6s %s\n", 474947265b6SKip Macy "max", "wait_max", "total", "wait_total", "count", "avg", "wait_avg", "cnt_hold", "cnt_lock", "name"); 4750c66dc67SJeff Roberson enabled = lock_prof_enable; 4760c66dc67SJeff Roberson lock_prof_enable = 0; 4773ac2ac2eSMateusz Guzik /* 4783ac2ac2eSMateusz Guzik * See the comment in lock_prof_reset 4793ac2ac2eSMateusz Guzik */ 4803ac2ac2eSMateusz Guzik cpus_fence_seq_cst(); 4813ac2ac2eSMateusz Guzik quiesce_all_critical(); 482eea4f254SJeff Roberson t = ticks; 483cbba2cb3SMateusz Guzik CPU_FOREACH(cpu) { 484d2be3ef0SMateusz Guzik lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[0], sb, 0, t); 485d2be3ef0SMateusz Guzik lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[1], sb, 1, t); 486eea4f254SJeff Roberson } 4873ac2ac2eSMateusz Guzik atomic_thread_fence_rel(); 4880c66dc67SJeff Roberson lock_prof_enable = enabled; 489eea4f254SJeff Roberson 4904e657159SMatthew D Fleming error = sbuf_finish(sb); 4914e657159SMatthew D Fleming /* Output a trailing NUL. */ 4924e657159SMatthew D Fleming if (error == 0) 4934e657159SMatthew D Fleming error = SYSCTL_OUT(req, "", 1); 494eea4f254SJeff Roberson sbuf_delete(sb); 495eea4f254SJeff Roberson return (error); 496eea4f254SJeff Roberson } 497eea4f254SJeff Roberson 498eea4f254SJeff Roberson static int 499eea4f254SJeff Roberson enable_lock_prof(SYSCTL_HANDLER_ARGS) 500eea4f254SJeff Roberson { 501eea4f254SJeff Roberson int error, v; 502eea4f254SJeff Roberson 503eea4f254SJeff Roberson v = lock_prof_enable; 504eea4f254SJeff Roberson error = sysctl_handle_int(oidp, &v, v, req); 505eea4f254SJeff Roberson if (error) 506eea4f254SJeff Roberson return (error); 507eea4f254SJeff Roberson if (req->newptr == NULL) 508eea4f254SJeff Roberson return (error); 509eea4f254SJeff Roberson if (v == lock_prof_enable) 510eea4f254SJeff Roberson return (0); 511eea4f254SJeff Roberson if (v == 1) 512eea4f254SJeff Roberson lock_prof_reset(); 513eea4f254SJeff Roberson lock_prof_enable = !!v; 514eea4f254SJeff Roberson 515eea4f254SJeff Roberson return (0); 516eea4f254SJeff Roberson } 517eea4f254SJeff Roberson 518eea4f254SJeff Roberson static int 519eea4f254SJeff Roberson reset_lock_prof_stats(SYSCTL_HANDLER_ARGS) 520eea4f254SJeff Roberson { 521eea4f254SJeff Roberson int error, v; 522eea4f254SJeff Roberson 523eea4f254SJeff Roberson v = 0; 524eea4f254SJeff Roberson error = sysctl_handle_int(oidp, &v, 0, req); 525eea4f254SJeff Roberson if (error) 526eea4f254SJeff Roberson return (error); 527eea4f254SJeff Roberson if (req->newptr == NULL) 528eea4f254SJeff Roberson return (error); 529eea4f254SJeff Roberson if (v == 0) 530eea4f254SJeff Roberson return (0); 531eea4f254SJeff Roberson lock_prof_reset(); 532eea4f254SJeff Roberson 533eea4f254SJeff Roberson return (0); 534eea4f254SJeff Roberson } 535eea4f254SJeff Roberson 536eea4f254SJeff Roberson static struct lock_prof * 537eea4f254SJeff Roberson lock_profile_lookup(struct lock_object *lo, int spin, const char *file, 538eea4f254SJeff Roberson int line) 539eea4f254SJeff Roberson { 540eea4f254SJeff Roberson const char *unknown = "(unknown)"; 541eea4f254SJeff Roberson struct lock_prof_type *type; 542eea4f254SJeff Roberson struct lock_prof *lp; 543eea4f254SJeff Roberson struct lphead *head; 544eea4f254SJeff Roberson const char *p; 545eea4f254SJeff Roberson u_int hash; 546eea4f254SJeff Roberson 547eea4f254SJeff Roberson p = file; 548eea4f254SJeff Roberson if (p == NULL || *p == '\0') 549eea4f254SJeff Roberson p = unknown; 550eea4f254SJeff Roberson hash = (uintptr_t)lo->lo_name * 31 + (uintptr_t)p * 31 + line; 551eea4f254SJeff Roberson hash &= LPROF_HASH_MASK; 552d2be3ef0SMateusz Guzik type = &LP_CPU_SELF->lpc_types[spin]; 553eea4f254SJeff Roberson head = &type->lpt_hash[hash]; 554eea4f254SJeff Roberson SLIST_FOREACH(lp, head, link) { 555eea4f254SJeff Roberson if (lp->line == line && lp->file == p && 556eea4f254SJeff Roberson lp->name == lo->lo_name) 557eea4f254SJeff Roberson return (lp); 558eea4f254SJeff Roberson } 559eea4f254SJeff Roberson lp = SLIST_FIRST(&type->lpt_lpalloc); 560eea4f254SJeff Roberson if (lp == NULL) { 561eea4f254SJeff Roberson lock_prof_rejected++; 562eea4f254SJeff Roberson return (lp); 563eea4f254SJeff Roberson } 564eea4f254SJeff Roberson SLIST_REMOVE_HEAD(&type->lpt_lpalloc, link); 565eea4f254SJeff Roberson lp->file = p; 566eea4f254SJeff Roberson lp->line = line; 5670c66dc67SJeff Roberson lp->class = LOCK_CLASS(lo); 568eea4f254SJeff Roberson lp->name = lo->lo_name; 569eea4f254SJeff Roberson SLIST_INSERT_HEAD(&type->lpt_hash[hash], lp, link); 570eea4f254SJeff Roberson return (lp); 571eea4f254SJeff Roberson } 572eea4f254SJeff Roberson 573eea4f254SJeff Roberson static struct lock_profile_object * 574eea4f254SJeff Roberson lock_profile_object_lookup(struct lock_object *lo, int spin, const char *file, 575eea4f254SJeff Roberson int line) 576eea4f254SJeff Roberson { 577eea4f254SJeff Roberson struct lock_profile_object *l; 578eea4f254SJeff Roberson struct lock_prof_type *type; 579eea4f254SJeff Roberson struct lpohead *head; 580eea4f254SJeff Roberson 581eea4f254SJeff Roberson head = &curthread->td_lprof[spin]; 582eea4f254SJeff Roberson LIST_FOREACH(l, head, lpo_link) 583eea4f254SJeff Roberson if (l->lpo_obj == lo && l->lpo_file == file && 584eea4f254SJeff Roberson l->lpo_line == line) 585eea4f254SJeff Roberson return (l); 586d2be3ef0SMateusz Guzik type = &LP_CPU_SELF->lpc_types[spin]; 587eea4f254SJeff Roberson l = LIST_FIRST(&type->lpt_lpoalloc); 588eea4f254SJeff Roberson if (l == NULL) { 589eea4f254SJeff Roberson lock_prof_rejected++; 590eea4f254SJeff Roberson return (NULL); 591eea4f254SJeff Roberson } 592eea4f254SJeff Roberson LIST_REMOVE(l, lpo_link); 593eea4f254SJeff Roberson l->lpo_obj = lo; 594eea4f254SJeff Roberson l->lpo_file = file; 595eea4f254SJeff Roberson l->lpo_line = line; 596eea4f254SJeff Roberson l->lpo_cnt = 0; 597eea4f254SJeff Roberson LIST_INSERT_HEAD(head, l, lpo_link); 598eea4f254SJeff Roberson 599eea4f254SJeff Roberson return (l); 600eea4f254SJeff Roberson } 601eea4f254SJeff Roberson 602eea4f254SJeff Roberson void 603*6a467cc5SMateusz Guzik lock_profile_obtain_lock_success(struct lock_object *lo, bool spin, 604*6a467cc5SMateusz Guzik int contested, uint64_t waittime, const char *file, int line) 605eea4f254SJeff Roberson { 606eea4f254SJeff Roberson struct lock_profile_object *l; 607*6a467cc5SMateusz Guzik 608*6a467cc5SMateusz Guzik #ifdef LOCK_PROFILING_DEBUG_SPIN 609*6a467cc5SMateusz Guzik bool is_spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK); 610*6a467cc5SMateusz Guzik if ((spin && !is_spin) || (!spin && is_spin)) 611*6a467cc5SMateusz Guzik printf("%s: lock %s spin mismatch (arg %d, flag %d)\n", __func__, 612*6a467cc5SMateusz Guzik lo->lo_name, spin, is_spin); 613*6a467cc5SMateusz Guzik #endif 614eea4f254SJeff Roberson 61535370593SAndriy Gapon if (SCHEDULER_STOPPED()) 61635370593SAndriy Gapon return; 61735370593SAndriy Gapon 618eea4f254SJeff Roberson /* don't reset the timer when/if recursing */ 619eea4f254SJeff Roberson if (!lock_prof_enable || (lo->lo_flags & LO_NOPROFILE)) 620eea4f254SJeff Roberson return; 621a0842e69SMateusz Guzik if (lock_contested_only && !contested) 622a0842e69SMateusz Guzik return; 623eea4f254SJeff Roberson if (spin && lock_prof_skipspin == 1) 624eea4f254SJeff Roberson return; 6252e6b8de4SJeff Roberson critical_enter(); 6262e6b8de4SJeff Roberson /* Recheck enabled now that we're in a critical section. */ 6272e6b8de4SJeff Roberson if (lock_prof_enable == 0) 6282e6b8de4SJeff Roberson goto out; 629eea4f254SJeff Roberson l = lock_profile_object_lookup(lo, spin, file, line); 630eea4f254SJeff Roberson if (l == NULL) 6312e6b8de4SJeff Roberson goto out; 632eea4f254SJeff Roberson l->lpo_cnt++; 633eea4f254SJeff Roberson if (++l->lpo_ref > 1) 6342e6b8de4SJeff Roberson goto out; 635eea4f254SJeff Roberson l->lpo_contest_locking = contested; 6367c0435b9SKip Macy l->lpo_acqtime = nanoseconds(); 637aa077979SKip Macy if (waittime && (l->lpo_acqtime > waittime)) 6387c0435b9SKip Macy l->lpo_waittime = l->lpo_acqtime - waittime; 639aa077979SKip Macy else 640aa077979SKip Macy l->lpo_waittime = 0; 6412e6b8de4SJeff Roberson out: 6423ac2ac2eSMateusz Guzik /* 6433ac2ac2eSMateusz Guzik * Paired with cpus_fence_seq_cst(). 6443ac2ac2eSMateusz Guzik */ 6453ac2ac2eSMateusz Guzik atomic_thread_fence_rel(); 6462e6b8de4SJeff Roberson critical_exit(); 6472e6b8de4SJeff Roberson } 6482e6b8de4SJeff Roberson 6492e6b8de4SJeff Roberson void 6502e6b8de4SJeff Roberson lock_profile_thread_exit(struct thread *td) 6512e6b8de4SJeff Roberson { 6522e6b8de4SJeff Roberson #ifdef INVARIANTS 6532e6b8de4SJeff Roberson struct lock_profile_object *l; 6542e6b8de4SJeff Roberson 6552e6b8de4SJeff Roberson MPASS(curthread->td_critnest == 0); 6562e6b8de4SJeff Roberson #endif 6572e6b8de4SJeff Roberson /* 6582e6b8de4SJeff Roberson * If lock profiling was disabled we have to wait for reset to 6592e6b8de4SJeff Roberson * clear our pointers before we can exit safely. 6602e6b8de4SJeff Roberson */ 6612e6b8de4SJeff Roberson lock_prof_reset_wait(); 6622e6b8de4SJeff Roberson #ifdef INVARIANTS 6632e6b8de4SJeff Roberson LIST_FOREACH(l, &td->td_lprof[0], lpo_link) 6642e6b8de4SJeff Roberson printf("thread still holds lock acquired at %s:%d\n", 6652e6b8de4SJeff Roberson l->lpo_file, l->lpo_line); 6662e6b8de4SJeff Roberson LIST_FOREACH(l, &td->td_lprof[1], lpo_link) 6672e6b8de4SJeff Roberson printf("thread still holds lock acquired at %s:%d\n", 6682e6b8de4SJeff Roberson l->lpo_file, l->lpo_line); 6692e6b8de4SJeff Roberson #endif 6702e6b8de4SJeff Roberson MPASS(LIST_FIRST(&td->td_lprof[0]) == NULL); 6712e6b8de4SJeff Roberson MPASS(LIST_FIRST(&td->td_lprof[1]) == NULL); 6727c0435b9SKip Macy } 6737c0435b9SKip Macy 674eea4f254SJeff Roberson void 675*6a467cc5SMateusz Guzik lock_profile_release_lock(struct lock_object *lo, bool spin) 6767c0435b9SKip Macy { 677eea4f254SJeff Roberson struct lock_profile_object *l; 678eea4f254SJeff Roberson struct lock_prof_type *type; 679eea4f254SJeff Roberson struct lock_prof *lp; 68060ae52f7SEd Schouten uint64_t curtime, holdtime; 681eea4f254SJeff Roberson struct lpohead *head; 682*6a467cc5SMateusz Guzik 683*6a467cc5SMateusz Guzik #ifdef LOCK_PROFILING_DEBUG_SPIN 684*6a467cc5SMateusz Guzik bool is_spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK); 685*6a467cc5SMateusz Guzik if ((spin && !is_spin) || (!spin && is_spin)) 686*6a467cc5SMateusz Guzik printf("%s: lock %s spin mismatch (arg %d, flag %d)\n", __func__, 687*6a467cc5SMateusz Guzik lo->lo_name, spin, is_spin); 688*6a467cc5SMateusz Guzik #endif 6897c0435b9SKip Macy 69035370593SAndriy Gapon if (SCHEDULER_STOPPED()) 69135370593SAndriy Gapon return; 6922e6b8de4SJeff Roberson if (lo->lo_flags & LO_NOPROFILE) 6937c0435b9SKip Macy return; 694eea4f254SJeff Roberson head = &curthread->td_lprof[spin]; 6952e6b8de4SJeff Roberson if (LIST_FIRST(head) == NULL) 6962e6b8de4SJeff Roberson return; 697eea4f254SJeff Roberson critical_enter(); 6982e6b8de4SJeff Roberson /* Recheck enabled now that we're in a critical section. */ 6992e6b8de4SJeff Roberson if (lock_prof_enable == 0 && lock_prof_resetting == 1) 7002e6b8de4SJeff Roberson goto out; 7012e6b8de4SJeff Roberson /* 7022e6b8de4SJeff Roberson * If lock profiling is not enabled we still want to remove the 7032e6b8de4SJeff Roberson * lpo from our queue. 7042e6b8de4SJeff Roberson */ 705eea4f254SJeff Roberson LIST_FOREACH(l, head, lpo_link) 706eea4f254SJeff Roberson if (l->lpo_obj == lo) 7077c0435b9SKip Macy break; 708eea4f254SJeff Roberson if (l == NULL) 709eea4f254SJeff Roberson goto out; 710eea4f254SJeff Roberson if (--l->lpo_ref > 0) 711eea4f254SJeff Roberson goto out; 712eea4f254SJeff Roberson lp = lock_profile_lookup(lo, spin, l->lpo_file, l->lpo_line); 713eea4f254SJeff Roberson if (lp == NULL) 714eea4f254SJeff Roberson goto release; 715e7154e7eSAndriy Gapon curtime = nanoseconds(); 716e7154e7eSAndriy Gapon if (curtime < l->lpo_acqtime) 717eea4f254SJeff Roberson goto release; 718e7154e7eSAndriy Gapon holdtime = curtime - l->lpo_acqtime; 719e7154e7eSAndriy Gapon 7207c0435b9SKip Macy /* 72183b72e3eSKip Macy * Record if the lock has been held longer now than ever 7227c0435b9SKip Macy * before. 7237c0435b9SKip Macy */ 724eea4f254SJeff Roberson if (holdtime > lp->cnt_max) 725eea4f254SJeff Roberson lp->cnt_max = holdtime; 726947265b6SKip Macy if (l->lpo_waittime > lp->cnt_wait_max) 727947265b6SKip Macy lp->cnt_wait_max = l->lpo_waittime; 728eea4f254SJeff Roberson lp->cnt_tot += holdtime; 729eea4f254SJeff Roberson lp->cnt_wait += l->lpo_waittime; 730eea4f254SJeff Roberson lp->cnt_contest_locking += l->lpo_contest_locking; 731eea4f254SJeff Roberson lp->cnt_cur += l->lpo_cnt; 732eea4f254SJeff Roberson release: 733eea4f254SJeff Roberson LIST_REMOVE(l, lpo_link); 734d2be3ef0SMateusz Guzik type = &LP_CPU_SELF->lpc_types[spin]; 735eea4f254SJeff Roberson LIST_INSERT_HEAD(&type->lpt_lpoalloc, l, lpo_link); 736eea4f254SJeff Roberson out: 7373ac2ac2eSMateusz Guzik /* 7383ac2ac2eSMateusz Guzik * Paired with cpus_fence_seq_cst(). 7393ac2ac2eSMateusz Guzik */ 7403ac2ac2eSMateusz Guzik atomic_thread_fence_rel(); 741eea4f254SJeff Roberson critical_exit(); 742eea4f254SJeff Roberson } 7437c0435b9SKip Macy 7447029da5cSPawel Biernacki static SYSCTL_NODE(_debug_lock, OID_AUTO, prof, 7457029da5cSPawel Biernacki CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 7466472ac3dSEd Schouten "lock profiling"); 747eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipspin, CTLFLAG_RW, 748eea4f254SJeff Roberson &lock_prof_skipspin, 0, "Skip profiling on spinlocks."); 749eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, rejected, CTLFLAG_RD, 750eea4f254SJeff Roberson &lock_prof_rejected, 0, "Number of rejected profiling records"); 751a0842e69SMateusz Guzik SYSCTL_INT(_debug_lock_prof, OID_AUTO, contested_only, CTLFLAG_RW, 752a0842e69SMateusz Guzik &lock_contested_only, 0, "Only profile contested acquires"); 7537029da5cSPawel Biernacki SYSCTL_PROC(_debug_lock_prof, OID_AUTO, stats, 7547029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 7557029da5cSPawel Biernacki dump_lock_prof_stats, "A", 7567029da5cSPawel Biernacki "Lock profiling statistics"); 7577029da5cSPawel Biernacki SYSCTL_PROC(_debug_lock_prof, OID_AUTO, reset, 7587029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0, 7597029da5cSPawel Biernacki reset_lock_prof_stats, "I", 7607029da5cSPawel Biernacki "Reset lock profiling statistics"); 7617029da5cSPawel Biernacki SYSCTL_PROC(_debug_lock_prof, OID_AUTO, enable, 7627029da5cSPawel Biernacki CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0, 7637029da5cSPawel Biernacki enable_lock_prof, "I", 7647029da5cSPawel Biernacki "Enable lock profiling"); 765eea4f254SJeff Roberson 7667c0435b9SKip Macy #endif 767