xref: /freebsd/sys/kern/kern_timeout.c (revision 71fe318b852b8dfb3e799cb12ef184750f7f8eac)
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$
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/callout.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 
49 /*
50  * TODO:
51  *	allocate more timeout table slots when table overflows.
52  */
53 
54 /* Exported to machdep.c and/or kern_clock.c.  */
55 struct callout *callout;
56 struct callout_list callfree;
57 int callwheelsize, callwheelbits, callwheelmask;
58 struct callout_tailq *callwheel;
59 int softticks;			/* Like ticks, but for softclock(). */
60 struct mtx callout_lock;
61 
62 static struct callout *nextsoftcheck;	/* Next callout to be checked. */
63 
64 /*
65  * kern_timeout_callwheel_alloc() - kernel low level callwheel initialization
66  *
67  *	This code is called very early in the kernel initialization sequence,
68  *	and may be called more then once.
69  */
70 caddr_t
71 kern_timeout_callwheel_alloc(caddr_t v)
72 {
73 	/*
74 	 * Calculate callout wheel size
75 	 */
76 	for (callwheelsize = 1, callwheelbits = 0;
77 	     callwheelsize < ncallout;
78 	     callwheelsize <<= 1, ++callwheelbits)
79 		;
80 	callwheelmask = callwheelsize - 1;
81 
82 	callout = (struct callout *)v;
83 	v = (caddr_t)(callout + ncallout);
84 	callwheel = (struct callout_tailq *)v;
85 	v = (caddr_t)(callwheel + callwheelsize);
86 	return(v);
87 }
88 
89 /*
90  * kern_timeout_callwheel_init() - initialize previously reserved callwheel
91  *				   space.
92  *
93  *	This code is called just once, after the space reserved for the
94  *	callout wheel has been finalized.
95  */
96 void
97 kern_timeout_callwheel_init(void)
98 {
99 	int i;
100 
101 	SLIST_INIT(&callfree);
102 	for (i = 0; i < ncallout; i++) {
103 		callout_init(&callout[i], 0);
104 		callout[i].c_flags = CALLOUT_LOCAL_ALLOC;
105 		SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle);
106 	}
107 	for (i = 0; i < callwheelsize; i++) {
108 		TAILQ_INIT(&callwheel[i]);
109 	}
110 	mtx_init(&callout_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
111 }
112 
113 /*
114  * The callout mechanism is based on the work of Adam M. Costello and
115  * George Varghese, published in a technical report entitled "Redesigning
116  * the BSD Callout and Timer Facilities" and modified slightly for inclusion
117  * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
118  * used in this implementation was published by G.Varghese and A. Lauck in
119  * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
120  * the Efficient Implementation of a Timer Facility" in the Proceedings of
121  * the 11th ACM Annual Symposium on Operating Systems Principles,
122  * Austin, Texas Nov 1987.
123  */
124 
125 /*
126  * Software (low priority) clock interrupt.
127  * Run periodic events from timeout queue.
128  */
129 void
130 softclock(void *dummy)
131 {
132 	struct callout *c;
133 	struct callout_tailq *bucket;
134 	int curticks;
135 	int steps;	/* #steps since we last allowed interrupts */
136 #ifdef DIAGNOSTIC
137 	struct bintime bt1, bt2;
138 	struct timespec ts2;
139 	static uint64_t maxdt = 18446744073709551LL;	/* 1 msec */
140 #endif
141 
142 #ifndef MAX_SOFTCLOCK_STEPS
143 #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
144 #endif /* MAX_SOFTCLOCK_STEPS */
145 
146 	steps = 0;
147 	mtx_lock_spin(&callout_lock);
148 	while (softticks != ticks) {
149 		softticks++;
150 		/*
151 		 * softticks may be modified by hard clock, so cache
152 		 * it while we work on a given bucket.
153 		 */
154 		curticks = softticks;
155 		bucket = &callwheel[curticks & callwheelmask];
156 		c = TAILQ_FIRST(bucket);
157 		while (c) {
158 			if (c->c_time != curticks) {
159 				c = TAILQ_NEXT(c, c_links.tqe);
160 				++steps;
161 				if (steps >= MAX_SOFTCLOCK_STEPS) {
162 					nextsoftcheck = c;
163 					/* Give interrupts a chance. */
164 					mtx_unlock_spin(&callout_lock);
165 					;	/* nothing */
166 					mtx_lock_spin(&callout_lock);
167 					c = nextsoftcheck;
168 					steps = 0;
169 				}
170 			} else {
171 				void (*c_func)(void *);
172 				void *c_arg;
173 				int c_flags;
174 
175 				nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
176 				TAILQ_REMOVE(bucket, c, c_links.tqe);
177 				c_func = c->c_func;
178 				c_arg = c->c_arg;
179 				c_flags = c->c_flags;
180 				c->c_func = NULL;
181 				if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
182 					c->c_flags = CALLOUT_LOCAL_ALLOC;
183 					SLIST_INSERT_HEAD(&callfree, c,
184 							  c_links.sle);
185 				} else {
186 					c->c_flags =
187 					    (c->c_flags & ~CALLOUT_PENDING);
188 				}
189 				mtx_unlock_spin(&callout_lock);
190 				if (!(c_flags & CALLOUT_MPSAFE))
191 					mtx_lock(&Giant);
192 #ifdef DIAGNOSTIC
193 				binuptime(&bt1);
194 #endif
195 				c_func(c_arg);
196 #ifdef DIAGNOSTIC
197 				binuptime(&bt2);
198 				bintime_sub(&bt2, &bt1);
199 				if (bt2.frac > maxdt) {
200 					bintime2timespec(&bt2, &ts2);
201 					printf(
202 			"Expensive timeout(9) function: %p(%p) %d.%09ld\n",
203 					c_func, c_arg,
204 					ts2.tv_sec, ts2.tv_nsec);
205 				}
206 #endif
207 				if (!(c_flags & CALLOUT_MPSAFE))
208 					mtx_unlock(&Giant);
209 				mtx_lock_spin(&callout_lock);
210 				steps = 0;
211 				c = nextsoftcheck;
212 			}
213 		}
214 	}
215 	nextsoftcheck = NULL;
216 	mtx_unlock_spin(&callout_lock);
217 }
218 
219 /*
220  * timeout --
221  *	Execute a function after a specified length of time.
222  *
223  * untimeout --
224  *	Cancel previous timeout function call.
225  *
226  * callout_handle_init --
227  *	Initialize a handle so that using it with untimeout is benign.
228  *
229  *	See AT&T BCI Driver Reference Manual for specification.  This
230  *	implementation differs from that one in that although an
231  *	identification value is returned from timeout, the original
232  *	arguments to timeout as well as the identifier are used to
233  *	identify entries for untimeout.
234  */
235 struct callout_handle
236 timeout(ftn, arg, to_ticks)
237 	timeout_t *ftn;
238 	void *arg;
239 	int to_ticks;
240 {
241 	struct callout *new;
242 	struct callout_handle handle;
243 
244 	mtx_lock_spin(&callout_lock);
245 
246 	/* Fill in the next free callout structure. */
247 	new = SLIST_FIRST(&callfree);
248 	if (new == NULL)
249 		/* XXX Attempt to malloc first */
250 		panic("timeout table full");
251 	SLIST_REMOVE_HEAD(&callfree, c_links.sle);
252 
253 	callout_reset(new, to_ticks, ftn, arg);
254 
255 	handle.callout = new;
256 	mtx_unlock_spin(&callout_lock);
257 	return (handle);
258 }
259 
260 void
261 untimeout(ftn, arg, handle)
262 	timeout_t *ftn;
263 	void *arg;
264 	struct callout_handle handle;
265 {
266 
267 	/*
268 	 * Check for a handle that was initialized
269 	 * by callout_handle_init, but never used
270 	 * for a real timeout.
271 	 */
272 	if (handle.callout == NULL)
273 		return;
274 
275 	mtx_lock_spin(&callout_lock);
276 	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
277 		callout_stop(handle.callout);
278 	mtx_unlock_spin(&callout_lock);
279 }
280 
281 void
282 callout_handle_init(struct callout_handle *handle)
283 {
284 	handle->callout = NULL;
285 }
286 
287 /*
288  * New interface; clients allocate their own callout structures.
289  *
290  * callout_reset() - establish or change a timeout
291  * callout_stop() - disestablish a timeout
292  * callout_init() - initialize a callout structure so that it can
293  *	safely be passed to callout_reset() and callout_stop()
294  *
295  * <sys/callout.h> defines three convenience macros:
296  *
297  * callout_active() - returns truth if callout has not been serviced
298  * callout_pending() - returns truth if callout is still waiting for timeout
299  * callout_deactivate() - marks the callout as having been serviced
300  */
301 void
302 callout_reset(c, to_ticks, ftn, arg)
303 	struct	callout *c;
304 	int	to_ticks;
305 	void	(*ftn)(void *);
306 	void	*arg;
307 {
308 
309 	mtx_lock_spin(&callout_lock);
310 	if (c->c_flags & CALLOUT_PENDING)
311 		callout_stop(c);
312 
313 	/*
314 	 * We could unlock callout_lock here and lock it again before the
315 	 * TAILQ_INSERT_TAIL, but there's no point since doing this setup
316 	 * doesn't take much time.
317 	 */
318 	if (to_ticks <= 0)
319 		to_ticks = 1;
320 
321 	c->c_arg = arg;
322 	c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
323 	c->c_func = ftn;
324 	c->c_time = ticks + to_ticks;
325 	TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask],
326 			  c, c_links.tqe);
327 	mtx_unlock_spin(&callout_lock);
328 }
329 
330 int
331 callout_stop(c)
332 	struct	callout *c;
333 {
334 
335 	mtx_lock_spin(&callout_lock);
336 	/*
337 	 * Don't attempt to delete a callout that's not on the queue.
338 	 */
339 	if (!(c->c_flags & CALLOUT_PENDING)) {
340 		c->c_flags &= ~CALLOUT_ACTIVE;
341 		mtx_unlock_spin(&callout_lock);
342 		return (0);
343 	}
344 	c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
345 
346 	if (nextsoftcheck == c) {
347 		nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
348 	}
349 	TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe);
350 	c->c_func = NULL;
351 
352 	if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
353 		SLIST_INSERT_HEAD(&callfree, c, c_links.sle);
354 	}
355 	mtx_unlock_spin(&callout_lock);
356 	return (1);
357 }
358 
359 void
360 callout_init(c, mpsafe)
361 	struct	callout *c;
362 	int mpsafe;
363 {
364 	bzero(c, sizeof *c);
365 	if (mpsafe)
366 		c->c_flags |= CALLOUT_MPSAFE;
367 }
368 
369 #ifdef APM_FIXUP_CALLTODO
370 /*
371  * Adjust the kernel calltodo timeout list.  This routine is used after
372  * an APM resume to recalculate the calltodo timer list values with the
373  * number of hz's we have been sleeping.  The next hardclock() will detect
374  * that there are fired timers and run softclock() to execute them.
375  *
376  * Please note, I have not done an exhaustive analysis of what code this
377  * might break.  I am motivated to have my select()'s and alarm()'s that
378  * have expired during suspend firing upon resume so that the applications
379  * which set the timer can do the maintanence the timer was for as close
380  * as possible to the originally intended time.  Testing this code for a
381  * week showed that resuming from a suspend resulted in 22 to 25 timers
382  * firing, which seemed independant on whether the suspend was 2 hours or
383  * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
384  */
385 void
386 adjust_timeout_calltodo(time_change)
387     struct timeval *time_change;
388 {
389 	register struct callout *p;
390 	unsigned long delta_ticks;
391 
392 	/*
393 	 * How many ticks were we asleep?
394 	 * (stolen from tvtohz()).
395 	 */
396 
397 	/* Don't do anything */
398 	if (time_change->tv_sec < 0)
399 		return;
400 	else if (time_change->tv_sec <= LONG_MAX / 1000000)
401 		delta_ticks = (time_change->tv_sec * 1000000 +
402 			       time_change->tv_usec + (tick - 1)) / tick + 1;
403 	else if (time_change->tv_sec <= LONG_MAX / hz)
404 		delta_ticks = time_change->tv_sec * hz +
405 			      (time_change->tv_usec + (tick - 1)) / tick + 1;
406 	else
407 		delta_ticks = LONG_MAX;
408 
409 	if (delta_ticks > INT_MAX)
410 		delta_ticks = INT_MAX;
411 
412 	/*
413 	 * Now rip through the timer calltodo list looking for timers
414 	 * to expire.
415 	 */
416 
417 	/* don't collide with softclock() */
418 	mtx_lock_spin(&callout_lock);
419 	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
420 		p->c_time -= delta_ticks;
421 
422 		/* Break if the timer had more time on it than delta_ticks */
423 		if (p->c_time > 0)
424 			break;
425 
426 		/* take back the ticks the timer didn't use (p->c_time <= 0) */
427 		delta_ticks = -p->c_time;
428 	}
429 	mtx_unlock_spin(&callout_lock);
430 
431 	return;
432 }
433 #endif /* APM_FIXUP_CALLTODO */
434