public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v1] x86: Add version of sched_getcpu that uses `rdpid`
@ 2022-02-21  3:12 Noah Goldstein
  2022-02-21  8:31 ` Florian Weimer
  0 siblings, 1 reply; 2+ messages in thread
From: Noah Goldstein @ 2022-02-21  3:12 UTC (permalink / raw)
  To: libc-alpha

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-02-21  8:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-21  3:12 [PATCH v1] x86: Add version of sched_getcpu that uses `rdpid` Noah Goldstein
2022-02-21  8:31 ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).