2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * From: @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_callout_profiling.h"
45 #include "opt_timer.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/callout.h>
53 #include <sys/domainset.h>
55 #include <sys/interrupt.h>
56 #include <sys/kernel.h>
59 #include <sys/malloc.h>
60 #include <sys/mutex.h>
63 #include <sys/sleepqueue.h>
64 #include <sys/sysctl.h>
69 #include <machine/_inttypes.h>
73 #include <machine/cpu.h>
76 #ifndef NO_EVENTTIMERS
77 DPCPU_DECLARE(sbintime_t, hardclocktime);
80 SDT_PROVIDER_DEFINE(callout_execute);
81 SDT_PROBE_DEFINE1(callout_execute, , , callout__start, "struct callout *");
82 SDT_PROBE_DEFINE1(callout_execute, , , callout__end, "struct callout *");
84 #ifdef CALLOUT_PROFILING
86 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
87 "Average number of items examined per softclock call. Units = 1/1000");
88 static int avg_gcalls;
89 SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
90 "Average number of Giant callouts made per softclock call. Units = 1/1000");
91 static int avg_lockcalls;
92 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0,
93 "Average number of lock callouts made per softclock call. Units = 1/1000");
94 static int avg_mpcalls;
95 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
96 "Average number of MP callouts made per softclock call. Units = 1/1000");
97 static int avg_depth_dir;
98 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth_dir, CTLFLAG_RD, &avg_depth_dir, 0,
99 "Average number of direct callouts examined per callout_process call. "
101 static int avg_lockcalls_dir;
102 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls_dir, CTLFLAG_RD,
103 &avg_lockcalls_dir, 0, "Average number of lock direct callouts made per "
104 "callout_process call. Units = 1/1000");
105 static int avg_mpcalls_dir;
106 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls_dir, CTLFLAG_RD, &avg_mpcalls_dir,
107 0, "Average number of MP direct callouts made per callout_process call. "
112 SYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &ncallout, 0,
113 "Number of entries in callwheel and size of timeout() preallocation");
116 static int pin_default_swi = 1;
117 static int pin_pcpu_swi = 1;
119 static int pin_default_swi = 0;
120 static int pin_pcpu_swi = 0;
123 SYSCTL_INT(_kern, OID_AUTO, pin_default_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_default_swi,
124 0, "Pin the default (non-per-cpu) swi (shared with PCPU 0 swi)");
125 SYSCTL_INT(_kern, OID_AUTO, pin_pcpu_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_pcpu_swi,
126 0, "Pin the per-CPU swis (except PCPU 0, which is also default");
130 * allocate more timeout table slots when table overflows.
132 static u_int __read_mostly callwheelsize;
133 static u_int __read_mostly callwheelmask;
136 * The callout cpu exec entities represent informations necessary for
137 * describing the state of callouts currently running on the CPU and the ones
138 * necessary for migrating callouts to the new callout cpu. In particular,
139 * the first entry of the array cc_exec_entity holds informations for callout
140 * running in SWI thread context, while the second one holds informations
141 * for callout running directly from hardware interrupt context.
142 * The cached informations are very important for deferring migration when
143 * the migrating callout is already running.
146 struct callout *cc_curr;
147 callout_func_t *cc_drain;
149 callout_func_t *ce_migration_func;
150 void *ce_migration_arg;
151 int ce_migration_cpu;
152 sbintime_t ce_migration_time;
153 sbintime_t ce_migration_prec;
160 * There is one struct callout_cpu per cpu, holding all relevant
161 * state for the callout processing thread on the individual CPU.
164 struct mtx_padalign cc_lock;
165 struct cc_exec cc_exec_entity[2];
166 struct callout *cc_next;
167 struct callout *cc_callout;
168 struct callout_list *cc_callwheel;
169 struct callout_tailq cc_expireq;
170 struct callout_slist cc_callfree;
171 sbintime_t cc_firstevent;
172 sbintime_t cc_lastscan;
176 char cc_ktr_event_name[20];
179 #define callout_migrating(c) ((c)->c_iflags & CALLOUT_DFRMIGRATION)
181 #define cc_exec_curr(cc, dir) cc->cc_exec_entity[dir].cc_curr
182 #define cc_exec_drain(cc, dir) cc->cc_exec_entity[dir].cc_drain
183 #define cc_exec_next(cc) cc->cc_next
184 #define cc_exec_cancel(cc, dir) cc->cc_exec_entity[dir].cc_cancel
185 #define cc_exec_waiting(cc, dir) cc->cc_exec_entity[dir].cc_waiting
187 #define cc_migration_func(cc, dir) cc->cc_exec_entity[dir].ce_migration_func
188 #define cc_migration_arg(cc, dir) cc->cc_exec_entity[dir].ce_migration_arg
189 #define cc_migration_cpu(cc, dir) cc->cc_exec_entity[dir].ce_migration_cpu
190 #define cc_migration_time(cc, dir) cc->cc_exec_entity[dir].ce_migration_time
191 #define cc_migration_prec(cc, dir) cc->cc_exec_entity[dir].ce_migration_prec
193 struct callout_cpu cc_cpu[MAXCPU];
194 #define CPUBLOCK MAXCPU
195 #define CC_CPU(cpu) (&cc_cpu[(cpu)])
196 #define CC_SELF() CC_CPU(PCPU_GET(cpuid))
198 struct callout_cpu cc_cpu;
199 #define CC_CPU(cpu) &cc_cpu
200 #define CC_SELF() &cc_cpu
202 #define CC_LOCK(cc) mtx_lock_spin(&(cc)->cc_lock)
203 #define CC_UNLOCK(cc) mtx_unlock_spin(&(cc)->cc_lock)
204 #define CC_LOCK_ASSERT(cc) mtx_assert(&(cc)->cc_lock, MA_OWNED)
206 static int __read_mostly timeout_cpu;
208 static void callout_cpu_init(struct callout_cpu *cc, int cpu);
209 static void softclock_call_cc(struct callout *c, struct callout_cpu *cc,
210 #ifdef CALLOUT_PROFILING
211 int *mpcalls, int *lockcalls, int *gcalls,
215 static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
219 * cc_curr - If a callout is in progress, it is cc_curr.
220 * If cc_curr is non-NULL, threads waiting in
221 * callout_drain() will be woken up as soon as the
222 * relevant callout completes.
223 * cc_cancel - Changing to 1 with both callout_lock and cc_lock held
224 * guarantees that the current callout will not run.
225 * The softclock() function sets this to 0 before it
226 * drops callout_lock to acquire c_lock, and it calls
227 * the handler only if curr_cancelled is still 0 after
228 * cc_lock is successfully acquired.
229 * cc_waiting - If a thread is waiting in callout_drain(), then
230 * callout_wait is nonzero. Set only when
231 * cc_curr is non-NULL.
235 * Resets the execution entity tied to a specific callout cpu.
238 cc_cce_cleanup(struct callout_cpu *cc, int direct)
241 cc_exec_curr(cc, direct) = NULL;
242 cc_exec_cancel(cc, direct) = false;
243 cc_exec_waiting(cc, direct) = false;
245 cc_migration_cpu(cc, direct) = CPUBLOCK;
246 cc_migration_time(cc, direct) = 0;
247 cc_migration_prec(cc, direct) = 0;
248 cc_migration_func(cc, direct) = NULL;
249 cc_migration_arg(cc, direct) = NULL;
254 * Checks if migration is requested by a specific callout cpu.
257 cc_cce_migrating(struct callout_cpu *cc, int direct)
261 return (cc_migration_cpu(cc, direct) != CPUBLOCK);
268 * Kernel low level callwheel initialization
269 * called on the BSP during kernel startup.
272 callout_callwheel_init(void *dummy)
274 struct callout_cpu *cc;
277 * Calculate the size of the callout wheel and the preallocated
278 * timeout() structures.
279 * XXX: Clip callout to result of previous function of maxusers
280 * maximum 384. This is still huge, but acceptable.
282 memset(CC_CPU(curcpu), 0, sizeof(cc_cpu));
283 ncallout = imin(16 + maxproc + maxfiles, 18508);
284 TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
287 * Calculate callout wheel size, should be next power of two higher
290 callwheelsize = 1 << fls(ncallout);
291 callwheelmask = callwheelsize - 1;
294 * Fetch whether we're pinning the swi's or not.
296 TUNABLE_INT_FETCH("kern.pin_default_swi", &pin_default_swi);
297 TUNABLE_INT_FETCH("kern.pin_pcpu_swi", &pin_pcpu_swi);
300 * Only BSP handles timeout(9) and receives a preallocation.
302 * XXX: Once all timeout(9) consumers are converted this can
305 timeout_cpu = PCPU_GET(cpuid);
306 cc = CC_CPU(timeout_cpu);
307 cc->cc_callout = malloc(ncallout * sizeof(struct callout),
308 M_CALLOUT, M_WAITOK);
309 callout_cpu_init(cc, timeout_cpu);
311 SYSINIT(callwheel_init, SI_SUB_CPU, SI_ORDER_ANY, callout_callwheel_init, NULL);
314 * Initialize the per-cpu callout structures.
317 callout_cpu_init(struct callout_cpu *cc, int cpu)
322 mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
323 SLIST_INIT(&cc->cc_callfree);
325 cc->cc_callwheel = malloc_domainset(sizeof(struct callout_list) *
326 callwheelsize, M_CALLOUT,
327 DOMAINSET_PREF(pcpu_find(cpu)->pc_domain), M_WAITOK);
328 for (i = 0; i < callwheelsize; i++)
329 LIST_INIT(&cc->cc_callwheel[i]);
330 TAILQ_INIT(&cc->cc_expireq);
331 cc->cc_firstevent = SBT_MAX;
332 for (i = 0; i < 2; i++)
333 cc_cce_cleanup(cc, i);
334 snprintf(cc->cc_ktr_event_name, sizeof(cc->cc_ktr_event_name),
335 "callwheel cpu %d", cpu);
336 if (cc->cc_callout == NULL) /* Only BSP handles timeout(9) */
338 for (i = 0; i < ncallout; i++) {
339 c = &cc->cc_callout[i];
341 c->c_iflags = CALLOUT_LOCAL_ALLOC;
342 SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
348 * Switches the cpu tied to a specific callout.
349 * The function expects a locked incoming callout cpu and returns with
350 * locked outcoming callout cpu.
352 static struct callout_cpu *
353 callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu)
355 struct callout_cpu *new_cc;
357 MPASS(c != NULL && cc != NULL);
361 * Avoid interrupts and preemption firing after the callout cpu
362 * is blocked in order to avoid deadlocks as the new thread
363 * may be willing to acquire the callout cpu lock.
368 new_cc = CC_CPU(new_cpu);
377 * Start standard softclock thread.
380 start_softclock(void *dummy)
382 struct callout_cpu *cc;
383 char name[MAXCOMLEN];
386 struct intr_event *ie;
389 cc = CC_CPU(timeout_cpu);
390 snprintf(name, sizeof(name), "clock (%d)", timeout_cpu);
391 if (swi_add(&clk_intr_event, name, softclock, cc, SWI_CLOCK,
392 INTR_MPSAFE, &cc->cc_cookie))
393 panic("died while creating standard software ithreads");
394 if (pin_default_swi &&
395 (intr_event_bind(clk_intr_event, timeout_cpu) != 0)) {
396 printf("%s: timeout clock couldn't be pinned to cpu %d\n",
403 if (cpu == timeout_cpu)
406 cc->cc_callout = NULL; /* Only BSP handles timeout(9). */
407 callout_cpu_init(cc, cpu);
408 snprintf(name, sizeof(name), "clock (%d)", cpu);
410 if (swi_add(&ie, name, softclock, cc, SWI_CLOCK,
411 INTR_MPSAFE, &cc->cc_cookie))
412 panic("died while creating standard software ithreads");
413 if (pin_pcpu_swi && (intr_event_bind(ie, cpu) != 0)) {
414 printf("%s: per-cpu clock couldn't be pinned to "
422 SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
424 #define CC_HASH_SHIFT 8
427 callout_hash(sbintime_t sbt)
430 return (sbt >> (32 - CC_HASH_SHIFT));
434 callout_get_bucket(sbintime_t sbt)
437 return (callout_hash(sbt) & callwheelmask);
441 callout_process(sbintime_t now)
443 struct callout *tmp, *tmpn;
444 struct callout_cpu *cc;
445 struct callout_list *sc;
446 sbintime_t first, last, max, tmp_max;
448 u_int firstb, lastb, nowb;
449 #ifdef CALLOUT_PROFILING
450 int depth_dir = 0, mpcalls_dir = 0, lockcalls_dir = 0;
454 mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
456 /* Compute the buckets of the last scan and present times. */
457 firstb = callout_hash(cc->cc_lastscan);
458 cc->cc_lastscan = now;
459 nowb = callout_hash(now);
461 /* Compute the last bucket and minimum time of the bucket after it. */
463 lookahead = (SBT_1S / 16);
464 else if (nowb - firstb == 1)
465 lookahead = (SBT_1S / 8);
467 lookahead = (SBT_1S / 2);
469 first += (lookahead / 2);
471 last &= (0xffffffffffffffffLLU << (32 - CC_HASH_SHIFT));
472 lastb = callout_hash(last) - 1;
476 * Check if we wrapped around the entire wheel from the last scan.
477 * In case, we need to scan entirely the wheel for pending callouts.
479 if (lastb - firstb >= callwheelsize) {
480 lastb = firstb + callwheelsize - 1;
481 if (nowb - firstb >= callwheelsize)
485 /* Iterate callwheel from firstb to nowb and then up to lastb. */
487 sc = &cc->cc_callwheel[firstb & callwheelmask];
488 tmp = LIST_FIRST(sc);
489 while (tmp != NULL) {
490 /* Run the callout if present time within allowed. */
491 if (tmp->c_time <= now) {
493 * Consumer told us the callout may be run
494 * directly from hardware interrupt context.
496 if (tmp->c_iflags & CALLOUT_DIRECT) {
497 #ifdef CALLOUT_PROFILING
501 LIST_NEXT(tmp, c_links.le);
502 cc->cc_bucket = firstb & callwheelmask;
503 LIST_REMOVE(tmp, c_links.le);
504 softclock_call_cc(tmp, cc,
505 #ifdef CALLOUT_PROFILING
506 &mpcalls_dir, &lockcalls_dir, NULL,
509 tmp = cc_exec_next(cc);
510 cc_exec_next(cc) = NULL;
512 tmpn = LIST_NEXT(tmp, c_links.le);
513 LIST_REMOVE(tmp, c_links.le);
514 TAILQ_INSERT_TAIL(&cc->cc_expireq,
516 tmp->c_iflags |= CALLOUT_PROCESSED;
521 /* Skip events from distant future. */
522 if (tmp->c_time >= max)
525 * Event minimal time is bigger than present maximal
526 * time, so it cannot be aggregated.
528 if (tmp->c_time > last) {
532 /* Update first and last time, respecting this event. */
533 if (tmp->c_time < first)
535 tmp_max = tmp->c_time + tmp->c_precision;
539 tmp = LIST_NEXT(tmp, c_links.le);
541 /* Proceed with the next bucket. */
544 * Stop if we looked after present time and found
545 * some event we can't execute at now.
546 * Stop if we looked far enough into the future.
548 } while (((int)(firstb - lastb)) <= 0);
549 cc->cc_firstevent = last;
550 #ifndef NO_EVENTTIMERS
551 cpu_new_callout(curcpu, last, first);
553 #ifdef CALLOUT_PROFILING
554 avg_depth_dir += (depth_dir * 1000 - avg_depth_dir) >> 8;
555 avg_mpcalls_dir += (mpcalls_dir * 1000 - avg_mpcalls_dir) >> 8;
556 avg_lockcalls_dir += (lockcalls_dir * 1000 - avg_lockcalls_dir) >> 8;
558 mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
560 * swi_sched acquires the thread lock, so we don't want to call it
561 * with cc_lock held; incorrect locking order.
563 if (!TAILQ_EMPTY(&cc->cc_expireq))
564 swi_sched(cc->cc_cookie, 0);
567 static struct callout_cpu *
568 callout_lock(struct callout *c)
570 struct callout_cpu *cc;
576 if (cpu == CPUBLOCK) {
577 while (c->c_cpu == CPUBLOCK)
592 callout_cc_add(struct callout *c, struct callout_cpu *cc,
593 sbintime_t sbt, sbintime_t precision, void (*func)(void *),
594 void *arg, int cpu, int flags)
599 if (sbt < cc->cc_lastscan)
600 sbt = cc->cc_lastscan;
602 c->c_iflags |= CALLOUT_PENDING;
603 c->c_iflags &= ~CALLOUT_PROCESSED;
604 c->c_flags |= CALLOUT_ACTIVE;
605 if (flags & C_DIRECT_EXEC)
606 c->c_iflags |= CALLOUT_DIRECT;
609 c->c_precision = precision;
610 bucket = callout_get_bucket(c->c_time);
611 CTR3(KTR_CALLOUT, "precision set for %p: %d.%08x",
612 c, (int)(c->c_precision >> 32),
613 (u_int)(c->c_precision & 0xffffffff));
614 LIST_INSERT_HEAD(&cc->cc_callwheel[bucket], c, c_links.le);
615 if (cc->cc_bucket == bucket)
616 cc_exec_next(cc) = c;
617 #ifndef NO_EVENTTIMERS
619 * Inform the eventtimers(4) subsystem there's a new callout
620 * that has been inserted, but only if really required.
622 if (SBT_MAX - c->c_time < c->c_precision)
623 c->c_precision = SBT_MAX - c->c_time;
624 sbt = c->c_time + c->c_precision;
625 if (sbt < cc->cc_firstevent) {
626 cc->cc_firstevent = sbt;
627 cpu_new_callout(cpu, sbt, c->c_time);
633 callout_cc_del(struct callout *c, struct callout_cpu *cc)
636 if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) == 0)
639 SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
643 softclock_call_cc(struct callout *c, struct callout_cpu *cc,
644 #ifdef CALLOUT_PROFILING
645 int *mpcalls, int *lockcalls, int *gcalls,
649 struct rm_priotracker tracker;
650 callout_func_t *c_func, *drain;
652 struct lock_class *class;
653 struct lock_object *c_lock;
654 uintptr_t lock_status;
657 struct callout_cpu *new_cc;
658 callout_func_t *new_func;
661 sbintime_t new_prec, new_time;
663 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
664 sbintime_t sbt1, sbt2;
666 static sbintime_t maxdt = 2 * SBT_1MS; /* 2 msec */
667 static callout_func_t *lastfunc;
670 KASSERT((c->c_iflags & CALLOUT_PENDING) == CALLOUT_PENDING,
671 ("softclock_call_cc: pend %p %x", c, c->c_iflags));
672 KASSERT((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE,
673 ("softclock_call_cc: act %p %x", c, c->c_flags));
674 class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL;
676 if (c->c_flags & CALLOUT_SHAREDLOCK) {
677 if (class == &lock_class_rm)
678 lock_status = (uintptr_t)&tracker;
685 c_iflags = c->c_iflags;
686 if (c->c_iflags & CALLOUT_LOCAL_ALLOC)
687 c->c_iflags = CALLOUT_LOCAL_ALLOC;
689 c->c_iflags &= ~CALLOUT_PENDING;
691 cc_exec_curr(cc, direct) = c;
692 cc_exec_cancel(cc, direct) = false;
693 cc_exec_drain(cc, direct) = NULL;
695 if (c_lock != NULL) {
696 class->lc_lock(c_lock, lock_status);
698 * The callout may have been cancelled
699 * while we switched locks.
701 if (cc_exec_cancel(cc, direct)) {
702 class->lc_unlock(c_lock);
705 /* The callout cannot be stopped now. */
706 cc_exec_cancel(cc, direct) = true;
707 if (c_lock == &Giant.lock_object) {
708 #ifdef CALLOUT_PROFILING
711 CTR3(KTR_CALLOUT, "callout giant %p func %p arg %p",
714 #ifdef CALLOUT_PROFILING
717 CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p",
721 #ifdef CALLOUT_PROFILING
724 CTR3(KTR_CALLOUT, "callout %p func %p arg %p",
727 KTR_STATE3(KTR_SCHED, "callout", cc->cc_ktr_event_name, "running",
728 "func:%p", c_func, "arg:%p", c_arg, "direct:%d", direct);
729 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
732 THREAD_NO_SLEEPING();
733 SDT_PROBE1(callout_execute, , , callout__start, c);
735 SDT_PROBE1(callout_execute, , , callout__end, c);
736 THREAD_SLEEPING_OK();
737 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
741 if (lastfunc != c_func || sbt2 > maxdt * 2) {
744 "Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
745 c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec);
751 KTR_STATE0(KTR_SCHED, "callout", cc->cc_ktr_event_name, "idle");
752 CTR1(KTR_CALLOUT, "callout %p finished", c);
753 if ((c_iflags & CALLOUT_RETURNUNLOCKED) == 0)
754 class->lc_unlock(c_lock);
757 KASSERT(cc_exec_curr(cc, direct) == c, ("mishandled cc_curr"));
758 cc_exec_curr(cc, direct) = NULL;
759 if (cc_exec_drain(cc, direct)) {
760 drain = cc_exec_drain(cc, direct);
761 cc_exec_drain(cc, direct) = NULL;
766 if (cc_exec_waiting(cc, direct)) {
768 * There is someone waiting for the
769 * callout to complete.
770 * If the callout was scheduled for
771 * migration just cancel it.
773 if (cc_cce_migrating(cc, direct)) {
774 cc_cce_cleanup(cc, direct);
777 * It should be assert here that the callout is not
778 * destroyed but that is not easy.
780 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
782 cc_exec_waiting(cc, direct) = false;
784 wakeup(&cc_exec_waiting(cc, direct));
786 } else if (cc_cce_migrating(cc, direct)) {
787 KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0,
788 ("Migrating legacy callout %p", c));
791 * If the callout was scheduled for
792 * migration just perform it now.
794 new_cpu = cc_migration_cpu(cc, direct);
795 new_time = cc_migration_time(cc, direct);
796 new_prec = cc_migration_prec(cc, direct);
797 new_func = cc_migration_func(cc, direct);
798 new_arg = cc_migration_arg(cc, direct);
799 cc_cce_cleanup(cc, direct);
802 * It should be assert here that the callout is not destroyed
803 * but that is not easy.
805 * As first thing, handle deferred callout stops.
807 if (!callout_migrating(c)) {
809 "deferred cancelled %p func %p arg %p",
810 c, new_func, new_arg);
811 callout_cc_del(c, cc);
814 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
816 new_cc = callout_cpu_switch(c, cc, new_cpu);
817 flags = (direct) ? C_DIRECT_EXEC : 0;
818 callout_cc_add(c, new_cc, new_time, new_prec, new_func,
819 new_arg, new_cpu, flags);
823 panic("migration should not happen");
827 * If the current callout is locally allocated (from
828 * timeout(9)) then put it on the freelist.
830 * Note: we need to check the cached copy of c_iflags because
831 * if it was not local, then it's not safe to deref the
834 KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0 ||
835 c->c_iflags == CALLOUT_LOCAL_ALLOC,
836 ("corrupted callout"));
837 if (c_iflags & CALLOUT_LOCAL_ALLOC)
838 callout_cc_del(c, cc);
842 * The callout mechanism is based on the work of Adam M. Costello and
843 * George Varghese, published in a technical report entitled "Redesigning
844 * the BSD Callout and Timer Facilities" and modified slightly for inclusion
845 * in FreeBSD by Justin T. Gibbs. The original work on the data structures
846 * used in this implementation was published by G. Varghese and T. Lauck in
847 * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
848 * the Efficient Implementation of a Timer Facility" in the Proceedings of
849 * the 11th ACM Annual Symposium on Operating Systems Principles,
850 * Austin, Texas Nov 1987.
854 * Software (low priority) clock interrupt.
855 * Run periodic events from timeout queue.
860 struct callout_cpu *cc;
862 #ifdef CALLOUT_PROFILING
863 int depth = 0, gcalls = 0, lockcalls = 0, mpcalls = 0;
866 cc = (struct callout_cpu *)arg;
868 while ((c = TAILQ_FIRST(&cc->cc_expireq)) != NULL) {
869 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
870 softclock_call_cc(c, cc,
871 #ifdef CALLOUT_PROFILING
872 &mpcalls, &lockcalls, &gcalls,
875 #ifdef CALLOUT_PROFILING
879 #ifdef CALLOUT_PROFILING
880 avg_depth += (depth * 1000 - avg_depth) >> 8;
881 avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
882 avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
883 avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
890 * Execute a function after a specified length of time.
893 * Cancel previous timeout function call.
895 * callout_handle_init --
896 * Initialize a handle so that using it with untimeout is benign.
898 * See AT&T BCI Driver Reference Manual for specification. This
899 * implementation differs from that one in that although an
900 * identification value is returned from timeout, the original
901 * arguments to timeout as well as the identifier are used to
902 * identify entries for untimeout.
904 struct callout_handle
905 timeout(timeout_t *ftn, void *arg, int to_ticks)
907 struct callout_cpu *cc;
909 struct callout_handle handle;
911 cc = CC_CPU(timeout_cpu);
913 /* Fill in the next free callout structure. */
914 new = SLIST_FIRST(&cc->cc_callfree);
916 /* XXX Attempt to malloc first */
917 panic("timeout table full");
918 SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle);
919 callout_reset(new, to_ticks, ftn, arg);
920 handle.callout = new;
927 untimeout(timeout_t *ftn, void *arg, struct callout_handle handle)
929 struct callout_cpu *cc;
932 * Check for a handle that was initialized
933 * by callout_handle_init, but never used
934 * for a real timeout.
936 if (handle.callout == NULL)
939 cc = callout_lock(handle.callout);
940 if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
941 callout_stop(handle.callout);
946 callout_handle_init(struct callout_handle *handle)
948 handle->callout = NULL;
952 callout_when(sbintime_t sbt, sbintime_t precision, int flags,
953 sbintime_t *res, sbintime_t *prec_res)
955 sbintime_t to_sbt, to_pr;
957 if ((flags & (C_ABSOLUTE | C_PRECALC)) != 0) {
959 *prec_res = precision;
962 if ((flags & C_HARDCLOCK) != 0 && sbt < tick_sbt)
964 if ((flags & C_HARDCLOCK) != 0 ||
965 #ifdef NO_EVENTTIMERS
966 sbt >= sbt_timethreshold) {
967 to_sbt = getsbinuptime();
969 /* Add safety belt for the case of hz > 1000. */
970 to_sbt += tc_tick_sbt - tick_sbt;
972 sbt >= sbt_tickthreshold) {
974 * Obtain the time of the last hardclock() call on
975 * this CPU directly from the kern_clocksource.c.
976 * This value is per-CPU, but it is equal for all
980 to_sbt = DPCPU_GET(hardclocktime);
983 to_sbt = DPCPU_GET(hardclocktime);
987 if (cold && to_sbt == 0)
988 to_sbt = sbinuptime();
989 if ((flags & C_HARDCLOCK) == 0)
992 to_sbt = sbinuptime();
993 if (SBT_MAX - to_sbt < sbt)
998 to_pr = ((C_PRELGET(flags) < 0) ? sbt >> tc_precexp :
999 sbt >> C_PRELGET(flags));
1000 *prec_res = to_pr > precision ? to_pr : precision;
1004 * New interface; clients allocate their own callout structures.
1006 * callout_reset() - establish or change a timeout
1007 * callout_stop() - disestablish a timeout
1008 * callout_init() - initialize a callout structure so that it can
1009 * safely be passed to callout_reset() and callout_stop()
1011 * <sys/callout.h> defines three convenience macros:
1013 * callout_active() - returns truth if callout has not been stopped,
1014 * drained, or deactivated since the last time the callout was
1016 * callout_pending() - returns truth if callout is still waiting for timeout
1017 * callout_deactivate() - marks the callout as having been serviced
1020 callout_reset_sbt_on(struct callout *c, sbintime_t sbt, sbintime_t prec,
1021 callout_func_t *ftn, void *arg, int cpu, int flags)
1023 sbintime_t to_sbt, precision;
1024 struct callout_cpu *cc;
1025 int cancelled, direct;
1031 } else if ((cpu >= MAXCPU) ||
1032 ((CC_CPU(cpu))->cc_inited == 0)) {
1033 /* Invalid CPU spec */
1034 panic("Invalid CPU in callout %d", cpu);
1036 callout_when(sbt, prec, flags, &to_sbt, &precision);
1039 * This flag used to be added by callout_cc_add, but the
1040 * first time you call this we could end up with the
1041 * wrong direct flag if we don't do it before we add.
1043 if (flags & C_DIRECT_EXEC) {
1048 KASSERT(!direct || c->c_lock == NULL,
1049 ("%s: direct callout %p has lock", __func__, c));
1050 cc = callout_lock(c);
1052 * Don't allow migration of pre-allocated callouts lest they
1053 * become unbalanced or handle the case where the user does
1056 if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) ||
1061 if (cc_exec_curr(cc, direct) == c) {
1063 * We're being asked to reschedule a callout which is
1064 * currently in progress. If there is a lock then we
1065 * can cancel the callout if it has not really started.
1067 if (c->c_lock != NULL && !cc_exec_cancel(cc, direct))
1068 cancelled = cc_exec_cancel(cc, direct) = true;
1069 if (cc_exec_waiting(cc, direct) || cc_exec_drain(cc, direct)) {
1071 * Someone has called callout_drain to kill this
1072 * callout. Don't reschedule.
1074 CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
1075 cancelled ? "cancelled" : "failed to cancel",
1076 c, c->c_func, c->c_arg);
1081 if (callout_migrating(c)) {
1083 * This only occurs when a second callout_reset_sbt_on
1084 * is made after a previous one moved it into
1085 * deferred migration (below). Note we do *not* change
1086 * the prev_cpu even though the previous target may
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;
1100 if (c->c_iflags & CALLOUT_PENDING) {
1101 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1102 if (cc_exec_next(cc) == c)
1103 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1104 LIST_REMOVE(c, c_links.le);
1106 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1109 c->c_iflags &= ~ CALLOUT_PENDING;
1110 c->c_flags &= ~ CALLOUT_ACTIVE;
1115 * If the callout must migrate try to perform it immediately.
1116 * If the callout is currently running, just defer the migration
1117 * to a more appropriate moment.
1119 if (c->c_cpu != cpu) {
1120 if (cc_exec_curr(cc, direct) == c) {
1122 * Pending will have been removed since we are
1123 * actually executing the callout on another
1124 * CPU. That callout should be waiting on the
1125 * lock the caller holds. If we set both
1126 * active/and/pending after we return and the
1127 * lock on the executing callout proceeds, it
1128 * will then see pending is true and return.
1129 * At the return from the actual callout execution
1130 * the migration will occur in softclock_call_cc
1131 * and this new callout will be placed on the
1132 * new CPU via a call to callout_cpu_switch() which
1133 * will get the lock on the right CPU followed
1134 * by a call callout_cc_add() which will add it there.
1135 * (see above in softclock_call_cc()).
1137 cc_migration_cpu(cc, direct) = cpu;
1138 cc_migration_time(cc, direct) = to_sbt;
1139 cc_migration_prec(cc, direct) = precision;
1140 cc_migration_func(cc, direct) = ftn;
1141 cc_migration_arg(cc, direct) = arg;
1142 c->c_iflags |= (CALLOUT_DFRMIGRATION | CALLOUT_PENDING);
1143 c->c_flags |= CALLOUT_ACTIVE;
1145 "migration of %p func %p arg %p in %d.%08x to %u deferred",
1146 c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1147 (u_int)(to_sbt & 0xffffffff), cpu);
1151 cc = callout_cpu_switch(c, cc, cpu);
1155 callout_cc_add(c, cc, to_sbt, precision, ftn, arg, cpu, flags);
1156 CTR6(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d.%08x",
1157 cancelled ? "re" : "", c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1158 (u_int)(to_sbt & 0xffffffff));
1165 * Common idioms that can be optimized in the future.
1168 callout_schedule_on(struct callout *c, int to_ticks, int cpu)
1170 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
1174 callout_schedule(struct callout *c, int to_ticks)
1176 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
1180 _callout_stop_safe(struct callout *c, int flags, callout_func_t *drain)
1182 struct callout_cpu *cc, *old_cc;
1183 struct lock_class *class;
1184 int direct, sq_locked, use_lock;
1185 int cancelled, not_on_a_list;
1187 if ((flags & CS_DRAIN) != 0)
1188 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, c->c_lock,
1189 "calling %s", __func__);
1192 * Some old subsystems don't hold Giant while running a callout_stop(),
1193 * so just discard this check for the moment.
1195 if ((flags & CS_DRAIN) == 0 && c->c_lock != NULL) {
1196 if (c->c_lock == &Giant.lock_object)
1197 use_lock = mtx_owned(&Giant);
1200 class = LOCK_CLASS(c->c_lock);
1201 class->lc_assert(c->c_lock, LA_XLOCKED);
1205 if (c->c_iflags & CALLOUT_DIRECT) {
1213 cc = callout_lock(c);
1215 if ((c->c_iflags & (CALLOUT_DFRMIGRATION | CALLOUT_PENDING)) ==
1216 (CALLOUT_DFRMIGRATION | CALLOUT_PENDING) &&
1217 ((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE)) {
1219 * Special case where this slipped in while we
1220 * were migrating *as* the callout is about to
1221 * execute. The caller probably holds the lock
1222 * the callout wants.
1224 * Get rid of the migration first. Then set
1225 * the flag that tells this code *not* to
1226 * try to remove it from any lists (its not
1227 * on one yet). When the callout wheel runs,
1228 * it will ignore this callout.
1230 c->c_iflags &= ~CALLOUT_PENDING;
1231 c->c_flags &= ~CALLOUT_ACTIVE;
1238 * If the callout was migrating while the callout cpu lock was
1239 * dropped, just drop the sleepqueue lock and check the states
1242 if (sq_locked != 0 && cc != old_cc) {
1245 sleepq_release(&cc_exec_waiting(old_cc, direct));
1250 panic("migration should not happen");
1255 * If the callout is running, try to stop it or drain it.
1257 if (cc_exec_curr(cc, direct) == c) {
1259 * Succeed we to stop it or not, we must clear the
1260 * active flag - this is what API users expect. If we're
1261 * draining and the callout is currently executing, first wait
1262 * until it finishes.
1264 if ((flags & CS_DRAIN) == 0)
1265 c->c_flags &= ~CALLOUT_ACTIVE;
1267 if ((flags & CS_DRAIN) != 0) {
1269 * The current callout is running (or just
1270 * about to run) and blocking is allowed, so
1271 * just wait for the current invocation to
1274 if (cc_exec_curr(cc, direct) == c) {
1276 * Use direct calls to sleepqueue interface
1277 * instead of cv/msleep in order to avoid
1278 * a LOR between cc_lock and sleepqueue
1279 * chain spinlocks. This piece of code
1280 * emulates a msleep_spin() call actually.
1282 * If we already have the sleepqueue chain
1283 * locked, then we can safely block. If we
1284 * don't already have it locked, however,
1285 * we have to drop the cc_lock to lock
1286 * it. This opens several races, so we
1287 * restart at the beginning once we have
1288 * both locks. If nothing has changed, then
1289 * we will end up back here with sq_locked
1295 &cc_exec_waiting(cc, direct));
1302 * Migration could be cancelled here, but
1303 * as long as it is still not sure when it
1304 * will be packed up, just let softclock()
1307 cc_exec_waiting(cc, direct) = true;
1311 &cc_exec_waiting(cc, direct),
1312 &cc->cc_lock.lock_object, "codrain",
1315 &cc_exec_waiting(cc, direct),
1320 /* Reacquire locks previously released. */
1324 c->c_flags &= ~CALLOUT_ACTIVE;
1325 } else if (use_lock &&
1326 !cc_exec_cancel(cc, direct) && (drain == NULL)) {
1329 * The current callout is waiting for its
1330 * lock which we hold. Cancel the callout
1331 * and return. After our caller drops the
1332 * lock, the callout will be skipped in
1333 * softclock(). This *only* works with a
1334 * callout_stop() *not* callout_drain() or
1335 * callout_async_drain().
1337 cc_exec_cancel(cc, direct) = true;
1338 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1339 c, c->c_func, c->c_arg);
1340 KASSERT(!cc_cce_migrating(cc, direct),
1341 ("callout wrongly scheduled for migration"));
1342 if (callout_migrating(c)) {
1343 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1345 cc_migration_cpu(cc, direct) = CPUBLOCK;
1346 cc_migration_time(cc, direct) = 0;
1347 cc_migration_prec(cc, direct) = 0;
1348 cc_migration_func(cc, direct) = NULL;
1349 cc_migration_arg(cc, direct) = NULL;
1353 KASSERT(!sq_locked, ("sleepqueue chain locked"));
1355 } else if (callout_migrating(c)) {
1357 * The callout is currently being serviced
1358 * and the "next" callout is scheduled at
1359 * its completion with a migration. We remove
1360 * the migration flag so it *won't* get rescheduled,
1361 * but we can't stop the one thats running so
1364 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1367 * We can't call cc_cce_cleanup here since
1368 * if we do it will remove .ce_curr and
1369 * its still running. This will prevent a
1370 * reschedule of the callout when the
1371 * execution completes.
1373 cc_migration_cpu(cc, direct) = CPUBLOCK;
1374 cc_migration_time(cc, direct) = 0;
1375 cc_migration_prec(cc, direct) = 0;
1376 cc_migration_func(cc, direct) = NULL;
1377 cc_migration_arg(cc, direct) = NULL;
1379 CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p",
1380 c, c->c_func, c->c_arg);
1382 cc_exec_drain(cc, direct) = drain;
1385 return ((flags & CS_EXECUTING) != 0);
1387 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1388 c, c->c_func, c->c_arg);
1390 cc_exec_drain(cc, direct) = drain;
1392 KASSERT(!sq_locked, ("sleepqueue chain still locked"));
1393 cancelled = ((flags & CS_EXECUTING) != 0);
1398 sleepq_release(&cc_exec_waiting(cc, direct));
1400 if ((c->c_iflags & CALLOUT_PENDING) == 0) {
1401 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1402 c, c->c_func, c->c_arg);
1404 * For not scheduled and not executing callout return
1407 if (cc_exec_curr(cc, direct) != c)
1413 c->c_iflags &= ~CALLOUT_PENDING;
1414 c->c_flags &= ~CALLOUT_ACTIVE;
1416 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1417 c, c->c_func, c->c_arg);
1418 if (not_on_a_list == 0) {
1419 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1420 if (cc_exec_next(cc) == c)
1421 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1422 LIST_REMOVE(c, c_links.le);
1424 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1427 callout_cc_del(c, cc);
1433 callout_init(struct callout *c, int mpsafe)
1435 bzero(c, sizeof *c);
1438 c->c_iflags = CALLOUT_RETURNUNLOCKED;
1440 c->c_lock = &Giant.lock_object;
1443 c->c_cpu = timeout_cpu;
1447 _callout_init_lock(struct callout *c, struct lock_object *lock, int flags)
1449 bzero(c, sizeof *c);
1451 KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
1452 ("callout_init_lock: bad flags %d", flags));
1453 KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
1454 ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
1455 KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
1456 (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
1458 c->c_iflags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
1459 c->c_cpu = timeout_cpu;
1462 #ifdef APM_FIXUP_CALLTODO
1464 * Adjust the kernel calltodo timeout list. This routine is used after
1465 * an APM resume to recalculate the calltodo timer list values with the
1466 * number of hz's we have been sleeping. The next hardclock() will detect
1467 * that there are fired timers and run softclock() to execute them.
1469 * Please note, I have not done an exhaustive analysis of what code this
1470 * might break. I am motivated to have my select()'s and alarm()'s that
1471 * have expired during suspend firing upon resume so that the applications
1472 * which set the timer can do the maintanence the timer was for as close
1473 * as possible to the originally intended time. Testing this code for a
1474 * week showed that resuming from a suspend resulted in 22 to 25 timers
1475 * firing, which seemed independent on whether the suspend was 2 hours or
1476 * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu>
1479 adjust_timeout_calltodo(struct timeval *time_change)
1482 unsigned long delta_ticks;
1485 * How many ticks were we asleep?
1486 * (stolen from tvtohz()).
1489 /* Don't do anything */
1490 if (time_change->tv_sec < 0)
1492 else if (time_change->tv_sec <= LONG_MAX / 1000000)
1493 delta_ticks = howmany(time_change->tv_sec * 1000000 +
1494 time_change->tv_usec, tick) + 1;
1495 else if (time_change->tv_sec <= LONG_MAX / hz)
1496 delta_ticks = time_change->tv_sec * hz +
1497 howmany(time_change->tv_usec, tick) + 1;
1499 delta_ticks = LONG_MAX;
1501 if (delta_ticks > INT_MAX)
1502 delta_ticks = INT_MAX;
1505 * Now rip through the timer calltodo list looking for timers
1509 /* don't collide with softclock() */
1511 for (p = calltodo.c_next; p != NULL; p = p->c_next) {
1512 p->c_time -= delta_ticks;
1514 /* Break if the timer had more time on it than delta_ticks */
1518 /* take back the ticks the timer didn't use (p->c_time <= 0) */
1519 delta_ticks = -p->c_time;
1525 #endif /* APM_FIXUP_CALLTODO */
1528 flssbt(sbintime_t sbt)
1531 sbt += (uint64_t)sbt >> 1;
1532 if (sizeof(long) >= sizeof(sbintime_t))
1535 return (flsl(((uint64_t)sbt) >> 32) + 32);
1540 * Dump immediate statistic snapshot of the scheduled callouts.
1543 sysctl_kern_callout_stat(SYSCTL_HANDLER_ARGS)
1545 struct callout *tmp;
1546 struct callout_cpu *cc;
1547 struct callout_list *sc;
1548 sbintime_t maxpr, maxt, medpr, medt, now, spr, st, t;
1549 int ct[64], cpr[64], ccpbk[32];
1550 int error, val, i, count, tcum, pcum, maxc, c, medc;
1556 error = sysctl_handle_int(oidp, &val, 0, req);
1557 if (error != 0 || req->newptr == NULL)
1560 st = spr = maxt = maxpr = 0;
1561 bzero(ccpbk, sizeof(ccpbk));
1562 bzero(ct, sizeof(ct));
1563 bzero(cpr, sizeof(cpr));
1569 cc = CC_CPU(timeout_cpu);
1572 for (i = 0; i < callwheelsize; i++) {
1573 sc = &cc->cc_callwheel[i];
1575 LIST_FOREACH(tmp, sc, c_links.le) {
1577 t = tmp->c_time - now;
1581 spr += tmp->c_precision / SBT_1US;
1584 if (tmp->c_precision > maxpr)
1585 maxpr = tmp->c_precision;
1587 cpr[flssbt(tmp->c_precision)]++;
1591 ccpbk[fls(c + c / 2)]++;
1599 for (i = 0, tcum = 0; i < 64 && tcum < count / 2; i++)
1601 medt = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1602 for (i = 0, pcum = 0; i < 64 && pcum < count / 2; i++)
1604 medpr = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1605 for (i = 0, c = 0; i < 32 && c < count / 2; i++)
1607 medc = (i >= 2) ? (1 << (i - 2)) : 0;
1609 printf("Scheduled callouts statistic snapshot:\n");
1610 printf(" Callouts: %6d Buckets: %6d*%-3d Bucket size: 0.%06ds\n",
1611 count, callwheelsize, mp_ncpus, 1000000 >> CC_HASH_SHIFT);
1612 printf(" C/Bk: med %5d avg %6d.%06jd max %6d\n",
1614 count / callwheelsize / mp_ncpus,
1615 (uint64_t)count * 1000000 / callwheelsize / mp_ncpus % 1000000,
1617 printf(" Time: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1618 medt / SBT_1S, (medt & 0xffffffff) * 1000000 >> 32,
1619 (st / count) / 1000000, (st / count) % 1000000,
1620 maxt / SBT_1S, (maxt & 0xffffffff) * 1000000 >> 32);
1621 printf(" Prec: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1622 medpr / SBT_1S, (medpr & 0xffffffff) * 1000000 >> 32,
1623 (spr / count) / 1000000, (spr / count) % 1000000,
1624 maxpr / SBT_1S, (maxpr & 0xffffffff) * 1000000 >> 32);
1625 printf(" Distribution: \tbuckets\t time\t tcum\t"
1627 for (i = 0, tcum = pcum = 0; i < 64; i++) {
1628 if (ct[i] == 0 && cpr[i] == 0)
1630 t = (i != 0) ? (((sbintime_t)1) << (i - 1)) : 0;
1633 printf(" %10jd.%06jds\t 2**%d\t%7d\t%7d\t%7d\t%7d\n",
1634 t / SBT_1S, (t & 0xffffffff) * 1000000 >> 32,
1635 i - 1 - (32 - CC_HASH_SHIFT),
1636 ct[i], tcum, cpr[i], pcum);
1640 SYSCTL_PROC(_kern, OID_AUTO, callout_stat,
1641 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1642 0, 0, sysctl_kern_callout_stat, "I",
1643 "Dump immediate statistic snapshot of the scheduled callouts");
1647 _show_callout(struct callout *c)
1650 db_printf("callout %p\n", c);
1651 #define C_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, c->e);
1652 db_printf(" &c_links = %p\n", &(c->c_links));
1653 C_DB_PRINTF("%" PRId64, c_time);
1654 C_DB_PRINTF("%" PRId64, c_precision);
1655 C_DB_PRINTF("%p", c_arg);
1656 C_DB_PRINTF("%p", c_func);
1657 C_DB_PRINTF("%p", c_lock);
1658 C_DB_PRINTF("%#x", c_flags);
1659 C_DB_PRINTF("%#x", c_iflags);
1660 C_DB_PRINTF("%d", c_cpu);
1664 DB_SHOW_COMMAND(callout, db_show_callout)
1668 db_printf("usage: show callout <struct callout *>\n");
1672 _show_callout((struct callout *)addr);