From patchwork Wed Apr 18 20:17:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Albert ARIBAUD X-Patchwork-Id: 26796 Received: (qmail 106390 invoked by alias); 18 Apr 2018 20:19:49 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 106276 invoked by uid 89); 18 Apr 2018 20:19:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, KAM_NUMSUBJECT, RCVD_IN_DNSWL_LOW, RCVD_IN_RP_RNBL autolearn=ham version=3.3.2 spammy=Fill, 1000000000 X-HELO: smtp3-g21.free.fr From: "Albert ARIBAUD (3ADEV)" To: libc-alpha@sourceware.org Cc: "Albert ARIBAUD (3ADEV)" Subject: [[PATCH RFC 2] 17/63] Y2038: add function __clock_getres64 Date: Wed, 18 Apr 2018 22:17:33 +0200 Message-Id: <20180418201819.15952-18-albert.aribaud@3adev.fr> In-Reply-To: <20180418201819.15952-17-albert.aribaud@3adev.fr> References: <20180418201819.15952-1-albert.aribaud@3adev.fr> <20180418201819.15952-2-albert.aribaud@3adev.fr> <20180418201819.15952-3-albert.aribaud@3adev.fr> <20180418201819.15952-4-albert.aribaud@3adev.fr> <20180418201819.15952-5-albert.aribaud@3adev.fr> <20180418201819.15952-6-albert.aribaud@3adev.fr> <20180418201819.15952-7-albert.aribaud@3adev.fr> <20180418201819.15952-8-albert.aribaud@3adev.fr> <20180418201819.15952-9-albert.aribaud@3adev.fr> <20180418201819.15952-10-albert.aribaud@3adev.fr> <20180418201819.15952-11-albert.aribaud@3adev.fr> <20180418201819.15952-12-albert.aribaud@3adev.fr> <20180418201819.15952-13-albert.aribaud@3adev.fr> <20180418201819.15952-14-albert.aribaud@3adev.fr> <20180418201819.15952-15-albert.aribaud@3adev.fr> <20180418201819.15952-16-albert.aribaud@3adev.fr> <20180418201819.15952-17-albert.aribaud@3adev.fr> --- include/time.h | 2 + sysdeps/posix/clock_getres.c | 96 +++++++++++++++++++++++++++++++++- sysdeps/unix/sysv/linux/clock_getres.c | 60 +++++++++++++++++++++ time/Versions | 1 + 4 files changed, 158 insertions(+), 1 deletion(-) diff --git a/include/time.h b/include/time.h index 8101b638c6..37909fae9a 100644 --- a/include/time.h +++ b/include/time.h @@ -37,6 +37,8 @@ extern int __clock_gettime64 (clockid_t __clock_id, struct __timespec64 *__tp) __THROW; extern int __clock_settime64 (clockid_t __clock_id, const struct __timespec64 *__tp) __THROW; +extern int __clock_getres64 (clockid_t __clock_id, + struct __timespec64 *__res) __THROW; /* Now define the internal interfaces. */ struct tm; diff --git a/sysdeps/posix/clock_getres.c b/sysdeps/posix/clock_getres.c index e7924e0891..28b16a6529 100644 --- a/sysdeps/posix/clock_getres.c +++ b/sysdeps/posix/clock_getres.c @@ -23,7 +23,6 @@ #include #include - #if HP_TIMING_AVAIL static long int nsec; /* Clock frequency of the processor. */ @@ -53,6 +52,33 @@ hp_timing_getres (struct timespec *res) return 0; } + +static int +hp_timing_getres64 (struct __timespec64 *res) +{ + if (__glibc_unlikely (nsec == 0)) + { + hp_timing_t freq; + + /* This can only happen if we haven't initialized the `nsec' + variable yet. Do this now. We don't have to protect this + code against multiple execution since all of them should + lead to the same result. */ + freq = __get_clockfreq (); + if (__glibc_unlikely (freq == 0)) + /* Something went wrong. */ + return -1; + + nsec = MAX (UINT64_C (1000000000) / freq, 1); + } + + /* Fill in the values. + The seconds are always zero (unless we have a 1Hz machine). */ + res->tv_sec = 0; + res->tv_nsec = nsec; + + return 0; +} #endif static inline int @@ -73,6 +99,28 @@ realtime_getres (struct timespec *res) return -1; } +/* Check that we are built with a 64-bit-time kernel */ +#ifdef __NR_clock_getres64 + +static inline int +realtime_getres64 (struct __timespec64 *res) +{ + long int clk_tck = sysconf (_SC_CLK_TCK); + + if (__glibc_likely (clk_tck != -1)) + { + /* This implementation assumes that the realtime clock has a + resolution higher than 1 second. This is the case for any + reasonable implementation. */ + res->tv_sec = 0; + res->tv_nsec = 1000000000 / clk_tck; + return 0; + } + + return -1; +} + +#endif /* Get resolution of clock. */ int @@ -116,3 +164,49 @@ __clock_getres (clockid_t clock_id, struct timespec *res) return retval; } weak_alias (__clock_getres, clock_getres) + +int +__clock_getres64 (clockid_t clock_id, struct __timespec64 *res) +{ + int retval = -1; + + switch (clock_id) + { +#ifdef SYSDEP_GETRES64 + SYSDEP_GETRES64; +#endif + +/* Check that we are built with a 64-bit-time kernel */ +#ifdef __NR_clock_getres64 + +# ifndef HANDLED_REALTIME64 + case CLOCK_REALTIME64: + retval = realtime_getres64 (res); + break; +# endif /* handled REALTIME */ + +#endif + + default: +#ifdef SYSDEP_GETRES_CPU64 + SYSDEP_GETRES_CPU64; +#endif +#if HP_TIMING_AVAIL + if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1)) + == CLOCK_THREAD_CPUTIME_ID) + retval = hp_timing_getres64 (res); + else +#endif + __set_errno (EINVAL); + break; + +#if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME + case CLOCK_PROCESS_CPUTIME_ID: + case CLOCK_THREAD_CPUTIME_ID: + retval = hp_timing_getres64 (res); + break; +#endif + } + + return retval; +} diff --git a/sysdeps/unix/sysv/linux/clock_getres.c b/sysdeps/unix/sysv/linux/clock_getres.c index 5d94f59afe..ecea873afe 100644 --- a/sysdeps/unix/sysv/linux/clock_getres.c +++ b/sysdeps/unix/sysv/linux/clock_getres.c @@ -48,4 +48,64 @@ #define SYSDEP_GETRES_CPU SYSCALL_GETRES #define SYSDEP_GETRES_CPUTIME /* Default catches them too. */ +/* The 64-bit version */ + +/* Check that we are built with a 64-bit-time kernel */ +#ifdef __NR_clock_getres64 + +extern int __y2038_linux_support; + +#define SYSCALL_GETRES64 \ + if (__y2038_linux_support) \ + { \ + retval = INLINE_VSYSCALL (clock_getres64, 2, clock_id, res); \ + } \ + else \ + { \ + retval = -1; \ + errno = ENOSYS; \ + } \ + if (retval == -1 && errno == ENOSYS) \ + { \ + retval = INLINE_VSYSCALL (clock_getres, 2, clock_id, &ts32); \ + if (retval==0) \ + { \ + timespec_to_timespec64(&ts32, res); \ + res->tv_pad = 0; \ + } \ + } \ + break + +#else + +#define SYSCALL_GETRES64 \ + retval = INLINE_VSYSCALL (clock_getres, 2, clock_id, &ts32); \ + if (retval==0) \ + { \ + timespec_to_timespec64(&ts32, res); \ + res->tv_pad = 0; \ + } \ + break + +#endif + +/* The REALTIME and MONOTONIC clock are definitely supported in the + kernel. */ +#define SYSDEP_GETRES64 \ + SYSDEP_GETRES_CPUTIME64 \ + case CLOCK_REALTIME: \ + case CLOCK_MONOTONIC: \ + case CLOCK_MONOTONIC_RAW: \ + case CLOCK_REALTIME_COARSE: \ + case CLOCK_MONOTONIC_COARSE: \ + SYSCALL_GETRES64 + +/* We handled the REALTIME clock here. */ +#define HANDLED_REALTIME64 1 +#define HANDLED_CPUTIME64 1 + +#define SYSDEP_GETRES_CPU64 SYSCALL_GETRES64 +#define SYSDEP_GETRES_CPUTIME64 \ + struct timespec ts32; + #include diff --git a/time/Versions b/time/Versions index a00184daae..98ac0abfbe 100644 --- a/time/Versions +++ b/time/Versions @@ -76,5 +76,6 @@ libc { __vdso_clock_gettime64; __y2038_kernel_support; __clock_settime64; + __clock_getres64; } }