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