1 /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version.
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include "kernel-posix-cpu-timers.h"
24 #include <bits/kernel-features.h>
27 #define SYSCALL_GETTIME \
28 retval = INLINE_SYSCALL (clock_gettime, 2, clock_id, tp); \
31 #ifdef __ASSUME_POSIX_TIMERS
33 /* This means the REALTIME and MONOTONIC clock are definitely
34 supported in the kernel. */
35 # define SYSDEP_GETTIME \
36 SYSDEP_GETTIME_CPUTIME \
37 case CLOCK_REALTIME: \
38 case CLOCK_MONOTONIC: \
41 # define __libc_missing_posix_timers 0
42 #elif defined __NR_clock_gettime
43 /* Is the syscall known to exist? */
44 int __libc_missing_posix_timers attribute_hidden
;
47 maybe_syscall_gettime (clockid_t clock_id
, struct timespec
*tp
)
51 if (!__libc_missing_posix_timers
)
53 INTERNAL_SYSCALL_DECL (err
);
54 int r
= INTERNAL_SYSCALL (clock_gettime
, err
, 2, clock_id
, tp
);
55 if (!INTERNAL_SYSCALL_ERROR_P (r
, err
))
58 e
= INTERNAL_SYSCALL_ERRNO (r
, err
);
61 __libc_missing_posix_timers
= 1;
69 /* The REALTIME and MONOTONIC clock might be available. Try the
71 # define SYSDEP_GETTIME \
72 SYSDEP_GETTIME_CPUTIME \
73 case CLOCK_REALTIME: \
74 case CLOCK_MONOTONIC: \
75 retval = maybe_syscall_gettime (clock_id, tp); \
78 /* Fallback code. */ \
79 if (retval == EINVAL && clock_id == CLOCK_REALTIME) \
80 retval = realtime_gettime (tp); \
83 __set_errno (retval); \
89 #ifdef __NR_clock_gettime
90 /* We handled the REALTIME clock here. */
91 # define HANDLED_REALTIME 1
92 # define HANDLED_CPUTIME 1
94 # if __ASSUME_POSIX_CPU_TIMERS > 0
96 # define SYSDEP_GETTIME_CPU SYSCALL_GETTIME
97 # define SYSDEP_GETTIME_CPUTIME /* Default catches them too. */
101 int __libc_missing_posix_cpu_timers attribute_hidden
;
104 maybe_syscall_gettime_cpu (clockid_t clock_id
, struct timespec
*tp
)
108 if (!__libc_missing_posix_cpu_timers
)
110 INTERNAL_SYSCALL_DECL (err
);
111 int r
= INTERNAL_SYSCALL (clock_gettime
, err
, 2, clock_id
, tp
);
112 if (!INTERNAL_SYSCALL_ERROR_P (r
, err
))
115 e
= INTERNAL_SYSCALL_ERRNO (r
, err
);
116 # ifndef __ASSUME_POSIX_TIMERS
119 __libc_missing_posix_timers
= 1;
120 __libc_missing_posix_cpu_timers
= 1;
128 /* Check whether the kernel supports CPU clocks at all.
129 If not, record it for the future. */
130 r
= INTERNAL_SYSCALL (clock_getres
, err
, 2,
131 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED
),
133 if (INTERNAL_SYSCALL_ERROR_P (r
, err
))
134 __libc_missing_posix_cpu_timers
= 1;
142 # define SYSDEP_GETTIME_CPU \
143 retval = maybe_syscall_gettime_cpu (clock_id, tp); \
146 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
148 __set_errno (retval); \
152 retval = -1 /* Otherwise continue on to the HP_TIMING version. */;
155 maybe_syscall_gettime_cputime (clockid_t clock_id
, struct timespec
*tp
)
157 return maybe_syscall_gettime_cpu
158 (clock_id
== CLOCK_THREAD_CPUTIME_ID
159 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED
)
160 : MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED
),
164 # define SYSDEP_GETTIME_CPUTIME \
165 case CLOCK_PROCESS_CPUTIME_ID: \
166 case CLOCK_THREAD_CPUTIME_ID: \
167 retval = maybe_syscall_gettime_cputime (clock_id, tp); \
170 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
172 __set_errno (retval); \
176 retval = hp_timing_gettime (clock_id, tp); \
178 # if !HP_TIMING_AVAIL
179 # define hp_timing_gettime(clock_id, tp) (__set_errno (EINVAL), -1)
188 #include <sys/time.h>
189 #include <libc-internal.h>
190 #include <ldsodefs.h>
194 /* Clock frequency of the processor. We make it a 64-bit variable
195 because some jokers are already playing with processors with more
197 static hp_timing_t freq
;
200 /* This function is defined in the thread library. */
201 extern int __pthread_clock_gettime (clockid_t clock_id
, hp_timing_t freq
,
203 __attribute__ ((__weak__
));
206 hp_timing_gettime (clockid_t clock_id
, struct timespec
*tp
)
210 if (__builtin_expect (freq
== 0, 0))
212 /* This can only happen if we haven't initialized the `freq'
213 variable yet. Do this now. We don't have to protect this
214 code against multiple execution since all of them should
215 lead to the same result. */
216 freq
= __get_clockfreq ();
217 if (__builtin_expect (freq
== 0, 0))
218 /* Something went wrong. */
222 if (clock_id
!= CLOCK_PROCESS_CPUTIME_ID
223 && __pthread_clock_gettime
!= NULL
)
224 return __pthread_clock_gettime (clock_id
, freq
, tp
);
226 /* Get the current counter. */
229 /* Compute the offset since the start time of the process. */
230 tsc
-= GL(dl_cpuclock_offset
);
232 /* Compute the seconds. */
233 tp
->tv_sec
= tsc
/ freq
;
235 /* And the nanoseconds. This computation should be stable until
236 we get machines with about 16GHz frequency. */
237 tp
->tv_nsec
= ((tsc
% freq
) * UINT64_C (1000000000)) / freq
;
245 realtime_gettime (struct timespec
*tp
)
248 int retval
= gettimeofday (&tv
, NULL
);
250 /* Convert into `timespec'. */
251 TIMEVAL_TO_TIMESPEC (&tv
, tp
);
255 librt_hidden_proto (clock_gettime
)
256 /* Get current value of CLOCK and store it in TP. */
258 clock_gettime (clockid_t clock_id
, struct timespec
*tp
)
261 #ifndef HANDLED_REALTIME
267 #ifdef SYSDEP_GETTIME
271 #ifndef HANDLED_REALTIME
273 retval
= gettimeofday (&tv
, NULL
);
275 TIMEVAL_TO_TIMESPEC (&tv
, tp
);
280 #ifdef SYSDEP_GETTIME_CPU
284 if ((clock_id
& ((1 << CLOCK_IDFIELD_SIZE
) - 1))
285 == CLOCK_THREAD_CPUTIME_ID
)
286 retval
= hp_timing_gettime (clock_id
, tp
);
289 __set_errno (EINVAL
);
292 #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
293 case CLOCK_PROCESS_CPUTIME_ID
:
294 retval
= hp_timing_gettime (clock_id
, tp
);
301 librt_hidden_def (clock_gettime
)