Commit | Line | Data |
---|---|---|
1fa9ff62 | 1 | /* kern_clock.c 4.31 82/06/26 */ |
83be5fac BJ |
2 | |
3 | #include "../h/param.h" | |
4 | #include "../h/systm.h" | |
d9b8447e | 5 | #include "../h/dk.h" |
0a34b6fd | 6 | #include "../h/callout.h" |
83be5fac BJ |
7 | #include "../h/seg.h" |
8 | #include "../h/dir.h" | |
9 | #include "../h/user.h" | |
10 | #include "../h/proc.h" | |
11 | #include "../h/reg.h" | |
12 | #include "../h/psl.h" | |
13 | #include "../h/vm.h" | |
14 | #include "../h/buf.h" | |
15 | #include "../h/text.h" | |
95ce0d37 BJ |
16 | #include "../h/vlimit.h" |
17 | #include "../h/mtpr.h" | |
18 | #include "../h/clock.h" | |
e5a79c70 | 19 | #include "../h/cpu.h" |
72857acf | 20 | #include "../h/protosw.h" |
83be5fac | 21 | |
738a68d6 | 22 | #include "bk.h" |
ec213dfb BJ |
23 | #include "dh.h" |
24 | #include "dz.h" | |
1fa9ff62 | 25 | #include "ps.h" |
6602c75b | 26 | |
83be5fac | 27 | /* |
f403d99f | 28 | * Hardclock is called straight from |
83be5fac | 29 | * the real time clock interrupt. |
f403d99f BJ |
30 | * We limit the work we do at real clock interrupt time to: |
31 | * reloading clock | |
32 | * decrementing time to callouts | |
33 | * recording cpu time usage | |
4512b9a4 | 34 | * modifying priority of current process |
f403d99f BJ |
35 | * arrange for soft clock interrupt |
36 | * kernel pc profiling | |
83be5fac | 37 | * |
964bcfb1 | 38 | * At software (softclock) interrupt time we: |
83be5fac | 39 | * implement callouts |
83be5fac | 40 | * maintain date |
83be5fac BJ |
41 | * lightning bolt wakeup (every second) |
42 | * alarm clock signals | |
43 | * jab the scheduler | |
f403d99f BJ |
44 | * |
45 | * On the vax softclock interrupts are implemented by | |
46 | * software interrupts. Note that we may have multiple softclock | |
47 | * interrupts compressed into one (due to excessive interrupt load), | |
48 | * but that hardclock interrupts should never be lost. | |
83be5fac | 49 | */ |
2752c877 | 50 | #ifdef KPROF |
296d05b2 | 51 | int kcounts[20000]; |
2752c877 | 52 | #endif |
83be5fac | 53 | |
72857acf BJ |
54 | /* |
55 | * Protoslow is like lbolt, but for slow protocol timeouts, counting | |
56 | * up to (hz/PR_SLOWHZ), then causing a pfslowtimo(). | |
57 | * Protofast is like lbolt, but for fast protocol timeouts, counting | |
58 | * up to (hz/PR_FASTHZ), then causing a pffasttimo(). | |
59 | */ | |
60 | int protoslow; | |
61 | int protofast; | |
62 | ||
260ea681 | 63 | /*ARGSUSED*/ |
f403d99f | 64 | hardclock(pc, ps) |
4512b9a4 | 65 | caddr_t pc; |
83be5fac | 66 | { |
0a34b6fd | 67 | register struct callout *p1; |
83be5fac | 68 | register struct proc *pp; |
f403d99f | 69 | register int s, cpstate; |
83be5fac BJ |
70 | |
71 | /* | |
72 | * reprime clock | |
73 | */ | |
74 | clkreld(); | |
75 | ||
1fa9ff62 SL |
76 | #if NPS > 0 |
77 | /* | |
78 | * sync referesh of picture system | |
79 | */ | |
80 | psextsync(pc, ps); | |
81 | #endif | |
82 | ||
83be5fac | 83 | /* |
f403d99f | 84 | * update callout times |
83be5fac | 85 | */ |
c4710996 BJ |
86 | for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next) |
87 | ; | |
88 | if (p1) | |
89 | p1->c_time--; | |
5da67d35 BJ |
90 | |
91 | /* | |
f403d99f | 92 | * Maintain iostat and per-process cpu statistics |
5da67d35 | 93 | */ |
83be5fac BJ |
94 | if (!noproc) { |
95 | s = u.u_procp->p_rssize; | |
96 | u.u_vm.vm_idsrss += s; | |
97 | if (u.u_procp->p_textp) { | |
98 | register int xrss = u.u_procp->p_textp->x_rssize; | |
99 | ||
100 | s += xrss; | |
101 | u.u_vm.vm_ixrss += xrss; | |
102 | } | |
103 | if (s > u.u_vm.vm_maxrss) | |
104 | u.u_vm.vm_maxrss = s; | |
0a34b6fd | 105 | if ((u.u_vm.vm_utime+u.u_vm.vm_stime+1)/hz > u.u_limit[LIM_CPU]) { |
39f2f769 BJ |
106 | psignal(u.u_procp, SIGXCPU); |
107 | if (u.u_limit[LIM_CPU] < INFINITY - 5) | |
108 | u.u_limit[LIM_CPU] += 5; | |
109 | } | |
83be5fac | 110 | } |
964bcfb1 BJ |
111 | /* |
112 | * Update iostat information. | |
113 | */ | |
83be5fac BJ |
114 | if (USERMODE(ps)) { |
115 | u.u_vm.vm_utime++; | |
116 | if(u.u_procp->p_nice > NZERO) | |
41888f16 BJ |
117 | cpstate = CP_NICE; |
118 | else | |
119 | cpstate = CP_USER; | |
83be5fac | 120 | } else { |
2752c877 BJ |
121 | #ifdef KPROF |
122 | int k = ((int)pc & 0x7fffffff) / 8; | |
123 | if (k < 20000) | |
124 | kcounts[k]++; | |
125 | #endif | |
41888f16 | 126 | cpstate = CP_SYS; |
83be5fac | 127 | if (noproc) |
41888f16 | 128 | cpstate = CP_IDLE; |
83be5fac BJ |
129 | else |
130 | u.u_vm.vm_stime++; | |
131 | } | |
2d7d59e9 | 132 | cp_time[cpstate]++; |
f403d99f BJ |
133 | for (s = 0; s < DK_NDRIVE; s++) |
134 | if (dk_busy&(1<<s)) | |
135 | dk_time[s]++; | |
964bcfb1 BJ |
136 | /* |
137 | * Adjust priority of current process. | |
138 | */ | |
83be5fac BJ |
139 | if (!noproc) { |
140 | pp = u.u_procp; | |
dd808ba3 | 141 | pp->p_cpticks++; |
83be5fac BJ |
142 | if(++pp->p_cpu == 0) |
143 | pp->p_cpu--; | |
16a64baa | 144 | if(pp->p_cpu % 4 == 0) { |
81263dba | 145 | (void) setpri(pp); |
83be5fac BJ |
146 | if (pp->p_pri >= PUSER) |
147 | pp->p_pri = pp->p_usrpri; | |
148 | } | |
149 | } | |
964bcfb1 BJ |
150 | /* |
151 | * Time moves on. | |
152 | */ | |
83be5fac | 153 | ++lbolt; |
72857acf BJ |
154 | |
155 | /* | |
156 | * Time moves on for protocols. | |
157 | */ | |
20bbf2f5 | 158 | --protoslow; --protofast; |
72857acf | 159 | |
e5a79c70 | 160 | #if VAX780 |
964bcfb1 BJ |
161 | /* |
162 | * On 780's, impelement a fast UBA watcher, | |
163 | * to make sure uba's don't get stuck. | |
164 | */ | |
287d9996 | 165 | if (cpu == VAX_780 && panicstr == 0 && !BASEPRI(ps)) |
f403d99f BJ |
166 | unhang(); |
167 | #endif | |
964bcfb1 BJ |
168 | /* |
169 | * Schedule a software interrupt for the rest | |
170 | * of clock activities. | |
171 | */ | |
f403d99f BJ |
172 | setsoftclock(); |
173 | } | |
174 | ||
175 | /* | |
16a64baa BJ |
176 | * The digital decay cpu usage priority assignment is scaled to run in |
177 | * time as expanded by the 1 minute load average. Each second we | |
178 | * multiply the the previous cpu usage estimate by | |
179 | * nrscale*avenrun[0] | |
180 | * The following relates the load average to the period over which | |
181 | * cpu usage is 90% forgotten: | |
182 | * loadav 1 5 seconds | |
183 | * loadav 5 24 seconds | |
184 | * loadav 10 47 seconds | |
185 | * loadav 20 93 seconds | |
186 | * This is a great improvement on the previous algorithm which | |
187 | * decayed the priorities by a constant, and decayed away all knowledge | |
188 | * of previous activity in about 20 seconds. Under heavy load, | |
189 | * the previous algorithm degenerated to round-robin with poor response | |
190 | * time when there was a high load average. | |
964bcfb1 | 191 | */ |
b620b354 | 192 | #undef ave |
16a64baa BJ |
193 | #define ave(a,b) ((int)(((int)(a*b))/(b+1))) |
194 | int nrscale = 2; | |
195 | double avenrun[]; | |
964bcfb1 BJ |
196 | |
197 | /* | |
198 | * Constant for decay filter for cpu usage field | |
199 | * in process table (used by ps au). | |
f403d99f BJ |
200 | */ |
201 | double ccpu = 0.95122942450071400909; /* exp(-1/20) */ | |
202 | ||
203 | /* | |
204 | * Software clock interrupt. | |
964bcfb1 | 205 | * This routine runs at lower priority than device interrupts. |
f403d99f | 206 | */ |
260ea681 | 207 | /*ARGSUSED*/ |
f403d99f | 208 | softclock(pc, ps) |
4512b9a4 | 209 | caddr_t pc; |
f403d99f | 210 | { |
dee48a1b | 211 | register struct callout *p1; |
f403d99f BJ |
212 | register struct proc *pp; |
213 | register int a, s; | |
c4710996 BJ |
214 | caddr_t arg; |
215 | int (*func)(); | |
f403d99f BJ |
216 | |
217 | /* | |
287d9996 | 218 | * Perform callouts (but not after panic's!) |
f403d99f | 219 | */ |
c4710996 BJ |
220 | if (panicstr == 0) { |
221 | for (;;) { | |
222 | s = spl7(); | |
849fc3ee BJ |
223 | if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) { |
224 | splx(s); | |
c4710996 | 225 | break; |
849fc3ee | 226 | } |
c4710996 BJ |
227 | calltodo.c_next = p1->c_next; |
228 | arg = p1->c_arg; | |
229 | func = p1->c_func; | |
230 | p1->c_next = callfree; | |
231 | callfree = p1; | |
232 | (void) splx(s); | |
233 | (*func)(arg); | |
f403d99f BJ |
234 | } |
235 | } | |
236 | ||
237 | /* | |
238 | * Drain silos. | |
239 | */ | |
3b90686d | 240 | #if NDH > 0 |
f403d99f BJ |
241 | s = spl5(); dhtimer(); splx(s); |
242 | #endif | |
3b90686d | 243 | #if NDZ > 0 |
f403d99f BJ |
244 | s = spl5(); dztimer(); splx(s); |
245 | #endif | |
246 | ||
4512b9a4 BJ |
247 | /* |
248 | * If idling and processes are waiting to swap in, | |
249 | * check on them. | |
250 | */ | |
251 | if (noproc && runin) { | |
252 | runin = 0; | |
253 | wakeup((caddr_t)&runin); | |
254 | } | |
255 | ||
f403d99f | 256 | /* |
16a64baa | 257 | * Run paging daemon every 1/4 sec. |
f403d99f | 258 | */ |
0a34b6fd | 259 | if (lbolt % (hz/4) == 0) { |
83be5fac | 260 | vmpago(); |
16a64baa BJ |
261 | } |
262 | ||
263 | /* | |
264 | * Reschedule every 1/10 sec. | |
265 | */ | |
266 | if (lbolt % (hz/10) == 0) { | |
83be5fac | 267 | runrun++; |
f403d99f | 268 | aston(); |
83be5fac | 269 | } |
f403d99f | 270 | |
72857acf BJ |
271 | /* |
272 | * Run network slow and fast timeouts. | |
273 | */ | |
20bbf2f5 BJ |
274 | if (protofast <= 0) { |
275 | protofast = hz / PR_FASTHZ; | |
72857acf | 276 | pffasttimo(); |
20bbf2f5 BJ |
277 | } |
278 | if (protoslow <= 0) { | |
279 | protoslow = hz / PR_SLOWHZ; | |
72857acf | 280 | pfslowtimo(); |
20bbf2f5 | 281 | } |
72857acf | 282 | |
f403d99f BJ |
283 | /* |
284 | * Lightning bolt every second: | |
285 | * sleep timeouts | |
286 | * process priority recomputation | |
287 | * process %cpu averaging | |
288 | * virtual memory metering | |
289 | * kick swapper if processes want in | |
290 | */ | |
0a34b6fd | 291 | if (lbolt >= hz) { |
287d9996 | 292 | /* |
964bcfb1 | 293 | * This doesn't mean much on VAX since we run at |
287d9996 BJ |
294 | * software interrupt time... if hardclock() |
295 | * calls softclock() directly, it prevents | |
296 | * this code from running when the priority | |
297 | * was raised when the clock interrupt occurred. | |
298 | */ | |
83be5fac BJ |
299 | if (BASEPRI(ps)) |
300 | return; | |
287d9996 BJ |
301 | |
302 | /* | |
303 | * If we didn't run a few times because of | |
304 | * long blockage at high ipl, we don't | |
305 | * really want to run this code several times, | |
306 | * so squish out all multiples of hz here. | |
307 | */ | |
308 | time += lbolt / hz; | |
309 | lbolt %= hz; | |
310 | ||
311 | /* | |
312 | * Wakeup lightning bolt sleepers. | |
313 | * Processes sleep on lbolt to wait | |
314 | * for short amounts of time (e.g. 1 second). | |
315 | */ | |
83be5fac | 316 | wakeup((caddr_t)&lbolt); |
287d9996 BJ |
317 | |
318 | /* | |
319 | * Recompute process priority and process | |
320 | * sleep() system calls as well as internal | |
321 | * sleeps with timeouts (tsleep() kernel routine). | |
322 | */ | |
323 | for (pp = proc; pp < procNPROC; pp++) | |
8418f526 | 324 | if (pp->p_stat && pp->p_stat!=SZOMB) { |
287d9996 BJ |
325 | /* |
326 | * Increase resident time, to max of 127 seconds | |
327 | * (it is kept in a character.) For | |
328 | * loaded processes this is time in core; for | |
329 | * swapped processes, this is time on drum. | |
330 | */ | |
331 | if (pp->p_time != 127) | |
83be5fac | 332 | pp->p_time++; |
287d9996 BJ |
333 | /* |
334 | * If process has clock counting down, and it | |
335 | * expires, set it running (if this is a tsleep()), | |
336 | * or give it an SIGALRM (if the user process | |
337 | * is using alarm signals. | |
338 | */ | |
339 | if (pp->p_clktim && --pp->p_clktim == 0) | |
340 | if (pp->p_flag & STIMO) { | |
341 | s = spl6(); | |
342 | switch (pp->p_stat) { | |
daac5944 | 343 | |
287d9996 BJ |
344 | case SSLEEP: |
345 | setrun(pp); | |
346 | break; | |
daac5944 | 347 | |
287d9996 BJ |
348 | case SSTOP: |
349 | unsleep(pp); | |
350 | break; | |
351 | } | |
352 | pp->p_flag &= ~STIMO; | |
353 | splx(s); | |
354 | } else | |
355 | psignal(pp, SIGALRM); | |
356 | /* | |
357 | * If process is blocked, increment computed | |
358 | * time blocked. This is used in swap scheduling. | |
359 | */ | |
360 | if (pp->p_stat==SSLEEP || pp->p_stat==SSTOP) | |
83be5fac BJ |
361 | if (pp->p_slptime != 127) |
362 | pp->p_slptime++; | |
287d9996 BJ |
363 | /* |
364 | * Update digital filter estimation of process | |
365 | * cpu utilization for loaded processes. | |
366 | */ | |
dd808ba3 BJ |
367 | if (pp->p_flag&SLOAD) |
368 | pp->p_pctcpu = ccpu * pp->p_pctcpu + | |
0a34b6fd | 369 | (1.0 - ccpu) * (pp->p_cpticks/(float)hz); |
287d9996 BJ |
370 | /* |
371 | * Recompute process priority. The number p_cpu | |
372 | * is a weighted estimate of cpu time consumed. | |
373 | * A process which consumes cpu time has this | |
374 | * increase regularly. We here decrease it by | |
16a64baa BJ |
375 | * a fraction based on load average giving a digital |
376 | * decay filter which damps out in about 5 seconds | |
377 | * when seconds are measured in time expanded by the | |
378 | * load average. | |
287d9996 BJ |
379 | * |
380 | * If a process is niced, then the nice directly | |
381 | * affects the new priority. The final priority | |
382 | * is in the range 0 to 255, to fit in a character. | |
383 | */ | |
dd808ba3 | 384 | pp->p_cpticks = 0; |
16a64baa BJ |
385 | a = ave((pp->p_cpu & 0377), avenrun[0]*nrscale) + |
386 | pp->p_nice - NZERO; | |
287d9996 | 387 | if (a < 0) |
83be5fac | 388 | a = 0; |
287d9996 | 389 | if (a > 255) |
83be5fac BJ |
390 | a = 255; |
391 | pp->p_cpu = a; | |
81263dba | 392 | (void) setpri(pp); |
287d9996 BJ |
393 | /* |
394 | * Now have computed new process priority | |
395 | * in p->p_usrpri. Carefully change p->p_pri. | |
396 | * A process is on a run queue associated with | |
397 | * this priority, so we must block out process | |
398 | * state changes during the transition. | |
399 | */ | |
83be5fac | 400 | s = spl6(); |
287d9996 | 401 | if (pp->p_pri >= PUSER) { |
83be5fac BJ |
402 | if ((pp != u.u_procp || noproc) && |
403 | pp->p_stat == SRUN && | |
404 | (pp->p_flag & SLOAD) && | |
405 | pp->p_pri != pp->p_usrpri) { | |
406 | remrq(pp); | |
407 | pp->p_pri = pp->p_usrpri; | |
408 | setrq(pp); | |
409 | } else | |
410 | pp->p_pri = pp->p_usrpri; | |
411 | } | |
412 | splx(s); | |
413 | } | |
287d9996 BJ |
414 | |
415 | /* | |
416 | * Perform virtual memory metering. | |
417 | */ | |
83be5fac | 418 | vmmeter(); |
287d9996 BJ |
419 | |
420 | /* | |
421 | * If the swap process is trying to bring | |
422 | * a process in, have it look again to see | |
423 | * if it is possible now. | |
424 | */ | |
425 | if (runin!=0) { | |
83be5fac BJ |
426 | runin = 0; |
427 | wakeup((caddr_t)&runin); | |
428 | } | |
287d9996 | 429 | |
83be5fac BJ |
430 | /* |
431 | * If there are pages that have been cleaned, | |
432 | * jolt the pageout daemon to process them. | |
433 | * We do this here so that these pages will be | |
434 | * freed if there is an abundance of memory and the | |
435 | * daemon would not be awakened otherwise. | |
436 | */ | |
437 | if (bclnlist != NULL) | |
438 | wakeup((caddr_t)&proc[2]); | |
287d9996 BJ |
439 | |
440 | /* | |
441 | * If the trap occurred from usermode, | |
442 | * then check to see if it has now been | |
443 | * running more than 10 minutes of user time | |
444 | * and should thus run with reduced priority | |
445 | * to give other processes a chance. | |
446 | */ | |
83be5fac BJ |
447 | if (USERMODE(ps)) { |
448 | pp = u.u_procp; | |
287d9996 BJ |
449 | if (pp->p_uid && pp->p_nice == NZERO && |
450 | u.u_vm.vm_utime > 600 * hz) | |
451 | pp->p_nice = NZERO+4; | |
81263dba | 452 | (void) setpri(pp); |
83be5fac | 453 | pp->p_pri = pp->p_usrpri; |
054016e1 | 454 | } |
83be5fac | 455 | } |
287d9996 BJ |
456 | /* |
457 | * If trapped user-mode, give it a profiling tick. | |
458 | */ | |
f403d99f BJ |
459 | if (USERMODE(ps) && u.u_prof.pr_scale) { |
460 | u.u_procp->p_flag |= SOWEUPC; | |
461 | aston(); | |
83be5fac | 462 | } |
83be5fac BJ |
463 | } |
464 | ||
465 | /* | |
964bcfb1 | 466 | * Timeout is called to arrange that |
0a34b6fd | 467 | * fun(arg) is called in tim/hz seconds. |
c4710996 | 468 | * An entry is linked into the callout |
964bcfb1 | 469 | * structure. The time in each structure |
0a34b6fd | 470 | * entry is the number of hz's more |
83be5fac BJ |
471 | * than the previous entry. |
472 | * In this way, decrementing the | |
473 | * first entry has the effect of | |
474 | * updating all entries. | |
475 | * | |
476 | * The panic is there because there is nothing | |
477 | * intelligent to be done if an entry won't fit. | |
478 | */ | |
479 | timeout(fun, arg, tim) | |
4512b9a4 BJ |
480 | int (*fun)(); |
481 | caddr_t arg; | |
83be5fac | 482 | { |
c4710996 | 483 | register struct callout *p1, *p2, *pnew; |
83be5fac BJ |
484 | register int t; |
485 | int s; | |
486 | ||
47477f34 BJ |
487 | /* DEBUGGING CODE */ |
488 | int ttrstrt(); | |
489 | ||
490 | if (fun == ttrstrt && arg == 0) | |
491 | panic("timeout ttrstr arg"); | |
492 | /* END DEBUGGING CODE */ | |
83be5fac | 493 | t = tim; |
83be5fac | 494 | s = spl7(); |
c4710996 BJ |
495 | pnew = callfree; |
496 | if (pnew == NULL) | |
497 | panic("timeout table overflow"); | |
498 | callfree = pnew->c_next; | |
499 | pnew->c_arg = arg; | |
500 | pnew->c_func = fun; | |
501 | for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2) | |
502 | t -= p2->c_time; | |
503 | p1->c_next = pnew; | |
504 | pnew->c_next = p2; | |
505 | pnew->c_time = t; | |
506 | if (p2) | |
507 | p2->c_time -= t; | |
83be5fac BJ |
508 | splx(s); |
509 | } | |
1fa9ff62 SL |
510 | |
511 | /* | |
512 | * untimeout is called to remove a function timeout call | |
513 | * from the callout structure. | |
514 | */ | |
515 | untimeout (fun, arg) | |
516 | int (*fun)(); | |
517 | caddr_t arg; | |
518 | { | |
519 | ||
520 | register struct callout *p1, *p2; | |
521 | register int s; | |
522 | ||
523 | s = spl7(); | |
524 | for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) { | |
525 | if (p2->c_func == fun && p2->c_arg == arg) { | |
526 | if (p2->c_next) | |
527 | p2->c_next->c_time += p2->c_time; | |
528 | p1->c_next = p2->c_next; | |
529 | p2->c_next = callfree; | |
530 | callfree = p2; | |
531 | break; | |
532 | } | |
533 | } | |
534 | splx(s); | |
535 | } |