Commit | Line | Data |
---|---|---|
db1f1262 | 1 | /* kern_clock.c 4.56 83/06/14 */ |
961945a8 SL |
2 | |
3 | #include "../machine/reg.h" | |
4 | #include "../machine/psl.h" | |
83be5fac BJ |
5 | |
6 | #include "../h/param.h" | |
7 | #include "../h/systm.h" | |
d9b8447e | 8 | #include "../h/dk.h" |
0a34b6fd | 9 | #include "../h/callout.h" |
83be5fac BJ |
10 | #include "../h/dir.h" |
11 | #include "../h/user.h" | |
f0da6d20 | 12 | #include "../h/kernel.h" |
83be5fac | 13 | #include "../h/proc.h" |
83be5fac | 14 | #include "../h/vm.h" |
83be5fac BJ |
15 | #include "../h/text.h" |
16 | ||
961945a8 SL |
17 | #ifdef vax |
18 | #include "../vax/mtpr.h" | |
19 | #endif | |
20 | ||
8487304f KM |
21 | #ifdef GPROF |
22 | #include "../h/gprof.h" | |
23 | #endif | |
24 | ||
76b2a182 BJ |
25 | /* |
26 | * Clock handling routines. | |
27 | * | |
53a32545 SL |
28 | * This code is written to operate with two timers which run |
29 | * independently of each other. The main clock, running at hz | |
30 | * times per second, is used to do scheduling and timeout calculations. | |
31 | * The second timer does resource utilization estimation statistically | |
32 | * based on the state of the machine phz times a second. Both functions | |
33 | * can be performed by a single clock (ie hz == phz), however the | |
34 | * statistics will be much more prone to errors. Ideally a machine | |
35 | * would have separate clocks measuring time spent in user state, system | |
36 | * state, interrupt state, and idle state. These clocks would allow a non- | |
37 | * approximate measure of resource utilization. | |
76b2a182 | 38 | */ |
6602c75b | 39 | |
76b2a182 BJ |
40 | /* |
41 | * TODO: | |
88a7a62a SL |
42 | * time of day, system/user timing, timeouts, profiling on separate timers |
43 | * allocate more timeout table slots when table overflows. | |
76b2a182 | 44 | */ |
83be5fac | 45 | |
76b2a182 | 46 | /* |
53a32545 SL |
47 | * The hz hardware interval timer. |
48 | * We update the events relating to real time. | |
49 | * If this timer is also being used to gather statistics, | |
50 | * we run through the statistics gathering routine as well. | |
76b2a182 | 51 | */ |
260ea681 | 52 | /*ARGSUSED*/ |
b4e32d36 | 53 | #ifdef vax |
f403d99f | 54 | hardclock(pc, ps) |
4512b9a4 | 55 | caddr_t pc; |
460ab27f | 56 | int ps; |
83be5fac | 57 | { |
460ab27f | 58 | #endif |
b4e32d36 | 59 | #ifdef sun |
460ab27f BJ |
60 | hardclock(regs) |
61 | struct regs regs; | |
62 | { | |
88a7a62a SL |
63 | #define ps regs.r_sr |
64 | #define pc (caddr_t)regs.r_pc | |
460ab27f | 65 | #endif |
0a34b6fd | 66 | register struct callout *p1; |
27b91f59 | 67 | register struct proc *p; |
f403d99f | 68 | register int s, cpstate; |
88a7a62a | 69 | int needsoft = 0; |
83be5fac | 70 | |
76b2a182 BJ |
71 | /* |
72 | * Update real-time timeout queue. | |
73 | * At front of queue are some number of events which are ``due''. | |
74 | * The time to these is <= 0 and if negative represents the | |
75 | * number of ticks which have passed since it was supposed to happen. | |
76 | * The rest of the q elements (times > 0) are events yet to happen, | |
77 | * where the time for each is given as a delta from the previous. | |
78 | * Decrementing just the first of these serves to decrement the time | |
79 | * to all events. | |
80 | */ | |
88a7a62a SL |
81 | p1 = calltodo.c_next; |
82 | while (p1) { | |
83 | if (--p1->c_time > 0) | |
84 | break; | |
85 | needsoft = 1; | |
86 | if (p1->c_time == 0) | |
87 | break; | |
88 | p1 = p1->c_next; | |
89 | } | |
5da67d35 | 90 | |
76b2a182 BJ |
91 | /* |
92 | * Charge the time out based on the mode the cpu is in. | |
93 | * Here again we fudge for the lack of proper interval timers | |
94 | * assuming that the current state has been around at least | |
95 | * one tick. | |
96 | */ | |
83be5fac | 97 | if (USERMODE(ps)) { |
88a7a62a SL |
98 | #ifdef sun |
99 | u.u_ar0 = ®s.r_r0; /* aston needs ar0 */ | |
100 | #endif | |
101 | if (u.u_prof.pr_scale) | |
102 | needsoft = 1; | |
76b2a182 BJ |
103 | /* |
104 | * CPU was in user state. Increment | |
105 | * user time counter, and process process-virtual time | |
877ef342 | 106 | * interval timer. |
76b2a182 BJ |
107 | */ |
108 | bumptime(&u.u_ru.ru_utime, tick); | |
27b91f59 BJ |
109 | if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) && |
110 | itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0) | |
111 | psignal(u.u_procp, SIGVTALRM); | |
f0da6d20 | 112 | if (u.u_procp->p_nice > NZERO) |
41888f16 BJ |
113 | cpstate = CP_NICE; |
114 | else | |
115 | cpstate = CP_USER; | |
83be5fac | 116 | } else { |
76b2a182 BJ |
117 | /* |
118 | * CPU was in system state. If profiling kernel | |
119 | * increment a counter. If no process is running | |
120 | * then this is a system tick if we were running | |
121 | * at a non-zero IPL (in a driver). If a process is running, | |
122 | * then we charge it with system time even if we were | |
123 | * at a non-zero IPL, since the system often runs | |
124 | * this way during processing of system calls. | |
125 | * This is approximate, but the lack of true interval | |
126 | * timers makes doing anything else difficult. | |
127 | */ | |
41888f16 | 128 | cpstate = CP_SYS; |
ddb3ced5 | 129 | if (noproc) { |
460ab27f | 130 | if (BASEPRI(ps)) |
ddb3ced5 | 131 | cpstate = CP_IDLE; |
f0da6d20 | 132 | } else { |
76b2a182 | 133 | bumptime(&u.u_ru.ru_stime, tick); |
f0da6d20 | 134 | } |
83be5fac | 135 | } |
27b91f59 | 136 | |
9fb1a8d0 SL |
137 | /* |
138 | * If the cpu is currently scheduled to a process, then | |
139 | * charge it with resource utilization for a tick, updating | |
140 | * statistics which run in (user+system) virtual time, | |
141 | * such as the cpu time limit and profiling timers. | |
142 | * This assumes that the current process has been running | |
143 | * the entire last tick. | |
144 | */ | |
145 | if (noproc == 0 && cpstate != CP_IDLE) { | |
146 | if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) > | |
147 | u.u_rlimit[RLIMIT_CPU].rlim_cur) { | |
148 | psignal(u.u_procp, SIGXCPU); | |
149 | if (u.u_rlimit[RLIMIT_CPU].rlim_cur < | |
150 | u.u_rlimit[RLIMIT_CPU].rlim_max) | |
151 | u.u_rlimit[RLIMIT_CPU].rlim_cur += 5; | |
152 | } | |
153 | if (timerisset(&u.u_timer[ITIMER_PROF].it_value) && | |
154 | itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0) | |
155 | psignal(u.u_procp, SIGPROF); | |
156 | s = u.u_procp->p_rssize; | |
157 | u.u_ru.ru_idrss += s; u.u_ru.ru_isrss += 0; /* XXX */ | |
158 | if (u.u_procp->p_textp) { | |
159 | register int xrss = u.u_procp->p_textp->x_rssize; | |
160 | ||
161 | s += xrss; | |
162 | u.u_ru.ru_ixrss += xrss; | |
163 | } | |
164 | if (s > u.u_ru.ru_maxrss) | |
165 | u.u_ru.ru_maxrss = s; | |
166 | } | |
167 | ||
76b2a182 BJ |
168 | /* |
169 | * We adjust the priority of the current process. | |
170 | * The priority of a process gets worse as it accumulates | |
171 | * CPU time. The cpu usage estimator (p_cpu) is increased here | |
172 | * and the formula for computing priorities (in kern_synch.c) | |
173 | * will compute a different value each time the p_cpu increases | |
174 | * by 4. The cpu usage estimator ramps up quite quickly when | |
175 | * the process is running (linearly), and decays away exponentially, | |
176 | * at a rate which is proportionally slower when the system is | |
177 | * busy. The basic principal is that the system will 90% forget | |
178 | * that a process used a lot of CPU time in 5*loadav seconds. | |
179 | * This causes the system to favor processes which haven't run | |
180 | * much recently, and to round-robin among other processes. | |
181 | */ | |
83be5fac | 182 | if (!noproc) { |
27b91f59 BJ |
183 | p = u.u_procp; |
184 | p->p_cpticks++; | |
185 | if (++p->p_cpu == 0) | |
186 | p->p_cpu--; | |
76b2a182 | 187 | if ((p->p_cpu&3) == 0) { |
27b91f59 BJ |
188 | (void) setpri(p); |
189 | if (p->p_pri >= PUSER) | |
190 | p->p_pri = p->p_usrpri; | |
83be5fac BJ |
191 | } |
192 | } | |
76b2a182 | 193 | |
53a32545 SL |
194 | /* |
195 | * If the alternate clock has not made itself known then | |
196 | * we must gather the statistics. | |
197 | */ | |
198 | if (phz == 0) | |
199 | gatherstats(pc, ps); | |
53a32545 | 200 | |
76b2a182 BJ |
201 | /* |
202 | * Increment the time-of-day, and schedule | |
203 | * processing of the callouts at a very low cpu priority, | |
204 | * so we don't keep the relatively high clock interrupt | |
205 | * priority any longer than necessary. | |
206 | */ | |
207 | bumptime(&time, tick); | |
88a7a62a SL |
208 | if (needsoft) |
209 | setsoftclock(); | |
f403d99f | 210 | } |
88a7a62a SL |
211 | #ifdef sun |
212 | #undef pc | |
213 | #undef ps | |
214 | #endif | |
f403d99f | 215 | |
53a32545 SL |
216 | /* |
217 | * Gather statistics on resource utilization. | |
218 | * | |
219 | * We make a gross assumption: that the system has been in the | |
220 | * state it is in (user state, kernel state, interrupt state, | |
221 | * or idle state) for the entire last time interval, and | |
222 | * update statistics accordingly. | |
223 | */ | |
88a7a62a | 224 | /*ARGSUSED*/ |
53a32545 SL |
225 | gatherstats(pc, ps) |
226 | caddr_t pc; | |
227 | int ps; | |
228 | { | |
229 | int cpstate, s; | |
230 | ||
231 | /* | |
232 | * Determine what state the cpu is in. | |
233 | */ | |
234 | if (USERMODE(ps)) { | |
235 | /* | |
236 | * CPU was in user state. | |
237 | */ | |
238 | if (u.u_procp->p_nice > NZERO) | |
239 | cpstate = CP_NICE; | |
240 | else | |
241 | cpstate = CP_USER; | |
242 | } else { | |
243 | /* | |
244 | * CPU was in system state. If profiling kernel | |
245 | * increment a counter. | |
246 | */ | |
247 | cpstate = CP_SYS; | |
248 | if (noproc && BASEPRI(ps)) | |
249 | cpstate = CP_IDLE; | |
250 | #ifdef GPROF | |
251 | s = pc - s_lowpc; | |
252 | if (profiling < 2 && s < s_textsize) | |
253 | kcount[s / (HISTFRACTION * sizeof (*kcount))]++; | |
254 | #endif | |
255 | } | |
256 | /* | |
257 | * We maintain statistics shown by user-level statistics | |
258 | * programs: the amount of time in each cpu state, and | |
259 | * the amount of time each of DK_NDRIVE ``drives'' is busy. | |
260 | */ | |
261 | cp_time[cpstate]++; | |
262 | for (s = 0; s < DK_NDRIVE; s++) | |
263 | if (dk_busy&(1<<s)) | |
264 | dk_time[s]++; | |
265 | } | |
266 | ||
76b2a182 BJ |
267 | /* |
268 | * Software priority level clock interrupt. | |
269 | * Run periodic events from timeout queue. | |
270 | */ | |
260ea681 | 271 | /*ARGSUSED*/ |
b4e32d36 | 272 | #ifdef vax |
f403d99f | 273 | softclock(pc, ps) |
4512b9a4 | 274 | caddr_t pc; |
460ab27f | 275 | int ps; |
f403d99f | 276 | { |
460ab27f | 277 | #endif |
b4e32d36 | 278 | #ifdef sun |
961945a8 | 279 | softclock() |
460ab27f | 280 | { |
88a7a62a SL |
281 | #define pc (caddr_t)u.u_ar0[PC] |
282 | #define ps u.u_ar0[PS] | |
460ab27f | 283 | #endif |
f403d99f | 284 | |
27b91f59 | 285 | for (;;) { |
76b2a182 BJ |
286 | register struct callout *p1; |
287 | register caddr_t arg; | |
288 | register int (*func)(); | |
289 | register int a, s; | |
290 | ||
27b91f59 BJ |
291 | s = spl7(); |
292 | if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { | |
293 | splx(s); | |
294 | break; | |
f403d99f | 295 | } |
76b2a182 | 296 | arg = p1->c_arg; func = p1->c_func; a = p1->c_time; |
27b91f59 | 297 | calltodo.c_next = p1->c_next; |
27b91f59 BJ |
298 | p1->c_next = callfree; |
299 | callfree = p1; | |
4f083fd7 | 300 | splx(s); |
d01b68d6 | 301 | (*func)(arg, a); |
f403d99f | 302 | } |
877ef342 | 303 | /* |
db1f1262 SL |
304 | * If trapped user-mode and profiling, give it |
305 | * a profiling tick. | |
877ef342 | 306 | */ |
db1f1262 SL |
307 | if (USERMODE(ps)) { |
308 | register struct proc *p = u.u_procp; | |
309 | ||
310 | if (u.u_prof.pr_scale) { | |
311 | p->p_flag |= SOWEUPC; | |
312 | aston(); | |
313 | } | |
314 | #ifdef vax | |
315 | /* | |
316 | * Check to see if process has accumulated | |
317 | * more than 10 minutes of user time. If so | |
318 | * reduce priority to give others a chance. | |
319 | */ | |
320 | if (p->p_uid && p->p_nice == NZERO && | |
321 | u.u_ru.ru_utime.tv_sec > 10 * 60) { | |
322 | p->p_nice = NZERO+4; | |
323 | (void) setpri(p); | |
324 | p->p_pri = p->p_usrpri; | |
325 | } | |
326 | #endif | |
877ef342 | 327 | } |
83be5fac BJ |
328 | } |
329 | ||
330 | /* | |
88a7a62a SL |
331 | * Bump a timeval by a small number of usec's. |
332 | */ | |
333 | bumptime(tp, usec) | |
334 | register struct timeval *tp; | |
335 | int usec; | |
336 | { | |
337 | ||
338 | tp->tv_usec += usec; | |
339 | if (tp->tv_usec >= 1000000) { | |
340 | tp->tv_usec -= 1000000; | |
341 | tp->tv_sec++; | |
342 | } | |
343 | } | |
344 | ||
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; |
88a7a62a | 354 | register int s = spl7(); |
83be5fac | 355 | |
88a7a62a SL |
356 | if (t == 0) |
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 | ||
386 | s = spl7(); | |
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; | |
d01b68d6 BJ |
410 | int s = spl7(); |
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 SL |
432 | |
433 | profil() | |
434 | { | |
435 | register struct a { | |
436 | short *bufbase; | |
437 | unsigned bufsize; | |
438 | unsigned pcoffset; | |
439 | unsigned pcscale; | |
440 | } *uap = (struct a *)u.u_ap; | |
441 | register struct uprof *upp = &u.u_prof; | |
442 | ||
443 | upp->pr_base = uap->bufbase; | |
444 | upp->pr_size = uap->bufsize; | |
445 | upp->pr_off = uap->pcoffset; | |
446 | upp->pr_scale = uap->pcscale; | |
447 | } | |
448 | ||
449 | opause() | |
450 | { | |
451 | ||
452 | for (;;) | |
453 | sleep((caddr_t)&u, PSLEP); | |
454 | } |