xref: /csrg-svn/sys/kern/kern_clock.c (revision 4250)
1*4250Swnj /*	kern_clock.c	4.24	81/08/28	*/
29Sbill 
39Sbill #include "../h/param.h"
49Sbill #include "../h/systm.h"
5329Sbill #include "../h/dk.h"
62768Swnj #include "../h/callout.h"
79Sbill #include "../h/seg.h"
89Sbill #include "../h/dir.h"
99Sbill #include "../h/user.h"
109Sbill #include "../h/proc.h"
119Sbill #include "../h/reg.h"
129Sbill #include "../h/psl.h"
139Sbill #include "../h/vm.h"
149Sbill #include "../h/buf.h"
159Sbill #include "../h/text.h"
16877Sbill #include "../h/vlimit.h"
17877Sbill #include "../h/mtpr.h"
18877Sbill #include "../h/clock.h"
192689Swnj #include "../h/cpu.h"
209Sbill 
213511Sroot #include "bk.h"
221943Swnj #include "dh.h"
231943Swnj #include "dz.h"
241559Sbill 
259Sbill /*
262442Swnj  * Hardclock is called straight from
279Sbill  * the real time clock interrupt.
282442Swnj  * We limit the work we do at real clock interrupt time to:
292442Swnj  *	reloading clock
302442Swnj  *	decrementing time to callouts
312442Swnj  *	recording cpu time usage
322450Swnj  *	modifying priority of current process
332442Swnj  *	arrange for soft clock interrupt
342442Swnj  *	kernel pc profiling
359Sbill  *
363110Swnj  * At software (softclock) interrupt time we:
379Sbill  *	implement callouts
389Sbill  *	maintain date
399Sbill  *	lightning bolt wakeup (every second)
409Sbill  *	alarm clock signals
419Sbill  *	jab the scheduler
422442Swnj  *
432442Swnj  * On the vax softclock interrupts are implemented by
442442Swnj  * software interrupts.  Note that we may have multiple softclock
452442Swnj  * interrupts compressed into one (due to excessive interrupt load),
462442Swnj  * but that hardclock interrupts should never be lost.
479Sbill  */
489Sbill 
492609Swnj /*ARGSUSED*/
502442Swnj hardclock(pc, ps)
512450Swnj 	caddr_t pc;
529Sbill {
532768Swnj 	register struct callout *p1;
549Sbill 	register struct proc *pp;
552442Swnj 	register int s, cpstate;
569Sbill 
579Sbill 	/*
589Sbill 	 * reprime clock
599Sbill 	 */
609Sbill 	clkreld();
619Sbill 
629Sbill 	/*
632442Swnj 	 * update callout times
649Sbill 	 */
653542Swnj 	for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next)
663542Swnj 		;
673542Swnj 	if (p1)
683542Swnj 		p1->c_time--;
69138Sbill 
70138Sbill 	/*
712442Swnj 	 * Maintain iostat and per-process cpu statistics
72138Sbill 	 */
739Sbill 	if (!noproc) {
749Sbill 		s = u.u_procp->p_rssize;
759Sbill 		u.u_vm.vm_idsrss += s;
769Sbill 		if (u.u_procp->p_textp) {
779Sbill 			register int xrss = u.u_procp->p_textp->x_rssize;
789Sbill 
799Sbill 			s += xrss;
809Sbill 			u.u_vm.vm_ixrss += xrss;
819Sbill 		}
829Sbill 		if (s > u.u_vm.vm_maxrss)
839Sbill 			u.u_vm.vm_maxrss = s;
842768Swnj 		if ((u.u_vm.vm_utime+u.u_vm.vm_stime+1)/hz > u.u_limit[LIM_CPU]) {
85375Sbill 			psignal(u.u_procp, SIGXCPU);
86375Sbill 			if (u.u_limit[LIM_CPU] < INFINITY - 5)
87375Sbill 				u.u_limit[LIM_CPU] += 5;
88375Sbill 		}
899Sbill 	}
903110Swnj 	/*
913110Swnj 	 * Update iostat information.
923110Swnj 	 */
939Sbill 	if (USERMODE(ps)) {
949Sbill 		u.u_vm.vm_utime++;
959Sbill 		if(u.u_procp->p_nice > NZERO)
96305Sbill 			cpstate = CP_NICE;
97305Sbill 		else
98305Sbill 			cpstate = CP_USER;
999Sbill 	} else {
100305Sbill 		cpstate = CP_SYS;
1019Sbill 		if (noproc)
102305Sbill 			cpstate = CP_IDLE;
1039Sbill 		else
1049Sbill 			u.u_vm.vm_stime++;
1059Sbill 	}
1061408Sbill 	cp_time[cpstate]++;
1072442Swnj 	for (s = 0; s < DK_NDRIVE; s++)
1082442Swnj 		if (dk_busy&(1<<s))
1092442Swnj 			dk_time[s]++;
1103110Swnj 	/*
1113110Swnj 	 * Adjust priority of current process.
1123110Swnj 	 */
1139Sbill 	if (!noproc) {
1149Sbill 		pp = u.u_procp;
1151399Sbill 		pp->p_cpticks++;
1169Sbill 		if(++pp->p_cpu == 0)
1179Sbill 			pp->p_cpu--;
1183876Swnj 		if(pp->p_cpu % 4 == 0) {
119125Sbill 			(void) setpri(pp);
1209Sbill 			if (pp->p_pri >= PUSER)
1219Sbill 				pp->p_pri = pp->p_usrpri;
1229Sbill 		}
1239Sbill 	}
1243110Swnj 	/*
1253110Swnj 	 * Time moves on.
1263110Swnj 	 */
1279Sbill 	++lbolt;
1282689Swnj #if VAX780
1293110Swnj 	/*
1303110Swnj 	 * On 780's, impelement a fast UBA watcher,
1313110Swnj 	 * to make sure uba's don't get stuck.
1323110Swnj 	 */
1332872Swnj 	if (cpu == VAX_780 && panicstr == 0 && !BASEPRI(ps))
1342442Swnj 		unhang();
1352442Swnj #endif
1363110Swnj 	/*
1373110Swnj 	 * Schedule a software interrupt for the rest
1383110Swnj 	 * of clock activities.
1393110Swnj 	 */
1402442Swnj 	setsoftclock();
1412442Swnj }
1422442Swnj 
1432442Swnj /*
1443876Swnj  * The digital decay cpu usage priority assignment is scaled to run in
1453876Swnj  * time as expanded by the 1 minute load average.  Each second we
1463876Swnj  * multiply the the previous cpu usage estimate by
1473876Swnj  *		nrscale*avenrun[0]
1483876Swnj  * The following relates the load average to the period over which
1493876Swnj  * cpu usage is 90% forgotten:
1503876Swnj  *	loadav 1	 5 seconds
1513876Swnj  *	loadav 5	24 seconds
1523876Swnj  *	loadav 10	47 seconds
1533876Swnj  *	loadav 20	93 seconds
1543876Swnj  * This is a great improvement on the previous algorithm which
1553876Swnj  * decayed the priorities by a constant, and decayed away all knowledge
1563876Swnj  * of previous activity in about 20 seconds.  Under heavy load,
1573876Swnj  * the previous algorithm degenerated to round-robin with poor response
1583876Swnj  * time when there was a high load average.
1592442Swnj  */
1603984Sroot #undef ave
1613876Swnj #define	ave(a,b) ((int)(((int)(a*b))/(b+1)))
1623876Swnj int	nrscale = 2;
1633876Swnj double	avenrun[];
1643110Swnj 
1653110Swnj /*
1663110Swnj  * Constant for decay filter for cpu usage field
1673110Swnj  * in process table (used by ps au).
1683110Swnj  */
1692442Swnj double	ccpu = 0.95122942450071400909;		/* exp(-1/20) */
1702442Swnj 
1712442Swnj /*
1722442Swnj  * Software clock interrupt.
1733110Swnj  * This routine runs at lower priority than device interrupts.
1742442Swnj  */
1752609Swnj /*ARGSUSED*/
1762442Swnj softclock(pc, ps)
1772450Swnj 	caddr_t pc;
1782442Swnj {
1793615Sroot 	register struct callout *p1;
1802442Swnj 	register struct proc *pp;
1812442Swnj 	register int a, s;
1823542Swnj 	caddr_t arg;
1833542Swnj 	int (*func)();
1842442Swnj 
1852442Swnj 	/*
1862872Swnj 	 * Perform callouts (but not after panic's!)
1872442Swnj 	 */
1883542Swnj 	if (panicstr == 0) {
1893542Swnj 		for (;;) {
1903542Swnj 			s = spl7();
191*4250Swnj 			if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
192*4250Swnj 				splx(s);
1933542Swnj 				break;
194*4250Swnj 			}
1953542Swnj 			calltodo.c_next = p1->c_next;
1963542Swnj 			arg = p1->c_arg;
1973542Swnj 			func = p1->c_func;
1983542Swnj 			p1->c_next = callfree;
1993542Swnj 			callfree = p1;
2003542Swnj 			(void) splx(s);
2013542Swnj 			(*func)(arg);
2022442Swnj 		}
2032442Swnj 	}
2042442Swnj 
2052442Swnj 	/*
2062442Swnj 	 * Drain silos.
2072442Swnj 	 */
2083511Sroot #if NBK > 0
2092647Swnj #if NDH > 0
2102442Swnj 	s = spl5(); dhtimer(); splx(s);
2112442Swnj #endif
2122647Swnj #if NDZ > 0
2132442Swnj 	s = spl5(); dztimer(); splx(s);
2142442Swnj #endif
2153511Sroot #endif
2162442Swnj 
2172442Swnj 	/*
2182450Swnj 	 * If idling and processes are waiting to swap in,
2192450Swnj 	 * check on them.
2202450Swnj 	 */
2212450Swnj 	if (noproc && runin) {
2222450Swnj 		runin = 0;
2232450Swnj 		wakeup((caddr_t)&runin);
2242450Swnj 	}
2252450Swnj 
2262450Swnj 	/*
2273876Swnj 	 * Run paging daemon every 1/4 sec.
2282442Swnj 	 */
2292768Swnj 	if (lbolt % (hz/4) == 0) {
2309Sbill 		vmpago();
2313876Swnj 	}
2323876Swnj 
2333876Swnj 	/*
2343876Swnj 	 * Reschedule every 1/10 sec.
2353876Swnj 	 */
2363876Swnj 	if (lbolt % (hz/10) == 0) {
2379Sbill 		runrun++;
2382442Swnj 		aston();
2399Sbill 	}
2402442Swnj 
2412442Swnj 	/*
2422442Swnj 	 * Lightning bolt every second:
2432442Swnj 	 *	sleep timeouts
2442442Swnj 	 *	process priority recomputation
2452442Swnj 	 *	process %cpu averaging
2462442Swnj 	 *	virtual memory metering
2472442Swnj 	 *	kick swapper if processes want in
2482442Swnj 	 */
2492768Swnj 	if (lbolt >= hz) {
2502872Swnj 		/*
2513110Swnj 		 * This doesn't mean much on VAX since we run at
2522872Swnj 		 * software interrupt time... if hardclock()
2532872Swnj 		 * calls softclock() directly, it prevents
2542872Swnj 		 * this code from running when the priority
2552872Swnj 		 * was raised when the clock interrupt occurred.
2562872Swnj 		 */
2579Sbill 		if (BASEPRI(ps))
2589Sbill 			return;
2592872Swnj 
2602872Swnj 		/*
2612872Swnj 		 * If we didn't run a few times because of
2622872Swnj 		 * long blockage at high ipl, we don't
2632872Swnj 		 * really want to run this code several times,
2642872Swnj 		 * so squish out all multiples of hz here.
2652872Swnj 		 */
2662872Swnj 		time += lbolt / hz;
2672872Swnj 		lbolt %= hz;
2682872Swnj 
2692872Swnj 		/*
2702872Swnj 		 * Wakeup lightning bolt sleepers.
2712872Swnj 		 * Processes sleep on lbolt to wait
2722872Swnj 		 * for short amounts of time (e.g. 1 second).
2732872Swnj 		 */
2749Sbill 		wakeup((caddr_t)&lbolt);
2752872Swnj 
2762872Swnj 		/*
2772872Swnj 		 * Recompute process priority and process
2782872Swnj 		 * sleep() system calls as well as internal
2792872Swnj 		 * sleeps with timeouts (tsleep() kernel routine).
2802872Swnj 		 */
2812872Swnj 		for (pp = proc; pp < procNPROC; pp++)
282928Sbill 		if (pp->p_stat && pp->p_stat!=SZOMB) {
2832872Swnj 			/*
2842872Swnj 			 * Increase resident time, to max of 127 seconds
2852872Swnj 			 * (it is kept in a character.)  For
2862872Swnj 			 * loaded processes this is time in core; for
2872872Swnj 			 * swapped processes, this is time on drum.
2882872Swnj 			 */
2892872Swnj 			if (pp->p_time != 127)
2909Sbill 				pp->p_time++;
2912872Swnj 			/*
2922872Swnj 			 * If process has clock counting down, and it
2932872Swnj 			 * expires, set it running (if this is a tsleep()),
2942872Swnj 			 * or give it an SIGALRM (if the user process
2952872Swnj 			 * is using alarm signals.
2962872Swnj 			 */
2972872Swnj 			if (pp->p_clktim && --pp->p_clktim == 0)
2982872Swnj 				if (pp->p_flag & STIMO) {
2992872Swnj 					s = spl6();
3002872Swnj 					switch (pp->p_stat) {
301204Sbill 
3022872Swnj 					case SSLEEP:
3032872Swnj 						setrun(pp);
3042872Swnj 						break;
305204Sbill 
3062872Swnj 					case SSTOP:
3072872Swnj 						unsleep(pp);
3082872Swnj 						break;
3092872Swnj 					}
3102872Swnj 					pp->p_flag &= ~STIMO;
3112872Swnj 					splx(s);
3122872Swnj 				} else
3132872Swnj 					psignal(pp, SIGALRM);
3142872Swnj 			/*
3152872Swnj 			 * If process is blocked, increment computed
3162872Swnj 			 * time blocked.  This is used in swap scheduling.
3172872Swnj 			 */
3182872Swnj 			if (pp->p_stat==SSLEEP || pp->p_stat==SSTOP)
3199Sbill 				if (pp->p_slptime != 127)
3209Sbill 					pp->p_slptime++;
3212872Swnj 			/*
3222872Swnj 			 * Update digital filter estimation of process
3232872Swnj 			 * cpu utilization for loaded processes.
3242872Swnj 			 */
3251399Sbill 			if (pp->p_flag&SLOAD)
3261399Sbill 				pp->p_pctcpu = ccpu * pp->p_pctcpu +
3272768Swnj 				    (1.0 - ccpu) * (pp->p_cpticks/(float)hz);
3282872Swnj 			/*
3292872Swnj 			 * Recompute process priority.  The number p_cpu
3302872Swnj 			 * is a weighted estimate of cpu time consumed.
3312872Swnj 			 * A process which consumes cpu time has this
3322872Swnj 			 * increase regularly.  We here decrease it by
3333876Swnj 			 * a fraction based on load average giving a digital
3343876Swnj 			 * decay filter which damps out in about 5 seconds
3353876Swnj 			 * when seconds are measured in time expanded by the
3363876Swnj 			 * load average.
3372872Swnj 			 *
3382872Swnj 			 * If a process is niced, then the nice directly
3392872Swnj 			 * affects the new priority.  The final priority
3402872Swnj 			 * is in the range 0 to 255, to fit in a character.
3412872Swnj 			 */
3421399Sbill 			pp->p_cpticks = 0;
3433876Swnj 			a = ave((pp->p_cpu & 0377), avenrun[0]*nrscale) +
3443876Swnj 			     pp->p_nice - NZERO;
3452872Swnj 			if (a < 0)
3469Sbill 				a = 0;
3472872Swnj 			if (a > 255)
3489Sbill 				a = 255;
3499Sbill 			pp->p_cpu = a;
350125Sbill 			(void) setpri(pp);
3512872Swnj 			/*
3522872Swnj 			 * Now have computed new process priority
3532872Swnj 			 * in p->p_usrpri.  Carefully change p->p_pri.
3542872Swnj 			 * A process is on a run queue associated with
3552872Swnj 			 * this priority, so we must block out process
3562872Swnj 			 * state changes during the transition.
3572872Swnj 			 */
3589Sbill 			s = spl6();
3592872Swnj 			if (pp->p_pri >= PUSER) {
3609Sbill 				if ((pp != u.u_procp || noproc) &&
3619Sbill 				    pp->p_stat == SRUN &&
3629Sbill 				    (pp->p_flag & SLOAD) &&
3639Sbill 				    pp->p_pri != pp->p_usrpri) {
3649Sbill 					remrq(pp);
3659Sbill 					pp->p_pri = pp->p_usrpri;
3669Sbill 					setrq(pp);
3679Sbill 				} else
3689Sbill 					pp->p_pri = pp->p_usrpri;
3699Sbill 			}
3709Sbill 			splx(s);
3719Sbill 		}
3722872Swnj 
3732872Swnj 		/*
3742872Swnj 		 * Perform virtual memory metering.
3752872Swnj 		 */
3769Sbill 		vmmeter();
3772872Swnj 
3782872Swnj 		/*
3792872Swnj 		 * If the swap process is trying to bring
3802872Swnj 		 * a process in, have it look again to see
3812872Swnj 		 * if it is possible now.
3822872Swnj 		 */
3832872Swnj 		if (runin!=0) {
3849Sbill 			runin = 0;
3859Sbill 			wakeup((caddr_t)&runin);
3869Sbill 		}
3872872Swnj 
3889Sbill 		/*
3899Sbill 		 * If there are pages that have been cleaned,
3909Sbill 		 * jolt the pageout daemon to process them.
3919Sbill 		 * We do this here so that these pages will be
3929Sbill 		 * freed if there is an abundance of memory and the
3939Sbill 		 * daemon would not be awakened otherwise.
3949Sbill 		 */
3959Sbill 		if (bclnlist != NULL)
3969Sbill 			wakeup((caddr_t)&proc[2]);
3972872Swnj 
3982872Swnj 		/*
3992872Swnj 		 * If the trap occurred from usermode,
4002872Swnj 		 * then check to see if it has now been
4012872Swnj 		 * running more than 10 minutes of user time
4022872Swnj 		 * and should thus run with reduced priority
4032872Swnj 		 * to give other processes a chance.
4042872Swnj 		 */
4059Sbill 		if (USERMODE(ps)) {
4069Sbill 			pp = u.u_procp;
4072872Swnj 			if (pp->p_uid && pp->p_nice == NZERO &&
4082872Swnj 			    u.u_vm.vm_utime > 600 * hz)
4092872Swnj 				pp->p_nice = NZERO+4;
410125Sbill 			(void) setpri(pp);
4119Sbill 			pp->p_pri = pp->p_usrpri;
4129Sbill 		}
4139Sbill 	}
4142872Swnj 	/*
4152872Swnj 	 * If trapped user-mode, give it a profiling tick.
4162872Swnj 	 */
4172442Swnj 	if (USERMODE(ps) && u.u_prof.pr_scale) {
4182442Swnj 		u.u_procp->p_flag |= SOWEUPC;
4192442Swnj 		aston();
4209Sbill 	}
4219Sbill }
4229Sbill 
4239Sbill /*
4243110Swnj  * Timeout is called to arrange that
4252768Swnj  * fun(arg) is called in tim/hz seconds.
4263542Swnj  * An entry is linked into the callout
4273110Swnj  * structure.  The time in each structure
4282768Swnj  * entry is the number of hz's more
4299Sbill  * than the previous entry.
4309Sbill  * In this way, decrementing the
4319Sbill  * first entry has the effect of
4329Sbill  * updating all entries.
4339Sbill  *
4349Sbill  * The panic is there because there is nothing
4359Sbill  * intelligent to be done if an entry won't fit.
4369Sbill  */
4379Sbill timeout(fun, arg, tim)
4382450Swnj 	int (*fun)();
4392450Swnj 	caddr_t arg;
4409Sbill {
4413542Swnj 	register struct callout *p1, *p2, *pnew;
4429Sbill 	register int t;
4439Sbill 	int s;
4449Sbill 
4453446Sroot /* DEBUGGING CODE */
4463446Sroot 	int ttrstrt();
4473446Sroot 
4483446Sroot 	if (fun == ttrstrt && arg == 0)
4493446Sroot 		panic("timeout ttrstr arg");
4503446Sroot /* END DEBUGGING CODE */
4519Sbill 	t = tim;
4529Sbill 	s = spl7();
4533542Swnj 	pnew = callfree;
4543542Swnj 	if (pnew == NULL)
4553542Swnj 		panic("timeout table overflow");
4563542Swnj 	callfree = pnew->c_next;
4573542Swnj 	pnew->c_arg = arg;
4583542Swnj 	pnew->c_func = fun;
4593542Swnj 	for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
4603542Swnj 		t -= p2->c_time;
4613542Swnj 	p1->c_next = pnew;
4623542Swnj 	pnew->c_next = p2;
4633542Swnj 	pnew->c_time = t;
4643542Swnj 	if (p2)
4653542Swnj 		p2->c_time -= t;
4669Sbill 	splx(s);
4679Sbill }
468