.\" $OpenBSD: microtime.9,v 1.23 2022/12/28 15:46:39 cheloha Exp $ .\" $NetBSD: microtime.9,v 1.2 1999/03/16 00:40:47 garbled Exp $ .\" .\" Copyright (c) 1998 The NetBSD Foundation, Inc. .\" All rights reserved. .\" .\" This code is derived from software contributed to The NetBSD Foundation .\" by Jeremy Cooper. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .Dd $Mdocdate: December 28 2022 $ .Dt MICROTIME 9 .Os .Sh NAME .Nm microuptime , .Nm getmicrouptime , .Nm nanouptime , .Nm getnanouptime , .Nm nsecuptime , .Nm getnsecuptime , .Nm getuptime , .Nm nanoruntime , .Nm getnsecruntime , .Nm microtime , .Nm getmicrotime , .Nm nanotime , .Nm getnanotime , .Nm gettime , .Nm microboottime , .Nm nanoboottime .Nd get the time .Sh SYNOPSIS .In sys/time.h .Ft void .Fo microuptime .Fa "struct timeval *tv" .Fc .Ft void .Fo getmicrouptime .Fa "struct timeval *tv" .Fc .Ft void .Fo nanouptime .Fa "struct timespec *ts" .Fc .Ft void .Fo getnanouptime .Fa "struct timespec *ts" .Fc .Ft uint64_t .Fo nsecuptime .Fa "void" .Fc .Ft uint64_t .Fo getnsecuptime .Fa "void" .Fc .Ft time_t .Fo getuptime .Fa "void" .Fc .Ft void .Fo nanoruntime .Fa "struct timespec *ts" .Fc .Ft uint64_t .Fo getnsecruntime .Fa "void" .Fc .Ft void .Fo microtime .Fa "struct timeval *tv" .Fc .Ft void .Fo getmicrotime .Fa "struct timeval *tv" .Fc .Ft void .Fo nanotime .Fa "struct timespec *ts" .Fc .Ft void .Fo getnanotime .Fa "struct timespec *ts" .Fc .Ft time_t .Fo gettime .Fa "void" .Fc .Ft void .Fo microboottime .Fa "struct timeval *tv" .Fc .Ft void .Fo nanoboottime .Fa "struct timespec *ts" .Fc .Sh DESCRIPTION The kernel has three clocks and a variety of interfaces for reading them. .Pp The .Sy uptime clock measures the time elapsed since the system booted. It begins at zero and advances monotonically. The uptime clock may be read with the following functions: .Bl -column "getmicrouptimeX" "Output Format" "Source" -offset indent .It Em Name Ta Em Output Format Ta Em Source .It Fn microuptime Ta Vt struct timeval Ta hardware .It Fn getmicrouptime Ta Vt struct timeval Ta timestamp .It Fn nanouptime Ta Vt struct timespec Ta hardware .It Fn getnanouptime Ta Vt struct timespec Ta timestamp .It Fn nsecuptime Ta Ft uint64_t Ta hardware .It Fn getnsecuptime Ta Ft uint64_t Ta timestamp .It Fn getuptime Ta Ft time_t Ta timestamp .El .Pp The .Sy runtime clock measures the time elapsed since the system booted, less any time the system is suspended or hibernating. It begins at zero and normally advances monotonically, but pauses while the system is suspended or hibernating. The runtime clock may be read with the following functions: .Bl -column "getnsecruntimeX" "Output Format" "Source" -offset indent .It Em Name Ta Em Output Format Ta Em Source .It Fn nanoruntime Ta Vt struct timespec Ta hardware .It Fn getnsecruntime Ta Ft uint64_t Ta timestamp .El .Pp The .Sy UTC clock measures the time elapsed since Jan 1 1970 00:00:00 .Pq the Unix Epoch . The clock normally advances monotonically, but jumps when a process calls .Xr clock_settime 2 or .Xr settimeofday 2 . The UTC clock may be read with the following functions: .Bl -column "getmicrotimeX" "Output Format" "Source" -offset indent .It Em Name Ta Em Output Format Ta Em Source .It Fn microtime Ta Vt struct timeval Ta hardware .It Fn getmicrotime Ta Vt struct timeval Ta timestamp .It Fn nanotime Ta Vt struct timespec Ta hardware .It Fn getnanotime Ta Vt struct timespec Ta timestamp .It Fn gettime Ta Ft time_t Ta timestamp .El .Pp The kernel also maintains a .Sy boot timestamp . It is the moment on the UTC clock when the system booted. The timestamp jumps when a process calls .Xr clock_settime 2 or .Xr settimeofday 2 . The boot timestamp may be read with the following functions: .Bl -column "microboottimeX" "Output Format" "Source" -offset indent .It Em Name Ta Em Output Format Ta Em Source .It Fn microboottime Ta Vt struct timeval Ta timestamp .It Fn nanoboottime Ta Vt struct timespec Ta timestamp .El .Pp Functions that source from the .Em hardware provide the most precise result possible. Functions that source from a .Em timestamp provide a far less precise result, but do so very quickly. On most platforms, timestamps are updated approximately 100 times per second. .Sh CONTEXT These functions may be called during autoconf, from process context, or from any interrupt context. .Sh RETURN VALUES .Fn nsecuptime , .Fn getnsecuptime , and .Fn getnsecruntime return a count of nanoseconds. .Pp .Fn getuptime and .Fn gettime return a count of seconds. .Sh ERRORS These functions are always successful, and no return value is reserved to indicate an error. .Sh CODE REFERENCES .Pa sys/kern/kern_tc.c .Sh SEE ALSO .Xr clock_settime 2 , .Xr settimeofday 2 , .Xr timeradd 3 , .Xr hardclock 9 , .Xr hz 9 , .Xr inittodr 9 , .Xr tc_init 9