2 * Copyright (c) 1997, 1998 Poul-Henning Kamp <phk@FreeBSD.org>
3 * Copyright (c) 1982, 1986, 1991, 1993
4 * The Regents of the University of California. All rights reserved.
5 * (c) UNIX System Laboratories, Inc.
6 * All or some portions of this file are derived from material licensed
7 * to the University of California by American Telephone and Telegraph
8 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
9 * the permission of UNIX System Laboratories, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
40 * $Id: kern_clock.c,v 1.87 1999/02/19 14:25:34 luoqi Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/dkstat.h>
46 #include <sys/callout.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/resourcevar.h>
51 #include <sys/signalvar.h>
52 #include <sys/timex.h>
56 #include <vm/vm_map.h>
57 #include <sys/sysctl.h>
59 #include <machine/cpu.h>
60 #include <machine/limits.h>
66 #if defined(SMP) && defined(BETTER_CLOCK)
67 #include <machine/smp.h>
70 /* This is where the NTIMECOUNTER option hangs out */
74 * Number of timecounters used to implement stable storage
77 #define NTIMECOUNTER 5
80 static MALLOC_DEFINE(M_TIMECOUNTER, "timecounter",
81 "Timecounter stable storage");
83 static void initclocks __P((void *dummy));
84 SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
86 static void tco_forward __P((int force));
87 static void tco_setscales __P((struct timecounter *tc));
88 static __inline unsigned tco_delta __P((struct timecounter *tc));
90 /* Some of these don't belong here, but it's easiest to concentrate them. */
91 #if defined(SMP) && defined(BETTER_CLOCK)
92 long cp_time[CPUSTATES];
94 static long cp_time[CPUSTATES];
105 * Which update policy to use.
106 * 0 - every tick, bad hardware may fail with "calcru negative..."
107 * 1 - more resistent to the above hardware, but less efficient.
109 static int tco_method;
112 * Implement a dummy timecounter which we can use until we get a real one
113 * in the air. This allows the console and other early stuff to use
118 dummy_get_timecount(struct timecounter *tc)
124 static struct timecounter dummy_timecounter = {
132 struct timecounter *timecounter = &dummy_timecounter;
135 * Clock handling routines.
137 * This code is written to operate with two timers that run independently of
140 * The main timer, running hz times per second, is used to trigger interval
141 * timers, timeouts and rescheduling as needed.
143 * The second timer handles kernel and user profiling,
144 * and does resource use estimation. If the second timer is programmable,
145 * it is randomized to avoid aliasing between the two clocks. For example,
146 * the randomization prevents an adversary from always giving up the cpu
147 * just before its quantum expires. Otherwise, it would never accumulate
148 * cpu ticks. The mean frequency of the second timer is stathz.
150 * If no second timer exists, stathz will be zero; in this case we drive
151 * profiling and statistics off the main clock. This WILL NOT be accurate;
152 * do not do it unless absolutely necessary.
154 * The statistics clock may (or may not) be run at a higher rate while
155 * profiling. This profile clock runs at profhz. We require that profhz
156 * be an integral multiple of stathz.
158 * If the statistics clock is running fast, it must be divided by the ratio
159 * profhz/stathz for statistics. (For profiling, every tick counts.)
161 * Time-of-day is maintained using a "timecounter", which may or may
162 * not be related to the hardware generating the above mentioned
168 static int profprocs;
170 static int psdiv, pscnt; /* prof => stat divider */
171 int psratio; /* ratio: prof / stat */
174 * Initialize clock frequencies and start both clocks running.
184 * Set divisors to 1 (normal case) and let the machine-specific
191 * Compute profhz/stathz, and fix profhz if needed.
193 i = stathz ? stathz : hz;
196 psratio = profhz / i;
200 * The real-time timer, interrupting hz times per second.
204 register struct clockframe *frame;
206 register struct proc *p;
210 register struct pstats *pstats;
213 * Run current process's virtual and profile time, as needed.
216 if (CLKF_USERMODE(frame) &&
217 timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
218 itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
219 psignal(p, SIGVTALRM);
220 if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
221 itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
225 #if defined(SMP) && defined(BETTER_CLOCK)
226 forward_hardclock(pscnt);
230 * If no separate statistics clock is available, run it from here.
239 * Process callouts at a very low cpu priority, so we don't keep the
240 * relatively high clock interrupt priority any longer than necessary.
242 if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
243 if (CLKF_BASEPRI(frame)) {
245 * Save the overhead of a software interrupt;
246 * it will happen as soon as we return, so do it now.
248 (void)splsoftclock();
252 } else if (softticks + 1 == ticks)
257 * Compute number of ticks in the specified amount of time.
263 register unsigned long ticks;
264 register long sec, usec;
267 * If the number of usecs in the whole seconds part of the time
268 * difference fits in a long, then the total number of usecs will
269 * fit in an unsigned long. Compute the total and convert it to
270 * ticks, rounding up and adding 1 to allow for the current tick
271 * to expire. Rounding also depends on unsigned long arithmetic
274 * Otherwise, if the number of ticks in the whole seconds part of
275 * the time difference fits in a long, then convert the parts to
276 * ticks separately and add, using similar rounding methods and
277 * overflow avoidance. This method would work in the previous
278 * case but it is slightly slower and assumes that hz is integral.
280 * Otherwise, round the time difference down to the maximum
281 * representable value.
283 * If ints have 32 bits, then the maximum value for any timeout in
284 * 10ms ticks is 248 days.
298 printf("tvotohz: negative time difference %ld sec %ld usec\n",
302 } else if (sec <= LONG_MAX / 1000000)
303 ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
305 else if (sec <= LONG_MAX / hz)
307 + ((unsigned long)usec + (tick - 1)) / tick + 1;
316 * Start profiling on a process.
318 * Kernel profiling passes proc0 which never exits and hence
319 * keeps the profile clock running constantly.
323 register struct proc *p;
327 if ((p->p_flag & P_PROFIL) == 0) {
328 p->p_flag |= P_PROFIL;
329 if (++profprocs == 1 && stathz != 0) {
331 psdiv = pscnt = psratio;
332 setstatclockrate(profhz);
339 * Stop profiling on a process.
343 register struct proc *p;
347 if (p->p_flag & P_PROFIL) {
348 p->p_flag &= ~P_PROFIL;
349 if (--profprocs == 0 && stathz != 0) {
352 setstatclockrate(stathz);
359 * Statistics clock. Grab profile sample, and if divider reaches 0,
360 * do process and kernel statistics.
364 register struct clockframe *frame;
367 register struct gmonparam *g;
370 register struct proc *p;
371 struct pstats *pstats;
376 if (curproc != NULL && CLKF_USERMODE(frame)) {
378 if (p->p_flag & P_PROFIL)
379 addupc_intr(p, CLKF_PC(frame), 1);
380 #if defined(SMP) && defined(BETTER_CLOCK)
382 forward_statclock(pscnt);
387 * Came from user mode; CPU was in user state.
388 * If this process is being profiled record the tick.
391 if (p->p_nice > NZERO)
398 * Kernel statistics are just like addupc_intr, only easier.
401 if (g->state == GMON_PROF_ON) {
402 i = CLKF_PC(frame) - g->lowpc;
403 if (i < g->textsize) {
404 i /= HISTFRACTION * sizeof(*g->kcount);
409 #if defined(SMP) && defined(BETTER_CLOCK)
411 forward_statclock(pscnt);
416 * Came from kernel mode, so we were:
417 * - handling an interrupt,
418 * - doing syscall or trap work on behalf of the current
420 * - spinning in the idle loop.
421 * Whichever it is, charge the time as appropriate.
422 * Note that we charge interrupts to the current process,
423 * regardless of whether they are ``for'' that process,
424 * so that we know how much of its real time was spent
425 * in ``non-process'' (i.e., interrupt) work.
428 if (CLKF_INTR(frame)) {
432 } else if (p != NULL) {
441 * We maintain statistics shown by user-level statistics
442 * programs: the amount of time in each cpu state.
446 * We adjust the priority of the current process. The priority of
447 * a process gets worse as it accumulates CPU time. The cpu usage
448 * estimator (p_estcpu) is increased here. The formula for computing
449 * priorities (in kern_synch.c) will compute a different value each
450 * time p_estcpu increases by 4. The cpu usage estimator ramps up
451 * quite quickly when the process is running (linearly), and decays
452 * away exponentially, at a rate which is proportionally slower when
453 * the system is busy. The basic principal is that the system will
454 * 90% forget that the process used a lot of CPU time in 5 * loadav
455 * seconds. This causes the system to favor processes which haven't
456 * run much recently, and to round-robin among other processes.
460 if (++p->p_estcpu == 0)
462 if ((p->p_estcpu & 3) == 0) {
464 if (p->p_priority >= PUSER)
465 p->p_priority = p->p_usrpri;
468 /* Update resource usage integrals and maximums. */
469 if ((pstats = p->p_stats) != NULL &&
470 (ru = &pstats->p_ru) != NULL &&
471 (vm = p->p_vmspace) != NULL) {
472 ru->ru_ixrss += pgtok(vm->vm_tsize);
473 ru->ru_idrss += pgtok(vm->vm_dsize);
474 ru->ru_isrss += pgtok(vm->vm_ssize);
475 rss = pgtok(vmspace_resident_count(vm));
476 if (ru->ru_maxrss < rss)
483 * Return information about system clocks.
486 sysctl_kern_clockrate SYSCTL_HANDLER_ARGS
488 struct clockinfo clkinfo;
490 * Construct clockinfo structure.
494 clkinfo.tickadj = tickadj;
495 clkinfo.profhz = profhz;
496 clkinfo.stathz = stathz ? stathz : hz;
497 return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
500 SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
501 0, 0, sysctl_kern_clockrate, "S,clockinfo","");
503 static __inline unsigned
504 tco_delta(struct timecounter *tc)
507 return ((tc->tc_get_timecount(tc) - tc->tc_offset_count) &
508 tc->tc_counter_mask);
512 * We have four functions for looking at the clock, two for microseconds
513 * and two for nanoseconds. For each there is fast but less precise
514 * version "get{nano|micro}time" which will return a time which is up
515 * to 1/HZ previous to the call, whereas the raw version "{nano|micro}time"
516 * will return a timestamp which is as precise as possible.
520 getmicrotime(struct timeval *tvp)
522 struct timecounter *tc;
526 *tvp = tc->tc_microtime;
533 getnanotime(struct timespec *tsp)
535 struct timecounter *tc;
539 *tsp = tc->tc_nanotime;
546 microtime(struct timeval *tv)
548 struct timecounter *tc;
550 tc = (struct timecounter *)timecounter;
551 tv->tv_sec = tc->tc_offset_sec;
552 tv->tv_usec = tc->tc_offset_micro;
553 tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32;
554 tv->tv_usec += boottime.tv_usec;
555 tv->tv_sec += boottime.tv_sec;
556 while (tv->tv_usec >= 1000000) {
557 tv->tv_usec -= 1000000;
563 nanotime(struct timespec *ts)
567 struct timecounter *tc;
569 tc = (struct timecounter *)timecounter;
570 ts->tv_sec = tc->tc_offset_sec;
571 count = tco_delta(tc);
572 delta = tc->tc_offset_nano;
573 delta += ((u_int64_t)count * tc->tc_scale_nano_f);
575 delta += ((u_int64_t)count * tc->tc_scale_nano_i);
576 delta += boottime.tv_usec * 1000;
577 ts->tv_sec += boottime.tv_sec;
578 while (delta >= 1000000000) {
586 timecounter_timespec(unsigned count, struct timespec *ts)
589 struct timecounter *tc;
591 tc = (struct timecounter *)timecounter;
592 ts->tv_sec = tc->tc_offset_sec;
593 count -= tc->tc_offset_count;
594 count &= tc->tc_counter_mask;
595 delta = tc->tc_offset_nano;
596 delta += ((u_int64_t)count * tc->tc_scale_nano_f);
598 delta += ((u_int64_t)count * tc->tc_scale_nano_i);
599 delta += boottime.tv_usec * 1000;
600 ts->tv_sec += boottime.tv_sec;
601 while (delta >= 1000000000) {
609 getmicrouptime(struct timeval *tvp)
611 struct timecounter *tc;
615 tvp->tv_sec = tc->tc_offset_sec;
616 tvp->tv_usec = tc->tc_offset_micro;
623 getnanouptime(struct timespec *tsp)
625 struct timecounter *tc;
629 tsp->tv_sec = tc->tc_offset_sec;
630 tsp->tv_nsec = tc->tc_offset_nano >> 32;
637 microuptime(struct timeval *tv)
639 struct timecounter *tc;
641 tc = (struct timecounter *)timecounter;
642 tv->tv_sec = tc->tc_offset_sec;
643 tv->tv_usec = tc->tc_offset_micro;
644 tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32;
645 if (tv->tv_usec >= 1000000) {
646 tv->tv_usec -= 1000000;
652 nanouptime(struct timespec *ts)
656 struct timecounter *tc;
658 tc = (struct timecounter *)timecounter;
659 ts->tv_sec = tc->tc_offset_sec;
660 count = tco_delta(tc);
661 delta = tc->tc_offset_nano;
662 delta += ((u_int64_t)count * tc->tc_scale_nano_f);
664 delta += ((u_int64_t)count * tc->tc_scale_nano_i);
665 if (delta >= 1000000000) {
673 tco_setscales(struct timecounter *tc)
677 scale = 1000000000LL << 32;
678 if (tc->tc_adjustment > 0)
679 scale += (tc->tc_adjustment * 1000LL) << 10;
681 scale -= (-tc->tc_adjustment * 1000LL) << 10;
682 scale /= tc->tc_frequency;
683 tc->tc_scale_micro = scale / 1000;
684 tc->tc_scale_nano_f = scale & 0xffffffff;
685 tc->tc_scale_nano_i = scale >> 32;
689 init_timecounter(struct timecounter *tc)
692 struct timecounter *t1, *t2, *t3;
695 tc->tc_adjustment = 0;
697 tc->tc_offset_count = tc->tc_get_timecount(tc);
699 MALLOC(t1, struct timecounter *, sizeof *t1, M_TIMECOUNTER, M_WAITOK);
702 for (i = 1; i < NTIMECOUNTER; i++) {
703 MALLOC(t3, struct timecounter *, sizeof *t3,
704 M_TIMECOUNTER, M_WAITOK);
712 printf("Timecounter \"%s\" frequency %lu Hz\n",
713 tc->tc_name, (u_long)tc->tc_frequency);
715 /* XXX: For now always start using the counter. */
716 tc->tc_offset_count = tc->tc_get_timecount(tc);
718 tc->tc_offset_nano = (u_int64_t)ts1.tv_nsec << 32;
719 tc->tc_offset_micro = ts1.tv_nsec / 1000;
720 tc->tc_offset_sec = ts1.tv_sec;
725 set_timecounter(struct timespec *ts)
730 boottime.tv_sec = ts->tv_sec - ts2.tv_sec;
731 boottime.tv_usec = (ts->tv_nsec - ts2.tv_nsec) / 1000;
732 if (boottime.tv_usec < 0) {
733 boottime.tv_usec += 1000000;
736 /* fiddle all the little crinkly bits around the fiords... */
741 #if 0 /* Currently unused */
743 switch_timecounter(struct timecounter *newtc)
746 struct timecounter *tc;
751 if (newtc == tc || newtc == tc->tc_other) {
756 newtc->tc_offset_sec = ts.tv_sec;
757 newtc->tc_offset_nano = (u_int64_t)ts.tv_nsec << 32;
758 newtc->tc_offset_micro = ts.tv_nsec / 1000;
759 newtc->tc_offset_count = newtc->tc_get_timecount(newtc);
765 static struct timecounter *
766 sync_other_counter(void)
768 struct timecounter *tc, *tcn, *tco;
776 delta = tco_delta(tc);
777 tc->tc_offset_count += delta;
778 tc->tc_offset_count &= tc->tc_counter_mask;
779 tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_f;
780 tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_i << 32;
785 tco_forward(int force)
787 struct timecounter *tc, *tco;
790 tc = sync_other_counter();
792 * We may be inducing a tiny error here, the tc_poll_pps() may
793 * process a latched count which happens after the tco_delta()
794 * in sync_other_counter(), which would extend the previous
795 * counters parameters into the domain of this new one.
796 * Since the timewindow is very small for this, the error is
797 * going to be only a few weenieseconds (as Dave Mills would
798 * say), so lets just not talk more about it, OK ?
800 if (tco->tc_poll_pps)
801 tco->tc_poll_pps(tco);
802 if (timedelta != 0) {
803 tc->tc_offset_nano += (u_int64_t)(tickdelta * 1000) << 32;
804 timedelta -= tickdelta;
808 while (tc->tc_offset_nano >= 1000000000ULL << 32) {
809 tc->tc_offset_nano -= 1000000000ULL << 32;
811 tc->tc_frequency = tc->tc_tweak->tc_frequency;
812 tc->tc_adjustment = tc->tc_tweak->tc_adjustment;
813 ntp_update_second(tc); /* XXX only needed if xntpd runs */
818 if (tco_method && !force)
821 tc->tc_offset_micro = (tc->tc_offset_nano / 1000) >> 32;
823 /* Figure out the wall-clock time */
824 tc->tc_nanotime.tv_sec = tc->tc_offset_sec + boottime.tv_sec;
825 tc->tc_nanotime.tv_nsec =
826 (tc->tc_offset_nano >> 32) + boottime.tv_usec * 1000;
827 tc->tc_microtime.tv_usec = tc->tc_offset_micro + boottime.tv_usec;
828 if (tc->tc_nanotime.tv_nsec >= 1000000000) {
829 tc->tc_nanotime.tv_nsec -= 1000000000;
830 tc->tc_microtime.tv_usec -= 1000000;
831 tc->tc_nanotime.tv_sec++;
833 time_second = tc->tc_microtime.tv_sec = tc->tc_nanotime.tv_sec;
839 sysctl_kern_timecounter_frequency SYSCTL_HANDLER_ARGS
842 return (sysctl_handle_opaque(oidp,
843 &timecounter->tc_tweak->tc_frequency,
844 sizeof(timecounter->tc_tweak->tc_frequency), req));
848 sysctl_kern_timecounter_adjustment SYSCTL_HANDLER_ARGS
851 return (sysctl_handle_opaque(oidp,
852 &timecounter->tc_tweak->tc_adjustment,
853 sizeof(timecounter->tc_tweak->tc_adjustment), req));
856 SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
858 SYSCTL_INT(_kern_timecounter, KERN_ARGMAX, method, CTLFLAG_RW, &tco_method, 0,
859 "This variable determines the method used for updating timecounters. "
860 "If the default algorithm (0) fails with \"calcru negative...\" messages "
861 "try the alternate algorithm (1) which handles bad hardware better."
865 SYSCTL_PROC(_kern_timecounter, OID_AUTO, frequency, CTLTYPE_INT | CTLFLAG_RW,
866 0, sizeof(u_int), sysctl_kern_timecounter_frequency, "I", "");
868 SYSCTL_PROC(_kern_timecounter, OID_AUTO, adjustment, CTLTYPE_INT | CTLFLAG_RW,
869 0, sizeof(int), sysctl_kern_timecounter_adjustment, "I", "");