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 <ddb/db_sym.h>
70 #include <machine/_inttypes.h>
74 #include <machine/cpu.h>
77 #ifndef NO_EVENTTIMERS
78 DPCPU_DECLARE(sbintime_t, hardclocktime);
81 SDT_PROVIDER_DEFINE(callout_execute);
82 SDT_PROBE_DEFINE1(callout_execute, , , callout__start, "struct callout *");
83 SDT_PROBE_DEFINE1(callout_execute, , , callout__end, "struct callout *");
85 #ifdef CALLOUT_PROFILING
87 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
88 "Average number of items examined per softclock call. Units = 1/1000");
89 static int avg_gcalls;
90 SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
91 "Average number of Giant callouts made per softclock call. Units = 1/1000");
92 static int avg_lockcalls;
93 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0,
94 "Average number of lock callouts made per softclock call. Units = 1/1000");
95 static int avg_mpcalls;
96 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
97 "Average number of MP callouts made per softclock call. Units = 1/1000");
98 static int avg_depth_dir;
99 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth_dir, CTLFLAG_RD, &avg_depth_dir, 0,
100 "Average number of direct callouts examined per callout_process call. "
102 static int avg_lockcalls_dir;
103 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls_dir, CTLFLAG_RD,
104 &avg_lockcalls_dir, 0, "Average number of lock direct callouts made per "
105 "callout_process call. Units = 1/1000");
106 static int avg_mpcalls_dir;
107 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls_dir, CTLFLAG_RD, &avg_mpcalls_dir,
108 0, "Average number of MP direct callouts made per callout_process call. "
113 SYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &ncallout, 0,
114 "Number of entries in callwheel and size of timeout() preallocation");
117 static int pin_default_swi = 1;
118 static int pin_pcpu_swi = 1;
120 static int pin_default_swi = 0;
121 static int pin_pcpu_swi = 0;
124 SYSCTL_INT(_kern, OID_AUTO, pin_default_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_default_swi,
125 0, "Pin the default (non-per-cpu) swi (shared with PCPU 0 swi)");
126 SYSCTL_INT(_kern, OID_AUTO, pin_pcpu_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_pcpu_swi,
127 0, "Pin the per-CPU swis (except PCPU 0, which is also default");
131 * allocate more timeout table slots when table overflows.
133 static u_int __read_mostly callwheelsize;
134 static u_int __read_mostly callwheelmask;
137 * The callout cpu exec entities represent informations necessary for
138 * describing the state of callouts currently running on the CPU and the ones
139 * necessary for migrating callouts to the new callout cpu. In particular,
140 * the first entry of the array cc_exec_entity holds informations for callout
141 * running in SWI thread context, while the second one holds informations
142 * for callout running directly from hardware interrupt context.
143 * The cached informations are very important for deferring migration when
144 * the migrating callout is already running.
147 struct callout *cc_curr;
148 callout_func_t *cc_drain;
152 callout_func_t *ce_migration_func;
153 void *ce_migration_arg;
154 sbintime_t ce_migration_time;
155 sbintime_t ce_migration_prec;
156 int ce_migration_cpu;
163 * There is one struct callout_cpu per cpu, holding all relevant
164 * state for the callout processing thread on the individual CPU.
167 struct mtx_padalign cc_lock;
168 struct cc_exec cc_exec_entity[2];
169 struct callout *cc_next;
170 struct callout_list *cc_callwheel;
171 struct callout_tailq cc_expireq;
172 sbintime_t cc_firstevent;
173 sbintime_t cc_lastscan;
178 char cc_ktr_event_name[20];
182 #define callout_migrating(c) ((c)->c_iflags & CALLOUT_DFRMIGRATION)
184 #define cc_exec_curr(cc, dir) cc->cc_exec_entity[dir].cc_curr
185 #define cc_exec_last_func(cc, dir) cc->cc_exec_entity[dir].cc_last_func
186 #define cc_exec_last_arg(cc, dir) cc->cc_exec_entity[dir].cc_last_arg
187 #define cc_exec_drain(cc, dir) cc->cc_exec_entity[dir].cc_drain
188 #define cc_exec_next(cc) cc->cc_next
189 #define cc_exec_cancel(cc, dir) cc->cc_exec_entity[dir].cc_cancel
190 #define cc_exec_waiting(cc, dir) cc->cc_exec_entity[dir].cc_waiting
192 #define cc_migration_func(cc, dir) cc->cc_exec_entity[dir].ce_migration_func
193 #define cc_migration_arg(cc, dir) cc->cc_exec_entity[dir].ce_migration_arg
194 #define cc_migration_cpu(cc, dir) cc->cc_exec_entity[dir].ce_migration_cpu
195 #define cc_migration_time(cc, dir) cc->cc_exec_entity[dir].ce_migration_time
196 #define cc_migration_prec(cc, dir) cc->cc_exec_entity[dir].ce_migration_prec
198 struct callout_cpu cc_cpu[MAXCPU];
199 #define CPUBLOCK MAXCPU
200 #define CC_CPU(cpu) (&cc_cpu[(cpu)])
201 #define CC_SELF() CC_CPU(PCPU_GET(cpuid))
203 struct callout_cpu cc_cpu;
204 #define CC_CPU(cpu) &cc_cpu
205 #define CC_SELF() &cc_cpu
207 #define CC_LOCK(cc) mtx_lock_spin(&(cc)->cc_lock)
208 #define CC_UNLOCK(cc) mtx_unlock_spin(&(cc)->cc_lock)
209 #define CC_LOCK_ASSERT(cc) mtx_assert(&(cc)->cc_lock, MA_OWNED)
211 static int __read_mostly cc_default_cpu;
213 static void callout_cpu_init(struct callout_cpu *cc, int cpu);
214 static void softclock_call_cc(struct callout *c, struct callout_cpu *cc,
215 #ifdef CALLOUT_PROFILING
216 int *mpcalls, int *lockcalls, int *gcalls,
220 static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
224 * cc_curr - If a callout is in progress, it is cc_curr.
225 * If cc_curr is non-NULL, threads waiting in
226 * callout_drain() will be woken up as soon as the
227 * relevant callout completes.
228 * cc_cancel - Changing to 1 with both callout_lock and cc_lock held
229 * guarantees that the current callout will not run.
230 * The softclock() function sets this to 0 before it
231 * drops callout_lock to acquire c_lock, and it calls
232 * the handler only if curr_cancelled is still 0 after
233 * cc_lock is successfully acquired.
234 * cc_waiting - If a thread is waiting in callout_drain(), then
235 * callout_wait is nonzero. Set only when
236 * cc_curr is non-NULL.
240 * Resets the execution entity tied to a specific callout cpu.
243 cc_cce_cleanup(struct callout_cpu *cc, int direct)
246 cc_exec_curr(cc, direct) = NULL;
247 cc_exec_cancel(cc, direct) = false;
248 cc_exec_waiting(cc, direct) = false;
250 cc_migration_cpu(cc, direct) = CPUBLOCK;
251 cc_migration_time(cc, direct) = 0;
252 cc_migration_prec(cc, direct) = 0;
253 cc_migration_func(cc, direct) = NULL;
254 cc_migration_arg(cc, direct) = NULL;
259 * Checks if migration is requested by a specific callout cpu.
262 cc_cce_migrating(struct callout_cpu *cc, int direct)
266 return (cc_migration_cpu(cc, direct) != CPUBLOCK);
273 * Kernel low level callwheel initialization
274 * called on the BSP during kernel startup.
277 callout_callwheel_init(void *dummy)
279 struct callout_cpu *cc;
283 * Calculate the size of the callout wheel and the preallocated
284 * timeout() structures.
285 * XXX: Clip callout to result of previous function of maxusers
286 * maximum 384. This is still huge, but acceptable.
288 memset(CC_CPU(curcpu), 0, sizeof(cc_cpu));
289 ncallout = imin(16 + maxproc + maxfiles, 18508);
290 TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
293 * Calculate callout wheel size, should be next power of two higher
296 callwheelsize = 1 << fls(ncallout);
297 callwheelmask = callwheelsize - 1;
300 * Fetch whether we're pinning the swi's or not.
302 TUNABLE_INT_FETCH("kern.pin_default_swi", &pin_default_swi);
303 TUNABLE_INT_FETCH("kern.pin_pcpu_swi", &pin_pcpu_swi);
306 * Initialize callout wheels. The software interrupt threads
309 cc_default_cpu = PCPU_GET(cpuid);
312 callout_cpu_init(cc, cpu);
315 SYSINIT(callwheel_init, SI_SUB_CPU, SI_ORDER_ANY, callout_callwheel_init, NULL);
318 * Initialize the per-cpu callout structures.
321 callout_cpu_init(struct callout_cpu *cc, int cpu)
325 mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
327 cc->cc_callwheel = malloc_domainset(sizeof(struct callout_list) *
328 callwheelsize, M_CALLOUT,
329 DOMAINSET_PREF(pcpu_find(cpu)->pc_domain), M_WAITOK);
330 for (i = 0; i < callwheelsize; i++)
331 LIST_INIT(&cc->cc_callwheel[i]);
332 TAILQ_INIT(&cc->cc_expireq);
333 cc->cc_firstevent = SBT_MAX;
334 for (i = 0; i < 2; i++)
335 cc_cce_cleanup(cc, i);
337 snprintf(cc->cc_ktr_event_name, sizeof(cc->cc_ktr_event_name),
338 "callwheel cpu %d", cpu);
344 * Switches the cpu tied to a specific callout.
345 * The function expects a locked incoming callout cpu and returns with
346 * locked outcoming callout cpu.
348 static struct callout_cpu *
349 callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu)
351 struct callout_cpu *new_cc;
353 MPASS(c != NULL && cc != NULL);
357 * Avoid interrupts and preemption firing after the callout cpu
358 * is blocked in order to avoid deadlocks as the new thread
359 * may be willing to acquire the callout cpu lock.
364 new_cc = CC_CPU(new_cpu);
373 * Start softclock threads.
376 start_softclock(void *dummy)
378 struct callout_cpu *cc;
379 char name[MAXCOMLEN];
382 struct intr_event *ie;
386 snprintf(name, sizeof(name), "clock (%d)", cpu);
388 if (swi_add(&ie, name, softclock, cc, SWI_CLOCK,
389 INTR_MPSAFE, &cc->cc_cookie))
390 panic("died while creating standard software ithreads");
391 if (cpu == cc_default_cpu)
392 pin_swi = pin_default_swi;
394 pin_swi = pin_pcpu_swi;
395 if (pin_swi && (intr_event_bind(ie, cpu) != 0)) {
396 printf("%s: %s clock couldn't be pinned to cpu %d\n",
398 cpu == cc_default_cpu ? "default" : "per-cpu",
403 SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
405 #define CC_HASH_SHIFT 8
408 callout_hash(sbintime_t sbt)
411 return (sbt >> (32 - CC_HASH_SHIFT));
415 callout_get_bucket(sbintime_t sbt)
418 return (callout_hash(sbt) & callwheelmask);
422 callout_process(sbintime_t now)
424 struct callout *tmp, *tmpn;
425 struct callout_cpu *cc;
426 struct callout_list *sc;
427 sbintime_t first, last, max, tmp_max;
429 u_int firstb, lastb, nowb;
430 #ifdef CALLOUT_PROFILING
431 int depth_dir = 0, mpcalls_dir = 0, lockcalls_dir = 0;
435 mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
437 /* Compute the buckets of the last scan and present times. */
438 firstb = callout_hash(cc->cc_lastscan);
439 cc->cc_lastscan = now;
440 nowb = callout_hash(now);
442 /* Compute the last bucket and minimum time of the bucket after it. */
444 lookahead = (SBT_1S / 16);
445 else if (nowb - firstb == 1)
446 lookahead = (SBT_1S / 8);
448 lookahead = (SBT_1S / 2);
450 first += (lookahead / 2);
452 last &= (0xffffffffffffffffLLU << (32 - CC_HASH_SHIFT));
453 lastb = callout_hash(last) - 1;
457 * Check if we wrapped around the entire wheel from the last scan.
458 * In case, we need to scan entirely the wheel for pending callouts.
460 if (lastb - firstb >= callwheelsize) {
461 lastb = firstb + callwheelsize - 1;
462 if (nowb - firstb >= callwheelsize)
466 /* Iterate callwheel from firstb to nowb and then up to lastb. */
468 sc = &cc->cc_callwheel[firstb & callwheelmask];
469 tmp = LIST_FIRST(sc);
470 while (tmp != NULL) {
471 /* Run the callout if present time within allowed. */
472 if (tmp->c_time <= now) {
474 * Consumer told us the callout may be run
475 * directly from hardware interrupt context.
477 if (tmp->c_iflags & CALLOUT_DIRECT) {
478 #ifdef CALLOUT_PROFILING
482 LIST_NEXT(tmp, c_links.le);
483 cc->cc_bucket = firstb & callwheelmask;
484 LIST_REMOVE(tmp, c_links.le);
485 softclock_call_cc(tmp, cc,
486 #ifdef CALLOUT_PROFILING
487 &mpcalls_dir, &lockcalls_dir, NULL,
490 tmp = cc_exec_next(cc);
491 cc_exec_next(cc) = NULL;
493 tmpn = LIST_NEXT(tmp, c_links.le);
494 LIST_REMOVE(tmp, c_links.le);
495 TAILQ_INSERT_TAIL(&cc->cc_expireq,
497 tmp->c_iflags |= CALLOUT_PROCESSED;
502 /* Skip events from distant future. */
503 if (tmp->c_time >= max)
506 * Event minimal time is bigger than present maximal
507 * time, so it cannot be aggregated.
509 if (tmp->c_time > last) {
513 /* Update first and last time, respecting this event. */
514 if (tmp->c_time < first)
516 tmp_max = tmp->c_time + tmp->c_precision;
520 tmp = LIST_NEXT(tmp, c_links.le);
522 /* Proceed with the next bucket. */
525 * Stop if we looked after present time and found
526 * some event we can't execute at now.
527 * Stop if we looked far enough into the future.
529 } while (((int)(firstb - lastb)) <= 0);
530 cc->cc_firstevent = last;
531 #ifndef NO_EVENTTIMERS
532 cpu_new_callout(curcpu, last, first);
534 #ifdef CALLOUT_PROFILING
535 avg_depth_dir += (depth_dir * 1000 - avg_depth_dir) >> 8;
536 avg_mpcalls_dir += (mpcalls_dir * 1000 - avg_mpcalls_dir) >> 8;
537 avg_lockcalls_dir += (lockcalls_dir * 1000 - avg_lockcalls_dir) >> 8;
539 mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
541 * swi_sched acquires the thread lock, so we don't want to call it
542 * with cc_lock held; incorrect locking order.
544 if (!TAILQ_EMPTY(&cc->cc_expireq))
545 swi_sched(cc->cc_cookie, 0);
548 static struct callout_cpu *
549 callout_lock(struct callout *c)
551 struct callout_cpu *cc;
557 if (cpu == CPUBLOCK) {
558 while (c->c_cpu == CPUBLOCK)
573 callout_cc_add(struct callout *c, struct callout_cpu *cc,
574 sbintime_t sbt, sbintime_t precision, void (*func)(void *),
575 void *arg, int cpu, int flags)
580 if (sbt < cc->cc_lastscan)
581 sbt = cc->cc_lastscan;
583 c->c_iflags |= CALLOUT_PENDING;
584 c->c_iflags &= ~CALLOUT_PROCESSED;
585 c->c_flags |= CALLOUT_ACTIVE;
586 if (flags & C_DIRECT_EXEC)
587 c->c_iflags |= CALLOUT_DIRECT;
590 c->c_precision = precision;
591 bucket = callout_get_bucket(c->c_time);
592 CTR3(KTR_CALLOUT, "precision set for %p: %d.%08x",
593 c, (int)(c->c_precision >> 32),
594 (u_int)(c->c_precision & 0xffffffff));
595 LIST_INSERT_HEAD(&cc->cc_callwheel[bucket], c, c_links.le);
596 if (cc->cc_bucket == bucket)
597 cc_exec_next(cc) = c;
598 #ifndef NO_EVENTTIMERS
600 * Inform the eventtimers(4) subsystem there's a new callout
601 * that has been inserted, but only if really required.
603 if (SBT_MAX - c->c_time < c->c_precision)
604 c->c_precision = SBT_MAX - c->c_time;
605 sbt = c->c_time + c->c_precision;
606 if (sbt < cc->cc_firstevent) {
607 cc->cc_firstevent = sbt;
608 cpu_new_callout(cpu, sbt, c->c_time);
614 softclock_call_cc(struct callout *c, struct callout_cpu *cc,
615 #ifdef CALLOUT_PROFILING
616 int *mpcalls, int *lockcalls, int *gcalls,
620 struct rm_priotracker tracker;
621 callout_func_t *c_func, *drain;
623 struct lock_class *class;
624 struct lock_object *c_lock;
625 uintptr_t lock_status;
628 struct callout_cpu *new_cc;
629 callout_func_t *new_func;
632 sbintime_t new_prec, new_time;
634 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
635 sbintime_t sbt1, sbt2;
637 static sbintime_t maxdt = 2 * SBT_1MS; /* 2 msec */
638 static callout_func_t *lastfunc;
641 KASSERT((c->c_iflags & CALLOUT_PENDING) == CALLOUT_PENDING,
642 ("softclock_call_cc: pend %p %x", c, c->c_iflags));
643 KASSERT((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE,
644 ("softclock_call_cc: act %p %x", c, c->c_flags));
645 class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL;
647 if (c->c_flags & CALLOUT_SHAREDLOCK) {
648 if (class == &lock_class_rm)
649 lock_status = (uintptr_t)&tracker;
656 c_iflags = c->c_iflags;
657 c->c_iflags &= ~CALLOUT_PENDING;
659 cc_exec_curr(cc, direct) = c;
660 cc_exec_last_func(cc, direct) = c_func;
661 cc_exec_last_arg(cc, direct) = c_arg;
662 cc_exec_cancel(cc, direct) = false;
663 cc_exec_drain(cc, direct) = NULL;
665 if (c_lock != NULL) {
666 class->lc_lock(c_lock, lock_status);
668 * The callout may have been cancelled
669 * while we switched locks.
671 if (cc_exec_cancel(cc, direct)) {
672 class->lc_unlock(c_lock);
675 /* The callout cannot be stopped now. */
676 cc_exec_cancel(cc, direct) = true;
677 if (c_lock == &Giant.lock_object) {
678 #ifdef CALLOUT_PROFILING
681 CTR3(KTR_CALLOUT, "callout giant %p func %p arg %p",
684 #ifdef CALLOUT_PROFILING
687 CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p",
691 #ifdef CALLOUT_PROFILING
694 CTR3(KTR_CALLOUT, "callout %p func %p arg %p",
697 KTR_STATE3(KTR_SCHED, "callout", cc->cc_ktr_event_name, "running",
698 "func:%p", c_func, "arg:%p", c_arg, "direct:%d", direct);
699 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
702 THREAD_NO_SLEEPING();
703 SDT_PROBE1(callout_execute, , , callout__start, c);
705 SDT_PROBE1(callout_execute, , , callout__end, c);
706 THREAD_SLEEPING_OK();
707 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
711 if (lastfunc != c_func || sbt2 > maxdt * 2) {
714 "Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
715 c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec);
721 KTR_STATE0(KTR_SCHED, "callout", cc->cc_ktr_event_name, "idle");
722 CTR1(KTR_CALLOUT, "callout %p finished", c);
723 if ((c_iflags & CALLOUT_RETURNUNLOCKED) == 0)
724 class->lc_unlock(c_lock);
727 KASSERT(cc_exec_curr(cc, direct) == c, ("mishandled cc_curr"));
728 cc_exec_curr(cc, direct) = NULL;
729 if (cc_exec_drain(cc, direct)) {
730 drain = cc_exec_drain(cc, direct);
731 cc_exec_drain(cc, direct) = NULL;
736 if (cc_exec_waiting(cc, direct)) {
738 * There is someone waiting for the
739 * callout to complete.
740 * If the callout was scheduled for
741 * migration just cancel it.
743 if (cc_cce_migrating(cc, direct)) {
744 cc_cce_cleanup(cc, direct);
747 * It should be assert here that the callout is not
748 * destroyed but that is not easy.
750 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
752 cc_exec_waiting(cc, direct) = false;
754 wakeup(&cc_exec_waiting(cc, direct));
756 } else if (cc_cce_migrating(cc, direct)) {
759 * If the callout was scheduled for
760 * migration just perform it now.
762 new_cpu = cc_migration_cpu(cc, direct);
763 new_time = cc_migration_time(cc, direct);
764 new_prec = cc_migration_prec(cc, direct);
765 new_func = cc_migration_func(cc, direct);
766 new_arg = cc_migration_arg(cc, direct);
767 cc_cce_cleanup(cc, direct);
770 * It should be assert here that the callout is not destroyed
771 * but that is not easy.
773 * As first thing, handle deferred callout stops.
775 if (!callout_migrating(c)) {
777 "deferred cancelled %p func %p arg %p",
778 c, new_func, new_arg);
781 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
783 new_cc = callout_cpu_switch(c, cc, new_cpu);
784 flags = (direct) ? C_DIRECT_EXEC : 0;
785 callout_cc_add(c, new_cc, new_time, new_prec, new_func,
786 new_arg, new_cpu, flags);
790 panic("migration should not happen");
796 * The callout mechanism is based on the work of Adam M. Costello and
797 * George Varghese, published in a technical report entitled "Redesigning
798 * the BSD Callout and Timer Facilities" and modified slightly for inclusion
799 * in FreeBSD by Justin T. Gibbs. The original work on the data structures
800 * used in this implementation was published by G. Varghese and T. Lauck in
801 * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
802 * the Efficient Implementation of a Timer Facility" in the Proceedings of
803 * the 11th ACM Annual Symposium on Operating Systems Principles,
804 * Austin, Texas Nov 1987.
808 * Software (low priority) clock interrupt.
809 * Run periodic events from timeout queue.
814 struct callout_cpu *cc;
816 #ifdef CALLOUT_PROFILING
817 int depth = 0, gcalls = 0, lockcalls = 0, mpcalls = 0;
820 cc = (struct callout_cpu *)arg;
822 while ((c = TAILQ_FIRST(&cc->cc_expireq)) != NULL) {
823 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
824 softclock_call_cc(c, cc,
825 #ifdef CALLOUT_PROFILING
826 &mpcalls, &lockcalls, &gcalls,
829 #ifdef CALLOUT_PROFILING
833 #ifdef CALLOUT_PROFILING
834 avg_depth += (depth * 1000 - avg_depth) >> 8;
835 avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
836 avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
837 avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
843 callout_when(sbintime_t sbt, sbintime_t precision, int flags,
844 sbintime_t *res, sbintime_t *prec_res)
846 sbintime_t to_sbt, to_pr;
848 if ((flags & (C_ABSOLUTE | C_PRECALC)) != 0) {
850 *prec_res = precision;
853 if ((flags & C_HARDCLOCK) != 0 && sbt < tick_sbt)
855 if ((flags & C_HARDCLOCK) != 0 ||
856 #ifdef NO_EVENTTIMERS
857 sbt >= sbt_timethreshold) {
858 to_sbt = getsbinuptime();
860 /* Add safety belt for the case of hz > 1000. */
861 to_sbt += tc_tick_sbt - tick_sbt;
863 sbt >= sbt_tickthreshold) {
865 * Obtain the time of the last hardclock() call on
866 * this CPU directly from the kern_clocksource.c.
867 * This value is per-CPU, but it is equal for all
871 to_sbt = DPCPU_GET(hardclocktime);
874 to_sbt = DPCPU_GET(hardclocktime);
878 if (cold && to_sbt == 0)
879 to_sbt = sbinuptime();
880 if ((flags & C_HARDCLOCK) == 0)
883 to_sbt = sbinuptime();
884 if (SBT_MAX - to_sbt < sbt)
889 to_pr = ((C_PRELGET(flags) < 0) ? sbt >> tc_precexp :
890 sbt >> C_PRELGET(flags));
891 *prec_res = to_pr > precision ? to_pr : precision;
895 * New interface; clients allocate their own callout structures.
897 * callout_reset() - establish or change a timeout
898 * callout_stop() - disestablish a timeout
899 * callout_init() - initialize a callout structure so that it can
900 * safely be passed to callout_reset() and callout_stop()
902 * <sys/callout.h> defines three convenience macros:
904 * callout_active() - returns truth if callout has not been stopped,
905 * drained, or deactivated since the last time the callout was
907 * callout_pending() - returns truth if callout is still waiting for timeout
908 * callout_deactivate() - marks the callout as having been serviced
911 callout_reset_sbt_on(struct callout *c, sbintime_t sbt, sbintime_t prec,
912 callout_func_t *ftn, void *arg, int cpu, int flags)
914 sbintime_t to_sbt, precision;
915 struct callout_cpu *cc;
916 int cancelled, direct;
922 } else if ((cpu >= MAXCPU) ||
923 ((CC_CPU(cpu))->cc_inited == 0)) {
924 /* Invalid CPU spec */
925 panic("Invalid CPU in callout %d", cpu);
927 callout_when(sbt, prec, flags, &to_sbt, &precision);
930 * This flag used to be added by callout_cc_add, but the
931 * first time you call this we could end up with the
932 * wrong direct flag if we don't do it before we add.
934 if (flags & C_DIRECT_EXEC) {
939 KASSERT(!direct || c->c_lock == NULL,
940 ("%s: direct callout %p has lock", __func__, c));
941 cc = callout_lock(c);
943 * Don't allow migration if the user does not care.
949 if (cc_exec_curr(cc, direct) == c) {
951 * We're being asked to reschedule a callout which is
952 * currently in progress. If there is a lock then we
953 * can cancel the callout if it has not really started.
955 if (c->c_lock != NULL && !cc_exec_cancel(cc, direct))
956 cancelled = cc_exec_cancel(cc, direct) = true;
957 if (cc_exec_waiting(cc, direct) || cc_exec_drain(cc, direct)) {
959 * Someone has called callout_drain to kill this
960 * callout. Don't reschedule.
962 CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
963 cancelled ? "cancelled" : "failed to cancel",
964 c, c->c_func, c->c_arg);
969 if (callout_migrating(c)) {
971 * This only occurs when a second callout_reset_sbt_on
972 * is made after a previous one moved it into
973 * deferred migration (below). Note we do *not* change
974 * the prev_cpu even though the previous target may
977 cc_migration_cpu(cc, direct) = cpu;
978 cc_migration_time(cc, direct) = to_sbt;
979 cc_migration_prec(cc, direct) = precision;
980 cc_migration_func(cc, direct) = ftn;
981 cc_migration_arg(cc, direct) = arg;
988 if (c->c_iflags & CALLOUT_PENDING) {
989 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
990 if (cc_exec_next(cc) == c)
991 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
992 LIST_REMOVE(c, c_links.le);
994 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
997 c->c_iflags &= ~ CALLOUT_PENDING;
998 c->c_flags &= ~ CALLOUT_ACTIVE;
1003 * If the callout must migrate try to perform it immediately.
1004 * If the callout is currently running, just defer the migration
1005 * to a more appropriate moment.
1007 if (c->c_cpu != cpu) {
1008 if (cc_exec_curr(cc, direct) == c) {
1010 * Pending will have been removed since we are
1011 * actually executing the callout on another
1012 * CPU. That callout should be waiting on the
1013 * lock the caller holds. If we set both
1014 * active/and/pending after we return and the
1015 * lock on the executing callout proceeds, it
1016 * will then see pending is true and return.
1017 * At the return from the actual callout execution
1018 * the migration will occur in softclock_call_cc
1019 * and this new callout will be placed on the
1020 * new CPU via a call to callout_cpu_switch() which
1021 * will get the lock on the right CPU followed
1022 * by a call callout_cc_add() which will add it there.
1023 * (see above in softclock_call_cc()).
1025 cc_migration_cpu(cc, direct) = cpu;
1026 cc_migration_time(cc, direct) = to_sbt;
1027 cc_migration_prec(cc, direct) = precision;
1028 cc_migration_func(cc, direct) = ftn;
1029 cc_migration_arg(cc, direct) = arg;
1030 c->c_iflags |= (CALLOUT_DFRMIGRATION | CALLOUT_PENDING);
1031 c->c_flags |= CALLOUT_ACTIVE;
1033 "migration of %p func %p arg %p in %d.%08x to %u deferred",
1034 c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1035 (u_int)(to_sbt & 0xffffffff), cpu);
1039 cc = callout_cpu_switch(c, cc, cpu);
1043 callout_cc_add(c, cc, to_sbt, precision, ftn, arg, cpu, flags);
1044 CTR6(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d.%08x",
1045 cancelled ? "re" : "", c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1046 (u_int)(to_sbt & 0xffffffff));
1053 * Common idioms that can be optimized in the future.
1056 callout_schedule_on(struct callout *c, int to_ticks, int cpu)
1058 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
1062 callout_schedule(struct callout *c, int to_ticks)
1064 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
1068 _callout_stop_safe(struct callout *c, int flags, callout_func_t *drain)
1070 struct callout_cpu *cc, *old_cc;
1071 struct lock_class *class;
1072 int direct, sq_locked, use_lock;
1073 int cancelled, not_on_a_list;
1075 if ((flags & CS_DRAIN) != 0)
1076 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, c->c_lock,
1077 "calling %s", __func__);
1080 * Some old subsystems don't hold Giant while running a callout_stop(),
1081 * so just discard this check for the moment.
1083 if ((flags & CS_DRAIN) == 0 && c->c_lock != NULL) {
1084 if (c->c_lock == &Giant.lock_object)
1085 use_lock = mtx_owned(&Giant);
1088 class = LOCK_CLASS(c->c_lock);
1089 class->lc_assert(c->c_lock, LA_XLOCKED);
1093 if (c->c_iflags & CALLOUT_DIRECT) {
1101 cc = callout_lock(c);
1103 if ((c->c_iflags & (CALLOUT_DFRMIGRATION | CALLOUT_PENDING)) ==
1104 (CALLOUT_DFRMIGRATION | CALLOUT_PENDING) &&
1105 ((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE)) {
1107 * Special case where this slipped in while we
1108 * were migrating *as* the callout is about to
1109 * execute. The caller probably holds the lock
1110 * the callout wants.
1112 * Get rid of the migration first. Then set
1113 * the flag that tells this code *not* to
1114 * try to remove it from any lists (its not
1115 * on one yet). When the callout wheel runs,
1116 * it will ignore this callout.
1118 c->c_iflags &= ~CALLOUT_PENDING;
1119 c->c_flags &= ~CALLOUT_ACTIVE;
1126 * If the callout was migrating while the callout cpu lock was
1127 * dropped, just drop the sleepqueue lock and check the states
1130 if (sq_locked != 0 && cc != old_cc) {
1133 sleepq_release(&cc_exec_waiting(old_cc, direct));
1138 panic("migration should not happen");
1143 * If the callout is running, try to stop it or drain it.
1145 if (cc_exec_curr(cc, direct) == c) {
1147 * Succeed we to stop it or not, we must clear the
1148 * active flag - this is what API users expect. If we're
1149 * draining and the callout is currently executing, first wait
1150 * until it finishes.
1152 if ((flags & CS_DRAIN) == 0)
1153 c->c_flags &= ~CALLOUT_ACTIVE;
1155 if ((flags & CS_DRAIN) != 0) {
1157 * The current callout is running (or just
1158 * about to run) and blocking is allowed, so
1159 * just wait for the current invocation to
1162 while (cc_exec_curr(cc, direct) == c) {
1164 * Use direct calls to sleepqueue interface
1165 * instead of cv/msleep in order to avoid
1166 * a LOR between cc_lock and sleepqueue
1167 * chain spinlocks. This piece of code
1168 * emulates a msleep_spin() call actually.
1170 * If we already have the sleepqueue chain
1171 * locked, then we can safely block. If we
1172 * don't already have it locked, however,
1173 * we have to drop the cc_lock to lock
1174 * it. This opens several races, so we
1175 * restart at the beginning once we have
1176 * both locks. If nothing has changed, then
1177 * we will end up back here with sq_locked
1183 &cc_exec_waiting(cc, direct));
1190 * Migration could be cancelled here, but
1191 * as long as it is still not sure when it
1192 * will be packed up, just let softclock()
1195 cc_exec_waiting(cc, direct) = true;
1199 &cc_exec_waiting(cc, direct),
1200 &cc->cc_lock.lock_object, "codrain",
1203 &cc_exec_waiting(cc, direct),
1208 /* Reacquire locks previously released. */
1212 c->c_flags &= ~CALLOUT_ACTIVE;
1213 } else if (use_lock &&
1214 !cc_exec_cancel(cc, direct) && (drain == NULL)) {
1217 * The current callout is waiting for its
1218 * lock which we hold. Cancel the callout
1219 * and return. After our caller drops the
1220 * lock, the callout will be skipped in
1221 * softclock(). This *only* works with a
1222 * callout_stop() *not* callout_drain() or
1223 * callout_async_drain().
1225 cc_exec_cancel(cc, direct) = true;
1226 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1227 c, c->c_func, c->c_arg);
1228 KASSERT(!cc_cce_migrating(cc, direct),
1229 ("callout wrongly scheduled for migration"));
1230 if (callout_migrating(c)) {
1231 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1233 cc_migration_cpu(cc, direct) = CPUBLOCK;
1234 cc_migration_time(cc, direct) = 0;
1235 cc_migration_prec(cc, direct) = 0;
1236 cc_migration_func(cc, direct) = NULL;
1237 cc_migration_arg(cc, direct) = NULL;
1241 KASSERT(!sq_locked, ("sleepqueue chain locked"));
1243 } else if (callout_migrating(c)) {
1245 * The callout is currently being serviced
1246 * and the "next" callout is scheduled at
1247 * its completion with a migration. We remove
1248 * the migration flag so it *won't* get rescheduled,
1249 * but we can't stop the one thats running so
1252 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1255 * We can't call cc_cce_cleanup here since
1256 * if we do it will remove .ce_curr and
1257 * its still running. This will prevent a
1258 * reschedule of the callout when the
1259 * execution completes.
1261 cc_migration_cpu(cc, direct) = CPUBLOCK;
1262 cc_migration_time(cc, direct) = 0;
1263 cc_migration_prec(cc, direct) = 0;
1264 cc_migration_func(cc, direct) = NULL;
1265 cc_migration_arg(cc, direct) = NULL;
1267 CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p",
1268 c, c->c_func, c->c_arg);
1270 cc_exec_drain(cc, direct) = drain;
1273 return ((flags & CS_EXECUTING) != 0);
1275 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1276 c, c->c_func, c->c_arg);
1278 cc_exec_drain(cc, direct) = drain;
1280 KASSERT(!sq_locked, ("sleepqueue chain still locked"));
1281 cancelled = ((flags & CS_EXECUTING) != 0);
1286 sleepq_release(&cc_exec_waiting(cc, direct));
1288 if ((c->c_iflags & CALLOUT_PENDING) == 0) {
1289 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1290 c, c->c_func, c->c_arg);
1292 * For not scheduled and not executing callout return
1295 if (cc_exec_curr(cc, direct) != c)
1301 c->c_iflags &= ~CALLOUT_PENDING;
1302 c->c_flags &= ~CALLOUT_ACTIVE;
1304 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1305 c, c->c_func, c->c_arg);
1306 if (not_on_a_list == 0) {
1307 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1308 if (cc_exec_next(cc) == c)
1309 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1310 LIST_REMOVE(c, c_links.le);
1312 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1320 callout_init(struct callout *c, int mpsafe)
1322 bzero(c, sizeof *c);
1325 c->c_iflags = CALLOUT_RETURNUNLOCKED;
1327 c->c_lock = &Giant.lock_object;
1330 c->c_cpu = cc_default_cpu;
1334 _callout_init_lock(struct callout *c, struct lock_object *lock, int flags)
1336 bzero(c, sizeof *c);
1338 KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
1339 ("callout_init_lock: bad flags %d", flags));
1340 KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
1341 ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
1342 KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
1343 (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
1345 c->c_iflags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
1346 c->c_cpu = cc_default_cpu;
1349 #ifdef APM_FIXUP_CALLTODO
1351 * Adjust the kernel calltodo timeout list. This routine is used after
1352 * an APM resume to recalculate the calltodo timer list values with the
1353 * number of hz's we have been sleeping. The next hardclock() will detect
1354 * that there are fired timers and run softclock() to execute them.
1356 * Please note, I have not done an exhaustive analysis of what code this
1357 * might break. I am motivated to have my select()'s and alarm()'s that
1358 * have expired during suspend firing upon resume so that the applications
1359 * which set the timer can do the maintanence the timer was for as close
1360 * as possible to the originally intended time. Testing this code for a
1361 * week showed that resuming from a suspend resulted in 22 to 25 timers
1362 * firing, which seemed independent on whether the suspend was 2 hours or
1363 * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu>
1366 adjust_timeout_calltodo(struct timeval *time_change)
1369 unsigned long delta_ticks;
1372 * How many ticks were we asleep?
1373 * (stolen from tvtohz()).
1376 /* Don't do anything */
1377 if (time_change->tv_sec < 0)
1379 else if (time_change->tv_sec <= LONG_MAX / 1000000)
1380 delta_ticks = howmany(time_change->tv_sec * 1000000 +
1381 time_change->tv_usec, tick) + 1;
1382 else if (time_change->tv_sec <= LONG_MAX / hz)
1383 delta_ticks = time_change->tv_sec * hz +
1384 howmany(time_change->tv_usec, tick) + 1;
1386 delta_ticks = LONG_MAX;
1388 if (delta_ticks > INT_MAX)
1389 delta_ticks = INT_MAX;
1392 * Now rip through the timer calltodo list looking for timers
1396 /* don't collide with softclock() */
1398 for (p = calltodo.c_next; p != NULL; p = p->c_next) {
1399 p->c_time -= delta_ticks;
1401 /* Break if the timer had more time on it than delta_ticks */
1405 /* take back the ticks the timer didn't use (p->c_time <= 0) */
1406 delta_ticks = -p->c_time;
1412 #endif /* APM_FIXUP_CALLTODO */
1415 flssbt(sbintime_t sbt)
1418 sbt += (uint64_t)sbt >> 1;
1419 if (sizeof(long) >= sizeof(sbintime_t))
1422 return (flsl(((uint64_t)sbt) >> 32) + 32);
1427 * Dump immediate statistic snapshot of the scheduled callouts.
1430 sysctl_kern_callout_stat(SYSCTL_HANDLER_ARGS)
1432 struct callout *tmp;
1433 struct callout_cpu *cc;
1434 struct callout_list *sc;
1435 sbintime_t maxpr, maxt, medpr, medt, now, spr, st, t;
1436 int ct[64], cpr[64], ccpbk[32];
1437 int error, val, i, count, tcum, pcum, maxc, c, medc;
1441 error = sysctl_handle_int(oidp, &val, 0, req);
1442 if (error != 0 || req->newptr == NULL)
1445 st = spr = maxt = maxpr = 0;
1446 bzero(ccpbk, sizeof(ccpbk));
1447 bzero(ct, sizeof(ct));
1448 bzero(cpr, sizeof(cpr));
1453 for (i = 0; i < callwheelsize; i++) {
1454 sc = &cc->cc_callwheel[i];
1456 LIST_FOREACH(tmp, sc, c_links.le) {
1458 t = tmp->c_time - now;
1462 spr += tmp->c_precision / SBT_1US;
1465 if (tmp->c_precision > maxpr)
1466 maxpr = tmp->c_precision;
1468 cpr[flssbt(tmp->c_precision)]++;
1472 ccpbk[fls(c + c / 2)]++;
1478 for (i = 0, tcum = 0; i < 64 && tcum < count / 2; i++)
1480 medt = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1481 for (i = 0, pcum = 0; i < 64 && pcum < count / 2; i++)
1483 medpr = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1484 for (i = 0, c = 0; i < 32 && c < count / 2; i++)
1486 medc = (i >= 2) ? (1 << (i - 2)) : 0;
1488 printf("Scheduled callouts statistic snapshot:\n");
1489 printf(" Callouts: %6d Buckets: %6d*%-3d Bucket size: 0.%06ds\n",
1490 count, callwheelsize, mp_ncpus, 1000000 >> CC_HASH_SHIFT);
1491 printf(" C/Bk: med %5d avg %6d.%06jd max %6d\n",
1493 count / callwheelsize / mp_ncpus,
1494 (uint64_t)count * 1000000 / callwheelsize / mp_ncpus % 1000000,
1496 printf(" Time: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1497 medt / SBT_1S, (medt & 0xffffffff) * 1000000 >> 32,
1498 (st / count) / 1000000, (st / count) % 1000000,
1499 maxt / SBT_1S, (maxt & 0xffffffff) * 1000000 >> 32);
1500 printf(" Prec: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1501 medpr / SBT_1S, (medpr & 0xffffffff) * 1000000 >> 32,
1502 (spr / count) / 1000000, (spr / count) % 1000000,
1503 maxpr / SBT_1S, (maxpr & 0xffffffff) * 1000000 >> 32);
1504 printf(" Distribution: \tbuckets\t time\t tcum\t"
1506 for (i = 0, tcum = pcum = 0; i < 64; i++) {
1507 if (ct[i] == 0 && cpr[i] == 0)
1509 t = (i != 0) ? (((sbintime_t)1) << (i - 1)) : 0;
1512 printf(" %10jd.%06jds\t 2**%d\t%7d\t%7d\t%7d\t%7d\n",
1513 t / SBT_1S, (t & 0xffffffff) * 1000000 >> 32,
1514 i - 1 - (32 - CC_HASH_SHIFT),
1515 ct[i], tcum, cpr[i], pcum);
1519 SYSCTL_PROC(_kern, OID_AUTO, callout_stat,
1520 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1521 0, 0, sysctl_kern_callout_stat, "I",
1522 "Dump immediate statistic snapshot of the scheduled callouts");
1526 _show_callout(struct callout *c)
1529 db_printf("callout %p\n", c);
1530 #define C_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, c->e);
1531 db_printf(" &c_links = %p\n", &(c->c_links));
1532 C_DB_PRINTF("%" PRId64, c_time);
1533 C_DB_PRINTF("%" PRId64, c_precision);
1534 C_DB_PRINTF("%p", c_arg);
1535 C_DB_PRINTF("%p", c_func);
1536 C_DB_PRINTF("%p", c_lock);
1537 C_DB_PRINTF("%#x", c_flags);
1538 C_DB_PRINTF("%#x", c_iflags);
1539 C_DB_PRINTF("%d", c_cpu);
1543 DB_SHOW_COMMAND(callout, db_show_callout)
1547 db_printf("usage: show callout <struct callout *>\n");
1551 _show_callout((struct callout *)addr);
1555 _show_last_callout(int cpu, int direct, const char *dirstr)
1557 struct callout_cpu *cc;
1561 func = cc_exec_last_func(cc, direct);
1562 arg = cc_exec_last_arg(cc, direct);
1563 db_printf("cpu %d last%s callout function: %p ", cpu, dirstr, func);
1564 db_printsym((db_expr_t)func, DB_STGY_ANY);
1565 db_printf("\ncpu %d last%s callout argument: %p\n", cpu, dirstr, arg);
1568 DB_SHOW_COMMAND(callout_last, db_show_callout_last)
1573 if (addr < 0 || addr > mp_maxid || CPU_ABSENT(addr)) {
1574 db_printf("no such cpu: %d\n", (int)addr);
1583 while (cpu <= last) {
1584 if (!CPU_ABSENT(cpu)) {
1585 _show_last_callout(cpu, 0, "");
1586 _show_last_callout(cpu, 1, " direct");