6ca6e8
commit 04efdcfac405723c23b25d124817bcfc1697e2d8
6ca6e8
Author: Noah Goldstein <goldstein.w.n@gmail.com>
6ca6e8
Date:   Wed Apr 27 15:13:02 2022 -0500
6ca6e8
6ca6e8
    sysdeps: Add 'get_fast_jitter' interace in fast-jitter.h
6ca6e8
    
6ca6e8
    'get_fast_jitter' is meant to be used purely for performance
6ca6e8
    purposes. In all cases it's used it should be acceptable to get no
6ca6e8
    randomness (see default case). An example use case is in setting
6ca6e8
    jitter for retries between threads at a lock. There is a
6ca6e8
    performance benefit to having jitter, but only if the jitter can
6ca6e8
    be generated very quickly and ultimately there is no serious issue
6ca6e8
    if no jitter is generated.
6ca6e8
    
6ca6e8
    The implementation generally uses 'HP_TIMING_NOW' iff it is
6ca6e8
    inlined (avoid any potential syscall paths).
6ca6e8
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
6ca6e8
    
6ca6e8
    (cherry picked from commit 911c63a51c690dd1a97dfc587097277029baf00f)
6ca6e8
6ca6e8
diff --git a/sysdeps/generic/fast-jitter.h b/sysdeps/generic/fast-jitter.h
6ca6e8
new file mode 100644
6ca6e8
index 0000000000000000..4dd53e3475c3dfe6
6ca6e8
--- /dev/null
6ca6e8
+++ b/sysdeps/generic/fast-jitter.h
6ca6e8
@@ -0,0 +1,42 @@
6ca6e8
+/* Fallback for fast jitter just return 0.
6ca6e8
+   Copyright (C) 2019-2022 Free Software Foundation, Inc.
6ca6e8
+   This file is part of the GNU C Library.
6ca6e8
+
6ca6e8
+   The GNU C Library is free software; you can redistribute it and/or
6ca6e8
+   modify it under the terms of the GNU Lesser General Public
6ca6e8
+   License as published by the Free Software Foundation; either
6ca6e8
+   version 2.1 of the License, or (at your option) any later version.
6ca6e8
+
6ca6e8
+   The GNU C Library is distributed in the hope that it will be useful,
6ca6e8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
6ca6e8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6ca6e8
+   Lesser General Public License for more details.
6ca6e8
+
6ca6e8
+   You should have received a copy of the GNU Lesser General Public
6ca6e8
+   License along with the GNU C Library; if not, see
6ca6e8
+   <https://www.gnu.org/licenses/>.  */
6ca6e8
+
6ca6e8
+#ifndef _FAST_JITTER_H
6ca6e8
+# define _FAST_JITTER_H
6ca6e8
+
6ca6e8
+# include <stdint.h>
6ca6e8
+# include <hp-timing.h>
6ca6e8
+
6ca6e8
+/* Baseline just return 0.  We could create jitter using a clock or
6ca6e8
+   'random_bits' but that may imply a syscall and the goal of
6ca6e8
+   'get_fast_jitter' is minimal overhead "randomness" when such
6ca6e8
+   randomness helps performance.  Adding high overhead the function
6ca6e8
+   defeats the purpose.  */
6ca6e8
+static inline uint32_t
6ca6e8
+get_fast_jitter (void)
6ca6e8
+{
6ca6e8
+# if HP_TIMING_INLINE
6ca6e8
+  hp_timing_t jitter;
6ca6e8
+  HP_TIMING_NOW (jitter);
6ca6e8
+  return (uint32_t) jitter;
6ca6e8
+# else
6ca6e8
+  return 0;
6ca6e8
+# endif
6ca6e8
+}
6ca6e8
+
6ca6e8
+#endif