xref: /original-bsd/sys/kern/kern_clock.c (revision 6c57d260)
1 /*	kern_clock.c	4.21	81/04/28	*/
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 % 16 == 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  * SCHMAG is the constant in the digital decay cpu
145  * usage priority assignment.  Each second we multiply
146  * the previous cpu usage estimate by SCHMAG.  At 9/10
147  * it tends to decay away all knowledge of previous activity
148  * in about 10 seconds.
149  */
150 #define	SCHMAG	9/10
151 
152 /*
153  * Constant for decay filter for cpu usage field
154  * in process table (used by ps au).
155  */
156 double	ccpu = 0.95122942450071400909;		/* exp(-1/20) */
157 
158 /*
159  * Software clock interrupt.
160  * This routine runs at lower priority than device interrupts.
161  */
162 /*ARGSUSED*/
163 softclock(pc, ps)
164 	caddr_t pc;
165 {
166 	register struct callout *p1;
167 	register struct proc *pp;
168 	register int a, s;
169 	caddr_t arg;
170 	int (*func)();
171 
172 	/*
173 	 * Perform callouts (but not after panic's!)
174 	 */
175 	if (panicstr == 0) {
176 		for (;;) {
177 			s = spl7();
178 			if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0)
179 				break;
180 			calltodo.c_next = p1->c_next;
181 			arg = p1->c_arg;
182 			func = p1->c_func;
183 			p1->c_next = callfree;
184 			callfree = p1;
185 			(void) splx(s);
186 			(*func)(arg);
187 		}
188 	}
189 
190 	/*
191 	 * Drain silos.
192 	 */
193 #if NBK > 0
194 #if NDH > 0
195 	s = spl5(); dhtimer(); splx(s);
196 #endif
197 #if NDZ > 0
198 	s = spl5(); dztimer(); splx(s);
199 #endif
200 #endif
201 
202 	/*
203 	 * If idling and processes are waiting to swap in,
204 	 * check on them.
205 	 */
206 	if (noproc && runin) {
207 		runin = 0;
208 		wakeup((caddr_t)&runin);
209 	}
210 
211 	/*
212 	 * Run paging daemon and reschedule every 1/4 sec.
213 	 */
214 	if (lbolt % (hz/4) == 0) {
215 		vmpago();
216 		runrun++;
217 		aston();
218 	}
219 
220 	/*
221 	 * Lightning bolt every second:
222 	 *	sleep timeouts
223 	 *	process priority recomputation
224 	 *	process %cpu averaging
225 	 *	virtual memory metering
226 	 *	kick swapper if processes want in
227 	 */
228 	if (lbolt >= hz) {
229 		/*
230 		 * This doesn't mean much on VAX since we run at
231 		 * software interrupt time... if hardclock()
232 		 * calls softclock() directly, it prevents
233 		 * this code from running when the priority
234 		 * was raised when the clock interrupt occurred.
235 		 */
236 		if (BASEPRI(ps))
237 			return;
238 
239 		/*
240 		 * If we didn't run a few times because of
241 		 * long blockage at high ipl, we don't
242 		 * really want to run this code several times,
243 		 * so squish out all multiples of hz here.
244 		 */
245 		time += lbolt / hz;
246 		lbolt %= hz;
247 
248 		/*
249 		 * Wakeup lightning bolt sleepers.
250 		 * Processes sleep on lbolt to wait
251 		 * for short amounts of time (e.g. 1 second).
252 		 */
253 		wakeup((caddr_t)&lbolt);
254 
255 		/*
256 		 * Recompute process priority and process
257 		 * sleep() system calls as well as internal
258 		 * sleeps with timeouts (tsleep() kernel routine).
259 		 */
260 		for (pp = proc; pp < procNPROC; pp++)
261 		if (pp->p_stat && pp->p_stat!=SZOMB) {
262 			/*
263 			 * Increase resident time, to max of 127 seconds
264 			 * (it is kept in a character.)  For
265 			 * loaded processes this is time in core; for
266 			 * swapped processes, this is time on drum.
267 			 */
268 			if (pp->p_time != 127)
269 				pp->p_time++;
270 			/*
271 			 * If process has clock counting down, and it
272 			 * expires, set it running (if this is a tsleep()),
273 			 * or give it an SIGALRM (if the user process
274 			 * is using alarm signals.
275 			 */
276 			if (pp->p_clktim && --pp->p_clktim == 0)
277 				if (pp->p_flag & STIMO) {
278 					s = spl6();
279 					switch (pp->p_stat) {
280 
281 					case SSLEEP:
282 						setrun(pp);
283 						break;
284 
285 					case SSTOP:
286 						unsleep(pp);
287 						break;
288 					}
289 					pp->p_flag &= ~STIMO;
290 					splx(s);
291 				} else
292 					psignal(pp, SIGALRM);
293 			/*
294 			 * If process is blocked, increment computed
295 			 * time blocked.  This is used in swap scheduling.
296 			 */
297 			if (pp->p_stat==SSLEEP || pp->p_stat==SSTOP)
298 				if (pp->p_slptime != 127)
299 					pp->p_slptime++;
300 			/*
301 			 * Update digital filter estimation of process
302 			 * cpu utilization for loaded processes.
303 			 */
304 			if (pp->p_flag&SLOAD)
305 				pp->p_pctcpu = ccpu * pp->p_pctcpu +
306 				    (1.0 - ccpu) * (pp->p_cpticks/(float)hz);
307 			/*
308 			 * Recompute process priority.  The number p_cpu
309 			 * is a weighted estimate of cpu time consumed.
310 			 * A process which consumes cpu time has this
311 			 * increase regularly.  We here decrease it by
312 			 * a fraction (SCHMAG is 90%), giving a digital
313 			 * decay filter which damps out in about 10 seconds.
314 			 *
315 			 * If a process is niced, then the nice directly
316 			 * affects the new priority.  The final priority
317 			 * is in the range 0 to 255, to fit in a character.
318 			 */
319 			pp->p_cpticks = 0;
320 			a = (pp->p_cpu & 0377)*SCHMAG + pp->p_nice - NZERO;
321 			if (a < 0)
322 				a = 0;
323 			if (a > 255)
324 				a = 255;
325 			pp->p_cpu = a;
326 			(void) setpri(pp);
327 			/*
328 			 * Now have computed new process priority
329 			 * in p->p_usrpri.  Carefully change p->p_pri.
330 			 * A process is on a run queue associated with
331 			 * this priority, so we must block out process
332 			 * state changes during the transition.
333 			 */
334 			s = spl6();
335 			if (pp->p_pri >= PUSER) {
336 				if ((pp != u.u_procp || noproc) &&
337 				    pp->p_stat == SRUN &&
338 				    (pp->p_flag & SLOAD) &&
339 				    pp->p_pri != pp->p_usrpri) {
340 					remrq(pp);
341 					pp->p_pri = pp->p_usrpri;
342 					setrq(pp);
343 				} else
344 					pp->p_pri = pp->p_usrpri;
345 			}
346 			splx(s);
347 		}
348 
349 		/*
350 		 * Perform virtual memory metering.
351 		 */
352 		vmmeter();
353 
354 		/*
355 		 * If the swap process is trying to bring
356 		 * a process in, have it look again to see
357 		 * if it is possible now.
358 		 */
359 		if (runin!=0) {
360 			runin = 0;
361 			wakeup((caddr_t)&runin);
362 		}
363 
364 		/*
365 		 * If there are pages that have been cleaned,
366 		 * jolt the pageout daemon to process them.
367 		 * We do this here so that these pages will be
368 		 * freed if there is an abundance of memory and the
369 		 * daemon would not be awakened otherwise.
370 		 */
371 		if (bclnlist != NULL)
372 			wakeup((caddr_t)&proc[2]);
373 
374 		/*
375 		 * If the trap occurred from usermode,
376 		 * then check to see if it has now been
377 		 * running more than 10 minutes of user time
378 		 * and should thus run with reduced priority
379 		 * to give other processes a chance.
380 		 */
381 		if (USERMODE(ps)) {
382 			pp = u.u_procp;
383 			if (pp->p_uid && pp->p_nice == NZERO &&
384 			    u.u_vm.vm_utime > 600 * hz)
385 				pp->p_nice = NZERO+4;
386 			(void) setpri(pp);
387 			pp->p_pri = pp->p_usrpri;
388 		}
389 	}
390 	/*
391 	 * If trapped user-mode, give it a profiling tick.
392 	 */
393 	if (USERMODE(ps) && u.u_prof.pr_scale) {
394 		u.u_procp->p_flag |= SOWEUPC;
395 		aston();
396 	}
397 }
398 
399 /*
400  * Timeout is called to arrange that
401  * fun(arg) is called in tim/hz seconds.
402  * An entry is linked into the callout
403  * structure.  The time in each structure
404  * entry is the number of hz's more
405  * than the previous entry.
406  * In this way, decrementing the
407  * first entry has the effect of
408  * updating all entries.
409  *
410  * The panic is there because there is nothing
411  * intelligent to be done if an entry won't fit.
412  */
413 timeout(fun, arg, tim)
414 	int (*fun)();
415 	caddr_t arg;
416 {
417 	register struct callout *p1, *p2, *pnew;
418 	register int t;
419 	int s;
420 
421 /* DEBUGGING CODE */
422 	int ttrstrt();
423 
424 	if (fun == ttrstrt && arg == 0)
425 		panic("timeout ttrstr arg");
426 /* END DEBUGGING CODE */
427 	t = tim;
428 	s = spl7();
429 	pnew = callfree;
430 	if (pnew == NULL)
431 		panic("timeout table overflow");
432 	callfree = pnew->c_next;
433 	pnew->c_arg = arg;
434 	pnew->c_func = fun;
435 	for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
436 		t -= p2->c_time;
437 	p1->c_next = pnew;
438 	pnew->c_next = p2;
439 	pnew->c_time = t;
440 	if (p2)
441 		p2->c_time -= t;
442 	splx(s);
443 }
444