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

Cache object: bc93166051a0058f83c8df0f72019f2d


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