2 * Copyright (c) 1997-1998 University of Utah and the Flux Group.
5 * This file is part of the Flux OSKit. The OSKit is free software, also known
6 * as "open source;" you can redistribute it and/or modify it under the terms
7 * of the GNU General Public License (GPL), version 2, as published by the Free
8 * Software Foundation (FSF). To explore alternate licensing terms, contact
9 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
11 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
14 * received a copy of the GPL along with the OSKit; see the file COPYING. If
15 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
18 * I am having no mercy with this file.
19 * I ripped out everything I didn't need
22 * Copyright (c) 1982, 1986, 1991, 1993
23 * The Regents of the University of California. All rights reserved.
24 * (c) UNIX System Laboratories, Inc.
25 * All or some portions of this file are derived from material licensed
26 * to the University of California by American Telephone and Telegraph
27 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
28 * the permission of UNIX System Laboratories, Inc.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
59 * $\Id: kern_clock.c,v 1.26 1996/07/30 16:59:22 bde Exp $
62 /* Portions of this software are covered by the following: */
63 /******************************************************************************
65 * Copyright (c) David L. Mills 1993, 1994 *
67 * Permission to use, copy, modify, and distribute this software and its *
68 * documentation for any purpose and without fee is hereby granted, provided *
69 * that the above copyright notice appears in all copies and that both the *
70 * copyright notice and this permission notice appear in supporting *
71 * documentation, and that the name University of Delaware not be used in *
72 * advertising or publicity pertaining to distribution of the software *
73 * without specific, written prior permission. The University of Delaware *
74 * makes no representations about the suitability this software for any *
75 * purpose. It is provided "as is" without express or implied warranty. *
77 *****************************************************************************/
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/callout.h>
82 #include <sys/kernel.h>
85 /* Exported to machdep.c. */
86 struct callout
*callfree
, *callout
;
88 struct callout calltodo
;
96 * Clock handling routines.
98 * This code is written to operate with two timers that run independently of
99 * each other. The main clock, running hz times per second, is used to keep
100 * track of real time. The second timer handles kernel and user profiling,
101 * and does resource use estimation. If the second timer is programmable,
102 * it is randomized to avoid aliasing between the two clocks. For example,
103 * the randomization prevents an adversary from always giving up the cpu
104 * just before its quantum expires. Otherwise, it would never accumulate
105 * cpu ticks. The mean frequency of the second timer is stathz.
107 * If no second timer exists, stathz will be zero; in this case we drive
108 * profiling and statistics off the main clock. This WILL NOT be accurate;
109 * do not do it unless absolutely necessary.
111 * The statistics clock may (or may not) be run at a higher rate while
112 * profiling. This profile clock runs at profhz. We require that profhz
113 * be an integral multiple of stathz.
115 * If the statistics clock is running fast, it must be divided by the ratio
116 * profhz/stathz for statistics. (For profiling, every tick counts.)
121 * allocate more timeout table slots when table overflows.
125 * Bump a timeval by a small number of usec's.
127 #define BUMPTIME(t, usec) { \
128 register volatile struct timeval *tp = (t); \
131 tp->tv_usec = us = tp->tv_usec + (usec); \
132 if (us >= 1000000) { \
133 tp->tv_usec = us - 1000000; \
139 volatile struct timeval kern_time
;
140 volatile struct timeval mono_time
;
143 * Software (low priority) clock interrupt.
144 * Run periodic events from timeout queue.
150 register struct callout
*c
;
152 register void (*func
) __P((void *));
156 while ((c
= calltodo
.c_next
) != NULL
&& c
->c_time
<= 0) {
159 calltodo
.c_next
= c
->c_next
;
160 c
->c_next
= callfree
;
171 * Execute a function after a specified length of time.
174 * Cancel previous timeout function call.
176 * See AT&T BCI Driver Reference Manual for specification. This
177 * implementation differs from that one in that no identification
178 * value is returned from timeout, rather, the original arguments
179 * to timeout are used to identify entries for untimeout.
182 timeout(ftn
, arg
, ticks
)
187 register struct callout
*new, *p
, *t
;
193 /* Lock out the clock. */
196 /* Fill in the next free callout structure. */
197 if (callfree
== NULL
)
198 panic("timeout table full");
200 callfree
= new->c_next
;
205 * The time for each event is stored as a difference from the time
206 * of the previous event on the queue. Walk the queue, correcting
207 * the ticks argument for queue entries passed. Correct the ticks
208 * value for the queue entry immediately after the insertion point
209 * as well. Watch out for negative c_time values; these represent
213 (t
= p
->c_next
) != NULL
&& ticks
> t
->c_time
; p
= t
)
220 /* Insert the new entry into the queue. */
231 register struct callout
*p
, *t
;
235 for (p
= &calltodo
; (t
= p
->c_next
) != NULL
; p
= t
)
236 if (t
->c_func
== ftn
&& t
->c_arg
== arg
) {
237 /* Increment next entry's tick count. */
238 if (t
->c_next
&& t
->c_time
> 0)
239 t
->c_next
->c_time
+= t
->c_time
;
241 /* Move entry from callout queue to callfree queue. */
242 p
->c_next
= t
->c_next
;
243 t
->c_next
= callfree
;
251 * Compute number of hz until specified time. Used to
252 * compute third argument to timeout() from an absolute time.
258 register unsigned long ticks
;
259 register long sec
, usec
;
263 * If the number of usecs in the whole seconds part of the time
264 * difference fits in a long, then the total number of usecs will
265 * fit in an unsigned long. Compute the total and convert it to
266 * ticks, rounding up and adding 1 to allow for the current tick
267 * to expire. Rounding also depends on unsigned long arithmetic
270 * Otherwise, if the number of ticks in the whole seconds part of
271 * the time difference fits in a long, then convert the parts to
272 * ticks separately and add, using similar rounding methods and
273 * overflow avoidance. This method would work in the previous
274 * case but it is slightly slower and assumes that hz is integral.
276 * Otherwise, round the time difference down to the maximum
277 * representable value.
279 * If ints have 32 bits, then the maximum value for any timeout in
280 * 10ms ticks is 248 days.
283 sec
= tv
->tv_sec
- kern_time
.tv_sec
;
284 usec
= tv
->tv_usec
- kern_time
.tv_usec
;
292 printf("hzto: negative time difference %ld sec %ld usec\n",
296 } else if (sec
<= LONG_MAX
/ 1000000)
297 ticks
= (sec
* 1000000 + (unsigned long)usec
+ (tick
- 1))
299 else if (sec
<= LONG_MAX
/ hz
)
301 + ((unsigned long)usec
+ (tick
- 1)) / tick
+ 1;