The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_lock.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 18fe74f1f89955fe604ff4c5c2436c00


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.