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 39c3aac50fSPeter Wemm * $FreeBSD$ 40df8bae1dSRodney W. Grimes */ 41df8bae1dSRodney W. Grimes 4232c20357SPoul-Henning Kamp #include "opt_ntp.h" 4332c20357SPoul-Henning Kamp 44df8bae1dSRodney W. Grimes #include <sys/param.h> 45df8bae1dSRodney W. Grimes #include <sys/systm.h> 46df8bae1dSRodney W. Grimes #include <sys/dkstat.h> 47df8bae1dSRodney W. Grimes #include <sys/callout.h> 48df8bae1dSRodney W. Grimes #include <sys/kernel.h> 49df8bae1dSRodney W. Grimes #include <sys/proc.h> 50df8bae1dSRodney W. Grimes #include <sys/resourcevar.h> 51797f2d22SPoul-Henning Kamp #include <sys/signalvar.h> 5291266b96SPoul-Henning Kamp #include <sys/timetc.h> 5332c20357SPoul-Henning Kamp #include <sys/timepps.h> 548a129caeSDavid Greenman #include <vm/vm.h> 55996c772fSJohn Dyson #include <sys/lock.h> 56efeaf95aSDavid Greenman #include <vm/pmap.h> 57efeaf95aSDavid Greenman #include <vm/vm_map.h> 58797f2d22SPoul-Henning Kamp #include <sys/sysctl.h> 59df8bae1dSRodney W. Grimes 60df8bae1dSRodney W. Grimes #include <machine/cpu.h> 61b1037dcdSBruce Evans #include <machine/limits.h> 62db6a4261SMatthew Dillon #include <machine/smp.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes #ifdef GPROF 65df8bae1dSRodney W. Grimes #include <sys/gmon.h> 66df8bae1dSRodney W. Grimes #endif 67df8bae1dSRodney W. Grimes 68eae8fc2cSSteve Passe 69d841aaa7SBruce Evans static void initclocks __P((void *dummy)); 702b14f991SJulian Elischer SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL) 712b14f991SJulian Elischer 72f23b4c91SGarrett Wollman /* Some of these don't belong here, but it's easiest to concentrate them. */ 73eae8fc2cSSteve Passe #if defined(SMP) && defined(BETTER_CLOCK) 74eae8fc2cSSteve Passe long cp_time[CPUSTATES]; 75eae8fc2cSSteve Passe #else 7627a0b398SPoul-Henning Kamp static long cp_time[CPUSTATES]; 77eae8fc2cSSteve Passe #endif 78f23b4c91SGarrett Wollman 79f23b4c91SGarrett Wollman long tk_cancc; 80f23b4c91SGarrett Wollman long tk_nin; 81f23b4c91SGarrett Wollman long tk_nout; 82f23b4c91SGarrett Wollman long tk_rawcc; 83f23b4c91SGarrett Wollman 843bac064fSPoul-Henning Kamp /* 85df8bae1dSRodney W. Grimes * Clock handling routines. 86df8bae1dSRodney W. Grimes * 87b05dcf3cSPoul-Henning Kamp * This code is written to operate with two timers that run independently of 88b05dcf3cSPoul-Henning Kamp * each other. 897ec73f64SPoul-Henning Kamp * 90b05dcf3cSPoul-Henning Kamp * The main timer, running hz times per second, is used to trigger interval 91b05dcf3cSPoul-Henning Kamp * timers, timeouts and rescheduling as needed. 927ec73f64SPoul-Henning Kamp * 93b05dcf3cSPoul-Henning Kamp * The second timer handles kernel and user profiling, 94b05dcf3cSPoul-Henning Kamp * and does resource use estimation. If the second timer is programmable, 95b05dcf3cSPoul-Henning Kamp * it is randomized to avoid aliasing between the two clocks. For example, 96b05dcf3cSPoul-Henning Kamp * the randomization prevents an adversary from always giving up the cpu 97df8bae1dSRodney W. Grimes * just before its quantum expires. Otherwise, it would never accumulate 98df8bae1dSRodney W. Grimes * cpu ticks. The mean frequency of the second timer is stathz. 99b05dcf3cSPoul-Henning Kamp * 100b05dcf3cSPoul-Henning Kamp * If no second timer exists, stathz will be zero; in this case we drive 101b05dcf3cSPoul-Henning Kamp * profiling and statistics off the main clock. This WILL NOT be accurate; 102b05dcf3cSPoul-Henning Kamp * do not do it unless absolutely necessary. 103b05dcf3cSPoul-Henning Kamp * 104df8bae1dSRodney W. Grimes * The statistics clock may (or may not) be run at a higher rate while 105b05dcf3cSPoul-Henning Kamp * profiling. This profile clock runs at profhz. We require that profhz 106b05dcf3cSPoul-Henning Kamp * be an integral multiple of stathz. 107b05dcf3cSPoul-Henning Kamp * 108b05dcf3cSPoul-Henning Kamp * If the statistics clock is running fast, it must be divided by the ratio 109b05dcf3cSPoul-Henning Kamp * profhz/stathz for statistics. (For profiling, every tick counts.) 110df8bae1dSRodney W. Grimes * 1117ec73f64SPoul-Henning Kamp * Time-of-day is maintained using a "timecounter", which may or may 1127ec73f64SPoul-Henning Kamp * not be related to the hardware generating the above mentioned 1137ec73f64SPoul-Henning Kamp * interrupts. 114df8bae1dSRodney W. Grimes */ 115df8bae1dSRodney W. Grimes 116df8bae1dSRodney W. Grimes int stathz; 117df8bae1dSRodney W. Grimes int profhz; 118cc3d5226SBruce Evans static int profprocs; 119df8bae1dSRodney W. Grimes int ticks; 120df8bae1dSRodney W. Grimes static int psdiv, pscnt; /* prof => stat divider */ 121cc3d5226SBruce Evans int psratio; /* ratio: prof / stat */ 122df8bae1dSRodney W. Grimes 123df8bae1dSRodney W. Grimes /* 124df8bae1dSRodney W. Grimes * Initialize clock frequencies and start both clocks running. 125df8bae1dSRodney W. Grimes */ 1262b14f991SJulian Elischer /* ARGSUSED*/ 1272b14f991SJulian Elischer static void 128d841aaa7SBruce Evans initclocks(dummy) 129d841aaa7SBruce Evans void *dummy; 130df8bae1dSRodney W. Grimes { 131df8bae1dSRodney W. Grimes register int i; 132df8bae1dSRodney W. Grimes 133df8bae1dSRodney W. Grimes /* 134df8bae1dSRodney W. Grimes * Set divisors to 1 (normal case) and let the machine-specific 135df8bae1dSRodney W. Grimes * code do its bit. 136df8bae1dSRodney W. Grimes */ 137df8bae1dSRodney W. Grimes psdiv = pscnt = 1; 138df8bae1dSRodney W. Grimes cpu_initclocks(); 139df8bae1dSRodney W. Grimes 140df8bae1dSRodney W. Grimes /* 141df8bae1dSRodney W. Grimes * Compute profhz/stathz, and fix profhz if needed. 142df8bae1dSRodney W. Grimes */ 143df8bae1dSRodney W. Grimes i = stathz ? stathz : hz; 144df8bae1dSRodney W. Grimes if (profhz == 0) 145df8bae1dSRodney W. Grimes profhz = i; 146df8bae1dSRodney W. Grimes psratio = profhz / i; 147df8bae1dSRodney W. Grimes } 148df8bae1dSRodney W. Grimes 149df8bae1dSRodney W. Grimes /* 150df8bae1dSRodney W. Grimes * The real-time timer, interrupting hz times per second. 151df8bae1dSRodney W. Grimes */ 152df8bae1dSRodney W. Grimes void 153df8bae1dSRodney W. Grimes hardclock(frame) 154df8bae1dSRodney W. Grimes register struct clockframe *frame; 155df8bae1dSRodney W. Grimes { 156df8bae1dSRodney W. Grimes register struct proc *p; 157df8bae1dSRodney W. Grimes 158df8bae1dSRodney W. Grimes p = curproc; 159df8bae1dSRodney W. Grimes if (p) { 160df8bae1dSRodney W. Grimes register struct pstats *pstats; 161df8bae1dSRodney W. Grimes 162df8bae1dSRodney W. Grimes /* 163df8bae1dSRodney W. Grimes * Run current process's virtual and profile time, as needed. 164df8bae1dSRodney W. Grimes */ 165df8bae1dSRodney W. Grimes pstats = p->p_stats; 166df8bae1dSRodney W. Grimes if (CLKF_USERMODE(frame) && 1674cf41af3SPoul-Henning Kamp timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) && 168df8bae1dSRodney W. Grimes itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0) 169df8bae1dSRodney W. Grimes psignal(p, SIGVTALRM); 1704cf41af3SPoul-Henning Kamp if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) && 171df8bae1dSRodney W. Grimes itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0) 172df8bae1dSRodney W. Grimes psignal(p, SIGPROF); 173df8bae1dSRodney W. Grimes } 174df8bae1dSRodney W. Grimes 175eae8fc2cSSteve Passe #if defined(SMP) && defined(BETTER_CLOCK) 176eae8fc2cSSteve Passe forward_hardclock(pscnt); 177eae8fc2cSSteve Passe #endif 178b05dcf3cSPoul-Henning Kamp 179df8bae1dSRodney W. Grimes /* 180df8bae1dSRodney W. Grimes * If no separate statistics clock is available, run it from here. 181df8bae1dSRodney W. Grimes */ 182df8bae1dSRodney W. Grimes if (stathz == 0) 183df8bae1dSRodney W. Grimes statclock(frame); 184df8bae1dSRodney W. Grimes 18591266b96SPoul-Henning Kamp tc_windup(); 186df8bae1dSRodney W. Grimes ticks++; 1873f31c649SGarrett Wollman 188b05dcf3cSPoul-Henning Kamp /* 189b05dcf3cSPoul-Henning Kamp * Process callouts at a very low cpu priority, so we don't keep the 190b05dcf3cSPoul-Henning Kamp * relatively high clock interrupt priority any longer than necessary. 191b05dcf3cSPoul-Henning Kamp */ 192b05dcf3cSPoul-Henning Kamp if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) { 193b05dcf3cSPoul-Henning Kamp if (CLKF_BASEPRI(frame)) { 194b05dcf3cSPoul-Henning Kamp /* 195b05dcf3cSPoul-Henning Kamp * Save the overhead of a software interrupt; 196b05dcf3cSPoul-Henning Kamp * it will happen as soon as we return, so do it now. 197b05dcf3cSPoul-Henning Kamp */ 198b05dcf3cSPoul-Henning Kamp (void)splsoftclock(); 199b05dcf3cSPoul-Henning Kamp softclock(); 200b05dcf3cSPoul-Henning Kamp } else 201eeb355f7SPoul-Henning Kamp setsoftclock(); 202b05dcf3cSPoul-Henning Kamp } else if (softticks + 1 == ticks) 203b05dcf3cSPoul-Henning Kamp ++softticks; 204ab36c067SJustin T. Gibbs } 205ab36c067SJustin T. Gibbs 206df8bae1dSRodney W. Grimes /* 207227ee8a1SPoul-Henning Kamp * Compute number of ticks in the specified amount of time. 208df8bae1dSRodney W. Grimes */ 209df8bae1dSRodney W. Grimes int 210227ee8a1SPoul-Henning Kamp tvtohz(tv) 211df8bae1dSRodney W. Grimes struct timeval *tv; 212df8bae1dSRodney W. Grimes { 2136976af69SBruce Evans register unsigned long ticks; 2146976af69SBruce Evans register long sec, usec; 215df8bae1dSRodney W. Grimes 216df8bae1dSRodney W. Grimes /* 2176976af69SBruce Evans * If the number of usecs in the whole seconds part of the time 2186976af69SBruce Evans * difference fits in a long, then the total number of usecs will 2196976af69SBruce Evans * fit in an unsigned long. Compute the total and convert it to 2206976af69SBruce Evans * ticks, rounding up and adding 1 to allow for the current tick 2216976af69SBruce Evans * to expire. Rounding also depends on unsigned long arithmetic 2226976af69SBruce Evans * to avoid overflow. 223df8bae1dSRodney W. Grimes * 2246976af69SBruce Evans * Otherwise, if the number of ticks in the whole seconds part of 2256976af69SBruce Evans * the time difference fits in a long, then convert the parts to 2266976af69SBruce Evans * ticks separately and add, using similar rounding methods and 2276976af69SBruce Evans * overflow avoidance. This method would work in the previous 2286976af69SBruce Evans * case but it is slightly slower and assumes that hz is integral. 2296976af69SBruce Evans * 2306976af69SBruce Evans * Otherwise, round the time difference down to the maximum 2316976af69SBruce Evans * representable value. 2326976af69SBruce Evans * 2336976af69SBruce Evans * If ints have 32 bits, then the maximum value for any timeout in 2346976af69SBruce Evans * 10ms ticks is 248 days. 235df8bae1dSRodney W. Grimes */ 236227ee8a1SPoul-Henning Kamp sec = tv->tv_sec; 237227ee8a1SPoul-Henning Kamp usec = tv->tv_usec; 2386976af69SBruce Evans if (usec < 0) { 2396976af69SBruce Evans sec--; 2406976af69SBruce Evans usec += 1000000; 2416976af69SBruce Evans } 2426976af69SBruce Evans if (sec < 0) { 2436976af69SBruce Evans #ifdef DIAGNOSTIC 244b05dcf3cSPoul-Henning Kamp if (usec > 0) { 2457ec73f64SPoul-Henning Kamp sec++; 2467ec73f64SPoul-Henning Kamp usec -= 1000000; 2477ec73f64SPoul-Henning Kamp } 248227ee8a1SPoul-Henning Kamp printf("tvotohz: negative time difference %ld sec %ld usec\n", 2496976af69SBruce Evans sec, usec); 2506976af69SBruce Evans #endif 2516976af69SBruce Evans ticks = 1; 2526976af69SBruce Evans } else if (sec <= LONG_MAX / 1000000) 2536976af69SBruce Evans ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1)) 2546976af69SBruce Evans / tick + 1; 2556976af69SBruce Evans else if (sec <= LONG_MAX / hz) 2566976af69SBruce Evans ticks = sec * hz 2576976af69SBruce Evans + ((unsigned long)usec + (tick - 1)) / tick + 1; 2586976af69SBruce Evans else 2596976af69SBruce Evans ticks = LONG_MAX; 2606976af69SBruce Evans if (ticks > INT_MAX) 2616976af69SBruce Evans ticks = INT_MAX; 262d6116663SAlexander Langer return ((int)ticks); 263df8bae1dSRodney W. Grimes } 264df8bae1dSRodney W. Grimes 265df8bae1dSRodney W. Grimes /* 266df8bae1dSRodney W. Grimes * Start profiling on a process. 267df8bae1dSRodney W. Grimes * 268df8bae1dSRodney W. Grimes * Kernel profiling passes proc0 which never exits and hence 269df8bae1dSRodney W. Grimes * keeps the profile clock running constantly. 270df8bae1dSRodney W. Grimes */ 271df8bae1dSRodney W. Grimes void 272df8bae1dSRodney W. Grimes startprofclock(p) 273df8bae1dSRodney W. Grimes register struct proc *p; 274df8bae1dSRodney W. Grimes { 275df8bae1dSRodney W. Grimes int s; 276df8bae1dSRodney W. Grimes 277df8bae1dSRodney W. Grimes if ((p->p_flag & P_PROFIL) == 0) { 278df8bae1dSRodney W. Grimes p->p_flag |= P_PROFIL; 279df8bae1dSRodney W. Grimes if (++profprocs == 1 && stathz != 0) { 280df8bae1dSRodney W. Grimes s = splstatclock(); 281df8bae1dSRodney W. Grimes psdiv = pscnt = psratio; 282df8bae1dSRodney W. Grimes setstatclockrate(profhz); 283df8bae1dSRodney W. Grimes splx(s); 284df8bae1dSRodney W. Grimes } 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes } 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes /* 289df8bae1dSRodney W. Grimes * Stop profiling on a process. 290df8bae1dSRodney W. Grimes */ 291df8bae1dSRodney W. Grimes void 292df8bae1dSRodney W. Grimes stopprofclock(p) 293df8bae1dSRodney W. Grimes register struct proc *p; 294df8bae1dSRodney W. Grimes { 295df8bae1dSRodney W. Grimes int s; 296df8bae1dSRodney W. Grimes 297df8bae1dSRodney W. Grimes if (p->p_flag & P_PROFIL) { 298df8bae1dSRodney W. Grimes p->p_flag &= ~P_PROFIL; 299df8bae1dSRodney W. Grimes if (--profprocs == 0 && stathz != 0) { 300df8bae1dSRodney W. Grimes s = splstatclock(); 301df8bae1dSRodney W. Grimes psdiv = pscnt = 1; 302df8bae1dSRodney W. Grimes setstatclockrate(stathz); 303df8bae1dSRodney W. Grimes splx(s); 304df8bae1dSRodney W. Grimes } 305df8bae1dSRodney W. Grimes } 306df8bae1dSRodney W. Grimes } 307df8bae1dSRodney W. Grimes 308df8bae1dSRodney W. Grimes /* 309df8bae1dSRodney W. Grimes * Statistics clock. Grab profile sample, and if divider reaches 0, 31071a62f8aSBruce Evans * do process and kernel statistics. Most of the statistics are only 31171a62f8aSBruce Evans * used by user-level statistics programs. The main exceptions are 31271a62f8aSBruce Evans * p->p_uticks, p->p_sticks, p->p_iticks, and p->p_estcpu. 313df8bae1dSRodney W. Grimes */ 314df8bae1dSRodney W. Grimes void 315df8bae1dSRodney W. Grimes statclock(frame) 316df8bae1dSRodney W. Grimes register struct clockframe *frame; 317df8bae1dSRodney W. Grimes { 318df8bae1dSRodney W. Grimes #ifdef GPROF 319df8bae1dSRodney W. Grimes register struct gmonparam *g; 320fffd686aSBruce Evans int i; 321df8bae1dSRodney W. Grimes #endif 322f5e9e8ecSBruce Evans register struct proc *p; 3238a129caeSDavid Greenman struct pstats *pstats; 324f5e9e8ecSBruce Evans long rss; 3258a129caeSDavid Greenman struct rusage *ru; 3268a129caeSDavid Greenman struct vmspace *vm; 3278a129caeSDavid Greenman 3288843cc35SSøren Schmidt if (curproc != NULL && CLKF_USERMODE(frame)) { 32971a62f8aSBruce Evans /* 33071a62f8aSBruce Evans * Came from user mode; CPU was in user state. 33171a62f8aSBruce Evans * If this process is being profiled, record the tick. 33271a62f8aSBruce Evans */ 333f5e9e8ecSBruce Evans p = curproc; 334df8bae1dSRodney W. Grimes if (p->p_flag & P_PROFIL) 335df8bae1dSRodney W. Grimes addupc_intr(p, CLKF_PC(frame), 1); 336eae8fc2cSSteve Passe #if defined(SMP) && defined(BETTER_CLOCK) 337eae8fc2cSSteve Passe if (stathz != 0) 338eae8fc2cSSteve Passe forward_statclock(pscnt); 339eae8fc2cSSteve Passe #endif 340df8bae1dSRodney W. Grimes if (--pscnt > 0) 341df8bae1dSRodney W. Grimes return; 342df8bae1dSRodney W. Grimes /* 34371a62f8aSBruce Evans * Charge the time as appropriate. 344df8bae1dSRodney W. Grimes */ 345df8bae1dSRodney W. Grimes p->p_uticks++; 346df8bae1dSRodney W. Grimes if (p->p_nice > NZERO) 347df8bae1dSRodney W. Grimes cp_time[CP_NICE]++; 348df8bae1dSRodney W. Grimes else 349df8bae1dSRodney W. Grimes cp_time[CP_USER]++; 350df8bae1dSRodney W. Grimes } else { 351df8bae1dSRodney W. Grimes #ifdef GPROF 352df8bae1dSRodney W. Grimes /* 353df8bae1dSRodney W. Grimes * Kernel statistics are just like addupc_intr, only easier. 354df8bae1dSRodney W. Grimes */ 355df8bae1dSRodney W. Grimes g = &_gmonparam; 356df8bae1dSRodney W. Grimes if (g->state == GMON_PROF_ON) { 357df8bae1dSRodney W. Grimes i = CLKF_PC(frame) - g->lowpc; 358df8bae1dSRodney W. Grimes if (i < g->textsize) { 359df8bae1dSRodney W. Grimes i /= HISTFRACTION * sizeof(*g->kcount); 360df8bae1dSRodney W. Grimes g->kcount[i]++; 361df8bae1dSRodney W. Grimes } 362df8bae1dSRodney W. Grimes } 363df8bae1dSRodney W. Grimes #endif 364eae8fc2cSSteve Passe #if defined(SMP) && defined(BETTER_CLOCK) 365eae8fc2cSSteve Passe if (stathz != 0) 366eae8fc2cSSteve Passe forward_statclock(pscnt); 367eae8fc2cSSteve Passe #endif 368df8bae1dSRodney W. Grimes if (--pscnt > 0) 369df8bae1dSRodney W. Grimes return; 370df8bae1dSRodney W. Grimes /* 371df8bae1dSRodney W. Grimes * Came from kernel mode, so we were: 372df8bae1dSRodney W. Grimes * - handling an interrupt, 373df8bae1dSRodney W. Grimes * - doing syscall or trap work on behalf of the current 374df8bae1dSRodney W. Grimes * user process, or 375df8bae1dSRodney W. Grimes * - spinning in the idle loop. 376df8bae1dSRodney W. Grimes * Whichever it is, charge the time as appropriate. 377df8bae1dSRodney W. Grimes * Note that we charge interrupts to the current process, 378df8bae1dSRodney W. Grimes * regardless of whether they are ``for'' that process, 379df8bae1dSRodney W. Grimes * so that we know how much of its real time was spent 380df8bae1dSRodney W. Grimes * in ``non-process'' (i.e., interrupt) work. 381df8bae1dSRodney W. Grimes */ 382f5e9e8ecSBruce Evans p = curproc; 383df8bae1dSRodney W. Grimes if (CLKF_INTR(frame)) { 384df8bae1dSRodney W. Grimes if (p != NULL) 385df8bae1dSRodney W. Grimes p->p_iticks++; 386df8bae1dSRodney W. Grimes cp_time[CP_INTR]++; 387b672aa4bSBruce Evans } else if (p != NULL) { 388df8bae1dSRodney W. Grimes p->p_sticks++; 389df8bae1dSRodney W. Grimes cp_time[CP_SYS]++; 390df8bae1dSRodney W. Grimes } else 391df8bae1dSRodney W. Grimes cp_time[CP_IDLE]++; 392df8bae1dSRodney W. Grimes } 393df8bae1dSRodney W. Grimes pscnt = psdiv; 394df8bae1dSRodney W. Grimes 395df8bae1dSRodney W. Grimes if (p != NULL) { 3968a9d4d98SBruce Evans schedclock(p); 397f5e9e8ecSBruce Evans 398f5e9e8ecSBruce Evans /* Update resource usage integrals and maximums. */ 399f5e9e8ecSBruce Evans if ((pstats = p->p_stats) != NULL && 400f5e9e8ecSBruce Evans (ru = &pstats->p_ru) != NULL && 401f5e9e8ecSBruce Evans (vm = p->p_vmspace) != NULL) { 4021c6d46f9SLuoqi Chen ru->ru_ixrss += pgtok(vm->vm_tsize); 4031c6d46f9SLuoqi Chen ru->ru_idrss += pgtok(vm->vm_dsize); 4041c6d46f9SLuoqi Chen ru->ru_isrss += pgtok(vm->vm_ssize); 4051c6d46f9SLuoqi Chen rss = pgtok(vmspace_resident_count(vm)); 406f5e9e8ecSBruce Evans if (ru->ru_maxrss < rss) 407f5e9e8ecSBruce Evans ru->ru_maxrss = rss; 408f5e9e8ecSBruce Evans } 409df8bae1dSRodney W. Grimes } 410df8bae1dSRodney W. Grimes } 411df8bae1dSRodney W. Grimes 412df8bae1dSRodney W. Grimes /* 413df8bae1dSRodney W. Grimes * Return information about system clocks. 414df8bae1dSRodney W. Grimes */ 415787d58f2SPoul-Henning Kamp static int 41682d9ae4eSPoul-Henning Kamp sysctl_kern_clockrate (SYSCTL_HANDLER_ARGS) 417df8bae1dSRodney W. Grimes { 418df8bae1dSRodney W. Grimes struct clockinfo clkinfo; 419df8bae1dSRodney W. Grimes /* 420df8bae1dSRodney W. Grimes * Construct clockinfo structure. 421df8bae1dSRodney W. Grimes */ 422df8bae1dSRodney W. Grimes clkinfo.hz = hz; 423df8bae1dSRodney W. Grimes clkinfo.tick = tick; 4245faa3121SJohn Hay clkinfo.tickadj = tickadj; 425df8bae1dSRodney W. Grimes clkinfo.profhz = profhz; 426df8bae1dSRodney W. Grimes clkinfo.stathz = stathz ? stathz : hz; 427ae0eb976SPoul-Henning Kamp return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req)); 428df8bae1dSRodney W. Grimes } 4293f31c649SGarrett Wollman 430946bb7a2SPoul-Henning Kamp SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD, 43165d0bc13SPoul-Henning Kamp 0, 0, sysctl_kern_clockrate, "S,clockinfo",""); 432