put vax headers in their place
[unix-history] / usr / src / sys / kern / kern_clock.c
CommitLineData
76b2a182 1/* kern_clock.c 4.40 82/09/08 */
83be5fac
BJ
2
3#include "../h/param.h"
4#include "../h/systm.h"
d9b8447e 5#include "../h/dk.h"
0a34b6fd 6#include "../h/callout.h"
83be5fac
BJ
7#include "../h/dir.h"
8#include "../h/user.h"
f0da6d20 9#include "../h/kernel.h"
83be5fac 10#include "../h/proc.h"
83be5fac
BJ
11#include "../h/psl.h"
12#include "../h/vm.h"
83be5fac 13#include "../h/text.h"
c53dce5d
RE
14#ifdef MUSH
15#include "../h/quota.h"
16#include "../h/share.h"
17#endif
83be5fac 18
76b2a182
BJ
19/*
20 * Clock handling routines.
21 *
22 * This code is written for a machine with only one interval timer,
23 * and does timing and resource utilization estimation statistically
24 * based on the state of the machine hz times a second. A machine
25 * with proper clocks (running separately in user state, system state,
26 * interrupt state and idle state) as well as a time-of-day clock
27 * would allow a non-approximate implementation.
28 */
6602c75b 29
76b2a182
BJ
30/*
31 * TODO:
32 * * Keep more accurate statistics by simulating good interval timers.
33 * * Use the time-of-day clock on the VAX to keep more accurate time
34 * than is possible by repeated use of the interval timer.
35 * * Allocate more timeout table slots when table overflows.
36 */
83be5fac 37
76b2a182
BJ
38/* bump a timeval by a small number of usec's */
39#define bumptime(tp, usec) \
40 (tp)->tv_usec += usec; \
27b91f59
BJ
41 if ((tp)->tv_usec >= 1000000) { \
42 (tp)->tv_usec -= 1000000; \
43 (tp)->tv_sec++; \
44 }
72857acf 45
76b2a182
BJ
46/*
47 * The (single) hardware interval timer.
48 * We update the events relating to real time, and then
49 * make a gross assumption: that the system has been in the
50 * state it is in (user state, kernel state, interrupt state,
51 * or idle state) for the entire last time interval, and
52 * update statistics accordingly.
53 */
260ea681 54/*ARGSUSED*/
f403d99f 55hardclock(pc, ps)
4512b9a4 56 caddr_t pc;
83be5fac 57{
0a34b6fd 58 register struct callout *p1;
27b91f59 59 register struct proc *p;
f403d99f 60 register int s, cpstate;
c53dce5d 61 extern double avenrun[];
83be5fac 62
76b2a182
BJ
63 /*
64 * Update real-time timeout queue.
65 * At front of queue are some number of events which are ``due''.
66 * The time to these is <= 0 and if negative represents the
67 * number of ticks which have passed since it was supposed to happen.
68 * The rest of the q elements (times > 0) are events yet to happen,
69 * where the time for each is given as a delta from the previous.
70 * Decrementing just the first of these serves to decrement the time
71 * to all events.
72 */
c4710996 73 for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next)
d01b68d6 74 --p1->c_time;
c4710996 75 if (p1)
d01b68d6 76 --p1->c_time;
5da67d35 77
76b2a182
BJ
78 /*
79 * If the cpu is currently scheduled to a process, then
80 * charge it with resource utilization for a tick, updating
81 * statistics which run in (user+system) virtual time,
82 * such as the cpu time limit and profiling timers.
83 * This assumes that the current process has been running
84 * the entire last tick.
85 */
83be5fac
BJ
86 if (!noproc) {
87 s = u.u_procp->p_rssize;
27b91f59 88 u.u_ru.ru_idrss += s; u.u_ru.ru_isrss += 0; /* XXX */
83be5fac
BJ
89 if (u.u_procp->p_textp) {
90 register int xrss = u.u_procp->p_textp->x_rssize;
91
92 s += xrss;
f0da6d20 93 u.u_ru.ru_ixrss += xrss;
83be5fac 94 }
f0da6d20
BJ
95 if (s > u.u_ru.ru_maxrss)
96 u.u_ru.ru_maxrss = s;
97 if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) >
98 u.u_rlimit[RLIMIT_CPU].rlim_cur) {
39f2f769 99 psignal(u.u_procp, SIGXCPU);
f0da6d20
BJ
100 if (u.u_rlimit[RLIMIT_CPU].rlim_cur <
101 u.u_rlimit[RLIMIT_CPU].rlim_max)
102 u.u_rlimit[RLIMIT_CPU].rlim_cur += 5;
39f2f769 103 }
27b91f59
BJ
104 if (timerisset(&u.u_timer[ITIMER_PROF].it_value) &&
105 itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0)
106 psignal(u.u_procp, SIGPROF);
83be5fac 107 }
27b91f59 108
76b2a182
BJ
109 /*
110 * Charge the time out based on the mode the cpu is in.
111 * Here again we fudge for the lack of proper interval timers
112 * assuming that the current state has been around at least
113 * one tick.
114 */
83be5fac 115 if (USERMODE(ps)) {
76b2a182
BJ
116 /*
117 * CPU was in user state. Increment
118 * user time counter, and process process-virtual time
119 * interval timer.
120 */
121 bumptime(&u.u_ru.ru_utime, tick);
27b91f59
BJ
122 if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) &&
123 itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0)
124 psignal(u.u_procp, SIGVTALRM);
f0da6d20 125 if (u.u_procp->p_nice > NZERO)
41888f16
BJ
126 cpstate = CP_NICE;
127 else
128 cpstate = CP_USER;
83be5fac 129 } else {
76b2a182
BJ
130 /*
131 * CPU was in system state. If profiling kernel
132 * increment a counter. If no process is running
133 * then this is a system tick if we were running
134 * at a non-zero IPL (in a driver). If a process is running,
135 * then we charge it with system time even if we were
136 * at a non-zero IPL, since the system often runs
137 * this way during processing of system calls.
138 * This is approximate, but the lack of true interval
139 * timers makes doing anything else difficult.
140 */
3484be37
BJ
141#ifdef GPROF
142 int k = pc - s_lowpc;
143 if (profiling < 2 && k < s_textsize)
144 kcount[k / sizeof (*kcount)]++;
2752c877 145#endif
41888f16 146 cpstate = CP_SYS;
ddb3ced5
SL
147 if (noproc) {
148 if ((ps&PSL_IPL) != 0)
149 cpstate = CP_IDLE;
f0da6d20 150 } else {
76b2a182 151 bumptime(&u.u_ru.ru_stime, tick);
f0da6d20 152 }
83be5fac 153 }
27b91f59 154
76b2a182
BJ
155 /*
156 * We maintain statistics shown by user-level statistics
157 * programs: the amount of time in each cpu state, and
158 * the amount of time each of DK_NDRIVE ``drives'' is busy.
159 */
2d7d59e9 160 cp_time[cpstate]++;
f403d99f
BJ
161 for (s = 0; s < DK_NDRIVE; s++)
162 if (dk_busy&(1<<s))
163 dk_time[s]++;
27b91f59 164
76b2a182
BJ
165 /*
166 * We adjust the priority of the current process.
167 * The priority of a process gets worse as it accumulates
168 * CPU time. The cpu usage estimator (p_cpu) is increased here
169 * and the formula for computing priorities (in kern_synch.c)
170 * will compute a different value each time the p_cpu increases
171 * by 4. The cpu usage estimator ramps up quite quickly when
172 * the process is running (linearly), and decays away exponentially,
173 * at a rate which is proportionally slower when the system is
174 * busy. The basic principal is that the system will 90% forget
175 * that a process used a lot of CPU time in 5*loadav seconds.
176 * This causes the system to favor processes which haven't run
177 * much recently, and to round-robin among other processes.
178 */
83be5fac 179 if (!noproc) {
27b91f59
BJ
180 p = u.u_procp;
181 p->p_cpticks++;
182 if (++p->p_cpu == 0)
183 p->p_cpu--;
c53dce5d 184#ifdef MUSH
27b91f59
BJ
185 p->p_quota->q_cost += (p->p_nice > NZERO ?
186 (shconsts.sc_tic * ((2*NZERO)-p->p_nice)) / NZERO :
c53dce5d
RE
187 shconsts.sc_tic) * (((int)avenrun[0]+2)/3);
188#endif
76b2a182 189 if ((p->p_cpu&3) == 0) {
27b91f59
BJ
190 (void) setpri(p);
191 if (p->p_pri >= PUSER)
192 p->p_pri = p->p_usrpri;
83be5fac
BJ
193 }
194 }
76b2a182
BJ
195
196 /*
197 * Increment the time-of-day, and schedule
198 * processing of the callouts at a very low cpu priority,
199 * so we don't keep the relatively high clock interrupt
200 * priority any longer than necessary.
201 */
202 bumptime(&time, tick);
f403d99f
BJ
203 setsoftclock();
204}
205
76b2a182
BJ
206/*
207 * Software priority level clock interrupt.
208 * Run periodic events from timeout queue.
209 */
260ea681 210/*ARGSUSED*/
f403d99f 211softclock(pc, ps)
4512b9a4 212 caddr_t pc;
f403d99f 213{
f403d99f 214
27b91f59 215 for (;;) {
76b2a182
BJ
216 register struct callout *p1;
217 register caddr_t arg;
218 register int (*func)();
219 register int a, s;
220
27b91f59
BJ
221 s = spl7();
222 if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
223 splx(s);
224 break;
f403d99f 225 }
76b2a182 226 arg = p1->c_arg; func = p1->c_func; a = p1->c_time;
27b91f59 227 calltodo.c_next = p1->c_next;
27b91f59
BJ
228 p1->c_next = callfree;
229 callfree = p1;
230 (void) splx(s);
d01b68d6 231 (*func)(arg, a);
f403d99f 232 }
83be5fac
BJ
233}
234
235/*
27b91f59 236 * Arrange that (*fun)(arg) is called in tim/hz seconds.
83be5fac
BJ
237 */
238timeout(fun, arg, tim)
4512b9a4
BJ
239 int (*fun)();
240 caddr_t arg;
27b91f59 241 int tim;
83be5fac 242{
c4710996 243 register struct callout *p1, *p2, *pnew;
83be5fac
BJ
244 register int t;
245 int s;
246
247 t = tim;
83be5fac 248 s = spl7();
c4710996
BJ
249 pnew = callfree;
250 if (pnew == NULL)
251 panic("timeout table overflow");
252 callfree = pnew->c_next;
253 pnew->c_arg = arg;
254 pnew->c_func = fun;
255 for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
256 t -= p2->c_time;
257 p1->c_next = pnew;
258 pnew->c_next = p2;
259 pnew->c_time = t;
260 if (p2)
261 p2->c_time -= t;
83be5fac
BJ
262 splx(s);
263}
1fa9ff62
SL
264
265/*
266 * untimeout is called to remove a function timeout call
267 * from the callout structure.
268 */
27b91f59 269untimeout(fun, arg)
1fa9ff62
SL
270 int (*fun)();
271 caddr_t arg;
272{
1fa9ff62
SL
273 register struct callout *p1, *p2;
274 register int s;
275
276 s = spl7();
277 for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) {
278 if (p2->c_func == fun && p2->c_arg == arg) {
d01b68d6 279 if (p2->c_next && p2->c_time > 0)
1fa9ff62
SL
280 p2->c_next->c_time += p2->c_time;
281 p1->c_next = p2->c_next;
282 p2->c_next = callfree;
283 callfree = p2;
284 break;
285 }
286 }
287 splx(s);
288}
d01b68d6 289
76b2a182
BJ
290/*
291 * Compute number of hz until specified time.
292 * Used to compute third argument to timeout() from an
293 * absolute time.
294 */
d01b68d6
BJ
295hzto(tv)
296 struct timeval *tv;
297{
76b2a182
BJ
298 register long ticks;
299 register long sec;
d01b68d6
BJ
300 int s = spl7();
301
76b2a182
BJ
302 /*
303 * If number of milliseconds will fit in 32 bit arithmetic,
304 * then compute number of milliseconds to time and scale to
305 * ticks. Otherwise just compute number of hz in time, rounding
306 * times greater than representible to maximum value.
307 *
308 * Delta times less than 25 days can be computed ``exactly''.
309 * Maximum value for any timeout in 10ms ticks is 250 days.
310 */
311 sec = tv->tv_sec - time.tv_sec;
312 if (sec <= 0x7fffffff / 1000 - 1000)
313 ticks = ((tv->tv_sec - time.tv_sec) * 1000 +
314 (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000);
315 else if (sec <= 0x7fffffff / hz)
316 ticks = sec * hz;
317 else
318 ticks = 0x7fffffff;
d01b68d6
BJ
319 splx(s);
320 return (ticks);
321}