kern_timeout.c revision 102936
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes * (c) UNIX System Laboratories, Inc.
51541Srgrimes * All or some portions of this file are derived from material licensed
61541Srgrimes * to the University of California by American Telephone and Telegraph
71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81541Srgrimes * the permission of UNIX System Laboratories, Inc.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 3. All advertising materials mentioning features or use of this software
191541Srgrimes *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
211541Srgrimes *	California, Berkeley and its contributors.
221541Srgrimes * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361541Srgrimes * SUCH DAMAGE.
371541Srgrimes *
3844510Swollman *	From: @(#)kern_clock.c	8.5 (Berkeley) 1/21/94
3950477Speter * $FreeBSD: head/sys/kern/kern_timeout.c 102936 2002-09-04 20:05:00Z phk $
401541Srgrimes */
411541Srgrimes
421541Srgrimes#include <sys/param.h>
431541Srgrimes#include <sys/systm.h>
4433392Sphk#include <sys/callout.h>
451541Srgrimes#include <sys/kernel.h>
4674914Sjhb#include <sys/lock.h>
4768840Sjhb#include <sys/mutex.h>
481541Srgrimes
4933392Sphk/*
5033392Sphk * TODO:
5133392Sphk *	allocate more timeout table slots when table overflows.
5233392Sphk */
5333392Sphk
5433392Sphk/* Exported to machdep.c and/or kern_clock.c.  */
5529680Sgibbsstruct callout *callout;
5629680Sgibbsstruct callout_list callfree;
5729680Sgibbsint callwheelsize, callwheelbits, callwheelmask;
5829680Sgibbsstruct callout_tailq *callwheel;
5933392Sphkint softticks;			/* Like ticks, but for softclock(). */
6068889Sjakestruct mtx callout_lock;
612112Swollman
6229680Sgibbsstatic struct callout *nextsoftcheck;	/* Next callout to be checked. */
631541Srgrimes
641541Srgrimes/*
6582127Sdillon * kern_timeout_callwheel_alloc() - kernel low level callwheel initialization
6682127Sdillon *
6782127Sdillon *	This code is called very early in the kernel initialization sequence,
6882127Sdillon *	and may be called more then once.
6982127Sdillon */
7082127Sdilloncaddr_t
7182127Sdillonkern_timeout_callwheel_alloc(caddr_t v)
7282127Sdillon{
7382127Sdillon	/*
7482127Sdillon	 * Calculate callout wheel size
7582127Sdillon	 */
7682127Sdillon	for (callwheelsize = 1, callwheelbits = 0;
7782127Sdillon	     callwheelsize < ncallout;
7882127Sdillon	     callwheelsize <<= 1, ++callwheelbits)
7982127Sdillon		;
8082127Sdillon	callwheelmask = callwheelsize - 1;
8182127Sdillon
8282127Sdillon	callout = (struct callout *)v;
8382127Sdillon	v = (caddr_t)(callout + ncallout);
8482127Sdillon	callwheel = (struct callout_tailq *)v;
8582127Sdillon	v = (caddr_t)(callwheel + callwheelsize);
8682127Sdillon	return(v);
8782127Sdillon}
8882127Sdillon
8982127Sdillon/*
9082127Sdillon * kern_timeout_callwheel_init() - initialize previously reserved callwheel
9182127Sdillon *				   space.
9282127Sdillon *
9382127Sdillon *	This code is called just once, after the space reserved for the
9482127Sdillon *	callout wheel has been finalized.
9582127Sdillon */
9682127Sdillonvoid
9782127Sdillonkern_timeout_callwheel_init(void)
9882127Sdillon{
9982127Sdillon	int i;
10082127Sdillon
10182127Sdillon	SLIST_INIT(&callfree);
10282127Sdillon	for (i = 0; i < ncallout; i++) {
10382127Sdillon		callout_init(&callout[i], 0);
10482127Sdillon		callout[i].c_flags = CALLOUT_LOCAL_ALLOC;
10582127Sdillon		SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle);
10682127Sdillon	}
10782127Sdillon	for (i = 0; i < callwheelsize; i++) {
10882127Sdillon		TAILQ_INIT(&callwheel[i]);
10982127Sdillon	}
11093818Sjhb	mtx_init(&callout_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
11182127Sdillon}
11282127Sdillon
11382127Sdillon/*
11429680Sgibbs * The callout mechanism is based on the work of Adam M. Costello and
11529680Sgibbs * George Varghese, published in a technical report entitled "Redesigning
11629680Sgibbs * the BSD Callout and Timer Facilities" and modified slightly for inclusion
11729680Sgibbs * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
11829680Sgibbs * used in this implementation was published by G.Varghese and A. Lauck in
11929680Sgibbs * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
12029680Sgibbs * the Efficient Implementation of a Timer Facility" in the Proceedings of
12129680Sgibbs * the 11th ACM Annual Symposium on Operating Systems Principles,
12229680Sgibbs * Austin, Texas Nov 1987.
12329680Sgibbs */
12432388Sphk
12529680Sgibbs/*
1261541Srgrimes * Software (low priority) clock interrupt.
1271541Srgrimes * Run periodic events from timeout queue.
1281541Srgrimes */
1291541Srgrimesvoid
13067551Sjhbsoftclock(void *dummy)
1311541Srgrimes{
132102936Sphk	struct callout *c;
133102936Sphk	struct callout_tailq *bucket;
134102936Sphk	int curticks;
135102936Sphk	int steps;	/* #steps since we last allowed interrupts */
136102936Sphk#ifdef DIAGNOSTIC
137102936Sphk	struct bintime bt1, bt2;
138102936Sphk	struct timespec ts2;
139102936Sphk	static uint64_t maxdt = 18446744073709551LL;	/* 1 msec */
140102936Sphk#endif
1411541Srgrimes
14233392Sphk#ifndef MAX_SOFTCLOCK_STEPS
14333392Sphk#define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
14433392Sphk#endif /* MAX_SOFTCLOCK_STEPS */
14529680Sgibbs
14629680Sgibbs	steps = 0;
14772200Sbmilekic	mtx_lock_spin(&callout_lock);
14829680Sgibbs	while (softticks != ticks) {
14929805Sgibbs		softticks++;
15029805Sgibbs		/*
15129805Sgibbs		 * softticks may be modified by hard clock, so cache
15229805Sgibbs		 * it while we work on a given bucket.
15329805Sgibbs		 */
15429805Sgibbs		curticks = softticks;
15529805Sgibbs		bucket = &callwheel[curticks & callwheelmask];
15629805Sgibbs		c = TAILQ_FIRST(bucket);
15729680Sgibbs		while (c) {
15829805Sgibbs			if (c->c_time != curticks) {
15929680Sgibbs				c = TAILQ_NEXT(c, c_links.tqe);
16029680Sgibbs				++steps;
16129680Sgibbs				if (steps >= MAX_SOFTCLOCK_STEPS) {
16229680Sgibbs					nextsoftcheck = c;
16329805Sgibbs					/* Give interrupts a chance. */
16472200Sbmilekic					mtx_unlock_spin(&callout_lock);
16581370Sjhb					;	/* nothing */
16672200Sbmilekic					mtx_lock_spin(&callout_lock);
16729680Sgibbs					c = nextsoftcheck;
16829680Sgibbs					steps = 0;
16929680Sgibbs				}
17029680Sgibbs			} else {
17129680Sgibbs				void (*c_func)(void *);
17229680Sgibbs				void *c_arg;
17368889Sjake				int c_flags;
17429680Sgibbs
17529680Sgibbs				nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
17629805Sgibbs				TAILQ_REMOVE(bucket, c, c_links.tqe);
17729680Sgibbs				c_func = c->c_func;
17829680Sgibbs				c_arg = c->c_arg;
17968889Sjake				c_flags = c->c_flags;
18029680Sgibbs				c->c_func = NULL;
18144510Swollman				if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
18244510Swollman					c->c_flags = CALLOUT_LOCAL_ALLOC;
18344510Swollman					SLIST_INSERT_HEAD(&callfree, c,
18444510Swollman							  c_links.sle);
18544510Swollman				} else {
18644510Swollman					c->c_flags =
18750673Sjlemon					    (c->c_flags & ~CALLOUT_PENDING);
18844510Swollman				}
18972200Sbmilekic				mtx_unlock_spin(&callout_lock);
19068889Sjake				if (!(c_flags & CALLOUT_MPSAFE))
19172200Sbmilekic					mtx_lock(&Giant);
192102936Sphk#ifdef DIAGNOSTIC
193102936Sphk				binuptime(&bt1);
194102936Sphk#endif
19529680Sgibbs				c_func(c_arg);
196102936Sphk#ifdef DIAGNOSTIC
197102936Sphk				binuptime(&bt2);
198102936Sphk				bintime_sub(&bt2, &bt1);
199102936Sphk				if (bt2.frac > maxdt) {
200102936Sphk					bintime2timespec(&bt2, &ts2);
201102936Sphk					printf(
202102936Sphk			"Expensive timeout(9) function: %p(%p) %d.%09d\n",
203102936Sphk					c_func, c_arg,
204102936Sphk					ts2.tv_sec, ts2.tv_nsec);
205102936Sphk				}
206102936Sphk#endif
20768889Sjake				if (!(c_flags & CALLOUT_MPSAFE))
20872200Sbmilekic					mtx_unlock(&Giant);
20972200Sbmilekic				mtx_lock_spin(&callout_lock);
21029680Sgibbs				steps = 0;
21129680Sgibbs				c = nextsoftcheck;
21229680Sgibbs			}
21329680Sgibbs		}
2141541Srgrimes	}
21529680Sgibbs	nextsoftcheck = NULL;
21672200Sbmilekic	mtx_unlock_spin(&callout_lock);
2171541Srgrimes}
2181541Srgrimes
2191541Srgrimes/*
2201541Srgrimes * timeout --
2211541Srgrimes *	Execute a function after a specified length of time.
2221541Srgrimes *
2231541Srgrimes * untimeout --
2241541Srgrimes *	Cancel previous timeout function call.
2251541Srgrimes *
22629680Sgibbs * callout_handle_init --
22729680Sgibbs *	Initialize a handle so that using it with untimeout is benign.
22829680Sgibbs *
2291541Srgrimes *	See AT&T BCI Driver Reference Manual for specification.  This
23029680Sgibbs *	implementation differs from that one in that although an
23129680Sgibbs *	identification value is returned from timeout, the original
23229680Sgibbs *	arguments to timeout as well as the identifier are used to
23329680Sgibbs *	identify entries for untimeout.
2341541Srgrimes */
23529680Sgibbsstruct callout_handle
23629680Sgibbstimeout(ftn, arg, to_ticks)
23733824Sbde	timeout_t *ftn;
2381541Srgrimes	void *arg;
23969147Sjlemon	int to_ticks;
2401541Srgrimes{
24129680Sgibbs	struct callout *new;
24229680Sgibbs	struct callout_handle handle;
2431541Srgrimes
24472200Sbmilekic	mtx_lock_spin(&callout_lock);
2451541Srgrimes
2461541Srgrimes	/* Fill in the next free callout structure. */
24729680Sgibbs	new = SLIST_FIRST(&callfree);
24829680Sgibbs	if (new == NULL)
24929680Sgibbs		/* XXX Attempt to malloc first */
2501541Srgrimes		panic("timeout table full");
25129680Sgibbs	SLIST_REMOVE_HEAD(&callfree, c_links.sle);
25244510Swollman
25344510Swollman	callout_reset(new, to_ticks, ftn, arg);
2541541Srgrimes
25544510Swollman	handle.callout = new;
25672200Sbmilekic	mtx_unlock_spin(&callout_lock);
25729680Sgibbs	return (handle);
2581541Srgrimes}
2591541Srgrimes
2601541Srgrimesvoid
26129680Sgibbsuntimeout(ftn, arg, handle)
26233824Sbde	timeout_t *ftn;
2631541Srgrimes	void *arg;
26429680Sgibbs	struct callout_handle handle;
2651541Srgrimes{
2661541Srgrimes
26729680Sgibbs	/*
26829680Sgibbs	 * Check for a handle that was initialized
26929680Sgibbs	 * by callout_handle_init, but never used
27029680Sgibbs	 * for a real timeout.
27129680Sgibbs	 */
27229680Sgibbs	if (handle.callout == NULL)
27329680Sgibbs		return;
27429680Sgibbs
27572200Sbmilekic	mtx_lock_spin(&callout_lock);
27644510Swollman	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
27744510Swollman		callout_stop(handle.callout);
27872200Sbmilekic	mtx_unlock_spin(&callout_lock);
2791541Srgrimes}
2801541Srgrimes
28124101Sbdevoid
28229680Sgibbscallout_handle_init(struct callout_handle *handle)
28329680Sgibbs{
28429680Sgibbs	handle->callout = NULL;
28529680Sgibbs}
28629680Sgibbs
28744510Swollman/*
28844510Swollman * New interface; clients allocate their own callout structures.
28944510Swollman *
29044510Swollman * callout_reset() - establish or change a timeout
29144510Swollman * callout_stop() - disestablish a timeout
29244510Swollman * callout_init() - initialize a callout structure so that it can
29344510Swollman *	safely be passed to callout_reset() and callout_stop()
29444510Swollman *
29550673Sjlemon * <sys/callout.h> defines three convenience macros:
29644510Swollman *
29750673Sjlemon * callout_active() - returns truth if callout has not been serviced
29850673Sjlemon * callout_pending() - returns truth if callout is still waiting for timeout
29950673Sjlemon * callout_deactivate() - marks the callout as having been serviced
30044510Swollman */
30144510Swollmanvoid
30269147Sjlemoncallout_reset(c, to_ticks, ftn, arg)
30344510Swollman	struct	callout *c;
30444510Swollman	int	to_ticks;
30592723Salfred	void	(*ftn)(void *);
30644510Swollman	void	*arg;
30744510Swollman{
30844510Swollman
30972200Sbmilekic	mtx_lock_spin(&callout_lock);
31044510Swollman	if (c->c_flags & CALLOUT_PENDING)
31144510Swollman		callout_stop(c);
31244510Swollman
31344510Swollman	/*
31481370Sjhb	 * We could unlock callout_lock here and lock it again before the
31581370Sjhb	 * TAILQ_INSERT_TAIL, but there's no point since doing this setup
31681370Sjhb	 * doesn't take much time.
31744510Swollman	 */
31844510Swollman	if (to_ticks <= 0)
31944510Swollman		to_ticks = 1;
32044510Swollman
32144510Swollman	c->c_arg = arg;
32269147Sjlemon	c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
32344510Swollman	c->c_func = ftn;
32444510Swollman	c->c_time = ticks + to_ticks;
32544510Swollman	TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask],
32644510Swollman			  c, c_links.tqe);
32772200Sbmilekic	mtx_unlock_spin(&callout_lock);
32844510Swollman}
32944510Swollman
33081481Sjhbint
33144510Swollmancallout_stop(c)
33244510Swollman	struct	callout *c;
33344510Swollman{
33444510Swollman
33572200Sbmilekic	mtx_lock_spin(&callout_lock);
33644510Swollman	/*
33744510Swollman	 * Don't attempt to delete a callout that's not on the queue.
33844510Swollman	 */
33944510Swollman	if (!(c->c_flags & CALLOUT_PENDING)) {
34050673Sjlemon		c->c_flags &= ~CALLOUT_ACTIVE;
34172200Sbmilekic		mtx_unlock_spin(&callout_lock);
34281481Sjhb		return (0);
34344510Swollman	}
34450673Sjlemon	c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
34544510Swollman
34644510Swollman	if (nextsoftcheck == c) {
34744510Swollman		nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
34844510Swollman	}
34944510Swollman	TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe);
35044510Swollman	c->c_func = NULL;
35144510Swollman
35244510Swollman	if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
35344510Swollman		SLIST_INSERT_HEAD(&callfree, c, c_links.sle);
35444510Swollman	}
35572200Sbmilekic	mtx_unlock_spin(&callout_lock);
35681481Sjhb	return (1);
35744510Swollman}
35844510Swollman
35944510Swollmanvoid
36069147Sjlemoncallout_init(c, mpsafe)
36144510Swollman	struct	callout *c;
36269147Sjlemon	int mpsafe;
36344510Swollman{
36444527Swollman	bzero(c, sizeof *c);
36569147Sjlemon	if (mpsafe)
36669147Sjlemon		c->c_flags |= CALLOUT_MPSAFE;
36744510Swollman}
36844510Swollman
36931950Snate#ifdef APM_FIXUP_CALLTODO
37031950Snate/*
37131950Snate * Adjust the kernel calltodo timeout list.  This routine is used after
37231950Snate * an APM resume to recalculate the calltodo timer list values with the
37331950Snate * number of hz's we have been sleeping.  The next hardclock() will detect
37431950Snate * that there are fired timers and run softclock() to execute them.
37531950Snate *
37631950Snate * Please note, I have not done an exhaustive analysis of what code this
37731950Snate * might break.  I am motivated to have my select()'s and alarm()'s that
37831950Snate * have expired during suspend firing upon resume so that the applications
37931950Snate * which set the timer can do the maintanence the timer was for as close
38031950Snate * as possible to the originally intended time.  Testing this code for a
38131950Snate * week showed that resuming from a suspend resulted in 22 to 25 timers
38231950Snate * firing, which seemed independant on whether the suspend was 2 hours or
38331950Snate * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
38431950Snate */
38531950Snatevoid
38631950Snateadjust_timeout_calltodo(time_change)
38731950Snate    struct timeval *time_change;
38831950Snate{
38931950Snate	register struct callout *p;
39031950Snate	unsigned long delta_ticks;
39131950Snate
39231950Snate	/*
39331950Snate	 * How many ticks were we asleep?
39436127Sbde	 * (stolen from tvtohz()).
39531950Snate	 */
39631950Snate
39731950Snate	/* Don't do anything */
39831950Snate	if (time_change->tv_sec < 0)
39931950Snate		return;
40031950Snate	else if (time_change->tv_sec <= LONG_MAX / 1000000)
40131950Snate		delta_ticks = (time_change->tv_sec * 1000000 +
40231950Snate			       time_change->tv_usec + (tick - 1)) / tick + 1;
40331950Snate	else if (time_change->tv_sec <= LONG_MAX / hz)
40431950Snate		delta_ticks = time_change->tv_sec * hz +
40531950Snate			      (time_change->tv_usec + (tick - 1)) / tick + 1;
40631950Snate	else
40731950Snate		delta_ticks = LONG_MAX;
40831950Snate
40931950Snate	if (delta_ticks > INT_MAX)
41031950Snate		delta_ticks = INT_MAX;
41131950Snate
41231950Snate	/*
41331950Snate	 * Now rip through the timer calltodo list looking for timers
41431950Snate	 * to expire.
41531950Snate	 */
41631950Snate
41731950Snate	/* don't collide with softclock() */
41872200Sbmilekic	mtx_lock_spin(&callout_lock);
41931950Snate	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
42031950Snate		p->c_time -= delta_ticks;
42131950Snate
42231950Snate		/* Break if the timer had more time on it than delta_ticks */
42331950Snate		if (p->c_time > 0)
42431950Snate			break;
42531950Snate
42631950Snate		/* take back the ticks the timer didn't use (p->c_time <= 0) */
42731950Snate		delta_ticks = -p->c_time;
42831950Snate	}
42972200Sbmilekic	mtx_unlock_spin(&callout_lock);
43031950Snate
43131950Snate	return;
43231950Snate}
43331950Snate#endif /* APM_FIXUP_CALLTODO */
434