change gdium conf to print message out both on uart and lcd
[pmon-gdium.git] / sys / kern / kern_clock.c
blob698b38f2aa588bf6c41343b1228d8681531157f2
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 $ */
5 /*-
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
16 * are met:
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
40 * SUCH DAMAGE.
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>
49 #include <sys/proc.h>
50 #include <sys/resourcevar.h>
51 #include <vm/vm.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.)
82 * TODO:
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); \
92 register long us; \
94 tp->tv_usec = us = tp->tv_usec + (usec); \
95 if (us >= 1000000) { \
96 tp->tv_usec = us - 1000000; \
97 tp->tv_sec++; \
98 } \
101 int stathz;
102 int schedhz;
103 int profhz;
104 int profprocs;
105 int ticks;
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.
117 void
118 initclocks()
120 register int i;
123 * Set divisors to 1 (normal case) and let the machine-specific
124 * code do its bit.
126 psdiv = pscnt = 1;
127 cpu_initclocks();
130 * Compute profhz/stathz, and fix profhz if needed.
132 i = stathz ? stathz : hz;
133 if (profhz == 0)
134 profhz = i;
135 psratio = profhz / i;
140 * The real-time timer, interrupting hz times per second.
142 void
143 hardclock(frame)
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
162 * to all events.
164 needsoft = 0;
165 for (p1 = calltodo.c_next; p1 != NULL; p1 = p1->c_next) {
166 if (--p1->c_time > 0)
167 break;
168 needsoft = 1;
169 if (p1->c_time == 0)
170 break;
173 p = curproc;
174 if (p) {
175 register struct pstats *pstats;
178 * Run current process's virtual and profile time, as needed.
180 pstats = p->p_stats;
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)
187 psignal(p, SIGPROF);
190 #ifdef NOTUSED_BY_PMON
192 * If no separate statistics clock is available, run it from here.
194 if (stathz == 0)
195 statclock(frame);
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.
206 ticks++;
207 delta = tick;
209 if (tickfix) {
210 tickfixcnt += tickfix;
211 if (tickfixcnt >= tickfixinterval) {
212 delta++;
213 tickfixcnt -= tickfixinterval;
217 #ifdef NOTUSED_BY_PMON
218 /* Imprecise 4bsd adjtime() handling */
219 if (timedelta != 0) {
220 delta += tickdelta;
221 timedelta -= tickdelta;
223 #endif /* NOTUSED_BY_PMON */
225 #ifdef notyet
226 microset();
227 #endif
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.
235 if (needsoft) {
236 #ifndef PMON
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();
243 softclock();
244 } else
245 #endif
246 setsoftclock();
251 * Software (low priority) clock interrupt.
252 * Run periodic events from timeout queue.
254 /*ARGSUSED*/
255 void
256 softclock()
258 register struct callout *c;
259 register void *arg;
260 register void (*func) __P((void *));
261 register int s;
263 s = splhigh();
264 while ((c = calltodo.c_next) != NULL && c->c_time <= 0) {
265 func = c->c_func;
266 arg = c->c_arg;
267 calltodo.c_next = c->c_next;
268 c->c_next = callfree;
269 callfree = c;
270 splx(s);
271 (*func)(arg);
272 (void) splhigh();
274 splx(s);
278 * timeout --
279 * Execute a function after a specified length of time.
281 * untimeout --
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.
289 void
290 timeout(ftn, arg, ticks)
291 void (*ftn) __P((void *));
292 void *arg;
293 register int ticks;
295 register struct callout *new, *p, *t;
296 register int s;
298 if (ticks <= 0)
299 ticks = 1;
301 /* Lock out the clock. */
302 s = splhigh();
304 /* Fill in the next free callout structure. */
305 if (callfree == NULL)
306 panic("timeout table full");
307 new = callfree;
308 callfree = new->c_next;
309 new->c_arg = arg;
310 new->c_func = ftn;
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
318 * overdue events.
320 for (p = &calltodo;
321 (t = p->c_next) != NULL && ticks > t->c_time; p = t)
322 if (t->c_time > 0)
323 ticks -= t->c_time;
324 new->c_time = ticks;
325 if (t != NULL)
326 t->c_time -= ticks;
328 /* Insert the new entry into the queue. */
329 p->c_next = new;
330 new->c_next = t;
331 splx(s);
334 void
335 untimeout(ftn, arg)
336 void (*ftn) __P((void *));
337 void *arg;
339 register struct callout *p, *t;
340 register int s;
342 s = splhigh();
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;
352 callfree = t;
353 break;
355 splx(s);
359 * Compute number of hz until specified time. Used to
360 * compute third argument to timeout() from an absolute time.
363 hzto(tv)
364 struct timeval *tv;
366 register long ticks, sec;
367 int s;
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
381 * ticks is 250 days.
383 s = splhigh();
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)
389 ticks = sec * hz;
390 else
391 ticks = 0x7fffffff;
392 splx(s);
393 return (ticks);