1CLOCK_GETRES(2)            Linux Programmer's Manual           CLOCK_GETRES(2)
2
3
4

NAME

6       clock_getres, clock_gettime, clock_settime - clock and time functions
7

SYNOPSIS

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.
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

DESCRIPTION

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 real-time clock.  Setting this clock requires appro‐
62              priate privileges.
63
64       CLOCK_MONOTONIC
65              Clock  that  cannot  be  set and represents monotonic time since
66              some unspecified starting point.
67
68       CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
69              Similar to CLOCK_MONOTONIC, but provides access to a  raw  hard‐
70              ware-based time that is not subject to NTP adjustments.
71
72       CLOCK_PROCESS_CPUTIME_ID
73              High-resolution per-process timer from the CPU.
74
75       CLOCK_THREAD_CPUTIME_ID
76              Thread-specific CPU-time clock.
77

RETURN VALUE

79       clock_gettime(),  clock_settime()  and clock_getres() return 0 for suc‐
80       cess, or -1 for failure (in which case errno is set appropriately).
81

ERRORS

83       EFAULT tp points outside the accessible address space.
84
85       EINVAL The clk_id specified is not supported on this system.
86
87       EPERM  clock_settime() does not have permission to set the clock  indi‐
88              cated.
89

CONFORMING TO

91       SUSv2, POSIX.1-2001.
92

AVAILABILITY

94       On  POSIX  systems  on  which these functions are available, the symbol
95       _POSIX_TIMERS is defined in <unistd.h> to a value greater than 0.   The
96       symbols  _POSIX_MONOTONIC_CLOCK,  _POSIX_CPUTIME, _POSIX_THREAD_CPUTIME
97       indicate      that      CLOCK_MONOTONIC,      CLOCK_PROCESS_CPUTIME_ID,
98       CLOCK_THREAD_CPUTIME_ID are available.  (See also sysconf(3).)
99

NOTES

101   Note for SMP systems
102       The  CLOCK_PROCESS_CPUTIME_ID  and  CLOCK_THREAD_CPUTIME_ID  clocks are
103       realized on many platforms using timers from the  CPUs  (TSC  on  i386,
104       AR.ITC  on  Itanium).  These registers may differ between CPUs and as a
105       consequence these clocks may return  bogus  results  if  a  process  is
106       migrated to another CPU.
107
108       If the CPUs in an SMP system have different clock sources then there is
109       no way to maintain a correlation between the timer registers since each
110       CPU  will  run  at a slightly different frequency.  If that is the case
111       then clock_getcpuclockid(0) will return ENOENT to signify  this  condi‐
112       tion.   The  two  clocks  will then only be useful if it can be ensured
113       that a process stays on a certain CPU.
114
115       The processors in an SMP system do not start all at  exactly  the  same
116       time and therefore the timer registers are typically running at an off‐
117       set.  Some architectures include code that attempts to limit these off‐
118       sets  on bootup.  However, the code cannot guarantee to accurately tune
119       the offsets.  Glibc contains no provisions to deal with  these  offsets
120       (unlike  the  Linux  Kernel).   Typically  these  offsets are small and
121       therefore the effects may be negligible in most cases.
122

BUGS

124       According   to   POSIX.1-2001,   the    CLOCK_PROCESS_CPUTIME_ID    and
125       CLOCK_THREAD_CPUTIME_ID  clocks  should  be  settable  using clock_set‐
126       time().  However, the clocks currently are not settable.
127

SEE ALSO

129       date(1),  adjtimex(2),   gettimeofday(2),   settimeofday(2),   time(2),
130       clock_getcpuclockid(3),  ctime(3),  ftime(3), pthread_getcpuclockid(3),
131       sysconf(3), time(7)
132

COLOPHON

134       This page is part of release 3.22 of the Linux  man-pages  project.   A
135       description  of  the project, and information about reporting bugs, can
136       be found at http://www.kernel.org/doc/man-pages/.
137
138
139
140                                  2009-02-05                   CLOCK_GETRES(2)
Impressum