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