2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * From: @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_callout_profiling.h"
41 #include "opt_kdtrace.h"
44 #include "opt_timer.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/callout.h>
52 #include <sys/interrupt.h>
53 #include <sys/kernel.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
60 #include <sys/sleepqueue.h>
61 #include <sys/sysctl.h>
66 #include <machine/_inttypes.h>
70 #include <machine/cpu.h>
73 #ifndef NO_EVENTTIMERS
74 DPCPU_DECLARE(sbintime_t, hardclocktime);
77 SDT_PROVIDER_DEFINE(callout_execute);
78 SDT_PROBE_DEFINE1(callout_execute, , , callout__start, "struct callout *");
79 SDT_PROBE_DEFINE1(callout_execute, , , callout__end, "struct callout *");
81 #ifdef CALLOUT_PROFILING
83 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
84 "Average number of items examined per softclock call. Units = 1/1000");
85 static int avg_gcalls;
86 SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
87 "Average number of Giant callouts made per softclock call. Units = 1/1000");
88 static int avg_lockcalls;
89 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0,
90 "Average number of lock callouts made per softclock call. Units = 1/1000");
91 static int avg_mpcalls;
92 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
93 "Average number of MP callouts made per softclock call. Units = 1/1000");
94 static int avg_depth_dir;
95 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth_dir, CTLFLAG_RD, &avg_depth_dir, 0,
96 "Average number of direct callouts examined per callout_process call. "
98 static int avg_lockcalls_dir;
99 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls_dir, CTLFLAG_RD,
100 &avg_lockcalls_dir, 0, "Average number of lock direct callouts made per "
101 "callout_process call. Units = 1/1000");
102 static int avg_mpcalls_dir;
103 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls_dir, CTLFLAG_RD, &avg_mpcalls_dir,
104 0, "Average number of MP direct callouts made per callout_process call. "
109 SYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN, &ncallout, 0,
110 "Number of entries in callwheel and size of timeout() preallocation");
114 * allocate more timeout table slots when table overflows.
116 u_int callwheelsize, callwheelmask;
119 * The callout cpu exec entities represent informations necessary for
120 * describing the state of callouts currently running on the CPU and the ones
121 * necessary for migrating callouts to the new callout cpu. In particular,
122 * the first entry of the array cc_exec_entity holds informations for callout
123 * running in SWI thread context, while the second one holds informations
124 * for callout running directly from hardware interrupt context.
125 * The cached informations are very important for deferring migration when
126 * the migrating callout is already running.
129 struct callout *cc_curr;
131 void (*ce_migration_func)(void *);
132 void *ce_migration_arg;
133 int ce_migration_cpu;
134 sbintime_t ce_migration_time;
135 sbintime_t ce_migration_prec;
142 * There is one struct callout_cpu per cpu, holding all relevant
143 * state for the callout processing thread on the individual CPU.
146 struct mtx_padalign cc_lock;
147 struct cc_exec cc_exec_entity[2];
148 struct callout *cc_next;
149 struct callout *cc_callout;
150 struct callout_list *cc_callwheel;
151 struct callout_tailq cc_expireq;
152 struct callout_slist cc_callfree;
153 sbintime_t cc_firstevent;
154 sbintime_t cc_lastscan;
158 char cc_ktr_event_name[20];
161 #define callout_migrating(c) ((c)->c_iflags & CALLOUT_DFRMIGRATION)
163 #define cc_exec_curr(cc, dir) cc->cc_exec_entity[dir].cc_curr
164 #define cc_exec_next(cc) cc->cc_next
165 #define cc_exec_cancel(cc, dir) cc->cc_exec_entity[dir].cc_cancel
166 #define cc_exec_waiting(cc, dir) cc->cc_exec_entity[dir].cc_waiting
168 #define cc_migration_func(cc, dir) cc->cc_exec_entity[dir].ce_migration_func
169 #define cc_migration_arg(cc, dir) cc->cc_exec_entity[dir].ce_migration_arg
170 #define cc_migration_cpu(cc, dir) cc->cc_exec_entity[dir].ce_migration_cpu
171 #define cc_migration_time(cc, dir) cc->cc_exec_entity[dir].ce_migration_time
172 #define cc_migration_prec(cc, dir) cc->cc_exec_entity[dir].ce_migration_prec
174 struct callout_cpu cc_cpu[MAXCPU];
175 #define CPUBLOCK MAXCPU
176 #define CC_CPU(cpu) (&cc_cpu[(cpu)])
177 #define CC_SELF() CC_CPU(PCPU_GET(cpuid))
179 struct callout_cpu cc_cpu;
180 #define CC_CPU(cpu) &cc_cpu
181 #define CC_SELF() &cc_cpu
183 #define CC_LOCK(cc) mtx_lock_spin(&(cc)->cc_lock)
184 #define CC_UNLOCK(cc) mtx_unlock_spin(&(cc)->cc_lock)
185 #define CC_LOCK_ASSERT(cc) mtx_assert(&(cc)->cc_lock, MA_OWNED)
187 static int timeout_cpu;
189 static void callout_cpu_init(struct callout_cpu *cc, int cpu);
190 static void softclock_call_cc(struct callout *c, struct callout_cpu *cc,
191 #ifdef CALLOUT_PROFILING
192 int *mpcalls, int *lockcalls, int *gcalls,
196 static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
200 * cc_curr - If a callout is in progress, it is cc_curr.
201 * If cc_curr is non-NULL, threads waiting in
202 * callout_drain() will be woken up as soon as the
203 * relevant callout completes.
204 * cc_cancel - Changing to 1 with both callout_lock and cc_lock held
205 * guarantees that the current callout will not run.
206 * The softclock() function sets this to 0 before it
207 * drops callout_lock to acquire c_lock, and it calls
208 * the handler only if curr_cancelled is still 0 after
209 * cc_lock is successfully acquired.
210 * cc_waiting - If a thread is waiting in callout_drain(), then
211 * callout_wait is nonzero. Set only when
212 * cc_curr is non-NULL.
216 * Resets the execution entity tied to a specific callout cpu.
219 cc_cce_cleanup(struct callout_cpu *cc, int direct)
222 cc_exec_curr(cc, direct) = NULL;
223 cc_exec_cancel(cc, direct) = false;
224 cc_exec_waiting(cc, direct) = false;
226 cc_migration_cpu(cc, direct) = CPUBLOCK;
227 cc_migration_time(cc, direct) = 0;
228 cc_migration_prec(cc, direct) = 0;
229 cc_migration_func(cc, direct) = NULL;
230 cc_migration_arg(cc, direct) = NULL;
235 * Checks if migration is requested by a specific callout cpu.
238 cc_cce_migrating(struct callout_cpu *cc, int direct)
242 return (cc_migration_cpu(cc, direct) != CPUBLOCK);
249 * Kernel low level callwheel initialization
250 * called on cpu0 during kernel startup.
253 callout_callwheel_init(void *dummy)
255 struct callout_cpu *cc;
258 * Calculate the size of the callout wheel and the preallocated
259 * timeout() structures.
260 * XXX: Clip callout to result of previous function of maxusers
261 * maximum 384. This is still huge, but acceptable.
263 memset(CC_CPU(0), 0, sizeof(cc_cpu));
264 ncallout = imin(16 + maxproc + maxfiles, 18508);
265 TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
268 * Calculate callout wheel size, should be next power of two higher
271 callwheelsize = 1 << fls(ncallout);
272 callwheelmask = callwheelsize - 1;
275 * Only cpu0 handles timeout(9) and receives a preallocation.
277 * XXX: Once all timeout(9) consumers are converted this can
280 timeout_cpu = PCPU_GET(cpuid);
281 cc = CC_CPU(timeout_cpu);
282 cc->cc_callout = malloc(ncallout * sizeof(struct callout),
283 M_CALLOUT, M_WAITOK);
284 callout_cpu_init(cc, timeout_cpu);
286 SYSINIT(callwheel_init, SI_SUB_CPU, SI_ORDER_ANY, callout_callwheel_init, NULL);
289 * Initialize the per-cpu callout structures.
292 callout_cpu_init(struct callout_cpu *cc, int cpu)
297 mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
298 SLIST_INIT(&cc->cc_callfree);
300 cc->cc_callwheel = malloc(sizeof(struct callout_list) * callwheelsize,
301 M_CALLOUT, M_WAITOK);
302 for (i = 0; i < callwheelsize; i++)
303 LIST_INIT(&cc->cc_callwheel[i]);
304 TAILQ_INIT(&cc->cc_expireq);
305 cc->cc_firstevent = SBT_MAX;
306 for (i = 0; i < 2; i++)
307 cc_cce_cleanup(cc, i);
308 snprintf(cc->cc_ktr_event_name, sizeof(cc->cc_ktr_event_name),
309 "callwheel cpu %d", cpu);
310 if (cc->cc_callout == NULL) /* Only cpu0 handles timeout(9) */
312 for (i = 0; i < ncallout; i++) {
313 c = &cc->cc_callout[i];
315 c->c_iflags = CALLOUT_LOCAL_ALLOC;
316 SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
322 * Switches the cpu tied to a specific callout.
323 * The function expects a locked incoming callout cpu and returns with
324 * locked outcoming callout cpu.
326 static struct callout_cpu *
327 callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu)
329 struct callout_cpu *new_cc;
331 MPASS(c != NULL && cc != NULL);
335 * Avoid interrupts and preemption firing after the callout cpu
336 * is blocked in order to avoid deadlocks as the new thread
337 * may be willing to acquire the callout cpu lock.
342 new_cc = CC_CPU(new_cpu);
351 * Start standard softclock thread.
354 start_softclock(void *dummy)
356 struct callout_cpu *cc;
361 cc = CC_CPU(timeout_cpu);
362 if (swi_add(&clk_intr_event, "clock", softclock, cc, SWI_CLOCK,
363 INTR_MPSAFE, &cc->cc_cookie))
364 panic("died while creating standard software ithreads");
367 if (cpu == timeout_cpu)
370 cc->cc_callout = NULL; /* Only cpu0 handles timeout(9). */
371 callout_cpu_init(cc, cpu);
372 if (swi_add(NULL, "clock", softclock, cc, SWI_CLOCK,
373 INTR_MPSAFE, &cc->cc_cookie))
374 panic("died while creating standard software ithreads");
378 SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
380 #define CC_HASH_SHIFT 8
383 callout_hash(sbintime_t sbt)
386 return (sbt >> (32 - CC_HASH_SHIFT));
390 callout_get_bucket(sbintime_t sbt)
393 return (callout_hash(sbt) & callwheelmask);
397 callout_process(sbintime_t now)
399 struct callout *tmp, *tmpn;
400 struct callout_cpu *cc;
401 struct callout_list *sc;
402 sbintime_t first, last, max, tmp_max;
404 u_int firstb, lastb, nowb;
405 #ifdef CALLOUT_PROFILING
406 int depth_dir = 0, mpcalls_dir = 0, lockcalls_dir = 0;
410 mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
412 /* Compute the buckets of the last scan and present times. */
413 firstb = callout_hash(cc->cc_lastscan);
414 cc->cc_lastscan = now;
415 nowb = callout_hash(now);
417 /* Compute the last bucket and minimum time of the bucket after it. */
419 lookahead = (SBT_1S / 16);
420 else if (nowb - firstb == 1)
421 lookahead = (SBT_1S / 8);
423 lookahead = (SBT_1S / 2);
425 first += (lookahead / 2);
427 last &= (0xffffffffffffffffLLU << (32 - CC_HASH_SHIFT));
428 lastb = callout_hash(last) - 1;
432 * Check if we wrapped around the entire wheel from the last scan.
433 * In case, we need to scan entirely the wheel for pending callouts.
435 if (lastb - firstb >= callwheelsize) {
436 lastb = firstb + callwheelsize - 1;
437 if (nowb - firstb >= callwheelsize)
441 /* Iterate callwheel from firstb to nowb and then up to lastb. */
443 sc = &cc->cc_callwheel[firstb & callwheelmask];
444 tmp = LIST_FIRST(sc);
445 while (tmp != NULL) {
446 /* Run the callout if present time within allowed. */
447 if (tmp->c_time <= now) {
449 * Consumer told us the callout may be run
450 * directly from hardware interrupt context.
452 if (tmp->c_iflags & CALLOUT_DIRECT) {
453 #ifdef CALLOUT_PROFILING
457 LIST_NEXT(tmp, c_links.le);
458 cc->cc_bucket = firstb & callwheelmask;
459 LIST_REMOVE(tmp, c_links.le);
460 softclock_call_cc(tmp, cc,
461 #ifdef CALLOUT_PROFILING
462 &mpcalls_dir, &lockcalls_dir, NULL,
465 tmp = cc_exec_next(cc);
466 cc_exec_next(cc) = NULL;
468 tmpn = LIST_NEXT(tmp, c_links.le);
469 LIST_REMOVE(tmp, c_links.le);
470 TAILQ_INSERT_TAIL(&cc->cc_expireq,
472 tmp->c_iflags |= CALLOUT_PROCESSED;
477 /* Skip events from distant future. */
478 if (tmp->c_time >= max)
481 * Event minimal time is bigger than present maximal
482 * time, so it cannot be aggregated.
484 if (tmp->c_time > last) {
488 /* Update first and last time, respecting this event. */
489 if (tmp->c_time < first)
491 tmp_max = tmp->c_time + tmp->c_precision;
495 tmp = LIST_NEXT(tmp, c_links.le);
497 /* Proceed with the next bucket. */
500 * Stop if we looked after present time and found
501 * some event we can't execute at now.
502 * Stop if we looked far enough into the future.
504 } while (((int)(firstb - lastb)) <= 0);
505 cc->cc_firstevent = last;
506 #ifndef NO_EVENTTIMERS
507 cpu_new_callout(curcpu, last, first);
509 #ifdef CALLOUT_PROFILING
510 avg_depth_dir += (depth_dir * 1000 - avg_depth_dir) >> 8;
511 avg_mpcalls_dir += (mpcalls_dir * 1000 - avg_mpcalls_dir) >> 8;
512 avg_lockcalls_dir += (lockcalls_dir * 1000 - avg_lockcalls_dir) >> 8;
514 mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
516 * swi_sched acquires the thread lock, so we don't want to call it
517 * with cc_lock held; incorrect locking order.
519 if (!TAILQ_EMPTY(&cc->cc_expireq))
520 swi_sched(cc->cc_cookie, 0);
523 static struct callout_cpu *
524 callout_lock(struct callout *c)
526 struct callout_cpu *cc;
532 if (cpu == CPUBLOCK) {
533 while (c->c_cpu == CPUBLOCK)
548 callout_cc_add(struct callout *c, struct callout_cpu *cc,
549 sbintime_t sbt, sbintime_t precision, void (*func)(void *),
550 void *arg, int cpu, int flags)
555 if (sbt < cc->cc_lastscan)
556 sbt = cc->cc_lastscan;
558 c->c_iflags |= CALLOUT_PENDING;
559 c->c_iflags &= ~CALLOUT_PROCESSED;
560 c->c_flags |= CALLOUT_ACTIVE;
561 if (flags & C_DIRECT_EXEC)
562 c->c_iflags |= CALLOUT_DIRECT;
565 c->c_precision = precision;
566 bucket = callout_get_bucket(c->c_time);
567 CTR3(KTR_CALLOUT, "precision set for %p: %d.%08x",
568 c, (int)(c->c_precision >> 32),
569 (u_int)(c->c_precision & 0xffffffff));
570 LIST_INSERT_HEAD(&cc->cc_callwheel[bucket], c, c_links.le);
571 if (cc->cc_bucket == bucket)
572 cc_exec_next(cc) = c;
573 #ifndef NO_EVENTTIMERS
575 * Inform the eventtimers(4) subsystem there's a new callout
576 * that has been inserted, but only if really required.
578 if (SBT_MAX - c->c_time < c->c_precision)
579 c->c_precision = SBT_MAX - c->c_time;
580 sbt = c->c_time + c->c_precision;
581 if (sbt < cc->cc_firstevent) {
582 cc->cc_firstevent = sbt;
583 cpu_new_callout(cpu, sbt, c->c_time);
589 callout_cc_del(struct callout *c, struct callout_cpu *cc)
592 if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) == 0)
595 SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
599 softclock_call_cc(struct callout *c, struct callout_cpu *cc,
600 #ifdef CALLOUT_PROFILING
601 int *mpcalls, int *lockcalls, int *gcalls,
605 struct rm_priotracker tracker;
606 void (*c_func)(void *);
608 struct lock_class *class;
609 struct lock_object *c_lock;
610 uintptr_t lock_status;
613 struct callout_cpu *new_cc;
614 void (*new_func)(void *);
617 sbintime_t new_prec, new_time;
619 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
620 sbintime_t sbt1, sbt2;
622 static sbintime_t maxdt = 2 * SBT_1MS; /* 2 msec */
623 static timeout_t *lastfunc;
626 KASSERT((c->c_iflags & CALLOUT_PENDING) == CALLOUT_PENDING,
627 ("softclock_call_cc: pend %p %x", c, c->c_iflags));
628 KASSERT((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE,
629 ("softclock_call_cc: act %p %x", c, c->c_flags));
630 class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL;
632 if (c->c_flags & CALLOUT_SHAREDLOCK) {
633 if (class == &lock_class_rm)
634 lock_status = (uintptr_t)&tracker;
641 c_iflags = c->c_iflags;
642 if (c->c_iflags & CALLOUT_LOCAL_ALLOC)
643 c->c_iflags = CALLOUT_LOCAL_ALLOC;
645 c->c_iflags &= ~CALLOUT_PENDING;
647 cc_exec_curr(cc, direct) = c;
648 cc_exec_cancel(cc, direct) = false;
650 if (c_lock != NULL) {
651 class->lc_lock(c_lock, lock_status);
653 * The callout may have been cancelled
654 * while we switched locks.
656 if (cc_exec_cancel(cc, direct)) {
657 class->lc_unlock(c_lock);
660 /* The callout cannot be stopped now. */
661 cc_exec_cancel(cc, direct) = true;
662 if (c_lock == &Giant.lock_object) {
663 #ifdef CALLOUT_PROFILING
666 CTR3(KTR_CALLOUT, "callout giant %p func %p arg %p",
669 #ifdef CALLOUT_PROFILING
672 CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p",
676 #ifdef CALLOUT_PROFILING
679 CTR3(KTR_CALLOUT, "callout %p func %p arg %p",
682 KTR_STATE3(KTR_SCHED, "callout", cc->cc_ktr_event_name, "running",
683 "func:%p", c_func, "arg:%p", c_arg, "direct:%d", direct);
684 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
687 THREAD_NO_SLEEPING();
688 SDT_PROBE1(callout_execute, , , callout__start, c);
690 SDT_PROBE1(callout_execute, , , callout__end, c);
691 THREAD_SLEEPING_OK();
692 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
696 if (lastfunc != c_func || sbt2 > maxdt * 2) {
699 "Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
700 c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec);
706 KTR_STATE0(KTR_SCHED, "callout", cc->cc_ktr_event_name, "idle");
707 CTR1(KTR_CALLOUT, "callout %p finished", c);
708 if ((c_iflags & CALLOUT_RETURNUNLOCKED) == 0)
709 class->lc_unlock(c_lock);
712 KASSERT(cc_exec_curr(cc, direct) == c, ("mishandled cc_curr"));
713 cc_exec_curr(cc, direct) = NULL;
714 if (cc_exec_waiting(cc, direct)) {
716 * There is someone waiting for the
717 * callout to complete.
718 * If the callout was scheduled for
719 * migration just cancel it.
721 if (cc_cce_migrating(cc, direct)) {
722 cc_cce_cleanup(cc, direct);
725 * It should be assert here that the callout is not
726 * destroyed but that is not easy.
728 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
730 cc_exec_waiting(cc, direct) = false;
732 wakeup(&cc_exec_waiting(cc, direct));
734 } else if (cc_cce_migrating(cc, direct)) {
735 KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0,
736 ("Migrating legacy callout %p", c));
739 * If the callout was scheduled for
740 * migration just perform it now.
742 new_cpu = cc_migration_cpu(cc, direct);
743 new_time = cc_migration_time(cc, direct);
744 new_prec = cc_migration_prec(cc, direct);
745 new_func = cc_migration_func(cc, direct);
746 new_arg = cc_migration_arg(cc, direct);
747 cc_cce_cleanup(cc, direct);
750 * It should be assert here that the callout is not destroyed
751 * but that is not easy.
753 * As first thing, handle deferred callout stops.
755 if (!callout_migrating(c)) {
757 "deferred cancelled %p func %p arg %p",
758 c, new_func, new_arg);
759 callout_cc_del(c, cc);
762 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
764 new_cc = callout_cpu_switch(c, cc, new_cpu);
765 flags = (direct) ? C_DIRECT_EXEC : 0;
766 callout_cc_add(c, new_cc, new_time, new_prec, new_func,
767 new_arg, new_cpu, flags);
771 panic("migration should not happen");
775 * If the current callout is locally allocated (from
776 * timeout(9)) then put it on the freelist.
778 * Note: we need to check the cached copy of c_iflags because
779 * if it was not local, then it's not safe to deref the
782 KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0 ||
783 c->c_iflags == CALLOUT_LOCAL_ALLOC,
784 ("corrupted callout"));
785 if (c_iflags & CALLOUT_LOCAL_ALLOC)
786 callout_cc_del(c, cc);
790 * The callout mechanism is based on the work of Adam M. Costello and
791 * George Varghese, published in a technical report entitled "Redesigning
792 * the BSD Callout and Timer Facilities" and modified slightly for inclusion
793 * in FreeBSD by Justin T. Gibbs. The original work on the data structures
794 * used in this implementation was published by G. Varghese and T. Lauck in
795 * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
796 * the Efficient Implementation of a Timer Facility" in the Proceedings of
797 * the 11th ACM Annual Symposium on Operating Systems Principles,
798 * Austin, Texas Nov 1987.
802 * Software (low priority) clock interrupt.
803 * Run periodic events from timeout queue.
808 struct callout_cpu *cc;
810 #ifdef CALLOUT_PROFILING
811 int depth = 0, gcalls = 0, lockcalls = 0, mpcalls = 0;
814 cc = (struct callout_cpu *)arg;
816 while ((c = TAILQ_FIRST(&cc->cc_expireq)) != NULL) {
817 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
818 softclock_call_cc(c, cc,
819 #ifdef CALLOUT_PROFILING
820 &mpcalls, &lockcalls, &gcalls,
823 #ifdef CALLOUT_PROFILING
827 #ifdef CALLOUT_PROFILING
828 avg_depth += (depth * 1000 - avg_depth) >> 8;
829 avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
830 avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
831 avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
838 * Execute a function after a specified length of time.
841 * Cancel previous timeout function call.
843 * callout_handle_init --
844 * Initialize a handle so that using it with untimeout is benign.
846 * See AT&T BCI Driver Reference Manual for specification. This
847 * implementation differs from that one in that although an
848 * identification value is returned from timeout, the original
849 * arguments to timeout as well as the identifier are used to
850 * identify entries for untimeout.
852 struct callout_handle
853 timeout(ftn, arg, to_ticks)
858 struct callout_cpu *cc;
860 struct callout_handle handle;
862 cc = CC_CPU(timeout_cpu);
864 /* Fill in the next free callout structure. */
865 new = SLIST_FIRST(&cc->cc_callfree);
867 /* XXX Attempt to malloc first */
868 panic("timeout table full");
869 SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle);
870 callout_reset(new, to_ticks, ftn, arg);
871 handle.callout = new;
878 untimeout(ftn, arg, handle)
881 struct callout_handle handle;
883 struct callout_cpu *cc;
886 * Check for a handle that was initialized
887 * by callout_handle_init, but never used
888 * for a real timeout.
890 if (handle.callout == NULL)
893 cc = callout_lock(handle.callout);
894 if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
895 callout_stop(handle.callout);
900 callout_handle_init(struct callout_handle *handle)
902 handle->callout = NULL;
906 callout_when(sbintime_t sbt, sbintime_t precision, int flags,
907 sbintime_t *res, sbintime_t *prec_res)
909 sbintime_t to_sbt, to_pr;
911 if ((flags & (C_ABSOLUTE | C_PRECALC)) != 0) {
913 *prec_res = precision;
916 if ((flags & C_HARDCLOCK) != 0 && sbt < tick_sbt)
918 if ((flags & C_HARDCLOCK) != 0 ||
919 #ifdef NO_EVENTTIMERS
920 sbt >= sbt_timethreshold) {
921 to_sbt = getsbinuptime();
923 /* Add safety belt for the case of hz > 1000. */
924 to_sbt += tc_tick_sbt - tick_sbt;
926 sbt >= sbt_tickthreshold) {
928 * Obtain the time of the last hardclock() call on
929 * this CPU directly from the kern_clocksource.c.
930 * This value is per-CPU, but it is equal for all
934 to_sbt = DPCPU_GET(hardclocktime);
937 to_sbt = DPCPU_GET(hardclocktime);
941 if ((flags & C_HARDCLOCK) == 0)
944 to_sbt = sbinuptime();
945 if (SBT_MAX - to_sbt < sbt)
950 to_pr = ((C_PRELGET(flags) < 0) ? sbt >> tc_precexp :
951 sbt >> C_PRELGET(flags));
952 *prec_res = to_pr > precision ? to_pr : precision;
956 * New interface; clients allocate their own callout structures.
958 * callout_reset() - establish or change a timeout
959 * callout_stop() - disestablish a timeout
960 * callout_init() - initialize a callout structure so that it can
961 * safely be passed to callout_reset() and callout_stop()
963 * <sys/callout.h> defines three convenience macros:
965 * callout_active() - returns truth if callout has not been stopped,
966 * drained, or deactivated since the last time the callout was
968 * callout_pending() - returns truth if callout is still waiting for timeout
969 * callout_deactivate() - marks the callout as having been serviced
972 callout_reset_sbt_on(struct callout *c, sbintime_t sbt, sbintime_t prec,
973 void (*ftn)(void *), void *arg, int cpu, int flags)
975 sbintime_t to_sbt, precision;
976 struct callout_cpu *cc;
977 int cancelled, direct;
983 } else if ((cpu >= MAXCPU) ||
984 ((CC_CPU(cpu))->cc_inited == 0)) {
985 /* Invalid CPU spec */
986 panic("Invalid CPU in callout %d", cpu);
988 callout_when(sbt, prec, flags, &to_sbt, &precision);
991 * This flag used to be added by callout_cc_add, but the
992 * first time you call this we could end up with the
993 * wrong direct flag if we don't do it before we add.
995 if (flags & C_DIRECT_EXEC) {
1000 KASSERT(!direct || c->c_lock == NULL,
1001 ("%s: direct callout %p has lock", __func__, c));
1002 cc = callout_lock(c);
1004 * Don't allow migration of pre-allocated callouts lest they
1005 * become unbalanced or handle the case where the user does
1008 if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) ||
1013 if (cc_exec_curr(cc, direct) == c) {
1015 * We're being asked to reschedule a callout which is
1016 * currently in progress. If there is a lock then we
1017 * can cancel the callout if it has not really started.
1019 if (c->c_lock != NULL && !cc_exec_cancel(cc, direct))
1020 cancelled = cc_exec_cancel(cc, direct) = true;
1021 if (cc_exec_waiting(cc, direct)) {
1023 * Someone has called callout_drain to kill this
1024 * callout. Don't reschedule.
1026 CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
1027 cancelled ? "cancelled" : "failed to cancel",
1028 c, c->c_func, c->c_arg);
1033 if (callout_migrating(c)) {
1035 * This only occurs when a second callout_reset_sbt_on
1036 * is made after a previous one moved it into
1037 * deferred migration (below). Note we do *not* change
1038 * the prev_cpu even though the previous target may
1041 cc_migration_cpu(cc, direct) = cpu;
1042 cc_migration_time(cc, direct) = to_sbt;
1043 cc_migration_prec(cc, direct) = precision;
1044 cc_migration_func(cc, direct) = ftn;
1045 cc_migration_arg(cc, direct) = arg;
1052 if (c->c_iflags & CALLOUT_PENDING) {
1053 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1054 if (cc_exec_next(cc) == c)
1055 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1056 LIST_REMOVE(c, c_links.le);
1058 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1061 c->c_iflags &= ~ CALLOUT_PENDING;
1062 c->c_flags &= ~ CALLOUT_ACTIVE;
1067 * If the callout must migrate try to perform it immediately.
1068 * If the callout is currently running, just defer the migration
1069 * to a more appropriate moment.
1071 if (c->c_cpu != cpu) {
1072 if (cc_exec_curr(cc, direct) == c) {
1074 * Pending will have been removed since we are
1075 * actually executing the callout on another
1076 * CPU. That callout should be waiting on the
1077 * lock the caller holds. If we set both
1078 * active/and/pending after we return and the
1079 * lock on the executing callout proceeds, it
1080 * will then see pending is true and return.
1081 * At the return from the actual callout execution
1082 * the migration will occur in softclock_call_cc
1083 * and this new callout will be placed on the
1084 * new CPU via a call to callout_cpu_switch() which
1085 * will get the lock on the right CPU followed
1086 * by a call callout_cc_add() which will add it there.
1087 * (see above in softclock_call_cc()).
1089 cc_migration_cpu(cc, direct) = cpu;
1090 cc_migration_time(cc, direct) = to_sbt;
1091 cc_migration_prec(cc, direct) = precision;
1092 cc_migration_func(cc, direct) = ftn;
1093 cc_migration_arg(cc, direct) = arg;
1094 c->c_iflags |= (CALLOUT_DFRMIGRATION | CALLOUT_PENDING);
1095 c->c_flags |= CALLOUT_ACTIVE;
1097 "migration of %p func %p arg %p in %d.%08x to %u deferred",
1098 c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1099 (u_int)(to_sbt & 0xffffffff), cpu);
1103 cc = callout_cpu_switch(c, cc, cpu);
1107 callout_cc_add(c, cc, to_sbt, precision, ftn, arg, cpu, flags);
1108 CTR6(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d.%08x",
1109 cancelled ? "re" : "", c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1110 (u_int)(to_sbt & 0xffffffff));
1117 * Common idioms that can be optimized in the future.
1120 callout_schedule_on(struct callout *c, int to_ticks, int cpu)
1122 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
1126 callout_schedule(struct callout *c, int to_ticks)
1128 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
1132 _callout_stop_safe(c, flags)
1136 struct callout_cpu *cc, *old_cc;
1137 struct lock_class *class;
1138 int direct, sq_locked, use_lock;
1142 * Some old subsystems don't hold Giant while running a callout_stop(),
1143 * so just discard this check for the moment.
1145 if ((flags & CS_DRAIN) == 0 && c->c_lock != NULL) {
1146 if (c->c_lock == &Giant.lock_object)
1147 use_lock = mtx_owned(&Giant);
1150 class = LOCK_CLASS(c->c_lock);
1151 class->lc_assert(c->c_lock, LA_XLOCKED);
1155 if (c->c_iflags & CALLOUT_DIRECT) {
1163 cc = callout_lock(c);
1165 if ((c->c_iflags & (CALLOUT_DFRMIGRATION | CALLOUT_PENDING)) ==
1166 (CALLOUT_DFRMIGRATION | CALLOUT_PENDING) &&
1167 ((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE)) {
1169 * Special case where this slipped in while we
1170 * were migrating *as* the callout is about to
1171 * execute. The caller probably holds the lock
1172 * the callout wants.
1174 * Get rid of the migration first. Then set
1175 * the flag that tells this code *not* to
1176 * try to remove it from any lists (its not
1177 * on one yet). When the callout wheel runs,
1178 * it will ignore this callout.
1180 c->c_iflags &= ~CALLOUT_PENDING;
1181 c->c_flags &= ~CALLOUT_ACTIVE;
1188 * If the callout was migrating while the callout cpu lock was
1189 * dropped, just drop the sleepqueue lock and check the states
1192 if (sq_locked != 0 && cc != old_cc) {
1195 sleepq_release(&cc_exec_waiting(old_cc, direct));
1200 panic("migration should not happen");
1205 * If the callout isn't pending, it's not on the queue, so
1206 * don't attempt to remove it from the queue. We can try to
1207 * stop it by other means however.
1209 if (!(c->c_iflags & CALLOUT_PENDING)) {
1210 c->c_flags &= ~CALLOUT_ACTIVE;
1213 * If it wasn't on the queue and it isn't the current
1214 * callout, then we can't stop it, so just bail.
1216 if (cc_exec_curr(cc, direct) != c) {
1217 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1218 c, c->c_func, c->c_arg);
1221 sleepq_release(&cc_exec_waiting(cc, direct));
1225 if ((flags & CS_DRAIN) != 0) {
1227 * The current callout is running (or just
1228 * about to run) and blocking is allowed, so
1229 * just wait for the current invocation to
1232 while (cc_exec_curr(cc, direct) == c) {
1234 * Use direct calls to sleepqueue interface
1235 * instead of cv/msleep in order to avoid
1236 * a LOR between cc_lock and sleepqueue
1237 * chain spinlocks. This piece of code
1238 * emulates a msleep_spin() call actually.
1240 * If we already have the sleepqueue chain
1241 * locked, then we can safely block. If we
1242 * don't already have it locked, however,
1243 * we have to drop the cc_lock to lock
1244 * it. This opens several races, so we
1245 * restart at the beginning once we have
1246 * both locks. If nothing has changed, then
1247 * we will end up back here with sq_locked
1253 &cc_exec_waiting(cc, direct));
1260 * Migration could be cancelled here, but
1261 * as long as it is still not sure when it
1262 * will be packed up, just let softclock()
1265 cc_exec_waiting(cc, direct) = true;
1269 &cc_exec_waiting(cc, direct),
1270 &cc->cc_lock.lock_object, "codrain",
1273 &cc_exec_waiting(cc, direct),
1278 /* Reacquire locks previously released. */
1282 } else if (use_lock &&
1283 !cc_exec_cancel(cc, direct)) {
1286 * The current callout is waiting for its
1287 * lock which we hold. Cancel the callout
1288 * and return. After our caller drops the
1289 * lock, the callout will be skipped in
1292 cc_exec_cancel(cc, direct) = true;
1293 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1294 c, c->c_func, c->c_arg);
1295 KASSERT(!cc_cce_migrating(cc, direct),
1296 ("callout wrongly scheduled for migration"));
1297 if (callout_migrating(c)) {
1298 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1300 cc_migration_cpu(cc, direct) = CPUBLOCK;
1301 cc_migration_time(cc, direct) = 0;
1302 cc_migration_prec(cc, direct) = 0;
1303 cc_migration_func(cc, direct) = NULL;
1304 cc_migration_arg(cc, direct) = NULL;
1308 KASSERT(!sq_locked, ("sleepqueue chain locked"));
1310 } else if (callout_migrating(c)) {
1312 * The callout is currently being serviced
1313 * and the "next" callout is scheduled at
1314 * its completion with a migration. We remove
1315 * the migration flag so it *won't* get rescheduled,
1316 * but we can't stop the one thats running so
1319 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1322 * We can't call cc_cce_cleanup here since
1323 * if we do it will remove .ce_curr and
1324 * its still running. This will prevent a
1325 * reschedule of the callout when the
1326 * execution completes.
1328 cc_migration_cpu(cc, direct) = CPUBLOCK;
1329 cc_migration_time(cc, direct) = 0;
1330 cc_migration_prec(cc, direct) = 0;
1331 cc_migration_func(cc, direct) = NULL;
1332 cc_migration_arg(cc, direct) = NULL;
1334 CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p",
1335 c, c->c_func, c->c_arg);
1337 return ((flags & CS_MIGRBLOCK) != 0);
1339 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1340 c, c->c_func, c->c_arg);
1342 KASSERT(!sq_locked, ("sleepqueue chain still locked"));
1346 sleepq_release(&cc_exec_waiting(cc, direct));
1348 c->c_iflags &= ~CALLOUT_PENDING;
1349 c->c_flags &= ~CALLOUT_ACTIVE;
1351 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1352 c, c->c_func, c->c_arg);
1353 if (not_on_a_list == 0) {
1354 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1355 if (cc_exec_next(cc) == c)
1356 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1357 LIST_REMOVE(c, c_links.le);
1359 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1362 callout_cc_del(c, cc);
1368 callout_init(c, mpsafe)
1372 bzero(c, sizeof *c);
1375 c->c_iflags = CALLOUT_RETURNUNLOCKED;
1377 c->c_lock = &Giant.lock_object;
1380 c->c_cpu = timeout_cpu;
1384 _callout_init_lock(c, lock, flags)
1386 struct lock_object *lock;
1389 bzero(c, sizeof *c);
1391 KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
1392 ("callout_init_lock: bad flags %d", flags));
1393 KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
1394 ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
1395 KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
1396 (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
1398 c->c_iflags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
1399 c->c_cpu = timeout_cpu;
1402 #ifdef APM_FIXUP_CALLTODO
1404 * Adjust the kernel calltodo timeout list. This routine is used after
1405 * an APM resume to recalculate the calltodo timer list values with the
1406 * number of hz's we have been sleeping. The next hardclock() will detect
1407 * that there are fired timers and run softclock() to execute them.
1409 * Please note, I have not done an exhaustive analysis of what code this
1410 * might break. I am motivated to have my select()'s and alarm()'s that
1411 * have expired during suspend firing upon resume so that the applications
1412 * which set the timer can do the maintanence the timer was for as close
1413 * as possible to the originally intended time. Testing this code for a
1414 * week showed that resuming from a suspend resulted in 22 to 25 timers
1415 * firing, which seemed independent on whether the suspend was 2 hours or
1416 * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu>
1419 adjust_timeout_calltodo(time_change)
1420 struct timeval *time_change;
1422 register struct callout *p;
1423 unsigned long delta_ticks;
1426 * How many ticks were we asleep?
1427 * (stolen from tvtohz()).
1430 /* Don't do anything */
1431 if (time_change->tv_sec < 0)
1433 else if (time_change->tv_sec <= LONG_MAX / 1000000)
1434 delta_ticks = (time_change->tv_sec * 1000000 +
1435 time_change->tv_usec + (tick - 1)) / tick + 1;
1436 else if (time_change->tv_sec <= LONG_MAX / hz)
1437 delta_ticks = time_change->tv_sec * hz +
1438 (time_change->tv_usec + (tick - 1)) / tick + 1;
1440 delta_ticks = LONG_MAX;
1442 if (delta_ticks > INT_MAX)
1443 delta_ticks = INT_MAX;
1446 * Now rip through the timer calltodo list looking for timers
1450 /* don't collide with softclock() */
1452 for (p = calltodo.c_next; p != NULL; p = p->c_next) {
1453 p->c_time -= delta_ticks;
1455 /* Break if the timer had more time on it than delta_ticks */
1459 /* take back the ticks the timer didn't use (p->c_time <= 0) */
1460 delta_ticks = -p->c_time;
1466 #endif /* APM_FIXUP_CALLTODO */
1469 flssbt(sbintime_t sbt)
1472 sbt += (uint64_t)sbt >> 1;
1473 if (sizeof(long) >= sizeof(sbintime_t))
1476 return (flsl(((uint64_t)sbt) >> 32) + 32);
1481 * Dump immediate statistic snapshot of the scheduled callouts.
1484 sysctl_kern_callout_stat(SYSCTL_HANDLER_ARGS)
1486 struct callout *tmp;
1487 struct callout_cpu *cc;
1488 struct callout_list *sc;
1489 sbintime_t maxpr, maxt, medpr, medt, now, spr, st, t;
1490 int ct[64], cpr[64], ccpbk[32];
1491 int error, val, i, count, tcum, pcum, maxc, c, medc;
1497 error = sysctl_handle_int(oidp, &val, 0, req);
1498 if (error != 0 || req->newptr == NULL)
1501 st = spr = maxt = maxpr = 0;
1502 bzero(ccpbk, sizeof(ccpbk));
1503 bzero(ct, sizeof(ct));
1504 bzero(cpr, sizeof(cpr));
1510 cc = CC_CPU(timeout_cpu);
1513 for (i = 0; i < callwheelsize; i++) {
1514 sc = &cc->cc_callwheel[i];
1516 LIST_FOREACH(tmp, sc, c_links.le) {
1518 t = tmp->c_time - now;
1522 spr += tmp->c_precision / SBT_1US;
1525 if (tmp->c_precision > maxpr)
1526 maxpr = tmp->c_precision;
1528 cpr[flssbt(tmp->c_precision)]++;
1532 ccpbk[fls(c + c / 2)]++;
1540 for (i = 0, tcum = 0; i < 64 && tcum < count / 2; i++)
1542 medt = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1543 for (i = 0, pcum = 0; i < 64 && pcum < count / 2; i++)
1545 medpr = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1546 for (i = 0, c = 0; i < 32 && c < count / 2; i++)
1548 medc = (i >= 2) ? (1 << (i - 2)) : 0;
1550 printf("Scheduled callouts statistic snapshot:\n");
1551 printf(" Callouts: %6d Buckets: %6d*%-3d Bucket size: 0.%06ds\n",
1552 count, callwheelsize, mp_ncpus, 1000000 >> CC_HASH_SHIFT);
1553 printf(" C/Bk: med %5d avg %6d.%06jd max %6d\n",
1555 count / callwheelsize / mp_ncpus,
1556 (uint64_t)count * 1000000 / callwheelsize / mp_ncpus % 1000000,
1558 printf(" Time: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1559 medt / SBT_1S, (medt & 0xffffffff) * 1000000 >> 32,
1560 (st / count) / 1000000, (st / count) % 1000000,
1561 maxt / SBT_1S, (maxt & 0xffffffff) * 1000000 >> 32);
1562 printf(" Prec: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1563 medpr / SBT_1S, (medpr & 0xffffffff) * 1000000 >> 32,
1564 (spr / count) / 1000000, (spr / count) % 1000000,
1565 maxpr / SBT_1S, (maxpr & 0xffffffff) * 1000000 >> 32);
1566 printf(" Distribution: \tbuckets\t time\t tcum\t"
1568 for (i = 0, tcum = pcum = 0; i < 64; i++) {
1569 if (ct[i] == 0 && cpr[i] == 0)
1571 t = (i != 0) ? (((sbintime_t)1) << (i - 1)) : 0;
1574 printf(" %10jd.%06jds\t 2**%d\t%7d\t%7d\t%7d\t%7d\n",
1575 t / SBT_1S, (t & 0xffffffff) * 1000000 >> 32,
1576 i - 1 - (32 - CC_HASH_SHIFT),
1577 ct[i], tcum, cpr[i], pcum);
1581 SYSCTL_PROC(_kern, OID_AUTO, callout_stat,
1582 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1583 0, 0, sysctl_kern_callout_stat, "I",
1584 "Dump immediate statistic snapshot of the scheduled callouts");
1588 _show_callout(struct callout *c)
1591 db_printf("callout %p\n", c);
1592 #define C_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, c->e);
1593 db_printf(" &c_links = %p\n", &(c->c_links));
1594 C_DB_PRINTF("%" PRId64, c_time);
1595 C_DB_PRINTF("%" PRId64, c_precision);
1596 C_DB_PRINTF("%p", c_arg);
1597 C_DB_PRINTF("%p", c_func);
1598 C_DB_PRINTF("%p", c_lock);
1599 C_DB_PRINTF("%#x", c_flags);
1600 C_DB_PRINTF("%#x", c_iflags);
1601 C_DB_PRINTF("%d", c_cpu);
1605 DB_SHOW_COMMAND(callout, db_show_callout)
1609 db_printf("usage: show callout <struct callout *>\n");
1613 _show_callout((struct callout *)addr);