Index: sys/kern/kern_tc.c =================================================================== --- sys/kern/kern_tc.c (revision 228435) +++ sys/kern/kern_tc.c (working copy) @@ -28,9 +28,7 @@ __FBSDID("$FreeBSD$"); #include #include #include -#ifdef FFCLOCK #include -#endif #include #include #include @@ -454,15 +452,6 @@ getmicrotime(struct timeval *tvp) #endif /* FFCLOCK */ #ifdef FFCLOCK -/* - * Support for feed-forward synchronization algorithms. This is heavily inspired - * by the timehands mechanism but kept independent from it. *_windup() functions - * have some connection to avoid accessing the timecounter hardware more than - * necessary. - */ - -int sysclock_active = SYSCLOCK_FBCK; - /* Feed-forward clock estimates kept updated by the synchronization daemon. */ struct ffclock_estimate ffclock_estimate; struct bintime ffclock_boottime; /* Feed-forward boot time estimate. */ @@ -956,9 +945,144 @@ getmicrotime(struct timeval *tvp) getmicrouptime_fromclock(tvp, sysclock_active); } + #endif /* FFCLOCK */ /* + * Support for feed-forward synchronization algorithms. This is heavily inspired + * by the timehands mechanism but kept independent from it. *_windup() functions + * have some connection to avoid accessing the timecounter hardware more than + * necessary. + */ + +int sysclock_active = SYSCLOCK_FBCK; + +/* Internal NTP status and error estimates. */ +extern int time_status; +extern long time_esterror; + +void +sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast) +{ + struct fbclock_info *fbi; + struct timehands *th; + struct bintime bt; + unsigned int delta, gen; +#ifdef FFCLOCK + ffcounter ffcount; + struct fftimehands *ffth; + struct ffclock_info *ffi; + struct ffclock_estimate cest; + + ffi = &clock_snap->ff_info; +#endif + + fbi = &clock_snap->fb_info; + delta = 0; + + do { + th = timehands; + gen = th->th_generation; + fbi->th_scale = th->th_scale; + fbi->tick_time = th->th_offset; +#ifdef FFCLOCK + ffth = fftimehands; + ffi->tick_time = ffth->tick_time_lerp; + ffi->tick_time_lerp = ffth->tick_time_lerp; + ffi->period = ffth->cest.period; + ffi->period_lerp = ffth->period_lerp; + clock_snap->ffcount = ffth->tick_ffcount; + cest = ffth->cest; +#endif + if (!fast) + delta = tc_delta(th); + } while (gen == 0 || gen != th->th_generation); + + clock_snap->delta = delta; + clock_snap->sysclock_active = sysclock_active; + + /* Record feedback clock status and error. */ + clock_snap->fb_info.status = time_status; + /* XXX: Very crude estimate of feedback clock error. */ + bt.sec = time_esterror / 1000000; + bt.frac = ((time_esterror - bt.sec) * 1000000) * + (uint64_t)18446744073709ULL; + clock_snap->fb_info.error = bt; + +#ifdef FFCLOCK + if (!fast) + clock_snap->ffcount += delta; + + /* Record feed-forward clock leap second adjustment. */ + ffi->leapsec_adjustment = cest.leapsec_total; + if (clock_snap->ffcount > cest.leapsec_next) + ffi->leapsec_adjustment -= cest.leapsec; + + /* Record feed-forward clock status and error. */ + clock_snap->ff_info.status = cest.status; + ffcount = clock_snap->ffcount - cest.update_ffcount; + ffclock_convert_delta(ffcount, cest.period, &bt); + /* 18446744073709 = int(2^64/1e12), err_bound_rate in [ps/s]. */ + bintime_mul(&bt, cest.errb_rate * (uint64_t)18446744073709ULL); + /* 18446744073 = int(2^64 / 1e9), since err_abs in [ns]. */ + bintime_addx(&bt, cest.errb_abs * (uint64_t)18446744073ULL); + clock_snap->ff_info.error = bt; +#endif +} + +int +sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt, + int whichclock, uint32_t flags) +{ +#ifdef FFCLOCK + struct bintime bt2; + uint64_t period; +#endif + + switch (whichclock) { + case SYSCLOCK_FBCK: + *bt = cs->fb_info.tick_time; + + /* If snapshot was created with !fast, delta will be >0. */ + if (cs->delta > 0) + bintime_addx(bt, cs->fb_info.th_scale * cs->delta); + + if ((flags & FBCLOCK_UPTIME) == 0) + bintime_add(bt, &boottimebin); + break; +#ifdef FFCLOCK + case SYSCLOCK_FFWD: + if (flags & FFCLOCK_LERP) { + *bt = cs->ff_info.tick_time_lerp; + period = cs->ff_info.period_lerp; + } else { + *bt = cs->ff_info.tick_time; + period = cs->ff_info.period; + } + + /* If snapshot was created with !fast, delta will be >0. */ + if (cs->delta > 0) { + ffclock_convert_delta(cs->delta, period, &bt2); + bintime_add(bt, &bt2); + } + + /* Leap second adjustment. */ + if (flags & FFCLOCK_LEAPSEC) + bt->sec -= cs->ff_info.leapsec_adjustment; + + /* Boot time adjustment, for uptime/monotonic clocks. */ + if (flags & FFCLOCK_UPTIME) + bintime_sub(bt, &ffclock_boottime); +#endif + default: + return (EINVAL); + break; + } + + return (0); +} + +/* * Initialize a new timecounter and possibly use it. */ void Index: sys/kern/kern_ntptime.c =================================================================== --- sys/kern/kern_ntptime.c (revision 228435) +++ sys/kern/kern_ntptime.c (working copy) @@ -148,13 +148,13 @@ typedef int64_t l_fp; #define SHIFT_FLL 2 /* FLL loop gain (shift) */ static int time_state = TIME_OK; /* clock state */ -static int time_status = STA_UNSYNC; /* clock status bits */ +int time_status = STA_UNSYNC; /* clock status bits */ static long time_tai; /* TAI offset (s) */ static long time_monitor; /* last time offset scaled (ns) */ static long time_constant; /* poll interval (shift) (s) */ static long time_precision = 1; /* clock precision (ns) */ static long time_maxerror = MAXPHASE / 1000; /* maximum error (us) */ -static long time_esterror = MAXPHASE / 1000; /* estimated error (us) */ +long time_esterror = MAXPHASE / 1000; /* estimated error (us) */ static long time_reftime; /* time at last adjustment (s) */ static l_fp time_offset; /* time offset (ns) */ static l_fp time_freq; /* frequency offset (ns/s) */ Index: sys/sys/timeffc.h =================================================================== --- sys/sys/timeffc.h (revision 228435) +++ sys/sys/timeffc.h (working copy) @@ -81,20 +81,75 @@ extern int sysclock_active; #define FFCLOCK_STA_WARMUP 2 /* - * Clock flags to select how the feed-forward counter is converted to absolute - * time by ffclock_convert_abs(). - * FAST: do not read the hardware counter, return feed-forward clock time - * at last tick. The time returned has the resolution of the kernel - * tick (1/hz [s]). - * LERP: linear interpolation of ffclock time to guarantee monotonic time. - * LEAPSEC: include leap seconds. - * UPTIME: removes time of boot. + * Flags for use by sysclock_snap2bintime() and various ffclock_ functions to + * control how the timecounter hardware is read and how the hardware snapshot is + * converted into absolute time. + * FFCLOCK_FAST: Do not read the hardware counter, instead using the + * value at last tick. The time returned has a resolution + * of the kernel tick timer (1/hz [s]). + * FFCLOCK_LERP: Linear interpolation of ffclock time to guarantee + * monotonic time. + * FFCLOCK_LEAPSEC: Include leap seconds. + * {FB|FF}CLOCK_UPTIME: Time stamp should be relative to system boot, not epoch. */ -#define FFCLOCK_FAST 1 -#define FFCLOCK_LERP 2 -#define FFCLOCK_LEAPSEC 4 -#define FFCLOCK_UPTIME 8 +#define FFCLOCK_FAST 0x00000001 +#define FFCLOCK_LERP 0x00000002 +#define FFCLOCK_LEAPSEC 0x00000004 +#define FFCLOCK_UPTIME 0x00000008 +#define FFCLOCK_MASK 0x0000ffff +#define FBCLOCK_UPTIME 0x00010000 +#define FBCLOCK_MASK 0xffff0000 + +/* + * Feedback clock specific info structure. The feedback clock's estimation of + * clock error is an absolute figure determined by the NTP algorithm. The status + * is determined by the userland daemon. + */ +struct fbclock_info { + struct bintime error; + struct bintime tick_time; + uint64_t th_scale; + int status; +}; + +/* + * Feed-forward clock specific info structure. The feed-forward clock's + * estimation of clock error is an upper bound, which although potentially + * looser than the feedback clock equivalent, is much more reliable. The status + * is determined by the userland daemon. + */ +struct ffclock_info { + struct bintime error; + struct bintime tick_time; + struct bintime tick_time_lerp; + uint64_t period; + uint64_t period_lerp; + int leapsec_adjustment; + int status; +}; + +/* + * Snapshot of system clocks and related information. Holds time read from each + * clock based on a single read of the active hardware timecounter, as well as + * respective clock information such as error estimates and the ffcounter value + * at the time of the read. + */ +struct sysclock_snap { + struct fbclock_info fb_info; + struct ffclock_info ff_info; + ffcounter ffcount; + unsigned int delta; + int sysclock_active; +}; + +/* Take a snapshot of the system clocks and related information. */ +void sysclock_getsnapshot(struct sysclock_snap *clock_snap, int fast); + +/* Convert a timestamp from the selected system clock into bintime. */ +int sysclock_snap2bintime(struct sysclock_snap *cs, struct bintime *bt, + int whichclock, uint32_t flags); + /* Resets feed-forward clock from RTC */ void ffclock_reset_clock(struct timespec *ts);