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 * 3. All advertising materials mentioning features or use of this software 19df8bae1dSRodney W. Grimes * must display the following acknowledgement: 20df8bae1dSRodney W. Grimes * This product includes software developed by the University of 21df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 22df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 23df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 24df8bae1dSRodney W. Grimes * without specific prior written permission. 25df8bae1dSRodney W. Grimes * 26df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36df8bae1dSRodney W. Grimes * SUCH DAMAGE. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 41677b542eSDavid E. O'Brien #include <sys/cdefs.h> 42677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 43677b542eSDavid E. O'Brien 4432c20357SPoul-Henning Kamp #include "opt_ntp.h" 45370c3cb5SSean Kelly #include "opt_ddb.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> 51df8bae1dSRodney W. Grimes #include <sys/kernel.h> 52f34fa851SJohn Baldwin #include <sys/lock.h> 5361d80e90SJohn Baldwin #include <sys/ktr.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> 606caa8a15SJohn Baldwin #include <sys/smp.h> 618a129caeSDavid Greenman #include <vm/vm.h> 62efeaf95aSDavid Greenman #include <vm/pmap.h> 63efeaf95aSDavid Greenman #include <vm/vm_map.h> 64797f2d22SPoul-Henning Kamp #include <sys/sysctl.h> 658088699fSJohn Baldwin #include <sys/bus.h> 668088699fSJohn Baldwin #include <sys/interrupt.h> 67104a9b7eSAlexander Kabaev #include <sys/limits.h> 68e7fa55afSPoul-Henning Kamp #include <sys/timetc.h> 69df8bae1dSRodney W. Grimes 70df8bae1dSRodney W. Grimes #include <machine/cpu.h> 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes #ifdef GPROF 73df8bae1dSRodney W. Grimes #include <sys/gmon.h> 74df8bae1dSRodney W. Grimes #endif 75df8bae1dSRodney W. Grimes 76370c3cb5SSean Kelly #ifdef DDB 77370c3cb5SSean Kelly #include <ddb/ddb.h> 78370c3cb5SSean Kelly #endif 79370c3cb5SSean Kelly 80e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 81e4fc250cSLuigi Rizzo extern void hardclock_device_poll(void); 82e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */ 83eae8fc2cSSteve Passe 844d77a549SAlfred Perlstein static void initclocks(void *dummy); 852b14f991SJulian Elischer SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL) 862b14f991SJulian Elischer 87f23b4c91SGarrett Wollman /* Some of these don't belong here, but it's easiest to concentrate them. */ 88eae8fc2cSSteve Passe long cp_time[CPUSTATES]; 89f23b4c91SGarrett Wollman 907f112b04SRobert Watson SYSCTL_OPAQUE(_kern, OID_AUTO, cp_time, CTLFLAG_RD, &cp_time, sizeof(cp_time), 917f112b04SRobert Watson "LU", "CPU time statistics"); 927f112b04SRobert Watson 93370c3cb5SSean Kelly #ifdef WATCHDOG 94370c3cb5SSean Kelly static int sysctl_watchdog_reset(SYSCTL_HANDLER_ARGS); 95370c3cb5SSean Kelly static void watchdog_fire(void); 96370c3cb5SSean Kelly 97370c3cb5SSean Kelly static int watchdog_enabled; 98370c3cb5SSean Kelly static unsigned int watchdog_ticks; 99370c3cb5SSean Kelly static int watchdog_timeout = 20; 100370c3cb5SSean Kelly 101370c3cb5SSean Kelly SYSCTL_NODE(_debug, OID_AUTO, watchdog, CTLFLAG_RW, 0, "System watchdog"); 102370c3cb5SSean Kelly SYSCTL_INT(_debug_watchdog, OID_AUTO, enabled, CTLFLAG_RW, &watchdog_enabled, 103370c3cb5SSean Kelly 0, "Enable the watchdog"); 104370c3cb5SSean Kelly SYSCTL_INT(_debug_watchdog, OID_AUTO, timeout, CTLFLAG_RW, &watchdog_timeout, 105370c3cb5SSean Kelly 0, "Timeout for watchdog checkins"); 106370c3cb5SSean Kelly 107370c3cb5SSean Kelly #endif /* WATCHDOG */ 108370c3cb5SSean Kelly 1093bac064fSPoul-Henning Kamp /* 110df8bae1dSRodney W. Grimes * Clock handling routines. 111df8bae1dSRodney W. Grimes * 112b05dcf3cSPoul-Henning Kamp * This code is written to operate with two timers that run independently of 113b05dcf3cSPoul-Henning Kamp * each other. 1147ec73f64SPoul-Henning Kamp * 115b05dcf3cSPoul-Henning Kamp * The main timer, running hz times per second, is used to trigger interval 116b05dcf3cSPoul-Henning Kamp * timers, timeouts and rescheduling as needed. 1177ec73f64SPoul-Henning Kamp * 118b05dcf3cSPoul-Henning Kamp * The second timer handles kernel and user profiling, 119b05dcf3cSPoul-Henning Kamp * and does resource use estimation. If the second timer is programmable, 120b05dcf3cSPoul-Henning Kamp * it is randomized to avoid aliasing between the two clocks. For example, 121b05dcf3cSPoul-Henning Kamp * the randomization prevents an adversary from always giving up the cpu 122df8bae1dSRodney W. Grimes * just before its quantum expires. Otherwise, it would never accumulate 123df8bae1dSRodney W. Grimes * cpu ticks. The mean frequency of the second timer is stathz. 124b05dcf3cSPoul-Henning Kamp * 125b05dcf3cSPoul-Henning Kamp * If no second timer exists, stathz will be zero; in this case we drive 126b05dcf3cSPoul-Henning Kamp * profiling and statistics off the main clock. This WILL NOT be accurate; 127b05dcf3cSPoul-Henning Kamp * do not do it unless absolutely necessary. 128b05dcf3cSPoul-Henning Kamp * 129df8bae1dSRodney W. Grimes * The statistics clock may (or may not) be run at a higher rate while 130b05dcf3cSPoul-Henning Kamp * profiling. This profile clock runs at profhz. We require that profhz 131b05dcf3cSPoul-Henning Kamp * be an integral multiple of stathz. 132b05dcf3cSPoul-Henning Kamp * 133b05dcf3cSPoul-Henning Kamp * If the statistics clock is running fast, it must be divided by the ratio 134b05dcf3cSPoul-Henning Kamp * profhz/stathz for statistics. (For profiling, every tick counts.) 135df8bae1dSRodney W. Grimes * 1367ec73f64SPoul-Henning Kamp * Time-of-day is maintained using a "timecounter", which may or may 1377ec73f64SPoul-Henning Kamp * not be related to the hardware generating the above mentioned 1387ec73f64SPoul-Henning Kamp * interrupts. 139df8bae1dSRodney W. Grimes */ 140df8bae1dSRodney W. Grimes 141df8bae1dSRodney W. Grimes int stathz; 142df8bae1dSRodney W. Grimes int profhz; 143238dd320SJake Burkholder int profprocs; 144df8bae1dSRodney W. Grimes int ticks; 145238dd320SJake Burkholder int psratio; 146df8bae1dSRodney W. Grimes 147df8bae1dSRodney W. Grimes /* 148df8bae1dSRodney W. Grimes * Initialize clock frequencies and start both clocks running. 149df8bae1dSRodney W. Grimes */ 1502b14f991SJulian Elischer /* ARGSUSED*/ 1512b14f991SJulian Elischer static void 152d841aaa7SBruce Evans initclocks(dummy) 153d841aaa7SBruce Evans void *dummy; 154df8bae1dSRodney W. Grimes { 155df8bae1dSRodney W. Grimes register int i; 156df8bae1dSRodney W. Grimes 157df8bae1dSRodney W. Grimes /* 158df8bae1dSRodney W. Grimes * Set divisors to 1 (normal case) and let the machine-specific 159df8bae1dSRodney W. Grimes * code do its bit. 160df8bae1dSRodney W. Grimes */ 161df8bae1dSRodney W. Grimes cpu_initclocks(); 162df8bae1dSRodney W. Grimes 163df8bae1dSRodney W. Grimes /* 164df8bae1dSRodney W. Grimes * Compute profhz/stathz, and fix profhz if needed. 165df8bae1dSRodney W. Grimes */ 166df8bae1dSRodney W. Grimes i = stathz ? stathz : hz; 167df8bae1dSRodney W. Grimes if (profhz == 0) 168df8bae1dSRodney W. Grimes profhz = i; 169df8bae1dSRodney W. Grimes psratio = profhz / i; 170df8bae1dSRodney W. Grimes } 171df8bae1dSRodney W. Grimes 172df8bae1dSRodney W. Grimes /* 173238dd320SJake Burkholder * Each time the real-time timer fires, this function is called on all CPUs. 174238dd320SJake Burkholder * Note that hardclock() calls hardclock_process() for the boot CPU, so only 175238dd320SJake Burkholder * the other CPUs in the system need to call this function. 1766caa8a15SJohn Baldwin */ 1776caa8a15SJohn Baldwin void 178238dd320SJake Burkholder hardclock_process(frame) 179238dd320SJake Burkholder register struct clockframe *frame; 1806caa8a15SJohn Baldwin { 1816caa8a15SJohn Baldwin struct pstats *pstats; 182238dd320SJake Burkholder struct thread *td = curthread; 183b40ce416SJulian Elischer struct proc *p = td->td_proc; 1846caa8a15SJohn Baldwin 1856caa8a15SJohn Baldwin /* 1866caa8a15SJohn Baldwin * Run current process's virtual and profile time, as needed. 1876caa8a15SJohn Baldwin */ 188238dd320SJake Burkholder mtx_lock_spin_flags(&sched_lock, MTX_QUIET); 1890e2a4d3aSDavid Xu if (p->p_flag & P_SA) { 190b40ce416SJulian Elischer /* XXXKSE What to do? */ 191b40ce416SJulian Elischer } else { 1926caa8a15SJohn Baldwin pstats = p->p_stats; 193238dd320SJake Burkholder if (CLKF_USERMODE(frame) && 1946caa8a15SJohn Baldwin timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) && 195b40ce416SJulian Elischer itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) { 196b40ce416SJulian Elischer p->p_sflag |= PS_ALRMPEND; 1974a338afdSJulian Elischer td->td_flags |= TDF_ASTPENDING; 198b40ce416SJulian Elischer } 1996caa8a15SJohn Baldwin if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) && 200b40ce416SJulian Elischer itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) { 201b40ce416SJulian Elischer p->p_sflag |= PS_PROFPEND; 2024a338afdSJulian Elischer td->td_flags |= TDF_ASTPENDING; 203b40ce416SJulian Elischer } 204b40ce416SJulian Elischer } 205238dd320SJake Burkholder mtx_unlock_spin_flags(&sched_lock, MTX_QUIET); 2066caa8a15SJohn Baldwin } 2076caa8a15SJohn Baldwin 2086caa8a15SJohn Baldwin /* 209df8bae1dSRodney W. Grimes * The real-time timer, interrupting hz times per second. 210df8bae1dSRodney W. Grimes */ 211df8bae1dSRodney W. Grimes void 212df8bae1dSRodney W. Grimes hardclock(frame) 213df8bae1dSRodney W. Grimes register struct clockframe *frame; 214df8bae1dSRodney W. Grimes { 215fa2fbc3dSJake Burkholder int need_softclock = 0; 216df8bae1dSRodney W. Grimes 217c9f4877dSJake Burkholder CTR0(KTR_CLK, "hardclock fired"); 218238dd320SJake Burkholder hardclock_process(frame); 219b05dcf3cSPoul-Henning Kamp 220e7fa55afSPoul-Henning Kamp tc_ticktock(); 221df8bae1dSRodney W. Grimes /* 222df8bae1dSRodney W. Grimes * If no separate statistics clock is available, run it from here. 2236caa8a15SJohn Baldwin * 2246caa8a15SJohn Baldwin * XXX: this only works for UP 225df8bae1dSRodney W. Grimes */ 226238dd320SJake Burkholder if (stathz == 0) { 227238dd320SJake Burkholder profclock(frame); 228df8bae1dSRodney W. Grimes statclock(frame); 229238dd320SJake Burkholder } 230df8bae1dSRodney W. Grimes 231e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING 232daccb638SLuigi Rizzo hardclock_device_poll(); /* this is very short and quick */ 233e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */ 2343f31c649SGarrett Wollman 235b05dcf3cSPoul-Henning Kamp /* 236b05dcf3cSPoul-Henning Kamp * Process callouts at a very low cpu priority, so we don't keep the 237b05dcf3cSPoul-Henning Kamp * relatively high clock interrupt priority any longer than necessary. 238b05dcf3cSPoul-Henning Kamp */ 23921a7a9aeSJohn Baldwin mtx_lock_spin_flags(&callout_lock, MTX_QUIET); 240fa2fbc3dSJake Burkholder ticks++; 241b05dcf3cSPoul-Henning Kamp if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) { 242fa2fbc3dSJake Burkholder need_softclock = 1; 243b05dcf3cSPoul-Henning Kamp } else if (softticks + 1 == ticks) 244b05dcf3cSPoul-Henning Kamp ++softticks; 24521a7a9aeSJohn Baldwin mtx_unlock_spin_flags(&callout_lock, MTX_QUIET); 246fa2fbc3dSJake Burkholder 247fa2fbc3dSJake Burkholder /* 248062d8ff5SJohn Baldwin * swi_sched acquires sched_lock, so we don't want to call it with 249fa2fbc3dSJake Burkholder * callout_lock held; incorrect locking order. 250fa2fbc3dSJake Burkholder */ 251fa2fbc3dSJake Burkholder if (need_softclock) 252c86b6ff5SJohn Baldwin swi_sched(softclock_ih, 0); 253370c3cb5SSean Kelly 254370c3cb5SSean Kelly #ifdef WATCHDOG 255370c3cb5SSean Kelly if (watchdog_enabled > 0 && 256370c3cb5SSean Kelly (int)(ticks - watchdog_ticks) >= (hz * watchdog_timeout)) 257370c3cb5SSean Kelly watchdog_fire(); 258370c3cb5SSean Kelly #endif /* WATCHDOG */ 259ab36c067SJustin T. Gibbs } 260ab36c067SJustin T. Gibbs 261df8bae1dSRodney W. Grimes /* 262227ee8a1SPoul-Henning Kamp * Compute number of ticks in the specified amount of time. 263df8bae1dSRodney W. Grimes */ 264df8bae1dSRodney W. Grimes int 265227ee8a1SPoul-Henning Kamp tvtohz(tv) 266df8bae1dSRodney W. Grimes struct timeval *tv; 267df8bae1dSRodney W. Grimes { 2686976af69SBruce Evans register unsigned long ticks; 2696976af69SBruce Evans register long sec, usec; 270df8bae1dSRodney W. Grimes 271df8bae1dSRodney W. Grimes /* 2726976af69SBruce Evans * If the number of usecs in the whole seconds part of the time 2736976af69SBruce Evans * difference fits in a long, then the total number of usecs will 2746976af69SBruce Evans * fit in an unsigned long. Compute the total and convert it to 2756976af69SBruce Evans * ticks, rounding up and adding 1 to allow for the current tick 2766976af69SBruce Evans * to expire. Rounding also depends on unsigned long arithmetic 2776976af69SBruce Evans * to avoid overflow. 278df8bae1dSRodney W. Grimes * 2796976af69SBruce Evans * Otherwise, if the number of ticks in the whole seconds part of 2806976af69SBruce Evans * the time difference fits in a long, then convert the parts to 2816976af69SBruce Evans * ticks separately and add, using similar rounding methods and 2826976af69SBruce Evans * overflow avoidance. This method would work in the previous 2836976af69SBruce Evans * case but it is slightly slower and assumes that hz is integral. 2846976af69SBruce Evans * 2856976af69SBruce Evans * Otherwise, round the time difference down to the maximum 2866976af69SBruce Evans * representable value. 2876976af69SBruce Evans * 2886976af69SBruce Evans * If ints have 32 bits, then the maximum value for any timeout in 2896976af69SBruce Evans * 10ms ticks is 248 days. 290df8bae1dSRodney W. Grimes */ 291227ee8a1SPoul-Henning Kamp sec = tv->tv_sec; 292227ee8a1SPoul-Henning Kamp usec = tv->tv_usec; 2936976af69SBruce Evans if (usec < 0) { 2946976af69SBruce Evans sec--; 2956976af69SBruce Evans usec += 1000000; 2966976af69SBruce Evans } 2976976af69SBruce Evans if (sec < 0) { 2986976af69SBruce Evans #ifdef DIAGNOSTIC 299b05dcf3cSPoul-Henning Kamp if (usec > 0) { 3007ec73f64SPoul-Henning Kamp sec++; 3017ec73f64SPoul-Henning Kamp usec -= 1000000; 3027ec73f64SPoul-Henning Kamp } 303227ee8a1SPoul-Henning Kamp printf("tvotohz: negative time difference %ld sec %ld usec\n", 3046976af69SBruce Evans sec, usec); 3056976af69SBruce Evans #endif 3066976af69SBruce Evans ticks = 1; 3076976af69SBruce Evans } else if (sec <= LONG_MAX / 1000000) 3086976af69SBruce Evans ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1)) 3096976af69SBruce Evans / tick + 1; 3106976af69SBruce Evans else if (sec <= LONG_MAX / hz) 3116976af69SBruce Evans ticks = sec * hz 3126976af69SBruce Evans + ((unsigned long)usec + (tick - 1)) / tick + 1; 3136976af69SBruce Evans else 3146976af69SBruce Evans ticks = LONG_MAX; 3156976af69SBruce Evans if (ticks > INT_MAX) 3166976af69SBruce Evans ticks = INT_MAX; 317d6116663SAlexander Langer return ((int)ticks); 318df8bae1dSRodney W. Grimes } 319df8bae1dSRodney W. Grimes 320df8bae1dSRodney W. Grimes /* 321df8bae1dSRodney W. Grimes * Start profiling on a process. 322df8bae1dSRodney W. Grimes * 323df8bae1dSRodney W. Grimes * Kernel profiling passes proc0 which never exits and hence 324df8bae1dSRodney W. Grimes * keeps the profile clock running constantly. 325df8bae1dSRodney W. Grimes */ 326df8bae1dSRodney W. Grimes void 327df8bae1dSRodney W. Grimes startprofclock(p) 328df8bae1dSRodney W. Grimes register struct proc *p; 329df8bae1dSRodney W. Grimes { 330df8bae1dSRodney W. Grimes 33101cd094cSJohn Baldwin /* 33201cd094cSJohn Baldwin * XXX; Right now sched_lock protects statclock(), but perhaps 33301cd094cSJohn Baldwin * it should be protected later on by a time_lock, which would 33401cd094cSJohn Baldwin * cover psdiv, etc. as well. 33501cd094cSJohn Baldwin */ 3369752f794SJohn Baldwin PROC_LOCK_ASSERT(p, MA_OWNED); 3379752f794SJohn Baldwin if (p->p_flag & P_STOPPROF) 338a282253aSJulian Elischer return; 3399752f794SJohn Baldwin if ((p->p_flag & P_PROFIL) == 0) { 3409752f794SJohn Baldwin mtx_lock_spin(&sched_lock); 3419752f794SJohn Baldwin p->p_flag |= P_PROFIL; 342238dd320SJake Burkholder if (++profprocs == 1) 343238dd320SJake Burkholder cpu_startprofclock(); 3449ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 345df8bae1dSRodney W. Grimes } 3469752f794SJohn Baldwin } 347df8bae1dSRodney W. Grimes 348df8bae1dSRodney W. Grimes /* 349df8bae1dSRodney W. Grimes * Stop profiling on a process. 350df8bae1dSRodney W. Grimes */ 351df8bae1dSRodney W. Grimes void 352df8bae1dSRodney W. Grimes stopprofclock(p) 353df8bae1dSRodney W. Grimes register struct proc *p; 354df8bae1dSRodney W. Grimes { 355df8bae1dSRodney W. Grimes 356a282253aSJulian Elischer PROC_LOCK_ASSERT(p, MA_OWNED); 3579752f794SJohn Baldwin if (p->p_flag & P_PROFIL) { 3589752f794SJohn Baldwin if (p->p_profthreads != 0) { 3599752f794SJohn Baldwin p->p_flag |= P_STOPPROF; 3609752f794SJohn Baldwin while (p->p_profthreads != 0) 361a282253aSJulian Elischer msleep(&p->p_profthreads, &p->p_mtx, PPAUSE, 362a89ec05eSPeter Wemm "stopprof", 0); 3639752f794SJohn Baldwin p->p_flag &= ~P_STOPPROF; 364a282253aSJulian Elischer } 3659752f794SJohn Baldwin mtx_lock_spin(&sched_lock); 3669752f794SJohn Baldwin p->p_flag &= ~P_PROFIL; 367238dd320SJake Burkholder if (--profprocs == 0) 368238dd320SJake Burkholder cpu_stopprofclock(); 3699ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 370df8bae1dSRodney W. Grimes } 3719752f794SJohn Baldwin } 372df8bae1dSRodney W. Grimes 373df8bae1dSRodney W. Grimes /* 374238dd320SJake Burkholder * Statistics clock. Grab profile sample, and if divider reaches 0, 375238dd320SJake Burkholder * do process and kernel statistics. Most of the statistics are only 37671a62f8aSBruce Evans * used by user-level statistics programs. The main exceptions are 377238dd320SJake Burkholder * ke->ke_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu. 378238dd320SJake Burkholder * This should be called by all active processors. 379df8bae1dSRodney W. Grimes */ 380df8bae1dSRodney W. Grimes void 381238dd320SJake Burkholder statclock(frame) 382238dd320SJake Burkholder register struct clockframe *frame; 383df8bae1dSRodney W. Grimes { 3848a129caeSDavid Greenman struct pstats *pstats; 3858a129caeSDavid Greenman struct rusage *ru; 3868a129caeSDavid Greenman struct vmspace *vm; 387238dd320SJake Burkholder struct thread *td; 388238dd320SJake Burkholder struct proc *p; 389238dd320SJake Burkholder long rss; 3908a129caeSDavid Greenman 391238dd320SJake Burkholder td = curthread; 392238dd320SJake Burkholder p = td->td_proc; 393238dd320SJake Burkholder 394238dd320SJake Burkholder mtx_lock_spin_flags(&sched_lock, MTX_QUIET); 395238dd320SJake Burkholder if (CLKF_USERMODE(frame)) { 396df8bae1dSRodney W. Grimes /* 39771a62f8aSBruce Evans * Charge the time as appropriate. 398df8bae1dSRodney W. Grimes */ 3990e2a4d3aSDavid Xu if (p->p_flag & P_SA) 4005215b187SJeff Roberson thread_statclock(1); 401e4625663SJeff Roberson p->p_uticks++; 4027cf90fb3SJeff Roberson if (td->td_ksegrp->kg_nice > NZERO) 403df8bae1dSRodney W. Grimes cp_time[CP_NICE]++; 404df8bae1dSRodney W. Grimes else 405df8bae1dSRodney W. Grimes cp_time[CP_USER]++; 406df8bae1dSRodney W. Grimes } else { 407df8bae1dSRodney W. Grimes /* 408df8bae1dSRodney W. Grimes * Came from kernel mode, so we were: 409df8bae1dSRodney W. Grimes * - handling an interrupt, 410df8bae1dSRodney W. Grimes * - doing syscall or trap work on behalf of the current 411df8bae1dSRodney W. Grimes * user process, or 412df8bae1dSRodney W. Grimes * - spinning in the idle loop. 413df8bae1dSRodney W. Grimes * Whichever it is, charge the time as appropriate. 414df8bae1dSRodney W. Grimes * Note that we charge interrupts to the current process, 415df8bae1dSRodney W. Grimes * regardless of whether they are ``for'' that process, 416df8bae1dSRodney W. Grimes * so that we know how much of its real time was spent 417df8bae1dSRodney W. Grimes * in ``non-process'' (i.e., interrupt) work. 418df8bae1dSRodney W. Grimes */ 419b40ce416SJulian Elischer if ((td->td_ithd != NULL) || td->td_intr_nesting_level >= 2) { 420e4625663SJeff Roberson p->p_iticks++; 421df8bae1dSRodney W. Grimes cp_time[CP_INTR]++; 4220384fff8SJason Evans } else { 4230e2a4d3aSDavid Xu if (p->p_flag & P_SA) 4245215b187SJeff Roberson thread_statclock(0); 425e4625663SJeff Roberson td->td_sticks++; 426e4625663SJeff Roberson p->p_sticks++; 427b40ce416SJulian Elischer if (p != PCPU_GET(idlethread)->td_proc) 428df8bae1dSRodney W. Grimes cp_time[CP_SYS]++; 4290384fff8SJason Evans else 430df8bae1dSRodney W. Grimes cp_time[CP_IDLE]++; 431df8bae1dSRodney W. Grimes } 4320384fff8SJason Evans } 433df8bae1dSRodney W. Grimes 4347cf90fb3SJeff Roberson sched_clock(td); 435f5e9e8ecSBruce Evans 436f5e9e8ecSBruce Evans /* Update resource usage integrals and maximums. */ 437f5e9e8ecSBruce Evans if ((pstats = p->p_stats) != NULL && 438f5e9e8ecSBruce Evans (ru = &pstats->p_ru) != NULL && 439f5e9e8ecSBruce Evans (vm = p->p_vmspace) != NULL) { 4401c6d46f9SLuoqi Chen ru->ru_ixrss += pgtok(vm->vm_tsize); 4411c6d46f9SLuoqi Chen ru->ru_idrss += pgtok(vm->vm_dsize); 4421c6d46f9SLuoqi Chen ru->ru_isrss += pgtok(vm->vm_ssize); 4431c6d46f9SLuoqi Chen rss = pgtok(vmspace_resident_count(vm)); 444f5e9e8ecSBruce Evans if (ru->ru_maxrss < rss) 445f5e9e8ecSBruce Evans ru->ru_maxrss = rss; 446f5e9e8ecSBruce Evans } 447238dd320SJake Burkholder mtx_unlock_spin_flags(&sched_lock, MTX_QUIET); 4486caa8a15SJohn Baldwin } 4496c567274SJohn Baldwin 4506caa8a15SJohn Baldwin void 451238dd320SJake Burkholder profclock(frame) 4526caa8a15SJohn Baldwin register struct clockframe *frame; 4536caa8a15SJohn Baldwin { 454238dd320SJake Burkholder struct thread *td; 455238dd320SJake Burkholder #ifdef GPROF 456238dd320SJake Burkholder struct gmonparam *g; 457238dd320SJake Burkholder int i; 458238dd320SJake Burkholder #endif 4596caa8a15SJohn Baldwin 4604a338afdSJulian Elischer td = curthread; 461238dd320SJake Burkholder if (CLKF_USERMODE(frame)) { 462238dd320SJake Burkholder /* 463238dd320SJake Burkholder * Came from user mode; CPU was in user state. 464238dd320SJake Burkholder * If this process is being profiled, record the tick. 465a282253aSJulian Elischer * if there is no related user location yet, don't 466a282253aSJulian Elischer * bother trying to count it. 467238dd320SJake Burkholder */ 468238dd320SJake Burkholder td = curthread; 4699752f794SJohn Baldwin if (td->td_proc->p_flag & P_PROFIL) 4704a338afdSJulian Elischer addupc_intr(td, CLKF_PC(frame), 1); 471238dd320SJake Burkholder } 472238dd320SJake Burkholder #ifdef GPROF 473238dd320SJake Burkholder else { 474238dd320SJake Burkholder /* 475238dd320SJake Burkholder * Kernel statistics are just like addupc_intr, only easier. 476238dd320SJake Burkholder */ 477238dd320SJake Burkholder g = &_gmonparam; 478238dd320SJake Burkholder if (g->state == GMON_PROF_ON) { 479238dd320SJake Burkholder i = CLKF_PC(frame) - g->lowpc; 480238dd320SJake Burkholder if (i < g->textsize) { 481238dd320SJake Burkholder i /= HISTFRACTION * sizeof(*g->kcount); 482238dd320SJake Burkholder g->kcount[i]++; 483238dd320SJake Burkholder } 484238dd320SJake Burkholder } 485238dd320SJake Burkholder } 486238dd320SJake Burkholder #endif 487df8bae1dSRodney W. Grimes } 488df8bae1dSRodney W. Grimes 489df8bae1dSRodney W. Grimes /* 490df8bae1dSRodney W. Grimes * Return information about system clocks. 491df8bae1dSRodney W. Grimes */ 492787d58f2SPoul-Henning Kamp static int 49382d9ae4eSPoul-Henning Kamp sysctl_kern_clockrate(SYSCTL_HANDLER_ARGS) 494df8bae1dSRodney W. Grimes { 495df8bae1dSRodney W. Grimes struct clockinfo clkinfo; 496df8bae1dSRodney W. Grimes /* 497df8bae1dSRodney W. Grimes * Construct clockinfo structure. 498df8bae1dSRodney W. Grimes */ 499a9a0f15aSBruce Evans bzero(&clkinfo, sizeof(clkinfo)); 500df8bae1dSRodney W. Grimes clkinfo.hz = hz; 501df8bae1dSRodney W. Grimes clkinfo.tick = tick; 502df8bae1dSRodney W. Grimes clkinfo.profhz = profhz; 503df8bae1dSRodney W. Grimes clkinfo.stathz = stathz ? stathz : hz; 504ae0eb976SPoul-Henning Kamp return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req)); 505df8bae1dSRodney W. Grimes } 5063f31c649SGarrett Wollman 507946bb7a2SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD, 508af1408e3SLuigi Rizzo 0, 0, sysctl_kern_clockrate, "S,clockinfo", 509af1408e3SLuigi Rizzo "Rate and period of various kernel clocks"); 510370c3cb5SSean Kelly 511370c3cb5SSean Kelly #ifdef WATCHDOG 512370c3cb5SSean Kelly /* 513370c3cb5SSean Kelly * Reset the watchdog timer to ticks, thus preventing the watchdog 514370c3cb5SSean Kelly * from firing for another watchdog timeout period. 515370c3cb5SSean Kelly */ 516370c3cb5SSean Kelly static int 517370c3cb5SSean Kelly sysctl_watchdog_reset(SYSCTL_HANDLER_ARGS) 518370c3cb5SSean Kelly { 519370c3cb5SSean Kelly int ret; 520370c3cb5SSean Kelly 521370c3cb5SSean Kelly ret = 0; 522370c3cb5SSean Kelly watchdog_ticks = ticks; 523370c3cb5SSean Kelly return sysctl_handle_int(oidp, &ret, 0, req); 524370c3cb5SSean Kelly } 525370c3cb5SSean Kelly 526370c3cb5SSean Kelly SYSCTL_PROC(_debug_watchdog, OID_AUTO, reset, CTLFLAG_RW, 0, 0, 527370c3cb5SSean Kelly sysctl_watchdog_reset, "I", "Reset the watchdog"); 528370c3cb5SSean Kelly 529370c3cb5SSean Kelly /* 530370c3cb5SSean Kelly * Handle a watchdog timeout by dumping interrupt information and 531370c3cb5SSean Kelly * then either dropping to DDB or panicing. 532370c3cb5SSean Kelly */ 533370c3cb5SSean Kelly static void 534370c3cb5SSean Kelly watchdog_fire(void) 535370c3cb5SSean Kelly { 536370c3cb5SSean Kelly int nintr; 537370c3cb5SSean Kelly u_int64_t inttotal; 538370c3cb5SSean Kelly u_long *curintr; 539370c3cb5SSean Kelly char *curname; 540370c3cb5SSean Kelly 541370c3cb5SSean Kelly curintr = intrcnt; 542370c3cb5SSean Kelly curname = intrnames; 543370c3cb5SSean Kelly inttotal = 0; 544370c3cb5SSean Kelly nintr = eintrcnt - intrcnt; 545370c3cb5SSean Kelly 546370c3cb5SSean Kelly printf("interrupt total\n"); 547370c3cb5SSean Kelly while (--nintr >= 0) { 548370c3cb5SSean Kelly if (*curintr) 549370c3cb5SSean Kelly printf("%-12s %20lu\n", curname, *curintr); 550370c3cb5SSean Kelly curname += strlen(curname) + 1; 551370c3cb5SSean Kelly inttotal += *curintr++; 552370c3cb5SSean Kelly } 5536cda4155SSean Kelly printf("Total %20ju\n", (uintmax_t)inttotal); 554370c3cb5SSean Kelly 555370c3cb5SSean Kelly #ifdef DDB 556370c3cb5SSean Kelly db_print_backtrace(); 557370c3cb5SSean Kelly Debugger("watchdog timeout"); 558370c3cb5SSean Kelly #else /* !DDB */ 559370c3cb5SSean Kelly panic("watchdog timeout"); 560370c3cb5SSean Kelly #endif /* DDB */ 561370c3cb5SSean Kelly } 562370c3cb5SSean Kelly 563370c3cb5SSean Kelly #endif /* WATCHDOG */ 564