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