public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Noah Goldstein <goldstein.w.n@gmail.com>
To: libc-alpha@sourceware.org
Subject: [PATCH v1 5/5] benchtests: Add more coverage for strcmp and strncmp benchmarks
Date: Sun,  9 Jan 2022 06:29:47 -0600	[thread overview]
Message-ID: <20220109122946.2754917-5-goldstein.w.n@gmail.com> (raw)
In-Reply-To: <20220109122946.2754917-1-goldstein.w.n@gmail.com>

Add more small and medium sized tests for strcmp and strncmp.

As well for strcmp add option for more direct control of
alignment. Previously alignment was being pushed to the end of the
page. While this is the most difficult case to implement, it is far
from the common case and so shouldn't be the only benchmark.

Signed-off-by: Noah Goldstein <goldstein.w.n@gmail.com>
---
 benchtests/bench-strcmp.c  | 142 ++++++++++++++++++++++++++-----------
 benchtests/bench-strncmp.c | 110 ++++++++++++++++++++--------
 2 files changed, 183 insertions(+), 69 deletions(-)

diff --git a/benchtests/bench-strcmp.c b/benchtests/bench-strcmp.c
index 387e76fcfb..3a60edfb15 100644
--- a/benchtests/bench-strcmp.c
+++ b/benchtests/bench-strcmp.c
@@ -99,8 +99,8 @@ do_one_test (json_ctx_t *json_ctx, impl_t *impl,
 }
 
 static void
-do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, int
-	 max_char, int exp_result)
+do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
+         int max_char, int exp_result, int at_end)
 {
   size_t i;
 
@@ -109,19 +109,28 @@ do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, int
   if (len == 0)
     return;
 
-  align1 &= 63;
+  align1 &= ~(CHARBYTES - 1);
+  align2 &= ~(CHARBYTES - 1);
+
+  align1 &= (getpagesize () - 1);
   if (align1 + (len + 1) * CHARBYTES >= page_size)
     return;
 
-  align2 &= 63;
+  align2 &= (getpagesize () - 1);
   if (align2 + (len + 1) * CHARBYTES >= page_size)
     return;
 
   /* Put them close to the end of page.  */
-  i = align1 + CHARBYTES * (len + 2);
-  s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1);
-  i = align2 + CHARBYTES * (len + 2);
-  s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16)  + align2);
+  if (at_end)
+    {
+      i = align1 + CHARBYTES * (len + 2);
+      align1 = ((page_size - i) / 16 * 16) + align1;
+      i = align2 + CHARBYTES * (len + 2);
+      align2 = ((page_size - i) / 16 * 16) + align2;
+    }
+
+  s1 = (CHAR *)(buf1 + align1);
+  s2 = (CHAR *)(buf2 + align2);
 
   for (i = 0; i < len; i++)
     s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
@@ -132,9 +141,9 @@ do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, int
   s2[len - 1] -= exp_result;
 
   json_element_object_begin (json_ctx);
-  json_attr_uint (json_ctx, "length", (double) len);
-  json_attr_uint (json_ctx, "align1", (double) align1);
-  json_attr_uint (json_ctx, "align2", (double) align2);
+  json_attr_uint (json_ctx, "length", (double)len);
+  json_attr_uint (json_ctx, "align1", (double)align1);
+  json_attr_uint (json_ctx, "align2", (double)align2);
   json_array_begin (json_ctx, "timings");
 
   FOR_EACH_IMPL (impl, 0)
