This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Randomize memcpy benchmark addresses.


Hi,

This patch randomizes address for memcpy and clears up unnecessary check
code in memcpy.

This makes results more accurate. We need to calculate averages of these
which I leave to Will.

OK to commit?

	* benchtests/bench-memcpy.c: Randomize address.

diff --git a/benchtests/bench-memcpy.c b/benchtests/bench-memcpy.c
index 1b12671..684044b 100644
--- a/benchtests/bench-memcpy.c
+++ b/benchtests/bench-memcpy.c
@@ -15,6 +15,8 @@
    You should have received a copy of the GNU Lesser General Public
    License along with the GNU C Library; if not, see
    <http://www.gnu.org/licenses/>.  */
+#define SIZE 2000000
+#include <stdint.h>
 
 #ifndef MEMCPY_RESULT
 # define MEMCPY_RESULT(dst, len) dst
@@ -49,36 +51,34 @@ builtin_memcpy (char *dst, const char *src, size_t n)
 typedef char *(*proto_t) (char *, const char *, size_t);
 
 static void
-do_one_test (impl_t *impl, char *dst, const char *src,
+do_one_test (impl_t *impl, size_t align1, size_t align2,
 	     size_t len)
 {
-  if (CALL (impl, dst, src, len) != MEMCPY_RESULT (dst, len))
+  static unsigned int r_seed = 42;
+  char *srcs[32];
+  char *dsts[32];
+  int offset1 = 0, offset2 = 0;
+  for (int i = 0; i < 32; i++)
     {
-      error (0, 0, "Wrong result in function %s %p %p", impl->name,
-	     CALL (impl, dst, src, len), MEMCPY_RESULT (dst, len));
-      ret = 1;
-      return;
-    }
-
-  if (memcmp (dst, src, len) != 0)
-    {
-      error (0, 0, "Wrong result in function %s dst \"%s\" src \"%s\"",
-	     impl->name, dst, src);
-      ret = 1;
-      return;
+      if (!align1)
+        offset1 = rand_r (&r_seed) % 64;
+      srcs[i] = buf1 + (rand_r (&r_seed) % (SIZE / 64)) * 64 + offset1;
+      if (!align2)
+        offset2 = rand_r (&r_seed) % 64;
+      dsts[i] = buf2 + (rand_r (&r_seed) % (SIZE / 64)) * 64 + offset2;
     }
 
   if (HP_TIMING_AVAIL)
     {
       hp_timing_t start __attribute ((unused));
       hp_timing_t stop __attribute ((unused));
-      hp_timing_t best_time = ~ (hp_timing_t) 0;
+      hp_timing_t best_time = ~(hp_timing_t) 0;
       size_t i;
 
       for (i = 0; i < 32; ++i)
 	{
 	  HP_TIMING_NOW (start);
-	  CALL (impl, dst, src, len);
+	  CALL (impl, dsts[i], srcs[i], len);
 	  HP_TIMING_NOW (stop);
 	  HP_TIMING_BEST (best_time, start, stop);
 	}
@@ -90,28 +90,11 @@ do_one_test (impl_t *impl, char *dst, const char *src,
 static void
 do_test (size_t align1, size_t align2, size_t len)
 {
-  size_t i, j;
-  char *s1, *s2;
-
-  align1 &= 63;
-  if (align1 + len >= page_size)
-    return;
-
-  align2 &= 63;
-  if (align2 + len >= page_size)
-    return;
-
-  s1 = (char *) (buf1 + align1);
-  s2 = (char *) (buf2 + align2);
-
-  for (i = 0, j = 1; i < len; i++, j += 23)
-    s1[i] = j;
-
   if (HP_TIMING_AVAIL)
-    printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
+    printf ("Length %4zd, aligned %c/%c:", len, align1 ? 'y' : 'n', align2 ? 'y' : 'n');
 
   FOR_EACH_IMPL (impl, 0)
-    do_one_test (impl, s2, s1, len);
+    do_one_test (impl, align1, align2, len);
 
   if (HP_TIMING_AVAIL)
     putchar ('\n');
@@ -123,6 +106,11 @@ test_main (void)
   size_t i;
 
   test_init ();
+  buf1 = malloc (2 * SIZE + 4096);
+  buf2 = malloc (2 * SIZE + 4096);
+  buf1 = buf1 + 4096 - ((uintptr_t) buf1) % 4096;
+  buf2 = buf2 + 4096 - ((uintptr_t) buf1) % 4096;
+
 
   printf ("%23s", "");
   FOR_EACH_IMPL (impl, 0)
@@ -132,17 +120,13 @@ test_main (void)
   for (i = 0; i < 18; ++i)
     {
       do_test (0, 0, 1 << i);
-      do_test (i, 0, 1 << i);
-      do_test (0, i, 1 << i);
-      do_test (i, i, 1 << i);
+      do_test (1, 1, 1 << i);
     }
 
   for (i = 0; i < 32; ++i)
     {
       do_test (0, 0, i);
-      do_test (i, 0, i);
-      do_test (0, i, i);
-      do_test (i, i, i);
+      do_test (1, 1, i);
     }
 
   for (i = 3; i < 32; ++i)
@@ -150,9 +134,7 @@ test_main (void)
       if ((i & (i - 1)) == 0)
 	continue;
       do_test (0, 0, 16 * i);
-      do_test (i, 0, 16 * i);
-      do_test (0, i, 16 * i);
-      do_test (i, i, 16 * i);
+      do_test (1, 1, 16 * i);
     }
 
   do_test (0, 0, getpagesize ());


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]