xref: /freebsd/sys/kern/kern_timeout.c (revision 9f3391243b97372aa534bac9f824eb135fa76cd9)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34acc8326dSGarrett Wollman  *	From: @(#)kern_clock.c	8.5 (Berkeley) 1/21/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
405b999a6bSDavide Italiano #include "opt_callout_profiling.h"
413af72c11SBjoern A. Zeeb #include "opt_ddb.h"
425b999a6bSDavide Italiano #if defined(__arm__)
435b999a6bSDavide Italiano #include "opt_timer.h"
445b999a6bSDavide Italiano #endif
45c445c3c7SAdrian Chadd #include "opt_rss.h"
4691dd9aaeSRobert Watson 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
48df8bae1dSRodney W. Grimes #include <sys/systm.h>
498d809d50SJeff Roberson #include <sys/bus.h>
5015b7a470SPoul-Henning Kamp #include <sys/callout.h>
51f8ccf82aSAndre Oppermann #include <sys/file.h>
528d809d50SJeff Roberson #include <sys/interrupt.h>
53df8bae1dSRodney W. Grimes #include <sys/kernel.h>
54ff7ec58aSRobert Watson #include <sys/ktr.h>
55f34fa851SJohn Baldwin #include <sys/lock.h>
568d809d50SJeff Roberson #include <sys/malloc.h>
57cb799bfeSJohn Baldwin #include <sys/mutex.h>
5821f9e816SJohn Baldwin #include <sys/proc.h>
5991dd9aaeSRobert Watson #include <sys/sdt.h>
606a0ce57dSAttilio Rao #include <sys/sleepqueue.h>
6122ee8c4fSPoul-Henning Kamp #include <sys/sysctl.h>
628d809d50SJeff Roberson #include <sys/smp.h>
63df8bae1dSRodney W. Grimes 
643af72c11SBjoern A. Zeeb #ifdef DDB
653af72c11SBjoern A. Zeeb #include <ddb/ddb.h>
663af72c11SBjoern A. Zeeb #include <machine/_inttypes.h>
673af72c11SBjoern A. Zeeb #endif
683af72c11SBjoern A. Zeeb 
691283e9cdSAttilio Rao #ifdef SMP
701283e9cdSAttilio Rao #include <machine/cpu.h>
711283e9cdSAttilio Rao #endif
721283e9cdSAttilio Rao 
735b999a6bSDavide Italiano #ifndef NO_EVENTTIMERS
745b999a6bSDavide Italiano DPCPU_DECLARE(sbintime_t, hardclocktime);
755b999a6bSDavide Italiano #endif
765b999a6bSDavide Italiano 
7791dd9aaeSRobert Watson SDT_PROVIDER_DEFINE(callout_execute);
7836160958SMark Johnston SDT_PROBE_DEFINE1(callout_execute, , , callout__start, "struct callout *");
7936160958SMark Johnston SDT_PROBE_DEFINE1(callout_execute, , , callout__end, "struct callout *");
8091dd9aaeSRobert Watson 
815b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
8222ee8c4fSPoul-Henning Kamp static int avg_depth;
8322ee8c4fSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, to_avg_depth, CTLFLAG_RD, &avg_depth, 0,
8422ee8c4fSPoul-Henning Kamp     "Average number of items examined per softclock call. Units = 1/1000");
8522ee8c4fSPoul-Henning Kamp static int avg_gcalls;
8622ee8c4fSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, to_avg_gcalls, CTLFLAG_RD, &avg_gcalls, 0,
8722ee8c4fSPoul-Henning Kamp     "Average number of Giant callouts made per softclock call. Units = 1/1000");
8864b9ee20SAttilio Rao static int avg_lockcalls;
8964b9ee20SAttilio Rao SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls, CTLFLAG_RD, &avg_lockcalls, 0,
9064b9ee20SAttilio Rao     "Average number of lock callouts made per softclock call. Units = 1/1000");
9122ee8c4fSPoul-Henning Kamp static int avg_mpcalls;
9222ee8c4fSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls, CTLFLAG_RD, &avg_mpcalls, 0,
9322ee8c4fSPoul-Henning Kamp     "Average number of MP callouts made per softclock call. Units = 1/1000");
945b999a6bSDavide Italiano static int avg_depth_dir;
955b999a6bSDavide Italiano SYSCTL_INT(_debug, OID_AUTO, to_avg_depth_dir, CTLFLAG_RD, &avg_depth_dir, 0,
965b999a6bSDavide Italiano     "Average number of direct callouts examined per callout_process call. "
975b999a6bSDavide Italiano     "Units = 1/1000");
985b999a6bSDavide Italiano static int avg_lockcalls_dir;
995b999a6bSDavide Italiano SYSCTL_INT(_debug, OID_AUTO, to_avg_lockcalls_dir, CTLFLAG_RD,
1005b999a6bSDavide Italiano     &avg_lockcalls_dir, 0, "Average number of lock direct callouts made per "
1015b999a6bSDavide Italiano     "callout_process call. Units = 1/1000");
1025b999a6bSDavide Italiano static int avg_mpcalls_dir;
1035b999a6bSDavide Italiano SYSCTL_INT(_debug, OID_AUTO, to_avg_mpcalls_dir, CTLFLAG_RD, &avg_mpcalls_dir,
1045b999a6bSDavide Italiano     0, "Average number of MP direct callouts made per callout_process call. "
1055b999a6bSDavide Italiano     "Units = 1/1000");
1065b999a6bSDavide Italiano #endif
107f8ccf82aSAndre Oppermann 
108f8ccf82aSAndre Oppermann static int ncallout;
109af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &ncallout, 0,
110f8ccf82aSAndre Oppermann     "Number of entries in callwheel and size of timeout() preallocation");
111f8ccf82aSAndre Oppermann 
112c445c3c7SAdrian Chadd #ifdef	RSS
113c445c3c7SAdrian Chadd static int pin_default_swi = 1;
114c445c3c7SAdrian Chadd static int pin_pcpu_swi = 1;
115c445c3c7SAdrian Chadd #else
116ac75ee9fSAdrian Chadd static int pin_default_swi = 0;
117ac75ee9fSAdrian Chadd static int pin_pcpu_swi = 0;
118c445c3c7SAdrian Chadd #endif
119ac75ee9fSAdrian Chadd 
120af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, pin_default_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_default_swi,
121ac75ee9fSAdrian Chadd     0, "Pin the default (non-per-cpu) swi (shared with PCPU 0 swi)");
122af3b2549SHans Petter Selasky SYSCTL_INT(_kern, OID_AUTO, pin_pcpu_swi, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &pin_pcpu_swi,
123ac75ee9fSAdrian Chadd     0, "Pin the per-CPU swis (except PCPU 0, which is also default");
124ac75ee9fSAdrian Chadd 
12515b7a470SPoul-Henning Kamp /*
12615b7a470SPoul-Henning Kamp  * TODO:
12715b7a470SPoul-Henning Kamp  *	allocate more timeout table slots when table overflows.
12815b7a470SPoul-Henning Kamp  */
1293f555c45SDavide Italiano u_int callwheelsize, callwheelmask;
130f23b4c91SGarrett Wollman 
13120c510f8SLuigi Rizzo /*
132a115fb62SHans Petter Selasky  * The callout cpu exec entities represent informations necessary for
133a115fb62SHans Petter Selasky  * describing the state of callouts currently running on the CPU and the ones
134a115fb62SHans Petter Selasky  * necessary for migrating callouts to the new callout cpu. In particular,
135a115fb62SHans Petter Selasky  * the first entry of the array cc_exec_entity holds informations for callout
136a115fb62SHans Petter Selasky  * running in SWI thread context, while the second one holds informations
137a115fb62SHans Petter Selasky  * for callout running directly from hardware interrupt context.
138a115fb62SHans Petter Selasky  * The cached informations are very important for deferring migration when
139a115fb62SHans Petter Selasky  * the migrating callout is already running.
1401283e9cdSAttilio Rao  */
1415b999a6bSDavide Italiano struct cc_exec {
1425b999a6bSDavide Italiano 	struct callout		*cc_curr;
14318b4fd62SRandall Stewart 	void			(*cc_drain)(void *);
144a115fb62SHans Petter Selasky #ifdef SMP
145a115fb62SHans Petter Selasky 	void			(*ce_migration_func)(void *);
146a115fb62SHans Petter Selasky 	void			*ce_migration_arg;
147a115fb62SHans Petter Selasky 	int			ce_migration_cpu;
148a115fb62SHans Petter Selasky 	sbintime_t		ce_migration_time;
149a115fb62SHans Petter Selasky 	sbintime_t		ce_migration_prec;
150a115fb62SHans Petter Selasky #endif
151a4a3ce99SDavide Italiano 	bool			cc_cancel;
152a115fb62SHans Petter Selasky 	bool			cc_waiting;
1531283e9cdSAttilio Rao };
1541283e9cdSAttilio Rao 
1551283e9cdSAttilio Rao /*
156a115fb62SHans Petter Selasky  * There is one struct callout_cpu per cpu, holding all relevant
15720c510f8SLuigi Rizzo  * state for the callout processing thread on the individual CPU.
15820c510f8SLuigi Rizzo  */
1598d809d50SJeff Roberson struct callout_cpu {
1604ceaf45dSAttilio Rao 	struct mtx_padalign	cc_lock;
1615b999a6bSDavide Italiano 	struct cc_exec 		cc_exec_entity[2];
16266525b2dSRandall Stewart 	struct callout		*cc_next;
1638d809d50SJeff Roberson 	struct callout		*cc_callout;
1645b999a6bSDavide Italiano 	struct callout_list	*cc_callwheel;
1655b999a6bSDavide Italiano 	struct callout_tailq	cc_expireq;
1665b999a6bSDavide Italiano 	struct callout_slist	cc_callfree;
1675b999a6bSDavide Italiano 	sbintime_t		cc_firstevent;
1685b999a6bSDavide Italiano 	sbintime_t		cc_lastscan;
1698d809d50SJeff Roberson 	void			*cc_cookie;
1705b999a6bSDavide Italiano 	u_int			cc_bucket;
17115b1eb14SRandall Stewart 	u_int			cc_inited;
172232e8b52SJohn Baldwin 	char			cc_ktr_event_name[20];
1738d809d50SJeff Roberson };
1748d809d50SJeff Roberson 
175403df7a6SRandall Stewart #define	callout_migrating(c)	((c)->c_iflags & CALLOUT_DFRMIGRATION)
176403df7a6SRandall Stewart 
177d2854fa4SRandall Stewart #define	cc_exec_curr(cc, dir)		cc->cc_exec_entity[dir].cc_curr
17818b4fd62SRandall Stewart #define	cc_exec_drain(cc, dir)		cc->cc_exec_entity[dir].cc_drain
17966525b2dSRandall Stewart #define	cc_exec_next(cc)		cc->cc_next
180d2854fa4SRandall Stewart #define	cc_exec_cancel(cc, dir)		cc->cc_exec_entity[dir].cc_cancel
181d2854fa4SRandall Stewart #define	cc_exec_waiting(cc, dir)	cc->cc_exec_entity[dir].cc_waiting
1828d809d50SJeff Roberson #ifdef SMP
183d2854fa4SRandall Stewart #define	cc_migration_func(cc, dir)	cc->cc_exec_entity[dir].ce_migration_func
184d2854fa4SRandall Stewart #define	cc_migration_arg(cc, dir)	cc->cc_exec_entity[dir].ce_migration_arg
185d2854fa4SRandall Stewart #define	cc_migration_cpu(cc, dir)	cc->cc_exec_entity[dir].ce_migration_cpu
186d2854fa4SRandall Stewart #define	cc_migration_time(cc, dir)	cc->cc_exec_entity[dir].ce_migration_time
187d2854fa4SRandall Stewart #define	cc_migration_prec(cc, dir)	cc->cc_exec_entity[dir].ce_migration_prec
188a115fb62SHans Petter Selasky 
1898d809d50SJeff Roberson struct callout_cpu cc_cpu[MAXCPU];
1901283e9cdSAttilio Rao #define	CPUBLOCK	MAXCPU
1918d809d50SJeff Roberson #define	CC_CPU(cpu)	(&cc_cpu[(cpu)])
1928d809d50SJeff Roberson #define	CC_SELF()	CC_CPU(PCPU_GET(cpuid))
1938d809d50SJeff Roberson #else
1948d809d50SJeff Roberson struct callout_cpu cc_cpu;
1958d809d50SJeff Roberson #define	CC_CPU(cpu)	&cc_cpu
1968d809d50SJeff Roberson #define	CC_SELF()	&cc_cpu
1978d809d50SJeff Roberson #endif
1988d809d50SJeff Roberson #define	CC_LOCK(cc)	mtx_lock_spin(&(cc)->cc_lock)
1998d809d50SJeff Roberson #define	CC_UNLOCK(cc)	mtx_unlock_spin(&(cc)->cc_lock)
2001283e9cdSAttilio Rao #define	CC_LOCK_ASSERT(cc)	mtx_assert(&(cc)->cc_lock, MA_OWNED)
2018d809d50SJeff Roberson 
2028d809d50SJeff Roberson static int timeout_cpu;
2035b999a6bSDavide Italiano 
204232e8b52SJohn Baldwin static void	callout_cpu_init(struct callout_cpu *cc, int cpu);
2055b999a6bSDavide Italiano static void	softclock_call_cc(struct callout *c, struct callout_cpu *cc,
2065b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
2075b999a6bSDavide Italiano 		    int *mpcalls, int *lockcalls, int *gcalls,
2085b999a6bSDavide Italiano #endif
2095b999a6bSDavide Italiano 		    int direct);
2108d809d50SJeff Roberson 
211d745c852SEd Schouten static MALLOC_DEFINE(M_CALLOUT, "callout", "Callout datastructures");
21249a74476SColin Percival 
213a115fb62SHans Petter Selasky /**
214a115fb62SHans Petter Selasky  * Locked by cc_lock:
215a115fb62SHans Petter Selasky  *   cc_curr         - If a callout is in progress, it is cc_curr.
216a115fb62SHans Petter Selasky  *                     If cc_curr is non-NULL, threads waiting in
217a115fb62SHans Petter Selasky  *                     callout_drain() will be woken up as soon as the
218a115fb62SHans Petter Selasky  *                     relevant callout completes.
219a115fb62SHans Petter Selasky  *   cc_cancel       - Changing to 1 with both callout_lock and cc_lock held
220a115fb62SHans Petter Selasky  *                     guarantees that the current callout will not run.
221a115fb62SHans Petter Selasky  *                     The softclock() function sets this to 0 before it
222a115fb62SHans Petter Selasky  *                     drops callout_lock to acquire c_lock, and it calls
223a115fb62SHans Petter Selasky  *                     the handler only if curr_cancelled is still 0 after
224a115fb62SHans Petter Selasky  *                     cc_lock is successfully acquired.
225a115fb62SHans Petter Selasky  *   cc_waiting      - If a thread is waiting in callout_drain(), then
226a115fb62SHans Petter Selasky  *                     callout_wait is nonzero.  Set only when
227a115fb62SHans Petter Selasky  *                     cc_curr is non-NULL.
228a115fb62SHans Petter Selasky  */
229a115fb62SHans Petter Selasky 
230df8bae1dSRodney W. Grimes /*
231a115fb62SHans Petter Selasky  * Resets the execution entity tied to a specific callout cpu.
232a115fb62SHans Petter Selasky  */
233a115fb62SHans Petter Selasky static void
234a115fb62SHans Petter Selasky cc_cce_cleanup(struct callout_cpu *cc, int direct)
235a115fb62SHans Petter Selasky {
236a115fb62SHans Petter Selasky 
237d2854fa4SRandall Stewart 	cc_exec_curr(cc, direct) = NULL;
238d2854fa4SRandall Stewart 	cc_exec_cancel(cc, direct) = false;
239d2854fa4SRandall Stewart 	cc_exec_waiting(cc, direct) = false;
240a115fb62SHans Petter Selasky #ifdef SMP
241d2854fa4SRandall Stewart 	cc_migration_cpu(cc, direct) = CPUBLOCK;
242d2854fa4SRandall Stewart 	cc_migration_time(cc, direct) = 0;
243d2854fa4SRandall Stewart 	cc_migration_prec(cc, direct) = 0;
244d2854fa4SRandall Stewart 	cc_migration_func(cc, direct) = NULL;
245d2854fa4SRandall Stewart 	cc_migration_arg(cc, direct) = NULL;
246a115fb62SHans Petter Selasky #endif
247a115fb62SHans Petter Selasky }
248a115fb62SHans Petter Selasky 
249a115fb62SHans Petter Selasky /*
250a115fb62SHans Petter Selasky  * Checks if migration is requested by a specific callout cpu.
251a115fb62SHans Petter Selasky  */
252a115fb62SHans Petter Selasky static int
253a115fb62SHans Petter Selasky cc_cce_migrating(struct callout_cpu *cc, int direct)
254a115fb62SHans Petter Selasky {
255a115fb62SHans Petter Selasky 
256a115fb62SHans Petter Selasky #ifdef SMP
257d2854fa4SRandall Stewart 	return (cc_migration_cpu(cc, direct) != CPUBLOCK);
258a115fb62SHans Petter Selasky #else
259a115fb62SHans Petter Selasky 	return (0);
260a115fb62SHans Petter Selasky #endif
261a115fb62SHans Petter Selasky }
262a115fb62SHans Petter Selasky 
263a115fb62SHans Petter Selasky /*
264a115fb62SHans Petter Selasky  * Kernel low level callwheel initialization
265a115fb62SHans Petter Selasky  * called on cpu0 during kernel startup.
266219d632cSMatthew Dillon  */
26715ae0c9aSAndre Oppermann static void
26815ae0c9aSAndre Oppermann callout_callwheel_init(void *dummy)
269219d632cSMatthew Dillon {
2708d809d50SJeff Roberson 	struct callout_cpu *cc;
2718d809d50SJeff Roberson 
272f8ccf82aSAndre Oppermann 	/*
273f8ccf82aSAndre Oppermann 	 * Calculate the size of the callout wheel and the preallocated
274f8ccf82aSAndre Oppermann 	 * timeout() structures.
275a7aea132SAndre Oppermann 	 * XXX: Clip callout to result of previous function of maxusers
276a7aea132SAndre Oppermann 	 * maximum 384.  This is still huge, but acceptable.
277f8ccf82aSAndre Oppermann 	 */
278a04d4122SBjoern A. Zeeb 	memset(CC_CPU(0), 0, sizeof(cc_cpu));
279f8ccf82aSAndre Oppermann 	ncallout = imin(16 + maxproc + maxfiles, 18508);
280f8ccf82aSAndre Oppermann 	TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
281f8ccf82aSAndre Oppermann 
282219d632cSMatthew Dillon 	/*
283922314f0SAlfred Perlstein 	 * Calculate callout wheel size, should be next power of two higher
284922314f0SAlfred Perlstein 	 * than 'ncallout'.
285219d632cSMatthew Dillon 	 */
286922314f0SAlfred Perlstein 	callwheelsize = 1 << fls(ncallout);
287219d632cSMatthew Dillon 	callwheelmask = callwheelsize - 1;
288219d632cSMatthew Dillon 
28915ae0c9aSAndre Oppermann 	/*
290ac75ee9fSAdrian Chadd 	 * Fetch whether we're pinning the swi's or not.
291ac75ee9fSAdrian Chadd 	 */
292ac75ee9fSAdrian Chadd 	TUNABLE_INT_FETCH("kern.pin_default_swi", &pin_default_swi);
293ac75ee9fSAdrian Chadd 	TUNABLE_INT_FETCH("kern.pin_pcpu_swi", &pin_pcpu_swi);
294ac75ee9fSAdrian Chadd 
295ac75ee9fSAdrian Chadd 	/*
29615ae0c9aSAndre Oppermann 	 * Only cpu0 handles timeout(9) and receives a preallocation.
29715ae0c9aSAndre Oppermann 	 *
29815ae0c9aSAndre Oppermann 	 * XXX: Once all timeout(9) consumers are converted this can
29915ae0c9aSAndre Oppermann 	 * be removed.
30015ae0c9aSAndre Oppermann 	 */
30115ae0c9aSAndre Oppermann 	timeout_cpu = PCPU_GET(cpuid);
30215ae0c9aSAndre Oppermann 	cc = CC_CPU(timeout_cpu);
30315ae0c9aSAndre Oppermann 	cc->cc_callout = malloc(ncallout * sizeof(struct callout),
30415ae0c9aSAndre Oppermann 	    M_CALLOUT, M_WAITOK);
305232e8b52SJohn Baldwin 	callout_cpu_init(cc, timeout_cpu);
306219d632cSMatthew Dillon }
30715ae0c9aSAndre Oppermann SYSINIT(callwheel_init, SI_SUB_CPU, SI_ORDER_ANY, callout_callwheel_init, NULL);
308219d632cSMatthew Dillon 
30915ae0c9aSAndre Oppermann /*
31015ae0c9aSAndre Oppermann  * Initialize the per-cpu callout structures.
31115ae0c9aSAndre Oppermann  */
3128d809d50SJeff Roberson static void
313232e8b52SJohn Baldwin callout_cpu_init(struct callout_cpu *cc, int cpu)
3148d809d50SJeff Roberson {
3158d809d50SJeff Roberson 	struct callout *c;
3168d809d50SJeff Roberson 	int i;
3178d809d50SJeff Roberson 
3188d809d50SJeff Roberson 	mtx_init(&cc->cc_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
3198d809d50SJeff Roberson 	SLIST_INIT(&cc->cc_callfree);
32015b1eb14SRandall Stewart 	cc->cc_inited = 1;
321c5904471SDavide Italiano 	cc->cc_callwheel = malloc(sizeof(struct callout_list) * callwheelsize,
32215ae0c9aSAndre Oppermann 	    M_CALLOUT, M_WAITOK);
3235b999a6bSDavide Italiano 	for (i = 0; i < callwheelsize; i++)
3245b999a6bSDavide Italiano 		LIST_INIT(&cc->cc_callwheel[i]);
3255b999a6bSDavide Italiano 	TAILQ_INIT(&cc->cc_expireq);
3264bc38a5aSDavide Italiano 	cc->cc_firstevent = SBT_MAX;
327a115fb62SHans Petter Selasky 	for (i = 0; i < 2; i++)
328a115fb62SHans Petter Selasky 		cc_cce_cleanup(cc, i);
329232e8b52SJohn Baldwin 	snprintf(cc->cc_ktr_event_name, sizeof(cc->cc_ktr_event_name),
330232e8b52SJohn Baldwin 	    "callwheel cpu %d", cpu);
33115ae0c9aSAndre Oppermann 	if (cc->cc_callout == NULL)	/* Only cpu0 handles timeout(9) */
3328d809d50SJeff Roberson 		return;
3338d809d50SJeff Roberson 	for (i = 0; i < ncallout; i++) {
3348d809d50SJeff Roberson 		c = &cc->cc_callout[i];
3358d809d50SJeff Roberson 		callout_init(c, 0);
33615b1eb14SRandall Stewart 		c->c_iflags = CALLOUT_LOCAL_ALLOC;
3378d809d50SJeff Roberson 		SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
3388d809d50SJeff Roberson 	}
3398d809d50SJeff Roberson }
3408d809d50SJeff Roberson 
341a115fb62SHans Petter Selasky #ifdef SMP
342a115fb62SHans Petter Selasky /*
343a115fb62SHans Petter Selasky  * Switches the cpu tied to a specific callout.
344a115fb62SHans Petter Selasky  * The function expects a locked incoming callout cpu and returns with
345a115fb62SHans Petter Selasky  * locked outcoming callout cpu.
346a115fb62SHans Petter Selasky  */
347a115fb62SHans Petter Selasky static struct callout_cpu *
348a115fb62SHans Petter Selasky callout_cpu_switch(struct callout *c, struct callout_cpu *cc, int new_cpu)
349a115fb62SHans Petter Selasky {
350a115fb62SHans Petter Selasky 	struct callout_cpu *new_cc;
351a115fb62SHans Petter Selasky 
352a115fb62SHans Petter Selasky 	MPASS(c != NULL && cc != NULL);
353a115fb62SHans Petter Selasky 	CC_LOCK_ASSERT(cc);
354a115fb62SHans Petter Selasky 
355a115fb62SHans Petter Selasky 	/*
356a115fb62SHans Petter Selasky 	 * Avoid interrupts and preemption firing after the callout cpu
357a115fb62SHans Petter Selasky 	 * is blocked in order to avoid deadlocks as the new thread
358a115fb62SHans Petter Selasky 	 * may be willing to acquire the callout cpu lock.
359a115fb62SHans Petter Selasky 	 */
360a115fb62SHans Petter Selasky 	c->c_cpu = CPUBLOCK;
361a115fb62SHans Petter Selasky 	spinlock_enter();
362a115fb62SHans Petter Selasky 	CC_UNLOCK(cc);
363a115fb62SHans Petter Selasky 	new_cc = CC_CPU(new_cpu);
364a115fb62SHans Petter Selasky 	CC_LOCK(new_cc);
365a115fb62SHans Petter Selasky 	spinlock_exit();
366a115fb62SHans Petter Selasky 	c->c_cpu = new_cpu;
367a115fb62SHans Petter Selasky 	return (new_cc);
368a115fb62SHans Petter Selasky }
369a115fb62SHans Petter Selasky #endif
370a115fb62SHans Petter Selasky 
371219d632cSMatthew Dillon /*
3728d809d50SJeff Roberson  * Start standard softclock thread.
3738d809d50SJeff Roberson  */
3748d809d50SJeff Roberson static void
3758d809d50SJeff Roberson start_softclock(void *dummy)
3768d809d50SJeff Roberson {
3778d809d50SJeff Roberson 	struct callout_cpu *cc;
378f44e2a4cSAdrian Chadd 	char name[MAXCOMLEN];
3798d809d50SJeff Roberson #ifdef SMP
3808d809d50SJeff Roberson 	int cpu;
381ac75ee9fSAdrian Chadd 	struct intr_event *ie;
3828d809d50SJeff Roberson #endif
3838d809d50SJeff Roberson 
3848d809d50SJeff Roberson 	cc = CC_CPU(timeout_cpu);
385f44e2a4cSAdrian Chadd 	snprintf(name, sizeof(name), "clock (%d)", timeout_cpu);
386f44e2a4cSAdrian Chadd 	if (swi_add(&clk_intr_event, name, softclock, cc, SWI_CLOCK,
3873350df48SJohn Baldwin 	    INTR_MPSAFE, &cc->cc_cookie))
3888d809d50SJeff Roberson 		panic("died while creating standard software ithreads");
389ac75ee9fSAdrian Chadd 	if (pin_default_swi &&
390ac75ee9fSAdrian Chadd 	    (intr_event_bind(clk_intr_event, timeout_cpu) != 0)) {
391ac75ee9fSAdrian Chadd 		printf("%s: timeout clock couldn't be pinned to cpu %d\n",
392ac75ee9fSAdrian Chadd 		    __func__,
393ac75ee9fSAdrian Chadd 		    timeout_cpu);
394ac75ee9fSAdrian Chadd 	}
395ac75ee9fSAdrian Chadd 
3968d809d50SJeff Roberson #ifdef SMP
3973aa6d94eSJohn Baldwin 	CPU_FOREACH(cpu) {
3988d809d50SJeff Roberson 		if (cpu == timeout_cpu)
3998d809d50SJeff Roberson 			continue;
4008d809d50SJeff Roberson 		cc = CC_CPU(cpu);
40115ae0c9aSAndre Oppermann 		cc->cc_callout = NULL;	/* Only cpu0 handles timeout(9). */
402232e8b52SJohn Baldwin 		callout_cpu_init(cc, cpu);
403f44e2a4cSAdrian Chadd 		snprintf(name, sizeof(name), "clock (%d)", cpu);
404ac75ee9fSAdrian Chadd 		ie = NULL;
405ac75ee9fSAdrian Chadd 		if (swi_add(&ie, name, softclock, cc, SWI_CLOCK,
4068d809d50SJeff Roberson 		    INTR_MPSAFE, &cc->cc_cookie))
4078d809d50SJeff Roberson 			panic("died while creating standard software ithreads");
408ac75ee9fSAdrian Chadd 		if (pin_pcpu_swi && (intr_event_bind(ie, cpu) != 0)) {
409ac75ee9fSAdrian Chadd 			printf("%s: per-cpu clock couldn't be pinned to "
410ac75ee9fSAdrian Chadd 			    "cpu %d\n",
411ac75ee9fSAdrian Chadd 			    __func__,
412ac75ee9fSAdrian Chadd 			    cpu);
413ac75ee9fSAdrian Chadd 		}
414219d632cSMatthew Dillon 	}
4158d809d50SJeff Roberson #endif
416219d632cSMatthew Dillon }
4178d809d50SJeff Roberson SYSINIT(start_softclock, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softclock, NULL);
4188d809d50SJeff Roberson 
4195b999a6bSDavide Italiano #define	CC_HASH_SHIFT	8
4208d809d50SJeff Roberson 
4215b999a6bSDavide Italiano static inline u_int
4225b999a6bSDavide Italiano callout_hash(sbintime_t sbt)
4235b999a6bSDavide Italiano {
4245b999a6bSDavide Italiano 
4255b999a6bSDavide Italiano 	return (sbt >> (32 - CC_HASH_SHIFT));
4265b999a6bSDavide Italiano }
4275b999a6bSDavide Italiano 
4285b999a6bSDavide Italiano static inline u_int
4295b999a6bSDavide Italiano callout_get_bucket(sbintime_t sbt)
4305b999a6bSDavide Italiano {
4315b999a6bSDavide Italiano 
4325b999a6bSDavide Italiano 	return (callout_hash(sbt) & callwheelmask);
4335b999a6bSDavide Italiano }
4345b999a6bSDavide Italiano 
4355b999a6bSDavide Italiano void
4365b999a6bSDavide Italiano callout_process(sbintime_t now)
4375b999a6bSDavide Italiano {
4385b999a6bSDavide Italiano 	struct callout *tmp, *tmpn;
4395b999a6bSDavide Italiano 	struct callout_cpu *cc;
4405b999a6bSDavide Italiano 	struct callout_list *sc;
4415b999a6bSDavide Italiano 	sbintime_t first, last, max, tmp_max;
4425b999a6bSDavide Italiano 	uint32_t lookahead;
4435b999a6bSDavide Italiano 	u_int firstb, lastb, nowb;
4445b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
4455b999a6bSDavide Italiano 	int depth_dir = 0, mpcalls_dir = 0, lockcalls_dir = 0;
4465b999a6bSDavide Italiano #endif
447a115fb62SHans Petter Selasky 
4488d809d50SJeff Roberson 	cc = CC_SELF();
449a115fb62SHans Petter Selasky 	mtx_lock_spin_flags(&cc->cc_lock, MTX_QUIET);
4505b999a6bSDavide Italiano 
4515b999a6bSDavide Italiano 	/* Compute the buckets of the last scan and present times. */
4525b999a6bSDavide Italiano 	firstb = callout_hash(cc->cc_lastscan);
4535b999a6bSDavide Italiano 	cc->cc_lastscan = now;
4545b999a6bSDavide Italiano 	nowb = callout_hash(now);
4555b999a6bSDavide Italiano 
4565b999a6bSDavide Italiano 	/* Compute the last bucket and minimum time of the bucket after it. */
4575b999a6bSDavide Italiano 	if (nowb == firstb)
4585b999a6bSDavide Italiano 		lookahead = (SBT_1S / 16);
4595b999a6bSDavide Italiano 	else if (nowb - firstb == 1)
4605b999a6bSDavide Italiano 		lookahead = (SBT_1S / 8);
4615b999a6bSDavide Italiano 	else
4625b999a6bSDavide Italiano 		lookahead = (SBT_1S / 2);
4635b999a6bSDavide Italiano 	first = last = now;
4645b999a6bSDavide Italiano 	first += (lookahead / 2);
4655b999a6bSDavide Italiano 	last += lookahead;
4665b999a6bSDavide Italiano 	last &= (0xffffffffffffffffLLU << (32 - CC_HASH_SHIFT));
4675b999a6bSDavide Italiano 	lastb = callout_hash(last) - 1;
4685b999a6bSDavide Italiano 	max = last;
4695b999a6bSDavide Italiano 
4705b999a6bSDavide Italiano 	/*
4715b999a6bSDavide Italiano 	 * Check if we wrapped around the entire wheel from the last scan.
4725b999a6bSDavide Italiano 	 * In case, we need to scan entirely the wheel for pending callouts.
4735b999a6bSDavide Italiano 	 */
4745b999a6bSDavide Italiano 	if (lastb - firstb >= callwheelsize) {
4755b999a6bSDavide Italiano 		lastb = firstb + callwheelsize - 1;
4765b999a6bSDavide Italiano 		if (nowb - firstb >= callwheelsize)
4775b999a6bSDavide Italiano 			nowb = lastb;
4789fc51b0bSJeff Roberson 	}
4795b999a6bSDavide Italiano 
4805b999a6bSDavide Italiano 	/* Iterate callwheel from firstb to nowb and then up to lastb. */
4815b999a6bSDavide Italiano 	do {
4825b999a6bSDavide Italiano 		sc = &cc->cc_callwheel[firstb & callwheelmask];
4835b999a6bSDavide Italiano 		tmp = LIST_FIRST(sc);
4845b999a6bSDavide Italiano 		while (tmp != NULL) {
4855b999a6bSDavide Italiano 			/* Run the callout if present time within allowed. */
4865b999a6bSDavide Italiano 			if (tmp->c_time <= now) {
4875b999a6bSDavide Italiano 				/*
4885b999a6bSDavide Italiano 				 * Consumer told us the callout may be run
4895b999a6bSDavide Italiano 				 * directly from hardware interrupt context.
4905b999a6bSDavide Italiano 				 */
49115b1eb14SRandall Stewart 				if (tmp->c_iflags & CALLOUT_DIRECT) {
4925b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
4935b999a6bSDavide Italiano 					++depth_dir;
4945b999a6bSDavide Italiano #endif
49566525b2dSRandall Stewart 					cc_exec_next(cc) =
4965b999a6bSDavide Italiano 					    LIST_NEXT(tmp, c_links.le);
4975b999a6bSDavide Italiano 					cc->cc_bucket = firstb & callwheelmask;
4985b999a6bSDavide Italiano 					LIST_REMOVE(tmp, c_links.le);
4995b999a6bSDavide Italiano 					softclock_call_cc(tmp, cc,
5005b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
5015b999a6bSDavide Italiano 					    &mpcalls_dir, &lockcalls_dir, NULL,
5025b999a6bSDavide Italiano #endif
5035b999a6bSDavide Italiano 					    1);
50466525b2dSRandall Stewart 					tmp = cc_exec_next(cc);
50566525b2dSRandall Stewart 					cc_exec_next(cc) = NULL;
5065b999a6bSDavide Italiano 				} else {
5075b999a6bSDavide Italiano 					tmpn = LIST_NEXT(tmp, c_links.le);
5085b999a6bSDavide Italiano 					LIST_REMOVE(tmp, c_links.le);
5095b999a6bSDavide Italiano 					TAILQ_INSERT_TAIL(&cc->cc_expireq,
5105b999a6bSDavide Italiano 					    tmp, c_links.tqe);
51115b1eb14SRandall Stewart 					tmp->c_iflags |= CALLOUT_PROCESSED;
5125b999a6bSDavide Italiano 					tmp = tmpn;
5139fc51b0bSJeff Roberson 				}
5145b999a6bSDavide Italiano 				continue;
5155b999a6bSDavide Italiano 			}
5165b999a6bSDavide Italiano 			/* Skip events from distant future. */
5175b999a6bSDavide Italiano 			if (tmp->c_time >= max)
5185b999a6bSDavide Italiano 				goto next;
5195b999a6bSDavide Italiano 			/*
5205b999a6bSDavide Italiano 			 * Event minimal time is bigger than present maximal
5215b999a6bSDavide Italiano 			 * time, so it cannot be aggregated.
5225b999a6bSDavide Italiano 			 */
5235b999a6bSDavide Italiano 			if (tmp->c_time > last) {
5245b999a6bSDavide Italiano 				lastb = nowb;
5255b999a6bSDavide Italiano 				goto next;
5265b999a6bSDavide Italiano 			}
5275b999a6bSDavide Italiano 			/* Update first and last time, respecting this event. */
5285b999a6bSDavide Italiano 			if (tmp->c_time < first)
5295b999a6bSDavide Italiano 				first = tmp->c_time;
5305b999a6bSDavide Italiano 			tmp_max = tmp->c_time + tmp->c_precision;
5315b999a6bSDavide Italiano 			if (tmp_max < last)
5325b999a6bSDavide Italiano 				last = tmp_max;
5335b999a6bSDavide Italiano next:
5345b999a6bSDavide Italiano 			tmp = LIST_NEXT(tmp, c_links.le);
5355b999a6bSDavide Italiano 		}
5365b999a6bSDavide Italiano 		/* Proceed with the next bucket. */
5375b999a6bSDavide Italiano 		firstb++;
5385b999a6bSDavide Italiano 		/*
5395b999a6bSDavide Italiano 		 * Stop if we looked after present time and found
5405b999a6bSDavide Italiano 		 * some event we can't execute at now.
5415b999a6bSDavide Italiano 		 * Stop if we looked far enough into the future.
5425b999a6bSDavide Italiano 		 */
5435b999a6bSDavide Italiano 	} while (((int)(firstb - lastb)) <= 0);
5445b999a6bSDavide Italiano 	cc->cc_firstevent = last;
5455b999a6bSDavide Italiano #ifndef NO_EVENTTIMERS
5465b999a6bSDavide Italiano 	cpu_new_callout(curcpu, last, first);
5475b999a6bSDavide Italiano #endif
5485b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
5495b999a6bSDavide Italiano 	avg_depth_dir += (depth_dir * 1000 - avg_depth_dir) >> 8;
5505b999a6bSDavide Italiano 	avg_mpcalls_dir += (mpcalls_dir * 1000 - avg_mpcalls_dir) >> 8;
5515b999a6bSDavide Italiano 	avg_lockcalls_dir += (lockcalls_dir * 1000 - avg_lockcalls_dir) >> 8;
5525b999a6bSDavide Italiano #endif
553a115fb62SHans Petter Selasky 	mtx_unlock_spin_flags(&cc->cc_lock, MTX_QUIET);
5548d809d50SJeff Roberson 	/*
5558d809d50SJeff Roberson 	 * swi_sched acquires the thread lock, so we don't want to call it
5568d809d50SJeff Roberson 	 * with cc_lock held; incorrect locking order.
5578d809d50SJeff Roberson 	 */
5585b999a6bSDavide Italiano 	if (!TAILQ_EMPTY(&cc->cc_expireq))
5598d809d50SJeff Roberson 		swi_sched(cc->cc_cookie, 0);
5608d809d50SJeff Roberson }
5618d809d50SJeff Roberson 
5628d809d50SJeff Roberson static struct callout_cpu *
5638d809d50SJeff Roberson callout_lock(struct callout *c)
5648d809d50SJeff Roberson {
5658d809d50SJeff Roberson 	struct callout_cpu *cc;
566a115fb62SHans Petter Selasky 	int cpu;
567a115fb62SHans Petter Selasky 
568a115fb62SHans Petter Selasky 	for (;;) {
569a115fb62SHans Petter Selasky 		cpu = c->c_cpu;
570a115fb62SHans Petter Selasky #ifdef SMP
571a115fb62SHans Petter Selasky 		if (cpu == CPUBLOCK) {
572a115fb62SHans Petter Selasky 			while (c->c_cpu == CPUBLOCK)
573a115fb62SHans Petter Selasky 				cpu_spinwait();
574a115fb62SHans Petter Selasky 			continue;
575a115fb62SHans Petter Selasky 		}
576a115fb62SHans Petter Selasky #endif
577a115fb62SHans Petter Selasky 		cc = CC_CPU(cpu);
5788d809d50SJeff Roberson 		CC_LOCK(cc);
579a115fb62SHans Petter Selasky 		if (cpu == c->c_cpu)
580a115fb62SHans Petter Selasky 			break;
581a115fb62SHans Petter Selasky 		CC_UNLOCK(cc);
582a115fb62SHans Petter Selasky 	}
5838d809d50SJeff Roberson 	return (cc);
584219d632cSMatthew Dillon }
585219d632cSMatthew Dillon 
586a115fb62SHans Petter Selasky static void
587a115fb62SHans Petter Selasky callout_cc_add(struct callout *c, struct callout_cpu *cc,
588a115fb62SHans Petter Selasky     sbintime_t sbt, sbintime_t precision, void (*func)(void *),
58966525b2dSRandall Stewart     void *arg, int cpu, int flags)
5901283e9cdSAttilio Rao {
5915b999a6bSDavide Italiano 	int bucket;
5921283e9cdSAttilio Rao 
5931283e9cdSAttilio Rao 	CC_LOCK_ASSERT(cc);
594a115fb62SHans Petter Selasky 	if (sbt < cc->cc_lastscan)
595a115fb62SHans Petter Selasky 		sbt = cc->cc_lastscan;
596a115fb62SHans Petter Selasky 	c->c_arg = arg;
59715b1eb14SRandall Stewart 	c->c_iflags |= CALLOUT_PENDING;
59815b1eb14SRandall Stewart 	c->c_iflags &= ~CALLOUT_PROCESSED;
59915b1eb14SRandall Stewart 	c->c_flags |= CALLOUT_ACTIVE;
60007a2df5dSRandall Stewart 	if (flags & C_DIRECT_EXEC)
601b132edb5SRandall Stewart 		c->c_iflags |= CALLOUT_DIRECT;
602a115fb62SHans Petter Selasky 	c->c_func = func;
603a115fb62SHans Petter Selasky 	c->c_time = sbt;
604a115fb62SHans Petter Selasky 	c->c_precision = precision;
6055b999a6bSDavide Italiano 	bucket = callout_get_bucket(c->c_time);
6065b999a6bSDavide Italiano 	CTR3(KTR_CALLOUT, "precision set for %p: %d.%08x",
6075b999a6bSDavide Italiano 	    c, (int)(c->c_precision >> 32),
6085b999a6bSDavide Italiano 	    (u_int)(c->c_precision & 0xffffffff));
6095b999a6bSDavide Italiano 	LIST_INSERT_HEAD(&cc->cc_callwheel[bucket], c, c_links.le);
6105b999a6bSDavide Italiano 	if (cc->cc_bucket == bucket)
61166525b2dSRandall Stewart 		cc_exec_next(cc) = c;
6125b999a6bSDavide Italiano #ifndef NO_EVENTTIMERS
6135b999a6bSDavide Italiano 	/*
6145b999a6bSDavide Italiano 	 * Inform the eventtimers(4) subsystem there's a new callout
6155b999a6bSDavide Italiano 	 * that has been inserted, but only if really required.
6165b999a6bSDavide Italiano 	 */
6174bc38a5aSDavide Italiano 	if (SBT_MAX - c->c_time < c->c_precision)
6184bc38a5aSDavide Italiano 		c->c_precision = SBT_MAX - c->c_time;
6195b999a6bSDavide Italiano 	sbt = c->c_time + c->c_precision;
6205b999a6bSDavide Italiano 	if (sbt < cc->cc_firstevent) {
6215b999a6bSDavide Italiano 		cc->cc_firstevent = sbt;
622a115fb62SHans Petter Selasky 		cpu_new_callout(cpu, sbt, c->c_time);
6231283e9cdSAttilio Rao 	}
6245b999a6bSDavide Italiano #endif
6251283e9cdSAttilio Rao }
6261283e9cdSAttilio Rao 
6276098e7acSKonstantin Belousov static void
6286098e7acSKonstantin Belousov callout_cc_del(struct callout *c, struct callout_cpu *cc)
6296098e7acSKonstantin Belousov {
6306098e7acSKonstantin Belousov 
63115b1eb14SRandall Stewart 	if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) == 0)
632a115fb62SHans Petter Selasky 		return;
6336098e7acSKonstantin Belousov 	c->c_func = NULL;
6346098e7acSKonstantin Belousov 	SLIST_INSERT_HEAD(&cc->cc_callfree, c, c_links.sle);
6356098e7acSKonstantin Belousov }
6366098e7acSKonstantin Belousov 
637eb8a7186SKonstantin Belousov static void
6385b999a6bSDavide Italiano softclock_call_cc(struct callout *c, struct callout_cpu *cc,
6395b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
6405b999a6bSDavide Italiano     int *mpcalls, int *lockcalls, int *gcalls,
6415b999a6bSDavide Italiano #endif
6425b999a6bSDavide Italiano     int direct)
6436098e7acSKonstantin Belousov {
644a115fb62SHans Petter Selasky 	struct rm_priotracker tracker;
645a115fb62SHans Petter Selasky 	void (*c_func)(void *);
6466098e7acSKonstantin Belousov 	void *c_arg;
647a115fb62SHans Petter Selasky 	struct lock_class *class;
6486098e7acSKonstantin Belousov 	struct lock_object *c_lock;
649a115fb62SHans Petter Selasky 	uintptr_t lock_status;
65015b1eb14SRandall Stewart 	int c_iflags;
651a115fb62SHans Petter Selasky #ifdef SMP
652a115fb62SHans Petter Selasky 	struct callout_cpu *new_cc;
653a115fb62SHans Petter Selasky 	void (*new_func)(void *);
654a115fb62SHans Petter Selasky 	void *new_arg;
655a115fb62SHans Petter Selasky 	int flags, new_cpu;
656a115fb62SHans Petter Selasky 	sbintime_t new_prec, new_time;
657a115fb62SHans Petter Selasky #endif
6585b999a6bSDavide Italiano #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
65903763781SDavide Italiano 	sbintime_t sbt1, sbt2;
6606098e7acSKonstantin Belousov 	struct timespec ts2;
6615b999a6bSDavide Italiano 	static sbintime_t maxdt = 2 * SBT_1MS;	/* 2 msec */
6626098e7acSKonstantin Belousov 	static timeout_t *lastfunc;
6636098e7acSKonstantin Belousov #endif
6646098e7acSKonstantin Belousov 
66515b1eb14SRandall Stewart 	KASSERT((c->c_iflags & CALLOUT_PENDING) == CALLOUT_PENDING,
66615b1eb14SRandall Stewart 	    ("softclock_call_cc: pend %p %x", c, c->c_iflags));
66715b1eb14SRandall Stewart 	KASSERT((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE,
66815b1eb14SRandall Stewart 	    ("softclock_call_cc: act %p %x", c, c->c_flags));
669a115fb62SHans Petter Selasky 	class = (c->c_lock != NULL) ? LOCK_CLASS(c->c_lock) : NULL;
670a115fb62SHans Petter Selasky 	lock_status = 0;
671a115fb62SHans Petter Selasky 	if (c->c_flags & CALLOUT_SHAREDLOCK) {
672a115fb62SHans Petter Selasky 		if (class == &lock_class_rm)
673a115fb62SHans Petter Selasky 			lock_status = (uintptr_t)&tracker;
674a115fb62SHans Petter Selasky 		else
675a115fb62SHans Petter Selasky 			lock_status = 1;
676a115fb62SHans Petter Selasky 	}
6776098e7acSKonstantin Belousov 	c_lock = c->c_lock;
6786098e7acSKonstantin Belousov 	c_func = c->c_func;
6796098e7acSKonstantin Belousov 	c_arg = c->c_arg;
68015b1eb14SRandall Stewart 	c_iflags = c->c_iflags;
68115b1eb14SRandall Stewart 	if (c->c_iflags & CALLOUT_LOCAL_ALLOC)
68215b1eb14SRandall Stewart 		c->c_iflags = CALLOUT_LOCAL_ALLOC;
683a115fb62SHans Petter Selasky 	else
68415b1eb14SRandall Stewart 		c->c_iflags &= ~CALLOUT_PENDING;
685d2854fa4SRandall Stewart 
686d2854fa4SRandall Stewart 	cc_exec_curr(cc, direct) = c;
687d2854fa4SRandall Stewart 	cc_exec_cancel(cc, direct) = false;
68818b4fd62SRandall Stewart 	cc_exec_drain(cc, direct) = NULL;
6896098e7acSKonstantin Belousov 	CC_UNLOCK(cc);
690a115fb62SHans Petter Selasky 	if (c_lock != NULL) {
691a115fb62SHans Petter Selasky 		class->lc_lock(c_lock, lock_status);
6926098e7acSKonstantin Belousov 		/*
693a115fb62SHans Petter Selasky 		 * The callout may have been cancelled
694a115fb62SHans Petter Selasky 		 * while we switched locks.
6956098e7acSKonstantin Belousov 		 */
696d2854fa4SRandall Stewart 		if (cc_exec_cancel(cc, direct)) {
697a115fb62SHans Petter Selasky 			class->lc_unlock(c_lock);
698a115fb62SHans Petter Selasky 			goto skip;
6996098e7acSKonstantin Belousov 		}
700a115fb62SHans Petter Selasky 		/* The callout cannot be stopped now. */
701d2854fa4SRandall Stewart 		cc_exec_cancel(cc, direct) = true;
7026098e7acSKonstantin Belousov 		if (c_lock == &Giant.lock_object) {
7035b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
7046098e7acSKonstantin Belousov 			(*gcalls)++;
7055b999a6bSDavide Italiano #endif
7065b999a6bSDavide Italiano 			CTR3(KTR_CALLOUT, "callout giant %p func %p arg %p",
7076098e7acSKonstantin Belousov 			    c, c_func, c_arg);
7086098e7acSKonstantin Belousov 		} else {
7095b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
7106098e7acSKonstantin Belousov 			(*lockcalls)++;
7115b999a6bSDavide Italiano #endif
7126098e7acSKonstantin Belousov 			CTR3(KTR_CALLOUT, "callout lock %p func %p arg %p",
7136098e7acSKonstantin Belousov 			    c, c_func, c_arg);
7146098e7acSKonstantin Belousov 		}
7156098e7acSKonstantin Belousov 	} else {
7165b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
7176098e7acSKonstantin Belousov 		(*mpcalls)++;
7185b999a6bSDavide Italiano #endif
7195b999a6bSDavide Italiano 		CTR3(KTR_CALLOUT, "callout %p func %p arg %p",
7206098e7acSKonstantin Belousov 		    c, c_func, c_arg);
7216098e7acSKonstantin Belousov 	}
722232e8b52SJohn Baldwin 	KTR_STATE3(KTR_SCHED, "callout", cc->cc_ktr_event_name, "running",
723232e8b52SJohn Baldwin 	    "func:%p", c_func, "arg:%p", c_arg, "direct:%d", direct);
72403763781SDavide Italiano #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
7255b999a6bSDavide Italiano 	sbt1 = sbinuptime();
7266098e7acSKonstantin Belousov #endif
7276098e7acSKonstantin Belousov 	THREAD_NO_SLEEPING();
72836160958SMark Johnston 	SDT_PROBE1(callout_execute, , , callout__start, c);
7296098e7acSKonstantin Belousov 	c_func(c_arg);
73036160958SMark Johnston 	SDT_PROBE1(callout_execute, , , callout__end, c);
7316098e7acSKonstantin Belousov 	THREAD_SLEEPING_OK();
73203763781SDavide Italiano #if defined(DIAGNOSTIC) || defined(CALLOUT_PROFILING)
73303763781SDavide Italiano 	sbt2 = sbinuptime();
73403763781SDavide Italiano 	sbt2 -= sbt1;
73503763781SDavide Italiano 	if (sbt2 > maxdt) {
73603763781SDavide Italiano 		if (lastfunc != c_func || sbt2 > maxdt * 2) {
73703763781SDavide Italiano 			ts2 = sbttots(sbt2);
7386098e7acSKonstantin Belousov 			printf(
7396098e7acSKonstantin Belousov 		"Expensive timeout(9) function: %p(%p) %jd.%09ld s\n",
7406098e7acSKonstantin Belousov 			    c_func, c_arg, (intmax_t)ts2.tv_sec, ts2.tv_nsec);
7416098e7acSKonstantin Belousov 		}
74203763781SDavide Italiano 		maxdt = sbt2;
7436098e7acSKonstantin Belousov 		lastfunc = c_func;
7446098e7acSKonstantin Belousov 	}
7456098e7acSKonstantin Belousov #endif
746232e8b52SJohn Baldwin 	KTR_STATE0(KTR_SCHED, "callout", cc->cc_ktr_event_name, "idle");
7476098e7acSKonstantin Belousov 	CTR1(KTR_CALLOUT, "callout %p finished", c);
74815b1eb14SRandall Stewart 	if ((c_iflags & CALLOUT_RETURNUNLOCKED) == 0)
749a115fb62SHans Petter Selasky 		class->lc_unlock(c_lock);
750a115fb62SHans Petter Selasky skip:
7516098e7acSKonstantin Belousov 	CC_LOCK(cc);
752d2854fa4SRandall Stewart 	KASSERT(cc_exec_curr(cc, direct) == c, ("mishandled cc_curr"));
753d2854fa4SRandall Stewart 	cc_exec_curr(cc, direct) = NULL;
75418b4fd62SRandall Stewart 	if (cc_exec_drain(cc, direct)) {
75518b4fd62SRandall Stewart 		void (*drain)(void *);
75618b4fd62SRandall Stewart 
75718b4fd62SRandall Stewart 		drain = cc_exec_drain(cc, direct);
75818b4fd62SRandall Stewart 		cc_exec_drain(cc, direct) = NULL;
75918b4fd62SRandall Stewart 		CC_UNLOCK(cc);
76018b4fd62SRandall Stewart 		drain(c_arg);
76118b4fd62SRandall Stewart 		CC_LOCK(cc);
76218b4fd62SRandall Stewart 	}
763d2854fa4SRandall Stewart 	if (cc_exec_waiting(cc, direct)) {
764bdf9120cSAttilio Rao 		/*
765a115fb62SHans Petter Selasky 		 * There is someone waiting for the
766a115fb62SHans Petter Selasky 		 * callout to complete.
767a115fb62SHans Petter Selasky 		 * If the callout was scheduled for
768a115fb62SHans Petter Selasky 		 * migration just cancel it.
769bdf9120cSAttilio Rao 		 */
770a115fb62SHans Petter Selasky 		if (cc_cce_migrating(cc, direct)) {
771a115fb62SHans Petter Selasky 			cc_cce_cleanup(cc, direct);
772a115fb62SHans Petter Selasky 
773a115fb62SHans Petter Selasky 			/*
774a115fb62SHans Petter Selasky 			 * It should be assert here that the callout is not
775a115fb62SHans Petter Selasky 			 * destroyed but that is not easy.
776a115fb62SHans Petter Selasky 			 */
77715b1eb14SRandall Stewart 			c->c_iflags &= ~CALLOUT_DFRMIGRATION;
7786098e7acSKonstantin Belousov 		}
779d2854fa4SRandall Stewart 		cc_exec_waiting(cc, direct) = false;
780a115fb62SHans Petter Selasky 		CC_UNLOCK(cc);
781d2854fa4SRandall Stewart 		wakeup(&cc_exec_waiting(cc, direct));
782a115fb62SHans Petter Selasky 		CC_LOCK(cc);
783a115fb62SHans Petter Selasky 	} else if (cc_cce_migrating(cc, direct)) {
78415b1eb14SRandall Stewart 		KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0,
785a115fb62SHans Petter Selasky 		    ("Migrating legacy callout %p", c));
786a115fb62SHans Petter Selasky #ifdef SMP
787a115fb62SHans Petter Selasky 		/*
788a115fb62SHans Petter Selasky 		 * If the callout was scheduled for
789a115fb62SHans Petter Selasky 		 * migration just perform it now.
790a115fb62SHans Petter Selasky 		 */
791d2854fa4SRandall Stewart 		new_cpu = cc_migration_cpu(cc, direct);
792d2854fa4SRandall Stewart 		new_time = cc_migration_time(cc, direct);
793d2854fa4SRandall Stewart 		new_prec = cc_migration_prec(cc, direct);
794d2854fa4SRandall Stewart 		new_func = cc_migration_func(cc, direct);
795d2854fa4SRandall Stewart 		new_arg = cc_migration_arg(cc, direct);
796a115fb62SHans Petter Selasky 		cc_cce_cleanup(cc, direct);
797a115fb62SHans Petter Selasky 
798a115fb62SHans Petter Selasky 		/*
799a115fb62SHans Petter Selasky 		 * It should be assert here that the callout is not destroyed
800a115fb62SHans Petter Selasky 		 * but that is not easy.
801a115fb62SHans Petter Selasky 		 *
802a115fb62SHans Petter Selasky 		 * As first thing, handle deferred callout stops.
803a115fb62SHans Petter Selasky 		 */
804d2854fa4SRandall Stewart 		if (!callout_migrating(c)) {
805a115fb62SHans Petter Selasky 			CTR3(KTR_CALLOUT,
806a115fb62SHans Petter Selasky 			     "deferred cancelled %p func %p arg %p",
807a115fb62SHans Petter Selasky 			     c, new_func, new_arg);
808a115fb62SHans Petter Selasky 			callout_cc_del(c, cc);
809a115fb62SHans Petter Selasky 			return;
810a115fb62SHans Petter Selasky 		}
81115b1eb14SRandall Stewart 		c->c_iflags &= ~CALLOUT_DFRMIGRATION;
812a115fb62SHans Petter Selasky 
813a115fb62SHans Petter Selasky 		new_cc = callout_cpu_switch(c, cc, new_cpu);
814a115fb62SHans Petter Selasky 		flags = (direct) ? C_DIRECT_EXEC : 0;
815a115fb62SHans Petter Selasky 		callout_cc_add(c, new_cc, new_time, new_prec, new_func,
81666525b2dSRandall Stewart 		    new_arg, new_cpu, flags);
817a115fb62SHans Petter Selasky 		CC_UNLOCK(new_cc);
818a115fb62SHans Petter Selasky 		CC_LOCK(cc);
819a115fb62SHans Petter Selasky #else
820a115fb62SHans Petter Selasky 		panic("migration should not happen");
821a115fb62SHans Petter Selasky #endif
822a115fb62SHans Petter Selasky 	}
823a115fb62SHans Petter Selasky 	/*
824a115fb62SHans Petter Selasky 	 * If the current callout is locally allocated (from
825a115fb62SHans Petter Selasky 	 * timeout(9)) then put it on the freelist.
826a115fb62SHans Petter Selasky 	 *
82715b1eb14SRandall Stewart 	 * Note: we need to check the cached copy of c_iflags because
828a115fb62SHans Petter Selasky 	 * if it was not local, then it's not safe to deref the
829a115fb62SHans Petter Selasky 	 * callout pointer.
830a115fb62SHans Petter Selasky 	 */
83115b1eb14SRandall Stewart 	KASSERT((c_iflags & CALLOUT_LOCAL_ALLOC) == 0 ||
83215b1eb14SRandall Stewart 	    c->c_iflags == CALLOUT_LOCAL_ALLOC,
833a115fb62SHans Petter Selasky 	    ("corrupted callout"));
83415b1eb14SRandall Stewart 	if (c_iflags & CALLOUT_LOCAL_ALLOC)
835a115fb62SHans Petter Selasky 		callout_cc_del(c, cc);
8366098e7acSKonstantin Belousov }
8376098e7acSKonstantin Belousov 
838219d632cSMatthew Dillon /*
839ab36c067SJustin T. Gibbs  * The callout mechanism is based on the work of Adam M. Costello and
840ab36c067SJustin T. Gibbs  * George Varghese, published in a technical report entitled "Redesigning
841ab36c067SJustin T. Gibbs  * the BSD Callout and Timer Facilities" and modified slightly for inclusion
842ab36c067SJustin T. Gibbs  * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
843024035e8SHiten Pandya  * used in this implementation was published by G. Varghese and T. Lauck in
844ab36c067SJustin T. Gibbs  * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
845ab36c067SJustin T. Gibbs  * the Efficient Implementation of a Timer Facility" in the Proceedings of
846ab36c067SJustin T. Gibbs  * the 11th ACM Annual Symposium on Operating Systems Principles,
847ab36c067SJustin T. Gibbs  * Austin, Texas Nov 1987.
848ab36c067SJustin T. Gibbs  */
849a50ec505SPoul-Henning Kamp 
850ab36c067SJustin T. Gibbs /*
851df8bae1dSRodney W. Grimes  * Software (low priority) clock interrupt.
852df8bae1dSRodney W. Grimes  * Run periodic events from timeout queue.
853df8bae1dSRodney W. Grimes  */
854df8bae1dSRodney W. Grimes void
8558d809d50SJeff Roberson softclock(void *arg)
856df8bae1dSRodney W. Grimes {
8578d809d50SJeff Roberson 	struct callout_cpu *cc;
858b336df68SPoul-Henning Kamp 	struct callout *c;
8595b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
8605b999a6bSDavide Italiano 	int depth = 0, gcalls = 0, lockcalls = 0, mpcalls = 0;
8615b999a6bSDavide Italiano #endif
862df8bae1dSRodney W. Grimes 
8638d809d50SJeff Roberson 	cc = (struct callout_cpu *)arg;
8648d809d50SJeff Roberson 	CC_LOCK(cc);
8655b999a6bSDavide Italiano 	while ((c = TAILQ_FIRST(&cc->cc_expireq)) != NULL) {
8665b999a6bSDavide Italiano 		TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
8675b999a6bSDavide Italiano 		softclock_call_cc(c, cc,
8685b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
8695b999a6bSDavide Italiano 		    &mpcalls, &lockcalls, &gcalls,
8705b999a6bSDavide Italiano #endif
8715b999a6bSDavide Italiano 		    0);
8725b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
8735b999a6bSDavide Italiano 		++depth;
8745b999a6bSDavide Italiano #endif
875df8bae1dSRodney W. Grimes 	}
8765b999a6bSDavide Italiano #ifdef CALLOUT_PROFILING
87722ee8c4fSPoul-Henning Kamp 	avg_depth += (depth * 1000 - avg_depth) >> 8;
87822ee8c4fSPoul-Henning Kamp 	avg_mpcalls += (mpcalls * 1000 - avg_mpcalls) >> 8;
87964b9ee20SAttilio Rao 	avg_lockcalls += (lockcalls * 1000 - avg_lockcalls) >> 8;
88022ee8c4fSPoul-Henning Kamp 	avg_gcalls += (gcalls * 1000 - avg_gcalls) >> 8;
8815b999a6bSDavide Italiano #endif
8828d809d50SJeff Roberson 	CC_UNLOCK(cc);
883df8bae1dSRodney W. Grimes }
884df8bae1dSRodney W. Grimes 
885df8bae1dSRodney W. Grimes /*
886df8bae1dSRodney W. Grimes  * timeout --
887df8bae1dSRodney W. Grimes  *	Execute a function after a specified length of time.
888df8bae1dSRodney W. Grimes  *
889df8bae1dSRodney W. Grimes  * untimeout --
890df8bae1dSRodney W. Grimes  *	Cancel previous timeout function call.
891df8bae1dSRodney W. Grimes  *
892ab36c067SJustin T. Gibbs  * callout_handle_init --
893ab36c067SJustin T. Gibbs  *	Initialize a handle so that using it with untimeout is benign.
894ab36c067SJustin T. Gibbs  *
895df8bae1dSRodney W. Grimes  *	See AT&T BCI Driver Reference Manual for specification.  This
896ab36c067SJustin T. Gibbs  *	implementation differs from that one in that although an
897ab36c067SJustin T. Gibbs  *	identification value is returned from timeout, the original
898ab36c067SJustin T. Gibbs  *	arguments to timeout as well as the identifier are used to
899ab36c067SJustin T. Gibbs  *	identify entries for untimeout.
900df8bae1dSRodney W. Grimes  */
901ab36c067SJustin T. Gibbs struct callout_handle
902e392e44cSDavide Italiano timeout(timeout_t *ftn, void *arg, int to_ticks)
903df8bae1dSRodney W. Grimes {
9048d809d50SJeff Roberson 	struct callout_cpu *cc;
905ab36c067SJustin T. Gibbs 	struct callout *new;
906ab36c067SJustin T. Gibbs 	struct callout_handle handle;
907df8bae1dSRodney W. Grimes 
9088d809d50SJeff Roberson 	cc = CC_CPU(timeout_cpu);
9098d809d50SJeff Roberson 	CC_LOCK(cc);
910df8bae1dSRodney W. Grimes 	/* Fill in the next free callout structure. */
9118d809d50SJeff Roberson 	new = SLIST_FIRST(&cc->cc_callfree);
912ab36c067SJustin T. Gibbs 	if (new == NULL)
913ab36c067SJustin T. Gibbs 		/* XXX Attempt to malloc first */
914df8bae1dSRodney W. Grimes 		panic("timeout table full");
9158d809d50SJeff Roberson 	SLIST_REMOVE_HEAD(&cc->cc_callfree, c_links.sle);
916a115fb62SHans Petter Selasky 	callout_reset(new, to_ticks, ftn, arg);
917ab36c067SJustin T. Gibbs 	handle.callout = new;
9188d809d50SJeff Roberson 	CC_UNLOCK(cc);
9198d809d50SJeff Roberson 
920ab36c067SJustin T. Gibbs 	return (handle);
921df8bae1dSRodney W. Grimes }
922df8bae1dSRodney W. Grimes 
923df8bae1dSRodney W. Grimes void
924e392e44cSDavide Italiano untimeout(timeout_t *ftn, void *arg, struct callout_handle handle)
925df8bae1dSRodney W. Grimes {
9268d809d50SJeff Roberson 	struct callout_cpu *cc;
927df8bae1dSRodney W. Grimes 
928ab36c067SJustin T. Gibbs 	/*
929ab36c067SJustin T. Gibbs 	 * Check for a handle that was initialized
930ab36c067SJustin T. Gibbs 	 * by callout_handle_init, but never used
931ab36c067SJustin T. Gibbs 	 * for a real timeout.
932ab36c067SJustin T. Gibbs 	 */
933ab36c067SJustin T. Gibbs 	if (handle.callout == NULL)
934ab36c067SJustin T. Gibbs 		return;
935df8bae1dSRodney W. Grimes 
9368d809d50SJeff Roberson 	cc = callout_lock(handle.callout);
937a115fb62SHans Petter Selasky 	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
9381a26c3c0SHans Petter Selasky 		callout_stop(handle.callout);
939a115fb62SHans Petter Selasky 	CC_UNLOCK(cc);
940df8bae1dSRodney W. Grimes }
941df8bae1dSRodney W. Grimes 
9423c816944SBruce Evans void
943ab36c067SJustin T. Gibbs callout_handle_init(struct callout_handle *handle)
944ab36c067SJustin T. Gibbs {
945ab36c067SJustin T. Gibbs 	handle->callout = NULL;
946ab36c067SJustin T. Gibbs }
947ab36c067SJustin T. Gibbs 
948acc8326dSGarrett Wollman /*
949acc8326dSGarrett Wollman  * New interface; clients allocate their own callout structures.
950acc8326dSGarrett Wollman  *
951acc8326dSGarrett Wollman  * callout_reset() - establish or change a timeout
952acc8326dSGarrett Wollman  * callout_stop() - disestablish a timeout
953acc8326dSGarrett Wollman  * callout_init() - initialize a callout structure so that it can
954acc8326dSGarrett Wollman  *	safely be passed to callout_reset() and callout_stop()
955acc8326dSGarrett Wollman  *
9569b8b58e0SJonathan Lemon  * <sys/callout.h> defines three convenience macros:
957acc8326dSGarrett Wollman  *
95886fd19deSColin Percival  * callout_active() - returns truth if callout has not been stopped,
95986fd19deSColin Percival  *	drained, or deactivated since the last time the callout was
96086fd19deSColin Percival  *	reset.
9619b8b58e0SJonathan Lemon  * callout_pending() - returns truth if callout is still waiting for timeout
9629b8b58e0SJonathan Lemon  * callout_deactivate() - marks the callout as having been serviced
963acc8326dSGarrett Wollman  */
964d04304d1SGleb Smirnoff int
9655b999a6bSDavide Italiano callout_reset_sbt_on(struct callout *c, sbintime_t sbt, sbintime_t precision,
966a115fb62SHans Petter Selasky     void (*ftn)(void *), void *arg, int cpu, int flags)
967acc8326dSGarrett Wollman {
968a115fb62SHans Petter Selasky 	sbintime_t to_sbt, pr;
969a115fb62SHans Petter Selasky 	struct callout_cpu *cc;
970a115fb62SHans Petter Selasky 	int cancelled, direct;
97115b1eb14SRandall Stewart 	int ignore_cpu=0;
972acc8326dSGarrett Wollman 
973a115fb62SHans Petter Selasky 	cancelled = 0;
97415b1eb14SRandall Stewart 	if (cpu == -1) {
97515b1eb14SRandall Stewart 		ignore_cpu = 1;
97615b1eb14SRandall Stewart 	} else if ((cpu >= MAXCPU) ||
977a04d4122SBjoern A. Zeeb 		   ((CC_CPU(cpu))->cc_inited == 0)) {
97815b1eb14SRandall Stewart 		/* Invalid CPU spec */
97915b1eb14SRandall Stewart 		panic("Invalid CPU in callout %d", cpu);
98015b1eb14SRandall Stewart 	}
981a115fb62SHans Petter Selasky 	if (flags & C_ABSOLUTE) {
982a115fb62SHans Petter Selasky 		to_sbt = sbt;
9835b999a6bSDavide Italiano 	} else {
984a115fb62SHans Petter Selasky 		if ((flags & C_HARDCLOCK) && (sbt < tick_sbt))
9855b999a6bSDavide Italiano 			sbt = tick_sbt;
986a115fb62SHans Petter Selasky 		if ((flags & C_HARDCLOCK) ||
9875b999a6bSDavide Italiano #ifdef NO_EVENTTIMERS
9885b999a6bSDavide Italiano 		    sbt >= sbt_timethreshold) {
989a115fb62SHans Petter Selasky 			to_sbt = getsbinuptime();
9905b999a6bSDavide Italiano 
9915b999a6bSDavide Italiano 			/* Add safety belt for the case of hz > 1000. */
992a115fb62SHans Petter Selasky 			to_sbt += tc_tick_sbt - tick_sbt;
9935b999a6bSDavide Italiano #else
9945b999a6bSDavide Italiano 		    sbt >= sbt_tickthreshold) {
9955b999a6bSDavide Italiano 			/*
9965b999a6bSDavide Italiano 			 * Obtain the time of the last hardclock() call on
9975b999a6bSDavide Italiano 			 * this CPU directly from the kern_clocksource.c.
9985b999a6bSDavide Italiano 			 * This value is per-CPU, but it is equal for all
9995b999a6bSDavide Italiano 			 * active ones.
10005b999a6bSDavide Italiano 			 */
10015b999a6bSDavide Italiano #ifdef __LP64__
1002a115fb62SHans Petter Selasky 			to_sbt = DPCPU_GET(hardclocktime);
10035b999a6bSDavide Italiano #else
10045b999a6bSDavide Italiano 			spinlock_enter();
1005a115fb62SHans Petter Selasky 			to_sbt = DPCPU_GET(hardclocktime);
10065b999a6bSDavide Italiano 			spinlock_exit();
10075b999a6bSDavide Italiano #endif
10085b999a6bSDavide Italiano #endif
1009a115fb62SHans Petter Selasky 			if ((flags & C_HARDCLOCK) == 0)
1010a115fb62SHans Petter Selasky 				to_sbt += tick_sbt;
10115b999a6bSDavide Italiano 		} else
1012a115fb62SHans Petter Selasky 			to_sbt = sbinuptime();
1013a115fb62SHans Petter Selasky 		if (SBT_MAX - to_sbt < sbt)
1014a115fb62SHans Petter Selasky 			to_sbt = SBT_MAX;
10151b0c144fSDavide Italiano 		else
1016a115fb62SHans Petter Selasky 			to_sbt += sbt;
1017a115fb62SHans Petter Selasky 		pr = ((C_PRELGET(flags) < 0) ? sbt >> tc_precexp :
1018a115fb62SHans Petter Selasky 		    sbt >> C_PRELGET(flags));
1019a115fb62SHans Petter Selasky 		if (pr > precision)
1020a115fb62SHans Petter Selasky 			precision = pr;
1021a115fb62SHans Petter Selasky 	}
1022a115fb62SHans Petter Selasky 	/*
102366525b2dSRandall Stewart 	 * This flag used to be added by callout_cc_add, but the
102466525b2dSRandall Stewart 	 * first time you call this we could end up with the
102566525b2dSRandall Stewart 	 * wrong direct flag if we don't do it before we add.
102666525b2dSRandall Stewart 	 */
102766525b2dSRandall Stewart 	if (flags & C_DIRECT_EXEC) {
102815b1eb14SRandall Stewart 		direct = 1;
102915b1eb14SRandall Stewart 	} else {
103015b1eb14SRandall Stewart 		direct = 0;
103166525b2dSRandall Stewart 	}
1032a115fb62SHans Petter Selasky 	KASSERT(!direct || c->c_lock == NULL,
1033a115fb62SHans Petter Selasky 	    ("%s: direct callout %p has lock", __func__, c));
1034a115fb62SHans Petter Selasky 	cc = callout_lock(c);
103515b1eb14SRandall Stewart 	/*
103615b1eb14SRandall Stewart 	 * Don't allow migration of pre-allocated callouts lest they
103715b1eb14SRandall Stewart 	 * become unbalanced or handle the case where the user does
103815b1eb14SRandall Stewart 	 * not care.
103915b1eb14SRandall Stewart 	 */
104015b1eb14SRandall Stewart 	if ((c->c_iflags & CALLOUT_LOCAL_ALLOC) ||
104115b1eb14SRandall Stewart 	    ignore_cpu) {
104215b1eb14SRandall Stewart 		cpu = c->c_cpu;
104315b1eb14SRandall Stewart 	}
104415b1eb14SRandall Stewart 
1045d2854fa4SRandall Stewart 	if (cc_exec_curr(cc, direct) == c) {
1046a115fb62SHans Petter Selasky 		/*
1047a115fb62SHans Petter Selasky 		 * We're being asked to reschedule a callout which is
1048a115fb62SHans Petter Selasky 		 * currently in progress.  If there is a lock then we
1049a115fb62SHans Petter Selasky 		 * can cancel the callout if it has not really started.
1050a115fb62SHans Petter Selasky 		 */
1051378d5c6cSAndriy Gapon 		if (c->c_lock != NULL && !cc_exec_cancel(cc, direct))
1052d2854fa4SRandall Stewart 			cancelled = cc_exec_cancel(cc, direct) = true;
105347e42809SGleb Smirnoff 		if (cc_exec_waiting(cc, direct)) {
1054a115fb62SHans Petter Selasky 			/*
1055a115fb62SHans Petter Selasky 			 * Someone has called callout_drain to kill this
1056a115fb62SHans Petter Selasky 			 * callout.  Don't reschedule.
1057a115fb62SHans Petter Selasky 			 */
1058a115fb62SHans Petter Selasky 			CTR4(KTR_CALLOUT, "%s %p func %p arg %p",
1059a115fb62SHans Petter Selasky 			    cancelled ? "cancelled" : "failed to cancel",
1060a115fb62SHans Petter Selasky 			    c, c->c_func, c->c_arg);
1061a115fb62SHans Petter Selasky 			CC_UNLOCK(cc);
1062a115fb62SHans Petter Selasky 			return (cancelled);
1063a115fb62SHans Petter Selasky 		}
1064d2854fa4SRandall Stewart #ifdef SMP
1065d2854fa4SRandall Stewart 		if (callout_migrating(c)) {
1066d2854fa4SRandall Stewart 			/*
1067d2854fa4SRandall Stewart 			 * This only occurs when a second callout_reset_sbt_on
1068d2854fa4SRandall Stewart 			 * is made after a previous one moved it into
1069d2854fa4SRandall Stewart 			 * deferred migration (below). Note we do *not* change
1070d2854fa4SRandall Stewart 			 * the prev_cpu even though the previous target may
1071d2854fa4SRandall Stewart 			 * be different.
1072d2854fa4SRandall Stewart 			 */
1073d2854fa4SRandall Stewart 			cc_migration_cpu(cc, direct) = cpu;
1074d2854fa4SRandall Stewart 			cc_migration_time(cc, direct) = to_sbt;
1075d2854fa4SRandall Stewart 			cc_migration_prec(cc, direct) = precision;
1076d2854fa4SRandall Stewart 			cc_migration_func(cc, direct) = ftn;
1077d2854fa4SRandall Stewart 			cc_migration_arg(cc, direct) = arg;
1078d2854fa4SRandall Stewart 			cancelled = 1;
1079d2854fa4SRandall Stewart 			CC_UNLOCK(cc);
1080d2854fa4SRandall Stewart 			return (cancelled);
1081d2854fa4SRandall Stewart 		}
1082d2854fa4SRandall Stewart #endif
1083a115fb62SHans Petter Selasky 	}
108415b1eb14SRandall Stewart 	if (c->c_iflags & CALLOUT_PENDING) {
108515b1eb14SRandall Stewart 		if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
108666525b2dSRandall Stewart 			if (cc_exec_next(cc) == c)
108766525b2dSRandall Stewart 				cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1088a115fb62SHans Petter Selasky 			LIST_REMOVE(c, c_links.le);
108915b1eb14SRandall Stewart 		} else {
1090a115fb62SHans Petter Selasky 			TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
109115b1eb14SRandall Stewart 		}
1092a115fb62SHans Petter Selasky 		cancelled = 1;
109315b1eb14SRandall Stewart 		c->c_iflags &= ~ CALLOUT_PENDING;
109415b1eb14SRandall Stewart 		c->c_flags &= ~ CALLOUT_ACTIVE;
10958d809d50SJeff Roberson 	}
10961283e9cdSAttilio Rao 
1097a115fb62SHans Petter Selasky #ifdef SMP
1098a115fb62SHans Petter Selasky 	/*
1099a115fb62SHans Petter Selasky 	 * If the callout must migrate try to perform it immediately.
1100a115fb62SHans Petter Selasky 	 * If the callout is currently running, just defer the migration
1101a115fb62SHans Petter Selasky 	 * to a more appropriate moment.
1102a115fb62SHans Petter Selasky 	 */
1103a115fb62SHans Petter Selasky 	if (c->c_cpu != cpu) {
1104d2854fa4SRandall Stewart 		if (cc_exec_curr(cc, direct) == c) {
1105d2854fa4SRandall Stewart 			/*
1106d2854fa4SRandall Stewart 			 * Pending will have been removed since we are
1107d2854fa4SRandall Stewart 			 * actually executing the callout on another
1108d2854fa4SRandall Stewart 			 * CPU. That callout should be waiting on the
1109d2854fa4SRandall Stewart 			 * lock the caller holds. If we set both
1110d2854fa4SRandall Stewart 			 * active/and/pending after we return and the
1111d2854fa4SRandall Stewart 			 * lock on the executing callout proceeds, it
1112d2854fa4SRandall Stewart 			 * will then see pending is true and return.
1113d2854fa4SRandall Stewart 			 * At the return from the actual callout execution
1114d2854fa4SRandall Stewart 			 * the migration will occur in softclock_call_cc
1115d2854fa4SRandall Stewart 			 * and this new callout will be placed on the
1116d2854fa4SRandall Stewart 			 * new CPU via a call to callout_cpu_switch() which
1117d2854fa4SRandall Stewart 			 * will get the lock on the right CPU followed
1118d2854fa4SRandall Stewart 			 * by a call callout_cc_add() which will add it there.
1119d2854fa4SRandall Stewart 			 * (see above in softclock_call_cc()).
1120d2854fa4SRandall Stewart 			 */
1121d2854fa4SRandall Stewart 			cc_migration_cpu(cc, direct) = cpu;
1122d2854fa4SRandall Stewart 			cc_migration_time(cc, direct) = to_sbt;
1123d2854fa4SRandall Stewart 			cc_migration_prec(cc, direct) = precision;
1124d2854fa4SRandall Stewart 			cc_migration_func(cc, direct) = ftn;
1125d2854fa4SRandall Stewart 			cc_migration_arg(cc, direct) = arg;
112615b1eb14SRandall Stewart 			c->c_iflags |= (CALLOUT_DFRMIGRATION | CALLOUT_PENDING);
112715b1eb14SRandall Stewart 			c->c_flags |= CALLOUT_ACTIVE;
1128a115fb62SHans Petter Selasky 			CTR6(KTR_CALLOUT,
1129a115fb62SHans Petter Selasky 		    "migration of %p func %p arg %p in %d.%08x to %u deferred",
1130a115fb62SHans Petter Selasky 			    c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1131a115fb62SHans Petter Selasky 			    (u_int)(to_sbt & 0xffffffff), cpu);
1132a115fb62SHans Petter Selasky 			CC_UNLOCK(cc);
1133a115fb62SHans Petter Selasky 			return (cancelled);
1134a115fb62SHans Petter Selasky 		}
1135a115fb62SHans Petter Selasky 		cc = callout_cpu_switch(c, cc, cpu);
1136a115fb62SHans Petter Selasky 	}
1137a115fb62SHans Petter Selasky #endif
1138a115fb62SHans Petter Selasky 
113966525b2dSRandall Stewart 	callout_cc_add(c, cc, to_sbt, precision, ftn, arg, cpu, flags);
1140a115fb62SHans Petter Selasky 	CTR6(KTR_CALLOUT, "%sscheduled %p func %p arg %p in %d.%08x",
1141a115fb62SHans Petter Selasky 	    cancelled ? "re" : "", c, c->c_func, c->c_arg, (int)(to_sbt >> 32),
1142a115fb62SHans Petter Selasky 	    (u_int)(to_sbt & 0xffffffff));
1143a115fb62SHans Petter Selasky 	CC_UNLOCK(cc);
1144a115fb62SHans Petter Selasky 
1145a115fb62SHans Petter Selasky 	return (cancelled);
1146acc8326dSGarrett Wollman }
1147acc8326dSGarrett Wollman 
11486e0186d5SSam Leffler /*
11496e0186d5SSam Leffler  * Common idioms that can be optimized in the future.
11506e0186d5SSam Leffler  */
11516e0186d5SSam Leffler int
11526e0186d5SSam Leffler callout_schedule_on(struct callout *c, int to_ticks, int cpu)
11536e0186d5SSam Leffler {
11546e0186d5SSam Leffler 	return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, cpu);
11556e0186d5SSam Leffler }
11566e0186d5SSam Leffler 
11576e0186d5SSam Leffler int
11586e0186d5SSam Leffler callout_schedule(struct callout *c, int to_ticks)
11596e0186d5SSam Leffler {
11606e0186d5SSam Leffler 	return callout_reset_on(c, to_ticks, c->c_func, c->c_arg, c->c_cpu);
11616e0186d5SSam Leffler }
11626e0186d5SSam Leffler 
11632c1bb207SColin Percival int
11645db9ed80SKonstantin Belousov _callout_stop_safe(struct callout *c, int flags, void (*drain)(void *))
11652c1bb207SColin Percival {
1166a115fb62SHans Petter Selasky 	struct callout_cpu *cc, *old_cc;
1167a115fb62SHans Petter Selasky 	struct lock_class *class;
1168a115fb62SHans Petter Selasky 	int direct, sq_locked, use_lock;
116947e42809SGleb Smirnoff 	int cancelled, not_on_a_list;
11701283e9cdSAttilio Rao 
11715db9ed80SKonstantin Belousov 	if ((flags & CS_DRAIN) != 0)
11729500dd9fSAdrian Chadd 		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, c->c_lock,
11739500dd9fSAdrian Chadd 		    "calling %s", __func__);
11749500dd9fSAdrian Chadd 
11751283e9cdSAttilio Rao 	/*
1176a115fb62SHans Petter Selasky 	 * Some old subsystems don't hold Giant while running a callout_stop(),
1177a115fb62SHans Petter Selasky 	 * so just discard this check for the moment.
11781283e9cdSAttilio Rao 	 */
11795db9ed80SKonstantin Belousov 	if ((flags & CS_DRAIN) == 0 && c->c_lock != NULL) {
1180a115fb62SHans Petter Selasky 		if (c->c_lock == &Giant.lock_object)
1181a115fb62SHans Petter Selasky 			use_lock = mtx_owned(&Giant);
1182a115fb62SHans Petter Selasky 		else {
1183a115fb62SHans Petter Selasky 			use_lock = 1;
1184a115fb62SHans Petter Selasky 			class = LOCK_CLASS(c->c_lock);
1185a115fb62SHans Petter Selasky 			class->lc_assert(c->c_lock, LA_XLOCKED);
11861283e9cdSAttilio Rao 		}
1187a115fb62SHans Petter Selasky 	} else
1188a115fb62SHans Petter Selasky 		use_lock = 0;
118915b1eb14SRandall Stewart 	if (c->c_iflags & CALLOUT_DIRECT) {
119015b1eb14SRandall Stewart 		direct = 1;
119115b1eb14SRandall Stewart 	} else {
119215b1eb14SRandall Stewart 		direct = 0;
119315b1eb14SRandall Stewart 	}
1194a115fb62SHans Petter Selasky 	sq_locked = 0;
1195a115fb62SHans Petter Selasky 	old_cc = NULL;
1196a115fb62SHans Petter Selasky again:
1197a115fb62SHans Petter Selasky 	cc = callout_lock(c);
1198a115fb62SHans Petter Selasky 
119915b1eb14SRandall Stewart 	if ((c->c_iflags & (CALLOUT_DFRMIGRATION | CALLOUT_PENDING)) ==
120015b1eb14SRandall Stewart 	    (CALLOUT_DFRMIGRATION | CALLOUT_PENDING) &&
120115b1eb14SRandall Stewart 	    ((c->c_flags & CALLOUT_ACTIVE) == CALLOUT_ACTIVE)) {
1202d2854fa4SRandall Stewart 		/*
1203d2854fa4SRandall Stewart 		 * Special case where this slipped in while we
1204d2854fa4SRandall Stewart 		 * were migrating *as* the callout is about to
1205d2854fa4SRandall Stewart 		 * execute. The caller probably holds the lock
1206d2854fa4SRandall Stewart 		 * the callout wants.
1207d2854fa4SRandall Stewart 		 *
1208d2854fa4SRandall Stewart 		 * Get rid of the migration first. Then set
1209d2854fa4SRandall Stewart 		 * the flag that tells this code *not* to
1210d2854fa4SRandall Stewart 		 * try to remove it from any lists (its not
1211d2854fa4SRandall Stewart 		 * on one yet). When the callout wheel runs,
1212d2854fa4SRandall Stewart 		 * it will ignore this callout.
1213d2854fa4SRandall Stewart 		 */
121415b1eb14SRandall Stewart 		c->c_iflags &= ~CALLOUT_PENDING;
121515b1eb14SRandall Stewart 		c->c_flags &= ~CALLOUT_ACTIVE;
1216d2854fa4SRandall Stewart 		not_on_a_list = 1;
1217d2854fa4SRandall Stewart 	} else {
1218d2854fa4SRandall Stewart 		not_on_a_list = 0;
1219d2854fa4SRandall Stewart 	}
1220d2854fa4SRandall Stewart 
1221a115fb62SHans Petter Selasky 	/*
1222a115fb62SHans Petter Selasky 	 * If the callout was migrating while the callout cpu lock was
1223a115fb62SHans Petter Selasky 	 * dropped,  just drop the sleepqueue lock and check the states
1224a115fb62SHans Petter Selasky 	 * again.
1225a115fb62SHans Petter Selasky 	 */
1226a115fb62SHans Petter Selasky 	if (sq_locked != 0 && cc != old_cc) {
1227a115fb62SHans Petter Selasky #ifdef SMP
1228a115fb62SHans Petter Selasky 		CC_UNLOCK(cc);
1229d2854fa4SRandall Stewart 		sleepq_release(&cc_exec_waiting(old_cc, direct));
1230a115fb62SHans Petter Selasky 		sq_locked = 0;
1231a115fb62SHans Petter Selasky 		old_cc = NULL;
1232a115fb62SHans Petter Selasky 		goto again;
1233a115fb62SHans Petter Selasky #else
1234a115fb62SHans Petter Selasky 		panic("migration should not happen");
1235a115fb62SHans Petter Selasky #endif
1236a115fb62SHans Petter Selasky 	}
123747e42809SGleb Smirnoff 
1238a115fb62SHans Petter Selasky 	/*
123947e42809SGleb Smirnoff 	 * If the callout is running, try to stop it or drain it.
1240a115fb62SHans Petter Selasky 	 */
124147e42809SGleb Smirnoff 	if (cc_exec_curr(cc, direct) == c) {
1242a115fb62SHans Petter Selasky 		/*
124347e42809SGleb Smirnoff 		 * Succeed we to stop it or not, we must clear the
124447e42809SGleb Smirnoff 		 * active flag - this is what API users expect.
1245a115fb62SHans Petter Selasky 		 */
124618b4fd62SRandall Stewart 		c->c_flags &= ~CALLOUT_ACTIVE;
124747e42809SGleb Smirnoff 
12485db9ed80SKonstantin Belousov 		if ((flags & CS_DRAIN) != 0) {
1249a115fb62SHans Petter Selasky 			/*
1250a115fb62SHans Petter Selasky 			 * The current callout is running (or just
1251a115fb62SHans Petter Selasky 			 * about to run) and blocking is allowed, so
1252a115fb62SHans Petter Selasky 			 * just wait for the current invocation to
1253a115fb62SHans Petter Selasky 			 * finish.
1254a115fb62SHans Petter Selasky 			 */
1255d2854fa4SRandall Stewart 			while (cc_exec_curr(cc, direct) == c) {
1256a115fb62SHans Petter Selasky 				/*
1257a115fb62SHans Petter Selasky 				 * Use direct calls to sleepqueue interface
1258a115fb62SHans Petter Selasky 				 * instead of cv/msleep in order to avoid
1259a115fb62SHans Petter Selasky 				 * a LOR between cc_lock and sleepqueue
1260a115fb62SHans Petter Selasky 				 * chain spinlocks.  This piece of code
1261a115fb62SHans Petter Selasky 				 * emulates a msleep_spin() call actually.
1262a115fb62SHans Petter Selasky 				 *
1263a115fb62SHans Petter Selasky 				 * If we already have the sleepqueue chain
1264a115fb62SHans Petter Selasky 				 * locked, then we can safely block.  If we
1265a115fb62SHans Petter Selasky 				 * don't already have it locked, however,
1266a115fb62SHans Petter Selasky 				 * we have to drop the cc_lock to lock
1267a115fb62SHans Petter Selasky 				 * it.  This opens several races, so we
1268a115fb62SHans Petter Selasky 				 * restart at the beginning once we have
1269a115fb62SHans Petter Selasky 				 * both locks.  If nothing has changed, then
1270a115fb62SHans Petter Selasky 				 * we will end up back here with sq_locked
1271a115fb62SHans Petter Selasky 				 * set.
1272a115fb62SHans Petter Selasky 				 */
1273a115fb62SHans Petter Selasky 				if (!sq_locked) {
1274a115fb62SHans Petter Selasky 					CC_UNLOCK(cc);
1275a115fb62SHans Petter Selasky 					sleepq_lock(
1276d2854fa4SRandall Stewart 					    &cc_exec_waiting(cc, direct));
1277a115fb62SHans Petter Selasky 					sq_locked = 1;
1278a115fb62SHans Petter Selasky 					old_cc = cc;
1279a115fb62SHans Petter Selasky 					goto again;
1280a115fb62SHans Petter Selasky 				}
128147e42809SGleb Smirnoff 
1282a115fb62SHans Petter Selasky 				/*
1283a115fb62SHans Petter Selasky 				 * Migration could be cancelled here, but
1284a115fb62SHans Petter Selasky 				 * as long as it is still not sure when it
1285a115fb62SHans Petter Selasky 				 * will be packed up, just let softclock()
1286a115fb62SHans Petter Selasky 				 * take care of it.
1287a115fb62SHans Petter Selasky 				 */
1288d2854fa4SRandall Stewart 				cc_exec_waiting(cc, direct) = true;
1289a115fb62SHans Petter Selasky 				DROP_GIANT();
1290a115fb62SHans Petter Selasky 				CC_UNLOCK(cc);
1291a115fb62SHans Petter Selasky 				sleepq_add(
1292d2854fa4SRandall Stewart 				    &cc_exec_waiting(cc, direct),
1293a115fb62SHans Petter Selasky 				    &cc->cc_lock.lock_object, "codrain",
1294a115fb62SHans Petter Selasky 				    SLEEPQ_SLEEP, 0);
1295a115fb62SHans Petter Selasky 				sleepq_wait(
1296d2854fa4SRandall Stewart 				    &cc_exec_waiting(cc, direct),
1297a115fb62SHans Petter Selasky 					     0);
1298a115fb62SHans Petter Selasky 				sq_locked = 0;
1299a115fb62SHans Petter Selasky 				old_cc = NULL;
1300a115fb62SHans Petter Selasky 
1301a115fb62SHans Petter Selasky 				/* Reacquire locks previously released. */
1302a115fb62SHans Petter Selasky 				PICKUP_GIANT();
1303a115fb62SHans Petter Selasky 				CC_LOCK(cc);
1304a115fb62SHans Petter Selasky 			}
1305a115fb62SHans Petter Selasky 		} else if (use_lock &&
130618b4fd62SRandall Stewart 			   !cc_exec_cancel(cc, direct) && (drain == NULL)) {
1307d2854fa4SRandall Stewart 
1308a115fb62SHans Petter Selasky 			/*
1309a115fb62SHans Petter Selasky 			 * The current callout is waiting for its
1310a115fb62SHans Petter Selasky 			 * lock which we hold.  Cancel the callout
1311a115fb62SHans Petter Selasky 			 * and return.  After our caller drops the
1312a115fb62SHans Petter Selasky 			 * lock, the callout will be skipped in
131318b4fd62SRandall Stewart 			 * softclock(). This *only* works with a
131418b4fd62SRandall Stewart 			 * callout_stop() *not* callout_drain() or
131518b4fd62SRandall Stewart 			 * callout_async_drain().
1316a115fb62SHans Petter Selasky 			 */
1317d2854fa4SRandall Stewart 			cc_exec_cancel(cc, direct) = true;
1318a115fb62SHans Petter Selasky 			CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
1319a115fb62SHans Petter Selasky 			    c, c->c_func, c->c_arg);
1320a115fb62SHans Petter Selasky 			KASSERT(!cc_cce_migrating(cc, direct),
1321a115fb62SHans Petter Selasky 			    ("callout wrongly scheduled for migration"));
132215b1eb14SRandall Stewart 			if (callout_migrating(c)) {
132315b1eb14SRandall Stewart 				c->c_iflags &= ~CALLOUT_DFRMIGRATION;
132415b1eb14SRandall Stewart #ifdef SMP
132515b1eb14SRandall Stewart 				cc_migration_cpu(cc, direct) = CPUBLOCK;
132615b1eb14SRandall Stewart 				cc_migration_time(cc, direct) = 0;
132715b1eb14SRandall Stewart 				cc_migration_prec(cc, direct) = 0;
132815b1eb14SRandall Stewart 				cc_migration_func(cc, direct) = NULL;
132915b1eb14SRandall Stewart 				cc_migration_arg(cc, direct) = NULL;
133015b1eb14SRandall Stewart #endif
133115b1eb14SRandall Stewart 			}
1332a115fb62SHans Petter Selasky 			CC_UNLOCK(cc);
1333a115fb62SHans Petter Selasky 			KASSERT(!sq_locked, ("sleepqueue chain locked"));
1334a115fb62SHans Petter Selasky 			return (1);
1335d2854fa4SRandall Stewart 		} else if (callout_migrating(c)) {
1336d2854fa4SRandall Stewart 			/*
1337d2854fa4SRandall Stewart 			 * The callout is currently being serviced
1338d2854fa4SRandall Stewart 			 * and the "next" callout is scheduled at
1339d2854fa4SRandall Stewart 			 * its completion with a migration. We remove
1340d2854fa4SRandall Stewart 			 * the migration flag so it *won't* get rescheduled,
1341d2854fa4SRandall Stewart 			 * but we can't stop the one thats running so
1342d2854fa4SRandall Stewart 			 * we return 0.
1343d2854fa4SRandall Stewart 			 */
134415b1eb14SRandall Stewart 			c->c_iflags &= ~CALLOUT_DFRMIGRATION;
1345d2854fa4SRandall Stewart #ifdef SMP
1346d2854fa4SRandall Stewart 			/*
1347d2854fa4SRandall Stewart 			 * We can't call cc_cce_cleanup here since
1348d2854fa4SRandall Stewart 			 * if we do it will remove .ce_curr and
1349d2854fa4SRandall Stewart 			 * its still running. This will prevent a
1350d2854fa4SRandall Stewart 			 * reschedule of the callout when the
1351d2854fa4SRandall Stewart 			 * execution completes.
1352d2854fa4SRandall Stewart 			 */
1353d2854fa4SRandall Stewart 			cc_migration_cpu(cc, direct) = CPUBLOCK;
1354d2854fa4SRandall Stewart 			cc_migration_time(cc, direct) = 0;
1355d2854fa4SRandall Stewart 			cc_migration_prec(cc, direct) = 0;
1356d2854fa4SRandall Stewart 			cc_migration_func(cc, direct) = NULL;
1357d2854fa4SRandall Stewart 			cc_migration_arg(cc, direct) = NULL;
1358d2854fa4SRandall Stewart #endif
1359a115fb62SHans Petter Selasky 			CTR3(KTR_CALLOUT, "postponing stop %p func %p arg %p",
1360a115fb62SHans Petter Selasky 			    c, c->c_func, c->c_arg);
136118b4fd62SRandall Stewart  			if (drain) {
136218b4fd62SRandall Stewart 				cc_exec_drain(cc, direct) = drain;
136318b4fd62SRandall Stewart 			}
1364a115fb62SHans Petter Selasky 			CC_UNLOCK(cc);
1365d153eeeeSGleb Smirnoff 			return ((flags & CS_EXECUTING) != 0);
1366a115fb62SHans Petter Selasky 		}
1367a115fb62SHans Petter Selasky 		CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
1368a115fb62SHans Petter Selasky 		    c, c->c_func, c->c_arg);
136918b4fd62SRandall Stewart 		if (drain) {
137018b4fd62SRandall Stewart 			cc_exec_drain(cc, direct) = drain;
137118b4fd62SRandall Stewart 		}
13723d84a188SRandall Stewart 		KASSERT(!sq_locked, ("sleepqueue chain still locked"));
137347e42809SGleb Smirnoff 		cancelled = ((flags & CS_EXECUTING) != 0);
137447e42809SGleb Smirnoff 	} else
137547e42809SGleb Smirnoff 		cancelled = 1;
137647e42809SGleb Smirnoff 
13773d84a188SRandall Stewart 	if (sq_locked)
13783d84a188SRandall Stewart 		sleepq_release(&cc_exec_waiting(cc, direct));
1379d153eeeeSGleb Smirnoff 
138047e42809SGleb Smirnoff 	if ((c->c_iflags & CALLOUT_PENDING) == 0) {
138147e42809SGleb Smirnoff 		CTR3(KTR_CALLOUT, "failed to stop %p func %p arg %p",
138247e42809SGleb Smirnoff 		    c, c->c_func, c->c_arg);
1383*9f339124SGleb Smirnoff 		/*
1384*9f339124SGleb Smirnoff 		 * For not scheduled and not executing callout return
1385*9f339124SGleb Smirnoff 		 * negative value.
1386*9f339124SGleb Smirnoff 		 */
1387*9f339124SGleb Smirnoff 		if (cc_exec_curr(cc, direct) != c)
1388*9f339124SGleb Smirnoff 			cancelled = -1;
138947e42809SGleb Smirnoff 		CC_UNLOCK(cc);
1390*9f339124SGleb Smirnoff 		return (cancelled);
139147e42809SGleb Smirnoff 	}
139247e42809SGleb Smirnoff 
139315b1eb14SRandall Stewart 	c->c_iflags &= ~CALLOUT_PENDING;
139415b1eb14SRandall Stewart 	c->c_flags &= ~CALLOUT_ACTIVE;
13951283e9cdSAttilio Rao 
139668a57ebfSGleb Smirnoff 	CTR3(KTR_CALLOUT, "cancelled %p func %p arg %p",
139768a57ebfSGleb Smirnoff 	    c, c->c_func, c->c_arg);
1398d2854fa4SRandall Stewart 	if (not_on_a_list == 0) {
139915b1eb14SRandall Stewart 		if ((c->c_iflags & CALLOUT_PROCESSED) == 0) {
140066525b2dSRandall Stewart 			if (cc_exec_next(cc) == c)
140166525b2dSRandall Stewart 				cc_exec_next(cc) = LIST_NEXT(c, c_links.le);
1402a115fb62SHans Petter Selasky 			LIST_REMOVE(c, c_links.le);
140315b1eb14SRandall Stewart 		} else {
1404a115fb62SHans Petter Selasky 			TAILQ_REMOVE(&cc->cc_expireq, c, c_links.tqe);
1405d2854fa4SRandall Stewart 		}
140615b1eb14SRandall Stewart 	}
1407a115fb62SHans Petter Selasky 	callout_cc_del(c, cc);
1408a115fb62SHans Petter Selasky 	CC_UNLOCK(cc);
140947e42809SGleb Smirnoff 	return (cancelled);
1410acc8326dSGarrett Wollman }
1411acc8326dSGarrett Wollman 
1412acc8326dSGarrett Wollman void
1413e392e44cSDavide Italiano callout_init(struct callout *c, int mpsafe)
1414acc8326dSGarrett Wollman {
1415a115fb62SHans Petter Selasky 	bzero(c, sizeof *c);
141698c926b2SIan Dowse 	if (mpsafe) {
1417a115fb62SHans Petter Selasky 		c->c_lock = NULL;
141815b1eb14SRandall Stewart 		c->c_iflags = CALLOUT_RETURNUNLOCKED;
141998c926b2SIan Dowse 	} else {
1420a115fb62SHans Petter Selasky 		c->c_lock = &Giant.lock_object;
142115b1eb14SRandall Stewart 		c->c_iflags = 0;
142298c926b2SIan Dowse 	}
1423a115fb62SHans Petter Selasky 	c->c_cpu = timeout_cpu;
142498c926b2SIan Dowse }
142598c926b2SIan Dowse 
142698c926b2SIan Dowse void
1427e392e44cSDavide Italiano _callout_init_lock(struct callout *c, struct lock_object *lock, int flags)
142898c926b2SIan Dowse {
142998c926b2SIan Dowse 	bzero(c, sizeof *c);
143064b9ee20SAttilio Rao 	c->c_lock = lock;
1431a115fb62SHans Petter Selasky 	KASSERT((flags & ~(CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK)) == 0,
1432a115fb62SHans Petter Selasky 	    ("callout_init_lock: bad flags %d", flags));
1433a115fb62SHans Petter Selasky 	KASSERT(lock != NULL || (flags & CALLOUT_RETURNUNLOCKED) == 0,
1434a115fb62SHans Petter Selasky 	    ("callout_init_lock: CALLOUT_RETURNUNLOCKED with no lock"));
1435a115fb62SHans Petter Selasky 	KASSERT(lock == NULL || !(LOCK_CLASS(lock)->lc_flags &
1436a115fb62SHans Petter Selasky 	    (LC_SPINLOCK | LC_SLEEPABLE)), ("%s: invalid lock class",
1437a115fb62SHans Petter Selasky 	    __func__));
143815b1eb14SRandall Stewart 	c->c_iflags = flags & (CALLOUT_RETURNUNLOCKED | CALLOUT_SHAREDLOCK);
14398d809d50SJeff Roberson 	c->c_cpu = timeout_cpu;
1440acc8326dSGarrett Wollman }
1441acc8326dSGarrett Wollman 
1442e1d6dc65SNate Williams #ifdef APM_FIXUP_CALLTODO
1443e1d6dc65SNate Williams /*
1444e1d6dc65SNate Williams  * Adjust the kernel calltodo timeout list.  This routine is used after
1445e1d6dc65SNate Williams  * an APM resume to recalculate the calltodo timer list values with the
1446e1d6dc65SNate Williams  * number of hz's we have been sleeping.  The next hardclock() will detect
1447e1d6dc65SNate Williams  * that there are fired timers and run softclock() to execute them.
1448e1d6dc65SNate Williams  *
1449e1d6dc65SNate Williams  * Please note, I have not done an exhaustive analysis of what code this
1450e1d6dc65SNate Williams  * might break.  I am motivated to have my select()'s and alarm()'s that
1451e1d6dc65SNate Williams  * have expired during suspend firing upon resume so that the applications
1452e1d6dc65SNate Williams  * which set the timer can do the maintanence the timer was for as close
1453e1d6dc65SNate Williams  * as possible to the originally intended time.  Testing this code for a
1454e1d6dc65SNate Williams  * week showed that resuming from a suspend resulted in 22 to 25 timers
1455e3043798SPedro F. Giffuni  * firing, which seemed independent on whether the suspend was 2 hours or
1456e1d6dc65SNate Williams  * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
1457e1d6dc65SNate Williams  */
1458e1d6dc65SNate Williams void
1459e392e44cSDavide Italiano adjust_timeout_calltodo(struct timeval *time_change)
1460e1d6dc65SNate Williams {
1461e1d6dc65SNate Williams 	register struct callout *p;
1462e1d6dc65SNate Williams 	unsigned long delta_ticks;
1463e1d6dc65SNate Williams 
1464e1d6dc65SNate Williams 	/*
1465e1d6dc65SNate Williams 	 * How many ticks were we asleep?
1466c8b47828SBruce Evans 	 * (stolen from tvtohz()).
1467e1d6dc65SNate Williams 	 */
1468e1d6dc65SNate Williams 
1469e1d6dc65SNate Williams 	/* Don't do anything */
1470e1d6dc65SNate Williams 	if (time_change->tv_sec < 0)
1471e1d6dc65SNate Williams 		return;
1472e1d6dc65SNate Williams 	else if (time_change->tv_sec <= LONG_MAX / 1000000)
147355e0987aSPedro F. Giffuni 		delta_ticks = howmany(time_change->tv_sec * 1000000 +
147455e0987aSPedro F. Giffuni 		    time_change->tv_usec, tick) + 1;
1475e1d6dc65SNate Williams 	else if (time_change->tv_sec <= LONG_MAX / hz)
1476e1d6dc65SNate Williams 		delta_ticks = time_change->tv_sec * hz +
147755e0987aSPedro F. Giffuni 		    howmany(time_change->tv_usec, tick) + 1;
1478e1d6dc65SNate Williams 	else
1479e1d6dc65SNate Williams 		delta_ticks = LONG_MAX;
1480e1d6dc65SNate Williams 
1481e1d6dc65SNate Williams 	if (delta_ticks > INT_MAX)
1482e1d6dc65SNate Williams 		delta_ticks = INT_MAX;
1483e1d6dc65SNate Williams 
1484e1d6dc65SNate Williams 	/*
1485e1d6dc65SNate Williams 	 * Now rip through the timer calltodo list looking for timers
1486e1d6dc65SNate Williams 	 * to expire.
1487e1d6dc65SNate Williams 	 */
1488e1d6dc65SNate Williams 
1489e1d6dc65SNate Williams 	/* don't collide with softclock() */
14908d809d50SJeff Roberson 	CC_LOCK(cc);
1491e1d6dc65SNate Williams 	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
1492e1d6dc65SNate Williams 		p->c_time -= delta_ticks;
1493e1d6dc65SNate Williams 
1494e1d6dc65SNate Williams 		/* Break if the timer had more time on it than delta_ticks */
1495e1d6dc65SNate Williams 		if (p->c_time > 0)
1496e1d6dc65SNate Williams 			break;
1497e1d6dc65SNate Williams 
1498e1d6dc65SNate Williams 		/* take back the ticks the timer didn't use (p->c_time <= 0) */
1499e1d6dc65SNate Williams 		delta_ticks = -p->c_time;
1500e1d6dc65SNate Williams 	}
15018d809d50SJeff Roberson 	CC_UNLOCK(cc);
1502e1d6dc65SNate Williams 
1503e1d6dc65SNate Williams 	return;
1504e1d6dc65SNate Williams }
1505e1d6dc65SNate Williams #endif /* APM_FIXUP_CALLTODO */
15065b999a6bSDavide Italiano 
15075b999a6bSDavide Italiano static int
15085b999a6bSDavide Italiano flssbt(sbintime_t sbt)
15095b999a6bSDavide Italiano {
15105b999a6bSDavide Italiano 
15115b999a6bSDavide Italiano 	sbt += (uint64_t)sbt >> 1;
15125b999a6bSDavide Italiano 	if (sizeof(long) >= sizeof(sbintime_t))
15135b999a6bSDavide Italiano 		return (flsl(sbt));
15145b999a6bSDavide Italiano 	if (sbt >= SBT_1S)
15155b999a6bSDavide Italiano 		return (flsl(((uint64_t)sbt) >> 32) + 32);
15165b999a6bSDavide Italiano 	return (flsl(sbt));
15175b999a6bSDavide Italiano }
15185b999a6bSDavide Italiano 
15195b999a6bSDavide Italiano /*
15205b999a6bSDavide Italiano  * Dump immediate statistic snapshot of the scheduled callouts.
15215b999a6bSDavide Italiano  */
15225b999a6bSDavide Italiano static int
15235b999a6bSDavide Italiano sysctl_kern_callout_stat(SYSCTL_HANDLER_ARGS)
15245b999a6bSDavide Italiano {
15255b999a6bSDavide Italiano 	struct callout *tmp;
15265b999a6bSDavide Italiano 	struct callout_cpu *cc;
15275b999a6bSDavide Italiano 	struct callout_list *sc;
15285b999a6bSDavide Italiano 	sbintime_t maxpr, maxt, medpr, medt, now, spr, st, t;
15295b999a6bSDavide Italiano 	int ct[64], cpr[64], ccpbk[32];
15305b999a6bSDavide Italiano 	int error, val, i, count, tcum, pcum, maxc, c, medc;
15315b999a6bSDavide Italiano #ifdef SMP
15325b999a6bSDavide Italiano 	int cpu;
15335b999a6bSDavide Italiano #endif
15345b999a6bSDavide Italiano 
15355b999a6bSDavide Italiano 	val = 0;
15365b999a6bSDavide Italiano 	error = sysctl_handle_int(oidp, &val, 0, req);
15375b999a6bSDavide Italiano 	if (error != 0 || req->newptr == NULL)
15385b999a6bSDavide Italiano 		return (error);
15395b999a6bSDavide Italiano 	count = maxc = 0;
15405b999a6bSDavide Italiano 	st = spr = maxt = maxpr = 0;
15415b999a6bSDavide Italiano 	bzero(ccpbk, sizeof(ccpbk));
15425b999a6bSDavide Italiano 	bzero(ct, sizeof(ct));
15435b999a6bSDavide Italiano 	bzero(cpr, sizeof(cpr));
15445b999a6bSDavide Italiano 	now = sbinuptime();
15455b999a6bSDavide Italiano #ifdef SMP
15465b999a6bSDavide Italiano 	CPU_FOREACH(cpu) {
15475b999a6bSDavide Italiano 		cc = CC_CPU(cpu);
15485b999a6bSDavide Italiano #else
15495b999a6bSDavide Italiano 		cc = CC_CPU(timeout_cpu);
15505b999a6bSDavide Italiano #endif
15515b999a6bSDavide Italiano 		CC_LOCK(cc);
15525b999a6bSDavide Italiano 		for (i = 0; i < callwheelsize; i++) {
15535b999a6bSDavide Italiano 			sc = &cc->cc_callwheel[i];
15545b999a6bSDavide Italiano 			c = 0;
15555b999a6bSDavide Italiano 			LIST_FOREACH(tmp, sc, c_links.le) {
15565b999a6bSDavide Italiano 				c++;
15575b999a6bSDavide Italiano 				t = tmp->c_time - now;
15585b999a6bSDavide Italiano 				if (t < 0)
15595b999a6bSDavide Italiano 					t = 0;
15605b999a6bSDavide Italiano 				st += t / SBT_1US;
15615b999a6bSDavide Italiano 				spr += tmp->c_precision / SBT_1US;
15625b999a6bSDavide Italiano 				if (t > maxt)
15635b999a6bSDavide Italiano 					maxt = t;
15645b999a6bSDavide Italiano 				if (tmp->c_precision > maxpr)
15655b999a6bSDavide Italiano 					maxpr = tmp->c_precision;
15665b999a6bSDavide Italiano 				ct[flssbt(t)]++;
15675b999a6bSDavide Italiano 				cpr[flssbt(tmp->c_precision)]++;
15685b999a6bSDavide Italiano 			}
15695b999a6bSDavide Italiano 			if (c > maxc)
15705b999a6bSDavide Italiano 				maxc = c;
15715b999a6bSDavide Italiano 			ccpbk[fls(c + c / 2)]++;
15725b999a6bSDavide Italiano 			count += c;
15735b999a6bSDavide Italiano 		}
15745b999a6bSDavide Italiano 		CC_UNLOCK(cc);
15755b999a6bSDavide Italiano #ifdef SMP
15765b999a6bSDavide Italiano 	}
15775b999a6bSDavide Italiano #endif
15785b999a6bSDavide Italiano 
15795b999a6bSDavide Italiano 	for (i = 0, tcum = 0; i < 64 && tcum < count / 2; i++)
15805b999a6bSDavide Italiano 		tcum += ct[i];
15815b999a6bSDavide Italiano 	medt = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
15825b999a6bSDavide Italiano 	for (i = 0, pcum = 0; i < 64 && pcum < count / 2; i++)
15835b999a6bSDavide Italiano 		pcum += cpr[i];
15845b999a6bSDavide Italiano 	medpr = (i >= 2) ? (((sbintime_t)1) << (i - 2)) : 0;
15855b999a6bSDavide Italiano 	for (i = 0, c = 0; i < 32 && c < count / 2; i++)
15865b999a6bSDavide Italiano 		c += ccpbk[i];
15875b999a6bSDavide Italiano 	medc = (i >= 2) ? (1 << (i - 2)) : 0;
15885b999a6bSDavide Italiano 
15895b999a6bSDavide Italiano 	printf("Scheduled callouts statistic snapshot:\n");
15905b999a6bSDavide Italiano 	printf("  Callouts: %6d  Buckets: %6d*%-3d  Bucket size: 0.%06ds\n",
15915b999a6bSDavide Italiano 	    count, callwheelsize, mp_ncpus, 1000000 >> CC_HASH_SHIFT);
15925b999a6bSDavide Italiano 	printf("  C/Bk: med %5d         avg %6d.%06jd  max %6d\n",
15935b999a6bSDavide Italiano 	    medc,
15945b999a6bSDavide Italiano 	    count / callwheelsize / mp_ncpus,
15955b999a6bSDavide Italiano 	    (uint64_t)count * 1000000 / callwheelsize / mp_ncpus % 1000000,
15965b999a6bSDavide Italiano 	    maxc);
15975b999a6bSDavide Italiano 	printf("  Time: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
15985b999a6bSDavide Italiano 	    medt / SBT_1S, (medt & 0xffffffff) * 1000000 >> 32,
15995b999a6bSDavide Italiano 	    (st / count) / 1000000, (st / count) % 1000000,
16005b999a6bSDavide Italiano 	    maxt / SBT_1S, (maxt & 0xffffffff) * 1000000 >> 32);
16015b999a6bSDavide Italiano 	printf("  Prec: med %5jd.%06jds avg %6jd.%06jds max %6jd.%06jds\n",
16025b999a6bSDavide Italiano 	    medpr / SBT_1S, (medpr & 0xffffffff) * 1000000 >> 32,
16035b999a6bSDavide Italiano 	    (spr / count) / 1000000, (spr / count) % 1000000,
16045b999a6bSDavide Italiano 	    maxpr / SBT_1S, (maxpr & 0xffffffff) * 1000000 >> 32);
16055b999a6bSDavide Italiano 	printf("  Distribution:       \tbuckets\t   time\t   tcum\t"
16065b999a6bSDavide Italiano 	    "   prec\t   pcum\n");
16075b999a6bSDavide Italiano 	for (i = 0, tcum = pcum = 0; i < 64; i++) {
16085b999a6bSDavide Italiano 		if (ct[i] == 0 && cpr[i] == 0)
16095b999a6bSDavide Italiano 			continue;
16105b999a6bSDavide Italiano 		t = (i != 0) ? (((sbintime_t)1) << (i - 1)) : 0;
16115b999a6bSDavide Italiano 		tcum += ct[i];
16125b999a6bSDavide Italiano 		pcum += cpr[i];
16135b999a6bSDavide Italiano 		printf("  %10jd.%06jds\t 2**%d\t%7d\t%7d\t%7d\t%7d\n",
16145b999a6bSDavide Italiano 		    t / SBT_1S, (t & 0xffffffff) * 1000000 >> 32,
16155b999a6bSDavide Italiano 		    i - 1 - (32 - CC_HASH_SHIFT),
16165b999a6bSDavide Italiano 		    ct[i], tcum, cpr[i], pcum);
16175b999a6bSDavide Italiano 	}
16185b999a6bSDavide Italiano 	return (error);
16195b999a6bSDavide Italiano }
16205b999a6bSDavide Italiano SYSCTL_PROC(_kern, OID_AUTO, callout_stat,
16215b999a6bSDavide Italiano     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
16225b999a6bSDavide Italiano     0, 0, sysctl_kern_callout_stat, "I",
16235b999a6bSDavide Italiano     "Dump immediate statistic snapshot of the scheduled callouts");
162447e42809SGleb Smirnoff 
16253af72c11SBjoern A. Zeeb #ifdef DDB
16263af72c11SBjoern A. Zeeb static void
16273af72c11SBjoern A. Zeeb _show_callout(struct callout *c)
16283af72c11SBjoern A. Zeeb {
16293af72c11SBjoern A. Zeeb 
16303af72c11SBjoern A. Zeeb 	db_printf("callout %p\n", c);
16313af72c11SBjoern A. Zeeb #define	C_DB_PRINTF(f, e)	db_printf("   %s = " f "\n", #e, c->e);
16323af72c11SBjoern A. Zeeb 	db_printf("   &c_links = %p\n", &(c->c_links));
16333af72c11SBjoern A. Zeeb 	C_DB_PRINTF("%" PRId64,	c_time);
16343af72c11SBjoern A. Zeeb 	C_DB_PRINTF("%" PRId64,	c_precision);
16353af72c11SBjoern A. Zeeb 	C_DB_PRINTF("%p",	c_arg);
16363af72c11SBjoern A. Zeeb 	C_DB_PRINTF("%p",	c_func);
16373af72c11SBjoern A. Zeeb 	C_DB_PRINTF("%p",	c_lock);
16383af72c11SBjoern A. Zeeb 	C_DB_PRINTF("%#x",	c_flags);
16393af72c11SBjoern A. Zeeb 	C_DB_PRINTF("%#x",	c_iflags);
16403af72c11SBjoern A. Zeeb 	C_DB_PRINTF("%d",	c_cpu);
16413af72c11SBjoern A. Zeeb #undef	C_DB_PRINTF
16423af72c11SBjoern A. Zeeb }
16433af72c11SBjoern A. Zeeb 
16443af72c11SBjoern A. Zeeb DB_SHOW_COMMAND(callout, db_show_callout)
16453af72c11SBjoern A. Zeeb {
16463af72c11SBjoern A. Zeeb 
16473af72c11SBjoern A. Zeeb 	if (!have_addr) {
16483af72c11SBjoern A. Zeeb 		db_printf("usage: show callout <struct callout *>\n");
16493af72c11SBjoern A. Zeeb 		return;
16503af72c11SBjoern A. Zeeb 	}
16513af72c11SBjoern A. Zeeb 
16523af72c11SBjoern A. Zeeb 	_show_callout((struct callout *)addr);
16533af72c11SBjoern A. Zeeb }
16543af72c11SBjoern A. Zeeb #endif /* DDB */
1655