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