]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
benchtests: Clean up the alloc_bufs
authorSiddhesh Poyarekar <siddhesh@sourceware.org>
Tue, 7 Aug 2018 19:14:56 +0000 (00:44 +0530)
committerSiddhesh Poyarekar <siddhesh@sourceware.org>
Tue, 7 Aug 2018 19:14:56 +0000 (00:44 +0530)
Drop realloc_bufs in favour of making alloc_bufs transparently
reallocate the buffers if it had allocated before.  Also consolidate
computation of buffer lengths so that they don't get repeated on every
reallocation.

* benchtests/bench-string.h (buf1_size, buf2_size): New
variables.
(init_sizes): New function.
(test_init): Use it.
(alloc_buf, exit_error): New functions.
(alloc_bufs): Use ALLOC_BUF.
(realloc_bufs): Remove.
* benchtests/bench-memcmp.c (do_test): Adjust.
* benchtests/bench-memset-large.c (do_test): Likewise.
* benchtests/bench-memset-walk.c (do_test): Likewise.
* benchtests/bench-memset.c (do_test): Likewise.
* benchtests/bench-strncmp.c (do_test): Likewise.

ChangeLog
benchtests/bench-memcmp.c
benchtests/bench-memset-large.c
benchtests/bench-memset-walk.c
benchtests/bench-memset.c
benchtests/bench-string.h
benchtests/bench-strncmp.c

index c8a1b706197b2ac65678f7786d0faf510bd9f9a7..7e1a5b081fbba246a8642d62f2c5769df9d9595f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2018-08-07  Siddhesh Poyarekar  <siddhesh@sourceware.org>
+
+       * benchtests/bench-string.h (buf1_size, buf2_size): New
+       variables.
+       (init_sizes): New function.
+       (test_init): Use it.
+       (alloc_buf, exit_error): New functions.
+       (alloc_bufs): Use ALLOC_BUF.
+       (realloc_bufs): Remove.
+       * benchtests/bench-memcmp.c (do_test): Adjust.
+       * benchtests/bench-memset-large.c (do_test): Likewise.
+       * benchtests/bench-memset-walk.c (do_test): Likewise.
+       * benchtests/bench-memset.c (do_test): Likewise.
+       * benchtests/bench-strncmp.c (do_test): Likewise.
+
 2018-08-06  Andreas Schwab  <schwab@suse.de>
 
        * sysdeps/riscv/nptl/tls.h (DB_THREAD_SELF): Use REGISTER instead
index 69ed9c958bbc9a6c05b7fec2910669db46302279..b566f48ae9f3cab2920593b208c28ff39bddfb52 100644 (file)
@@ -132,7 +132,7 @@ do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
       s2[len - 1] -= exp_result;
 
       do_one_test (json_ctx, impl, s1, s2, len, exp_result);
-      realloc_bufs ();
+      alloc_bufs ();
     }
 
   json_array_end (json_ctx);
index 248289108ba499c8a5f0ee3ed4f705fa1f730c0a..1f7bf81fd1063a0469eb23efdb1480356a1c1c6a 100644 (file)
@@ -92,7 +92,7 @@ do_test (json_ctx_t *json_ctx, size_t align, int c, size_t len)
   FOR_EACH_IMPL (impl, 0)
     {
       do_one_test (json_ctx, impl, (CHAR *) (buf1) + align, c, len);
-      realloc_bufs ();
+      alloc_bufs ();
     }
 
   json_array_end (json_ctx);
index 4dcb1529cfe86d6d1734fdf5fe6456d395f2f778..753d6f36f3f371e18330972b3eeee444d5374201 100644 (file)
@@ -88,7 +88,7 @@ do_test (json_ctx_t *json_ctx, int c, size_t len)
     {
       do_one_test (json_ctx, impl, (CHAR *) buf1,
                   (CHAR *) buf1 + MIN_PAGE_SIZE - len, c, len);
-      realloc_bufs ();
+      alloc_bufs ();
     }
 
   json_array_end (json_ctx);
index 0ae201d4a595a2b05ad95b928a715385b8d51e1b..a47620cbfca6cbf2e582aa2288263c716836c9bd 100644 (file)
@@ -134,7 +134,7 @@ do_test (json_ctx_t *json_ctx, size_t align, int c, size_t len)
   FOR_EACH_IMPL (impl, 0)
     {
       do_one_test (json_ctx, impl, (CHAR *) (buf1) + align, c, len);
-      realloc_bufs ();
+      alloc_bufs ();
     }
 
   json_array_end (json_ctx);
