183a81bcbSJohn Baldwin /*- 283a81bcbSJohn Baldwin * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org> 383a81bcbSJohn Baldwin * All rights reserved. 483a81bcbSJohn Baldwin * 583a81bcbSJohn Baldwin * Redistribution and use in source and binary forms, with or without 683a81bcbSJohn Baldwin * modification, are permitted provided that the following conditions 783a81bcbSJohn Baldwin * are met: 883a81bcbSJohn Baldwin * 1. Redistributions of source code must retain the above copyright 983a81bcbSJohn Baldwin * notice, this list of conditions and the following disclaimer. 1083a81bcbSJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright 1183a81bcbSJohn Baldwin * notice, this list of conditions and the following disclaimer in the 1283a81bcbSJohn Baldwin * documentation and/or other materials provided with the distribution. 1383a81bcbSJohn Baldwin * 1483a81bcbSJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1583a81bcbSJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1683a81bcbSJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1783a81bcbSJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1883a81bcbSJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1983a81bcbSJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2083a81bcbSJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2183a81bcbSJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2283a81bcbSJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2383a81bcbSJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2483a81bcbSJohn Baldwin * SUCH DAMAGE. 2583a81bcbSJohn Baldwin */ 2683a81bcbSJohn Baldwin 2783a81bcbSJohn Baldwin /* 2883a81bcbSJohn Baldwin * This module holds the global variables and functions used to maintain 2983a81bcbSJohn Baldwin * lock_object structures. 3083a81bcbSJohn Baldwin */ 3183a81bcbSJohn Baldwin 3283a81bcbSJohn Baldwin #include <sys/cdefs.h> 3383a81bcbSJohn Baldwin __FBSDID("$FreeBSD$"); 3483a81bcbSJohn Baldwin 356ef970a9SJohn Baldwin #include "opt_ddb.h" 367c0435b9SKip Macy #include "opt_mprof.h" 376ef970a9SJohn Baldwin 3883a81bcbSJohn Baldwin #include <sys/param.h> 3983a81bcbSJohn Baldwin #include <sys/systm.h> 40eea4f254SJeff Roberson #include <sys/kernel.h> 4183a81bcbSJohn Baldwin #include <sys/ktr.h> 4283a81bcbSJohn Baldwin #include <sys/lock.h> 437c0435b9SKip Macy #include <sys/lock_profile.h> 44eea4f254SJeff Roberson #include <sys/malloc.h> 452e6b8de4SJeff Roberson #include <sys/mutex.h> 46eea4f254SJeff Roberson #include <sys/pcpu.h> 47eea4f254SJeff Roberson #include <sys/proc.h> 48eea4f254SJeff Roberson #include <sys/sbuf.h> 492e6b8de4SJeff Roberson #include <sys/sched.h> 50eea4f254SJeff Roberson #include <sys/smp.h> 51eea4f254SJeff Roberson #include <sys/sysctl.h> 5283a81bcbSJohn Baldwin 5383a81bcbSJohn Baldwin #ifdef DDB 5483a81bcbSJohn Baldwin #include <ddb/ddb.h> 5583a81bcbSJohn Baldwin #endif 5683a81bcbSJohn Baldwin 57eea4f254SJeff Roberson #include <machine/cpufunc.h> 58eea4f254SJeff Roberson 5983a81bcbSJohn Baldwin CTASSERT(LOCK_CLASS_MAX == 15); 6083a81bcbSJohn Baldwin 6183a81bcbSJohn Baldwin struct lock_class *lock_classes[LOCK_CLASS_MAX + 1] = { 6283a81bcbSJohn Baldwin &lock_class_mtx_spin, 6383a81bcbSJohn Baldwin &lock_class_mtx_sleep, 6483a81bcbSJohn Baldwin &lock_class_sx, 65f53d15feSStephan Uphoff &lock_class_rm, 66cd32bd7aSJohn Baldwin &lock_class_rm_sleepable, 673f08bd8bSJohn Baldwin &lock_class_rw, 6861bd5e21SKip Macy &lock_class_lockmgr, 6983a81bcbSJohn Baldwin }; 7083a81bcbSJohn Baldwin 7183a81bcbSJohn Baldwin void 7283a81bcbSJohn Baldwin lock_init(struct lock_object *lock, struct lock_class *class, const char *name, 7383a81bcbSJohn Baldwin const char *type, int flags) 7483a81bcbSJohn Baldwin { 7583a81bcbSJohn Baldwin int i; 7683a81bcbSJohn Baldwin 7783a81bcbSJohn Baldwin /* Check for double-init and zero object. */ 78fd07ddcfSDmitry Chagin KASSERT(flags & LO_NEW || !lock_initialized(lock), 79fd07ddcfSDmitry Chagin ("lock \"%s\" %p already initialized", name, lock)); 8083a81bcbSJohn Baldwin 8183a81bcbSJohn Baldwin /* Look up lock class to find its index. */ 8283a81bcbSJohn Baldwin for (i = 0; i < LOCK_CLASS_MAX; i++) 8383a81bcbSJohn Baldwin if (lock_classes[i] == class) { 8483a81bcbSJohn Baldwin lock->lo_flags = i << LO_CLASSSHIFT; 8583a81bcbSJohn Baldwin break; 8683a81bcbSJohn Baldwin } 8783a81bcbSJohn Baldwin KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class)); 8883a81bcbSJohn Baldwin 8983a81bcbSJohn Baldwin /* Initialize the lock object. */ 9083a81bcbSJohn Baldwin lock->lo_name = name; 9183a81bcbSJohn Baldwin lock->lo_flags |= flags | LO_INITIALIZED; 9283a81bcbSJohn Baldwin LOCK_LOG_INIT(lock, 0); 9390356491SAttilio Rao WITNESS_INIT(lock, (type != NULL) ? type : name); 9483a81bcbSJohn Baldwin } 9583a81bcbSJohn Baldwin 9683a81bcbSJohn Baldwin void 9783a81bcbSJohn Baldwin lock_destroy(struct lock_object *lock) 9883a81bcbSJohn Baldwin { 9983a81bcbSJohn Baldwin 1003a6cdc4eSJohn-Mark Gurney KASSERT(lock_initialized(lock), ("lock %p is not initialized", lock)); 10183a81bcbSJohn Baldwin WITNESS_DESTROY(lock); 10283a81bcbSJohn Baldwin LOCK_LOG_DESTROY(lock, 0); 10383a81bcbSJohn Baldwin lock->lo_flags &= ~LO_INITIALIZED; 10483a81bcbSJohn Baldwin } 10583a81bcbSJohn Baldwin 106*1ada9041SMateusz Guzik void 107*1ada9041SMateusz Guzik lock_delay(struct lock_delay_arg *la) 108*1ada9041SMateusz Guzik { 109*1ada9041SMateusz Guzik u_int i, delay, backoff, min, max; 110*1ada9041SMateusz Guzik struct lock_delay_config *lc = la->config; 111*1ada9041SMateusz Guzik 112*1ada9041SMateusz Guzik delay = la->delay; 113*1ada9041SMateusz Guzik 114*1ada9041SMateusz Guzik if (delay == 0) 115*1ada9041SMateusz Guzik delay = lc->initial; 116*1ada9041SMateusz Guzik else { 117*1ada9041SMateusz Guzik delay += lc->step; 118*1ada9041SMateusz Guzik max = lc->max; 119*1ada9041SMateusz Guzik if (delay > max) 120*1ada9041SMateusz Guzik delay = max; 121*1ada9041SMateusz Guzik } 122*1ada9041SMateusz Guzik 123*1ada9041SMateusz Guzik backoff = cpu_ticks() % delay; 124*1ada9041SMateusz Guzik min = lc->min; 125*1ada9041SMateusz Guzik if (backoff < min) 126*1ada9041SMateusz Guzik backoff = min; 127*1ada9041SMateusz Guzik for (i = 0; i < backoff; i++) 128*1ada9041SMateusz Guzik cpu_spinwait(); 129*1ada9041SMateusz Guzik 130*1ada9041SMateusz Guzik la->delay = delay; 131*1ada9041SMateusz Guzik la->spin_cnt += backoff; 132*1ada9041SMateusz Guzik } 133*1ada9041SMateusz Guzik 13483a81bcbSJohn Baldwin #ifdef DDB 13583a81bcbSJohn Baldwin DB_SHOW_COMMAND(lock, db_show_lock) 13683a81bcbSJohn Baldwin { 13783a81bcbSJohn Baldwin struct lock_object *lock; 13883a81bcbSJohn Baldwin struct lock_class *class; 13983a81bcbSJohn Baldwin 14083a81bcbSJohn Baldwin if (!have_addr) 14183a81bcbSJohn Baldwin return; 14283a81bcbSJohn Baldwin lock = (struct lock_object *)addr; 14383a81bcbSJohn Baldwin if (LO_CLASSINDEX(lock) > LOCK_CLASS_MAX) { 14483a81bcbSJohn Baldwin db_printf("Unknown lock class: %d\n", LO_CLASSINDEX(lock)); 14583a81bcbSJohn Baldwin return; 14683a81bcbSJohn Baldwin } 14783a81bcbSJohn Baldwin class = LOCK_CLASS(lock); 14883a81bcbSJohn Baldwin db_printf(" class: %s\n", class->lc_name); 14983a81bcbSJohn Baldwin db_printf(" name: %s\n", lock->lo_name); 15083a81bcbSJohn Baldwin class->lc_ddb_show(lock); 15183a81bcbSJohn Baldwin } 15283a81bcbSJohn Baldwin #endif 1537c0435b9SKip Macy 1547c0435b9SKip Macy #ifdef LOCK_PROFILING 155eea4f254SJeff Roberson 156eea4f254SJeff Roberson /* 157eea4f254SJeff Roberson * One object per-thread for each lock the thread owns. Tracks individual 158eea4f254SJeff Roberson * lock instances. 159eea4f254SJeff Roberson */ 160eea4f254SJeff Roberson struct lock_profile_object { 161eea4f254SJeff Roberson LIST_ENTRY(lock_profile_object) lpo_link; 162eea4f254SJeff Roberson struct lock_object *lpo_obj; 163eea4f254SJeff Roberson const char *lpo_file; 164eea4f254SJeff Roberson int lpo_line; 165eea4f254SJeff Roberson uint16_t lpo_ref; 166eea4f254SJeff Roberson uint16_t lpo_cnt; 16760ae52f7SEd Schouten uint64_t lpo_acqtime; 16860ae52f7SEd Schouten uint64_t lpo_waittime; 169eea4f254SJeff Roberson u_int lpo_contest_locking; 170eea4f254SJeff Roberson }; 171eea4f254SJeff Roberson 172eea4f254SJeff Roberson /* 173eea4f254SJeff Roberson * One lock_prof for each (file, line, lock object) triple. 174eea4f254SJeff Roberson */ 175eea4f254SJeff Roberson struct lock_prof { 176eea4f254SJeff Roberson SLIST_ENTRY(lock_prof) link; 1770c66dc67SJeff Roberson struct lock_class *class; 178eea4f254SJeff Roberson const char *file; 179eea4f254SJeff Roberson const char *name; 180eea4f254SJeff Roberson int line; 181eea4f254SJeff Roberson int ticks; 182947265b6SKip Macy uintmax_t cnt_wait_max; 183eea4f254SJeff Roberson uintmax_t cnt_max; 184eea4f254SJeff Roberson uintmax_t cnt_tot; 185eea4f254SJeff Roberson uintmax_t cnt_wait; 186eea4f254SJeff Roberson uintmax_t cnt_cur; 187eea4f254SJeff Roberson uintmax_t cnt_contest_locking; 188eea4f254SJeff Roberson }; 189eea4f254SJeff Roberson 190eea4f254SJeff Roberson SLIST_HEAD(lphead, lock_prof); 191eea4f254SJeff Roberson 192eea4f254SJeff Roberson #define LPROF_HASH_SIZE 4096 193eea4f254SJeff Roberson #define LPROF_HASH_MASK (LPROF_HASH_SIZE - 1) 194eea4f254SJeff Roberson #define LPROF_CACHE_SIZE 4096 195eea4f254SJeff Roberson 196eea4f254SJeff Roberson /* 197eea4f254SJeff Roberson * Array of objects and profs for each type of object for each cpu. Spinlocks 198b1ce21c6SRebecca Cran * are handled separately because a thread may be preempted and acquire a 199eea4f254SJeff Roberson * spinlock while in the lock profiling code of a non-spinlock. In this way 200eea4f254SJeff Roberson * we only need a critical section to protect the per-cpu lists. 201eea4f254SJeff Roberson */ 202eea4f254SJeff Roberson struct lock_prof_type { 203eea4f254SJeff Roberson struct lphead lpt_lpalloc; 204eea4f254SJeff Roberson struct lpohead lpt_lpoalloc; 205eea4f254SJeff Roberson struct lphead lpt_hash[LPROF_HASH_SIZE]; 206eea4f254SJeff Roberson struct lock_prof lpt_prof[LPROF_CACHE_SIZE]; 207eea4f254SJeff Roberson struct lock_profile_object lpt_objs[LPROF_CACHE_SIZE]; 208eea4f254SJeff Roberson }; 209eea4f254SJeff Roberson 210eea4f254SJeff Roberson struct lock_prof_cpu { 211eea4f254SJeff Roberson struct lock_prof_type lpc_types[2]; /* One for spin one for other. */ 212eea4f254SJeff Roberson }; 213eea4f254SJeff Roberson 214eea4f254SJeff Roberson struct lock_prof_cpu *lp_cpu[MAXCPU]; 215eea4f254SJeff Roberson 2162e6b8de4SJeff Roberson volatile int lock_prof_enable = 0; 2172e6b8de4SJeff Roberson static volatile int lock_prof_resetting; 218eea4f254SJeff Roberson 2194e657159SMatthew D Fleming #define LPROF_SBUF_SIZE 256 220eea4f254SJeff Roberson 221eea4f254SJeff Roberson static int lock_prof_rejected; 222eea4f254SJeff Roberson static int lock_prof_skipspin; 223eea4f254SJeff Roberson static int lock_prof_skipcount; 224eea4f254SJeff Roberson 225eea4f254SJeff Roberson #ifndef USE_CPU_NANOSECONDS 22660ae52f7SEd Schouten uint64_t 227eea4f254SJeff Roberson nanoseconds(void) 2287c0435b9SKip Macy { 229eea4f254SJeff Roberson struct bintime bt; 23060ae52f7SEd Schouten uint64_t ns; 2317c0435b9SKip Macy 232eea4f254SJeff Roberson binuptime(&bt); 233eea4f254SJeff Roberson /* From bintime2timespec */ 23460ae52f7SEd Schouten ns = bt.sec * (uint64_t)1000000000; 235eea4f254SJeff Roberson ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32; 236eea4f254SJeff Roberson return (ns); 237eea4f254SJeff Roberson } 238eea4f254SJeff Roberson #endif 239fe68a916SKip Macy 240eea4f254SJeff Roberson static void 241eea4f254SJeff Roberson lock_prof_init_type(struct lock_prof_type *type) 242eea4f254SJeff Roberson { 243eea4f254SJeff Roberson int i; 244fe68a916SKip Macy 245eea4f254SJeff Roberson SLIST_INIT(&type->lpt_lpalloc); 246eea4f254SJeff Roberson LIST_INIT(&type->lpt_lpoalloc); 247eea4f254SJeff Roberson for (i = 0; i < LPROF_CACHE_SIZE; i++) { 248eea4f254SJeff Roberson SLIST_INSERT_HEAD(&type->lpt_lpalloc, &type->lpt_prof[i], 249eea4f254SJeff Roberson link); 250eea4f254SJeff Roberson LIST_INSERT_HEAD(&type->lpt_lpoalloc, &type->lpt_objs[i], 251eea4f254SJeff Roberson lpo_link); 252eea4f254SJeff Roberson } 253eea4f254SJeff Roberson } 254eea4f254SJeff Roberson 255eea4f254SJeff Roberson static void 256eea4f254SJeff Roberson lock_prof_init(void *arg) 257eea4f254SJeff Roberson { 258eea4f254SJeff Roberson int cpu; 259eea4f254SJeff Roberson 260eea4f254SJeff Roberson for (cpu = 0; cpu <= mp_maxid; cpu++) { 261eea4f254SJeff Roberson lp_cpu[cpu] = malloc(sizeof(*lp_cpu[cpu]), M_DEVBUF, 262eea4f254SJeff Roberson M_WAITOK | M_ZERO); 263eea4f254SJeff Roberson lock_prof_init_type(&lp_cpu[cpu]->lpc_types[0]); 264eea4f254SJeff Roberson lock_prof_init_type(&lp_cpu[cpu]->lpc_types[1]); 265eea4f254SJeff Roberson } 266eea4f254SJeff Roberson } 267eea4f254SJeff Roberson SYSINIT(lockprof, SI_SUB_SMP, SI_ORDER_ANY, lock_prof_init, NULL); 268eea4f254SJeff Roberson 2692e6b8de4SJeff Roberson static void 2702e6b8de4SJeff Roberson lock_prof_reset_wait(void) 2712e6b8de4SJeff Roberson { 2722e6b8de4SJeff Roberson 2732e6b8de4SJeff Roberson /* 27428d91af3SJeff Roberson * Spin relinquishing our cpu so that quiesce_all_cpus may 27528d91af3SJeff Roberson * complete. 2762e6b8de4SJeff Roberson */ 2772e6b8de4SJeff Roberson while (lock_prof_resetting) 2782e6b8de4SJeff Roberson sched_relinquish(curthread); 2792e6b8de4SJeff Roberson } 2802e6b8de4SJeff Roberson 281eea4f254SJeff Roberson static void 282eea4f254SJeff Roberson lock_prof_reset(void) 283eea4f254SJeff Roberson { 284eea4f254SJeff Roberson struct lock_prof_cpu *lpc; 285eea4f254SJeff Roberson int enabled, i, cpu; 286eea4f254SJeff Roberson 2872e6b8de4SJeff Roberson /* 2882e6b8de4SJeff Roberson * We not only race with acquiring and releasing locks but also 2892e6b8de4SJeff Roberson * thread exit. To be certain that threads exit without valid head 2902e6b8de4SJeff Roberson * pointers they must see resetting set before enabled is cleared. 2912e6b8de4SJeff Roberson * Otherwise a lock may not be removed from a per-thread list due 2922e6b8de4SJeff Roberson * to disabled being set but not wait for reset() to remove it below. 2932e6b8de4SJeff Roberson */ 2942e6b8de4SJeff Roberson atomic_store_rel_int(&lock_prof_resetting, 1); 295eea4f254SJeff Roberson enabled = lock_prof_enable; 296eea4f254SJeff Roberson lock_prof_enable = 0; 29728d91af3SJeff Roberson quiesce_all_cpus("profreset", 0); 2982e6b8de4SJeff Roberson /* 2992e6b8de4SJeff Roberson * Some objects may have migrated between CPUs. Clear all links 3002e6b8de4SJeff Roberson * before we zero the structures. Some items may still be linked 3012e6b8de4SJeff Roberson * into per-thread lists as well. 3022e6b8de4SJeff Roberson */ 303eea4f254SJeff Roberson for (cpu = 0; cpu <= mp_maxid; cpu++) { 304eea4f254SJeff Roberson lpc = lp_cpu[cpu]; 305eea4f254SJeff Roberson for (i = 0; i < LPROF_CACHE_SIZE; i++) { 306eea4f254SJeff Roberson LIST_REMOVE(&lpc->lpc_types[0].lpt_objs[i], lpo_link); 307eea4f254SJeff Roberson LIST_REMOVE(&lpc->lpc_types[1].lpt_objs[i], lpo_link); 308eea4f254SJeff Roberson } 3092e6b8de4SJeff Roberson } 3102e6b8de4SJeff Roberson for (cpu = 0; cpu <= mp_maxid; cpu++) { 3112e6b8de4SJeff Roberson lpc = lp_cpu[cpu]; 312eea4f254SJeff Roberson bzero(lpc, sizeof(*lpc)); 313eea4f254SJeff Roberson lock_prof_init_type(&lpc->lpc_types[0]); 314eea4f254SJeff Roberson lock_prof_init_type(&lpc->lpc_types[1]); 315eea4f254SJeff Roberson } 3162e6b8de4SJeff Roberson atomic_store_rel_int(&lock_prof_resetting, 0); 317eea4f254SJeff Roberson lock_prof_enable = enabled; 318eea4f254SJeff Roberson } 319eea4f254SJeff Roberson 320eea4f254SJeff Roberson static void 321eea4f254SJeff Roberson lock_prof_output(struct lock_prof *lp, struct sbuf *sb) 322eea4f254SJeff Roberson { 323eea4f254SJeff Roberson const char *p; 324eea4f254SJeff Roberson 325eea4f254SJeff Roberson for (p = lp->file; p != NULL && strncmp(p, "../", 3) == 0; p += 3); 326eea4f254SJeff Roberson sbuf_printf(sb, 327947265b6SKip Macy "%8ju %9ju %11ju %11ju %11ju %6ju %6ju %2ju %6ju %s:%d (%s:%s)\n", 328947265b6SKip Macy lp->cnt_max / 1000, lp->cnt_wait_max / 1000, lp->cnt_tot / 1000, 329eea4f254SJeff Roberson lp->cnt_wait / 1000, lp->cnt_cur, 330eea4f254SJeff Roberson lp->cnt_cur == 0 ? (uintmax_t)0 : 331eea4f254SJeff Roberson lp->cnt_tot / (lp->cnt_cur * 1000), 332eea4f254SJeff Roberson lp->cnt_cur == 0 ? (uintmax_t)0 : 333eea4f254SJeff Roberson lp->cnt_wait / (lp->cnt_cur * 1000), 334eea4f254SJeff Roberson (uintmax_t)0, lp->cnt_contest_locking, 3350c66dc67SJeff Roberson p, lp->line, lp->class->lc_name, lp->name); 336eea4f254SJeff Roberson } 337eea4f254SJeff Roberson 338eea4f254SJeff Roberson static void 339eea4f254SJeff Roberson lock_prof_sum(struct lock_prof *match, struct lock_prof *dst, int hash, 340eea4f254SJeff Roberson int spin, int t) 341eea4f254SJeff Roberson { 342eea4f254SJeff Roberson struct lock_prof_type *type; 343eea4f254SJeff Roberson struct lock_prof *l; 344eea4f254SJeff Roberson int cpu; 345eea4f254SJeff Roberson 346eea4f254SJeff Roberson dst->file = match->file; 347eea4f254SJeff Roberson dst->line = match->line; 3480c66dc67SJeff Roberson dst->class = match->class; 349eea4f254SJeff Roberson dst->name = match->name; 350eea4f254SJeff Roberson 351eea4f254SJeff Roberson for (cpu = 0; cpu <= mp_maxid; cpu++) { 352eea4f254SJeff Roberson if (lp_cpu[cpu] == NULL) 353eea4f254SJeff Roberson continue; 354eea4f254SJeff Roberson type = &lp_cpu[cpu]->lpc_types[spin]; 355eea4f254SJeff Roberson SLIST_FOREACH(l, &type->lpt_hash[hash], link) { 356eea4f254SJeff Roberson if (l->ticks == t) 357eea4f254SJeff Roberson continue; 358eea4f254SJeff Roberson if (l->file != match->file || l->line != match->line || 3590c66dc67SJeff Roberson l->name != match->name) 360eea4f254SJeff Roberson continue; 361eea4f254SJeff Roberson l->ticks = t; 362eea4f254SJeff Roberson if (l->cnt_max > dst->cnt_max) 363eea4f254SJeff Roberson dst->cnt_max = l->cnt_max; 364947265b6SKip Macy if (l->cnt_wait_max > dst->cnt_wait_max) 365947265b6SKip Macy dst->cnt_wait_max = l->cnt_wait_max; 366eea4f254SJeff Roberson dst->cnt_tot += l->cnt_tot; 367eea4f254SJeff Roberson dst->cnt_wait += l->cnt_wait; 368eea4f254SJeff Roberson dst->cnt_cur += l->cnt_cur; 369eea4f254SJeff Roberson dst->cnt_contest_locking += l->cnt_contest_locking; 370eea4f254SJeff Roberson } 371eea4f254SJeff Roberson } 372eea4f254SJeff Roberson 373eea4f254SJeff Roberson } 374eea4f254SJeff Roberson 375eea4f254SJeff Roberson static void 376eea4f254SJeff Roberson lock_prof_type_stats(struct lock_prof_type *type, struct sbuf *sb, int spin, 377eea4f254SJeff Roberson int t) 378eea4f254SJeff Roberson { 379eea4f254SJeff Roberson struct lock_prof *l; 380eea4f254SJeff Roberson int i; 381eea4f254SJeff Roberson 382eea4f254SJeff Roberson for (i = 0; i < LPROF_HASH_SIZE; ++i) { 383eea4f254SJeff Roberson SLIST_FOREACH(l, &type->lpt_hash[i], link) { 384eea4f254SJeff Roberson struct lock_prof lp = {}; 385eea4f254SJeff Roberson 386eea4f254SJeff Roberson if (l->ticks == t) 387eea4f254SJeff Roberson continue; 388eea4f254SJeff Roberson lock_prof_sum(l, &lp, i, spin, t); 389eea4f254SJeff Roberson lock_prof_output(&lp, sb); 390eea4f254SJeff Roberson } 391eea4f254SJeff Roberson } 392eea4f254SJeff Roberson } 393eea4f254SJeff Roberson 394eea4f254SJeff Roberson static int 395eea4f254SJeff Roberson dump_lock_prof_stats(SYSCTL_HANDLER_ARGS) 396eea4f254SJeff Roberson { 397eea4f254SJeff Roberson struct sbuf *sb; 398eea4f254SJeff Roberson int error, cpu, t; 3990c66dc67SJeff Roberson int enabled; 400eea4f254SJeff Roberson 40100f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 40200f0e671SMatthew D Fleming if (error != 0) 40300f0e671SMatthew D Fleming return (error); 4044e657159SMatthew D Fleming sb = sbuf_new_for_sysctl(NULL, NULL, LPROF_SBUF_SIZE, req); 405947265b6SKip Macy sbuf_printf(sb, "\n%8s %9s %11s %11s %11s %6s %6s %2s %6s %s\n", 406947265b6SKip Macy "max", "wait_max", "total", "wait_total", "count", "avg", "wait_avg", "cnt_hold", "cnt_lock", "name"); 4070c66dc67SJeff Roberson enabled = lock_prof_enable; 4080c66dc67SJeff Roberson lock_prof_enable = 0; 40928d91af3SJeff Roberson quiesce_all_cpus("profstat", 0); 410eea4f254SJeff Roberson t = ticks; 411eea4f254SJeff Roberson for (cpu = 0; cpu <= mp_maxid; cpu++) { 412eea4f254SJeff Roberson if (lp_cpu[cpu] == NULL) 413eea4f254SJeff Roberson continue; 414eea4f254SJeff Roberson lock_prof_type_stats(&lp_cpu[cpu]->lpc_types[0], sb, 0, t); 415eea4f254SJeff Roberson lock_prof_type_stats(&lp_cpu[cpu]->lpc_types[1], sb, 1, t); 416eea4f254SJeff Roberson } 4170c66dc67SJeff Roberson lock_prof_enable = enabled; 418eea4f254SJeff Roberson 4194e657159SMatthew D Fleming error = sbuf_finish(sb); 4204e657159SMatthew D Fleming /* Output a trailing NUL. */ 4214e657159SMatthew D Fleming if (error == 0) 4224e657159SMatthew D Fleming error = SYSCTL_OUT(req, "", 1); 423eea4f254SJeff Roberson sbuf_delete(sb); 424eea4f254SJeff Roberson return (error); 425eea4f254SJeff Roberson } 426eea4f254SJeff Roberson 427eea4f254SJeff Roberson static int 428eea4f254SJeff Roberson enable_lock_prof(SYSCTL_HANDLER_ARGS) 429eea4f254SJeff Roberson { 430eea4f254SJeff Roberson int error, v; 431eea4f254SJeff Roberson 432eea4f254SJeff Roberson v = lock_prof_enable; 433eea4f254SJeff Roberson error = sysctl_handle_int(oidp, &v, v, req); 434eea4f254SJeff Roberson if (error) 435eea4f254SJeff Roberson return (error); 436eea4f254SJeff Roberson if (req->newptr == NULL) 437eea4f254SJeff Roberson return (error); 438eea4f254SJeff Roberson if (v == lock_prof_enable) 439eea4f254SJeff Roberson return (0); 440eea4f254SJeff Roberson if (v == 1) 441eea4f254SJeff Roberson lock_prof_reset(); 442eea4f254SJeff Roberson lock_prof_enable = !!v; 443eea4f254SJeff Roberson 444eea4f254SJeff Roberson return (0); 445eea4f254SJeff Roberson } 446eea4f254SJeff Roberson 447eea4f254SJeff Roberson static int 448eea4f254SJeff Roberson reset_lock_prof_stats(SYSCTL_HANDLER_ARGS) 449eea4f254SJeff Roberson { 450eea4f254SJeff Roberson int error, v; 451eea4f254SJeff Roberson 452eea4f254SJeff Roberson v = 0; 453eea4f254SJeff Roberson error = sysctl_handle_int(oidp, &v, 0, req); 454eea4f254SJeff Roberson if (error) 455eea4f254SJeff Roberson return (error); 456eea4f254SJeff Roberson if (req->newptr == NULL) 457eea4f254SJeff Roberson return (error); 458eea4f254SJeff Roberson if (v == 0) 459eea4f254SJeff Roberson return (0); 460eea4f254SJeff Roberson lock_prof_reset(); 461eea4f254SJeff Roberson 462eea4f254SJeff Roberson return (0); 463eea4f254SJeff Roberson } 464eea4f254SJeff Roberson 465eea4f254SJeff Roberson static struct lock_prof * 466eea4f254SJeff Roberson lock_profile_lookup(struct lock_object *lo, int spin, const char *file, 467eea4f254SJeff Roberson int line) 468eea4f254SJeff Roberson { 469eea4f254SJeff Roberson const char *unknown = "(unknown)"; 470eea4f254SJeff Roberson struct lock_prof_type *type; 471eea4f254SJeff Roberson struct lock_prof *lp; 472eea4f254SJeff Roberson struct lphead *head; 473eea4f254SJeff Roberson const char *p; 474eea4f254SJeff Roberson u_int hash; 475eea4f254SJeff Roberson 476eea4f254SJeff Roberson p = file; 477eea4f254SJeff Roberson if (p == NULL || *p == '\0') 478eea4f254SJeff Roberson p = unknown; 479eea4f254SJeff Roberson hash = (uintptr_t)lo->lo_name * 31 + (uintptr_t)p * 31 + line; 480eea4f254SJeff Roberson hash &= LPROF_HASH_MASK; 481eea4f254SJeff Roberson type = &lp_cpu[PCPU_GET(cpuid)]->lpc_types[spin]; 482eea4f254SJeff Roberson head = &type->lpt_hash[hash]; 483eea4f254SJeff Roberson SLIST_FOREACH(lp, head, link) { 484eea4f254SJeff Roberson if (lp->line == line && lp->file == p && 485eea4f254SJeff Roberson lp->name == lo->lo_name) 486eea4f254SJeff Roberson return (lp); 487eea4f254SJeff Roberson 488eea4f254SJeff Roberson } 489eea4f254SJeff Roberson lp = SLIST_FIRST(&type->lpt_lpalloc); 490eea4f254SJeff Roberson if (lp == NULL) { 491eea4f254SJeff Roberson lock_prof_rejected++; 492eea4f254SJeff Roberson return (lp); 493eea4f254SJeff Roberson } 494eea4f254SJeff Roberson SLIST_REMOVE_HEAD(&type->lpt_lpalloc, link); 495eea4f254SJeff Roberson lp->file = p; 496eea4f254SJeff Roberson lp->line = line; 4970c66dc67SJeff Roberson lp->class = LOCK_CLASS(lo); 498eea4f254SJeff Roberson lp->name = lo->lo_name; 499eea4f254SJeff Roberson SLIST_INSERT_HEAD(&type->lpt_hash[hash], lp, link); 500eea4f254SJeff Roberson return (lp); 501eea4f254SJeff Roberson } 502eea4f254SJeff Roberson 503eea4f254SJeff Roberson static struct lock_profile_object * 504eea4f254SJeff Roberson lock_profile_object_lookup(struct lock_object *lo, int spin, const char *file, 505eea4f254SJeff Roberson int line) 506eea4f254SJeff Roberson { 507eea4f254SJeff Roberson struct lock_profile_object *l; 508eea4f254SJeff Roberson struct lock_prof_type *type; 509eea4f254SJeff Roberson struct lpohead *head; 510eea4f254SJeff Roberson 511eea4f254SJeff Roberson head = &curthread->td_lprof[spin]; 512eea4f254SJeff Roberson LIST_FOREACH(l, head, lpo_link) 513eea4f254SJeff Roberson if (l->lpo_obj == lo && l->lpo_file == file && 514eea4f254SJeff Roberson l->lpo_line == line) 515eea4f254SJeff Roberson return (l); 516eea4f254SJeff Roberson type = &lp_cpu[PCPU_GET(cpuid)]->lpc_types[spin]; 517eea4f254SJeff Roberson l = LIST_FIRST(&type->lpt_lpoalloc); 518eea4f254SJeff Roberson if (l == NULL) { 519eea4f254SJeff Roberson lock_prof_rejected++; 520eea4f254SJeff Roberson return (NULL); 521eea4f254SJeff Roberson } 522eea4f254SJeff Roberson LIST_REMOVE(l, lpo_link); 523eea4f254SJeff Roberson l->lpo_obj = lo; 524eea4f254SJeff Roberson l->lpo_file = file; 525eea4f254SJeff Roberson l->lpo_line = line; 526eea4f254SJeff Roberson l->lpo_cnt = 0; 527eea4f254SJeff Roberson LIST_INSERT_HEAD(head, l, lpo_link); 528eea4f254SJeff Roberson 529eea4f254SJeff Roberson return (l); 530eea4f254SJeff Roberson } 531eea4f254SJeff Roberson 532eea4f254SJeff Roberson void 533eea4f254SJeff Roberson lock_profile_obtain_lock_success(struct lock_object *lo, int contested, 534eea4f254SJeff Roberson uint64_t waittime, const char *file, int line) 535eea4f254SJeff Roberson { 536eea4f254SJeff Roberson static int lock_prof_count; 537eea4f254SJeff Roberson struct lock_profile_object *l; 538eea4f254SJeff Roberson int spin; 539eea4f254SJeff Roberson 54035370593SAndriy Gapon if (SCHEDULER_STOPPED()) 54135370593SAndriy Gapon return; 54235370593SAndriy Gapon 543eea4f254SJeff Roberson /* don't reset the timer when/if recursing */ 544eea4f254SJeff Roberson if (!lock_prof_enable || (lo->lo_flags & LO_NOPROFILE)) 545eea4f254SJeff Roberson return; 546eea4f254SJeff Roberson if (lock_prof_skipcount && 547357911ceSKris Kennaway (++lock_prof_count % lock_prof_skipcount) != 0) 548eea4f254SJeff Roberson return; 54913ddf72dSAttilio Rao spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0; 550eea4f254SJeff Roberson if (spin && lock_prof_skipspin == 1) 551eea4f254SJeff Roberson return; 5522e6b8de4SJeff Roberson critical_enter(); 5532e6b8de4SJeff Roberson /* Recheck enabled now that we're in a critical section. */ 5542e6b8de4SJeff Roberson if (lock_prof_enable == 0) 5552e6b8de4SJeff Roberson goto out; 556eea4f254SJeff Roberson l = lock_profile_object_lookup(lo, spin, file, line); 557eea4f254SJeff Roberson if (l == NULL) 5582e6b8de4SJeff Roberson goto out; 559eea4f254SJeff Roberson l->lpo_cnt++; 560eea4f254SJeff Roberson if (++l->lpo_ref > 1) 5612e6b8de4SJeff Roberson goto out; 562eea4f254SJeff Roberson l->lpo_contest_locking = contested; 5637c0435b9SKip Macy l->lpo_acqtime = nanoseconds(); 564aa077979SKip Macy if (waittime && (l->lpo_acqtime > waittime)) 5657c0435b9SKip Macy l->lpo_waittime = l->lpo_acqtime - waittime; 566aa077979SKip Macy else 567aa077979SKip Macy l->lpo_waittime = 0; 5682e6b8de4SJeff Roberson out: 5692e6b8de4SJeff Roberson critical_exit(); 5702e6b8de4SJeff Roberson } 5712e6b8de4SJeff Roberson 5722e6b8de4SJeff Roberson void 5732e6b8de4SJeff Roberson lock_profile_thread_exit(struct thread *td) 5742e6b8de4SJeff Roberson { 5752e6b8de4SJeff Roberson #ifdef INVARIANTS 5762e6b8de4SJeff Roberson struct lock_profile_object *l; 5772e6b8de4SJeff Roberson 5782e6b8de4SJeff Roberson MPASS(curthread->td_critnest == 0); 5792e6b8de4SJeff Roberson #endif 5802e6b8de4SJeff Roberson /* 5812e6b8de4SJeff Roberson * If lock profiling was disabled we have to wait for reset to 5822e6b8de4SJeff Roberson * clear our pointers before we can exit safely. 5832e6b8de4SJeff Roberson */ 5842e6b8de4SJeff Roberson lock_prof_reset_wait(); 5852e6b8de4SJeff Roberson #ifdef INVARIANTS 5862e6b8de4SJeff Roberson LIST_FOREACH(l, &td->td_lprof[0], lpo_link) 5872e6b8de4SJeff Roberson printf("thread still holds lock acquired at %s:%d\n", 5882e6b8de4SJeff Roberson l->lpo_file, l->lpo_line); 5892e6b8de4SJeff Roberson LIST_FOREACH(l, &td->td_lprof[1], lpo_link) 5902e6b8de4SJeff Roberson printf("thread still holds lock acquired at %s:%d\n", 5912e6b8de4SJeff Roberson l->lpo_file, l->lpo_line); 5922e6b8de4SJeff Roberson #endif 5932e6b8de4SJeff Roberson MPASS(LIST_FIRST(&td->td_lprof[0]) == NULL); 5942e6b8de4SJeff Roberson MPASS(LIST_FIRST(&td->td_lprof[1]) == NULL); 5957c0435b9SKip Macy } 5967c0435b9SKip Macy 597eea4f254SJeff Roberson void 598eea4f254SJeff Roberson lock_profile_release_lock(struct lock_object *lo) 5997c0435b9SKip Macy { 600eea4f254SJeff Roberson struct lock_profile_object *l; 601eea4f254SJeff Roberson struct lock_prof_type *type; 602eea4f254SJeff Roberson struct lock_prof *lp; 60360ae52f7SEd Schouten uint64_t curtime, holdtime; 604eea4f254SJeff Roberson struct lpohead *head; 605eea4f254SJeff Roberson int spin; 6067c0435b9SKip Macy 60735370593SAndriy Gapon if (SCHEDULER_STOPPED()) 60835370593SAndriy Gapon return; 6092e6b8de4SJeff Roberson if (lo->lo_flags & LO_NOPROFILE) 6107c0435b9SKip Macy return; 61113ddf72dSAttilio Rao spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0; 612eea4f254SJeff Roberson head = &curthread->td_lprof[spin]; 6132e6b8de4SJeff Roberson if (LIST_FIRST(head) == NULL) 6142e6b8de4SJeff Roberson return; 615eea4f254SJeff Roberson critical_enter(); 6162e6b8de4SJeff Roberson /* Recheck enabled now that we're in a critical section. */ 6172e6b8de4SJeff Roberson if (lock_prof_enable == 0 && lock_prof_resetting == 1) 6182e6b8de4SJeff Roberson goto out; 6192e6b8de4SJeff Roberson /* 6202e6b8de4SJeff Roberson * If lock profiling is not enabled we still want to remove the 6212e6b8de4SJeff Roberson * lpo from our queue. 6222e6b8de4SJeff Roberson */ 623eea4f254SJeff Roberson LIST_FOREACH(l, head, lpo_link) 624eea4f254SJeff Roberson if (l->lpo_obj == lo) 6257c0435b9SKip Macy break; 626eea4f254SJeff Roberson if (l == NULL) 627eea4f254SJeff Roberson goto out; 628eea4f254SJeff Roberson if (--l->lpo_ref > 0) 629eea4f254SJeff Roberson goto out; 630eea4f254SJeff Roberson lp = lock_profile_lookup(lo, spin, l->lpo_file, l->lpo_line); 631eea4f254SJeff Roberson if (lp == NULL) 632eea4f254SJeff Roberson goto release; 633e7154e7eSAndriy Gapon curtime = nanoseconds(); 634e7154e7eSAndriy Gapon if (curtime < l->lpo_acqtime) 635eea4f254SJeff Roberson goto release; 636e7154e7eSAndriy Gapon holdtime = curtime - l->lpo_acqtime; 637e7154e7eSAndriy Gapon 6387c0435b9SKip Macy /* 63983b72e3eSKip Macy * Record if the lock has been held longer now than ever 6407c0435b9SKip Macy * before. 6417c0435b9SKip Macy */ 642eea4f254SJeff Roberson if (holdtime > lp->cnt_max) 643eea4f254SJeff Roberson lp->cnt_max = holdtime; 644947265b6SKip Macy if (l->lpo_waittime > lp->cnt_wait_max) 645947265b6SKip Macy lp->cnt_wait_max = l->lpo_waittime; 646eea4f254SJeff Roberson lp->cnt_tot += holdtime; 647eea4f254SJeff Roberson lp->cnt_wait += l->lpo_waittime; 648eea4f254SJeff Roberson lp->cnt_contest_locking += l->lpo_contest_locking; 649eea4f254SJeff Roberson lp->cnt_cur += l->lpo_cnt; 650eea4f254SJeff Roberson release: 651eea4f254SJeff Roberson LIST_REMOVE(l, lpo_link); 652eea4f254SJeff Roberson type = &lp_cpu[PCPU_GET(cpuid)]->lpc_types[spin]; 653eea4f254SJeff Roberson LIST_INSERT_HEAD(&type->lpt_lpoalloc, l, lpo_link); 654eea4f254SJeff Roberson out: 655eea4f254SJeff Roberson critical_exit(); 656eea4f254SJeff Roberson } 6577c0435b9SKip Macy 6586472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, lock, CTLFLAG_RD, NULL, "lock debugging"); 6596472ac3dSEd Schouten static SYSCTL_NODE(_debug_lock, OID_AUTO, prof, CTLFLAG_RD, NULL, 6606472ac3dSEd Schouten "lock profiling"); 661eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipspin, CTLFLAG_RW, 662eea4f254SJeff Roberson &lock_prof_skipspin, 0, "Skip profiling on spinlocks."); 663eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipcount, CTLFLAG_RW, 664eea4f254SJeff Roberson &lock_prof_skipcount, 0, "Sample approximately every N lock acquisitions."); 665eea4f254SJeff Roberson SYSCTL_INT(_debug_lock_prof, OID_AUTO, rejected, CTLFLAG_RD, 666eea4f254SJeff Roberson &lock_prof_rejected, 0, "Number of rejected profiling records"); 667eea4f254SJeff Roberson SYSCTL_PROC(_debug_lock_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD, 668eea4f254SJeff Roberson NULL, 0, dump_lock_prof_stats, "A", "Lock profiling statistics"); 669eea4f254SJeff Roberson SYSCTL_PROC(_debug_lock_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW, 670eea4f254SJeff Roberson NULL, 0, reset_lock_prof_stats, "I", "Reset lock profiling statistics"); 671eea4f254SJeff Roberson SYSCTL_PROC(_debug_lock_prof, OID_AUTO, enable, CTLTYPE_INT | CTLFLAG_RW, 672eea4f254SJeff Roberson NULL, 0, enable_lock_prof, "I", "Enable lock profiling"); 673eea4f254SJeff Roberson 6747c0435b9SKip Macy #endif 675