Home | History | Annotate | Line # | Download | only in kern
kern_clock.c revision 1.150
      1  1.150  riastrad /*	$NetBSD: kern_clock.c,v 1.150 2023/07/07 12:34:50 riastradh 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.150  riastrad __KERNEL_RCSID(0, "$NetBSD: kern_clock.c,v 1.150 2023/07/07 12:34:50 riastradh 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.150  riastrad #include "opt_heartbeat.h"
     78  1.145       ryo #include "opt_multiprocessor.h"
     79  1.133     pooka #endif
     80   1.19       cgd 
     81   1.19       cgd #include <sys/param.h>
     82   1.19       cgd #include <sys/systm.h>
     83   1.19       cgd #include <sys/callout.h>
     84   1.19       cgd #include <sys/kernel.h>
     85   1.19       cgd #include <sys/proc.h>
     86   1.19       cgd #include <sys/resourcevar.h>
     87   1.25  christos #include <sys/signalvar.h>
     88   1.26  christos #include <sys/sysctl.h>
     89   1.27  jonathan #include <sys/timex.h>
     90   1.45      ross #include <sys/sched.h>
     91   1.82   thorpej #include <sys/time.h>
     92   1.99    kardel #include <sys/timetc.h>
     93  1.109        ad #include <sys/cpu.h>
     94  1.118        ad #include <sys/atomic.h>
     95  1.144  riastrad #include <sys/rndsource.h>
     96  1.150  riastrad #include <sys/heartbeat.h>
     97  1.118        ad 
     98   1.19       cgd #ifdef GPROF
     99   1.19       cgd #include <sys/gmon.h>
    100   1.19       cgd #endif
    101   1.19       cgd 
    102  1.131       chs #ifdef KDTRACE_HOOKS
    103  1.131       chs #include <sys/dtrace_bsd.h>
    104  1.131       chs #include <sys/cpu.h>
    105  1.131       chs 
    106  1.131       chs cyclic_clock_func_t	cyclic_clock_func[MAXCPUS];
    107  1.131       chs #endif
    108  1.131       chs 
    109  1.132     pooka static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
    110  1.132     pooka 
    111   1.19       cgd /*
    112   1.19       cgd  * Clock handling routines.
    113   1.19       cgd  *
    114   1.19       cgd  * This code is written to operate with two timers that run independently of
    115   1.19       cgd  * each other.  The main clock, running hz times per second, is used to keep
    116   1.19       cgd  * track of real time.  The second timer handles kernel and user profiling,
    117   1.19       cgd  * and does resource use estimation.  If the second timer is programmable,
    118   1.19       cgd  * it is randomized to avoid aliasing between the two clocks.  For example,
    119   1.90       wiz  * the randomization prevents an adversary from always giving up the CPU
    120   1.19       cgd  * just before its quantum expires.  Otherwise, it would never accumulate
    121   1.90       wiz  * CPU ticks.  The mean frequency of the second timer is stathz.
    122   1.19       cgd  *
    123   1.19       cgd  * If no second timer exists, stathz will be zero; in this case we drive
    124   1.19       cgd  * profiling and statistics off the main clock.  This WILL NOT be accurate;
    125   1.19       cgd  * do not do it unless absolutely necessary.
    126   1.19       cgd  *
    127   1.19       cgd  * The statistics clock may (or may not) be run at a higher rate while
    128   1.19       cgd  * profiling.  This profile clock runs at profhz.  We require that profhz
    129   1.19       cgd  * be an integral multiple of stathz.
    130   1.19       cgd  *
    131   1.19       cgd  * If the statistics clock is running fast, it must be divided by the ratio
    132   1.19       cgd  * profhz/stathz for statistics.  (For profiling, every tick counts.)
    133   1.19       cgd  */
    134   1.19       cgd 
    135   1.19       cgd int	stathz;
    136   1.19       cgd int	profhz;
    137   1.80    briggs int	profsrc;
    138   1.75    simonb int	schedhz;
    139   1.19       cgd int	profprocs;
    140  1.148  riastrad static int hardclock_ticks;
    141  1.114        ad static int hardscheddiv; /* hard => sched divider (used if schedhz == 0) */
    142   1.70  sommerfe static int psdiv;			/* prof => stat divider */
    143   1.22       cgd int	psratio;			/* ratio: prof / stat */
    144   1.19       cgd 
    145  1.144  riastrad struct clockrnd {
    146  1.144  riastrad 	struct krndsource source;
    147  1.144  riastrad 	unsigned needed;
    148  1.144  riastrad };
    149  1.144  riastrad 
    150  1.144  riastrad static struct clockrnd hardclockrnd __aligned(COHERENCY_UNIT);
    151  1.144  riastrad static struct clockrnd statclockrnd __aligned(COHERENCY_UNIT);
    152  1.144  riastrad 
    153  1.144  riastrad static void
    154  1.144  riastrad clockrnd_get(size_t needed, void *cookie)
    155  1.144  riastrad {
    156  1.144  riastrad 	struct clockrnd *C = cookie;
    157  1.144  riastrad 
    158  1.144  riastrad 	/* Start sampling.  */
    159  1.144  riastrad 	atomic_store_relaxed(&C->needed, 2*NBBY*needed);
    160  1.144  riastrad }
    161  1.144  riastrad 
    162  1.144  riastrad static void
    163  1.144  riastrad clockrnd_sample(struct clockrnd *C)
    164  1.144  riastrad {
    165  1.144  riastrad 	struct cpu_info *ci = curcpu();
    166  1.144  riastrad 
    167  1.144  riastrad 	/* If there's nothing needed right now, stop here.  */
    168  1.147  riastrad 	if (__predict_true(atomic_load_relaxed(&C->needed) == 0))
    169  1.144  riastrad 		return;
    170  1.144  riastrad 
    171  1.144  riastrad 	/*
    172  1.144  riastrad 	 * If we're not the primary core of a package, we're probably
    173  1.144  riastrad 	 * driven by the same clock as the primary core, so don't
    174  1.144  riastrad 	 * bother.
    175  1.144  riastrad 	 */
    176  1.144  riastrad 	if (ci != ci->ci_package1st)
    177  1.144  riastrad 		return;
    178  1.144  riastrad 
    179  1.144  riastrad 	/* Take a sample and enter it into the pool.  */
    180  1.144  riastrad 	rnd_add_uint32(&C->source, 0);
    181  1.144  riastrad 
    182  1.144  riastrad 	/*
    183  1.144  riastrad 	 * On the primary CPU, count down.  Using an atomic decrement
    184  1.144  riastrad 	 * here isn't really necessary -- on every platform we care
    185  1.144  riastrad 	 * about, stores to unsigned int are atomic, and the only other
    186  1.144  riastrad 	 * memory operation that could happen here is for another CPU
    187  1.144  riastrad 	 * to store a higher value for needed.  But using an atomic
    188  1.144  riastrad 	 * decrement avoids giving the impression of data races, and is
    189  1.144  riastrad 	 * unlikely to hurt because only one CPU will ever be writing
    190  1.144  riastrad 	 * to the location.
    191  1.144  riastrad 	 */
    192  1.144  riastrad 	if (CPU_IS_PRIMARY(curcpu())) {
    193  1.144  riastrad 		unsigned needed __diagused;
    194  1.144  riastrad 
    195  1.144  riastrad 		needed = atomic_dec_uint_nv(&C->needed);
    196  1.144  riastrad 		KASSERT(needed != UINT_MAX);
    197  1.144  riastrad 	}
    198  1.144  riastrad }
    199  1.144  riastrad 
    200   1.99    kardel static u_int get_intr_timecount(struct timecounter *);
    201   1.99    kardel 
    202   1.99    kardel static struct timecounter intr_timecounter = {
    203  1.138  riastrad 	.tc_get_timecount	= get_intr_timecount,
    204  1.138  riastrad 	.tc_poll_pps		= NULL,
    205  1.138  riastrad 	.tc_counter_mask	= ~0u,
    206  1.138  riastrad 	.tc_frequency		= 0,
    207  1.138  riastrad 	.tc_name		= "clockinterrupt",
    208  1.138  riastrad 	/* quality - minimum implementation level for a clock */
    209  1.138  riastrad 	.tc_quality		= 0,
    210  1.138  riastrad 	.tc_priv		= NULL,
    211   1.99    kardel };
    212   1.99    kardel 
    213   1.99    kardel static u_int
    214  1.104      yamt get_intr_timecount(struct timecounter *tc)
    215   1.99    kardel {
    216  1.104      yamt 
    217  1.140      maxv 	return (u_int)getticks();
    218  1.140      maxv }
    219  1.140      maxv 
    220  1.140      maxv int
    221  1.140      maxv getticks(void)
    222  1.140      maxv {
    223  1.140      maxv 	return atomic_load_relaxed(&hardclock_ticks);
    224   1.99    kardel }
    225   1.73   thorpej 
    226   1.66   thorpej /*
    227   1.19       cgd  * Initialize clock frequencies and start both clocks running.
    228   1.19       cgd  */
    229   1.19       cgd void
    230   1.63   thorpej initclocks(void)
    231   1.19       cgd {
    232  1.132     pooka 	static struct sysctllog *clog;
    233   1.55  augustss 	int i;
    234   1.19       cgd 
    235   1.19       cgd 	/*
    236   1.19       cgd 	 * Set divisors to 1 (normal case) and let the machine-specific
    237   1.19       cgd 	 * code do its bit.
    238   1.19       cgd 	 */
    239   1.70  sommerfe 	psdiv = 1;
    240  1.142   thorpej 
    241  1.142   thorpej 	/*
    242  1.142   thorpej 	 * Call cpu_initclocks() before registering the default
    243  1.142   thorpej 	 * timecounter, in case it needs to adjust hz.
    244  1.142   thorpej 	 */
    245  1.142   thorpej 	const int old_hz = hz;
    246  1.142   thorpej 	cpu_initclocks();
    247  1.142   thorpej 	if (old_hz != hz) {
    248  1.142   thorpej 		tick = 1000000 / hz;
    249  1.142   thorpej 		tickadj = (240000 / (60 * hz)) ? (240000 / (60 * hz)) : 1;
    250  1.142   thorpej 	}
    251  1.142   thorpej 
    252   1.99    kardel 	/*
    253   1.99    kardel 	 * provide minimum default time counter
    254   1.99    kardel 	 * will only run at interrupt resolution
    255   1.99    kardel 	 */
    256   1.99    kardel 	intr_timecounter.tc_frequency = hz;
    257   1.99    kardel 	tc_init(&intr_timecounter);
    258   1.19       cgd 
    259   1.19       cgd 	/*
    260  1.108      yamt 	 * Compute profhz and stathz, fix profhz if needed.
    261   1.19       cgd 	 */
    262   1.19       cgd 	i = stathz ? stathz : hz;
    263   1.19       cgd 	if (profhz == 0)
    264   1.19       cgd 		profhz = i;
    265   1.19       cgd 	psratio = profhz / i;
    266   1.91      yamt 	if (schedhz == 0) {
    267   1.91      yamt 		/* 16Hz is best */
    268  1.114        ad 		hardscheddiv = hz / 16;
    269  1.114        ad 		if (hardscheddiv <= 0)
    270  1.114        ad 			panic("hardscheddiv");
    271   1.91      yamt 	}
    272   1.31   mycroft 
    273  1.132     pooka 	sysctl_createv(&clog, 0, NULL, NULL,
    274  1.132     pooka 		       CTLFLAG_PERMANENT,
    275  1.132     pooka 		       CTLTYPE_STRUCT, "clockrate",
    276  1.132     pooka 		       SYSCTL_DESCR("Kernel clock rates"),
    277  1.132     pooka 		       sysctl_kern_clockrate, 0, NULL,
    278  1.132     pooka 		       sizeof(struct clockinfo),
    279  1.132     pooka 		       CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
    280  1.132     pooka 	sysctl_createv(&clog, 0, NULL, NULL,
    281  1.132     pooka 		       CTLFLAG_PERMANENT,
    282  1.132     pooka 		       CTLTYPE_INT, "hardclock_ticks",
    283  1.132     pooka 		       SYSCTL_DESCR("Number of hardclock ticks"),
    284  1.132     pooka 		       NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
    285  1.132     pooka 		       CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
    286  1.144  riastrad 
    287  1.144  riastrad 	rndsource_setcb(&hardclockrnd.source, clockrnd_get, &hardclockrnd);
    288  1.144  riastrad 	rnd_attach_source(&hardclockrnd.source, "hardclock", RND_TYPE_SKEW,
    289  1.149  riastrad 	    RND_FLAG_COLLECT_TIME|RND_FLAG_ESTIMATE_TIME|RND_FLAG_HASCB);
    290  1.144  riastrad 	if (stathz) {
    291  1.144  riastrad 		rndsource_setcb(&statclockrnd.source, clockrnd_get,
    292  1.144  riastrad 		    &statclockrnd);
    293  1.144  riastrad 		rnd_attach_source(&statclockrnd.source, "statclock",
    294  1.149  riastrad 		    RND_TYPE_SKEW,
    295  1.149  riastrad 		    (RND_FLAG_COLLECT_TIME|RND_FLAG_ESTIMATE_TIME|
    296  1.149  riastrad 			RND_FLAG_HASCB));
    297  1.144  riastrad 	}
    298   1.19       cgd }
    299   1.19       cgd 
    300   1.19       cgd /*
    301   1.19       cgd  * The real-time timer, interrupting hz times per second.
    302   1.19       cgd  */
    303   1.19       cgd void
    304   1.63   thorpej hardclock(struct clockframe *frame)
    305   1.19       cgd {
    306   1.82   thorpej 	struct lwp *l;
    307  1.120        ad 	struct cpu_info *ci;
    308   1.19       cgd 
    309  1.144  riastrad 	clockrnd_sample(&hardclockrnd);
    310  1.144  riastrad 
    311  1.120        ad 	ci = curcpu();
    312  1.139        ad 	l = ci->ci_onproc;
    313  1.120        ad 
    314  1.143   thorpej 	ptimer_tick(l, CLKF_USERMODE(frame));
    315   1.19       cgd 
    316   1.19       cgd 	/*
    317   1.19       cgd 	 * If no separate statistics clock is available, run it from here.
    318   1.19       cgd 	 */
    319   1.19       cgd 	if (stathz == 0)
    320   1.19       cgd 		statclock(frame);
    321  1.114        ad 	/*
    322  1.114        ad 	 * If no separate schedclock is provided, call it here
    323  1.114        ad 	 * at about 16 Hz.
    324  1.114        ad 	 */
    325  1.114        ad 	if (schedhz == 0) {
    326  1.114        ad 		if ((int)(--ci->ci_schedstate.spc_schedticks) <= 0) {
    327  1.114        ad 			schedclock(l);
    328  1.114        ad 			ci->ci_schedstate.spc_schedticks = hardscheddiv;
    329  1.114        ad 		}
    330  1.114        ad 	}
    331  1.108      yamt 	if ((--ci->ci_schedstate.spc_ticks) <= 0)
    332  1.108      yamt 		sched_tick(ci);
    333   1.93     perry 
    334  1.123        ad 	if (CPU_IS_PRIMARY(ci)) {
    335  1.140      maxv 		atomic_store_relaxed(&hardclock_ticks,
    336  1.140      maxv 		    atomic_load_relaxed(&hardclock_ticks) + 1);
    337  1.121        ad 		tc_ticktock();
    338  1.121        ad 	}
    339   1.19       cgd 
    340  1.150  riastrad #ifdef HEARTBEAT
    341  1.150  riastrad 	/*
    342  1.150  riastrad 	 * Make sure the CPUs and timecounter are making progress.
    343  1.150  riastrad 	 */
    344  1.150  riastrad 	heartbeat();
    345  1.150  riastrad #endif
    346  1.150  riastrad 
    347   1.19       cgd 	/*
    348  1.126     pooka 	 * Update real-time timeout queue.
    349  1.106        ad 	 */
    350  1.109        ad 	callout_hardclock();
    351   1.19       cgd }
    352   1.19       cgd 
    353   1.19       cgd /*
    354   1.19       cgd  * Start profiling on a process.
    355   1.19       cgd  *
    356   1.19       cgd  * Kernel profiling passes proc0 which never exits and hence
    357   1.19       cgd  * keeps the profile clock running constantly.
    358   1.19       cgd  */
    359   1.19       cgd void
    360   1.63   thorpej startprofclock(struct proc *p)
    361   1.19       cgd {
    362   1.19       cgd 
    363  1.109        ad 	KASSERT(mutex_owned(&p->p_stmutex));
    364  1.105        ad 
    365  1.105        ad 	if ((p->p_stflag & PST_PROFIL) == 0) {
    366  1.105        ad 		p->p_stflag |= PST_PROFIL;
    367   1.80    briggs 		/*
    368   1.80    briggs 		 * This is only necessary if using the clock as the
    369   1.80    briggs 		 * profiling source.
    370   1.80    briggs 		 */
    371   1.70  sommerfe 		if (++profprocs == 1 && stathz != 0)
    372   1.70  sommerfe 			psdiv = psratio;
    373   1.19       cgd 	}
    374   1.19       cgd }
    375   1.19       cgd 
    376   1.19       cgd /*
    377   1.19       cgd  * Stop profiling on a process.
    378   1.19       cgd  */
    379   1.19       cgd void
    380   1.63   thorpej stopprofclock(struct proc *p)
    381   1.19       cgd {
    382   1.19       cgd 
    383  1.109        ad 	KASSERT(mutex_owned(&p->p_stmutex));
    384  1.105        ad 
    385  1.105        ad 	if (p->p_stflag & PST_PROFIL) {
    386  1.105        ad 		p->p_stflag &= ~PST_PROFIL;
    387   1.80    briggs 		/*
    388   1.80    briggs 		 * This is only necessary if using the clock as the
    389   1.80    briggs 		 * profiling source.
    390   1.80    briggs 		 */
    391   1.70  sommerfe 		if (--profprocs == 0 && stathz != 0)
    392   1.70  sommerfe 			psdiv = 1;
    393   1.19       cgd 	}
    394   1.19       cgd }
    395   1.19       cgd 
    396  1.108      yamt void
    397  1.108      yamt schedclock(struct lwp *l)
    398  1.108      yamt {
    399  1.108      yamt 	if ((l->l_flag & LW_IDLE) != 0)
    400  1.108      yamt 		return;
    401  1.108      yamt 
    402  1.108      yamt 	sched_schedclock(l);
    403  1.108      yamt }
    404  1.108      yamt 
    405   1.19       cgd /*
    406   1.19       cgd  * Statistics clock.  Grab profile sample, and if divider reaches 0,
    407   1.19       cgd  * do process and kernel statistics.
    408   1.19       cgd  */
    409   1.19       cgd void
    410   1.63   thorpej statclock(struct clockframe *frame)
    411   1.19       cgd {
    412   1.19       cgd #ifdef GPROF
    413   1.55  augustss 	struct gmonparam *g;
    414   1.68       eeh 	intptr_t i;
    415   1.19       cgd #endif
    416   1.60   thorpej 	struct cpu_info *ci = curcpu();
    417   1.60   thorpej 	struct schedstate_percpu *spc = &ci->ci_schedstate;
    418   1.55  augustss 	struct proc *p;
    419   1.98  christos 	struct lwp *l;
    420   1.19       cgd 
    421  1.144  riastrad 	if (stathz)
    422  1.144  riastrad 		clockrnd_sample(&statclockrnd);
    423  1.144  riastrad 
    424   1.70  sommerfe 	/*
    425   1.70  sommerfe 	 * Notice changes in divisor frequency, and adjust clock
    426   1.70  sommerfe 	 * frequency accordingly.
    427   1.70  sommerfe 	 */
    428   1.70  sommerfe 	if (spc->spc_psdiv != psdiv) {
    429   1.70  sommerfe 		spc->spc_psdiv = psdiv;
    430   1.70  sommerfe 		spc->spc_pscnt = psdiv;
    431   1.70  sommerfe 		if (psdiv == 1) {
    432   1.70  sommerfe 			setstatclockrate(stathz);
    433   1.70  sommerfe 		} else {
    434   1.93     perry 			setstatclockrate(profhz);
    435   1.70  sommerfe 		}
    436   1.70  sommerfe 	}
    437  1.139        ad 	l = ci->ci_onproc;
    438  1.108      yamt 	if ((l->l_flag & LW_IDLE) != 0) {
    439  1.108      yamt 		/*
    440  1.108      yamt 		 * don't account idle lwps as swapper.
    441  1.108      yamt 		 */
    442  1.108      yamt 		p = NULL;
    443  1.108      yamt 	} else {
    444  1.108      yamt 		p = l->l_proc;
    445  1.105        ad 		mutex_spin_enter(&p->p_stmutex);
    446  1.108      yamt 	}
    447  1.108      yamt 
    448   1.19       cgd 	if (CLKF_USERMODE(frame)) {
    449  1.135      maxv 		KASSERT(p != NULL);
    450  1.105        ad 		if ((p->p_stflag & PST_PROFIL) && profsrc == PROFSRC_CLOCK)
    451  1.105        ad 			addupc_intr(l, CLKF_PC(frame));
    452  1.105        ad 		if (--spc->spc_pscnt > 0) {
    453  1.105        ad 			mutex_spin_exit(&p->p_stmutex);
    454   1.19       cgd 			return;
    455  1.105        ad 		}
    456  1.105        ad 
    457   1.19       cgd 		/*
    458   1.19       cgd 		 * Came from user mode; CPU was in user state.
    459   1.19       cgd 		 * If this process is being profiled record the tick.
    460   1.19       cgd 		 */
    461   1.19       cgd 		p->p_uticks++;
    462   1.19       cgd 		if (p->p_nice > NZERO)
    463   1.60   thorpej 			spc->spc_cp_time[CP_NICE]++;
    464   1.19       cgd 		else
    465   1.60   thorpej 			spc->spc_cp_time[CP_USER]++;
    466   1.19       cgd 	} else {
    467   1.19       cgd #ifdef GPROF
    468   1.19       cgd 		/*
    469   1.19       cgd 		 * Kernel statistics are just like addupc_intr, only easier.
    470   1.19       cgd 		 */
    471  1.146       ryo #if defined(MULTIPROCESSOR) && !defined(_RUMPKERNEL)
    472  1.145       ryo 		g = curcpu()->ci_gmon;
    473  1.145       ryo 		if (g != NULL &&
    474  1.145       ryo 		    profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {
    475  1.145       ryo #else
    476   1.19       cgd 		g = &_gmonparam;
    477   1.80    briggs 		if (profsrc == PROFSRC_CLOCK && g->state == GMON_PROF_ON) {
    478  1.145       ryo #endif
    479   1.19       cgd 			i = CLKF_PC(frame) - g->lowpc;
    480   1.19       cgd 			if (i < g->textsize) {
    481   1.19       cgd 				i /= HISTFRACTION * sizeof(*g->kcount);
    482   1.19       cgd 				g->kcount[i]++;
    483   1.19       cgd 			}
    484   1.19       cgd 		}
    485   1.19       cgd #endif
    486   1.82   thorpej #ifdef LWP_PC
    487  1.108      yamt 		if (p != NULL && profsrc == PROFSRC_CLOCK &&
    488  1.108      yamt 		    (p->p_stflag & PST_PROFIL)) {
    489  1.105        ad 			addupc_intr(l, LWP_PC(l));
    490  1.108      yamt 		}
    491   1.72   mycroft #endif
    492  1.105        ad 		if (--spc->spc_pscnt > 0) {
    493  1.105        ad 			if (p != NULL)
    494  1.105        ad 				mutex_spin_exit(&p->p_stmutex);
    495   1.19       cgd 			return;
    496  1.105        ad 		}
    497   1.19       cgd 		/*
    498   1.19       cgd 		 * Came from kernel mode, so we were:
    499   1.19       cgd 		 * - handling an interrupt,
    500   1.19       cgd 		 * - doing syscall or trap work on behalf of the current
    501   1.19       cgd 		 *   user process, or
    502   1.19       cgd 		 * - spinning in the idle loop.
    503   1.19       cgd 		 * Whichever it is, charge the time as appropriate.
    504   1.19       cgd 		 * Note that we charge interrupts to the current process,
    505   1.19       cgd 		 * regardless of whether they are ``for'' that process,
    506   1.19       cgd 		 * so that we know how much of its real time was spent
    507   1.19       cgd 		 * in ``non-process'' (i.e., interrupt) work.
    508   1.19       cgd 		 */
    509  1.114        ad 		if (CLKF_INTR(frame) || (curlwp->l_pflag & LP_INTR) != 0) {
    510  1.108      yamt 			if (p != NULL) {
    511   1.19       cgd 				p->p_iticks++;
    512  1.108      yamt 			}
    513   1.60   thorpej 			spc->spc_cp_time[CP_INTR]++;
    514   1.19       cgd 		} else if (p != NULL) {
    515   1.19       cgd 			p->p_sticks++;
    516   1.60   thorpej 			spc->spc_cp_time[CP_SYS]++;
    517  1.108      yamt 		} else {
    518   1.60   thorpej 			spc->spc_cp_time[CP_IDLE]++;
    519  1.108      yamt 		}
    520   1.19       cgd 	}
    521   1.70  sommerfe 	spc->spc_pscnt = psdiv;
    522   1.19       cgd 
    523   1.97      elad 	if (p != NULL) {
    524  1.125     rmind 		atomic_inc_uint(&l->l_cpticks);
    525  1.105        ad 		mutex_spin_exit(&p->p_stmutex);
    526  1.108      yamt 	}
    527  1.141        ad 
    528  1.141        ad #ifdef KDTRACE_HOOKS
    529  1.141        ad 	cyclic_clock_func_t func = cyclic_clock_func[cpu_index(ci)];
    530  1.141        ad 	if (func) {
    531  1.141        ad 		(*func)((struct clockframe *)frame);
    532  1.141        ad 	}
    533  1.141        ad #endif
    534   1.19       cgd }
    535  1.132     pooka 
    536  1.132     pooka /*
    537  1.132     pooka  * sysctl helper routine for kern.clockrate. Assembles a struct on
    538  1.132     pooka  * the fly to be returned to the caller.
    539  1.132     pooka  */
    540  1.132     pooka static int
    541  1.132     pooka sysctl_kern_clockrate(SYSCTLFN_ARGS)
    542  1.132     pooka {
    543  1.132     pooka 	struct clockinfo clkinfo;
    544  1.132     pooka 	struct sysctlnode node;
    545  1.132     pooka 
    546  1.132     pooka 	clkinfo.tick = tick;
    547  1.132     pooka 	clkinfo.tickadj = tickadj;
    548  1.132     pooka 	clkinfo.hz = hz;
    549  1.132     pooka 	clkinfo.profhz = profhz;
    550  1.132     pooka 	clkinfo.stathz = stathz ? stathz : hz;
    551  1.132     pooka 
    552  1.132     pooka 	node = *rnode;
    553  1.132     pooka 	node.sysctl_data = &clkinfo;
    554  1.132     pooka 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    555  1.132     pooka }
    556