[PATCH v1] x86: Add version of sched_getcpu that uses `rdpid`
Noah Goldstein
goldstein.w.n@gmail.com
Mon Feb 21 03:12:16 GMT 2022
The `rdpid` version is slightly faster:
Measured on Tigerlake 2.8 GHz
rdpid -> 0.215 Calls / ns
rseq -> 0.312 Calls / ns
tst-skeleton-thread-affinity and tst-skeleton-affinity pass w and w.o
multiarch.
---
sysdeps/unix/sysv/linux/sched_getcpu.c | 7 +++-
sysdeps/unix/sysv/linux/x86/sched_getcpu.c | 48 ++++++++++++++++++++++
2 files changed, 53 insertions(+), 2 deletions(-)
create mode 100644 sysdeps/unix/sysv/linux/x86/sched_getcpu.c
diff --git a/sysdeps/unix/sysv/linux/sched_getcpu.c b/sysdeps/unix/sysv/linux/sched_getcpu.c
index 5c3301004c..e659aeaaf8 100644
--- a/sysdeps/unix/sysv/linux/sched_getcpu.c
+++ b/sysdeps/unix/sysv/linux/sched_getcpu.c
@@ -35,15 +35,18 @@ vsyscall_sched_getcpu (void)
#ifdef RSEQ_SIG
int
-sched_getcpu (void)
+__sched_getcpu (void)
{
int cpu_id = THREAD_GETMEM_VOLATILE (THREAD_SELF, rseq_area.cpu_id);
return __glibc_likely (cpu_id >= 0) ? cpu_id : vsyscall_sched_getcpu ();
}
#else /* RSEQ_SIG */
int
-sched_getcpu (void)
+__sched_getcpu (void)
{
return vsyscall_sched_getcpu ();
}
#endif /* RSEQ_SIG */
+#ifndef USE_IFUNC_SCHED_GETCPU
+weak_alias (__sched_getcpu, sched_getcpu)
+#endif /* !USE_IFUNC_SCHED_GETCPU */
diff --git a/sysdeps/unix/sysv/linux/x86/sched_getcpu.c b/sysdeps/unix/sysv/linux/x86/sched_getcpu.c
new file mode 100644
index 0000000000..38afed78c6
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86/sched_getcpu.c
@@ -0,0 +1,48 @@
+/* sched_getcpu -- Get current processor. Linux/x86 version.
+ Copyright (C) 2022 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+
+
+#if defined __x86_64__ && defined USE_MULTIARCH && IS_IN (libc)
+# define USE_IFUNC_SCHED_GETCPU
+
+# include <init-arch.h>
+int __sched_getcpu (void);
+
+int
+__sched_getcpu_rdpid (void)
+{
+# if __has_builtin (__builtin_ia32_rdpid)
+ return __builtin_ia32_rdpid();
+# else
+ unsigned long cpu;
+ asm volatile("rdpid %[cpu]" : [cpu] "=r" (cpu) : :);
+ return cpu;
+# endif
+}
+
+# undef INIT_ARCH
+# define INIT_ARCH() \
+ const struct cpu_features *cpu_features = __get_cpu_features ();
+
+libc_ifunc (sched_getcpu, CPU_FEATURE_USABLE_P (cpu_features, RDPID)
+ ? (void *) __sched_getcpu_rdpid
+ : (void *) __sched_getcpu)
+
+#endif
+#include <sysdeps/unix/sysv/linux/sched_getcpu.c>
--
2.25.1
More information about the Libc-alpha
mailing list