]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/clock_gettime.2
MFV: less v632.
[FreeBSD/FreeBSD.git] / lib / libc / sys / clock_gettime.2
1 .\"     $OpenBSD: clock_gettime.2,v 1.4 1997/05/08 20:21:16 kstailey Exp $
2 .\"
3 .\" Copyright (c) 1980, 1991, 1993
4 .\"     The Regents of the University of California.  All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\"    may be used to endorse or promote products derived from this software
16 .\"    without specific prior written permission.
17 .\"
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" SUCH DAMAGE.
29 .\"
30 .\" $FreeBSD$
31 .\"
32 .Dd July 8, 2022
33 .Dt CLOCK_GETTIME 2
34 .Os
35 .Sh NAME
36 .Nm clock_gettime ,
37 .Nm clock_settime ,
38 .Nm clock_getres
39 .Nd get/set/calibrate date and time
40 .Sh LIBRARY
41 .Lb libc
42 .Sh SYNOPSIS
43 .In time.h
44 .Ft int
45 .Fn clock_gettime "clockid_t clock_id" "struct timespec *tp"
46 .Ft int
47 .Fn clock_settime "clockid_t clock_id" "const struct timespec *tp"
48 .Ft int
49 .Fn clock_getres "clockid_t clock_id" "struct timespec *tp"
50 .Sh DESCRIPTION
51 The
52 .Fn clock_gettime
53 and
54 .Fn clock_settime
55 system calls allow the calling process to retrieve or set the value
56 used by a clock which is specified by
57 .Fa clock_id .
58 .Pp
59 The
60 .Fa clock_id
61 argument can be a value obtained from
62 .Xr clock_getcpuclockid 3
63 or
64 .Xr pthread_getcpuclockid 3
65 as well as the following values:
66 .Pp
67 .Bl -tag -width indent -compact
68 .It Dv CLOCK_REALTIME
69 .It Dv CLOCK_REALTIME_PRECISE
70 .It Dv CLOCK_REALTIME_FAST
71 .It Dv CLOCK_REALTIME_COARSE
72 Increments as a wall clock should.
73 .It Dv CLOCK_MONOTONIC
74 .It Dv CLOCK_MONOTONIC_PRECISE
75 .It Dv CLOCK_MONOTONIC_FAST
76 .It Dv CLOCK_MONOTONIC_COARSE
77 Increments in SI seconds.
78 .It Dv CLOCK_UPTIME
79 .It Dv CLOCK_UPTIME_PRECISE
80 .It Dv CLOCK_UPTIME_FAST
81 .It Dv CLOCK_BOOTTIME
82 Starts at zero when the kernel boots and increments
83 monotonically in SI seconds while the machine is running.
84 .It Dv CLOCK_VIRTUAL
85 Increments only when
86 the CPU is running in user mode on behalf of the calling process.
87 .It Dv CLOCK_PROF
88 Increments when the CPU is running in user or kernel mode.
89 .It Dv CLOCK_SECOND
90 Returns the current second without performing a full time counter
91 query, using an in-kernel cached value of the current second.
92 .It Dv CLOCK_PROCESS_CPUTIME_ID
93 Returns the execution time of the calling process.
94 .It Dv CLOCK_THREAD_CPUTIME_ID
95 Returns the execution time of the calling thread.
96 .El
97 .Pp
98 The clock IDs
99 .Fa CLOCK_REALTIME ,
100 .Fa CLOCK_MONOTONIC ,
101 and
102 .Fa CLOCK_UPTIME
103 perform a full time counter query.
104 The clock IDs with the _FAST suffix, i.e.,
105 .Fa CLOCK_REALTIME_FAST ,
106 .Fa CLOCK_MONOTONIC_FAST ,
107 and
108 .Fa CLOCK_UPTIME_FAST ,
109 do not perform
110 a full time counter query, so their accuracy is one timer tick.
111 Similarly,
112 .Fa CLOCK_REALTIME_PRECISE ,
113 .Fa CLOCK_MONOTONIC_PRECISE ,
114 and
115 .Fa CLOCK_UPTIME_PRECISE
116 are used to get the most exact value as possible, at the expense of
117 execution time.
118 The clock IDs
119 .Fa CLOCK_REALTIME_COARSE
120 and
121 .Fa CLOCK_MONOTONIC_COARSE
122 are aliases of corresponding IDs with _FAST suffix for compatibility with other
123 systems.
124 Finally,
125 .Dv CLOCK_BOOTTIME
126 is an alias for
127 .Dv CLOCK_UPTIME
128 for compatibility with other systems.
129 .Pp
130 The structure pointed to by
131 .Fa tp
132 is defined in
133 .In sys/timespec.h
134 as:
135 .Bd -literal
136 struct timespec {
137         time_t  tv_sec;         /* seconds */
138         long    tv_nsec;        /* and nanoseconds */
139 };
140 .Ed
141 .Pp
142 Only the super-user may set the time of day, using only
143 .Fa CLOCK_REALTIME .
144 If the system
145 .Xr securelevel 7
146 is greater than 1 (see
147 .Xr init 8 ) ,
148 the time may only be advanced.
149 This limitation is imposed to prevent a malicious super-user
150 from setting arbitrary time stamps on files.
151 The system time can still be adjusted backwards using the
152 .Xr adjtime 2
153 system call even when the system is secure.
154 .Pp
155 The resolution (granularity) of a clock is returned by the
156 .Fn clock_getres
157 system call.
158 This value is placed in a (non-NULL)
159 .Fa *tp .
160 .Sh RETURN VALUES
161 .Rv -std
162 .Sh ERRORS
163 The following error codes may be set in
164 .Va errno :
165 .Bl -tag -width Er
166 .It Bq Er EINVAL
167 The
168 .Fa clock_id
169 or
170 .Fa timespec
171 argument
172 was not a valid value.
173 .It Bq Er EPERM
174 A user other than the super-user attempted to set the time.
175 .El
176 .Sh SEE ALSO
177 .Xr date 1 ,
178 .Xr adjtime 2 ,
179 .Xr clock_getcpuclockid 3 ,
180 .Xr ctime 3 ,
181 .Xr pthread_getcpuclockid 3
182 .Sh STANDARDS
183 The
184 .Fn clock_gettime ,
185 .Fn clock_settime ,
186 and
187 .Fn clock_getres
188 system calls conform to
189 .St -p1003.1b-93 .
190 The clock IDs
191 .Fa CLOCK_REALTIME_FAST ,
192 .Fa CLOCK_REALTIME_PRECISE ,
193 .Fa CLOCK_MONOTONIC_FAST ,
194 .Fa CLOCK_MONOTONIC_PRECISE ,
195 .Fa CLOCK_UPTIME ,
196 .Fa CLOCK_UPTIME_FAST ,
197 .Fa CLOCK_UPTIME_PRECISE ,
198 .Fa CLOCK_SECOND
199 are
200 .Fx
201 extensions to the POSIX interface.
202 .Sh HISTORY
203 The
204 .Fn clock_gettime ,
205 .Fn clock_settime ,
206 and
207 .Fn clock_getres
208 system calls first appeared in
209 .Fx 3.0 .