1df8bae1dSRodney W. Grimes /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * (c) UNIX System Laboratories, Inc. 5df8bae1dSRodney W. Grimes * All or some portions of this file are derived from material licensed 6df8bae1dSRodney W. Grimes * to the University of California by American Telephone and Telegraph 7df8bae1dSRodney W. Grimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8df8bae1dSRodney W. Grimes * the permission of UNIX System Laboratories, Inc. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37677b542eSDavid E. O'Brien #include <sys/cdefs.h> 38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 39677b542eSDavid E. O'Brien 40911d16b8SEd Maste #include "opt_kdb.h" 41f0796cd2SGleb Smirnoff #include "opt_device_polling.h" 424da0d332SPeter Wemm #include "opt_hwpmc_hooks.h" 4332c20357SPoul-Henning Kamp #include "opt_ntp.h" 44370c3cb5SSean Kelly #include "opt_watchdog.h" 4532c20357SPoul-Henning Kamp 46df8bae1dSRodney W. Grimes #include <sys/param.h> 47df8bae1dSRodney W. Grimes #include <sys/systm.h> 48df8bae1dSRodney W. Grimes #include <sys/callout.h> 492d50560aSMarcel Moolenaar #include <sys/kdb.h> 50df8bae1dSRodney W. Grimes #include <sys/kernel.h> 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> 59797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 60f7829d0dSAttilio Rao #include <sys/sleepqueue.h> 616caa8a15SJohn Baldwin #include <sys/smp.h> 628a129caeSDavid Greenman #include <vm/vm.h> 63efeaf95aSDavid Greenman #include <vm/pmap.h> 64efeaf95aSDavid Greenman #include <vm/vm_map.h> 65797f2d22SPoul-Henning Kamp #include <sys/sysctl.h> 668088699fSJohn Baldwin #include <sys/bus.h> 678088699fSJohn Baldwin #include <sys/interrupt.h> 68104a9b7eSAlexander Kabaev #include <sys/limits.h> 69e7fa55afSPoul-Henning Kamp #include <sys/timetc.h> 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes #ifdef GPROF 72df8bae1dSRodney W. Grimes #include <sys/gmon.h> 73df8bae1dSRodney W. Grimes #endif 74df8bae1dSRodney W. Grimes 7536c0fd9dSJoseph Koshy #ifdef HWPMC_HOOKS 7636c0fd9dSJoseph Koshy #include <sys/pmckern.h> 7736c0fd9dSJoseph Koshy #endif 7836c0fd9dSJoseph Koshy 79e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 80e4fc250cSLuigi Rizzo extern void hardclock_device_poll(void); 81e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */ 82eae8fc2cSSteve Passe 834d77a549SAlfred Perlstein static void initclocks(void *dummy); 84237fdd78SRobert Watson SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL); 852b14f991SJulian Elischer 868b98fec9SJeff Roberson /* Spin-lock protecting profiling statistics. */ 8786a49deaSAttilio Rao static struct mtx time_lock; 888b98fec9SJeff Roberson 8962919d78SPeter Wemm static int 9062919d78SPeter Wemm sysctl_kern_cp_time(SYSCTL_HANDLER_ARGS) 9162919d78SPeter Wemm { 9262919d78SPeter Wemm int error; 937628402bSPeter Wemm long cp_time[CPUSTATES]; 94cff2e749SPaul Saab #ifdef SCTL_MASK32 9562919d78SPeter Wemm int i; 9662919d78SPeter Wemm unsigned int cp_time32[CPUSTATES]; 977628402bSPeter Wemm #endif 9862919d78SPeter Wemm 997628402bSPeter Wemm read_cpu_time(cp_time); 1007628402bSPeter Wemm #ifdef SCTL_MASK32 101cff2e749SPaul Saab if (req->flags & SCTL_MASK32) { 10262919d78SPeter Wemm if (!req->oldptr) 10362919d78SPeter Wemm return SYSCTL_OUT(req, 0, sizeof(cp_time32)); 10462919d78SPeter Wemm for (i = 0; i < CPUSTATES; i++) 10562919d78SPeter Wemm cp_time32[i] = (unsigned int)cp_time[i]; 10662919d78SPeter Wemm error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32)); 10762919d78SPeter Wemm } else 10862919d78SPeter Wemm #endif 10962919d78SPeter Wemm { 11062919d78SPeter Wemm if (!req->oldptr) 11162919d78SPeter Wemm return SYSCTL_OUT(req, 0, sizeof(cp_time)); 11262919d78SPeter Wemm error = SYSCTL_OUT(req, cp_time, sizeof(cp_time)); 11362919d78SPeter Wemm } 11462919d78SPeter Wemm return error; 11562919d78SPeter Wemm } 11662919d78SPeter Wemm 117c383c221SEd Schouten SYSCTL_PROC(_kern, OID_AUTO, cp_time, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE, 11862919d78SPeter Wemm 0,0, sysctl_kern_cp_time, "LU", "CPU time statistics"); 1197f112b04SRobert Watson 1207628402bSPeter Wemm static long empty[CPUSTATES]; 1217628402bSPeter Wemm 1227628402bSPeter Wemm static int 1237628402bSPeter Wemm sysctl_kern_cp_times(SYSCTL_HANDLER_ARGS) 1247628402bSPeter Wemm { 1257628402bSPeter Wemm struct pcpu *pcpu; 1267628402bSPeter Wemm int error; 127ef54068bSRobert Watson int c; 1287628402bSPeter Wemm long *cp_time; 1297628402bSPeter Wemm #ifdef SCTL_MASK32 1307628402bSPeter Wemm unsigned int cp_time32[CPUSTATES]; 131ef54068bSRobert Watson int i; 1327628402bSPeter Wemm #endif 1337628402bSPeter Wemm 1347628402bSPeter Wemm if (!req->oldptr) { 1357628402bSPeter Wemm #ifdef SCTL_MASK32 1367628402bSPeter Wemm if (req->flags & SCTL_MASK32) 1377628402bSPeter Wemm return SYSCTL_OUT(req, 0, sizeof(cp_time32) * (mp_maxid + 1)); 1387628402bSPeter Wemm else 1397628402bSPeter Wemm #endif 1407628402bSPeter Wemm return SYSCTL_OUT(req, 0, sizeof(long) * CPUSTATES * (mp_maxid + 1)); 1417628402bSPeter Wemm } 1427628402bSPeter Wemm for (error = 0, c = 0; error == 0 && c <= mp_maxid; c++) { 1437628402bSPeter Wemm if (!CPU_ABSENT(c)) { 1447628402bSPeter Wemm pcpu = pcpu_find(c); 1457628402bSPeter Wemm cp_time = pcpu->pc_cp_time; 1467628402bSPeter Wemm } else { 1477628402bSPeter Wemm cp_time = empty; 1487628402bSPeter Wemm } 1497628402bSPeter Wemm #ifdef SCTL_MASK32 1507628402bSPeter Wemm if (req->flags & SCTL_MASK32) { 1517628402bSPeter Wemm for (i = 0; i < CPUSTATES; i++) 1527628402bSPeter Wemm cp_time32[i] = (unsigned int)cp_time[i]; 1537628402bSPeter Wemm error = SYSCTL_OUT(req, cp_time32, sizeof(cp_time32)); 1547628402bSPeter Wemm } else 1557628402bSPeter Wemm #endif 1567628402bSPeter Wemm error = SYSCTL_OUT(req, cp_time, sizeof(long) * CPUSTATES); 1577628402bSPeter Wemm } 1587628402bSPeter Wemm return error; 1597628402bSPeter Wemm } 1607628402bSPeter Wemm 161c383c221SEd Schouten SYSCTL_PROC(_kern, OID_AUTO, cp_times, CTLTYPE_LONG|CTLFLAG_RD|CTLFLAG_MPSAFE, 1627628402bSPeter Wemm 0,0, sysctl_kern_cp_times, "LU", "per-CPU time statistics"); 1637628402bSPeter Wemm 164f7829d0dSAttilio Rao #ifdef DEADLKRES 16536e51f65SAttilio Rao static const char *blessed[] = { 16695335fd8SAttilio Rao "getblk", 16736e51f65SAttilio Rao "so_snd_sx", 16836e51f65SAttilio Rao "so_rcv_sx", 16936e51f65SAttilio Rao NULL 17036e51f65SAttilio Rao }; 171f7829d0dSAttilio Rao static int slptime_threshold = 1800; 172f7829d0dSAttilio Rao static int blktime_threshold = 900; 173f7829d0dSAttilio Rao static int sleepfreq = 3; 174f7829d0dSAttilio Rao 175f7829d0dSAttilio Rao static void 176f7829d0dSAttilio Rao deadlkres(void) 177f7829d0dSAttilio Rao { 178f7829d0dSAttilio Rao struct proc *p; 179f7829d0dSAttilio Rao struct thread *td; 180f7829d0dSAttilio Rao void *wchan; 18136e51f65SAttilio Rao int blkticks, i, slpticks, slptype, tryl, tticks; 182f7829d0dSAttilio Rao 183f7829d0dSAttilio Rao tryl = 0; 184f7829d0dSAttilio Rao for (;;) { 185f7829d0dSAttilio Rao blkticks = blktime_threshold * hz; 186f7829d0dSAttilio Rao slpticks = slptime_threshold * hz; 187f7829d0dSAttilio Rao 188f7829d0dSAttilio Rao /* 189f7829d0dSAttilio Rao * Avoid to sleep on the sx_lock in order to avoid a possible 190f7829d0dSAttilio Rao * priority inversion problem leading to starvation. 191f7829d0dSAttilio Rao * If the lock can't be held after 100 tries, panic. 192f7829d0dSAttilio Rao */ 193f7829d0dSAttilio Rao if (!sx_try_slock(&allproc_lock)) { 194f7829d0dSAttilio Rao if (tryl > 100) 195f7829d0dSAttilio Rao panic("%s: possible deadlock detected on allproc_lock\n", 196f7829d0dSAttilio Rao __func__); 197f7829d0dSAttilio Rao tryl++; 198f7829d0dSAttilio Rao pause("allproc_lock deadlkres", sleepfreq * hz); 199f7829d0dSAttilio Rao continue; 200f7829d0dSAttilio Rao } 201f7829d0dSAttilio Rao tryl = 0; 202f7829d0dSAttilio Rao FOREACH_PROC_IN_SYSTEM(p) { 203f7829d0dSAttilio Rao PROC_LOCK(p); 204f7829d0dSAttilio Rao FOREACH_THREAD_IN_PROC(p, td) { 205f7829d0dSAttilio Rao thread_lock(td); 206f7829d0dSAttilio Rao if (TD_ON_LOCK(td)) { 207f7829d0dSAttilio Rao 208f7829d0dSAttilio Rao /* 209f7829d0dSAttilio Rao * The thread should be blocked on a 210f7829d0dSAttilio Rao * turnstile, simply check if the 211f7829d0dSAttilio Rao * turnstile channel is in good state. 212f7829d0dSAttilio Rao */ 213f7829d0dSAttilio Rao MPASS(td->td_blocked != NULL); 21436e51f65SAttilio Rao 21536e51f65SAttilio Rao /* Handle ticks wrap-up. */ 21636e51f65SAttilio Rao if (ticks < td->td_blktick) 21736e51f65SAttilio Rao continue; 218f7829d0dSAttilio Rao tticks = ticks - td->td_blktick; 219f7829d0dSAttilio Rao thread_unlock(td); 220f7829d0dSAttilio Rao if (tticks > blkticks) { 221f7829d0dSAttilio Rao 222f7829d0dSAttilio Rao /* 223f7829d0dSAttilio Rao * Accordingly with provided 224f7829d0dSAttilio Rao * thresholds, this thread is 225f7829d0dSAttilio Rao * stuck for too long on a 226f7829d0dSAttilio Rao * turnstile. 227f7829d0dSAttilio Rao */ 228f7829d0dSAttilio Rao PROC_UNLOCK(p); 229f7829d0dSAttilio Rao sx_sunlock(&allproc_lock); 230f7829d0dSAttilio Rao panic("%s: possible deadlock detected for %p, blocked for %d ticks\n", 231f7829d0dSAttilio Rao __func__, td, tticks); 232f7829d0dSAttilio Rao } 233f7829d0dSAttilio Rao } else if (TD_IS_SLEEPING(td)) { 234f7829d0dSAttilio Rao 23536e51f65SAttilio Rao /* Handle ticks wrap-up. */ 23636e51f65SAttilio Rao if (ticks < td->td_blktick) 23736e51f65SAttilio Rao continue; 23836e51f65SAttilio Rao 239f7829d0dSAttilio Rao /* 240f7829d0dSAttilio Rao * Check if the thread is sleeping on a 241f7829d0dSAttilio Rao * lock, otherwise skip the check. 242f7829d0dSAttilio Rao * Drop the thread lock in order to 243f7829d0dSAttilio Rao * avoid a LOR with the sleepqueue 244f7829d0dSAttilio Rao * spinlock. 245f7829d0dSAttilio Rao */ 246f7829d0dSAttilio Rao wchan = td->td_wchan; 247f7829d0dSAttilio Rao tticks = ticks - td->td_slptick; 248f7829d0dSAttilio Rao thread_unlock(td); 249f7829d0dSAttilio Rao slptype = sleepq_type(wchan); 250f7829d0dSAttilio Rao if ((slptype == SLEEPQ_SX || 251f7829d0dSAttilio Rao slptype == SLEEPQ_LK) && 252f7829d0dSAttilio Rao tticks > slpticks) { 253f7829d0dSAttilio Rao 254f7829d0dSAttilio Rao /* 255f7829d0dSAttilio Rao * Accordingly with provided 256f7829d0dSAttilio Rao * thresholds, this thread is 257f7829d0dSAttilio Rao * stuck for too long on a 258f7829d0dSAttilio Rao * sleepqueue. 25936e51f65SAttilio Rao * However, being on a 26036e51f65SAttilio Rao * sleepqueue, we might still 26136e51f65SAttilio Rao * check for the blessed 26236e51f65SAttilio Rao * list. 263f7829d0dSAttilio Rao */ 26436e51f65SAttilio Rao tryl = 0; 26536e51f65SAttilio Rao for (i = 0; blessed[i] != NULL; 26636e51f65SAttilio Rao i++) { 26736e51f65SAttilio Rao if (!strcmp(blessed[i], 26836e51f65SAttilio Rao td->td_wmesg)) { 26936e51f65SAttilio Rao tryl = 1; 27036e51f65SAttilio Rao break; 27136e51f65SAttilio Rao } 27236e51f65SAttilio Rao } 27336e51f65SAttilio Rao if (tryl != 0) { 27436e51f65SAttilio Rao tryl = 0; 27536e51f65SAttilio Rao continue; 27636e51f65SAttilio Rao } 277f7829d0dSAttilio Rao PROC_UNLOCK(p); 278f7829d0dSAttilio Rao sx_sunlock(&allproc_lock); 279f7829d0dSAttilio Rao panic("%s: possible deadlock detected for %p, blocked for %d ticks\n", 280f7829d0dSAttilio Rao __func__, td, tticks); 281f7829d0dSAttilio Rao } 282f7829d0dSAttilio Rao } else 283f7829d0dSAttilio Rao thread_unlock(td); 284f7829d0dSAttilio Rao } 285f7829d0dSAttilio Rao PROC_UNLOCK(p); 286f7829d0dSAttilio Rao } 287f7829d0dSAttilio Rao sx_sunlock(&allproc_lock); 288f7829d0dSAttilio Rao 289f7829d0dSAttilio Rao /* Sleep for sleepfreq seconds. */ 290f7829d0dSAttilio Rao pause("deadlkres", sleepfreq * hz); 291f7829d0dSAttilio Rao } 292f7829d0dSAttilio Rao } 293f7829d0dSAttilio Rao 294f7829d0dSAttilio Rao static struct kthread_desc deadlkres_kd = { 295f7829d0dSAttilio Rao "deadlkres", 296f7829d0dSAttilio Rao deadlkres, 297f7829d0dSAttilio Rao (struct thread **)NULL 298f7829d0dSAttilio Rao }; 299f7829d0dSAttilio Rao 300f7829d0dSAttilio Rao SYSINIT(deadlkres, SI_SUB_CLOCKS, SI_ORDER_ANY, kthread_start, &deadlkres_kd); 301f7829d0dSAttilio Rao 302f7829d0dSAttilio Rao SYSCTL_NODE(_debug, OID_AUTO, deadlkres, CTLFLAG_RW, 0, "Deadlock resolver"); 303f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, slptime_threshold, CTLFLAG_RW, 304f7829d0dSAttilio Rao &slptime_threshold, 0, 305f7829d0dSAttilio Rao "Number of seconds within is valid to sleep on a sleepqueue"); 306f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, blktime_threshold, CTLFLAG_RW, 307f7829d0dSAttilio Rao &blktime_threshold, 0, 308f7829d0dSAttilio Rao "Number of seconds within is valid to block on a turnstile"); 309f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, sleepfreq, CTLFLAG_RW, &sleepfreq, 0, 310f7829d0dSAttilio Rao "Number of seconds between any deadlock resolver thread run"); 311f7829d0dSAttilio Rao #endif /* DEADLKRES */ 312f7829d0dSAttilio Rao 3137628402bSPeter Wemm void 3147628402bSPeter Wemm read_cpu_time(long *cp_time) 3157628402bSPeter Wemm { 3167628402bSPeter Wemm struct pcpu *pc; 3177628402bSPeter Wemm int i, j; 3187628402bSPeter Wemm 3197628402bSPeter Wemm /* Sum up global cp_time[]. */ 3207628402bSPeter Wemm bzero(cp_time, sizeof(long) * CPUSTATES); 321*3aa6d94eSJohn Baldwin CPU_FOREACH(i) { 3227628402bSPeter Wemm pc = pcpu_find(i); 3237628402bSPeter Wemm for (j = 0; j < CPUSTATES; j++) 3247628402bSPeter Wemm cp_time[j] += pc->pc_cp_time[j]; 3257628402bSPeter Wemm } 3267628402bSPeter Wemm } 3277628402bSPeter Wemm 3284103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG 3294103b765SPoul-Henning Kamp #include <sys/watchdog.h> 330370c3cb5SSean Kelly 3314103b765SPoul-Henning Kamp static int watchdog_ticks; 332370c3cb5SSean Kelly static int watchdog_enabled; 3334103b765SPoul-Henning Kamp static void watchdog_fire(void); 3344103b765SPoul-Henning Kamp static void watchdog_config(void *, u_int, int *); 3354103b765SPoul-Henning Kamp #endif /* SW_WATCHDOG */ 336370c3cb5SSean Kelly 3373bac064fSPoul-Henning Kamp /* 338df8bae1dSRodney W. Grimes * Clock handling routines. 339df8bae1dSRodney W. Grimes * 340b05dcf3cSPoul-Henning Kamp * This code is written to operate with two timers that run independently of 341b05dcf3cSPoul-Henning Kamp * each other. 3427ec73f64SPoul-Henning Kamp * 343b05dcf3cSPoul-Henning Kamp * The main timer, running hz times per second, is used to trigger interval 344b05dcf3cSPoul-Henning Kamp * timers, timeouts and rescheduling as needed. 3457ec73f64SPoul-Henning Kamp * 346b05dcf3cSPoul-Henning Kamp * The second timer handles kernel and user profiling, 347b05dcf3cSPoul-Henning Kamp * and does resource use estimation. If the second timer is programmable, 348b05dcf3cSPoul-Henning Kamp * it is randomized to avoid aliasing between the two clocks. For example, 349b05dcf3cSPoul-Henning Kamp * the randomization prevents an adversary from always giving up the cpu 350df8bae1dSRodney W. Grimes * just before its quantum expires. Otherwise, it would never accumulate 351df8bae1dSRodney W. Grimes * cpu ticks. The mean frequency of the second timer is stathz. 352b05dcf3cSPoul-Henning Kamp * 353b05dcf3cSPoul-Henning Kamp * If no second timer exists, stathz will be zero; in this case we drive 354b05dcf3cSPoul-Henning Kamp * profiling and statistics off the main clock. This WILL NOT be accurate; 355b05dcf3cSPoul-Henning Kamp * do not do it unless absolutely necessary. 356b05dcf3cSPoul-Henning Kamp * 357df8bae1dSRodney W. Grimes * The statistics clock may (or may not) be run at a higher rate while 358b05dcf3cSPoul-Henning Kamp * profiling. This profile clock runs at profhz. We require that profhz 359b05dcf3cSPoul-Henning Kamp * be an integral multiple of stathz. 360b05dcf3cSPoul-Henning Kamp * 361b05dcf3cSPoul-Henning Kamp * If the statistics clock is running fast, it must be divided by the ratio 362b05dcf3cSPoul-Henning Kamp * profhz/stathz for statistics. (For profiling, every tick counts.) 363df8bae1dSRodney W. Grimes * 3647ec73f64SPoul-Henning Kamp * Time-of-day is maintained using a "timecounter", which may or may 3657ec73f64SPoul-Henning Kamp * not be related to the hardware generating the above mentioned 3667ec73f64SPoul-Henning Kamp * interrupts. 367df8bae1dSRodney W. Grimes */ 368df8bae1dSRodney W. Grimes 369df8bae1dSRodney W. Grimes int stathz; 370df8bae1dSRodney W. Grimes int profhz; 371238dd320SJake Burkholder int profprocs; 372df8bae1dSRodney W. Grimes int ticks; 373238dd320SJake Burkholder int psratio; 374df8bae1dSRodney W. Grimes 375dbd55f3fSAlexander Motin int timer1hz; 376dbd55f3fSAlexander Motin int timer2hz; 377dbd55f3fSAlexander Motin static DPCPU_DEFINE(u_int, hard_cnt); 378dbd55f3fSAlexander Motin static DPCPU_DEFINE(u_int, stat_cnt); 379dbd55f3fSAlexander Motin static DPCPU_DEFINE(u_int, prof_cnt); 380dbd55f3fSAlexander Motin 381df8bae1dSRodney W. Grimes /* 382df8bae1dSRodney W. Grimes * Initialize clock frequencies and start both clocks running. 383df8bae1dSRodney W. Grimes */ 3842b14f991SJulian Elischer /* ARGSUSED*/ 3852b14f991SJulian Elischer static void 386d841aaa7SBruce Evans initclocks(dummy) 387d841aaa7SBruce Evans void *dummy; 388df8bae1dSRodney W. Grimes { 389df8bae1dSRodney W. Grimes register int i; 390df8bae1dSRodney W. Grimes 391df8bae1dSRodney W. Grimes /* 392df8bae1dSRodney W. Grimes * Set divisors to 1 (normal case) and let the machine-specific 393df8bae1dSRodney W. Grimes * code do its bit. 394df8bae1dSRodney W. Grimes */ 3958b98fec9SJeff Roberson mtx_init(&time_lock, "time lock", NULL, MTX_SPIN); 39663d69d25SRobert Watson cpu_initclocks(); 397df8bae1dSRodney W. Grimes 398df8bae1dSRodney W. Grimes /* 399df8bae1dSRodney W. Grimes * Compute profhz/stathz, and fix profhz if needed. 400df8bae1dSRodney W. Grimes */ 401df8bae1dSRodney W. Grimes i = stathz ? stathz : hz; 402df8bae1dSRodney W. Grimes if (profhz == 0) 403df8bae1dSRodney W. Grimes profhz = i; 404df8bae1dSRodney W. Grimes psratio = profhz / i; 4054103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG 4064103b765SPoul-Henning Kamp EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0); 4074103b765SPoul-Henning Kamp #endif 408df8bae1dSRodney W. Grimes } 409df8bae1dSRodney W. Grimes 410dbd55f3fSAlexander Motin void 411dbd55f3fSAlexander Motin timer1clock(int usermode, uintfptr_t pc) 412dbd55f3fSAlexander Motin { 413dbd55f3fSAlexander Motin u_int *cnt; 414dbd55f3fSAlexander Motin 415dbd55f3fSAlexander Motin cnt = DPCPU_PTR(hard_cnt); 416dbd55f3fSAlexander Motin *cnt += hz; 417dbd55f3fSAlexander Motin if (*cnt >= timer1hz) { 418dbd55f3fSAlexander Motin *cnt -= timer1hz; 419dbd55f3fSAlexander Motin if (*cnt >= timer1hz) 420dbd55f3fSAlexander Motin *cnt = 0; 421dbd55f3fSAlexander Motin if (PCPU_GET(cpuid) == 0) 422dbd55f3fSAlexander Motin hardclock(usermode, pc); 423dbd55f3fSAlexander Motin else 424dbd55f3fSAlexander Motin hardclock_cpu(usermode); 425dbd55f3fSAlexander Motin } 426dbd55f3fSAlexander Motin if (timer2hz == 0) 427dbd55f3fSAlexander Motin timer2clock(usermode, pc); 428dbd55f3fSAlexander Motin } 429dbd55f3fSAlexander Motin 430dbd55f3fSAlexander Motin void 431dbd55f3fSAlexander Motin timer2clock(int usermode, uintfptr_t pc) 432dbd55f3fSAlexander Motin { 433dbd55f3fSAlexander Motin u_int *cnt; 434dbd55f3fSAlexander Motin int t2hz = timer2hz ? timer2hz : timer1hz; 435dbd55f3fSAlexander Motin 436dbd55f3fSAlexander Motin cnt = DPCPU_PTR(stat_cnt); 437dbd55f3fSAlexander Motin *cnt += stathz; 438dbd55f3fSAlexander Motin if (*cnt >= t2hz) { 439dbd55f3fSAlexander Motin *cnt -= t2hz; 440dbd55f3fSAlexander Motin if (*cnt >= t2hz) 441dbd55f3fSAlexander Motin *cnt = 0; 442dbd55f3fSAlexander Motin statclock(usermode); 443dbd55f3fSAlexander Motin } 444dbd55f3fSAlexander Motin if (profprocs == 0) 445dbd55f3fSAlexander Motin return; 446dbd55f3fSAlexander Motin cnt = DPCPU_PTR(prof_cnt); 447dbd55f3fSAlexander Motin *cnt += profhz; 448dbd55f3fSAlexander Motin if (*cnt >= t2hz) { 449dbd55f3fSAlexander Motin *cnt -= t2hz; 450dbd55f3fSAlexander Motin if (*cnt >= t2hz) 451dbd55f3fSAlexander Motin *cnt = 0; 452dbd55f3fSAlexander Motin profclock(usermode, pc); 453dbd55f3fSAlexander Motin } 454dbd55f3fSAlexander Motin } 455dbd55f3fSAlexander Motin 456df8bae1dSRodney W. Grimes /* 457238dd320SJake Burkholder * Each time the real-time timer fires, this function is called on all CPUs. 458b439e431SJohn Baldwin * Note that hardclock() calls hardclock_cpu() for the boot CPU, so only 459238dd320SJake Burkholder * the other CPUs in the system need to call this function. 4606caa8a15SJohn Baldwin */ 4616caa8a15SJohn Baldwin void 462b439e431SJohn Baldwin hardclock_cpu(int usermode) 4636caa8a15SJohn Baldwin { 4646caa8a15SJohn Baldwin struct pstats *pstats; 465238dd320SJake Burkholder struct thread *td = curthread; 466b40ce416SJulian Elischer struct proc *p = td->td_proc; 467b61ce5b0SJeff Roberson int flags; 4686caa8a15SJohn Baldwin 4696caa8a15SJohn Baldwin /* 4706caa8a15SJohn Baldwin * Run current process's virtual and profile time, as needed. 4716caa8a15SJohn Baldwin */ 472ad1e7d28SJulian Elischer pstats = p->p_stats; 473b61ce5b0SJeff Roberson flags = 0; 474ad1e7d28SJulian Elischer if (usermode && 47540acdeabSJeff Roberson timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) { 47640acdeabSJeff Roberson PROC_SLOCK(p); 477b61ce5b0SJeff Roberson if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) 478b61ce5b0SJeff Roberson flags |= TDF_ALRMPEND | TDF_ASTPENDING; 47940acdeabSJeff Roberson PROC_SUNLOCK(p); 48040acdeabSJeff Roberson } 48140acdeabSJeff Roberson if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) { 48240acdeabSJeff Roberson PROC_SLOCK(p); 483b61ce5b0SJeff Roberson if (itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) 484b61ce5b0SJeff Roberson flags |= TDF_PROFPEND | TDF_ASTPENDING; 48540acdeabSJeff Roberson PROC_SUNLOCK(p); 48640acdeabSJeff Roberson } 48740acdeabSJeff Roberson thread_lock(td); 48840acdeabSJeff Roberson sched_tick(); 489b61ce5b0SJeff Roberson td->td_flags |= flags; 49040acdeabSJeff Roberson thread_unlock(td); 49136c0fd9dSJoseph Koshy 49236c0fd9dSJoseph Koshy #ifdef HWPMC_HOOKS 49336c0fd9dSJoseph Koshy if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid))) 49436c0fd9dSJoseph Koshy PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL); 49536c0fd9dSJoseph Koshy #endif 4968d809d50SJeff Roberson callout_tick(); 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 5068d809d50SJeff Roberson atomic_add_int((volatile int *)&ticks, 1); 507b439e431SJohn Baldwin hardclock_cpu(usermode); 508e7fa55afSPoul-Henning Kamp tc_ticktock(); 509df8bae1dSRodney W. Grimes /* 510df8bae1dSRodney W. Grimes * If no separate statistics clock is available, run it from here. 5116caa8a15SJohn Baldwin * 5126caa8a15SJohn Baldwin * XXX: this only works for UP 513df8bae1dSRodney W. Grimes */ 514238dd320SJake Burkholder if (stathz == 0) { 515b439e431SJohn Baldwin profclock(usermode, pc); 516b439e431SJohn Baldwin statclock(usermode); 517238dd320SJake Burkholder } 518e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 519daccb638SLuigi Rizzo hardclock_device_poll(); /* this is very short and quick */ 520e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */ 5214103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG 5224103b765SPoul-Henning Kamp if (watchdog_enabled > 0 && --watchdog_ticks <= 0) 523370c3cb5SSean Kelly watchdog_fire(); 5244103b765SPoul-Henning Kamp #endif /* SW_WATCHDOG */ 525ab36c067SJustin T. Gibbs } 526ab36c067SJustin T. Gibbs 527df8bae1dSRodney W. Grimes /* 528227ee8a1SPoul-Henning Kamp * Compute number of ticks in the specified amount of time. 529df8bae1dSRodney W. Grimes */ 530df8bae1dSRodney W. Grimes int 531227ee8a1SPoul-Henning Kamp tvtohz(tv) 532df8bae1dSRodney W. Grimes struct timeval *tv; 533df8bae1dSRodney W. Grimes { 5346976af69SBruce Evans register unsigned long ticks; 5356976af69SBruce Evans register long sec, usec; 536df8bae1dSRodney W. Grimes 537df8bae1dSRodney W. Grimes /* 5386976af69SBruce Evans * If the number of usecs in the whole seconds part of the time 5396976af69SBruce Evans * difference fits in a long, then the total number of usecs will 5406976af69SBruce Evans * fit in an unsigned long. Compute the total and convert it to 5416976af69SBruce Evans * ticks, rounding up and adding 1 to allow for the current tick 5426976af69SBruce Evans * to expire. Rounding also depends on unsigned long arithmetic 5436976af69SBruce Evans * to avoid overflow. 544df8bae1dSRodney W. Grimes * 5456976af69SBruce Evans * Otherwise, if the number of ticks in the whole seconds part of 5466976af69SBruce Evans * the time difference fits in a long, then convert the parts to 5476976af69SBruce Evans * ticks separately and add, using similar rounding methods and 5486976af69SBruce Evans * overflow avoidance. This method would work in the previous 5496976af69SBruce Evans * case but it is slightly slower and assumes that hz is integral. 5506976af69SBruce Evans * 5516976af69SBruce Evans * Otherwise, round the time difference down to the maximum 5526976af69SBruce Evans * representable value. 5536976af69SBruce Evans * 5546976af69SBruce Evans * If ints have 32 bits, then the maximum value for any timeout in 5556976af69SBruce Evans * 10ms ticks is 248 days. 556df8bae1dSRodney W. Grimes */ 557227ee8a1SPoul-Henning Kamp sec = tv->tv_sec; 558227ee8a1SPoul-Henning Kamp usec = tv->tv_usec; 5596976af69SBruce Evans if (usec < 0) { 5606976af69SBruce Evans sec--; 5616976af69SBruce Evans usec += 1000000; 5626976af69SBruce Evans } 5636976af69SBruce Evans if (sec < 0) { 5646976af69SBruce Evans #ifdef DIAGNOSTIC 565b05dcf3cSPoul-Henning Kamp if (usec > 0) { 5667ec73f64SPoul-Henning Kamp sec++; 5677ec73f64SPoul-Henning Kamp usec -= 1000000; 5687ec73f64SPoul-Henning Kamp } 569227ee8a1SPoul-Henning Kamp printf("tvotohz: negative time difference %ld sec %ld usec\n", 5706976af69SBruce Evans sec, usec); 5716976af69SBruce Evans #endif 5726976af69SBruce Evans ticks = 1; 5736976af69SBruce Evans } else if (sec <= LONG_MAX / 1000000) 5746976af69SBruce Evans ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1)) 5756976af69SBruce Evans / tick + 1; 5766976af69SBruce Evans else if (sec <= LONG_MAX / hz) 5776976af69SBruce Evans ticks = sec * hz 5786976af69SBruce Evans + ((unsigned long)usec + (tick - 1)) / tick + 1; 5796976af69SBruce Evans else 5806976af69SBruce Evans ticks = LONG_MAX; 5816976af69SBruce Evans if (ticks > INT_MAX) 5826976af69SBruce Evans ticks = INT_MAX; 583d6116663SAlexander Langer return ((int)ticks); 584df8bae1dSRodney W. Grimes } 585df8bae1dSRodney W. Grimes 586df8bae1dSRodney W. Grimes /* 587df8bae1dSRodney W. Grimes * Start profiling on a process. 588df8bae1dSRodney W. Grimes * 589df8bae1dSRodney W. Grimes * Kernel profiling passes proc0 which never exits and hence 590df8bae1dSRodney W. Grimes * keeps the profile clock running constantly. 591df8bae1dSRodney W. Grimes */ 592df8bae1dSRodney W. Grimes void 593df8bae1dSRodney W. Grimes startprofclock(p) 594df8bae1dSRodney W. Grimes register struct proc *p; 595df8bae1dSRodney W. Grimes { 596df8bae1dSRodney W. Grimes 5979752f794SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 5989752f794SJohn Baldwin if (p->p_flag & P_STOPPROF) 599a282253aSJulian Elischer return; 6009752f794SJohn Baldwin if ((p->p_flag & P_PROFIL) == 0) { 6019752f794SJohn Baldwin p->p_flag |= P_PROFIL; 6028b98fec9SJeff Roberson mtx_lock_spin(&time_lock); 603238dd320SJake Burkholder if (++profprocs == 1) 604238dd320SJake Burkholder cpu_startprofclock(); 6058b98fec9SJeff Roberson mtx_unlock_spin(&time_lock); 606df8bae1dSRodney W. Grimes } 6079752f794SJohn Baldwin } 608df8bae1dSRodney W. Grimes 609df8bae1dSRodney W. Grimes /* 610df8bae1dSRodney W. Grimes * Stop profiling on a process. 611df8bae1dSRodney W. Grimes */ 612df8bae1dSRodney W. Grimes void 613df8bae1dSRodney W. Grimes stopprofclock(p) 614df8bae1dSRodney W. Grimes register struct proc *p; 615df8bae1dSRodney W. Grimes { 616df8bae1dSRodney W. Grimes 617a282253aSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 6189752f794SJohn Baldwin if (p->p_flag & P_PROFIL) { 6199752f794SJohn Baldwin if (p->p_profthreads != 0) { 6209752f794SJohn Baldwin p->p_flag |= P_STOPPROF; 6219752f794SJohn Baldwin while (p->p_profthreads != 0) 622a282253aSJulian Elischer msleep(&p->p_profthreads, &p->p_mtx, PPAUSE, 623a89ec05eSPeter Wemm "stopprof", 0); 6249752f794SJohn Baldwin p->p_flag &= ~P_STOPPROF; 625a282253aSJulian Elischer } 626b62b2304SColin Percival if ((p->p_flag & P_PROFIL) == 0) 627b62b2304SColin Percival return; 6289752f794SJohn Baldwin p->p_flag &= ~P_PROFIL; 6298b98fec9SJeff Roberson mtx_lock_spin(&time_lock); 630238dd320SJake Burkholder if (--profprocs == 0) 631238dd320SJake Burkholder cpu_stopprofclock(); 6328b98fec9SJeff Roberson mtx_unlock_spin(&time_lock); 633df8bae1dSRodney W. Grimes } 6349752f794SJohn Baldwin } 635df8bae1dSRodney W. Grimes 636df8bae1dSRodney W. Grimes /* 6371c4bcd05SJeff Roberson * Statistics clock. Updates rusage information and calls the scheduler 6381c4bcd05SJeff Roberson * to adjust priorities of the active thread. 6391c4bcd05SJeff Roberson * 640238dd320SJake Burkholder * This should be called by all active processors. 641df8bae1dSRodney W. Grimes */ 642df8bae1dSRodney W. Grimes void 643b439e431SJohn Baldwin statclock(int usermode) 644df8bae1dSRodney W. Grimes { 6458a129caeSDavid Greenman struct rusage *ru; 6468a129caeSDavid Greenman struct vmspace *vm; 647238dd320SJake Burkholder struct thread *td; 648238dd320SJake Burkholder struct proc *p; 649238dd320SJake Burkholder long rss; 6507628402bSPeter Wemm long *cp_time; 6518a129caeSDavid Greenman 652238dd320SJake Burkholder td = curthread; 653238dd320SJake Burkholder p = td->td_proc; 654238dd320SJake Burkholder 6557628402bSPeter Wemm cp_time = (long *)PCPU_PTR(cp_time); 656b439e431SJohn Baldwin if (usermode) { 657df8bae1dSRodney W. Grimes /* 65871a62f8aSBruce Evans * Charge the time as appropriate. 659df8bae1dSRodney W. Grimes */ 660e8444a7eSPoul-Henning Kamp td->td_uticks++; 661fa885116SJulian Elischer if (p->p_nice > NZERO) 6627628402bSPeter Wemm cp_time[CP_NICE]++; 663df8bae1dSRodney W. Grimes else 6647628402bSPeter Wemm cp_time[CP_USER]++; 665df8bae1dSRodney W. Grimes } else { 666df8bae1dSRodney W. Grimes /* 667df8bae1dSRodney W. Grimes * Came from kernel mode, so we were: 668df8bae1dSRodney W. Grimes * - handling an interrupt, 669df8bae1dSRodney W. Grimes * - doing syscall or trap work on behalf of the current 670df8bae1dSRodney W. Grimes * user process, or 671df8bae1dSRodney W. Grimes * - spinning in the idle loop. 672df8bae1dSRodney W. Grimes * Whichever it is, charge the time as appropriate. 673df8bae1dSRodney W. Grimes * Note that we charge interrupts to the current process, 674df8bae1dSRodney W. Grimes * regardless of whether they are ``for'' that process, 675df8bae1dSRodney W. Grimes * so that we know how much of its real time was spent 676df8bae1dSRodney W. Grimes * in ``non-process'' (i.e., interrupt) work. 677df8bae1dSRodney W. Grimes */ 678e0f66ef8SJohn Baldwin if ((td->td_pflags & TDP_ITHREAD) || 679e0f66ef8SJohn Baldwin td->td_intr_nesting_level >= 2) { 680e8444a7eSPoul-Henning Kamp td->td_iticks++; 6817628402bSPeter Wemm cp_time[CP_INTR]++; 6820384fff8SJason Evans } else { 683eb2da9a5SPoul-Henning Kamp td->td_pticks++; 684e8444a7eSPoul-Henning Kamp td->td_sticks++; 685486a9414SJulian Elischer if (!TD_IS_IDLETHREAD(td)) 6867628402bSPeter Wemm cp_time[CP_SYS]++; 6870384fff8SJason Evans else 6887628402bSPeter Wemm cp_time[CP_IDLE]++; 689df8bae1dSRodney W. Grimes } 6900384fff8SJason Evans } 691f5e9e8ecSBruce Evans 692f5e9e8ecSBruce Evans /* Update resource usage integrals and maximums. */ 69316f9f205SJohn Baldwin MPASS(p->p_vmspace != NULL); 69416f9f205SJohn Baldwin vm = p->p_vmspace; 6951c4bcd05SJeff Roberson ru = &td->td_ru; 6961c6d46f9SLuoqi Chen ru->ru_ixrss += pgtok(vm->vm_tsize); 6971c6d46f9SLuoqi Chen ru->ru_idrss += pgtok(vm->vm_dsize); 6981c6d46f9SLuoqi Chen ru->ru_isrss += pgtok(vm->vm_ssize); 6991c6d46f9SLuoqi Chen rss = pgtok(vmspace_resident_count(vm)); 700f5e9e8ecSBruce Evans if (ru->ru_maxrss < rss) 701f5e9e8ecSBruce Evans ru->ru_maxrss = rss; 7028f51ad55SJeff Roberson KTR_POINT2(KTR_SCHED, "thread", sched_tdname(td), "statclock", 7038f51ad55SJeff Roberson "prio:%d", td->td_priority, "stathz:%d", (stathz)?stathz:hz); 7047628402bSPeter Wemm thread_lock_flags(td, MTX_QUIET); 70540acdeabSJeff Roberson sched_clock(td); 70640acdeabSJeff Roberson thread_unlock(td); 7076caa8a15SJohn Baldwin } 7086c567274SJohn Baldwin 7096caa8a15SJohn Baldwin void 710b439e431SJohn Baldwin profclock(int usermode, uintfptr_t pc) 7116caa8a15SJohn Baldwin { 712238dd320SJake Burkholder struct thread *td; 713238dd320SJake Burkholder #ifdef GPROF 714238dd320SJake Burkholder struct gmonparam *g; 7155c8b4441SJohn Baldwin uintfptr_t i; 716238dd320SJake Burkholder #endif 7176caa8a15SJohn Baldwin 7184a338afdSJulian Elischer td = curthread; 719b439e431SJohn Baldwin if (usermode) { 720238dd320SJake Burkholder /* 721238dd320SJake Burkholder * Came from user mode; CPU was in user state. 722238dd320SJake Burkholder * If this process is being profiled, record the tick. 723a282253aSJulian Elischer * if there is no related user location yet, don't 724a282253aSJulian Elischer * bother trying to count it. 725238dd320SJake Burkholder */ 7269752f794SJohn Baldwin if (td->td_proc->p_flag & P_PROFIL) 727b439e431SJohn Baldwin addupc_intr(td, pc, 1); 728238dd320SJake Burkholder } 729238dd320SJake Burkholder #ifdef GPROF 730238dd320SJake Burkholder else { 731238dd320SJake Burkholder /* 732238dd320SJake Burkholder * Kernel statistics are just like addupc_intr, only easier. 733238dd320SJake Burkholder */ 734238dd320SJake Burkholder g = &_gmonparam; 735b439e431SJohn Baldwin if (g->state == GMON_PROF_ON && pc >= g->lowpc) { 736b439e431SJohn Baldwin i = PC_TO_I(g, pc); 737238dd320SJake Burkholder if (i < g->textsize) { 738b439e431SJohn Baldwin KCOUNT(g, i)++; 739238dd320SJake Burkholder } 740238dd320SJake Burkholder } 741238dd320SJake Burkholder } 742238dd320SJake Burkholder #endif 743df8bae1dSRodney W. Grimes } 744df8bae1dSRodney W. Grimes 745df8bae1dSRodney W. Grimes /* 746df8bae1dSRodney W. Grimes * Return information about system clocks. 747df8bae1dSRodney W. Grimes */ 748787d58f2SPoul-Henning Kamp static int 74982d9ae4eSPoul-Henning Kamp sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS) 750df8bae1dSRodney W. Grimes { 751df8bae1dSRodney W. Grimes struct clockinfo clkinfo; 752df8bae1dSRodney W. Grimes /* 753df8bae1dSRodney W. Grimes * Construct clockinfo structure. 754df8bae1dSRodney W. Grimes */ 755a9a0f15aSBruce Evans bzero(&clkinfo, sizeof(clkinfo)); 756df8bae1dSRodney W. Grimes clkinfo.hz = hz; 757df8bae1dSRodney W. Grimes clkinfo.tick = tick; 758df8bae1dSRodney W. Grimes clkinfo.profhz = profhz; 759df8bae1dSRodney W. Grimes clkinfo.stathz = stathz ? stathz : hz; 760ae0eb976SPoul-Henning Kamp return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req)); 761df8bae1dSRodney W. Grimes } 7623f31c649SGarrett Wollman 763c383c221SEd Schouten SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, 764c383c221SEd Schouten CTLTYPE_STRUCT|CTLFLAG_RD|CTLFLAG_MPSAFE, 765af1408e3SLuigi Rizzo 0, 0, sysctl_kern_clockrate, "S,clockinfo", 766af1408e3SLuigi Rizzo "Rate and period of various kernel clocks"); 767370c3cb5SSean Kelly 7684103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG 7694103b765SPoul-Henning Kamp 7704103b765SPoul-Henning Kamp static void 7719079fff5SNick Hibma watchdog_config(void *unused __unused, u_int cmd, int *error) 772370c3cb5SSean Kelly { 7734103b765SPoul-Henning Kamp u_int u; 774370c3cb5SSean Kelly 7754103b765SPoul-Henning Kamp u = cmd & WD_INTERVAL; 7769079fff5SNick Hibma if (u >= WD_TO_1SEC) { 7774103b765SPoul-Henning Kamp watchdog_ticks = (1 << (u - WD_TO_1SEC)) * hz; 7784103b765SPoul-Henning Kamp watchdog_enabled = 1; 7799079fff5SNick Hibma *error = 0; 7804103b765SPoul-Henning Kamp } else { 7814103b765SPoul-Henning Kamp watchdog_enabled = 0; 782370c3cb5SSean Kelly } 7834103b765SPoul-Henning Kamp } 784370c3cb5SSean Kelly 785370c3cb5SSean Kelly /* 786370c3cb5SSean Kelly * Handle a watchdog timeout by dumping interrupt information and 787911d16b8SEd Maste * then either dropping to DDB or panicking. 788370c3cb5SSean Kelly */ 789370c3cb5SSean Kelly static void 790370c3cb5SSean Kelly watchdog_fire(void) 791370c3cb5SSean Kelly { 792370c3cb5SSean Kelly int nintr; 793370c3cb5SSean Kelly u_int64_t inttotal; 794370c3cb5SSean Kelly u_long *curintr; 795370c3cb5SSean Kelly char *curname; 796370c3cb5SSean Kelly 797370c3cb5SSean Kelly curintr = intrcnt; 798370c3cb5SSean Kelly curname = intrnames; 799370c3cb5SSean Kelly inttotal = 0; 800370c3cb5SSean Kelly nintr = eintrcnt - intrcnt; 801370c3cb5SSean Kelly 802370c3cb5SSean Kelly printf("interrupt total\n"); 803370c3cb5SSean Kelly while (--nintr >= 0) { 804370c3cb5SSean Kelly if (*curintr) 805370c3cb5SSean Kelly printf("%-12s %20lu\n", curname, *curintr); 806370c3cb5SSean Kelly curname += strlen(curname) + 1; 807370c3cb5SSean Kelly inttotal += *curintr++; 808370c3cb5SSean Kelly } 8096cda4155SSean Kelly printf("Total %20ju\n", (uintmax_t)inttotal); 810911d16b8SEd Maste 811911d16b8SEd Maste #if defined(KDB) && !defined(KDB_UNATTENDED) 812911d16b8SEd Maste kdb_backtrace(); 8133de213ccSRobert Watson kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout"); 814911d16b8SEd Maste #else 815370c3cb5SSean Kelly panic("watchdog timeout"); 816911d16b8SEd Maste #endif 817370c3cb5SSean Kelly } 818370c3cb5SSean Kelly 8194103b765SPoul-Henning Kamp #endif /* SW_WATCHDOG */ 820