public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v1 1/2] x86: Implement sched_yield syscall for x86 only.
@ 2023-06-08  9:00 Noah Goldstein
  2023-06-08  9:00 ` [PATCH v1 2/2] x86: Implement clock_nanosleep{_time64} " Noah Goldstein
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Noah Goldstein @ 2023-06-08  9:00 UTC (permalink / raw)
  To: libc-alpha; +Cc: goldstein.w.n, hjl.tools, carlos

We slightly optimize it by using `vzeroall` before the actual syscall.
This returns the SSE, AVX, and ZMM_HI256 xsave/xrstor states to the
init-state which allows the imminent context switch to skip
saving/restoring those states.
---
 .../unix/sysv/linux/x86_64/sched-yield-impl.h | 29 ++++++++++
 sysdeps/unix/sysv/linux/x86_64/sched_yield.c  | 56 +++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/x86_64/sched-yield-impl.h
 create mode 100644 sysdeps/unix/sysv/linux/x86_64/sched_yield.c

diff --git a/sysdeps/unix/sysv/linux/x86_64/sched-yield-impl.h b/sysdeps/unix/sysv/linux/x86_64/sched-yield-impl.h
new file mode 100644
index 0000000000..03622ccea4
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86_64/sched-yield-impl.h
@@ -0,0 +1,29 @@
+/* Yield current process.  Linux specific syscall.
+   Copyright (C) 2023 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/>.  */
+
+#include <sysdep.h>
+
+static int TARGET
+SCHED_YIELD (void)
+{
+  PREPARE_CONTEXT_SWITCH ();
+  return INLINE_SYSCALL_CALL (sched_yield);
+}
+#undef TARGET
+#undef SCHED_YIELD
+#undef PREPARE_CONTEXT_SWITCH
diff --git a/sysdeps/unix/sysv/linux/x86_64/sched_yield.c b/sysdeps/unix/sysv/linux/x86_64/sched_yield.c
new file mode 100644
index 0000000000..e87acf124b
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86_64/sched_yield.c
@@ -0,0 +1,56 @@
+/* clock_nanosleep for x86_64.
+   Copyright (C) 2023 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/>.  */
+
+/* Only difference is if we have AVX, use vzeroall to clear inuse for SSE, AVX,
+   and ZMM_HI256 xsave/xrstor state.  This enables the init-state optimization
+   saving overhead on context switches.  */
+
+#include <isa-level.h>
+#if ISA_SHOULD_BUILD(4)
+# include <immintrin.h>
+# define TARGET __attribute__ ((target ("avx")))
+# define PREPARE_CONTEXT_SWITCH() _mm256_zeroall ()
+# define SCHED_YIELD __sched_yield_avx
+# include "sched-yield-impl.h"
+#endif
+#if ISA_SHOULD_BUILD(2)
+# define TARGET
+# define PREPARE_CONTEXT_SWITCH()
+# define SCHED_YIELD __sched_yield_generic
+# include "sched-yield-impl.h"
+#endif
+
+#include <init-arch.h>
+#include <ifunc-init.h>
+
+static inline void *
+__sched_yield_ifunc_selector (void)
+{
+#if MINIMUM_X86_ISA_LEVEL >= 3
+  return __sched_yield_avx;
+#else
+  const struct cpu_features *cpu_features = __get_cpu_features ();
+  if (X86_ISA_CPU_FEATURE_USABLE_P (cpu_features, AVX))
+    return __sched_yield_avx;
+  return __sched_yield_generic;
+#endif
+}
+
+libc_ifunc (__sched_yield, __sched_yield_ifunc_selector ());
+libc_hidden_def (__sched_yield);
+weak_alias (__sched_yield, sched_yield);
-- 
2.34.1


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

end of thread, other threads:[~2023-06-10  4:59 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-08  9:00 [PATCH v1 1/2] x86: Implement sched_yield syscall for x86 only Noah Goldstein
2023-06-08  9:00 ` [PATCH v1 2/2] x86: Implement clock_nanosleep{_time64} " Noah Goldstein
2023-06-08 10:13 ` [PATCH v1 1/2] x86: Implement sched_yield " Gabriel Ravier
2023-06-08 17:43   ` Noah Goldstein
2023-06-08 11:43 ` Florian Weimer
2023-06-08 12:08   ` Adhemerval Zanella Netto
2023-06-08 17:39     ` Noah Goldstein
2023-06-08 18:26       ` Zack Weinberg
2023-06-08 19:41         ` Florian Weimer
2023-06-08 19:53           ` Noah Goldstein
2023-06-08 20:22             ` Zack Weinberg
2023-06-08 20:38               ` Noah Goldstein
2023-06-08 20:44                 ` Zack Weinberg
2023-06-08 21:06                   ` Noah Goldstein
2023-06-08 21:25                     ` Florian Weimer
2023-06-09  5:59                       ` Zack Weinberg
2023-06-10  1:11                         ` Noah Goldstein
2023-06-10  2:07                           ` Gabriel Ravier
2023-06-10  4:59                             ` Noah Goldstein

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