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