]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/clock_gettime.2
libc: Fix most issues reported by mandoc
[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 December 7, 2019
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 Increments as a wall clock should.
72 .It Dv CLOCK_MONOTONIC
73 .It Dv CLOCK_MONOTONIC_PRECISE
74 .It Dv CLOCK_MONOTONIC_FAST
75 Increments in SI seconds.
76 .It Dv CLOCK_UPTIME
77 .It Dv CLOCK_UPTIME_PRECISE
78 .It Dv CLOCK_UPTIME_FAST
79 Starts at zero when the kernel boots and increments
80 monotonically in SI seconds while the machine is running.
81 .It Dv CLOCK_VIRTUAL
82 Increments only when
83 the CPU is running in user mode on behalf of the calling process.
84 .It Dv CLOCK_PROF
85 Increments when the CPU is running in user or kernel mode.
86 .It Dv CLOCK_SECOND
87 Returns the current second without performing a full time counter
88 query, using an in-kernel cached value of the current second.
89 .It Dv CLOCK_PROCESS_CPUTIME_ID
90 Returns the execution time of the calling process.
91 .It Dv CLOCK_THREAD_CPUTIME_ID
92 Returns the execution time of the calling thread.
93 .El
94 .Pp
95 The clock IDs
96 .Fa CLOCK_REALTIME_FAST ,
97 .Fa CLOCK_MONOTONIC_FAST ,
98 .Fa CLOCK_UPTIME_FAST
99 are analogs of corresponding IDs without _FAST suffix but do not perform
100 a full time counter query, so their accuracy is one timer tick.
101 Similarly,
102 .Fa CLOCK_REALTIME_PRECISE ,
103 .Fa CLOCK_MONOTONIC_PRECISE ,
104 .Fa CLOCK_UPTIME_PRECISE
105 are used to get the most exact value as possible, at the expense of
106 execution time.
107 .Pp
108 The structure pointed to by
109 .Fa tp
110 is defined in
111 .In sys/timespec.h
112 as:
113 .Bd -literal
114 struct timespec {
115         time_t  tv_sec;         /* seconds */
116         long    tv_nsec;        /* and nanoseconds */
117 };
118 .Ed
119 .Pp
120 Only the super-user may set the time of day, using only
121 .Fa CLOCK_REALTIME .
122 If the system securelevel is greater than 1 (see
123 .Xr init 8 ) ,
124 the time may only be advanced.
125 This limitation is imposed to prevent a malicious super-user
126 from setting arbitrary time stamps on files.
127 The system time can still be adjusted backwards using the
128 .Xr adjtime 2
129 system call even when the system is secure.
130 .Pp
131 The resolution (granularity) of a clock is returned by the
132 .Fn clock_getres
133 system call.
134 This value is placed in a (non-NULL)
135 .Fa *tp .
136 .Sh RETURN VALUES
137 .Rv -std
138 .Sh ERRORS
139 The following error codes may be set in
140 .Va errno :
141 .Bl -tag -width Er
142 .It Bq Er EINVAL
143 The
144 .Fa clock_id
145 or
146 .Fa timespec
147 argument
148 was not a valid value.
149 .It Bq Er EPERM
150 A user other than the super-user attempted to set the time.
151 .El
152 .Sh SEE ALSO
153 .Xr date 1 ,
154 .Xr adjtime 2 ,
155 .Xr clock_getcpuclockid 3 ,
156 .Xr ctime 3 ,
157 .Xr pthread_getcpuclockid 3 ,
158 .Xr timed 8
159 .Sh STANDARDS
160 The
161 .Fn clock_gettime ,
162 .Fn clock_settime ,
163 and
164 .Fn clock_getres
165 system calls conform to
166 .St -p1003.1b-93 .
167 The clock IDs
168 .Fa CLOCK_REALTIME_FAST ,
169 .Fa CLOCK_REALTIME_PRECISE ,
170 .Fa CLOCK_MONOTONIC_FAST ,
171 .Fa CLOCK_MONOTONIC_PRECISE ,
172 .Fa CLOCK_UPTIME ,
173 .Fa CLOCK_UPTIME_FAST ,
174 .Fa CLOCK_UPTIME_PRECISE ,
175 .Fa CLOCK_SECOND
176 are FreeBSD extensions to the POSIX interface.
177 .Sh HISTORY
178 The
179 .Fn clock_gettime ,
180 .Fn clock_settime ,
181 and
182 .Fn clock_getres
183 system calls first appeared in
184 .Fx 3.0 .