xref: /csrg-svn/sys/kern/kern_clock.c (revision 1559)
1*1559Sbill /*	10/14/12	3.22	kern_clock.c	*/
29Sbill 
39Sbill #include "../h/param.h"
49Sbill #include "../h/systm.h"
5329Sbill #include "../h/dk.h"
69Sbill #include "../h/callo.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"
199Sbill 
20*1559Sbill #include "../conf/dh.h"
21*1559Sbill #include "../conf/dz.h"
22*1559Sbill 
239Sbill #define	SCHMAG	9/10
249Sbill 
251399Sbill /*
261399Sbill  * Constant for decay filter for cpu usage.
271399Sbill  */
281408Sbill double	ccpu = 0.95122942450071400909;		/* exp(-1/20) */
299Sbill 
309Sbill /*
311399Sbill  * Clock is called straight from
329Sbill  * the real time clock interrupt.
339Sbill  *
349Sbill  * Functions:
359Sbill  *	implement callouts
369Sbill  *	maintain user/system times
379Sbill  *	maintain date
389Sbill  *	profile
399Sbill  *	lightning bolt wakeup (every second)
409Sbill  *	alarm clock signals
419Sbill  *	jab the scheduler
429Sbill  */
439Sbill #ifdef KPROF
44104Sbill unsigned short kcount[20000];
459Sbill #endif
469Sbill 
47115Sbill /*
48115Sbill  * We handle regular calls to the dh and dz silo input processors
49115Sbill  * without using timeouts to save a little time.
50115Sbill  */
51142Sbill int	rintvl = 0;		/* every 1/60'th of sec check receivers */
52115Sbill int	rcnt;
53115Sbill 
549Sbill clock(pc, ps)
559Sbill caddr_t pc;
569Sbill {
579Sbill 	register struct callo *p1, *p2;
589Sbill 	register struct proc *pp;
599Sbill 	register int s;
601408Sbill 	int a, cpstate, i;
619Sbill 
629Sbill 	/*
639Sbill 	 * reprime clock
649Sbill 	 */
659Sbill 	clkreld();
669Sbill 
679Sbill 	/*
689Sbill 	 * callouts
699Sbill 	 * else update first non-zero time
709Sbill 	 */
719Sbill 
729Sbill 	if(callout[0].c_func == NULL)
739Sbill 		goto out;
749Sbill 	p2 = &callout[0];
759Sbill 	while(p2->c_time<=0 && p2->c_func!=NULL)
769Sbill 		p2++;
779Sbill 	p2->c_time--;
789Sbill 
799Sbill 	/*
809Sbill 	 * if ps is high, just return
819Sbill 	 */
829Sbill 	if (BASEPRI(ps))
839Sbill 		goto out;
849Sbill 
859Sbill 	/*
869Sbill 	 * callout
879Sbill 	 */
889Sbill 
899Sbill 	if(callout[0].c_time <= 0) {
909Sbill 		p1 = &callout[0];
919Sbill 		while(p1->c_func != 0 && p1->c_time <= 0) {
929Sbill 			(*p1->c_func)(p1->c_arg);
939Sbill 			p1++;
949Sbill 		}
959Sbill 		p2 = &callout[0];
969Sbill 		while(p2->c_func = p1->c_func) {
979Sbill 			p2->c_time = p1->c_time;
989Sbill 			p2->c_arg = p1->c_arg;
999Sbill 			p1++;
1009Sbill 			p2++;
1019Sbill 		}
1029Sbill 	}
1039Sbill 
1049Sbill 	/*
1059Sbill 	 * lightning bolt time-out
1069Sbill 	 * and time of day
1079Sbill 	 */
1089Sbill out:
109138Sbill 
110138Sbill 	/*
111138Sbill 	 * In order to not take input character interrupts to use
112138Sbill 	 * the input silo on DZ's we have to guarantee to echo
113138Sbill 	 * characters regularly.  This means that we have to
114138Sbill 	 * call the timer routines predictably.  Since blocking
115138Sbill 	 * in these routines is at spl5(), we have to make spl5()
116138Sbill 	 * really spl6() blocking off the clock to put this code
117138Sbill 	 * here.  Note also that it is critical that we run spl5()
118138Sbill 	 * (i.e. really spl6()) in the receiver interrupt routines
119138Sbill 	 * so we can't enter them recursively and transpose characters.
120138Sbill 	 */
121138Sbill 	if (rcnt >= rintvl) {
122*1559Sbill #if NDH11 > 0
123138Sbill 		dhtimer();
124*1559Sbill #endif
125*1559Sbill #if NDZ11 > 0
126138Sbill 		dztimer();
127*1559Sbill #endif
128138Sbill 		rcnt = 0;
129138Sbill 	} else
130138Sbill 		rcnt++;
1319Sbill 	if (!noproc) {
1329Sbill 		s = u.u_procp->p_rssize;
1339Sbill 		u.u_vm.vm_idsrss += s;
1349Sbill 		if (u.u_procp->p_textp) {
1359Sbill 			register int xrss = u.u_procp->p_textp->x_rssize;
1369Sbill 
1379Sbill 			s += xrss;
1389Sbill 			u.u_vm.vm_ixrss += xrss;
1399Sbill 		}
1409Sbill 		if (s > u.u_vm.vm_maxrss)
1419Sbill 			u.u_vm.vm_maxrss = s;
142375Sbill 		if ((u.u_vm.vm_utime+u.u_vm.vm_stime+1)/HZ > u.u_limit[LIM_CPU]) {
143375Sbill 			psignal(u.u_procp, SIGXCPU);
144375Sbill 			if (u.u_limit[LIM_CPU] < INFINITY - 5)
145375Sbill 				u.u_limit[LIM_CPU] += 5;
146375Sbill 		}
1479Sbill 	}
1489Sbill 	if (USERMODE(ps)) {
1499Sbill 		u.u_vm.vm_utime++;
1509Sbill 		if(u.u_procp->p_nice > NZERO)
151305Sbill 			cpstate = CP_NICE;
152305Sbill 		else
153305Sbill 			cpstate = CP_USER;
1549Sbill 	} else {
155305Sbill 		cpstate = CP_SYS;
1569Sbill 		if (noproc)
157305Sbill 			cpstate = CP_IDLE;
1589Sbill 		else
1599Sbill 			u.u_vm.vm_stime++;
1609Sbill 	}
1611408Sbill 	cp_time[cpstate]++;
1621408Sbill 	for (i = 0; i < DK_NDRIVE; i++)
1631408Sbill 		if (dk_busy&(1<<i))
1641408Sbill 			dk_time[i]++;
1659Sbill 	if (!noproc) {
1669Sbill 		pp = u.u_procp;
1671399Sbill 		pp->p_cpticks++;
1689Sbill 		if(++pp->p_cpu == 0)
1699Sbill 			pp->p_cpu--;
1709Sbill 		if(pp->p_cpu % 16 == 0) {
171125Sbill 			(void) setpri(pp);
1729Sbill 			if (pp->p_pri >= PUSER)
1739Sbill 				pp->p_pri = pp->p_usrpri;
1749Sbill 		}
1759Sbill 	}
1769Sbill 	++lbolt;
1779Sbill 	if (lbolt % (HZ/4) == 0) {
1789Sbill 		vmpago();
1799Sbill 		runrun++;
1809Sbill 	}
1819Sbill 	if (lbolt >= HZ) {
182877Sbill 		extern int hangcnt;
183877Sbill 
1849Sbill 		if (BASEPRI(ps))
1859Sbill 			return;
1869Sbill 		lbolt -= HZ;
1879Sbill 		++time;
188125Sbill 		(void) spl1();
189877Sbill 		/*
190877Sbill 		 * machdep.c:unhang uses hangcnt to make sure uba
191877Sbill 		 * doesn't forget to interrupt (this has been observed).
192877Sbill 		 * This prevents an accumulation of < 5 second uba failures
193877Sbill 		 * from summing to a uba reset.
194877Sbill 		 */
195877Sbill 		if (hangcnt)
196877Sbill 			hangcnt--;
1979Sbill 		runrun++;
1989Sbill 		wakeup((caddr_t)&lbolt);
1999Sbill 		for(pp = &proc[0]; pp < &proc[NPROC]; pp++)
200928Sbill 		if (pp->p_stat && pp->p_stat!=SZOMB) {
2019Sbill 			if(pp->p_time != 127)
2029Sbill 				pp->p_time++;
2039Sbill 			if(pp->p_clktim)
2049Sbill 				if(--pp->p_clktim == 0)
205101Sbill 					if (pp->p_flag & STIMO) {
206101Sbill 						s = spl6();
207204Sbill 						switch (pp->p_stat) {
208204Sbill 
209204Sbill 						case SSLEEP:
210101Sbill 							setrun(pp);
211204Sbill 							break;
212204Sbill 
213204Sbill 						case SSTOP:
214204Sbill 							unsleep(pp);
215204Sbill 							break;
216204Sbill 						}
217101Sbill 						pp->p_flag &= ~STIMO;
218101Sbill 						splx(s);
219101Sbill 					} else
220166Sbill 						psignal(pp, SIGALRM);
2219Sbill 			if(pp->p_stat==SSLEEP||pp->p_stat==SSTOP)
2229Sbill 				if (pp->p_slptime != 127)
2239Sbill 					pp->p_slptime++;
2241399Sbill 			if (pp->p_flag&SLOAD)
2251399Sbill 				pp->p_pctcpu = ccpu * pp->p_pctcpu +
2261399Sbill 				    (1.0 - ccpu) * (pp->p_cpticks/(float)HZ);
2271399Sbill 			pp->p_cpticks = 0;
2289Sbill 			a = (pp->p_cpu & 0377)*SCHMAG + pp->p_nice - NZERO;
2299Sbill 			if(a < 0)
2309Sbill 				a = 0;
2319Sbill 			if(a > 255)
2329Sbill 				a = 255;
2339Sbill 			pp->p_cpu = a;
234125Sbill 			(void) setpri(pp);
2359Sbill 			s = spl6();
2369Sbill 			if(pp->p_pri >= PUSER) {
2379Sbill 				if ((pp != u.u_procp || noproc) &&
2389Sbill 				    pp->p_stat == SRUN &&
2399Sbill 				    (pp->p_flag & SLOAD) &&
2409Sbill 				    pp->p_pri != pp->p_usrpri) {
2419Sbill 					remrq(pp);
2429Sbill 					pp->p_pri = pp->p_usrpri;
2439Sbill 					setrq(pp);
2449Sbill 				} else
2459Sbill 					pp->p_pri = pp->p_usrpri;
2469Sbill 			}
2479Sbill 			splx(s);
2489Sbill 		}
2499Sbill 		vmmeter();
2509Sbill 		if(runin!=0) {
2519Sbill 			runin = 0;
2529Sbill 			wakeup((caddr_t)&runin);
2539Sbill 		}
2549Sbill 		/*
2559Sbill 		 * If there are pages that have been cleaned,
2569Sbill 		 * jolt the pageout daemon to process them.
2579Sbill 		 * We do this here so that these pages will be
2589Sbill 		 * freed if there is an abundance of memory and the
2599Sbill 		 * daemon would not be awakened otherwise.
2609Sbill 		 */
2619Sbill 		if (bclnlist != NULL)
2629Sbill 			wakeup((caddr_t)&proc[2]);
2639Sbill 		if (USERMODE(ps)) {
2649Sbill 			pp = u.u_procp;
265362Sbill #ifdef ERNIE
2669Sbill 			if (pp->p_uid)
2679Sbill 				if (pp->p_nice == NZERO && u.u_vm.vm_utime > 600 * HZ)
2689Sbill 					pp->p_nice = NZERO+4;
269125Sbill 			(void) setpri(pp);
2709Sbill 			pp->p_pri = pp->p_usrpri;
271362Sbill #endif
2729Sbill 		}
2739Sbill 	}
274277Sbill 	if (!BASEPRI(ps))
275277Sbill 		unhang();
2769Sbill 	if (USERMODE(ps)) {
2779Sbill 		/*
2789Sbill 		 * We do this last since it
2799Sbill 		 * may block on a page fault in user space.
2809Sbill 		 */
2819Sbill 		if (u.u_prof.pr_scale)
2829Sbill 			addupc(pc, &u.u_prof, 1);
2839Sbill 	}
2849Sbill #ifdef KPROF
2859Sbill 	else if (!noproc) {
286104Sbill 		register int indx = ((int)pc & 0x7fffffff) / 4;
2879Sbill 
2889Sbill 		if (indx >= 0 && indx < 20000)
289104Sbill 			if (++kcount[indx] == 0)
290104Sbill 				--kcount[indx];
2919Sbill 	}
2929Sbill #endif
2939Sbill }
2949Sbill 
2959Sbill /*
2969Sbill  * timeout is called to arrange that
2979Sbill  * fun(arg) is called in tim/HZ seconds.
2989Sbill  * An entry is sorted into the callout
2999Sbill  * structure. The time in each structure
3009Sbill  * entry is the number of HZ's more
3019Sbill  * than the previous entry.
3029Sbill  * In this way, decrementing the
3039Sbill  * first entry has the effect of
3049Sbill  * updating all entries.
3059Sbill  *
3069Sbill  * The panic is there because there is nothing
3079Sbill  * intelligent to be done if an entry won't fit.
3089Sbill  */
3099Sbill timeout(fun, arg, tim)
3109Sbill int (*fun)();
3119Sbill caddr_t arg;
3129Sbill {
3139Sbill 	register struct callo *p1, *p2;
3149Sbill 	register int t;
3159Sbill 	int s;
3169Sbill 
3179Sbill 	t = tim;
3189Sbill 	p1 = &callout[0];
3199Sbill 	s = spl7();
3209Sbill 	while(p1->c_func != 0 && p1->c_time <= t) {
3219Sbill 		t -= p1->c_time;
3229Sbill 		p1++;
3239Sbill 	}
3249Sbill 	if (p1 >= &callout[NCALL-1])
3259Sbill 		panic("Timeout table overflow");
3269Sbill 	p1->c_time -= t;
3279Sbill 	p2 = p1;
3289Sbill 	while(p2->c_func != 0)
3299Sbill 		p2++;
3309Sbill 	while(p2 >= p1) {
3319Sbill 		(p2+1)->c_time = p2->c_time;
3329Sbill 		(p2+1)->c_func = p2->c_func;
3339Sbill 		(p2+1)->c_arg = p2->c_arg;
3349Sbill 		p2--;
3359Sbill 	}
3369Sbill 	p1->c_time = t;
3379Sbill 	p1->c_func = fun;
3389Sbill 	p1->c_arg = arg;
3399Sbill 	splx(s);
3409Sbill }
341