xref: /freebsd/sys/kern/kern_clock.c (revision af1408e33f6f7a5a5fc49bd4a67cb8368c2d1ede)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
4232c20357SPoul-Henning Kamp #include "opt_ntp.h"
4332c20357SPoul-Henning Kamp 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46df8bae1dSRodney W. Grimes #include <sys/dkstat.h>
47df8bae1dSRodney W. Grimes #include <sys/callout.h>
48df8bae1dSRodney W. Grimes #include <sys/kernel.h>
49f34fa851SJohn Baldwin #include <sys/lock.h>
5061d80e90SJohn Baldwin #include <sys/ktr.h>
5135e0e5b3SJohn Baldwin #include <sys/mutex.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
53df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
54797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
556caa8a15SJohn Baldwin #include <sys/smp.h>
5691266b96SPoul-Henning Kamp #include <sys/timetc.h>
5732c20357SPoul-Henning Kamp #include <sys/timepps.h>
588a129caeSDavid Greenman #include <vm/vm.h>
59efeaf95aSDavid Greenman #include <vm/pmap.h>
60efeaf95aSDavid Greenman #include <vm/vm_map.h>
61797f2d22SPoul-Henning Kamp #include <sys/sysctl.h>
628088699fSJohn Baldwin #include <sys/bus.h>
638088699fSJohn Baldwin #include <sys/interrupt.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <machine/cpu.h>
66b1037dcdSBruce Evans #include <machine/limits.h>
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #ifdef GPROF
69df8bae1dSRodney W. Grimes #include <sys/gmon.h>
70df8bae1dSRodney W. Grimes #endif
71df8bae1dSRodney W. Grimes 
72e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
73e4fc250cSLuigi Rizzo #include <net/netisr.h>		/* for NETISR_POLL */
74e4fc250cSLuigi Rizzo 
75e4fc250cSLuigi Rizzo extern void ether_poll1(void);
76e4fc250cSLuigi Rizzo extern void hardclock_device_poll(void);
77e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
78eae8fc2cSSteve Passe 
79d841aaa7SBruce Evans static void initclocks __P((void *dummy));
802b14f991SJulian Elischer SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
812b14f991SJulian Elischer 
82f23b4c91SGarrett Wollman /* Some of these don't belong here, but it's easiest to concentrate them. */
83eae8fc2cSSteve Passe long cp_time[CPUSTATES];
84f23b4c91SGarrett Wollman 
857f112b04SRobert Watson SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time),
867f112b04SRobert Watson     "LU", "CPU time statistics");
877f112b04SRobert Watson 
88f23b4c91SGarrett Wollman long tk_cancc;
89f23b4c91SGarrett Wollman long tk_nin;
90f23b4c91SGarrett Wollman long tk_nout;
91f23b4c91SGarrett Wollman long tk_rawcc;
92f23b4c91SGarrett Wollman 
933bac064fSPoul-Henning Kamp /*
94df8bae1dSRodney W. Grimes  * Clock handling routines.
95df8bae1dSRodney W. Grimes  *
96b05dcf3cSPoul-Henning Kamp  * This code is written to operate with two timers that run independently of
97b05dcf3cSPoul-Henning Kamp  * each other.
987ec73f64SPoul-Henning Kamp  *
99b05dcf3cSPoul-Henning Kamp  * The main timer, running hz times per second, is used to trigger interval
100b05dcf3cSPoul-Henning Kamp  * timers, timeouts and rescheduling as needed.
1017ec73f64SPoul-Henning Kamp  *
102b05dcf3cSPoul-Henning Kamp  * The second timer handles kernel and user profiling,
103b05dcf3cSPoul-Henning Kamp  * and does resource use estimation.  If the second timer is programmable,
104b05dcf3cSPoul-Henning Kamp  * it is randomized to avoid aliasing between the two clocks.  For example,
105b05dcf3cSPoul-Henning Kamp  * the randomization prevents an adversary from always giving up the cpu
106df8bae1dSRodney W. Grimes  * just before its quantum expires.  Otherwise, it would never accumulate
107df8bae1dSRodney W. Grimes  * cpu ticks.  The mean frequency of the second timer is stathz.
108b05dcf3cSPoul-Henning Kamp  *
109b05dcf3cSPoul-Henning Kamp  * If no second timer exists, stathz will be zero; in this case we drive
110b05dcf3cSPoul-Henning Kamp  * profiling and statistics off the main clock.  This WILL NOT be accurate;
111b05dcf3cSPoul-Henning Kamp  * do not do it unless absolutely necessary.
112b05dcf3cSPoul-Henning Kamp  *
113df8bae1dSRodney W. Grimes  * The statistics clock may (or may not) be run at a higher rate while
114b05dcf3cSPoul-Henning Kamp  * profiling.  This profile clock runs at profhz.  We require that profhz
115b05dcf3cSPoul-Henning Kamp  * be an integral multiple of stathz.
116b05dcf3cSPoul-Henning Kamp  *
117b05dcf3cSPoul-Henning Kamp  * If the statistics clock is running fast, it must be divided by the ratio
118b05dcf3cSPoul-Henning Kamp  * profhz/stathz for statistics.  (For profiling, every tick counts.)
119df8bae1dSRodney W. Grimes  *
1207ec73f64SPoul-Henning Kamp  * Time-of-day is maintained using a "timecounter", which may or may
1217ec73f64SPoul-Henning Kamp  * not be related to the hardware generating the above mentioned
1227ec73f64SPoul-Henning Kamp  * interrupts.
123df8bae1dSRodney W. Grimes  */
124df8bae1dSRodney W. Grimes 
125df8bae1dSRodney W. Grimes int	stathz;
126df8bae1dSRodney W. Grimes int	profhz;
127cc3d5226SBruce Evans static int profprocs;
128df8bae1dSRodney W. Grimes int	ticks;
129df8bae1dSRodney W. Grimes static int psdiv, pscnt;		/* prof => stat divider */
130cc3d5226SBruce Evans int	psratio;			/* ratio: prof / stat */
131df8bae1dSRodney W. Grimes 
132df8bae1dSRodney W. Grimes /*
133df8bae1dSRodney W. Grimes  * Initialize clock frequencies and start both clocks running.
134df8bae1dSRodney W. Grimes  */
1352b14f991SJulian Elischer /* ARGSUSED*/
1362b14f991SJulian Elischer static void
137d841aaa7SBruce Evans initclocks(dummy)
138d841aaa7SBruce Evans 	void *dummy;
139df8bae1dSRodney W. Grimes {
140df8bae1dSRodney W. Grimes 	register int i;
141df8bae1dSRodney W. Grimes 
142df8bae1dSRodney W. Grimes 	/*
143df8bae1dSRodney W. Grimes 	 * Set divisors to 1 (normal case) and let the machine-specific
144df8bae1dSRodney W. Grimes 	 * code do its bit.
145df8bae1dSRodney W. Grimes 	 */
146df8bae1dSRodney W. Grimes 	psdiv = pscnt = 1;
147df8bae1dSRodney W. Grimes 	cpu_initclocks();
148df8bae1dSRodney W. Grimes 
149e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
150e4fc250cSLuigi Rizzo 	register_netisr(NETISR_POLL, ether_poll1);
151e4fc250cSLuigi Rizzo #endif
152df8bae1dSRodney W. Grimes 	/*
153df8bae1dSRodney W. Grimes 	 * Compute profhz/stathz, and fix profhz if needed.
154df8bae1dSRodney W. Grimes 	 */
155df8bae1dSRodney W. Grimes 	i = stathz ? stathz : hz;
156df8bae1dSRodney W. Grimes 	if (profhz == 0)
157df8bae1dSRodney W. Grimes 		profhz = i;
158df8bae1dSRodney W. Grimes 	psratio = profhz / i;
159df8bae1dSRodney W. Grimes }
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes /*
1626caa8a15SJohn Baldwin  * Each time the real-time timer fires, this function is called on all CPUs
163b40ce416SJulian Elischer  * with each CPU passing in its curthread as the first argument.  If possible
1646caa8a15SJohn Baldwin  * a nice optimization in the future would be to allow the CPU receiving the
1656caa8a15SJohn Baldwin  * actual real-time timer interrupt to call this function on behalf of the
1666caa8a15SJohn Baldwin  * other CPUs rather than sending an IPI to all other CPUs so that they
1676caa8a15SJohn Baldwin  * can call this function.  Note that hardclock() calls hardclock_process()
1686caa8a15SJohn Baldwin  * for the CPU receiving the timer interrupt, so only the other CPUs in the
1696caa8a15SJohn Baldwin  * system need to call this function (or have it called on their behalf.
1706caa8a15SJohn Baldwin  */
1716caa8a15SJohn Baldwin void
172b40ce416SJulian Elischer hardclock_process(td, user)
173b40ce416SJulian Elischer 	struct thread *td;
1746caa8a15SJohn Baldwin 	int user;
1756caa8a15SJohn Baldwin {
1766caa8a15SJohn Baldwin 	struct pstats *pstats;
177b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1786caa8a15SJohn Baldwin 
1796caa8a15SJohn Baldwin 	/*
1806caa8a15SJohn Baldwin 	 * Run current process's virtual and profile time, as needed.
1816caa8a15SJohn Baldwin 	 */
1826caa8a15SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
183b40ce416SJulian Elischer 	if (p->p_flag & P_KSES) {
184b40ce416SJulian Elischer 		/* XXXKSE What to do? */
185b40ce416SJulian Elischer 	} else {
1866caa8a15SJohn Baldwin 		pstats = p->p_stats;
1876caa8a15SJohn Baldwin 		if (user &&
1886caa8a15SJohn Baldwin 		    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
189b40ce416SJulian Elischer 		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) {
190b40ce416SJulian Elischer 			p->p_sflag |= PS_ALRMPEND;
191b40ce416SJulian Elischer 			td->td_kse->ke_flags |= KEF_ASTPENDING;
192b40ce416SJulian Elischer 		}
1936caa8a15SJohn Baldwin 		if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
194b40ce416SJulian Elischer 		    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) {
195b40ce416SJulian Elischer 			p->p_sflag |= PS_PROFPEND;
196b40ce416SJulian Elischer 			td->td_kse->ke_flags |= KEF_ASTPENDING;
197b40ce416SJulian Elischer 		}
198b40ce416SJulian Elischer 	}
1996caa8a15SJohn Baldwin }
2006caa8a15SJohn Baldwin 
2016caa8a15SJohn Baldwin /*
202df8bae1dSRodney W. Grimes  * The real-time timer, interrupting hz times per second.
203df8bae1dSRodney W. Grimes  */
204df8bae1dSRodney W. Grimes void
205df8bae1dSRodney W. Grimes hardclock(frame)
206df8bae1dSRodney W. Grimes 	register struct clockframe *frame;
207df8bae1dSRodney W. Grimes {
208fa2fbc3dSJake Burkholder 	int need_softclock = 0;
209df8bae1dSRodney W. Grimes 
210c9c1406fSJohn Baldwin 	CTR0(KTR_INTR, "hardclock fired");
21121a7a9aeSJohn Baldwin 	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
212b40ce416SJulian Elischer 	hardclock_process(curthread, CLKF_USERMODE(frame));
21321a7a9aeSJohn Baldwin 	mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
214b05dcf3cSPoul-Henning Kamp 
215df8bae1dSRodney W. Grimes 	/*
216df8bae1dSRodney W. Grimes 	 * If no separate statistics clock is available, run it from here.
2176caa8a15SJohn Baldwin 	 *
2186caa8a15SJohn Baldwin 	 * XXX: this only works for UP
219df8bae1dSRodney W. Grimes 	 */
220df8bae1dSRodney W. Grimes 	if (stathz == 0)
221df8bae1dSRodney W. Grimes 		statclock(frame);
222df8bae1dSRodney W. Grimes 
22391266b96SPoul-Henning Kamp 	tc_windup();
224e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
225e4fc250cSLuigi Rizzo 	hardclock_device_poll();
226e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
2273f31c649SGarrett Wollman 
228b05dcf3cSPoul-Henning Kamp 	/*
229b05dcf3cSPoul-Henning Kamp 	 * Process callouts at a very low cpu priority, so we don't keep the
230b05dcf3cSPoul-Henning Kamp 	 * relatively high clock interrupt priority any longer than necessary.
231b05dcf3cSPoul-Henning Kamp 	 */
23221a7a9aeSJohn Baldwin 	mtx_lock_spin_flags(&callout_lock, MTX_QUIET);
233fa2fbc3dSJake Burkholder 	ticks++;
234b05dcf3cSPoul-Henning Kamp 	if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
235fa2fbc3dSJake Burkholder 		need_softclock = 1;
236b05dcf3cSPoul-Henning Kamp 	} else if (softticks + 1 == ticks)
237b05dcf3cSPoul-Henning Kamp 		++softticks;
23821a7a9aeSJohn Baldwin 	mtx_unlock_spin_flags(&callout_lock, MTX_QUIET);
239fa2fbc3dSJake Burkholder 
240fa2fbc3dSJake Burkholder 	/*
241062d8ff5SJohn Baldwin 	 * swi_sched acquires sched_lock, so we don't want to call it with
242fa2fbc3dSJake Burkholder 	 * callout_lock held; incorrect locking order.
243fa2fbc3dSJake Burkholder 	 */
244fa2fbc3dSJake Burkholder 	if (need_softclock)
245062d8ff5SJohn Baldwin 		swi_sched(softclock_ih, SWI_NOSWITCH);
246ab36c067SJustin T. Gibbs }
247ab36c067SJustin T. Gibbs 
248df8bae1dSRodney W. Grimes /*
249227ee8a1SPoul-Henning Kamp  * Compute number of ticks in the specified amount of time.
250df8bae1dSRodney W. Grimes  */
251df8bae1dSRodney W. Grimes int
252227ee8a1SPoul-Henning Kamp tvtohz(tv)
253df8bae1dSRodney W. Grimes 	struct timeval *tv;
254df8bae1dSRodney W. Grimes {
2556976af69SBruce Evans 	register unsigned long ticks;
2566976af69SBruce Evans 	register long sec, usec;
257df8bae1dSRodney W. Grimes 
258df8bae1dSRodney W. Grimes 	/*
2596976af69SBruce Evans 	 * If the number of usecs in the whole seconds part of the time
2606976af69SBruce Evans 	 * difference fits in a long, then the total number of usecs will
2616976af69SBruce Evans 	 * fit in an unsigned long.  Compute the total and convert it to
2626976af69SBruce Evans 	 * ticks, rounding up and adding 1 to allow for the current tick
2636976af69SBruce Evans 	 * to expire.  Rounding also depends on unsigned long arithmetic
2646976af69SBruce Evans 	 * to avoid overflow.
265df8bae1dSRodney W. Grimes 	 *
2666976af69SBruce Evans 	 * Otherwise, if the number of ticks in the whole seconds part of
2676976af69SBruce Evans 	 * the time difference fits in a long, then convert the parts to
2686976af69SBruce Evans 	 * ticks separately and add, using similar rounding methods and
2696976af69SBruce Evans 	 * overflow avoidance.  This method would work in the previous
2706976af69SBruce Evans 	 * case but it is slightly slower and assumes that hz is integral.
2716976af69SBruce Evans 	 *
2726976af69SBruce Evans 	 * Otherwise, round the time difference down to the maximum
2736976af69SBruce Evans 	 * representable value.
2746976af69SBruce Evans 	 *
2756976af69SBruce Evans 	 * If ints have 32 bits, then the maximum value for any timeout in
2766976af69SBruce Evans 	 * 10ms ticks is 248 days.
277df8bae1dSRodney W. Grimes 	 */
278227ee8a1SPoul-Henning Kamp 	sec = tv->tv_sec;
279227ee8a1SPoul-Henning Kamp 	usec = tv->tv_usec;
2806976af69SBruce Evans 	if (usec < 0) {
2816976af69SBruce Evans 		sec--;
2826976af69SBruce Evans 		usec += 1000000;
2836976af69SBruce Evans 	}
2846976af69SBruce Evans 	if (sec < 0) {
2856976af69SBruce Evans #ifdef DIAGNOSTIC
286b05dcf3cSPoul-Henning Kamp 		if (usec > 0) {
2877ec73f64SPoul-Henning Kamp 			sec++;
2887ec73f64SPoul-Henning Kamp 			usec -= 1000000;
2897ec73f64SPoul-Henning Kamp 		}
290227ee8a1SPoul-Henning Kamp 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
2916976af69SBruce Evans 		       sec, usec);
2926976af69SBruce Evans #endif
2936976af69SBruce Evans 		ticks = 1;
2946976af69SBruce Evans 	} else if (sec <= LONG_MAX / 1000000)
2956976af69SBruce Evans 		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
2966976af69SBruce Evans 			/ tick + 1;
2976976af69SBruce Evans 	else if (sec <= LONG_MAX / hz)
2986976af69SBruce Evans 		ticks = sec * hz
2996976af69SBruce Evans 			+ ((unsigned long)usec + (tick - 1)) / tick + 1;
3006976af69SBruce Evans 	else
3016976af69SBruce Evans 		ticks = LONG_MAX;
3026976af69SBruce Evans 	if (ticks > INT_MAX)
3036976af69SBruce Evans 		ticks = INT_MAX;
304d6116663SAlexander Langer 	return ((int)ticks);
305df8bae1dSRodney W. Grimes }
306df8bae1dSRodney W. Grimes 
307df8bae1dSRodney W. Grimes /*
308df8bae1dSRodney W. Grimes  * Start profiling on a process.
309df8bae1dSRodney W. Grimes  *
310df8bae1dSRodney W. Grimes  * Kernel profiling passes proc0 which never exits and hence
311df8bae1dSRodney W. Grimes  * keeps the profile clock running constantly.
312df8bae1dSRodney W. Grimes  */
313df8bae1dSRodney W. Grimes void
314df8bae1dSRodney W. Grimes startprofclock(p)
315df8bae1dSRodney W. Grimes 	register struct proc *p;
316df8bae1dSRodney W. Grimes {
317df8bae1dSRodney W. Grimes 	int s;
318df8bae1dSRodney W. Grimes 
31901cd094cSJohn Baldwin 	/*
32001cd094cSJohn Baldwin 	 * XXX; Right now sched_lock protects statclock(), but perhaps
32101cd094cSJohn Baldwin 	 * it should be protected later on by a time_lock, which would
32201cd094cSJohn Baldwin 	 * cover psdiv, etc. as well.
32301cd094cSJohn Baldwin 	 */
3249ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
32501cd094cSJohn Baldwin 	if ((p->p_sflag & PS_PROFIL) == 0) {
32601cd094cSJohn Baldwin 		p->p_sflag |= PS_PROFIL;
327df8bae1dSRodney W. Grimes 		if (++profprocs == 1 && stathz != 0) {
328df8bae1dSRodney W. Grimes 			s = splstatclock();
329df8bae1dSRodney W. Grimes 			psdiv = pscnt = psratio;
330df8bae1dSRodney W. Grimes 			setstatclockrate(profhz);
331df8bae1dSRodney W. Grimes 			splx(s);
332df8bae1dSRodney W. Grimes 		}
333df8bae1dSRodney W. Grimes 	}
3349ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
335df8bae1dSRodney W. Grimes }
336df8bae1dSRodney W. Grimes 
337df8bae1dSRodney W. Grimes /*
338df8bae1dSRodney W. Grimes  * Stop profiling on a process.
339df8bae1dSRodney W. Grimes  */
340df8bae1dSRodney W. Grimes void
341df8bae1dSRodney W. Grimes stopprofclock(p)
342df8bae1dSRodney W. Grimes 	register struct proc *p;
343df8bae1dSRodney W. Grimes {
344df8bae1dSRodney W. Grimes 	int s;
345df8bae1dSRodney W. Grimes 
3469ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
34701cd094cSJohn Baldwin 	if (p->p_sflag & PS_PROFIL) {
34801cd094cSJohn Baldwin 		p->p_sflag &= ~PS_PROFIL;
349df8bae1dSRodney W. Grimes 		if (--profprocs == 0 && stathz != 0) {
350df8bae1dSRodney W. Grimes 			s = splstatclock();
351df8bae1dSRodney W. Grimes 			psdiv = pscnt = 1;
352df8bae1dSRodney W. Grimes 			setstatclockrate(stathz);
353df8bae1dSRodney W. Grimes 			splx(s);
354df8bae1dSRodney W. Grimes 		}
355df8bae1dSRodney W. Grimes 	}
3569ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
357df8bae1dSRodney W. Grimes }
358df8bae1dSRodney W. Grimes 
359df8bae1dSRodney W. Grimes /*
3606caa8a15SJohn Baldwin  * Do process and kernel statistics.  Most of the statistics are only
36171a62f8aSBruce Evans  * used by user-level statistics programs.  The main exceptions are
362b40ce416SJulian Elischer  * ke->ke_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.  This function
3636caa8a15SJohn Baldwin  * should be called by all CPUs in the system for each statistics clock
3646caa8a15SJohn Baldwin  * interrupt.  See the description of hardclock_process for more detail on
3656caa8a15SJohn Baldwin  * this function's relationship to statclock.
366df8bae1dSRodney W. Grimes  */
367df8bae1dSRodney W. Grimes void
368b40ce416SJulian Elischer statclock_process(ke, pc, user)
369b40ce416SJulian Elischer 	struct kse *ke;
3706caa8a15SJohn Baldwin 	register_t pc;
3716caa8a15SJohn Baldwin 	int user;
372df8bae1dSRodney W. Grimes {
373df8bae1dSRodney W. Grimes #ifdef GPROF
3746caa8a15SJohn Baldwin 	struct gmonparam *g;
375fffd686aSBruce Evans 	int i;
376df8bae1dSRodney W. Grimes #endif
3778a129caeSDavid Greenman 	struct pstats *pstats;
378f5e9e8ecSBruce Evans 	long rss;
3798a129caeSDavid Greenman 	struct rusage *ru;
3808a129caeSDavid Greenman 	struct vmspace *vm;
381b40ce416SJulian Elischer 	struct proc *p = ke->ke_proc;
382b40ce416SJulian Elischer 	struct thread *td = ke->ke_thread; /* current thread */
3838a129caeSDavid Greenman 
384b40ce416SJulian Elischer 	KASSERT(ke == curthread->td_kse, ("statclock_process: td != curthread"));
3856caa8a15SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
3866caa8a15SJohn Baldwin 	if (user) {
38771a62f8aSBruce Evans 		/*
38871a62f8aSBruce Evans 		 * Came from user mode; CPU was in user state.
38971a62f8aSBruce Evans 		 * If this process is being profiled, record the tick.
39071a62f8aSBruce Evans 		 */
39101cd094cSJohn Baldwin 		if (p->p_sflag & PS_PROFIL)
392b40ce416SJulian Elischer 			addupc_intr(ke, pc, 1);
3936caa8a15SJohn Baldwin 		if (pscnt < psdiv)
394df8bae1dSRodney W. Grimes 			return;
395df8bae1dSRodney W. Grimes 		/*
39671a62f8aSBruce Evans 		 * Charge the time as appropriate.
397df8bae1dSRodney W. Grimes 		 */
398b40ce416SJulian Elischer 		ke->ke_uticks++;
399b40ce416SJulian Elischer 		if (ke->ke_ksegrp->kg_nice > NZERO)
400df8bae1dSRodney W. Grimes 			cp_time[CP_NICE]++;
401df8bae1dSRodney W. Grimes 		else
402df8bae1dSRodney W. Grimes 			cp_time[CP_USER]++;
403df8bae1dSRodney W. Grimes 	} else {
404df8bae1dSRodney W. Grimes #ifdef GPROF
405df8bae1dSRodney W. Grimes 		/*
406df8bae1dSRodney W. Grimes 		 * Kernel statistics are just like addupc_intr, only easier.
407df8bae1dSRodney W. Grimes 		 */
408df8bae1dSRodney W. Grimes 		g = &_gmonparam;
409df8bae1dSRodney W. Grimes 		if (g->state == GMON_PROF_ON) {
4106caa8a15SJohn Baldwin 			i = pc - g->lowpc;
411df8bae1dSRodney W. Grimes 			if (i < g->textsize) {
412df8bae1dSRodney W. Grimes 				i /= HISTFRACTION * sizeof(*g->kcount);
413df8bae1dSRodney W. Grimes 				g->kcount[i]++;
414df8bae1dSRodney W. Grimes 			}
415df8bae1dSRodney W. Grimes 		}
416df8bae1dSRodney W. Grimes #endif
4176caa8a15SJohn Baldwin 		if (pscnt < psdiv)
418df8bae1dSRodney W. Grimes 			return;
419df8bae1dSRodney W. Grimes 		/*
420df8bae1dSRodney W. Grimes 		 * Came from kernel mode, so we were:
421df8bae1dSRodney W. Grimes 		 * - handling an interrupt,
422df8bae1dSRodney W. Grimes 		 * - doing syscall or trap work on behalf of the current
423df8bae1dSRodney W. Grimes 		 *   user process, or
424df8bae1dSRodney W. Grimes 		 * - spinning in the idle loop.
425df8bae1dSRodney W. Grimes 		 * Whichever it is, charge the time as appropriate.
426df8bae1dSRodney W. Grimes 		 * Note that we charge interrupts to the current process,
427df8bae1dSRodney W. Grimes 		 * regardless of whether they are ``for'' that process,
428df8bae1dSRodney W. Grimes 		 * so that we know how much of its real time was spent
429df8bae1dSRodney W. Grimes 		 * in ``non-process'' (i.e., interrupt) work.
430df8bae1dSRodney W. Grimes 		 */
431b40ce416SJulian Elischer 		if ((td->td_ithd != NULL) || td->td_intr_nesting_level >= 2) {
432b40ce416SJulian Elischer 			ke->ke_iticks++;
433df8bae1dSRodney W. Grimes 			cp_time[CP_INTR]++;
4340384fff8SJason Evans 		} else {
435b40ce416SJulian Elischer 			ke->ke_sticks++;
436b40ce416SJulian Elischer 			if (p != PCPU_GET(idlethread)->td_proc)
437df8bae1dSRodney W. Grimes 				cp_time[CP_SYS]++;
4380384fff8SJason Evans 			else
439df8bae1dSRodney W. Grimes 				cp_time[CP_IDLE]++;
440df8bae1dSRodney W. Grimes 		}
4410384fff8SJason Evans 	}
442df8bae1dSRodney W. Grimes 
443b40ce416SJulian Elischer 	schedclock(ke->ke_thread);
444f5e9e8ecSBruce Evans 
445f5e9e8ecSBruce Evans 	/* Update resource usage integrals and maximums. */
446f5e9e8ecSBruce Evans 	if ((pstats = p->p_stats) != NULL &&
447f5e9e8ecSBruce Evans 	    (ru = &pstats->p_ru) != NULL &&
448f5e9e8ecSBruce Evans 	    (vm = p->p_vmspace) != NULL) {
4491c6d46f9SLuoqi Chen 		ru->ru_ixrss += pgtok(vm->vm_tsize);
4501c6d46f9SLuoqi Chen 		ru->ru_idrss += pgtok(vm->vm_dsize);
4511c6d46f9SLuoqi Chen 		ru->ru_isrss += pgtok(vm->vm_ssize);
4521c6d46f9SLuoqi Chen 		rss = pgtok(vmspace_resident_count(vm));
453f5e9e8ecSBruce Evans 		if (ru->ru_maxrss < rss)
454f5e9e8ecSBruce Evans 			ru->ru_maxrss = rss;
455f5e9e8ecSBruce Evans 	}
4566caa8a15SJohn Baldwin }
4576c567274SJohn Baldwin 
4586caa8a15SJohn Baldwin /*
4596caa8a15SJohn Baldwin  * Statistics clock.  Grab profile sample, and if divider reaches 0,
4606caa8a15SJohn Baldwin  * do process and kernel statistics.  Most of the statistics are only
4616caa8a15SJohn Baldwin  * used by user-level statistics programs.  The main exceptions are
462b40ce416SJulian Elischer  * ke->ke_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.
4636caa8a15SJohn Baldwin  */
4646caa8a15SJohn Baldwin void
4656caa8a15SJohn Baldwin statclock(frame)
4666caa8a15SJohn Baldwin 	register struct clockframe *frame;
4676caa8a15SJohn Baldwin {
4686caa8a15SJohn Baldwin 
469c9c1406fSJohn Baldwin 	CTR0(KTR_INTR, "statclock fired");
47021a7a9aeSJohn Baldwin 	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
4716caa8a15SJohn Baldwin 	if (--pscnt == 0)
4726caa8a15SJohn Baldwin 		pscnt = psdiv;
473b40ce416SJulian Elischer 	statclock_process(curthread->td_kse, CLKF_PC(frame), CLKF_USERMODE(frame));
47421a7a9aeSJohn Baldwin 	mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
475df8bae1dSRodney W. Grimes }
476df8bae1dSRodney W. Grimes 
477df8bae1dSRodney W. Grimes /*
478df8bae1dSRodney W. Grimes  * Return information about system clocks.
479df8bae1dSRodney W. Grimes  */
480787d58f2SPoul-Henning Kamp static int
48182d9ae4eSPoul-Henning Kamp sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
482df8bae1dSRodney W. Grimes {
483df8bae1dSRodney W. Grimes 	struct clockinfo clkinfo;
484df8bae1dSRodney W. Grimes 	/*
485df8bae1dSRodney W. Grimes 	 * Construct clockinfo structure.
486df8bae1dSRodney W. Grimes 	 */
487df8bae1dSRodney W. Grimes 	clkinfo.hz = hz;
488df8bae1dSRodney W. Grimes 	clkinfo.tick = tick;
4895faa3121SJohn Hay 	clkinfo.tickadj = tickadj;
490df8bae1dSRodney W. Grimes 	clkinfo.profhz = profhz;
491df8bae1dSRodney W. Grimes 	clkinfo.stathz = stathz ? stathz : hz;
492ae0eb976SPoul-Henning Kamp 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
493df8bae1dSRodney W. Grimes }
4943f31c649SGarrett Wollman 
495946bb7a2SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
496af1408e3SLuigi Rizzo 	0, 0, sysctl_kern_clockrate, "S,clockinfo",
497af1408e3SLuigi Rizzo 	"Rate and period of various kernel clocks");
498