xref: /csrg-svn/sys/kern/kern_clock.c (revision 64428)
149594Sbostic /*-
263170Sbostic  * Copyright (c) 1982, 1986, 1991, 1993
363170Sbostic  *	The Regents of the University of California.  All rights reserved.
423366Smckusick  *
549594Sbostic  * %sccs.include.redist.c%
649594Sbostic  *
7*64428Sbostic  *	@(#)kern_clock.c	8.2 (Berkeley) 09/05/93
823366Smckusick  */
99Sbill 
1056517Sbostic #include <sys/param.h>
1156517Sbostic #include <sys/systm.h>
1256517Sbostic #include <sys/dkstat.h>
1356517Sbostic #include <sys/callout.h>
1456517Sbostic #include <sys/kernel.h>
1556517Sbostic #include <sys/proc.h>
1656517Sbostic #include <sys/resourcevar.h>
179Sbill 
1856517Sbostic #include <machine/cpu.h>
1935406Skarels 
2010291Smckusick #ifdef GPROF
2156517Sbostic #include <sys/gmon.h>
2210291Smckusick #endif
2310291Smckusick 
248124Sroot /*
258124Sroot  * Clock handling routines.
268124Sroot  *
2754791Storek  * This code is written to operate with two timers that run independently of
2854791Storek  * each other.  The main clock, running hz times per second, is used to keep
2954791Storek  * track of real time.  The second timer handles kernel and user profiling,
3054791Storek  * and does resource use estimation.  If the second timer is programmable,
3154791Storek  * it is randomized to avoid aliasing between the two clocks.  For example,
3254791Storek  * the randomization prevents an adversary from always giving up the cpu
3354791Storek  * just before its quantum expires.  Otherwise, it would never accumulate
3454791Storek  * cpu ticks.  The mean frequency of the second timer is stathz.
3554791Storek  *
3654791Storek  * If no second timer exists, stathz will be zero; in this case we drive
3754791Storek  * profiling and statistics off the main clock.  This WILL NOT be accurate;
3854791Storek  * do not do it unless absolutely necessary.
3954791Storek  *
4054791Storek  * The statistics clock may (or may not) be run at a higher rate while
4154791Storek  * profiling.  This profile clock runs at profhz.  We require that profhz
4254791Storek  * be an integral multiple of stathz.
4354791Storek  *
4454791Storek  * If the statistics clock is running fast, it must be divided by the ratio
4554791Storek  * profhz/stathz for statistics.  (For profiling, every tick counts.)
468124Sroot  */
471559Sbill 
488124Sroot /*
498124Sroot  * TODO:
5012747Ssam  *	allocate more timeout table slots when table overflows.
518124Sroot  */
5226265Skarels 
5317007Smckusick /*
5417007Smckusick  * Bump a timeval by a small number of usec's.
5517007Smckusick  */
5617007Smckusick #define BUMPTIME(t, usec) { \
5754791Storek 	register volatile struct timeval *tp = (t); \
5854791Storek 	register long us; \
5917007Smckusick  \
6054791Storek 	tp->tv_usec = us = tp->tv_usec + (usec); \
6154791Storek 	if (us >= 1000000) { \
6254791Storek 		tp->tv_usec = us - 1000000; \
6317007Smckusick 		tp->tv_sec++; \
6417007Smckusick 	} \
6517007Smckusick }
6617007Smckusick 
6754124Smckusick int	stathz;
6853011Ssklower int	profhz;
6954138Smckusick int	profprocs;
7056338Ssklower int	ticks;
7156317Shibler static int psdiv, pscnt;	/* prof => stat divider */
7256855Storek int	psratio;		/* ratio: prof / stat */
7354791Storek 
7454791Storek volatile struct	timeval time;
7554791Storek volatile struct	timeval mono_time;
7654791Storek 
778124Sroot /*
7854791Storek  * Initialize clock frequencies and start both clocks running.
798124Sroot  */
8054791Storek void
8154791Storek initclocks()
8254791Storek {
8354791Storek 	register int i;
8454791Storek 
8554791Storek 	/*
8654791Storek 	 * Set divisors to 1 (normal case) and let the machine-specific
8754791Storek 	 * code do its bit.
8854791Storek 	 */
8954791Storek 	psdiv = pscnt = 1;
9054791Storek 	cpu_initclocks();
9154791Storek 
9254791Storek 	/*
9354791Storek 	 * Compute profhz/stathz, and fix profhz if needed.
9454791Storek 	 */
9554791Storek 	i = stathz ? stathz : hz;
9654791Storek 	if (profhz == 0)
9754791Storek 		profhz = i;
9854791Storek 	psratio = profhz / i;
9954791Storek }
10054791Storek 
10154791Storek /*
10254791Storek  * The real-time timer, interrupting hz times per second.
10354791Storek  */
10454791Storek void
10544774Swilliam hardclock(frame)
10654791Storek 	register struct clockframe *frame;
1079Sbill {
1082768Swnj 	register struct callout *p1;
10954791Storek 	register struct proc *p;
11055294Storek 	register int delta, needsoft;
11128947Skarels 	extern int tickdelta;
11228947Skarels 	extern long timedelta;
1139Sbill 
1148124Sroot 	/*
1158124Sroot 	 * Update real-time timeout queue.
1168124Sroot 	 * At front of queue are some number of events which are ``due''.
1178124Sroot 	 * The time to these is <= 0 and if negative represents the
1188124Sroot 	 * number of ticks which have passed since it was supposed to happen.
1198124Sroot 	 * The rest of the q elements (times > 0) are events yet to happen,
1208124Sroot 	 * where the time for each is given as a delta from the previous.
1218124Sroot 	 * Decrementing just the first of these serves to decrement the time
1228124Sroot 	 * to all events.
1238124Sroot 	 */
12454791Storek 	needsoft = 0;
12554791Storek 	for (p1 = calltodo.c_next; p1 != NULL; p1 = p1->c_next) {
12612747Ssam 		if (--p1->c_time > 0)
12712747Ssam 			break;
12816172Skarels 		needsoft = 1;
12912747Ssam 		if (p1->c_time == 0)
13012747Ssam 			break;
13112747Ssam 	}
132138Sbill 
13354791Storek 	p = curproc;
13454791Storek 	if (p) {
13554791Storek 		register struct pstats *pstats;
13654791Storek 
1378124Sroot 		/*
13854791Storek 		 * Run current process's virtual and profile time, as needed.
1398124Sroot 		 */
14054791Storek 		pstats = p->p_stats;
14154791Storek 		if (CLKF_USERMODE(frame) &&
14254791Storek 		    timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
14347546Skarels 		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
14440674Smarc 			psignal(p, SIGVTALRM);
14547546Skarels 		if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
14647546Skarels 		    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
14740674Smarc 			psignal(p, SIGPROF);
1489Sbill 	}
1498124Sroot 
1508124Sroot 	/*
15154791Storek 	 * If no separate statistics clock is available, run it from here.
15211392Ssam 	 */
15354124Smckusick 	if (stathz == 0)
15454791Storek 		statclock(frame);
15511392Ssam 
15611392Ssam 	/*
15755294Storek 	 * Increment the time-of-day.  The increment is just ``tick'' unless
15855294Storek 	 * we are still adjusting the clock; see adjtime().
1598124Sroot 	 */
16056338Ssklower 	ticks++;
16155294Storek 	if (timedelta == 0)
16255294Storek 		delta = tick;
16355294Storek 	else {
16455294Storek 		delta = tick + tickdelta;
16555294Storek 		timedelta -= tickdelta;
16617356Skarels 	}
16755294Storek 	BUMPTIME(&time, delta);
16855294Storek 	BUMPTIME(&mono_time, delta);
16954791Storek 
17054791Storek 	/*
17154791Storek 	 * Process callouts at a very low cpu priority, so we don't keep the
17254791Storek 	 * relatively high clock interrupt priority any longer than necessary.
17354791Storek 	 */
17416525Skarels 	if (needsoft) {
17554791Storek 		if (CLKF_BASEPRI(frame)) {
17616525Skarels 			/*
17716525Skarels 			 * Save the overhead of a software interrupt;
17816525Skarels 			 * it will happen as soon as we return, so do it now.
17916525Skarels 			 */
18054791Storek 			(void)splsoftclock();
18154791Storek 			softclock();
18216525Skarels 		} else
18316525Skarels 			setsoftclock();
18416525Skarels 	}
1852442Swnj }
1862442Swnj 
1878124Sroot /*
18854791Storek  * Software (low priority) clock interrupt.
1898124Sroot  * Run periodic events from timeout queue.
1908124Sroot  */
1912609Swnj /*ARGSUSED*/
19254791Storek void
19354791Storek softclock()
1942442Swnj {
19554791Storek 	register struct callout *c;
19654791Storek 	register void *arg;
19754791Storek 	register void (*func) __P((void *));
19854791Storek 	register int s;
1992442Swnj 
20054791Storek 	s = splhigh();
20154791Storek 	while ((c = calltodo.c_next) != NULL && c->c_time <= 0) {
20254791Storek 		func = c->c_func;
20354791Storek 		arg = c->c_arg;
20454791Storek 		calltodo.c_next = c->c_next;
20554791Storek 		c->c_next = callfree;
20654791Storek 		callfree = c;
2079157Ssam 		splx(s);
20854791Storek 		(*func)(arg);
20954791Storek 		(void) splhigh();
2102442Swnj 	}
21154791Storek 	splx(s);
2129Sbill }
2139Sbill 
2149Sbill /*
215*64428Sbostic  * timeout --
216*64428Sbostic  *	Execute a function after a specified length of time.
217*64428Sbostic  *
218*64428Sbostic  * untimeout --
219*64428Sbostic  *	Cancel previous timeout function call.
220*64428Sbostic  *
221*64428Sbostic  *	See AT&T BCI Driver Reference Manual for specification.  This
222*64428Sbostic  *	implementation differs from that one in that no identification
223*64428Sbostic  *	value is returned from timeout, rather, the original arguments
224*64428Sbostic  *	to timeout are used to identify entries for untimeout.
22512747Ssam  */
22654791Storek void
227*64428Sbostic timeout(ftn, arg, ticks)
228*64428Sbostic 	void (*ftn) __P((void *));
22954791Storek 	void *arg;
230*64428Sbostic 	register int ticks;
2319Sbill {
232*64428Sbostic 	register struct callout *new, *p, *t;
23354791Storek 	register int s;
2349Sbill 
235*64428Sbostic 	if (ticks <= 0)
236*64428Sbostic 		ticks = 1;
237*64428Sbostic 
238*64428Sbostic 	/* Lock out the clock. */
23954791Storek 	s = splhigh();
240*64428Sbostic 
241*64428Sbostic 	/* Fill in the next free callout structure. */
242*64428Sbostic 	if (callfree == NULL)
243*64428Sbostic 		panic("timeout table full");
244*64428Sbostic 	new = callfree;
245*64428Sbostic 	callfree = new->c_next;
246*64428Sbostic 	new->c_arg = arg;
247*64428Sbostic 	new->c_func = ftn;
248*64428Sbostic 
249*64428Sbostic 	/*
250*64428Sbostic 	 * The time for each event is stored as a difference from the time
251*64428Sbostic 	 * of the previous event on the queue.  Walk the queue, correcting
252*64428Sbostic 	 * the ticks argument for queue entries passed.  Correct the ticks
253*64428Sbostic 	 * value for the queue entry immediately after the insertion point
254*64428Sbostic 	 * as well.
255*64428Sbostic 	 */
256*64428Sbostic 	for (p = &calltodo;
257*64428Sbostic 	    (t = p->c_next) != NULL && ticks > t->c_time; p = t)
258*64428Sbostic 		ticks -= t->c_time;
259*64428Sbostic 	new->c_time = ticks;
260*64428Sbostic 	if (t != NULL)
261*64428Sbostic 		t->c_time -= ticks;
262*64428Sbostic 
263*64428Sbostic 	/* Insert the new entry into the queue. */
264*64428Sbostic 	p->c_next = new;
265*64428Sbostic 	new->c_next = t;
2669Sbill 	splx(s);
2679Sbill }
2687305Ssam 
26954791Storek void
270*64428Sbostic untimeout(ftn, arg)
271*64428Sbostic 	void (*ftn) __P((void *));
27254791Storek 	void *arg;
2737305Ssam {
274*64428Sbostic 	register struct callout *p, *t;
2757305Ssam 	register int s;
2767305Ssam 
27726265Skarels 	s = splhigh();
278*64428Sbostic 	for (p = &calltodo; (t = p->c_next) != NULL; p = t)
279*64428Sbostic 		if (t->c_func == ftn && t->c_arg == arg) {
280*64428Sbostic 			/* Increment next entry's tick count. */
281*64428Sbostic 			if (t->c_next && t->c_time > 0)
282*64428Sbostic 				t->c_next->c_time += t->c_time;
283*64428Sbostic 
284*64428Sbostic 			/* Move entry from callout queue to callfree queue. */
285*64428Sbostic 			p->c_next = t->c_next;
286*64428Sbostic 			t->c_next = callfree;
287*64428Sbostic 			callfree = t;
2887305Ssam 			break;
2897305Ssam 		}
2907305Ssam 	splx(s);
2917305Ssam }
2928112Sroot 
2938124Sroot /*
294*64428Sbostic  * Compute number of hz until specified time.  Used to
295*64428Sbostic  * compute third argument to timeout() from an absolute time.
2968124Sroot  */
29754791Storek int
2988112Sroot hzto(tv)
2998112Sroot 	struct timeval *tv;
3008112Sroot {
30154791Storek 	register long ticks, sec;
30254791Storek 	int s;
3038112Sroot 
3048124Sroot 	/*
3058124Sroot 	 * If number of milliseconds will fit in 32 bit arithmetic,
3068124Sroot 	 * then compute number of milliseconds to time and scale to
3078124Sroot 	 * ticks.  Otherwise just compute number of hz in time, rounding
3088124Sroot 	 * times greater than representible to maximum value.
3098124Sroot 	 *
3108124Sroot 	 * Delta times less than 25 days can be computed ``exactly''.
3118124Sroot 	 * Maximum value for any timeout in 10ms ticks is 250 days.
3128124Sroot 	 */
31354791Storek 	s = splhigh();
3148124Sroot 	sec = tv->tv_sec - time.tv_sec;
3158124Sroot 	if (sec <= 0x7fffffff / 1000 - 1000)
3168124Sroot 		ticks = ((tv->tv_sec - time.tv_sec) * 1000 +
3178124Sroot 			(tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000);
3188124Sroot 	else if (sec <= 0x7fffffff / hz)
3198124Sroot 		ticks = sec * hz;
3208124Sroot 	else
3218124Sroot 		ticks = 0x7fffffff;
3228112Sroot 	splx(s);
3238112Sroot 	return (ticks);
3248112Sroot }
32552668Smckusick 
32652668Smckusick /*
32754791Storek  * Start profiling on a process.
32854791Storek  *
32954791Storek  * Kernel profiling passes proc0 which never exits and hence
33054791Storek  * keeps the profile clock running constantly.
33154791Storek  */
33254791Storek void
33354791Storek startprofclock(p)
33454791Storek 	register struct proc *p;
33554791Storek {
33654791Storek 	int s;
33754791Storek 
33854791Storek 	if ((p->p_flag & SPROFIL) == 0) {
33954791Storek 		p->p_flag |= SPROFIL;
34054791Storek 		if (++profprocs == 1 && stathz != 0) {
34154791Storek 			s = splstatclock();
34254791Storek 			psdiv = pscnt = psratio;
34354791Storek 			setstatclockrate(profhz);
34454791Storek 			splx(s);
34554791Storek 		}
34654791Storek 	}
34754791Storek }
34854791Storek 
34954791Storek /*
35054791Storek  * Stop profiling on a process.
35154791Storek  */
35254791Storek void
35354791Storek stopprofclock(p)
35454791Storek 	register struct proc *p;
35554791Storek {
35654791Storek 	int s;
35754791Storek 
35854791Storek 	if (p->p_flag & SPROFIL) {
35954791Storek 		p->p_flag &= ~SPROFIL;
36054791Storek 		if (--profprocs == 0 && stathz != 0) {
36154791Storek 			s = splstatclock();
36254791Storek 			psdiv = pscnt = 1;
36354791Storek 			setstatclockrate(stathz);
36454791Storek 			splx(s);
36554791Storek 		}
36654791Storek 	}
36754791Storek }
36854791Storek 
36954791Storek int	dk_ndrive = DK_NDRIVE;
37054791Storek 
37154791Storek /*
37254791Storek  * Statistics clock.  Grab profile sample, and if divider reaches 0,
37354791Storek  * do process and kernel statistics.
37454791Storek  */
37554791Storek void
37654791Storek statclock(frame)
37754791Storek 	register struct clockframe *frame;
37854791Storek {
37954791Storek #ifdef GPROF
38054791Storek 	register struct gmonparam *g;
38154791Storek #endif
38254791Storek 	register struct proc *p;
38354791Storek 	register int i;
38454791Storek 
38554791Storek 	if (CLKF_USERMODE(frame)) {
38654791Storek 		p = curproc;
38754791Storek 		if (p->p_flag & SPROFIL)
38854791Storek 			addupc_intr(p, CLKF_PC(frame), 1);
38954791Storek 		if (--pscnt > 0)
39054791Storek 			return;
39154791Storek 		/*
39254791Storek 		 * Came from user mode; CPU was in user state.
39354791Storek 		 * If this process is being profiled record the tick.
39454791Storek 		 */
39554791Storek 		p->p_uticks++;
39654791Storek 		if (p->p_nice > NZERO)
39754791Storek 			cp_time[CP_NICE]++;
39854791Storek 		else
39954791Storek 			cp_time[CP_USER]++;
40054791Storek 	} else {
40154791Storek #ifdef GPROF
40254791Storek 		/*
40354791Storek 		 * Kernel statistics are just like addupc_intr, only easier.
40454791Storek 		 */
40554791Storek 		g = &_gmonparam;
40654791Storek 		if (g->state == GMON_PROF_ON) {
40754791Storek 			i = CLKF_PC(frame) - g->lowpc;
40859204Smckusick 			if (i < g->textsize) {
40959204Smckusick 				i /= HISTFRACTION * sizeof(*g->kcount);
41059204Smckusick 				g->kcount[i]++;
41159204Smckusick 			}
41254791Storek 		}
41354791Storek #endif
41454791Storek 		if (--pscnt > 0)
41554791Storek 			return;
41654791Storek 		/*
41754791Storek 		 * Came from kernel mode, so we were:
41854791Storek 		 * - handling an interrupt,
41954791Storek 		 * - doing syscall or trap work on behalf of the current
42054791Storek 		 *   user process, or
42154791Storek 		 * - spinning in the idle loop.
42254791Storek 		 * Whichever it is, charge the time as appropriate.
42354791Storek 		 * Note that we charge interrupts to the current process,
42454791Storek 		 * regardless of whether they are ``for'' that process,
42554791Storek 		 * so that we know how much of its real time was spent
42654791Storek 		 * in ``non-process'' (i.e., interrupt) work.
42754791Storek 		 */
42854791Storek 		p = curproc;
42954791Storek 		if (CLKF_INTR(frame)) {
43054791Storek 			if (p != NULL)
43154791Storek 				p->p_iticks++;
43254791Storek 			cp_time[CP_INTR]++;
43354791Storek 		} else if (p != NULL) {
43454791Storek 			p->p_sticks++;
43554791Storek 			cp_time[CP_SYS]++;
43654791Storek 		} else
43754791Storek 			cp_time[CP_IDLE]++;
43854791Storek 	}
43954791Storek 	pscnt = psdiv;
44054791Storek 
44154791Storek 	/*
44254791Storek 	 * We maintain statistics shown by user-level statistics
44354791Storek 	 * programs:  the amount of time in each cpu state, and
44454791Storek 	 * the amount of time each of DK_NDRIVE ``drives'' is busy.
44554791Storek 	 *
44654791Storek 	 * XXX	should either run linked list of drives, or (better)
44754791Storek 	 *	grab timestamps in the start & done code.
44854791Storek 	 */
44954791Storek 	for (i = 0; i < DK_NDRIVE; i++)
45054791Storek 		if (dk_busy & (1 << i))
45154791Storek 			dk_time[i]++;
45254791Storek 
45354791Storek 	/*
45454791Storek 	 * We adjust the priority of the current process.
45554791Storek 	 * The priority of a process gets worse as it accumulates
45654791Storek 	 * CPU time.  The cpu usage estimator (p_cpu) is increased here
45754791Storek 	 * and the formula for computing priorities (in kern_synch.c)
45854791Storek 	 * will compute a different value each time the p_cpu increases
45954791Storek 	 * by 4.  The cpu usage estimator ramps up quite quickly when
46054791Storek 	 * the process is running (linearly), and decays away
46154791Storek 	 * exponentially, at a rate which is proportionally slower
46254791Storek 	 * when the system is busy.  The basic principal is that the
46354791Storek 	 * system will 90% forget that a process used a lot of CPU
46454791Storek 	 * time in 5*loadav seconds.  This causes the system to favor
46554791Storek 	 * processes which haven't run much recently, and to
46654791Storek 	 * round-robin among other processes.
46754791Storek 	 */
46854791Storek 	if (p != NULL) {
46954791Storek 		p->p_cpticks++;
47054791Storek 		if (++p->p_cpu == 0)
47154791Storek 			p->p_cpu--;
47254791Storek 		if ((p->p_cpu & 3) == 0) {
473*64428Sbostic 			resetpriority(p);
47454791Storek 			if (p->p_pri >= PUSER)
47554791Storek 				p->p_pri = p->p_usrpri;
47654791Storek 		}
47754791Storek 	}
47854791Storek }
47954791Storek 
48054791Storek /*
48152668Smckusick  * Return information about system clocks.
48252668Smckusick  */
48357840Smckusick sysctl_clockrate(where, sizep)
48452668Smckusick 	register char *where;
48558464Sbostic 	size_t *sizep;
48652668Smckusick {
48757840Smckusick 	struct clockinfo clkinfo;
48852668Smckusick 
48952668Smckusick 	/*
49057840Smckusick 	 * Construct clockinfo structure.
49152668Smckusick 	 */
49257840Smckusick 	clkinfo.hz = hz;
49357840Smckusick 	clkinfo.tick = tick;
49457840Smckusick 	clkinfo.profhz = profhz;
49557840Smckusick 	clkinfo.stathz = stathz ? stathz : hz;
49657840Smckusick 	return (sysctl_rdstruct(where, sizep, NULL, &clkinfo, sizeof(clkinfo)));
49752668Smckusick }
498