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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: 7f65880b8d267af01dc1ad30087d7042


[ 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.