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>
54 #include <sys/interrupt.h>
55 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/mutex.h>
62 #include <sys/sleepqueue.h>
63 #include <sys/sysctl.h>
68 #include <machine/_inttypes.h>
72 #include <machine/cpu.h>
75 #ifndef NO_EVENTTIMERS
76 DPCPU_DECLARE(sbintime_t, hardclocktime);
79 SDT_PROVIDER_DEFINE(callout_execute);
80 SDT_PROBE_DEFINE1(callout_execute, , , callout__start, "struct callout *");
81 SDT_PROBE_DEFINE1(callout_execute, , , callout__end, "struct callout *");
83 #ifdef CALLOUT_PROFILING
85 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
86 "Average number of items examined per softclock call. Units = 1/1000");
87 static int avg_gcalls;
88 SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
89 "Average number of Giant callouts made per softclock call. Units = 1/1000");
90 static int avg_lockcalls;
91 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0,
92 "Average number of lock callouts made per softclock call. Units = 1/1000");
93 static int avg_mpcalls;
94 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
95 "Average number of MP callouts made per softclock call. Units = 1/1000");
96 static int avg_depth_dir;
97 SYSCTL_INT(_debug, OID_AUTO, to_avg_depth_dir, CTLFLAG_RD, &avg_depth_dir, 0,
98 "Average number of direct callouts examined per callout_process call. "
100 static int avg_lockcalls_dir;
101 SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls_dir, CTLFLAG_RD,
102 &avg_lockcalls_dir, 0, "Average number of lock direct callouts made per "
103 "callout_process call. Units = 1/1000");
104 static int avg_mpcalls_dir;
105 SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls_dir, CTLFLAG_RD, &avg_mpcalls_dir,
106 0, "Average number of MP direct callouts made per callout_process call. "
111 SYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &ncallout, 0,
112 "Number of entries in callwheel and size of timeout() preallocation");
115 static int pin_default_swi = 1;
116 static int pin_pcpu_swi = 1;
118 static int pin_default_swi = 0;
119 static int pin_pcpu_swi = 0;
122 SYSCTL_INT(_kern, OID_AUTO, pin_default_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_default_swi,
123 0, "Pin the default (non-per-cpu) swi (shared with PCPU 0 swi)");
124 SYSCTL_INT(_kern, OID_AUTO, pin_pcpu_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_pcpu_swi,
125 0, "Pin the per-CPU swis (except PCPU 0, which is also default");
129 * allocate more timeout table slots when table overflows.
131 u_int callwheelsize, callwheelmask;
134 * The callout cpu exec entities represent informations necessary for
135 * describing the state of callouts currently running on the CPU and the ones
136 * necessary for migrating callouts to the new callout cpu. In particular,
137 * the first entry of the array cc_exec_entity holds informations for callout
138 * running in SWI thread context, while the second one holds informations
139 * for callout running directly from hardware interrupt context.
140 * The cached informations are very important for deferring migration when
141 * the migrating callout is already running.
144 struct callout *cc_curr;
145 void (*cc_drain)(void *);
147 void (*ce_migration_func)(void *);
148 void *ce_migration_arg;
149 int ce_migration_cpu;
150 sbintime_t ce_migration_time;
151 sbintime_t ce_migration_prec;
158 * There is one struct callout_cpu per cpu, holding all relevant
159 * state for the callout processing thread on the individual CPU.
162 struct mtx_padalign cc_lock;
163 struct cc_exec cc_exec_entity[2];
164 struct callout *cc_next;
165 struct callout *cc_callout;
166 struct callout_list *cc_callwheel;
167 struct callout_tailq cc_expireq;
168 struct callout_slist cc_callfree;
169 sbintime_t cc_firstevent;
170 sbintime_t cc_lastscan;
174 char cc_ktr_event_name[20];
177 #define callout_migrating(c) ((c)->c_iflags & CALLOUT_DFRMIGRATION)
179 #define cc_exec_curr(cc, dir) cc->cc_exec_entity[dir].cc_curr
180 #define cc_exec_drain(cc, dir) cc->cc_exec_entity[dir].cc_drain
181 #define cc_exec_next(cc) cc->cc_next
182 #define cc_exec_cancel(cc, dir) cc->cc_exec_entity[dir].cc_cancel
183 #define cc_exec_waiting(cc, dir) cc->cc_exec_entity[dir].cc_waiting
185 #define cc_migration_func(cc, dir) cc->cc_exec_entity[dir].ce_migration_func
186 #define cc_migration_arg(cc, dir) cc->cc_exec_entity[dir].ce_migration_arg
187 #define cc_migration_cpu(cc, dir) cc->cc_exec_entity[dir].ce_migration_cpu
188 #define cc_migration_time(cc, dir) cc->cc_exec_entity[dir].ce_migration_time
189 #define cc_migration_prec(cc, dir) cc->cc_exec_entity[dir].ce_migration_prec
191 struct callout_cpu cc_cpu[MAXCPU];
192 #define CPUBLOCK MAXCPU
193 #define CC_CPU(cpu) (&cc_cpu[(cpu)])
194 #define CC_SELF() CC_CPU(PCPU_GET(cpuid))
196 struct callout_cpu cc_cpu;
197 #define CC_CPU(cpu) &cc_cpu
198 #define CC_SELF() &cc_cpu
200 #define CC_LOCK(cc) mtx_lock_spin(&(cc)->cc_lock)
201 #define CC_UNLOCK(cc) mtx_unlock_spin(&(cc)->cc_lock)
202 #define CC_LOCK_ASSERT(cc) mtx_assert(&(cc)->cc_lock, MA_OWNED)
204 static int timeout_cpu;
206 static void callout_cpu_init(struct callout_cpu *cc, int cpu);
207 static void softclock_call_cc(struct callout *c, struct callout_cpu *cc,
208 #ifdef CALLOUT_PROFILING
209 int *mpcalls, int *lockcalls, int *gcalls,
213 static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
217 * cc_curr - If a callout is in progress, it is cc_curr.
218 * If cc_curr is non-NULL, threads waiting in
219 * callout_drain() will be woken up as soon as the
220 * relevant callout completes.
221 * cc_cancel - Changing to 1 with both callout_lock and cc_lock held
222 * guarantees that the current callout will not run.
223 * The softclock() function sets this to 0 before it
224 * drops callout_lock to acquire c_lock, and it calls
225 * the handler only if curr_cancelled is still 0 after
226 * cc_lock is successfully acquired.
227 * cc_waiting - If a thread is waiting in callout_drain(), then
228 * callout_wait is nonzero. Set only when
229 * cc_curr is non-NULL.
233 * Resets the execution entity tied to a specific callout cpu.
236 cc_cce_cleanup(struct callout_cpu *cc, int direct)
239 cc_exec_curr(cc, direct) = NULL;
240 cc_exec_cancel(cc, direct) = false;
241 cc_exec_waiting(cc, direct) = false;
243 cc_migration_cpu(cc, direct) = CPUBLOCK;
244 cc_migration_time(cc, direct) = 0;
245 cc_migration_prec(cc, direct) = 0;
246 cc_migration_func(cc, direct) = NULL;
247 cc_migration_arg(cc, direct) = NULL;
252 * Checks if migration is requested by a specific callout cpu.
255 cc_cce_migrating(struct callout_cpu *cc, int direct)
259 return (cc_migration_cpu(cc, direct) != CPUBLOCK);
266 * Kernel low level callwheel initialization
267 * called on the BSP during kernel startup.
270 callout_callwheel_init(void *dummy)
272 struct callout_cpu *cc;
275 * Calculate the size of the callout wheel and the preallocated
276 * timeout() structures.
277 * XXX: Clip callout to result of previous function of maxusers
278 * maximum 384. This is still huge, but acceptable.
280 memset(CC_CPU(curcpu), 0, sizeof(cc_cpu));
281 ncallout = imin(16 + maxproc + maxfiles, 18508);
282 TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
285 * Calculate callout wheel size, should be next power of two higher
288 callwheelsize = 1 << fls(ncallout);
289 callwheelmask = callwheelsize - 1;
292 * Fetch whether we're pinning the swi's or not.
294 TUNABLE_INT_FETCH("kern.pin_default_swi", &pin_default_swi);
295 TUNABLE_INT_FETCH("kern.pin_pcpu_swi", &pin_pcpu_swi);
298 * Only BSP handles timeout(9) and receives a preallocation.
300 * XXX: Once all timeout(9) consumers are converted this can
303 timeout_cpu = PCPU_GET(cpuid);
304 cc = CC_CPU(timeout_cpu);
305 cc->cc_callout = malloc(ncallout * sizeof(struct callout),
306 M_CALLOUT, M_WAITOK);
307 callout_cpu_init(cc, timeout_cpu);
309 SYSINIT(callwheel_init, SI_SUB_CPU, SI_ORDER_ANY, callout_callwheel_init, NULL);
312 * Initialize the per-cpu callout structures.
315 callout_cpu_init(struct callout_cpu *cc, int cpu)
320 mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
321 SLIST_INIT(&cc->cc_callfree);
323 cc->cc_callwheel = malloc(sizeof(struct callout_list) * callwheelsize,
324 M_CALLOUT, M_WAITOK);
325 for (i = 0; i < callwheelsize; i++)
326 LIST_INIT(&cc->cc_callwheel[i]);
327 TAILQ_INIT(&cc->cc_expireq);
328 cc->cc_firstevent = SBT_MAX;
329 for (i = 0; i < 2; i++)
330 cc_cce_cleanup(cc, i);
331 snprintf(cc->cc_ktr_event_name, sizeof(cc->cc_ktr_event_name),
332 "callwheel cpu %d", cpu);
333 if (cc->cc_callout == NULL) /* Only BSP handles timeout(9) */
335 for (i = 0; i < ncallout; i++) {
336 c = &cc->cc_callout[i];
338 c->c_iflags = CALLOUT_LOCAL_ALLOC;
339 SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
345 * Switches the cpu tied to a specific callout.
346 * The function expects a locked incoming callout cpu and returns with
347 * locked outcoming callout cpu.
349 static struct callout_cpu *
350 callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu)
352 struct callout_cpu *new_cc;
354 MPASS(c != NULL && cc != NULL);
358 * Avoid interrupts and preemption firing after the callout cpu
359 * is blocked in order to avoid deadlocks as the new thread
360 * may be willing to acquire the callout cpu lock.
365 new_cc = CC_CPU(new_cpu);
374 * Start standard softclock thread.
377 start_softclock(void *dummy)
379 struct callout_cpu *cc;
380 char name[MAXCOMLEN];
383 struct intr_event *ie;
386 cc = CC_CPU(timeout_cpu);
387 snprintf(name, sizeof(name), "clock (%d)", timeout_cpu);
388 if (swi_add(&clk_intr_event, name, softclock, cc, SWI_CLOCK,
389 INTR_MPSAFE, &cc->cc_cookie))
390 panic("died while creating standard software ithreads");
391 if (pin_default_swi &&
392 (intr_event_bind(clk_intr_event, timeout_cpu) != 0)) {
393 printf("%s: timeout clock couldn't be pinned to cpu %d\n",
400 if (cpu == timeout_cpu)
403 cc->cc_callout = NULL; /* Only BSP handles timeout(9). */
404 callout_cpu_init(cc, cpu);
405 snprintf(name, sizeof(name), "clock (%d)", cpu);
407 if (swi_add(&ie, name, softclock, cc, SWI_CLOCK,
408 INTR_MPSAFE, &cc->cc_cookie))
409 panic("died while creating standard software ithreads");
410 if (pin_pcpu_swi && (intr_event_bind(ie, cpu) != 0)) {
411 printf("%s: per-cpu clock couldn't be pinned to "
419 SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
421 #define CC_HASH_SHIFT 8
424 callout_hash(sbintime_t sbt)
427 return (sbt >> (32 - CC_HASH_SHIFT));
431 callout_get_bucket(sbintime_t sbt)
434 return (callout_hash(sbt) & callwheelmask);
438 callout_process(sbintime_t now)
440 struct callout *tmp, *tmpn;
441 struct callout_cpu *cc;
442 struct callout_list *sc;
443 sbintime_t first, last, max, tmp_max;
445 u_int firstb, lastb, nowb;
446 #ifdef CALLOUT_PROFILING
447 int depth_dir = 0, mpcalls_dir = 0, lockcalls_dir = 0;
451 mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
453 /* Compute the buckets of the last scan and present times. */
454 firstb = callout_hash(cc->cc_lastscan);
455 cc->cc_lastscan = now;
456 nowb = callout_hash(now);
458 /* Compute the last bucket and minimum time of the bucket after it. */
460 lookahead = (SBT_1S / 16);
461 else if (nowb - firstb == 1)
462 lookahead = (SBT_1S / 8);
464 lookahead = (SBT_1S / 2);
466 first += (lookahead / 2);
468 last &= (0xffffffffffffffffLLU << (32 - CC_HASH_SHIFT));
469 lastb = callout_hash(last) - 1;
473 * Check if we wrapped around the entire wheel from the last scan.
474 * In case, we need to scan entirely the wheel for pending callouts.
476 if (lastb - firstb >= callwheelsize) {
477 lastb = firstb + callwheelsize - 1;
478 if (nowb - firstb >= callwheelsize)
482 /* Iterate callwheel from firstb to nowb and then up to lastb. */
484 sc = &cc->cc_callwheel[firstb & callwheelmask];
485 tmp = LIST_FIRST(sc);
486 while (tmp != NULL) {
487 /* Run the callout if present time within allowed. */
488 if (tmp->c_time <= now) {
490 * Consumer told us the callout may be run
491 * directly from hardware interrupt context.
493 if (tmp->c_iflags & CALLOUT_DIRECT) {
494 #ifdef CALLOUT_PROFILING
498 LIST_NEXT(tmp, c_links.le);
499 cc->cc_bucket = firstb & callwheelmask;
500 LIST_REMOVE(tmp, c_links.le);
501 softclock_call_cc(tmp, cc,
502 #ifdef CALLOUT_PROFILING
503 &mpcalls_dir, &lockcalls_dir, NULL,
506 tmp = cc_exec_next(cc);
507 cc_exec_next(cc) = NULL;
509 tmpn = LIST_NEXT(tmp, c_links.le);
510 LIST_REMOVE(tmp, c_links.le);
511 TAILQ_INSERT_TAIL(&cc->cc_expireq,
513 tmp->c_iflags |= CALLOUT_PROCESSED;
518 /* Skip events from distant future. */
519 if (tmp->c_time >= max)
522 * Event minimal time is bigger than present maximal
523 * time, so it cannot be aggregated.
525 if (tmp->c_time > last) {
529 /* Update first and last time, respecting this event. */
530 if (tmp->c_time < first)
532 tmp_max = tmp->c_time + tmp->c_precision;
536 tmp = LIST_NEXT(tmp, c_links.le);
538 /* Proceed with the next bucket. */
541 * Stop if we looked after present time and found
542 * some event we can't execute at now.
543 * Stop if we looked far enough into the future.
545 } while (((int)(firstb - lastb)) <= 0);
546 cc->cc_firstevent = last;
547 #ifndef NO_EVENTTIMERS
548 cpu_new_callout(curcpu, last, first);
550 #ifdef CALLOUT_PROFILING
551 avg_depth_dir += (depth_dir * 1000 - avg_depth_dir) >> 8;
552 avg_mpcalls_dir += (mpcalls_dir * 1000 - avg_mpcalls_dir) >> 8;
553 avg_lockcalls_dir += (lockcalls_dir * 1000 - avg_lockcalls_dir) >> 8;
555 mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
557 * swi_sched acquires the thread lock, so we don't want to call it
558 * with cc_lock held; incorrect locking order.
560 if (!TAILQ_EMPTY(&cc->cc_expireq))
561 swi_sched(cc->cc_cookie, 0);
564 static struct callout_cpu *
565 callout_lock(struct callout *c)
567 struct callout_cpu *cc;
573 if (cpu == CPUBLOCK) {
574 while (c->c_cpu == CPUBLOCK)
589 callout_cc_add(struct callout *c, struct callout_cpu *cc,
590 sbintime_t sbt, sbintime_t precision, void (*func)(void *),
591 void *arg, int cpu, int flags)
596 if (sbt < cc->cc_lastscan)
597 sbt = cc->cc_lastscan;
599 c->c_iflags |= CALLOUT_PENDING;
600 c->c_iflags &= ~CALLOUT_PROCESSED;
601 c->c_flags |= CALLOUT_ACTIVE;
602 if (flags & C_DIRECT_EXEC)
603 c->c_iflags |= CALLOUT_DIRECT;
606 c->c_precision = precision;
607 bucket = callout_get_bucket(c->c_time);
608 CTR3(KTR_CALLOUT, "precision set for %p: %d.%08x",
609 c, (int)(c->c_precision >> 32),
610 (u_int)(c->c_precision & 0xffffffff));
611 LIST_INSERT_HEAD(&cc->cc_callwheel[bucket], c, c_links.le);
612 if (cc->cc_bucket == bucket)
613 cc_exec_next(cc) = c;
614 #ifndef NO_EVENTTIMERS
616 * Inform the eventtimers(4) subsystem there's a new callout
617 * that has been inserted, but only if really required.
619 if (SBT_MAX - c->c_time < c->c_precision)
620 c->c_precision = SBT_MAX - c->c_time;
621 sbt = c->c_time + c->c_precision;
622 if (sbt < cc->cc_firstevent) {
623 cc->cc_firstevent = sbt;
624 cpu_new_callout(cpu, sbt, c->c_time);
630 callout_cc_del(struct callout *c, struct callout_cpu *cc)
633 if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) == 0)
636 SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
640 softclock_call_cc(struct callout *c, struct callout_cpu *cc,
641 #ifdef CALLOUT_PROFILING
642 int *mpcalls, int *lockcalls, int *gcalls,
646 struct rm_priotracker tracker;
647 void (*c_func)(void *);
649 struct lock_class *class;
650 struct lock_object *c_lock;
651 uintptr_t lock_status;
654 struct callout_cpu *new_cc;
655 void (*new_func)(void *);
658 sbintime_t new_prec, new_time;
660 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
661 sbintime_t sbt1, sbt2;
663 static sbintime_t maxdt = 2 * SBT_1MS; /* 2 msec */
664 static timeout_t *lastfunc;
667 KASSERT((c->c_iflags & CALLOUT_PENDING) == CALLOUT_PENDING,
668 ("softclock_call_cc: pend %p %x", c, c->c_iflags));
669 KASSERT((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE,
670 ("softclock_call_cc: act %p %x", c, c->c_flags));
671 class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL;
673 if (c->c_flags & CALLOUT_SHAREDLOCK) {
674 if (class == &lock_class_rm)
675 lock_status = (uintptr_t)&tracker;
682 c_iflags = c->c_iflags;
683 if (c->c_iflags & CALLOUT_LOCAL_ALLOC)
684 c->c_iflags = CALLOUT_LOCAL_ALLOC;
686 c->c_iflags &= ~CALLOUT_PENDING;
688 cc_exec_curr(cc, direct) = c;
689 cc_exec_cancel(cc, direct) = false;
690 cc_exec_drain(cc, direct) = NULL;
692 if (c_lock != NULL) {
693 class->lc_lock(c_lock, lock_status);
695 * The callout may have been cancelled
696 * while we switched locks.
698 if (cc_exec_cancel(cc, direct)) {
699 class->lc_unlock(c_lock);
702 /* The callout cannot be stopped now. */
703 cc_exec_cancel(cc, direct) = true;
704 if (c_lock == &Giant.lock_object) {
705 #ifdef CALLOUT_PROFILING
708 CTR3(KTR_CALLOUT, "callout giant %p func %p arg %p",
711 #ifdef CALLOUT_PROFILING
714 CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p",
718 #ifdef CALLOUT_PROFILING
721 CTR3(KTR_CALLOUT, "callout %p func %p arg %p",
724 KTR_STATE3(KTR_SCHED, "callout", cc->cc_ktr_event_name, "running",
725 "func:%p", c_func, "arg:%p", c_arg, "direct:%d", direct);
726 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
729 THREAD_NO_SLEEPING();
730 SDT_PROBE1(callout_execute, , , callout__start, c);
732 SDT_PROBE1(callout_execute, , , callout__end, c);
733 THREAD_SLEEPING_OK();
734 #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
738 if (lastfunc != c_func || sbt2 > maxdt * 2) {
741 "Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
742 c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec);
748 KTR_STATE0(KTR_SCHED, "callout", cc->cc_ktr_event_name, "idle");
749 CTR1(KTR_CALLOUT, "callout %p finished", c);
750 if ((c_iflags & CALLOUT_RETURNUNLOCKED) == 0)
751 class->lc_unlock(c_lock);
754 KASSERT(cc_exec_curr(cc, direct) == c, ("mishandled cc_curr"));
755 cc_exec_curr(cc, direct) = NULL;
756 if (cc_exec_drain(cc, direct)) {
757 void (*drain)(void *);
759 drain = cc_exec_drain(cc, direct);
760 cc_exec_drain(cc, direct) = NULL;
765 if (cc_exec_waiting(cc, direct)) {
767 * There is someone waiting for the
768 * callout to complete.
769 * If the callout was scheduled for
770 * migration just cancel it.
772 if (cc_cce_migrating(cc, direct)) {
773 cc_cce_cleanup(cc, direct);
776 * It should be assert here that the callout is not
777 * destroyed but that is not easy.
779 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
781 cc_exec_waiting(cc, direct) = false;
783 wakeup(&cc_exec_waiting(cc, direct));
785 } else if (cc_cce_migrating(cc, direct)) {
786 KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0,
787 ("Migrating legacy callout %p", c));
790 * If the callout was scheduled for
791 * migration just perform it now.
793 new_cpu = cc_migration_cpu(cc, direct);
794 new_time = cc_migration_time(cc, direct);
795 new_prec = cc_migration_prec(cc, direct);
796 new_func = cc_migration_func(cc, direct);
797 new_arg = cc_migration_arg(cc, direct);
798 cc_cce_cleanup(cc, direct);
801 * It should be assert here that the callout is not destroyed
802 * but that is not easy.
804 * As first thing, handle deferred callout stops.
806 if (!callout_migrating(c)) {
808 "deferred cancelled %p func %p arg %p",
809 c, new_func, new_arg);
810 callout_cc_del(c, cc);
813 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
815 new_cc = callout_cpu_switch(c, cc, new_cpu);
816 flags = (direct) ? C_DIRECT_EXEC : 0;
817 callout_cc_add(c, new_cc, new_time, new_prec, new_func,
818 new_arg, new_cpu, flags);
822 panic("migration should not happen");
826 * If the current callout is locally allocated (from
827 * timeout(9)) then put it on the freelist.
829 * Note: we need to check the cached copy of c_iflags because
830 * if it was not local, then it's not safe to deref the
833 KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0 ||
834 c->c_iflags == CALLOUT_LOCAL_ALLOC,
835 ("corrupted callout"));
836 if (c_iflags & CALLOUT_LOCAL_ALLOC)
837 callout_cc_del(c, cc);
841 * The callout mechanism is based on the work of Adam M. Costello and
842 * George Varghese, published in a technical report entitled "Redesigning
843 * the BSD Callout and Timer Facilities" and modified slightly for inclusion
844 * in FreeBSD by Justin T. Gibbs. The original work on the data structures
845 * used in this implementation was published by G. Varghese and T. Lauck in
846 * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
847 * the Efficient Implementation of a Timer Facility" in the Proceedings of
848 * the 11th ACM Annual Symposium on Operating Systems Principles,
849 * Austin, Texas Nov 1987.
853 * Software (low priority) clock interrupt.
854 * Run periodic events from timeout queue.
859 struct callout_cpu *cc;
861 #ifdef CALLOUT_PROFILING
862 int depth = 0, gcalls = 0, lockcalls = 0, mpcalls = 0;
865 cc = (struct callout_cpu *)arg;
867 while ((c = TAILQ_FIRST(&cc->cc_expireq)) != NULL) {
868 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
869 softclock_call_cc(c, cc,
870 #ifdef CALLOUT_PROFILING
871 &mpcalls, &lockcalls, &gcalls,
874 #ifdef CALLOUT_PROFILING
878 #ifdef CALLOUT_PROFILING
879 avg_depth += (depth * 1000 - avg_depth) >> 8;
880 avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
881 avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
882 avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
889 * Execute a function after a specified length of time.
892 * Cancel previous timeout function call.
894 * callout_handle_init --
895 * Initialize a handle so that using it with untimeout is benign.
897 * See AT&T BCI Driver Reference Manual for specification. This
898 * implementation differs from that one in that although an
899 * identification value is returned from timeout, the original
900 * arguments to timeout as well as the identifier are used to
901 * identify entries for untimeout.
903 struct callout_handle
904 timeout(timeout_t *ftn, void *arg, int to_ticks)
906 struct callout_cpu *cc;
908 struct callout_handle handle;
910 cc = CC_CPU(timeout_cpu);
912 /* Fill in the next free callout structure. */
913 new = SLIST_FIRST(&cc->cc_callfree);
915 /* XXX Attempt to malloc first */
916 panic("timeout table full");
917 SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle);
918 callout_reset(new, to_ticks, ftn, arg);
919 handle.callout = new;
926 untimeout(timeout_t *ftn, void *arg, struct callout_handle handle)
928 struct callout_cpu *cc;
931 * Check for a handle that was initialized
932 * by callout_handle_init, but never used
933 * for a real timeout.
935 if (handle.callout == NULL)
938 cc = callout_lock(handle.callout);
939 if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
940 callout_stop(handle.callout);
945 callout_handle_init(struct callout_handle *handle)
947 handle->callout = NULL;
951 callout_when(sbintime_t sbt, sbintime_t precision, int flags,
952 sbintime_t *res, sbintime_t *prec_res)
954 sbintime_t to_sbt, to_pr;
956 if ((flags & (C_ABSOLUTE | C_PRECALC)) != 0) {
958 *prec_res = precision;
961 if ((flags & C_HARDCLOCK) != 0 && sbt < tick_sbt)
963 if ((flags & C_HARDCLOCK) != 0 ||
964 #ifdef NO_EVENTTIMERS
965 sbt >= sbt_timethreshold) {
966 to_sbt = getsbinuptime();
968 /* Add safety belt for the case of hz > 1000. */
969 to_sbt += tc_tick_sbt - tick_sbt;
971 sbt >= sbt_tickthreshold) {
973 * Obtain the time of the last hardclock() call on
974 * this CPU directly from the kern_clocksource.c.
975 * This value is per-CPU, but it is equal for all
979 to_sbt = DPCPU_GET(hardclocktime);
982 to_sbt = DPCPU_GET(hardclocktime);
986 if (cold && to_sbt == 0)
987 to_sbt = sbinuptime();
988 if ((flags & C_HARDCLOCK) == 0)
991 to_sbt = sbinuptime();
992 if (SBT_MAX - to_sbt < sbt)
997 to_pr = ((C_PRELGET(flags) < 0) ? sbt >> tc_precexp :
998 sbt >> C_PRELGET(flags));
999 *prec_res = to_pr > precision ? to_pr : precision;
1003 * New interface; clients allocate their own callout structures.
1005 * callout_reset() - establish or change a timeout
1006 * callout_stop() - disestablish a timeout
1007 * callout_init() - initialize a callout structure so that it can
1008 * safely be passed to callout_reset() and callout_stop()
1010 * <sys/callout.h> defines three convenience macros:
1012 * callout_active() - returns truth if callout has not been stopped,
1013 * drained, or deactivated since the last time the callout was
1015 * callout_pending() - returns truth if callout is still waiting for timeout
1016 * callout_deactivate() - marks the callout as having been serviced
1019 callout_reset_sbt_on(struct callout *c, sbintime_t sbt, sbintime_t prec,
1020 void (*ftn)(void *), void *arg, int cpu, int flags)
1022 sbintime_t to_sbt, precision;
1023 struct callout_cpu *cc;
1024 int cancelled, direct;
1030 } else if ((cpu >= MAXCPU) ||
1031 ((CC_CPU(cpu))->cc_inited == 0)) {
1032 /* Invalid CPU spec */
1033 panic("Invalid CPU in callout %d", cpu);
1035 callout_when(sbt, prec, flags, &to_sbt, &precision);
1038 * This flag used to be added by callout_cc_add, but the
1039 * first time you call this we could end up with the
1040 * wrong direct flag if we don't do it before we add.
1042 if (flags & C_DIRECT_EXEC) {
1047 KASSERT(!direct || c->c_lock == NULL,
1048 ("%s: direct callout %p has lock", __func__, c));
1049 cc = callout_lock(c);
1051 * Don't allow migration of pre-allocated callouts lest they
1052 * become unbalanced or handle the case where the user does
1055 if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) ||
1060 if (cc_exec_curr(cc, direct) == c) {
1062 * We're being asked to reschedule a callout which is
1063 * currently in progress. If there is a lock then we
1064 * can cancel the callout if it has not really started.
1066 if (c->c_lock != NULL && !cc_exec_cancel(cc, direct))
1067 cancelled = cc_exec_cancel(cc, direct) = true;
1068 if (cc_exec_waiting(cc, direct) || cc_exec_drain(cc, direct)) {
1070 * Someone has called callout_drain to kill this
1071 * callout. Don't reschedule.
1073 CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
1074 cancelled ? "cancelled" : "failed to cancel",
1075 c, c->c_func, c->c_arg);
1080 if (callout_migrating(c)) {
1082 * This only occurs when a second callout_reset_sbt_on
1083 * is made after a previous one moved it into
1084 * deferred migration (below). Note we do *not* change
1085 * the prev_cpu even though the previous target may
1088 cc_migration_cpu(cc, direct) = cpu;
1089 cc_migration_time(cc, direct) = to_sbt;
1090 cc_migration_prec(cc, direct) = precision;
1091 cc_migration_func(cc, direct) = ftn;
1092 cc_migration_arg(cc, direct) = arg;
1099 if (c->c_iflags & CALLOUT_PENDING) {
1100 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1101 if (cc_exec_next(cc) == c)
1102 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1103 LIST_REMOVE(c, c_links.le);
1105 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1108 c->c_iflags &= ~ CALLOUT_PENDING;
1109 c->c_flags &= ~ CALLOUT_ACTIVE;
1114 * If the callout must migrate try to perform it immediately.
1115 * If the callout is currently running, just defer the migration
1116 * to a more appropriate moment.
1118 if (c->c_cpu != cpu) {
1119 if (cc_exec_curr(cc, direct) == c) {
1121 * Pending will have been removed since we are
1122 * actually executing the callout on another
1123 * CPU. That callout should be waiting on the
1124 * lock the caller holds. If we set both
1125 * active/and/pending after we return and the
1126 * lock on the executing callout proceeds, it
1127 * will then see pending is true and return.
1128 * At the return from the actual callout execution
1129 * the migration will occur in softclock_call_cc
1130 * and this new callout will be placed on the
1131 * new CPU via a call to callout_cpu_switch() which
1132 * will get the lock on the right CPU followed
1133 * by a call callout_cc_add() which will add it there.
1134 * (see above in softclock_call_cc()).
1136 cc_migration_cpu(cc, direct) = cpu;
1137 cc_migration_time(cc, direct) = to_sbt;
1138 cc_migration_prec(cc, direct) = precision;
1139 cc_migration_func(cc, direct) = ftn;
1140 cc_migration_arg(cc, direct) = arg;
1141 c->c_iflags |= (CALLOUT_DFRMIGRATION | CALLOUT_PENDING);
1142 c->c_flags |= CALLOUT_ACTIVE;
1144 "migration of %p func %p arg %p in %d.%08x to %u deferred",
1145 c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1146 (u_int)(to_sbt & 0xffffffff), cpu);
1150 cc = callout_cpu_switch(c, cc, cpu);
1154 callout_cc_add(c, cc, to_sbt, precision, ftn, arg, cpu, flags);
1155 CTR6(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d.%08x",
1156 cancelled ? "re" : "", c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1157 (u_int)(to_sbt & 0xffffffff));
1164 * Common idioms that can be optimized in the future.
1167 callout_schedule_on(struct callout *c, int to_ticks, int cpu)
1169 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
1173 callout_schedule(struct callout *c, int to_ticks)
1175 return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
1179 _callout_stop_safe(struct callout *c, int flags, void (*drain)(void *))
1181 struct callout_cpu *cc, *old_cc;
1182 struct lock_class *class;
1183 int direct, sq_locked, use_lock;
1184 int cancelled, not_on_a_list;
1186 if ((flags & CS_DRAIN) != 0)
1187 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, c->c_lock,
1188 "calling %s", __func__);
1191 * Some old subsystems don't hold Giant while running a callout_stop(),
1192 * so just discard this check for the moment.
1194 if ((flags & CS_DRAIN) == 0 && c->c_lock != NULL) {
1195 if (c->c_lock == &Giant.lock_object)
1196 use_lock = mtx_owned(&Giant);
1199 class = LOCK_CLASS(c->c_lock);
1200 class->lc_assert(c->c_lock, LA_XLOCKED);
1204 if (c->c_iflags & CALLOUT_DIRECT) {
1212 cc = callout_lock(c);
1214 if ((c->c_iflags & (CALLOUT_DFRMIGRATION | CALLOUT_PENDING)) ==
1215 (CALLOUT_DFRMIGRATION | CALLOUT_PENDING) &&
1216 ((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE)) {
1218 * Special case where this slipped in while we
1219 * were migrating *as* the callout is about to
1220 * execute. The caller probably holds the lock
1221 * the callout wants.
1223 * Get rid of the migration first. Then set
1224 * the flag that tells this code *not* to
1225 * try to remove it from any lists (its not
1226 * on one yet). When the callout wheel runs,
1227 * it will ignore this callout.
1229 c->c_iflags &= ~CALLOUT_PENDING;
1230 c->c_flags &= ~CALLOUT_ACTIVE;
1237 * If the callout was migrating while the callout cpu lock was
1238 * dropped, just drop the sleepqueue lock and check the states
1241 if (sq_locked != 0 && cc != old_cc) {
1244 sleepq_release(&cc_exec_waiting(old_cc, direct));
1249 panic("migration should not happen");
1254 * If the callout is running, try to stop it or drain it.
1256 if (cc_exec_curr(cc, direct) == c) {
1258 * Succeed we to stop it or not, we must clear the
1259 * active flag - this is what API users expect. If we're
1260 * draining and the callout is currently executing, first wait
1261 * until it finishes.
1263 if ((flags & CS_DRAIN) == 0)
1264 c->c_flags &= ~CALLOUT_ACTIVE;
1266 if ((flags & CS_DRAIN) != 0) {
1268 * The current callout is running (or just
1269 * about to run) and blocking is allowed, so
1270 * just wait for the current invocation to
1273 while (cc_exec_curr(cc, direct) == c) {
1275 * Use direct calls to sleepqueue interface
1276 * instead of cv/msleep in order to avoid
1277 * a LOR between cc_lock and sleepqueue
1278 * chain spinlocks. This piece of code
1279 * emulates a msleep_spin() call actually.
1281 * If we already have the sleepqueue chain
1282 * locked, then we can safely block. If we
1283 * don't already have it locked, however,
1284 * we have to drop the cc_lock to lock
1285 * it. This opens several races, so we
1286 * restart at the beginning once we have
1287 * both locks. If nothing has changed, then
1288 * we will end up back here with sq_locked
1294 &cc_exec_waiting(cc, direct));
1301 * Migration could be cancelled here, but
1302 * as long as it is still not sure when it
1303 * will be packed up, just let softclock()
1306 cc_exec_waiting(cc, direct) = true;
1310 &cc_exec_waiting(cc, direct),
1311 &cc->cc_lock.lock_object, "codrain",
1314 &cc_exec_waiting(cc, direct),
1319 /* Reacquire locks previously released. */
1323 c->c_flags &= ~CALLOUT_ACTIVE;
1324 } else if (use_lock &&
1325 !cc_exec_cancel(cc, direct) && (drain == NULL)) {
1328 * The current callout is waiting for its
1329 * lock which we hold. Cancel the callout
1330 * and return. After our caller drops the
1331 * lock, the callout will be skipped in
1332 * softclock(). This *only* works with a
1333 * callout_stop() *not* callout_drain() or
1334 * callout_async_drain().
1336 cc_exec_cancel(cc, direct) = true;
1337 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1338 c, c->c_func, c->c_arg);
1339 KASSERT(!cc_cce_migrating(cc, direct),
1340 ("callout wrongly scheduled for migration"));
1341 if (callout_migrating(c)) {
1342 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1344 cc_migration_cpu(cc, direct) = CPUBLOCK;
1345 cc_migration_time(cc, direct) = 0;
1346 cc_migration_prec(cc, direct) = 0;
1347 cc_migration_func(cc, direct) = NULL;
1348 cc_migration_arg(cc, direct) = NULL;
1352 KASSERT(!sq_locked, ("sleepqueue chain locked"));
1354 } else if (callout_migrating(c)) {
1356 * The callout is currently being serviced
1357 * and the "next" callout is scheduled at
1358 * its completion with a migration. We remove
1359 * the migration flag so it *won't* get rescheduled,
1360 * but we can't stop the one thats running so
1363 c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1366 * We can't call cc_cce_cleanup here since
1367 * if we do it will remove .ce_curr and
1368 * its still running. This will prevent a
1369 * reschedule of the callout when the
1370 * execution completes.
1372 cc_migration_cpu(cc, direct) = CPUBLOCK;
1373 cc_migration_time(cc, direct) = 0;
1374 cc_migration_prec(cc, direct) = 0;
1375 cc_migration_func(cc, direct) = NULL;
1376 cc_migration_arg(cc, direct) = NULL;
1378 CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p",
1379 c, c->c_func, c->c_arg);
1381 cc_exec_drain(cc, direct) = drain;
1384 return ((flags & CS_EXECUTING) != 0);
1386 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1387 c, c->c_func, c->c_arg);
1389 cc_exec_drain(cc, direct) = drain;
1391 KASSERT(!sq_locked, ("sleepqueue chain still locked"));
1392 cancelled = ((flags & CS_EXECUTING) != 0);
1397 sleepq_release(&cc_exec_waiting(cc, direct));
1399 if ((c->c_iflags & CALLOUT_PENDING) == 0) {
1400 CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1401 c, c->c_func, c->c_arg);
1403 * For not scheduled and not executing callout return
1406 if (cc_exec_curr(cc, direct) != c)
1412 c->c_iflags &= ~CALLOUT_PENDING;
1413 c->c_flags &= ~CALLOUT_ACTIVE;
1415 CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1416 c, c->c_func, c->c_arg);
1417 if (not_on_a_list == 0) {
1418 if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
1419 if (cc_exec_next(cc) == c)
1420 cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1421 LIST_REMOVE(c, c_links.le);
1423 TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1426 callout_cc_del(c, cc);
1432 callout_init(struct callout *c, int mpsafe)
1434 bzero(c, sizeof *c);
1437 c->c_iflags = CALLOUT_RETURNUNLOCKED;
1439 c->c_lock = &Giant.lock_object;
1442 c->c_cpu = timeout_cpu;
1446 _callout_init_lock(struct callout *c, struct lock_object *lock, int flags)
1448 bzero(c, sizeof *c);
1450 KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
1451 ("callout_init_lock: bad flags %d", flags));
1452 KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
1453 ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
1454 KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
1455 (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
1457 c->c_iflags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
1458 c->c_cpu = timeout_cpu;
1461 #ifdef APM_FIXUP_CALLTODO
1463 * Adjust the kernel calltodo timeout list. This routine is used after
1464 * an APM resume to recalculate the calltodo timer list values with the
1465 * number of hz's we have been sleeping. The next hardclock() will detect
1466 * that there are fired timers and run softclock() to execute them.
1468 * Please note, I have not done an exhaustive analysis of what code this
1469 * might break. I am motivated to have my select()'s and alarm()'s that
1470 * have expired during suspend firing upon resume so that the applications
1471 * which set the timer can do the maintanence the timer was for as close
1472 * as possible to the originally intended time. Testing this code for a
1473 * week showed that resuming from a suspend resulted in 22 to 25 timers
1474 * firing, which seemed independent on whether the suspend was 2 hours or
1475 * 2 days. Your milage may vary. - Ken Key <key@cs.utk.edu>
1478 adjust_timeout_calltodo(struct timeval *time_change)
1481 unsigned long delta_ticks;
1484 * How many ticks were we asleep?
1485 * (stolen from tvtohz()).
1488 /* Don't do anything */
1489 if (time_change->tv_sec < 0)
1491 else if (time_change->tv_sec <= LONG_MAX / 1000000)
1492 delta_ticks = howmany(time_change->tv_sec * 1000000 +
1493 time_change->tv_usec, tick) + 1;
1494 else if (time_change->tv_sec <= LONG_MAX / hz)
1495 delta_ticks = time_change->tv_sec * hz +
1496 howmany(time_change->tv_usec, tick) + 1;
1498 delta_ticks = LONG_MAX;
1500 if (delta_ticks > INT_MAX)
1501 delta_ticks = INT_MAX;
1504 * Now rip through the timer calltodo list looking for timers
1508 /* don't collide with softclock() */
1510 for (p = calltodo.c_next; p != NULL; p = p->c_next) {
1511 p->c_time -= delta_ticks;
1513 /* Break if the timer had more time on it than delta_ticks */
1517 /* take back the ticks the timer didn't use (p->c_time <= 0) */
1518 delta_ticks = -p->c_time;
1524 #endif /* APM_FIXUP_CALLTODO */
1527 flssbt(sbintime_t sbt)
1530 sbt += (uint64_t)sbt >> 1;
1531 if (sizeof(long) >= sizeof(sbintime_t))
1534 return (flsl(((uint64_t)sbt) >> 32) + 32);
1539 * Dump immediate statistic snapshot of the scheduled callouts.
1542 sysctl_kern_callout_stat(SYSCTL_HANDLER_ARGS)
1544 struct callout *tmp;
1545 struct callout_cpu *cc;
1546 struct callout_list *sc;
1547 sbintime_t maxpr, maxt, medpr, medt, now, spr, st, t;
1548 int ct[64], cpr[64], ccpbk[32];
1549 int error, val, i, count, tcum, pcum, maxc, c, medc;
1555 error = sysctl_handle_int(oidp, &val, 0, req);
1556 if (error != 0 || req->newptr == NULL)
1559 st = spr = maxt = maxpr = 0;
1560 bzero(ccpbk, sizeof(ccpbk));
1561 bzero(ct, sizeof(ct));
1562 bzero(cpr, sizeof(cpr));
1568 cc = CC_CPU(timeout_cpu);
1571 for (i = 0; i < callwheelsize; i++) {
1572 sc = &cc->cc_callwheel[i];
1574 LIST_FOREACH(tmp, sc, c_links.le) {
1576 t = tmp->c_time - now;
1580 spr += tmp->c_precision / SBT_1US;
1583 if (tmp->c_precision > maxpr)
1584 maxpr = tmp->c_precision;
1586 cpr[flssbt(tmp->c_precision)]++;
1590 ccpbk[fls(c + c / 2)]++;
1598 for (i = 0, tcum = 0; i < 64 && tcum < count / 2; i++)
1600 medt = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1601 for (i = 0, pcum = 0; i < 64 && pcum < count / 2; i++)
1603 medpr = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
1604 for (i = 0, c = 0; i < 32 && c < count / 2; i++)
1606 medc = (i >= 2) ? (1 << (i - 2)) : 0;
1608 printf("Scheduled callouts statistic snapshot:\n");
1609 printf(" Callouts: %6d Buckets: %6d*%-3d Bucket size: 0.%06ds\n",
1610 count, callwheelsize, mp_ncpus, 1000000 >> CC_HASH_SHIFT);
1611 printf(" C/Bk: med %5d avg %6d.%06jd max %6d\n",
1613 count / callwheelsize / mp_ncpus,
1614 (uint64_t)count * 1000000 / callwheelsize / mp_ncpus % 1000000,
1616 printf(" Time: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1617 medt / SBT_1S, (medt & 0xffffffff) * 1000000 >> 32,
1618 (st / count) / 1000000, (st / count) % 1000000,
1619 maxt / SBT_1S, (maxt & 0xffffffff) * 1000000 >> 32);
1620 printf(" Prec: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
1621 medpr / SBT_1S, (medpr & 0xffffffff) * 1000000 >> 32,
1622 (spr / count) / 1000000, (spr / count) % 1000000,
1623 maxpr / SBT_1S, (maxpr & 0xffffffff) * 1000000 >> 32);
1624 printf(" Distribution: \tbuckets\t time\t tcum\t"
1626 for (i = 0, tcum = pcum = 0; i < 64; i++) {
1627 if (ct[i] == 0 && cpr[i] == 0)
1629 t = (i != 0) ? (((sbintime_t)1) << (i - 1)) : 0;
1632 printf(" %10jd.%06jds\t 2**%d\t%7d\t%7d\t%7d\t%7d\n",
1633 t / SBT_1S, (t & 0xffffffff) * 1000000 >> 32,
1634 i - 1 - (32 - CC_HASH_SHIFT),
1635 ct[i], tcum, cpr[i], pcum);
1639 SYSCTL_PROC(_kern, OID_AUTO, callout_stat,
1640 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
1641 0, 0, sysctl_kern_callout_stat, "I",
1642 "Dump immediate statistic snapshot of the scheduled callouts");
1646 _show_callout(struct callout *c)
1649 db_printf("callout %p\n", c);
1650 #define C_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, c->e);
1651 db_printf(" &c_links = %p\n", &(c->c_links));
1652 C_DB_PRINTF("%" PRId64, c_time);
1653 C_DB_PRINTF("%" PRId64, c_precision);
1654 C_DB_PRINTF("%p", c_arg);
1655 C_DB_PRINTF("%p", c_func);
1656 C_DB_PRINTF("%p", c_lock);
1657 C_DB_PRINTF("%#x", c_flags);
1658 C_DB_PRINTF("%#x", c_iflags);
1659 C_DB_PRINTF("%d", c_cpu);
1663 DB_SHOW_COMMAND(callout, db_show_callout)
1667 db_printf("usage: show callout <struct callout *>\n");
1671 _show_callout((struct callout *)addr);