Home | History | Annotate | Line # | Download | only in kern
kern_timeout.c revision 1.35.2.1
      1  1.35.2.1      yamt /*	$NetBSD: kern_timeout.c,v 1.35.2.1 2008/05/18 12:35:09 yamt Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*-
      4      1.32        ad  * Copyright (c) 2003, 2006, 2007, 2008 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.35.2.1      yamt __KERNEL_RCSID(0, "$NetBSD: kern_timeout.c,v 1.35.2.1 2008/05/18 12:35:09 yamt 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.35.2.1      yamt  * value of c_cpu->cc_ticks when the timeout should be called.  There are
     67  1.35.2.1      yamt  * four levels with 256 buckets each. See 'Scheme 7' in "Hashed and
     68  1.35.2.1      yamt  * Hierarchical Timing Wheels: Efficient Data Structures for Implementing
     69  1.35.2.1      yamt  * 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.35.2.1      yamt  * - 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.20        ad #include <sys/mutex.h>
     91      1.22        ad #include <sys/proc.h>
     92      1.22        ad #include <sys/sleepq.h>
     93      1.22        ad #include <sys/syncobj.h>
     94      1.22        ad #include <sys/evcnt.h>
     95      1.27        ad #include <sys/intr.h>
     96      1.33        ad #include <sys/cpu.h>
     97  1.35.2.1      yamt #include <sys/kmem.h>
     98       1.1   thorpej 
     99       1.1   thorpej #ifdef DDB
    100       1.1   thorpej #include <machine/db_machdep.h>
    101       1.1   thorpej #include <ddb/db_interface.h>
    102       1.1   thorpej #include <ddb/db_access.h>
    103       1.1   thorpej #include <ddb/db_sym.h>
    104       1.1   thorpej #include <ddb/db_output.h>
    105       1.1   thorpej #endif
    106       1.1   thorpej 
    107      1.22        ad #define BUCKETS		1024
    108      1.22        ad #define WHEELSIZE	256
    109      1.22        ad #define WHEELMASK	255
    110      1.22        ad #define WHEELBITS	8
    111      1.22        ad 
    112       1.1   thorpej #define MASKWHEEL(wheel, time) (((time) >> ((wheel)*WHEELBITS)) & WHEELMASK)
    113       1.1   thorpej 
    114  1.35.2.1      yamt #define BUCKET(cc, rel, abs)						\
    115       1.1   thorpej     (((rel) <= (1 << (2*WHEELBITS)))					\
    116       1.1   thorpej     	? ((rel) <= (1 << WHEELBITS))					\
    117  1.35.2.1      yamt             ? &(cc)->cc_wheel[MASKWHEEL(0, (abs))]			\
    118  1.35.2.1      yamt             : &(cc)->cc_wheel[MASKWHEEL(1, (abs)) + WHEELSIZE]		\
    119       1.1   thorpej         : ((rel) <= (1 << (3*WHEELBITS)))				\
    120  1.35.2.1      yamt             ? &(cc)->cc_wheel[MASKWHEEL(2, (abs)) + 2*WHEELSIZE]	\
    121  1.35.2.1      yamt             : &(cc)->cc_wheel[MASKWHEEL(3, (abs)) + 3*WHEELSIZE])
    122       1.1   thorpej 
    123  1.35.2.1      yamt #define MOVEBUCKET(cc, wheel, time)					\
    124  1.35.2.1      yamt     CIRCQ_APPEND(&(cc)->cc_todo,					\
    125  1.35.2.1      yamt         &(cc)->cc_wheel[MASKWHEEL((wheel), (time)) + (wheel)*WHEELSIZE])
    126       1.1   thorpej 
    127       1.1   thorpej /*
    128       1.1   thorpej  * Circular queue definitions.
    129       1.1   thorpej  */
    130       1.1   thorpej 
    131      1.11       scw #define CIRCQ_INIT(list)						\
    132       1.1   thorpej do {									\
    133      1.11       scw         (list)->cq_next_l = (list);					\
    134      1.11       scw         (list)->cq_prev_l = (list);					\
    135       1.1   thorpej } while (/*CONSTCOND*/0)
    136       1.1   thorpej 
    137       1.1   thorpej #define CIRCQ_INSERT(elem, list)					\
    138       1.1   thorpej do {									\
    139      1.11       scw         (elem)->cq_prev_e = (list)->cq_prev_e;				\
    140      1.11       scw         (elem)->cq_next_l = (list);					\
    141      1.11       scw         (list)->cq_prev_l->cq_next_l = (elem);				\
    142      1.11       scw         (list)->cq_prev_l = (elem);					\
    143       1.1   thorpej } while (/*CONSTCOND*/0)
    144       1.1   thorpej 
    145       1.1   thorpej #define CIRCQ_APPEND(fst, snd)						\
    146       1.1   thorpej do {									\
    147       1.1   thorpej         if (!CIRCQ_EMPTY(snd)) {					\
    148      1.11       scw                 (fst)->cq_prev_l->cq_next_l = (snd)->cq_next_l;		\
    149      1.11       scw                 (snd)->cq_next_l->cq_prev_l = (fst)->cq_prev_l;		\
    150      1.11       scw                 (snd)->cq_prev_l->cq_next_l = (fst);			\
    151      1.11       scw                 (fst)->cq_prev_l = (snd)->cq_prev_l;			\
    152       1.1   thorpej                 CIRCQ_INIT(snd);					\
    153       1.1   thorpej         }								\
    154       1.1   thorpej } while (/*CONSTCOND*/0)
    155       1.1   thorpej 
    156       1.1   thorpej #define CIRCQ_REMOVE(elem)						\
    157       1.1   thorpej do {									\
    158      1.11       scw         (elem)->cq_next_l->cq_prev_e = (elem)->cq_prev_e;		\
    159      1.11       scw         (elem)->cq_prev_l->cq_next_e = (elem)->cq_next_e;		\
    160       1.1   thorpej } while (/*CONSTCOND*/0)
    161       1.1   thorpej 
    162      1.11       scw #define CIRCQ_FIRST(list)	((list)->cq_next_e)
    163      1.11       scw #define CIRCQ_NEXT(elem)	((elem)->cq_next_e)
    164      1.11       scw #define CIRCQ_LAST(elem,list)	((elem)->cq_next_l == (list))
    165      1.11       scw #define CIRCQ_EMPTY(list)	((list)->cq_next_l == (list))
    166       1.1   thorpej 
    167      1.22        ad static void	callout_softclock(void *);
    168      1.22        ad 
    169  1.35.2.1      yamt struct callout_cpu {
    170  1.35.2.1      yamt 	kmutex_t	cc_lock;
    171  1.35.2.1      yamt 	sleepq_t	cc_sleepq;
    172  1.35.2.1      yamt 	u_int		cc_nwait;
    173  1.35.2.1      yamt 	u_int		cc_ticks;
    174  1.35.2.1      yamt 	lwp_t		*cc_lwp;
    175  1.35.2.1      yamt 	callout_impl_t	*cc_active;
    176  1.35.2.1      yamt 	callout_impl_t	*cc_cancel;
    177  1.35.2.1      yamt 	struct evcnt	cc_ev_late;
    178  1.35.2.1      yamt 	struct evcnt	cc_ev_block;
    179  1.35.2.1      yamt 	struct callout_circq cc_todo;		/* Worklist */
    180  1.35.2.1      yamt 	struct callout_circq cc_wheel[BUCKETS];	/* Queues of timeouts */
    181  1.35.2.1      yamt 	char		cc_name1[12];
    182  1.35.2.1      yamt 	char		cc_name2[12];
    183  1.35.2.1      yamt };
    184  1.35.2.1      yamt 
    185  1.35.2.1      yamt static struct callout_cpu callout_cpu0;
    186  1.35.2.1      yamt static void *callout_sih;
    187  1.35.2.1      yamt 
    188  1.35.2.1      yamt static inline kmutex_t *
    189  1.35.2.1      yamt callout_lock(callout_impl_t *c)
    190  1.35.2.1      yamt {
    191  1.35.2.1      yamt 	kmutex_t *lock;
    192  1.35.2.1      yamt 
    193  1.35.2.1      yamt 	for (;;) {
    194  1.35.2.1      yamt 		lock = &c->c_cpu->cc_lock;
    195  1.35.2.1      yamt 		mutex_spin_enter(lock);
    196  1.35.2.1      yamt 		if (__predict_true(lock == &c->c_cpu->cc_lock))
    197  1.35.2.1      yamt 			return lock;
    198  1.35.2.1      yamt 		mutex_spin_exit(lock);
    199  1.35.2.1      yamt 	}
    200  1.35.2.1      yamt }
    201       1.5   thorpej 
    202       1.1   thorpej /*
    203       1.1   thorpej  * callout_startup:
    204       1.1   thorpej  *
    205       1.1   thorpej  *	Initialize the callout facility, called at system startup time.
    206  1.35.2.1      yamt  *	Do just enough to allow callouts to be safely registered.
    207       1.1   thorpej  */
    208       1.1   thorpej void
    209       1.1   thorpej callout_startup(void)
    210       1.1   thorpej {
    211  1.35.2.1      yamt 	struct callout_cpu *cc;
    212       1.1   thorpej 	int b;
    213       1.1   thorpej 
    214  1.35.2.1      yamt 	KASSERT(curcpu()->ci_data.cpu_callout == NULL);
    215      1.22        ad 
    216  1.35.2.1      yamt 	cc = &callout_cpu0;
    217  1.35.2.1      yamt 	mutex_init(&cc->cc_lock, MUTEX_DEFAULT, IPL_SCHED);
    218  1.35.2.1      yamt 	CIRCQ_INIT(&cc->cc_todo);
    219       1.1   thorpej 	for (b = 0; b < BUCKETS; b++)
    220  1.35.2.1      yamt 		CIRCQ_INIT(&cc->cc_wheel[b]);
    221  1.35.2.1      yamt 	curcpu()->ci_data.cpu_callout = cc;
    222      1.22        ad }
    223      1.22        ad 
    224      1.22        ad /*
    225  1.35.2.1      yamt  * callout_init_cpu:
    226      1.22        ad  *
    227  1.35.2.1      yamt  *	Per-CPU initialization.
    228      1.22        ad  */
    229      1.22        ad void
    230  1.35.2.1      yamt callout_init_cpu(struct cpu_info *ci)
    231      1.22        ad {
    232  1.35.2.1      yamt 	struct callout_cpu *cc;
    233  1.35.2.1      yamt 	int b;
    234      1.22        ad 
    235  1.35.2.1      yamt 	KASSERT(sizeof(callout_impl_t) <= sizeof(callout_t));
    236  1.35.2.1      yamt 
    237  1.35.2.1      yamt 	if ((cc = ci->ci_data.cpu_callout) == NULL) {
    238  1.35.2.1      yamt 		cc = kmem_zalloc(sizeof(*cc), KM_SLEEP);
    239  1.35.2.1      yamt 		if (cc == NULL)
    240  1.35.2.1      yamt 			panic("callout_init_cpu (1)");
    241  1.35.2.1      yamt 		mutex_init(&cc->cc_lock, MUTEX_DEFAULT, IPL_SCHED);
    242  1.35.2.1      yamt 		CIRCQ_INIT(&cc->cc_todo);
    243  1.35.2.1      yamt 		for (b = 0; b < BUCKETS; b++)
    244  1.35.2.1      yamt 			CIRCQ_INIT(&cc->cc_wheel[b]);
    245  1.35.2.1      yamt 	} else {
    246  1.35.2.1      yamt 		/* Boot CPU, one time only. */
    247  1.35.2.1      yamt 		callout_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    248  1.35.2.1      yamt 		    callout_softclock, NULL);
    249  1.35.2.1      yamt 		if (callout_sih == NULL)
    250  1.35.2.1      yamt 			panic("callout_init_cpu (2)");
    251  1.35.2.1      yamt 	}
    252  1.35.2.1      yamt 
    253  1.35.2.1      yamt 	sleepq_init(&cc->cc_sleepq, &cc->cc_lock);
    254  1.35.2.1      yamt 
    255  1.35.2.1      yamt 	snprintf(cc->cc_name1, sizeof(cc->cc_name1), "late/%u",
    256  1.35.2.1      yamt 	    cpu_index(ci));
    257  1.35.2.1      yamt 	evcnt_attach_dynamic(&cc->cc_ev_late, EVCNT_TYPE_MISC,
    258  1.35.2.1      yamt 	    NULL, "callout", cc->cc_name1);
    259  1.35.2.1      yamt 
    260  1.35.2.1      yamt 	snprintf(cc->cc_name2, sizeof(cc->cc_name2), "wait/%u",
    261  1.35.2.1      yamt 	    cpu_index(ci));
    262  1.35.2.1      yamt 	evcnt_attach_dynamic(&cc->cc_ev_block, EVCNT_TYPE_MISC,
    263  1.35.2.1      yamt 	    NULL, "callout", cc->cc_name2);
    264  1.35.2.1      yamt 
    265  1.35.2.1      yamt 	ci->ci_data.cpu_callout = cc;
    266       1.1   thorpej }
    267       1.1   thorpej 
    268       1.1   thorpej /*
    269       1.1   thorpej  * callout_init:
    270       1.1   thorpej  *
    271  1.35.2.1      yamt  *	Initialize a callout structure.  This must be quick, so we fill
    272  1.35.2.1      yamt  *	only the minimum number of fields.
    273       1.1   thorpej  */
    274       1.1   thorpej void
    275      1.22        ad callout_init(callout_t *cs, u_int flags)
    276       1.1   thorpej {
    277      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    278  1.35.2.1      yamt 	struct callout_cpu *cc;
    279      1.22        ad 
    280      1.22        ad 	KASSERT((flags & ~CALLOUT_FLAGMASK) == 0);
    281       1.1   thorpej 
    282  1.35.2.1      yamt 	cc = curcpu()->ci_data.cpu_callout;
    283  1.35.2.1      yamt 	c->c_func = NULL;
    284      1.22        ad 	c->c_magic = CALLOUT_MAGIC;
    285  1.35.2.1      yamt 	if (__predict_true((flags & CALLOUT_MPSAFE) != 0 && cc != NULL)) {
    286  1.35.2.1      yamt 		c->c_flags = flags;
    287  1.35.2.1      yamt 		c->c_cpu = cc;
    288  1.35.2.1      yamt 		return;
    289  1.35.2.1      yamt 	}
    290  1.35.2.1      yamt 	c->c_flags = flags | CALLOUT_BOUND;
    291  1.35.2.1      yamt 	c->c_cpu = &callout_cpu0;
    292      1.22        ad }
    293      1.22        ad 
    294      1.22        ad /*
    295      1.22        ad  * callout_destroy:
    296      1.22        ad  *
    297      1.22        ad  *	Destroy a callout structure.  The callout must be stopped.
    298      1.22        ad  */
    299      1.22        ad void
    300      1.22        ad callout_destroy(callout_t *cs)
    301      1.22        ad {
    302      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    303      1.22        ad 
    304      1.22        ad 	/*
    305      1.22        ad 	 * It's not necessary to lock in order to see the correct value
    306      1.22        ad 	 * of c->c_flags.  If the callout could potentially have been
    307      1.22        ad 	 * running, the current thread should have stopped it.
    308      1.22        ad 	 */
    309      1.22        ad 	KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
    310  1.35.2.1      yamt 	KASSERT(c->c_cpu->cc_lwp == curlwp || c->c_cpu->cc_active != c);
    311      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    312      1.22        ad 	c->c_magic = 0;
    313       1.1   thorpej }
    314       1.1   thorpej 
    315       1.1   thorpej /*
    316      1.29     joerg  * callout_schedule_locked:
    317       1.1   thorpej  *
    318      1.29     joerg  *	Schedule a callout to run.  The function and argument must
    319      1.29     joerg  *	already be set in the callout structure.  Must be called with
    320      1.29     joerg  *	callout_lock.
    321       1.1   thorpej  */
    322      1.29     joerg static void
    323  1.35.2.1      yamt callout_schedule_locked(callout_impl_t *c, kmutex_t *lock, int to_ticks)
    324       1.1   thorpej {
    325  1.35.2.1      yamt 	struct callout_cpu *cc, *occ;
    326      1.20        ad 	int old_time;
    327       1.1   thorpej 
    328       1.1   thorpej 	KASSERT(to_ticks >= 0);
    329      1.29     joerg 	KASSERT(c->c_func != NULL);
    330       1.1   thorpej 
    331       1.1   thorpej 	/* Initialize the time here, it won't change. */
    332  1.35.2.1      yamt 	occ = c->c_cpu;
    333      1.22        ad 	c->c_flags &= ~CALLOUT_FIRED;
    334       1.1   thorpej 
    335       1.1   thorpej 	/*
    336       1.1   thorpej 	 * If this timeout is already scheduled and now is moved
    337  1.35.2.1      yamt 	 * earlier, reschedule it now.  Otherwise leave it in place
    338       1.1   thorpej 	 * and let it be rescheduled later.
    339       1.1   thorpej 	 */
    340      1.22        ad 	if ((c->c_flags & CALLOUT_PENDING) != 0) {
    341  1.35.2.1      yamt 		/* Leave on existing CPU. */
    342  1.35.2.1      yamt 		old_time = c->c_time;
    343  1.35.2.1      yamt 		c->c_time = to_ticks + occ->cc_ticks;
    344       1.4      yamt 		if (c->c_time - old_time < 0) {
    345       1.1   thorpej 			CIRCQ_REMOVE(&c->c_list);
    346  1.35.2.1      yamt 			CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
    347       1.1   thorpej 		}
    348  1.35.2.1      yamt 		mutex_spin_exit(lock);
    349  1.35.2.1      yamt 		return;
    350  1.35.2.1      yamt 	}
    351  1.35.2.1      yamt 
    352  1.35.2.1      yamt 	cc = curcpu()->ci_data.cpu_callout;
    353  1.35.2.1      yamt 	if ((c->c_flags & CALLOUT_BOUND) != 0 || cc == occ ||
    354  1.35.2.1      yamt 	    !mutex_tryenter(&cc->cc_lock)) {
    355  1.35.2.1      yamt 		/* Leave on existing CPU. */
    356  1.35.2.1      yamt 		c->c_time = to_ticks + occ->cc_ticks;
    357  1.35.2.1      yamt 		c->c_flags |= CALLOUT_PENDING;
    358  1.35.2.1      yamt 		CIRCQ_INSERT(&c->c_list, &occ->cc_todo);
    359       1.1   thorpej 	} else {
    360  1.35.2.1      yamt 		/* Move to this CPU. */
    361  1.35.2.1      yamt 		c->c_cpu = cc;
    362  1.35.2.1      yamt 		c->c_time = to_ticks + cc->cc_ticks;
    363       1.1   thorpej 		c->c_flags |= CALLOUT_PENDING;
    364  1.35.2.1      yamt 		CIRCQ_INSERT(&c->c_list, &cc->cc_todo);
    365  1.35.2.1      yamt 		mutex_spin_exit(&cc->cc_lock);
    366       1.1   thorpej 	}
    367  1.35.2.1      yamt 	mutex_spin_exit(lock);
    368      1.29     joerg }
    369      1.29     joerg 
    370      1.29     joerg /*
    371      1.29     joerg  * callout_reset:
    372      1.29     joerg  *
    373      1.29     joerg  *	Reset a callout structure with a new function and argument, and
    374      1.29     joerg  *	schedule it to run.
    375      1.29     joerg  */
    376      1.29     joerg void
    377      1.29     joerg callout_reset(callout_t *cs, int to_ticks, void (*func)(void *), void *arg)
    378      1.29     joerg {
    379      1.29     joerg 	callout_impl_t *c = (callout_impl_t *)cs;
    380  1.35.2.1      yamt 	kmutex_t *lock;
    381      1.29     joerg 
    382      1.29     joerg 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    383      1.29     joerg 
    384  1.35.2.1      yamt 	lock = callout_lock(c);
    385      1.29     joerg 	c->c_func = func;
    386      1.29     joerg 	c->c_arg = arg;
    387  1.35.2.1      yamt 	callout_schedule_locked(c, lock, to_ticks);
    388       1.1   thorpej }
    389       1.1   thorpej 
    390       1.1   thorpej /*
    391       1.1   thorpej  * callout_schedule:
    392       1.1   thorpej  *
    393       1.1   thorpej  *	Schedule a callout to run.  The function and argument must
    394       1.1   thorpej  *	already be set in the callout structure.
    395       1.1   thorpej  */
    396       1.1   thorpej void
    397      1.22        ad callout_schedule(callout_t *cs, int to_ticks)
    398       1.1   thorpej {
    399      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    400  1.35.2.1      yamt 	kmutex_t *lock;
    401       1.1   thorpej 
    402      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    403       1.1   thorpej 
    404  1.35.2.1      yamt 	lock = callout_lock(c);
    405  1.35.2.1      yamt 	callout_schedule_locked(c, lock, to_ticks);
    406       1.1   thorpej }
    407       1.1   thorpej 
    408       1.1   thorpej /*
    409       1.1   thorpej  * callout_stop:
    410       1.1   thorpej  *
    411  1.35.2.1      yamt  *	Try to cancel a pending callout.  It may be too late: the callout
    412  1.35.2.1      yamt  *	could be running on another CPU.  If called from interrupt context,
    413  1.35.2.1      yamt  *	the callout could already be in progress at a lower priority.
    414       1.1   thorpej  */
    415      1.22        ad bool
    416      1.22        ad callout_stop(callout_t *cs)
    417       1.1   thorpej {
    418      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    419  1.35.2.1      yamt 	struct callout_cpu *cc;
    420  1.35.2.1      yamt 	kmutex_t *lock;
    421      1.22        ad 	bool expired;
    422      1.22        ad 
    423      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    424       1.1   thorpej 
    425  1.35.2.1      yamt 	lock = callout_lock(c);
    426      1.20        ad 
    427      1.22        ad 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    428       1.1   thorpej 		CIRCQ_REMOVE(&c->c_list);
    429      1.32        ad 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
    430      1.32        ad 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    431      1.32        ad 
    432  1.35.2.1      yamt 	cc = c->c_cpu;
    433  1.35.2.1      yamt 	if (cc->cc_active == c) {
    434      1.32        ad 		/*
    435      1.32        ad 		 * This is for non-MPSAFE callouts only.  To synchronize
    436      1.32        ad 		 * effectively we must be called with kernel_lock held.
    437      1.32        ad 		 * It's also taken in callout_softclock.
    438      1.32        ad 		 */
    439  1.35.2.1      yamt 		cc->cc_cancel = c;
    440      1.32        ad 	}
    441      1.32        ad 
    442  1.35.2.1      yamt 	mutex_spin_exit(lock);
    443      1.32        ad 
    444      1.32        ad 	return expired;
    445      1.32        ad }
    446      1.32        ad 
    447      1.32        ad /*
    448      1.32        ad  * callout_halt:
    449      1.32        ad  *
    450      1.32        ad  *	Cancel a pending callout.  If in-flight, block until it completes.
    451  1.35.2.1      yamt  *	May not be called from a hard interrupt handler.  If the callout
    452  1.35.2.1      yamt  * 	can take locks, the caller of callout_halt() must not hold any of
    453  1.35.2.1      yamt  *	those locks, otherwise the two could deadlock.  If 'interlock' is
    454  1.35.2.1      yamt  *	non-NULL and we must wait for the callout to complete, it will be
    455  1.35.2.1      yamt  *	released and re-acquired before returning.
    456      1.32        ad  */
    457      1.32        ad bool
    458  1.35.2.1      yamt callout_halt(callout_t *cs, void *interlock)
    459      1.32        ad {
    460      1.32        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    461  1.35.2.1      yamt 	struct callout_cpu *cc;
    462      1.32        ad 	struct lwp *l;
    463  1.35.2.1      yamt 	kmutex_t *lock, *relock;
    464      1.32        ad 	bool expired;
    465      1.32        ad 
    466      1.32        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    467      1.32        ad 	KASSERT(!cpu_intr_p());
    468      1.32        ad 
    469  1.35.2.1      yamt 	lock = callout_lock(c);
    470  1.35.2.1      yamt 	relock = NULL;
    471       1.1   thorpej 
    472      1.22        ad 	expired = ((c->c_flags & CALLOUT_FIRED) != 0);
    473      1.32        ad 	if ((c->c_flags & CALLOUT_PENDING) != 0)
    474      1.32        ad 		CIRCQ_REMOVE(&c->c_list);
    475       1.9        he 	c->c_flags &= ~(CALLOUT_PENDING|CALLOUT_FIRED);
    476       1.1   thorpej 
    477      1.32        ad 	l = curlwp;
    478  1.35.2.1      yamt 	for (;;) {
    479  1.35.2.1      yamt 		cc = c->c_cpu;
    480  1.35.2.1      yamt 		if (__predict_true(cc->cc_active != c || cc->cc_lwp == l))
    481  1.35.2.1      yamt 			break;
    482  1.35.2.1      yamt 		if (interlock != NULL) {
    483  1.35.2.1      yamt 			/*
    484  1.35.2.1      yamt 			 * Avoid potential scheduler lock order problems by
    485  1.35.2.1      yamt 			 * dropping the interlock without the callout lock
    486  1.35.2.1      yamt 			 * held.
    487  1.35.2.1      yamt 			 */
    488  1.35.2.1      yamt 			mutex_spin_exit(lock);
    489  1.35.2.1      yamt 			mutex_exit(interlock);
    490  1.35.2.1      yamt 			relock = interlock;
    491  1.35.2.1      yamt 			interlock = NULL;
    492  1.35.2.1      yamt 		} else {
    493  1.35.2.1      yamt 			/* XXX Better to do priority inheritance. */
    494  1.35.2.1      yamt 			KASSERT(l->l_wchan == NULL);
    495  1.35.2.1      yamt 			cc->cc_nwait++;
    496  1.35.2.1      yamt 			cc->cc_ev_block.ev_count++;
    497  1.35.2.1      yamt 			l->l_kpriority = true;
    498  1.35.2.1      yamt 			sleepq_enter(&cc->cc_sleepq, l);
    499  1.35.2.1      yamt 			sleepq_enqueue(&cc->cc_sleepq, cc, "callout",
    500  1.35.2.1      yamt 			    &sleep_syncobj);
    501  1.35.2.1      yamt 			KERNEL_UNLOCK_ALL(l, &l->l_biglocks);
    502  1.35.2.1      yamt 			sleepq_block(0, false);
    503  1.35.2.1      yamt 		}
    504  1.35.2.1      yamt 		lock = callout_lock(c);
    505      1.32        ad 	}
    506      1.32        ad 
    507  1.35.2.1      yamt 	mutex_spin_exit(lock);
    508  1.35.2.1      yamt 	if (__predict_false(relock != NULL))
    509  1.35.2.1      yamt 		mutex_enter(relock);
    510      1.22        ad 
    511      1.22        ad 	return expired;
    512      1.22        ad }
    513      1.22        ad 
    514  1.35.2.1      yamt #ifdef notyet
    515  1.35.2.1      yamt /*
    516  1.35.2.1      yamt  * callout_bind:
    517  1.35.2.1      yamt  *
    518  1.35.2.1      yamt  *	Bind a callout so that it will only execute on one CPU.
    519  1.35.2.1      yamt  *	The callout must be stopped, and must be MPSAFE.
    520  1.35.2.1      yamt  *
    521  1.35.2.1      yamt  *	XXX Disabled for now until it is decided how to handle
    522  1.35.2.1      yamt  *	offlined CPUs.  We may want weak+strong binding.
    523  1.35.2.1      yamt  */
    524  1.35.2.1      yamt void
    525  1.35.2.1      yamt callout_bind(callout_t *cs, struct cpu_info *ci)
    526  1.35.2.1      yamt {
    527  1.35.2.1      yamt 	callout_impl_t *c = (callout_impl_t *)cs;
    528  1.35.2.1      yamt 	struct callout_cpu *cc;
    529  1.35.2.1      yamt 	kmutex_t *lock;
    530  1.35.2.1      yamt 
    531  1.35.2.1      yamt 	KASSERT((c->c_flags & CALLOUT_PENDING) == 0);
    532  1.35.2.1      yamt 	KASSERT(c->c_cpu->cc_active != c);
    533  1.35.2.1      yamt 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    534  1.35.2.1      yamt 	KASSERT((c->c_flags & CALLOUT_MPSAFE) != 0);
    535  1.35.2.1      yamt 
    536  1.35.2.1      yamt 	lock = callout_lock(c);
    537  1.35.2.1      yamt 	cc = ci->ci_data.cpu_callout;
    538  1.35.2.1      yamt 	c->c_flags |= CALLOUT_BOUND;
    539  1.35.2.1      yamt 	if (c->c_cpu != cc) {
    540  1.35.2.1      yamt 		/*
    541  1.35.2.1      yamt 		 * Assigning c_cpu effectively unlocks the callout
    542  1.35.2.1      yamt 		 * structure, as we don't hold the new CPU's lock.
    543  1.35.2.1      yamt 		 * Issue memory barrier to prevent accesses being
    544  1.35.2.1      yamt 		 * reordered.
    545  1.35.2.1      yamt 		 */
    546  1.35.2.1      yamt 		membar_exit();
    547  1.35.2.1      yamt 		c->c_cpu = cc;
    548  1.35.2.1      yamt 	}
    549  1.35.2.1      yamt 	mutex_spin_exit(lock);
    550  1.35.2.1      yamt }
    551  1.35.2.1      yamt #endif
    552  1.35.2.1      yamt 
    553      1.22        ad void
    554      1.22        ad callout_setfunc(callout_t *cs, void (*func)(void *), void *arg)
    555      1.22        ad {
    556      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    557  1.35.2.1      yamt 	kmutex_t *lock;
    558      1.22        ad 
    559      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    560      1.22        ad 
    561  1.35.2.1      yamt 	lock = callout_lock(c);
    562      1.22        ad 	c->c_func = func;
    563      1.22        ad 	c->c_arg = arg;
    564  1.35.2.1      yamt 	mutex_spin_exit(lock);
    565      1.22        ad }
    566      1.22        ad 
    567      1.22        ad bool
    568      1.22        ad callout_expired(callout_t *cs)
    569      1.22        ad {
    570      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    571  1.35.2.1      yamt 	kmutex_t *lock;
    572      1.22        ad 	bool rv;
    573      1.22        ad 
    574      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    575      1.22        ad 
    576  1.35.2.1      yamt 	lock = callout_lock(c);
    577      1.22        ad 	rv = ((c->c_flags & CALLOUT_FIRED) != 0);
    578  1.35.2.1      yamt 	mutex_spin_exit(lock);
    579      1.22        ad 
    580      1.22        ad 	return rv;
    581      1.22        ad }
    582      1.22        ad 
    583      1.22        ad bool
    584      1.22        ad callout_active(callout_t *cs)
    585      1.22        ad {
    586      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    587  1.35.2.1      yamt 	kmutex_t *lock;
    588      1.22        ad 	bool rv;
    589      1.22        ad 
    590      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    591      1.22        ad 
    592  1.35.2.1      yamt 	lock = callout_lock(c);
    593      1.22        ad 	rv = ((c->c_flags & (CALLOUT_PENDING|CALLOUT_FIRED)) != 0);
    594  1.35.2.1      yamt 	mutex_spin_exit(lock);
    595      1.22        ad 
    596      1.22        ad 	return rv;
    597      1.22        ad }
    598      1.22        ad 
    599      1.22        ad bool
    600      1.22        ad callout_pending(callout_t *cs)
    601      1.22        ad {
    602      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    603  1.35.2.1      yamt 	kmutex_t *lock;
    604      1.22        ad 	bool rv;
    605      1.22        ad 
    606      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    607      1.22        ad 
    608  1.35.2.1      yamt 	lock = callout_lock(c);
    609      1.22        ad 	rv = ((c->c_flags & CALLOUT_PENDING) != 0);
    610  1.35.2.1      yamt 	mutex_spin_exit(lock);
    611      1.22        ad 
    612      1.22        ad 	return rv;
    613      1.22        ad }
    614      1.22        ad 
    615      1.22        ad bool
    616      1.22        ad callout_invoking(callout_t *cs)
    617      1.22        ad {
    618      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    619  1.35.2.1      yamt 	kmutex_t *lock;
    620      1.22        ad 	bool rv;
    621      1.22        ad 
    622      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    623      1.22        ad 
    624  1.35.2.1      yamt 	lock = callout_lock(c);
    625      1.22        ad 	rv = ((c->c_flags & CALLOUT_INVOKING) != 0);
    626  1.35.2.1      yamt 	mutex_spin_exit(lock);
    627      1.22        ad 
    628      1.22        ad 	return rv;
    629      1.22        ad }
    630      1.22        ad 
    631      1.22        ad void
    632      1.22        ad callout_ack(callout_t *cs)
    633      1.22        ad {
    634      1.22        ad 	callout_impl_t *c = (callout_impl_t *)cs;
    635  1.35.2.1      yamt 	kmutex_t *lock;
    636      1.22        ad 
    637      1.22        ad 	KASSERT(c->c_magic == CALLOUT_MAGIC);
    638      1.22        ad 
    639  1.35.2.1      yamt 	lock = callout_lock(c);
    640      1.22        ad 	c->c_flags &= ~CALLOUT_INVOKING;
    641  1.35.2.1      yamt 	mutex_spin_exit(lock);
    642       1.1   thorpej }
    643       1.1   thorpej 
    644       1.1   thorpej /*
    645  1.35.2.1      yamt  * callout_hardclock:
    646  1.35.2.1      yamt  *
    647  1.35.2.1      yamt  *	Called from hardclock() once every tick.  We schedule a soft
    648  1.35.2.1      yamt  *	interrupt if there is work to be done.
    649       1.1   thorpej  */
    650      1.22        ad void
    651       1.1   thorpej callout_hardclock(void)
    652       1.1   thorpej {
    653  1.35.2.1      yamt 	struct callout_cpu *cc;
    654  1.35.2.1      yamt 	int needsoftclock, ticks;
    655       1.1   thorpej 
    656  1.35.2.1      yamt 	cc = curcpu()->ci_data.cpu_callout;
    657  1.35.2.1      yamt 	mutex_spin_enter(&cc->cc_lock);
    658       1.1   thorpej 
    659  1.35.2.1      yamt 	ticks = ++cc->cc_ticks;
    660  1.35.2.1      yamt 
    661  1.35.2.1      yamt 	MOVEBUCKET(cc, 0, ticks);
    662  1.35.2.1      yamt 	if (MASKWHEEL(0, ticks) == 0) {
    663  1.35.2.1      yamt 		MOVEBUCKET(cc, 1, ticks);
    664  1.35.2.1      yamt 		if (MASKWHEEL(1, ticks) == 0) {
    665  1.35.2.1      yamt 			MOVEBUCKET(cc, 2, ticks);
    666  1.35.2.1      yamt 			if (MASKWHEEL(2, ticks) == 0)
    667  1.35.2.1      yamt 				MOVEBUCKET(cc, 3, ticks);
    668       1.1   thorpej 		}
    669       1.1   thorpej 	}
    670       1.1   thorpej 
    671  1.35.2.1      yamt 	needsoftclock = !CIRCQ_EMPTY(&cc->cc_todo);
    672  1.35.2.1      yamt 	mutex_spin_exit(&cc->cc_lock);
    673       1.1   thorpej 
    674      1.22        ad 	if (needsoftclock)
    675  1.35.2.1      yamt 		softint_schedule(callout_sih);
    676       1.1   thorpej }
    677       1.1   thorpej 
    678  1.35.2.1      yamt /*
    679  1.35.2.1      yamt  * callout_softclock:
    680  1.35.2.1      yamt  *
    681  1.35.2.1      yamt  *	Soft interrupt handler, scheduled above if there is work to
    682  1.35.2.1      yamt  * 	be done.  Callouts are made in soft interrupt context.
    683  1.35.2.1      yamt  */
    684      1.22        ad static void
    685      1.22        ad callout_softclock(void *v)
    686       1.1   thorpej {
    687      1.22        ad 	callout_impl_t *c;
    688  1.35.2.1      yamt 	struct callout_cpu *cc;
    689       1.1   thorpej 	void (*func)(void *);
    690       1.1   thorpej 	void *arg;
    691  1.35.2.1      yamt 	int mpsafe, count, ticks, delta;
    692      1.22        ad 	lwp_t *l;
    693       1.1   thorpej 
    694      1.22        ad 	l = curlwp;
    695  1.35.2.1      yamt 	KASSERT(l->l_cpu == curcpu());
    696  1.35.2.1      yamt 	cc = l->l_cpu->ci_data.cpu_callout;
    697       1.1   thorpej 
    698  1.35.2.1      yamt 	mutex_spin_enter(&cc->cc_lock);
    699  1.35.2.1      yamt 	cc->cc_lwp = l;
    700  1.35.2.1      yamt 	while (!CIRCQ_EMPTY(&cc->cc_todo)) {
    701  1.35.2.1      yamt 		c = CIRCQ_FIRST(&cc->cc_todo);
    702      1.22        ad 		KASSERT(c->c_magic == CALLOUT_MAGIC);
    703      1.22        ad 		KASSERT(c->c_func != NULL);
    704  1.35.2.1      yamt 		KASSERT(c->c_cpu == cc);
    705      1.26        ad 		KASSERT((c->c_flags & CALLOUT_PENDING) != 0);
    706      1.26        ad 		KASSERT((c->c_flags & CALLOUT_FIRED) == 0);
    707       1.1   thorpej 		CIRCQ_REMOVE(&c->c_list);
    708       1.1   thorpej 
    709       1.1   thorpej 		/* If due run it, otherwise insert it into the right bucket. */
    710  1.35.2.1      yamt 		ticks = cc->cc_ticks;
    711  1.35.2.1      yamt 		delta = c->c_time - ticks;
    712  1.35.2.1      yamt 		if (delta > 0) {
    713  1.35.2.1      yamt 			CIRCQ_INSERT(&c->c_list, BUCKET(cc, delta, c->c_time));
    714  1.35.2.1      yamt 			continue;
    715  1.35.2.1      yamt 		}
    716  1.35.2.1      yamt 		if (delta < 0)
    717  1.35.2.1      yamt 			cc->cc_ev_late.ev_count++;
    718       1.1   thorpej 
    719  1.35.2.1      yamt 		c->c_flags ^= (CALLOUT_PENDING | CALLOUT_FIRED);
    720  1.35.2.1      yamt 		mpsafe = (c->c_flags & CALLOUT_MPSAFE);
    721  1.35.2.1      yamt 		func = c->c_func;
    722  1.35.2.1      yamt 		arg = c->c_arg;
    723  1.35.2.1      yamt 		cc->cc_active = c;
    724  1.35.2.1      yamt 
    725  1.35.2.1      yamt 		mutex_spin_exit(&cc->cc_lock);
    726  1.35.2.1      yamt 		if (!mpsafe) {
    727  1.35.2.1      yamt 			KERNEL_LOCK(1, NULL);
    728  1.35.2.1      yamt 			(*func)(arg);
    729  1.35.2.1      yamt 			KERNEL_UNLOCK_ONE(NULL);
    730  1.35.2.1      yamt 		} else
    731  1.35.2.1      yamt 			(*func)(arg);
    732  1.35.2.1      yamt 		mutex_spin_enter(&cc->cc_lock);
    733       1.1   thorpej 
    734  1.35.2.1      yamt 		/*
    735  1.35.2.1      yamt 		 * We can't touch 'c' here because it might be
    736  1.35.2.1      yamt 		 * freed already.  If LWPs waiting for callout
    737  1.35.2.1      yamt 		 * to complete, awaken them.
    738  1.35.2.1      yamt 		 */
    739  1.35.2.1      yamt 		cc->cc_active = NULL;
    740  1.35.2.1      yamt 		if ((count = cc->cc_nwait) != 0) {
    741  1.35.2.1      yamt 			cc->cc_nwait = 0;
    742  1.35.2.1      yamt 			/* sleepq_wake() drops the lock. */
    743  1.35.2.1      yamt 			sleepq_wake(&cc->cc_sleepq, cc, count);
    744  1.35.2.1      yamt 			mutex_spin_enter(&cc->cc_lock);
    745       1.1   thorpej 		}
    746       1.1   thorpej 	}
    747  1.35.2.1      yamt 	cc->cc_lwp = NULL;
    748  1.35.2.1      yamt 	mutex_spin_exit(&cc->cc_lock);
    749       1.1   thorpej }
    750       1.1   thorpej 
    751       1.1   thorpej #ifdef DDB
    752       1.1   thorpej static void
    753  1.35.2.1      yamt db_show_callout_bucket(struct callout_cpu *cc, struct callout_circq *bucket)
    754       1.1   thorpej {
    755      1.22        ad 	callout_impl_t *c;
    756       1.1   thorpej 	db_expr_t offset;
    757      1.15  christos 	const char *name;
    758      1.15  christos 	static char question[] = "?";
    759  1.35.2.1      yamt 	int b;
    760       1.1   thorpej 
    761      1.11       scw 	if (CIRCQ_EMPTY(bucket))
    762      1.11       scw 		return;
    763      1.11       scw 
    764      1.11       scw 	for (c = CIRCQ_FIRST(bucket); /*nothing*/; c = CIRCQ_NEXT(&c->c_list)) {
    765      1.10       scw 		db_find_sym_and_offset((db_addr_t)(intptr_t)c->c_func, &name,
    766      1.10       scw 		    &offset);
    767      1.15  christos 		name = name ? name : question;
    768  1.35.2.1      yamt 		b = (bucket - cc->cc_wheel);
    769  1.35.2.1      yamt 		if (b < 0)
    770  1.35.2.1      yamt 			b = -WHEELSIZE;
    771  1.35.2.1      yamt 		db_printf("%9d %2d/%-4d %16lx  %s\n",
    772  1.35.2.1      yamt 		    c->c_time - cc->cc_ticks, b / WHEELSIZE, b,
    773  1.35.2.1      yamt 		    (u_long)c->c_arg, name);
    774      1.11       scw 		if (CIRCQ_LAST(&c->c_list, bucket))
    775      1.11       scw 			break;
    776       1.1   thorpej 	}
    777       1.1   thorpej }
    778       1.1   thorpej 
    779       1.1   thorpej void
    780      1.21      matt db_show_callout(db_expr_t addr, bool haddr, db_expr_t count, const char *modif)
    781       1.1   thorpej {
    782  1.35.2.1      yamt 	CPU_INFO_ITERATOR cii;
    783  1.35.2.1      yamt 	struct callout_cpu *cc;
    784  1.35.2.1      yamt 	struct cpu_info *ci;
    785       1.1   thorpej 	int b;
    786       1.1   thorpej 
    787       1.1   thorpej 	db_printf("hardclock_ticks now: %d\n", hardclock_ticks);
    788       1.1   thorpej 	db_printf("    ticks  wheel               arg  func\n");
    789       1.1   thorpej 
    790       1.1   thorpej 	/*
    791       1.1   thorpej 	 * Don't lock the callwheel; all the other CPUs are paused
    792       1.1   thorpej 	 * anyhow, and we might be called in a circumstance where
    793       1.1   thorpej 	 * some other CPU was paused while holding the lock.
    794       1.1   thorpej 	 */
    795  1.35.2.1      yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    796  1.35.2.1      yamt 		cc = ci->ci_data.cpu_callout;
    797  1.35.2.1      yamt 		db_show_callout_bucket(cc, &cc->cc_todo);
    798  1.35.2.1      yamt 	}
    799  1.35.2.1      yamt 	for (b = 0; b < BUCKETS; b++) {
    800  1.35.2.1      yamt 		for (CPU_INFO_FOREACH(cii, ci)) {
    801  1.35.2.1      yamt 			cc = ci->ci_data.cpu_callout;
    802  1.35.2.1      yamt 			db_show_callout_bucket(cc, &cc->cc_wheel[b]);
    803  1.35.2.1      yamt 		}
    804  1.35.2.1      yamt 	}
    805       1.1   thorpej }
    806       1.1   thorpej #endif /* DDB */
    807