xref: /csrg-svn/sys/kern/kern_clock.c (revision 3984)
1*3984Sroot /*	kern_clock.c	4.23	81/07/09	*/
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  */
160*3984Sroot #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();
1913542Swnj 			if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0)
1923542Swnj 				break;
1933542Swnj 			calltodo.c_next = p1->c_next;
1943542Swnj 			arg = p1->c_arg;
1953542Swnj 			func = p1->c_func;
1963542Swnj 			p1->c_next = callfree;
1973542Swnj 			callfree = p1;
1983542Swnj 			(void) splx(s);
1993542Swnj 			(*func)(arg);
2002442Swnj 		}
2012442Swnj 	}
2022442Swnj 
2032442Swnj 	/*
2042442Swnj 	 * Drain silos.
2052442Swnj 	 */
2063511Sroot #if NBK > 0
2072647Swnj #if NDH > 0
2082442Swnj 	s = spl5(); dhtimer(); splx(s);
2092442Swnj #endif
2102647Swnj #if NDZ > 0
2112442Swnj 	s = spl5(); dztimer(); splx(s);
2122442Swnj #endif
2133511Sroot #endif
2142442Swnj 
2152442Swnj 	/*
2162450Swnj 	 * If idling and processes are waiting to swap in,
2172450Swnj 	 * check on them.
2182450Swnj 	 */
2192450Swnj 	if (noproc && runin) {
2202450Swnj 		runin = 0;
2212450Swnj 		wakeup((caddr_t)&runin);
2222450Swnj 	}
2232450Swnj 
2242450Swnj 	/*
2253876Swnj 	 * Run paging daemon every 1/4 sec.
2262442Swnj 	 */
2272768Swnj 	if (lbolt % (hz/4) == 0) {
2289Sbill 		vmpago();
2293876Swnj 	}
2303876Swnj 
2313876Swnj 	/*
2323876Swnj 	 * Reschedule every 1/10 sec.
2333876Swnj 	 */
2343876Swnj 	if (lbolt % (hz/10) == 0) {
2359Sbill 		runrun++;
2362442Swnj 		aston();
2379Sbill 	}
2382442Swnj 
2392442Swnj 	/*
2402442Swnj 	 * Lightning bolt every second:
2412442Swnj 	 *	sleep timeouts
2422442Swnj 	 *	process priority recomputation
2432442Swnj 	 *	process %cpu averaging
2442442Swnj 	 *	virtual memory metering
2452442Swnj 	 *	kick swapper if processes want in
2462442Swnj 	 */
2472768Swnj 	if (lbolt >= hz) {
2482872Swnj 		/*
2493110Swnj 		 * This doesn't mean much on VAX since we run at
2502872Swnj 		 * software interrupt time... if hardclock()
2512872Swnj 		 * calls softclock() directly, it prevents
2522872Swnj 		 * this code from running when the priority
2532872Swnj 		 * was raised when the clock interrupt occurred.
2542872Swnj 		 */
2559Sbill 		if (BASEPRI(ps))
2569Sbill 			return;
2572872Swnj 
2582872Swnj 		/*
2592872Swnj 		 * If we didn't run a few times because of
2602872Swnj 		 * long blockage at high ipl, we don't
2612872Swnj 		 * really want to run this code several times,
2622872Swnj 		 * so squish out all multiples of hz here.
2632872Swnj 		 */
2642872Swnj 		time += lbolt / hz;
2652872Swnj 		lbolt %= hz;
2662872Swnj 
2672872Swnj 		/*
2682872Swnj 		 * Wakeup lightning bolt sleepers.
2692872Swnj 		 * Processes sleep on lbolt to wait
2702872Swnj 		 * for short amounts of time (e.g. 1 second).
2712872Swnj 		 */
2729Sbill 		wakeup((caddr_t)&lbolt);
2732872Swnj 
2742872Swnj 		/*
2752872Swnj 		 * Recompute process priority and process
2762872Swnj 		 * sleep() system calls as well as internal
2772872Swnj 		 * sleeps with timeouts (tsleep() kernel routine).
2782872Swnj 		 */
2792872Swnj 		for (pp = proc; pp < procNPROC; pp++)
280928Sbill 		if (pp->p_stat && pp->p_stat!=SZOMB) {
2812872Swnj 			/*
2822872Swnj 			 * Increase resident time, to max of 127 seconds
2832872Swnj 			 * (it is kept in a character.)  For
2842872Swnj 			 * loaded processes this is time in core; for
2852872Swnj 			 * swapped processes, this is time on drum.
2862872Swnj 			 */
2872872Swnj 			if (pp->p_time != 127)
2889Sbill 				pp->p_time++;
2892872Swnj 			/*
2902872Swnj 			 * If process has clock counting down, and it
2912872Swnj 			 * expires, set it running (if this is a tsleep()),
2922872Swnj 			 * or give it an SIGALRM (if the user process
2932872Swnj 			 * is using alarm signals.
2942872Swnj 			 */
2952872Swnj 			if (pp->p_clktim && --pp->p_clktim == 0)
2962872Swnj 				if (pp->p_flag & STIMO) {
2972872Swnj 					s = spl6();
2982872Swnj 					switch (pp->p_stat) {
299204Sbill 
3002872Swnj 					case SSLEEP:
3012872Swnj 						setrun(pp);
3022872Swnj 						break;
303204Sbill 
3042872Swnj 					case SSTOP:
3052872Swnj 						unsleep(pp);
3062872Swnj 						break;
3072872Swnj 					}
3082872Swnj 					pp->p_flag &= ~STIMO;
3092872Swnj 					splx(s);
3102872Swnj 				} else
3112872Swnj 					psignal(pp, SIGALRM);
3122872Swnj 			/*
3132872Swnj 			 * If process is blocked, increment computed
3142872Swnj 			 * time blocked.  This is used in swap scheduling.
3152872Swnj 			 */
3162872Swnj 			if (pp->p_stat==SSLEEP || pp->p_stat==SSTOP)
3179Sbill 				if (pp->p_slptime != 127)
3189Sbill 					pp->p_slptime++;
3192872Swnj 			/*
3202872Swnj 			 * Update digital filter estimation of process
3212872Swnj 			 * cpu utilization for loaded processes.
3222872Swnj 			 */
3231399Sbill 			if (pp->p_flag&SLOAD)
3241399Sbill 				pp->p_pctcpu = ccpu * pp->p_pctcpu +
3252768Swnj 				    (1.0 - ccpu) * (pp->p_cpticks/(float)hz);
3262872Swnj 			/*
3272872Swnj 			 * Recompute process priority.  The number p_cpu
3282872Swnj 			 * is a weighted estimate of cpu time consumed.
3292872Swnj 			 * A process which consumes cpu time has this
3302872Swnj 			 * increase regularly.  We here decrease it by
3313876Swnj 			 * a fraction based on load average giving a digital
3323876Swnj 			 * decay filter which damps out in about 5 seconds
3333876Swnj 			 * when seconds are measured in time expanded by the
3343876Swnj 			 * load average.
3352872Swnj 			 *
3362872Swnj 			 * If a process is niced, then the nice directly
3372872Swnj 			 * affects the new priority.  The final priority
3382872Swnj 			 * is in the range 0 to 255, to fit in a character.
3392872Swnj 			 */
3401399Sbill 			pp->p_cpticks = 0;
3413876Swnj 			a = ave((pp->p_cpu & 0377), avenrun[0]*nrscale) +
3423876Swnj 			     pp->p_nice - NZERO;
3432872Swnj 			if (a < 0)
3449Sbill 				a = 0;
3452872Swnj 			if (a > 255)
3469Sbill 				a = 255;
3479Sbill 			pp->p_cpu = a;
348125Sbill 			(void) setpri(pp);
3492872Swnj 			/*
3502872Swnj 			 * Now have computed new process priority
3512872Swnj 			 * in p->p_usrpri.  Carefully change p->p_pri.
3522872Swnj 			 * A process is on a run queue associated with
3532872Swnj 			 * this priority, so we must block out process
3542872Swnj 			 * state changes during the transition.
3552872Swnj 			 */
3569Sbill 			s = spl6();
3572872Swnj 			if (pp->p_pri >= PUSER) {
3589Sbill 				if ((pp != u.u_procp || noproc) &&
3599Sbill 				    pp->p_stat == SRUN &&
3609Sbill 				    (pp->p_flag & SLOAD) &&
3619Sbill 				    pp->p_pri != pp->p_usrpri) {
3629Sbill 					remrq(pp);
3639Sbill 					pp->p_pri = pp->p_usrpri;
3649Sbill 					setrq(pp);
3659Sbill 				} else
3669Sbill 					pp->p_pri = pp->p_usrpri;
3679Sbill 			}
3689Sbill 			splx(s);
3699Sbill 		}
3702872Swnj 
3712872Swnj 		/*
3722872Swnj 		 * Perform virtual memory metering.
3732872Swnj 		 */
3749Sbill 		vmmeter();
3752872Swnj 
3762872Swnj 		/*
3772872Swnj 		 * If the swap process is trying to bring
3782872Swnj 		 * a process in, have it look again to see
3792872Swnj 		 * if it is possible now.
3802872Swnj 		 */
3812872Swnj 		if (runin!=0) {
3829Sbill 			runin = 0;
3839Sbill 			wakeup((caddr_t)&runin);
3849Sbill 		}
3852872Swnj 
3869Sbill 		/*
3879Sbill 		 * If there are pages that have been cleaned,
3889Sbill 		 * jolt the pageout daemon to process them.
3899Sbill 		 * We do this here so that these pages will be
3909Sbill 		 * freed if there is an abundance of memory and the
3919Sbill 		 * daemon would not be awakened otherwise.
3929Sbill 		 */
3939Sbill 		if (bclnlist != NULL)
3949Sbill 			wakeup((caddr_t)&proc[2]);
3952872Swnj 
3962872Swnj 		/*
3972872Swnj 		 * If the trap occurred from usermode,
3982872Swnj 		 * then check to see if it has now been
3992872Swnj 		 * running more than 10 minutes of user time
4002872Swnj 		 * and should thus run with reduced priority
4012872Swnj 		 * to give other processes a chance.
4022872Swnj 		 */
4039Sbill 		if (USERMODE(ps)) {
4049Sbill 			pp = u.u_procp;
4052872Swnj 			if (pp->p_uid && pp->p_nice == NZERO &&
4062872Swnj 			    u.u_vm.vm_utime > 600 * hz)
4072872Swnj 				pp->p_nice = NZERO+4;
408125Sbill 			(void) setpri(pp);
4099Sbill 			pp->p_pri = pp->p_usrpri;
4109Sbill 		}
4119Sbill 	}
4122872Swnj 	/*
4132872Swnj 	 * If trapped user-mode, give it a profiling tick.
4142872Swnj 	 */
4152442Swnj 	if (USERMODE(ps) && u.u_prof.pr_scale) {
4162442Swnj 		u.u_procp->p_flag |= SOWEUPC;
4172442Swnj 		aston();
4189Sbill 	}
4199Sbill }
4209Sbill 
4219Sbill /*
4223110Swnj  * Timeout is called to arrange that
4232768Swnj  * fun(arg) is called in tim/hz seconds.
4243542Swnj  * An entry is linked into the callout
4253110Swnj  * structure.  The time in each structure
4262768Swnj  * entry is the number of hz's more
4279Sbill  * than the previous entry.
4289Sbill  * In this way, decrementing the
4299Sbill  * first entry has the effect of
4309Sbill  * updating all entries.
4319Sbill  *
4329Sbill  * The panic is there because there is nothing
4339Sbill  * intelligent to be done if an entry won't fit.
4349Sbill  */
4359Sbill timeout(fun, arg, tim)
4362450Swnj 	int (*fun)();
4372450Swnj 	caddr_t arg;
4389Sbill {
4393542Swnj 	register struct callout *p1, *p2, *pnew;
4409Sbill 	register int t;
4419Sbill 	int s;
4429Sbill 
4433446Sroot /* DEBUGGING CODE */
4443446Sroot 	int ttrstrt();
4453446Sroot 
4463446Sroot 	if (fun == ttrstrt && arg == 0)
4473446Sroot 		panic("timeout ttrstr arg");
4483446Sroot /* END DEBUGGING CODE */
4499Sbill 	t = tim;
4509Sbill 	s = spl7();
4513542Swnj 	pnew = callfree;
4523542Swnj 	if (pnew == NULL)
4533542Swnj 		panic("timeout table overflow");
4543542Swnj 	callfree = pnew->c_next;
4553542Swnj 	pnew->c_arg = arg;
4563542Swnj 	pnew->c_func = fun;
4573542Swnj 	for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
4583542Swnj 		t -= p2->c_time;
4593542Swnj 	p1->c_next = pnew;
4603542Swnj 	pnew->c_next = p2;
4613542Swnj 	pnew->c_time = t;
4623542Swnj 	if (p2)
4633542Swnj 		p2->c_time -= t;
4649Sbill 	splx(s);
4659Sbill }
466