OSDN Git Service

d52e828f921565373e77500bbd56c12a3d3a0d28
[linuxjm/LDP_man-pages.git] / original / man2 / clock_getres.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (c) 2003 Nick Clifford (zaf@nrc.co.nz), Jan 25, 2003
4 .\" Copyright (c) 2003 Andries Brouwer (aeb@cwi.nl), Aug 24, 2003
5 .\"
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 .\"
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 2010-02-03 "" "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.
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 real-time clock.
124 Setting this clock requires appropriate privileges.
125 .TP
126 .B CLOCK_MONOTONIC
127 Clock that cannot be set and represents monotonic time since
128 some unspecified starting point.
129 .TP
130 .BR CLOCK_MONOTONIC_RAW " (since Linux 2.6.28; Linux-specific)"
131 .\" Added in commit 2d42244ae71d6c7b0884b5664cf2eda30fb2ae68, John Stultz
132 Similar to
133 .BR CLOCK_MONOTONIC ,
134 but provides access to a raw hardware-based time
135 that is not subject to NTP adjustments.
136 .TP
137 .B CLOCK_PROCESS_CPUTIME_ID
138 High-resolution per-process timer from the CPU.
139 .TP
140 .B CLOCK_THREAD_CPUTIME_ID
141 Thread-specific CPU-time clock.
142 .SH "RETURN VALUE"
143 .BR clock_gettime (),
144 .BR clock_settime ()
145 and
146 .BR clock_getres ()
147 return 0 for success, or \-1 for failure (in which case
148 .I errno
149 is set appropriately).
150 .SH ERRORS
151 .TP
152 .B EFAULT
153 .I tp
154 points outside the accessible address space.
155 .TP
156 .B EINVAL
157 The
158 .I clk_id
159 specified is not supported on this system.
160 .\" Linux also gives this error on attempts to set CLOCK_PROCESS_CPUTIME_ID
161 .\" and CLOCK_THREAD_CPUTIME_ID, when probably the proper error should be
162 .\" EPERM.
163 .TP
164 .B EPERM
165 .BR clock_settime ()
166 does not have permission to set the clock indicated.
167 .SH "CONFORMING TO"
168 SUSv2, POSIX.1-2001.
169 .SH AVAILABILITY
170 On POSIX systems on which these functions are available, the symbol
171 .B _POSIX_TIMERS
172 is defined in \fI<unistd.h>\fP to a value greater than 0.
173 The symbols
174 .BR _POSIX_MONOTONIC_CLOCK ,
175 .BR _POSIX_CPUTIME ,
176 .B _POSIX_THREAD_CPUTIME
177 indicate that
178 .BR CLOCK_MONOTONIC ,
179 .BR CLOCK_PROCESS_CPUTIME_ID ,
180 .B CLOCK_THREAD_CPUTIME_ID
181 are available.
182 (See also
183 .BR sysconf (3).)
184 .SH NOTES
185 .SS Note for SMP systems
186 The
187 .B CLOCK_PROCESS_CPUTIME_ID
188 and
189 .B CLOCK_THREAD_CPUTIME_ID
190 clocks are realized on many platforms using timers from the CPUs
191 (TSC on i386, AR.ITC on Itanium).
192 These registers may differ between CPUs and as a consequence
193 these clocks may return
194 .B bogus results
195 if a process is migrated to another CPU.
196 .PP
197 If the CPUs in an SMP system have different clock sources then
198 there is no way to maintain a correlation between the timer registers since
199 each CPU will run at a slightly different frequency.
200 If that is the case then
201 .I clock_getcpuclockid(0)
202 will return
203 .B ENOENT
204 to signify this condition.
205 The two clocks will then only be useful if it
206 can be ensured that a process stays on a certain CPU.
207 .PP
208 The processors in an SMP system do not start all at exactly the same
209 time and therefore the timer registers are typically running at an offset.
210 Some architectures include code that attempts to limit these offsets on bootup.
211 However, the code cannot guarantee to accurately tune the offsets.
212 Glibc contains no provisions to deal with these offsets (unlike the Linux
213 Kernel).
214 Typically these offsets are small and therefore the effects may be
215 negligible in most cases.
216 .SH BUGS
217 According to POSIX.1-2001, a process with "appropriate privileges" may set the
218 .B CLOCK_PROCESS_CPUTIME_ID
219 and
220 .B CLOCK_THREAD_CPUTIME_ID
221 clocks using
222 .BR clock_settime ().
223 On Linux, these clocks are not settable
224 (i.e., no process has "appropriate privileges").
225 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=11972
226 .SH "SEE ALSO"
227 .BR date (1),
228 .BR adjtimex (2),
229 .BR gettimeofday (2),
230 .BR settimeofday (2),
231 .BR time (2),
232 .BR clock_getcpuclockid (3),
233 .BR ctime (3),
234 .BR ftime (3),
235 .BR pthread_getcpuclockid (3),
236 .BR sysconf (3),
237 .BR time (7)