32 #include <sys/cdefs.h>
36 #include "opt_mprof.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
43 #include <sys/lock_profile.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
49 #include <sys/sched.h>
51 #include <sys/sysctl.h>
57 #include <machine/cpufunc.h>
72 lock_init(
struct lock_object *lock,
struct lock_class *
class,
const char *
name,
73 const char *
type,
int flags)
78 KASSERT(!lock_initalized(lock), (
"lock \"%s\" %p already initialized",
82 for (i = 0; i < LOCK_CLASS_MAX; i++)
84 lock->lo_flags = i << LO_CLASSSHIFT;
87 KASSERT(i < LOCK_CLASS_MAX, (
"unknown lock class %p",
class));
91 lock->lo_flags |= flags | LO_INITIALIZED;
92 LOCK_LOG_INIT(lock, 0);
93 WITNESS_INIT(lock, (type != NULL) ? type : name);
100 KASSERT(lock_initalized(lock), (
"lock %p is not initialized", lock));
101 WITNESS_DESTROY(lock);
102 LOCK_LOG_DESTROY(lock, 0);
103 lock->lo_flags &= ~LO_INITIALIZED;
107 DB_SHOW_COMMAND(lock, db_show_lock)
109 struct lock_object *lock;
110 struct lock_class *
class;
114 lock = (
struct lock_object *)addr;
115 if (LO_CLASSINDEX(lock) > LOCK_CLASS_MAX) {
116 db_printf(
"Unknown lock class: %d\n", LO_CLASSINDEX(lock));
119 class = LOCK_CLASS(lock);
120 db_printf(
" class: %s\n", class->lc_name);
121 db_printf(
" name: %s\n", lock->lo_name);
122 class->lc_ddb_show(lock);
126 #ifdef LOCK_PROFILING
132 struct lock_profile_object {
133 LIST_ENTRY(lock_profile_object) lpo_link;
134 struct lock_object *lpo_obj;
135 const
char *lpo_file;
139 uint64_t lpo_acqtime;
140 uint64_t lpo_waittime;
141 u_int lpo_contest_locking;
148 SLIST_ENTRY(lock_prof) link;
149 struct lock_class *class;
154 uintmax_t cnt_wait_max;
159 uintmax_t cnt_contest_locking;
164 #define LPROF_HASH_SIZE 4096
165 #define LPROF_HASH_MASK (LPROF_HASH_SIZE - 1)
166 #define LPROF_CACHE_SIZE 4096
174 struct lock_prof_type {
175 struct lphead lpt_lpalloc;
176 struct lpohead lpt_lpoalloc;
177 struct lphead lpt_hash[LPROF_HASH_SIZE];
178 struct lock_prof lpt_prof[LPROF_CACHE_SIZE];
179 struct lock_profile_object lpt_objs[LPROF_CACHE_SIZE];
182 struct lock_prof_cpu {
183 struct lock_prof_type lpc_types[2];
186 struct lock_prof_cpu *lp_cpu[MAXCPU];
188 volatile int lock_prof_enable = 0;
189 static volatile int lock_prof_resetting;
191 #define LPROF_SBUF_SIZE 256
193 static int lock_prof_rejected;
194 static int lock_prof_skipspin;
195 static int lock_prof_skipcount;
197 #ifndef USE_CPU_NANOSECONDS
206 ns = bt.sec * (uint64_t)1000000000;
207 ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
213 lock_prof_init_type(
struct lock_prof_type *
type)
217 SLIST_INIT(&type->lpt_lpalloc);
218 LIST_INIT(&type->lpt_lpoalloc);
219 for (i = 0; i < LPROF_CACHE_SIZE; i++) {
220 SLIST_INSERT_HEAD(&type->lpt_lpalloc, &type->lpt_prof[i],
222 LIST_INSERT_HEAD(&type->lpt_lpoalloc, &type->lpt_objs[i],
228 lock_prof_init(
void *arg)
232 for (cpu = 0; cpu <=
mp_maxid; cpu++) {
233 lp_cpu[cpu] =
malloc(
sizeof(*lp_cpu[cpu]), M_DEVBUF,
235 lock_prof_init_type(&lp_cpu[cpu]->lpc_types[0]);
236 lock_prof_init_type(&lp_cpu[cpu]->lpc_types[1]);
239 SYSINIT(lockprof, SI_SUB_SMP, SI_ORDER_ANY, lock_prof_init, NULL);
263 lock_prof_reset_wait(
void)
270 while (lock_prof_resetting)
275 lock_prof_reset(
void)
277 struct lock_prof_cpu *lpc;
287 atomic_store_rel_int(&lock_prof_resetting, 1);
288 enabled = lock_prof_enable;
289 lock_prof_enable = 0;
296 for (cpu = 0; cpu <=
mp_maxid; cpu++) {
298 for (i = 0; i < LPROF_CACHE_SIZE; i++) {
299 LIST_REMOVE(&lpc->lpc_types[0].lpt_objs[i], lpo_link);
300 LIST_REMOVE(&lpc->lpc_types[1].lpt_objs[i], lpo_link);
303 for (cpu = 0; cpu <=
mp_maxid; cpu++) {
305 bzero(lpc,
sizeof(*lpc));
306 lock_prof_init_type(&lpc->lpc_types[0]);
307 lock_prof_init_type(&lpc->lpc_types[1]);
309 atomic_store_rel_int(&lock_prof_resetting, 0);
310 lock_prof_enable = enabled;
314 lock_prof_output(
struct lock_prof *lp,
struct sbuf *sb)
318 for (p = lp->file; p != NULL && strncmp(p,
"../", 3) == 0; p += 3);
320 "%8ju %9ju %11ju %11ju %11ju %6ju %6ju %2ju %6ju %s:%d (%s:%s)\n",
321 lp->cnt_max / 1000, lp->cnt_wait_max / 1000, lp->cnt_tot / 1000,
322 lp->cnt_wait / 1000, lp->cnt_cur,
323 lp->cnt_cur == 0 ? (uintmax_t)0 :
324 lp->cnt_tot / (lp->cnt_cur * 1000),
325 lp->cnt_cur == 0 ? (uintmax_t)0 :
326 lp->cnt_wait / (lp->cnt_cur * 1000),
327 (uintmax_t)0, lp->cnt_contest_locking,
328 p, lp->line, lp->class->lc_name, lp->name);
332 lock_prof_sum(
struct lock_prof *match,
struct lock_prof *dst,
int hash,
335 struct lock_prof_type *
type;
339 dst->file = match->file;
340 dst->line = match->line;
341 dst->class = match->class;
342 dst->name = match->name;
344 for (cpu = 0; cpu <=
mp_maxid; cpu++) {
345 if (lp_cpu[cpu] == NULL)
347 type = &lp_cpu[cpu]->lpc_types[spin];
348 SLIST_FOREACH(l, &type->lpt_hash[hash], link) {
351 if (l->file != match->file || l->line != match->line ||
352 l->name != match->name)
355 if (l->cnt_max > dst->cnt_max)
356 dst->cnt_max = l->cnt_max;
357 if (l->cnt_wait_max > dst->cnt_wait_max)
358 dst->cnt_wait_max = l->cnt_wait_max;
359 dst->cnt_tot += l->cnt_tot;
360 dst->cnt_wait += l->cnt_wait;
361 dst->cnt_cur += l->cnt_cur;
362 dst->cnt_contest_locking += l->cnt_contest_locking;
369 lock_prof_type_stats(
struct lock_prof_type *type,
struct sbuf *sb,
int spin,
375 for (i = 0; i < LPROF_HASH_SIZE; ++i) {
376 SLIST_FOREACH(l, &type->lpt_hash[i], link) {
377 struct lock_prof lp = {};
381 lock_prof_sum(l, &lp, i, spin, t);
382 lock_prof_output(&lp, sb);
388 dump_lock_prof_stats(SYSCTL_HANDLER_ARGS)
398 sbuf_printf(sb,
"\n%8s %9s %11s %11s %11s %6s %6s %2s %6s %s\n",
399 "max",
"wait_max",
"total",
"wait_total",
"count",
"avg",
"wait_avg",
"cnt_hold",
"cnt_lock",
"name");
400 enabled = lock_prof_enable;
401 lock_prof_enable = 0;
404 for (cpu = 0; cpu <=
mp_maxid; cpu++) {
405 if (lp_cpu[cpu] == NULL)
407 lock_prof_type_stats(&lp_cpu[cpu]->lpc_types[0], sb, 0, t);
408 lock_prof_type_stats(&lp_cpu[cpu]->lpc_types[1], sb, 1, t);
410 lock_prof_enable = enabled;
415 error = SYSCTL_OUT(req,
"", 1);
421 enable_lock_prof(SYSCTL_HANDLER_ARGS)
425 v = lock_prof_enable;
429 if (req->newptr == NULL)
431 if (v == lock_prof_enable)
435 lock_prof_enable = !!v;
441 reset_lock_prof_stats(SYSCTL_HANDLER_ARGS)
449 if (req->newptr == NULL)
458 static struct lock_prof *
459 lock_profile_lookup(
struct lock_object *lo,
int spin,
const char *file,
462 const char *unknown =
"(unknown)";
463 struct lock_prof_type *
type;
464 struct lock_prof *lp;
470 if (p == NULL || *p ==
'\0')
472 hash = (uintptr_t)lo->lo_name * 31 + (uintptr_t)p * 31 + line;
473 hash &= LPROF_HASH_MASK;
474 type = &lp_cpu[PCPU_GET(cpuid)]->lpc_types[spin];
475 head = &type->lpt_hash[hash];
476 SLIST_FOREACH(lp, head, link) {
477 if (lp->line == line && lp->file == p &&
478 lp->name == lo->lo_name)
482 lp = SLIST_FIRST(&type->lpt_lpalloc);
484 lock_prof_rejected++;
487 SLIST_REMOVE_HEAD(&type->lpt_lpalloc, link);
490 lp->class = LOCK_CLASS(lo);
491 lp->name = lo->lo_name;
492 SLIST_INSERT_HEAD(&type->lpt_hash[hash], lp, link);
496 static struct lock_profile_object *
497 lock_profile_object_lookup(
struct lock_object *lo,
int spin,
const char *file,
500 struct lock_profile_object *l;
501 struct lock_prof_type *
type;
502 struct lpohead *head;
504 head = &curthread->td_lprof[spin];
505 LIST_FOREACH(l, head, lpo_link)
506 if (l->lpo_obj == lo && l->lpo_file == file &&
509 type = &lp_cpu[PCPU_GET(cpuid)]->lpc_types[spin];
510 l = LIST_FIRST(&type->lpt_lpoalloc);
512 lock_prof_rejected++;
515 LIST_REMOVE(l, lpo_link);
520 LIST_INSERT_HEAD(head, l, lpo_link);
526 lock_profile_obtain_lock_success(
struct lock_object *lo,
int contested,
527 uint64_t
waittime,
const char *file,
int line)
529 static int lock_prof_count;
530 struct lock_profile_object *l;
533 if (SCHEDULER_STOPPED())
537 if (!lock_prof_enable || (lo->lo_flags & LO_NOPROFILE))
539 if (lock_prof_skipcount &&
540 (++lock_prof_count % lock_prof_skipcount) != 0)
542 spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0;
543 if (spin && lock_prof_skipspin == 1)
547 if (lock_prof_enable == 0)
549 l = lock_profile_object_lookup(lo, spin, file, line);
553 if (++l->lpo_ref > 1)
555 l->lpo_contest_locking = contested;
556 l->lpo_acqtime = nanoseconds();
557 if (waittime && (l->lpo_acqtime > waittime))
558 l->lpo_waittime = l->lpo_acqtime -
waittime;
566 lock_profile_thread_exit(
struct thread *td)
569 struct lock_profile_object *l;
571 MPASS(curthread->td_critnest == 0);
577 lock_prof_reset_wait();
579 LIST_FOREACH(l, &td->td_lprof[0], lpo_link)
580 printf("thread still holds lock acquired at %s:%d\n",
581 l->lpo_file, l->lpo_line);
582 LIST_FOREACH(l, &td->td_lprof[1], lpo_link)
583 printf("thread still holds lock acquired at %s:%d\n",
584 l->lpo_file, l->lpo_line);
586 MPASS(LIST_FIRST(&td->td_lprof[0]) == NULL);
587 MPASS(LIST_FIRST(&td->td_lprof[1]) == NULL);
591 lock_profile_release_lock(
struct lock_object *lo)
593 struct lock_profile_object *l;
594 struct lock_prof_type *
type;
595 struct lock_prof *lp;
596 uint64_t curtime, holdtime;
597 struct lpohead *head;
600 if (SCHEDULER_STOPPED())
602 if (lo->lo_flags & LO_NOPROFILE)
604 spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0;
605 head = &curthread->td_lprof[spin];
606 if (LIST_FIRST(head) == NULL)
610 if (lock_prof_enable == 0 && lock_prof_resetting == 1)
616 LIST_FOREACH(l, head, lpo_link)
617 if (l->lpo_obj == lo)
621 if (--l->lpo_ref > 0)
623 lp = lock_profile_lookup(lo, spin, l->lpo_file, l->lpo_line);
626 curtime = nanoseconds();
627 if (curtime < l->lpo_acqtime)
629 holdtime = curtime - l->lpo_acqtime;
635 if (holdtime > lp->cnt_max)
636 lp->cnt_max = holdtime;
637 if (l->lpo_waittime > lp->cnt_wait_max)
638 lp->cnt_wait_max = l->lpo_waittime;
639 lp->cnt_tot += holdtime;
640 lp->cnt_wait += l->lpo_waittime;
641 lp->cnt_contest_locking += l->lpo_contest_locking;
642 lp->cnt_cur += l->lpo_cnt;
644 LIST_REMOVE(l, lpo_link);
645 type = &lp_cpu[PCPU_GET(cpuid)]->lpc_types[spin];
646 LIST_INSERT_HEAD(&type->lpt_lpoalloc, l, lpo_link);
651 static
SYSCTL_NODE(_debug, OID_AUTO, lock, CTLFLAG_RD, NULL, "lock debugging");
652 static
SYSCTL_NODE(_debug_lock, OID_AUTO, prof, CTLFLAG_RD, NULL,
654 SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipspin, CTLFLAG_RW,
655 &lock_prof_skipspin, 0, "Skip profiling on spinlocks.");
656 SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipcount, CTLFLAG_RW,
657 &lock_prof_skipcount, 0, "Sample approximately every N lock acquisitions.");
658 SYSCTL_INT(_debug_lock_prof, OID_AUTO, rejected, CTLFLAG_RD,
659 &lock_prof_rejected, 0, "Number of rejected profiling records");
660 SYSCTL_PROC(_debug_lock_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD,
661 NULL, 0, dump_lock_prof_stats, "A", "Lock profiling statistics");
662 SYSCTL_PROC(_debug_lock_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW,
663 NULL, 0, reset_lock_prof_stats, "
I", "Reset lock profiling statistics");
664 SYSCTL_PROC(_debug_lock_prof, OID_AUTO, enable, CTLTYPE_INT | CTLFLAG_RW,
665 NULL, 0, enable_lock_prof, "I", "Enable lock profiling");
struct lock_class lock_class_rm_sleepable
struct lock_class lock_class_mtx_spin
void sched_relinquish(struct thread *td)
static SYSCTL_NODE(_debug, OID_AUTO, cpufreq, CTLFLAG_RD, NULL,"cpufreq debugging")
struct lock_class lock_class_rm
CTASSERT(LOCK_CLASS_MAX==15)
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
struct lock_class lock_class_rw
SYSINIT(placeholder, SI_SUB_DUMMY, SI_ORDER_ANY, NULL, NULL)
SYSCTL_INT(_debug, OID_AUTO, boothowto, CTLFLAG_RD,&boothowto, 0,"Boot control flags, passed from loader")
void lock_init(struct lock_object *lock, struct lock_class *class, const char *name, const char *type, int flags)
int sbuf_printf(struct sbuf *s, const char *fmt,...)
struct lock_class lock_class_sx
int sysctl_handle_int(SYSCTL_HANDLER_ARGS)
struct lock_class * lock_classes[LOCK_CLASS_MAX+1]
struct lock_class lock_class_mtx_sleep
SYSCTL_PROC(_kern, OID_AUTO, acct_chkfreq, CTLTYPE_INT|CTLFLAG_RW,&acctchkfreq, 0, sysctl_acct_chkfreq,"I","frequency for checking the free space")
int printf(const char *fmt,...)
void sbuf_delete(struct sbuf *s)
int sysctl_wire_old_buffer(struct sysctl_req *req, size_t len)
struct lock_class lock_class_lockmgr
void bintime(struct bintime *bt)
void sched_bind(struct thread *td, int cpu)
int sbuf_finish(struct sbuf *s)
void lock_destroy(struct lock_object *lock)
SLIST_HEAD(et_eventtimers_list, eventtimer)
void binuptime(struct bintime *bt)
void sched_unbind(struct thread *td)
void critical_enter(void)
struct sbuf * sbuf_new_for_sysctl(struct sbuf *s, char *buf, int length, struct sysctl_req *req)