Merge remote-tracking branch 'origin/vendor/BINUTILS227'
[dragonfly.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: head/lib/libc/sys/clock_gettime.2 292777 2015-12-27 15:37:07Z dchagin $
31 .\"
32 .Dd September 26, 2016
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 sys/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
62 can be one of the following values:
63 .Dv CLOCK_REALTIME ,
64 .Dv CLOCK_REALTIME_PRECISE ,
65 and
66 .Dv CLOCK_REALTIME_FAST
67 for time that increments as a wall clock should;
68 .Dv CLOCK_MONOTONIC ,
69 .Dv CLOCK_MONOTONIC_PRECISE ,
70 and
71 .Dv CLOCK_MONOTONIC_FAST
72 which increments in SI seconds;
73 .Dv CLOCK_UPTIME ,
74 .Dv CLOCK_UPTIME_PRECISE ,
75 and
76 .Dv CLOCK_UPTIME_FAST
77 which starts at zero when the kernel boots and increments
78 monotonically in SI seconds while the machine is running;
79 .Dv CLOCK_VIRTUAL
80 for time that increments only when
81 the CPU is running in user mode on behalf of the calling process;
82 .Dv CLOCK_PROF
83 for time that increments when the CPU is running in user or
84 kernel mode;
85 .Dv CLOCK_SECOND
86 which returns the current second without performing a full time counter
87 query, using in-kernel cached value of current second;
88 .Dv CLOCK_PROCESS_CPUTIME_ID
89 which returns the CPU-time clock of the calling process; or
90 .Dv CLOCK_THREAD_CPUTIME_ID
91 which returns the CPU-time clock of the calling thread.
92 .Pp
93 The clock IDs
94 .Dv CLOCK_REALTIME_FAST ,
95 .Dv CLOCK_MONOTONIC_FAST ,
96 and
97 .Dv CLOCK_UPTIME_FAST
98 are analogs of corresponding IDs without _FAST suffix but do not perform
99 a full time counter query, so their accuracy is one timer tick.
100 Similarly,
101 .Dv CLOCK_REALTIME_PRECISE ,
102 .Dv CLOCK_MONOTONIC_PRECISE ,
103 and
104 .Dv 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 clock IDs
109 .Dv CLOCK_UPTIME_FAST ,
110 .Dv CLOCK_MONOTONIC_FAST ,
111 .Dv CLOCK_REALTIME_FAST ,
112 and
113 .Dv CLOCK_SECOND
114 make use of
115 .Xr kpmap 4
116 and do not incur any system call overhead after a certain amount of calls.
117 .Pp
118 The structure pointed to by
119 .Fa tp
120 is defined in
121 .In sys/_timespec.h
122 as:
123 .Bd -literal
124 struct timespec {
125         time_t  tv_sec;         /* seconds */
126         long    tv_nsec;        /* and nanoseconds */
127 };
128 .Ed
129 .Pp
130 Only the super-user may set the time of day, using only
131 .Dv CLOCK_REALTIME .
132 If the system securelevel is greater than 1 (see
133 .Xr init 8 ) ,
134 the time may only be advanced.
135 This limitation is imposed to prevent a malicious super-user
136 from setting arbitrary time stamps on files.
137 The system time can still be adjusted backwards using the
138 .Xr adjtime 2
139 system call even when the system is secure.
140 .Pp
141 The resolution (granularity) of a clock is returned by the
142 .Fn clock_getres
143 system call.
144 This value is placed in a (non-NULL)
145 .Fa *tp .
146 .Sh RETURN VALUES
147 .Rv -std
148 .Sh ERRORS
149 The following error codes may be set in
150 .Va errno :
151 .Bl -tag -width Er
152 .It Bq Er EINVAL
153 The
154 .Fa clock_id
155 or
156 .Fa tp
157 argument was not a valid value.
158 .It Bq Er EPERM
159 A user other than the super-user attempted to set the time.
160 .El
161 .Sh SEE ALSO
162 .Xr date 1 ,
163 .Xr adjtime 2 ,
164 .Xr ctime 3 ,
165 .Xr timed 8
166 .Sh STANDARDS
167 The
168 .Fn clock_gettime ,
169 .Fn clock_settime ,
170 and
171 .Fn clock_getres
172 system calls conform to
173 .St -p1003.1b-93 .
174 The clock IDs
175 .Dv CLOCK_REALTIME_FAST ,
176 .Dv CLOCK_REALTIME_PRECISE ,
177 .Dv CLOCK_MONOTONIC_FAST ,
178 .Dv CLOCK_MONOTONIC_PRECISE ,
179 .Dv CLOCK_UPTIME ,
180 .Dv CLOCK_UPTIME_FAST ,
181 .Dv CLOCK_UPTIME_PRECISE ,
182 and
183 .Dv CLOCK_SECOND
184 are
185 .Fx
186 extensions to the POSIX interface.