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