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