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