1CLOCK_GETRES(2) Linux Programmer's Manual CLOCK_GETRES(2)
2
3
4
6 clock_getres, clock_gettime, clock_settime - clock and time functions
7
9 #include <time.h>
10
11 int clock_getres(clockid_t clk_id, struct timespec *res);
12
13 int clock_gettime(clockid_t clk_id, struct timespec *tp);
14
15 int clock_settime(clockid_t clk_id, const struct timespec *tp);
16
17 Link with -lrt (only for glibc versions before 2.17).
18
19 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21 clock_getres(), clock_gettime(), clock_settime():
22 _POSIX_C_SOURCE >= 199309L
23
25 The function clock_getres() finds the resolution (precision) of the
26 specified clock clk_id, and, if res is non-NULL, stores it in the
27 struct timespec pointed to by res. The resolution of clocks depends on
28 the implementation and cannot be configured by a particular process.
29 If the time value pointed to by the argument tp of clock_settime() is
30 not a multiple of res, then it is truncated to a multiple of res.
31
32 The functions clock_gettime() and clock_settime() retrieve and set the
33 time of the specified clock clk_id.
34
35 The res and tp arguments are timespec structures, as specified in
36 <time.h>:
37
38 struct timespec {
39 time_t tv_sec; /* seconds */
40 long tv_nsec; /* nanoseconds */
41 };
42
43 The clk_id argument is the identifier of the particular clock on which
44 to act. A clock may be system-wide and hence visible for all pro‐
45 cesses, or per-process if it measures time only within a single
46 process.
47
48 All implementations support the system-wide real-time clock, which is
49 identified by CLOCK_REALTIME. Its time represents seconds and nanosec‐
50 onds since the Epoch. When its time is changed, timers for a relative
51 interval are unaffected, but timers for an absolute point in time are
52 affected.
53
54 More clocks may be implemented. The interpretation of the correspond‐
55 ing time values and the effect on timers is unspecified.
56
57 Sufficiently recent versions of glibc and the Linux kernel support the
58 following clocks:
59
60 CLOCK_REALTIME
61 System-wide clock that measures real (i.e., wall-clock) time.
62 Setting this clock requires appropriate privileges. This clock
63 is affected by discontinuous jumps in the system time (e.g., if
64 the system administrator manually changes the clock), and by the
65 incremental adjustments performed by adjtime(3) and NTP.
66
67 CLOCK_REALTIME_COARSE (since Linux 2.6.32; Linux-specific)
68 A faster but less precise version of CLOCK_REALTIME. Use when
69 you need very fast, but not fine-grained timestamps.
70
71
72 CLOCK_MONOTONIC
73 Clock that cannot be set and represents monotonic time
74 since some unspecified starting point. This clock is not
75 affected by discontinuous jumps in the system time (e.g.,
76 if the system administrator manually changes the clock),
77 but is affected by the incremental adjustments performed
78 by adjtime(3) and NTP.
79
80 CLOCK_MONOTONIC_COARSE (since Linux 2.6.32; Linux-specific)
81 A faster but less precise version of CLOCK_MONOTONIC.
82 Use when you need very fast, but not fine-grained time‐
83 stamps.
84
85 CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
86 Similar to CLOCK_MONOTONIC, but provides access to a raw
87 hardware-based time that is not subject to NTP adjust‐
88 ments or the incremental adjustments performed by adj‐
89 time(3).
90
91 CLOCK_BOOTTIME (since Linux 2.6.39; Linux-specific)
92 Identical to CLOCK_MONOTONIC, except it also includes any
93 time that the system is suspended. This allows applica‐
94 tions to get a suspend-aware monotonic clock without hav‐
95 ing to deal with the complications of CLOCK_REALTIME,
96 which may have discontinuities if the time is changed
97 using settimeofday(2).
98
99 CLOCK_PROCESS_CPUTIME_ID
100 High-resolution per-process timer from the CPU.
101
102 CLOCK_THREAD_CPUTIME_ID
103 Thread-specific CPU-time clock.
104
106 clock_gettime(), clock_settime() and clock_getres() return 0 for
107 success, or -1 for failure (in which case errno is set appropri‐
108 ately).
109
111 EFAULT tp points outside the accessible address space.
112
113 EINVAL The clk_id specified is not supported on this system.
114
115 EPERM clock_settime() does not have permission to set the clock
116 indicated.
117
119 SUSv2, POSIX.1-2001.
120
122 On POSIX systems on which these functions are available, the
123 symbol _POSIX_TIMERS is defined in <unistd.h> to a value greater
124 than 0. The symbols _POSIX_MONOTONIC_CLOCK, _POSIX_CPUTIME,
125 _POSIX_THREAD_CPUTIME indicate that CLOCK_MONOTONIC,
126 CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID are available.
127 (See also sysconf(3).)
128
130 Note for SMP systems
131 The CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID clocks
132 are realized on many platforms using timers from the CPUs (TSC
133 on i386, AR.ITC on Itanium). These registers may differ between
134 CPUs and as a consequence these clocks may return bogus results
135 if a process is migrated to another CPU.
136
137 If the CPUs in an SMP system have different clock sources then
138 there is no way to maintain a correlation between the timer reg‐
139 isters since each CPU will run at a slightly different fre‐
140 quency. If that is the case then clock_getcpuclockid(0) will
141 return ENOENT to signify this condition. The two clocks will
142 then be useful only if it can be ensured that a process stays on
143 a certain CPU.
144
145 The processors in an SMP system do not start all at exactly the
146 same time and therefore the timer registers are typically run‐
147 ning at an offset. Some architectures include code that
148 attempts to limit these offsets on bootup. However, the code
149 cannot guarantee to accurately tune the offsets. Glibc contains
150 no provisions to deal with these offsets (unlike the Linux Ker‐
151 nel). Typically these offsets are small and therefore the
152 effects may be negligible in most cases.
153
155 According to POSIX.1-2001, a process with "appropriate privi‐
156 leges" may set the CLOCK_PROCESS_CPUTIME_ID and
157 CLOCK_THREAD_CPUTIME_ID clocks using clock_settime(). On Linux,
158 these clocks are not settable (i.e., no process has "appropriate
159 privileges").
160
162 date(1), gettimeofday(2), settimeofday(2), time(2), adjtime(3),
163 clock_getcpuclockid(3), ctime(3), ftime(3), pthread_getcpu‐
164 clockid(3), sysconf(3), time(7)
165
167 This page is part of release 3.53 of the Linux man-pages
168 project. A description of the project, and information about
169 reporting bugs, can be found at
170 http://www.kernel.org/doc/man-pages/.
171
172
173
174 2013-02-25 CLOCK_GETRES(2)