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 | * | |
2100bbbc | 6 | * @(#)kern_clock.c 7.5 (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 | |
33 | int 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 | 76 | hardclock(pc, ps) |
4512b9a4 | 77 | caddr_t pc; |
460ab27f | 78 | int ps; |
83be5fac | 79 | { |
0a34b6fd | 80 | register struct callout *p1; |
27b91f59 | 81 | register struct proc *p; |
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 | */ |
ad8023d1 | 115 | BUMPTIME(&u.u_ru.ru_utime, tick); |
27b91f59 BJ |
116 | if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) && |
117 | itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0) | |
118 | psignal(u.u_procp, SIGVTALRM); | |
83be5fac | 119 | } else { |
76b2a182 | 120 | /* |
0b355a6e | 121 | * CPU was in system state. |
76b2a182 | 122 | */ |
9c5cfb8b | 123 | if (!noproc) |
ad8023d1 | 124 | BUMPTIME(&u.u_ru.ru_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) { |
9fb1a8d0 SL |
136 | if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) > |
137 | u.u_rlimit[RLIMIT_CPU].rlim_cur) { | |
138 | psignal(u.u_procp, SIGXCPU); | |
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) | |
145 | psignal(u.u_procp, SIGPROF); | |
146 | s = u.u_procp->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 | |
9fb1a8d0 SL |
151 | if (u.u_procp->p_textp) { |
152 | register int xrss = u.u_procp->p_textp->x_rssize; | |
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 = u.u_procp; |
177 | p->p_cpticks++; | |
178 | if (++p->p_cpu == 0) | |
179 | p->p_cpu--; | |
76b2a182 | 180 | if ((p->p_cpu&3) == 0) { |
27b91f59 BJ |
181 | (void) setpri(p); |
182 | if (p->p_pri >= PUSER) | |
183 | p->p_pri = p->p_usrpri; | |
83be5fac BJ |
184 | } |
185 | } | |
76b2a182 | 186 | |
53a32545 SL |
187 | /* |
188 | * If the alternate clock has not made itself known then | |
189 | * we must gather the statistics. | |
190 | */ | |
191 | if (phz == 0) | |
192 | gatherstats(pc, ps); | |
53a32545 | 193 | |
76b2a182 BJ |
194 | /* |
195 | * Increment the time-of-day, and schedule | |
196 | * processing of the callouts at a very low cpu priority, | |
197 | * so we don't keep the relatively high clock interrupt | |
198 | * priority any longer than necessary. | |
199 | */ | |
45e9acec MK |
200 | #ifdef ADJTIME |
201 | if (adjtimedelta == 0) | |
202 | bumptime(&time, tick); | |
203 | else { | |
204 | if (adjtimedelta < 0) { | |
205 | bumptime(&time, tick-ADJ_TICK); | |
206 | adjtimedelta++; | |
207 | } else { | |
208 | bumptime(&time, tick+ADJ_TICK); | |
209 | adjtimedelta--; | |
210 | } | |
211 | } | |
212 | #else | |
4ca0d0d6 | 213 | if (timedelta == 0) |
99e47f6b MK |
214 | BUMPTIME(&time, tick) |
215 | else { | |
216 | register delta; | |
217 | ||
4ca0d0d6 MK |
218 | if (timedelta < 0) { |
219 | delta = tick - tickdelta; | |
220 | timedelta += tickdelta; | |
99e47f6b | 221 | } else { |
4ca0d0d6 MK |
222 | delta = tick + tickdelta; |
223 | timedelta -= tickdelta; | |
99e47f6b MK |
224 | } |
225 | BUMPTIME(&time, delta); | |
226 | } | |
45e9acec | 227 | #endif |
ca6b57a4 | 228 | setsoftclock(); |
f403d99f BJ |
229 | } |
230 | ||
d976d466 | 231 | int dk_ndrive = DK_NDRIVE; |
53a32545 SL |
232 | /* |
233 | * Gather statistics on resource utilization. | |
234 | * | |
235 | * We make a gross assumption: that the system has been in the | |
236 | * state it is in (user state, kernel state, interrupt state, | |
237 | * or idle state) for the entire last time interval, and | |
238 | * update statistics accordingly. | |
239 | */ | |
88a7a62a | 240 | /*ARGSUSED*/ |
53a32545 SL |
241 | gatherstats(pc, ps) |
242 | caddr_t pc; | |
243 | int ps; | |
244 | { | |
9c5cfb8b | 245 | register int cpstate, s; |
53a32545 SL |
246 | |
247 | /* | |
248 | * Determine what state the cpu is in. | |
249 | */ | |
250 | if (USERMODE(ps)) { | |
251 | /* | |
252 | * CPU was in user state. | |
253 | */ | |
254 | if (u.u_procp->p_nice > NZERO) | |
255 | cpstate = CP_NICE; | |
256 | else | |
257 | cpstate = CP_USER; | |
258 | } else { | |
259 | /* | |
260 | * CPU was in system state. If profiling kernel | |
0b355a6e JB |
261 | * increment a counter. If no process is running |
262 | * then this is a system tick if we were running | |
263 | * at a non-zero IPL (in a driver). If a process is running, | |
264 | * then we charge it with system time even if we were | |
265 | * at a non-zero IPL, since the system often runs | |
266 | * this way during processing of system calls. | |
267 | * This is approximate, but the lack of true interval | |
268 | * timers makes doing anything else difficult. | |
53a32545 SL |
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++) | |
fb1db32c | 286 | if (dk_busy&(1<<s)) |
53a32545 SL |
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 | 295 | softclock(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 | ||
9c5cfb8b | 306 | s = splhigh(); |
27b91f59 BJ |
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 | 346 | timeout(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; |
9c5cfb8b | 352 | register int s = splhigh(); |
83be5fac | 353 | |
ba96129b | 354 | if (t <= 0) |
88a7a62a | 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 | 377 | untimeout(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 | ||
9c5cfb8b | 384 | s = splhigh(); |
1fa9ff62 SL |
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 |
403 | hzto(tv) |
404 | struct timeval *tv; | |
405 | { | |
76b2a182 BJ |
406 | register long ticks; |
407 | register long sec; | |
9c5cfb8b | 408 | int s = splhigh(); |
d01b68d6 | 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 | |
431 | profil() | |
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 | } |