⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ktime.c

📁 内核中关于nano计时的功能
💻 C
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************** *								       * * Copyright (c) David L. Mills 1993-2001			       * *								       * * Permission to use, copy, modify, and distribute this software and   * * its documentation for any purpose and without fee is hereby	       * * granted, provided that the above copyright notice appears in all    * * copies and that both the copyright notice and this permission       * * notice appear in supporting documentation, and that the name	       * * University of Delaware not be used in advertising or publicity      * * pertaining to distribution of the software without specific,	       * * written prior permission. The University of Delaware makes no       * * representations about the suitability this software for any	       * * purpose. It is provided "as is" without express or implied	       * * warranty.							       * *								       * **********************************************************************/#include "kern.h"/* * Generic NTP kernel interface * * These routines constitute the Network Time Protocol (NTP) interfaces * for user and daemon application programs. The ntp_gettime() routine * provides the time, maximum error (synch distance) and estimated error * (dispersion) to client user application programs. The ntp_adjtime() * routine is used by the NTP daemon to adjust the system clock to an * externally derived time. The time offset and related variables set by * this routine are used by other routines in this module to adjust the * phase and frequency of the clock discipline loop which controls the * system clock. * * When the kernel time is reckoned directly in nanoseconds (NTP_NANO * defined), the time at each tick interrupt is derived directly from * the kernel time variable. When the kernel time is reckoned in * microseconds, (NTP_NANO undefined), the time is derived from the * kernel time variable together with a variable representing the * leftover nanoseconds at the last tick interrupt. In either case, the * current nanosecond time is reckoned from these values plus an * interpolated value derived by the clock routines in another * architecture-specific module. The interpolation can use either a * dedicated counter or a processor cycle counter (PCC) implemented in * some architectures. * * Note that all routines must run at priority splclock or higher. *//* * Phase/frequency-lock loop (PLL/FLL) definitions * * The nanosecond clock discipline uses two variable types, time * variables and frequency variables. Both types are represented as 64- * bit fixed-point quantities with the decimal point between two 32-bit * halves. On a 32-bit machine, each half is represented as a single * word and mathematical operations are done using multiple-precision * arithmetic. On a 64-bit machine, ordinary computer arithmetic is * used. * * A time variable is a signed 64-bit fixed-point number in ns and * fraction. It represents the remaining time offset to be amortized * over succeeding tick interrupts. The maximum time offset is about * 0.5 s and the resolution is about 2.3e-10 ns. * *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |s s s|			 ns				   | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |			    fraction				   | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * * A frequency variable is a signed 64-bit fixed-point number in ns/s * and fraction. It represents the ns and fraction to be added to the * kernel time variable at each second. The maximum frequency offset is * about +-500000 ns/s and the resolution is about 2.3e-10 ns/s. * *			1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |s s s s s s s s s s s s s|	          ns/s			   | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * |			    fraction				   | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ *//* * The following variables establish the state of the PLL/FLL and the * residual time and frequency offset of the local clock. */#define SHIFT_PLL	4	/* PLL loop gain (shift) */#define SHIFT_FLL	2	/* FLL loop gain (shift) */int time_state = TIME_OK;	/* clock state */int time_status = STA_UNSYNC;	/* clock status bits */long time_tai;			/* TAI offset (s) */long time_monitor;		/* last time offset scaled (ns) */long time_constant;		/* poll interval (shift) (s) */long time_precision = 1;	/* clock precision (ns) */long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */long time_esterror = MAXPHASE / 1000; /* estimated error (us) */long time_reftime;		/* time at last adjustment (s) */long time_tick;			/* nanoseconds per tick (ns) */#if !defined(NTP_NANO)long time_nano;			/* nanoseconds past last tick */#endif /* NTP_NANO */l_fp time_offset;		/* time offset (ns) */l_fp time_freq;			/* frequency offset (ns/s) */l_fp time_adj;			/* tick adjust (ns/s) */l_fp time_phase;		/* time phase (ns) */#ifdef PPS_SYNC/* * The following variables are used when a pulse-per-second (PPS) signal * is available and connected via a modem control lead. They establish * the engineering parameters of the clock discipline loop when * controlled by the PPS signal. */#define PPS_FAVG	2	/* min freq avg interval (s) (shift) */#define PPS_FAVGDEF	8	/* default freq avg int (s) (shift) */#define PPS_FAVGMAX	15	/* max freq avg interval (s) (shift) */#define PPS_PAVG	4	/* phase avg interval (s) (shift) */#define PPS_VALID	120	/* PPS signal watchdog max (s) */#define PPS_MAXWANDER	100000	/* max PPS wander (ns/s) */#define PPS_POPCORN	2	/* popcorn spike threshold (shift) */struct timespec pps_tf[3];	/* phase median filter */l_fp pps_freq;			/* scaled frequency offset (ns/s) */long pps_lastfreq;		/* last scaled freq offset (ns/s) */long pps_fcount;		/* frequency accumulator */long pps_jitter;		/* nominal jitter (ns) */long pps_stabil;		/* nominal stability (scaled ns/s) */long pps_lastcount;		/* last counter offset */long pps_lastsec;		/* time at last calibration (s) */int pps_valid;			/* signal watchdog counter */int pps_shift = PPS_FAVG;	/* interval duration (s) (shift) */int pps_shiftmax = PPS_FAVGDEF;	/* max interval duration (s) (shift) */int pps_intcnt;			/* wander counter *//* * PPS signal quality monitors */long pps_calcnt;		/* calibration intervals */long pps_jitcnt;		/* jitter limit exceeded */long pps_stbcnt;		/* stability limit exceeded */long pps_errcnt;		/* calibration errors */#endif /* PPS_SYNC *//* * End of phase/frequency-lock loop (PLL/FLL) definitions */void hardupdate();/* * ntp_gettime() - NTP user application interface * * See the timex.h header file for synopsis and API description. Note * that the TAI offset is returned in the ntvtimeval.tai structure * member.  */intntp_gettime(tp)	struct ntptimeval *tp;	/* pointer to argument structure */{	struct ntptimeval ntv;	/* temporary structure */	struct timespec atv;	/* nanosecond time */	int s;			/* caller priority */	s = splclock();	nano_time(&atv);#ifdef NTP_NANO	ntv.time.tv_sec = atv.tv_sec;	ntv.time.tv_nsec = atv.tv_nsec;#else	if (!(time_status & STA_NANO))		atv.tv_nsec /= 1000;	ntv.time.tv_sec = atv.tv_sec;	ntv.time.tv_usec = atv.tv_nsec;#endif /* NTP_NANO */	ntv.maxerror = time_maxerror;	ntv.esterror = time_esterror;	ntv.tai = time_tai;	splx(s);	*tp = ntv;		/* copy out the result structure */	/*	 * Status word error decode. If any of these conditions occur,	 * an error is returned, instead of the status word. Most	 * applications will care only about the fact the system clock	 * may not be trusted, not about the details.	 *	 * Hardware or software error	 */	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||	/*	 * PPS signal lost when either time or frequency synchronization	 * requested	 */	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&	    !(time_status & STA_PPSSIGNAL)) ||	/*	 * PPS jitter exceeded when time synchronization requested	 */	    (time_status & STA_PPSTIME &&	    time_status & STA_PPSJITTER) ||	/*	 * PPS wander exceeded or calibration error when frequency	 * synchronization requested	 */	    (time_status & STA_PPSFREQ &&	    time_status & (STA_PPSWANDER | STA_PPSERROR)))		return (TIME_ERROR);	return (time_state);}/* * ntp_adjtime() - NTP daemon application interface * * See the timex.h header file for synopsis and API description. Note * that the timex.constant structure member has a dual purpose to set * the time constant and to set the TAI offset. */intntp_adjtime(tp)	struct timex *tp;	/* pointer to argument structure */{	struct timex ntv;	/* temporary structure */	long freq;		/* frequency ns/s) */	int modes;		/* mode bits from structure */	int s;			/* caller priority */	ntv = *tp;		/* copy in the argument structure */	/*	 * Update selected clock variables - only the superuser can	 * change anything. Note that there is no error checking here on	 * the assumption the superuser should know what it is doing.	 * Note that either the time constant or TAI offset are loaded	 * from the ntv.constant member, depending on the mode bits. If	 * the STA_PLL bit in the status word is cleared, the state and	 * status words are reset to the initial values at boot.	 */	modes = ntv.modes;	if (ROOT)		return (EPERM);	s = splclock();	if (modes & MOD_MAXERROR)		time_maxerror = ntv.maxerror;	if (modes & MOD_ESTERROR)		time_esterror = ntv.esterror;	if (modes & MOD_STATUS) {		if (time_status & STA_PLL && !(ntv.status & STA_PLL)) {			time_state = TIME_OK;			time_status = STA_UNSYNC;#ifdef PPS_SYNC			pps_shift = PPS_FAVG;#endif /* PPS_SYNC */		}		time_status &= STA_RONLY;		time_status |= ntv.status & ~STA_RONLY;	}	if (modes & MOD_TIMECONST) {		if (ntv.constant < 0)			time_constant = 0;		else if (ntv.constant > MAXTC)			time_constant = MAXTC;		else			time_constant = ntv.constant;	}	if (modes & MOD_TAI) {		if (ntv.constant > 0)			time_tai = ntv.constant;	}#ifdef PPS_SYNC	if (modes & MOD_PPSMAX) {		if (ntv.shift < PPS_FAVG)			pps_shiftmax = PPS_FAVG;		else if (ntv.shift > PPS_FAVGMAX)			pps_shiftmax = PPS_FAVGMAX;		else			pps_shiftmax = ntv.shift;	}#endif /* PPS_SYNC */	if (modes & MOD_NANO)		time_status |= STA_NANO;	if (modes & MOD_MICRO)		time_status &= ~STA_NANO;	if (modes & MOD_CLKB)		time_status |= STA_CLK;	if (modes & MOD_CLKA)		time_status &= ~STA_CLK;	if (modes & MOD_OFFSET) {		if (time_status & STA_NANO)			hardupdate(&TIMEVAR, ntv.offset);		else			hardupdate(&TIMEVAR, ntv.offset * 1000);	}	if (modes & MOD_FREQUENCY) {		freq = ntv.freq / SCALE_PPM;		if (freq > MAXFREQ)			L_LINT(time_freq, MAXFREQ);		else if (freq < -MAXFREQ)			L_LINT(time_freq, -MAXFREQ);		else			L_LINT(time_freq, freq);#ifdef PPS_SYNC		pps_freq = time_freq;#endif /* PPS_SYNC */	}	/*	 * Retrieve all clock variables. Note that the TAI offset is	 * returned only by ntp_gettime();	 */	if (time_status & STA_NANO)		ntv.offset = time_monitor;	else		ntv.offset = time_monitor / 1000;	ntv.freq = L_GINT(time_freq) * SCALE_PPM;	ntv.maxerror = time_maxerror;	ntv.esterror = time_esterror;	ntv.status = time_status;	ntv.constant = time_constant;	if (time_status & STA_NANO)		ntv.precision = time_precision;	else		ntv.precision = time_precision / 1000;	ntv.tolerance = MAXFREQ * SCALE_PPM;#ifdef PPS_SYNC	ntv.shift = pps_shift;	ntv.ppsfreq = L_GINT(pps_freq) * SCALE_PPM;	if (time_status & STA_NANO)		ntv.jitter = pps_jitter;	else		ntv.jitter = pps_jitter / 1000;	ntv.stabil = pps_stabil;	ntv.calcnt = pps_calcnt;	ntv.errcnt = pps_errcnt;	ntv.jitcnt = pps_jitcnt;	ntv.stbcnt = pps_stbcnt;#endif /* PPS_SYNC */	splx(s);	*tp = ntv;		/* copy out the result structure */	/*	 * Status word error decode. See comments in	 * ntp_gettime() routine.	 */	if ((time_status & (STA_UNSYNC | STA_CLOCKERR)) ||	    (time_status & (STA_PPSFREQ | STA_PPSTIME) &&	    !(time_status & STA_PPSSIGNAL)) ||	    (time_status & STA_PPSTIME &&	    time_status & STA_PPSJITTER) ||	    (time_status & STA_PPSFREQ &&	    time_status & (STA_PPSWANDER | STA_PPSERROR)))		return (TIME_ERROR);	return (time_state);}/* * ntp_tick_adjust() - called every tick for precision time adjustment * * This routine is ordinarily called from the tick interrupt routine * hardclock(). To minimize the jitter that might result when a higher * priority interrupt occurs after the tick interrupt is taken and * before the system clock is updated, this routine (and the following * routine second_overflow()) should be called early in the hardclock() * code path.  */voidntp_tick_adjust(tvp, tick_update)#ifdef NTP_NANO	struct timespec *tvp;	/* pointer to nanosecond clock */	int tick_update;	/* residual from adjtime() (ns) */#else	struct timeval *tvp;	/* pointer to microsecond clock */	int tick_update;	/* residual from adjtime() (us) */#endif /* NTP_NANO */{	long ltemp, time_update;	/*	 * Update the nanosecond and microsecond clocks. If the phase	 * increment exceeds the tick period, update the clock phase.	 */#ifdef NTP_NANO	time_update = tick_update;	L_ADD(time_phase, time_adj);	ltemp = L_GINT(time_phase) / hz;	time_update += ltemp;	L_ADDHI(time_phase, -ltemp * hz);	tvp->tv_nsec += time_update;#else	time_update = tick_update;	L_ADD(time_phase, time_adj);	ltemp = L_GINT(time_phase) / (1000 * hz);	time_update += ltemp;	L_ADDHI(time_phase, -ltemp * (1000 * hz));	tvp->tv_usec += time_update;	time_nano = L_GINT(time_phase) / hz;#endif /* NTP_NANO */}/* * second_overflow() - called after ntp_tick_adjust() * * This routine is ordinarily called immediately following the above * routine ntp_tick_adjust(). While these two routines are normally * combined, they are separated here only for the purposes of * simulation. */voidsecond_overflow(tvp)#ifdef NTP_NANO	struct timespec *tvp;	/* pointer to nanosecond clock */#else	struct timeval *tvp;	/* pointer to microsecond clock */#endif /* NTP_NANO */{	l_fp ftemp;		/* 32/64-bit temporary */	/*	 * On rollover of the second both the nanosecond and microsecond	 * clocks are updated and the state machine cranked as	 * necessary. The phase adjustment to be used for the next	 * second is calculated and the maximum error is increased by	 * the tolerance.	 */#ifdef NTP_NANO	if (tvp->tv_nsec >= NANOSECOND) {		tvp->tv_nsec -= NANOSECOND;#else	if (tvp->tv_usec >= 1000000) {		tvp->tv_usec -= 1000000;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -