1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006 John Baldwin <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * This module holds the global variables and functions used to maintain
30 * lock_object structures.
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "opt_ddb.h"
37 #include "opt_mprof.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/ktr.h>
43 #include <sys/lock.h>
44 #include <sys/lock_profile.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/pcpu.h>
48 #include <sys/proc.h>
49 #include <sys/sbuf.h>
50 #include <sys/sched.h>
51 #include <sys/smp.h>
52 #include <sys/sysctl.h>
53
54 #ifdef DDB
55 #include <ddb/ddb.h>
56 #endif
57
58 #include <machine/cpufunc.h>
59
60 SDT_PROVIDER_DEFINE(lock);
61 SDT_PROBE_DEFINE1(lock, , , starvation, "u_int");
62
63 CTASSERT(LOCK_CLASS_MAX == 15);
64
65 struct lock_class *lock_classes[LOCK_CLASS_MAX + 1] = {
66 &lock_class_mtx_spin,
67 &lock_class_mtx_sleep,
68 &lock_class_sx,
69 &lock_class_rm,
70 &lock_class_rm_sleepable,
71 &lock_class_rw,
72 &lock_class_lockmgr,
73 };
74
75 void
lock_init(struct lock_object * lock,struct lock_class * class,const char * name,const char * type,int flags)76 lock_init(struct lock_object *lock, struct lock_class *class, const char *name,
77 const char *type, int flags)
78 {
79 int i;
80
81 /* Check for double-init and zero object. */
82 KASSERT(flags & LO_NEW || !lock_initialized(lock),
83 ("lock \"%s\" %p already initialized", name, lock));
84
85 /* Look up lock class to find its index. */
86 for (i = 0; i < LOCK_CLASS_MAX; i++)
87 if (lock_classes[i] == class) {
88 lock->lo_flags = i << LO_CLASSSHIFT;
89 break;
90 }
91 KASSERT(i < LOCK_CLASS_MAX, ("unknown lock class %p", class));
92
93 /* Initialize the lock object. */
94 lock->lo_name = name;
95 lock->lo_flags |= flags | LO_INITIALIZED;
96 LOCK_LOG_INIT(lock, 0);
97 WITNESS_INIT(lock, (type != NULL) ? type : name);
98 }
99
100 void
lock_destroy(struct lock_object * lock)101 lock_destroy(struct lock_object *lock)
102 {
103
104 KASSERT(lock_initialized(lock), ("lock %p is not initialized", lock));
105 WITNESS_DESTROY(lock);
106 LOCK_LOG_DESTROY(lock, 0);
107 lock->lo_flags &= ~LO_INITIALIZED;
108 }
109
110 static SYSCTL_NODE(_debug, OID_AUTO, lock, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
111 "lock debugging");
112 static SYSCTL_NODE(_debug_lock, OID_AUTO, delay,
113 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
114 "lock delay");
115
116 static u_int __read_mostly starvation_limit = 131072;
117 SYSCTL_INT(_debug_lock_delay, OID_AUTO, starvation_limit, CTLFLAG_RW,
118 &starvation_limit, 0, "");
119
120 static u_int __read_mostly restrict_starvation = 0;
121 SYSCTL_INT(_debug_lock_delay, OID_AUTO, restrict_starvation, CTLFLAG_RW,
122 &restrict_starvation, 0, "");
123
124 void
lock_delay(struct lock_delay_arg * la)125 lock_delay(struct lock_delay_arg *la)
126 {
127 struct lock_delay_config *lc = la->config;
128 u_short i;
129
130 la->delay <<= 1;
131 if (__predict_false(la->delay > lc->max))
132 la->delay = lc->max;
133
134 for (i = la->delay; i > 0; i--)
135 cpu_spinwait();
136
137 la->spin_cnt += la->delay;
138 if (__predict_false(la->spin_cnt > starvation_limit)) {
139 SDT_PROBE1(lock, , , starvation, la->delay);
140 if (restrict_starvation)
141 la->delay = lc->base;
142 }
143 }
144
145 static u_int
lock_roundup_2(u_int val)146 lock_roundup_2(u_int val)
147 {
148 u_int res;
149
150 for (res = 1; res <= val; res <<= 1)
151 continue;
152
153 return (res);
154 }
155
156 void
lock_delay_default_init(struct lock_delay_config * lc)157 lock_delay_default_init(struct lock_delay_config *lc)
158 {
159
160 lc->base = 1;
161 lc->max = lock_roundup_2(mp_ncpus) * 256;
162 if (lc->max > 32678)
163 lc->max = 32678;
164 }
165
166 struct lock_delay_config __read_frequently locks_delay;
167 u_short __read_frequently locks_delay_retries;
168 u_short __read_frequently locks_delay_loops;
169
170 SYSCTL_U16(_debug_lock, OID_AUTO, delay_base, CTLFLAG_RW, &locks_delay.base,
171 0, "");
172 SYSCTL_U16(_debug_lock, OID_AUTO, delay_max, CTLFLAG_RW, &locks_delay.max,
173 0, "");
174 SYSCTL_U16(_debug_lock, OID_AUTO, delay_retries, CTLFLAG_RW, &locks_delay_retries,
175 0, "");
176 SYSCTL_U16(_debug_lock, OID_AUTO, delay_loops, CTLFLAG_RW, &locks_delay_loops,
177 0, "");
178
179 static void
locks_delay_init(void * arg __unused)180 locks_delay_init(void *arg __unused)
181 {
182
183 lock_delay_default_init(&locks_delay);
184 locks_delay_retries = 10;
185 locks_delay_loops = max(10000, locks_delay.max);
186 }
187 LOCK_DELAY_SYSINIT(locks_delay_init);
188
189 #ifdef DDB
DB_SHOW_COMMAND(lock,db_show_lock)190 DB_SHOW_COMMAND(lock, db_show_lock)
191 {
192 struct lock_object *lock;
193 struct lock_class *class;
194
195 if (!have_addr)
196 return;
197 lock = (struct lock_object *)addr;
198 if (LO_CLASSINDEX(lock) > LOCK_CLASS_MAX) {
199 db_printf("Unknown lock class: %d\n", LO_CLASSINDEX(lock));
200 return;
201 }
202 class = LOCK_CLASS(lock);
203 db_printf(" class: %s\n", class->lc_name);
204 db_printf(" name: %s\n", lock->lo_name);
205 class->lc_ddb_show(lock);
206 }
207 #endif
208
209 #ifdef LOCK_PROFILING
210
211 /*
212 * One object per-thread for each lock the thread owns. Tracks individual
213 * lock instances.
214 */
215 struct lock_profile_object {
216 LIST_ENTRY(lock_profile_object) lpo_link;
217 struct lock_object *lpo_obj;
218 const char *lpo_file;
219 int lpo_line;
220 uint16_t lpo_ref;
221 uint16_t lpo_cnt;
222 uint64_t lpo_acqtime;
223 uint64_t lpo_waittime;
224 u_int lpo_contest_locking;
225 };
226
227 /*
228 * One lock_prof for each (file, line, lock object) triple.
229 */
230 struct lock_prof {
231 SLIST_ENTRY(lock_prof) link;
232 struct lock_class *class;
233 const char *file;
234 const char *name;
235 int line;
236 int ticks;
237 uintmax_t cnt_wait_max;
238 uintmax_t cnt_max;
239 uintmax_t cnt_tot;
240 uintmax_t cnt_wait;
241 uintmax_t cnt_cur;
242 uintmax_t cnt_contest_locking;
243 };
244
245 SLIST_HEAD(lphead, lock_prof);
246
247 #define LPROF_HASH_SIZE 4096
248 #define LPROF_HASH_MASK (LPROF_HASH_SIZE - 1)
249 #define LPROF_CACHE_SIZE 4096
250
251 /*
252 * Array of objects and profs for each type of object for each cpu. Spinlocks
253 * are handled separately because a thread may be preempted and acquire a
254 * spinlock while in the lock profiling code of a non-spinlock. In this way
255 * we only need a critical section to protect the per-cpu lists.
256 */
257 struct lock_prof_type {
258 struct lphead lpt_lpalloc;
259 struct lpohead lpt_lpoalloc;
260 struct lphead lpt_hash[LPROF_HASH_SIZE];
261 struct lock_prof lpt_prof[LPROF_CACHE_SIZE];
262 struct lock_profile_object lpt_objs[LPROF_CACHE_SIZE];
263 };
264
265 struct lock_prof_cpu {
266 struct lock_prof_type lpc_types[2]; /* One for spin one for other. */
267 };
268
269 DPCPU_DEFINE_STATIC(struct lock_prof_cpu, lp);
270 #define LP_CPU_SELF (DPCPU_PTR(lp))
271 #define LP_CPU(cpu) (DPCPU_ID_PTR((cpu), lp))
272
273 volatile int __read_mostly lock_prof_enable;
274 static volatile int lock_prof_resetting;
275
276 #define LPROF_SBUF_SIZE 256
277
278 static int lock_prof_rejected;
279 static int lock_prof_skipspin;
280 static int lock_prof_skipcount;
281
282 #ifndef USE_CPU_NANOSECONDS
283 uint64_t
nanoseconds(void)284 nanoseconds(void)
285 {
286 struct bintime bt;
287 uint64_t ns;
288
289 binuptime(&bt);
290 /* From bintime2timespec */
291 ns = bt.sec * (uint64_t)1000000000;
292 ns += ((uint64_t)1000000000 * (uint32_t)(bt.frac >> 32)) >> 32;
293 return (ns);
294 }
295 #endif
296
297 static void
lock_prof_init_type(struct lock_prof_type * type)298 lock_prof_init_type(struct lock_prof_type *type)
299 {
300 int i;
301
302 SLIST_INIT(&type->lpt_lpalloc);
303 LIST_INIT(&type->lpt_lpoalloc);
304 for (i = 0; i < LPROF_CACHE_SIZE; i++) {
305 SLIST_INSERT_HEAD(&type->lpt_lpalloc, &type->lpt_prof[i],
306 link);
307 LIST_INSERT_HEAD(&type->lpt_lpoalloc, &type->lpt_objs[i],
308 lpo_link);
309 }
310 }
311
312 static void
lock_prof_init(void * arg)313 lock_prof_init(void *arg)
314 {
315 int cpu;
316
317 CPU_FOREACH(cpu) {
318 lock_prof_init_type(&LP_CPU(cpu)->lpc_types[0]);
319 lock_prof_init_type(&LP_CPU(cpu)->lpc_types[1]);
320 }
321 }
322 SYSINIT(lockprof, SI_SUB_SMP, SI_ORDER_ANY, lock_prof_init, NULL);
323
324 static void
lock_prof_reset_wait(void)325 lock_prof_reset_wait(void)
326 {
327
328 /*
329 * Spin relinquishing our cpu so that quiesce_all_cpus may
330 * complete.
331 */
332 while (lock_prof_resetting)
333 sched_relinquish(curthread);
334 }
335
336 static void
lock_prof_reset(void)337 lock_prof_reset(void)
338 {
339 struct lock_prof_cpu *lpc;
340 int enabled, i, cpu;
341
342 /*
343 * We not only race with acquiring and releasing locks but also
344 * thread exit. To be certain that threads exit without valid head
345 * pointers they must see resetting set before enabled is cleared.
346 * Otherwise a lock may not be removed from a per-thread list due
347 * to disabled being set but not wait for reset() to remove it below.
348 */
349 atomic_store_rel_int(&lock_prof_resetting, 1);
350 enabled = lock_prof_enable;
351 lock_prof_enable = 0;
352 /*
353 * This both publishes lock_prof_enable as disabled and makes sure
354 * everyone else reads it if they are not far enough. We wait for the
355 * rest down below.
356 */
357 cpus_fence_seq_cst();
358 quiesce_all_critical();
359 /*
360 * Some objects may have migrated between CPUs. Clear all links
361 * before we zero the structures. Some items may still be linked
362 * into per-thread lists as well.
363 */
364 CPU_FOREACH(cpu) {
365 lpc = LP_CPU(cpu);
366 for (i = 0; i < LPROF_CACHE_SIZE; i++) {
367 LIST_REMOVE(&lpc->lpc_types[0].lpt_objs[i], lpo_link);
368 LIST_REMOVE(&lpc->lpc_types[1].lpt_objs[i], lpo_link);
369 }
370 }
371 CPU_FOREACH(cpu) {
372 lpc = LP_CPU(cpu);
373 bzero(lpc, sizeof(*lpc));
374 lock_prof_init_type(&lpc->lpc_types[0]);
375 lock_prof_init_type(&lpc->lpc_types[1]);
376 }
377 /*
378 * Paired with the fence from cpus_fence_seq_cst()
379 */
380 atomic_store_rel_int(&lock_prof_resetting, 0);
381 lock_prof_enable = enabled;
382 }
383
384 static void
lock_prof_output(struct lock_prof * lp,struct sbuf * sb)385 lock_prof_output(struct lock_prof *lp, struct sbuf *sb)
386 {
387 const char *p;
388
389 for (p = lp->file; p != NULL && strncmp(p, "../", 3) == 0; p += 3);
390 sbuf_printf(sb,
391 "%8ju %9ju %11ju %11ju %11ju %6ju %6ju %2ju %6ju %s:%d (%s:%s)\n",
392 lp->cnt_max / 1000, lp->cnt_wait_max / 1000, lp->cnt_tot / 1000,
393 lp->cnt_wait / 1000, lp->cnt_cur,
394 lp->cnt_cur == 0 ? (uintmax_t)0 :
395 lp->cnt_tot / (lp->cnt_cur * 1000),
396 lp->cnt_cur == 0 ? (uintmax_t)0 :
397 lp->cnt_wait / (lp->cnt_cur * 1000),
398 (uintmax_t)0, lp->cnt_contest_locking,
399 p, lp->line, lp->class->lc_name, lp->name);
400 }
401
402 static void
lock_prof_sum(struct lock_prof * match,struct lock_prof * dst,int hash,int spin,int t)403 lock_prof_sum(struct lock_prof *match, struct lock_prof *dst, int hash,
404 int spin, int t)
405 {
406 struct lock_prof_type *type;
407 struct lock_prof *l;
408 int cpu;
409
410 dst->file = match->file;
411 dst->line = match->line;
412 dst->class = match->class;
413 dst->name = match->name;
414
415 CPU_FOREACH(cpu) {
416 type = &LP_CPU(cpu)->lpc_types[spin];
417 SLIST_FOREACH(l, &type->lpt_hash[hash], link) {
418 if (l->ticks == t)
419 continue;
420 if (l->file != match->file || l->line != match->line ||
421 l->name != match->name)
422 continue;
423 l->ticks = t;
424 if (l->cnt_max > dst->cnt_max)
425 dst->cnt_max = l->cnt_max;
426 if (l->cnt_wait_max > dst->cnt_wait_max)
427 dst->cnt_wait_max = l->cnt_wait_max;
428 dst->cnt_tot += l->cnt_tot;
429 dst->cnt_wait += l->cnt_wait;
430 dst->cnt_cur += l->cnt_cur;
431 dst->cnt_contest_locking += l->cnt_contest_locking;
432 }
433 }
434 }
435
436 static void
lock_prof_type_stats(struct lock_prof_type * type,struct sbuf * sb,int spin,int t)437 lock_prof_type_stats(struct lock_prof_type *type, struct sbuf *sb, int spin,
438 int t)
439 {
440 struct lock_prof *l;
441 int i;
442
443 for (i = 0; i < LPROF_HASH_SIZE; ++i) {
444 SLIST_FOREACH(l, &type->lpt_hash[i], link) {
445 struct lock_prof lp = {};
446
447 if (l->ticks == t)
448 continue;
449 lock_prof_sum(l, &lp, i, spin, t);
450 lock_prof_output(&lp, sb);
451 }
452 }
453 }
454
455 static int
dump_lock_prof_stats(SYSCTL_HANDLER_ARGS)456 dump_lock_prof_stats(SYSCTL_HANDLER_ARGS)
457 {
458 struct sbuf *sb;
459 int error, cpu, t;
460 int enabled;
461
462 error = sysctl_wire_old_buffer(req, 0);
463 if (error != 0)
464 return (error);
465 sb = sbuf_new_for_sysctl(NULL, NULL, LPROF_SBUF_SIZE, req);
466 sbuf_printf(sb, "\n%8s %9s %11s %11s %11s %6s %6s %2s %6s %s\n",
467 "max", "wait_max", "total", "wait_total", "count", "avg", "wait_avg", "cnt_hold", "cnt_lock", "name");
468 enabled = lock_prof_enable;
469 lock_prof_enable = 0;
470 /*
471 * See the comment in lock_prof_reset
472 */
473 cpus_fence_seq_cst();
474 quiesce_all_critical();
475 t = ticks;
476 CPU_FOREACH(cpu) {
477 lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[0], sb, 0, t);
478 lock_prof_type_stats(&LP_CPU(cpu)->lpc_types[1], sb, 1, t);
479 }
480 atomic_thread_fence_rel();
481 lock_prof_enable = enabled;
482
483 error = sbuf_finish(sb);
484 /* Output a trailing NUL. */
485 if (error == 0)
486 error = SYSCTL_OUT(req, "", 1);
487 sbuf_delete(sb);
488 return (error);
489 }
490
491 static int
enable_lock_prof(SYSCTL_HANDLER_ARGS)492 enable_lock_prof(SYSCTL_HANDLER_ARGS)
493 {
494 int error, v;
495
496 v = lock_prof_enable;
497 error = sysctl_handle_int(oidp, &v, v, req);
498 if (error)
499 return (error);
500 if (req->newptr == NULL)
501 return (error);
502 if (v == lock_prof_enable)
503 return (0);
504 if (v == 1)
505 lock_prof_reset();
506 lock_prof_enable = !!v;
507
508 return (0);
509 }
510
511 static int
reset_lock_prof_stats(SYSCTL_HANDLER_ARGS)512 reset_lock_prof_stats(SYSCTL_HANDLER_ARGS)
513 {
514 int error, v;
515
516 v = 0;
517 error = sysctl_handle_int(oidp, &v, 0, req);
518 if (error)
519 return (error);
520 if (req->newptr == NULL)
521 return (error);
522 if (v == 0)
523 return (0);
524 lock_prof_reset();
525
526 return (0);
527 }
528
529 static struct lock_prof *
lock_profile_lookup(struct lock_object * lo,int spin,const char * file,int line)530 lock_profile_lookup(struct lock_object *lo, int spin, const char *file,
531 int line)
532 {
533 const char *unknown = "(unknown)";
534 struct lock_prof_type *type;
535 struct lock_prof *lp;
536 struct lphead *head;
537 const char *p;
538 u_int hash;
539
540 p = file;
541 if (p == NULL || *p == '\0')
542 p = unknown;
543 hash = (uintptr_t)lo->lo_name * 31 + (uintptr_t)p * 31 + line;
544 hash &= LPROF_HASH_MASK;
545 type = &LP_CPU_SELF->lpc_types[spin];
546 head = &type->lpt_hash[hash];
547 SLIST_FOREACH(lp, head, link) {
548 if (lp->line == line && lp->file == p &&
549 lp->name == lo->lo_name)
550 return (lp);
551 }
552 lp = SLIST_FIRST(&type->lpt_lpalloc);
553 if (lp == NULL) {
554 lock_prof_rejected++;
555 return (lp);
556 }
557 SLIST_REMOVE_HEAD(&type->lpt_lpalloc, link);
558 lp->file = p;
559 lp->line = line;
560 lp->class = LOCK_CLASS(lo);
561 lp->name = lo->lo_name;
562 SLIST_INSERT_HEAD(&type->lpt_hash[hash], lp, link);
563 return (lp);
564 }
565
566 static struct lock_profile_object *
lock_profile_object_lookup(struct lock_object * lo,int spin,const char * file,int line)567 lock_profile_object_lookup(struct lock_object *lo, int spin, const char *file,
568 int line)
569 {
570 struct lock_profile_object *l;
571 struct lock_prof_type *type;
572 struct lpohead *head;
573
574 head = &curthread->td_lprof[spin];
575 LIST_FOREACH(l, head, lpo_link)
576 if (l->lpo_obj == lo && l->lpo_file == file &&
577 l->lpo_line == line)
578 return (l);
579 type = &LP_CPU_SELF->lpc_types[spin];
580 l = LIST_FIRST(&type->lpt_lpoalloc);
581 if (l == NULL) {
582 lock_prof_rejected++;
583 return (NULL);
584 }
585 LIST_REMOVE(l, lpo_link);
586 l->lpo_obj = lo;
587 l->lpo_file = file;
588 l->lpo_line = line;
589 l->lpo_cnt = 0;
590 LIST_INSERT_HEAD(head, l, lpo_link);
591
592 return (l);
593 }
594
595 void
lock_profile_obtain_lock_success(struct lock_object * lo,int contested,uint64_t waittime,const char * file,int line)596 lock_profile_obtain_lock_success(struct lock_object *lo, int contested,
597 uint64_t waittime, const char *file, int line)
598 {
599 static int lock_prof_count;
600 struct lock_profile_object *l;
601 int spin;
602
603 if (SCHEDULER_STOPPED())
604 return;
605
606 /* don't reset the timer when/if recursing */
607 if (!lock_prof_enable || (lo->lo_flags & LO_NOPROFILE))
608 return;
609 if (lock_prof_skipcount &&
610 (++lock_prof_count % lock_prof_skipcount) != 0)
611 return;
612 spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0;
613 if (spin && lock_prof_skipspin == 1)
614 return;
615 critical_enter();
616 /* Recheck enabled now that we're in a critical section. */
617 if (lock_prof_enable == 0)
618 goto out;
619 l = lock_profile_object_lookup(lo, spin, file, line);
620 if (l == NULL)
621 goto out;
622 l->lpo_cnt++;
623 if (++l->lpo_ref > 1)
624 goto out;
625 l->lpo_contest_locking = contested;
626 l->lpo_acqtime = nanoseconds();
627 if (waittime && (l->lpo_acqtime > waittime))
628 l->lpo_waittime = l->lpo_acqtime - waittime;
629 else
630 l->lpo_waittime = 0;
631 out:
632 /*
633 * Paired with cpus_fence_seq_cst().
634 */
635 atomic_thread_fence_rel();
636 critical_exit();
637 }
638
639 void
lock_profile_thread_exit(struct thread * td)640 lock_profile_thread_exit(struct thread *td)
641 {
642 #ifdef INVARIANTS
643 struct lock_profile_object *l;
644
645 MPASS(curthread->td_critnest == 0);
646 #endif
647 /*
648 * If lock profiling was disabled we have to wait for reset to
649 * clear our pointers before we can exit safely.
650 */
651 lock_prof_reset_wait();
652 #ifdef INVARIANTS
653 LIST_FOREACH(l, &td->td_lprof[0], lpo_link)
654 printf("thread still holds lock acquired at %s:%d\n",
655 l->lpo_file, l->lpo_line);
656 LIST_FOREACH(l, &td->td_lprof[1], lpo_link)
657 printf("thread still holds lock acquired at %s:%d\n",
658 l->lpo_file, l->lpo_line);
659 #endif
660 MPASS(LIST_FIRST(&td->td_lprof[0]) == NULL);
661 MPASS(LIST_FIRST(&td->td_lprof[1]) == NULL);
662 }
663
664 void
lock_profile_release_lock(struct lock_object * lo)665 lock_profile_release_lock(struct lock_object *lo)
666 {
667 struct lock_profile_object *l;
668 struct lock_prof_type *type;
669 struct lock_prof *lp;
670 uint64_t curtime, holdtime;
671 struct lpohead *head;
672 int spin;
673
674 if (SCHEDULER_STOPPED())
675 return;
676 if (lo->lo_flags & LO_NOPROFILE)
677 return;
678 spin = (LOCK_CLASS(lo)->lc_flags & LC_SPINLOCK) ? 1 : 0;
679 head = &curthread->td_lprof[spin];
680 if (LIST_FIRST(head) == NULL)
681 return;
682 critical_enter();
683 /* Recheck enabled now that we're in a critical section. */
684 if (lock_prof_enable == 0 && lock_prof_resetting == 1)
685 goto out;
686 /*
687 * If lock profiling is not enabled we still want to remove the
688 * lpo from our queue.
689 */
690 LIST_FOREACH(l, head, lpo_link)
691 if (l->lpo_obj == lo)
692 break;
693 if (l == NULL)
694 goto out;
695 if (--l->lpo_ref > 0)
696 goto out;
697 lp = lock_profile_lookup(lo, spin, l->lpo_file, l->lpo_line);
698 if (lp == NULL)
699 goto release;
700 curtime = nanoseconds();
701 if (curtime < l->lpo_acqtime)
702 goto release;
703 holdtime = curtime - l->lpo_acqtime;
704
705 /*
706 * Record if the lock has been held longer now than ever
707 * before.
708 */
709 if (holdtime > lp->cnt_max)
710 lp->cnt_max = holdtime;
711 if (l->lpo_waittime > lp->cnt_wait_max)
712 lp->cnt_wait_max = l->lpo_waittime;
713 lp->cnt_tot += holdtime;
714 lp->cnt_wait += l->lpo_waittime;
715 lp->cnt_contest_locking += l->lpo_contest_locking;
716 lp->cnt_cur += l->lpo_cnt;
717 release:
718 LIST_REMOVE(l, lpo_link);
719 type = &LP_CPU_SELF->lpc_types[spin];
720 LIST_INSERT_HEAD(&type->lpt_lpoalloc, l, lpo_link);
721 out:
722 /*
723 * Paired with cpus_fence_seq_cst().
724 */
725 atomic_thread_fence_rel();
726 critical_exit();
727 }
728
729 static SYSCTL_NODE(_debug_lock, OID_AUTO, prof,
730 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
731 "lock profiling");
732 SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipspin, CTLFLAG_RW,
733 &lock_prof_skipspin, 0, "Skip profiling on spinlocks.");
734 SYSCTL_INT(_debug_lock_prof, OID_AUTO, skipcount, CTLFLAG_RW,
735 &lock_prof_skipcount, 0, "Sample approximately every N lock acquisitions.");
736 SYSCTL_INT(_debug_lock_prof, OID_AUTO, rejected, CTLFLAG_RD,
737 &lock_prof_rejected, 0, "Number of rejected profiling records");
738 SYSCTL_PROC(_debug_lock_prof, OID_AUTO, stats,
739 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
740 dump_lock_prof_stats, "A",
741 "Lock profiling statistics");
742 SYSCTL_PROC(_debug_lock_prof, OID_AUTO, reset,
743 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, NULL, 0,
744 reset_lock_prof_stats, "I",
745 "Reset lock profiling statistics");
746 SYSCTL_PROC(_debug_lock_prof, OID_AUTO, enable,
747 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
748 enable_lock_prof, "I",
749 "Enable lock profiling");
750
751 #endif
752