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