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