kern: clarify boot time

In FreeBSD, the current time is computed from uptime + boottime. Uptime
is a continuous, smooth function that's monotonically increasing. To
effect changes to the current time, boottime is adjusted.  boottime is
mutable and shouldn't be cached against future need. Document the
current implementation, with the caveat that we may stop stepping
boottime on resume in the future and will step uptime instead (noted in
the commit message, but not in the code).

Sponsored by:		Netflix
Reviewed by:		phk, rpokala
Differential Revision:	https://reviews.freebsd.org/D30116
This commit is contained in:
Warner Losh 2021-05-05 12:32:13 -06:00
parent cb58805943
commit a512d0ab00
2 changed files with 21 additions and 3 deletions

View File

@ -31,7 +31,7 @@
.\" .\"
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd September 17, 2004 .Dd May 4, 2021
.Dt TIME 9 .Dt TIME 9
.Os .Os
.Sh NAME .Sh NAME
@ -48,7 +48,18 @@
.Sh DESCRIPTION .Sh DESCRIPTION
The The
.Va boottime .Va boottime
variable holds the system boot time. variable holds the estimated system boot time.
This time is initially set when the system boots, either from the RTC, or from a
time estimated from the system's root filesystem.
When the current system time is set, stepped by
.Xr ntpd 8 ,
or a new time is read from the RTC as the system resumes,
.Va boottime
is recomputed as new_time - uptime.
The
.Xr sysctl 8
.Va kern.boottime
returns this value.
.Pp .Pp
The The
.Va time_second .Va time_second
@ -83,6 +94,7 @@ and in an atomic manner.
The The
.Va boottime .Va boottime
variable may be read and written without special precautions. variable may be read and written without special precautions.
It is adjusted when the phase of the system time changes.
.Sh SEE ALSO .Sh SEE ALSO
.Xr clock_settime 2 , .Xr clock_settime 2 ,
.Xr ntp_adjtime 2 , .Xr ntp_adjtime 2 ,

View File

@ -103,11 +103,17 @@ int tc_min_ticktock_freq = 1;
volatile time_t time_second = 1; volatile time_t time_second = 1;
volatile time_t time_uptime = 1; volatile time_t time_uptime = 1;
/*
* The system time is always computed by summing the estimated boot time and the
* system uptime. The timehands track boot time, but it changes when the system
* time is set by the user, stepped by ntpd or adjusted when resuming. It
* is set to new_time - uptime.
*/
static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS); static int sysctl_kern_boottime(SYSCTL_HANDLER_ARGS);
SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime,
CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
sysctl_kern_boottime, "S,timeval", sysctl_kern_boottime, "S,timeval",
"System boottime"); "Estimated system boottime");
SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, SYSCTL_NODE(_kern, OID_AUTO, timecounter, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
""); "");