xref: /freebsd/sys/kern/kern_clock.c (revision 104a9b7e3edbd88cdda0698c5c77a2ad6dafcc16)
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/callout.h>
47df8bae1dSRodney W. Grimes #include <sys/kernel.h>
48f34fa851SJohn Baldwin #include <sys/lock.h>
4961d80e90SJohn Baldwin #include <sys/ktr.h>
5035e0e5b3SJohn Baldwin #include <sys/mutex.h>
51df8bae1dSRodney W. Grimes #include <sys/proc.h>
52e4625663SJeff Roberson #include <sys/resource.h>
53df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
54b43179fbSJeff Roberson #include <sys/sched.h>
55797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
566caa8a15SJohn Baldwin #include <sys/smp.h>
578a129caeSDavid Greenman #include <vm/vm.h>
58efeaf95aSDavid Greenman #include <vm/pmap.h>
59efeaf95aSDavid Greenman #include <vm/vm_map.h>
60797f2d22SPoul-Henning Kamp #include <sys/sysctl.h>
618088699fSJohn Baldwin #include <sys/bus.h>
628088699fSJohn Baldwin #include <sys/interrupt.h>
63104a9b7eSAlexander Kabaev #include <sys/limits.h>
64e7fa55afSPoul-Henning Kamp #include <sys/timetc.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <machine/cpu.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 extern void hardclock_device_poll(void);
74e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
75eae8fc2cSSteve Passe 
764d77a549SAlfred Perlstein static void initclocks(void *dummy);
772b14f991SJulian Elischer SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
782b14f991SJulian Elischer 
79f23b4c91SGarrett Wollman /* Some of these don't belong here, but it's easiest to concentrate them. */
80eae8fc2cSSteve Passe long cp_time[CPUSTATES];
81f23b4c91SGarrett Wollman 
827f112b04SRobert Watson SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time),
837f112b04SRobert Watson     "LU", "CPU time statistics");
847f112b04SRobert Watson 
853bac064fSPoul-Henning Kamp /*
86df8bae1dSRodney W. Grimes  * Clock handling routines.
87df8bae1dSRodney W. Grimes  *
88b05dcf3cSPoul-Henning Kamp  * This code is written to operate with two timers that run independently of
89b05dcf3cSPoul-Henning Kamp  * each other.
907ec73f64SPoul-Henning Kamp  *
91b05dcf3cSPoul-Henning Kamp  * The main timer, running hz times per second, is used to trigger interval
92b05dcf3cSPoul-Henning Kamp  * timers, timeouts and rescheduling as needed.
937ec73f64SPoul-Henning Kamp  *
94b05dcf3cSPoul-Henning Kamp  * The second timer handles kernel and user profiling,
95b05dcf3cSPoul-Henning Kamp  * and does resource use estimation.  If the second timer is programmable,
96b05dcf3cSPoul-Henning Kamp  * it is randomized to avoid aliasing between the two clocks.  For example,
97b05dcf3cSPoul-Henning Kamp  * the randomization prevents an adversary from always giving up the cpu
98df8bae1dSRodney W. Grimes  * just before its quantum expires.  Otherwise, it would never accumulate
99df8bae1dSRodney W. Grimes  * cpu ticks.  The mean frequency of the second timer is stathz.
100b05dcf3cSPoul-Henning Kamp  *
101b05dcf3cSPoul-Henning Kamp  * If no second timer exists, stathz will be zero; in this case we drive
102b05dcf3cSPoul-Henning Kamp  * profiling and statistics off the main clock.  This WILL NOT be accurate;
103b05dcf3cSPoul-Henning Kamp  * do not do it unless absolutely necessary.
104b05dcf3cSPoul-Henning Kamp  *
105df8bae1dSRodney W. Grimes  * The statistics clock may (or may not) be run at a higher rate while
106b05dcf3cSPoul-Henning Kamp  * profiling.  This profile clock runs at profhz.  We require that profhz
107b05dcf3cSPoul-Henning Kamp  * be an integral multiple of stathz.
108b05dcf3cSPoul-Henning Kamp  *
109b05dcf3cSPoul-Henning Kamp  * If the statistics clock is running fast, it must be divided by the ratio
110b05dcf3cSPoul-Henning Kamp  * profhz/stathz for statistics.  (For profiling, every tick counts.)
111df8bae1dSRodney W. Grimes  *
1127ec73f64SPoul-Henning Kamp  * Time-of-day is maintained using a "timecounter", which may or may
1137ec73f64SPoul-Henning Kamp  * not be related to the hardware generating the above mentioned
1147ec73f64SPoul-Henning Kamp  * interrupts.
115df8bae1dSRodney W. Grimes  */
116df8bae1dSRodney W. Grimes 
117df8bae1dSRodney W. Grimes int	stathz;
118df8bae1dSRodney W. Grimes int	profhz;
119238dd320SJake Burkholder int	profprocs;
120df8bae1dSRodney W. Grimes int	ticks;
121238dd320SJake Burkholder int	psratio;
122df8bae1dSRodney W. Grimes 
123df8bae1dSRodney W. Grimes /*
124df8bae1dSRodney W. Grimes  * Initialize clock frequencies and start both clocks running.
125df8bae1dSRodney W. Grimes  */
1262b14f991SJulian Elischer /* ARGSUSED*/
1272b14f991SJulian Elischer static void
128d841aaa7SBruce Evans initclocks(dummy)
129d841aaa7SBruce Evans 	void *dummy;
130df8bae1dSRodney W. Grimes {
131df8bae1dSRodney W. Grimes 	register int i;
132df8bae1dSRodney W. Grimes 
133df8bae1dSRodney W. Grimes 	/*
134df8bae1dSRodney W. Grimes 	 * Set divisors to 1 (normal case) and let the machine-specific
135df8bae1dSRodney W. Grimes 	 * code do its bit.
136df8bae1dSRodney W. Grimes 	 */
137df8bae1dSRodney W. Grimes 	cpu_initclocks();
138df8bae1dSRodney W. Grimes 
139df8bae1dSRodney W. Grimes 	/*
140df8bae1dSRodney W. Grimes 	 * Compute profhz/stathz, and fix profhz if needed.
141df8bae1dSRodney W. Grimes 	 */
142df8bae1dSRodney W. Grimes 	i = stathz ? stathz : hz;
143df8bae1dSRodney W. Grimes 	if (profhz == 0)
144df8bae1dSRodney W. Grimes 		profhz = i;
145df8bae1dSRodney W. Grimes 	psratio = profhz / i;
146df8bae1dSRodney W. Grimes }
147df8bae1dSRodney W. Grimes 
148df8bae1dSRodney W. Grimes /*
149238dd320SJake Burkholder  * Each time the real-time timer fires, this function is called on all CPUs.
150238dd320SJake Burkholder  * Note that hardclock() calls hardclock_process() for the boot CPU, so only
151238dd320SJake Burkholder  * the other CPUs in the system need to call this function.
1526caa8a15SJohn Baldwin  */
1536caa8a15SJohn Baldwin void
154238dd320SJake Burkholder hardclock_process(frame)
155238dd320SJake Burkholder 	register struct clockframe *frame;
1566caa8a15SJohn Baldwin {
1576caa8a15SJohn Baldwin 	struct pstats *pstats;
158238dd320SJake Burkholder 	struct thread *td = curthread;
159b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
1606caa8a15SJohn Baldwin 
1616caa8a15SJohn Baldwin 	/*
1626caa8a15SJohn Baldwin 	 * Run current process's virtual and profile time, as needed.
1636caa8a15SJohn Baldwin 	 */
164238dd320SJake Burkholder 	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
165ac2e4153SJulian Elischer 	if (p->p_flag & P_THREADED) {
166b40ce416SJulian Elischer 		/* XXXKSE What to do? */
167b40ce416SJulian Elischer 	} else {
1686caa8a15SJohn Baldwin 		pstats = p->p_stats;
169238dd320SJake Burkholder 		if (CLKF_USERMODE(frame) &&
1706caa8a15SJohn Baldwin 		    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
171b40ce416SJulian Elischer 		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) {
172b40ce416SJulian Elischer 			p->p_sflag |= PS_ALRMPEND;
1734a338afdSJulian Elischer 			td->td_flags |= TDF_ASTPENDING;
174b40ce416SJulian Elischer 		}
1756caa8a15SJohn Baldwin 		if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
176b40ce416SJulian Elischer 		    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) {
177b40ce416SJulian Elischer 			p->p_sflag |= PS_PROFPEND;
1784a338afdSJulian Elischer 			td->td_flags |= TDF_ASTPENDING;
179b40ce416SJulian Elischer 		}
180b40ce416SJulian Elischer 	}
181238dd320SJake Burkholder 	mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
1826caa8a15SJohn Baldwin }
1836caa8a15SJohn Baldwin 
1846caa8a15SJohn Baldwin /*
185df8bae1dSRodney W. Grimes  * The real-time timer, interrupting hz times per second.
186df8bae1dSRodney W. Grimes  */
187df8bae1dSRodney W. Grimes void
188df8bae1dSRodney W. Grimes hardclock(frame)
189df8bae1dSRodney W. Grimes 	register struct clockframe *frame;
190df8bae1dSRodney W. Grimes {
191fa2fbc3dSJake Burkholder 	int need_softclock = 0;
192df8bae1dSRodney W. Grimes 
193c9f4877dSJake Burkholder 	CTR0(KTR_CLK, "hardclock fired");
194238dd320SJake Burkholder 	hardclock_process(frame);
195b05dcf3cSPoul-Henning Kamp 
196e7fa55afSPoul-Henning Kamp 	tc_ticktock();
197df8bae1dSRodney W. Grimes 	/*
198df8bae1dSRodney W. Grimes 	 * If no separate statistics clock is available, run it from here.
1996caa8a15SJohn Baldwin 	 *
2006caa8a15SJohn Baldwin 	 * XXX: this only works for UP
201df8bae1dSRodney W. Grimes 	 */
202238dd320SJake Burkholder 	if (stathz == 0) {
203238dd320SJake Burkholder 		profclock(frame);
204df8bae1dSRodney W. Grimes 		statclock(frame);
205238dd320SJake Burkholder 	}
206df8bae1dSRodney W. Grimes 
207e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
208daccb638SLuigi Rizzo 	hardclock_device_poll();	/* this is very short and quick */
209e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
2103f31c649SGarrett Wollman 
211b05dcf3cSPoul-Henning Kamp 	/*
212b05dcf3cSPoul-Henning Kamp 	 * Process callouts at a very low cpu priority, so we don't keep the
213b05dcf3cSPoul-Henning Kamp 	 * relatively high clock interrupt priority any longer than necessary.
214b05dcf3cSPoul-Henning Kamp 	 */
21521a7a9aeSJohn Baldwin 	mtx_lock_spin_flags(&callout_lock, MTX_QUIET);
216fa2fbc3dSJake Burkholder 	ticks++;
217b05dcf3cSPoul-Henning Kamp 	if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
218fa2fbc3dSJake Burkholder 		need_softclock = 1;
219b05dcf3cSPoul-Henning Kamp 	} else if (softticks + 1 == ticks)
220b05dcf3cSPoul-Henning Kamp 		++softticks;
22121a7a9aeSJohn Baldwin 	mtx_unlock_spin_flags(&callout_lock, MTX_QUIET);
222fa2fbc3dSJake Burkholder 
223fa2fbc3dSJake Burkholder 	/*
224062d8ff5SJohn Baldwin 	 * swi_sched acquires sched_lock, so we don't want to call it with
225fa2fbc3dSJake Burkholder 	 * callout_lock held; incorrect locking order.
226fa2fbc3dSJake Burkholder 	 */
227fa2fbc3dSJake Burkholder 	if (need_softclock)
228c86b6ff5SJohn Baldwin 		swi_sched(softclock_ih, 0);
229ab36c067SJustin T. Gibbs }
230ab36c067SJustin T. Gibbs 
231df8bae1dSRodney W. Grimes /*
232227ee8a1SPoul-Henning Kamp  * Compute number of ticks in the specified amount of time.
233df8bae1dSRodney W. Grimes  */
234df8bae1dSRodney W. Grimes int
235227ee8a1SPoul-Henning Kamp tvtohz(tv)
236df8bae1dSRodney W. Grimes 	struct timeval *tv;
237df8bae1dSRodney W. Grimes {
2386976af69SBruce Evans 	register unsigned long ticks;
2396976af69SBruce Evans 	register long sec, usec;
240df8bae1dSRodney W. Grimes 
241df8bae1dSRodney W. Grimes 	/*
2426976af69SBruce Evans 	 * If the number of usecs in the whole seconds part of the time
2436976af69SBruce Evans 	 * difference fits in a long, then the total number of usecs will
2446976af69SBruce Evans 	 * fit in an unsigned long.  Compute the total and convert it to
2456976af69SBruce Evans 	 * ticks, rounding up and adding 1 to allow for the current tick
2466976af69SBruce Evans 	 * to expire.  Rounding also depends on unsigned long arithmetic
2476976af69SBruce Evans 	 * to avoid overflow.
248df8bae1dSRodney W. Grimes 	 *
2496976af69SBruce Evans 	 * Otherwise, if the number of ticks in the whole seconds part of
2506976af69SBruce Evans 	 * the time difference fits in a long, then convert the parts to
2516976af69SBruce Evans 	 * ticks separately and add, using similar rounding methods and
2526976af69SBruce Evans 	 * overflow avoidance.  This method would work in the previous
2536976af69SBruce Evans 	 * case but it is slightly slower and assumes that hz is integral.
2546976af69SBruce Evans 	 *
2556976af69SBruce Evans 	 * Otherwise, round the time difference down to the maximum
2566976af69SBruce Evans 	 * representable value.
2576976af69SBruce Evans 	 *
2586976af69SBruce Evans 	 * If ints have 32 bits, then the maximum value for any timeout in
2596976af69SBruce Evans 	 * 10ms ticks is 248 days.
260df8bae1dSRodney W. Grimes 	 */
261227ee8a1SPoul-Henning Kamp 	sec = tv->tv_sec;
262227ee8a1SPoul-Henning Kamp 	usec = tv->tv_usec;
2636976af69SBruce Evans 	if (usec < 0) {
2646976af69SBruce Evans 		sec--;
2656976af69SBruce Evans 		usec += 1000000;
2666976af69SBruce Evans 	}
2676976af69SBruce Evans 	if (sec < 0) {
2686976af69SBruce Evans #ifdef DIAGNOSTIC
269b05dcf3cSPoul-Henning Kamp 		if (usec > 0) {
2707ec73f64SPoul-Henning Kamp 			sec++;
2717ec73f64SPoul-Henning Kamp 			usec -= 1000000;
2727ec73f64SPoul-Henning Kamp 		}
273227ee8a1SPoul-Henning Kamp 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
2746976af69SBruce Evans 		       sec, usec);
2756976af69SBruce Evans #endif
2766976af69SBruce Evans 		ticks = 1;
2776976af69SBruce Evans 	} else if (sec <= LONG_MAX / 1000000)
2786976af69SBruce Evans 		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
2796976af69SBruce Evans 			/ tick + 1;
2806976af69SBruce Evans 	else if (sec <= LONG_MAX / hz)
2816976af69SBruce Evans 		ticks = sec * hz
2826976af69SBruce Evans 			+ ((unsigned long)usec + (tick - 1)) / tick + 1;
2836976af69SBruce Evans 	else
2846976af69SBruce Evans 		ticks = LONG_MAX;
2856976af69SBruce Evans 	if (ticks > INT_MAX)
2866976af69SBruce Evans 		ticks = INT_MAX;
287d6116663SAlexander Langer 	return ((int)ticks);
288df8bae1dSRodney W. Grimes }
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes /*
291df8bae1dSRodney W. Grimes  * Start profiling on a process.
292df8bae1dSRodney W. Grimes  *
293df8bae1dSRodney W. Grimes  * Kernel profiling passes proc0 which never exits and hence
294df8bae1dSRodney W. Grimes  * keeps the profile clock running constantly.
295df8bae1dSRodney W. Grimes  */
296df8bae1dSRodney W. Grimes void
297df8bae1dSRodney W. Grimes startprofclock(p)
298df8bae1dSRodney W. Grimes 	register struct proc *p;
299df8bae1dSRodney W. Grimes {
300df8bae1dSRodney W. Grimes 
30101cd094cSJohn Baldwin 	/*
30201cd094cSJohn Baldwin 	 * XXX; Right now sched_lock protects statclock(), but perhaps
30301cd094cSJohn Baldwin 	 * it should be protected later on by a time_lock, which would
30401cd094cSJohn Baldwin 	 * cover psdiv, etc. as well.
30501cd094cSJohn Baldwin 	 */
3069752f794SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3079752f794SJohn Baldwin 	if (p->p_flag & P_STOPPROF)
308a282253aSJulian Elischer 		return;
3099752f794SJohn Baldwin 	if ((p->p_flag & P_PROFIL) == 0) {
3109752f794SJohn Baldwin 		mtx_lock_spin(&sched_lock);
3119752f794SJohn Baldwin 		p->p_flag |= P_PROFIL;
312238dd320SJake Burkholder 		if (++profprocs == 1)
313238dd320SJake Burkholder 			cpu_startprofclock();
3149ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
315df8bae1dSRodney W. Grimes 	}
3169752f794SJohn Baldwin }
317df8bae1dSRodney W. Grimes 
318df8bae1dSRodney W. Grimes /*
319df8bae1dSRodney W. Grimes  * Stop profiling on a process.
320df8bae1dSRodney W. Grimes  */
321df8bae1dSRodney W. Grimes void
322df8bae1dSRodney W. Grimes stopprofclock(p)
323df8bae1dSRodney W. Grimes 	register struct proc *p;
324df8bae1dSRodney W. Grimes {
325df8bae1dSRodney W. Grimes 
326a282253aSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
3279752f794SJohn Baldwin 	if (p->p_flag & P_PROFIL) {
3289752f794SJohn Baldwin 		if (p->p_profthreads != 0) {
3299752f794SJohn Baldwin 			p->p_flag |= P_STOPPROF;
3309752f794SJohn Baldwin 			while (p->p_profthreads != 0)
331a282253aSJulian Elischer 				msleep(&p->p_profthreads, &p->p_mtx, PPAUSE,
332a282253aSJulian Elischer 				    "stopprof", NULL);
3339752f794SJohn Baldwin 			p->p_flag &= ~P_STOPPROF;
334a282253aSJulian Elischer 		}
3359752f794SJohn Baldwin 		mtx_lock_spin(&sched_lock);
3369752f794SJohn Baldwin 		p->p_flag &= ~P_PROFIL;
337238dd320SJake Burkholder 		if (--profprocs == 0)
338238dd320SJake Burkholder 			cpu_stopprofclock();
3399ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
340df8bae1dSRodney W. Grimes 	}
3419752f794SJohn Baldwin }
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes /*
344238dd320SJake Burkholder  * Statistics clock.  Grab profile sample, and if divider reaches 0,
345238dd320SJake Burkholder  * do process and kernel statistics.  Most of the statistics are only
34671a62f8aSBruce Evans  * used by user-level statistics programs.  The main exceptions are
347238dd320SJake Burkholder  * ke->ke_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu.
348238dd320SJake Burkholder  * This should be called by all active processors.
349df8bae1dSRodney W. Grimes  */
350df8bae1dSRodney W. Grimes void
351238dd320SJake Burkholder statclock(frame)
352238dd320SJake Burkholder 	register struct clockframe *frame;
353df8bae1dSRodney W. Grimes {
3548a129caeSDavid Greenman 	struct pstats *pstats;
3558a129caeSDavid Greenman 	struct rusage *ru;
3568a129caeSDavid Greenman 	struct vmspace *vm;
357238dd320SJake Burkholder 	struct thread *td;
358238dd320SJake Burkholder 	struct kse *ke;
359238dd320SJake Burkholder 	struct proc *p;
360238dd320SJake Burkholder 	long rss;
3618a129caeSDavid Greenman 
362238dd320SJake Burkholder 	td = curthread;
363238dd320SJake Burkholder 	p = td->td_proc;
364238dd320SJake Burkholder 
365238dd320SJake Burkholder 	mtx_lock_spin_flags(&sched_lock, MTX_QUIET);
366238dd320SJake Burkholder 	ke = td->td_kse;
367238dd320SJake Burkholder 	if (CLKF_USERMODE(frame)) {
368df8bae1dSRodney W. Grimes 		/*
36971a62f8aSBruce Evans 		 * Charge the time as appropriate.
370df8bae1dSRodney W. Grimes 		 */
371ac2e4153SJulian Elischer 		if (p->p_flag & P_THREADED)
3725215b187SJeff Roberson 			thread_statclock(1);
373e4625663SJeff Roberson 		p->p_uticks++;
3746f8132a8SJulian Elischer 		if (ke->ke_ksegrp->kg_nice > NZERO)
375df8bae1dSRodney W. Grimes 			cp_time[CP_NICE]++;
376df8bae1dSRodney W. Grimes 		else
377df8bae1dSRodney W. Grimes 			cp_time[CP_USER]++;
378df8bae1dSRodney W. Grimes 	} else {
379df8bae1dSRodney W. Grimes 		/*
380df8bae1dSRodney W. Grimes 		 * Came from kernel mode, so we were:
381df8bae1dSRodney W. Grimes 		 * - handling an interrupt,
382df8bae1dSRodney W. Grimes 		 * - doing syscall or trap work on behalf of the current
383df8bae1dSRodney W. Grimes 		 *   user process, or
384df8bae1dSRodney W. Grimes 		 * - spinning in the idle loop.
385df8bae1dSRodney W. Grimes 		 * Whichever it is, charge the time as appropriate.
386df8bae1dSRodney W. Grimes 		 * Note that we charge interrupts to the current process,
387df8bae1dSRodney W. Grimes 		 * regardless of whether they are ``for'' that process,
388df8bae1dSRodney W. Grimes 		 * so that we know how much of its real time was spent
389df8bae1dSRodney W. Grimes 		 * in ``non-process'' (i.e., interrupt) work.
390df8bae1dSRodney W. Grimes 		 */
391b40ce416SJulian Elischer 		if ((td->td_ithd != NULL) || td->td_intr_nesting_level >= 2) {
392e4625663SJeff Roberson 			p->p_iticks++;
393df8bae1dSRodney W. Grimes 			cp_time[CP_INTR]++;
3940384fff8SJason Evans 		} else {
395ac2e4153SJulian Elischer 			if (p->p_flag & P_THREADED)
3965215b187SJeff Roberson 				thread_statclock(0);
397e4625663SJeff Roberson 			td->td_sticks++;
398e4625663SJeff Roberson 			p->p_sticks++;
399b40ce416SJulian Elischer 			if (p != PCPU_GET(idlethread)->td_proc)
400df8bae1dSRodney W. Grimes 				cp_time[CP_SYS]++;
4010384fff8SJason Evans 			else
402df8bae1dSRodney W. Grimes 				cp_time[CP_IDLE]++;
403df8bae1dSRodney W. Grimes 		}
4040384fff8SJason Evans 	}
405df8bae1dSRodney W. Grimes 
406f6f230feSJeff Roberson 	sched_clock(ke);
407f5e9e8ecSBruce Evans 
408f5e9e8ecSBruce Evans 	/* Update resource usage integrals and maximums. */
409f5e9e8ecSBruce Evans 	if ((pstats = p->p_stats) != NULL &&
410f5e9e8ecSBruce Evans 	    (ru = &pstats->p_ru) != NULL &&
411f5e9e8ecSBruce Evans 	    (vm = p->p_vmspace) != NULL) {
4121c6d46f9SLuoqi Chen 		ru->ru_ixrss += pgtok(vm->vm_tsize);
4131c6d46f9SLuoqi Chen 		ru->ru_idrss += pgtok(vm->vm_dsize);
4141c6d46f9SLuoqi Chen 		ru->ru_isrss += pgtok(vm->vm_ssize);
4151c6d46f9SLuoqi Chen 		rss = pgtok(vmspace_resident_count(vm));
416f5e9e8ecSBruce Evans 		if (ru->ru_maxrss < rss)
417f5e9e8ecSBruce Evans 			ru->ru_maxrss = rss;
418f5e9e8ecSBruce Evans 	}
419238dd320SJake Burkholder 	mtx_unlock_spin_flags(&sched_lock, MTX_QUIET);
4206caa8a15SJohn Baldwin }
4216c567274SJohn Baldwin 
4226caa8a15SJohn Baldwin void
423238dd320SJake Burkholder profclock(frame)
4246caa8a15SJohn Baldwin 	register struct clockframe *frame;
4256caa8a15SJohn Baldwin {
426238dd320SJake Burkholder 	struct thread *td;
427238dd320SJake Burkholder #ifdef GPROF
428238dd320SJake Burkholder 	struct gmonparam *g;
429238dd320SJake Burkholder 	int i;
430238dd320SJake Burkholder #endif
4316caa8a15SJohn Baldwin 
4324a338afdSJulian Elischer 	td = curthread;
433238dd320SJake Burkholder 	if (CLKF_USERMODE(frame)) {
434238dd320SJake Burkholder 		/*
435238dd320SJake Burkholder 		 * Came from user mode; CPU was in user state.
436238dd320SJake Burkholder 		 * If this process is being profiled, record the tick.
437a282253aSJulian Elischer 		 * if there is no related user location yet, don't
438a282253aSJulian Elischer 		 * bother trying to count it.
439238dd320SJake Burkholder 		 */
440238dd320SJake Burkholder 		td = curthread;
4419752f794SJohn Baldwin 		if (td->td_proc->p_flag & P_PROFIL)
4424a338afdSJulian Elischer 			addupc_intr(td, CLKF_PC(frame), 1);
443238dd320SJake Burkholder 	}
444238dd320SJake Burkholder #ifdef GPROF
445238dd320SJake Burkholder 	else {
446238dd320SJake Burkholder 		/*
447238dd320SJake Burkholder 		 * Kernel statistics are just like addupc_intr, only easier.
448238dd320SJake Burkholder 		 */
449238dd320SJake Burkholder 		g = &_gmonparam;
450238dd320SJake Burkholder 		if (g->state == GMON_PROF_ON) {
451238dd320SJake Burkholder 			i = CLKF_PC(frame) - g->lowpc;
452238dd320SJake Burkholder 			if (i < g->textsize) {
453238dd320SJake Burkholder 				i /= HISTFRACTION * sizeof(*g->kcount);
454238dd320SJake Burkholder 				g->kcount[i]++;
455238dd320SJake Burkholder 			}
456238dd320SJake Burkholder 		}
457238dd320SJake Burkholder 	}
458238dd320SJake Burkholder #endif
459df8bae1dSRodney W. Grimes }
460df8bae1dSRodney W. Grimes 
461df8bae1dSRodney W. Grimes /*
462df8bae1dSRodney W. Grimes  * Return information about system clocks.
463df8bae1dSRodney W. Grimes  */
464787d58f2SPoul-Henning Kamp static int
46582d9ae4eSPoul-Henning Kamp sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
466df8bae1dSRodney W. Grimes {
467df8bae1dSRodney W. Grimes 	struct clockinfo clkinfo;
468df8bae1dSRodney W. Grimes 	/*
469df8bae1dSRodney W. Grimes 	 * Construct clockinfo structure.
470df8bae1dSRodney W. Grimes 	 */
471a9a0f15aSBruce Evans 	bzero(&clkinfo, sizeof(clkinfo));
472df8bae1dSRodney W. Grimes 	clkinfo.hz = hz;
473df8bae1dSRodney W. Grimes 	clkinfo.tick = tick;
474df8bae1dSRodney W. Grimes 	clkinfo.profhz = profhz;
475df8bae1dSRodney W. Grimes 	clkinfo.stathz = stathz ? stathz : hz;
476ae0eb976SPoul-Henning Kamp 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
477df8bae1dSRodney W. Grimes }
4783f31c649SGarrett Wollman 
479946bb7a2SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
480af1408e3SLuigi Rizzo 	0, 0, sysctl_kern_clockrate, "S,clockinfo",
481af1408e3SLuigi Rizzo 	"Rate and period of various kernel clocks");
482