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