Home | History | Annotate | Line # | Download | only in kern
kern_clock.c revision 1.26
      1  1.26  christos /*	$NetBSD: kern_clock.c,v 1.26 1996/02/09 18:59:24 christos Exp $	*/
      2  1.19       cgd 
      3  1.19       cgd /*-
      4  1.19       cgd  * Copyright (c) 1982, 1986, 1991, 1993
      5  1.19       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.19       cgd  * (c) UNIX System Laboratories, Inc.
      7  1.19       cgd  * All or some portions of this file are derived from material licensed
      8  1.19       cgd  * to the University of California by American Telephone and Telegraph
      9  1.19       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.19       cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.19       cgd  *
     12  1.19       cgd  * Redistribution and use in source and binary forms, with or without
     13  1.19       cgd  * modification, are permitted provided that the following conditions
     14  1.19       cgd  * are met:
     15  1.19       cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.19       cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.19       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.19       cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.19       cgd  *    documentation and/or other materials provided with the distribution.
     20  1.19       cgd  * 3. All advertising materials mentioning features or use of this software
     21  1.19       cgd  *    must display the following acknowledgement:
     22  1.19       cgd  *	This product includes software developed by the University of
     23  1.19       cgd  *	California, Berkeley and its contributors.
     24  1.19       cgd  * 4. Neither the name of the University nor the names of its contributors
     25  1.19       cgd  *    may be used to endorse or promote products derived from this software
     26  1.19       cgd  *    without specific prior written permission.
     27  1.19       cgd  *
     28  1.19       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.19       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.19       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.19       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.19       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.19       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.19       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.19       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.19       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.19       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.19       cgd  * SUCH DAMAGE.
     39  1.19       cgd  *
     40  1.19       cgd  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
     41  1.19       cgd  */
     42  1.19       cgd 
     43  1.19       cgd #include <sys/param.h>
     44  1.19       cgd #include <sys/systm.h>
     45  1.19       cgd #include <sys/dkstat.h>
     46  1.19       cgd #include <sys/callout.h>
     47  1.19       cgd #include <sys/kernel.h>
     48  1.19       cgd #include <sys/proc.h>
     49  1.19       cgd #include <sys/resourcevar.h>
     50  1.25  christos #include <sys/signalvar.h>
     51  1.26  christos #include <sys/cpu.h>
     52  1.26  christos #include <vm/vm.h>
     53  1.26  christos #include <sys/sysctl.h>
     54  1.19       cgd 
     55  1.19       cgd #include <machine/cpu.h>
     56  1.25  christos 
     57  1.19       cgd #ifdef GPROF
     58  1.19       cgd #include <sys/gmon.h>
     59  1.19       cgd #endif
     60  1.19       cgd 
     61  1.19       cgd /*
     62  1.19       cgd  * Clock handling routines.
     63  1.19       cgd  *
     64  1.19       cgd  * This code is written to operate with two timers that run independently of
     65  1.19       cgd  * each other.  The main clock, running hz times per second, is used to keep
     66  1.19       cgd  * track of real time.  The second timer handles kernel and user profiling,
     67  1.19       cgd  * and does resource use estimation.  If the second timer is programmable,
     68  1.19       cgd  * it is randomized to avoid aliasing between the two clocks.  For example,
     69  1.19       cgd  * the randomization prevents an adversary from always giving up the cpu
     70  1.19       cgd  * just before its quantum expires.  Otherwise, it would never accumulate
     71  1.19       cgd  * cpu ticks.  The mean frequency of the second timer is stathz.
     72  1.19       cgd  *
     73  1.19       cgd  * If no second timer exists, stathz will be zero; in this case we drive
     74  1.19       cgd  * profiling and statistics off the main clock.  This WILL NOT be accurate;
     75  1.19       cgd  * do not do it unless absolutely necessary.
     76  1.19       cgd  *
     77  1.19       cgd  * The statistics clock may (or may not) be run at a higher rate while
     78  1.19       cgd  * profiling.  This profile clock runs at profhz.  We require that profhz
     79  1.19       cgd  * be an integral multiple of stathz.
     80  1.19       cgd  *
     81  1.19       cgd  * If the statistics clock is running fast, it must be divided by the ratio
     82  1.19       cgd  * profhz/stathz for statistics.  (For profiling, every tick counts.)
     83  1.19       cgd  */
     84  1.19       cgd 
     85  1.19       cgd /*
     86  1.19       cgd  * TODO:
     87  1.19       cgd  *	allocate more timeout table slots when table overflows.
     88  1.19       cgd  */
     89  1.19       cgd 
     90  1.19       cgd /*
     91  1.19       cgd  * Bump a timeval by a small number of usec's.
     92  1.19       cgd  */
     93  1.19       cgd #define BUMPTIME(t, usec) { \
     94  1.19       cgd 	register volatile struct timeval *tp = (t); \
     95  1.19       cgd 	register long us; \
     96  1.19       cgd  \
     97  1.19       cgd 	tp->tv_usec = us = tp->tv_usec + (usec); \
     98  1.19       cgd 	if (us >= 1000000) { \
     99  1.19       cgd 		tp->tv_usec = us - 1000000; \
    100  1.19       cgd 		tp->tv_sec++; \
    101  1.19       cgd 	} \
    102  1.19       cgd }
    103  1.19       cgd 
    104  1.19       cgd int	stathz;
    105  1.19       cgd int	profhz;
    106  1.19       cgd int	profprocs;
    107  1.19       cgd int	ticks;
    108  1.22       cgd static int psdiv, pscnt;		/* prof => stat divider */
    109  1.22       cgd int	psratio;			/* ratio: prof / stat */
    110  1.22       cgd int	tickfix, tickfixinterval;	/* used if tick not really integral */
    111  1.22       cgd static int tickfixcnt;			/* number of ticks since last fix */
    112  1.19       cgd 
    113  1.19       cgd volatile struct	timeval time;
    114  1.19       cgd volatile struct	timeval mono_time;
    115  1.19       cgd 
    116  1.19       cgd /*
    117  1.19       cgd  * Initialize clock frequencies and start both clocks running.
    118  1.19       cgd  */
    119  1.19       cgd void
    120  1.19       cgd initclocks()
    121  1.19       cgd {
    122  1.19       cgd 	register int i;
    123  1.19       cgd 
    124  1.19       cgd 	/*
    125  1.19       cgd 	 * Set divisors to 1 (normal case) and let the machine-specific
    126  1.19       cgd 	 * code do its bit.
    127  1.19       cgd 	 */
    128  1.19       cgd 	psdiv = pscnt = 1;
    129  1.19       cgd 	cpu_initclocks();
    130  1.19       cgd 
    131  1.19       cgd 	/*
    132  1.19       cgd 	 * Compute profhz/stathz, and fix profhz if needed.
    133  1.19       cgd 	 */
    134  1.19       cgd 	i = stathz ? stathz : hz;
    135  1.19       cgd 	if (profhz == 0)
    136  1.19       cgd 		profhz = i;
    137  1.19       cgd 	psratio = profhz / i;
    138  1.19       cgd }
    139  1.19       cgd 
    140  1.19       cgd /*
    141  1.19       cgd  * The real-time timer, interrupting hz times per second.
    142  1.19       cgd  */
    143  1.19       cgd void
    144  1.19       cgd hardclock(frame)
    145  1.19       cgd 	register struct clockframe *frame;
    146  1.19       cgd {
    147  1.19       cgd 	register struct callout *p1;
    148  1.19       cgd 	register struct proc *p;
    149  1.19       cgd 	register int delta, needsoft;
    150  1.19       cgd 	extern int tickdelta;
    151  1.19       cgd 	extern long timedelta;
    152  1.19       cgd 
    153  1.19       cgd 	/*
    154  1.19       cgd 	 * Update real-time timeout queue.
    155  1.19       cgd 	 * At front of queue are some number of events which are ``due''.
    156  1.19       cgd 	 * The time to these is <= 0 and if negative represents the
    157  1.19       cgd 	 * number of ticks which have passed since it was supposed to happen.
    158  1.19       cgd 	 * The rest of the q elements (times > 0) are events yet to happen,
    159  1.19       cgd 	 * where the time for each is given as a delta from the previous.
    160  1.19       cgd 	 * Decrementing just the first of these serves to decrement the time
    161  1.19       cgd 	 * to all events.
    162  1.19       cgd 	 */
    163  1.19       cgd 	needsoft = 0;
    164  1.19       cgd 	for (p1 = calltodo.c_next; p1 != NULL; p1 = p1->c_next) {
    165  1.19       cgd 		if (--p1->c_time > 0)
    166  1.19       cgd 			break;
    167  1.19       cgd 		needsoft = 1;
    168  1.19       cgd 		if (p1->c_time == 0)
    169  1.19       cgd 			break;
    170  1.19       cgd 	}
    171  1.19       cgd 
    172  1.19       cgd 	p = curproc;
    173  1.19       cgd 	if (p) {
    174  1.19       cgd 		register struct pstats *pstats;
    175  1.19       cgd 
    176  1.19       cgd 		/*
    177  1.19       cgd 		 * Run current process's virtual and profile time, as needed.
    178  1.19       cgd 		 */
    179  1.19       cgd 		pstats = p->p_stats;
    180  1.19       cgd 		if (CLKF_USERMODE(frame) &&
    181  1.19       cgd 		    timerisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
    182  1.19       cgd 		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
    183  1.19       cgd 			psignal(p, SIGVTALRM);
    184  1.19       cgd 		if (timerisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
    185  1.19       cgd 		    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
    186  1.19       cgd 			psignal(p, SIGPROF);
    187  1.19       cgd 	}
    188  1.19       cgd 
    189  1.19       cgd 	/*
    190  1.19       cgd 	 * If no separate statistics clock is available, run it from here.
    191  1.19       cgd 	 */
    192  1.19       cgd 	if (stathz == 0)
    193  1.19       cgd 		statclock(frame);
    194  1.19       cgd 
    195  1.19       cgd 	/*
    196  1.22       cgd 	 * Increment the time-of-day.  The increment is normally just
    197  1.22       cgd 	 * ``tick''.  If the machine is one which has a clock frequency
    198  1.22       cgd 	 * such that ``hz'' would not divide the second evenly into
    199  1.22       cgd 	 * milliseconds, a periodic adjustment must be applied.  Finally,
    200  1.22       cgd 	 * if we are still adjusting the time (see adjtime()),
    201  1.22       cgd 	 * ``tickdelta'' may also be added in.
    202  1.19       cgd 	 */
    203  1.19       cgd 	ticks++;
    204  1.22       cgd 	delta = tick;
    205  1.22       cgd 	if (tickfix) {
    206  1.22       cgd 		tickfixcnt++;
    207  1.24       cgd 		if (tickfixcnt >= tickfixinterval) {
    208  1.22       cgd 			delta += tickfix;
    209  1.22       cgd 			tickfixcnt = 0;
    210  1.22       cgd 		}
    211  1.22       cgd 	}
    212  1.22       cgd 	if (timedelta != 0) {
    213  1.19       cgd 		delta = tick + tickdelta;
    214  1.19       cgd 		timedelta -= tickdelta;
    215  1.19       cgd 	}
    216  1.19       cgd 	BUMPTIME(&time, delta);
    217  1.19       cgd 	BUMPTIME(&mono_time, delta);
    218  1.19       cgd 
    219  1.19       cgd 	/*
    220  1.19       cgd 	 * Process callouts at a very low cpu priority, so we don't keep the
    221  1.19       cgd 	 * relatively high clock interrupt priority any longer than necessary.
    222  1.19       cgd 	 */
    223  1.19       cgd 	if (needsoft) {
    224  1.19       cgd 		if (CLKF_BASEPRI(frame)) {
    225  1.19       cgd 			/*
    226  1.19       cgd 			 * Save the overhead of a software interrupt;
    227  1.19       cgd 			 * it will happen as soon as we return, so do it now.
    228  1.19       cgd 			 */
    229  1.19       cgd 			(void)splsoftclock();
    230  1.19       cgd 			softclock();
    231  1.19       cgd 		} else
    232  1.19       cgd 			setsoftclock();
    233  1.19       cgd 	}
    234  1.19       cgd }
    235  1.19       cgd 
    236  1.19       cgd /*
    237  1.19       cgd  * Software (low priority) clock interrupt.
    238  1.19       cgd  * Run periodic events from timeout queue.
    239  1.19       cgd  */
    240  1.19       cgd /*ARGSUSED*/
    241  1.19       cgd void
    242  1.19       cgd softclock()
    243  1.19       cgd {
    244  1.19       cgd 	register struct callout *c;
    245  1.19       cgd 	register void *arg;
    246  1.19       cgd 	register void (*func) __P((void *));
    247  1.19       cgd 	register int s;
    248  1.19       cgd 
    249  1.19       cgd 	s = splhigh();
    250  1.19       cgd 	while ((c = calltodo.c_next) != NULL && c->c_time <= 0) {
    251  1.19       cgd 		func = c->c_func;
    252  1.19       cgd 		arg = c->c_arg;
    253  1.19       cgd 		calltodo.c_next = c->c_next;
    254  1.19       cgd 		c->c_next = callfree;
    255  1.19       cgd 		callfree = c;
    256  1.19       cgd 		splx(s);
    257  1.19       cgd 		(*func)(arg);
    258  1.19       cgd 		(void) splhigh();
    259  1.19       cgd 	}
    260  1.19       cgd 	splx(s);
    261  1.19       cgd }
    262  1.19       cgd 
    263  1.19       cgd /*
    264  1.19       cgd  * timeout --
    265  1.19       cgd  *	Execute a function after a specified length of time.
    266  1.19       cgd  *
    267  1.19       cgd  * untimeout --
    268  1.19       cgd  *	Cancel previous timeout function call.
    269  1.19       cgd  *
    270  1.19       cgd  *	See AT&T BCI Driver Reference Manual for specification.  This
    271  1.19       cgd  *	implementation differs from that one in that no identification
    272  1.19       cgd  *	value is returned from timeout, rather, the original arguments
    273  1.19       cgd  *	to timeout are used to identify entries for untimeout.
    274  1.19       cgd  */
    275  1.19       cgd void
    276  1.19       cgd timeout(ftn, arg, ticks)
    277  1.19       cgd 	void (*ftn) __P((void *));
    278  1.19       cgd 	void *arg;
    279  1.19       cgd 	register int ticks;
    280  1.19       cgd {
    281  1.19       cgd 	register struct callout *new, *p, *t;
    282  1.19       cgd 	register int s;
    283  1.19       cgd 
    284  1.19       cgd 	if (ticks <= 0)
    285  1.19       cgd 		ticks = 1;
    286  1.19       cgd 
    287  1.19       cgd 	/* Lock out the clock. */
    288  1.19       cgd 	s = splhigh();
    289  1.19       cgd 
    290  1.19       cgd 	/* Fill in the next free callout structure. */
    291  1.19       cgd 	if (callfree == NULL)
    292  1.19       cgd 		panic("timeout table full");
    293  1.19       cgd 	new = callfree;
    294  1.19       cgd 	callfree = new->c_next;
    295  1.19       cgd 	new->c_arg = arg;
    296  1.19       cgd 	new->c_func = ftn;
    297  1.19       cgd 
    298  1.19       cgd 	/*
    299  1.19       cgd 	 * The time for each event is stored as a difference from the time
    300  1.19       cgd 	 * of the previous event on the queue.  Walk the queue, correcting
    301  1.19       cgd 	 * the ticks argument for queue entries passed.  Correct the ticks
    302  1.19       cgd 	 * value for the queue entry immediately after the insertion point
    303  1.19       cgd 	 * as well.  Watch out for negative c_time values; these represent
    304  1.19       cgd 	 * overdue events.
    305  1.19       cgd 	 */
    306  1.19       cgd 	for (p = &calltodo;
    307  1.19       cgd 	    (t = p->c_next) != NULL && ticks > t->c_time; p = t)
    308  1.19       cgd 		if (t->c_time > 0)
    309  1.19       cgd 			ticks -= t->c_time;
    310  1.19       cgd 	new->c_time = ticks;
    311  1.19       cgd 	if (t != NULL)
    312  1.19       cgd 		t->c_time -= ticks;
    313  1.19       cgd 
    314  1.19       cgd 	/* Insert the new entry into the queue. */
    315  1.19       cgd 	p->c_next = new;
    316  1.19       cgd 	new->c_next = t;
    317  1.19       cgd 	splx(s);
    318  1.19       cgd }
    319  1.19       cgd 
    320  1.19       cgd void
    321  1.19       cgd untimeout(ftn, arg)
    322  1.19       cgd 	void (*ftn) __P((void *));
    323  1.19       cgd 	void *arg;
    324  1.19       cgd {
    325  1.19       cgd 	register struct callout *p, *t;
    326  1.19       cgd 	register int s;
    327  1.19       cgd 
    328  1.19       cgd 	s = splhigh();
    329  1.19       cgd 	for (p = &calltodo; (t = p->c_next) != NULL; p = t)
    330  1.19       cgd 		if (t->c_func == ftn && t->c_arg == arg) {
    331  1.19       cgd 			/* Increment next entry's tick count. */
    332  1.19       cgd 			if (t->c_next && t->c_time > 0)
    333  1.19       cgd 				t->c_next->c_time += t->c_time;
    334  1.19       cgd 
    335  1.19       cgd 			/* Move entry from callout queue to callfree queue. */
    336  1.19       cgd 			p->c_next = t->c_next;
    337  1.19       cgd 			t->c_next = callfree;
    338  1.19       cgd 			callfree = t;
    339  1.19       cgd 			break;
    340  1.19       cgd 		}
    341  1.19       cgd 	splx(s);
    342  1.19       cgd }
    343  1.19       cgd 
    344  1.19       cgd /*
    345  1.19       cgd  * Compute number of hz until specified time.  Used to
    346  1.19       cgd  * compute third argument to timeout() from an absolute time.
    347  1.19       cgd  */
    348  1.19       cgd int
    349  1.19       cgd hzto(tv)
    350  1.19       cgd 	struct timeval *tv;
    351  1.19       cgd {
    352  1.19       cgd 	register long ticks, sec;
    353  1.19       cgd 	int s;
    354  1.19       cgd 
    355  1.19       cgd 	/*
    356  1.22       cgd 	 * If number of microseconds will fit in 32 bit arithmetic,
    357  1.22       cgd 	 * then compute number of microseconds to time and scale to
    358  1.19       cgd 	 * ticks.  Otherwise just compute number of hz in time, rounding
    359  1.22       cgd 	 * times greater than representible to maximum value.  (We must
    360  1.22       cgd 	 * compute in microseconds, because hz can be greater than 1000,
    361  1.22       cgd 	 * and thus tick can be less than one millisecond).
    362  1.19       cgd 	 *
    363  1.22       cgd 	 * Delta times less than 14 hours can be computed ``exactly''.
    364  1.22       cgd 	 * (Note that if hz would yeild a non-integral number of us per
    365  1.22       cgd 	 * tick, i.e. tickfix is nonzero, timouts can be a tick longer
    366  1.22       cgd 	 * than they should be.)  Maximum value for any timeout in 10ms
    367  1.22       cgd 	 * ticks is 250 days.
    368  1.19       cgd 	 */
    369  1.19       cgd 	s = splhigh();
    370  1.19       cgd 	sec = tv->tv_sec - time.tv_sec;
    371  1.22       cgd 	if (sec <= 0x7fffffff / 1000000 - 1)
    372  1.22       cgd 		ticks = ((tv->tv_sec - time.tv_sec) * 1000000 +
    373  1.22       cgd 			(tv->tv_usec - time.tv_usec)) / tick;
    374  1.19       cgd 	else if (sec <= 0x7fffffff / hz)
    375  1.19       cgd 		ticks = sec * hz;
    376  1.19       cgd 	else
    377  1.19       cgd 		ticks = 0x7fffffff;
    378  1.19       cgd 	splx(s);
    379  1.19       cgd 	return (ticks);
    380  1.19       cgd }
    381  1.19       cgd 
    382  1.19       cgd /*
    383  1.19       cgd  * Start profiling on a process.
    384  1.19       cgd  *
    385  1.19       cgd  * Kernel profiling passes proc0 which never exits and hence
    386  1.19       cgd  * keeps the profile clock running constantly.
    387  1.19       cgd  */
    388  1.19       cgd void
    389  1.19       cgd startprofclock(p)
    390  1.19       cgd 	register struct proc *p;
    391  1.19       cgd {
    392  1.19       cgd 	int s;
    393  1.19       cgd 
    394  1.19       cgd 	if ((p->p_flag & P_PROFIL) == 0) {
    395  1.19       cgd 		p->p_flag |= P_PROFIL;
    396  1.19       cgd 		if (++profprocs == 1 && stathz != 0) {
    397  1.19       cgd 			s = splstatclock();
    398  1.19       cgd 			psdiv = pscnt = psratio;
    399  1.19       cgd 			setstatclockrate(profhz);
    400  1.19       cgd 			splx(s);
    401  1.19       cgd 		}
    402  1.19       cgd 	}
    403  1.19       cgd }
    404  1.19       cgd 
    405  1.19       cgd /*
    406  1.19       cgd  * Stop profiling on a process.
    407  1.19       cgd  */
    408  1.19       cgd void
    409  1.19       cgd stopprofclock(p)
    410  1.19       cgd 	register struct proc *p;
    411  1.19       cgd {
    412  1.19       cgd 	int s;
    413  1.19       cgd 
    414  1.19       cgd 	if (p->p_flag & P_PROFIL) {
    415  1.19       cgd 		p->p_flag &= ~P_PROFIL;
    416  1.19       cgd 		if (--profprocs == 0 && stathz != 0) {
    417  1.19       cgd 			s = splstatclock();
    418  1.19       cgd 			psdiv = pscnt = 1;
    419  1.19       cgd 			setstatclockrate(stathz);
    420  1.19       cgd 			splx(s);
    421  1.19       cgd 		}
    422  1.19       cgd 	}
    423  1.19       cgd }
    424  1.19       cgd 
    425  1.19       cgd /*
    426  1.19       cgd  * Statistics clock.  Grab profile sample, and if divider reaches 0,
    427  1.19       cgd  * do process and kernel statistics.
    428  1.19       cgd  */
    429  1.19       cgd void
    430  1.19       cgd statclock(frame)
    431  1.19       cgd 	register struct clockframe *frame;
    432  1.19       cgd {
    433  1.19       cgd #ifdef GPROF
    434  1.19       cgd 	register struct gmonparam *g;
    435  1.19       cgd #endif
    436  1.19       cgd 	register struct proc *p;
    437  1.19       cgd 	register int i;
    438  1.19       cgd 
    439  1.19       cgd 	if (CLKF_USERMODE(frame)) {
    440  1.19       cgd 		p = curproc;
    441  1.19       cgd 		if (p->p_flag & P_PROFIL)
    442  1.19       cgd 			addupc_intr(p, CLKF_PC(frame), 1);
    443  1.19       cgd 		if (--pscnt > 0)
    444  1.19       cgd 			return;
    445  1.19       cgd 		/*
    446  1.19       cgd 		 * Came from user mode; CPU was in user state.
    447  1.19       cgd 		 * If this process is being profiled record the tick.
    448  1.19       cgd 		 */
    449  1.19       cgd 		p->p_uticks++;
    450  1.19       cgd 		if (p->p_nice > NZERO)
    451  1.19       cgd 			cp_time[CP_NICE]++;
    452  1.19       cgd 		else
    453  1.19       cgd 			cp_time[CP_USER]++;
    454  1.19       cgd 	} else {
    455  1.19       cgd #ifdef GPROF
    456  1.19       cgd 		/*
    457  1.19       cgd 		 * Kernel statistics are just like addupc_intr, only easier.
    458  1.19       cgd 		 */
    459  1.19       cgd 		g = &_gmonparam;
    460  1.19       cgd 		if (g->state == GMON_PROF_ON) {
    461  1.19       cgd 			i = CLKF_PC(frame) - g->lowpc;
    462  1.19       cgd 			if (i < g->textsize) {
    463  1.19       cgd 				i /= HISTFRACTION * sizeof(*g->kcount);
    464  1.19       cgd 				g->kcount[i]++;
    465  1.19       cgd 			}
    466  1.19       cgd 		}
    467  1.19       cgd #endif
    468  1.19       cgd 		if (--pscnt > 0)
    469  1.19       cgd 			return;
    470  1.19       cgd 		/*
    471  1.19       cgd 		 * Came from kernel mode, so we were:
    472  1.19       cgd 		 * - handling an interrupt,
    473  1.19       cgd 		 * - doing syscall or trap work on behalf of the current
    474  1.19       cgd 		 *   user process, or
    475  1.19       cgd 		 * - spinning in the idle loop.
    476  1.19       cgd 		 * Whichever it is, charge the time as appropriate.
    477  1.19       cgd 		 * Note that we charge interrupts to the current process,
    478  1.19       cgd 		 * regardless of whether they are ``for'' that process,
    479  1.19       cgd 		 * so that we know how much of its real time was spent
    480  1.19       cgd 		 * in ``non-process'' (i.e., interrupt) work.
    481  1.19       cgd 		 */
    482  1.19       cgd 		p = curproc;
    483  1.19       cgd 		if (CLKF_INTR(frame)) {
    484  1.19       cgd 			if (p != NULL)
    485  1.19       cgd 				p->p_iticks++;
    486  1.19       cgd 			cp_time[CP_INTR]++;
    487  1.19       cgd 		} else if (p != NULL) {
    488  1.19       cgd 			p->p_sticks++;
    489  1.19       cgd 			cp_time[CP_SYS]++;
    490  1.19       cgd 		} else
    491  1.19       cgd 			cp_time[CP_IDLE]++;
    492  1.19       cgd 	}
    493  1.19       cgd 	pscnt = psdiv;
    494  1.19       cgd 
    495  1.19       cgd 	/*
    496  1.23   thorpej 	 * XXX Support old-style instrumentation for now.
    497  1.23   thorpej 	 *
    498  1.19       cgd 	 * We maintain statistics shown by user-level statistics
    499  1.19       cgd 	 * programs:  the amount of time in each cpu state, and
    500  1.19       cgd 	 * the amount of time each of DK_NDRIVE ``drives'' is busy.
    501  1.19       cgd 	 *
    502  1.19       cgd 	 * XXX	should either run linked list of drives, or (better)
    503  1.19       cgd 	 *	grab timestamps in the start & done code.
    504  1.19       cgd 	 */
    505  1.19       cgd 	for (i = 0; i < DK_NDRIVE; i++)
    506  1.19       cgd 		if (dk_busy & (1 << i))
    507  1.19       cgd 			dk_time[i]++;
    508  1.19       cgd 
    509  1.19       cgd 	/*
    510  1.19       cgd 	 * We adjust the priority of the current process.  The priority of
    511  1.19       cgd 	 * a process gets worse as it accumulates CPU time.  The cpu usage
    512  1.19       cgd 	 * estimator (p_estcpu) is increased here.  The formula for computing
    513  1.19       cgd 	 * priorities (in kern_synch.c) will compute a different value each
    514  1.19       cgd 	 * time p_estcpu increases by 4.  The cpu usage estimator ramps up
    515  1.19       cgd 	 * quite quickly when the process is running (linearly), and decays
    516  1.19       cgd 	 * away exponentially, at a rate which is proportionally slower when
    517  1.19       cgd 	 * the system is busy.  The basic principal is that the system will
    518  1.19       cgd 	 * 90% forget that the process used a lot of CPU time in 5 * loadav
    519  1.19       cgd 	 * seconds.  This causes the system to favor processes which haven't
    520  1.19       cgd 	 * run much recently, and to round-robin among other processes.
    521  1.19       cgd 	 */
    522  1.19       cgd 	if (p != NULL) {
    523  1.19       cgd 		p->p_cpticks++;
    524  1.19       cgd 		if (++p->p_estcpu == 0)
    525  1.19       cgd 			p->p_estcpu--;
    526  1.19       cgd 		if ((p->p_estcpu & 3) == 0) {
    527  1.19       cgd 			resetpriority(p);
    528  1.19       cgd 			if (p->p_priority >= PUSER)
    529  1.19       cgd 				p->p_priority = p->p_usrpri;
    530  1.19       cgd 		}
    531  1.19       cgd 	}
    532  1.19       cgd }
    533  1.19       cgd 
    534  1.19       cgd /*
    535  1.19       cgd  * Return information about system clocks.
    536  1.19       cgd  */
    537  1.25  christos int
    538  1.19       cgd sysctl_clockrate(where, sizep)
    539  1.19       cgd 	register char *where;
    540  1.19       cgd 	size_t *sizep;
    541  1.19       cgd {
    542  1.19       cgd 	struct clockinfo clkinfo;
    543  1.19       cgd 
    544  1.19       cgd 	/*
    545  1.19       cgd 	 * Construct clockinfo structure.
    546  1.19       cgd 	 */
    547  1.20   mycroft 	clkinfo.tick = tick;
    548  1.20   mycroft 	clkinfo.tickadj = tickadj;
    549  1.19       cgd 	clkinfo.hz = hz;
    550  1.19       cgd 	clkinfo.profhz = profhz;
    551  1.19       cgd 	clkinfo.stathz = stathz ? stathz : hz;
    552  1.19       cgd 	return (sysctl_rdstruct(where, sizep, NULL, &clkinfo, sizeof(clkinfo)));
    553  1.19       cgd }
    554  1.19       cgd 
    555  1.19       cgd #ifdef DDB
    556  1.21   mycroft #include <machine/db_machdep.h>
    557  1.21   mycroft 
    558  1.25  christos #include <ddb/db_interface.h>
    559  1.19       cgd #include <ddb/db_access.h>
    560  1.19       cgd #include <ddb/db_sym.h>
    561  1.25  christos #include <ddb/db_output.h>
    562  1.19       cgd 
    563  1.25  christos void db_show_callout(addr, haddr, count, modif)
    564  1.25  christos 	db_expr_t addr;
    565  1.25  christos 	int haddr;
    566  1.25  christos 	db_expr_t count;
    567  1.25  christos 	char *modif;
    568  1.19       cgd {
    569  1.19       cgd 	register struct callout *p1;
    570  1.19       cgd 	register int	cum;
    571  1.19       cgd 	register int	s;
    572  1.19       cgd 	db_expr_t	offset;
    573  1.19       cgd 	char		*name;
    574  1.19       cgd 
    575  1.19       cgd         db_printf("      cum     ticks      arg  func\n");
    576  1.19       cgd 	s = splhigh();
    577  1.19       cgd 	for (cum = 0, p1 = calltodo.c_next; p1; p1 = p1->c_next) {
    578  1.19       cgd 		register int t = p1->c_time;
    579  1.19       cgd 
    580  1.19       cgd 		if (t > 0)
    581  1.19       cgd 			cum += t;
    582  1.19       cgd 
    583  1.21   mycroft 		db_find_sym_and_offset((db_addr_t)p1->c_func, &name, &offset);
    584  1.19       cgd 		if (name == NULL)
    585  1.19       cgd 			name = "?";
    586  1.19       cgd 
    587  1.19       cgd                 db_printf("%9d %9d %8x  %s (%x)\n",
    588  1.19       cgd 			  cum, t, p1->c_arg, name, p1->c_func);
    589  1.19       cgd 	}
    590  1.19       cgd 	splx(s);
    591  1.19       cgd }
    592  1.19       cgd #endif
    593