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