xref: /csrg-svn/sys/kern/kern_clock.c (revision 9751)
1*9751Ssam /*	kern_clock.c	4.48	82/12/17	*/
29Sbill 
3*9751Ssam #include "../machine/reg.h"
4*9751Ssam #include "../machine/psl.h"
5*9751Ssam 
69Sbill #include "../h/param.h"
79Sbill #include "../h/systm.h"
8329Sbill #include "../h/dk.h"
92768Swnj #include "../h/callout.h"
109Sbill #include "../h/dir.h"
119Sbill #include "../h/user.h"
128028Sroot #include "../h/kernel.h"
139Sbill #include "../h/proc.h"
149Sbill #include "../h/vm.h"
159Sbill #include "../h/text.h"
167490Skre #ifdef MUSH
177490Skre #include "../h/quota.h"
187490Skre #include "../h/share.h"
197490Skre #endif
209Sbill 
21*9751Ssam #ifdef vax
22*9751Ssam #include "../vax/mtpr.h"
23*9751Ssam #endif
24*9751Ssam 
25*9751Ssam #
268124Sroot /*
278124Sroot  * Clock handling routines.
288124Sroot  *
298124Sroot  * This code is written for a machine with only one interval timer,
308124Sroot  * and does timing and resource utilization estimation statistically
318124Sroot  * based on the state of the machine hz times a second.  A machine
328124Sroot  * with proper clocks (running separately in user state, system state,
338124Sroot  * interrupt state and idle state) as well as a time-of-day clock
348124Sroot  * would allow a non-approximate implementation.
358124Sroot  */
361559Sbill 
378124Sroot /*
388124Sroot  * TODO:
398124Sroot  *	* Keep more accurate statistics by simulating good interval timers.
408124Sroot  *	* Use the time-of-day clock on the VAX to keep more accurate time
418124Sroot  *	  than is possible by repeated use of the interval timer.
428124Sroot  *	* Allocate more timeout table slots when table overflows.
438124Sroot  */
449Sbill 
458124Sroot /* bump a timeval by a small number of usec's */
468124Sroot #define	bumptime(tp, usec) \
478124Sroot 	(tp)->tv_usec += usec; \
488097Sroot 	if ((tp)->tv_usec >= 1000000) { \
498097Sroot 		(tp)->tv_usec -= 1000000; \
508097Sroot 		(tp)->tv_sec++; \
518097Sroot 	}
525247Sroot 
538124Sroot /*
548124Sroot  * The (single) hardware interval timer.
558124Sroot  * We update the events relating to real time, and then
568124Sroot  * make a gross assumption: that the system has been in the
578124Sroot  * state it is in (user state, kernel state, interrupt state,
588124Sroot  * or idle state) for the entire last time interval, and
598124Sroot  * update statistics accordingly.
608124Sroot  */
612609Swnj /*ARGSUSED*/
628965Sroot #ifdef vax
632442Swnj hardclock(pc, ps)
642450Swnj 	caddr_t pc;
658944Sroot 	int ps;
669Sbill {
678944Sroot #endif
688965Sroot #ifdef sun
698944Sroot hardclock(regs)
708944Sroot 	struct regs regs;
718944Sroot {
728944Sroot 	int ps = regs.r_sr;
738944Sroot 	caddr_t pc = (caddr_t)regs.r_pc;
748944Sroot #endif
752768Swnj 	register struct callout *p1;
768097Sroot 	register struct proc *p;
772442Swnj 	register int s, cpstate;
789Sbill 
79*9751Ssam #ifdef sun
80*9751Ssam 	if (USERMODE(ps))		/* aston needs ar0 */
81*9751Ssam 		u.u_ar0 = &regs.r_r0;
82*9751Ssam #endif
838124Sroot 	/*
848124Sroot 	 * Update real-time timeout queue.
858124Sroot 	 * At front of queue are some number of events which are ``due''.
868124Sroot 	 * The time to these is <= 0 and if negative represents the
878124Sroot 	 * number of ticks which have passed since it was supposed to happen.
888124Sroot 	 * The rest of the q elements (times > 0) are events yet to happen,
898124Sroot 	 * where the time for each is given as a delta from the previous.
908124Sroot 	 * Decrementing just the first of these serves to decrement the time
918124Sroot 	 * to all events.
928124Sroot 	 */
933542Swnj 	for (p1 = calltodo.c_next; p1 && p1->c_time <= 0; p1 = p1->c_next)
948112Sroot 		--p1->c_time;
953542Swnj 	if (p1)
968112Sroot 		--p1->c_time;
97138Sbill 
988124Sroot 	/*
998124Sroot 	 * If the cpu is currently scheduled to a process, then
1008124Sroot 	 * charge it with resource utilization for a tick, updating
1018124Sroot 	 * statistics which run in (user+system) virtual time,
1028124Sroot 	 * such as the cpu time limit and profiling timers.
1038124Sroot 	 * This assumes that the current process has been running
1048124Sroot 	 * the entire last tick.
1058124Sroot 	 */
1069Sbill 	if (!noproc) {
1079Sbill 		s = u.u_procp->p_rssize;
1088097Sroot 		u.u_ru.ru_idrss += s; u.u_ru.ru_isrss += 0;	/* XXX */
1099Sbill 		if (u.u_procp->p_textp) {
1109Sbill 			register int xrss = u.u_procp->p_textp->x_rssize;
1119Sbill 
1129Sbill 			s += xrss;
1138028Sroot 			u.u_ru.ru_ixrss += xrss;
1149Sbill 		}
1158028Sroot 		if (s > u.u_ru.ru_maxrss)
1168028Sroot 			u.u_ru.ru_maxrss = s;
1178028Sroot 		if ((u.u_ru.ru_utime.tv_sec+u.u_ru.ru_stime.tv_sec+1) >
1188028Sroot 		    u.u_rlimit[RLIMIT_CPU].rlim_cur) {
119375Sbill 			psignal(u.u_procp, SIGXCPU);
1208028Sroot 			if (u.u_rlimit[RLIMIT_CPU].rlim_cur <
1218028Sroot 			    u.u_rlimit[RLIMIT_CPU].rlim_max)
1228028Sroot 				u.u_rlimit[RLIMIT_CPU].rlim_cur += 5;
123375Sbill 		}
1248097Sroot 		if (timerisset(&u.u_timer[ITIMER_PROF].it_value) &&
1258097Sroot 		    itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0)
1268097Sroot 			psignal(u.u_procp, SIGPROF);
1279Sbill 	}
1288097Sroot 
1298124Sroot 	/*
1308124Sroot 	 * Charge the time out based on the mode the cpu is in.
1318124Sroot 	 * Here again we fudge for the lack of proper interval timers
1328124Sroot 	 * assuming that the current state has been around at least
1338124Sroot 	 * one tick.
1348124Sroot 	 */
1359Sbill 	if (USERMODE(ps)) {
1368124Sroot 		/*
1378124Sroot 		 * CPU was in user state.  Increment
1388124Sroot 		 * user time counter, and process process-virtual time
1399604Ssam 		 * interval timer.
1408124Sroot 		 */
1418124Sroot 		bumptime(&u.u_ru.ru_utime, tick);
1428097Sroot 		if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) &&
1438097Sroot 		    itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0)
1448097Sroot 			psignal(u.u_procp, SIGVTALRM);
1458028Sroot 		if (u.u_procp->p_nice > NZERO)
146305Sbill 			cpstate = CP_NICE;
147305Sbill 		else
148305Sbill 			cpstate = CP_USER;
1499Sbill 	} else {
1508124Sroot 		/*
1518124Sroot 		 * CPU was in system state.  If profiling kernel
1528124Sroot 		 * increment a counter.  If no process is running
1538124Sroot 		 * then this is a system tick if we were running
1548124Sroot 		 * at a non-zero IPL (in a driver).  If a process is running,
1558124Sroot 		 * then we charge it with system time even if we were
1568124Sroot 		 * at a non-zero IPL, since the system often runs
1578124Sroot 		 * this way during processing of system calls.
1588124Sroot 		 * This is approximate, but the lack of true interval
1598124Sroot 		 * timers makes doing anything else difficult.
1608124Sroot 		 */
1617388Sroot #ifdef GPROF
1627388Sroot 		int k = pc - s_lowpc;
1637388Sroot 		if (profiling < 2 && k < s_textsize)
1647388Sroot 			kcount[k / sizeof (*kcount)]++;
1654968Swnj #endif
166305Sbill 		cpstate = CP_SYS;
1677315Ssam 		if (noproc) {
1688944Sroot 			if (BASEPRI(ps))
1697315Ssam 				cpstate = CP_IDLE;
1708028Sroot 		} else {
1718124Sroot 			bumptime(&u.u_ru.ru_stime, tick);
1728028Sroot 		}
1739Sbill 	}
1748097Sroot 
1758124Sroot 	/*
1768124Sroot 	 * We maintain statistics shown by user-level statistics
1778124Sroot 	 * programs:  the amount of time in each cpu state, and
1788124Sroot 	 * the amount of time each of DK_NDRIVE ``drives'' is busy.
1798124Sroot 	 */
1801408Sbill 	cp_time[cpstate]++;
1812442Swnj 	for (s = 0; s < DK_NDRIVE; s++)
1822442Swnj 		if (dk_busy&(1<<s))
1832442Swnj 			dk_time[s]++;
1848097Sroot 
1858124Sroot 	/*
1868124Sroot 	 * We adjust the priority of the current process.
1878124Sroot 	 * The priority of a process gets worse as it accumulates
1888124Sroot 	 * CPU time.  The cpu usage estimator (p_cpu) is increased here
1898124Sroot 	 * and the formula for computing priorities (in kern_synch.c)
1908124Sroot 	 * will compute a different value each time the p_cpu increases
1918124Sroot 	 * by 4.  The cpu usage estimator ramps up quite quickly when
1928124Sroot 	 * the process is running (linearly), and decays away exponentially,
1938124Sroot 	 * at a rate which is proportionally slower when the system is
1948124Sroot 	 * busy.  The basic principal is that the system will 90% forget
1958124Sroot 	 * that a process used a lot of CPU time in 5*loadav seconds.
1968124Sroot 	 * This causes the system to favor processes which haven't run
1978124Sroot 	 * much recently, and to round-robin among other processes.
1988124Sroot 	 */
1999Sbill 	if (!noproc) {
2008097Sroot 		p = u.u_procp;
2018097Sroot 		p->p_cpticks++;
2028097Sroot 		if (++p->p_cpu == 0)
2038097Sroot 			p->p_cpu--;
2047490Skre #ifdef MUSH
2058097Sroot 		p->p_quota->q_cost += (p->p_nice > NZERO ?
2068097Sroot 		    (shconsts.sc_tic * ((2*NZERO)-p->p_nice)) / NZERO :
2077490Skre 		    shconsts.sc_tic) * (((int)avenrun[0]+2)/3);
2087490Skre #endif
2098124Sroot 		if ((p->p_cpu&3) == 0) {
2108097Sroot 			(void) setpri(p);
2118097Sroot 			if (p->p_pri >= PUSER)
2128097Sroot 				p->p_pri = p->p_usrpri;
2139Sbill 		}
2149Sbill 	}
2158124Sroot 
2168124Sroot 	/*
2178124Sroot 	 * Increment the time-of-day, and schedule
2188124Sroot 	 * processing of the callouts at a very low cpu priority,
2198124Sroot 	 * so we don't keep the relatively high clock interrupt
2208124Sroot 	 * priority any longer than necessary.
2218124Sroot 	 */
2228124Sroot 	bumptime(&time, tick);
2232442Swnj 	setsoftclock();
2242442Swnj }
2252442Swnj 
2268124Sroot /*
2278124Sroot  * Software priority level clock interrupt.
2288124Sroot  * Run periodic events from timeout queue.
2298124Sroot  */
2302609Swnj /*ARGSUSED*/
2318965Sroot #ifdef vax
2322442Swnj softclock(pc, ps)
2332450Swnj 	caddr_t pc;
2348944Sroot 	int ps;
2352442Swnj {
2368944Sroot #endif
2378965Sroot #ifdef sun
238*9751Ssam softclock()
2398944Sroot {
240*9751Ssam 	int ps = u.u_ar0[PS];
241*9751Ssam 	caddr_t pc = (caddr_t)u.u_ar0[PC];
2428944Sroot #endif
2432442Swnj 
2448097Sroot 	for (;;) {
2458124Sroot 		register struct callout *p1;
2468124Sroot 		register caddr_t arg;
2478124Sroot 		register int (*func)();
2488124Sroot 		register int a, s;
2498124Sroot 
2508097Sroot 		s = spl7();
2518097Sroot 		if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
2528097Sroot 			splx(s);
2538097Sroot 			break;
2542442Swnj 		}
2558124Sroot 		arg = p1->c_arg; func = p1->c_func; a = p1->c_time;
2568097Sroot 		calltodo.c_next = p1->c_next;
2578097Sroot 		p1->c_next = callfree;
2588097Sroot 		callfree = p1;
2599157Ssam 		splx(s);
2608112Sroot 		(*func)(arg, a);
2612442Swnj 	}
2629604Ssam 	/*
2639604Ssam 	 * If trapped user-mode, give it a profiling tick.
2649604Ssam 	 */
2659604Ssam 	if (USERMODE(ps) && u.u_prof.pr_scale) {
2669604Ssam 		u.u_procp->p_flag |= SOWEUPC;
2679604Ssam 		aston();
2689604Ssam 	}
2699Sbill }
2709Sbill 
2719Sbill /*
2728097Sroot  * Arrange that (*fun)(arg) is called in tim/hz seconds.
2739Sbill  */
2749Sbill timeout(fun, arg, tim)
2752450Swnj 	int (*fun)();
2762450Swnj 	caddr_t arg;
2778097Sroot 	int tim;
2789Sbill {
2793542Swnj 	register struct callout *p1, *p2, *pnew;
2809Sbill 	register int t;
2819Sbill 	int s;
2829Sbill 
2839Sbill 	t = tim;
2849Sbill 	s = spl7();
2853542Swnj 	pnew = callfree;
2863542Swnj 	if (pnew == NULL)
2873542Swnj 		panic("timeout table overflow");
2883542Swnj 	callfree = pnew->c_next;
2893542Swnj 	pnew->c_arg = arg;
2903542Swnj 	pnew->c_func = fun;
2913542Swnj 	for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
2929742Ssam 		if (p2->c_time > 0)
2939742Ssam 			t -= p2->c_time;
2943542Swnj 	p1->c_next = pnew;
2953542Swnj 	pnew->c_next = p2;
2963542Swnj 	pnew->c_time = t;
2973542Swnj 	if (p2)
2983542Swnj 		p2->c_time -= t;
2999Sbill 	splx(s);
3009Sbill }
3017305Ssam 
3027305Ssam /*
3037305Ssam  * untimeout is called to remove a function timeout call
3047305Ssam  * from the callout structure.
3057305Ssam  */
3068097Sroot untimeout(fun, arg)
3077305Ssam 	int (*fun)();
3087305Ssam 	caddr_t arg;
3097305Ssam {
3107305Ssam 	register struct callout *p1, *p2;
3117305Ssam 	register int s;
3127305Ssam 
3137305Ssam 	s = spl7();
3147305Ssam 	for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) {
3157305Ssam 		if (p2->c_func == fun && p2->c_arg == arg) {
3168112Sroot 			if (p2->c_next && p2->c_time > 0)
3177305Ssam 				p2->c_next->c_time += p2->c_time;
3187305Ssam 			p1->c_next = p2->c_next;
3197305Ssam 			p2->c_next = callfree;
3207305Ssam 			callfree = p2;
3217305Ssam 			break;
3227305Ssam 		}
3237305Ssam 	}
3247305Ssam 	splx(s);
3257305Ssam }
3268112Sroot 
3278124Sroot /*
3288124Sroot  * Compute number of hz until specified time.
3298124Sroot  * Used to compute third argument to timeout() from an
3308124Sroot  * absolute time.
3318124Sroot  */
3328112Sroot hzto(tv)
3338112Sroot 	struct timeval *tv;
3348112Sroot {
3358124Sroot 	register long ticks;
3368124Sroot 	register long sec;
3378112Sroot 	int s = spl7();
3388112Sroot 
3398124Sroot 	/*
3408124Sroot 	 * If number of milliseconds will fit in 32 bit arithmetic,
3418124Sroot 	 * then compute number of milliseconds to time and scale to
3428124Sroot 	 * ticks.  Otherwise just compute number of hz in time, rounding
3438124Sroot 	 * times greater than representible to maximum value.
3448124Sroot 	 *
3458124Sroot 	 * Delta times less than 25 days can be computed ``exactly''.
3468124Sroot 	 * Maximum value for any timeout in 10ms ticks is 250 days.
3478124Sroot 	 */
3488124Sroot 	sec = tv->tv_sec - time.tv_sec;
3498124Sroot 	if (sec <= 0x7fffffff / 1000 - 1000)
3508124Sroot 		ticks = ((tv->tv_sec - time.tv_sec) * 1000 +
3518124Sroot 			(tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000);
3528124Sroot 	else if (sec <= 0x7fffffff / hz)
3538124Sroot 		ticks = sec * hz;
3548124Sroot 	else
3558124Sroot 		ticks = 0x7fffffff;
3568112Sroot 	splx(s);
3578112Sroot 	return (ticks);
3588112Sroot }
359