OSDN Git Service

3b5a0874e46c6667ed6f0d5987b4eee195fb1c34
[linuxjm/LDP_man-pages.git] / original / 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 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .\" 2003-08-23 Martin Schulze <joey@infodrom.org> improvements
27 .\" 2003-08-24 aeb, large parts rewritten
28 .\" 2004-08-06 Christoph Lameter <clameter@sgi.com>, SMP note
29 .\"
30 .TH CLOCK_GETRES 2 2013-12-28 "" "Linux Programmer's Manual"
31 .SH NAME
32 clock_getres, clock_gettime, clock_settime \- clock and time functions
33 .SH SYNOPSIS
34 .B #include <time.h>
35 .sp
36 .BI "int clock_getres(clockid_t " clk_id ", struct timespec *" res );
37
38 .BI "int clock_gettime(clockid_t " clk_id ", struct timespec *" tp );
39
40 .BI "int clock_settime(clockid_t " clk_id ", const struct timespec *" tp );
41 .sp
42 Link with \fI\-lrt\fP (only for glibc versions before 2.17).
43 .sp
44 .in -4n
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
47 .in
48 .sp
49 .ad l
50 .BR clock_getres (),
51 .BR clock_gettime (),
52 .BR clock_settime ():
53 .RS
54 _POSIX_C_SOURCE\ >=\ 199309L
55 .RE
56 .ad b
57 .SH DESCRIPTION
58 The function
59 .BR clock_getres ()
60 finds the resolution (precision) of the specified clock
61 .IR clk_id ,
62 and, if
63 .I res
64 is non-NULL, stores it in the \fIstruct timespec\fP pointed to by
65 .IR res .
66 The resolution of clocks depends on the implementation and cannot be
67 configured by a particular process.
68 If the time value pointed to by the argument
69 .I tp
70 of
71 .BR clock_settime ()
72 is not a multiple of
73 .IR res ,
74 then it is truncated to a multiple of
75 .IR res .
76 .PP
77 The functions
78 .BR clock_gettime ()
79 and
80 .BR clock_settime ()
81 retrieve and set the time of the specified clock
82 .IR clk_id .
83 .PP
84 The
85 .I res
86 and
87 .I tp
88 arguments are
89 .I timespec
90 structures, as specified in
91 .IR <time.h> :
92 .sp
93 .in +4n
94 .nf
95 struct timespec {
96     time_t   tv_sec;        /* seconds */
97     long     tv_nsec;       /* nanoseconds */
98 };
99 .fi
100 .in
101 .PP
102 The
103 .I clk_id
104 argument is the identifier of the particular clock on which to act.
105 A clock may be system-wide and hence visible for all processes, or
106 per-process if it measures time only within a single process.
107 .LP
108 All implementations support the system-wide real-time clock,
109 which is identified by
110 .BR CLOCK_REALTIME .
111 Its time represents seconds and nanoseconds since the Epoch.
112 When its time is changed, timers for a relative interval are
113 unaffected, but timers for an absolute point in time are affected.
114 .LP
115 More clocks may be implemented.
116 The interpretation of the
117 corresponding time values and the effect on timers is unspecified.
118 .LP
119 Sufficiently recent versions of glibc and the Linux kernel
120 support the following clocks:
121 .TP
122 .B CLOCK_REALTIME
123 System-wide clock that measures real (i.e., wall-clock) time.
124 Setting this clock requires appropriate privileges.
125 This clock is affected by discontinuous jumps in the system time
126 (e.g., if the system administrator manually changes the clock),
127 and by the incremental adjustments performed by
128 .BR adjtime (3)
129 and NTP.
130 .TP
131 .BR CLOCK_REALTIME_COARSE " (since Linux 2.6.32; Linux-specific)"
132 .\" Added in commit da15cfdae03351c689736f8d142618592e3cebc3
133 A faster but less precise version of
134 .BR CLOCK_REALTIME .
135 Use when you need very fast, but not fine-grained timestamps.
136 .TP
137 .TP
138 .B CLOCK_MONOTONIC
139 Clock that cannot be set and represents monotonic time since
140 some unspecified starting point.
141 This clock is not affected by discontinuous jumps in the system time
142 (e.g., if the system administrator manually changes the clock),
143 but is affected by the incremental adjustments performed by
144 .BR adjtime (3)
145 and NTP.
146 .TP
147 .BR CLOCK_MONOTONIC_COARSE " (since Linux 2.6.32; Linux-specific)"
148 .\" Added in commit da15cfdae03351c689736f8d142618592e3cebc3
149 A faster but less precise version of
150 .BR CLOCK_MONOTONIC .
151 Use when you need very fast, but not fine-grained timestamps.
152 .TP
153 .BR CLOCK_MONOTONIC_RAW " (since Linux 2.6.28; Linux-specific)"
154 .\" Added in commit 2d42244ae71d6c7b0884b5664cf2eda30fb2ae68, John Stultz
155 Similar to
156 .BR CLOCK_MONOTONIC ,
157 but provides access to a raw hardware-based time
158 that is not subject to NTP adjustments or
159 the incremental adjustments performed by
160 .BR adjtime (3).
161 .TP
162 .BR CLOCK_BOOTTIME " (since Linux 2.6.39; Linux-specific)"
163 .\" commit 7fdd7f89006dd5a4c702fa0ce0c272345fa44ae0
164 .\" commit 70a08cca1227dc31c784ec930099a4417a06e7d0
165 Identical to
166 .BR CLOCK_MONOTONIC ,
167 except it also includes any time that the system is suspended.
168 This allows applications to get a suspend-aware monotonic clock
169 without having to deal with the complications of
170 .BR CLOCK_REALTIME ,
171 which may have discontinuities if the time is changed using
172 .BR settimeofday (2).
173 .TP
174 .BR CLOCK_PROCESS_CPUTIME_ID " (since Linux 2.6.12)"
175 Per-process CPU-time clock
176 (measures CPU time consumed by all threads in the process).
177 .TP
178 .BR CLOCK_THREAD_CPUTIME_ID " (since Linux 2.6.12)"
179 Thread-specific CPU-time clock.
180 .SH RETURN VALUE
181 .BR clock_gettime (),
182 .BR clock_settime ()
183 and
184 .BR clock_getres ()
185 return 0 for success, or \-1 for failure (in which case
186 .I errno
187 is set appropriately).
188 .SH ERRORS
189 .TP
190 .B EFAULT
191 .I tp
192 points outside the accessible address space.
193 .TP
194 .B EINVAL
195 The
196 .I clk_id
197 specified is not supported on this system.
198 .\" Linux also gives this error on attempts to set CLOCK_PROCESS_CPUTIME_ID
199 .\" and CLOCK_THREAD_CPUTIME_ID, when probably the proper error should be
200 .\" EPERM.
201 .TP
202 .B EPERM
203 .BR clock_settime ()
204 does not have permission to set the clock indicated.
205 .SH VERSIONS
206 These system calls first appeared in Linux 2.6.
207 .SH CONFORMING TO
208 SUSv2, POSIX.1-2001.
209 .SH AVAILABILITY
210 On POSIX systems on which these functions are available, the symbol
211 .B _POSIX_TIMERS
212 is defined in \fI<unistd.h>\fP to a value greater than 0.
213 The symbols
214 .BR _POSIX_MONOTONIC_CLOCK ,
215 .BR _POSIX_CPUTIME ,
216 .B _POSIX_THREAD_CPUTIME
217 indicate that
218 .BR CLOCK_MONOTONIC ,
219 .BR CLOCK_PROCESS_CPUTIME_ID ,
220 .B CLOCK_THREAD_CPUTIME_ID
221 are available.
222 (See also
223 .BR sysconf (3).)
224 .SH NOTES
225 .SS Historical note for SMP systems
226 Before Linux added kernel support for
227 .B CLOCK_PROCESS_CPUTIME_ID
228 and
229 .BR CLOCK_THREAD_CPUTIME_ID ,
230 glibc implemented these clocks on many platforms using timer
231 registers from the CPUs
232 (TSC on i386, AR.ITC on Itanium).
233 These registers may differ between CPUs and as a consequence
234 these clocks may return
235 .B bogus results
236 if a process is migrated to another CPU.
237 .PP
238 If the CPUs in an SMP system have different clock sources, then
239 there is no way to maintain a correlation between the timer registers since
240 each CPU will run at a slightly different frequency.
241 If that is the case, then
242 .I clock_getcpuclockid(0)
243 will return
244 .B ENOENT
245 to signify this condition.
246 The two clocks will then be useful only if it
247 can be ensured that a process stays on a certain CPU.
248 .PP
249 The processors in an SMP system do not start all at exactly the same
250 time and therefore the timer registers are typically running at an offset.
251 Some architectures include code that attempts to limit these offsets on bootup.
252 However, the code cannot guarantee to accurately tune the offsets.
253 Glibc contains no provisions to deal with these offsets (unlike the Linux
254 Kernel).
255 Typically these offsets are small and therefore the effects may be
256 negligible in most cases.
257
258 Since glibc 2.4,
259 the wrapper functions for the system calls described in this page avoid
260 the abovementioned problems by employing the kernel implementation of
261 .B CLOCK_PROCESS_CPUTIME_ID
262 and
263 .BR CLOCK_THREAD_CPUTIME_ID ,
264 on systems that provide such an implementation
265 (i.e., Linux 2.6.12 and later).
266 .SH BUGS
267 According to POSIX.1-2001, a process with "appropriate privileges" may set the
268 .B CLOCK_PROCESS_CPUTIME_ID
269 and
270 .B CLOCK_THREAD_CPUTIME_ID
271 clocks using
272 .BR clock_settime ().
273 On Linux, these clocks are not settable
274 (i.e., no process has "appropriate privileges").
275 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=11972
276 .SH SEE ALSO
277 .BR date (1),
278 .BR gettimeofday (2),
279 .BR settimeofday (2),
280 .BR time (2),
281 .BR adjtime (3),
282 .BR clock_getcpuclockid (3),
283 .BR ctime (3),
284 .BR ftime (3),
285 .BR pthread_getcpuclockid (3),
286 .BR sysconf (3),
287 .BR time (7)
288 .SH COLOPHON
289 This page is part of release 3.67 of the Linux
290 .I man-pages
291 project.
292 A description of the project,
293 information about reporting bugs,
294 and the latest version of this page,
295 can be found at
296 \%http://www.kernel.org/doc/man\-pages/.