@@ -202,7 +211,8 @@ int
 test_main (void)
 {
   json_ctx_t json_ctx;
-  size_t i;
+  size_t i, j, k;
+  size_t pg_sz = getpagesize ();
 
   test_init ();
 
@@ -221,36 +231,88 @@ test_main (void)
   json_array_end (&json_ctx);
 
   json_array_begin (&json_ctx, "results");
-
-  for (i = 1; i < 32; ++i)
-    {
-      do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0);
-      do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1);
-      do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1);
-    }
-
-  for (i = 1; i < 10 + CHARBYTESLOG; ++i)
+  for (k = 0; k < 2; ++k)
     {
-      do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 0);
-      do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 0);
-      do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 1);
-      do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 1);
-      do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, -1);
-      do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, -1);
-      do_test (&json_ctx, 0, CHARBYTES * i, 2 << i, MIDCHAR, 1);
-      do_test (&json_ctx, CHARBYTES * i, CHARBYTES * (i + 1), 2 << i, LARGECHAR, 1);
+      for (i = 1; i < 32; ++i)
+        {
+          do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0, k);
+          do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1, k);
+          do_test (&json_ctx, CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1, k);
+        }
+
+      for (i = 1; i <= 8192;)
+        {
+          /* No page crosses.  */
+          do_test (&json_ctx, 0, 0, i, MIDCHAR, 0, k);
+          do_test (&json_ctx, i * CHARBYTES, 0, i, MIDCHAR, 0, k);
+          do_test (&json_ctx, 0, i * CHARBYTES, i, MIDCHAR, 0, k);
+
+          /* False page crosses.  */
+          do_test (&json_ctx, pg_sz / 2, pg_sz / 2 - CHARBYTES, i, MIDCHAR, 0,
+                   k);
+          do_test (&json_ctx, pg_sz / 2 - CHARBYTES, pg_sz / 2, i, MIDCHAR, 0,
+                   k);
+
+          do_test (&json_ctx, pg_sz - (i * CHARBYTES), 0, i, MIDCHAR, 0, k);
+          do_test (&json_ctx, 0, pg_sz - (i * CHARBYTES), i, MIDCHAR, 0, k);
+
+          /* Real page cross.  */
+          for (j = 16; j < 128; j += 16)
+            {
+              do_test (&json_ctx, pg_sz - j, 0, i, MIDCHAR, 0, k);
+              do_test (&json_ctx, 0, pg_sz - j, i, MIDCHAR, 0, k);
+
+              do_test (&json_ctx, pg_sz - j, pg_sz - j / 2, i, MIDCHAR, 0, k);
+              do_test (&json_ctx, pg_sz - j / 2, pg_sz - j, i, MIDCHAR, 0, k);
+            }
+
+          if (i < 32)
+            {
+              ++i;
+            }
+          else if (i < 160)
+            {
+              i += 8;
+            }
+          else if (i < 512)
+            {
+              i += 32;
+            }
+          else
+            {
+              i *= 2;
+            }
+        }
+
+      for (i = 1; i < 10 + CHARBYTESLOG; ++i)
+        {
+          do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 0, k);
+          do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 0, k);
+          do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, 1, k);
+          do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, 1, k);
+          do_test (&json_ctx, 0, 0, 2 << i, MIDCHAR, -1, k);
+          do_test (&json_ctx, 0, 0, 2 << i, LARGECHAR, -1, k);
+          do_test (&json_ctx, 0, CHARBYTES * i, 2 << i, MIDCHAR, 1, k);
+          do_test (&json_ctx, CHARBYTES * i, CHARBYTES * (i + 1), 2 << i,
+                   LARGECHAR, 1, k);
+        }
+
+      for (i = 1; i < 8; ++i)
+        {
+          do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i,
+                   MIDCHAR, 0, k);
+          do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i,
+                   LARGECHAR, 0, k);
+          do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i,
+                   MIDCHAR, 1, k);
+          do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i,
+                   LARGECHAR, 1, k);
+          do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i,
+                   MIDCHAR, -1, k);
+          do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i,
+                   LARGECHAR, -1, k);
+        }
     }
-
-  for (i = 1; i < 8; ++i)
-    {
-      do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 0);
-      do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 0);
-      do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 1);
-      do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 1);
-      do_test (&json_ctx, CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, -1);
-      do_test (&json_ctx, 2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, -1);
-    }
-
   do_test_page_boundary (&json_ctx);
 
   json_array_end (&json_ctx);
diff --git a/benchtests/bench-strncmp.c b/benchtests/bench-strncmp.c
index b7a01fde64..6673a53521 100644
--- a/benchtests/bench-strncmp.c
+++ b/benchtests/bench-strncmp.c
@@ -150,43 +150,43 @@ do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len, size_t
   if (n == 0)
     return;
 
-  align1 &= 63;
+  align1 &= getpagesize () - 1;
   if (align1 + (n + 1) * CHARBYTES >= page_size)
     return;
 
-  align2 &= 7;
+  align2 &= getpagesize () - 1;
   if (align2 + (n + 1) * CHARBYTES >= page_size)
     return;
 
   json_element_object_begin (json_ctx);
-  json_attr_uint (json_ctx, "strlen", (double) len);
-  json_attr_uint (json_ctx, "len", (double) n);
-  json_attr_uint (json_ctx, "align1", (double) align1);
-  json_attr_uint (json_ctx, "align2", (double) align2);
+  json_attr_uint (json_ctx, "strlen", (double)len);
+  json_attr_uint (json_ctx, "len", (double)n);
+  json_attr_uint (json_ctx, "align1", (double)align1);
+  json_attr_uint (json_ctx, "align2", (double)align2);
   json_array_begin (json_ctx, "timings");
 
   FOR_EACH_IMPL (impl, 0)
