From: Matthew Dillon Date: Sat, 4 Dec 2004 20:38:45 +0000 (+0000) Subject: Fix the boottime calculation when the time of day is set in absolute terms. X-Git-Tag: v2.0.1~9596 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/a81931cc8f67b284993e55aa842b2e982bcba4b2 Fix the boottime calculation when the time of day is set in absolute terms. We want boottime to be calculated based on the current real time minus our best uptime guess. gd_time_seconds survives a lot of time issues and is our best uptime guess and since it is already used to calculate the new basetime, we can just assign boottime to basetime. Note that boottime is not defined quite the same way as basetime. The system's internal timebase is not compensated for drift or ever jumped, and is guarenteed to be monotonically increasing. Drift compensation is accomplished by adjusting basetime, so basetime will always slowly diverge from boottime if e.g. ntpd is running. Submitted-by: Paul Herman --- diff --git a/sys/kern/kern_clock.c b/sys/kern/kern_clock.c index 42d31dc8b8..d11e1921f7 100644 --- a/sys/kern/kern_clock.c +++ b/sys/kern/kern_clock.c @@ -70,7 +70,7 @@ * * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94 * $FreeBSD: src/sys/kern/kern_clock.c,v 1.105.2.10 2002/10/17 13:19:40 maxim Exp $ - * $DragonFly: src/sys/kern/kern_clock.c,v 1.27 2004/11/20 20:25:09 dillon Exp $ + * $DragonFly: src/sys/kern/kern_clock.c,v 1.28 2004/12/04 20:38:45 dillon Exp $ */ #include "opt_ntp.h" @@ -227,7 +227,20 @@ set_timeofday(struct timespec *ts) basetime.tv_nsec += 1000000000; --basetime.tv_sec; } - boottime.tv_sec = basetime.tv_sec - mycpu->gd_time_seconds; + + /* + * Note that basetime diverges from boottime as the clock drift is + * compensated for, so we cannot do away with boottime. When setting + * the absolute time of day the drift is 0 (for an instant) and we + * can simply assign boottime to basetime. + * + * Note that nanouptime() is based on gd_time_seconds which is drift + * compensated up to a point (it is guarenteed to remain monotonically + * increasing). gd_time_seconds is thus our best uptime guess and + * suitable for use in the boottime calculation. It is already taken + * into account in the basetime calculation above. + */ + boottime.tv_sec = basetime.tv_sec; timedelta = 0; crit_exit(); }