xref: /csrg-svn/sys/kern/kern_clock.c (revision 40674)
123366Smckusick /*
229086Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323366Smckusick  * All rights reserved.  The Berkeley software License Agreement
423366Smckusick  * specifies the terms and conditions for redistribution.
523366Smckusick  *
6*40674Smarc  *	@(#)kern_clock.c	7.6 (Berkeley) 04/02/90
723366Smckusick  */
89Sbill 
917088Sbloom #include "param.h"
1017088Sbloom #include "systm.h"
1129946Skarels #include "dkstat.h"
1217088Sbloom #include "callout.h"
1317088Sbloom #include "user.h"
1417088Sbloom #include "kernel.h"
1517088Sbloom #include "proc.h"
1617088Sbloom #include "vm.h"
1717088Sbloom #include "text.h"
189Sbill 
1937493Smckusick #include "machine/reg.h"
2037493Smckusick #include "machine/psl.h"
2135406Skarels 
2229946Skarels #if defined(vax) || defined(tahoe)
2337493Smckusick #include "machine/mtpr.h"
2437493Smckusick #include "machine/clock.h"
259751Ssam #endif
269751Ssam 
2710291Smckusick #ifdef GPROF
2817088Sbloom #include "gprof.h"
2910291Smckusick #endif
3010291Smckusick 
318124Sroot /*
328124Sroot  * Clock handling routines.
338124Sroot  *
3411392Ssam  * This code is written to operate with two timers which run
3511392Ssam  * independently of each other. The main clock, running at hz
3611392Ssam  * times per second, is used to do scheduling and timeout calculations.
3711392Ssam  * The second timer does resource utilization estimation statistically
3811392Ssam  * based on the state of the machine phz times a second. Both functions
3911392Ssam  * can be performed by a single clock (ie hz == phz), however the
4011392Ssam  * statistics will be much more prone to errors. Ideally a machine
4111392Ssam  * would have separate clocks measuring time spent in user state, system
4211392Ssam  * state, interrupt state, and idle state. These clocks would allow a non-
4311392Ssam  * approximate measure of resource utilization.
448124Sroot  */
451559Sbill 
468124Sroot /*
478124Sroot  * TODO:
4812747Ssam  *	time of day, system/user timing, timeouts, profiling on separate timers
4912747Ssam  *	allocate more timeout table slots when table overflows.
508124Sroot  */
5126265Skarels 
5217007Smckusick /*
5317007Smckusick  * Bump a timeval by a small number of usec's.
5417007Smckusick  */
5517007Smckusick #define BUMPTIME(t, usec) { \
5617007Smckusick 	register struct timeval *tp = (t); \
5717007Smckusick  \
5817007Smckusick 	tp->tv_usec += (usec); \
5917007Smckusick 	if (tp->tv_usec >= 1000000) { \
6017007Smckusick 		tp->tv_usec -= 1000000; \
6117007Smckusick 		tp->tv_sec++; \
6217007Smckusick 	} \
6317007Smckusick }
6417007Smckusick 
658124Sroot /*
6611392Ssam  * The hz hardware interval timer.
6711392Ssam  * We update the events relating to real time.
6811392Ssam  * If this timer is also being used to gather statistics,
6911392Ssam  * we run through the statistics gathering routine as well.
708124Sroot  */
712609Swnj /*ARGSUSED*/
722442Swnj hardclock(pc, ps)
732450Swnj 	caddr_t pc;
748944Sroot 	int ps;
759Sbill {
762768Swnj 	register struct callout *p1;
77*40674Smarc 	register struct proc *p = u.u_procp;
7824524Sbloom 	register int s;
7916172Skarels 	int needsoft = 0;
8028947Skarels 	extern int tickdelta;
8128947Skarels 	extern long timedelta;
829Sbill 
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 	 */
9312747Ssam 	p1 = calltodo.c_next;
9412747Ssam 	while (p1) {
9512747Ssam 		if (--p1->c_time > 0)
9612747Ssam 			break;
9716172Skarels 		needsoft = 1;
9812747Ssam 		if (p1->c_time == 0)
9912747Ssam 			break;
10012747Ssam 		p1 = p1->c_next;
10112747Ssam 	}
102138Sbill 
1038124Sroot 	/*
1048124Sroot 	 * Charge the time out based on the mode the cpu is in.
1058124Sroot 	 * Here again we fudge for the lack of proper interval timers
1068124Sroot 	 * assuming that the current state has been around at least
1078124Sroot 	 * one tick.
1088124Sroot 	 */
1099Sbill 	if (USERMODE(ps)) {
11016172Skarels 		if (u.u_prof.pr_scale)
11116172Skarels 			needsoft = 1;
1128124Sroot 		/*
1138124Sroot 		 * CPU was in user state.  Increment
1148124Sroot 		 * user time counter, and process process-virtual time
1159604Ssam 		 * interval timer.
1168124Sroot 		 */
117*40674Smarc 		BUMPTIME(&p->p_utime, tick);
1188097Sroot 		if (timerisset(&u.u_timer[ITIMER_VIRTUAL].it_value) &&
1198097Sroot 		    itimerdecr(&u.u_timer[ITIMER_VIRTUAL], tick) == 0)
120*40674Smarc 			psignal(p, SIGVTALRM);
1219Sbill 	} else {
1228124Sroot 		/*
12324524Sbloom 		 * CPU was in system state.
1248124Sroot 		 */
12526265Skarels 		if (!noproc)
126*40674Smarc 			BUMPTIME(&p->p_stime, tick);
1279Sbill 	}
1288097Sroot 
1298124Sroot 	/*
13010388Ssam 	 * If the cpu is currently scheduled to a process, then
13110388Ssam 	 * charge it with resource utilization for a tick, updating
13210388Ssam 	 * statistics which run in (user+system) virtual time,
13310388Ssam 	 * such as the cpu time limit and profiling timers.
13410388Ssam 	 * This assumes that the current process has been running
13510388Ssam 	 * the entire last tick.
13610388Ssam 	 */
13718585Skarels 	if (noproc == 0) {
138*40674Smarc 		if ((p->p_utime.tv_sec+p->p_stime.tv_sec+1) >
13910388Ssam 		    u.u_rlimit[RLIMIT_CPU].rlim_cur) {
140*40674Smarc 			psignal(p, SIGXCPU);
14110388Ssam 			if (u.u_rlimit[RLIMIT_CPU].rlim_cur <
14210388Ssam 			    u.u_rlimit[RLIMIT_CPU].rlim_max)
14310388Ssam 				u.u_rlimit[RLIMIT_CPU].rlim_cur += 5;
14410388Ssam 		}
14510388Ssam 		if (timerisset(&u.u_timer[ITIMER_PROF].it_value) &&
14610388Ssam 		    itimerdecr(&u.u_timer[ITIMER_PROF], tick) == 0)
147*40674Smarc 			psignal(p, SIGPROF);
148*40674Smarc 		s = p->p_rssize;
14926265Skarels 		u.u_ru.ru_idrss += s;
15026265Skarels #ifdef notdef
15126265Skarels 		u.u_ru.ru_isrss += 0;		/* XXX (haven't got this) */
15226265Skarels #endif
153*40674Smarc 		if (p->p_textp) {
154*40674Smarc 			register int xrss = p->p_textp->x_rssize;
15510388Ssam 
15610388Ssam 			s += xrss;
15710388Ssam 			u.u_ru.ru_ixrss += xrss;
15810388Ssam 		}
15910388Ssam 		if (s > u.u_ru.ru_maxrss)
16010388Ssam 			u.u_ru.ru_maxrss = s;
16110388Ssam 	}
16210388Ssam 
16310388Ssam 	/*
1648124Sroot 	 * We adjust the priority of the current process.
1658124Sroot 	 * The priority of a process gets worse as it accumulates
1668124Sroot 	 * CPU time.  The cpu usage estimator (p_cpu) is increased here
1678124Sroot 	 * and the formula for computing priorities (in kern_synch.c)
1688124Sroot 	 * will compute a different value each time the p_cpu increases
1698124Sroot 	 * by 4.  The cpu usage estimator ramps up quite quickly when
1708124Sroot 	 * the process is running (linearly), and decays away exponentially,
1718124Sroot 	 * at a rate which is proportionally slower when the system is
1728124Sroot 	 * busy.  The basic principal is that the system will 90% forget
1738124Sroot 	 * that a process used a lot of CPU time in 5*loadav seconds.
1748124Sroot 	 * This causes the system to favor processes which haven't run
1758124Sroot 	 * much recently, and to round-robin among other processes.
1768124Sroot 	 */
1779Sbill 	if (!noproc) {
1788097Sroot 		p->p_cpticks++;
1798097Sroot 		if (++p->p_cpu == 0)
1808097Sroot 			p->p_cpu--;
1818124Sroot 		if ((p->p_cpu&3) == 0) {
1828097Sroot 			(void) setpri(p);
1838097Sroot 			if (p->p_pri >= PUSER)
1848097Sroot 				p->p_pri = p->p_usrpri;
1859Sbill 		}
1869Sbill 	}
1878124Sroot 
1888124Sroot 	/*
18911392Ssam 	 * If the alternate clock has not made itself known then
19011392Ssam 	 * we must gather the statistics.
19111392Ssam 	 */
19211392Ssam 	if (phz == 0)
19311392Ssam 		gatherstats(pc, ps);
19411392Ssam 
19511392Ssam 	/*
1968124Sroot 	 * Increment the time-of-day, and schedule
1978124Sroot 	 * processing of the callouts at a very low cpu priority,
1988124Sroot 	 * so we don't keep the relatively high clock interrupt
1998124Sroot 	 * priority any longer than necessary.
2008124Sroot 	 */
20128828Skarels 	if (timedelta == 0)
20217356Skarels 		BUMPTIME(&time, tick)
20317356Skarels 	else {
20417356Skarels 		register delta;
20517356Skarels 
20628828Skarels 		if (timedelta < 0) {
20728828Skarels 			delta = tick - tickdelta;
20828828Skarels 			timedelta += tickdelta;
20917356Skarels 		} else {
21028828Skarels 			delta = tick + tickdelta;
21128828Skarels 			timedelta -= tickdelta;
21217356Skarels 		}
21317356Skarels 		BUMPTIME(&time, delta);
21417356Skarels 	}
21516525Skarels 	if (needsoft) {
21616525Skarels 		if (BASEPRI(ps)) {
21716525Skarels 			/*
21816525Skarels 			 * Save the overhead of a software interrupt;
21916525Skarels 			 * it will happen as soon as we return, so do it now.
22016525Skarels 			 */
22116525Skarels 			(void) splsoftclock();
22216525Skarels 			softclock(pc, ps);
22316525Skarels 		} else
22416525Skarels 			setsoftclock();
22516525Skarels 	}
2262442Swnj }
2272442Swnj 
22815191Ssam int	dk_ndrive = DK_NDRIVE;
2298124Sroot /*
23011392Ssam  * Gather statistics on resource utilization.
23111392Ssam  *
23211392Ssam  * We make a gross assumption: that the system has been in the
23311392Ssam  * state it is in (user state, kernel state, interrupt state,
23411392Ssam  * or idle state) for the entire last time interval, and
23511392Ssam  * update statistics accordingly.
23611392Ssam  */
23712747Ssam /*ARGSUSED*/
23811392Ssam gatherstats(pc, ps)
23911392Ssam 	caddr_t pc;
24011392Ssam 	int ps;
24111392Ssam {
24226265Skarels 	register int cpstate, s;
24311392Ssam 
24411392Ssam 	/*
24511392Ssam 	 * Determine what state the cpu is in.
24611392Ssam 	 */
24711392Ssam 	if (USERMODE(ps)) {
24811392Ssam 		/*
24911392Ssam 		 * CPU was in user state.
25011392Ssam 		 */
25111392Ssam 		if (u.u_procp->p_nice > NZERO)
25211392Ssam 			cpstate = CP_NICE;
25311392Ssam 		else
25411392Ssam 			cpstate = CP_USER;
25511392Ssam 	} else {
25611392Ssam 		/*
25711392Ssam 		 * CPU was in system state.  If profiling kernel
25824524Sbloom 		 * increment a counter.  If no process is running
25924524Sbloom 		 * then this is a system tick if we were running
26024524Sbloom 		 * at a non-zero IPL (in a driver).  If a process is running,
26124524Sbloom 		 * then we charge it with system time even if we were
26224524Sbloom 		 * at a non-zero IPL, since the system often runs
26324524Sbloom 		 * this way during processing of system calls.
26424524Sbloom 		 * This is approximate, but the lack of true interval
26524524Sbloom 		 * timers makes doing anything else difficult.
26611392Ssam 		 */
26711392Ssam 		cpstate = CP_SYS;
26811392Ssam 		if (noproc && BASEPRI(ps))
26911392Ssam 			cpstate = CP_IDLE;
27011392Ssam #ifdef GPROF
27111392Ssam 		s = pc - s_lowpc;
27211392Ssam 		if (profiling < 2 && s < s_textsize)
27311392Ssam 			kcount[s / (HISTFRACTION * sizeof (*kcount))]++;
27411392Ssam #endif
27511392Ssam 	}
27611392Ssam 	/*
27711392Ssam 	 * We maintain statistics shown by user-level statistics
27811392Ssam 	 * programs:  the amount of time in each cpu state, and
27911392Ssam 	 * the amount of time each of DK_NDRIVE ``drives'' is busy.
28011392Ssam 	 */
28111392Ssam 	cp_time[cpstate]++;
28211392Ssam 	for (s = 0; s < DK_NDRIVE; s++)
28329946Skarels 		if (dk_busy&(1<<s))
28411392Ssam 			dk_time[s]++;
28511392Ssam }
28611392Ssam 
28711392Ssam /*
2888124Sroot  * Software priority level clock interrupt.
2898124Sroot  * Run periodic events from timeout queue.
2908124Sroot  */
2912609Swnj /*ARGSUSED*/
2922442Swnj softclock(pc, ps)
2932450Swnj 	caddr_t pc;
2948944Sroot 	int ps;
2952442Swnj {
2962442Swnj 
2978097Sroot 	for (;;) {
2988124Sroot 		register struct callout *p1;
2998124Sroot 		register caddr_t arg;
3008124Sroot 		register int (*func)();
3018124Sroot 		register int a, s;
3028124Sroot 
30326265Skarels 		s = splhigh();
3048097Sroot 		if ((p1 = calltodo.c_next) == 0 || p1->c_time > 0) {
3058097Sroot 			splx(s);
3068097Sroot 			break;
3072442Swnj 		}
3088124Sroot 		arg = p1->c_arg; func = p1->c_func; a = p1->c_time;
3098097Sroot 		calltodo.c_next = p1->c_next;
3108097Sroot 		p1->c_next = callfree;
3118097Sroot 		callfree = p1;
3129157Ssam 		splx(s);
3138112Sroot 		(*func)(arg, a);
3142442Swnj 	}
3159604Ssam 	/*
31613127Ssam 	 * If trapped user-mode and profiling, give it
31713127Ssam 	 * a profiling tick.
3189604Ssam 	 */
31913127Ssam 	if (USERMODE(ps)) {
32013127Ssam 		register struct proc *p = u.u_procp;
32113127Ssam 
32213127Ssam 		if (u.u_prof.pr_scale) {
32313127Ssam 			p->p_flag |= SOWEUPC;
32413127Ssam 			aston();
32513127Ssam 		}
32613127Ssam 		/*
32713127Ssam 		 * Check to see if process has accumulated
32813127Ssam 		 * more than 10 minutes of user time.  If so
32913127Ssam 		 * reduce priority to give others a chance.
33013127Ssam 		 */
33113127Ssam 		if (p->p_uid && p->p_nice == NZERO &&
332*40674Smarc 		    p->p_utime.tv_sec > 10 * 60) {
33313127Ssam 			p->p_nice = NZERO+4;
33413127Ssam 			(void) setpri(p);
33513127Ssam 			p->p_pri = p->p_usrpri;
33613127Ssam 		}
3379604Ssam 	}
3389Sbill }
3399Sbill 
3409Sbill /*
34112747Ssam  * Arrange that (*fun)(arg) is called in t/hz seconds.
34212747Ssam  */
34312747Ssam timeout(fun, arg, t)
3442450Swnj 	int (*fun)();
3452450Swnj 	caddr_t arg;
34612747Ssam 	register int t;
3479Sbill {
3483542Swnj 	register struct callout *p1, *p2, *pnew;
34926265Skarels 	register int s = splhigh();
3509Sbill 
35118282Smckusick 	if (t <= 0)
35212747Ssam 		t = 1;
3533542Swnj 	pnew = callfree;
3543542Swnj 	if (pnew == NULL)
3553542Swnj 		panic("timeout table overflow");
3563542Swnj 	callfree = pnew->c_next;
3573542Swnj 	pnew->c_arg = arg;
3583542Swnj 	pnew->c_func = fun;
3593542Swnj 	for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
3609742Ssam 		if (p2->c_time > 0)
3619742Ssam 			t -= p2->c_time;
3623542Swnj 	p1->c_next = pnew;
3633542Swnj 	pnew->c_next = p2;
3643542Swnj 	pnew->c_time = t;
3653542Swnj 	if (p2)
3663542Swnj 		p2->c_time -= t;
3679Sbill 	splx(s);
3689Sbill }
3697305Ssam 
3707305Ssam /*
3717305Ssam  * untimeout is called to remove a function timeout call
3727305Ssam  * from the callout structure.
3737305Ssam  */
3748097Sroot untimeout(fun, arg)
3757305Ssam 	int (*fun)();
3767305Ssam 	caddr_t arg;
3777305Ssam {
3787305Ssam 	register struct callout *p1, *p2;
3797305Ssam 	register int s;
3807305Ssam 
38126265Skarels 	s = splhigh();
3827305Ssam 	for (p1 = &calltodo; (p2 = p1->c_next) != 0; p1 = p2) {
3837305Ssam 		if (p2->c_func == fun && p2->c_arg == arg) {
3848112Sroot 			if (p2->c_next && p2->c_time > 0)
3857305Ssam 				p2->c_next->c_time += p2->c_time;
3867305Ssam 			p1->c_next = p2->c_next;
3877305Ssam 			p2->c_next = callfree;
3887305Ssam 			callfree = p2;
3897305Ssam 			break;
3907305Ssam 		}
3917305Ssam 	}
3927305Ssam 	splx(s);
3937305Ssam }
3948112Sroot 
3958124Sroot /*
3968124Sroot  * Compute number of hz until specified time.
3978124Sroot  * Used to compute third argument to timeout() from an
3988124Sroot  * absolute time.
3998124Sroot  */
4008112Sroot hzto(tv)
4018112Sroot 	struct timeval *tv;
4028112Sroot {
4038124Sroot 	register long ticks;
4048124Sroot 	register long sec;
40526265Skarels 	int s = splhigh();
4068112Sroot 
4078124Sroot 	/*
4088124Sroot 	 * If number of milliseconds will fit in 32 bit arithmetic,
4098124Sroot 	 * then compute number of milliseconds to time and scale to
4108124Sroot 	 * ticks.  Otherwise just compute number of hz in time, rounding
4118124Sroot 	 * times greater than representible to maximum value.
4128124Sroot 	 *
4138124Sroot 	 * Delta times less than 25 days can be computed ``exactly''.
4148124Sroot 	 * Maximum value for any timeout in 10ms ticks is 250 days.
4158124Sroot 	 */
4168124Sroot 	sec = tv->tv_sec - time.tv_sec;
4178124Sroot 	if (sec <= 0x7fffffff / 1000 - 1000)
4188124Sroot 		ticks = ((tv->tv_sec - time.tv_sec) * 1000 +
4198124Sroot 			(tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000);
4208124Sroot 	else if (sec <= 0x7fffffff / hz)
4218124Sroot 		ticks = sec * hz;
4228124Sroot 	else
4238124Sroot 		ticks = 0x7fffffff;
4248112Sroot 	splx(s);
4258112Sroot 	return (ticks);
4268112Sroot }
42712747Ssam 
42812747Ssam profil()
42912747Ssam {
43012747Ssam 	register struct a {
43112747Ssam 		short	*bufbase;
43212747Ssam 		unsigned bufsize;
43312747Ssam 		unsigned pcoffset;
43412747Ssam 		unsigned pcscale;
43512747Ssam 	} *uap = (struct a *)u.u_ap;
43612747Ssam 	register struct uprof *upp = &u.u_prof;
43712747Ssam 
43812747Ssam 	upp->pr_base = uap->bufbase;
43912747Ssam 	upp->pr_size = uap->bufsize;
44012747Ssam 	upp->pr_off = uap->pcoffset;
44112747Ssam 	upp->pr_scale = uap->pcscale;
44212747Ssam }
443