-    {
-      alloc_bufs ();
-      s1 = (CHAR *) (buf1 + align1);
-      s2 = (CHAR *) (buf2 + align2);
-
-      for (i = 0; i < n; i++)
-	s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
-
-      s1[n] = 24 + exp_result;
-      s2[n] = 23;
-      s1[len] = 0;
-      s2[len] = 0;
-      if (exp_result < 0)
-	s2[len] = 32;
-      else if (exp_result > 0)
-	s1[len] = 64;
-      if (len >= n)
-	s2[n - 1] -= exp_result;
+  {
+    alloc_bufs ();
+    s1 = (CHAR *)(buf1 + align1);
+    s2 = (CHAR *)(buf2 + align2);
+
+    for (i = 0; i < n; i++)
+      s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
+
+    s1[n] = 24 + exp_result;
+    s2[n] = 23;
+    s1[len] = 0;
+    s2[len] = 0;
+    if (exp_result < 0)
+      s2[len] = 32;
+    else if (exp_result > 0)
+      s1[len] = 64;
+    if (len >= n)
+      s2[n - 1] -= exp_result;
 
-      do_one_test (json_ctx, impl, s1, s2, n, exp_result);
-    }
+    do_one_test (json_ctx, impl, s1, s2, n, exp_result);
+  }
 
   json_array_end (json_ctx);
   json_element_object_end (json_ctx);
