Home | History | Annotate | Line # | Download | only in kern
kern_clock.c revision 1.120
      1 /*	$NetBSD: kern_clock.c,v 1.120 2008/04/21 00:13:46 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  * This code is derived from software contributed to The NetBSD Foundation
     11  * by Charles M. Hannum.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by the NetBSD
     24  *	Foundation, Inc. and its contributors.
     25  * 4. Neither the name of The NetBSD Foundation nor the names of its
     26  *    contributors may be used to endorse or promote products derived
     27  *    from this software without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     39  * POSSIBILITY OF SUCH DAMAGE.
     40  */
     41 
     42 /*-
     43  * Copyright (c) 1982, 1986, 1991, 1993
     44  *	The Regents of the University of California.  All rights reserved.
     45  * (c) UNIX System Laboratories, Inc.
     46  * All or some portions of this file are derived from material licensed
     47  * to the University of California by American Telephone and Telegraph
     48  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     49  * the permission of UNIX System Laboratories, Inc.
     50  *
     51  * Redistribution and use in source and binary forms, with or without
     52  * modification, are permitted provided that the following conditions
     53  * are met:
     54  * 1. Redistributions of source code must retain the above copyright
     55  *    notice, this list of conditions and the following disclaimer.
     56  * 2. Redistributions in binary form must reproduce the above copyright
     57  *    notice, this list of conditions and the following disclaimer in the
     58  *    documentation and/or other materials provided with the distribution.
     59  * 3. Neither the name of the University nor the names of its contributors
     60  *    may be used to endorse or promote products derived from this software
     61  *    without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  * SUCH DAMAGE.
     74  *
     75  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
     76  */
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.120 2008/04/21 00:13:46 ad Exp $");
     80 
     81 #include "opt_ntp.h"
     82 #include "opt_multiprocessor.h"
     83 #include "opt_perfctrs.h"
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/callout.h>
     88 #include <sys/kernel.h>
     89 #include <sys/proc.h>
     90 #include <sys/resourcevar.h>
     91 #include <sys/signalvar.h>
     92 #include <sys/sysctl.h>
     93 #include <sys/timex.h>
     94 #include <sys/sched.h>
     95 #include <sys/time.h>
     96 #include <sys/timetc.h>
     97 #include <sys/cpu.h>
     98 #include <sys/atomic.h>
     99 
    100 #include <uvm/uvm_extern.h>
    101 
    102 #ifdef GPROF
    103 #include <sys/gmon.h>
    104 #endif
    105 
    106 /*
    107  * Clock handling routines.
    108  *
    109  * This code is written to operate with two timers that run independently of
    110  * each other.  The main clock, running hz times per second, is used to keep
    111  * track of real time.  The second timer handles kernel and user profiling,
    112  * and does resource use estimation.  If the second timer is programmable,
    113  * it is randomized to avoid aliasing between the two clocks.  For example,
    114  * the randomization prevents an adversary from always giving up the CPU
    115  * just before its quantum expires.  Otherwise, it would never accumulate
    116  * CPU ticks.  The mean frequency of the second timer is stathz.
    117  *
    118  * If no second timer exists, stathz will be zero; in this case we drive
    119  * profiling and statistics off the main clock.  This WILL NOT be accurate;
    120  * do not do it unless absolutely necessary.
    121  *
    122  * The statistics clock may (or may not) be run at a higher rate while
    123  * profiling.  This profile clock runs at profhz.  We require that profhz
    124  * be an integral multiple of stathz.
    125  *
    126  * If the statistics clock is running fast, it must be divided by the ratio
    127  * profhz/stathz for statistics.  (For profiling, every tick counts.)
    128  */
    129 
    130 int	stathz;
    131 int	profhz;
    132 int	profsrc;
    133 int	schedhz;
    134 int	profprocs;
    135 int	hardclock_ticks;
    136 static int hardscheddiv; /* hard => sched divider (used if schedhz == 0) */
    137 static int psdiv;			/* prof => stat divider */
    138 int	psratio;			/* ratio: prof / stat */
    139 
    140 static u_int get_intr_timecount(struct timecounter *);
    141 
    142 static struct timecounter intr_timecounter = {
    143 	get_intr_timecount,	/* get_timecount */
    144 	0,			/* no poll_pps */
    145 	~0u,			/* counter_mask */
    146 	0,		        /* frequency */
    147 	"clockinterrupt",	/* name */
    148 	0,			/* quality - minimum implementation level for a clock */
    149 	NULL,			/* prev */
    150 	NULL,			/* next */
    151 };
    152 
    153 static u_int
    154 get_intr_timecount(struct timecounter *tc)
    155 {
    156 
    157 	return (u_int)hardclock_ticks;
    158 }
    159 
    160 /*
    161  * Initialize clock frequencies and start both clocks running.
    162  */
    163 void
    164 initclocks(void)
    165 {
    166 	int i;
    167 
    168 	/*
    169 	 * Set divisors to 1 (normal case) and let the machine-specific
    170 	 * code do its bit.
    171 	 */
    172 	psdiv = 1;
    173 	/*
    174 	 * provide minimum default time counter
    175 	 * will only run at interrupt resolution
    176 	 */
    177 	intr_timecounter.tc_frequency = hz;
    178 	tc_init(&intr_timecounter);
    179 	cpu_initclocks();
    180 
    181 	/*
    182 	 * Compute profhz and stathz, fix profhz if needed.
    183 	 */
    184 	i = stathz ? stathz : hz;
    185 	if (profhz == 0)
    186 		profhz = i;
    187 	psratio = profhz / i;
    188 	if (schedhz == 0) {
    189 		/* 16Hz is best */
    190 		hardscheddiv = hz / 16;
    191 		if (hardscheddiv <= 0)
    192 			panic("hardscheddiv");
    193 	}
    194 
    195 }
    196 
    197 /*
    198  * The real-time timer, interrupting hz times per second.
    199  */
    200 void
    201 hardclock(struct clockframe *frame)
    202 {
    203 	struct lwp *l;
    204 	struct cpu_info *ci;
    205 
    206 	ci = curcpu();
    207 	l = ci->ci_data.cpu_onproc;
    208 
    209 	timer_tick(l, CLKF_USERMODE(frame));
    210 
    211 	/*
    212 	 * If no separate statistics clock is available, run it from here.
    213 	 */
    214 	if (stathz == 0)
    215 		statclock(frame);
    216 	/*
    217 	 * If no separate schedclock is provided, call it here
    218 	 * at about 16 Hz.
    219 	 */
    220 	if (schedhz == 0) {
    221 		if ((int)(--ci->ci_schedstate.spc_schedticks) <= 0) {
    222 			schedclock(l);
    223 			ci->ci_schedstate.spc_schedticks = hardscheddiv;
    224 		}
    225 	}
    226 	if ((--ci->ci_schedstate.spc_ticks) <= 0)
    227 		sched_tick(ci);
    228 
    229 #if defined(MULTIPROCESSOR)
    230 	/*
    231 	 * If we are not the primary CPU, we're not allowed to do
    232 	 * any more work.
    233 	 */
    234 	if (CPU_IS_PRIMARY(ci) == 0)
    235 		return;
    236 #endif
    237 
    238 	hardclock_ticks++;
    239 
    240 	tc_ticktock();
    241 
    242 	/*
    243 	 * Update real-time timeout queue.  Callouts are processed at a
    244 	 * very low CPU priority, so we don't keep the relatively high
    245 	 * clock interrupt priority any longer than necessary.
    246 	 */
    247 	callout_hardclock();
    248 }
    249 
    250 /*
    251  * Start profiling on a process.
    252  *
    253  * Kernel profiling passes proc0 which never exits and hence
    254  * keeps the profile clock running constantly.
    255  */
    256 void
    257 startprofclock(struct proc *p)
    258 {
    259 
    260 	KASSERT(mutex_owned(&p->p_stmutex));
    261 
    262 	if ((p->p_stflag & PST_PROFIL) == 0) {
    263 		p->p_stflag |= PST_PROFIL;
    264 		/*
    265 		 * This is only necessary if using the clock as the
    266 		 * profiling source.
    267 		 */
    268 		if (++profprocs == 1 && stathz != 0)
    269 			psdiv = psratio;
    270 	}
    271 }
    272 
    273 /*
    274  * Stop profiling on a process.
    275  */
    276 void
    277 stopprofclock(struct proc *p)
    278 {
    279 
    280 	KASSERT(mutex_owned(&p->p_stmutex));
    281 
    282 	if (p->p_stflag & PST_PROFIL) {
    283 		p->p_stflag &= ~PST_PROFIL;
    284 		/*
    285 		 * This is only necessary if using the clock as the
    286 		 * profiling source.
    287 		 */
    288 		if (--profprocs == 0 && stathz != 0)
    289 			psdiv = 1;
    290 	}
    291 }
    292 
    293 #if defined(PERFCTRS)
    294 /*
    295  * Independent profiling "tick" in case we're using a separate
    296  * clock or profiling event source.  Currently, that's just
    297  * performance counters--hence the wrapper.
    298  */
    299 void
    300 proftick(struct clockframe *frame)
    301 {
    302 #ifdef GPROF
    303         struct gmonparam *g;
    304         intptr_t i;
    305 #endif
    306 	struct lwp *l;
    307 	struct proc *p;
    308 
    309 	l = curcpu()->ci_data.cpu_onproc;
    310 	p = (l ? l->l_proc : NULL);
    311 	if (CLKF_USERMODE(frame)) {
    312 		mutex_spin_enter(&p->p_stmutex);
    313 		if (p->p_stflag & PST_PROFIL)
    314 			addupc_intr(l, CLKF_PC(frame));
    315 		mutex_spin_exit(&p->p_stmutex);
    316 	} else {
    317 #ifdef GPROF
    318 		g = &_gmonparam;
    319 		if (g->state == GMON_PROF_ON) {
    320 			i = CLKF_PC(frame) - g->lowpc;
    321 			if (i < g->textsize) {
    322 				i /= HISTFRACTION * sizeof(*g->kcount);
    323 				g->kcount[i]++;
    324 			}
    325 		}
    326 #endif
    327 #ifdef LWP_PC
    328 		if (p != NULL && (p->p_stflag & PST_PROFIL) != 0)
    329 			addupc_intr(l, LWP_PC(l));
    330 #endif
    331 	}
    332 }
    333 #endif
    334 
    335 void
    336 schedclock(struct lwp *l)
    337 {
    338 	struct cpu_info *ci;
    339 
    340 	ci = l->l_cpu;
    341 
    342 	/* Accumulate syscall and context switch counts. */
    343 	atomic_add_int((unsigned *)&uvmexp.swtch, ci->ci_data.cpu_nswtch);
    344 	ci->ci_data.cpu_nswtch = 0;
    345 	atomic_add_int((unsigned *)&uvmexp.syscalls, ci->ci_data.cpu_nsyscall);
    346 	ci->ci_data.cpu_nsyscall = 0;
    347 
    348 	if ((l->l_flag & LW_IDLE) != 0)
    349 		return;
    350 
    351 	sched_schedclock(l);
    352 }
    353 
    354 /*
    355  * Statistics clock.  Grab profile sample, and if divider reaches 0,
    356  * do process and kernel statistics.
    357  */
    358 void
    359 statclock(struct clockframe *frame)
    360 {
    361 #ifdef GPROF
    362 	struct gmonparam *g;
    363 	intptr_t i;
    364 #endif
    365 	struct cpu_info *ci = curcpu();
    366 	struct schedstate_percpu *spc = &ci->ci_schedstate;
    367 	struct proc *p;
    368 	struct lwp *l;
    369 
    370 	/*
    371 	 * Notice changes in divisor frequency, and adjust clock
    372 	 * frequency accordingly.
    373 	 */
    374 	if (spc->spc_psdiv != psdiv) {
    375 		spc->spc_psdiv = psdiv;
    376 		spc->spc_pscnt = psdiv;
    377 		if (psdiv == 1) {
    378 			setstatclockrate(stathz);
    379 		} else {
    380 			setstatclockrate(profhz);
    381 		}
    382 	}
    383 	l = ci->ci_data.cpu_onproc;
    384 	if ((l->l_flag & LW_IDLE) != 0) {
    385 		/*
    386 		 * don't account idle lwps as swapper.
    387 		 */
    388 		p = NULL;
    389 	} else {
    390 		p = l->l_proc;
    391 		mutex_spin_enter(&p->p_stmutex);
    392 	}
    393 
    394 	if (CLKF_USERMODE(frame)) {
    395 		if ((p->p_stflag & PST_PROFIL) && profsrc == PROFSRC_CLOCK)
    396 			addupc_intr(l, CLKF_PC(frame));
    397 		if (--spc->spc_pscnt > 0) {
    398 			mutex_spin_exit(&p->p_stmutex);
    399 			return;
    400 		}
    401 
    402 		/*
    403 		 * Came from user mode; CPU was in user state.
    404 		 * If this process is being profiled record the tick.
    405 		 */
    406 		p->p_uticks++;
    407 		if (p->p_nice > NZERO)
    408 			spc->spc_cp_time[CP_NICE]++;
    409 		else
    410 			spc->spc_cp_time[CP_USER]++;
    411 	} else {
    412 #ifdef GPROF
    413 		/*
    414 		 * Kernel statistics are just like addupc_intr, only easier.
    415 		 */
    416 		g = &_gmonparam;
    417 		if (profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {
    418 			i = CLKF_PC(frame) - g->lowpc;
    419 			if (i < g->textsize) {
    420 				i /= HISTFRACTION * sizeof(*g->kcount);
    421 				g->kcount[i]++;
    422 			}
    423 		}
    424 #endif
    425 #ifdef LWP_PC
    426 		if (p != NULL && profsrc == PROFSRC_CLOCK &&
    427 		    (p->p_stflag & PST_PROFIL)) {
    428 			addupc_intr(l, LWP_PC(l));
    429 		}
    430 #endif
    431 		if (--spc->spc_pscnt > 0) {
    432 			if (p != NULL)
    433 				mutex_spin_exit(&p->p_stmutex);
    434 			return;
    435 		}
    436 		/*
    437 		 * Came from kernel mode, so we were:
    438 		 * - handling an interrupt,
    439 		 * - doing syscall or trap work on behalf of the current
    440 		 *   user process, or
    441 		 * - spinning in the idle loop.
    442 		 * Whichever it is, charge the time as appropriate.
    443 		 * Note that we charge interrupts to the current process,
    444 		 * regardless of whether they are ``for'' that process,
    445 		 * so that we know how much of its real time was spent
    446 		 * in ``non-process'' (i.e., interrupt) work.
    447 		 */
    448 		if (CLKF_INTR(frame) || (curlwp->l_pflag & LP_INTR) != 0) {
    449 			if (p != NULL) {
    450 				p->p_iticks++;
    451 			}
    452 			spc->spc_cp_time[CP_INTR]++;
    453 		} else if (p != NULL) {
    454 			p->p_sticks++;
    455 			spc->spc_cp_time[CP_SYS]++;
    456 		} else {
    457 			spc->spc_cp_time[CP_IDLE]++;
    458 		}
    459 	}
    460 	spc->spc_pscnt = psdiv;
    461 
    462 	if (p != NULL) {
    463 		++l->l_cpticks;
    464 		mutex_spin_exit(&p->p_stmutex);
    465 	}
    466 }
    467