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"
43 #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 | CTLFLAG_NOFETCH, &ncallout, 0,
110 "Number of entries in callwheel and size of timeout() preallocation");
113 static int pin_default_swi = 1;
114 static int pin_pcpu_swi = 1;
116 static int pin_default_swi = 0;
117 static int pin_pcpu_swi = 0;
120 SYSCTL_INT(_kern, OID_AUTO, pin_default_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_default_swi,
121 0, "Pin the default (non-per-cpu) swi (shared with PCPU 0 swi)");
122 SYSCTL_INT(_kern, OID_AUTO, pin_pcpu_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_pcpu_swi,
123 0, "Pin the per-CPU swis (except PCPU 0, which is also default");
127 * allocate more timeout table slots when table overflows.
129 u_int callwheelsize, callwheelmask;
132 * The callout cpu exec entities represent informations necessary for
133 * describing the state of callouts currently running on the CPU and the ones
134 * necessary for migrating callouts to the new callout cpu. In particular,
135 * the first entry of the array cc_exec_entity holds informations for callout
136 * running in SWI thread context, while the second one holds informations
137 * for callout running directly from hardware interrupt context.
138 * The cached informations are very important for deferring migration when
139 * the migrating callout is already running.
142 struct callout *cc_curr;
143 void (*cc_drain)(void *);
145 void (*ce_migration_func)(void *);
146 void *ce_migration_arg;
147 int ce_migration_cpu;
148 sbintime_t ce_migration_time;
149 sbintime_t ce_migration_prec;
156 * There is one struct callout_cpu per cpu, holding all relevant
157 * state for the callout processing thread on the individual CPU.
160 struct mtx_padalign cc_lock;
161 struct cc_exec cc_exec_entity[2];
162 struct callout *cc_next;
163 struct callout *cc_callout;
164 struct callout_list *cc_callwheel;
165 struct callout_tailq cc_expireq;
166 struct callout_slist cc_callfree;
167 sbintime_t cc_firstevent;
168 sbintime_t cc_lastscan;
172 char cc_ktr_event_name[20];
175 #define callout_migrating(c) ((c)->c_iflags & CALLOUT_DFRMIGRATION)
177 #define cc_exec_curr(cc, dir) cc->cc_exec_entity[dir].cc_curr
178 #define cc_exec_drain(cc, dir) cc->cc_exec_entity[dir].cc_drain
179 #define cc_exec_next(cc) cc->cc_next
180 #define cc_exec_cancel(cc, dir) cc->cc_exec_entity[dir].cc_cancel
181 #define cc_exec_waiting(cc, dir) cc->cc_exec_entity[dir].cc_waiting
183 #define cc_migration_func(cc, dir) cc->cc_exec_entity[dir].ce_migration_func
184 #define cc_migration_arg(cc, dir) cc->cc_exec_entity[dir].ce_migration_arg
185 #define cc_migration_cpu(cc, dir) cc->cc_exec_entity[dir].ce_migration_cpu
186 #define cc_migration_time(cc, dir) cc->cc_exec_entity[dir].ce_migration_time
187 #define cc_migration_prec(cc, dir) cc->cc_exec_entity[dir].ce_migration_prec
189 struct callout_cpu cc_cpu[MAXCPU];
190 #define CPUBLOCK MAXCPU
191 #define CC_CPU(cpu) (&cc_cpu[(cpu)])
192 #define CC_SELF() CC_CPU(PCPU_GET(cpuid))
194 struct callout_cpu cc_cpu;
195 #define CC_CPU(cpu) &cc_cpu
196 #define CC_SELF() &cc_cpu
198 #define CC_LOCK(cc) mtx_lock_spin(&(cc)->cc_lock)
199 #define CC_UNLOCK(cc) mtx_unlock_spin(&(cc)->cc_lock)
200 #define CC_LOCK_ASSERT(cc) mtx_assert(&(cc)->cc_lock, MA_OWNED)
202 static int timeout_cpu;
204 static void callout_cpu_init(struct callout_cpu *cc, int cpu);
205 static void softclock_call_cc(struct callout *c, struct callout_cpu *cc,
206 #ifdef CALLOUT_PROFILING
207 int *mpcalls, int *lockcalls, int *gcalls,
211 static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
215 * cc_curr - If a callout is in progress, it is cc_curr.
216 * If cc_curr is non-NULL, threads waiting in
217 * callout_drain() will be woken up as soon as the
218 * relevant callout completes.
219 * cc_cancel - Changing to 1 with both callout_lock and cc_lock held
220 * guarantees that the current callout will not run.
221 * The softclock() function sets this to 0 before it
222 * drops callout_lock to acquire c_lock, and it calls
223 * the handler only if curr_cancelled is still 0 after
224 * cc_lock is successfully acquired.
225 * cc_waiting - If a thread is waiting in callout_drain(), then
226 * callout_wait is nonzero. Set only when
227 * cc_curr is non-NULL.
231 * Resets the execution entity tied to a specific callout cpu.
234 cc_cce_cleanup(struct callout_cpu *cc, int direct)
237 cc_exec_curr(cc, direct) = NULL;
238 cc_exec_cancel(cc, direct) = false;
239 cc_exec_waiting(cc, direct) = false;
241 cc_migration_cpu(cc, direct) = CPUBLOCK;
242 cc_migration_time(cc, direct) = 0;
243 cc_migration_prec(cc, direct) = 0;
244 cc_migration_func(cc, direct) = NULL;
245 cc_migration_arg(cc, direct) = NULL;
250 * Checks if migration is requested by a specific callout cpu.
253 cc_cce_migrating(struct callout_cpu *cc, int direct)
257 return (cc_migration_cpu(cc, direct) != CPUBLOCK);
264 * Kernel low level callwheel initialization
265 * called on cpu0 during kernel startup.
268 callout_callwheel_init(void *dummy)
270 struct callout_cpu *cc;
273 * Calculate the size of the callout wheel and the preallocated
274 * timeout() structures.
275 * XXX: Clip callout to result of previous function of maxusers
276 * maximum 384. This is still huge, but acceptable.
278 memset(CC_CPU(0), 0, sizeof(cc_cpu));
279 ncallout = imin(16 + maxproc + maxfiles, 18508);
280 TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
283 * Calculate callout wheel size, should be next power of two higher
286 callwheelsize = 1 << fls(ncallout);
287 callwheelmask = callwheelsize - 1;
290 * Fetch whether we're pinning the swi's or not.
292 TUNABLE_INT_FETCH("kern.pin_default_swi", &pin_default_swi);
293 TUNABLE_INT_FETCH("kern.pin_pcpu_swi", &pin_pcpu_swi);
296 * Only cpu0 handles timeout(9) and receives a preallocation.
298 * XXX: Once all timeout(9) consumers are converted this can
301 timeout_cpu = PCPU_GET(cpuid);
302 cc = CC_CPU(timeout_cpu);
303 cc->cc_callout = malloc(ncallout * sizeof(struct callout),
304 M_CALLOUT, M_WAITOK);
305 callout_cpu_init(cc, timeout_cpu);
307 SYSINIT(callwheel_init, SI_SUB_CPU, SI_ORDER_ANY, callout_callwheel_init, NULL);
310 * Initialize the per-cpu callout structures.
313 callout_cpu_init(struct callout_cpu *cc, int cpu)
318 mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
319 SLIST_INIT(&cc->cc_callfree);
321 cc->cc_callwheel = malloc(sizeof(struct callout_list) * callwheelsize,
322 M_CALLOUT, M_WAITOK);
323 for (i = 0; i < callwheelsize; i++)
324 LIST_INIT(&cc->cc_callwheel[i]);
325 TAILQ_INIT(&cc->cc_expireq);
326 cc->cc_firstevent = SBT_MAX;
327 for (i = 0; i < 2; i++)
328 cc_cce_cleanup(cc, i);
329 snprintf(cc->cc_ktr_event_name, sizeof(cc->cc_ktr_event_name),
330 "callwheel cpu %d", cpu);
331 if (cc->cc_callout == NULL) /* Only cpu0 handles timeout(9) */
333 for (i = 0; i < ncallout; i++) {
334 c = &cc->cc_callout[i];
336 c->c_iflags = CALLOUT_LOCAL_ALLOC;
337 SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
343 * Switches the cpu tied to a specific callout.
344 * The function expects a locked incoming callout cpu and returns with
345 * locked outcoming callout cpu.
347 static struct callout_cpu *
348 callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu)
350 struct callout_cpu *new_cc;
352 MPASS(c != NULL && cc != NULL);
356 * Avoid interrupts and preemption firing after the callout cpu
357 * is blocked in order to avoid deadlocks as the new thread
358 * may be willing to acquire the callout cpu lock.
363 new_cc = CC_CPU(new_cpu);
372 * Start standard softclock thread.
375 start_softclock(void *dummy)
377 struct callout_cpu *cc;
378 char name[MAXCOMLEN];
381 struct intr_event *ie;
384 cc = CC_CPU(timeout_cpu);
385 snprintf(name, sizeof(name), "clock (%d)", timeout_cpu);
386 if (swi_add(&clk_intr_event, name, softclock, cc, SWI_CLOCK,
387 INTR_MPSAFE, &cc->cc_cookie))
388 panic("died while creating standard software ithreads");
389 if (pin_default_swi &&
390 (intr_event_bind(clk_intr_event, timeout_cpu) != 0)) {
391 printf("%s: timeout clock couldn't be pinned to cpu %d\n",
398 if (cpu == timeout_cpu)
401 cc->cc_callout = NULL; /* Only cpu0 handles timeout(9). */
402 callout_cpu_init(cc, cpu);
403 snprintf(name, sizeof(name), "clock (%d)", cpu);
405 if (swi_add(&ie, name, softclock, cc, SWI_CLOCK,
406 INTR_MPSAFE, &cc->cc_cookie))
407 panic("died while creating standard software ithreads");
408 if (pin_pcpu_swi && (intr_event_bind(ie, cpu) != 0)) {
409 printf("%s: per-cpu clock couldn't be pinned to "
417 SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
419 #define CC_HASH_SHIFT 8
422 callout_hash(sbintime_t sbt)
425 return (sbt >> (32 - CC_HASH_SHIFT));
429 callout_get_bucket(sbintime_t sbt)
432 return (callout_hash(sbt) & callwheelmask);
436 callout_process(sbintime_t now)
438 struct callout *tmp, *tmpn;
439 struct callout_cpu *cc;
440 struct callout_list *sc;
441 sbintime_t first, last, max, tmp_max;
443 u_int firstb, lastb, nowb;
444 #ifdef CALLOUT_PROFILING
445 int depth_dir = 0, mpcalls_dir = 0, lockcalls_dir = 0;
449 mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
451 /* Compute the buckets of the last scan and present times. */
452 firstb = callout_hash(cc->cc_lastscan);
453 cc->cc_lastscan = now;
454 nowb = callout_hash(now);
456 /* Compute the last bucket and minimum time of the bucket after it. */
458 lookahead = (SBT_1S / 16);
459 else if (nowb - firstb == 1)
460 lookahead = (SBT_1S / 8);
462 lookahead = (SBT_1S / 2);
464 first += (lookahead / 2);
466 last &= (0xffffffffffffffffLLU << (32 - CC_HASH_SHIFT));
467 lastb = callout_hash(last) - 1;
471 * Check if we wrapped around the entire wheel from the last scan.
472 * In case, we need to scan entirely the wheel for pending callouts.
474 if (lastb - firstb >= callwheelsize) {
475 lastb = firstb + callwheelsize - 1;
476 if (nowb - firstb >= callwheelsize)
480 /* Iterate callwheel from firstb to nowb and then up to lastb. */
482 sc = &cc->cc_callwheel[firstb & callwheelmask];
483 tmp = LIST_FIRST(sc);
484 while (tmp != NULL) {
485 /* Run the callout if present time within allowed. */
486 if (tmp->c_time <= now) {
488 * Consumer told us the callout may be run
489 * directly from hardware interrupt context.
491 if (tmp->c_iflags & CALLOUT_DIRECT) {
492 #ifdef CALLOUT_PROFILING
496 LIST_NEXT(tmp, c_links.le);
497 cc->cc_bucket = firstb & callwheelmask;
498 LIST_REMOVE(tmp, c_links.le);
499 softclock_call_cc(tmp, cc,
500 #ifdef CALLOUT_PROFILING
501 &mpcalls_dir, &lockcalls_dir, NULL,
504 tmp = cc_exec_next(cc);
505 cc_exec_next(cc) = NULL;
507 tmpn = LIST_NEXT(tmp, c_links.le);
508 LIST_REMOVE(tmp, c_links.le);
509 TAILQ_INSERT_TAIL(&cc->cc_expireq,
511 tmp->c_iflags |= CALLOUT_PROCESSED;
516 /* Skip events from distant future. */
517 if (tmp->c_time >= max)
520 * Event minimal time is bigger than present maximal
521 * time, so it cannot be aggregated.
523 if (tmp->c_time > last) {
527 /* Update first and last time, respecting this event. */
528 if (tmp->c_time < first)
530 tmp_max = tmp->c_time + tmp->c_precision;
534 tmp = LIST_NEXT(tmp, c_links.le);
536 /* Proceed with the next bucket. */
539 * Stop if we looked after present time and found
540 * some event we can't execute at now.
541 * Stop if we looked far enough into the future.
543 } while (((int)(firstb - lastb)) <= 0);
544 cc->cc_firstevent = last;
545 #ifndef NO_EVENTTIMERS
546 cpu_new_callout(curcpu, last, first);
548 #ifdef CALLOUT_PROFILING
549 avg_depth_dir += (depth_dir * 1000 - avg_depth_dir) >> 8;
550 avg_mpcalls_dir += (mpcalls_dir * 1000 - avg_mpcalls_dir) >> 8;
551 avg_lockcalls_dir += (lockcalls_dir * 1000 - avg_lockcalls_dir) >> 8;
553 mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
555 * swi_sched acquires the thread lock, so we don't want to call it
556 * with cc_lock held; incorrect locking order.
558 if (!TAILQ_EMPTY(&cc->cc_expireq))
559 swi_sched(cc->cc_cookie, 0);
562 static struct callout_cpu *
563 callout_lock(struct callout *c)
565 struct callout_cpu *cc;
571 if (cpu == CPUBLOCK) {
572 while (c->c_cpu == CPUBLOCK)
587 callout_cc_add(struct callout *c, struct callout_cpu *cc,
588 sbintime_t sbt, sbintime_t precision, void (*func)(void *),
589 void *arg, int cpu, int flags)
594 if (sbt < cc->cc_lastscan)
595 sbt = cc->cc_lastscan;
597 c->c_iflags |= CALLOUT_PENDING;
598 c->c_iflags &= ~CALLOUT_PROCESSED;
599 c->c_flags |= CALLOUT_ACTIVE;
600 if (flags & C_DIRECT_EXEC)
601 c->c_iflags |= CALLOUT_DIRECT;
604 c->c_precision = precision;
605 bucket = callout_get_bucket(c->c_time);
606 CTR3(KTR_CALLOUT, "precision set for %p: %d.%08x",
607 c, (int)(c->c_precision >> 32),
608 (u_int)(c->c_precision & 0xffffffff));
609 LIST_INSERT_HEAD(&cc->cc_callwheel[bucket], c, c_links.le);
610 if (cc->cc_bucket == bucket)
611 cc_exec_next(cc) = c;
612 #ifndef NO_EVENTTIMERS
614 * Inform the eventtimers(4) subsystem there's a new callout
615 * that has been inserted, but only if really required.
617 if (SBT_MAX - c->c_time < c->c_precision)
618 c->c_precision = SBT_MAX - c->c_time;
619 sbt = c->c_time + c->c_precision;
620 if (sbt < cc->cc_firstevent) {
621 cc->cc_firstevent = sbt;
622 cpu_new_callout(cpu, sbt, c->c_time);
628 callout_cc_del(struct callout *c, struct callout_cpu *cc)
631 if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) == 0)
634 SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
638 softclock_call_cc(struct callout *c, struct callout_cpu *cc,
639 #ifdef CALLOUT_PROFILING
640 int *mpcalls, int *lockcalls, int *gcalls,
644 struct rm_priotracker tracker;
645 void (*c_func)(void *);
647 struct lock_class *class;
648 struct lock_object *c_lock;
649 uintptr_t lock_status;
652 struct callout_cpu *new_cc;
653 void (*new_func)(void *);
656 sbintime_t new_prec, new_time;
658 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
659 sbintime_t sbt1, sbt2;
661 static sbintime_t maxdt = 2 * SBT_1MS; /* 2 msec */
662 static timeout_t *lastfunc;
665 KASSERT((c->c_iflags & CALLOUT_PENDING) == CALLOUT_PENDING,
666 ("softclock_call_cc: pend %p %x", c, c->c_iflags));
667 KASSERT((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE,
668 ("softclock_call_cc: act %p %x", c, c->c_flags));
669 class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL;
671 if (c->c_flags & CALLOUT_SHAREDLOCK) {
672 if (class == &lock_class_rm)
673 lock_status = (uintptr_t)&tracker;
680 c_iflags = c->c_iflags;
681 if (c->c_iflags & CALLOUT_LOCAL_ALLOC)
682 c->c_iflags = CALLOUT_LOCAL_ALLOC;
684 c->c_iflags &= ~CALLOUT_PENDING;
686 cc_exec_curr(cc, direct) = c;
687 cc_exec_cancel(cc, direct) = false;
688 cc_exec_drain(cc, direct) = NULL;
690 if (c_lock != NULL) {
691 class->lc_lock(c_lock, lock_status);
693 * The callout may have been cancelled
694 * while we switched locks.
696 if (cc_exec_cancel(cc, direct)) {
697 class->lc_unlock(c_lock);
700 /* The callout cannot be stopped now. */
701 cc_exec_cancel(cc, direct) = true;
702 if (c_lock == &Giant.lock_object) {
703 #ifdef CALLOUT_PROFILING
706 CTR3(KTR_CALLOUT, "callout giant %p func %p arg %p",
709 #ifdef CALLOUT_PROFILING
712 CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p",
716 #ifdef CALLOUT_PROFILING
719 CTR3(KTR_CALLOUT, "callout %p func %p arg %p",
722 KTR_STATE3(KTR_SCHED, "callout", cc->cc_ktr_event_name, "running",
723 "func:%p", c_func, "arg:%p", c_arg, "direct:%d", direct);
724 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
727 THREAD_NO_SLEEPING();
728 SDT_PROBE1(callout_execute, , , callout__start, c);
730 SDT_PROBE1(callout_execute, , , callout__end, c);
731 THREAD_SLEEPING_OK();
732 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
736 if (lastfunc != c_func || sbt2 > maxdt * 2) {
739 "Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
740 c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec);
746 KTR_STATE0(KTR_SCHED, "callout", cc->cc_ktr_event_name, "idle");
747 CTR1(KTR_CALLOUT, "callout %p finished", c);
748 if ((c_iflags & CALLOUT_RETURNUNLOCKED) == 0)
749 class->lc_unlock(c_lock);
752 KASSERT(cc_exec_curr(cc, direct) == c, ("mishandled cc_curr"));
753 cc_exec_curr(cc, direct) = NULL;
754 if (cc_exec_drain(cc, direct)) {
755 void (*drain)(void *);
757 drain = cc_exec_drain(cc, direct);
758 cc_exec_drain(cc, direct) = NULL;
763 if (cc_exec_waiting(cc, direct)) {
765 * There is someone waiting for the
766 * callout to complete.
767 * If the callout was scheduled for
768 * migration just cancel it.
770 if (cc_cce_migrating(cc, direct)) {
771 cc_cce_cleanup(cc, direct);
774 * It should be assert here that the callout is not
775 * destroyed but that is not easy.
777 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
779 cc_exec_waiting(cc, direct) = false;
781 wakeup(&cc_exec_waiting(cc, direct));
783 } else if (cc_cce_migrating(cc, direct)) {
784 KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0,
785 ("Migrating legacy callout %p", c));
788 * If the callout was scheduled for
789 * migration just perform it now.
791 new_cpu = cc_migration_cpu(cc, direct);
792 new_time = cc_migration_time(cc, direct);
793 new_prec = cc_migration_prec(cc, direct);
794 new_func = cc_migration_func(cc, direct);
795 new_arg = cc_migration_arg(cc, direct);
796 cc_cce_cleanup(cc, direct);
799 * It should be assert here that the callout is not destroyed
800 * but that is not easy.
802 * As first thing, handle deferred callout stops.
804 if (!callout_migrating(c)) {
806 "deferred cancelled %p func %p arg %p",
807 c, new_func, new_arg);
808 callout_cc_del(c, cc);
811 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
813 new_cc = callout_cpu_switch(c, cc, new_cpu);
814 flags = (direct) ? C_DIRECT_EXEC : 0;
815 callout_cc_add(c, new_cc, new_time, new_prec, new_func,
816 new_arg, new_cpu, flags);
820 panic("migration should not happen");
824 * If the current callout is locally allocated (from
825 * timeout(9)) then put it on the freelist.
827 * Note: we need to check the cached copy of c_iflags because
828 * if it was not local, then it's not safe to deref the
831 KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0 ||
832 c->c_iflags == CALLOUT_LOCAL_ALLOC,
833 ("corrupted callout"));
834 if (c_iflags & CALLOUT_LOCAL_ALLOC)
835 callout_cc_del(c, cc);
839 * The callout mechanism is based on the work of Adam M. Costello and
840 * George Varghese, published in a technical report entitled "Redesigning
841 * the BSD Callout and Timer Facilities" and modified slightly for inclusion
842 * in FreeBSD by Justin T. Gibbs. The original work on the data structures
843 * used in this implementation was published by G. Varghese and T. Lauck in
844 * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
845 * the Efficient Implementation of a Timer Facility" in the Proceedings of
846 * the 11th ACM Annual Symposium on Operating Systems Principles,
847 * Austin, Texas Nov 1987.
851 * Software (low priority) clock interrupt.
852 * Run periodic events from timeout queue.
857 struct callout_cpu *cc;
859 #ifdef CALLOUT_PROFILING
860 int depth = 0, gcalls = 0, lockcalls = 0, mpcalls = 0;
863 cc = (struct callout_cpu *)arg;
865 while ((c = TAILQ_FIRST(&cc->cc_expireq)) != NULL) {
866 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
867 softclock_call_cc(c, cc,
868 #ifdef CALLOUT_PROFILING
869 &mpcalls, &lockcalls, &gcalls,
872 #ifdef CALLOUT_PROFILING
876 #ifdef CALLOUT_PROFILING
877 avg_depth += (depth * 1000 - avg_depth) >> 8;
878 avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
879 avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
880 avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
887 * Execute a function after a specified length of time.
890 * Cancel previous timeout function call.
892 * callout_handle_init --
893 * Initialize a handle so that using it with untimeout is benign.
895 * See AT&T BCI Driver Reference Manual for specification. This
896 * implementation differs from that one in that although an
897 * identification value is returned from timeout, the original
898 * arguments to timeout as well as the identifier are used to
899 * identify entries for untimeout.
901 struct callout_handle
902 timeout(timeout_t *ftn, void *arg, int to_ticks)
904 struct callout_cpu *cc;
906 struct callout_handle handle;
908 cc = CC_CPU(timeout_cpu);
910 /* Fill in the next free callout structure. */
911 new = SLIST_FIRST(&cc->cc_callfree);
913 /* XXX Attempt to malloc first */
914 panic("timeout table full");
915 SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle);
916 callout_reset(new, to_ticks, ftn, arg);
917 handle.callout = new;
924 untimeout(timeout_t *ftn, void *arg, struct callout_handle handle)
926 struct callout_cpu *cc;
929 * Check for a handle that was initialized
930 * by callout_handle_init, but never used
931 * for a real timeout.
933 if (handle.callout == NULL)
936 cc = callout_lock(handle.callout);
937 if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
938 callout_stop(handle.callout);
943 callout_handle_init(struct callout_handle *handle)
945 handle->callout = NULL;
949 * New interface; clients allocate their own callout structures.
951 * callout_reset() - establish or change a timeout
952 * callout_stop() - disestablish a timeout
953 * callout_init() - initialize a callout structure so that it can
954 * safely be passed to callout_reset() and callout_stop()
956 * <sys/callout.h> defines three convenience macros:
958 * callout_active() - returns truth if callout has not been stopped,
959 * drained, or deactivated since the last time the callout was
961 * callout_pending() - returns truth if callout is still waiting for timeout
962 * callout_deactivate() - marks the callout as having been serviced
965 callout_reset_sbt_on(struct callout *c, sbintime_t sbt, sbintime_t precision,
966 void (*ftn)(void *), void *arg, int cpu, int flags)
968 sbintime_t to_sbt, pr;
969 struct callout_cpu *cc;
970 int cancelled, direct;
976 } else if ((cpu >= MAXCPU) ||
977 ((CC_CPU(cpu))->cc_inited == 0)) {
978 /* Invalid CPU spec */
979 panic("Invalid CPU in callout %d", cpu);
981 if (flags & C_ABSOLUTE) {
984 if ((flags & C_HARDCLOCK) && (sbt < tick_sbt))
986 if ((flags & C_HARDCLOCK) ||
987 #ifdef NO_EVENTTIMERS
988 sbt >= sbt_timethreshold) {
989 to_sbt = getsbinuptime();
991 /* Add safety belt for the case of hz > 1000. */
992 to_sbt += tc_tick_sbt - tick_sbt;
994 sbt >= sbt_tickthreshold) {
996 * Obtain the time of the last hardclock() call on
997 * this CPU directly from the kern_clocksource.c.
998 * This value is per-CPU, but it is equal for all
1002 to_sbt = DPCPU_GET(hardclocktime);
1005 to_sbt = DPCPU_GET(hardclocktime);
1009 if ((flags & C_HARDCLOCK) == 0)
1012 to_sbt = sbinuptime();
1013 if (SBT_MAX - to_sbt < sbt)
1017 pr = ((C_PRELGET(flags) < 0) ? sbt >> tc_precexp :
1018 sbt >> C_PRELGET(flags));
1023 * This flag used to be added by callout_cc_add, but the
1024 * first time you call this we could end up with the
1025 * wrong direct flag if we don't do it before we add.
1027 if (flags & C_DIRECT_EXEC) {
1032 KASSERT(!direct || c->c_lock == NULL,
1033 ("%s: direct callout %p has lock", __func__, c));
1034 cc = callout_lock(c);
1036 * Don't allow migration of pre-allocated callouts lest they
1037 * become unbalanced or handle the case where the user does
1040 if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) ||
1045 if (cc_exec_curr(cc, direct) == c) {
1047 * We're being asked to reschedule a callout which is
1048 * currently in progress. If there is a lock then we
1049 * can cancel the callout if it has not really started.
1051 if (c->c_lock != NULL && !cc_exec_cancel(cc, direct))
1052 cancelled = cc_exec_cancel(cc, direct) = true;
1053 if (cc_exec_waiting(cc, direct)) {
1055 * Someone has called callout_drain to kill this
1056 * callout. Don't reschedule.
1058 CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
1059 cancelled ? "cancelled" : "failed to cancel",
1060 c, c->c_func, c->c_arg);
1065 if (callout_migrating(c)) {
1067 * This only occurs when a second callout_reset_sbt_on
1068 * is made after a previous one moved it into
1069 * deferred migration (below). Note we do *not* change
1070 * the prev_cpu even though the previous target may
1073 cc_migration_cpu(cc, direct) = cpu;
1074 cc_migration_time(cc, direct) = to_sbt;
1075 cc_migration_prec(cc, direct) = precision;
1076 cc_migration_func(cc, direct) = ftn;
1077 cc_migration_arg(cc, direct) = arg;
1084 if (c->c_iflags & CALLOUT_PENDING) {
1085 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1086 if (cc_exec_next(cc) == c)
1087 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1088 LIST_REMOVE(c, c_links.le);
1090 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1093 c->c_iflags &= ~ CALLOUT_PENDING;
1094 c->c_flags &= ~ CALLOUT_ACTIVE;
1099 * If the callout must migrate try to perform it immediately.
1100 * If the callout is currently running, just defer the migration
1101 * to a more appropriate moment.
1103 if (c->c_cpu != cpu) {
1104 if (cc_exec_curr(cc, direct) == c) {
1106 * Pending will have been removed since we are
1107 * actually executing the callout on another
1108 * CPU. That callout should be waiting on the
1109 * lock the caller holds. If we set both
1110 * active/and/pending after we return and the
1111 * lock on the executing callout proceeds, it
1112 * will then see pending is true and return.
1113 * At the return from the actual callout execution
1114 * the migration will occur in softclock_call_cc
1115 * and this new callout will be placed on the
1116 * new CPU via a call to callout_cpu_switch() which
1117 * will get the lock on the right CPU followed
1118 * by a call callout_cc_add() which will add it there.
1119 * (see above in softclock_call_cc()).
1121 cc_migration_cpu(cc, direct) = cpu;
1122 cc_migration_time(cc, direct) = to_sbt;
1123 cc_migration_prec(cc, direct) = precision;
1124 cc_migration_func(cc, direct) = ftn;
1125 cc_migration_arg(cc, direct) = arg;
1126 c->c_iflags |= (CALLOUT_DFRMIGRATION | CALLOUT_PENDING);
1127 c->c_flags |= CALLOUT_ACTIVE;
1129 "migration of %p func %p arg %p in %d.%08x to %u deferred",
1130 c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1131 (u_int)(to_sbt & 0xffffffff), cpu);
1135 cc = callout_cpu_switch(c, cc, cpu);
1139 callout_cc_add(c, cc, to_sbt, precision, ftn, arg, cpu, flags);
1140 CTR6(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d.%08x",
1141 cancelled ? "re" : "", c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1142 (u_int)(to_sbt & 0xffffffff));
1149 * Common idioms that can be optimized in the future.
1152 callout_schedule_on(struct callout *c, int to_ticks, int cpu)
1154 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
1158 callout_schedule(struct callout *c, int to_ticks)
1160 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
1164 _callout_stop_safe(struct callout *c, int flags, void (*drain)(void *))
1166 struct callout_cpu *cc, *old_cc;
1167 struct lock_class *class;
1168 int direct, sq_locked, use_lock;
1169 int cancelled, not_on_a_list;
1171 if ((flags & CS_DRAIN) != 0)
1172 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, c->c_lock,
1173 "calling %s", __func__);
1176 * Some old subsystems don't hold Giant while running a callout_stop(),
1177 * so just discard this check for the moment.
1179 if ((flags & CS_DRAIN) == 0 && c->c_lock != NULL) {
1180 if (c->c_lock == &Giant.lock_object)
1181 use_lock = mtx_owned(&Giant);
1184 class = LOCK_CLASS(c->c_lock);
1185 class->lc_assert(c->c_lock, LA_XLOCKED);
1189 if (c->c_iflags & CALLOUT_DIRECT) {
1197 cc = callout_lock(c);
1199 if ((c->c_iflags & (CALLOUT_DFRMIGRATION | CALLOUT_PENDING)) ==
1200 (CALLOUT_DFRMIGRATION | CALLOUT_PENDING) &&
1201 ((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE)) {
1203 * Special case where this slipped in while we
1204 * were migrating *as* the callout is about to
1205 * execute. The caller probably holds the lock
1206 * the callout wants.
1208 * Get rid of the migration first. Then set
1209 * the flag that tells this code *not* to
1210 * try to remove it from any lists (its not
1211 * on one yet). When the callout wheel runs,
1212 * it will ignore this callout.
1214 c->c_iflags &= ~CALLOUT_PENDING;
1215 c->c_flags &= ~CALLOUT_ACTIVE;
1222 * If the callout was migrating while the callout cpu lock was
1223 * dropped, just drop the sleepqueue lock and check the states
1226 if (sq_locked != 0 && cc != old_cc) {
1229 sleepq_release(&cc_exec_waiting(old_cc, direct));
1234 panic("migration should not happen");
1239 * If the callout is running, try to stop it or drain it.
1241 if (cc_exec_curr(cc, direct) == c) {
1243 * Succeed we to stop it or not, we must clear the
1244 * active flag - this is what API users expect.
1246 c->c_flags &= ~CALLOUT_ACTIVE;
1248 if ((flags & CS_DRAIN) != 0) {
1250 * The current callout is running (or just
1251 * about to run) and blocking is allowed, so
1252 * just wait for the current invocation to
1255 while (cc_exec_curr(cc, direct) == c) {
1257 * Use direct calls to sleepqueue interface
1258 * instead of cv/msleep in order to avoid
1259 * a LOR between cc_lock and sleepqueue
1260 * chain spinlocks. This piece of code
1261 * emulates a msleep_spin() call actually.
1263 * If we already have the sleepqueue chain
1264 * locked, then we can safely block. If we
1265 * don't already have it locked, however,
1266 * we have to drop the cc_lock to lock
1267 * it. This opens several races, so we
1268 * restart at the beginning once we have
1269 * both locks. If nothing has changed, then
1270 * we will end up back here with sq_locked
1276 &cc_exec_waiting(cc, direct));
1283 * Migration could be cancelled here, but
1284 * as long as it is still not sure when it
1285 * will be packed up, just let softclock()
1288 cc_exec_waiting(cc, direct) = true;
1292 &cc_exec_waiting(cc, direct),
1293 &cc->cc_lock.lock_object, "codrain",
1296 &cc_exec_waiting(cc, direct),
1301 /* Reacquire locks previously released. */
1305 } else if (use_lock &&
1306 !cc_exec_cancel(cc, direct) && (drain == NULL)) {
1309 * The current callout is waiting for its
1310 * lock which we hold. Cancel the callout
1311 * and return. After our caller drops the
1312 * lock, the callout will be skipped in
1313 * softclock(). This *only* works with a
1314 * callout_stop() *not* callout_drain() or
1315 * callout_async_drain().
1317 cc_exec_cancel(cc, direct) = true;
1318 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1319 c, c->c_func, c->c_arg);
1320 KASSERT(!cc_cce_migrating(cc, direct),
1321 ("callout wrongly scheduled for migration"));
1322 if (callout_migrating(c)) {
1323 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1325 cc_migration_cpu(cc, direct) = CPUBLOCK;
1326 cc_migration_time(cc, direct) = 0;
1327 cc_migration_prec(cc, direct) = 0;
1328 cc_migration_func(cc, direct) = NULL;
1329 cc_migration_arg(cc, direct) = NULL;
1333 KASSERT(!sq_locked, ("sleepqueue chain locked"));
1335 } else if (callout_migrating(c)) {
1337 * The callout is currently being serviced
1338 * and the "next" callout is scheduled at
1339 * its completion with a migration. We remove
1340 * the migration flag so it *won't* get rescheduled,
1341 * but we can't stop the one thats running so
1344 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1347 * We can't call cc_cce_cleanup here since
1348 * if we do it will remove .ce_curr and
1349 * its still running. This will prevent a
1350 * reschedule of the callout when the
1351 * execution completes.
1353 cc_migration_cpu(cc, direct) = CPUBLOCK;
1354 cc_migration_time(cc, direct) = 0;
1355 cc_migration_prec(cc, direct) = 0;
1356 cc_migration_func(cc, direct) = NULL;
1357 cc_migration_arg(cc, direct) = NULL;
1359 CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p",
1360 c, c->c_func, c->c_arg);
1362 cc_exec_drain(cc, direct) = drain;
1365 return ((flags & CS_EXECUTING) != 0);
1367 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1368 c, c->c_func, c->c_arg);
1370 cc_exec_drain(cc, direct) = drain;
1372 KASSERT(!sq_locked, ("sleepqueue chain still locked"));
1373 cancelled = ((flags & CS_EXECUTING) != 0);
1378 sleepq_release(&cc_exec_waiting(cc, direct));
1380 if ((c->c_iflags & CALLOUT_PENDING) == 0) {
1381 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1382 c, c->c_func, c->c_arg);
1387 c->c_iflags &= ~CALLOUT_PENDING;
1388 c->c_flags &= ~CALLOUT_ACTIVE;
1390 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1391 c, c->c_func, c->c_arg);
1392 if (not_on_a_list == 0) {
1393 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1394 if (cc_exec_next(cc) == c)
1395 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1396 LIST_REMOVE(c, c_links.le);
1398 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1401 callout_cc_del(c, cc);
1407 callout_init(struct callout *c, int mpsafe)
1409 bzero(c, sizeof *c);
1412 c->c_iflags = CALLOUT_RETURNUNLOCKED;
1414 c->c_lock = &Giant.lock_object;
1417 c->c_cpu = timeout_cpu;
1421 _callout_init_lock(struct callout *c, struct lock_object *lock, int flags)
1423 bzero(c, sizeof *c);
1425 KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
1426 ("callout_init_lock: bad flags %d", flags));
1427 KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
1428 ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
1429 KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
1430 (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
1432 c->c_iflags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
1433 c->c_cpu = timeout_cpu;
1436 #ifdef APM_FIXUP_CALLTODO
1438 * Adjust the kernel calltodo timeout list. This routine is used after
1439 * an APM resume to recalculate the calltodo timer list values with the
1440 * number of hz's we have been sleeping. The next hardclock() will detect
1441 * that there are fired timers and run softclock() to execute them.
1443 * Please note, I have not done an exhaustive analysis of what code this
1444 * might break. I am motivated to have my select()'s and alarm()'s that
1445 * have expired during suspend firing upon resume so that the applications
1446 * which set the timer can do the maintanence the timer was for as close
1447 * as possible to the originally intended time. Testing this code for a
1448 * week showed that resuming from a suspend resulted in 22 to 25 timers
1449 * firing, which seemed independent on whether the suspend was 2 hours or
1450 * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu>
1453 adjust_timeout_calltodo(struct timeval *time_change)
1455 register struct callout *p;
1456 unsigned long delta_ticks;
1459 * How many ticks were we asleep?
1460 * (stolen from tvtohz()).
1463 /* Don't do anything */
1464 if (time_change->tv_sec < 0)
1466 else if (time_change->tv_sec <= LONG_MAX / 1000000)
1467 delta_ticks = howmany(time_change->tv_sec * 1000000 +
1468 time_change->tv_usec, tick) + 1;
1469 else if (time_change->tv_sec <= LONG_MAX / hz)
1470 delta_ticks = time_change->tv_sec * hz +
1471 howmany(time_change->tv_usec, tick) + 1;
1473 delta_ticks = LONG_MAX;
1475 if (delta_ticks > INT_MAX)
1476 delta_ticks = INT_MAX;
1479 * Now rip through the timer calltodo list looking for timers
1483 /* don't collide with softclock() */
1485 for (p = calltodo.c_next; p != NULL; p = p->c_next) {
1486 p->c_time -= delta_ticks;
1488 /* Break if the timer had more time on it than delta_ticks */
1492 /* take back the ticks the timer didn't use (p->c_time <= 0) */
1493 delta_ticks = -p->c_time;
1499 #endif /* APM_FIXUP_CALLTODO */
1502 flssbt(sbintime_t sbt)
1505 sbt += (uint64_t)sbt >> 1;
1506 if (sizeof(long) >= sizeof(sbintime_t))
1509 return (flsl(((uint64_t)sbt) >> 32) + 32);
1514 * Dump immediate statistic snapshot of the scheduled callouts.
1517 sysctl_kern_callout_stat(SYSCTL_HANDLER_ARGS)
1519 struct callout *tmp;
1520 struct callout_cpu *cc;
1521 struct callout_list *sc;
1522 sbintime_t maxpr, maxt, medpr, medt, now, spr, st, t;
1523 int ct[64], cpr[64], ccpbk[32];
1524 int error, val, i, count, tcum, pcum, maxc, c, medc;
1530 error = sysctl_handle_int(oidp, &val, 0, req);
1531 if (error != 0 || req->newptr == NULL)
1534 st = spr = maxt = maxpr = 0;
1535 bzero(ccpbk, sizeof(ccpbk));
1536 bzero(ct, sizeof(ct));
1537 bzero(cpr, sizeof(cpr));
1543 cc = CC_CPU(timeout_cpu);
1546 for (i = 0; i < callwheelsize; i++) {
1547 sc = &cc->cc_callwheel[i];
1549 LIST_FOREACH(tmp, sc, c_links.le) {
1551 t = tmp->c_time - now;
1555 spr += tmp->c_precision / SBT_1US;
1558 if (tmp->c_precision > maxpr)
1559 maxpr = tmp->c_precision;
1561 cpr[flssbt(tmp->c_precision)]++;
1565 ccpbk[fls(c + c / 2)]++;
1573 for (i = 0, tcum = 0; i < 64 && tcum < count / 2; i++)
1575 medt = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1576 for (i = 0, pcum = 0; i < 64 && pcum < count / 2; i++)
1578 medpr = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1579 for (i = 0, c = 0; i < 32 && c < count / 2; i++)
1581 medc = (i >= 2) ? (1 << (i - 2)) : 0;
1583 printf("Scheduled callouts statistic snapshot:\n");
1584 printf(" Callouts: %6d Buckets: %6d*%-3d Bucket size: 0.%06ds\n",
1585 count, callwheelsize, mp_ncpus, 1000000 >> CC_HASH_SHIFT);
1586 printf(" C/Bk: med %5d avg %6d.%06jd max %6d\n",
1588 count / callwheelsize / mp_ncpus,
1589 (uint64_t)count * 1000000 / callwheelsize / mp_ncpus % 1000000,
1591 printf(" Time: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1592 medt / SBT_1S, (medt & 0xffffffff) * 1000000 >> 32,
1593 (st / count) / 1000000, (st / count) % 1000000,
1594 maxt / SBT_1S, (maxt & 0xffffffff) * 1000000 >> 32);
1595 printf(" Prec: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1596 medpr / SBT_1S, (medpr & 0xffffffff) * 1000000 >> 32,
1597 (spr / count) / 1000000, (spr / count) % 1000000,
1598 maxpr / SBT_1S, (maxpr & 0xffffffff) * 1000000 >> 32);
1599 printf(" Distribution: \tbuckets\t time\t tcum\t"
1601 for (i = 0, tcum = pcum = 0; i < 64; i++) {
1602 if (ct[i] == 0 && cpr[i] == 0)
1604 t = (i != 0) ? (((sbintime_t)1) << (i - 1)) : 0;
1607 printf(" %10jd.%06jds\t 2**%d\t%7d\t%7d\t%7d\t%7d\n",
1608 t / SBT_1S, (t & 0xffffffff) * 1000000 >> 32,
1609 i - 1 - (32 - CC_HASH_SHIFT),
1610 ct[i], tcum, cpr[i], pcum);
1614 SYSCTL_PROC(_kern, OID_AUTO, callout_stat,
1615 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1616 0, 0, sysctl_kern_callout_stat, "I",
1617 "Dump immediate statistic snapshot of the scheduled callouts");
1621 _show_callout(struct callout *c)
1624 db_printf("callout %p\n", c);
1625 #define C_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, c->e);
1626 db_printf(" &c_links = %p\n", &(c->c_links));
1627 C_DB_PRINTF("%" PRId64, c_time);
1628 C_DB_PRINTF("%" PRId64, c_precision);
1629 C_DB_PRINTF("%p", c_arg);
1630 C_DB_PRINTF("%p", c_func);
1631 C_DB_PRINTF("%p", c_lock);
1632 C_DB_PRINTF("%#x", c_flags);
1633 C_DB_PRINTF("%#x", c_iflags);
1634 C_DB_PRINTF("%d", c_cpu);
1638 DB_SHOW_COMMAND(callout, db_show_callout)
1642 db_printf("usage: show callout <struct callout *>\n");
1646 _show_callout((struct callout *)addr);