xref: /csrg-svn/sys/kern/kern_clock.c (revision 56317)
149594Sbostic /*-
249594Sbostic  * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
349594Sbostic  * All rights reserved.
423366Smckusick  *
549594Sbostic  * %sccs.include.redist.c%
649594Sbostic  *
7*56317Shibler  *	@(#)kern_clock.c	7.25 (Berkeley) 09/21/92
823366Smckusick  */
99Sbill 
1017088Sbloom #include "param.h"
1117088Sbloom #include "systm.h"
1229946Skarels #include "dkstat.h"
1317088Sbloom #include "callout.h"
1417088Sbloom #include "kernel.h"
1517088Sbloom #include "proc.h"
1648979Skarels #include "resourcevar.h"
179Sbill 
1847546Skarels #include "machine/cpu.h"
1935406Skarels 
2010291Smckusick #ifdef GPROF
2154791Storek #include "gmon.h"
2254791Storek extern u_short *kcount;
2310291Smckusick #endif
2410291Smckusick 
258124Sroot /*
268124Sroot  * Clock handling routines.
278124Sroot  *
2854791Storek  * This code is written to operate with two timers that run independently of
2954791Storek  * each other.  The main clock, running hz times per second, is used to keep
3054791Storek  * track of real time.  The second timer handles kernel and user profiling,
3154791Storek  * and does resource use estimation.  If the second timer is programmable,
3254791Storek  * it is randomized to avoid aliasing between the two clocks.  For example,
3354791Storek  * the randomization prevents an adversary from always giving up the cpu
3454791Storek  * just before its quantum expires.  Otherwise, it would never accumulate
3554791Storek  * cpu ticks.  The mean frequency of the second timer is stathz.
3654791Storek  *
3754791Storek  * If no second timer exists, stathz will be zero; in this case we drive
3854791Storek  * profiling and statistics off the main clock.  This WILL NOT be accurate;
3954791Storek  * do not do it unless absolutely necessary.
4054791Storek  *
4154791Storek  * The statistics clock may (or may not) be run at a higher rate while
4254791Storek  * profiling.  This profile clock runs at profhz.  We require that profhz
4354791Storek  * be an integral multiple of stathz.
4454791Storek  *
4554791Storek  * If the statistics clock is running fast, it must be divided by the ratio
4654791Storek  * profhz/stathz for statistics.  (For profiling, every tick counts.)
478124Sroot  */
481559Sbill 
498124Sroot /*
508124Sroot  * TODO:
5112747Ssam  *	allocate more timeout table slots when table overflows.
528124Sroot  */
5326265Skarels 
5417007Smckusick /*
5517007Smckusick  * Bump a timeval by a small number of usec's.
5617007Smckusick  */
5717007Smckusick #define BUMPTIME(t, usec) { \
5854791Storek 	register volatile struct timeval *tp = (t); \
5954791Storek 	register long us; \
6017007Smckusick  \
6154791Storek 	tp->tv_usec = us = tp->tv_usec + (usec); \
6254791Storek 	if (us >= 1000000) { \
6354791Storek 		tp->tv_usec = us - 1000000; \
6417007Smckusick 		tp->tv_sec++; \
6517007Smckusick 	} \
6617007Smckusick }
6717007Smckusick 
6854124Smckusick int	stathz;
6953011Ssklower int	profhz;
7054138Smckusick int	profprocs;
71*56317Shibler static int psdiv, pscnt;	/* prof => stat divider */
72*56317Shibler int	psratio;		/* ratio: prot / 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 	 */
16055294Storek 	if (timedelta == 0)
16155294Storek 		delta = tick;
16255294Storek 	else {
16355294Storek 		delta = tick + tickdelta;
16455294Storek 		timedelta -= tickdelta;
16517356Skarels 	}
16655294Storek 	BUMPTIME(&time, delta);
16755294Storek 	BUMPTIME(&mono_time, delta);
16854791Storek 
16954791Storek 	/*
17054791Storek 	 * Process callouts at a very low cpu priority, so we don't keep the
17154791Storek 	 * relatively high clock interrupt priority any longer than necessary.
17254791Storek 	 */
17316525Skarels 	if (needsoft) {
17454791Storek 		if (CLKF_BASEPRI(frame)) {
17516525Skarels 			/*
17616525Skarels 			 * Save the overhead of a software interrupt;
17716525Skarels 			 * it will happen as soon as we return, so do it now.
17816525Skarels 			 */
17954791Storek 			(void)splsoftclock();
18054791Storek 			softclock();
18116525Skarels 		} else
18216525Skarels 			setsoftclock();
18316525Skarels 	}
1842442Swnj }
1852442Swnj 
1868124Sroot /*
18754791Storek  * Software (low priority) clock interrupt.
1888124Sroot  * Run periodic events from timeout queue.
1898124Sroot  */
1902609Swnj /*ARGSUSED*/
19154791Storek void
19254791Storek softclock()
1932442Swnj {
19454791Storek 	register struct callout *c;
19554791Storek 	register void *arg;
19654791Storek 	register void (*func) __P((void *));
19754791Storek 	register int s;
1982442Swnj 
19954791Storek 	s = splhigh();
20054791Storek 	while ((c = calltodo.c_next) != NULL && c->c_time <= 0) {
20154791Storek 		func = c->c_func;
20254791Storek 		arg = c->c_arg;
20354791Storek 		calltodo.c_next = c->c_next;
20454791Storek 		c->c_next = callfree;
20554791Storek 		callfree = c;
2069157Ssam 		splx(s);
20754791Storek 		(*func)(arg);
20854791Storek 		(void) splhigh();
2092442Swnj 	}
21054791Storek 	splx(s);
2119Sbill }
2129Sbill 
2139Sbill /*
21447546Skarels  * Arrange that (*func)(arg) is called in t/hz seconds.
21512747Ssam  */
21654791Storek void
21747546Skarels timeout(func, arg, t)
21854791Storek 	void (*func) __P((void *));
21954791Storek 	void *arg;
22012747Ssam 	register int t;
2219Sbill {
2223542Swnj 	register struct callout *p1, *p2, *pnew;
22354791Storek 	register int s;
2249Sbill 
22554791Storek 	s = splhigh();
22618282Smckusick 	if (t <= 0)
22712747Ssam 		t = 1;
2283542Swnj 	pnew = callfree;
2293542Swnj 	if (pnew == NULL)
2303542Swnj 		panic("timeout table overflow");
2313542Swnj 	callfree = pnew->c_next;
2323542Swnj 	pnew->c_arg = arg;
23347546Skarels 	pnew->c_func = func;
2343542Swnj 	for (p1 = &calltodo; (p2 = p1->c_next) && p2->c_time < t; p1 = p2)
2359742Ssam 		if (p2->c_time > 0)
2369742Ssam 			t -= p2->c_time;
2373542Swnj 	p1->c_next = pnew;
2383542Swnj 	pnew->c_next = p2;
2393542Swnj 	pnew->c_time = t;
2403542Swnj 	if (p2)
2413542Swnj 		p2->c_time -= t;
2429Sbill 	splx(s);
2439Sbill }
2447305Ssam 
2457305Ssam /*
2467305Ssam  * untimeout is called to remove a function timeout call
2477305Ssam  * from the callout structure.
2487305Ssam  */
24954791Storek void
25047546Skarels untimeout(func, arg)
25154791Storek 	void (*func) __P((void *));
25254791Storek 	void *arg;
2537305Ssam {
2547305Ssam 	register struct callout *p1, *p2;
2557305Ssam 	register int s;
2567305Ssam 
25726265Skarels 	s = splhigh();
25854791Storek 	for (p1 = &calltodo; (p2 = p1->c_next) != NULL; p1 = p2) {
25947546Skarels 		if (p2->c_func == func && p2->c_arg == arg) {
2608112Sroot 			if (p2->c_next && p2->c_time > 0)
2617305Ssam 				p2->c_next->c_time += p2->c_time;
2627305Ssam 			p1->c_next = p2->c_next;
2637305Ssam 			p2->c_next = callfree;
2647305Ssam 			callfree = p2;
2657305Ssam 			break;
2667305Ssam 		}
2677305Ssam 	}
2687305Ssam 	splx(s);
2697305Ssam }
2708112Sroot 
2718124Sroot /*
2728124Sroot  * Compute number of hz until specified time.
2738124Sroot  * Used to compute third argument to timeout() from an
2748124Sroot  * absolute time.
2758124Sroot  */
27654791Storek int
2778112Sroot hzto(tv)
2788112Sroot 	struct timeval *tv;
2798112Sroot {
28054791Storek 	register long ticks, sec;
28154791Storek 	int s;
2828112Sroot 
2838124Sroot 	/*
2848124Sroot 	 * If number of milliseconds will fit in 32 bit arithmetic,
2858124Sroot 	 * then compute number of milliseconds to time and scale to
2868124Sroot 	 * ticks.  Otherwise just compute number of hz in time, rounding
2878124Sroot 	 * times greater than representible to maximum value.
2888124Sroot 	 *
2898124Sroot 	 * Delta times less than 25 days can be computed ``exactly''.
2908124Sroot 	 * Maximum value for any timeout in 10ms ticks is 250 days.
2918124Sroot 	 */
29254791Storek 	s = splhigh();
2938124Sroot 	sec = tv->tv_sec - time.tv_sec;
2948124Sroot 	if (sec <= 0x7fffffff / 1000 - 1000)
2958124Sroot 		ticks = ((tv->tv_sec - time.tv_sec) * 1000 +
2968124Sroot 			(tv->tv_usec - time.tv_usec) / 1000) / (tick / 1000);
2978124Sroot 	else if (sec <= 0x7fffffff / hz)
2988124Sroot 		ticks = sec * hz;
2998124Sroot 	else
3008124Sroot 		ticks = 0x7fffffff;
3018112Sroot 	splx(s);
3028112Sroot 	return (ticks);
3038112Sroot }
30452668Smckusick 
30552668Smckusick /*
30654791Storek  * Start profiling on a process.
30754791Storek  *
30854791Storek  * Kernel profiling passes proc0 which never exits and hence
30954791Storek  * keeps the profile clock running constantly.
31054791Storek  */
31154791Storek void
31254791Storek startprofclock(p)
31354791Storek 	register struct proc *p;
31454791Storek {
31554791Storek 	int s;
31654791Storek 
31754791Storek 	if ((p->p_flag & SPROFIL) == 0) {
31854791Storek 		p->p_flag |= SPROFIL;
31954791Storek 		if (++profprocs == 1 && stathz != 0) {
32054791Storek 			s = splstatclock();
32154791Storek 			psdiv = pscnt = psratio;
32254791Storek 			setstatclockrate(profhz);
32354791Storek 			splx(s);
32454791Storek 		}
32554791Storek 	}
32654791Storek }
32754791Storek 
32854791Storek /*
32954791Storek  * Stop profiling on a process.
33054791Storek  */
33154791Storek void
33254791Storek stopprofclock(p)
33354791Storek 	register struct proc *p;
33454791Storek {
33554791Storek 	int s;
33654791Storek 
33754791Storek 	if (p->p_flag & SPROFIL) {
33854791Storek 		p->p_flag &= ~SPROFIL;
33954791Storek 		if (--profprocs == 0 && stathz != 0) {
34054791Storek 			s = splstatclock();
34154791Storek 			psdiv = pscnt = 1;
34254791Storek 			setstatclockrate(stathz);
34354791Storek 			splx(s);
34454791Storek 		}
34554791Storek 	}
34654791Storek }
34754791Storek 
34854791Storek int	dk_ndrive = DK_NDRIVE;
34954791Storek 
35054791Storek /*
35154791Storek  * Statistics clock.  Grab profile sample, and if divider reaches 0,
35254791Storek  * do process and kernel statistics.
35354791Storek  */
35454791Storek void
35554791Storek statclock(frame)
35654791Storek 	register struct clockframe *frame;
35754791Storek {
35854791Storek #ifdef GPROF
35954791Storek 	register struct gmonparam *g;
36054791Storek #endif
36154791Storek 	register struct proc *p;
36254791Storek 	register int i;
36354791Storek 
36454791Storek 	if (CLKF_USERMODE(frame)) {
36554791Storek 		p = curproc;
36654791Storek 		if (p->p_flag & SPROFIL)
36754791Storek 			addupc_intr(p, CLKF_PC(frame), 1);
36854791Storek 		if (--pscnt > 0)
36954791Storek 			return;
37054791Storek 		/*
37154791Storek 		 * Came from user mode; CPU was in user state.
37254791Storek 		 * If this process is being profiled record the tick.
37354791Storek 		 */
37454791Storek 		p->p_uticks++;
37554791Storek 		if (p->p_nice > NZERO)
37654791Storek 			cp_time[CP_NICE]++;
37754791Storek 		else
37854791Storek 			cp_time[CP_USER]++;
37954791Storek 	} else {
38054791Storek #ifdef GPROF
38154791Storek 		/*
38254791Storek 		 * Kernel statistics are just like addupc_intr, only easier.
38354791Storek 		 */
38454791Storek 		g = &_gmonparam;
38554791Storek 		if (g->state == GMON_PROF_ON) {
38654791Storek 			i = CLKF_PC(frame) - g->lowpc;
38754791Storek 			if (i < g->textsize)
38854886Storek 				kcount[i / (HISTFRACTION * sizeof(*kcount))]++;
38954791Storek 		}
39054791Storek #endif
39154791Storek 		if (--pscnt > 0)
39254791Storek 			return;
39354791Storek 		/*
39454791Storek 		 * Came from kernel mode, so we were:
39554791Storek 		 * - handling an interrupt,
39654791Storek 		 * - doing syscall or trap work on behalf of the current
39754791Storek 		 *   user process, or
39854791Storek 		 * - spinning in the idle loop.
39954791Storek 		 * Whichever it is, charge the time as appropriate.
40054791Storek 		 * Note that we charge interrupts to the current process,
40154791Storek 		 * regardless of whether they are ``for'' that process,
40254791Storek 		 * so that we know how much of its real time was spent
40354791Storek 		 * in ``non-process'' (i.e., interrupt) work.
40454791Storek 		 */
40554791Storek 		p = curproc;
40654791Storek 		if (CLKF_INTR(frame)) {
40754791Storek 			if (p != NULL)
40854791Storek 				p->p_iticks++;
40954791Storek 			cp_time[CP_INTR]++;
41054791Storek 		} else if (p != NULL) {
41154791Storek 			p->p_sticks++;
41254791Storek 			cp_time[CP_SYS]++;
41354791Storek 		} else
41454791Storek 			cp_time[CP_IDLE]++;
41554791Storek 	}
41654791Storek 	pscnt = psdiv;
41754791Storek 
41854791Storek 	/*
41954791Storek 	 * We maintain statistics shown by user-level statistics
42054791Storek 	 * programs:  the amount of time in each cpu state, and
42154791Storek 	 * the amount of time each of DK_NDRIVE ``drives'' is busy.
42254791Storek 	 *
42354791Storek 	 * XXX	should either run linked list of drives, or (better)
42454791Storek 	 *	grab timestamps in the start & done code.
42554791Storek 	 */
42654791Storek 	for (i = 0; i < DK_NDRIVE; i++)
42754791Storek 		if (dk_busy & (1 << i))
42854791Storek 			dk_time[i]++;
42954791Storek 
43054791Storek 	/*
43154791Storek 	 * We adjust the priority of the current process.
43254791Storek 	 * The priority of a process gets worse as it accumulates
43354791Storek 	 * CPU time.  The cpu usage estimator (p_cpu) is increased here
43454791Storek 	 * and the formula for computing priorities (in kern_synch.c)
43554791Storek 	 * will compute a different value each time the p_cpu increases
43654791Storek 	 * by 4.  The cpu usage estimator ramps up quite quickly when
43754791Storek 	 * the process is running (linearly), and decays away
43854791Storek 	 * exponentially, at a rate which is proportionally slower
43954791Storek 	 * when the system is busy.  The basic principal is that the
44054791Storek 	 * system will 90% forget that a process used a lot of CPU
44154791Storek 	 * time in 5*loadav seconds.  This causes the system to favor
44254791Storek 	 * processes which haven't run much recently, and to
44354791Storek 	 * round-robin among other processes.
44454791Storek 	 */
44554791Storek 	if (p != NULL) {
44654791Storek 		p->p_cpticks++;
44754791Storek 		if (++p->p_cpu == 0)
44854791Storek 			p->p_cpu--;
44954791Storek 		if ((p->p_cpu & 3) == 0) {
45054791Storek 			setpri(p);
45154791Storek 			if (p->p_pri >= PUSER)
45254791Storek 				p->p_pri = p->p_usrpri;
45354791Storek 		}
45454791Storek 	}
45554791Storek }
45654791Storek 
45754791Storek /*
45852668Smckusick  * Return information about system clocks.
45952668Smckusick  */
46052668Smckusick /* ARGSUSED */
46152668Smckusick kinfo_clockrate(op, where, acopysize, arg, aneeded)
46252668Smckusick 	int op;
46352668Smckusick 	register char *where;
46452668Smckusick 	int *acopysize, arg, *aneeded;
46552668Smckusick {
46652668Smckusick 	int buflen, error;
46752668Smckusick 	struct clockinfo clockinfo;
46852668Smckusick 
46952668Smckusick 	*aneeded = sizeof(clockinfo);
47052668Smckusick 	if (where == NULL)
47152668Smckusick 		return (0);
47252668Smckusick 	/*
47352668Smckusick 	 * Check for enough buffering.
47452668Smckusick 	 */
47552668Smckusick 	buflen = *acopysize;
47652668Smckusick 	if (buflen < sizeof(clockinfo)) {
47752668Smckusick 		*acopysize = 0;
47852668Smckusick 		return (0);
47952668Smckusick 	}
48052668Smckusick 	/*
48152668Smckusick 	 * Copyout clockinfo structure.
48252668Smckusick 	 */
48352668Smckusick 	clockinfo.hz = hz;
48452668Smckusick 	clockinfo.tick = tick;
48552668Smckusick 	clockinfo.profhz = profhz;
48654791Storek 	clockinfo.stathz = stathz ? stathz : hz;
48752668Smckusick 	if (error = copyout((caddr_t)&clockinfo, where, sizeof(clockinfo)))
48852668Smckusick 		return (error);
48952668Smckusick 	*acopysize = sizeof(clockinfo);
49052668Smckusick 	return (0);
49152668Smckusick }
492