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>
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
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
12 .TH clock_getres 2 (date) "Linux man-pages (unreleased)"
14 clock_getres, clock_gettime, clock_settime \- clock and time functions
17 .RI ( libc ", " \-lc ),
22 .RI ( librt ", " \-lrt )
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 );
34 Feature Test Macro Requirements for glibc (see
35 .BR feature_test_macros (7)):
42 _POSIX_C_SOURCE >= 199309L
47 finds the resolution (precision) of the specified clock
51 is non-NULL, stores it in the \fIstruct timespec\fP pointed to by
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
61 then it is truncated to a multiple of
68 retrieve and set the time of the specified clock
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
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:
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
107 .BR clock_adjtime (2),
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)"
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
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
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.
139 not experience discontinuities or frequency adjustments caused by
140 inserting leap seconds as
144 The acronym TAI refers to International Atomic Time.
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.
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.
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
174 .BR CLOCK_MONOTONIC_RAW " (since Linux 2.6.28; Linux-specific)"
175 .\" Added in commit 2d42244ae71d6c7b0884b5664cf2eda30fb2ae68, John Stultz
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
191 which may have discontinuities if the time is changed using
195 .BR CLOCK_BOOTTIME_ALARM " (since Linux 3.0; Linux-specific)"
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.
213 In addition to the hard-coded System-V style clock IDs described above,
215 POSIX clock operations on certain character devices.
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
231 #define FD_TO_CLOCKID(fd) ((\[ti](clockid_t) (fd) << 3) | CLOCKFD)
232 #define CLOCKID_TO_FD(clk) ((unsigned int) \[ti]((clk) >> 3))
238 fd = open("/dev/ptp0", O_RDWR);
239 clkid = FD_TO_CLOCKID(fd);
240 clock_gettime(clkid, &ts);
244 .BR clock_gettime (),
245 .BR clock_settime (),
248 return 0 for success.
249 On error, \-1 is returned and
251 is set to indicate the error.
256 does not have write permission for the dynamic POSIX
257 clock device indicated.
261 points outside the accessible address space.
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
275 .RB ( clock_settime ()):
279 is outside the range [0, 999,999,999].
284 specified in a call to
286 is not a settable clock.
288 .BR EINVAL " (since Linux 4.3)"
289 .\" commit e1d7ba8735551ed79c7a0463a042353574b96da3
296 attempted to set the time to a value less than
297 the current value of the
302 The hot-pluggable device (like USB for example) represented by a
305 has disappeared after its character device was opened.
308 The operation is not supported by the dynamic POSIX clock device
312 The timestamp would not fit in
315 This can happen if an executable with 32-bit
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
320 range in other situations, the behavior is undefined.
324 does not have permission to set the clock indicated.
326 For an explanation of the terms used in this section, see
332 Interface Attribute Value
337 .BR clock_gettime (),
339 T} Thread safety MT-Safe
342 POSIX.1 specifies the following:
345 Setting the value of the
349 shall have no effect on threads that are blocked waiting for a relative time
350 service based upon this clock, including the
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
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
377 On POSIX systems on which these functions are available, the symbol
379 is defined in \fI<unistd.h>\fP to a value greater than 0.
381 .BR _POSIX_MONOTONIC_CLOCK ,
383 .B _POSIX_THREAD_CPUTIME
385 .BR CLOCK_MONOTONIC ,
386 .BR CLOCK_PROCESS_CPUTIME_ID ,
387 .B CLOCK_THREAD_CPUTIME_ID
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
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)
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
423 Typically these offsets are small and therefore the effects may be
424 negligible in most cases.
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).
435 The program below demonstrates the use of
440 This is an example of what we might see when running the program:
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
457 .\" SRC BEGIN (clock_getres.c)
461 Licensed under GNU General Public License v2 or later.
463 #define _XOPEN_SOURCE 600
470 #define SECS_IN_DAY (24 * 60 * 60)
473 displayClock(clockid_t clock, const char *name, bool showRes)
478 if (clock_gettime(clock, &ts) == \-1) {
479 perror("clock_gettime");
483 printf("%\-15s: %10jd.%03ld (", name,
484 (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
486 days = ts.tv_sec / SECS_IN_DAY;
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);
496 if (clock_getres(clock, &ts) == \-1) {
497 perror("clock_getres");
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);
513 displayClock(CLOCK_TAI, "CLOCK_TAI", showRes);
515 displayClock(CLOCK_MONOTONIC, "CLOCK_MONOTONIC", showRes);
516 #ifdef CLOCK_BOOTTIME
517 displayClock(CLOCK_BOOTTIME, "CLOCK_BOOTTIME", showRes);
525 .BR gettimeofday (2),
526 .BR settimeofday (2),
529 .BR clock_getcpuclockid (3),
532 .BR pthread_getcpuclockid (3),
536 .BR time_namespaces (7),