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