kern_tc.c revision 41306
1/*-
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
26 *
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
37 * SUCH DAMAGE.
38 *
39 *	@(#)kern_clock.c	8.5 (Berkeley) 1/21/94
40 * $Id: kern_clock.c,v 1.84 1998/11/23 09:34:19 sos Exp $
41 */
42
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>
48#include <sys/proc.h>
49#include <sys/malloc.h>
50#include <sys/resourcevar.h>
51#include <sys/signalvar.h>
52#include <sys/timex.h>
53#include <vm/vm.h>
54#include <sys/lock.h>
55#include <vm/pmap.h>
56#include <vm/vm_map.h>
57#include <sys/sysctl.h>
58
59#include <machine/cpu.h>
60#include <machine/limits.h>
61
62#ifdef GPROF
63#include <sys/gmon.h>
64#endif
65
66#if defined(SMP) && defined(BETTER_CLOCK)
67#include <machine/smp.h>
68#endif
69
70/* This is where the NTIMECOUNTER option hangs out */
71#include "opt_ntp.h"
72
73/*
74 * Number of timecounters used to implement stable storage
75 */
76#ifndef NTIMECOUNTER
77#define NTIMECOUNTER	5
78#endif
79
80static MALLOC_DEFINE(M_TIMECOUNTER, "timecounter",
81	"Timecounter stable storage");
82
83static void initclocks __P((void *dummy));
84SYSINIT(clocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, initclocks, NULL)
85
86static void tco_forward __P((int force));
87static void tco_setscales __P((struct timecounter *tc));
88static __inline unsigned tco_delta __P((struct timecounter *tc));
89
90/* Some of these don't belong here, but it's easiest to concentrate them. */
91#if defined(SMP) && defined(BETTER_CLOCK)
92long cp_time[CPUSTATES];
93#else
94static long cp_time[CPUSTATES];
95#endif
96
97long tk_cancc;
98long tk_nin;
99long tk_nout;
100long tk_rawcc;
101
102time_t time_second;
103
104/*
105 * Implement a dummy timecounter which we can use until we get a real one
106 * in the air.  This allows the console and other early stuff to use
107 * timeservices.
108 */
109
110static unsigned
111dummy_get_timecount(struct timecounter *tc)
112{
113	static unsigned now;
114	return (++now);
115}
116
117static struct timecounter dummy_timecounter = {
118	dummy_get_timecount,
119	0,
120	~0u,
121	1000000,
122	"dummy"
123};
124
125struct timecounter *timecounter = &dummy_timecounter;
126
127/*
128 * Clock handling routines.
129 *
130 * This code is written to operate with two timers that run independently of
131 * each other.
132 *
133 * The main timer, running hz times per second, is used to trigger interval
134 * timers, timeouts and rescheduling as needed.
135 *
136 * The second timer handles kernel and user profiling,
137 * and does resource use estimation.  If the second timer is programmable,
138 * it is randomized to avoid aliasing between the two clocks.  For example,
139 * the randomization prevents an adversary from always giving up the cpu
140 * just before its quantum expires.  Otherwise, it would never accumulate
141 * cpu ticks.  The mean frequency of the second timer is stathz.
142 *
143 * If no second timer exists, stathz will be zero; in this case we drive
144 * profiling and statistics off the main clock.  This WILL NOT be accurate;
145 * do not do it unless absolutely necessary.
146 *
147 * The statistics clock may (or may not) be run at a higher rate while
148 * profiling.  This profile clock runs at profhz.  We require that profhz
149 * be an integral multiple of stathz.
150 *
151 * If the statistics clock is running fast, it must be divided by the ratio
152 * profhz/stathz for statistics.  (For profiling, every tick counts.)
153 *
154 * Time-of-day is maintained using a "timecounter", which may or may
155 * not be related to the hardware generating the above mentioned
156 * interrupts.
157 */
158
159int	stathz;
160int	profhz;
161static int profprocs;
162int	ticks;
163static int psdiv, pscnt;		/* prof => stat divider */
164int	psratio;			/* ratio: prof / stat */
165
166/*
167 * Initialize clock frequencies and start both clocks running.
168 */
169/* ARGSUSED*/
170static void
171initclocks(dummy)
172	void *dummy;
173{
174	register int i;
175
176	/*
177	 * Set divisors to 1 (normal case) and let the machine-specific
178	 * code do its bit.
179	 */
180	psdiv = pscnt = 1;
181	cpu_initclocks();
182
183	/*
184	 * Compute profhz/stathz, and fix profhz if needed.
185	 */
186	i = stathz ? stathz : hz;
187	if (profhz == 0)
188		profhz = i;
189	psratio = profhz / i;
190}
191
192/*
193 * The real-time timer, interrupting hz times per second.
194 */
195void
196hardclock(frame)
197	register struct clockframe *frame;
198{
199	register struct proc *p;
200
201	p = curproc;
202	if (p) {
203		register struct pstats *pstats;
204
205		/*
206		 * Run current process's virtual and profile time, as needed.
207		 */
208		pstats = p->p_stats;
209		if (CLKF_USERMODE(frame) &&
210		    timevalisset(&pstats->p_timer[ITIMER_VIRTUAL].it_value) &&
211		    itimerdecr(&pstats->p_timer[ITIMER_VIRTUAL], tick) == 0)
212			psignal(p, SIGVTALRM);
213		if (timevalisset(&pstats->p_timer[ITIMER_PROF].it_value) &&
214		    itimerdecr(&pstats->p_timer[ITIMER_PROF], tick) == 0)
215			psignal(p, SIGPROF);
216	}
217
218#if defined(SMP) && defined(BETTER_CLOCK)
219	forward_hardclock(pscnt);
220#endif
221
222	/*
223	 * If no separate statistics clock is available, run it from here.
224	 */
225	if (stathz == 0)
226		statclock(frame);
227
228	tco_forward(0);
229	ticks++;
230
231	/*
232	 * Process callouts at a very low cpu priority, so we don't keep the
233	 * relatively high clock interrupt priority any longer than necessary.
234	 */
235	if (TAILQ_FIRST(&callwheel[ticks & callwheelmask]) != NULL) {
236		if (CLKF_BASEPRI(frame)) {
237			/*
238			 * Save the overhead of a software interrupt;
239			 * it will happen as soon as we return, so do it now.
240			 */
241			(void)splsoftclock();
242			softclock();
243		} else
244			setsoftclock();
245	} else if (softticks + 1 == ticks)
246		++softticks;
247}
248
249/*
250 * Compute number of ticks in the specified amount of time.
251 */
252int
253tvtohz(tv)
254	struct timeval *tv;
255{
256	register unsigned long ticks;
257	register long sec, usec;
258
259	/*
260	 * If the number of usecs in the whole seconds part of the time
261	 * difference fits in a long, then the total number of usecs will
262	 * fit in an unsigned long.  Compute the total and convert it to
263	 * ticks, rounding up and adding 1 to allow for the current tick
264	 * to expire.  Rounding also depends on unsigned long arithmetic
265	 * to avoid overflow.
266	 *
267	 * Otherwise, if the number of ticks in the whole seconds part of
268	 * the time difference fits in a long, then convert the parts to
269	 * ticks separately and add, using similar rounding methods and
270	 * overflow avoidance.  This method would work in the previous
271	 * case but it is slightly slower and assumes that hz is integral.
272	 *
273	 * Otherwise, round the time difference down to the maximum
274	 * representable value.
275	 *
276	 * If ints have 32 bits, then the maximum value for any timeout in
277	 * 10ms ticks is 248 days.
278	 */
279	sec = tv->tv_sec;
280	usec = tv->tv_usec;
281	if (usec < 0) {
282		sec--;
283		usec += 1000000;
284	}
285	if (sec < 0) {
286#ifdef DIAGNOSTIC
287		if (usec > 0) {
288			sec++;
289			usec -= 1000000;
290		}
291		printf("tvotohz: negative time difference %ld sec %ld usec\n",
292		       sec, usec);
293#endif
294		ticks = 1;
295	} else if (sec <= LONG_MAX / 1000000)
296		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
297			/ tick + 1;
298	else if (sec <= LONG_MAX / hz)
299		ticks = sec * hz
300			+ ((unsigned long)usec + (tick - 1)) / tick + 1;
301	else
302		ticks = LONG_MAX;
303	if (ticks > INT_MAX)
304		ticks = INT_MAX;
305	return ((int)ticks);
306}
307
308/*
309 * Start profiling on a process.
310 *
311 * Kernel profiling passes proc0 which never exits and hence
312 * keeps the profile clock running constantly.
313 */
314void
315startprofclock(p)
316	register struct proc *p;
317{
318	int s;
319
320	if ((p->p_flag & P_PROFIL) == 0) {
321		p->p_flag |= P_PROFIL;
322		if (++profprocs == 1 && stathz != 0) {
323			s = splstatclock();
324			psdiv = pscnt = psratio;
325			setstatclockrate(profhz);
326			splx(s);
327		}
328	}
329}
330
331/*
332 * Stop profiling on a process.
333 */
334void
335stopprofclock(p)
336	register struct proc *p;
337{
338	int s;
339
340	if (p->p_flag & P_PROFIL) {
341		p->p_flag &= ~P_PROFIL;
342		if (--profprocs == 0 && stathz != 0) {
343			s = splstatclock();
344			psdiv = pscnt = 1;
345			setstatclockrate(stathz);
346			splx(s);
347		}
348	}
349}
350
351/*
352 * Statistics clock.  Grab profile sample, and if divider reaches 0,
353 * do process and kernel statistics.
354 */
355void
356statclock(frame)
357	register struct clockframe *frame;
358{
359#ifdef GPROF
360	register struct gmonparam *g;
361	int i;
362#endif
363	register struct proc *p;
364	struct pstats *pstats;
365	long rss;
366	struct rusage *ru;
367	struct vmspace *vm;
368
369	if (curproc != NULL && CLKF_USERMODE(frame)) {
370		p = curproc;
371		if (p->p_flag & P_PROFIL)
372			addupc_intr(p, CLKF_PC(frame), 1);
373#if defined(SMP) && defined(BETTER_CLOCK)
374		if (stathz != 0)
375			forward_statclock(pscnt);
376#endif
377		if (--pscnt > 0)
378			return;
379		/*
380		 * Came from user mode; CPU was in user state.
381		 * If this process is being profiled record the tick.
382		 */
383		p->p_uticks++;
384		if (p->p_nice > NZERO)
385			cp_time[CP_NICE]++;
386		else
387			cp_time[CP_USER]++;
388	} else {
389#ifdef GPROF
390		/*
391		 * Kernel statistics are just like addupc_intr, only easier.
392		 */
393		g = &_gmonparam;
394		if (g->state == GMON_PROF_ON) {
395			i = CLKF_PC(frame) - g->lowpc;
396			if (i < g->textsize) {
397				i /= HISTFRACTION * sizeof(*g->kcount);
398				g->kcount[i]++;
399			}
400		}
401#endif
402#if defined(SMP) && defined(BETTER_CLOCK)
403		if (stathz != 0)
404			forward_statclock(pscnt);
405#endif
406		if (--pscnt > 0)
407			return;
408		/*
409		 * Came from kernel mode, so we were:
410		 * - handling an interrupt,
411		 * - doing syscall or trap work on behalf of the current
412		 *   user process, or
413		 * - spinning in the idle loop.
414		 * Whichever it is, charge the time as appropriate.
415		 * Note that we charge interrupts to the current process,
416		 * regardless of whether they are ``for'' that process,
417		 * so that we know how much of its real time was spent
418		 * in ``non-process'' (i.e., interrupt) work.
419		 */
420		p = curproc;
421		if (CLKF_INTR(frame)) {
422			if (p != NULL)
423				p->p_iticks++;
424			cp_time[CP_INTR]++;
425		} else if (p != NULL) {
426			p->p_sticks++;
427			cp_time[CP_SYS]++;
428		} else
429			cp_time[CP_IDLE]++;
430	}
431	pscnt = psdiv;
432
433	/*
434	 * We maintain statistics shown by user-level statistics
435	 * programs:  the amount of time in each cpu state.
436	 */
437
438	/*
439	 * We adjust the priority of the current process.  The priority of
440	 * a process gets worse as it accumulates CPU time.  The cpu usage
441	 * estimator (p_estcpu) is increased here.  The formula for computing
442	 * priorities (in kern_synch.c) will compute a different value each
443	 * time p_estcpu increases by 4.  The cpu usage estimator ramps up
444	 * quite quickly when the process is running (linearly), and decays
445	 * away exponentially, at a rate which is proportionally slower when
446	 * the system is busy.  The basic principal is that the system will
447	 * 90% forget that the process used a lot of CPU time in 5 * loadav
448	 * seconds.  This causes the system to favor processes which haven't
449	 * run much recently, and to round-robin among other processes.
450	 */
451	if (p != NULL) {
452		p->p_cpticks++;
453		if (++p->p_estcpu == 0)
454			p->p_estcpu--;
455		if ((p->p_estcpu & 3) == 0) {
456			resetpriority(p);
457			if (p->p_priority >= PUSER)
458				p->p_priority = p->p_usrpri;
459		}
460
461		/* Update resource usage integrals and maximums. */
462		if ((pstats = p->p_stats) != NULL &&
463		    (ru = &pstats->p_ru) != NULL &&
464		    (vm = p->p_vmspace) != NULL) {
465			ru->ru_ixrss += vm->vm_tsize * PAGE_SIZE / 1024;
466			ru->ru_idrss += vm->vm_dsize * PAGE_SIZE / 1024;
467			ru->ru_isrss += vm->vm_ssize * PAGE_SIZE / 1024;
468			rss = vm->vm_pmap.pm_stats.resident_count *
469			      PAGE_SIZE / 1024;
470			if (ru->ru_maxrss < rss)
471				ru->ru_maxrss = rss;
472        	}
473	}
474}
475
476/*
477 * Return information about system clocks.
478 */
479static int
480sysctl_kern_clockrate SYSCTL_HANDLER_ARGS
481{
482	struct clockinfo clkinfo;
483	/*
484	 * Construct clockinfo structure.
485	 */
486	clkinfo.hz = hz;
487	clkinfo.tick = tick;
488	clkinfo.tickadj = tickadj;
489	clkinfo.profhz = profhz;
490	clkinfo.stathz = stathz ? stathz : hz;
491	return (sysctl_handle_opaque(oidp, &clkinfo, sizeof clkinfo, req));
492}
493
494SYSCTL_PROC(_kern, KERN_CLOCKRATE, clockrate, CTLTYPE_STRUCT|CTLFLAG_RD,
495	0, 0, sysctl_kern_clockrate, "S,clockinfo","");
496
497static __inline unsigned
498tco_delta(struct timecounter *tc)
499{
500
501	return ((tc->tc_get_timecount(tc) - tc->tc_offset_count) &
502	    tc->tc_counter_mask);
503}
504
505/*
506 * We have four functions for looking at the clock, two for microseconds
507 * and two for nanoseconds.  For each there is fast but less precise
508 * version "get{nano|micro}time" which will return a time which is up
509 * to 1/HZ previous to the call, whereas the raw version "{nano|micro}time"
510 * will return a timestamp which is as precise as possible.
511 */
512
513void
514getmicrotime(struct timeval *tvp)
515{
516	struct timecounter *tc;
517
518	tc = timecounter;
519	*tvp = tc->tc_microtime;
520}
521
522void
523getnanotime(struct timespec *tsp)
524{
525	struct timecounter *tc;
526
527	tc = timecounter;
528	*tsp = tc->tc_nanotime;
529}
530
531void
532microtime(struct timeval *tv)
533{
534	struct timecounter *tc;
535
536	tc = (struct timecounter *)timecounter;
537	tv->tv_sec = tc->tc_offset_sec;
538	tv->tv_usec = tc->tc_offset_micro;
539	tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32;
540	tv->tv_usec += boottime.tv_usec;
541	tv->tv_sec += boottime.tv_sec;
542	while (tv->tv_usec >= 1000000) {
543		tv->tv_usec -= 1000000;
544		tv->tv_sec++;
545	}
546}
547
548void
549nanotime(struct timespec *ts)
550{
551	unsigned count;
552	u_int64_t delta;
553	struct timecounter *tc;
554
555	tc = (struct timecounter *)timecounter;
556	ts->tv_sec = tc->tc_offset_sec;
557	count = tco_delta(tc);
558	delta = tc->tc_offset_nano;
559	delta += ((u_int64_t)count * tc->tc_scale_nano_f);
560	delta >>= 32;
561	delta += ((u_int64_t)count * tc->tc_scale_nano_i);
562	delta += boottime.tv_usec * 1000;
563	ts->tv_sec += boottime.tv_sec;
564	while (delta >= 1000000000) {
565		delta -= 1000000000;
566		ts->tv_sec++;
567	}
568	ts->tv_nsec = delta;
569}
570
571void
572timecounter_timespec(unsigned count, struct timespec *ts)
573{
574	u_int64_t delta;
575	struct timecounter *tc;
576
577	tc = (struct timecounter *)timecounter;
578	ts->tv_sec = tc->tc_offset_sec;
579	count -= tc->tc_offset_count;
580	count &= tc->tc_counter_mask;
581	delta = tc->tc_offset_nano;
582	delta += ((u_int64_t)count * tc->tc_scale_nano_f);
583	delta >>= 32;
584	delta += ((u_int64_t)count * tc->tc_scale_nano_i);
585	delta += boottime.tv_usec * 1000;
586	ts->tv_sec += boottime.tv_sec;
587	while (delta >= 1000000000) {
588		delta -= 1000000000;
589		ts->tv_sec++;
590	}
591	ts->tv_nsec = delta;
592}
593
594void
595getmicrouptime(struct timeval *tvp)
596{
597	struct timecounter *tc;
598
599	tc = timecounter;
600	tvp->tv_sec = tc->tc_offset_sec;
601	tvp->tv_usec = tc->tc_offset_micro;
602}
603
604void
605getnanouptime(struct timespec *tsp)
606{
607	struct timecounter *tc;
608
609	tc = timecounter;
610	tsp->tv_sec = tc->tc_offset_sec;
611	tsp->tv_nsec = tc->tc_offset_nano >> 32;
612}
613
614void
615microuptime(struct timeval *tv)
616{
617	struct timecounter *tc;
618
619	tc = (struct timecounter *)timecounter;
620	tv->tv_sec = tc->tc_offset_sec;
621	tv->tv_usec = tc->tc_offset_micro;
622	tv->tv_usec += ((u_int64_t)tco_delta(tc) * tc->tc_scale_micro) >> 32;
623	if (tv->tv_usec >= 1000000) {
624		tv->tv_usec -= 1000000;
625		tv->tv_sec++;
626	}
627}
628
629void
630nanouptime(struct timespec *ts)
631{
632	unsigned count;
633	u_int64_t delta;
634	struct timecounter *tc;
635
636	tc = (struct timecounter *)timecounter;
637	ts->tv_sec = tc->tc_offset_sec;
638	count = tco_delta(tc);
639	delta = tc->tc_offset_nano;
640	delta += ((u_int64_t)count * tc->tc_scale_nano_f);
641	delta >>= 32;
642	delta += ((u_int64_t)count * tc->tc_scale_nano_i);
643	if (delta >= 1000000000) {
644		delta -= 1000000000;
645		ts->tv_sec++;
646	}
647	ts->tv_nsec = delta;
648}
649
650static void
651tco_setscales(struct timecounter *tc)
652{
653	u_int64_t scale;
654
655	scale = 1000000000LL << 32;
656	if (tc->tc_adjustment > 0)
657		scale += (tc->tc_adjustment * 1000LL) << 10;
658	else
659		scale -= (-tc->tc_adjustment * 1000LL) << 10;
660	scale /= tc->tc_frequency;
661	tc->tc_scale_micro = scale / 1000;
662	tc->tc_scale_nano_f = scale & 0xffffffff;
663	tc->tc_scale_nano_i = scale >> 32;
664}
665
666void
667init_timecounter(struct timecounter *tc)
668{
669	struct timespec ts1;
670	struct timecounter *t1, *t2, *t3;
671	int i;
672
673	tc->tc_adjustment = 0;
674	tco_setscales(tc);
675	tc->tc_offset_count = tc->tc_get_timecount(tc);
676	tc->tc_tweak = tc;
677	MALLOC(t1, struct timecounter *, sizeof *t1, M_TIMECOUNTER, M_WAITOK);
678	*t1 = *tc;
679	t2 = t1;
680	for (i = 1; i < NTIMECOUNTER; i++) {
681		MALLOC(t3, struct timecounter *, sizeof *t3,
682		    M_TIMECOUNTER, M_WAITOK);
683		*t3 = *tc;
684		t3->tc_other = t2;
685		t2 = t3;
686	}
687	t1->tc_other = t3;
688	tc = t1;
689
690	printf("Timecounter \"%s\"  frequency %lu Hz\n",
691	    tc->tc_name, (u_long)tc->tc_frequency);
692
693	/* XXX: For now always start using the counter. */
694	tc->tc_offset_count = tc->tc_get_timecount(tc);
695	nanouptime(&ts1);
696	tc->tc_offset_nano = (u_int64_t)ts1.tv_nsec << 32;
697	tc->tc_offset_micro = ts1.tv_nsec / 1000;
698	tc->tc_offset_sec = ts1.tv_sec;
699	timecounter = tc;
700}
701
702void
703set_timecounter(struct timespec *ts)
704{
705	struct timespec ts2;
706
707	nanouptime(&ts2);
708	boottime.tv_sec = ts->tv_sec - ts2.tv_sec;
709	boottime.tv_usec = (ts->tv_nsec - ts2.tv_nsec) / 1000;
710	if (boottime.tv_usec < 0) {
711		boottime.tv_usec += 1000000;
712		boottime.tv_sec--;
713	}
714	/* fiddle all the little crinkly bits around the fiords... */
715	tco_forward(1);
716}
717
718
719#if 0 /* Currently unused */
720void
721switch_timecounter(struct timecounter *newtc)
722{
723	int s;
724	struct timecounter *tc;
725	struct timespec ts;
726
727	s = splclock();
728	tc = timecounter;
729	if (newtc == tc || newtc == tc->tc_other) {
730		splx(s);
731		return;
732	}
733	nanouptime(&ts);
734	newtc->tc_offset_sec = ts.tv_sec;
735	newtc->tc_offset_nano = (u_int64_t)ts.tv_nsec << 32;
736	newtc->tc_offset_micro = ts.tv_nsec / 1000;
737	newtc->tc_offset_count = newtc->tc_get_timecount(newtc);
738	timecounter = newtc;
739	splx(s);
740}
741#endif
742
743static struct timecounter *
744sync_other_counter(void)
745{
746	struct timecounter *tc, *tcn, *tco;
747	unsigned delta;
748
749	tco = timecounter;
750	tc = tco->tc_other;
751	tcn = tc->tc_other;
752	*tc = *tco;
753	tc->tc_other = tcn;
754	delta = tco_delta(tc);
755	tc->tc_offset_count += delta;
756	tc->tc_offset_count &= tc->tc_counter_mask;
757	tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_f;
758	tc->tc_offset_nano += (u_int64_t)delta * tc->tc_scale_nano_i << 32;
759	return (tc);
760}
761
762static void
763tco_forward(int force)
764{
765	struct timecounter *tc, *tco;
766
767	tco = timecounter;
768	tc = sync_other_counter();
769	/*
770	 * We may be inducing a tiny error here, the tc_poll_pps() may
771	 * process a latched count which happens after the tco_delta()
772	 * in sync_other_counter(), which would extend the previous
773	 * counters parameters into the domain of this new one.
774	 * Since the timewindow is very small for this, the error is
775	 * going to be only a few weenieseconds (as Dave Mills would
776	 * say), so lets just not talk more about it, OK ?
777	 */
778	if (tco->tc_poll_pps)
779		tco->tc_poll_pps(tco);
780	if (timedelta != 0) {
781		tc->tc_offset_nano += (u_int64_t)(tickdelta * 1000) << 32;
782		timedelta -= tickdelta;
783		force++;
784	}
785
786	while (tc->tc_offset_nano >= 1000000000ULL << 32) {
787		tc->tc_offset_nano -= 1000000000ULL << 32;
788		tc->tc_offset_sec++;
789		tc->tc_frequency = tc->tc_tweak->tc_frequency;
790		tc->tc_adjustment = tc->tc_tweak->tc_adjustment;
791		ntp_update_second(tc);	/* XXX only needed if xntpd runs */
792		tco_setscales(tc);
793		force++;
794	}
795
796	if (!force)
797		return;
798
799	tc->tc_offset_micro = (tc->tc_offset_nano / 1000) >> 32;
800
801	/* Figure out the wall-clock time */
802	tc->tc_nanotime.tv_sec = tc->tc_offset_sec + boottime.tv_sec;
803	tc->tc_nanotime.tv_nsec =
804	    (tc->tc_offset_nano >> 32) + boottime.tv_usec * 1000;
805	tc->tc_microtime.tv_usec = tc->tc_offset_micro + boottime.tv_usec;
806	if (tc->tc_nanotime.tv_nsec >= 1000000000) {
807		tc->tc_nanotime.tv_nsec -= 1000000000;
808		tc->tc_microtime.tv_usec -= 1000000;
809		tc->tc_nanotime.tv_sec++;
810	}
811	time_second = tc->tc_microtime.tv_sec = tc->tc_nanotime.tv_sec;
812
813	timecounter = tc;
814}
815
816static int
817sysctl_kern_timecounter_frequency SYSCTL_HANDLER_ARGS
818{
819
820	return (sysctl_handle_opaque(oidp,
821	    &timecounter->tc_tweak->tc_frequency,
822	    sizeof(timecounter->tc_tweak->tc_frequency), req));
823}
824
825static int
826sysctl_kern_timecounter_adjustment SYSCTL_HANDLER_ARGS
827{
828
829	return (sysctl_handle_opaque(oidp,
830	    &timecounter->tc_tweak->tc_adjustment,
831	    sizeof(timecounter->tc_tweak->tc_adjustment), req));
832}
833
834SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW, 0, "");
835
836SYSCTL_PROC(_kern_timecounter, OID_AUTO, frequency, CTLTYPE_INT | CTLFLAG_RW,
837    0, sizeof(u_int), sysctl_kern_timecounter_frequency, "I", "");
838
839SYSCTL_PROC(_kern_timecounter, OID_AUTO, adjustment, CTLTYPE_INT | CTLFLAG_RW,
840    0, sizeof(int), sysctl_kern_timecounter_adjustment, "I", "");
841