xref: /dragonfly/sys/kern/kern_timeout.c (revision af79c6e5)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	From: @(#)kern_clock.c	8.5 (Berkeley) 1/21/94
39  * $FreeBSD: src/sys/kern/kern_timeout.c,v 1.59.2.1 2001/11/13 18:24:52 archie Exp $
40  * $DragonFly: src/sys/kern/kern_timeout.c,v 1.6 2003/08/26 21:09:02 rob Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/callout.h>
46 #include <sys/kernel.h>
47 #include <sys/interrupt.h>
48 #include <sys/thread.h>
49 #include <machine/ipl.h>
50 
51 /*
52  * TODO:
53  *	allocate more timeout table slots when table overflows.
54  */
55 
56 /* Exported to machdep.c and/or kern_clock.c.  */
57 struct callout *callout;
58 struct callout_list callfree;
59 int callwheelsize, callwheelbits, callwheelmask;
60 struct callout_tailq *callwheel;
61 int softticks;			/* Like ticks, but for softclock(). */
62 
63 static struct callout *nextsoftcheck;	/* Next callout to be checked. */
64 
65 /*
66  * The callout mechanism is based on the work of Adam M. Costello and
67  * George Varghese, published in a technical report entitled "Redesigning
68  * the BSD Callout and Timer Facilities" and modified slightly for inclusion
69  * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
70  * used in this implementation was published by G.Varghese and A. Lauck in
71  * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
72  * the Efficient Implementation of a Timer Facility" in the Proceedings of
73  * the 11th ACM Annual Symposium on Operating Systems Principles,
74  * Austin, Texas Nov 1987.
75  */
76 
77 /*
78  * Software (low priority) clock interrupt.
79  * Run periodic events from timeout queue.
80  */
81 static void
82 swi_softclock(void *dummy)
83 {
84 	struct callout *c;
85 	struct callout_tailq *bucket;
86 	int s;
87 	int curticks;
88 	int steps;	/* #steps since we last allowed interrupts */
89 
90 #ifndef MAX_SOFTCLOCK_STEPS
91 #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
92 #endif /* MAX_SOFTCLOCK_STEPS */
93 
94 	steps = 0;
95 	s = splhigh();
96 	while (softticks != ticks) {
97 		softticks++;
98 		/*
99 		 * softticks may be modified by hard clock, so cache
100 		 * it while we work on a given bucket.
101 		 */
102 		curticks = softticks;
103 		bucket = &callwheel[curticks & callwheelmask];
104 		c = TAILQ_FIRST(bucket);
105 		while (c) {
106 			if (c->c_time != curticks) {
107 				c = TAILQ_NEXT(c, c_links.tqe);
108 				++steps;
109 				if (steps >= MAX_SOFTCLOCK_STEPS) {
110 					nextsoftcheck = c;
111 					/* Give interrupts a chance. */
112 					splx(s);
113 					s = splhigh();
114 					c = nextsoftcheck;
115 					steps = 0;
116 				}
117 			} else {
118 				void (*c_func)(void *);
119 				void *c_arg;
120 
121 				nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
122 				TAILQ_REMOVE(bucket, c, c_links.tqe);
123 				c_func = c->c_func;
124 				c_arg = c->c_arg;
125 				c->c_func = NULL;
126 				if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
127 					c->c_flags = CALLOUT_LOCAL_ALLOC;
128 					SLIST_INSERT_HEAD(&callfree, c,
129 							  c_links.sle);
130 				} else {
131 					c->c_flags =
132 					    (c->c_flags & ~CALLOUT_PENDING);
133 				}
134 				splx(s);
135 				c_func(c_arg);
136 				s = splhigh();
137 				steps = 0;
138 				c = nextsoftcheck;
139 			}
140 		}
141 	}
142 	nextsoftcheck = NULL;
143 	splx(s);
144 }
145 
146 /*
147  * timeout --
148  *	Execute a function after a specified length of time.
149  *
150  * untimeout --
151  *	Cancel previous timeout function call.
152  *
153  * callout_handle_init --
154  *	Initialize a handle so that using it with untimeout is benign.
155  *
156  *	See AT&T BCI Driver Reference Manual for specification.  This
157  *	implementation differs from that one in that although an
158  *	identification value is returned from timeout, the original
159  *	arguments to timeout as well as the identifier are used to
160  *	identify entries for untimeout.
161  */
162 struct callout_handle
163 timeout(ftn, arg, to_ticks)
164 	timeout_t *ftn;
165 	void *arg;
166 	int to_ticks;
167 {
168 	int s;
169 	struct callout *new;
170 	struct callout_handle handle;
171 
172 	s = splhigh();
173 
174 	/* Fill in the next free callout structure. */
175 	new = SLIST_FIRST(&callfree);
176 	if (new == NULL)
177 		/* XXX Attempt to malloc first */
178 		panic("timeout table full");
179 	SLIST_REMOVE_HEAD(&callfree, c_links.sle);
180 
181 	callout_reset(new, to_ticks, ftn, arg);
182 
183 	handle.callout = new;
184 	splx(s);
185 	return (handle);
186 }
187 
188 void
189 untimeout(ftn, arg, handle)
190 	timeout_t *ftn;
191 	void *arg;
192 	struct callout_handle handle;
193 {
194 	int s;
195 
196 	/*
197 	 * Check for a handle that was initialized
198 	 * by callout_handle_init, but never used
199 	 * for a real timeout.
200 	 */
201 	if (handle.callout == NULL)
202 		return;
203 
204 	s = splhigh();
205 	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
206 		callout_stop(handle.callout);
207 	splx(s);
208 }
209 
210 void
211 callout_handle_init(struct callout_handle *handle)
212 {
213 	handle->callout = NULL;
214 }
215 
216 /*
217  * New interface; clients allocate their own callout structures.
218  *
219  * callout_reset() - establish or change a timeout
220  * callout_stop() - disestablish a timeout
221  * callout_init() - initialize a callout structure so that it can
222  *	safely be passed to callout_reset() and callout_stop()
223  *
224  * <sys/callout.h> defines three convenience macros:
225  *
226  * callout_active() - returns truth if callout has not been serviced
227  * callout_pending() - returns truth if callout is still waiting for timeout
228  * callout_deactivate() - marks the callout as having been serviced
229  */
230 void
231 callout_reset(c, to_ticks, ftn, arg)
232 	struct	callout *c;
233 	int	to_ticks;
234 	void	(*ftn) (void *);
235 	void	*arg;
236 {
237 	int	s;
238 
239 	s = splhigh();
240 	if (c->c_flags & CALLOUT_PENDING)
241 		callout_stop(c);
242 
243 	/*
244 	 * We could spl down here and back up at the TAILQ_INSERT_TAIL,
245 	 * but there's no point since doing this setup doesn't take much
246 	 * time.
247 	 */
248 	if (to_ticks <= 0)
249 		to_ticks = 1;
250 
251 	c->c_arg = arg;
252 	c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
253 	c->c_func = ftn;
254 	c->c_time = ticks + to_ticks;
255 	TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask],
256 			  c, c_links.tqe);
257 	splx(s);
258 
259 }
260 
261 int
262 callout_stop(c)
263 	struct	callout *c;
264 {
265 	int	s;
266 
267 	s = splhigh();
268 	/*
269 	 * Don't attempt to delete a callout that's not on the queue.
270 	 */
271 	if (!(c->c_flags & CALLOUT_PENDING)) {
272 		c->c_flags &= ~CALLOUT_ACTIVE;
273 		splx(s);
274 		return (0);
275 	}
276 	c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
277 
278 	if (nextsoftcheck == c) {
279 		nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
280 	}
281 	TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe);
282 	c->c_func = NULL;
283 
284 	if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
285 		SLIST_INSERT_HEAD(&callfree, c, c_links.sle);
286 	}
287 	splx(s);
288 	return (1);
289 }
290 
291 void
292 callout_init(c)
293 	struct	callout *c;
294 {
295 	bzero(c, sizeof *c);
296 }
297 
298 static void
299 swi_softclock_setup(void *arg)
300 {
301        register_swi(SWI_CLOCK, swi_softclock, NULL, "swi_sftclk");
302        swi_setpriority(SWI_CLOCK, TDPRI_SOFT_TIMER);
303 }
304 
305 SYSINIT(vm_setup, SI_SUB_CPU, SI_ORDER_ANY, swi_softclock_setup, NULL);
306 
307 #ifdef APM_FIXUP_CALLTODO
308 /*
309  * Adjust the kernel calltodo timeout list.  This routine is used after
310  * an APM resume to recalculate the calltodo timer list values with the
311  * number of hz's we have been sleeping.  The next hardclock() will detect
312  * that there are fired timers and run softclock() to execute them.
313  *
314  * Please note, I have not done an exhaustive analysis of what code this
315  * might break.  I am motivated to have my select()'s and alarm()'s that
316  * have expired during suspend firing upon resume so that the applications
317  * which set the timer can do the maintanence the timer was for as close
318  * as possible to the originally intended time.  Testing this code for a
319  * week showed that resuming from a suspend resulted in 22 to 25 timers
320  * firing, which seemed independant on whether the suspend was 2 hours or
321  * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
322  */
323 void
324 adjust_timeout_calltodo(time_change)
325     struct timeval *time_change;
326 {
327 	struct callout *p;
328 	unsigned long delta_ticks;
329 	int s;
330 
331 	/*
332 	 * How many ticks were we asleep?
333 	 * (stolen from tvtohz()).
334 	 */
335 
336 	/* Don't do anything */
337 	if (time_change->tv_sec < 0)
338 		return;
339 	else if (time_change->tv_sec <= LONG_MAX / 1000000)
340 		delta_ticks = (time_change->tv_sec * 1000000 +
341 			       time_change->tv_usec + (tick - 1)) / tick + 1;
342 	else if (time_change->tv_sec <= LONG_MAX / hz)
343 		delta_ticks = time_change->tv_sec * hz +
344 			      (time_change->tv_usec + (tick - 1)) / tick + 1;
345 	else
346 		delta_ticks = LONG_MAX;
347 
348 	if (delta_ticks > INT_MAX)
349 		delta_ticks = INT_MAX;
350 
351 	/*
352 	 * Now rip through the timer calltodo list looking for timers
353 	 * to expire.
354 	 */
355 
356 	/* don't collide with softclock() */
357 	s = splhigh();
358 	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
359 		p->c_time -= delta_ticks;
360 
361 		/* Break if the timer had more time on it than delta_ticks */
362 		if (p->c_time > 0)
363 			break;
364 
365 		/* take back the ticks the timer didn't use (p->c_time <= 0) */
366 		delta_ticks = -p->c_time;
367 	}
368 	splx(s);
369 
370 	return;
371 }
372 #endif /* APM_FIXUP_CALLTODO */
373