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> 512d50560aSMarcel Moolenaar #include <sys/kdb.h> 52df8bae1dSRodney W. Grimes #include <sys/kernel.h> 53f7829d0dSAttilio Rao #include <sys/kthread.h> 5461d80e90SJohn Baldwin #include <sys/ktr.h> 55f7829d0dSAttilio Rao #include <sys/lock.h> 5635e0e5b3SJohn Baldwin #include <sys/mutex.h> 57df8bae1dSRodney W. Grimes #include <sys/proc.h> 58e4625663SJeff Roberson #include <sys/resource.h> 59df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 60b43179fbSJeff Roberson #include <sys/sched.h> 61b3e9e682SRyan Stone #include <sys/sdt.h> 62797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 63f7829d0dSAttilio Rao #include <sys/sleepqueue.h> 646caa8a15SJohn Baldwin #include <sys/smp.h> 658a129caeSDavid Greenman #include <vm/vm.h> 66efeaf95aSDavid Greenman #include <vm/pmap.h> 67efeaf95aSDavid Greenman #include <vm/vm_map.h> 68797f2d22SPoul-Henning Kamp #include <sys/sysctl.h> 698088699fSJohn Baldwin #include <sys/bus.h> 708088699fSJohn Baldwin #include <sys/interrupt.h> 71104a9b7eSAlexander Kabaev #include <sys/limits.h> 72e7fa55afSPoul-Henning Kamp #include <sys/timetc.h> 73df8bae1dSRodney W. Grimes 74df8bae1dSRodney W. Grimes #ifdef GPROF 75df8bae1dSRodney W. Grimes #include <sys/gmon.h> 76df8bae1dSRodney W. Grimes #endif 77df8bae1dSRodney W. Grimes 7836c0fd9dSJoseph Koshy #ifdef HWPMC_HOOKS 7936c0fd9dSJoseph Koshy #include <sys/pmckern.h> 80f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , clock, hard); 81f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , clock, stat); 82d49302aeSFabien Thomas PMC_SOFT_DEFINE_EX( , , clock, prof, \ 83d49302aeSFabien Thomas cpu_startprofclock, cpu_stopprofclock); 8436c0fd9dSJoseph Koshy #endif 8536c0fd9dSJoseph Koshy 86e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 87e4fc250cSLuigi Rizzo extern void hardclock_device_poll(void); 88e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */ 89eae8fc2cSSteve Passe 904d77a549SAlfred Perlstein static void initclocks(void *dummy); 91237fdd78SRobert Watson SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL); 922b14f991SJulian Elischer 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 186f7829d0dSAttilio Rao deadlkres(void) 187f7829d0dSAttilio Rao { 188f7829d0dSAttilio Rao struct proc *p; 189f7829d0dSAttilio Rao struct thread *td; 190f7829d0dSAttilio Rao void *wchan; 19136e51f65SAttilio Rao int blkticks, i, slpticks, slptype, tryl, tticks; 192f7829d0dSAttilio Rao 193f7829d0dSAttilio Rao tryl = 0; 194f7829d0dSAttilio Rao for (;;) { 195f7829d0dSAttilio Rao blkticks = blktime_threshold * hz; 196f7829d0dSAttilio Rao slpticks = slptime_threshold * hz; 197f7829d0dSAttilio Rao 198f7829d0dSAttilio Rao /* 199f7829d0dSAttilio Rao * Avoid to sleep on the sx_lock in order to avoid a possible 200f7829d0dSAttilio Rao * priority inversion problem leading to starvation. 201f7829d0dSAttilio Rao * If the lock can't be held after 100 tries, panic. 202f7829d0dSAttilio Rao */ 203f7829d0dSAttilio Rao if (!sx_try_slock(&allproc_lock)) { 204f7829d0dSAttilio Rao if (tryl > 100) 205f7829d0dSAttilio Rao panic("%s: possible deadlock detected on allproc_lock\n", 206f7829d0dSAttilio Rao __func__); 207f7829d0dSAttilio Rao tryl++; 208b5850804SJohn Baldwin pause("allproc", sleepfreq * hz); 209f7829d0dSAttilio Rao continue; 210f7829d0dSAttilio Rao } 211f7829d0dSAttilio Rao tryl = 0; 212f7829d0dSAttilio Rao FOREACH_PROC_IN_SYSTEM(p) { 213f7829d0dSAttilio Rao PROC_LOCK(p); 214e806d352SJohn Baldwin if (p->p_state == PRS_NEW) { 215e806d352SJohn Baldwin PROC_UNLOCK(p); 216e806d352SJohn Baldwin continue; 217e806d352SJohn Baldwin } 218f7829d0dSAttilio Rao FOREACH_THREAD_IN_PROC(p, td) { 219631cb86fSAttilio Rao 220f7829d0dSAttilio Rao thread_lock(td); 2214a7d0bfcSRyan Stone if (TD_ON_LOCK(td)) { 222f7829d0dSAttilio Rao 223f7829d0dSAttilio Rao /* 224f7829d0dSAttilio Rao * The thread should be blocked on a 225f7829d0dSAttilio Rao * turnstile, simply check if the 226f7829d0dSAttilio Rao * turnstile channel is in good state. 227f7829d0dSAttilio Rao */ 228f7829d0dSAttilio Rao MPASS(td->td_blocked != NULL); 22936e51f65SAttilio Rao 230f7829d0dSAttilio Rao tticks = ticks - td->td_blktick; 231f7829d0dSAttilio Rao thread_unlock(td); 232f7829d0dSAttilio Rao if (tticks > blkticks) { 233f7829d0dSAttilio Rao 234f7829d0dSAttilio Rao /* 235f7829d0dSAttilio Rao * Accordingly with provided 236f7829d0dSAttilio Rao * thresholds, this thread is 237f7829d0dSAttilio Rao * stuck for too long on a 238f7829d0dSAttilio Rao * turnstile. 239f7829d0dSAttilio Rao */ 240f7829d0dSAttilio Rao PROC_UNLOCK(p); 241f7829d0dSAttilio Rao sx_sunlock(&allproc_lock); 242f7829d0dSAttilio Rao panic("%s: possible deadlock detected for %p, blocked for %d ticks\n", 243f7829d0dSAttilio Rao __func__, td, tticks); 244f7829d0dSAttilio Rao } 245631cb86fSAttilio Rao } else if (TD_IS_SLEEPING(td) && 2464a7d0bfcSRyan Stone TD_ON_SLEEPQ(td)) { 24736e51f65SAttilio Rao 248f7829d0dSAttilio Rao /* 249f7829d0dSAttilio Rao * Check if the thread is sleeping on a 250f7829d0dSAttilio Rao * lock, otherwise skip the check. 251f7829d0dSAttilio Rao * Drop the thread lock in order to 252f7829d0dSAttilio Rao * avoid a LOR with the sleepqueue 253f7829d0dSAttilio Rao * spinlock. 254f7829d0dSAttilio Rao */ 255f7829d0dSAttilio Rao wchan = td->td_wchan; 256f7829d0dSAttilio Rao tticks = ticks - td->td_slptick; 257f7829d0dSAttilio Rao thread_unlock(td); 258f7829d0dSAttilio Rao slptype = sleepq_type(wchan); 259f7829d0dSAttilio Rao if ((slptype == SLEEPQ_SX || 260f7829d0dSAttilio Rao slptype == SLEEPQ_LK) && 261f7829d0dSAttilio Rao tticks > slpticks) { 262f7829d0dSAttilio Rao 263f7829d0dSAttilio Rao /* 264f7829d0dSAttilio Rao * Accordingly with provided 265f7829d0dSAttilio Rao * thresholds, this thread is 266f7829d0dSAttilio Rao * stuck for too long on a 267f7829d0dSAttilio Rao * sleepqueue. 26836e51f65SAttilio Rao * However, being on a 26936e51f65SAttilio Rao * sleepqueue, we might still 27036e51f65SAttilio Rao * check for the blessed 27136e51f65SAttilio Rao * list. 272f7829d0dSAttilio Rao */ 27336e51f65SAttilio Rao tryl = 0; 27436e51f65SAttilio Rao for (i = 0; blessed[i] != NULL; 27536e51f65SAttilio Rao i++) { 27636e51f65SAttilio Rao if (!strcmp(blessed[i], 27736e51f65SAttilio Rao td->td_wmesg)) { 27836e51f65SAttilio Rao tryl = 1; 27936e51f65SAttilio Rao break; 28036e51f65SAttilio Rao } 28136e51f65SAttilio Rao } 28236e51f65SAttilio Rao if (tryl != 0) { 28336e51f65SAttilio Rao tryl = 0; 28436e51f65SAttilio Rao continue; 28536e51f65SAttilio Rao } 286f7829d0dSAttilio Rao PROC_UNLOCK(p); 287f7829d0dSAttilio Rao sx_sunlock(&allproc_lock); 288f7829d0dSAttilio Rao panic("%s: possible deadlock detected for %p, blocked for %d ticks\n", 289f7829d0dSAttilio Rao __func__, td, tticks); 290f7829d0dSAttilio Rao } 291f7829d0dSAttilio Rao } else 292f7829d0dSAttilio Rao thread_unlock(td); 293f7829d0dSAttilio Rao } 294f7829d0dSAttilio Rao PROC_UNLOCK(p); 295f7829d0dSAttilio Rao } 296f7829d0dSAttilio Rao sx_sunlock(&allproc_lock); 297f7829d0dSAttilio Rao 298f7829d0dSAttilio Rao /* Sleep for sleepfreq seconds. */ 299b5850804SJohn Baldwin pause("-", sleepfreq * hz); 300f7829d0dSAttilio Rao } 301f7829d0dSAttilio Rao } 302f7829d0dSAttilio Rao 303f7829d0dSAttilio Rao static struct kthread_desc deadlkres_kd = { 304f7829d0dSAttilio Rao "deadlkres", 305f7829d0dSAttilio Rao deadlkres, 306f7829d0dSAttilio Rao (struct thread **)NULL 307f7829d0dSAttilio Rao }; 308f7829d0dSAttilio Rao 309f7829d0dSAttilio Rao SYSINIT(deadlkres, SI_SUB_CLOCKS, SI_ORDER_ANY, kthread_start, &deadlkres_kd); 310f7829d0dSAttilio Rao 3116472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, deadlkres, CTLFLAG_RW, 0, 3126472ac3dSEd Schouten "Deadlock resolver"); 313f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, slptime_threshold, CTLFLAG_RW, 314f7829d0dSAttilio Rao &slptime_threshold, 0, 315f7829d0dSAttilio Rao "Number of seconds within is valid to sleep on a sleepqueue"); 316f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, blktime_threshold, CTLFLAG_RW, 317f7829d0dSAttilio Rao &blktime_threshold, 0, 318f7829d0dSAttilio Rao "Number of seconds within is valid to block on a turnstile"); 319f7829d0dSAttilio Rao SYSCTL_INT(_debug_deadlkres, OID_AUTO, sleepfreq, CTLFLAG_RW, &sleepfreq, 0, 320f7829d0dSAttilio Rao "Number of seconds between any deadlock resolver thread run"); 321f7829d0dSAttilio Rao #endif /* DEADLKRES */ 322f7829d0dSAttilio Rao 3237628402bSPeter Wemm void 3247628402bSPeter Wemm read_cpu_time(long *cp_time) 3257628402bSPeter Wemm { 3267628402bSPeter Wemm struct pcpu *pc; 3277628402bSPeter Wemm int i, j; 3287628402bSPeter Wemm 3297628402bSPeter Wemm /* Sum up global cp_time[]. */ 3307628402bSPeter Wemm bzero(cp_time, sizeof(long) * CPUSTATES); 3313aa6d94eSJohn Baldwin CPU_FOREACH(i) { 3327628402bSPeter Wemm pc = pcpu_find(i); 3337628402bSPeter Wemm for (j = 0; j < CPUSTATES; j++) 3347628402bSPeter Wemm cp_time[j] += pc->pc_cp_time[j]; 3357628402bSPeter Wemm } 3367628402bSPeter Wemm } 3377628402bSPeter Wemm 3384103b765SPoul-Henning Kamp #include <sys/watchdog.h> 339370c3cb5SSean Kelly 3404103b765SPoul-Henning Kamp static int watchdog_ticks; 341370c3cb5SSean Kelly static int watchdog_enabled; 3424103b765SPoul-Henning Kamp static void watchdog_fire(void); 3434103b765SPoul-Henning Kamp static void watchdog_config(void *, u_int, int *); 344*d626b50bSMike Karels 345*d626b50bSMike Karels static void 346*d626b50bSMike Karels watchdog_attach(void) 347*d626b50bSMike Karels { 348*d626b50bSMike Karels EVENTHANDLER_REGISTER(watchdog_list, watchdog_config, NULL, 0); 349*d626b50bSMike Karels } 350370c3cb5SSean Kelly 3513bac064fSPoul-Henning Kamp /* 352df8bae1dSRodney W. Grimes * Clock handling routines. 353df8bae1dSRodney W. Grimes * 354b05dcf3cSPoul-Henning Kamp * This code is written to operate with two timers that run independently of 355b05dcf3cSPoul-Henning Kamp * each other. 3567ec73f64SPoul-Henning Kamp * 357b05dcf3cSPoul-Henning Kamp * The main timer, running hz times per second, is used to trigger interval 358b05dcf3cSPoul-Henning Kamp * timers, timeouts and rescheduling as needed. 3597ec73f64SPoul-Henning Kamp * 360b05dcf3cSPoul-Henning Kamp * The second timer handles kernel and user profiling, 361b05dcf3cSPoul-Henning Kamp * and does resource use estimation. If the second timer is programmable, 362b05dcf3cSPoul-Henning Kamp * it is randomized to avoid aliasing between the two clocks. For example, 363b05dcf3cSPoul-Henning Kamp * the randomization prevents an adversary from always giving up the cpu 364df8bae1dSRodney W. Grimes * just before its quantum expires. Otherwise, it would never accumulate 365df8bae1dSRodney W. Grimes * cpu ticks. The mean frequency of the second timer is stathz. 366b05dcf3cSPoul-Henning Kamp * 367b05dcf3cSPoul-Henning Kamp * If no second timer exists, stathz will be zero; in this case we drive 368b05dcf3cSPoul-Henning Kamp * profiling and statistics off the main clock. This WILL NOT be accurate; 369b05dcf3cSPoul-Henning Kamp * do not do it unless absolutely necessary. 370b05dcf3cSPoul-Henning Kamp * 371df8bae1dSRodney W. Grimes * The statistics clock may (or may not) be run at a higher rate while 372b05dcf3cSPoul-Henning Kamp * profiling. This profile clock runs at profhz. We require that profhz 373b05dcf3cSPoul-Henning Kamp * be an integral multiple of stathz. 374b05dcf3cSPoul-Henning Kamp * 375b05dcf3cSPoul-Henning Kamp * If the statistics clock is running fast, it must be divided by the ratio 376b05dcf3cSPoul-Henning Kamp * profhz/stathz for statistics. (For profiling, every tick counts.) 377df8bae1dSRodney W. Grimes * 3787ec73f64SPoul-Henning Kamp * Time-of-day is maintained using a "timecounter", which may or may 3797ec73f64SPoul-Henning Kamp * not be related to the hardware generating the above mentioned 3807ec73f64SPoul-Henning Kamp * interrupts. 381df8bae1dSRodney W. Grimes */ 382df8bae1dSRodney W. Grimes 383df8bae1dSRodney W. Grimes int stathz; 384df8bae1dSRodney W. Grimes int profhz; 385238dd320SJake Burkholder int profprocs; 386a8df530dSJohn Baldwin volatile int ticks; 387238dd320SJake Burkholder int psratio; 388df8bae1dSRodney W. Grimes 3893e288e62SDimitry Andric static DPCPU_DEFINE(int, pcputicks); /* Per-CPU version of ticks. */ 3905760b029SKonstantin Belousov #ifdef DEVICE_POLLING 3915760b029SKonstantin Belousov static int devpoll_run = 0; 3925760b029SKonstantin Belousov #endif 393dbd55f3fSAlexander Motin 394df8bae1dSRodney W. Grimes /* 395df8bae1dSRodney W. Grimes * Initialize clock frequencies and start both clocks running. 396df8bae1dSRodney W. Grimes */ 3972b14f991SJulian Elischer /* ARGSUSED*/ 3982b14f991SJulian Elischer static void 3993e85b721SEd Maste initclocks(void *dummy) 400df8bae1dSRodney W. Grimes { 4013e85b721SEd Maste int i; 402df8bae1dSRodney W. Grimes 403df8bae1dSRodney W. Grimes /* 404df8bae1dSRodney W. Grimes * Set divisors to 1 (normal case) and let the machine-specific 405df8bae1dSRodney W. Grimes * code do its bit. 406df8bae1dSRodney W. Grimes */ 407875b8844SAlexander Motin mtx_init(&time_lock, "time lock", NULL, MTX_DEF); 40863d69d25SRobert Watson cpu_initclocks(); 409df8bae1dSRodney W. Grimes 410df8bae1dSRodney W. Grimes /* 411df8bae1dSRodney W. Grimes * Compute profhz/stathz, and fix profhz if needed. 412df8bae1dSRodney W. Grimes */ 413df8bae1dSRodney W. Grimes i = stathz ? stathz : hz; 414df8bae1dSRodney W. Grimes if (profhz == 0) 415df8bae1dSRodney W. Grimes profhz = i; 416df8bae1dSRodney W. Grimes psratio = profhz / i; 417*d626b50bSMike Karels 4184103b765SPoul-Henning Kamp #ifdef SW_WATCHDOG 419*d626b50bSMike Karels /* Enable hardclock watchdog now, even if a hardware watchdog exists. */ 420*d626b50bSMike Karels watchdog_attach(); 421*d626b50bSMike Karels #else 422*d626b50bSMike Karels /* Volunteer to run a software watchdog. */ 423*d626b50bSMike Karels if (wdog_software_attach == NULL) 424*d626b50bSMike Karels wdog_software_attach = watchdog_attach; 4254103b765SPoul-Henning Kamp #endif 426df8bae1dSRodney W. Grimes } 427df8bae1dSRodney W. Grimes 428df8bae1dSRodney W. Grimes /* 429238dd320SJake Burkholder * Each time the real-time timer fires, this function is called on all CPUs. 430b439e431SJohn Baldwin * Note that hardclock() calls hardclock_cpu() for the boot CPU, so only 431238dd320SJake Burkholder * the other CPUs in the system need to call this function. 4326caa8a15SJohn Baldwin */ 4336caa8a15SJohn Baldwin void 434b439e431SJohn Baldwin hardclock_cpu(int usermode) 4356caa8a15SJohn Baldwin { 4366caa8a15SJohn Baldwin struct pstats *pstats; 437238dd320SJake Burkholder struct thread *td = curthread; 438b40ce416SJulian Elischer struct proc *p = td->td_proc; 439b61ce5b0SJeff Roberson int flags; 4406caa8a15SJohn Baldwin 4416caa8a15SJohn Baldwin /* 4426caa8a15SJohn Baldwin * Run current process's virtual and profile time, as needed. 4436caa8a15SJohn Baldwin */ 444ad1e7d28SJulian Elischer pstats = p->p_stats; 445b61ce5b0SJeff Roberson flags = 0; 446ad1e7d28SJulian Elischer if (usermode && 44740acdeabSJeff Roberson timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) { 4485c7bebf9SKonstantin Belousov PROC_ITIMLOCK(p); 449b61ce5b0SJeff Roberson if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) 450b61ce5b0SJeff Roberson flags |= TDF_ALRMPEND | TDF_ASTPENDING; 4515c7bebf9SKonstantin Belousov PROC_ITIMUNLOCK(p); 45240acdeabSJeff Roberson } 45340acdeabSJeff Roberson if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) { 4545c7bebf9SKonstantin Belousov PROC_ITIMLOCK(p); 455b61ce5b0SJeff Roberson if (itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) 456b61ce5b0SJeff Roberson flags |= TDF_PROFPEND | TDF_ASTPENDING; 4575c7bebf9SKonstantin Belousov PROC_ITIMUNLOCK(p); 45840acdeabSJeff Roberson } 45940acdeabSJeff Roberson thread_lock(td); 460b61ce5b0SJeff Roberson td->td_flags |= flags; 46140acdeabSJeff Roberson thread_unlock(td); 46236c0fd9dSJoseph Koshy 46336c0fd9dSJoseph Koshy #ifdef HWPMC_HOOKS 46436c0fd9dSJoseph Koshy if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid))) 46536c0fd9dSJoseph Koshy PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL); 466f5f9340bSFabien Thomas if (td->td_intr_frame != NULL) 467f5f9340bSFabien Thomas PMC_SOFT_CALL_TF( , , clock, hard, td->td_intr_frame); 46836c0fd9dSJoseph Koshy #endif 4695b999a6bSDavide Italiano callout_process(sbinuptime()); 4706caa8a15SJohn Baldwin } 4716caa8a15SJohn Baldwin 4726caa8a15SJohn Baldwin /* 473df8bae1dSRodney W. Grimes * The real-time timer, interrupting hz times per second. 474df8bae1dSRodney W. Grimes */ 475df8bae1dSRodney W. Grimes void 476b439e431SJohn Baldwin hardclock(int usermode, uintfptr_t pc) 477df8bae1dSRodney W. Grimes { 478df8bae1dSRodney W. Grimes 479a8df530dSJohn Baldwin atomic_add_int(&ticks, 1); 480b439e431SJohn Baldwin hardclock_cpu(usermode); 4810e189873SAlexander Motin tc_ticktock(1); 482a157e425SAlexander Motin cpu_tick_calibration(); 483df8bae1dSRodney W. Grimes /* 484df8bae1dSRodney W. Grimes * If no separate statistics clock is available, run it from here. 4856caa8a15SJohn Baldwin * 4866caa8a15SJohn Baldwin * XXX: this only works for UP 487df8bae1dSRodney W. Grimes */ 488238dd320SJake Burkholder if (stathz == 0) { 489b439e431SJohn Baldwin profclock(usermode, pc); 490b439e431SJohn Baldwin statclock(usermode); 491238dd320SJake Burkholder } 492e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 493daccb638SLuigi Rizzo hardclock_device_poll(); /* this is very short and quick */ 494e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */ 4954103b765SPoul-Henning Kamp if (watchdog_enabled > 0 && --watchdog_ticks <= 0) 496370c3cb5SSean Kelly watchdog_fire(); 497ab36c067SJustin T. Gibbs } 498ab36c067SJustin T. Gibbs 499a157e425SAlexander Motin void 500bcfd016cSAlexander Motin hardclock_cnt(int cnt, int usermode) 501a157e425SAlexander Motin { 502a157e425SAlexander Motin struct pstats *pstats; 503a157e425SAlexander Motin struct thread *td = curthread; 504a157e425SAlexander Motin struct proc *p = td->td_proc; 505a157e425SAlexander Motin int *t = DPCPU_PTR(pcputicks); 5064763a8b8SAlexander Motin int flags, global, newticks; 5074763a8b8SAlexander Motin int i; 508a157e425SAlexander Motin 509a157e425SAlexander Motin /* 510a157e425SAlexander Motin * Update per-CPU and possibly global ticks values. 511a157e425SAlexander Motin */ 512a157e425SAlexander Motin *t += cnt; 513a157e425SAlexander Motin do { 514a157e425SAlexander Motin global = ticks; 515a157e425SAlexander Motin newticks = *t - global; 516a157e425SAlexander Motin if (newticks <= 0) { 517a157e425SAlexander Motin if (newticks < -1) 518a157e425SAlexander Motin *t = global - 1; 519a157e425SAlexander Motin newticks = 0; 520a157e425SAlexander Motin break; 521a157e425SAlexander Motin } 522a157e425SAlexander Motin } while (!atomic_cmpset_int(&ticks, global, *t)); 523a157e425SAlexander Motin 524a157e425SAlexander Motin /* 525a157e425SAlexander Motin * Run current process's virtual and profile time, as needed. 526a157e425SAlexander Motin */ 527a157e425SAlexander Motin pstats = p->p_stats; 528a157e425SAlexander Motin flags = 0; 529a157e425SAlexander Motin if (usermode && 530a157e425SAlexander Motin timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value)) { 5315c7bebf9SKonstantin Belousov PROC_ITIMLOCK(p); 532a157e425SAlexander Motin if (itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], 533a157e425SAlexander Motin tick * cnt) == 0) 534a157e425SAlexander Motin flags |= TDF_ALRMPEND | TDF_ASTPENDING; 5355c7bebf9SKonstantin Belousov PROC_ITIMUNLOCK(p); 536a157e425SAlexander Motin } 537a157e425SAlexander Motin if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value)) { 5385c7bebf9SKonstantin Belousov PROC_ITIMLOCK(p); 539a157e425SAlexander Motin if (itimerdecr(&pstats->p_timer[ITIMER_PROF], 540a157e425SAlexander Motin tick * cnt) == 0) 541a157e425SAlexander Motin flags |= TDF_PROFPEND | TDF_ASTPENDING; 5425c7bebf9SKonstantin Belousov PROC_ITIMUNLOCK(p); 543a157e425SAlexander Motin } 544ef89d843SMark Johnston if (flags != 0) { 545a157e425SAlexander Motin thread_lock(td); 546a157e425SAlexander Motin td->td_flags |= flags; 547a157e425SAlexander Motin thread_unlock(td); 548ef89d843SMark Johnston } 549a157e425SAlexander Motin 550a157e425SAlexander Motin #ifdef HWPMC_HOOKS 551a157e425SAlexander Motin if (PMC_CPU_HAS_SAMPLES(PCPU_GET(cpuid))) 552a157e425SAlexander Motin PMC_CALL_HOOK_UNLOCKED(curthread, PMC_FN_DO_SAMPLES, NULL); 553f5f9340bSFabien Thomas if (td->td_intr_frame != NULL) 554f5f9340bSFabien Thomas PMC_SOFT_CALL_TF( , , clock, hard, td->td_intr_frame); 555a157e425SAlexander Motin #endif 556a157e425SAlexander Motin /* We are in charge to handle this tick duty. */ 557a157e425SAlexander Motin if (newticks > 0) { 5580e189873SAlexander Motin tc_ticktock(newticks); 559a157e425SAlexander Motin #ifdef DEVICE_POLLING 5605760b029SKonstantin Belousov /* Dangerous and no need to call these things concurrently. */ 5615760b029SKonstantin Belousov if (atomic_cmpset_acq_int(&devpoll_run, 0, 1)) { 5624763a8b8SAlexander Motin /* This is very short and quick. */ 5634763a8b8SAlexander Motin hardclock_device_poll(); 5645760b029SKonstantin Belousov atomic_store_rel_int(&devpoll_run, 0); 5654763a8b8SAlexander Motin } 5665760b029SKonstantin Belousov #endif /* DEVICE_POLLING */ 567a157e425SAlexander Motin if (watchdog_enabled > 0) { 5684763a8b8SAlexander Motin i = atomic_fetchadd_int(&watchdog_ticks, -newticks); 5694763a8b8SAlexander Motin if (i > 0 && i <= newticks) 570a157e425SAlexander Motin watchdog_fire(); 571a157e425SAlexander Motin } 572a157e425SAlexander Motin } 573a157e425SAlexander Motin if (curcpu == CPU_FIRST()) 574a157e425SAlexander Motin cpu_tick_calibration(); 575a157e425SAlexander Motin } 576a157e425SAlexander Motin 577a157e425SAlexander Motin void 578a157e425SAlexander Motin hardclock_sync(int cpu) 579a157e425SAlexander Motin { 580efe67753SNathan Whitehorn int *t; 581efe67753SNathan Whitehorn KASSERT(!CPU_ABSENT(cpu), ("Absent CPU %d", cpu)); 582efe67753SNathan Whitehorn t = DPCPU_ID_PTR(cpu, pcputicks); 583a157e425SAlexander Motin 584a157e425SAlexander Motin *t = ticks; 585a157e425SAlexander Motin } 586a157e425SAlexander Motin 587df8bae1dSRodney W. Grimes /* 588227ee8a1SPoul-Henning Kamp * Compute number of ticks in the specified amount of time. 589df8bae1dSRodney W. Grimes */ 590df8bae1dSRodney W. Grimes int 5913e85b721SEd Maste tvtohz(struct timeval *tv) 592df8bae1dSRodney W. Grimes { 5933e85b721SEd Maste unsigned long ticks; 5943e85b721SEd Maste long sec, usec; 595df8bae1dSRodney W. Grimes 596df8bae1dSRodney W. Grimes /* 5976976af69SBruce Evans * If the number of usecs in the whole seconds part of the time 5986976af69SBruce Evans * difference fits in a long, then the total number of usecs will 5996976af69SBruce Evans * fit in an unsigned long. Compute the total and convert it to 6006976af69SBruce Evans * ticks, rounding up and adding 1 to allow for the current tick 6016976af69SBruce Evans * to expire. Rounding also depends on unsigned long arithmetic 6026976af69SBruce Evans * to avoid overflow. 603df8bae1dSRodney W. Grimes * 6046976af69SBruce Evans * Otherwise, if the number of ticks in the whole seconds part of 6056976af69SBruce Evans * the time difference fits in a long, then convert the parts to 6066976af69SBruce Evans * ticks separately and add, using similar rounding methods and 6076976af69SBruce Evans * overflow avoidance. This method would work in the previous 6086976af69SBruce Evans * case but it is slightly slower and assumes that hz is integral. 6096976af69SBruce Evans * 6106976af69SBruce Evans * Otherwise, round the time difference down to the maximum 6116976af69SBruce Evans * representable value. 6126976af69SBruce Evans * 6136976af69SBruce Evans * If ints have 32 bits, then the maximum value for any timeout in 6146976af69SBruce Evans * 10ms ticks is 248 days. 615df8bae1dSRodney W. Grimes */ 616227ee8a1SPoul-Henning Kamp sec = tv->tv_sec; 617227ee8a1SPoul-Henning Kamp usec = tv->tv_usec; 6186976af69SBruce Evans if (usec < 0) { 6196976af69SBruce Evans sec--; 6206976af69SBruce Evans usec += 1000000; 6216976af69SBruce Evans } 6226976af69SBruce Evans if (sec < 0) { 6236976af69SBruce Evans #ifdef DIAGNOSTIC 624b05dcf3cSPoul-Henning Kamp if (usec > 0) { 6257ec73f64SPoul-Henning Kamp sec++; 6267ec73f64SPoul-Henning Kamp usec -= 1000000; 6277ec73f64SPoul-Henning Kamp } 628227ee8a1SPoul-Henning Kamp printf("tvotohz: negative time difference %ld sec %ld usec\n", 6296976af69SBruce Evans sec, usec); 6306976af69SBruce Evans #endif 6316976af69SBruce Evans ticks = 1; 6326976af69SBruce Evans } else if (sec <= LONG_MAX / 1000000) 63355e0987aSPedro F. Giffuni ticks = howmany(sec * 1000000 + (unsigned long)usec, tick) + 1; 6346976af69SBruce Evans else if (sec <= LONG_MAX / hz) 6356976af69SBruce Evans ticks = sec * hz 63655e0987aSPedro F. Giffuni + howmany((unsigned long)usec, tick) + 1; 6376976af69SBruce Evans else 6386976af69SBruce Evans ticks = LONG_MAX; 6396976af69SBruce Evans if (ticks > INT_MAX) 6406976af69SBruce Evans ticks = INT_MAX; 641d6116663SAlexander Langer return ((int)ticks); 642df8bae1dSRodney W. Grimes } 643df8bae1dSRodney W. Grimes 644df8bae1dSRodney W. Grimes /* 645df8bae1dSRodney W. Grimes * Start profiling on a process. 646df8bae1dSRodney W. Grimes * 647df8bae1dSRodney W. Grimes * Kernel profiling passes proc0 which never exits and hence 648df8bae1dSRodney W. Grimes * keeps the profile clock running constantly. 649df8bae1dSRodney W. Grimes */ 650df8bae1dSRodney W. Grimes void 6513e85b721SEd Maste startprofclock(struct proc *p) 652df8bae1dSRodney W. Grimes { 653df8bae1dSRodney W. Grimes 6549752f794SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 6559752f794SJohn Baldwin if (p->p_flag & P_STOPPROF) 656a282253aSJulian Elischer return; 6579752f794SJohn Baldwin if ((p->p_flag & P_PROFIL) == 0) { 6589752f794SJohn Baldwin p->p_flag |= P_PROFIL; 659875b8844SAlexander Motin mtx_lock(&time_lock); 660238dd320SJake Burkholder if (++profprocs == 1) 661238dd320SJake Burkholder cpu_startprofclock(); 662875b8844SAlexander Motin mtx_unlock(&time_lock); 663df8bae1dSRodney W. Grimes } 6649752f794SJohn Baldwin } 665df8bae1dSRodney W. Grimes 666df8bae1dSRodney W. Grimes /* 667df8bae1dSRodney W. Grimes * Stop profiling on a process. 668df8bae1dSRodney W. Grimes */ 669df8bae1dSRodney W. Grimes void 6703e85b721SEd Maste stopprofclock(struct proc *p) 671df8bae1dSRodney W. Grimes { 672df8bae1dSRodney W. Grimes 673a282253aSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 6749752f794SJohn Baldwin if (p->p_flag & P_PROFIL) { 6759752f794SJohn Baldwin if (p->p_profthreads != 0) { 6760436fcb8SKonstantin Belousov while (p->p_profthreads != 0) { 6779752f794SJohn Baldwin p->p_flag |= P_STOPPROF; 678a282253aSJulian Elischer msleep(&p->p_profthreads, &p->p_mtx, PPAUSE, 679a89ec05eSPeter Wemm "stopprof", 0); 6800436fcb8SKonstantin Belousov } 681a282253aSJulian Elischer } 682b62b2304SColin Percival if ((p->p_flag & P_PROFIL) == 0) 683b62b2304SColin Percival return; 6849752f794SJohn Baldwin p->p_flag &= ~P_PROFIL; 685875b8844SAlexander Motin mtx_lock(&time_lock); 686238dd320SJake Burkholder if (--profprocs == 0) 687238dd320SJake Burkholder cpu_stopprofclock(); 688875b8844SAlexander Motin mtx_unlock(&time_lock); 689df8bae1dSRodney W. Grimes } 6909752f794SJohn Baldwin } 691df8bae1dSRodney W. Grimes 692df8bae1dSRodney W. Grimes /* 6931c4bcd05SJeff Roberson * Statistics clock. Updates rusage information and calls the scheduler 6941c4bcd05SJeff Roberson * to adjust priorities of the active thread. 6951c4bcd05SJeff Roberson * 696238dd320SJake Burkholder * This should be called by all active processors. 697df8bae1dSRodney W. Grimes */ 698df8bae1dSRodney W. Grimes void 699b439e431SJohn Baldwin statclock(int usermode) 700df8bae1dSRodney W. Grimes { 701bcfd016cSAlexander Motin 702bcfd016cSAlexander Motin statclock_cnt(1, usermode); 703bcfd016cSAlexander Motin } 704bcfd016cSAlexander Motin 705bcfd016cSAlexander Motin void 706bcfd016cSAlexander Motin statclock_cnt(int cnt, int usermode) 707bcfd016cSAlexander Motin { 7088a129caeSDavid Greenman struct rusage *ru; 7098a129caeSDavid Greenman struct vmspace *vm; 710238dd320SJake Burkholder struct thread *td; 711238dd320SJake Burkholder struct proc *p; 712238dd320SJake Burkholder long rss; 7137628402bSPeter Wemm long *cp_time; 7148a129caeSDavid Greenman 715238dd320SJake Burkholder td = curthread; 716238dd320SJake Burkholder p = td->td_proc; 717238dd320SJake Burkholder 7187628402bSPeter Wemm cp_time = (long *)PCPU_PTR(cp_time); 719b439e431SJohn Baldwin if (usermode) { 720df8bae1dSRodney W. Grimes /* 72171a62f8aSBruce Evans * Charge the time as appropriate. 722df8bae1dSRodney W. Grimes */ 723bcfd016cSAlexander Motin td->td_uticks += cnt; 724fa885116SJulian Elischer if (p->p_nice > NZERO) 725bcfd016cSAlexander Motin cp_time[CP_NICE] += cnt; 726df8bae1dSRodney W. Grimes else 727bcfd016cSAlexander Motin cp_time[CP_USER] += cnt; 728df8bae1dSRodney W. Grimes } else { 729df8bae1dSRodney W. Grimes /* 730df8bae1dSRodney W. Grimes * Came from kernel mode, so we were: 731df8bae1dSRodney W. Grimes * - handling an interrupt, 732df8bae1dSRodney W. Grimes * - doing syscall or trap work on behalf of the current 733df8bae1dSRodney W. Grimes * user process, or 734df8bae1dSRodney W. Grimes * - spinning in the idle loop. 735df8bae1dSRodney W. Grimes * Whichever it is, charge the time as appropriate. 736df8bae1dSRodney W. Grimes * Note that we charge interrupts to the current process, 737df8bae1dSRodney W. Grimes * regardless of whether they are ``for'' that process, 738df8bae1dSRodney W. Grimes * so that we know how much of its real time was spent 739df8bae1dSRodney W. Grimes * in ``non-process'' (i.e., interrupt) work. 740df8bae1dSRodney W. Grimes */ 741e0f66ef8SJohn Baldwin if ((td->td_pflags & TDP_ITHREAD) || 742e0f66ef8SJohn Baldwin td->td_intr_nesting_level >= 2) { 743bcfd016cSAlexander Motin td->td_iticks += cnt; 744bcfd016cSAlexander Motin cp_time[CP_INTR] += cnt; 7450384fff8SJason Evans } else { 746bcfd016cSAlexander Motin td->td_pticks += cnt; 747bcfd016cSAlexander Motin td->td_sticks += cnt; 748486a9414SJulian Elischer if (!TD_IS_IDLETHREAD(td)) 749bcfd016cSAlexander Motin cp_time[CP_SYS] += cnt; 7500384fff8SJason Evans else 751bcfd016cSAlexander Motin cp_time[CP_IDLE] += cnt; 752df8bae1dSRodney W. Grimes } 7530384fff8SJason Evans } 754f5e9e8ecSBruce Evans 755f5e9e8ecSBruce Evans /* Update resource usage integrals and maximums. */ 75616f9f205SJohn Baldwin MPASS(p->p_vmspace != NULL); 75716f9f205SJohn Baldwin vm = p->p_vmspace; 7581c4bcd05SJeff Roberson ru = &td->td_ru; 759bcfd016cSAlexander Motin ru->ru_ixrss += pgtok(vm->vm_tsize) * cnt; 760bcfd016cSAlexander Motin ru->ru_idrss += pgtok(vm->vm_dsize) * cnt; 761bcfd016cSAlexander Motin ru->ru_isrss += pgtok(vm->vm_ssize) * cnt; 7621c6d46f9SLuoqi Chen rss = pgtok(vmspace_resident_count(vm)); 763f5e9e8ecSBruce Evans if (ru->ru_maxrss < rss) 764f5e9e8ecSBruce Evans ru->ru_maxrss = rss; 7658f51ad55SJeff Roberson KTR_POINT2(KTR_SCHED, "thread", sched_tdname(td), "statclock", 7668f51ad55SJeff Roberson "prio:%d", td->td_priority, "stathz:%d", (stathz)?stathz:hz); 767b3e9e682SRyan Stone SDT_PROBE2(sched, , , tick, td, td->td_proc); 7687628402bSPeter Wemm thread_lock_flags(td, MTX_QUIET); 769bcfd016cSAlexander Motin for ( ; cnt > 0; cnt--) 77040acdeabSJeff Roberson sched_clock(td); 77140acdeabSJeff Roberson thread_unlock(td); 772f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 773f5f9340bSFabien Thomas if (td->td_intr_frame != NULL) 774f5f9340bSFabien Thomas PMC_SOFT_CALL_TF( , , clock, stat, td->td_intr_frame); 775f5f9340bSFabien Thomas #endif 7766caa8a15SJohn Baldwin } 7776c567274SJohn Baldwin 7786caa8a15SJohn Baldwin void 779b439e431SJohn Baldwin profclock(int usermode, uintfptr_t pc) 7806caa8a15SJohn Baldwin { 781bcfd016cSAlexander Motin 782bcfd016cSAlexander Motin profclock_cnt(1, usermode, pc); 783bcfd016cSAlexander Motin } 784bcfd016cSAlexander Motin 785bcfd016cSAlexander Motin void 786bcfd016cSAlexander Motin profclock_cnt(int cnt, int usermode, uintfptr_t pc) 787bcfd016cSAlexander Motin { 788238dd320SJake Burkholder struct thread *td; 789238dd320SJake Burkholder #ifdef GPROF 790238dd320SJake Burkholder struct gmonparam *g; 7915c8b4441SJohn Baldwin uintfptr_t i; 792238dd320SJake Burkholder #endif 7936caa8a15SJohn Baldwin 7944a338afdSJulian Elischer td = curthread; 795b439e431SJohn Baldwin if (usermode) { 796238dd320SJake Burkholder /* 797238dd320SJake Burkholder * Came from user mode; CPU was in user state. 798238dd320SJake Burkholder * If this process is being profiled, record the tick. 799a282253aSJulian Elischer * if there is no related user location yet, don't 800a282253aSJulian Elischer * bother trying to count it. 801238dd320SJake Burkholder */ 8029752f794SJohn Baldwin if (td->td_proc->p_flag & P_PROFIL) 803bcfd016cSAlexander Motin addupc_intr(td, pc, cnt); 804238dd320SJake Burkholder } 805238dd320SJake Burkholder #ifdef GPROF 806238dd320SJake Burkholder else { 807238dd320SJake Burkholder /* 808238dd320SJake Burkholder * Kernel statistics are just like addupc_intr, only easier. 809238dd320SJake Burkholder */ 810238dd320SJake Burkholder g = &_gmonparam; 811b439e431SJohn Baldwin if (g->state == GMON_PROF_ON && pc >= g->lowpc) { 812b439e431SJohn Baldwin i = PC_TO_I(g, pc); 813238dd320SJake Burkholder if (i < g->textsize) { 814bcfd016cSAlexander Motin KCOUNT(g, i) += cnt; 815238dd320SJake Burkholder } 816238dd320SJake Burkholder } 817238dd320SJake Burkholder } 818238dd320SJake Burkholder #endif 8191af19ee4SAlexander Motin #ifdef HWPMC_HOOKS 8201af19ee4SAlexander Motin if (td->td_intr_frame != NULL) 8211af19ee4SAlexander Motin PMC_SOFT_CALL_TF( , , clock, prof, td->td_intr_frame); 8221af19ee4SAlexander Motin #endif 823df8bae1dSRodney W. Grimes } 824df8bae1dSRodney W. Grimes 825df8bae1dSRodney W. Grimes /* 826df8bae1dSRodney W. Grimes * Return information about system clocks. 827df8bae1dSRodney W. Grimes */ 828787d58f2SPoul-Henning Kamp static int 82982d9ae4eSPoul-Henning Kamp sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS) 830df8bae1dSRodney W. Grimes { 831df8bae1dSRodney W. Grimes struct clockinfo clkinfo; 832df8bae1dSRodney W. Grimes /* 833df8bae1dSRodney W. Grimes * Construct clockinfo structure. 834df8bae1dSRodney W. Grimes */ 835a9a0f15aSBruce Evans bzero(&clkinfo, sizeof(clkinfo)); 836df8bae1dSRodney W. Grimes clkinfo.hz = hz; 837df8bae1dSRodney W. Grimes clkinfo.tick = tick; 838df8bae1dSRodney W. Grimes clkinfo.profhz = profhz; 839df8bae1dSRodney W. Grimes clkinfo.stathz = stathz ? stathz : hz; 840ae0eb976SPoul-Henning Kamp return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req)); 841df8bae1dSRodney W. Grimes } 8423f31c649SGarrett Wollman 843c383c221SEd Schouten SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, 844c383c221SEd Schouten CTLTYPE_STRUCT|CTLFLAG_RD|CTLFLAG_MPSAFE, 845af1408e3SLuigi Rizzo 0, 0, sysctl_kern_clockrate, "S,clockinfo", 846af1408e3SLuigi Rizzo "Rate and period of various kernel clocks"); 847370c3cb5SSean Kelly 8484103b765SPoul-Henning Kamp static void 8499079fff5SNick Hibma watchdog_config(void *unused __unused, u_int cmd, int *error) 850370c3cb5SSean Kelly { 8514103b765SPoul-Henning Kamp u_int u; 852370c3cb5SSean Kelly 8534103b765SPoul-Henning Kamp u = cmd & WD_INTERVAL; 8549079fff5SNick Hibma if (u >= WD_TO_1SEC) { 8554103b765SPoul-Henning Kamp watchdog_ticks = (1 << (u - WD_TO_1SEC)) * hz; 8564103b765SPoul-Henning Kamp watchdog_enabled = 1; 8579079fff5SNick Hibma *error = 0; 8584103b765SPoul-Henning Kamp } else { 8594103b765SPoul-Henning Kamp watchdog_enabled = 0; 860370c3cb5SSean Kelly } 8614103b765SPoul-Henning Kamp } 862370c3cb5SSean Kelly 863370c3cb5SSean Kelly /* 864370c3cb5SSean Kelly * Handle a watchdog timeout by dumping interrupt information and 865911d16b8SEd Maste * then either dropping to DDB or panicking. 866370c3cb5SSean Kelly */ 867370c3cb5SSean Kelly static void 868370c3cb5SSean Kelly watchdog_fire(void) 869370c3cb5SSean Kelly { 870370c3cb5SSean Kelly int nintr; 87160ae52f7SEd Schouten uint64_t inttotal; 872370c3cb5SSean Kelly u_long *curintr; 873370c3cb5SSean Kelly char *curname; 874370c3cb5SSean Kelly 875370c3cb5SSean Kelly curintr = intrcnt; 876370c3cb5SSean Kelly curname = intrnames; 877370c3cb5SSean Kelly inttotal = 0; 878556a5850SAlexander Motin nintr = sintrcnt / sizeof(u_long); 879370c3cb5SSean Kelly 880370c3cb5SSean Kelly printf("interrupt total\n"); 881370c3cb5SSean Kelly while (--nintr >= 0) { 882370c3cb5SSean Kelly if (*curintr) 883370c3cb5SSean Kelly printf("%-12s %20lu\n", curname, *curintr); 884370c3cb5SSean Kelly curname += strlen(curname) + 1; 885370c3cb5SSean Kelly inttotal += *curintr++; 886370c3cb5SSean Kelly } 8876cda4155SSean Kelly printf("Total %20ju\n", (uintmax_t)inttotal); 888911d16b8SEd Maste 889911d16b8SEd Maste #if defined(KDB) && !defined(KDB_UNATTENDED) 890911d16b8SEd Maste kdb_backtrace(); 8913de213ccSRobert Watson kdb_enter(KDB_WHY_WATCHDOG, "watchdog timeout"); 892911d16b8SEd Maste #else 893370c3cb5SSean Kelly panic("watchdog timeout"); 894911d16b8SEd Maste #endif 895370c3cb5SSean Kelly } 896