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