]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (c) 2003 Nick Clifford (zaf@nrc.co.nz), Jan 25, 2003 |
2 | .\" Copyright (c) 2003 Andries Brouwer (aeb@cwi.nl), Aug 24, 2003 | |
3 | .\" | |
93015253 | 4 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
5 | .\" Permission is granted to make and distribute verbatim copies of this |
6 | .\" manual provided the copyright notice and this permission notice are | |
7 | .\" preserved on all copies. | |
8 | .\" | |
9 | .\" Permission is granted to copy and distribute modified versions of this | |
10 | .\" manual under the conditions for verbatim copying, provided that the | |
11 | .\" entire resulting derived work is distributed under the terms of a | |
12 | .\" permission notice identical to this one. | |
c13182ef | 13 | .\" |
fea681da MK |
14 | .\" Since the Linux kernel and libraries are constantly changing, this |
15 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
16 | .\" responsibility for errors or omissions, or for damages resulting from | |
17 | .\" the use of the information contained herein. The author(s) may not | |
18 | .\" have taken the same level of care in the production of this manual, | |
19 | .\" which is licensed free of charge, as they might when working | |
20 | .\" professionally. | |
c13182ef | 21 | .\" |
fea681da MK |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 24 | .\" %%%LICENSE_END |
fea681da MK |
25 | .\" |
26 | .\" 2003-08-23 Martin Schulze <joey@infodrom.org> improvements | |
27 | .\" 2003-08-24 aeb, large parts rewritten | |
28 | .\" 2004-08-06 Christoph Lameter <clameter@sgi.com>, SMP note | |
29 | .\" | |
9ba01802 | 30 | .TH CLOCK_GETRES 2 2019-03-06 "" "Linux Programmer's Manual" |
fea681da MK |
31 | .SH NAME |
32 | clock_getres, clock_gettime, clock_settime \- clock and time functions | |
33 | .SH SYNOPSIS | |
34 | .B #include <time.h> | |
68e4db0a | 35 | .PP |
fea681da | 36 | .BI "int clock_getres(clockid_t " clk_id ", struct timespec *" res ); |
dbfe9c70 | 37 | .PP |
fea681da | 38 | .BI "int clock_gettime(clockid_t " clk_id ", struct timespec *" tp ); |
dbfe9c70 | 39 | .PP |
fea681da | 40 | .BI "int clock_settime(clockid_t " clk_id ", const struct timespec *" tp ); |
68e4db0a | 41 | .PP |
02959991 | 42 | Link with \fI\-lrt\fP (only for glibc versions before 2.17). |
68e4db0a | 43 | .PP |
cc4615cc MK |
44 | .in -4n |
45 | Feature Test Macro Requirements for glibc (see | |
46 | .BR feature_test_macros (7)): | |
47 | .in | |
68e4db0a | 48 | .PP |
cc4615cc MK |
49 | .ad l |
50 | .BR clock_getres (), | |
51 | .BR clock_gettime (), | |
52 | .BR clock_settime (): | |
8260c01f | 53 | .RS |
cc4615cc | 54 | _POSIX_C_SOURCE\ >=\ 199309L |
8260c01f | 55 | .RE |
cc4615cc | 56 | .ad b |
fea681da MK |
57 | .SH DESCRIPTION |
58 | The function | |
59 | .BR clock_getres () | |
60 | finds the resolution (precision) of the specified clock | |
61 | .IR clk_id , | |
62 | and, if | |
63 | .I res | |
0c2ec4f1 | 64 | is non-NULL, stores it in the \fIstruct timespec\fP pointed to by |
fea681da MK |
65 | .IR res . |
66 | The resolution of clocks depends on the implementation and cannot be | |
67 | configured by a particular process. | |
68 | If the time value pointed to by the argument | |
69 | .I tp | |
70 | of | |
71 | .BR clock_settime () | |
72 | is not a multiple of | |
73 | .IR res , | |
74 | then it is truncated to a multiple of | |
75 | .IR res . | |
76 | .PP | |
77 | The functions | |
78 | .BR clock_gettime () | |
79 | and | |
80 | .BR clock_settime () | |
81 | retrieve and set the time of the specified clock | |
82 | .IR clk_id . | |
83 | .PP | |
84 | The | |
85 | .I res | |
86 | and | |
87 | .I tp | |
88 | arguments are | |
f19a0f03 | 89 | .I timespec |
4e836144 | 90 | structures, as specified in |
c13182ef | 91 | .IR <time.h> : |
0ffeaeae | 92 | .PP |
3ad4ddcd | 93 | .in +4n |
0ffeaeae | 94 | .EX |
fea681da | 95 | struct timespec { |
cc4615cc MK |
96 | time_t tv_sec; /* seconds */ |
97 | long tv_nsec; /* nanoseconds */ | |
fea681da | 98 | }; |
0ffeaeae | 99 | .EE |
3ad4ddcd | 100 | .in |
fea681da | 101 | .PP |
c13182ef | 102 | The |
fea681da MK |
103 | .I clk_id |
104 | argument is the identifier of the particular clock on which to act. | |
105 | A clock may be system-wide and hence visible for all processes, or | |
106 | per-process if it measures time only within a single process. | |
dd3568a1 | 107 | .PP |
6f36deb4 | 108 | All implementations support the system-wide real-time clock, |
fea681da MK |
109 | which is identified by |
110 | .BR CLOCK_REALTIME . | |
111 | Its time represents seconds and nanoseconds since the Epoch. | |
112 | When its time is changed, timers for a relative interval are | |
113 | unaffected, but timers for an absolute point in time are affected. | |
dd3568a1 | 114 | .PP |
c13182ef MK |
115 | More clocks may be implemented. |
116 | The interpretation of the | |
fea681da | 117 | corresponding time values and the effect on timers is unspecified. |
dd3568a1 | 118 | .PP |
5260fe08 | 119 | Sufficiently recent versions of glibc and the Linux kernel |
fea681da MK |
120 | support the following clocks: |
121 | .TP | |
122 | .B CLOCK_REALTIME | |
22594a3c | 123 | System-wide clock that measures real (i.e., wall-clock) time. |
fea681da | 124 | Setting this clock requires appropriate privileges. |
22594a3c MK |
125 | This clock is affected by discontinuous jumps in the system time |
126 | (e.g., if the system administrator manually changes the clock), | |
127 | and by the incremental adjustments performed by | |
65a0c012 | 128 | .BR adjtime (3) |
22594a3c | 129 | and NTP. |
f5546fcd | 130 | .TP |
55f749ae CH |
131 | .BR CLOCK_REALTIME_COARSE " (since Linux 2.6.32; Linux-specific)" |
132 | .\" Added in commit da15cfdae03351c689736f8d142618592e3cebc3 | |
1d33f7d7 MK |
133 | A faster but less precise version of |
134 | .BR CLOCK_REALTIME . | |
55f749ae | 135 | Use when you need very fast, but not fine-grained timestamps. |
c74bc519 MK |
136 | Requires per-architecture support, |
137 | and probably also architecture support for this flag in the | |
138 | .BR vdso (7). | |
55f749ae | 139 | .TP |
fea681da | 140 | .B CLOCK_MONOTONIC |
7d882a4a MK |
141 | Clock that cannot be set and represents monotonic time since\(emas described |
142 | by POSIX\(em"some unspecified point in the past". | |
143 | On Linux, that point corresponds to the number of seconds that the system | |
144 | has been running since it was booted. | |
145 | .IP | |
146 | The | |
147 | .B CLOCK_MONOTONIC | |
148 | clock is not affected by discontinuous jumps in the system time | |
63f0f3d9 MK |
149 | (e.g., if the system administrator manually changes the clock), |
150 | but is affected by the incremental adjustments performed by | |
65a0c012 | 151 | .BR adjtime (3) |
63f0f3d9 | 152 | and NTP. |
f224d2bc | 153 | This clock does not count time that the system is suspended. |
f5546fcd | 154 | .TP |
55f749ae CH |
155 | .BR CLOCK_MONOTONIC_COARSE " (since Linux 2.6.32; Linux-specific)" |
156 | .\" Added in commit da15cfdae03351c689736f8d142618592e3cebc3 | |
1d33f7d7 MK |
157 | A faster but less precise version of |
158 | .BR CLOCK_MONOTONIC . | |
55f749ae | 159 | Use when you need very fast, but not fine-grained timestamps. |
c74bc519 MK |
160 | Requires per-architecture support, |
161 | and probably also architecture support for this flag in the | |
162 | .BR vdso (7). | |
fea681da | 163 | .TP |
1ba3fba8 MK |
164 | .BR CLOCK_MONOTONIC_RAW " (since Linux 2.6.28; Linux-specific)" |
165 | .\" Added in commit 2d42244ae71d6c7b0884b5664cf2eda30fb2ae68, John Stultz | |
166 | Similar to | |
167 | .BR CLOCK_MONOTONIC , | |
168 | but provides access to a raw hardware-based time | |
63f0f3d9 MK |
169 | that is not subject to NTP adjustments or |
170 | the incremental adjustments performed by | |
65a0c012 | 171 | .BR adjtime (3). |
4eae8eb7 | 172 | This clock does not count time that the system is suspended. |
1ba3fba8 | 173 | .TP |
b28712e1 | 174 | .BR CLOCK_BOOTTIME " (since Linux 2.6.39; Linux-specific)" |
a5d9ce65 MK |
175 | .\" commit 7fdd7f89006dd5a4c702fa0ce0c272345fa44ae0 |
176 | .\" commit 70a08cca1227dc31c784ec930099a4417a06e7d0 | |
b28712e1 CH |
177 | Identical to |
178 | .BR CLOCK_MONOTONIC , | |
a5d9ce65 MK |
179 | except it also includes any time that the system is suspended. |
180 | This allows applications to get a suspend-aware monotonic clock | |
181 | without having to deal with the complications of | |
182 | .BR CLOCK_REALTIME , | |
183 | which may have discontinuities if the time is changed using | |
0bbe872f MK |
184 | .BR settimeofday (2) |
185 | or similar. | |
b28712e1 | 186 | .TP |
783d6cb4 | 187 | .BR CLOCK_PROCESS_CPUTIME_ID " (since Linux 2.6.12)" |
2fd2c35c MK |
188 | Per-process CPU-time clock |
189 | (measures CPU time consumed by all threads in the process). | |
fea681da | 190 | .TP |
783d6cb4 | 191 | .BR CLOCK_THREAD_CPUTIME_ID " (since Linux 2.6.12)" |
fea681da | 192 | Thread-specific CPU-time clock. |
47297adb | 193 | .SH RETURN VALUE |
e1d6264d | 194 | .BR clock_gettime (), |
cc83f4d1 | 195 | .BR clock_settime (), |
fea681da MK |
196 | and |
197 | .BR clock_getres () | |
198 | return 0 for success, or \-1 for failure (in which case | |
199 | .I errno | |
200 | is set appropriately). | |
201 | .SH ERRORS | |
202 | .TP | |
203 | .B EFAULT | |
204 | .I tp | |
205 | points outside the accessible address space. | |
206 | .TP | |
207 | .B EINVAL | |
c13182ef | 208 | The |
fea681da MK |
209 | .I clk_id |
210 | specified is not supported on this system. | |
a113440c MK |
211 | .\" Linux also gives this error on attempts to set CLOCK_PROCESS_CPUTIME_ID |
212 | .\" and CLOCK_THREAD_CPUTIME_ID, when probably the proper error should be | |
213 | .\" EPERM. | |
fea681da | 214 | .TP |
9f23d947 MK |
215 | .B EINVAL |
216 | .RB ( clock_settime ()): | |
217 | .I tp.tv_sec | |
218 | is negative or | |
9a82d4d9 | 219 | .I tp.tv_nsec |
9f23d947 MK |
220 | is outside the range [0..999,999,999]. |
221 | .TP | |
bccacb94 MK |
222 | .BR EINVAL " (since Linux 4.3)" |
223 | .\" commit e1d7ba8735551ed79c7a0463a042353574b96da3 | |
224 | A call to | |
225 | .BR clock_settime () | |
226 | with a | |
227 | .I clk_id | |
228 | of | |
229 | .B CLOCK_REALTIME | |
230 | attempted to set the time to a value less than | |
231 | the current value of the | |
232 | .B CLOCK_MONOTONIC | |
233 | clock. | |
234 | .TP | |
fea681da MK |
235 | .B EPERM |
236 | .BR clock_settime () | |
237 | does not have permission to set the clock indicated. | |
e272bb60 | 238 | .SH VERSIONS |
50b25c38 | 239 | These system calls first appeared in Linux 2.6. |
546e17ce ZL |
240 | .SH ATTRIBUTES |
241 | For an explanation of the terms used in this section, see | |
242 | .BR attributes (7). | |
243 | .TS | |
244 | allbox; | |
6a95087e | 245 | lbw32 lb lb |
546e17ce ZL |
246 | l l l. |
247 | Interface Attribute Value | |
248 | T{ | |
249 | .BR clock_getres (), | |
250 | .BR clock_gettime (), | |
251 | .BR clock_settime () | |
252 | T} Thread safety MT-Safe | |
253 | .TE | |
efeece04 | 254 | .sp 1 |
47297adb | 255 | .SH CONFORMING TO |
75ee6a07 | 256 | POSIX.1-2001, POSIX.1-2008, SUSv2. |
2b2581ee MK |
257 | .SH AVAILABILITY |
258 | On POSIX systems on which these functions are available, the symbol | |
259 | .B _POSIX_TIMERS | |
c84371c6 | 260 | is defined in \fI<unistd.h>\fP to a value greater than 0. |
2b2581ee MK |
261 | The symbols |
262 | .BR _POSIX_MONOTONIC_CLOCK , | |
263 | .BR _POSIX_CPUTIME , | |
264 | .B _POSIX_THREAD_CPUTIME | |
265 | indicate that | |
266 | .BR CLOCK_MONOTONIC , | |
267 | .BR CLOCK_PROCESS_CPUTIME_ID , | |
268 | .B CLOCK_THREAD_CPUTIME_ID | |
269 | are available. | |
270 | (See also | |
271 | .BR sysconf (3).) | |
19c98696 | 272 | .SH NOTES |
f086b6aa MK |
273 | POSIX.1 specifies the following: |
274 | .RS | |
275 | .PP | |
276 | Setting the value of the | |
277 | .B CLOCK_REALTIME | |
278 | clock via | |
e470433f | 279 | .BR clock_settime () |
f086b6aa MK |
280 | shall have no effect on threads that are blocked waiting for a relative time |
281 | service based upon this clock, including the | |
282 | .BR nanosleep () | |
283 | function; nor on the expiration of relative timers based upon this clock. | |
284 | Consequently, these time services shall expire when the requested relative | |
285 | interval elapses, independently of the new or old value of the clock. | |
a3847715 | 286 | .RE |
f086b6aa | 287 | .\" |
f8c419a4 MK |
288 | .SS C library/kernel differences |
289 | On some architectures, an implementation of | |
290 | .BR clock_gettime () | |
291 | is provided in the | |
292 | .BR vdso (7). | |
293 | .\" | |
78638aae MK |
294 | .SS Historical note for SMP systems |
295 | Before Linux added kernel support for | |
fea681da MK |
296 | .B CLOCK_PROCESS_CPUTIME_ID |
297 | and | |
78638aae MK |
298 | .BR CLOCK_THREAD_CPUTIME_ID , |
299 | glibc implemented these clocks on many platforms using timer | |
300 | registers from the CPUs | |
fea681da MK |
301 | (TSC on i386, AR.ITC on Itanium). |
302 | These registers may differ between CPUs and as a consequence | |
303 | these clocks may return | |
304 | .B bogus results | |
305 | if a process is migrated to another CPU. | |
306 | .PP | |
f14ae16e | 307 | If the CPUs in an SMP system have different clock sources, then |
fea681da | 308 | there is no way to maintain a correlation between the timer registers since |
c13182ef | 309 | each CPU will run at a slightly different frequency. |
f14ae16e | 310 | If that is the case, then |
fea681da MK |
311 | .I clock_getcpuclockid(0) |
312 | will return | |
313 | .B ENOENT | |
c13182ef | 314 | to signify this condition. |
33a0ccb2 | 315 | The two clocks will then be useful only if it |
fea681da MK |
316 | can be ensured that a process stays on a certain CPU. |
317 | .PP | |
318 | The processors in an SMP system do not start all at exactly the same | |
319 | time and therefore the timer registers are typically running at an offset. | |
320 | Some architectures include code that attempts to limit these offsets on bootup. | |
321 | However, the code cannot guarantee to accurately tune the offsets. | |
322 | Glibc contains no provisions to deal with these offsets (unlike the Linux | |
c13182ef MK |
323 | Kernel). |
324 | Typically these offsets are small and therefore the effects may be | |
fea681da | 325 | negligible in most cases. |
efeece04 | 326 | .PP |
78638aae MK |
327 | Since glibc 2.4, |
328 | the wrapper functions for the system calls described in this page avoid | |
329 | the abovementioned problems by employing the kernel implementation of | |
330 | .B CLOCK_PROCESS_CPUTIME_ID | |
331 | and | |
332 | .BR CLOCK_THREAD_CPUTIME_ID , | |
333 | on systems that provide such an implementation | |
334 | (i.e., Linux 2.6.12 and later). | |
3847be78 | 335 | .SH BUGS |
a113440c | 336 | According to POSIX.1-2001, a process with "appropriate privileges" may set the |
3847be78 MK |
337 | .B CLOCK_PROCESS_CPUTIME_ID |
338 | and | |
339 | .B CLOCK_THREAD_CPUTIME_ID | |
a113440c | 340 | clocks using |
3847be78 | 341 | .BR clock_settime (). |
a113440c MK |
342 | On Linux, these clocks are not settable |
343 | (i.e., no process has "appropriate privileges"). | |
344 | .\" See http://bugzilla.kernel.org/show_bug.cgi?id=11972 | |
47297adb | 345 | .SH SEE ALSO |
fea681da | 346 | .BR date (1), |
fea681da MK |
347 | .BR gettimeofday (2), |
348 | .BR settimeofday (2), | |
349 | .BR time (2), | |
65a0c012 | 350 | .BR adjtime (3), |
edf60acb | 351 | .BR clock_getcpuclockid (3), |
fea681da MK |
352 | .BR ctime (3), |
353 | .BR ftime (3), | |
24ad017d | 354 | .BR pthread_getcpuclockid (3), |
1d7c4d16 | 355 | .BR sysconf (3), |
18688d17 | 356 | .BR time (7), |
1ce611a3 MK |
357 | .BR vdso (7), |
358 | .BR hwclock (8) |