|
|
1.1 root 1: /*-
2: * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by the University of
16: * California, Berkeley and its contributors.
17: * 4. Neither the name of the University nor the names of its contributors
18: * may be used to endorse or promote products derived from this software
19: * without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: *
33: * @(#)kern_clock.c 7.16 (Berkeley) 5/9/91
1.1.1.3 ! root 34: *
! 35: * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
! 36: * -------------------- ----- ----------------------
! 37: * CURRENT PATCH LEVEL: 1 00113
! 38: * -------------------- ----- ----------------------
! 39: *
! 40: * 08 Apr 93 Poul-Henning Kamp Add support for dcfclock
1.1 root 41: */
42:
43: #include "param.h"
44: #include "systm.h"
45: #include "dkstat.h"
46: #include "callout.h"
47: #include "kernel.h"
48: #include "proc.h"
49: #include "resourcevar.h"
50:
51: #include "machine/cpu.h"
52:
53: #ifdef GPROF
54: #include "gprof.h"
55: #endif
56:
57: /*
58: * Clock handling routines.
59: *
60: * This code is written to operate with two timers which run
61: * independently of each other. The main clock, running at hz
62: * times per second, is used to do scheduling and timeout calculations.
63: * The second timer does resource utilization estimation statistically
64: * based on the state of the machine phz times a second. Both functions
65: * can be performed by a single clock (ie hz == phz), however the
66: * statistics will be much more prone to errors. Ideally a machine
67: * would have separate clocks measuring time spent in user state, system
68: * state, interrupt state, and idle state. These clocks would allow a non-
69: * approximate measure of resource utilization.
70: */
71:
72: /*
73: * TODO:
74: * time of day, system/user timing, timeouts, profiling on separate timers
75: * allocate more timeout table slots when table overflows.
76: */
77:
78: /*
79: * Bump a timeval by a small number of usec's.
80: */
81: #define BUMPTIME(t, usec) { \
82: register struct timeval *tp = (t); \
83: \
84: tp->tv_usec += (usec); \
85: if (tp->tv_usec >= 1000000) { \
86: tp->tv_usec -= 1000000; \
87: tp->tv_sec++; \
88: } \
89: }
90:
91: /*
92: * The hz hardware interval timer.
93: * We update the events relating to real time.
94: * If this timer is also being used to gather statistics,
95: * we run through the statistics gathering routine as well.
96: */
97: hardclock(frame)
98: clockframe frame;
99: {
100: register struct callout *p1;
101: register struct proc *p = curproc;
102: register struct pstats *pstats;
103: register int s;
104: int needsoft = 0;
105: extern int tickdelta;
106: extern long timedelta;
107:
108: /*
109: * Update real-time timeout queue.
110: * At front of queue are some number of events which are ``due''.
111: * The time to these is <= 0 and if negative represents the
112: * number of ticks which have passed since it was supposed to happen.
113: * The rest of the q elements (times > 0) are events yet to happen,
114: * where the time for each is given as a delta from the previous.
115: * Decrementing just the first of these serves to decrement the time
116: * to all events.
117: */
118: p1 = calltodo.c_next;
119: while (p1) {
120: if (--p1->c_time > 0)
121: break;
122: needsoft = 1;
123: if (p1->c_time == 0)
124: break;
125: p1 = p1->c_next;
126: }
127:
128: /*
129: * Curproc (now in p) is null if no process is running.
130: * We assume that curproc is set in user mode!
131: */
132: if (p)
133: pstats = p->p_stats;
134: /*
135: * Charge the time out based on the mode the cpu is in.
136: * Here again we fudge for the lack of proper interval timers
137: * assuming that the current state has been around at least
138: * one tick.
139: */
140: if (CLKF_USERMODE(&frame)) {
141: if (pstats->p_prof.pr_scale)
142: needsoft = 1;
143: /*
144: * CPU was in user state. Increment
145: * user time counter, and process process-virtual time
146: * interval timer.
147: */
148: BUMPTIME(&p->p_utime, tick);
149: if (timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
150: itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
151: psignal(p, SIGVTALRM);
152: } else {
153: /*
154: * CPU was in system state.
155: */
156: if (p)
157: BUMPTIME(&p->p_stime, tick);
158: }
159:
160: /*
161: * If the cpu is currently scheduled to a process, then
162: * charge it with resource utilization for a tick, updating
163: * statistics which run in (user+system) virtual time,
164: * such as the cpu time limit and profiling timers.
165: * This assumes that the current process has been running
166: * the entire last tick.
167: */
168: if (p) {
169: if ((p->p_utime.tv_sec+p->p_stime.tv_sec+1) >
170: p->p_rlimit[RLIMIT_CPU].rlim_cur) {
171: psignal(p, SIGXCPU);
172: if (p->p_rlimit[RLIMIT_CPU].rlim_cur <
173: p->p_rlimit[RLIMIT_CPU].rlim_max)
174: p->p_rlimit[RLIMIT_CPU].rlim_cur += 5;
175: }
176: if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
177: itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
178: psignal(p, SIGPROF);
179:
180: /*
181: * We adjust the priority of the current process.
182: * The priority of a process gets worse as it accumulates
183: * CPU time. The cpu usage estimator (p_cpu) is increased here
184: * and the formula for computing priorities (in kern_synch.c)
185: * will compute a different value each time the p_cpu increases
186: * by 4. The cpu usage estimator ramps up quite quickly when
187: * the process is running (linearly), and decays away
188: * exponentially, * at a rate which is proportionally slower
189: * when the system is busy. The basic principal is that the
190: * system will 90% forget that a process used a lot of CPU
191: * time in 5*loadav seconds. This causes the system to favor
192: * processes which haven't run much recently, and to
193: * round-robin among other processes.
194: */
195: p->p_cpticks++;
196: if (++p->p_cpu == 0)
197: p->p_cpu--;
198: if ((p->p_cpu&3) == 0) {
199: setpri(p);
200: if (p->p_pri >= PUSER)
201: p->p_pri = p->p_usrpri;
202: }
203: }
204:
205: /*
206: * If the alternate clock has not made itself known then
207: * we must gather the statistics.
208: */
209: if (phz == 0)
210: gatherstats(&frame);
211:
212: /*
213: * Increment the time-of-day, and schedule
214: * processing of the callouts at a very low cpu priority,
215: * so we don't keep the relatively high clock interrupt
216: * priority any longer than necessary.
217: */
218: if (timedelta == 0)
219: BUMPTIME(&time, tick)
220: else {
221: register delta;
222:
223: if (timedelta < 0) {
224: delta = tick - tickdelta;
225: timedelta += tickdelta;
226: } else {
227: delta = tick + tickdelta;
228: timedelta -= tickdelta;
229: }
230: BUMPTIME(&time, delta);
231: }
1.1.1.3 ! root 232: #ifdef DCFCLK
! 233: /*
! 234: * This is lousy, but until I can get the $&^%&^(!!! signal onto one
! 235: * of the interrupt's I'll have to poll it. No, it will not work if
! 236: * you attempt -DHZ=1000, things break.
! 237: * But keep the NDCFCLK low, to avoid waste of cycles...
! 238: * [email protected]
! 239: */
! 240: dcfclk_worker();
! 241: #endif
1.1 root 242: if (needsoft) {
243: if (CLKF_BASEPRI(&frame)) {
244: /*
245: * Save the overhead of a software interrupt;
246: * it will happen as soon as we return, so do it now.
247: */
248: (void) splsoftclock();
249: softclock(frame);
250: } else
251: setsoftclock();
252: }
253: }
254:
255: int dk_ndrive = DK_NDRIVE;
256: /*
257: * Gather statistics on resource utilization.
258: *
259: * We make a gross assumption: that the system has been in the
260: * state it is in (user state, kernel state, interrupt state,
261: * or idle state) for the entire last time interval, and
262: * update statistics accordingly.
263: */
264: gatherstats(framep)
265: clockframe *framep;
266: {
267: register int cpstate, s;
268:
269: /*
270: * Determine what state the cpu is in.
271: */
272: if (CLKF_USERMODE(framep)) {
273: /*
274: * CPU was in user state.
275: */
276: if (curproc->p_nice > NZERO)
277: cpstate = CP_NICE;
278: else
279: cpstate = CP_USER;
280: } else {
281: /*
282: * CPU was in system state. If profiling kernel
283: * increment a counter. If no process is running
284: * then this is a system tick if we were running
285: * at a non-zero IPL (in a driver). If a process is running,
286: * then we charge it with system time even if we were
287: * at a non-zero IPL, since the system often runs
288: * this way during processing of system calls.
289: * This is approximate, but the lack of true interval
290: * timers makes doing anything else difficult.
291: */
292: cpstate = CP_SYS;
293: if (curproc == NULL && CLKF_BASEPRI(framep))
294: cpstate = CP_IDLE;
295: #ifdef GPROF
1.1.1.2 root 296: s = (u_long) CLKF_PC(framep) - (u_long) s_lowpc;
1.1 root 297: if (profiling < 2 && s < s_textsize)
298: kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
299: #endif
300: }
301: /*
302: * We maintain statistics shown by user-level statistics
303: * programs: the amount of time in each cpu state, and
304: * the amount of time each of DK_NDRIVE ``drives'' is busy.
305: */
306: cp_time[cpstate]++;
307: for (s = 0; s < DK_NDRIVE; s++)
308: if (dk_busy&(1<<s))
309: dk_time[s]++;
310: }
311:
312: /*
313: * Software priority level clock interrupt.
314: * Run periodic events from timeout queue.
315: */
316: /*ARGSUSED*/
317: softclock(frame)
318: clockframe frame;
319: {
320:
321: for (;;) {
322: register struct callout *p1;
323: register caddr_t arg;
324: register int (*func)();
325: register int a, s;
326:
327: s = splhigh();
328: if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
329: splx(s);
330: break;
331: }
332: arg = p1->c_arg; func = p1->c_func; a = p1->c_time;
333: calltodo.c_next = p1->c_next;
334: p1->c_next = callfree;
335: callfree = p1;
336: splx(s);
337: (*func)(arg, a);
338: }
1.1.1.2 root 339:
340: /*
341: * If no process to work with, we're finished.
342: */
343: if (curproc == 0) return;
344:
1.1 root 345: /*
346: * If trapped user-mode and profiling, give it
347: * a profiling tick.
348: */
349: if (CLKF_USERMODE(&frame)) {
350: register struct proc *p = curproc;
351:
352: if (p->p_stats->p_prof.pr_scale)
353: profile_tick(p, &frame);
354: /*
355: * Check to see if process has accumulated
356: * more than 10 minutes of user time. If so
357: * reduce priority to give others a chance.
358: */
359: if (p->p_ucred->cr_uid && p->p_nice == NZERO &&
360: p->p_utime.tv_sec > 10 * 60) {
361: p->p_nice = NZERO + 4;
362: setpri(p);
363: p->p_pri = p->p_usrpri;
364: }
365: }
366: }
367:
368: /*
369: * Arrange that (*func)(arg) is called in t/hz seconds.
370: */
371: timeout(func, arg, t)
372: int (*func)();
373: caddr_t arg;
374: register int t;
375: {
376: register struct callout *p1, *p2, *pnew;
377: register int s = splhigh();
378:
379: if (t <= 0)
380: t = 1;
381: pnew = callfree;
382: if (pnew == NULL)
383: panic("timeout table overflow");
384: callfree = pnew->c_next;
385: pnew->c_arg = arg;
386: pnew->c_func = func;
387: for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
388: if (p2->c_time > 0)
389: t -= p2->c_time;
390: p1->c_next = pnew;
391: pnew->c_next = p2;
392: pnew->c_time = t;
393: if (p2)
394: p2->c_time -= t;
395: splx(s);
396: }
397:
398: /*
399: * untimeout is called to remove a function timeout call
400: * from the callout structure.
401: */
402: untimeout(func, arg)
403: int (*func)();
404: caddr_t arg;
405: {
406: register struct callout *p1, *p2;
407: register int s;
408:
409: s = splhigh();
410: for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) {
411: if (p2->c_func == func && p2->c_arg == arg) {
412: if (p2->c_next && p2->c_time > 0)
413: p2->c_next->c_time += p2->c_time;
414: p1->c_next = p2->c_next;
415: p2->c_next = callfree;
416: callfree = p2;
417: break;
418: }
419: }
420: splx(s);
421: }
422:
423: /*
424: * Compute number of hz until specified time.
425: * Used to compute third argument to timeout() from an
426: * absolute time.
427: */
428: hzto(tv)
429: struct timeval *tv;
430: {
431: register long ticks;
432: register long sec;
433: int s = splhigh();
434:
435: /*
436: * If number of milliseconds will fit in 32 bit arithmetic,
437: * then compute number of milliseconds to time and scale to
438: * ticks. Otherwise just compute number of hz in time, rounding
439: * times greater than representible to maximum value.
440: *
441: * Delta times less than 25 days can be computed ``exactly''.
442: * Maximum value for any timeout in 10ms ticks is 250 days.
443: */
444: sec = tv->tv_sec - time.tv_sec;
445: if (sec <= 0x7fffffff / 1000 - 1000)
446: ticks = ((tv->tv_sec - time.tv_sec) * 1000 +
447: (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000);
448: else if (sec <= 0x7fffffff / hz)
449: ticks = sec * hz;
450: else
451: ticks = 0x7fffffff;
452: splx(s);
453: return (ticks);
454: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.