Home | History | Annotate | Line # | Download | only in kern
kern_timeout.c revision 1.57.2.1
      1  1.57.2.1        ad /*	$NetBSD: kern_timeout.c,v 1.57.2.1 2020/01/25 22:38:51 ad Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*-
      4      1.57        ad  * Copyright (c) 2003, 2006, 2007, 2008, 2009, 2019 The NetBSD Foundation, Inc.
      5       1.1   thorpej  * All rights reserved.
      6       1.1   thorpej  *
      7       1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.22        ad  * by Jason R. Thorpe, and by Andrew Doran.
      9       1.1   thorpej  *
     10       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11       1.1   thorpej  * modification, are permitted provided that the following conditions
     12       1.1   thorpej  * are met:
     13       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18       1.1   thorpej  *
     19       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1   thorpej  */
     31       1.1   thorpej 
     32       1.1   thorpej /*
     33       1.1   thorpej  * Copyright (c) 2001 Thomas Nordin <nordin (at) openbsd.org>
     34       1.1   thorpej  * Copyright (c) 2000-2001 Artur Grabowski <art (at) openbsd.org>
     35      1.14     perry  * All rights reserved.
     36      1.14     perry  *
     37      1.14     perry  * Redistribution and use in source and binary forms, with or without
     38      1.14     perry  * modification, are permitted provided that the following conditions
     39      1.14     perry  * are met:
     40       1.1   thorpej  *
     41      1.14     perry  * 1. Redistributions of source code must retain the above copyright
     42      1.14     perry  *    notice, this list of conditions and the following disclaimer.
     43      1.14     perry  * 2. Redistributions in binary form must reproduce the above copyright
     44      1.14     perry  *    notice, this list of conditions and the following disclaimer in the
     45      1.14     perry  *    documentation and/or other materials provided with the distribution.
     46       1.1   thorpej  * 3. The name of the author may not be used to endorse or promote products
     47      1.14     perry  *    derived from this software without specific prior written permission.
     48       1.1   thorpej  *
     49       1.1   thorpej  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     50       1.1   thorpej  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     51       1.1   thorpej  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     52       1.1   thorpej  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     53       1.1   thorpej  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     54       1.1   thorpej  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     55       1.1   thorpej  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     56       1.1   thorpej  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     57       1.1   thorpej  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     58      1.14     perry  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59       1.1   thorpej  */
     60       1.7     lukem 
     61       1.7     lukem #include <sys/cdefs.h>
     62  1.57.2.1        ad __KERNEL_RCSID(0, "$NetBSD: kern_timeout.c,v 1.57.2.1 2020/01/25 22:38:51 ad Exp $");
     63       1.1   thorpej 
     64       1.1   thorpej /*
     65      1.22        ad  * Timeouts are kept in a hierarchical timing wheel.  The c_time is the
     66      1.36        ad  * value of c_cpu->cc_ticks when the timeout should be called.  There are
     67      1.36        ad  * four levels with 256 buckets each. See 'Scheme 7' in "Hashed and
     68      1.36        ad  * Hierarchical Timing Wheels: Efficient Data Structures for Implementing
     69      1.36        ad  * a Timer Facility" by George Varghese and Tony Lauck.
     70      1.22        ad  *
     71      1.22        ad  * Some of the "math" in here is a bit tricky.  We have to beware of
     72      1.22        ad  * wrapping ints.
     73      1.22        ad  *
     74      1.22        ad  * We use the fact that any element added to the queue must be added with
     75      1.22        ad  * a positive time.  That means that any element `to' on the queue cannot
     76      1.22        ad  * be scheduled to timeout further in time than INT_MAX, but c->c_time can
     77      1.22        ad  * be positive or negative so comparing it with anything is dangerous.
     78      1.22        ad  * The only way we can use the c->c_time value in any predictable way is
     79      1.22        ad  * when we calculate how far in the future `to' will timeout - "c->c_time
     80      1.36        ad  * - c->c_cpu->cc_ticks".  The result will always be positive for future
     81      1.22        ad  * timeouts and 0 or negative for due timeouts.
     82       1.1   thorpej  */
     83       1.1   thorpej 
     84      1.24        ad #define	_CALLOUT_PRIVATE
     85      1.24        ad 
     86       1.1   thorpej #include <sys/param.h>
     87       1.1   thorpej #include <sys/systm.h>
     88       1.1   thorpej #include <sys/kernel.h>
     89       1.1   thorpej #include <sys/callout.h>
     90      1.45     rmind #include <sys/lwp.h>
     91      1.20        ad #include <sys/mutex.h>
     92      1.22        ad #include <sys/proc.h>
     93      1.22        ad #include <sys/sleepq.h>
     94      1.22        ad #include <sys/syncobj.h>
     95      1.22        ad #include <sys/evcnt.h>
     96      1.27        ad #include <sys/intr.h>
     97      1.33        ad #include <sys/cpu.h>
     98      1.36        ad #include <sys/kmem.h>
     99       1.1   thorpej 
    100       1.1   thorpej #ifdef DDB
    101       1.1   thorpej #include <machine/db_machdep.h>
    102       1.1   thorpej #include <ddb/db_interface.h>
    103       1.1   thorpej #include <ddb/db_access.h>
    104      1.49  christos #include <ddb/db_cpu.h>
    105       1.1   thorpej #include <ddb/db_sym.h>
    106       1.1   thorpej #include <ddb/db_output.h>
    107       1.1   thorpej #endif
    108       1.1   thorpej 
    109      1.22        ad #define BUCKETS		1024
    110      1.22        ad #define WHEELSIZE	256
    111      1.22        ad #define WHEELMASK	255
    112      1.22        ad #define WHEELBITS	8
    113      1.22        ad 
    114       1.1   thorpej #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK)
    115       1.1   thorpej 
    116      1.36        ad #define BUCKET(cc, rel, abs)						\
    117       1.1   thorpej     (((rel) <= (1 << (2*WHEELBITS)))					\
    118       1.1   thorpej     	? ((rel) <= (1 << WHEELBITS))					\
    119      1.36        ad             ? &(cc)->cc_wheel[MASKWHEEL(0, (abs))]			\
    120      1.36        ad             : &(cc)->cc_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE]		\
    121       1.1   thorpej         : ((rel) <= (1 << (3*WHEELBITS)))				\
    122      1.36        ad             ? &(cc)->cc_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE]	\
    123      1.36        ad             : &(cc)->cc_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE])
    124       1.1   thorpej 
    125      1.36        ad #define MOVEBUCKET(cc, wheel, time)					\
    126      1.36        ad     CIRCQ_APPEND(&(cc)->cc_todo,					\
    127      1.36        ad         &(cc)->cc_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE])
    128       1.1   thorpej 
    129       1.1   thorpej /*
    130       1.1   thorpej  * Circular queue definitions.
    131       1.1   thorpej  */
    132       1.1   thorpej 
    133      1.11       scw #define CIRCQ_INIT(list)						\
    134       1.1   thorpej do {									\
    135      1.11       scw         (list)->cq_next_l = (list);					\
    136      1.11       scw         (list)->cq_prev_l = (list);					\
    137       1.1   thorpej } while (/*CONSTCOND*/0)
    138       1.1   thorpej 
    139       1.1   thorpej #define CIRCQ_INSERT(elem, list)					\
    140       1.1   thorpej do {									\
    141      1.11       scw         (elem)->cq_prev_e = (list)->cq_prev_e;				\
    142      1.11       scw         (elem)->cq_next_l = (list);					\
    143      1.11       scw         (list)->cq_prev_l->cq_next_l = (elem);				\
    144      1.11       scw         (list)->cq_prev_l = (elem);					\
    145       1.1   thorpej } while (/*CONSTCOND*/0)
    146       1.1   thorpej 
    147       1.1   thorpej #define CIRCQ_APPEND(fst, snd)						\
    148       1.1   thorpej do {									\
    149       1.1   thorpej         if (!CIRCQ_EMPTY(snd)) {					\
    150      1.11       scw                 (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l;		\
    151      1.11       scw                 (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l;		\
    152      1.11       scw                 (snd)->cq_prev_l->cq_next_l = (fst);			\
    153      1.11       scw                 (fst)->cq_prev_l = (snd)->cq_prev_l;			\
    154       1.1   thorpej                 CIRCQ_INIT(snd);					\
    155       1.1   thorpej         }								\
    156       1.1   thorpej } while (/*CONSTCOND*/0)
    157       1.1   thorpej 
    158       1.1   thorpej #define CIRCQ_REMOVE(elem)						\
    159       1.1   thorpej do {									\
    160      1.11       scw         (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e;		\
    161      1.11       scw         (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e;		\
    162       1.1   thorpej } while (/*CONSTCOND*/0)
    163       1.1   thorpej 
    164      1.11       scw #define CIRCQ_FIRST(list)	((list)->cq_next_e)
    165      1.11       scw #define CIRCQ_NEXT(elem)	((elem)->cq_next_e)
    166      1.11       scw #define CIRCQ_LAST(elem,list)	((elem)->cq_next_l == (list))
    167      1.11       scw #define CIRCQ_EMPTY(list)	((list)->cq_next_l == (list))
    168       1.1   thorpej 
    169      1.36        ad struct callout_cpu {
    170      1.44        ad 	kmutex_t	*cc_lock;
    171      1.36        ad 	sleepq_t	cc_sleepq;
    172      1.36        ad 	u_int		cc_nwait;
    173      1.36        ad 	u_int		cc_ticks;
    174      1.36        ad 	lwp_t		*cc_lwp;
    175      1.36        ad 	callout_impl_t	*cc_active;
    176      1.36        ad 	callout_impl_t	*cc_cancel;
    177      1.36        ad 	struct evcnt	cc_ev_late;
    178      1.36        ad 	struct evcnt	cc_ev_block;
    179      1.36        ad 	struct callout_circq cc_todo;		/* Worklist */
    180      1.36        ad 	struct callout_circq cc_wheel[BUCKETS];	/* Queues of timeouts */
    181      1.36        ad 	char		cc_name1[12];
    182      1.36        ad 	char		cc_name2[12];
    183      1.36        ad };
    184      1.36        ad 
    185      1.49  christos #ifndef CRASH
    186      1.49  christos 
    187      1.49  christos static void	callout_softclock(void *);
    188      1.57        ad static void	callout_wait(callout_impl_t *, void *, kmutex_t *);
    189      1.57        ad 
    190      1.57        ad static struct callout_cpu callout_cpu0 __cacheline_aligned;
    191      1.57        ad static void *callout_sih __read_mostly;
    192      1.36        ad 
    193      1.36        ad static inline kmutex_t *
    194      1.36        ad callout_lock(callout_impl_t *c)
    195      1.36        ad {
    196      1.44        ad 	struct callout_cpu *cc;
    197      1.36        ad 	kmutex_t *lock;
    198      1.36        ad 
    199      1.36        ad 	for (;;) {
    200      1.44        ad 		cc = c->c_cpu;
    201      1.44        ad 		lock = cc->cc_lock;
    202      1.36        ad 		mutex_spin_enter(lock);
    203      1.44        ad 		if (__predict_true(cc == c->c_cpu))
    204      1.36        ad 			return lock;
    205      1.36        ad 		mutex_spin_exit(lock);
    206      1.36        ad 	}
    207      1.36        ad }
    208       1.5   thorpej 
    209       1.1   thorpej /*
    210       1.1   thorpej  * callout_startup:
    211       1.1   thorpej  *
    212       1.1   thorpej  *	Initialize the callout facility, called at system startup time.
    213      1.36        ad  *	Do just enough to allow callouts to be safely registered.
    214       1.1   thorpej  */
    215       1.1   thorpej void
    216       1.1   thorpej callout_startup(void)
    217       1.1   thorpej {
    218      1.36        ad 	struct callout_cpu *cc;
    219       1.1   thorpej 	int b;
    220       1.1   thorpej 
    221      1.36        ad 	KASSERT(curcpu()->ci_data.cpu_callout == NULL);
    222      1.22        ad 
    223      1.36        ad 	cc = &callout_cpu0;
    224      1.44        ad 	cc->cc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
    225      1.36        ad 	CIRCQ_INIT(&cc->cc_todo);
    226       1.1   thorpej 	for (b = 0; b < BUCKETS; b++)
    227      1.36        ad 		CIRCQ_INIT(&cc->cc_wheel[b]);
    228      1.36        ad 	curcpu()->ci_data.cpu_callout = cc;
    229      1.22        ad }
    230      1.22        ad 
    231      1.22        ad /*
    232      1.36        ad  * callout_init_cpu:
    233      1.22        ad  *
    234      1.36        ad  *	Per-CPU initialization.
    235      1.22        ad  */
    236      1.47    martin CTASSERT(sizeof(callout_impl_t) <= sizeof(callout_t));
    237      1.47    martin 
    238      1.22        ad void
    239      1.36        ad callout_init_cpu(struct cpu_info *ci)
    240      1.22        ad {
    241      1.36        ad 	struct callout_cpu *cc;
    242      1.36        ad 	int b;
    243      1.22        ad 
    244      1.36        ad 	if ((cc = ci->ci_data.cpu_callout) == NULL) {
    245      1.36        ad 		cc = kmem_zalloc(sizeof(*cc), KM_SLEEP);
    246      1.44        ad 		cc->cc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
    247      1.36        ad 		CIRCQ_INIT(&cc->cc_todo);
    248      1.36        ad 		for (b = 0; b < BUCKETS; b++)
    249      1.36        ad 			CIRCQ_INIT(&cc->cc_wheel[b]);
    250      1.36        ad 	} else {
    251      1.36        ad 		/* Boot CPU, one time only. */
    252      1.36        ad 		callout_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    253      1.36        ad 		    callout_softclock, NULL);
    254      1.36        ad 		if (callout_sih == NULL)
    255      1.36        ad 			panic("callout_init_cpu (2)");
    256      1.36        ad 	}
    257      1.36        ad 
    258      1.40        ad 	sleepq_init(&cc->cc_sleepq);
    259      1.36        ad 
    260      1.36        ad 	snprintf(cc->cc_name1, sizeof(cc->cc_name1), "late/%u",
    261      1.36        ad 	    cpu_index(ci));
    262      1.36        ad 	evcnt_attach_dynamic(&cc->cc_ev_late, EVCNT_TYPE_MISC,
    263      1.36        ad 	    NULL, "callout", cc->cc_name1);
    264      1.36        ad 
    265      1.36        ad 	snprintf(cc->cc_name2, sizeof(cc->cc_name2), "wait/%u",
    266      1.36        ad 	    cpu_index(ci));
    267      1.36        ad 	evcnt_attach_dynamic(&cc->cc_ev_block, EVCNT_TYPE_MISC,
    268      1.36        ad 	    NULL, "callout", cc->cc_name2);
    269      1.36        ad 
    270      1.36        ad 	ci->ci_data.cpu_callout = cc;
    271       1.1   thorpej }
    272       1.1   thorpej 
    273       1.1   thorpej /*
    274       1.1   thorpej  * callout_init:
    275       1.1   thorpej  *
    276      1.36        ad  *	Initialize a callout structure.  This must be quick, so we fill
    277      1.36        ad  *	only the minimum number of fields.
    278       1.1   thorpej  */
    279       1.1   thorpej void
    280      1.22        ad callout_init(callout_t *cs, u_int flags)
    281       1.1   thorpej {
    282      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    283      1.36        ad 	struct callout_cpu *cc;
    284      1.22        ad 
    285      1.22        ad 	KASSERT((flags & ~CALLOUT_FLAGMASK) == 0);
    286       1.1   thorpej 
    287      1.36        ad 	cc = curcpu()->ci_data.cpu_callout;
    288      1.36        ad 	c->c_func = NULL;
    289      1.22        ad 	c->c_magic = CALLOUT_MAGIC;
    290      1.36        ad 	if (__predict_true((flags & CALLOUT_MPSAFE) != 0 && cc != NULL)) {
    291      1.36        ad 		c->c_flags = flags;
    292      1.36        ad 		c->c_cpu = cc;
    293      1.36        ad 		return;
    294      1.36        ad 	}
    295      1.36        ad 	c->c_flags = flags | CALLOUT_BOUND;
    296      1.36        ad 	c->c_cpu = &callout_cpu0;
    297      1.22        ad }
    298      1.22        ad 
    299      1.22        ad /*
    300      1.22        ad  * callout_destroy:
    301      1.22        ad  *
    302      1.22        ad  *	Destroy a callout structure.  The callout must be stopped.
    303      1.22        ad  */
    304      1.22        ad void
    305      1.22        ad callout_destroy(callout_t *cs)
    306      1.22        ad {
    307      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    308      1.22        ad 
    309      1.53  christos 	KASSERTMSG(c->c_magic == CALLOUT_MAGIC,
    310      1.53  christos 	    "callout %p: c_magic (%#x) != CALLOUT_MAGIC (%#x)",
    311      1.53  christos 	    c, c->c_magic, CALLOUT_MAGIC);
    312      1.22        ad 	/*
    313      1.22        ad 	 * It's not necessary to lock in order to see the correct value
    314      1.22        ad 	 * of c->c_flags.  If the callout could potentially have been
    315      1.22        ad 	 * running, the current thread should have stopped it.
    316      1.22        ad 	 */
    317      1.48    martin 	KASSERTMSG((c->c_flags & CALLOUT_PENDING) == 0,
    318      1.48    martin 	    "callout %p: c_func (%p) c_flags (%#x) destroyed from %p",
    319      1.48    martin 	    c, c->c_func, c->c_flags, __builtin_return_address(0));
    320      1.36        ad 	KASSERT(c->c_cpu->cc_lwp == curlwp || c->c_cpu->cc_active != c);
    321      1.22        ad 	c->c_magic = 0;
    322       1.1   thorpej }
    323       1.1   thorpej 
    324       1.1   thorpej /*
    325      1.29     joerg  * callout_schedule_locked:
    326       1.1   thorpej  *
    327      1.29     joerg  *	Schedule a callout to run.  The function and argument must
    328      1.29     joerg  *	already be set in the callout structure.  Must be called with
    329      1.29     joerg  *	callout_lock.
    330       1.1   thorpej  */
    331      1.29     joerg static void
    332      1.36        ad callout_schedule_locked(callout_impl_t *c, kmutex_t *lock, int to_ticks)
    333       1.1   thorpej {
    334      1.36        ad 	struct callout_cpu *cc, *occ;
    335      1.20        ad 	int old_time;
    336       1.1   thorpej 
    337       1.1   thorpej 	KASSERT(to_ticks >= 0);
    338      1.29     joerg 	KASSERT(c->c_func != NULL);
    339       1.1   thorpej 
    340       1.1   thorpej 	/* Initialize the time here, it won't change. */
    341      1.36        ad 	occ = c->c_cpu;
    342      1.43        ad 	c->c_flags &= ~(CALLOUT_FIRED | CALLOUT_INVOKING);
    343       1.1   thorpej 
    344       1.1   thorpej 	/*
    345       1.1   thorpej 	 * If this timeout is already scheduled and now is moved
    346      1.36        ad 	 * earlier, reschedule it now.  Otherwise leave it in place
    347       1.1   thorpej 	 * and let it be rescheduled later.
    348       1.1   thorpej 	 */
    349      1.22        ad 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    350      1.36        ad 		/* Leave on existing CPU. */
    351      1.36        ad 		old_time = c->c_time;
    352      1.36        ad 		c->c_time = to_ticks + occ->cc_ticks;
    353       1.4      yamt 		if (c->c_time - old_time < 0) {
    354       1.1   thorpej 			CIRCQ_REMOVE(&c->c_list);
    355      1.36        ad 			CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
    356       1.1   thorpej 		}
    357      1.36        ad 		mutex_spin_exit(lock);
    358      1.36        ad 		return;
    359      1.36        ad 	}
    360      1.36        ad 
    361      1.36        ad 	cc = curcpu()->ci_data.cpu_callout;
    362      1.36        ad 	if ((c->c_flags & CALLOUT_BOUND) != 0 || cc == occ ||
    363      1.44        ad 	    !mutex_tryenter(cc->cc_lock)) {
    364      1.36        ad 		/* Leave on existing CPU. */
    365      1.36        ad 		c->c_time = to_ticks + occ->cc_ticks;
    366      1.36        ad 		c->c_flags |= CALLOUT_PENDING;
    367      1.36        ad 		CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
    368       1.1   thorpej 	} else {
    369      1.36        ad 		/* Move to this CPU. */
    370      1.36        ad 		c->c_cpu = cc;
    371      1.36        ad 		c->c_time = to_ticks + cc->cc_ticks;
    372       1.1   thorpej 		c->c_flags |= CALLOUT_PENDING;
    373      1.36        ad 		CIRCQ_INSERT(&c->c_list, &cc->cc_todo);
    374      1.44        ad 		mutex_spin_exit(cc->cc_lock);
    375       1.1   thorpej 	}
    376      1.36        ad 	mutex_spin_exit(lock);
    377      1.29     joerg }
    378      1.29     joerg 
    379      1.29     joerg /*
    380      1.29     joerg  * callout_reset:
    381      1.29     joerg  *
    382      1.29     joerg  *	Reset a callout structure with a new function and argument, and
    383      1.29     joerg  *	schedule it to run.
    384      1.29     joerg  */
    385      1.29     joerg void
    386      1.29     joerg callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
    387      1.29     joerg {
    388      1.29     joerg 	callout_impl_t *c = (callout_impl_t *)cs;
    389      1.36        ad 	kmutex_t *lock;
    390      1.29     joerg 
    391      1.29     joerg 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    392      1.42     rmind 	KASSERT(func != NULL);
    393      1.29     joerg 
    394      1.36        ad 	lock = callout_lock(c);
    395      1.29     joerg 	c->c_func = func;
    396      1.29     joerg 	c->c_arg = arg;
    397      1.36        ad 	callout_schedule_locked(c, lock, to_ticks);
    398       1.1   thorpej }
    399       1.1   thorpej 
    400       1.1   thorpej /*
    401       1.1   thorpej  * callout_schedule:
    402       1.1   thorpej  *
    403       1.1   thorpej  *	Schedule a callout to run.  The function and argument must
    404       1.1   thorpej  *	already be set in the callout structure.
    405       1.1   thorpej  */
    406       1.1   thorpej void
    407      1.22        ad callout_schedule(callout_t *cs, int to_ticks)
    408       1.1   thorpej {
    409      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    410      1.36        ad 	kmutex_t *lock;
    411       1.1   thorpej 
    412      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    413       1.1   thorpej 
    414      1.36        ad 	lock = callout_lock(c);
    415      1.36        ad 	callout_schedule_locked(c, lock, to_ticks);
    416       1.1   thorpej }
    417       1.1   thorpej 
    418       1.1   thorpej /*
    419       1.1   thorpej  * callout_stop:
    420       1.1   thorpej  *
    421      1.36        ad  *	Try to cancel a pending callout.  It may be too late: the callout
    422      1.36        ad  *	could be running on another CPU.  If called from interrupt context,
    423      1.36        ad  *	the callout could already be in progress at a lower priority.
    424       1.1   thorpej  */
    425      1.22        ad bool
    426      1.22        ad callout_stop(callout_t *cs)
    427       1.1   thorpej {
    428      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    429      1.36        ad 	struct callout_cpu *cc;
    430      1.36        ad 	kmutex_t *lock;
    431      1.22        ad 	bool expired;
    432      1.22        ad 
    433      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    434       1.1   thorpej 
    435      1.36        ad 	lock = callout_lock(c);
    436      1.20        ad 
    437      1.22        ad 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    438       1.1   thorpej 		CIRCQ_REMOVE(&c->c_list);
    439      1.32        ad 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
    440      1.32        ad 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    441      1.32        ad 
    442      1.36        ad 	cc = c->c_cpu;
    443      1.36        ad 	if (cc->cc_active == c) {
    444      1.32        ad 		/*
    445      1.32        ad 		 * This is for non-MPSAFE callouts only.  To synchronize
    446      1.32        ad 		 * effectively we must be called with kernel_lock held.
    447      1.32        ad 		 * It's also taken in callout_softclock.
    448      1.32        ad 		 */
    449      1.36        ad 		cc->cc_cancel = c;
    450      1.32        ad 	}
    451      1.32        ad 
    452      1.36        ad 	mutex_spin_exit(lock);
    453      1.32        ad 
    454      1.32        ad 	return expired;
    455      1.32        ad }
    456      1.32        ad 
    457      1.32        ad /*
    458      1.32        ad  * callout_halt:
    459      1.32        ad  *
    460      1.32        ad  *	Cancel a pending callout.  If in-flight, block until it completes.
    461      1.36        ad  *	May not be called from a hard interrupt handler.  If the callout
    462      1.36        ad  * 	can take locks, the caller of callout_halt() must not hold any of
    463      1.37        ad  *	those locks, otherwise the two could deadlock.  If 'interlock' is
    464      1.37        ad  *	non-NULL and we must wait for the callout to complete, it will be
    465      1.37        ad  *	released and re-acquired before returning.
    466      1.32        ad  */
    467      1.32        ad bool
    468      1.38        ad callout_halt(callout_t *cs, void *interlock)
    469      1.32        ad {
    470      1.32        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    471      1.57        ad 	kmutex_t *lock;
    472      1.57        ad 	int flags;
    473      1.32        ad 
    474      1.32        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    475      1.32        ad 	KASSERT(!cpu_intr_p());
    476      1.54     ozaki 	KASSERT(interlock == NULL || mutex_owned(interlock));
    477      1.32        ad 
    478      1.57        ad 	/* Fast path. */
    479      1.36        ad 	lock = callout_lock(c);
    480      1.57        ad 	flags = c->c_flags;
    481      1.57        ad 	if ((flags & CALLOUT_PENDING) != 0)
    482      1.57        ad 		CIRCQ_REMOVE(&c->c_list);
    483      1.57        ad 	c->c_flags = flags & ~(CALLOUT_PENDING|CALLOUT_FIRED);
    484      1.57        ad 	if (__predict_false(flags & CALLOUT_FIRED)) {
    485      1.57        ad 		callout_wait(c, interlock, lock);
    486      1.57        ad 		return true;
    487      1.57        ad 	}
    488      1.57        ad 	mutex_spin_exit(lock);
    489      1.57        ad 	return false;
    490      1.57        ad }
    491       1.1   thorpej 
    492      1.57        ad /*
    493      1.57        ad  * callout_wait:
    494      1.57        ad  *
    495      1.57        ad  *	Slow path for callout_halt().  Deliberately marked __noinline to
    496      1.57        ad  *	prevent unneeded overhead in the caller.
    497      1.57        ad  */
    498      1.57        ad static void __noinline
    499      1.57        ad callout_wait(callout_impl_t *c, void *interlock, kmutex_t *lock)
    500      1.57        ad {
    501      1.57        ad 	struct callout_cpu *cc;
    502      1.57        ad 	struct lwp *l;
    503      1.57        ad 	kmutex_t *relock;
    504       1.1   thorpej 
    505      1.32        ad 	l = curlwp;
    506      1.57        ad 	relock = NULL;
    507      1.36        ad 	for (;;) {
    508  1.57.2.1        ad 		/*
    509  1.57.2.1        ad 		 * At this point we know the callout is not pending, but it
    510  1.57.2.1        ad 		 * could be running on a CPU somewhere.  That can be curcpu
    511  1.57.2.1        ad 		 * in a few cases:
    512  1.57.2.1        ad 		 *
    513  1.57.2.1        ad 		 * - curlwp is a higher priority soft interrupt
    514  1.57.2.1        ad 		 * - the callout blocked on a lock and is currently asleep
    515  1.57.2.1        ad 		 * - the callout itself has called callout_halt() (nice!)
    516  1.57.2.1        ad 		 */
    517      1.36        ad 		cc = c->c_cpu;
    518      1.36        ad 		if (__predict_true(cc->cc_active != c || cc->cc_lwp == l))
    519      1.36        ad 			break;
    520  1.57.2.1        ad 
    521  1.57.2.1        ad 		/* It's running - need to wait for it to complete. */
    522      1.37        ad 		if (interlock != NULL) {
    523      1.37        ad 			/*
    524      1.37        ad 			 * Avoid potential scheduler lock order problems by
    525      1.37        ad 			 * dropping the interlock without the callout lock
    526  1.57.2.1        ad 			 * held; then retry.
    527      1.37        ad 			 */
    528      1.37        ad 			mutex_spin_exit(lock);
    529      1.37        ad 			mutex_exit(interlock);
    530      1.37        ad 			relock = interlock;
    531      1.37        ad 			interlock = NULL;
    532      1.37        ad 		} else {
    533      1.37        ad 			/* XXX Better to do priority inheritance. */
    534      1.37        ad 			KASSERT(l->l_wchan == NULL);
    535      1.37        ad 			cc->cc_nwait++;
    536      1.37        ad 			cc->cc_ev_block.ev_count++;
    537      1.37        ad 			l->l_kpriority = true;
    538      1.44        ad 			sleepq_enter(&cc->cc_sleepq, l, cc->cc_lock);
    539      1.37        ad 			sleepq_enqueue(&cc->cc_sleepq, cc, "callout",
    540      1.37        ad 			    &sleep_syncobj);
    541      1.37        ad 			sleepq_block(0, false);
    542      1.37        ad 		}
    543  1.57.2.1        ad 
    544  1.57.2.1        ad 		/*
    545  1.57.2.1        ad 		 * Re-lock the callout and check the state of play again.
    546  1.57.2.1        ad 		 * It's a common design pattern for callouts to re-schedule
    547  1.57.2.1        ad 		 * themselves so put a stop to it again if needed.
    548  1.57.2.1        ad 		 */
    549      1.36        ad 		lock = callout_lock(c);
    550  1.57.2.1        ad 		if ((c->c_flags & CALLOUT_PENDING) != 0)
    551  1.57.2.1        ad 			CIRCQ_REMOVE(&c->c_list);
    552  1.57.2.1        ad 		c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    553      1.32        ad 	}
    554      1.32        ad 
    555      1.36        ad 	mutex_spin_exit(lock);
    556      1.37        ad 	if (__predict_false(relock != NULL))
    557      1.37        ad 		mutex_enter(relock);
    558      1.22        ad }
    559      1.22        ad 
    560      1.36        ad #ifdef notyet
    561      1.36        ad /*
    562      1.36        ad  * callout_bind:
    563      1.36        ad  *
    564      1.36        ad  *	Bind a callout so that it will only execute on one CPU.
    565      1.36        ad  *	The callout must be stopped, and must be MPSAFE.
    566      1.36        ad  *
    567      1.36        ad  *	XXX Disabled for now until it is decided how to handle
    568      1.36        ad  *	offlined CPUs.  We may want weak+strong binding.
    569      1.36        ad  */
    570      1.36        ad void
    571      1.36        ad callout_bind(callout_t *cs, struct cpu_info *ci)
    572      1.36        ad {
    573      1.36        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    574      1.36        ad 	struct callout_cpu *cc;
    575      1.36        ad 	kmutex_t *lock;
    576      1.36        ad 
    577      1.36        ad 	KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
    578      1.36        ad 	KASSERT(c->c_cpu->cc_active != c);
    579      1.36        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    580      1.36        ad 	KASSERT((c->c_flags & CALLOUT_MPSAFE) != 0);
    581      1.36        ad 
    582      1.36        ad 	lock = callout_lock(c);
    583      1.36        ad 	cc = ci->ci_data.cpu_callout;
    584      1.36        ad 	c->c_flags |= CALLOUT_BOUND;
    585      1.36        ad 	if (c->c_cpu != cc) {
    586      1.36        ad 		/*
    587      1.36        ad 		 * Assigning c_cpu effectively unlocks the callout
    588      1.36        ad 		 * structure, as we don't hold the new CPU's lock.
    589      1.36        ad 		 * Issue memory barrier to prevent accesses being
    590      1.36        ad 		 * reordered.
    591      1.36        ad 		 */
    592      1.36        ad 		membar_exit();
    593      1.36        ad 		c->c_cpu = cc;
    594      1.36        ad 	}
    595      1.36        ad 	mutex_spin_exit(lock);
    596      1.36        ad }
    597      1.36        ad #endif
    598      1.36        ad 
    599      1.22        ad void
    600      1.22        ad callout_setfunc(callout_t *cs, void (*func)(void *), void *arg)
    601      1.22        ad {
    602      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    603      1.36        ad 	kmutex_t *lock;
    604      1.22        ad 
    605      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    606      1.42     rmind 	KASSERT(func != NULL);
    607      1.22        ad 
    608      1.36        ad 	lock = callout_lock(c);
    609      1.22        ad 	c->c_func = func;
    610      1.22        ad 	c->c_arg = arg;
    611      1.36        ad 	mutex_spin_exit(lock);
    612      1.22        ad }
    613      1.22        ad 
    614      1.22        ad bool
    615      1.22        ad callout_expired(callout_t *cs)
    616      1.22        ad {
    617      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    618      1.36        ad 	kmutex_t *lock;
    619      1.22        ad 	bool rv;
    620      1.22        ad 
    621      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    622      1.22        ad 
    623      1.36        ad 	lock = callout_lock(c);
    624      1.22        ad 	rv = ((c->c_flags & CALLOUT_FIRED) != 0);
    625      1.36        ad 	mutex_spin_exit(lock);
    626      1.22        ad 
    627      1.22        ad 	return rv;
    628      1.22        ad }
    629      1.22        ad 
    630      1.22        ad bool
    631      1.22        ad callout_active(callout_t *cs)
    632      1.22        ad {
    633      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    634      1.36        ad 	kmutex_t *lock;
    635      1.22        ad 	bool rv;
    636      1.22        ad 
    637      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    638      1.22        ad 
    639      1.36        ad 	lock = callout_lock(c);
    640      1.22        ad 	rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
    641      1.36        ad 	mutex_spin_exit(lock);
    642      1.22        ad 
    643      1.22        ad 	return rv;
    644      1.22        ad }
    645      1.22        ad 
    646      1.22        ad bool
    647      1.22        ad callout_pending(callout_t *cs)
    648      1.22        ad {
    649      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    650      1.36        ad 	kmutex_t *lock;
    651      1.22        ad 	bool rv;
    652      1.22        ad 
    653      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    654      1.22        ad 
    655      1.36        ad 	lock = callout_lock(c);
    656      1.22        ad 	rv = ((c->c_flags & CALLOUT_PENDING) != 0);
    657      1.36        ad 	mutex_spin_exit(lock);
    658      1.22        ad 
    659      1.22        ad 	return rv;
    660      1.22        ad }
    661      1.22        ad 
    662      1.22        ad bool
    663      1.22        ad callout_invoking(callout_t *cs)
    664      1.22        ad {
    665      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    666      1.36        ad 	kmutex_t *lock;
    667      1.22        ad 	bool rv;
    668      1.22        ad 
    669      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    670      1.22        ad 
    671      1.36        ad 	lock = callout_lock(c);
    672      1.22        ad 	rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
    673      1.36        ad 	mutex_spin_exit(lock);
    674      1.22        ad 
    675      1.22        ad 	return rv;
    676      1.22        ad }
    677      1.22        ad 
    678      1.22        ad void
    679      1.22        ad callout_ack(callout_t *cs)
    680      1.22        ad {
    681      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    682      1.36        ad 	kmutex_t *lock;
    683      1.22        ad 
    684      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    685      1.22        ad 
    686      1.36        ad 	lock = callout_lock(c);
    687      1.22        ad 	c->c_flags &= ~CALLOUT_INVOKING;
    688      1.36        ad 	mutex_spin_exit(lock);
    689       1.1   thorpej }
    690       1.1   thorpej 
    691       1.1   thorpej /*
    692      1.36        ad  * callout_hardclock:
    693      1.36        ad  *
    694      1.36        ad  *	Called from hardclock() once every tick.  We schedule a soft
    695      1.36        ad  *	interrupt if there is work to be done.
    696       1.1   thorpej  */
    697      1.22        ad void
    698       1.1   thorpej callout_hardclock(void)
    699       1.1   thorpej {
    700      1.36        ad 	struct callout_cpu *cc;
    701      1.36        ad 	int needsoftclock, ticks;
    702       1.1   thorpej 
    703      1.36        ad 	cc = curcpu()->ci_data.cpu_callout;
    704      1.44        ad 	mutex_spin_enter(cc->cc_lock);
    705       1.1   thorpej 
    706      1.36        ad 	ticks = ++cc->cc_ticks;
    707      1.36        ad 
    708      1.36        ad 	MOVEBUCKET(cc, 0, ticks);
    709      1.36        ad 	if (MASKWHEEL(0, ticks) == 0) {
    710      1.36        ad 		MOVEBUCKET(cc, 1, ticks);
    711      1.36        ad 		if (MASKWHEEL(1, ticks) == 0) {
    712      1.36        ad 			MOVEBUCKET(cc, 2, ticks);
    713      1.36        ad 			if (MASKWHEEL(2, ticks) == 0)
    714      1.36        ad 				MOVEBUCKET(cc, 3, ticks);
    715       1.1   thorpej 		}
    716       1.1   thorpej 	}
    717       1.1   thorpej 
    718      1.36        ad 	needsoftclock = !CIRCQ_EMPTY(&cc->cc_todo);
    719      1.44        ad 	mutex_spin_exit(cc->cc_lock);
    720       1.1   thorpej 
    721      1.22        ad 	if (needsoftclock)
    722      1.36        ad 		softint_schedule(callout_sih);
    723       1.1   thorpej }
    724       1.1   thorpej 
    725      1.36        ad /*
    726      1.36        ad  * callout_softclock:
    727      1.36        ad  *
    728      1.36        ad  *	Soft interrupt handler, scheduled above if there is work to
    729      1.36        ad  * 	be done.  Callouts are made in soft interrupt context.
    730      1.36        ad  */
    731      1.22        ad static void
    732      1.22        ad callout_softclock(void *v)
    733       1.1   thorpej {
    734      1.22        ad 	callout_impl_t *c;
    735      1.36        ad 	struct callout_cpu *cc;
    736       1.1   thorpej 	void (*func)(void *);
    737       1.1   thorpej 	void *arg;
    738      1.36        ad 	int mpsafe, count, ticks, delta;
    739      1.22        ad 	lwp_t *l;
    740       1.1   thorpej 
    741      1.22        ad 	l = curlwp;
    742      1.36        ad 	KASSERT(l->l_cpu == curcpu());
    743      1.36        ad 	cc = l->l_cpu->ci_data.cpu_callout;
    744       1.1   thorpej 
    745      1.44        ad 	mutex_spin_enter(cc->cc_lock);
    746      1.36        ad 	cc->cc_lwp = l;
    747      1.36        ad 	while (!CIRCQ_EMPTY(&cc->cc_todo)) {
    748      1.36        ad 		c = CIRCQ_FIRST(&cc->cc_todo);
    749      1.22        ad 		KASSERT(c->c_magic == CALLOUT_MAGIC);
    750      1.22        ad 		KASSERT(c->c_func != NULL);
    751      1.36        ad 		KASSERT(c->c_cpu == cc);
    752      1.26        ad 		KASSERT((c->c_flags & CALLOUT_PENDING) != 0);
    753      1.26        ad 		KASSERT((c->c_flags & CALLOUT_FIRED) == 0);
    754       1.1   thorpej 		CIRCQ_REMOVE(&c->c_list);
    755       1.1   thorpej 
    756       1.1   thorpej 		/* If due run it, otherwise insert it into the right bucket. */
    757      1.36        ad 		ticks = cc->cc_ticks;
    758      1.56       kre 		delta = (int)((unsigned)c->c_time - (unsigned)ticks);
    759      1.56       kre 		if (delta > 0) {
    760      1.36        ad 			CIRCQ_INSERT(&c->c_list, BUCKET(cc, delta, c->c_time));
    761      1.36        ad 			continue;
    762      1.36        ad 		}
    763      1.56       kre 		if (delta < 0)
    764      1.36        ad 			cc->cc_ev_late.ev_count++;
    765      1.36        ad 
    766      1.43        ad 		c->c_flags = (c->c_flags & ~CALLOUT_PENDING) |
    767      1.43        ad 		    (CALLOUT_FIRED | CALLOUT_INVOKING);
    768      1.36        ad 		mpsafe = (c->c_flags & CALLOUT_MPSAFE);
    769      1.36        ad 		func = c->c_func;
    770      1.36        ad 		arg = c->c_arg;
    771      1.36        ad 		cc->cc_active = c;
    772      1.36        ad 
    773      1.44        ad 		mutex_spin_exit(cc->cc_lock);
    774      1.42     rmind 		KASSERT(func != NULL);
    775      1.44        ad 		if (__predict_false(!mpsafe)) {
    776      1.36        ad 			KERNEL_LOCK(1, NULL);
    777      1.36        ad 			(*func)(arg);
    778      1.36        ad 			KERNEL_UNLOCK_ONE(NULL);
    779      1.36        ad 		} else
    780      1.36        ad 			(*func)(arg);
    781      1.44        ad 		mutex_spin_enter(cc->cc_lock);
    782      1.36        ad 
    783      1.36        ad 		/*
    784      1.36        ad 		 * We can't touch 'c' here because it might be
    785      1.36        ad 		 * freed already.  If LWPs waiting for callout
    786      1.36        ad 		 * to complete, awaken them.
    787      1.36        ad 		 */
    788      1.36        ad 		cc->cc_active = NULL;
    789      1.36        ad 		if ((count = cc->cc_nwait) != 0) {
    790      1.36        ad 			cc->cc_nwait = 0;
    791      1.36        ad 			/* sleepq_wake() drops the lock. */
    792      1.44        ad 			sleepq_wake(&cc->cc_sleepq, cc, count, cc->cc_lock);
    793      1.44        ad 			mutex_spin_enter(cc->cc_lock);
    794       1.1   thorpej 		}
    795       1.1   thorpej 	}
    796      1.36        ad 	cc->cc_lwp = NULL;
    797      1.44        ad 	mutex_spin_exit(cc->cc_lock);
    798       1.1   thorpej }
    799      1.49  christos #endif
    800       1.1   thorpej 
    801       1.1   thorpej #ifdef DDB
    802       1.1   thorpej static void
    803      1.51  christos db_show_callout_bucket(struct callout_cpu *cc, struct callout_circq *kbucket,
    804      1.51  christos     struct callout_circq *bucket)
    805       1.1   thorpej {
    806      1.49  christos 	callout_impl_t *c, ci;
    807       1.1   thorpej 	db_expr_t offset;
    808      1.15  christos 	const char *name;
    809      1.15  christos 	static char question[] = "?";
    810      1.36        ad 	int b;
    811       1.1   thorpej 
    812      1.51  christos 	if (CIRCQ_LAST(bucket, kbucket))
    813      1.11       scw 		return;
    814      1.11       scw 
    815      1.51  christos 	for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) {
    816      1.49  christos 		db_read_bytes((db_addr_t)c, sizeof(ci), (char *)&ci);
    817      1.49  christos 		c = &ci;
    818      1.10       scw 		db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name,
    819      1.10       scw 		    &offset);
    820      1.15  christos 		name = name ? name : question;
    821      1.36        ad 		b = (bucket - cc->cc_wheel);
    822      1.36        ad 		if (b < 0)
    823      1.36        ad 			b = -WHEELSIZE;
    824      1.36        ad 		db_printf("%9d %2d/%-4d %16lx  %s\n",
    825      1.36        ad 		    c->c_time - cc->cc_ticks, b / WHEELSIZE, b,
    826      1.36        ad 		    (u_long)c->c_arg, name);
    827      1.51  christos 		if (CIRCQ_LAST(&c->c_list, kbucket))
    828      1.11       scw 			break;
    829       1.1   thorpej 	}
    830       1.1   thorpej }
    831       1.1   thorpej 
    832       1.1   thorpej void
    833      1.21      matt db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    834       1.1   thorpej {
    835      1.51  christos 	struct callout_cpu *cc, ccb;
    836      1.51  christos 	struct cpu_info *ci, cib;
    837       1.1   thorpej 	int b;
    838       1.1   thorpej 
    839      1.49  christos #ifndef CRASH
    840       1.1   thorpej 	db_printf("hardclock_ticks now: %d\n", hardclock_ticks);
    841      1.49  christos #endif
    842       1.1   thorpej 	db_printf("    ticks  wheel               arg  func\n");
    843       1.1   thorpej 
    844       1.1   thorpej 	/*
    845       1.1   thorpej 	 * Don't lock the callwheel; all the other CPUs are paused
    846       1.1   thorpej 	 * anyhow, and we might be called in a circumstance where
    847       1.1   thorpej 	 * some other CPU was paused while holding the lock.
    848       1.1   thorpej 	 */
    849      1.49  christos 	for (ci = db_cpu_first(); ci != NULL; ci = db_cpu_next(ci)) {
    850      1.51  christos 		db_read_bytes((db_addr_t)ci, sizeof(cib), (char *)&cib);
    851      1.51  christos 		cc = cib.ci_data.cpu_callout;
    852      1.51  christos 		db_read_bytes((db_addr_t)cc, sizeof(ccb), (char *)&ccb);
    853      1.51  christos 		db_show_callout_bucket(&ccb, &cc->cc_todo, &ccb.cc_todo);
    854      1.36        ad 	}
    855      1.36        ad 	for (b = 0; b < BUCKETS; b++) {
    856      1.49  christos 		for (ci = db_cpu_first(); ci != NULL; ci = db_cpu_next(ci)) {
    857      1.51  christos 			db_read_bytes((db_addr_t)ci, sizeof(cib), (char *)&cib);
    858      1.51  christos 			cc = cib.ci_data.cpu_callout;
    859      1.51  christos 			db_read_bytes((db_addr_t)cc, sizeof(ccb), (char *)&ccb);
    860      1.51  christos 			db_show_callout_bucket(&ccb, &cc->cc_wheel[b],
    861      1.51  christos 			    &ccb.cc_wheel[b]);
    862      1.36        ad 		}
    863      1.36        ad 	}
    864       1.1   thorpej }
    865       1.1   thorpej #endif /* DDB */
    866