1 /* $Id: kern_clock.c,v 1.1.1.1 2006/09/14 01:59:08 root Exp $ */
2 /* $OpenBSD: kern_clock.c,v 1.21 1999/08/15 00:07:43 pjanzen Exp $ */
3 /* $NetBSD: kern_clock.c,v 1.34 1996/06/09 04:51:03 briggs Exp $ */
6 * Copyright (c) 1982, 1986, 1991, 1993
7 * The Regents of the University of California. All rights reserved.
8 * (c) UNIX System Laboratories, Inc.
9 * All or some portions of this file are derived from material licensed
10 * to the University of California by American Telephone and Telegraph
11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12 * the permission of UNIX System Laboratories, Inc.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/callout.h>
48 #include <sys/kernel.h>
50 #include <sys/resourcevar.h>
53 #include <machine/cpu.h>
55 extern void psignal
__P((struct proc
*p
, int sig
));
58 * Clock handling routines.
60 * This code is written to operate with two timers that run independently of
61 * each other. The main clock, running hz times per second, is used to keep
62 * track of real time. The second timer handles kernel and user profiling,
63 * and does resource use estimation. If the second timer is programmable,
64 * it is randomized to avoid aliasing between the two clocks. For example,
65 * the randomization prevents an adversary from always giving up the cpu
66 * just before its quantum expires. Otherwise, it would never accumulate
67 * cpu ticks. The mean frequency of the second timer is stathz.
69 * If no second timer exists, stathz will be zero; in this case we drive
70 * profiling and statistics off the main clock. This WILL NOT be accurate;
71 * do not do it unless absolutely necessary.
73 * The statistics clock may (or may not) be run at a higher rate while
74 * profiling. This profile clock runs at profhz. We require that profhz
75 * be an integral multiple of stathz.
77 * If the statistics clock is running fast, it must be divided by the ratio
78 * profhz/stathz for statistics. (For profiling, every tick counts.)
83 * allocate more timeout table slots when table overflows.
88 * Bump a timeval by a small number of usec's.
90 #define BUMPTIME(t, usec) { \
91 register volatile struct timeval *tp = (t); \
94 tp->tv_usec = us = tp->tv_usec + (usec); \
95 if (us >= 1000000) { \
96 tp->tv_usec = us - 1000000; \
106 static int psdiv
, pscnt
; /* prof => stat divider */
107 int psratio
; /* ratio: prof / stat */
108 int tickfix
, tickfixinterval
; /* used if tick not really integral */
109 static int tickfixcnt
; /* accumulated fractional error */
112 volatile struct timeval time
;
115 * Initialize clock frequencies and start both clocks running.
123 * Set divisors to 1 (normal case) and let the machine-specific
130 * Compute profhz/stathz, and fix profhz if needed.
132 i
= stathz
? stathz
: hz
;
135 psratio
= profhz
/ i
;
140 * The real-time timer, interrupting hz times per second.
144 register struct clockframe
*frame
;
146 register struct callout
*p1
;
147 register struct proc
*p
;
148 register int delta
, needsoft
;
149 #ifdef NOTUSED_BY_PMON
150 extern int tickdelta
;
151 extern long timedelta
;
152 #endif /* NOTUSED_BY_PMON */
155 * Update real-time timeout queue.
156 * At front of queue are some number of events which are ``due''.
157 * The time to these is <= 0 and if negative represents the
158 * number of ticks which have passed since it was supposed to happen.
159 * The rest of the q elements (times > 0) are events yet to happen,
160 * where the time for each is given as a delta from the previous.
161 * Decrementing just the first of these serves to decrement the time
165 for (p1
= calltodo
.c_next
; p1
!= NULL
; p1
= p1
->c_next
) {
166 if (--p1
->c_time
> 0)
175 register struct pstats
*pstats
;
178 * Run current process's virtual and profile time, as needed.
181 if (CLKF_USERMODE(frame
) &&
182 timerisset(&pstats
->p_timer
[ITIMER_VIRTUAL
].it_value
) &&
183 itimerdecr(&pstats
->p_timer
[ITIMER_VIRTUAL
], tick
) == 0)
184 psignal(p
, SIGVTALRM
);
185 if (timerisset(&pstats
->p_timer
[ITIMER_PROF
].it_value
) &&
186 itimerdecr(&pstats
->p_timer
[ITIMER_PROF
], tick
) == 0)
190 #ifdef NOTUSED_BY_PMON
192 * If no separate statistics clock is available, run it from here.
196 #endif /* NOTUSED_BY_PMON */
199 * Increment the time-of-day. The increment is normally just
200 * ``tick''. If the machine is one which has a clock frequency
201 * such that ``hz'' would not divide the second evenly into
202 * milliseconds, a periodic adjustment must be applied. Finally,
203 * if we are still adjusting the time (see adjtime()),
204 * ``tickdelta'' may also be added in.
210 tickfixcnt
+= tickfix
;
211 if (tickfixcnt
>= tickfixinterval
) {
213 tickfixcnt
-= tickfixinterval
;
217 #ifdef NOTUSED_BY_PMON
218 /* Imprecise 4bsd adjtime() handling */
219 if (timedelta
!= 0) {
221 timedelta
-= tickdelta
;
223 #endif /* NOTUSED_BY_PMON */
229 BUMPTIME(&time
, delta
);
232 * Process callouts at a very low cpu priority, so we don't keep the
233 * relatively high clock interrupt priority any longer than necessary.
237 if (CLKF_BASEPRI(frame
)) {
239 * Save the overhead of a software interrupt;
240 * it will happen as soon as we return, so do it now.
242 (void)splsoftclock();
251 * Software (low priority) clock interrupt.
252 * Run periodic events from timeout queue.
258 register struct callout
*c
;
260 register void (*func
) __P((void *));
264 while ((c
= calltodo
.c_next
) != NULL
&& c
->c_time
<= 0) {
267 calltodo
.c_next
= c
->c_next
;
268 c
->c_next
= callfree
;
279 * Execute a function after a specified length of time.
282 * Cancel previous timeout function call.
284 * See AT&T BCI Driver Reference Manual for specification. This
285 * implementation differs from that one in that no identification
286 * value is returned from timeout, rather, the original arguments
287 * to timeout are used to identify entries for untimeout.
290 timeout(ftn
, arg
, ticks
)
291 void (*ftn
) __P((void *));
295 register struct callout
*new, *p
, *t
;
301 /* Lock out the clock. */
304 /* Fill in the next free callout structure. */
305 if (callfree
== NULL
)
306 panic("timeout table full");
308 callfree
= new->c_next
;
313 * The time for each event is stored as a difference from the time
314 * of the previous event on the queue. Walk the queue, correcting
315 * the ticks argument for queue entries passed. Correct the ticks
316 * value for the queue entry immediately after the insertion point
317 * as well. Watch out for negative c_time values; these represent
321 (t
= p
->c_next
) != NULL
&& ticks
> t
->c_time
; p
= t
)
328 /* Insert the new entry into the queue. */
336 void (*ftn
) __P((void *));
339 register struct callout
*p
, *t
;
343 for (p
= &calltodo
; (t
= p
->c_next
) != NULL
; p
= t
)
344 if (t
->c_func
== ftn
&& t
->c_arg
== arg
) {
345 /* Increment next entry's tick count. */
346 if (t
->c_next
&& t
->c_time
> 0)
347 t
->c_next
->c_time
+= t
->c_time
;
349 /* Move entry from callout queue to callfree queue. */
350 p
->c_next
= t
->c_next
;
351 t
->c_next
= callfree
;
359 * Compute number of hz until specified time. Used to
360 * compute third argument to timeout() from an absolute time.
366 register long ticks
, sec
;
370 * If number of microseconds will fit in 32 bit arithmetic,
371 * then compute number of microseconds to time and scale to
372 * ticks. Otherwise just compute number of hz in time, rounding
373 * times greater than representible to maximum value. (We must
374 * compute in microseconds, because hz can be greater than 1000,
375 * and thus tick can be less than one millisecond).
377 * Delta times less than 14 hours can be computed ``exactly''.
378 * (Note that if hz would yeild a non-integral number of us per
379 * tick, i.e. tickfix is nonzero, timouts can be a tick longer
380 * than they should be.) Maximum value for any timeout in 10ms
384 sec
= tv
->tv_sec
- time
.tv_sec
;
385 if (sec
<= 0x7fffffff / 1000000 - 1)
386 ticks
= ((tv
->tv_sec
- time
.tv_sec
) * 1000000 +
387 (tv
->tv_usec
- time
.tv_usec
)) / tick
;
388 else if (sec
<= 0x7fffffff / hz
)