index 94aaafdaf2e1319b8cebb5baaf51f02b52182d5e..f8389982e9106df83d755a31188100cc85f38ac9 100644 (file)
@@ -76,10 +76,8 @@ extern impl_t __start_impls[], __stop_impls[];
 
 # define INNER_LOOP_ITERS 64
 
-unsigned char *buf1, *buf2;
 int ret, do_srandom;
 unsigned int seed;
-size_t page_size;
 
 # ifndef ITERATIONS
 size_t iterations = 100000;
@@ -182,47 +180,57 @@ static impl_t *impl_array;
 #  define BUF1PAGES 1
 # endif
 
+unsigned char *buf1, *buf2;
+static size_t buf1_size, buf2_size, page_size;
+
 static void
-alloc_bufs (void)
+init_sizes (void)
 {
   page_size = 2 * getpagesize ();
 # ifdef MIN_PAGE_SIZE
   if (page_size < MIN_PAGE_SIZE)
     page_size = MIN_PAGE_SIZE;
 # endif
-  buf1 = mmap (0, (BUF1PAGES + 1) * page_size, PROT_READ | PROT_WRITE,
-              MAP_PRIVATE | MAP_ANON, -1, 0);
-  if (buf1 == MAP_FAILED)
-    error (EXIT_FAILURE, errno, "mmap failed for buf1");
-  if (mprotect (buf1 + BUF1PAGES * page_size, page_size, PROT_NONE))
-    error (EXIT_FAILURE, errno, "mprotect failed for buf1");
-  buf2 = mmap (0, 2 * page_size, PROT_READ | PROT_WRITE,
-              MAP_PRIVATE | MAP_ANON, -1, 0);
-  if (buf2 == MAP_FAILED)
-    error (EXIT_FAILURE, errno, "mmap failed for buf2");
-  if (mprotect (buf2 + page_size, page_size, PROT_NONE))
-    error (EXIT_FAILURE, errno, "mprotect failed for buf2");
+
+  buf1_size = BUF1PAGES * page_size;
+  buf2_size = page_size;
+}
+
+static void
+exit_error (const char *id, const char *func)
+{
+  error (EXIT_FAILURE, errno, "%s: %s failed", id, func);
 }
 
+/* Allocate a buffer of size SIZE with a guard page at the end.  */
 static void
-__attribute__ ((unused))
-realloc_bufs (void)
+alloc_buf (const char *id, size_t size, unsigned char **retbuf)
 {
-  int ret = 0;
+  size_t alloc_size = size + page_size;
 
-  if (buf1)
-    ret = munmap (buf1, (BUF1PAGES + 1) * page_size);
+  if (*retbuf != NULL)
+    {
+       int ret = munmap (*retbuf, alloc_size);
+       if (ret != 0)
+         exit_error (id, "munmap");
+    }
 
-  if (ret != 0)
-    error (EXIT_FAILURE, errno, "munmap failed for buf1");
+  unsigned char *buf = mmap (0, alloc_size, PROT_READ | PROT_WRITE,
+                            MAP_PRIVATE | MAP_ANON, -1, 0);
 
-  if (buf2)
-    ret = munmap (buf2, 2 * page_size);
+  if (buf == MAP_FAILED)
+    exit_error (id, "mmap");
+  if (mprotect (buf + size, page_size, PROT_NONE))
+    exit_error (id, "mprotect");
 
-  if (ret != 0)
-    error (EXIT_FAILURE, errno, "munmap failed for buf2");
+  *retbuf = buf;
+}
 
-  alloc_bufs ();
+static void
+alloc_bufs (void)
+{
+  alloc_buf ("buf1", buf1_size, &buf1);
+  alloc_buf ("buf2", buf2_size, &buf2);
 }
 
 static void
@@ -234,6 +242,7 @@ test_init (void)
                                        / sizeof func_list[0]));
 # endif
 
+  init_sizes ();
   alloc_bufs ();
 
   if (do_srandom)
index 97dc39069d70ee6b0cff62c6366f19e2ca57246c..92cf0d214553c71ae2a0f09e8ec3a3ce08a4c204 100644 (file)
@@ -150,7 +150,7 @@ do_test_limit (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
 
   FOR_EACH_IMPL (impl, 0)
     {
-      realloc_bufs ();
+      alloc_bufs ();
       s1 = (CHAR *) (buf1 + page_size - n * CHARBYTES);
       s2 = (CHAR *) (buf2 + page_size - n * CHARBYTES);
 
@@ -207,7 +207,7 @@ do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, size_t
 
   FOR_EACH_IMPL (impl, 0)
     {
-      realloc_bufs ();
+      alloc_bufs ();
       s1 = (CHAR *) (buf1 + align1);
       s2 = (CHAR *) (buf2 + align2);