man-pages/man2/clock_getres.2.html
2021-03-31 01:06:50 +01:00

532 lines
13 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of CLOCK_GETRES</TITLE>
</HEAD><BODY>
<H1>CLOCK_GETRES</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
clock_getres, clock_gettime, clock_settime - clock and time functions
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;</B>
<P>
<B>int clock_getres(clockid_t </B><I>clk_id</I><B>, struct timespec *</B><I>res</I><B>);</B>
<P>
<B>int clock_gettime(clockid_t </B><I>clk_id</I><B>, struct timespec *</B><I>tp</I><B>);</B>
<P>
<B>int clock_settime(clockid_t </B><I>clk_id</I><B>, const struct timespec *</B><I>tp</I><B>);</B>
<P>
Link with <I>-lrt</I> (only for glibc versions before 2.17).
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>clock_getres</B>(),
<B>clock_gettime</B>(),
<B>clock_settime</B>():
<DL COMPACT><DT id="1"><DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;199309L
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The function
<B>clock_getres</B>()
finds the resolution (precision) of the specified clock
<I>clk_id</I>,
and, if
<I>res</I>
is non-NULL, stores it in the <I>struct timespec</I> pointed to by
<I>res</I>.
The resolution of clocks depends on the implementation and cannot be
configured by a particular process.
If the time value pointed to by the argument
<I>tp</I>
of
<B>clock_settime</B>()
is not a multiple of
<I>res</I>,
then it is truncated to a multiple of
<I>res</I>.
<P>
The functions
<B>clock_gettime</B>()
and
<B>clock_settime</B>()
retrieve and set the time of the specified clock
<I>clk_id</I>.
<P>
The
<I>res</I>
and
<I>tp</I>
arguments are
<I>timespec</I>
structures, as specified in
<I>&lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;</I>:
<P>
struct timespec {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;time_t&nbsp;&nbsp;&nbsp;tv_sec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;seconds&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tv_nsec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;nanoseconds&nbsp;*/
};
<P>
The
<I>clk_id</I>
argument is the identifier of the particular clock on which to act.
A clock may be system-wide and hence visible for all processes, or
per-process if it measures time only within a single process.
<P>
All implementations support the system-wide real-time clock,
which is identified by
<B>CLOCK_REALTIME</B>.
Its time represents seconds and nanoseconds since the Epoch.
When its time is changed, timers for a relative interval are
unaffected, but timers for an absolute point in time are affected.
<P>
More clocks may be implemented.
The interpretation of the
corresponding time values and the effect on timers is unspecified.
<P>
Sufficiently recent versions of glibc and the Linux kernel
support the following clocks:
<DL COMPACT>
<DT id="2"><B>CLOCK_REALTIME</B>
<DD>
System-wide clock that measures real (i.e., wall-clock) time.
Setting this clock requires appropriate privileges.
This clock is affected by discontinuous jumps in the system time
(e.g., if the system administrator manually changes the clock),
and by the incremental adjustments performed by
<B><A HREF="/cgi-bin/man/man2html?3+adjtime">adjtime</A></B>(3)
and NTP.
<DT id="3"><B>CLOCK_REALTIME_COARSE</B> (since Linux 2.6.32; Linux-specific)
<DD>
A faster but less precise version of
<B>CLOCK_REALTIME</B>.
Use when you need very fast, but not fine-grained timestamps.
Requires per-architecture support,
and probably also architecture support for this flag in the
<B><A HREF="/cgi-bin/man/man2html?7+vdso">vdso</A></B>(7).
<DT id="4"><B>CLOCK_MONOTONIC</B>
<DD>
Clock that cannot be set and represents monotonic time since---as described
by POSIX---&quot;some unspecified point in the past&quot;.
On Linux, that point corresponds to the number of seconds that the system
has been running since it was booted.
<DT id="5"><DD>
The
<B>CLOCK_MONOTONIC</B>
clock is not affected by discontinuous jumps in the system time
(e.g., if the system administrator manually changes the clock),
but is affected by the incremental adjustments performed by
<B><A HREF="/cgi-bin/man/man2html?3+adjtime">adjtime</A></B>(3)
and NTP.
This clock does not count time that the system is suspended.
<DT id="6"><B>CLOCK_MONOTONIC_COARSE</B> (since Linux 2.6.32; Linux-specific)
<DD>
A faster but less precise version of
<B>CLOCK_MONOTONIC</B>.
Use when you need very fast, but not fine-grained timestamps.
Requires per-architecture support,
and probably also architecture support for this flag in the
<B><A HREF="/cgi-bin/man/man2html?7+vdso">vdso</A></B>(7).
<DT id="7"><B>CLOCK_MONOTONIC_RAW</B> (since Linux 2.6.28; Linux-specific)
<DD>
Similar to
<B>CLOCK_MONOTONIC</B>,
but provides access to a raw hardware-based time
that is not subject to NTP adjustments or
the incremental adjustments performed by
<B><A HREF="/cgi-bin/man/man2html?3+adjtime">adjtime</A></B>(3).
This clock does not count time that the system is suspended.
<DT id="8"><B>CLOCK_BOOTTIME</B> (since Linux 2.6.39; Linux-specific)
<DD>
Identical to
<B>CLOCK_MONOTONIC</B>,
except it also includes any time that the system is suspended.
This allows applications to get a suspend-aware monotonic clock
without having to deal with the complications of
<B>CLOCK_REALTIME</B>,
which may have discontinuities if the time is changed using
<B><A HREF="/cgi-bin/man/man2html?2+settimeofday">settimeofday</A></B>(2)
or similar.
<DT id="9"><B>CLOCK_PROCESS_CPUTIME_ID</B> (since Linux 2.6.12)
<DD>
Per-process CPU-time clock
(measures CPU time consumed by all threads in the process).
<DT id="10"><B>CLOCK_THREAD_CPUTIME_ID</B> (since Linux 2.6.12)
<DD>
Thread-specific CPU-time clock.
</DL>
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
<B>clock_gettime</B>(),
<B>clock_settime</B>(),
and
<B>clock_getres</B>()
return 0 for success, or -1 for failure (in which case
<I>errno</I>
is set appropriately).
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="11"><B>EFAULT</B>
<DD>
<I>tp</I>
points outside the accessible address space.
<DT id="12"><B>EINVAL</B>
<DD>
The
<I>clk_id</I>
specified is not supported on this system.
<DT id="13"><B>EINVAL</B>
<DD>
(<B>clock_settime</B>()):
<I>tp.tv_sec</I>
is negative or
<I>tp.tv_nsec</I>
is outside the range [0..999,999,999].
<DT id="14"><B>EINVAL</B> (since Linux 4.3)
<DD>
A call to
<B>clock_settime</B>()
with a
<I>clk_id</I>
of
<B>CLOCK_REALTIME</B>
attempted to set the time to a value less than
the current value of the
<B>CLOCK_MONOTONIC</B>
clock.
<DT id="15"><B>EPERM</B>
<DD>
<B>clock_settime</B>()
does not have permission to set the clock indicated.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
These system calls first appeared in Linux 2.6.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>clock_getres</B>(),
<B>clock_gettime</B>(),
<B>clock_settime</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, SUSv2.
<A NAME="lbAJ">&nbsp;</A>
<H2>AVAILABILITY</H2>
On POSIX systems on which these functions are available, the symbol
<B>_POSIX_TIMERS</B>
is defined in <I>&lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</I> to a value greater than 0.
The symbols
<B>_POSIX_MONOTONIC_CLOCK</B>,
<B>_POSIX_CPUTIME</B>,
<B>_POSIX_THREAD_CPUTIME</B>
indicate that
<B>CLOCK_MONOTONIC</B>,
<B>CLOCK_PROCESS_CPUTIME_ID</B>,
<B>CLOCK_THREAD_CPUTIME_ID</B>
are available.
(See also
<B><A HREF="/cgi-bin/man/man2html?3+sysconf">sysconf</A></B>(3).)
<A NAME="lbAK">&nbsp;</A>
<H2>NOTES</H2>
POSIX.1 specifies the following:
<DL COMPACT><DT id="16"><DD>
<P>
Setting the value of the
<B>CLOCK_REALTIME</B>
clock via
<B>clock_settime</B>()
shall have no effect on threads that are blocked waiting for a relative time
service based upon this clock, including the
<B>nanosleep</B>()
function; nor on the expiration of relative timers based upon this clock.
Consequently, these time services shall expire when the requested relative
interval elapses, independently of the new or old value of the clock.
</DL>
<A NAME="lbAL">&nbsp;</A>
<H3>C library/kernel differences</H3>
On some architectures, an implementation of
<B>clock_gettime</B>()
is provided in the
<B><A HREF="/cgi-bin/man/man2html?7+vdso">vdso</A></B>(7).
<A NAME="lbAM">&nbsp;</A>
<H3>Historical note for SMP systems</H3>
Before Linux added kernel support for
<B>CLOCK_PROCESS_CPUTIME_ID</B>
and
<B>CLOCK_THREAD_CPUTIME_ID</B>,
glibc implemented these clocks on many platforms using timer
registers from the CPUs
(TSC on i386, AR.ITC on Itanium).
These registers may differ between CPUs and as a consequence
these clocks may return
<B>bogus results</B>
if a process is migrated to another CPU.
<P>
If the CPUs in an SMP system have different clock sources, then
there is no way to maintain a correlation between the timer registers since
each CPU will run at a slightly different frequency.
If that is the case, then
<I>clock_getcpuclockid(0)</I>
will return
<B>ENOENT</B>
to signify this condition.
The two clocks will then be useful only if it
can be ensured that a process stays on a certain CPU.
<P>
The processors in an SMP system do not start all at exactly the same
time and therefore the timer registers are typically running at an offset.
Some architectures include code that attempts to limit these offsets on bootup.
However, the code cannot guarantee to accurately tune the offsets.
Glibc contains no provisions to deal with these offsets (unlike the Linux
Kernel).
Typically these offsets are small and therefore the effects may be
negligible in most cases.
<P>
Since glibc 2.4,
the wrapper functions for the system calls described in this page avoid
the abovementioned problems by employing the kernel implementation of
<B>CLOCK_PROCESS_CPUTIME_ID</B>
and
<B>CLOCK_THREAD_CPUTIME_ID</B>,
on systems that provide such an implementation
(i.e., Linux 2.6.12 and later).
<A NAME="lbAN">&nbsp;</A>
<H2>BUGS</H2>
According to POSIX.1-2001, a process with &quot;appropriate privileges&quot; may set the
<B>CLOCK_PROCESS_CPUTIME_ID</B>
and
<B>CLOCK_THREAD_CPUTIME_ID</B>
clocks using
<B>clock_settime</B>().
On Linux, these clocks are not settable
(i.e., no process has &quot;appropriate privileges&quot;).
<A NAME="lbAO">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+date">date</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+gettimeofday">gettimeofday</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+settimeofday">settimeofday</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+time">time</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+adjtime">adjtime</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+clock_getcpuclockid">clock_getcpuclockid</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+ctime">ctime</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+ftime">ftime</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_getcpuclockid">pthread_getcpuclockid</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sysconf">sysconf</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+time">time</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?7+vdso">vdso</A></B>(7),
<B><A HREF="/cgi-bin/man/man2html?8+hwclock">hwclock</A></B>(8)
<A NAME="lbAP">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="17"><A HREF="#lbAB">NAME</A><DD>
<DT id="18"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="19"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="20"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="21"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="22"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="23"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="24"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="25"><A HREF="#lbAJ">AVAILABILITY</A><DD>
<DT id="26"><A HREF="#lbAK">NOTES</A><DD>
<DL>
<DT id="27"><A HREF="#lbAL">C library/kernel differences</A><DD>
<DT id="28"><A HREF="#lbAM">Historical note for SMP systems</A><DD>
</DL>
<DT id="29"><A HREF="#lbAN">BUGS</A><DD>
<DT id="30"><A HREF="#lbAO">SEE ALSO</A><DD>
<DT id="31"><A HREF="#lbAP">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:32 GMT, March 31, 2021
</BODY>
</HTML>