xref: /freebsd/sys/kern/kern_clock.c (revision 8f51ad55e728371ce9a00ef8fb9fcc89b9b3a7e6)
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  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
40911d16b8SEd Maste #include "opt_kdb.h"
41f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
424da0d332SPeter Wemm #include "opt_hwpmc_hooks.h"
4332c20357SPoul-Henning Kamp #include "opt_ntp.h"
44370c3cb5SSean Kelly #include "opt_watchdog.h"
4532c20357SPoul-Henning Kamp 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47df8bae1dSRodney W. Grimes #include <sys/systm.h>
48df8bae1dSRodney W. Grimes #include <sys/callout.h>
492d50560aSMarcel Moolenaar #include <sys/kdb.h>
50df8bae1dSRodney W. Grimes #include <sys/kernel.h>
51f34fa851SJohn Baldwin #include <sys/lock.h>
5261d80e90SJohn Baldwin #include <sys/ktr.h>
5335e0e5b3SJohn Baldwin #include <sys/mutex.h>
54df8bae1dSRodney W. Grimes #include <sys/proc.h>
55e4625663SJeff Roberson #include <sys/resource.h>
56df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
57b43179fbSJeff Roberson #include <sys/sched.h>
58797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
596caa8a15SJohn Baldwin #include <sys/smp.h>
608a129caeSDavid Greenman #include <vm/vm.h>
61efeaf95aSDavid Greenman #include <vm/pmap.h>
62efeaf95aSDavid Greenman #include <vm/vm_map.h>
63797f2d22SPoul-Henning Kamp #include <sys/sysctl.h>
648088699fSJohn Baldwin #include <sys/bus.h>
658088699fSJohn Baldwin #include <sys/interrupt.h>
66104a9b7eSAlexander Kabaev #include <sys/limits.h>
67e7fa55afSPoul-Henning Kamp #include <sys/timetc.h>
68df8bae1dSRodney W. Grimes 
69df8bae1dSRodney W. Grimes #ifdef GPROF
70df8bae1dSRodney W. Grimes #include <sys/gmon.h>
71df8bae1dSRodney W. Grimes #endif
72df8bae1dSRodney W. Grimes 
7336c0fd9dSJoseph Koshy #ifdef HWPMC_HOOKS
7436c0fd9dSJoseph Koshy #include <sys/pmckern.h>
7536c0fd9dSJoseph Koshy #endif
7636c0fd9dSJoseph Koshy 
77e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
78e4fc250cSLuigi Rizzo extern void hardclock_device_poll(void);
79e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
80eae8fc2cSSteve Passe 
814d77a549SAlfred Perlstein static void initclocks(void *dummy);
82237fdd78SRobert Watson SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL);
832b14f991SJulian Elischer 
848b98fec9SJeff Roberson /* Spin-lock protecting profiling statistics. */
8586a49deaSAttilio Rao static struct mtx time_lock;
868b98fec9SJeff Roberson 
8762919d78SPeter Wemm static int
8862919d78SPeter Wemm sysctl_kern_cp_time(SYSCTL_HANDLER_ARGS)
8962919d78SPeter Wemm {
9062919d78SPeter Wemm 	int error;
917628402bSPeter Wemm 	long cp_time[CPUSTATES];
92cff2e749SPaul Saab #ifdef SCTL_MASK32
9362919d78SPeter Wemm 	int i;
9462919d78SPeter Wemm 	unsigned int cp_time32[CPUSTATES];
957628402bSPeter Wemm #endif
9662919d78SPeter Wemm 
977628402bSPeter Wemm 	read_cpu_time(cp_time);
987628402bSPeter Wemm #ifdef SCTL_MASK32
99cff2e749SPaul Saab 	if (req->flags & SCTL_MASK32) {
10062919d78SPeter Wemm 		if (!req->oldptr)
10162919d78SPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(cp_time32));
10262919d78SPeter Wemm 		for (i = 0; i < CPUSTATES; i++)
10362919d78SPeter Wemm 			cp_time32[i] = (unsigned int)cp_time[i];
10462919d78SPeter Wemm 		error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
10562919d78SPeter Wemm 	} else
10662919d78SPeter Wemm #endif
10762919d78SPeter Wemm 	{
10862919d78SPeter Wemm 		if (!req->oldptr)
10962919d78SPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(cp_time));
11062919d78SPeter Wemm 		error = SYSCTL_OUT(req, cp_time, sizeof(cp_time));
11162919d78SPeter Wemm 	}
11262919d78SPeter Wemm 	return error;
11362919d78SPeter Wemm }
11462919d78SPeter Wemm 
11562919d78SPeter Wemm SYSCTL_PROC(_kern, OID_AUTO, cp_time, CTLTYPE_LONG|CTLFLAG_RD,
11662919d78SPeter Wemm     0,0, sysctl_kern_cp_time, "LU", "CPU time statistics");
1177f112b04SRobert Watson 
1187628402bSPeter Wemm static long empty[CPUSTATES];
1197628402bSPeter Wemm 
1207628402bSPeter Wemm static int
1217628402bSPeter Wemm sysctl_kern_cp_times(SYSCTL_HANDLER_ARGS)
1227628402bSPeter Wemm {
1237628402bSPeter Wemm 	struct pcpu *pcpu;
1247628402bSPeter Wemm 	int error;
125ef54068bSRobert Watson 	int c;
1267628402bSPeter Wemm 	long *cp_time;
1277628402bSPeter Wemm #ifdef SCTL_MASK32
1287628402bSPeter Wemm 	unsigned int cp_time32[CPUSTATES];
129ef54068bSRobert Watson 	int i;
1307628402bSPeter Wemm #endif
1317628402bSPeter Wemm 
1327628402bSPeter Wemm 	if (!req->oldptr) {
1337628402bSPeter Wemm #ifdef SCTL_MASK32
1347628402bSPeter Wemm 		if (req->flags & SCTL_MASK32)
1357628402bSPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(cp_time32) * (mp_maxid + 1));
1367628402bSPeter Wemm 		else
1377628402bSPeter Wemm #endif
1387628402bSPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(long) * CPUSTATES * (mp_maxid + 1));
1397628402bSPeter Wemm 	}
1407628402bSPeter Wemm 	for (error = 0, c = 0; error == 0 && c <= mp_maxid; c++) {
1417628402bSPeter Wemm 		if (!CPU_ABSENT(c)) {
1427628402bSPeter Wemm 			pcpu = pcpu_find(c);
1437628402bSPeter Wemm 			cp_time = pcpu->pc_cp_time;
1447628402bSPeter Wemm 		} else {
1457628402bSPeter Wemm 			cp_time = empty;
1467628402bSPeter Wemm 		}
1477628402bSPeter Wemm #ifdef SCTL_MASK32
1487628402bSPeter Wemm 		if (req->flags & SCTL_MASK32) {
1497628402bSPeter Wemm 			for (i = 0; i < CPUSTATES; i++)
1507628402bSPeter Wemm 				cp_time32[i] = (unsigned int)cp_time[i];
1517628402bSPeter Wemm 			error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
1527628402bSPeter Wemm 		} else
1537628402bSPeter Wemm #endif
1547628402bSPeter Wemm 			error = SYSCTL_OUT(req, cp_time, sizeof(long) * CPUSTATES);
1557628402bSPeter Wemm 	}
1567628402bSPeter Wemm 	return error;
1577628402bSPeter Wemm }
1587628402bSPeter Wemm 
1597628402bSPeter Wemm SYSCTL_PROC(_kern, OID_AUTO, cp_times, CTLTYPE_LONG|CTLFLAG_RD,
1607628402bSPeter Wemm     0,0, sysctl_kern_cp_times, "LU", "per-CPU time statistics");
1617628402bSPeter Wemm 
1627628402bSPeter Wemm void
1637628402bSPeter Wemm read_cpu_time(long *cp_time)
1647628402bSPeter Wemm {
1657628402bSPeter Wemm 	struct pcpu *pc;
1667628402bSPeter Wemm 	int i, j;
1677628402bSPeter Wemm 
1687628402bSPeter Wemm 	/* Sum up global cp_time[]. */
1697628402bSPeter Wemm 	bzero(cp_time, sizeof(long) * CPUSTATES);
1707628402bSPeter Wemm 	for (i = 0; i <= mp_maxid; i++) {
1717628402bSPeter Wemm 		if (CPU_ABSENT(i))
1727628402bSPeter Wemm 			continue;
1737628402bSPeter Wemm 		pc = pcpu_find(i);
1747628402bSPeter Wemm 		for (j = 0; j < CPUSTATES; j++)
1757628402bSPeter Wemm 			cp_time[j] += pc->pc_cp_time[j];
1767628402bSPeter Wemm 	}
1777628402bSPeter Wemm }
1787628402bSPeter Wemm 
1794103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG
1804103b765SPoul-Henning Kamp #include <sys/watchdog.h>
181370c3cb5SSean Kelly 
1824103b765SPoul-Henning Kamp static int watchdog_ticks;
183370c3cb5SSean Kelly static int watchdog_enabled;
1844103b765SPoul-Henning Kamp static void watchdog_fire(void);
1854103b765SPoul-Henning Kamp static void watchdog_config(void *, u_int, int *);
1864103b765SPoul-Henning Kamp #endif /* SW_WATCHDOG */
187370c3cb5SSean Kelly 
1883bac064fSPoul-Henning Kamp /*
189df8bae1dSRodney W. Grimes  * Clock handling routines.
190df8bae1dSRodney W. Grimes  *
191b05dcf3cSPoul-Henning Kamp  * This code is written to operate with two timers that run independently of
192b05dcf3cSPoul-Henning Kamp  * each other.
1937ec73f64SPoul-Henning Kamp  *
194b05dcf3cSPoul-Henning Kamp  * The main timer, running hz times per second, is used to trigger interval
195b05dcf3cSPoul-Henning Kamp  * timers, timeouts and rescheduling as needed.
1967ec73f64SPoul-Henning Kamp  *
197b05dcf3cSPoul-Henning Kamp  * The second timer handles kernel and user profiling,
198b05dcf3cSPoul-Henning Kamp  * and does resource use estimation.  If the second timer is programmable,
199b05dcf3cSPoul-Henning Kamp  * it is randomized to avoid aliasing between the two clocks.  For example,
200b05dcf3cSPoul-Henning Kamp  * the randomization prevents an adversary from always giving up the cpu
201df8bae1dSRodney W. Grimes  * just before its quantum expires.  Otherwise, it would never accumulate
202df8bae1dSRodney W. Grimes  * cpu ticks.  The mean frequency of the second timer is stathz.
203b05dcf3cSPoul-Henning Kamp  *
204b05dcf3cSPoul-Henning Kamp  * If no second timer exists, stathz will be zero; in this case we drive
205b05dcf3cSPoul-Henning Kamp  * profiling and statistics off the main clock.  This WILL NOT be accurate;
206b05dcf3cSPoul-Henning Kamp  * do not do it unless absolutely necessary.
207b05dcf3cSPoul-Henning Kamp  *
208df8bae1dSRodney W. Grimes  * The statistics clock may (or may not) be run at a higher rate while
209b05dcf3cSPoul-Henning Kamp  * profiling.  This profile clock runs at profhz.  We require that profhz
210b05dcf3cSPoul-Henning Kamp  * be an integral multiple of stathz.
211b05dcf3cSPoul-Henning Kamp  *
212b05dcf3cSPoul-Henning Kamp  * If the statistics clock is running fast, it must be divided by the ratio
213b05dcf3cSPoul-Henning Kamp  * profhz/stathz for statistics.  (For profiling, every tick counts.)
214df8bae1dSRodney W. Grimes  *
2157ec73f64SPoul-Henning Kamp  * Time-of-day is maintained using a "timecounter", which may or may
2167ec73f64SPoul-Henning Kamp  * not be related to the hardware generating the above mentioned
2177ec73f64SPoul-Henning Kamp  * interrupts.
218df8bae1dSRodney W. Grimes  */
219df8bae1dSRodney W. Grimes 
220df8bae1dSRodney W. Grimes int	stathz;
221df8bae1dSRodney W. Grimes int	profhz;
222238dd320SJake Burkholder int	profprocs;
223df8bae1dSRodney W. Grimes int	ticks;
224238dd320SJake Burkholder int	psratio;
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes /*
227df8bae1dSRodney W. Grimes  * Initialize clock frequencies and start both clocks running.
228df8bae1dSRodney W. Grimes  */
2292b14f991SJulian Elischer /* ARGSUSED*/
2302b14f991SJulian Elischer static void
231d841aaa7SBruce Evans initclocks(dummy)
232d841aaa7SBruce Evans 	void *dummy;
233df8bae1dSRodney W. Grimes {
234df8bae1dSRodney W. Grimes 	register int i;
235df8bae1dSRodney W. Grimes 
236df8bae1dSRodney W. Grimes 	/*
237df8bae1dSRodney W. Grimes 	 * Set divisors to 1 (normal case) and let the machine-specific
238df8bae1dSRodney W. Grimes 	 * code do its bit.
239df8bae1dSRodney W. Grimes 	 */
2408b98fec9SJeff Roberson 	mtx_init(&time_lock, "time lock", NULL, MTX_SPIN);
24163d69d25SRobert Watson 	cpu_initclocks();
242df8bae1dSRodney W. Grimes 
243df8bae1dSRodney W. Grimes 	/*
244df8bae1dSRodney W. Grimes 	 * Compute profhz/stathz, and fix profhz if needed.
245df8bae1dSRodney W. Grimes 	 */
246df8bae1dSRodney W. Grimes 	i = stathz ? stathz : hz;
247df8bae1dSRodney W. Grimes 	if (profhz == 0)
248df8bae1dSRodney W. Grimes 		profhz = i;
249df8bae1dSRodney W. Grimes 	psratio = profhz / i;
2504103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG
2514103b765SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0);
2524103b765SPoul-Henning Kamp #endif
253df8bae1dSRodney W. Grimes }
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes /*
256238dd320SJake Burkholder  * Each time the real-time timer fires, this function is called on all CPUs.
257b439e431SJohn Baldwin  * Note that hardclock() calls hardclock_cpu() for the boot CPU, so only
258238dd320SJake Burkholder  * the other CPUs in the system need to call this function.
2596caa8a15SJohn Baldwin  */
2606caa8a15SJohn Baldwin void
261b439e431SJohn Baldwin hardclock_cpu(int usermode)
2626caa8a15SJohn Baldwin {
2636caa8a15SJohn Baldwin 	struct pstats *pstats;
264238dd320SJake Burkholder 	struct thread *td = curthread;
265b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
266b61ce5b0SJeff Roberson 	int flags;
2676caa8a15SJohn Baldwin 
2686caa8a15SJohn Baldwin 	/*
2696caa8a15SJohn Baldwin 	 * Run current process's virtual and profile time, as needed.
2706caa8a15SJohn Baldwin 	 */
271ad1e7d28SJulian Elischer 	pstats = p->p_stats;
272b61ce5b0SJeff Roberson 	flags = 0;
273ad1e7d28SJulian Elischer 	if (usermode &&
27440acdeabSJeff Roberson 	    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) {
27540acdeabSJeff Roberson 		PROC_SLOCK(p);
276b61ce5b0SJeff Roberson 		if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
277b61ce5b0SJeff Roberson 			flags |= TDF_ALRMPEND | TDF_ASTPENDING;
27840acdeabSJeff Roberson 		PROC_SUNLOCK(p);
27940acdeabSJeff Roberson 	}
28040acdeabSJeff Roberson 	if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) {
28140acdeabSJeff Roberson 		PROC_SLOCK(p);
282b61ce5b0SJeff Roberson 		if (itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
283b61ce5b0SJeff Roberson 			flags |= TDF_PROFPEND | TDF_ASTPENDING;
28440acdeabSJeff Roberson 		PROC_SUNLOCK(p);
28540acdeabSJeff Roberson 	}
28640acdeabSJeff Roberson 	thread_lock(td);
28740acdeabSJeff Roberson 	sched_tick();
288b61ce5b0SJeff Roberson 	td->td_flags |= flags;
28940acdeabSJeff Roberson 	thread_unlock(td);
29036c0fd9dSJoseph Koshy 
29136c0fd9dSJoseph Koshy #ifdef	HWPMC_HOOKS
29236c0fd9dSJoseph Koshy 	if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
29336c0fd9dSJoseph Koshy 		PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
29436c0fd9dSJoseph Koshy #endif
2958d809d50SJeff Roberson 	callout_tick();
2966caa8a15SJohn Baldwin }
2976caa8a15SJohn Baldwin 
2986caa8a15SJohn Baldwin /*
299df8bae1dSRodney W. Grimes  * The real-time timer, interrupting hz times per second.
300df8bae1dSRodney W. Grimes  */
301df8bae1dSRodney W. Grimes void
302b439e431SJohn Baldwin hardclock(int usermode, uintfptr_t pc)
303df8bae1dSRodney W. Grimes {
304df8bae1dSRodney W. Grimes 
3058d809d50SJeff Roberson 	atomic_add_int((volatile int *)&ticks, 1);
306b439e431SJohn Baldwin 	hardclock_cpu(usermode);
307e7fa55afSPoul-Henning Kamp 	tc_ticktock();
308df8bae1dSRodney W. Grimes 	/*
309df8bae1dSRodney W. Grimes 	 * If no separate statistics clock is available, run it from here.
3106caa8a15SJohn Baldwin 	 *
3116caa8a15SJohn Baldwin 	 * XXX: this only works for UP
312df8bae1dSRodney W. Grimes 	 */
313238dd320SJake Burkholder 	if (stathz == 0) {
314b439e431SJohn Baldwin 		profclock(usermode, pc);
315b439e431SJohn Baldwin 		statclock(usermode);
316238dd320SJake Burkholder 	}
317e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
318daccb638SLuigi Rizzo 	hardclock_device_poll();	/* this is very short and quick */
319e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3204103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG
3214103b765SPoul-Henning Kamp 	if (watchdog_enabled > 0 && --watchdog_ticks <= 0)
322370c3cb5SSean Kelly 		watchdog_fire();
3234103b765SPoul-Henning Kamp #endif /* SW_WATCHDOG */
324ab36c067SJustin T. Gibbs }
325ab36c067SJustin T. Gibbs 
326df8bae1dSRodney W. Grimes /*
327227ee8a1SPoul-Henning Kamp  * Compute number of ticks in the specified amount of time.
328df8bae1dSRodney W. Grimes  */
329df8bae1dSRodney W. Grimes int
330227ee8a1SPoul-Henning Kamp tvtohz(tv)
331df8bae1dSRodney W. Grimes 	struct timeval *tv;
332df8bae1dSRodney W. Grimes {
3336976af69SBruce Evans 	register unsigned long ticks;
3346976af69SBruce Evans 	register long sec, usec;
335df8bae1dSRodney W. Grimes 
336df8bae1dSRodney W. Grimes 	/*
3376976af69SBruce Evans 	 * If the number of usecs in the whole seconds part of the time
3386976af69SBruce Evans 	 * difference fits in a long, then the total number of usecs will
3396976af69SBruce Evans 	 * fit in an unsigned long.  Compute the total and convert it to
3406976af69SBruce Evans 	 * ticks, rounding up and adding 1 to allow for the current tick
3416976af69SBruce Evans 	 * to expire.  Rounding also depends on unsigned long arithmetic
3426976af69SBruce Evans 	 * to avoid overflow.
343df8bae1dSRodney W. Grimes 	 *
3446976af69SBruce Evans 	 * Otherwise, if the number of ticks in the whole seconds part of
3456976af69SBruce Evans 	 * the time difference fits in a long, then convert the parts to
3466976af69SBruce Evans 	 * ticks separately and add, using similar rounding methods and
3476976af69SBruce Evans 	 * overflow avoidance.  This method would work in the previous
3486976af69SBruce Evans 	 * case but it is slightly slower and assumes that hz is integral.
3496976af69SBruce Evans 	 *
3506976af69SBruce Evans 	 * Otherwise, round the time difference down to the maximum
3516976af69SBruce Evans 	 * representable value.
3526976af69SBruce Evans 	 *
3536976af69SBruce Evans 	 * If ints have 32 bits, then the maximum value for any timeout in
3546976af69SBruce Evans 	 * 10ms ticks is 248 days.
355df8bae1dSRodney W. Grimes 	 */
356227ee8a1SPoul-Henning Kamp 	sec = tv->tv_sec;
357227ee8a1SPoul-Henning Kamp 	usec = tv->tv_usec;
3586976af69SBruce Evans 	if (usec < 0) {
3596976af69SBruce Evans 		sec--;
3606976af69SBruce Evans 		usec += 1000000;
3616976af69SBruce Evans 	}
3626976af69SBruce Evans 	if (sec < 0) {
3636976af69SBruce Evans #ifdef DIAGNOSTIC
364b05dcf3cSPoul-Henning Kamp 		if (usec > 0) {
3657ec73f64SPoul-Henning Kamp 			sec++;
3667ec73f64SPoul-Henning Kamp 			usec -= 1000000;
3677ec73f64SPoul-Henning Kamp 		}
368227ee8a1SPoul-Henning Kamp 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
3696976af69SBruce Evans 		       sec, usec);
3706976af69SBruce Evans #endif
3716976af69SBruce Evans 		ticks = 1;
3726976af69SBruce Evans 	} else if (sec <= LONG_MAX / 1000000)
3736976af69SBruce Evans 		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
3746976af69SBruce Evans 			/ tick + 1;
3756976af69SBruce Evans 	else if (sec <= LONG_MAX / hz)
3766976af69SBruce Evans 		ticks = sec * hz
3776976af69SBruce Evans 			+ ((unsigned long)usec + (tick - 1)) / tick + 1;
3786976af69SBruce Evans 	else
3796976af69SBruce Evans 		ticks = LONG_MAX;
3806976af69SBruce Evans 	if (ticks > INT_MAX)
3816976af69SBruce Evans 		ticks = INT_MAX;
382d6116663SAlexander Langer 	return ((int)ticks);
383df8bae1dSRodney W. Grimes }
384df8bae1dSRodney W. Grimes 
385df8bae1dSRodney W. Grimes /*
386df8bae1dSRodney W. Grimes  * Start profiling on a process.
387df8bae1dSRodney W. Grimes  *
388df8bae1dSRodney W. Grimes  * Kernel profiling passes proc0 which never exits and hence
389df8bae1dSRodney W. Grimes  * keeps the profile clock running constantly.
390df8bae1dSRodney W. Grimes  */
391df8bae1dSRodney W. Grimes void
392df8bae1dSRodney W. Grimes startprofclock(p)
393df8bae1dSRodney W. Grimes 	register struct proc *p;
394df8bae1dSRodney W. Grimes {
395df8bae1dSRodney W. Grimes 
3969752f794SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
3979752f794SJohn Baldwin 	if (p->p_flag & P_STOPPROF)
398a282253aSJulian Elischer 		return;
3999752f794SJohn Baldwin 	if ((p->p_flag & P_PROFIL) == 0) {
4009752f794SJohn Baldwin 		p->p_flag |= P_PROFIL;
4018b98fec9SJeff Roberson 		mtx_lock_spin(&time_lock);
402238dd320SJake Burkholder 		if (++profprocs == 1)
403238dd320SJake Burkholder 			cpu_startprofclock();
4048b98fec9SJeff Roberson 		mtx_unlock_spin(&time_lock);
405df8bae1dSRodney W. Grimes 	}
4069752f794SJohn Baldwin }
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes /*
409df8bae1dSRodney W. Grimes  * Stop profiling on a process.
410df8bae1dSRodney W. Grimes  */
411df8bae1dSRodney W. Grimes void
412df8bae1dSRodney W. Grimes stopprofclock(p)
413df8bae1dSRodney W. Grimes 	register struct proc *p;
414df8bae1dSRodney W. Grimes {
415df8bae1dSRodney W. Grimes 
416a282253aSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
4179752f794SJohn Baldwin 	if (p->p_flag & P_PROFIL) {
4189752f794SJohn Baldwin 		if (p->p_profthreads != 0) {
4199752f794SJohn Baldwin 			p->p_flag |= P_STOPPROF;
4209752f794SJohn Baldwin 			while (p->p_profthreads != 0)
421a282253aSJulian Elischer 				msleep(&p->p_profthreads, &p->p_mtx, PPAUSE,
422a89ec05eSPeter Wemm 				    "stopprof", 0);
4239752f794SJohn Baldwin 			p->p_flag &= ~P_STOPPROF;
424a282253aSJulian Elischer 		}
425b62b2304SColin Percival 		if ((p->p_flag & P_PROFIL) == 0)
426b62b2304SColin Percival 			return;
4279752f794SJohn Baldwin 		p->p_flag &= ~P_PROFIL;
4288b98fec9SJeff Roberson 		mtx_lock_spin(&time_lock);
429238dd320SJake Burkholder 		if (--profprocs == 0)
430238dd320SJake Burkholder 			cpu_stopprofclock();
4318b98fec9SJeff Roberson 		mtx_unlock_spin(&time_lock);
432df8bae1dSRodney W. Grimes 	}
4339752f794SJohn Baldwin }
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes /*
4361c4bcd05SJeff Roberson  * Statistics clock.  Updates rusage information and calls the scheduler
4371c4bcd05SJeff Roberson  * to adjust priorities of the active thread.
4381c4bcd05SJeff Roberson  *
439238dd320SJake Burkholder  * This should be called by all active processors.
440df8bae1dSRodney W. Grimes  */
441df8bae1dSRodney W. Grimes void
442b439e431SJohn Baldwin statclock(int usermode)
443df8bae1dSRodney W. Grimes {
4448a129caeSDavid Greenman 	struct rusage *ru;
4458a129caeSDavid Greenman 	struct vmspace *vm;
446238dd320SJake Burkholder 	struct thread *td;
447238dd320SJake Burkholder 	struct proc *p;
448238dd320SJake Burkholder 	long rss;
4497628402bSPeter Wemm 	long *cp_time;
4508a129caeSDavid Greenman 
451238dd320SJake Burkholder 	td = curthread;
452238dd320SJake Burkholder 	p = td->td_proc;
453238dd320SJake Burkholder 
4547628402bSPeter Wemm 	cp_time = (long *)PCPU_PTR(cp_time);
455b439e431SJohn Baldwin 	if (usermode) {
456df8bae1dSRodney W. Grimes 		/*
45771a62f8aSBruce Evans 		 * Charge the time as appropriate.
458df8bae1dSRodney W. Grimes 		 */
459e8444a7eSPoul-Henning Kamp 		td->td_uticks++;
460fa885116SJulian Elischer 		if (p->p_nice > NZERO)
4617628402bSPeter Wemm 			cp_time[CP_NICE]++;
462df8bae1dSRodney W. Grimes 		else
4637628402bSPeter Wemm 			cp_time[CP_USER]++;
464df8bae1dSRodney W. Grimes 	} else {
465df8bae1dSRodney W. Grimes 		/*
466df8bae1dSRodney W. Grimes 		 * Came from kernel mode, so we were:
467df8bae1dSRodney W. Grimes 		 * - handling an interrupt,
468df8bae1dSRodney W. Grimes 		 * - doing syscall or trap work on behalf of the current
469df8bae1dSRodney W. Grimes 		 *   user process, or
470df8bae1dSRodney W. Grimes 		 * - spinning in the idle loop.
471df8bae1dSRodney W. Grimes 		 * Whichever it is, charge the time as appropriate.
472df8bae1dSRodney W. Grimes 		 * Note that we charge interrupts to the current process,
473df8bae1dSRodney W. Grimes 		 * regardless of whether they are ``for'' that process,
474df8bae1dSRodney W. Grimes 		 * so that we know how much of its real time was spent
475df8bae1dSRodney W. Grimes 		 * in ``non-process'' (i.e., interrupt) work.
476df8bae1dSRodney W. Grimes 		 */
477e0f66ef8SJohn Baldwin 		if ((td->td_pflags & TDP_ITHREAD) ||
478e0f66ef8SJohn Baldwin 		    td->td_intr_nesting_level >= 2) {
479e8444a7eSPoul-Henning Kamp 			td->td_iticks++;
4807628402bSPeter Wemm 			cp_time[CP_INTR]++;
4810384fff8SJason Evans 		} else {
482eb2da9a5SPoul-Henning Kamp 			td->td_pticks++;
483e8444a7eSPoul-Henning Kamp 			td->td_sticks++;
484486a9414SJulian Elischer 			if (!TD_IS_IDLETHREAD(td))
4857628402bSPeter Wemm 				cp_time[CP_SYS]++;
4860384fff8SJason Evans 			else
4877628402bSPeter Wemm 				cp_time[CP_IDLE]++;
488df8bae1dSRodney W. Grimes 		}
4890384fff8SJason Evans 	}
490f5e9e8ecSBruce Evans 
491f5e9e8ecSBruce Evans 	/* Update resource usage integrals and maximums. */
49216f9f205SJohn Baldwin 	MPASS(p->p_vmspace != NULL);
49316f9f205SJohn Baldwin 	vm = p->p_vmspace;
4941c4bcd05SJeff Roberson 	ru = &td->td_ru;
4951c6d46f9SLuoqi Chen 	ru->ru_ixrss += pgtok(vm->vm_tsize);
4961c6d46f9SLuoqi Chen 	ru->ru_idrss += pgtok(vm->vm_dsize);
4971c6d46f9SLuoqi Chen 	ru->ru_isrss += pgtok(vm->vm_ssize);
4981c6d46f9SLuoqi Chen 	rss = pgtok(vmspace_resident_count(vm));
499f5e9e8ecSBruce Evans 	if (ru->ru_maxrss < rss)
500f5e9e8ecSBruce Evans 		ru->ru_maxrss = rss;
5018f51ad55SJeff Roberson 	KTR_POINT2(KTR_SCHED, "thread", sched_tdname(td), "statclock",
5028f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, "stathz:%d", (stathz)?stathz:hz);
5037628402bSPeter Wemm 	thread_lock_flags(td, MTX_QUIET);
50440acdeabSJeff Roberson 	sched_clock(td);
50540acdeabSJeff Roberson 	thread_unlock(td);
5066caa8a15SJohn Baldwin }
5076c567274SJohn Baldwin 
5086caa8a15SJohn Baldwin void
509b439e431SJohn Baldwin profclock(int usermode, uintfptr_t pc)
5106caa8a15SJohn Baldwin {
511238dd320SJake Burkholder 	struct thread *td;
512238dd320SJake Burkholder #ifdef GPROF
513238dd320SJake Burkholder 	struct gmonparam *g;
5145c8b4441SJohn Baldwin 	uintfptr_t i;
515238dd320SJake Burkholder #endif
5166caa8a15SJohn Baldwin 
5174a338afdSJulian Elischer 	td = curthread;
518b439e431SJohn Baldwin 	if (usermode) {
519238dd320SJake Burkholder 		/*
520238dd320SJake Burkholder 		 * Came from user mode; CPU was in user state.
521238dd320SJake Burkholder 		 * If this process is being profiled, record the tick.
522a282253aSJulian Elischer 		 * if there is no related user location yet, don't
523a282253aSJulian Elischer 		 * bother trying to count it.
524238dd320SJake Burkholder 		 */
5259752f794SJohn Baldwin 		if (td->td_proc->p_flag & P_PROFIL)
526b439e431SJohn Baldwin 			addupc_intr(td, pc, 1);
527238dd320SJake Burkholder 	}
528238dd320SJake Burkholder #ifdef GPROF
529238dd320SJake Burkholder 	else {
530238dd320SJake Burkholder 		/*
531238dd320SJake Burkholder 		 * Kernel statistics are just like addupc_intr, only easier.
532238dd320SJake Burkholder 		 */
533238dd320SJake Burkholder 		g = &_gmonparam;
534b439e431SJohn Baldwin 		if (g->state == GMON_PROF_ON && pc >= g->lowpc) {
535b439e431SJohn Baldwin 			i = PC_TO_I(g, pc);
536238dd320SJake Burkholder 			if (i < g->textsize) {
537b439e431SJohn Baldwin 				KCOUNT(g, i)++;
538238dd320SJake Burkholder 			}
539238dd320SJake Burkholder 		}
540238dd320SJake Burkholder 	}
541238dd320SJake Burkholder #endif
542df8bae1dSRodney W. Grimes }
543df8bae1dSRodney W. Grimes 
544df8bae1dSRodney W. Grimes /*
545df8bae1dSRodney W. Grimes  * Return information about system clocks.
546df8bae1dSRodney W. Grimes  */
547787d58f2SPoul-Henning Kamp static int
54882d9ae4eSPoul-Henning Kamp sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
549df8bae1dSRodney W. Grimes {
550df8bae1dSRodney W. Grimes 	struct clockinfo clkinfo;
551df8bae1dSRodney W. Grimes 	/*
552df8bae1dSRodney W. Grimes 	 * Construct clockinfo structure.
553df8bae1dSRodney W. Grimes 	 */
554a9a0f15aSBruce Evans 	bzero(&clkinfo, sizeof(clkinfo));
555df8bae1dSRodney W. Grimes 	clkinfo.hz = hz;
556df8bae1dSRodney W. Grimes 	clkinfo.tick = tick;
557df8bae1dSRodney W. Grimes 	clkinfo.profhz = profhz;
558df8bae1dSRodney W. Grimes 	clkinfo.stathz = stathz ? stathz : hz;
559ae0eb976SPoul-Henning Kamp 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
560df8bae1dSRodney W. Grimes }
5613f31c649SGarrett Wollman 
562946bb7a2SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
563af1408e3SLuigi Rizzo 	0, 0, sysctl_kern_clockrate, "S,clockinfo",
564af1408e3SLuigi Rizzo 	"Rate and period of various kernel clocks");
565370c3cb5SSean Kelly 
5664103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG
5674103b765SPoul-Henning Kamp 
5684103b765SPoul-Henning Kamp static void
5699079fff5SNick Hibma watchdog_config(void *unused __unused, u_int cmd, int *error)
570370c3cb5SSean Kelly {
5714103b765SPoul-Henning Kamp 	u_int u;
572370c3cb5SSean Kelly 
5734103b765SPoul-Henning Kamp 	u = cmd & WD_INTERVAL;
5749079fff5SNick Hibma 	if (u >= WD_TO_1SEC) {
5754103b765SPoul-Henning Kamp 		watchdog_ticks = (1 << (u - WD_TO_1SEC)) * hz;
5764103b765SPoul-Henning Kamp 		watchdog_enabled = 1;
5779079fff5SNick Hibma 		*error = 0;
5784103b765SPoul-Henning Kamp 	} else {
5794103b765SPoul-Henning Kamp 		watchdog_enabled = 0;
580370c3cb5SSean Kelly 	}
5814103b765SPoul-Henning Kamp }
582370c3cb5SSean Kelly 
583370c3cb5SSean Kelly /*
584370c3cb5SSean Kelly  * Handle a watchdog timeout by dumping interrupt information and
585911d16b8SEd Maste  * then either dropping to DDB or panicking.
586370c3cb5SSean Kelly  */
587370c3cb5SSean Kelly static void
588370c3cb5SSean Kelly watchdog_fire(void)
589370c3cb5SSean Kelly {
590370c3cb5SSean Kelly 	int nintr;
591370c3cb5SSean Kelly 	u_int64_t inttotal;
592370c3cb5SSean Kelly 	u_long *curintr;
593370c3cb5SSean Kelly 	char *curname;
594370c3cb5SSean Kelly 
595370c3cb5SSean Kelly 	curintr = intrcnt;
596370c3cb5SSean Kelly 	curname = intrnames;
597370c3cb5SSean Kelly 	inttotal = 0;
598370c3cb5SSean Kelly 	nintr = eintrcnt - intrcnt;
599370c3cb5SSean Kelly 
600370c3cb5SSean Kelly 	printf("interrupt                   total\n");
601370c3cb5SSean Kelly 	while (--nintr >= 0) {
602370c3cb5SSean Kelly 		if (*curintr)
603370c3cb5SSean Kelly 			printf("%-12s %20lu\n", curname, *curintr);
604370c3cb5SSean Kelly 		curname += strlen(curname) + 1;
605370c3cb5SSean Kelly 		inttotal += *curintr++;
606370c3cb5SSean Kelly 	}
6076cda4155SSean Kelly 	printf("Total        %20ju\n", (uintmax_t)inttotal);
608911d16b8SEd Maste 
609911d16b8SEd Maste #if defined(KDB) && !defined(KDB_UNATTENDED)
610911d16b8SEd Maste 	kdb_backtrace();
6113de213ccSRobert Watson 	kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout");
612911d16b8SEd Maste #else
613370c3cb5SSean Kelly 	panic("watchdog timeout");
614911d16b8SEd Maste #endif
615370c3cb5SSean Kelly }
616370c3cb5SSean Kelly 
6174103b765SPoul-Henning Kamp #endif /* SW_WATCHDOG */
618