chksize() can now use constants calculated by vminit() in vm_page.c
[unix-history] / usr / src / sys / kern / kern_clock.c
CommitLineData
99e47f6b 1/* kern_clock.c 6.10 84/11/14 */
961945a8
SL
2
3#include "../machine/reg.h"
4#include "../machine/psl.h"
83be5fac 5
94368568
JB
6#include "param.h"
7#include "systm.h"
8#include "dk.h"
9#include "callout.h"
10#include "dir.h"
11#include "user.h"
12#include "kernel.h"
13#include "proc.h"
14#include "vm.h"
15#include "text.h"
83be5fac 16
961945a8
SL
17#ifdef vax
18#include "../vax/mtpr.h"
19#endif
20
8487304f 21#ifdef GPROF
94368568 22#include "gprof.h"
8487304f
KM
23#endif
24
45e9acec
MK
25#define ADJTIME /* For now... */
26#define ADJ_TICK 1000
27int adjtimedelta;
28
76b2a182
BJ
29/*
30 * Clock handling routines.
31 *
53a32545
SL
32 * This code is written to operate with two timers which run
33 * independently of each other. The main clock, running at hz
34 * times per second, is used to do scheduling and timeout calculations.
35 * The second timer does resource utilization estimation statistically
36 * based on the state of the machine phz times a second. Both functions
37 * can be performed by a single clock (ie hz == phz), however the
38 * statistics will be much more prone to errors. Ideally a machine
39 * would have separate clocks measuring time spent in user state, system
40 * state, interrupt state, and idle state. These clocks would allow a non-
41 * approximate measure of resource utilization.
76b2a182 42 */
6602c75b 43
76b2a182
BJ
44/*
45 * TODO:
88a7a62a
SL
46 * time of day, system/user timing, timeouts, profiling on separate timers
47 * allocate more timeout table slots when table overflows.
76b2a182 48 */
ad8023d1
KM
49#ifdef notdef
50/*
51 * Bump a timeval by a small number of usec's.
52 */
53bumptime(tp, usec)
54 register struct timeval *tp;
55 int usec;
56{
57
58 tp->tv_usec += usec;
59 if (tp->tv_usec >= 1000000) {
60 tp->tv_usec -= 1000000;
61 tp->tv_sec++;
62 }
63}
64#endif notdef
65#define BUMPTIME(t, usec) { \
66 register struct timeval *tp = (t); \
67 \
68 tp->tv_usec += (usec); \
69 if (tp->tv_usec >= 1000000) { \
70 tp->tv_usec -= 1000000; \
71 tp->tv_sec++; \
72 } \
73}
83be5fac 74
76b2a182 75/*
53a32545
SL
76 * The hz hardware interval timer.
77 * We update the events relating to real time.
78 * If this timer is also being used to gather statistics,
79 * we run through the statistics gathering routine as well.
76b2a182 80 */
260ea681 81/*ARGSUSED*/
f403d99f 82hardclock(pc, ps)
4512b9a4 83 caddr_t pc;
460ab27f 84 int ps;
83be5fac 85{
0a34b6fd 86 register struct callout *p1;
27b91f59 87 register struct proc *p;
f403d99f 88 register int s, cpstate;
83be5fac 89
76b2a182
BJ
90 /*
91 * Update real-time timeout queue.
92 * At front of queue are some number of events which are ``due''.
93 * The time to these is <= 0 and if negative represents the
94 * number of ticks which have passed since it was supposed to happen.
95 * The rest of the q elements (times > 0) are events yet to happen,
96 * where the time for each is given as a delta from the previous.
97 * Decrementing just the first of these serves to decrement the time
98 * to all events.
99 */
88a7a62a
SL
100 p1 = calltodo.c_next;
101 while (p1) {
102 if (--p1->c_time > 0)
103 break;
88a7a62a
SL
104 if (p1->c_time == 0)
105 break;
106 p1 = p1->c_next;
107 }
5da67d35 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
877ef342 119 * interval timer.
76b2a182 120 */
ad8023d1 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 */
41888f16 141 cpstate = CP_SYS;
ddb3ced5 142 if (noproc) {
460ab27f 143 if (BASEPRI(ps))
ddb3ced5 144 cpstate = CP_IDLE;
f0da6d20 145 } else {
ad8023d1 146 BUMPTIME(&u.u_ru.ru_stime, tick);
f0da6d20 147 }
83be5fac 148 }
27b91f59 149
9fb1a8d0
SL
150 /*
151 * If the cpu is currently scheduled to a process, then
152 * charge it with resource utilization for a tick, updating
153 * statistics which run in (user+system) virtual time,
154 * such as the cpu time limit and profiling timers.
155 * This assumes that the current process has been running
156 * the entire last tick.
157 */
158 if (noproc == 0 && cpstate != CP_IDLE) {
159 if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) >
160 u.u_rlimit[RLIMIT_CPU].rlim_cur) {
161 psignal(u.u_procp, SIGXCPU);
162 if (u.u_rlimit[RLIMIT_CPU].rlim_cur <
163 u.u_rlimit[RLIMIT_CPU].rlim_max)
164 u.u_rlimit[RLIMIT_CPU].rlim_cur += 5;
165 }
166 if (timerisset(&u.u_timer[ITIMER_PROF].it_value) &&
167 itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0)
168 psignal(u.u_procp, SIGPROF);
169 s = u.u_procp->p_rssize;
170 u.u_ru.ru_idrss += s; u.u_ru.ru_isrss += 0; /* XXX */
171 if (u.u_procp->p_textp) {
172 register int xrss = u.u_procp->p_textp->x_rssize;
173
174 s += xrss;
175 u.u_ru.ru_ixrss += xrss;
176 }
177 if (s > u.u_ru.ru_maxrss)
178 u.u_ru.ru_maxrss = s;
179 }
180
76b2a182
BJ
181 /*
182 * We adjust the priority of the current process.
183 * The priority of a process gets worse as it accumulates
184 * CPU time. The cpu usage estimator (p_cpu) is increased here
185 * and the formula for computing priorities (in kern_synch.c)
186 * will compute a different value each time the p_cpu increases
187 * by 4. The cpu usage estimator ramps up quite quickly when
188 * the process is running (linearly), and decays away exponentially,
189 * at a rate which is proportionally slower when the system is
190 * busy. The basic principal is that the system will 90% forget
191 * that a process used a lot of CPU time in 5*loadav seconds.
192 * This causes the system to favor processes which haven't run
193 * much recently, and to round-robin among other processes.
194 */
83be5fac 195 if (!noproc) {
27b91f59
BJ
196 p = u.u_procp;
197 p->p_cpticks++;
198 if (++p->p_cpu == 0)
199 p->p_cpu--;
76b2a182 200 if ((p->p_cpu&3) == 0) {
27b91f59
BJ
201 (void) setpri(p);
202 if (p->p_pri >= PUSER)
203 p->p_pri = p->p_usrpri;
83be5fac
BJ
204 }
205 }
76b2a182 206
53a32545
SL
207 /*
208 * If the alternate clock has not made itself known then
209 * we must gather the statistics.
210 */
211 if (phz == 0)
212 gatherstats(pc, ps);
53a32545 213
76b2a182
BJ
214 /*
215 * Increment the time-of-day, and schedule
216 * processing of the callouts at a very low cpu priority,
217 * so we don't keep the relatively high clock interrupt
218 * priority any longer than necessary.
219 */
45e9acec
MK
220#ifdef ADJTIME
221 if (adjtimedelta == 0)
222 bumptime(&time, tick);
223 else {
224 if (adjtimedelta < 0) {
225 bumptime(&time, tick-ADJ_TICK);
226 adjtimedelta++;
227 } else {
228 bumptime(&time, tick+ADJ_TICK);
229 adjtimedelta--;
230 }
231 }
232#else
99e47f6b
MK
233 if (adjtimedelta == 0)
234 BUMPTIME(&time, tick)
235 else {
236 register delta;
237
238 if (adjtimedelta < 0) {
239 delta = tick - tickadj;
240 adjtimedelta += tickadj;
241 } else {
242 delta = tick + tickadj;
243 adjtimedelta -= tickadj;
244 }
245 BUMPTIME(&time, delta);
246 }
45e9acec 247#endif
ca6b57a4 248 setsoftclock();
f403d99f
BJ
249}
250
d976d466 251int dk_ndrive = DK_NDRIVE;
53a32545
SL
252/*
253 * Gather statistics on resource utilization.
254 *
255 * We make a gross assumption: that the system has been in the
256 * state it is in (user state, kernel state, interrupt state,
257 * or idle state) for the entire last time interval, and
258 * update statistics accordingly.
259 */
88a7a62a 260/*ARGSUSED*/
53a32545
SL
261gatherstats(pc, ps)
262 caddr_t pc;
263 int ps;
264{
265 int cpstate, s;
266
267 /*
268 * Determine what state the cpu is in.
269 */
270 if (USERMODE(ps)) {
271 /*
272 * CPU was in user state.
273 */
274 if (u.u_procp->p_nice > NZERO)
275 cpstate = CP_NICE;
276 else
277 cpstate = CP_USER;
278 } else {
279 /*
280 * CPU was in system state. If profiling kernel
281 * increment a counter.
282 */
283 cpstate = CP_SYS;
284 if (noproc && BASEPRI(ps))
285 cpstate = CP_IDLE;
286#ifdef GPROF
287 s = pc - s_lowpc;
288 if (profiling < 2 && s < s_textsize)
289 kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
290#endif
291 }
292 /*
293 * We maintain statistics shown by user-level statistics
294 * programs: the amount of time in each cpu state, and
295 * the amount of time each of DK_NDRIVE ``drives'' is busy.
296 */
297 cp_time[cpstate]++;
298 for (s = 0; s < DK_NDRIVE; s++)
299 if (dk_busy&(1<<s))
300 dk_time[s]++;
301}
302
76b2a182
BJ
303/*
304 * Software priority level clock interrupt.
305 * Run periodic events from timeout queue.
306 */
260ea681 307/*ARGSUSED*/
f403d99f 308softclock(pc, ps)
4512b9a4 309 caddr_t pc;
460ab27f 310 int ps;
f403d99f 311{
f403d99f 312
27b91f59 313 for (;;) {
76b2a182
BJ
314 register struct callout *p1;
315 register caddr_t arg;
316 register int (*func)();
317 register int a, s;
318
27b91f59
BJ
319 s = spl7();
320 if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
321 splx(s);
322 break;
f403d99f 323 }
76b2a182 324 arg = p1->c_arg; func = p1->c_func; a = p1->c_time;
27b91f59 325 calltodo.c_next = p1->c_next;
27b91f59
BJ
326 p1->c_next = callfree;
327 callfree = p1;
4f083fd7 328 splx(s);
d01b68d6 329 (*func)(arg, a);
f403d99f 330 }
877ef342 331 /*
db1f1262
SL
332 * If trapped user-mode and profiling, give it
333 * a profiling tick.
877ef342 334 */
db1f1262
SL
335 if (USERMODE(ps)) {
336 register struct proc *p = u.u_procp;
337
338 if (u.u_prof.pr_scale) {
339 p->p_flag |= SOWEUPC;
340 aston();
341 }
db1f1262
SL
342 /*
343 * Check to see if process has accumulated
344 * more than 10 minutes of user time. If so
345 * reduce priority to give others a chance.
346 */
347 if (p->p_uid && p->p_nice == NZERO &&
348 u.u_ru.ru_utime.tv_sec > 10 * 60) {
349 p->p_nice = NZERO+4;
350 (void) setpri(p);
351 p->p_pri = p->p_usrpri;
352 }
877ef342 353 }
83be5fac
BJ
354}
355
88a7a62a
SL
356/*
357 * Arrange that (*fun)(arg) is called in t/hz seconds.
83be5fac 358 */
88a7a62a 359timeout(fun, arg, t)
4512b9a4
BJ
360 int (*fun)();
361 caddr_t arg;
88a7a62a 362 register int t;
83be5fac 363{
c4710996 364 register struct callout *p1, *p2, *pnew;
88a7a62a 365 register int s = spl7();
83be5fac 366
88a7a62a
SL
367 if (t == 0)
368 t = 1;
c4710996
BJ
369 pnew = callfree;
370 if (pnew == NULL)
371 panic("timeout table overflow");
372 callfree = pnew->c_next;
373 pnew->c_arg = arg;
374 pnew->c_func = fun;
375 for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
d45b61eb
SL
376 if (p2->c_time > 0)
377 t -= p2->c_time;
c4710996
BJ
378 p1->c_next = pnew;
379 pnew->c_next = p2;
380 pnew->c_time = t;
381 if (p2)
382 p2->c_time -= t;
83be5fac
BJ
383 splx(s);
384}
1fa9ff62
SL
385
386/*
387 * untimeout is called to remove a function timeout call
388 * from the callout structure.
389 */
27b91f59 390untimeout(fun, arg)
1fa9ff62
SL
391 int (*fun)();
392 caddr_t arg;
393{
1fa9ff62
SL
394 register struct callout *p1, *p2;
395 register int s;
396
397 s = spl7();
398 for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) {
399 if (p2->c_func == fun && p2->c_arg == arg) {
d01b68d6 400 if (p2->c_next && p2->c_time > 0)
1fa9ff62
SL
401 p2->c_next->c_time += p2->c_time;
402 p1->c_next = p2->c_next;
403 p2->c_next = callfree;
404 callfree = p2;
405 break;
406 }
407 }
408 splx(s);
409}
d01b68d6 410
76b2a182
BJ
411/*
412 * Compute number of hz until specified time.
413 * Used to compute third argument to timeout() from an
414 * absolute time.
415 */
d01b68d6
BJ
416hzto(tv)
417 struct timeval *tv;
418{
76b2a182
BJ
419 register long ticks;
420 register long sec;
d01b68d6
BJ
421 int s = spl7();
422
76b2a182
BJ
423 /*
424 * If number of milliseconds will fit in 32 bit arithmetic,
425 * then compute number of milliseconds to time and scale to
426 * ticks. Otherwise just compute number of hz in time, rounding
427 * times greater than representible to maximum value.
428 *
429 * Delta times less than 25 days can be computed ``exactly''.
430 * Maximum value for any timeout in 10ms ticks is 250 days.
431 */
432 sec = tv->tv_sec - time.tv_sec;
433 if (sec <= 0x7fffffff / 1000 - 1000)
434 ticks = ((tv->tv_sec - time.tv_sec) * 1000 +
435 (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000);
436 else if (sec <= 0x7fffffff / hz)
437 ticks = sec * hz;
438 else
439 ticks = 0x7fffffff;
d01b68d6
BJ
440 splx(s);
441 return (ticks);
442}
88a7a62a
SL
443
444profil()
445{
446 register struct a {
447 short *bufbase;
448 unsigned bufsize;
449 unsigned pcoffset;
450 unsigned pcscale;
451 } *uap = (struct a *)u.u_ap;
452 register struct uprof *upp = &u.u_prof;
453
454 upp->pr_base = uap->bufbase;
455 upp->pr_size = uap->bufsize;
456 upp->pr_off = uap->pcoffset;
457 upp->pr_scale = uap->pcscale;
458}
459
460opause()
461{
462
463 for (;;)
464 sleep((caddr_t)&u, PSLEP);
465}