Add the half of time-of-day clock resolution when we adjust system time from
time-of-day clock or vice versa. For x86 systems, RTC resolution is one second and we used to lose up to one second whenever we initialize system time from RTC or write system time back to RTC. With this change, margin of error per conversion is roughly between -0.5 and +0.5 second rather than between -1 and 0 second. Note that it does not take care of errors from getnanotime(9) (which is up to 1/hz second) or CLOCK_GETTIME() latency. These are just too expensive to correct and it is not worthy of the cost.
This commit is contained in:
parent
f02d86e269
commit
116a77bda7
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=211230
@ -62,6 +62,7 @@ __FBSDID("$FreeBSD$");
|
||||
|
||||
static device_t clock_dev = NULL;
|
||||
static long clock_res;
|
||||
static struct timespec clock_adj;
|
||||
|
||||
/* XXX: should be kern. now, it's no longer machdep. */
|
||||
static int disable_rtc_set;
|
||||
@ -87,9 +88,12 @@ clock_register(device_t dev, long res) /* res has units of microseconds */
|
||||
}
|
||||
clock_dev = dev;
|
||||
clock_res = res;
|
||||
clock_adj.tv_sec = res / 2 / 1000000;
|
||||
clock_adj.tv_nsec = res / 2 % 1000000 * 1000;
|
||||
if (bootverbose)
|
||||
device_printf(dev, "registered as a time-of-day clock "
|
||||
"(resolution %ldus)\n", res);
|
||||
"(resolution %ldus, adjustment %jd.%09jds)\n", res,
|
||||
(intmax_t)clock_adj.tv_sec, (intmax_t)clock_adj.tv_nsec);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -127,6 +131,7 @@ inittodr(time_t base)
|
||||
}
|
||||
|
||||
ts.tv_sec += utc_offset();
|
||||
timespecadd(&ts, &clock_adj);
|
||||
tc_setclock(&ts);
|
||||
return;
|
||||
|
||||
@ -151,6 +156,7 @@ resettodr(void)
|
||||
return;
|
||||
|
||||
getnanotime(&ts);
|
||||
timespecadd(&ts, &clock_adj);
|
||||
ts.tv_sec -= utc_offset();
|
||||
/* XXX: We should really set all registered RTCs */
|
||||
if ((error = CLOCK_SETTIME(clock_dev, &ts)) != 0)
|
||||
|
Loading…
Reference in New Issue
Block a user