@@ -319,7 +319,8 @@ int
 test_main (void)
 {
   json_ctx_t json_ctx;
-  size_t i;
+  size_t i, j, len;
+  size_t pg_sz = getpagesize ();
 
   test_init ();
 
@@ -334,12 +335,12 @@ test_main (void)
 
   json_array_begin (&json_ctx, "ifuncs");
   FOR_EACH_IMPL (impl, 0)
-    json_element_string (&json_ctx, impl->name);
+  json_element_string (&json_ctx, impl->name);
   json_array_end (&json_ctx);
 
   json_array_begin (&json_ctx, "results");
 
-  for (i =0; i < 16; ++i)
+  for (i = 0; i < 16; ++i)
     {
       do_test (&json_ctx, 0, 0, 8, i, 127, 0);
       do_test (&json_ctx, 0, 0, 8, i, 127, -1);
@@ -361,6 +362,57 @@ test_main (void)
       do_test (&json_ctx, i, 3 * i, 8, i, 255, -1);
     }
 
+  for (len = 0; len <= 128; len += 64)
+    {
+      for (i = 1; i <= 8192;)
+        {
+          /* No page crosses.  */
+          do_test (&json_ctx, 0, 0, i, i + len, 127, 0);
+          do_test (&json_ctx, i * CHARBYTES, 0, i, i + len, 127, 0);
+          do_test (&json_ctx, 0, i * CHARBYTES, i, i + len, 127, 0);
+
+          /* False page crosses.  */
+          do_test (&json_ctx, pg_sz / 2, pg_sz / 2 - CHARBYTES, i, i + len,
+                   127, 0);
+          do_test (&json_ctx, pg_sz / 2 - CHARBYTES, pg_sz / 2, i, i + len,
+                   127, 0);
+
+          do_test (&json_ctx, pg_sz - (i * CHARBYTES), 0, i, i + len, 127,
+                   0);
+          do_test (&json_ctx, 0, pg_sz - (i * CHARBYTES), i, i + len, 127,
+                   0);
+
+          /* Real page cross.  */
+          for (j = 16; j < 128; j += 16)
+            {
+              do_test (&json_ctx, pg_sz - j, 0, i, i + len, 127, 0);
+              do_test (&json_ctx, 0, pg_sz - j, i, i + len, 127, 0);
+
+              do_test (&json_ctx, pg_sz - j, pg_sz - j / 2, i, i + len,
+                       127, 0);
+              do_test (&json_ctx, pg_sz - j / 2, pg_sz - j, i, i + len,
+                       127, 0);
+            }
+
+          if (i < 32)
+            {
+              ++i;
+            }
+          else if (i < 160)
+            {
+              i += 8;
+            }
+          else if (i < 256)
+            {
+              i += 32;
+            }
+          else
+            {
+              i *= 2;
+            }
+        }
+    }
+
   for (i = 1; i < 8; ++i)
     {
       do_test (&json_ctx, 0, 0, 8 << i, 16 << i, 127, 0);
-- 
2.25.1


  parent reply	other threads:[~2022-01-09 12:30 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-09 12:29 [PATCH v1 1/5] x86: Optimize strcmp-avx2.S and fix for [BZ# 28755] Noah Goldstein
2022-01-09 12:29 ` [PATCH v1 2/5] x86: Optimize strcmp-evex.S " Noah Goldstein
2022-01-09 12:29 ` [PATCH v1 3/5] string: remove stupid_[strcmp, strncmp, wcscmp, wcsncmp] Noah Goldstein
2022-01-09 12:29 ` [PATCH v1 4/5] string: Improve coverage in test-strcmp.c and test-strncmp.c Noah Goldstein
2022-01-09 12:29 ` Noah Goldstein [this message]
2022-01-09 12:35 ` [PATCH v1 1/5] x86: Optimize strcmp-avx2.S and fix for [BZ# 28755] Noah Goldstein
2022-01-09 14:07   ` H.J. Lu
2022-01-10  0:29     ` Noah Goldstein
2022-01-10  0:27 ` [PATCH v2 1/7] x86: Fix __wcsncmp_avx2 in strcmp-avx2.S " Noah Goldstein
2022-01-10  0:27   ` [PATCH v2 2/7] x86: Fix __wcsncmp_evex in strcmp-evex.S " Noah Goldstein
2022-01-10  0:35     ` H.J. Lu
2022-01-10  0:27   ` [PATCH v2 3/7] string/test-str*cmp: remove stupid_[strcmp, strncmp, wcscmp, wcsncmp] Noah Goldstein
2022-01-10  0:37     ` H.J. Lu
2022-01-10  0:27   ` [PATCH v2 4/7] string: Improve coverage in test-strcmp.c and test-strncmp.c Noah Goldstein
2022-01-10  0:38     ` H.J. Lu
2022-01-10  2:51       ` Noah Goldstein
2022-01-10  0:27   ` [PATCH v2 5/7] x86: Optimize strcmp-avx2.S Noah Goldstein
2022-01-10  0:41     ` H.J. Lu
2022-01-10  1:06       ` Noah Goldstein
2022-01-10  1:58         ` H.J. Lu
2022-01-10  2:54           ` Noah Goldstein
2022-01-10  0:27   ` [PATCH v2 6/7] x86: Optimize strcmp-evex.S Noah Goldstein
2022-01-10  0:41     ` H.J. Lu
2022-01-10  0:27   ` [PATCH v2 7/7] benchtests: Add more coverage for strcmp and strncmp benchmarks Noah Goldstein
2022-01-10  0:34   ` [PATCH v2 1/7] x86: Fix __wcsncmp_avx2 in strcmp-avx2.S [BZ# 28755] H.J. Lu
2022-01-10 21:35 ` [PATCH v3 " Noah Goldstein
2022-01-10 21:35   ` [PATCH v3 2/7] x86: Fix __wcsncmp_evex in strcmp-evex.S " Noah Goldstein
2022-01-11  2:15     ` H.J. Lu
2022-01-26 22:04       ` H.J. Lu
2022-04-29 22:05         ` Sunil Pandey
2022-01-10 21:35   ` [PATCH v3 3/7] string/test-str*cmp: remove stupid_[strcmp, strncmp, wcscmp, wcsncmp] Noah Goldstein
2022-01-10 21:35   ` [PATCH v3 4/7] string: Improve coverage in test-strcmp.c and test-strncmp.c Noah Goldstein
2022-01-10 21:35   ` [PATCH v3 5/7] x86: Optimize strcmp-avx2.S Noah Goldstein
2022-02-14 14:10     ` Andreas Schwab
2022-02-14 18:23       ` H.J. Lu
2022-02-14 19:16         ` Andreas Schwab
2022-02-14 19:30           ` H.J. Lu
2022-02-14 19:35             ` Andreas Schwab
2022-02-14 20:59               ` H.J. Lu
2022-02-14 21:10                 ` H.J. Lu
2022-02-15 11:11                   ` Andreas Schwab
2022-02-15 12:55                   ` Andreas Schwab
2022-02-15 12:58                     ` Noah Goldstein
2022-02-15 13:09                       ` Noah Goldstein
2022-02-15 13:32                         ` Noah Goldstein
2022-02-15 13:37                           ` Noah Goldstein
2022-02-15 16:33                             ` Noah Goldstein
2022-02-14 23:42                 ` Noah Goldstein
2022-02-15 10:43                   ` Andreas Schwab
2022-02-15 11:22                   ` Andreas Schwab
2022-02-15 11:28                     ` Noah Goldstein
2022-02-15 12:24                       ` Andreas Schwab
2022-01-10 21:35   ` [PATCH v3 6/7] x86: Optimize strcmp-evex.S Noah Goldstein
2022-01-10 21:35   ` [PATCH v3 7/7] benchtests: Add more coverage for strcmp and strncmp benchmarks Noah Goldstein
2022-01-11  2:15   ` [PATCH v3 1/7] x86: Fix __wcsncmp_avx2 in strcmp-avx2.S [BZ# 28755] H.J. Lu
2022-01-26 22:05     ` H.J. Lu
2022-01-27  4:29       ` H.J. Lu
2022-01-27  5:10         ` H.J. Lu
2022-01-27  5:52           ` Noah Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220109122946.2754917-5-goldstein.w.n@gmail.com \
    --to=goldstein.w.n@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).