Debian Codegraph @

upstream/debian-trixie/man2/clock_getres.2

raw
1 '\" t
2 .\" Copyright (c) 2003 Nick Clifford ([email protected]), Jan 25, 2003
3 .\" Copyright (c) 2003 Andries Brouwer ([email protected]), Aug 24, 2003
4 .\" Copyright (c) 2020 Michael Kerrisk <[email protected]>
5 .\"
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\"
8 .\" 2003-08-23 Martin Schulze <[email protected]> improvements
9 .\" 2003-08-24 aeb, large parts rewritten
10 .\" 2004-08-06 Christoph Lameter <[email protected]>, SMP note
11 .\"
12 .TH clock_getres 2 2024-06-15 "Linux man-pages 6.9.1"
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
19 .P
20 Before glibc 2.17,
21 Real-time library
22 .RI ( librt ", " \-lrt )
23 .SH SYNOPSIS
24 .nf
25 .B #include <time.h>
26 .P
27 .BI "int clock_getres(clockid_t " clockid ", struct timespec *_Nullable " res );
28 .P
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
32 .P
33 .RS -4
34 Feature Test Macro Requirements for glibc (see
35 .BR feature_test_macros (7)):
36 .RE
37 .P
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
57 of
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 .
63 .P
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 .
70 .P
71 The
72 .I res
73 and
74 .I tp
75 arguments are
76 .BR timespec (3)
77 structures.
78 .P
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.
84 .P
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.
91 .P
92 More clocks may be implemented.
93 The interpretation of the
94 corresponding time values and the effect on timers is unspecified.
95 .P
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),
108 and
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.
115 .TP
116 .BR CLOCK_REALTIME_ALARM " (since Linux 3.0; Linux-specific)"
117 Like
118 .BR CLOCK_REALTIME ,
119 but not settable.
120 See
121 .BR timer_create (2)
122 for further details.
123 .TP
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).
133 .TP
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.
143 .IP
144 The acronym TAI refers to International Atomic Time.
145 .TP
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.
152 .IP
153 The
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.
159 All
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.
164 .TP
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).
173 .TP
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.
181 .TP
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.
194 .TP
195 .BR CLOCK_BOOTTIME_ALARM " (since Linux 3.0; Linux-specific)"
196 Like
197 .BR CLOCK_BOOTTIME .
198 See
199 .BR timer_create (2)
200 for further details.
201 .TP
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.
206 .TP
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.
210 .P
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.
218 .P
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 (),
223 and
224 .BR clock_adjtime (2).
225 The following example shows how to convert a file descriptor into a
226 dynamic clock ID.
227 .P
228 .in +4n
229 .EX
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))
233 \&
234 struct timespec ts;
235 clockid_t clkid;
236 int fd;
237 \&
238 fd = open("/dev/ptp0", O_RDWR);
239 clkid = FD_TO_CLOCKID(fd);
240 clock_gettime(clkid, &ts);
241 .EE
242 .in
243 .SH RETURN VALUE
244 .BR clock_gettime (),
245 .BR clock_settime (),
246 and
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
253 .TP
254 .B EACCES
255 .BR clock_settime ()
256 does not have write permission for the dynamic POSIX
257 clock device indicated.
258 .TP
259 .B EFAULT
260 .I tp
261 points outside the accessible address space.
262 .TP
263 .B EINVAL
264 The
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.
273 .TP
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].
280 .TP
281 .B EINVAL
282 The
283 .I clockid
284 specified in a call to
285 .BR clock_settime ()
286 is not a settable clock.
287 .TP
288 .BR EINVAL " (since Linux 4.3)"
289 .\" commit e1d7ba8735551ed79c7a0463a042353574b96da3
290 A call to
291 .BR clock_settime ()
292 with a
293 .I clockid
294 of
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.
300 .TP
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.
306 .TP
307 .B ENOTSUP
308 The operation is not supported by the dynamic POSIX clock device
309 specified.
310 .TP
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.
321 .TP
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).
328 .TS
329 allbox;
330 lbx lb lb
331 l l l.
332 Interface Attribute Value
333 T{
334 .na
335 .nh
336 .BR clock_getres (),
337 .BR clock_gettime (),
338 .BR clock_settime ()
339 T} Thread safety MT-Safe
340 .TE
341 .SH VERSIONS
342 POSIX.1 specifies the following:
343 .RS
344 .P
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.
355 .RE
356 .P
357 According to POSIX.1-2001, a process with "appropriate privileges" may set the
358 .B CLOCK_PROCESS_CPUTIME_ID
359 and
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.
376 .P
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 POSIX.1-2008 makes these functions mandatory.
381 .P
382 The symbols
383 .BR _POSIX_MONOTONIC_CLOCK ,
384 .BR _POSIX_CPUTIME ,
385 .B _POSIX_THREAD_CPUTIME
386 indicate that
387 .BR CLOCK_MONOTONIC ,
388 .BR CLOCK_PROCESS_CPUTIME_ID ,
389 .B CLOCK_THREAD_CPUTIME_ID
390 are available.
391 (See also
392 .BR sysconf (3).)
393 .\"
394 .SS Historical note for SMP systems
395 Before Linux added kernel support for
396 .B CLOCK_PROCESS_CPUTIME_ID
397 and
398 .BR CLOCK_THREAD_CPUTIME_ID ,
399 glibc implemented these clocks on many platforms using timer
400 registers from the CPUs
401 (TSC on i386, AR.ITC on Itanium).
402 These registers may differ between CPUs and as a consequence
403 these clocks may return
404 .B bogus results
405 if a process is migrated to another CPU.
406 .P
407 If the CPUs in an SMP system have different clock sources, then
408 there is no way to maintain a correlation between the timer registers since
409 each CPU will run at a slightly different frequency.
410 If that is the case, then
411 .I clock_getcpuclockid(0)
412 will return
413 .B ENOENT
414 to signify this condition.
415 The two clocks will then be useful only if it
416 can be ensured that a process stays on a certain CPU.
417 .P
418 The processors in an SMP system do not start all at exactly the same
419 time and therefore the timer registers are typically running at an offset.
420 Some architectures include code that attempts to limit these offsets on bootup.
421 However, the code cannot guarantee to accurately tune the offsets.
422 glibc contains no provisions to deal with these offsets (unlike the Linux
423 Kernel).
424 Typically these offsets are small and therefore the effects may be
425 negligible in most cases.
426 .P
427 Since glibc 2.4,
428 the wrapper functions for the system calls described in this page avoid
429 the abovementioned problems by employing the kernel implementation of
430 .B CLOCK_PROCESS_CPUTIME_ID
431 and
432 .BR CLOCK_THREAD_CPUTIME_ID ,
433 on systems that provide such an implementation
434 (i.e., Linux 2.6.12 and later).
435 .SH EXAMPLES
436 The program below demonstrates the use of
437 .BR clock_gettime ()
438 and
439 .BR clock_getres ()
440 with various clocks.
441 This is an example of what we might see when running the program:
442 .P
443 .in +4n
444 .EX
445 $ \fB./clock_times x\fP
446 CLOCK_REALTIME : 1585985459.446 (18356 days + 7h 30m 59s)
447 resolution: 0.000000001
448 CLOCK_TAI : 1585985496.447 (18356 days + 7h 31m 36s)
449 resolution: 0.000000001
450 CLOCK_MONOTONIC: 52395.722 (14h 33m 15s)
451 resolution: 0.000000001
452 CLOCK_BOOTTIME : 72691.019 (20h 11m 31s)
453 resolution: 0.000000001
454 .EE
455 .in
456 .SS Program source
457 \&
458 .\" SRC BEGIN (clock_getres.c)
459 .EX
460 /* clock_times.c
461 \&
462 Licensed under GNU General Public License v2 or later.
463 */
464 #define _XOPEN_SOURCE 600
465 #include <stdbool.h>
466 #include <stdint.h>
467 #include <stdio.h>
468 #include <stdlib.h>
469 #include <time.h>
470 \&
471 #define SECS_IN_DAY (24 * 60 * 60)
472 \&
473 static void
474 displayClock(clockid_t clock, const char *name, bool showRes)
475 {
476 long days;
477 struct timespec ts;
478 \&
479 if (clock_gettime(clock, &ts) == \-1) {
480 perror("clock_gettime");
481 exit(EXIT_FAILURE);
482 }
483 \&
484 printf("%\-15s: %10jd.%03ld (", name,
485 (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
486 \&
487 days = ts.tv_sec / SECS_IN_DAY;
488 if (days > 0)
489 printf("%ld days + ", days);
490 \&
491 printf("%2dh %2dm %2ds",
492 (int) (ts.tv_sec % SECS_IN_DAY) / 3600,
493 (int) (ts.tv_sec % 3600) / 60,
494 (int) ts.tv_sec % 60);
495 printf(")\[rs]n");
496 \&
497 if (clock_getres(clock, &ts) == \-1) {
498 perror("clock_getres");
499 exit(EXIT_FAILURE);
500 }
501 \&
502 if (showRes)
503 printf(" resolution: %10jd.%09ld\[rs]n",
504 (intmax_t) ts.tv_sec, ts.tv_nsec);
505 }
506 \&
507 int
508 main(int argc, char *argv[])
509 {
510 bool showRes = argc > 1;
511 \&
512 displayClock(CLOCK_REALTIME, "CLOCK_REALTIME", showRes);
513 #ifdef CLOCK_TAI
514 displayClock(CLOCK_TAI, "CLOCK_TAI", showRes);
515 #endif
516 displayClock(CLOCK_MONOTONIC, "CLOCK_MONOTONIC", showRes);
517 #ifdef CLOCK_BOOTTIME
518 displayClock(CLOCK_BOOTTIME, "CLOCK_BOOTTIME", showRes);
519 #endif
520 exit(EXIT_SUCCESS);
521 }
522 .EE
523 .\" SRC END
524 .SH SEE ALSO
525 .BR date (1),
526 .BR gettimeofday (2),
527 .BR settimeofday (2),
528 .BR time (2),
529 .BR adjtime (3),
530 .BR clock_getcpuclockid (3),
531 .BR ctime (3),
532 .BR ftime (3),
533 .BR pthread_getcpuclockid (3),
534 .BR sysconf (3),
535 .BR timespec (3),
536 .BR time (7),
537 .BR time_namespaces (7),
538 .BR vdso (7),
539 .BR hwclock (8)