xref: /freebsd/sys/kern/kern_clock.c (revision 69a2875821ed9404a95f41bbeeb561908e3c38dc)
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.
18*69a28758SEd Maste  * 3. 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>
51f7829d0dSAttilio Rao #include <sys/kthread.h>
5261d80e90SJohn Baldwin #include <sys/ktr.h>
53f7829d0dSAttilio Rao #include <sys/lock.h>
5435e0e5b3SJohn Baldwin #include <sys/mutex.h>
55df8bae1dSRodney W. Grimes #include <sys/proc.h>
56e4625663SJeff Roberson #include <sys/resource.h>
57df8bae1dSRodney W. Grimes #include <sys/resourcevar.h>
58b43179fbSJeff Roberson #include <sys/sched.h>
59b3e9e682SRyan Stone #include <sys/sdt.h>
60797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
61f7829d0dSAttilio Rao #include <sys/sleepqueue.h>
626caa8a15SJohn Baldwin #include <sys/smp.h>
638a129caeSDavid Greenman #include <vm/vm.h>
64efeaf95aSDavid Greenman #include <vm/pmap.h>
65efeaf95aSDavid Greenman #include <vm/vm_map.h>
66797f2d22SPoul-Henning Kamp #include <sys/sysctl.h>
678088699fSJohn Baldwin #include <sys/bus.h>
688088699fSJohn Baldwin #include <sys/interrupt.h>
69104a9b7eSAlexander Kabaev #include <sys/limits.h>
70e7fa55afSPoul-Henning Kamp #include <sys/timetc.h>
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes #ifdef GPROF
73df8bae1dSRodney W. Grimes #include <sys/gmon.h>
74df8bae1dSRodney W. Grimes #endif
75df8bae1dSRodney W. Grimes 
7636c0fd9dSJoseph Koshy #ifdef HWPMC_HOOKS
7736c0fd9dSJoseph Koshy #include <sys/pmckern.h>
78f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , clock, hard);
79f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , clock, stat);
80d49302aeSFabien Thomas PMC_SOFT_DEFINE_EX( , , clock, prof, \
81d49302aeSFabien Thomas     cpu_startprofclock, cpu_stopprofclock);
8236c0fd9dSJoseph Koshy #endif
8336c0fd9dSJoseph Koshy 
84e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
85e4fc250cSLuigi Rizzo extern void hardclock_device_poll(void);
86e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
87eae8fc2cSSteve Passe 
884d77a549SAlfred Perlstein static void initclocks(void *dummy);
89237fdd78SRobert Watson SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL);
902b14f991SJulian Elischer 
918b98fec9SJeff Roberson /* Spin-lock protecting profiling statistics. */
9286a49deaSAttilio Rao static struct mtx time_lock;
938b98fec9SJeff Roberson 
94b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(sched);
95d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , tick, "struct thread *", "struct proc *");
96b3e9e682SRyan Stone 
9762919d78SPeter Wemm static int
9862919d78SPeter Wemm sysctl_kern_cp_time(SYSCTL_HANDLER_ARGS)
9962919d78SPeter Wemm {
10062919d78SPeter Wemm 	int error;
1017628402bSPeter Wemm 	long cp_time[CPUSTATES];
102cff2e749SPaul Saab #ifdef SCTL_MASK32
10362919d78SPeter Wemm 	int i;
10462919d78SPeter Wemm 	unsigned int cp_time32[CPUSTATES];
1057628402bSPeter Wemm #endif
10662919d78SPeter Wemm 
1077628402bSPeter Wemm 	read_cpu_time(cp_time);
1087628402bSPeter Wemm #ifdef SCTL_MASK32
109cff2e749SPaul Saab 	if (req->flags & SCTL_MASK32) {
11062919d78SPeter Wemm 		if (!req->oldptr)
11162919d78SPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(cp_time32));
11262919d78SPeter Wemm 		for (i = 0; i < CPUSTATES; i++)
11362919d78SPeter Wemm 			cp_time32[i] = (unsigned int)cp_time[i];
11462919d78SPeter Wemm 		error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
11562919d78SPeter Wemm 	} else
11662919d78SPeter Wemm #endif
11762919d78SPeter Wemm 	{
11862919d78SPeter Wemm 		if (!req->oldptr)
11962919d78SPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(cp_time));
12062919d78SPeter Wemm 		error = SYSCTL_OUT(req, cp_time, sizeof(cp_time));
12162919d78SPeter Wemm 	}
12262919d78SPeter Wemm 	return error;
12362919d78SPeter Wemm }
12462919d78SPeter Wemm 
125c383c221SEd Schouten SYSCTL_PROC(_kern, OID_AUTO, cp_time, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE,
12662919d78SPeter Wemm     0,0, sysctl_kern_cp_time, "LU", "CPU time statistics");
1277f112b04SRobert Watson 
1287628402bSPeter Wemm static long empty[CPUSTATES];
1297628402bSPeter Wemm 
1307628402bSPeter Wemm static int
1317628402bSPeter Wemm sysctl_kern_cp_times(SYSCTL_HANDLER_ARGS)
1327628402bSPeter Wemm {
1337628402bSPeter Wemm 	struct pcpu *pcpu;
1347628402bSPeter Wemm 	int error;
135ef54068bSRobert Watson 	int c;
1367628402bSPeter Wemm 	long *cp_time;
1377628402bSPeter Wemm #ifdef SCTL_MASK32
1387628402bSPeter Wemm 	unsigned int cp_time32[CPUSTATES];
139ef54068bSRobert Watson 	int i;
1407628402bSPeter Wemm #endif
1417628402bSPeter Wemm 
1427628402bSPeter Wemm 	if (!req->oldptr) {
1437628402bSPeter Wemm #ifdef SCTL_MASK32
1447628402bSPeter Wemm 		if (req->flags & SCTL_MASK32)
1457628402bSPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(cp_time32) * (mp_maxid + 1));
1467628402bSPeter Wemm 		else
1477628402bSPeter Wemm #endif
1487628402bSPeter Wemm 			return SYSCTL_OUT(req, 0, sizeof(long) * CPUSTATES * (mp_maxid + 1));
1497628402bSPeter Wemm 	}
1507628402bSPeter Wemm 	for (error = 0, c = 0; error == 0 && c <= mp_maxid; c++) {
1517628402bSPeter Wemm 		if (!CPU_ABSENT(c)) {
1527628402bSPeter Wemm 			pcpu = pcpu_find(c);
1537628402bSPeter Wemm 			cp_time = pcpu->pc_cp_time;
1547628402bSPeter Wemm 		} else {
1557628402bSPeter Wemm 			cp_time = empty;
1567628402bSPeter Wemm 		}
1577628402bSPeter Wemm #ifdef SCTL_MASK32
1587628402bSPeter Wemm 		if (req->flags & SCTL_MASK32) {
1597628402bSPeter Wemm 			for (i = 0; i < CPUSTATES; i++)
1607628402bSPeter Wemm 				cp_time32[i] = (unsigned int)cp_time[i];
1617628402bSPeter Wemm 			error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32));
1627628402bSPeter Wemm 		} else
1637628402bSPeter Wemm #endif
1647628402bSPeter Wemm 			error = SYSCTL_OUT(req, cp_time, sizeof(long) * CPUSTATES);
1657628402bSPeter Wemm 	}
1667628402bSPeter Wemm 	return error;
1677628402bSPeter Wemm }
1687628402bSPeter Wemm 
169c383c221SEd Schouten SYSCTL_PROC(_kern, OID_AUTO, cp_times, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE,
1707628402bSPeter Wemm     0,0, sysctl_kern_cp_times, "LU", "per-CPU time statistics");
1717628402bSPeter Wemm 
172f7829d0dSAttilio Rao #ifdef DEADLKRES
17336e51f65SAttilio Rao static const char *blessed[] = {
17495335fd8SAttilio Rao 	"getblk",
17536e51f65SAttilio Rao 	"so_snd_sx",
17636e51f65SAttilio Rao 	"so_rcv_sx",
17736e51f65SAttilio Rao 	NULL
17836e51f65SAttilio Rao };
179f7829d0dSAttilio Rao static int slptime_threshold = 1800;
180f7829d0dSAttilio Rao static int blktime_threshold = 900;
181f7829d0dSAttilio Rao static int sleepfreq = 3;
182f7829d0dSAttilio Rao 
183f7829d0dSAttilio Rao static void
184f7829d0dSAttilio Rao deadlkres(void)
185f7829d0dSAttilio Rao {
186f7829d0dSAttilio Rao 	struct proc *p;
187f7829d0dSAttilio Rao 	struct thread *td;
188f7829d0dSAttilio Rao 	void *wchan;
18936e51f65SAttilio Rao 	int blkticks, i, slpticks, slptype, tryl, tticks;
190f7829d0dSAttilio Rao 
191f7829d0dSAttilio Rao 	tryl = 0;
192f7829d0dSAttilio Rao 	for (;;) {
193f7829d0dSAttilio Rao 		blkticks = blktime_threshold * hz;
194f7829d0dSAttilio Rao 		slpticks = slptime_threshold * hz;
195f7829d0dSAttilio Rao 
196f7829d0dSAttilio Rao 		/*
197f7829d0dSAttilio Rao 		 * Avoid to sleep on the sx_lock in order to avoid a possible
198f7829d0dSAttilio Rao 		 * priority inversion problem leading to starvation.
199f7829d0dSAttilio Rao 		 * If the lock can't be held after 100 tries, panic.
200f7829d0dSAttilio Rao 		 */
201f7829d0dSAttilio Rao 		if (!sx_try_slock(&allproc_lock)) {
202f7829d0dSAttilio Rao 			if (tryl > 100)
203f7829d0dSAttilio Rao 		panic("%s: possible deadlock detected on allproc_lock\n",
204f7829d0dSAttilio Rao 				    __func__);
205f7829d0dSAttilio Rao 			tryl++;
206b5850804SJohn Baldwin 			pause("allproc", sleepfreq * hz);
207f7829d0dSAttilio Rao 			continue;
208f7829d0dSAttilio Rao 		}
209f7829d0dSAttilio Rao 		tryl = 0;
210f7829d0dSAttilio Rao 		FOREACH_PROC_IN_SYSTEM(p) {
211f7829d0dSAttilio Rao 			PROC_LOCK(p);
212e806d352SJohn Baldwin 			if (p->p_state == PRS_NEW) {
213e806d352SJohn Baldwin 				PROC_UNLOCK(p);
214e806d352SJohn Baldwin 				continue;
215e806d352SJohn Baldwin 			}
216f7829d0dSAttilio Rao 			FOREACH_THREAD_IN_PROC(p, td) {
217631cb86fSAttilio Rao 
218f7829d0dSAttilio Rao 				thread_lock(td);
2194a7d0bfcSRyan Stone 				if (TD_ON_LOCK(td)) {
220f7829d0dSAttilio Rao 
221f7829d0dSAttilio Rao 					/*
222f7829d0dSAttilio Rao 					 * The thread should be blocked on a
223f7829d0dSAttilio Rao 					 * turnstile, simply check if the
224f7829d0dSAttilio Rao 					 * turnstile channel is in good state.
225f7829d0dSAttilio Rao 					 */
226f7829d0dSAttilio Rao 					MPASS(td->td_blocked != NULL);
22736e51f65SAttilio Rao 
228f7829d0dSAttilio Rao 					tticks = ticks - td->td_blktick;
229f7829d0dSAttilio Rao 					thread_unlock(td);
230f7829d0dSAttilio Rao 					if (tticks > blkticks) {
231f7829d0dSAttilio Rao 
232f7829d0dSAttilio Rao 						/*
233f7829d0dSAttilio Rao 						 * Accordingly with provided
234f7829d0dSAttilio Rao 						 * thresholds, this thread is
235f7829d0dSAttilio Rao 						 * stuck for too long on a
236f7829d0dSAttilio Rao 						 * turnstile.
237f7829d0dSAttilio Rao 						 */
238f7829d0dSAttilio Rao 						PROC_UNLOCK(p);
239f7829d0dSAttilio Rao 						sx_sunlock(&allproc_lock);
240f7829d0dSAttilio Rao 	panic("%s: possible deadlock detected for %p, blocked for %d ticks\n",
241f7829d0dSAttilio Rao 						    __func__, td, tticks);
242f7829d0dSAttilio Rao 					}
243631cb86fSAttilio Rao 				} else if (TD_IS_SLEEPING(td) &&
2444a7d0bfcSRyan Stone 				    TD_ON_SLEEPQ(td)) {
24536e51f65SAttilio Rao 
246f7829d0dSAttilio Rao 					/*
247f7829d0dSAttilio Rao 					 * Check if the thread is sleeping on a
248f7829d0dSAttilio Rao 					 * lock, otherwise skip the check.
249f7829d0dSAttilio Rao 					 * Drop the thread lock in order to
250f7829d0dSAttilio Rao 					 * avoid a LOR with the sleepqueue
251f7829d0dSAttilio Rao 					 * spinlock.
252f7829d0dSAttilio Rao 					 */
253f7829d0dSAttilio Rao 					wchan = td->td_wchan;
254f7829d0dSAttilio Rao 					tticks = ticks - td->td_slptick;
255f7829d0dSAttilio Rao 					thread_unlock(td);
256f7829d0dSAttilio Rao 					slptype = sleepq_type(wchan);
257f7829d0dSAttilio Rao 					if ((slptype == SLEEPQ_SX ||
258f7829d0dSAttilio Rao 					    slptype == SLEEPQ_LK) &&
259f7829d0dSAttilio Rao 					    tticks > slpticks) {
260f7829d0dSAttilio Rao 
261f7829d0dSAttilio Rao 						/*
262f7829d0dSAttilio Rao 						 * Accordingly with provided
263f7829d0dSAttilio Rao 						 * thresholds, this thread is
264f7829d0dSAttilio Rao 						 * stuck for too long on a
265f7829d0dSAttilio Rao 						 * sleepqueue.
26636e51f65SAttilio Rao 						 * However, being on a
26736e51f65SAttilio Rao 						 * sleepqueue, we might still
26836e51f65SAttilio Rao 						 * check for the blessed
26936e51f65SAttilio Rao 						 * list.
270f7829d0dSAttilio Rao 						 */
27136e51f65SAttilio Rao 						tryl = 0;
27236e51f65SAttilio Rao 						for (i = 0; blessed[i] != NULL;
27336e51f65SAttilio Rao 						    i++) {
27436e51f65SAttilio Rao 							if (!strcmp(blessed[i],
27536e51f65SAttilio Rao 							    td->td_wmesg)) {
27636e51f65SAttilio Rao 								tryl = 1;
27736e51f65SAttilio Rao 								break;
27836e51f65SAttilio Rao 							}
27936e51f65SAttilio Rao 						}
28036e51f65SAttilio Rao 						if (tryl != 0) {
28136e51f65SAttilio Rao 							tryl = 0;
28236e51f65SAttilio Rao 							continue;
28336e51f65SAttilio Rao 						}
284f7829d0dSAttilio Rao 						PROC_UNLOCK(p);
285f7829d0dSAttilio Rao 						sx_sunlock(&allproc_lock);
286f7829d0dSAttilio Rao 	panic("%s: possible deadlock detected for %p, blocked for %d ticks\n",
287f7829d0dSAttilio Rao 						    __func__, td, tticks);
288f7829d0dSAttilio Rao 					}
289f7829d0dSAttilio Rao 				} else
290f7829d0dSAttilio Rao 					thread_unlock(td);
291f7829d0dSAttilio Rao 			}
292f7829d0dSAttilio Rao 			PROC_UNLOCK(p);
293f7829d0dSAttilio Rao 		}
294f7829d0dSAttilio Rao 		sx_sunlock(&allproc_lock);
295f7829d0dSAttilio Rao 
296f7829d0dSAttilio Rao 		/* Sleep for sleepfreq seconds. */
297b5850804SJohn Baldwin 		pause("-", sleepfreq * hz);
298f7829d0dSAttilio Rao 	}
299f7829d0dSAttilio Rao }
300f7829d0dSAttilio Rao 
301f7829d0dSAttilio Rao static struct kthread_desc deadlkres_kd = {
302f7829d0dSAttilio Rao 	"deadlkres",
303f7829d0dSAttilio Rao 	deadlkres,
304f7829d0dSAttilio Rao 	(struct thread **)NULL
305f7829d0dSAttilio Rao };
306f7829d0dSAttilio Rao 
307f7829d0dSAttilio Rao SYSINIT(deadlkres, SI_SUB_CLOCKS, SI_ORDER_ANY, kthread_start, &deadlkres_kd);
308f7829d0dSAttilio Rao 
3096472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, deadlkres, CTLFLAG_RW, 0,
3106472ac3dSEd Schouten     "Deadlock resolver");
311f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, slptime_threshold, CTLFLAG_RW,
312f7829d0dSAttilio Rao     &slptime_threshold, 0,
313f7829d0dSAttilio Rao     "Number of seconds within is valid to sleep on a sleepqueue");
314f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, blktime_threshold, CTLFLAG_RW,
315f7829d0dSAttilio Rao     &blktime_threshold, 0,
316f7829d0dSAttilio Rao     "Number of seconds within is valid to block on a turnstile");
317f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, sleepfreq, CTLFLAG_RW, &sleepfreq, 0,
318f7829d0dSAttilio Rao     "Number of seconds between any deadlock resolver thread run");
319f7829d0dSAttilio Rao #endif	/* DEADLKRES */
320f7829d0dSAttilio Rao 
3217628402bSPeter Wemm void
3227628402bSPeter Wemm read_cpu_time(long *cp_time)
3237628402bSPeter Wemm {
3247628402bSPeter Wemm 	struct pcpu *pc;
3257628402bSPeter Wemm 	int i, j;
3267628402bSPeter Wemm 
3277628402bSPeter Wemm 	/* Sum up global cp_time[]. */
3287628402bSPeter Wemm 	bzero(cp_time, sizeof(long) * CPUSTATES);
3293aa6d94eSJohn Baldwin 	CPU_FOREACH(i) {
3307628402bSPeter Wemm 		pc = pcpu_find(i);
3317628402bSPeter Wemm 		for (j = 0; j < CPUSTATES; j++)
3327628402bSPeter Wemm 			cp_time[j] += pc->pc_cp_time[j];
3337628402bSPeter Wemm 	}
3347628402bSPeter Wemm }
3357628402bSPeter Wemm 
3364103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG
3374103b765SPoul-Henning Kamp #include <sys/watchdog.h>
338370c3cb5SSean Kelly 
3394103b765SPoul-Henning Kamp static int watchdog_ticks;
340370c3cb5SSean Kelly static int watchdog_enabled;
3414103b765SPoul-Henning Kamp static void watchdog_fire(void);
3424103b765SPoul-Henning Kamp static void watchdog_config(void *, u_int, int *);
3434103b765SPoul-Henning Kamp #endif /* SW_WATCHDOG */
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 
3833e288e62SDimitry Andric static DPCPU_DEFINE(int, pcputicks);	/* Per-CPU version of ticks. */
3845760b029SKonstantin Belousov #ifdef DEVICE_POLLING
3855760b029SKonstantin Belousov static int devpoll_run = 0;
3865760b029SKonstantin Belousov #endif
387dbd55f3fSAlexander Motin 
388df8bae1dSRodney W. Grimes /*
389df8bae1dSRodney W. Grimes  * Initialize clock frequencies and start both clocks running.
390df8bae1dSRodney W. Grimes  */
3912b14f991SJulian Elischer /* ARGSUSED*/
3922b14f991SJulian Elischer static void
393d841aaa7SBruce Evans initclocks(dummy)
394d841aaa7SBruce Evans 	void *dummy;
395df8bae1dSRodney W. Grimes {
396fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
397fdce57a0SJohn Baldwin 	struct proc *p;
398fdce57a0SJohn Baldwin 	struct thread *td;
399fdce57a0SJohn Baldwin #endif
400df8bae1dSRodney W. Grimes 	register int i;
401df8bae1dSRodney W. Grimes 
402df8bae1dSRodney W. Grimes 	/*
403df8bae1dSRodney W. Grimes 	 * Set divisors to 1 (normal case) and let the machine-specific
404df8bae1dSRodney W. Grimes 	 * code do its bit.
405df8bae1dSRodney W. Grimes 	 */
406875b8844SAlexander Motin 	mtx_init(&time_lock, "time lock", NULL, MTX_DEF);
40763d69d25SRobert Watson 	cpu_initclocks();
408df8bae1dSRodney W. Grimes 
409df8bae1dSRodney W. Grimes 	/*
410df8bae1dSRodney W. Grimes 	 * Compute profhz/stathz, and fix profhz if needed.
411df8bae1dSRodney W. Grimes 	 */
412df8bae1dSRodney W. Grimes 	i = stathz ? stathz : hz;
413df8bae1dSRodney W. Grimes 	if (profhz == 0)
414df8bae1dSRodney W. Grimes 		profhz = i;
415df8bae1dSRodney W. Grimes 	psratio = profhz / i;
4164103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG
4174103b765SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0);
4184103b765SPoul-Henning Kamp #endif
4190c56c4f1SPeter Wemm 	/*
4200c56c4f1SPeter Wemm 	 * Arrange for ticks to wrap 10 minutes after boot to help catch
4210c56c4f1SPeter Wemm 	 * sign problems sooner.
4220c56c4f1SPeter Wemm 	 */
4230c56c4f1SPeter Wemm 	ticks = INT_MAX - (hz * 10 * 60);
424fdce57a0SJohn Baldwin 
425fdce57a0SJohn Baldwin #ifdef EARLY_AP_STARTUP
426fdce57a0SJohn Baldwin 	/*
427fdce57a0SJohn Baldwin 	 * Fixup the tick counts in any blocked or sleeping threads to
428fdce57a0SJohn Baldwin 	 * account for the jump above.
429fdce57a0SJohn Baldwin 	 */
430fdce57a0SJohn Baldwin 	sx_slock(&allproc_lock);
431fdce57a0SJohn Baldwin 	FOREACH_PROC_IN_SYSTEM(p) {
432fdce57a0SJohn Baldwin 		PROC_LOCK(p);
433fdce57a0SJohn Baldwin 		if (p->p_state == PRS_NEW) {
434fdce57a0SJohn Baldwin 			PROC_UNLOCK(p);
435fdce57a0SJohn Baldwin 			continue;
436fdce57a0SJohn Baldwin 		}
437fdce57a0SJohn Baldwin 		FOREACH_THREAD_IN_PROC(p, td) {
438fdce57a0SJohn Baldwin 			thread_lock(td);
439fdce57a0SJohn Baldwin 			if (TD_ON_LOCK(td)) {
440fdce57a0SJohn Baldwin 				MPASS(td->td_blktick == 0);
441fdce57a0SJohn Baldwin 				td->td_blktick = ticks;
442fdce57a0SJohn Baldwin 			}
443fdce57a0SJohn Baldwin 			if (TD_ON_SLEEPQ(td)) {
444fdce57a0SJohn Baldwin 				MPASS(td->td_slptick == 0);
445fdce57a0SJohn Baldwin 				td->td_slptick = ticks;
446fdce57a0SJohn Baldwin 			}
447fdce57a0SJohn Baldwin 			thread_unlock(td);
448fdce57a0SJohn Baldwin 		}
449fdce57a0SJohn Baldwin 		PROC_UNLOCK(p);
450fdce57a0SJohn Baldwin 	}
451fdce57a0SJohn Baldwin 	sx_sunlock(&allproc_lock);
452fdce57a0SJohn Baldwin #endif
453df8bae1dSRodney W. Grimes }
454df8bae1dSRodney W. Grimes 
455df8bae1dSRodney W. Grimes /*
456238dd320SJake Burkholder  * Each time the real-time timer fires, this function is called on all CPUs.
457b439e431SJohn Baldwin  * Note that hardclock() calls hardclock_cpu() for the boot CPU, so only
458238dd320SJake Burkholder  * the other CPUs in the system need to call this function.
4596caa8a15SJohn Baldwin  */
4606caa8a15SJohn Baldwin void
461b439e431SJohn Baldwin hardclock_cpu(int usermode)
4626caa8a15SJohn Baldwin {
4636caa8a15SJohn Baldwin 	struct pstats *pstats;
464238dd320SJake Burkholder 	struct thread *td = curthread;
465b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
466b61ce5b0SJeff Roberson 	int flags;
4676caa8a15SJohn Baldwin 
4686caa8a15SJohn Baldwin 	/*
4696caa8a15SJohn Baldwin 	 * Run current process's virtual and profile time, as needed.
4706caa8a15SJohn Baldwin 	 */
471ad1e7d28SJulian Elischer 	pstats = p->p_stats;
472b61ce5b0SJeff Roberson 	flags = 0;
473ad1e7d28SJulian Elischer 	if (usermode &&
47440acdeabSJeff Roberson 	    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) {
4755c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
476b61ce5b0SJeff Roberson 		if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
477b61ce5b0SJeff Roberson 			flags |= TDF_ALRMPEND | TDF_ASTPENDING;
4785c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
47940acdeabSJeff Roberson 	}
48040acdeabSJeff Roberson 	if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) {
4815c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
482b61ce5b0SJeff Roberson 		if (itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
483b61ce5b0SJeff Roberson 			flags |= TDF_PROFPEND | TDF_ASTPENDING;
4845c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
48540acdeabSJeff Roberson 	}
48640acdeabSJeff Roberson 	thread_lock(td);
487b61ce5b0SJeff Roberson 	td->td_flags |= flags;
48840acdeabSJeff Roberson 	thread_unlock(td);
48936c0fd9dSJoseph Koshy 
49036c0fd9dSJoseph Koshy #ifdef HWPMC_HOOKS
49136c0fd9dSJoseph Koshy 	if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
49236c0fd9dSJoseph Koshy 		PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
493f5f9340bSFabien Thomas 	if (td->td_intr_frame != NULL)
494f5f9340bSFabien Thomas 		PMC_SOFT_CALL_TF( , , clock, hard, td->td_intr_frame);
49536c0fd9dSJoseph Koshy #endif
4965b999a6bSDavide Italiano 	callout_process(sbinuptime());
4976caa8a15SJohn Baldwin }
4986caa8a15SJohn Baldwin 
4996caa8a15SJohn Baldwin /*
500df8bae1dSRodney W. Grimes  * The real-time timer, interrupting hz times per second.
501df8bae1dSRodney W. Grimes  */
502df8bae1dSRodney W. Grimes void
503b439e431SJohn Baldwin hardclock(int usermode, uintfptr_t pc)
504df8bae1dSRodney W. Grimes {
505df8bae1dSRodney W. Grimes 
506a8df530dSJohn Baldwin 	atomic_add_int(&ticks, 1);
507b439e431SJohn Baldwin 	hardclock_cpu(usermode);
5080e189873SAlexander Motin 	tc_ticktock(1);
509a157e425SAlexander Motin 	cpu_tick_calibration();
510df8bae1dSRodney W. Grimes 	/*
511df8bae1dSRodney W. Grimes 	 * If no separate statistics clock is available, run it from here.
5126caa8a15SJohn Baldwin 	 *
5136caa8a15SJohn Baldwin 	 * XXX: this only works for UP
514df8bae1dSRodney W. Grimes 	 */
515238dd320SJake Burkholder 	if (stathz == 0) {
516b439e431SJohn Baldwin 		profclock(usermode, pc);
517b439e431SJohn Baldwin 		statclock(usermode);
518238dd320SJake Burkholder 	}
519e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
520daccb638SLuigi Rizzo 	hardclock_device_poll();	/* this is very short and quick */
521e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
5224103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG
5234103b765SPoul-Henning Kamp 	if (watchdog_enabled > 0 && --watchdog_ticks <= 0)
524370c3cb5SSean Kelly 		watchdog_fire();
5254103b765SPoul-Henning Kamp #endif /* SW_WATCHDOG */
526ab36c067SJustin T. Gibbs }
527ab36c067SJustin T. Gibbs 
528a157e425SAlexander Motin void
529bcfd016cSAlexander Motin hardclock_cnt(int cnt, int usermode)
530a157e425SAlexander Motin {
531a157e425SAlexander Motin 	struct pstats *pstats;
532a157e425SAlexander Motin 	struct thread *td = curthread;
533a157e425SAlexander Motin 	struct proc *p = td->td_proc;
534a157e425SAlexander Motin 	int *t = DPCPU_PTR(pcputicks);
5354763a8b8SAlexander Motin 	int flags, global, newticks;
5364763a8b8SAlexander Motin #ifdef SW_WATCHDOG
5374763a8b8SAlexander Motin 	int i;
5384763a8b8SAlexander Motin #endif /* SW_WATCHDOG */
539a157e425SAlexander Motin 
540a157e425SAlexander Motin 	/*
541a157e425SAlexander Motin 	 * Update per-CPU and possibly global ticks values.
542a157e425SAlexander Motin 	 */
543a157e425SAlexander Motin 	*t += cnt;
544a157e425SAlexander Motin 	do {
545a157e425SAlexander Motin 		global = ticks;
546a157e425SAlexander Motin 		newticks = *t - global;
547a157e425SAlexander Motin 		if (newticks <= 0) {
548a157e425SAlexander Motin 			if (newticks < -1)
549a157e425SAlexander Motin 				*t = global - 1;
550a157e425SAlexander Motin 			newticks = 0;
551a157e425SAlexander Motin 			break;
552a157e425SAlexander Motin 		}
553a157e425SAlexander Motin 	} while (!atomic_cmpset_int(&ticks, global, *t));
554a157e425SAlexander Motin 
555a157e425SAlexander Motin 	/*
556a157e425SAlexander Motin 	 * Run current process's virtual and profile time, as needed.
557a157e425SAlexander Motin 	 */
558a157e425SAlexander Motin 	pstats = p->p_stats;
559a157e425SAlexander Motin 	flags = 0;
560a157e425SAlexander Motin 	if (usermode &&
561a157e425SAlexander Motin 	    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) {
5625c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
563a157e425SAlexander Motin 		if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL],
564a157e425SAlexander Motin 		    tick * cnt) == 0)
565a157e425SAlexander Motin 			flags |= TDF_ALRMPEND | TDF_ASTPENDING;
5665c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
567a157e425SAlexander Motin 	}
568a157e425SAlexander Motin 	if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) {
5695c7bebf9SKonstantin Belousov 		PROC_ITIMLOCK(p);
570a157e425SAlexander Motin 		if (itimerdecr(&pstats->p_timer[ITIMER_PROF],
571a157e425SAlexander Motin 		    tick * cnt) == 0)
572a157e425SAlexander Motin 			flags |= TDF_PROFPEND | TDF_ASTPENDING;
5735c7bebf9SKonstantin Belousov 		PROC_ITIMUNLOCK(p);
574a157e425SAlexander Motin 	}
575ef89d843SMark Johnston 	if (flags != 0) {
576a157e425SAlexander Motin 		thread_lock(td);
577a157e425SAlexander Motin 		td->td_flags |= flags;
578a157e425SAlexander Motin 		thread_unlock(td);
579ef89d843SMark Johnston 	}
580a157e425SAlexander Motin 
581a157e425SAlexander Motin #ifdef	HWPMC_HOOKS
582a157e425SAlexander Motin 	if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid)))
583a157e425SAlexander Motin 		PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL);
584f5f9340bSFabien Thomas 	if (td->td_intr_frame != NULL)
585f5f9340bSFabien Thomas 		PMC_SOFT_CALL_TF( , , clock, hard, td->td_intr_frame);
586a157e425SAlexander Motin #endif
587a157e425SAlexander Motin 	/* We are in charge to handle this tick duty. */
588a157e425SAlexander Motin 	if (newticks > 0) {
5890e189873SAlexander Motin 		tc_ticktock(newticks);
590a157e425SAlexander Motin #ifdef DEVICE_POLLING
5915760b029SKonstantin Belousov 		/* Dangerous and no need to call these things concurrently. */
5925760b029SKonstantin Belousov 		if (atomic_cmpset_acq_int(&devpoll_run, 0, 1)) {
5934763a8b8SAlexander Motin 			/* This is very short and quick. */
5944763a8b8SAlexander Motin 			hardclock_device_poll();
5955760b029SKonstantin Belousov 			atomic_store_rel_int(&devpoll_run, 0);
5964763a8b8SAlexander Motin 		}
5975760b029SKonstantin Belousov #endif /* DEVICE_POLLING */
598a157e425SAlexander Motin #ifdef SW_WATCHDOG
599a157e425SAlexander Motin 		if (watchdog_enabled > 0) {
6004763a8b8SAlexander Motin 			i = atomic_fetchadd_int(&watchdog_ticks, -newticks);
6014763a8b8SAlexander Motin 			if (i > 0 && i <= newticks)
602a157e425SAlexander Motin 				watchdog_fire();
603a157e425SAlexander Motin 		}
604a157e425SAlexander Motin #endif /* SW_WATCHDOG */
605a157e425SAlexander Motin 	}
606a157e425SAlexander Motin 	if (curcpu == CPU_FIRST())
607a157e425SAlexander Motin 		cpu_tick_calibration();
608a157e425SAlexander Motin }
609a157e425SAlexander Motin 
610a157e425SAlexander Motin void
611a157e425SAlexander Motin hardclock_sync(int cpu)
612a157e425SAlexander Motin {
613a157e425SAlexander Motin 	int	*t = DPCPU_ID_PTR(cpu, pcputicks);
614a157e425SAlexander Motin 
615a157e425SAlexander Motin 	*t = ticks;
616a157e425SAlexander Motin }
617a157e425SAlexander Motin 
618df8bae1dSRodney W. Grimes /*
619227ee8a1SPoul-Henning Kamp  * Compute number of ticks in the specified amount of time.
620df8bae1dSRodney W. Grimes  */
621df8bae1dSRodney W. Grimes int
622227ee8a1SPoul-Henning Kamp tvtohz(tv)
623df8bae1dSRodney W. Grimes 	struct timeval *tv;
624df8bae1dSRodney W. Grimes {
6256976af69SBruce Evans 	register unsigned long ticks;
6266976af69SBruce Evans 	register long sec, usec;
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes 	/*
6296976af69SBruce Evans 	 * If the number of usecs in the whole seconds part of the time
6306976af69SBruce Evans 	 * difference fits in a long, then the total number of usecs will
6316976af69SBruce Evans 	 * fit in an unsigned long.  Compute the total and convert it to
6326976af69SBruce Evans 	 * ticks, rounding up and adding 1 to allow for the current tick
6336976af69SBruce Evans 	 * to expire.  Rounding also depends on unsigned long arithmetic
6346976af69SBruce Evans 	 * to avoid overflow.
635df8bae1dSRodney W. Grimes 	 *
6366976af69SBruce Evans 	 * Otherwise, if the number of ticks in the whole seconds part of
6376976af69SBruce Evans 	 * the time difference fits in a long, then convert the parts to
6386976af69SBruce Evans 	 * ticks separately and add, using similar rounding methods and
6396976af69SBruce Evans 	 * overflow avoidance.  This method would work in the previous
6406976af69SBruce Evans 	 * case but it is slightly slower and assumes that hz is integral.
6416976af69SBruce Evans 	 *
6426976af69SBruce Evans 	 * Otherwise, round the time difference down to the maximum
6436976af69SBruce Evans 	 * representable value.
6446976af69SBruce Evans 	 *
6456976af69SBruce Evans 	 * If ints have 32 bits, then the maximum value for any timeout in
6466976af69SBruce Evans 	 * 10ms ticks is 248 days.
647df8bae1dSRodney W. Grimes 	 */
648227ee8a1SPoul-Henning Kamp 	sec = tv->tv_sec;
649227ee8a1SPoul-Henning Kamp 	usec = tv->tv_usec;
6506976af69SBruce Evans 	if (usec < 0) {
6516976af69SBruce Evans 		sec--;
6526976af69SBruce Evans 		usec += 1000000;
6536976af69SBruce Evans 	}
6546976af69SBruce Evans 	if (sec < 0) {
6556976af69SBruce Evans #ifdef DIAGNOSTIC
656b05dcf3cSPoul-Henning Kamp 		if (usec > 0) {
6577ec73f64SPoul-Henning Kamp 			sec++;
6587ec73f64SPoul-Henning Kamp 			usec -= 1000000;
6597ec73f64SPoul-Henning Kamp 		}
660227ee8a1SPoul-Henning Kamp 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
6616976af69SBruce Evans 		       sec, usec);
6626976af69SBruce Evans #endif
6636976af69SBruce Evans 		ticks = 1;
6646976af69SBruce Evans 	} else if (sec <= LONG_MAX / 1000000)
66555e0987aSPedro F. Giffuni 		ticks = howmany(sec * 1000000 + (unsigned long)usec, tick) + 1;
6666976af69SBruce Evans 	else if (sec <= LONG_MAX / hz)
6676976af69SBruce Evans 		ticks = sec * hz
66855e0987aSPedro F. Giffuni 			+ howmany((unsigned long)usec, tick) + 1;
6696976af69SBruce Evans 	else
6706976af69SBruce Evans 		ticks = LONG_MAX;
6716976af69SBruce Evans 	if (ticks > INT_MAX)
6726976af69SBruce Evans 		ticks = INT_MAX;
673d6116663SAlexander Langer 	return ((int)ticks);
674df8bae1dSRodney W. Grimes }
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes /*
677df8bae1dSRodney W. Grimes  * Start profiling on a process.
678df8bae1dSRodney W. Grimes  *
679df8bae1dSRodney W. Grimes  * Kernel profiling passes proc0 which never exits and hence
680df8bae1dSRodney W. Grimes  * keeps the profile clock running constantly.
681df8bae1dSRodney W. Grimes  */
682df8bae1dSRodney W. Grimes void
683df8bae1dSRodney W. Grimes startprofclock(p)
684df8bae1dSRodney W. Grimes 	register struct proc *p;
685df8bae1dSRodney W. Grimes {
686df8bae1dSRodney W. Grimes 
6879752f794SJohn Baldwin 	PROC_LOCK_ASSERT(p, MA_OWNED);
6889752f794SJohn Baldwin 	if (p->p_flag & P_STOPPROF)
689a282253aSJulian Elischer 		return;
6909752f794SJohn Baldwin 	if ((p->p_flag & P_PROFIL) == 0) {
6919752f794SJohn Baldwin 		p->p_flag |= P_PROFIL;
692875b8844SAlexander Motin 		mtx_lock(&time_lock);
693238dd320SJake Burkholder 		if (++profprocs == 1)
694238dd320SJake Burkholder 			cpu_startprofclock();
695875b8844SAlexander Motin 		mtx_unlock(&time_lock);
696df8bae1dSRodney W. Grimes 	}
6979752f794SJohn Baldwin }
698df8bae1dSRodney W. Grimes 
699df8bae1dSRodney W. Grimes /*
700df8bae1dSRodney W. Grimes  * Stop profiling on a process.
701df8bae1dSRodney W. Grimes  */
702df8bae1dSRodney W. Grimes void
703df8bae1dSRodney W. Grimes stopprofclock(p)
704df8bae1dSRodney W. Grimes 	register struct proc *p;
705df8bae1dSRodney W. Grimes {
706df8bae1dSRodney W. Grimes 
707a282253aSJulian Elischer 	PROC_LOCK_ASSERT(p, MA_OWNED);
7089752f794SJohn Baldwin 	if (p->p_flag & P_PROFIL) {
7099752f794SJohn Baldwin 		if (p->p_profthreads != 0) {
7100436fcb8SKonstantin Belousov 			while (p->p_profthreads != 0) {
7119752f794SJohn Baldwin 				p->p_flag |= P_STOPPROF;
712a282253aSJulian Elischer 				msleep(&p->p_profthreads, &p->p_mtx, PPAUSE,
713a89ec05eSPeter Wemm 				    "stopprof", 0);
7140436fcb8SKonstantin Belousov 			}
715a282253aSJulian Elischer 		}
716b62b2304SColin Percival 		if ((p->p_flag & P_PROFIL) == 0)
717b62b2304SColin Percival 			return;
7189752f794SJohn Baldwin 		p->p_flag &= ~P_PROFIL;
719875b8844SAlexander Motin 		mtx_lock(&time_lock);
720238dd320SJake Burkholder 		if (--profprocs == 0)
721238dd320SJake Burkholder 			cpu_stopprofclock();
722875b8844SAlexander Motin 		mtx_unlock(&time_lock);
723df8bae1dSRodney W. Grimes 	}
7249752f794SJohn Baldwin }
725df8bae1dSRodney W. Grimes 
726df8bae1dSRodney W. Grimes /*
7271c4bcd05SJeff Roberson  * Statistics clock.  Updates rusage information and calls the scheduler
7281c4bcd05SJeff Roberson  * to adjust priorities of the active thread.
7291c4bcd05SJeff Roberson  *
730238dd320SJake Burkholder  * This should be called by all active processors.
731df8bae1dSRodney W. Grimes  */
732df8bae1dSRodney W. Grimes void
733b439e431SJohn Baldwin statclock(int usermode)
734df8bae1dSRodney W. Grimes {
735bcfd016cSAlexander Motin 
736bcfd016cSAlexander Motin 	statclock_cnt(1, usermode);
737bcfd016cSAlexander Motin }
738bcfd016cSAlexander Motin 
739bcfd016cSAlexander Motin void
740bcfd016cSAlexander Motin statclock_cnt(int cnt, int usermode)
741bcfd016cSAlexander Motin {
7428a129caeSDavid Greenman 	struct rusage *ru;
7438a129caeSDavid Greenman 	struct vmspace *vm;
744238dd320SJake Burkholder 	struct thread *td;
745238dd320SJake Burkholder 	struct proc *p;
746238dd320SJake Burkholder 	long rss;
7477628402bSPeter Wemm 	long *cp_time;
7488a129caeSDavid Greenman 
749238dd320SJake Burkholder 	td = curthread;
750238dd320SJake Burkholder 	p = td->td_proc;
751238dd320SJake Burkholder 
7527628402bSPeter Wemm 	cp_time = (long *)PCPU_PTR(cp_time);
753b439e431SJohn Baldwin 	if (usermode) {
754df8bae1dSRodney W. Grimes 		/*
75571a62f8aSBruce Evans 		 * Charge the time as appropriate.
756df8bae1dSRodney W. Grimes 		 */
757bcfd016cSAlexander Motin 		td->td_uticks += cnt;
758fa885116SJulian Elischer 		if (p->p_nice > NZERO)
759bcfd016cSAlexander Motin 			cp_time[CP_NICE] += cnt;
760df8bae1dSRodney W. Grimes 		else
761bcfd016cSAlexander Motin 			cp_time[CP_USER] += cnt;
762df8bae1dSRodney W. Grimes 	} else {
763df8bae1dSRodney W. Grimes 		/*
764df8bae1dSRodney W. Grimes 		 * Came from kernel mode, so we were:
765df8bae1dSRodney W. Grimes 		 * - handling an interrupt,
766df8bae1dSRodney W. Grimes 		 * - doing syscall or trap work on behalf of the current
767df8bae1dSRodney W. Grimes 		 *   user process, or
768df8bae1dSRodney W. Grimes 		 * - spinning in the idle loop.
769df8bae1dSRodney W. Grimes 		 * Whichever it is, charge the time as appropriate.
770df8bae1dSRodney W. Grimes 		 * Note that we charge interrupts to the current process,
771df8bae1dSRodney W. Grimes 		 * regardless of whether they are ``for'' that process,
772df8bae1dSRodney W. Grimes 		 * so that we know how much of its real time was spent
773df8bae1dSRodney W. Grimes 		 * in ``non-process'' (i.e., interrupt) work.
774df8bae1dSRodney W. Grimes 		 */
775e0f66ef8SJohn Baldwin 		if ((td->td_pflags & TDP_ITHREAD) ||
776e0f66ef8SJohn Baldwin 		    td->td_intr_nesting_level >= 2) {
777bcfd016cSAlexander Motin 			td->td_iticks += cnt;
778bcfd016cSAlexander Motin 			cp_time[CP_INTR] += cnt;
7790384fff8SJason Evans 		} else {
780bcfd016cSAlexander Motin 			td->td_pticks += cnt;
781bcfd016cSAlexander Motin 			td->td_sticks += cnt;
782486a9414SJulian Elischer 			if (!TD_IS_IDLETHREAD(td))
783bcfd016cSAlexander Motin 				cp_time[CP_SYS] += cnt;
7840384fff8SJason Evans 			else
785bcfd016cSAlexander Motin 				cp_time[CP_IDLE] += cnt;
786df8bae1dSRodney W. Grimes 		}
7870384fff8SJason Evans 	}
788f5e9e8ecSBruce Evans 
789f5e9e8ecSBruce Evans 	/* Update resource usage integrals and maximums. */
79016f9f205SJohn Baldwin 	MPASS(p->p_vmspace != NULL);
79116f9f205SJohn Baldwin 	vm = p->p_vmspace;
7921c4bcd05SJeff Roberson 	ru = &td->td_ru;
793bcfd016cSAlexander Motin 	ru->ru_ixrss += pgtok(vm->vm_tsize) * cnt;
794bcfd016cSAlexander Motin 	ru->ru_idrss += pgtok(vm->vm_dsize) * cnt;
795bcfd016cSAlexander Motin 	ru->ru_isrss += pgtok(vm->vm_ssize) * cnt;
7961c6d46f9SLuoqi Chen 	rss = pgtok(vmspace_resident_count(vm));
797f5e9e8ecSBruce Evans 	if (ru->ru_maxrss < rss)
798f5e9e8ecSBruce Evans 		ru->ru_maxrss = rss;
7998f51ad55SJeff Roberson 	KTR_POINT2(KTR_SCHED, "thread", sched_tdname(td), "statclock",
8008f51ad55SJeff Roberson 	    "prio:%d", td->td_priority, "stathz:%d", (stathz)?stathz:hz);
801b3e9e682SRyan Stone 	SDT_PROBE2(sched, , , tick, td, td->td_proc);
8027628402bSPeter Wemm 	thread_lock_flags(td, MTX_QUIET);
803bcfd016cSAlexander Motin 	for ( ; cnt > 0; cnt--)
80440acdeabSJeff Roberson 		sched_clock(td);
80540acdeabSJeff Roberson 	thread_unlock(td);
806f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
807f5f9340bSFabien Thomas 	if (td->td_intr_frame != NULL)
808f5f9340bSFabien Thomas 		PMC_SOFT_CALL_TF( , , clock, stat, td->td_intr_frame);
809f5f9340bSFabien Thomas #endif
8106caa8a15SJohn Baldwin }
8116c567274SJohn Baldwin 
8126caa8a15SJohn Baldwin void
813b439e431SJohn Baldwin profclock(int usermode, uintfptr_t pc)
8146caa8a15SJohn Baldwin {
815bcfd016cSAlexander Motin 
816bcfd016cSAlexander Motin 	profclock_cnt(1, usermode, pc);
817bcfd016cSAlexander Motin }
818bcfd016cSAlexander Motin 
819bcfd016cSAlexander Motin void
820bcfd016cSAlexander Motin profclock_cnt(int cnt, int usermode, uintfptr_t pc)
821bcfd016cSAlexander Motin {
822238dd320SJake Burkholder 	struct thread *td;
823238dd320SJake Burkholder #ifdef GPROF
824238dd320SJake Burkholder 	struct gmonparam *g;
8255c8b4441SJohn Baldwin 	uintfptr_t i;
826238dd320SJake Burkholder #endif
8276caa8a15SJohn Baldwin 
8284a338afdSJulian Elischer 	td = curthread;
829b439e431SJohn Baldwin 	if (usermode) {
830238dd320SJake Burkholder 		/*
831238dd320SJake Burkholder 		 * Came from user mode; CPU was in user state.
832238dd320SJake Burkholder 		 * If this process is being profiled, record the tick.
833a282253aSJulian Elischer 		 * if there is no related user location yet, don't
834a282253aSJulian Elischer 		 * bother trying to count it.
835238dd320SJake Burkholder 		 */
8369752f794SJohn Baldwin 		if (td->td_proc->p_flag & P_PROFIL)
837bcfd016cSAlexander Motin 			addupc_intr(td, pc, cnt);
838238dd320SJake Burkholder 	}
839238dd320SJake Burkholder #ifdef GPROF
840238dd320SJake Burkholder 	else {
841238dd320SJake Burkholder 		/*
842238dd320SJake Burkholder 		 * Kernel statistics are just like addupc_intr, only easier.
843238dd320SJake Burkholder 		 */
844238dd320SJake Burkholder 		g = &_gmonparam;
845b439e431SJohn Baldwin 		if (g->state == GMON_PROF_ON && pc >= g->lowpc) {
846b439e431SJohn Baldwin 			i = PC_TO_I(g, pc);
847238dd320SJake Burkholder 			if (i < g->textsize) {
848bcfd016cSAlexander Motin 				KCOUNT(g, i) += cnt;
849238dd320SJake Burkholder 			}
850238dd320SJake Burkholder 		}
851238dd320SJake Burkholder 	}
852238dd320SJake Burkholder #endif
8531af19ee4SAlexander Motin #ifdef HWPMC_HOOKS
8541af19ee4SAlexander Motin 	if (td->td_intr_frame != NULL)
8551af19ee4SAlexander Motin 		PMC_SOFT_CALL_TF( , , clock, prof, td->td_intr_frame);
8561af19ee4SAlexander Motin #endif
857df8bae1dSRodney W. Grimes }
858df8bae1dSRodney W. Grimes 
859df8bae1dSRodney W. Grimes /*
860df8bae1dSRodney W. Grimes  * Return information about system clocks.
861df8bae1dSRodney W. Grimes  */
862787d58f2SPoul-Henning Kamp static int
86382d9ae4eSPoul-Henning Kamp sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS)
864df8bae1dSRodney W. Grimes {
865df8bae1dSRodney W. Grimes 	struct clockinfo clkinfo;
866df8bae1dSRodney W. Grimes 	/*
867df8bae1dSRodney W. Grimes 	 * Construct clockinfo structure.
868df8bae1dSRodney W. Grimes 	 */
869a9a0f15aSBruce Evans 	bzero(&clkinfo, sizeof(clkinfo));
870df8bae1dSRodney W. Grimes 	clkinfo.hz = hz;
871df8bae1dSRodney W. Grimes 	clkinfo.tick = tick;
872df8bae1dSRodney W. Grimes 	clkinfo.profhz = profhz;
873df8bae1dSRodney W. Grimes 	clkinfo.stathz = stathz ? stathz : hz;
874ae0eb976SPoul-Henning Kamp 	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
875df8bae1dSRodney W. Grimes }
8763f31c649SGarrett Wollman 
877c383c221SEd Schouten SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate,
878c383c221SEd Schouten 	CTLTYPE_STRUCT|CTLFLAG_RD|CTLFLAG_MPSAFE,
879af1408e3SLuigi Rizzo 	0, 0, sysctl_kern_clockrate, "S,clockinfo",
880af1408e3SLuigi Rizzo 	"Rate and period of various kernel clocks");
881370c3cb5SSean Kelly 
8824103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG
8834103b765SPoul-Henning Kamp 
8844103b765SPoul-Henning Kamp static void
8859079fff5SNick Hibma watchdog_config(void *unused __unused, u_int cmd, int *error)
886370c3cb5SSean Kelly {
8874103b765SPoul-Henning Kamp 	u_int u;
888370c3cb5SSean Kelly 
8894103b765SPoul-Henning Kamp 	u = cmd & WD_INTERVAL;
8909079fff5SNick Hibma 	if (u >= WD_TO_1SEC) {
8914103b765SPoul-Henning Kamp 		watchdog_ticks = (1 << (u - WD_TO_1SEC)) * hz;
8924103b765SPoul-Henning Kamp 		watchdog_enabled = 1;
8939079fff5SNick Hibma 		*error = 0;
8944103b765SPoul-Henning Kamp 	} else {
8954103b765SPoul-Henning Kamp 		watchdog_enabled = 0;
896370c3cb5SSean Kelly 	}
8974103b765SPoul-Henning Kamp }
898370c3cb5SSean Kelly 
899370c3cb5SSean Kelly /*
900370c3cb5SSean Kelly  * Handle a watchdog timeout by dumping interrupt information and
901911d16b8SEd Maste  * then either dropping to DDB or panicking.
902370c3cb5SSean Kelly  */
903370c3cb5SSean Kelly static void
904370c3cb5SSean Kelly watchdog_fire(void)
905370c3cb5SSean Kelly {
906370c3cb5SSean Kelly 	int nintr;
90760ae52f7SEd Schouten 	uint64_t inttotal;
908370c3cb5SSean Kelly 	u_long *curintr;
909370c3cb5SSean Kelly 	char *curname;
910370c3cb5SSean Kelly 
911370c3cb5SSean Kelly 	curintr = intrcnt;
912370c3cb5SSean Kelly 	curname = intrnames;
913370c3cb5SSean Kelly 	inttotal = 0;
914556a5850SAlexander Motin 	nintr = sintrcnt / sizeof(u_long);
915370c3cb5SSean Kelly 
916370c3cb5SSean Kelly 	printf("interrupt                   total\n");
917370c3cb5SSean Kelly 	while (--nintr >= 0) {
918370c3cb5SSean Kelly 		if (*curintr)
919370c3cb5SSean Kelly 			printf("%-12s %20lu\n", curname, *curintr);
920370c3cb5SSean Kelly 		curname += strlen(curname) + 1;
921370c3cb5SSean Kelly 		inttotal += *curintr++;
922370c3cb5SSean Kelly 	}
9236cda4155SSean Kelly 	printf("Total        %20ju\n", (uintmax_t)inttotal);
924911d16b8SEd Maste 
925911d16b8SEd Maste #if defined(KDB) && !defined(KDB_UNATTENDED)
926911d16b8SEd Maste 	kdb_backtrace();
9273de213ccSRobert Watson 	kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout");
928911d16b8SEd Maste #else
929370c3cb5SSean Kelly 	panic("watchdog timeout");
930911d16b8SEd Maste #endif
931370c3cb5SSean Kelly }
932370c3cb5SSean Kelly 
9334103b765SPoul-Henning Kamp #endif /* SW_WATCHDOG */
934