xref: /openbsd-src/share/man/man9/microtime.9 (revision 744ba8024bacc06e9eb179030527f8bb55d04da8)
1*744ba802Scheloha.\"	$OpenBSD: microtime.9,v 1.23 2022/12/28 15:46:39 cheloha Exp $
28545c142Sespie.\"     $NetBSD: microtime.9,v 1.2 1999/03/16 00:40:47 garbled Exp $
38545c142Sespie.\"
48545c142Sespie.\" Copyright (c) 1998 The NetBSD Foundation, Inc.
58545c142Sespie.\" All rights reserved.
68545c142Sespie.\"
78545c142Sespie.\" This code is derived from software contributed to The NetBSD Foundation
88545c142Sespie.\" by Jeremy Cooper.
98545c142Sespie.\"
108545c142Sespie.\" Redistribution and use in source and binary forms, with or without
118545c142Sespie.\" modification, are permitted provided that the following conditions
128545c142Sespie.\" are met:
138545c142Sespie.\" 1. Redistributions of source code must retain the above copyright
148545c142Sespie.\"    notice, this list of conditions and the following disclaimer.
158545c142Sespie.\" 2. Redistributions in binary form must reproduce the above copyright
168545c142Sespie.\"    notice, this list of conditions and the following disclaimer in the
178545c142Sespie.\"    documentation and/or other materials provided with the distribution.
188545c142Sespie.\"
198545c142Sespie.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
208545c142Sespie.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
218545c142Sespie.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
228545c142Sespie.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
238545c142Sespie.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
248545c142Sespie.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
258545c142Sespie.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
268545c142Sespie.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
278545c142Sespie.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
288545c142Sespie.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
298545c142Sespie.\" POSSIBILITY OF SUCH DAMAGE.
308545c142Sespie.\"
31*744ba802Scheloha.Dd $Mdocdate: December 28 2022 $
328545c142Sespie.Dt MICROTIME 9
338545c142Sespie.Os
348545c142Sespie.Sh NAME
35a3266d92Sart.Nm microuptime ,
36a3266d92Sart.Nm getmicrouptime ,
37a3266d92Sart.Nm nanouptime ,
385ad1aef5Sjmc.Nm getnanouptime ,
39*744ba802Scheloha.Nm nsecuptime ,
40*744ba802Scheloha.Nm getnsecuptime ,
41*744ba802Scheloha.Nm getuptime ,
4287ba7848Scheloha.Nm nanoruntime ,
43*744ba802Scheloha.Nm getnsecruntime ,
44*744ba802Scheloha.Nm microtime ,
45*744ba802Scheloha.Nm getmicrotime ,
46*744ba802Scheloha.Nm nanotime ,
47*744ba802Scheloha.Nm getnanotime ,
488dca5d44Scheloha.Nm gettime ,
49*744ba802Scheloha.Nm microboottime ,
50*744ba802Scheloha.Nm nanoboottime
51*744ba802Scheloha.Nd get the time
528545c142Sespie.Sh SYNOPSIS
53dddd2645Sschwarze.In sys/time.h
548545c142Sespie.Ft void
558af959d5Sschwarze.Fo microuptime
56a3266d92Sart.Fa "struct timeval *tv"
57a3266d92Sart.Fc
58a3266d92Sart.Ft void
598af959d5Sschwarze.Fo getmicrouptime
60a3266d92Sart.Fa "struct timeval *tv"
61a3266d92Sart.Fc
62a3266d92Sart.Ft void
638af959d5Sschwarze.Fo nanouptime
64*744ba802Scheloha.Fa "struct timespec *ts"
65a3266d92Sart.Fc
66a3266d92Sart.Ft void
678af959d5Sschwarze.Fo getnanouptime
68*744ba802Scheloha.Fa "struct timespec *ts"
69a3266d92Sart.Fc
70*744ba802Scheloha.Ft uint64_t
71*744ba802Scheloha.Fo nsecuptime
72*744ba802Scheloha.Fa "void"
7387ba7848Scheloha.Fc
74*744ba802Scheloha.Ft uint64_t
75*744ba802Scheloha.Fo getnsecuptime
768dca5d44Scheloha.Fa "void"
778dca5d44Scheloha.Fc
788dca5d44Scheloha.Ft time_t
798dca5d44Scheloha.Fo getuptime
808dca5d44Scheloha.Fa "void"
818dca5d44Scheloha.Fc
82*744ba802Scheloha.Ft void
83*744ba802Scheloha.Fo nanoruntime
84*744ba802Scheloha.Fa "struct timespec *ts"
85*744ba802Scheloha.Fc
86*744ba802Scheloha.Ft uint64_t
87*744ba802Scheloha.Fo getnsecruntime
88*744ba802Scheloha.Fa "void"
89*744ba802Scheloha.Fc
90*744ba802Scheloha.Ft void
91*744ba802Scheloha.Fo microtime
92*744ba802Scheloha.Fa "struct timeval *tv"
93*744ba802Scheloha.Fc
94*744ba802Scheloha.Ft void
95*744ba802Scheloha.Fo getmicrotime
96*744ba802Scheloha.Fa "struct timeval *tv"
97*744ba802Scheloha.Fc
98*744ba802Scheloha.Ft void
99*744ba802Scheloha.Fo nanotime
100*744ba802Scheloha.Fa "struct timespec *ts"
101*744ba802Scheloha.Fc
102*744ba802Scheloha.Ft void
103*744ba802Scheloha.Fo getnanotime
104*744ba802Scheloha.Fa "struct timespec *ts"
105*744ba802Scheloha.Fc
106*744ba802Scheloha.Ft time_t
107*744ba802Scheloha.Fo gettime
108*744ba802Scheloha.Fa "void"
109*744ba802Scheloha.Fc
110*744ba802Scheloha.Ft void
111*744ba802Scheloha.Fo microboottime
112*744ba802Scheloha.Fa "struct timeval *tv"
113*744ba802Scheloha.Fc
114*744ba802Scheloha.Ft void
115*744ba802Scheloha.Fo nanoboottime
116*744ba802Scheloha.Fa "struct timespec *ts"
117*744ba802Scheloha.Fc
1188545c142Sespie.Sh DESCRIPTION
119*744ba802SchelohaThe kernel has three clocks and a variety of interfaces for reading them.
120*744ba802Scheloha.Pp
121*744ba802SchelohaThe
122*744ba802Scheloha.Sy uptime
123*744ba802Schelohaclock measures the time elapsed since the system booted.
124*744ba802SchelohaIt begins at zero and advances monotonically.
125*744ba802SchelohaThe uptime clock may be read with the following functions:
126*744ba802Scheloha.Bl -column "getmicrouptimeX" "Output Format" "Source" -offset indent
127*744ba802Scheloha.It Em Name            Ta Em Output Format    Ta Em Source
128*744ba802Scheloha.It Fn microuptime     Ta Vt struct timeval   Ta hardware
129*744ba802Scheloha.It Fn getmicrouptime  Ta Vt struct timeval   Ta timestamp
130*744ba802Scheloha.It Fn nanouptime      Ta Vt struct timespec  Ta hardware
131*744ba802Scheloha.It Fn getnanouptime   Ta Vt struct timespec  Ta timestamp
132*744ba802Scheloha.It Fn nsecuptime      Ta Ft uint64_t         Ta hardware
133*744ba802Scheloha.It Fn getnsecuptime   Ta Ft uint64_t         Ta timestamp
134*744ba802Scheloha.It Fn getuptime       Ta Ft time_t           Ta timestamp
135a3266d92Sart.El
136a3266d92Sart.Pp
137*744ba802SchelohaThe
138*744ba802Scheloha.Sy runtime
139*744ba802Schelohaclock measures the time elapsed since the system booted,
140*744ba802Schelohaless any time the system is suspended or hibernating.
141*744ba802SchelohaIt begins at zero and normally advances monotonically,
142*744ba802Schelohabut pauses while the system is suspended or hibernating.
143*744ba802SchelohaThe runtime clock may be read with the following functions:
144*744ba802Scheloha.Bl -column "getnsecruntimeX" "Output Format" "Source" -offset indent
145*744ba802Scheloha.It Em Name            Ta Em Output Format    Ta Em Source
146*744ba802Scheloha.It Fn nanoruntime     Ta Vt struct timespec  Ta hardware
147*744ba802Scheloha.It Fn getnsecruntime  Ta Ft uint64_t         Ta timestamp
148*744ba802Scheloha.El
149*744ba802Scheloha.Pp
150*744ba802SchelohaThe
151*744ba802Scheloha.Sy UTC
152*744ba802Schelohaclock measures the time elapsed since Jan 1 1970 00:00:00
153*744ba802Scheloha.Pq the Unix Epoch .
154*744ba802SchelohaThe clock normally advances monotonically,
155*744ba802Schelohabut jumps when a process calls
156*744ba802Scheloha.Xr clock_settime 2
157*744ba802Schelohaor
158*744ba802Scheloha.Xr settimeofday 2 .
159*744ba802SchelohaThe UTC clock may be read with the following functions:
160*744ba802Scheloha.Bl -column "getmicrotimeX" "Output Format" "Source" -offset indent
161*744ba802Scheloha.It Em Name            Ta Em Output Format    Ta Em Source
162*744ba802Scheloha.It Fn microtime       Ta Vt struct timeval   Ta hardware
163*744ba802Scheloha.It Fn getmicrotime    Ta Vt struct timeval   Ta timestamp
164*744ba802Scheloha.It Fn nanotime        Ta Vt struct timespec  Ta hardware
165*744ba802Scheloha.It Fn getnanotime     Ta Vt struct timespec  Ta timestamp
166*744ba802Scheloha.It Fn gettime         Ta Ft time_t           Ta timestamp
167*744ba802Scheloha.El
168*744ba802Scheloha.Pp
169*744ba802SchelohaThe kernel also maintains a
170*744ba802Scheloha.Sy boot timestamp .
171*744ba802SchelohaIt is the moment on the UTC clock when the system booted.
172*744ba802SchelohaThe timestamp jumps when a process calls
173*744ba802Scheloha.Xr clock_settime 2
174*744ba802Schelohaor
175*744ba802Scheloha.Xr settimeofday 2 .
176*744ba802SchelohaThe boot timestamp may be read with the following functions:
177*744ba802Scheloha.Bl -column "microboottimeX" "Output Format" "Source" -offset indent
178*744ba802Scheloha.It Em Name            Ta Em Output Format    Ta Em Source
179*744ba802Scheloha.It Fn microboottime   Ta Vt struct timeval   Ta timestamp
180*744ba802Scheloha.It Fn nanoboottime    Ta Vt struct timespec  Ta timestamp
181*744ba802Scheloha.El
182*744ba802Scheloha.Pp
183*744ba802SchelohaFunctions that source from the
184*744ba802Scheloha.Em hardware
185*744ba802Schelohaprovide the most precise result possible.
186*744ba802SchelohaFunctions that source from a
187*744ba802Scheloha.Em timestamp
188*744ba802Schelohaprovide a far less precise result,
189*744ba802Schelohabut do so very quickly.
190*744ba802SchelohaOn most platforms,
191*744ba802Schelohatimestamps are updated approximately 100 times per second.
192*744ba802Scheloha.Sh CONTEXT
193*744ba802SchelohaThese functions may be called during autoconf,
194*744ba802Schelohafrom process context,
195*744ba802Schelohaor from any interrupt context.
196*744ba802Scheloha.Sh RETURN VALUES
197*744ba802Scheloha.Fn nsecuptime ,
198*744ba802Scheloha.Fn getnsecuptime ,
199*744ba802Schelohaand
200*744ba802Scheloha.Fn getnsecruntime
201*744ba802Schelohareturn a count of nanoseconds.
202*744ba802Scheloha.Pp
203*744ba802Scheloha.Fn getuptime
204*744ba802Schelohaand
205*744ba802Scheloha.Fn gettime
206*744ba802Schelohareturn a count of seconds.
207*744ba802Scheloha.Sh ERRORS
208*744ba802SchelohaThese functions are always successful,
209*744ba802Schelohaand no return value is reserved to indicate an error.
2104f82885cSjasper.Sh CODE REFERENCES
211*744ba802Scheloha.Pa sys/kern/kern_tc.c
2128545c142Sespie.Sh SEE ALSO
213*744ba802Scheloha.Xr clock_settime 2 ,
214c4f2acd3Sderaadt.Xr settimeofday 2 ,
215*744ba802Scheloha.Xr timeradd 3 ,
2168545c142Sespie.Xr hardclock 9 ,
2178a7802e4Sjmc.Xr hz 9 ,
218*744ba802Scheloha.Xr inittodr 9 ,
219*744ba802Scheloha.Xr tc_init 9
220