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