fix round-trip timing: rexmt timer shouldn't screw rtt up,
[unix-history] / usr / src / sys / kern / kern_clock.c
CommitLineData
94368568 1/* kern_clock.c 6.9 84/08/29 */
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
ad8023d1 233 BUMPTIME(&time, tick);
45e9acec 234#endif
ca6b57a4 235 setsoftclock();
f403d99f
BJ
236}
237
d976d466 238int dk_ndrive = DK_NDRIVE;
53a32545
SL
239/*
240 * Gather statistics on resource utilization.
241 *
242 * We make a gross assumption: that the system has been in the
243 * state it is in (user state, kernel state, interrupt state,
244 * or idle state) for the entire last time interval, and
245 * update statistics accordingly.
246 */
88a7a62a 247/*ARGSUSED*/
53a32545
SL
248gatherstats(pc, ps)
249 caddr_t pc;
250 int ps;
251{
252 int cpstate, s;
253
254 /*
255 * Determine what state the cpu is in.
256 */
257 if (USERMODE(ps)) {
258 /*
259 * CPU was in user state.
260 */
261 if (u.u_procp->p_nice > NZERO)
262 cpstate = CP_NICE;
263 else
264 cpstate = CP_USER;
265 } else {
266 /*
267 * CPU was in system state. If profiling kernel
268 * increment a counter.
269 */
270 cpstate = CP_SYS;
271 if (noproc && BASEPRI(ps))
272 cpstate = CP_IDLE;
273#ifdef GPROF
274 s = pc - s_lowpc;
275 if (profiling < 2 && s < s_textsize)
276 kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
277#endif
278 }
279 /*
280 * We maintain statistics shown by user-level statistics
281 * programs: the amount of time in each cpu state, and
282 * the amount of time each of DK_NDRIVE ``drives'' is busy.
283 */
284 cp_time[cpstate]++;
285 for (s = 0; s < DK_NDRIVE; s++)
286 if (dk_busy&(1<<s))
287 dk_time[s]++;
288}
289
76b2a182
BJ
290/*
291 * Software priority level clock interrupt.
292 * Run periodic events from timeout queue.
293 */
260ea681 294/*ARGSUSED*/
f403d99f 295softclock(pc, ps)
4512b9a4 296 caddr_t pc;
460ab27f 297 int ps;
f403d99f 298{
f403d99f 299
27b91f59 300 for (;;) {
76b2a182
BJ
301 register struct callout *p1;
302 register caddr_t arg;
303 register int (*func)();
304 register int a, s;
305
27b91f59
BJ
306 s = spl7();
307 if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
308 splx(s);
309 break;
f403d99f 310 }
76b2a182 311 arg = p1->c_arg; func = p1->c_func; a = p1->c_time;
27b91f59 312 calltodo.c_next = p1->c_next;
27b91f59
BJ
313 p1->c_next = callfree;
314 callfree = p1;
4f083fd7 315 splx(s);
d01b68d6 316 (*func)(arg, a);
f403d99f 317 }
877ef342 318 /*
db1f1262
SL
319 * If trapped user-mode and profiling, give it
320 * a profiling tick.
877ef342 321 */
db1f1262
SL
322 if (USERMODE(ps)) {
323 register struct proc *p = u.u_procp;
324
325 if (u.u_prof.pr_scale) {
326 p->p_flag |= SOWEUPC;
327 aston();
328 }
db1f1262
SL
329 /*
330 * Check to see if process has accumulated
331 * more than 10 minutes of user time. If so
332 * reduce priority to give others a chance.
333 */
334 if (p->p_uid && p->p_nice == NZERO &&
335 u.u_ru.ru_utime.tv_sec > 10 * 60) {
336 p->p_nice = NZERO+4;
337 (void) setpri(p);
338 p->p_pri = p->p_usrpri;
339 }
877ef342 340 }
83be5fac
BJ
341}
342
88a7a62a
SL
343/*
344 * Arrange that (*fun)(arg) is called in t/hz seconds.
83be5fac 345 */
88a7a62a 346timeout(fun, arg, t)
4512b9a4
BJ
347 int (*fun)();
348 caddr_t arg;
88a7a62a 349 register int t;
83be5fac 350{
c4710996 351 register struct callout *p1, *p2, *pnew;
88a7a62a 352 register int s = spl7();
83be5fac 353
88a7a62a
SL
354 if (t == 0)
355 t = 1;
c4710996
BJ
356 pnew = callfree;
357 if (pnew == NULL)
358 panic("timeout table overflow");
359 callfree = pnew->c_next;
360 pnew->c_arg = arg;
361 pnew->c_func = fun;
362 for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
d45b61eb
SL
363 if (p2->c_time > 0)
364 t -= p2->c_time;
c4710996
BJ
365 p1->c_next = pnew;
366 pnew->c_next = p2;
367 pnew->c_time = t;
368 if (p2)
369 p2->c_time -= t;
83be5fac
BJ
370 splx(s);
371}
1fa9ff62
SL
372
373/*
374 * untimeout is called to remove a function timeout call
375 * from the callout structure.
376 */
27b91f59 377untimeout(fun, arg)
1fa9ff62
SL
378 int (*fun)();
379 caddr_t arg;
380{
1fa9ff62
SL
381 register struct callout *p1, *p2;
382 register int s;
383
384 s = spl7();
385 for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) {
386 if (p2->c_func == fun && p2->c_arg == arg) {
d01b68d6 387 if (p2->c_next && p2->c_time > 0)
1fa9ff62
SL
388 p2->c_next->c_time += p2->c_time;
389 p1->c_next = p2->c_next;
390 p2->c_next = callfree;
391 callfree = p2;
392 break;
393 }
394 }
395 splx(s);
396}
d01b68d6 397
76b2a182
BJ
398/*
399 * Compute number of hz until specified time.
400 * Used to compute third argument to timeout() from an
401 * absolute time.
402 */
d01b68d6
BJ
403hzto(tv)
404 struct timeval *tv;
405{
76b2a182
BJ
406 register long ticks;
407 register long sec;
d01b68d6
BJ
408 int s = spl7();
409
76b2a182
BJ
410 /*
411 * If number of milliseconds will fit in 32 bit arithmetic,
412 * then compute number of milliseconds to time and scale to
413 * ticks. Otherwise just compute number of hz in time, rounding
414 * times greater than representible to maximum value.
415 *
416 * Delta times less than 25 days can be computed ``exactly''.
417 * Maximum value for any timeout in 10ms ticks is 250 days.
418 */
419 sec = tv->tv_sec - time.tv_sec;
420 if (sec <= 0x7fffffff / 1000 - 1000)
421 ticks = ((tv->tv_sec - time.tv_sec) * 1000 +
422 (tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000);
423 else if (sec <= 0x7fffffff / hz)
424 ticks = sec * hz;
425 else
426 ticks = 0x7fffffff;
d01b68d6
BJ
427 splx(s);
428 return (ticks);
429}
88a7a62a
SL
430
431profil()
432{
433 register struct a {
434 short *bufbase;
435 unsigned bufsize;
436 unsigned pcoffset;
437 unsigned pcscale;
438 } *uap = (struct a *)u.u_ap;
439 register struct uprof *upp = &u.u_prof;
440
441 upp->pr_base = uap->bufbase;
442 upp->pr_size = uap->bufsize;
443 upp->pr_off = uap->pcoffset;
444 upp->pr_scale = uap->pcscale;
445}
446
447opause()
448{
449
450 for (;;)
451 sleep((caddr_t)&u, PSLEP);
452}