xref: /freebsd/sys/kern/kern_clock.c (revision 35a33d14b593ba93feb8fed8d0689bc8e0edd15b)
1df8bae1dSRodney W. Grimes /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
7df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
8df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
9df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
2069a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39677b542eSDavid E. O'Brien #include <sys/cdefs.h>
40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
41677b542eSDavid E. O'Brien 
42911d16b8SEd Maste #include "opt_kdb.h"
43f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
444da0d332SPeter Wemm #include "opt_hwpmc_hooks.h"
4532c20357SPoul-Henning Kamp #include "opt_ntp.h"
46370c3cb5SSean Kelly #include "opt_watchdog.h"
4732c20357SPoul-Henning Kamp 
48df8bae1dSRodney W. Grimes #include <sys/param.h>
49df8bae1dSRodney W. Grimes #include <sys/systm.h>
50df8bae1dSRodney W. Grimes #include <sys/callout.h>
51c4d901e9SMatt Macy #include <sys/epoch.h>
52e2e050c8SConrad Meyer #include <sys/eventhandler.h>
53a5f10424SMatt Macy #include <sys/gtaskqueue.h>
542d50560aSMarcel Moolenaar #include <sys/kdb.h>
55df8bae1dSRodney W. Grimes #include <sys/kernel.h>
56f7829d0dSAttilio Rao #include <sys/kthread.h>
5761d80e90SJohn Baldwin #include <sys/ktr.h>
58f7829d0dSAttilio Rao #include <sys/lock.h>
5935e0e5b3SJohn Baldwin #include <sys/mutex.h>
60df8bae1dSRodney W. Grimes #include <sys/proc.h>
61e4625663SJeff Roberson #include <sys/resource.h>
62df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
63b43179fbSJeff Roberson #include <sys/sched.h>
64b3e9e682SRyan Stone #include <sys/sdt.h>
65797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
66f7829d0dSAttilio Rao #include <sys/sleepqueue.h>
676caa8a15SJohn Baldwin #include <sys/smp.h>
688a129caeSDavid Greenman #include <vm/vm.h>
69efeaf95aSDavid Greenman #include <vm/pmap.h>
70efeaf95aSDavid Greenman #include <vm/vm_map.h>
71797f2d22SPoul-Henning Kamp #include <sys/sysctl.h>
728088699fSJohn Baldwin #include <sys/bus.h>
738088699fSJohn Baldwin #include <sys/interrupt.h>
74104a9b7eSAlexander Kabaev #include <sys/limits.h>
75e7fa55afSPoul-Henning Kamp #include <sys/timetc.h>
76df8bae1dSRodney W. Grimes 
77df8bae1dSRodney W. Grimes #ifdef GPROF
78df8bae1dSRodney W. Grimes #include <sys/gmon.h>
79df8bae1dSRodney W. Grimes #endif
80df8bae1dSRodney W. Grimes 
8136c0fd9dSJoseph Koshy #ifdef HWPMC_HOOKS
8236c0fd9dSJoseph Koshy #include <sys/pmckern.h>
83f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , clock, hard);
84f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , clock, stat);
85d49302aeSFabien Thomas PMC_SOFT_DEFINE_EX( , , clock, prof, \
86d49302aeSFabien Thomas     cpu_startprofclock, cpu_stopprofclock);
8736c0fd9dSJoseph Koshy #endif
8836c0fd9dSJoseph Koshy 
89e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
90e4fc250cSLuigi Rizzo extern void hardclock_device_poll(void);
91e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
92eae8fc2cSSteve Passe 
938b98fec9SJeff Roberson /* Spin-lock protecting profiling statistics. */
9486a49deaSAttilio Rao static struct mtx time_lock;
958b98fec9SJeff Roberson 
96b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(sched);
97d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , tick, "struct thread *", "struct proc *");
98b3e9e682SRyan Stone 
9962919d78SPeter Wemm static int
10062919d78SPeter Wemm sysctl_kern_cp_time(SYSCTL_HANDLER_ARGS)
10162919d78SPeter Wemm {
10262919d78SPeter Wemm 	int error;
1037628402bSPeter Wemm 	long cp_time[CPUSTATES];
104cff2e749SPaul Saab #ifdef SCTL_MASK32
10562919d78SPeter Wemm 	int i;
10662919d78SPeter Wemm 	unsigned int cp_time32[CPUSTATES];
1077628402bSPeter Wemm #endif
10862919d78SPeter Wemm 
1097628402bSPeter Wemm 	read_cpu_time(cp_time);
1107628402bSPeter Wemm #ifdef SCTL_MASK32
111cff2e749SPaul Saab 	if (req->flags & SCTL_MASK32) {
11262919d78SPeter Wemm 		if (!req->oldptr)
11362919d78SPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(cp_time32));
11462919d78SPeter Wemm 		for (i = 0; i < CPUSTATES; i++)
11562919d78SPeter Wemm 			cp_time32[i] = (unsigned int)cp_time[i];
11662919d78SPeter Wemm 		error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
11762919d78SPeter Wemm 	} else
11862919d78SPeter Wemm #endif
11962919d78SPeter Wemm 	{
12062919d78SPeter Wemm 		if (!req->oldptr)
12162919d78SPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(cp_time));
12262919d78SPeter Wemm 		error = SYSCTL_OUT(req, cp_time, sizeof(cp_time));
12362919d78SPeter Wemm 	}
12462919d78SPeter Wemm 	return error;
12562919d78SPeter Wemm }
12662919d78SPeter Wemm 
127c383c221SEd Schouten SYSCTL_PROC(_kern, OID_AUTO, cp_time, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE,
12862919d78SPeter Wemm     0,0, sysctl_kern_cp_time, "LU", "CPU time statistics");
1297f112b04SRobert Watson 
1307628402bSPeter Wemm static long empty[CPUSTATES];
1317628402bSPeter Wemm 
1327628402bSPeter Wemm static int
1337628402bSPeter Wemm sysctl_kern_cp_times(SYSCTL_HANDLER_ARGS)
1347628402bSPeter Wemm {
1357628402bSPeter Wemm 	struct pcpu *pcpu;
1367628402bSPeter Wemm 	int error;
137ef54068bSRobert Watson 	int c;
1387628402bSPeter Wemm 	long *cp_time;
1397628402bSPeter Wemm #ifdef SCTL_MASK32
1407628402bSPeter Wemm 	unsigned int cp_time32[CPUSTATES];
141ef54068bSRobert Watson 	int i;
1427628402bSPeter Wemm #endif
1437628402bSPeter Wemm 
1447628402bSPeter Wemm 	if (!req->oldptr) {
1457628402bSPeter Wemm #ifdef SCTL_MASK32
1467628402bSPeter Wemm 		if (req->flags & SCTL_MASK32)
1477628402bSPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(cp_time32) * (mp_maxid + 1));
1487628402bSPeter Wemm 		else
1497628402bSPeter Wemm #endif
1507628402bSPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(long) * CPUSTATES * (mp_maxid + 1));
1517628402bSPeter Wemm 	}
1527628402bSPeter Wemm 	for (error = 0, c = 0; error == 0 && c <= mp_maxid; c++) {
1537628402bSPeter Wemm 		if (!CPU_ABSENT(c)) {
1547628402bSPeter Wemm 			pcpu = pcpu_find(c);
1557628402bSPeter Wemm 			cp_time = pcpu->pc_cp_time;
1567628402bSPeter Wemm 		} else {
1577628402bSPeter Wemm 			cp_time = empty;
1587628402bSPeter Wemm 		}
1597628402bSPeter Wemm #ifdef SCTL_MASK32
1607628402bSPeter Wemm 		if (req->flags & SCTL_MASK32) {
1617628402bSPeter Wemm 			for (i = 0; i < CPUSTATES; i++)
1627628402bSPeter Wemm 				cp_time32[i] = (unsigned int)cp_time[i];
1637628402bSPeter Wemm 			error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
1647628402bSPeter Wemm 		} else
1657628402bSPeter Wemm #endif
1667628402bSPeter Wemm 			error = SYSCTL_OUT(req, cp_time, sizeof(long) * CPUSTATES);
1677628402bSPeter Wemm 	}
1687628402bSPeter Wemm 	return error;
1697628402bSPeter Wemm }
1707628402bSPeter Wemm 
171c383c221SEd Schouten SYSCTL_PROC(_kern, OID_AUTO, cp_times, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE,
1727628402bSPeter Wemm     0,0, sysctl_kern_cp_times, "LU", "per-CPU time statistics");
1737628402bSPeter Wemm 
174f7829d0dSAttilio Rao #ifdef DEADLKRES
17536e51f65SAttilio Rao static const char *blessed[] = {
17695335fd8SAttilio Rao 	"getblk",
17736e51f65SAttilio Rao 	"so_snd_sx",
17836e51f65SAttilio Rao 	"so_rcv_sx",
17936e51f65SAttilio Rao 	NULL
18036e51f65SAttilio Rao };
181f7829d0dSAttilio Rao static int slptime_threshold = 1800;
182f7829d0dSAttilio Rao static int blktime_threshold = 900;
183f7829d0dSAttilio Rao static int sleepfreq = 3;
184f7829d0dSAttilio Rao 
185f7829d0dSAttilio Rao static void
1861534cd19SBjoern A. Zeeb deadlres_td_on_lock(struct proc *p, struct thread *td, int blkticks)
1871534cd19SBjoern A. Zeeb {
1881534cd19SBjoern A. Zeeb 	int tticks;
1891534cd19SBjoern A. Zeeb 
1901534cd19SBjoern A. Zeeb 	sx_assert(&allproc_lock, SX_LOCKED);
1911534cd19SBjoern A. Zeeb 	PROC_LOCK_ASSERT(p, MA_OWNED);
1921534cd19SBjoern A. Zeeb 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1931534cd19SBjoern A. Zeeb 	/*
1941534cd19SBjoern A. Zeeb 	 * The thread should be blocked on a turnstile, simply check
1951534cd19SBjoern A. Zeeb 	 * if the turnstile channel is in good state.
1961534cd19SBjoern A. Zeeb 	 */
1971534cd19SBjoern A. Zeeb 	MPASS(td->td_blocked != NULL);
1981534cd19SBjoern A. Zeeb 
1991534cd19SBjoern A. Zeeb 	tticks = ticks - td->td_blktick;
2001534cd19SBjoern A. Zeeb 	if (tticks > blkticks)
2011534cd19SBjoern A. Zeeb 		/*
2021534cd19SBjoern A. Zeeb 		 * Accordingly with provided thresholds, this thread is stuck
2031534cd19SBjoern A. Zeeb 		 * for too long on a turnstile.
2041534cd19SBjoern A. Zeeb 		 */
2058abaf6a7SJason A. Harmening 		panic("%s: possible deadlock detected for %p (%s), "
2068abaf6a7SJason A. Harmening 		    "blocked for %d ticks\n", __func__,
2078abaf6a7SJason A. Harmening 		    td, sched_tdname(td), tticks);
2081534cd19SBjoern A. Zeeb }
2091534cd19SBjoern A. Zeeb 
2101534cd19SBjoern A. Zeeb static void
2111534cd19SBjoern A. Zeeb deadlres_td_sleep_q(struct proc *p, struct thread *td, int slpticks)
2121534cd19SBjoern A. Zeeb {
213fea73412SConrad Meyer 	const void *wchan;
2141534cd19SBjoern A. Zeeb 	int i, slptype, tticks;
2151534cd19SBjoern A. Zeeb 
2161534cd19SBjoern A. Zeeb 	sx_assert(&allproc_lock, SX_LOCKED);
2171534cd19SBjoern A. Zeeb 	PROC_LOCK_ASSERT(p, MA_OWNED);
2181534cd19SBjoern A. Zeeb 	THREAD_LOCK_ASSERT(td, MA_OWNED);
2191534cd19SBjoern A. Zeeb 	/*
2201534cd19SBjoern A. Zeeb 	 * Check if the thread is sleeping on a lock, otherwise skip the check.
2211534cd19SBjoern A. Zeeb 	 * Drop the thread lock in order to avoid a LOR with the sleepqueue
2221534cd19SBjoern A. Zeeb 	 * spinlock.
2231534cd19SBjoern A. Zeeb 	 */
2241534cd19SBjoern A. Zeeb 	wchan = td->td_wchan;
2251534cd19SBjoern A. Zeeb 	tticks = ticks - td->td_slptick;
2261534cd19SBjoern A. Zeeb 	slptype = sleepq_type(wchan);
2271534cd19SBjoern A. Zeeb 	if ((slptype == SLEEPQ_SX || slptype == SLEEPQ_LK) &&
2281534cd19SBjoern A. Zeeb 	    tticks > slpticks) {
2291534cd19SBjoern A. Zeeb 		/*
2301534cd19SBjoern A. Zeeb 		 * Accordingly with provided thresholds, this thread is stuck
2311534cd19SBjoern A. Zeeb 		 * for too long on a sleepqueue.
2321534cd19SBjoern A. Zeeb 		 * However, being on a sleepqueue, we might still check for the
2331534cd19SBjoern A. Zeeb 		 * blessed list.
2341534cd19SBjoern A. Zeeb 		 */
2351534cd19SBjoern A. Zeeb 		for (i = 0; blessed[i] != NULL; i++)
2361534cd19SBjoern A. Zeeb 			if (!strcmp(blessed[i], td->td_wmesg))
2371534cd19SBjoern A. Zeeb 				return;
2381534cd19SBjoern A. Zeeb 
2398abaf6a7SJason A. Harmening 		panic("%s: possible deadlock detected for %p (%s), "
2408abaf6a7SJason A. Harmening 		    "blocked for %d ticks\n", __func__,
2418abaf6a7SJason A. Harmening 		    td, sched_tdname(td), tticks);
2421534cd19SBjoern A. Zeeb 	}
2431534cd19SBjoern A. Zeeb }
2441534cd19SBjoern A. Zeeb 
2451534cd19SBjoern A. Zeeb static void
246f7829d0dSAttilio Rao deadlkres(void)
247f7829d0dSAttilio Rao {
248f7829d0dSAttilio Rao 	struct proc *p;
249f7829d0dSAttilio Rao 	struct thread *td;
2501534cd19SBjoern A. Zeeb 	int blkticks, slpticks, tryl;
251f7829d0dSAttilio Rao 
252f7829d0dSAttilio Rao 	tryl = 0;
253f7829d0dSAttilio Rao 	for (;;) {
254f7829d0dSAttilio Rao 		blkticks = blktime_threshold * hz;
255f7829d0dSAttilio Rao 		slpticks = slptime_threshold * hz;
256f7829d0dSAttilio Rao 
257f7829d0dSAttilio Rao 		/*
2581534cd19SBjoern A. Zeeb 		 * Avoid to sleep on the sx_lock in order to avoid a
2591534cd19SBjoern A. Zeeb 		 * possible priority inversion problem leading to
2601534cd19SBjoern A. Zeeb 		 * starvation.
261f7829d0dSAttilio Rao 		 * If the lock can't be held after 100 tries, panic.
262f7829d0dSAttilio Rao 		 */
263f7829d0dSAttilio Rao 		if (!sx_try_slock(&allproc_lock)) {
264f7829d0dSAttilio Rao 			if (tryl > 100)
2651534cd19SBjoern A. Zeeb 				panic("%s: possible deadlock detected "
2661534cd19SBjoern A. Zeeb 				    "on allproc_lock\n", __func__);
267f7829d0dSAttilio Rao 			tryl++;
268b5850804SJohn Baldwin 			pause("allproc", sleepfreq * hz);
269f7829d0dSAttilio Rao 			continue;
270f7829d0dSAttilio Rao 		}
271f7829d0dSAttilio Rao 		tryl = 0;
272f7829d0dSAttilio Rao 		FOREACH_PROC_IN_SYSTEM(p) {
273f7829d0dSAttilio Rao 			PROC_LOCK(p);
274e806d352SJohn Baldwin 			if (p->p_state == PRS_NEW) {
275e806d352SJohn Baldwin 				PROC_UNLOCK(p);
276e806d352SJohn Baldwin 				continue;
277e806d352SJohn Baldwin 			}
278f7829d0dSAttilio Rao 			FOREACH_THREAD_IN_PROC(p, td) {
279f7829d0dSAttilio Rao 				thread_lock(td);
2801534cd19SBjoern A. Zeeb 				if (TD_ON_LOCK(td))
2811534cd19SBjoern A. Zeeb 					deadlres_td_on_lock(p, td,
2821534cd19SBjoern A. Zeeb 					    blkticks);
28361a74c5cSJeff Roberson 				else if (TD_IS_SLEEPING(td))
2841534cd19SBjoern A. Zeeb 					deadlres_td_sleep_q(p, td,
2851534cd19SBjoern A. Zeeb 					    slpticks);
286f7829d0dSAttilio Rao 				thread_unlock(td);
287f7829d0dSAttilio Rao 			}
288f7829d0dSAttilio Rao 			PROC_UNLOCK(p);
289f7829d0dSAttilio Rao 		}
290f7829d0dSAttilio Rao 		sx_sunlock(&allproc_lock);
291f7829d0dSAttilio Rao 
292f7829d0dSAttilio Rao 		/* Sleep for sleepfreq seconds. */
293b5850804SJohn Baldwin 		pause("-", sleepfreq * hz);
294f7829d0dSAttilio Rao 	}
295f7829d0dSAttilio Rao }
296f7829d0dSAttilio Rao 
297f7829d0dSAttilio Rao static struct kthread_desc deadlkres_kd = {
298f7829d0dSAttilio Rao 	"deadlkres",
299f7829d0dSAttilio Rao 	deadlkres,
300f7829d0dSAttilio Rao 	(struct thread **)NULL
301f7829d0dSAttilio Rao };
302f7829d0dSAttilio Rao 
303f7829d0dSAttilio Rao SYSINIT(deadlkres, SI_SUB_CLOCKS, SI_ORDER_ANY, kthread_start, &deadlkres_kd);
304f7829d0dSAttilio Rao 
3057029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, deadlkres, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3066472ac3dSEd Schouten     "Deadlock resolver");
307f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, slptime_threshold, CTLFLAG_RW,
308f7829d0dSAttilio Rao     &slptime_threshold, 0,
309f7829d0dSAttilio Rao     "Number of seconds within is valid to sleep on a sleepqueue");
310f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, blktime_threshold, CTLFLAG_RW,
311f7829d0dSAttilio Rao     &blktime_threshold, 0,
312f7829d0dSAttilio Rao     "Number of seconds within is valid to block on a turnstile");
313f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, sleepfreq, CTLFLAG_RW, &sleepfreq, 0,
314f7829d0dSAttilio Rao     "Number of seconds between any deadlock resolver thread run");
315f7829d0dSAttilio Rao #endif	/* DEADLKRES */
316f7829d0dSAttilio Rao 
3177628402bSPeter Wemm void
3187628402bSPeter Wemm read_cpu_time(long *cp_time)
3197628402bSPeter Wemm {
3207628402bSPeter Wemm 	struct pcpu *pc;
3217628402bSPeter Wemm 	int i, j;
3227628402bSPeter Wemm 
3237628402bSPeter Wemm 	/* Sum up global cp_time[]. */
3247628402bSPeter Wemm 	bzero(cp_time, sizeof(long) * CPUSTATES);
3253aa6d94eSJohn Baldwin 	CPU_FOREACH(i) {
3267628402bSPeter Wemm 		pc = pcpu_find(i);
3277628402bSPeter Wemm 		for (j = 0; j < CPUSTATES; j++)
3287628402bSPeter Wemm 			cp_time[j] += pc->pc_cp_time[j];
3297628402bSPeter Wemm 	}
3307628402bSPeter Wemm }
3317628402bSPeter Wemm 
3324103b765SPoul-Henning Kamp #include <sys/watchdog.h>
333370c3cb5SSean Kelly 
3344103b765SPoul-Henning Kamp static int watchdog_ticks;
335370c3cb5SSean Kelly static int watchdog_enabled;
3364103b765SPoul-Henning Kamp static void watchdog_fire(void);
3374103b765SPoul-Henning Kamp static void watchdog_config(void *, u_int, int *);
338d626b50bSMike Karels 
339d626b50bSMike Karels static void
340d626b50bSMike Karels watchdog_attach(void)
341d626b50bSMike Karels {
342d626b50bSMike Karels 	EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0);
343d626b50bSMike Karels }
344370c3cb5SSean Kelly 
3453bac064fSPoul-Henning Kamp /*
346df8bae1dSRodney W. Grimes  * Clock handling routines.
347df8bae1dSRodney W. Grimes  *
348b05dcf3cSPoul-Henning Kamp  * This code is written to operate with two timers that run independently of
349b05dcf3cSPoul-Henning Kamp  * each other.
3507ec73f64SPoul-Henning Kamp  *
351b05dcf3cSPoul-Henning Kamp  * The main timer, running hz times per second, is used to trigger interval
352b05dcf3cSPoul-Henning Kamp  * timers, timeouts and rescheduling as needed.
3537ec73f64SPoul-Henning Kamp  *
354b05dcf3cSPoul-Henning Kamp  * The second timer handles kernel and user profiling,
355b05dcf3cSPoul-Henning Kamp  * and does resource use estimation.  If the second timer is programmable,
356b05dcf3cSPoul-Henning Kamp  * it is randomized to avoid aliasing between the two clocks.  For example,
357b05dcf3cSPoul-Henning Kamp  * the randomization prevents an adversary from always giving up the cpu
358df8bae1dSRodney W. Grimes  * just before its quantum expires.  Otherwise, it would never accumulate
359df8bae1dSRodney W. Grimes  * cpu ticks.  The mean frequency of the second timer is stathz.
360b05dcf3cSPoul-Henning Kamp  *
361b05dcf3cSPoul-Henning Kamp  * If no second timer exists, stathz will be zero; in this case we drive
362b05dcf3cSPoul-Henning Kamp  * profiling and statistics off the main clock.  This WILL NOT be accurate;
363b05dcf3cSPoul-Henning Kamp  * do not do it unless absolutely necessary.
364b05dcf3cSPoul-Henning Kamp  *
365df8bae1dSRodney W. Grimes  * The statistics clock may (or may not) be run at a higher rate while
366b05dcf3cSPoul-Henning Kamp  * profiling.  This profile clock runs at profhz.  We require that profhz
367b05dcf3cSPoul-Henning Kamp  * be an integral multiple of stathz.
368b05dcf3cSPoul-Henning Kamp  *
369b05dcf3cSPoul-Henning Kamp  * If the statistics clock is running fast, it must be divided by the ratio
370b05dcf3cSPoul-Henning Kamp  * profhz/stathz for statistics.  (For profiling, every tick counts.)
371df8bae1dSRodney W. Grimes  *
3727ec73f64SPoul-Henning Kamp  * Time-of-day is maintained using a "timecounter", which may or may
3737ec73f64SPoul-Henning Kamp  * not be related to the hardware generating the above mentioned
3747ec73f64SPoul-Henning Kamp  * interrupts.
375df8bae1dSRodney W. Grimes  */
376df8bae1dSRodney W. Grimes 
377df8bae1dSRodney W. Grimes int	stathz;
378df8bae1dSRodney W. Grimes int	profhz;
379238dd320SJake Burkholder int	profprocs;
380a8df530dSJohn Baldwin volatile int	ticks;
381238dd320SJake Burkholder int	psratio;
382df8bae1dSRodney W. Grimes 
3832bf95012SAndrew Turner DPCPU_DEFINE_STATIC(int, pcputicks);	/* Per-CPU version of ticks. */
3845760b029SKonstantin Belousov #ifdef DEVICE_POLLING
3855760b029SKonstantin Belousov static int devpoll_run = 0;
3865760b029SKonstantin Belousov #endif
387dbd55f3fSAlexander Motin 
388c6d31b83SKonstantin Belousov static void
389c6d31b83SKonstantin Belousov ast_oweupc(struct thread *td, int tda __unused)
390c6d31b83SKonstantin Belousov {
391c6d31b83SKonstantin Belousov 	if ((td->td_proc->p_flag & P_PROFIL) == 0)
392c6d31b83SKonstantin Belousov 		return;
393c6d31b83SKonstantin Belousov 	addupc_task(td, td->td_profil_addr, td->td_profil_ticks);
394c6d31b83SKonstantin Belousov 	td->td_profil_ticks = 0;
395c6d31b83SKonstantin Belousov 	td->td_pflags &= ~TDP_OWEUPC;
396c6d31b83SKonstantin Belousov }
397c6d31b83SKonstantin Belousov 
398c6d31b83SKonstantin Belousov static void
399c6d31b83SKonstantin Belousov ast_alrm(struct thread *td, int tda __unused)
400c6d31b83SKonstantin Belousov {
401c6d31b83SKonstantin Belousov 	struct proc *p;
402c6d31b83SKonstantin Belousov 
403c6d31b83SKonstantin Belousov 	p = td->td_proc;
404c6d31b83SKonstantin Belousov 	PROC_LOCK(p);
405c6d31b83SKonstantin Belousov 	kern_psignal(p, SIGVTALRM);
406c6d31b83SKonstantin Belousov 	PROC_UNLOCK(p);
407c6d31b83SKonstantin Belousov }
408c6d31b83SKonstantin Belousov 
409c6d31b83SKonstantin Belousov static void
410c6d31b83SKonstantin Belousov ast_prof(struct thread *td, int tda __unused)
411c6d31b83SKonstantin Belousov {
412c6d31b83SKonstantin Belousov 	struct proc *p;
413c6d31b83SKonstantin Belousov 
414c6d31b83SKonstantin Belousov 	p = td->td_proc;
415c6d31b83SKonstantin Belousov 	PROC_LOCK(p);
416c6d31b83SKonstantin Belousov 	kern_psignal(p, SIGPROF);
417c6d31b83SKonstantin Belousov 	PROC_UNLOCK(p);
418c6d31b83SKonstantin Belousov }
419c6d31b83SKonstantin Belousov 
420df8bae1dSRodney W. Grimes /*
421df8bae1dSRodney W. Grimes  * Initialize clock frequencies and start both clocks running.
422df8bae1dSRodney W. Grimes  */
4232b14f991SJulian Elischer static void
4242287ced2SMark Johnston initclocks(void *dummy __unused)
425df8bae1dSRodney W. Grimes {
4263e85b721SEd Maste 	int i;
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes 	/*
429df8bae1dSRodney W. Grimes 	 * Set divisors to 1 (normal case) and let the machine-specific
430df8bae1dSRodney W. Grimes 	 * code do its bit.
431df8bae1dSRodney W. Grimes 	 */
432875b8844SAlexander Motin 	mtx_init(&time_lock, "time lock", NULL, MTX_DEF);
43363d69d25SRobert Watson 	cpu_initclocks();
434df8bae1dSRodney W. Grimes 
435df8bae1dSRodney W. Grimes 	/*
436df8bae1dSRodney W. Grimes 	 * Compute profhz/stathz, and fix profhz if needed.
437df8bae1dSRodney W. Grimes 	 */
438df8bae1dSRodney W. Grimes 	i = stathz ? stathz : hz;
439df8bae1dSRodney W. Grimes 	if (profhz == 0)
440df8bae1dSRodney W. Grimes 		profhz = i;
441df8bae1dSRodney W. Grimes 	psratio = profhz / i;
442d626b50bSMike Karels 
443c6d31b83SKonstantin Belousov 	ast_register(TDA_OWEUPC, ASTR_ASTF_REQUIRED, 0, ast_oweupc);
444c6d31b83SKonstantin Belousov 	ast_register(TDA_ALRM, ASTR_ASTF_REQUIRED, 0, ast_alrm);
445c6d31b83SKonstantin Belousov 	ast_register(TDA_PROF, ASTR_ASTF_REQUIRED, 0, ast_prof);
446c6d31b83SKonstantin Belousov 
4474103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG
448d626b50bSMike Karels 	/* Enable hardclock watchdog now, even if a hardware watchdog exists. */
449d626b50bSMike Karels 	watchdog_attach();
450d626b50bSMike Karels #else
451d626b50bSMike Karels 	/* Volunteer to run a software watchdog. */
452d626b50bSMike Karels 	if (wdog_software_attach == NULL)
453d626b50bSMike Karels 		wdog_software_attach = watchdog_attach;
4544103b765SPoul-Henning Kamp #endif
455df8bae1dSRodney W. Grimes }
4562287ced2SMark Johnston SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL);
457df8bae1dSRodney W. Grimes 
4583277792bSMateusz Guzik static __noinline void
4593277792bSMateusz Guzik hardclock_itimer(struct thread *td, struct pstats *pstats, int cnt, int usermode)
460a157e425SAlexander Motin {
4613277792bSMateusz Guzik 	struct proc *p;
462c6d31b83SKonstantin Belousov 	int ast;
463a157e425SAlexander Motin 
464c6d31b83SKonstantin Belousov 	ast = 0;
4653277792bSMateusz Guzik 	p = td->td_proc;
466a157e425SAlexander Motin 	if (usermode &&
467a157e425SAlexander Motin 	    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) {
4685c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
469a157e425SAlexander Motin 		if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL],
470a157e425SAlexander Motin 		    tick * cnt) == 0)
471c6d31b83SKonstantin Belousov 			ast |= TDAI(TDA_ALRM);
4725c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
473a157e425SAlexander Motin 	}
474a157e425SAlexander Motin 	if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) {
4755c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
476a157e425SAlexander Motin 		if (itimerdecr(&pstats->p_timer[ITIMER_PROF],
477a157e425SAlexander Motin 		    tick * cnt) == 0)
478c6d31b83SKonstantin Belousov 			ast |= TDAI(TDA_PROF);
4795c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
480a157e425SAlexander Motin 	}
481c6d31b83SKonstantin Belousov 	if (ast != 0)
482c6d31b83SKonstantin Belousov 		ast_sched_mask(td, ast);
4833277792bSMateusz Guzik }
4843277792bSMateusz Guzik 
4853277792bSMateusz Guzik void
4863277792bSMateusz Guzik hardclock(int cnt, int usermode)
4873277792bSMateusz Guzik {
4883277792bSMateusz Guzik 	struct pstats *pstats;
4893277792bSMateusz Guzik 	struct thread *td = curthread;
4903277792bSMateusz Guzik 	struct proc *p = td->td_proc;
4913277792bSMateusz Guzik 	int *t = DPCPU_PTR(pcputicks);
4923277792bSMateusz Guzik 	int global, i, newticks;
4933277792bSMateusz Guzik 
4943277792bSMateusz Guzik 	/*
4953277792bSMateusz Guzik 	 * Update per-CPU and possibly global ticks values.
4963277792bSMateusz Guzik 	 */
4973277792bSMateusz Guzik 	*t += cnt;
4983277792bSMateusz Guzik 	global = ticks;
4993277792bSMateusz Guzik 	do {
5003277792bSMateusz Guzik 		newticks = *t - global;
5013277792bSMateusz Guzik 		if (newticks <= 0) {
5023277792bSMateusz Guzik 			if (newticks < -1)
5033277792bSMateusz Guzik 				*t = global - 1;
5043277792bSMateusz Guzik 			newticks = 0;
5053277792bSMateusz Guzik 			break;
5063277792bSMateusz Guzik 		}
5073277792bSMateusz Guzik 	} while (!atomic_fcmpset_int(&ticks, &global, *t));
5083277792bSMateusz Guzik 
5093277792bSMateusz Guzik 	/*
5103277792bSMateusz Guzik 	 * Run current process's virtual and profile time, as needed.
5113277792bSMateusz Guzik 	 */
5123277792bSMateusz Guzik 	pstats = p->p_stats;
5133277792bSMateusz Guzik 	if (__predict_false(
5143277792bSMateusz Guzik 	    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) ||
5153277792bSMateusz Guzik 	    timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)))
5163277792bSMateusz Guzik 		hardclock_itimer(td, pstats, cnt, usermode);
517a157e425SAlexander Motin 
518a157e425SAlexander Motin #ifdef	HWPMC_HOOKS
519a157e425SAlexander Motin 	if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
520a157e425SAlexander Motin 		PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
521f5f9340bSFabien Thomas 	if (td->td_intr_frame != NULL)
522f5f9340bSFabien Thomas 		PMC_SOFT_CALL_TF( , , clock, hard, td->td_intr_frame);
523a157e425SAlexander Motin #endif
524a157e425SAlexander Motin 	/* We are in charge to handle this tick duty. */
525a157e425SAlexander Motin 	if (newticks > 0) {
5260e189873SAlexander Motin 		tc_ticktock(newticks);
527a157e425SAlexander Motin #ifdef DEVICE_POLLING
5285760b029SKonstantin Belousov 		/* Dangerous and no need to call these things concurrently. */
5295760b029SKonstantin Belousov 		if (atomic_cmpset_acq_int(&devpoll_run, 0, 1)) {
5304763a8b8SAlexander Motin 			/* This is very short and quick. */
5314763a8b8SAlexander Motin 			hardclock_device_poll();
5325760b029SKonstantin Belousov 			atomic_store_rel_int(&devpoll_run, 0);
5334763a8b8SAlexander Motin 		}
5345760b029SKonstantin Belousov #endif /* DEVICE_POLLING */
535a157e425SAlexander Motin 		if (watchdog_enabled > 0) {
5364763a8b8SAlexander Motin 			i = atomic_fetchadd_int(&watchdog_ticks, -newticks);
5374763a8b8SAlexander Motin 			if (i > 0 && i <= newticks)
538a157e425SAlexander Motin 				watchdog_fire();
539a157e425SAlexander Motin 		}
540aba10e13SAlexander Motin 		intr_event_handle(clk_intr_event, NULL);
541a157e425SAlexander Motin 	}
542a157e425SAlexander Motin 	if (curcpu == CPU_FIRST())
543a157e425SAlexander Motin 		cpu_tick_calibration();
544a5f10424SMatt Macy 	if (__predict_false(DPCPU_GET(epoch_cb_count)))
545a5f10424SMatt Macy 		GROUPTASK_ENQUEUE(DPCPU_PTR(epoch_cb_task));
546a157e425SAlexander Motin }
547a157e425SAlexander Motin 
548a157e425SAlexander Motin void
549a157e425SAlexander Motin hardclock_sync(int cpu)
550a157e425SAlexander Motin {
551efe67753SNathan Whitehorn 	int *t;
552efe67753SNathan Whitehorn 	KASSERT(!CPU_ABSENT(cpu), ("Absent CPU %d", cpu));
553efe67753SNathan Whitehorn 	t = DPCPU_ID_PTR(cpu, pcputicks);
554a157e425SAlexander Motin 
555a157e425SAlexander Motin 	*t = ticks;
556a157e425SAlexander Motin }
557a157e425SAlexander Motin 
558df8bae1dSRodney W. Grimes /*
559*35a33d14SHans Petter Selasky  * Regular integer scaling formula without loosing precision:
560*35a33d14SHans Petter Selasky  */
561*35a33d14SHans Petter Selasky #define	TIME_INT_SCALE(value, mul, div) \
562*35a33d14SHans Petter Selasky 	(((value) / (div)) * (mul) + (((value) % (div)) * (mul)) / (div))
563*35a33d14SHans Petter Selasky 
564*35a33d14SHans Petter Selasky /*
565*35a33d14SHans Petter Selasky  * Macro for converting seconds and microseconds into actual ticks,
566*35a33d14SHans Petter Selasky  * based on the given hz value:
567*35a33d14SHans Petter Selasky  */
568*35a33d14SHans Petter Selasky #define	TIME_TO_TICKS(sec, usec, hz) \
569*35a33d14SHans Petter Selasky 	((sec) * (hz) + TIME_INT_SCALE(usec, hz, 1 << 6) / (1000000 >> 6))
570*35a33d14SHans Petter Selasky 
571*35a33d14SHans Petter Selasky #define	TIME_ASSERT_VALID_HZ(hz)	\
572*35a33d14SHans Petter Selasky 	_Static_assert(TIME_TO_TICKS(INT_MAX / (hz) - 1, 999999, hz) >= 0 && \
573*35a33d14SHans Petter Selasky 		       TIME_TO_TICKS(INT_MAX / (hz) - 1, 999999, hz) < INT_MAX,	\
574*35a33d14SHans Petter Selasky 		       "tvtohz() can overflow the regular integer type")
575*35a33d14SHans Petter Selasky 
576*35a33d14SHans Petter Selasky /*
577*35a33d14SHans Petter Selasky  * Compile time assert the maximum and minimum values to fit into a
578*35a33d14SHans Petter Selasky  * regular integer when computing TIME_TO_TICKS():
579*35a33d14SHans Petter Selasky  */
580*35a33d14SHans Petter Selasky TIME_ASSERT_VALID_HZ(HZ_MAXIMUM);
581*35a33d14SHans Petter Selasky TIME_ASSERT_VALID_HZ(HZ_MINIMUM);
582*35a33d14SHans Petter Selasky 
583*35a33d14SHans Petter Selasky /*
584*35a33d14SHans Petter Selasky  * The forumla is mostly linear, but test some more common values just
585*35a33d14SHans Petter Selasky  * in case:
586*35a33d14SHans Petter Selasky  */
587*35a33d14SHans Petter Selasky TIME_ASSERT_VALID_HZ(1024);
588*35a33d14SHans Petter Selasky TIME_ASSERT_VALID_HZ(1000);
589*35a33d14SHans Petter Selasky TIME_ASSERT_VALID_HZ(128);
590*35a33d14SHans Petter Selasky TIME_ASSERT_VALID_HZ(100);
591*35a33d14SHans Petter Selasky 
592*35a33d14SHans Petter Selasky /*
593*35a33d14SHans Petter Selasky  * Compute number of ticks representing the specified amount of time.
594*35a33d14SHans Petter Selasky  * If the specified time is negative, a value of 1 is returned. This
595*35a33d14SHans Petter Selasky  * function returns a value from 1 up to and including INT_MAX.
596df8bae1dSRodney W. Grimes  */
597df8bae1dSRodney W. Grimes int
5983e85b721SEd Maste tvtohz(struct timeval *tv)
599df8bae1dSRodney W. Grimes {
600*35a33d14SHans Petter Selasky 	int retval;
601df8bae1dSRodney W. Grimes 
602df8bae1dSRodney W. Grimes 	/*
603*35a33d14SHans Petter Selasky 	 * The values passed here may come from user-space and these
604*35a33d14SHans Petter Selasky 	 * checks ensure "tv_usec" is within its allowed range:
605df8bae1dSRodney W. Grimes 	 */
606*35a33d14SHans Petter Selasky 
607*35a33d14SHans Petter Selasky 	/* check for tv_usec underflow */
608*35a33d14SHans Petter Selasky 	if (__predict_false(tv->tv_usec < 0)) {
609*35a33d14SHans Petter Selasky 		tv->tv_sec += tv->tv_usec / 1000000;
610*35a33d14SHans Petter Selasky 		tv->tv_usec = tv->tv_usec % 1000000;
611*35a33d14SHans Petter Selasky 		/* convert tv_usec to a positive value */
612*35a33d14SHans Petter Selasky 		if (__predict_true(tv->tv_usec < 0)) {
613*35a33d14SHans Petter Selasky 			tv->tv_usec += 1000000;
614*35a33d14SHans Petter Selasky 			tv->tv_sec -= 1;
6156976af69SBruce Evans 		}
616*35a33d14SHans Petter Selasky 	/* check for tv_usec overflow */
617*35a33d14SHans Petter Selasky 	} else if (__predict_false(tv->tv_usec >= 1000000)) {
618*35a33d14SHans Petter Selasky 		tv->tv_sec += tv->tv_usec / 1000000;
619*35a33d14SHans Petter Selasky 		tv->tv_usec = tv->tv_usec % 1000000;
6207ec73f64SPoul-Henning Kamp 	}
621*35a33d14SHans Petter Selasky 
622*35a33d14SHans Petter Selasky 	/* check for tv_sec underflow */
623*35a33d14SHans Petter Selasky 	if (__predict_false(tv->tv_sec < 0))
624*35a33d14SHans Petter Selasky 		return (1);
625*35a33d14SHans Petter Selasky 	/* check for tv_sec overflow (including room for the tv_usec part) */
626*35a33d14SHans Petter Selasky 	else if (__predict_false(tv->tv_sec >= tick_seconds_max))
627*35a33d14SHans Petter Selasky 		return (INT_MAX);
628*35a33d14SHans Petter Selasky 
629*35a33d14SHans Petter Selasky 	/* cast to "int" to avoid platform differences */
630*35a33d14SHans Petter Selasky 	retval = TIME_TO_TICKS((int)tv->tv_sec, (int)tv->tv_usec, hz);
631*35a33d14SHans Petter Selasky 
632*35a33d14SHans Petter Selasky 	/* add one additional tick */
633*35a33d14SHans Petter Selasky 	return (retval + 1);
634df8bae1dSRodney W. Grimes }
635df8bae1dSRodney W. Grimes 
636df8bae1dSRodney W. Grimes /*
637df8bae1dSRodney W. Grimes  * Start profiling on a process.
638df8bae1dSRodney W. Grimes  *
639df8bae1dSRodney W. Grimes  * Kernel profiling passes proc0 which never exits and hence
640df8bae1dSRodney W. Grimes  * keeps the profile clock running constantly.
641df8bae1dSRodney W. Grimes  */
642df8bae1dSRodney W. Grimes void
6433e85b721SEd Maste startprofclock(struct proc *p)
644df8bae1dSRodney W. Grimes {
645df8bae1dSRodney W. Grimes 
6469752f794SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
6479752f794SJohn Baldwin 	if (p->p_flag & P_STOPPROF)
648a282253aSJulian Elischer 		return;
6499752f794SJohn Baldwin 	if ((p->p_flag & P_PROFIL) == 0) {
6509752f794SJohn Baldwin 		p->p_flag |= P_PROFIL;
651875b8844SAlexander Motin 		mtx_lock(&time_lock);
652238dd320SJake Burkholder 		if (++profprocs == 1)
653238dd320SJake Burkholder 			cpu_startprofclock();
654875b8844SAlexander Motin 		mtx_unlock(&time_lock);
655df8bae1dSRodney W. Grimes 	}
6569752f794SJohn Baldwin }
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes /*
659df8bae1dSRodney W. Grimes  * Stop profiling on a process.
660df8bae1dSRodney W. Grimes  */
661df8bae1dSRodney W. Grimes void
6623e85b721SEd Maste stopprofclock(struct proc *p)
663df8bae1dSRodney W. Grimes {
664df8bae1dSRodney W. Grimes 
665a282253aSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
6669752f794SJohn Baldwin 	if (p->p_flag & P_PROFIL) {
6679752f794SJohn Baldwin 		if (p->p_profthreads != 0) {
6680436fcb8SKonstantin Belousov 			while (p->p_profthreads != 0) {
6699752f794SJohn Baldwin 				p->p_flag |= P_STOPPROF;
670a282253aSJulian Elischer 				msleep(&p->p_profthreads, &p->p_mtx, PPAUSE,
671a89ec05eSPeter Wemm 				    "stopprof", 0);
6720436fcb8SKonstantin Belousov 			}
673a282253aSJulian Elischer 		}
674b62b2304SColin Percival 		if ((p->p_flag & P_PROFIL) == 0)
675b62b2304SColin Percival 			return;
6769752f794SJohn Baldwin 		p->p_flag &= ~P_PROFIL;
677875b8844SAlexander Motin 		mtx_lock(&time_lock);
678238dd320SJake Burkholder 		if (--profprocs == 0)
679238dd320SJake Burkholder 			cpu_stopprofclock();
680875b8844SAlexander Motin 		mtx_unlock(&time_lock);
681df8bae1dSRodney W. Grimes 	}
6829752f794SJohn Baldwin }
683df8bae1dSRodney W. Grimes 
684df8bae1dSRodney W. Grimes /*
6851c4bcd05SJeff Roberson  * Statistics clock.  Updates rusage information and calls the scheduler
6861c4bcd05SJeff Roberson  * to adjust priorities of the active thread.
6871c4bcd05SJeff Roberson  *
688238dd320SJake Burkholder  * This should be called by all active processors.
689df8bae1dSRodney W. Grimes  */
690df8bae1dSRodney W. Grimes void
691cc4f3d0aSMark Johnston statclock(int cnt, int usermode)
692bcfd016cSAlexander Motin {
6938a129caeSDavid Greenman 	struct rusage *ru;
6948a129caeSDavid Greenman 	struct vmspace *vm;
695238dd320SJake Burkholder 	struct thread *td;
696238dd320SJake Burkholder 	struct proc *p;
697238dd320SJake Burkholder 	long rss;
6987628402bSPeter Wemm 	long *cp_time;
699c80038a0SAlexander Motin 	uint64_t runtime, new_switchtime;
7008a129caeSDavid Greenman 
701238dd320SJake Burkholder 	td = curthread;
702238dd320SJake Burkholder 	p = td->td_proc;
703238dd320SJake Burkholder 
7047628402bSPeter Wemm 	cp_time = (long *)PCPU_PTR(cp_time);
705b439e431SJohn Baldwin 	if (usermode) {
706df8bae1dSRodney W. Grimes 		/*
70771a62f8aSBruce Evans 		 * Charge the time as appropriate.
708df8bae1dSRodney W. Grimes 		 */
709bcfd016cSAlexander Motin 		td->td_uticks += cnt;
710fa885116SJulian Elischer 		if (p->p_nice > NZERO)
711bcfd016cSAlexander Motin 			cp_time[CP_NICE] += cnt;
712df8bae1dSRodney W. Grimes 		else
713bcfd016cSAlexander Motin 			cp_time[CP_USER] += cnt;
714df8bae1dSRodney W. Grimes 	} else {
715df8bae1dSRodney W. Grimes 		/*
716df8bae1dSRodney W. Grimes 		 * Came from kernel mode, so we were:
717df8bae1dSRodney W. Grimes 		 * - handling an interrupt,
718df8bae1dSRodney W. Grimes 		 * - doing syscall or trap work on behalf of the current
719df8bae1dSRodney W. Grimes 		 *   user process, or
720df8bae1dSRodney W. Grimes 		 * - spinning in the idle loop.
721df8bae1dSRodney W. Grimes 		 * Whichever it is, charge the time as appropriate.
722df8bae1dSRodney W. Grimes 		 * Note that we charge interrupts to the current process,
723df8bae1dSRodney W. Grimes 		 * regardless of whether they are ``for'' that process,
724df8bae1dSRodney W. Grimes 		 * so that we know how much of its real time was spent
725df8bae1dSRodney W. Grimes 		 * in ``non-process'' (i.e., interrupt) work.
726df8bae1dSRodney W. Grimes 		 */
727e0f66ef8SJohn Baldwin 		if ((td->td_pflags & TDP_ITHREAD) ||
728e0f66ef8SJohn Baldwin 		    td->td_intr_nesting_level >= 2) {
729bcfd016cSAlexander Motin 			td->td_iticks += cnt;
730bcfd016cSAlexander Motin 			cp_time[CP_INTR] += cnt;
7310384fff8SJason Evans 		} else {
732bcfd016cSAlexander Motin 			td->td_pticks += cnt;
733bcfd016cSAlexander Motin 			td->td_sticks += cnt;
734486a9414SJulian Elischer 			if (!TD_IS_IDLETHREAD(td))
735bcfd016cSAlexander Motin 				cp_time[CP_SYS] += cnt;
7360384fff8SJason Evans 			else
737bcfd016cSAlexander Motin 				cp_time[CP_IDLE] += cnt;
738df8bae1dSRodney W. Grimes 		}
7390384fff8SJason Evans 	}
740f5e9e8ecSBruce Evans 
741f5e9e8ecSBruce Evans 	/* Update resource usage integrals and maximums. */
74216f9f205SJohn Baldwin 	MPASS(p->p_vmspace != NULL);
74316f9f205SJohn Baldwin 	vm = p->p_vmspace;
7441c4bcd05SJeff Roberson 	ru = &td->td_ru;
745bcfd016cSAlexander Motin 	ru->ru_ixrss += pgtok(vm->vm_tsize) * cnt;
746bcfd016cSAlexander Motin 	ru->ru_idrss += pgtok(vm->vm_dsize) * cnt;
747bcfd016cSAlexander Motin 	ru->ru_isrss += pgtok(vm->vm_ssize) * cnt;
7481c6d46f9SLuoqi Chen 	rss = pgtok(vmspace_resident_count(vm));
749f5e9e8ecSBruce Evans 	if (ru->ru_maxrss < rss)
750f5e9e8ecSBruce Evans 		ru->ru_maxrss = rss;
7518f51ad55SJeff Roberson 	KTR_POINT2(KTR_SCHED, "thread", sched_tdname(td), "statclock",
7528f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, "stathz:%d", (stathz)?stathz:hz);
753b3e9e682SRyan Stone 	SDT_PROBE2(sched, , , tick, td, td->td_proc);
7547628402bSPeter Wemm 	thread_lock_flags(td, MTX_QUIET);
755c80038a0SAlexander Motin 
756c80038a0SAlexander Motin 	/*
757c80038a0SAlexander Motin 	 * Compute the amount of time during which the current
758c80038a0SAlexander Motin 	 * thread was running, and add that to its total so far.
759c80038a0SAlexander Motin 	 */
760c80038a0SAlexander Motin 	new_switchtime = cpu_ticks();
761c80038a0SAlexander Motin 	runtime = new_switchtime - PCPU_GET(switchtime);
762c80038a0SAlexander Motin 	td->td_runtime += runtime;
763c80038a0SAlexander Motin 	td->td_incruntime += runtime;
764c80038a0SAlexander Motin 	PCPU_SET(switchtime, new_switchtime);
765c80038a0SAlexander Motin 
766c3cccf95SJeff Roberson 	sched_clock(td, cnt);
76740acdeabSJeff Roberson 	thread_unlock(td);
768f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
769f5f9340bSFabien Thomas 	if (td->td_intr_frame != NULL)
770f5f9340bSFabien Thomas 		PMC_SOFT_CALL_TF( , , clock, stat, td->td_intr_frame);
771f5f9340bSFabien Thomas #endif
7726caa8a15SJohn Baldwin }
7736c567274SJohn Baldwin 
7746caa8a15SJohn Baldwin void
775cc4f3d0aSMark Johnston profclock(int cnt, int usermode, uintfptr_t pc)
776bcfd016cSAlexander Motin {
777238dd320SJake Burkholder 	struct thread *td;
778238dd320SJake Burkholder #ifdef GPROF
779238dd320SJake Burkholder 	struct gmonparam *g;
7805c8b4441SJohn Baldwin 	uintfptr_t i;
781238dd320SJake Burkholder #endif
7826caa8a15SJohn Baldwin 
7834a338afdSJulian Elischer 	td = curthread;
784b439e431SJohn Baldwin 	if (usermode) {
785238dd320SJake Burkholder 		/*
786238dd320SJake Burkholder 		 * Came from user mode; CPU was in user state.
787238dd320SJake Burkholder 		 * If this process is being profiled, record the tick.
788a282253aSJulian Elischer 		 * if there is no related user location yet, don't
789a282253aSJulian Elischer 		 * bother trying to count it.
790238dd320SJake Burkholder 		 */
7919752f794SJohn Baldwin 		if (td->td_proc->p_flag & P_PROFIL)
792bcfd016cSAlexander Motin 			addupc_intr(td, pc, cnt);
793238dd320SJake Burkholder 	}
794238dd320SJake Burkholder #ifdef GPROF
795238dd320SJake Burkholder 	else {
796238dd320SJake Burkholder 		/*
797238dd320SJake Burkholder 		 * Kernel statistics are just like addupc_intr, only easier.
798238dd320SJake Burkholder 		 */
799238dd320SJake Burkholder 		g = &_gmonparam;
800b439e431SJohn Baldwin 		if (g->state == GMON_PROF_ON && pc >= g->lowpc) {
801b439e431SJohn Baldwin 			i = PC_TO_I(g, pc);
802238dd320SJake Burkholder 			if (i < g->textsize) {
803bcfd016cSAlexander Motin 				KCOUNT(g, i) += cnt;
804238dd320SJake Burkholder 			}
805238dd320SJake Burkholder 		}
806238dd320SJake Burkholder 	}
807238dd320SJake Burkholder #endif
8081af19ee4SAlexander Motin #ifdef HWPMC_HOOKS
8091af19ee4SAlexander Motin 	if (td->td_intr_frame != NULL)
8101af19ee4SAlexander Motin 		PMC_SOFT_CALL_TF( , , clock, prof, td->td_intr_frame);
8111af19ee4SAlexander Motin #endif
812df8bae1dSRodney W. Grimes }
813df8bae1dSRodney W. Grimes 
814df8bae1dSRodney W. Grimes /*
815df8bae1dSRodney W. Grimes  * Return information about system clocks.
816df8bae1dSRodney W. Grimes  */
817787d58f2SPoul-Henning Kamp static int
81882d9ae4eSPoul-Henning Kamp sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
819df8bae1dSRodney W. Grimes {
820df8bae1dSRodney W. Grimes 	struct clockinfo clkinfo;
821df8bae1dSRodney W. Grimes 	/*
822df8bae1dSRodney W. Grimes 	 * Construct clockinfo structure.
823df8bae1dSRodney W. Grimes 	 */
824a9a0f15aSBruce Evans 	bzero(&clkinfo, sizeof(clkinfo));
825df8bae1dSRodney W. Grimes 	clkinfo.hz = hz;
826df8bae1dSRodney W. Grimes 	clkinfo.tick = tick;
827df8bae1dSRodney W. Grimes 	clkinfo.profhz = profhz;
828df8bae1dSRodney W. Grimes 	clkinfo.stathz = stathz ? stathz : hz;
829ae0eb976SPoul-Henning Kamp 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
830df8bae1dSRodney W. Grimes }
8313f31c649SGarrett Wollman 
832c383c221SEd Schouten SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate,
833c383c221SEd Schouten 	CTLTYPE_STRUCT|CTLFLAG_RD|CTLFLAG_MPSAFE,
834af1408e3SLuigi Rizzo 	0, 0, sysctl_kern_clockrate, "S,clockinfo",
835af1408e3SLuigi Rizzo 	"Rate and period of various kernel clocks");
836370c3cb5SSean Kelly 
8374103b765SPoul-Henning Kamp static void
8389079fff5SNick Hibma watchdog_config(void *unused __unused, u_int cmd, int *error)
839370c3cb5SSean Kelly {
8404103b765SPoul-Henning Kamp 	u_int u;
841370c3cb5SSean Kelly 
8424103b765SPoul-Henning Kamp 	u = cmd & WD_INTERVAL;
8439079fff5SNick Hibma 	if (u >= WD_TO_1SEC) {
8444103b765SPoul-Henning Kamp 		watchdog_ticks = (1 << (u - WD_TO_1SEC)) * hz;
8454103b765SPoul-Henning Kamp 		watchdog_enabled = 1;
8469079fff5SNick Hibma 		*error = 0;
8474103b765SPoul-Henning Kamp 	} else {
8484103b765SPoul-Henning Kamp 		watchdog_enabled = 0;
849370c3cb5SSean Kelly 	}
8504103b765SPoul-Henning Kamp }
851370c3cb5SSean Kelly 
852370c3cb5SSean Kelly /*
853370c3cb5SSean Kelly  * Handle a watchdog timeout by dumping interrupt information and
854911d16b8SEd Maste  * then either dropping to DDB or panicking.
855370c3cb5SSean Kelly  */
856370c3cb5SSean Kelly static void
857370c3cb5SSean Kelly watchdog_fire(void)
858370c3cb5SSean Kelly {
859370c3cb5SSean Kelly 	int nintr;
86060ae52f7SEd Schouten 	uint64_t inttotal;
861370c3cb5SSean Kelly 	u_long *curintr;
862370c3cb5SSean Kelly 	char *curname;
863370c3cb5SSean Kelly 
864370c3cb5SSean Kelly 	curintr = intrcnt;
865370c3cb5SSean Kelly 	curname = intrnames;
866370c3cb5SSean Kelly 	inttotal = 0;
867556a5850SAlexander Motin 	nintr = sintrcnt / sizeof(u_long);
868370c3cb5SSean Kelly 
869370c3cb5SSean Kelly 	printf("interrupt                   total\n");
870370c3cb5SSean Kelly 	while (--nintr >= 0) {
871370c3cb5SSean Kelly 		if (*curintr)
872370c3cb5SSean Kelly 			printf("%-12s %20lu\n", curname, *curintr);
873370c3cb5SSean Kelly 		curname += strlen(curname) + 1;
874370c3cb5SSean Kelly 		inttotal += *curintr++;
875370c3cb5SSean Kelly 	}
8766cda4155SSean Kelly 	printf("Total        %20ju\n", (uintmax_t)inttotal);
877911d16b8SEd Maste 
878911d16b8SEd Maste #if defined(KDB) && !defined(KDB_UNATTENDED)
879911d16b8SEd Maste 	kdb_backtrace();
8803de213ccSRobert Watson 	kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout");
881911d16b8SEd Maste #else
882370c3cb5SSean Kelly 	panic("watchdog timeout");
883911d16b8SEd Maste #endif
884370c3cb5SSean Kelly }
885