]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/clock_getres.2
b2e00f4a5416fe49aa8d191eeb0063b14c2e72e9
[thirdparty/man-pages.git] / man2 / clock_getres.2
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 .\" Copyright (c) 2020 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .\" 2003-08-23 Martin Schulze <joey@infodrom.org> improvements
8 .\" 2003-08-24 aeb, large parts rewritten
9 .\" 2004-08-06 Christoph Lameter <clameter@sgi.com>, SMP note
10 .\"
11 .TH CLOCK_GETRES 2 2021-03-22 "" "Linux Programmer's Manual"
12 .SH NAME
13 clock_getres, clock_gettime, clock_settime \- clock and time functions
14 .SH LIBRARY
15 Standard C library
16 .RI ( libc ", " \-lc ),
17 since glibc 2.17
18 .PP
19 Before glibc 2.17,
20 Real-time library
21 .RI ( librt ", " \-lrt )
22 .SH SYNOPSIS
23 .nf
24 .B #include <time.h>
25 .PP
26 .BI "int clock_getres(clockid_t " clockid ", struct timespec *" res );
27 .PP
28 .BI "int clock_gettime(clockid_t " clockid ", struct timespec *" tp );
29 .BI "int clock_settime(clockid_t " clockid ", const struct timespec *" tp );
30 .fi
31 .PP
32 .RS -4
33 Feature Test Macro Requirements for glibc (see
34 .BR feature_test_macros (7)):
35 .RE
36 .PP
37 .BR clock_getres (),
38 .BR clock_gettime (),
39 .BR clock_settime ():
40 .nf
41 _POSIX_C_SOURCE >= 199309L
42 .fi
43 .SH DESCRIPTION
44 The function
45 .BR clock_getres ()
46 finds the resolution (precision) of the specified clock
47 .IR clockid ,
48 and, if
49 .I res
50 is non-NULL, stores it in the \fIstruct timespec\fP pointed to by
51 .IR res .
52 The resolution of clocks depends on the implementation and cannot be
53 configured by a particular process.
54 If the time value pointed to by the argument
55 .I tp
56 of
57 .BR clock_settime ()
58 is not a multiple of
59 .IR res ,
60 then it is truncated to a multiple of
61 .IR res .
62 .PP
63 The functions
64 .BR clock_gettime ()
65 and
66 .BR clock_settime ()
67 retrieve and set the time of the specified clock
68 .IR clockid .
69 .PP
70 The
71 .I res
72 and
73 .I tp
74 arguments are
75 .BR timespec (3)
76 structures.
77 .PP
78 The
79 .I clockid
80 argument is the identifier of the particular clock on which to act.
81 A clock may be system-wide and hence visible for all processes, or
82 per-process if it measures time only within a single process.
83 .PP
84 All implementations support the system-wide real-time clock,
85 which is identified by
86 .BR CLOCK_REALTIME .
87 Its time represents seconds and nanoseconds since the Epoch.
88 When its time is changed, timers for a relative interval are
89 unaffected, but timers for an absolute point in time are affected.
90 .PP
91 More clocks may be implemented.
92 The interpretation of the
93 corresponding time values and the effect on timers is unspecified.
94 .PP
95 Sufficiently recent versions of glibc and the Linux kernel
96 support the following clocks:
97 .TP
98 .B CLOCK_REALTIME
99 A settable system-wide clock that measures real (i.e., wall-clock) time.
100 Setting this clock requires appropriate privileges.
101 This clock is affected by discontinuous jumps in the system time
102 (e.g., if the system administrator manually changes the clock),
103 and by the incremental adjustments performed by
104 .BR adjtime (3)
105 and NTP.
106 .TP
107 .BR CLOCK_REALTIME_ALARM " (since Linux 3.0; Linux-specific)"
108 Like
109 .BR CLOCK_REALTIME ,
110 but not settable.
111 See
112 .BR timer_create (2)
113 for further details.
114 .TP
115 .BR CLOCK_REALTIME_COARSE " (since Linux 2.6.32; Linux-specific)"
116 .\" Added in commit da15cfdae03351c689736f8d142618592e3cebc3
117 A faster but less precise version of
118 .BR CLOCK_REALTIME .
119 This clock is not settable.
120 Use when you need very fast, but not fine-grained timestamps.
121 Requires per-architecture support,
122 and probably also architecture support for this flag in the
123 .BR vdso (7).
124 .TP
125 .BR CLOCK_TAI " (since Linux 3.10; Linux-specific)"
126 .\" commit 1ff3c9677bff7e468e0c487d0ffefe4e901d33f4
127 A nonsettable system-wide clock derived from wall-clock time
128 but ignoring leap seconds.
129 This clock does
130 not experience discontinuities and backwards jumps caused by NTP
131 inserting leap seconds as
132 .BR CLOCK_REALTIME
133 does.
134 .IP
135 The acronym TAI refers to International Atomic Time.
136 .TP
137 .B CLOCK_MONOTONIC
138 A nonsettable system-wide clock that
139 represents monotonic time since\(emas described
140 by POSIX\(em"some unspecified point in the past".
141 On Linux, that point corresponds to the number of seconds that the system
142 has been running since it was booted.
143 .IP
144 The
145 .B CLOCK_MONOTONIC
146 clock is not affected by discontinuous jumps in the system time
147 (e.g., if the system administrator manually changes the clock),
148 but is affected by the incremental adjustments performed by
149 .BR adjtime (3)
150 and NTP.
151 This clock does not count time that the system is suspended.
152 All
153 .B CLOCK_MONOTONIC
154 variants guarantee that the time returned by consecutive calls will not go
155 backwards, but successive calls may\(emdepending on the architecture\(emreturn
156 identical (not-increased) time values.
157 .TP
158 .BR CLOCK_MONOTONIC_COARSE " (since Linux 2.6.32; Linux-specific)"
159 .\" Added in commit da15cfdae03351c689736f8d142618592e3cebc3
160 A faster but less precise version of
161 .BR CLOCK_MONOTONIC .
162 Use when you need very fast, but not fine-grained timestamps.
163 Requires per-architecture support,
164 and probably also architecture support for this flag in the
165 .BR vdso (7).
166 .TP
167 .BR CLOCK_MONOTONIC_RAW " (since Linux 2.6.28; Linux-specific)"
168 .\" Added in commit 2d42244ae71d6c7b0884b5664cf2eda30fb2ae68, John Stultz
169 Similar to
170 .BR CLOCK_MONOTONIC ,
171 but provides access to a raw hardware-based time
172 that is not subject to NTP adjustments or
173 the incremental adjustments performed by
174 .BR adjtime (3).
175 This clock does not count time that the system is suspended.
176 .TP
177 .BR CLOCK_BOOTTIME " (since Linux 2.6.39; Linux-specific)"
178 .\" commit 7fdd7f89006dd5a4c702fa0ce0c272345fa44ae0
179 .\" commit 70a08cca1227dc31c784ec930099a4417a06e7d0
180 A nonsettable system-wide clock that is identical to
181 .BR CLOCK_MONOTONIC ,
182 except that it also includes any time that the system is suspended.
183 This allows applications to get a suspend-aware monotonic clock
184 without having to deal with the complications of
185 .BR CLOCK_REALTIME ,
186 which may have discontinuities if the time is changed using
187 .BR settimeofday (2)
188 or similar.
189 .TP
190 .BR CLOCK_BOOTTIME_ALARM " (since Linux 3.0; Linux-specific)"
191 Like
192 .BR CLOCK_BOOTTIME .
193 See
194 .BR timer_create (2)
195 for further details.
196 .TP
197 .BR CLOCK_PROCESS_CPUTIME_ID " (since Linux 2.6.12)"
198 This is a clock that measures CPU time consumed by this process
199 (i.e., CPU time consumed by all threads in the process).
200 On Linux, this clock is not settable.
201 .TP
202 .BR CLOCK_THREAD_CPUTIME_ID " (since Linux 2.6.12)"
203 This is a clock that measures CPU time consumed by this thread.
204 On Linux, this clock is not settable.
205 .PP
206 Linux also implements dynamic clock instances as described below.
207 .SS Dynamic clocks
208 In addition to the hard-coded System-V style clock IDs described above,
209 Linux also supports
210 POSIX clock operations on certain character devices.
211 Such devices are
212 called "dynamic" clocks, and are supported since Linux 2.6.39.
213 .PP
214 Using the appropriate macros, open file
215 descriptors may be converted into clock IDs and passed to
216 .BR clock_gettime (),
217 .BR clock_settime (),
218 and
219 .BR clock_adjtime (2).
220 The following example shows how to convert a file descriptor into a
221 dynamic clock ID.
222 .PP
223 .in +4n
224 .EX
225 #define CLOCKFD 3
226 #define FD_TO_CLOCKID(fd) ((\(ti(clockid_t) (fd) << 3) | CLOCKFD)
227 #define CLOCKID_TO_FD(clk) ((unsigned int) \(ti((clk) >> 3))
228
229 struct timespec ts;
230 clockid_t clkid;
231 int fd;
232
233 fd = open("/dev/ptp0", O_RDWR);
234 clkid = FD_TO_CLOCKID(fd);
235 clock_gettime(clkid, &ts);
236 .EE
237 .in
238 .SH RETURN VALUE
239 .BR clock_gettime (),
240 .BR clock_settime (),
241 and
242 .BR clock_getres ()
243 return 0 for success.
244 On error, \-1 is returned and
245 .I errno
246 is set to indicate the error.
247 .SH ERRORS
248 .TP
249 .B EACCES
250 .BR clock_settime ()
251 does not have write permission for the dynamic POSIX
252 clock device indicated.
253 .TP
254 .B EFAULT
255 .I tp
256 points outside the accessible address space.
257 .TP
258 .B EINVAL
259 The
260 .I clockid
261 specified is invalid for one of two reasons.
262 Either the System-V style
263 hard coded positive value is out of range, or the dynamic clock ID
264 does not refer to a valid instance of a clock object.
265 .\" Linux also gives this error on attempts to set CLOCK_PROCESS_CPUTIME_ID
266 .\" and CLOCK_THREAD_CPUTIME_ID, when probably the proper error should be
267 .\" EPERM.
268 .TP
269 .B EINVAL
270 .RB ( clock_settime ()):
271 .I tp.tv_sec
272 is negative or
273 .I tp.tv_nsec
274 is outside the range [0..999,999,999].
275 .TP
276 .B EINVAL
277 The
278 .I clockid
279 specified in a call to
280 .BR clock_settime ()
281 is not a settable clock.
282 .TP
283 .BR EINVAL " (since Linux 4.3)"
284 .\" commit e1d7ba8735551ed79c7a0463a042353574b96da3
285 A call to
286 .BR clock_settime ()
287 with a
288 .I clockid
289 of
290 .B CLOCK_REALTIME
291 attempted to set the time to a value less than
292 the current value of the
293 .B CLOCK_MONOTONIC
294 clock.
295 .TP
296 .B ENODEV
297 The hot-pluggable device (like USB for example) represented by a
298 dynamic
299 .I clk_id
300 has disappeared after its character device was opened.
301 .TP
302 .B ENOTSUP
303 The operation is not supported by the dynamic POSIX clock device
304 specified.
305 .TP
306 .B EPERM
307 .BR clock_settime ()
308 does not have permission to set the clock indicated.
309 .SH VERSIONS
310 These system calls first appeared in Linux 2.6.
311 .SH ATTRIBUTES
312 For an explanation of the terms used in this section, see
313 .BR attributes (7).
314 .ad l
315 .nh
316 .TS
317 allbox;
318 lbx lb lb
319 l l l.
320 Interface Attribute Value
321 T{
322 .BR clock_getres (),
323 .BR clock_gettime (),
324 .BR clock_settime ()
325 T} Thread safety MT-Safe
326 .TE
327 .hy
328 .ad
329 .sp 1
330 .SH CONFORMING TO
331 POSIX.1-2001, POSIX.1-2008, SUSv2.
332 .PP
333 On POSIX systems on which these functions are available, the symbol
334 .B _POSIX_TIMERS
335 is defined in \fI<unistd.h>\fP to a value greater than 0.
336 The symbols
337 .BR _POSIX_MONOTONIC_CLOCK ,
338 .BR _POSIX_CPUTIME ,
339 .B _POSIX_THREAD_CPUTIME
340 indicate that
341 .BR CLOCK_MONOTONIC ,
342 .BR CLOCK_PROCESS_CPUTIME_ID ,
343 .B CLOCK_THREAD_CPUTIME_ID
344 are available.
345 (See also
346 .BR sysconf (3).)
347 .SH NOTES
348 POSIX.1 specifies the following:
349 .RS
350 .PP
351 Setting the value of the
352 .B CLOCK_REALTIME
353 clock via
354 .BR clock_settime ()
355 shall have no effect on threads that are blocked waiting for a relative time
356 service based upon this clock, including the
357 .BR nanosleep ()
358 function; nor on the expiration of relative timers based upon this clock.
359 Consequently, these time services shall expire when the requested relative
360 interval elapses, independently of the new or old value of the clock.
361 .RE
362 .PP
363 According to POSIX.1-2001, a process with "appropriate privileges" may set the
364 .B CLOCK_PROCESS_CPUTIME_ID
365 and
366 .B CLOCK_THREAD_CPUTIME_ID
367 clocks using
368 .BR clock_settime ().
369 On Linux, these clocks are not settable
370 (i.e., no process has "appropriate privileges").
371 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=11972
372 .\"
373 .SS C library/kernel differences
374 On some architectures, an implementation of
375 .BR clock_gettime ()
376 is provided in the
377 .BR vdso (7).
378 .\"
379 .SS Historical note for SMP systems
380 Before Linux added kernel support for
381 .B CLOCK_PROCESS_CPUTIME_ID
382 and
383 .BR CLOCK_THREAD_CPUTIME_ID ,
384 glibc implemented these clocks on many platforms using timer
385 registers from the CPUs
386 (TSC on i386, AR.ITC on Itanium).
387 These registers may differ between CPUs and as a consequence
388 these clocks may return
389 .B bogus results
390 if a process is migrated to another CPU.
391 .PP
392 If the CPUs in an SMP system have different clock sources, then
393 there is no way to maintain a correlation between the timer registers since
394 each CPU will run at a slightly different frequency.
395 If that is the case, then
396 .I clock_getcpuclockid(0)
397 will return
398 .B ENOENT
399 to signify this condition.
400 The two clocks will then be useful only if it
401 can be ensured that a process stays on a certain CPU.
402 .PP
403 The processors in an SMP system do not start all at exactly the same
404 time and therefore the timer registers are typically running at an offset.
405 Some architectures include code that attempts to limit these offsets on bootup.
406 However, the code cannot guarantee to accurately tune the offsets.
407 Glibc contains no provisions to deal with these offsets (unlike the Linux
408 Kernel).
409 Typically these offsets are small and therefore the effects may be
410 negligible in most cases.
411 .PP
412 Since glibc 2.4,
413 the wrapper functions for the system calls described in this page avoid
414 the abovementioned problems by employing the kernel implementation of
415 .B CLOCK_PROCESS_CPUTIME_ID
416 and
417 .BR CLOCK_THREAD_CPUTIME_ID ,
418 on systems that provide such an implementation
419 (i.e., Linux 2.6.12 and later).
420 .SH EXAMPLES
421 The program below demonstrates the use of
422 .BR clock_gettime ()
423 and
424 .BR clock_getres ()
425 with various clocks.
426 This is an example of what we might see when running the program:
427 .PP
428 .in +4n
429 .EX
430 $ \fB./clock_times x\fP
431 CLOCK_REALTIME : 1585985459.446 (18356 days + 7h 30m 59s)
432 resolution: 0.000000001
433 CLOCK_TAI : 1585985496.447 (18356 days + 7h 31m 36s)
434 resolution: 0.000000001
435 CLOCK_MONOTONIC: 52395.722 (14h 33m 15s)
436 resolution: 0.000000001
437 CLOCK_BOOTTIME : 72691.019 (20h 11m 31s)
438 resolution: 0.000000001
439 .EE
440 .in
441 .SS Program source
442 \&
443 .EX
444 /* clock_times.c
445
446 Licensed under GNU General Public License v2 or later.
447 */
448 #define _XOPEN_SOURCE 600
449 #include <time.h>
450 #include <stdint.h>
451 #include <stdio.h>
452 #include <stdlib.h>
453 #include <stdbool.h>
454 #include <unistd.h>
455
456 #define SECS_IN_DAY (24 * 60 * 60)
457
458 static void
459 displayClock(clockid_t clock, const char *name, bool showRes)
460 {
461 struct timespec ts;
462
463 if (clock_gettime(clock, &ts) == \-1) {
464 perror("clock_gettime");
465 exit(EXIT_FAILURE);
466 }
467
468 printf("%\-15s: %10jd.%03ld (", name,
469 (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
470
471 long days = ts.tv_sec / SECS_IN_DAY;
472 if (days > 0)
473 printf("%ld days + ", days);
474
475 printf("%2dh %2dm %2ds",
476 (int) (ts.tv_sec % SECS_IN_DAY) / 3600,
477 (int) (ts.tv_sec % 3600) / 60,
478 (int) ts.tv_sec % 60);
479 printf(")\en");
480
481 if (clock_getres(clock, &ts) == \-1) {
482 perror("clock_getres");
483 exit(EXIT_FAILURE);
484 }
485
486 if (showRes)
487 printf(" resolution: %10jd.%09ld\en",
488 (intmax_t) ts.tv_sec, ts.tv_nsec);
489 }
490
491 int
492 main(int argc, char *argv[])
493 {
494 bool showRes = argc > 1;
495
496 displayClock(CLOCK_REALTIME, "CLOCK_REALTIME", showRes);
497 #ifdef CLOCK_TAI
498 displayClock(CLOCK_TAI, "CLOCK_TAI", showRes);
499 #endif
500 displayClock(CLOCK_MONOTONIC, "CLOCK_MONOTONIC", showRes);
501 #ifdef CLOCK_BOOTTIME
502 displayClock(CLOCK_BOOTTIME, "CLOCK_BOOTTIME", showRes);
503 #endif
504 exit(EXIT_SUCCESS);
505 }
506 .EE
507 .SH SEE ALSO
508 .BR date (1),
509 .BR gettimeofday (2),
510 .BR settimeofday (2),
511 .BR time (2),
512 .BR adjtime (3),
513 .BR clock_getcpuclockid (3),
514 .BR ctime (3),
515 .BR ftime (3),
516 .BR pthread_getcpuclockid (3),
517 .BR sysconf (3),
518 .BR timespec (3),
519 .BR time (7),
520 .BR time_namespaces (7),
521 .BR vdso (7),
522 .BR hwclock (8)