From patchwork Mon Mar 18 12:27:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 87310 X-Patchwork-Delegate: arjun.is@lostca.se Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E61F23858D37 for ; Mon, 18 Mar 2024 12:28:26 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 822C23858D1E for ; Mon, 18 Mar 2024 12:27:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 822C23858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 822C23858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::632 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710764881; cv=none; b=sML8dZumY/L/PhJi0b2SlvDIznMqtqj562MZ88HG2y4IqwJhBLJWkRzsNUbwR0fBwU4/1ZR/cik/EtlX9DzobcZcQP9ae0lcuVyhLSSdXoT0J6gWWCoEiWZkT4Dm+u4Y5LD4Ny2aR9guAjfDFWANG5z2o5qk4BqHOWV4KGe+0AU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710764881; c=relaxed/simple; bh=UHgcFIAq2bW/v3MzgGfGtpbVrN+lc25L6FYuVMys1AQ=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ntStcHsVD5zylJwRhSbtU7l20zhR4wHgKs8DkWHFWAjOxbJ24gG57LMwD8kLBiQus0+7bhjbF/gByGi9wKHMyMhW/ptGEtQZd61wC7maa+8WMwbl1DInmAsuE+QxIoAHPiFCe24TiJyNhF6VyL0jV5jex6Z8DeHYTxXcJXy6mwo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1def142ae7bso25571745ad.3 for ; Mon, 18 Mar 2024 05:27:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1710764877; x=1711369677; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Iujy/e+LL79Xg4TDF53WjV8blsjZ4Ht+e8cq9ob1rh4=; b=E7++bfMzuuzfQxM7v1VkI7ovEvqDklw2OF/86oNN0KWz/fBGIidhmnz7i3ZMga6X1K 8hAcJeGL5KUoR1Zy1FzLAOYb5Zclpv4Zkq8AbDPuGehlq4Zrcoou4B3xFUQZznMeO+/e nFUDdxhp3IG7uKI8jql6a4FPapB/kKGXjXu63GYvV8pvQNIYKn2Fxc4NvYPvtN2iEvaW nHncBroybE7ybJJiK4qUSUzKBcU0AiH5mnaYQ7j4lLpnFKOVCTokeotY3t+0vOkeoZRs kTPEtKClTCMEWgNaZHaOKGactnwFjUzRkLmmEtl1WST5BwO/rzsXzX+zlzNcQE/DDqOp s0Dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710764877; x=1711369677; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Iujy/e+LL79Xg4TDF53WjV8blsjZ4Ht+e8cq9ob1rh4=; b=jcxJszgWb2KbJAG38+wL2UJyPNIyXUyOCw3j236d+qIjaCI3jMOSwgDdu60J5Q9Kb4 MdfKWkQ1Q5bt92pC7+H+h4wb8zB7tInSnigP4gOLUwqV4MNdUqfGSZl//0Eh4bxNC4P2 E2yysQbc9S0EtTD1j5NQLCLmU3p1UBR3KEyp9EJJ5VNAqMu80i+coQD5QkxOWqBMlNLo XnmtW7tR8ema4RqkNnJN+2TosVHibviQ22oK9BI5hC9zbGnj3BZMboL0YgyMBaoKxLOT iKdE0qS9Jrch2Rbo5ie41Cl3kaiYNJkntGdJnEMbZRcDY8xy+TTOGWbx++FYv7wkJA5Z +lzw== X-Gm-Message-State: AOJu0YzAiYpT87wc+LtXJee/Exu1nqErOMZoFUU2C4Rsyn84PD3HCojy Jfmzz6BeWygACGQtfMCVar85ztgkwg0qNu8OvtEhwwM75oEYtrk8UmPCQlFIt0dlj2HF79Al8r/ N X-Google-Smtp-Source: AGHT+IFSg5YGOmegSpRETh5YgQrKCjZl1A3qNnhmdrajI6rL5wUMZ2OJx9FZ/SBc/zVizecoIuMZaQ== X-Received: by 2002:a17:902:d4c5:b0:1e0:823:eb2d with SMTP id o5-20020a170902d4c500b001e00823eb2dmr5616585plg.24.1710764876689; Mon, 18 Mar 2024 05:27:56 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:8dfd:52c6:3e80:a55a:6392]) by smtp.gmail.com with ESMTPSA id lk15-20020a17090308cf00b001d74502d261sm9122892plb.115.2024.03.18.05.27.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 05:27:56 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Peter Bergner Subject: [PATCH] benchtests: Improve benchtests for strstr Date: Mon, 18 Mar 2024 09:27:52 -0300 Message-Id: <20240318122752.197208-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Use same strategy as bench-strstr.c (93eebae5168e5cf2 and 80b2bfb53504) and use json_ctx for output to help standardize format across all benchtests. Reviewed-by: Arjun Shankar --- benchtests/bench-strcasestr.c | 349 ++++++++++++++++++++++++++-------- 1 file changed, 273 insertions(+), 76 deletions(-) diff --git a/benchtests/bench-strcasestr.c b/benchtests/bench-strcasestr.c index f6d1a78fba..84a0bef38f 100644 --- a/benchtests/bench-strcasestr.c +++ b/benchtests/bench-strcasestr.c @@ -16,10 +16,36 @@ License along with the GNU C Library; if not, see . */ +#define MIN_PAGE_SIZE 131072 #define TEST_MAIN #define TEST_NAME "strcasestr" #include "bench-string.h" +#include "json-lib.h" + +static const char input[] = +"This manual is written with the assumption that you are at least " +"somewhat familiar with the C programming language and basic programming " +"concepts. Specifically, familiarity with ISO standard C (*note ISO " +"C::), rather than “traditional” pre-ISO C dialects, is assumed.\n" + +" The GNU C Library includes several “header files”, each of which " +"provides definitions and declarations for a group of related facilities; " +"this information is used by the C compiler when processing your program. " +"For example, the header file ‘stdio.h’ declares facilities for " +"performing input and output, and the header file ‘string.h’ declares " +"string processing utilities. The organization of this manual generally " +"follows the same division as the header files.\n" + +" If you are reading this manual for the first time, you should read " +"all of the introductory material and skim the remaining chapters. There " +"are a _lot_ of functions in the GNU C Library and it’s not realistic to " +"expect that you will be able to remember exactly _how_ to use each and " +"every one of them. It’s more important to become generally familiar " +"with the kinds of facilities that the library provides, so that when you " +"are writing your programs you can recognize _when_ to make use of " +"library functions, and _where_ in this manual you can find more specific " +"information about them.\n"; #define STRCASESTR simple_strcasestr #define NO_ALIAS @@ -32,123 +58,294 @@ typedef char *(*proto_t) (const char *, const char *); IMPL (simple_strcasestr, 0) IMPL (strcasestr, 1) - static void -do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result) +do_one_test (json_ctx_t *json_ctx, impl_t *impl, const char *s1, + const char *s2, char *exp_result) { - size_t i, iters = INNER_LOOP_ITERS_SMALL; + size_t i, iters = INNER_LOOP_ITERS_SMALL / 8; timing_t start, stop, cur; + char *res; TIMING_NOW (start); for (i = 0; i < iters; ++i) - { - CALL (impl, s1, s2); - } + res = CALL (impl, s1, s2); TIMING_NOW (stop); TIMING_DIFF (cur, start, stop); - TIMING_PRINT_MEAN ((double) cur, (double) iters); -} + json_element_double (json_ctx, (double) cur / (double) iters); + if (res != exp_result) + { + error (0, 0, "Wrong result in function %s %s %s", impl->name, + (res == NULL) ? "(null)" : res, + (exp_result == NULL) ? "(null)" : exp_result); + ret = 1; + } +} static void -do_test (size_t align1, size_t align2, size_t len1, size_t len2, - int fail) +do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len1, + size_t len2, int fail) { char *s1 = (char *) (buf1 + align1); char *s2 = (char *) (buf2 + align2); - static const char d[] = "1234567890abcxyz"; -#define dl (sizeof (d) - 1) - char *ss2 = s2; - for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0) - { - size_t t = l > dl ? dl : l; - ss2 = mempcpy (ss2, d, t); - } - s2[len2] = '\0'; + size_t size = sizeof (input) - 1; + size_t pos = (len1 + len2) % size; - if (fail) + char *ss2 = s2; + for (size_t l = len2; l > 0; l = l > size ? l - size : 0) { - char *ss1 = s1; - for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0) + size_t t = l > size ? size : l; + if (pos + t <= size) + ss2 = mempcpy (ss2, input + pos, t); + else { - size_t t = l > dl ? dl : l; - memcpy (ss1, d, t); - ++ss1[len2 > 7 ? 7 : len2 - 1]; - ss1 += t; + ss2 = mempcpy (ss2, input + pos, size - pos); + ss2 = mempcpy (ss2, input, t - (size - pos)); } } - else + s2[len2] = '\0'; + + char *ss1 = s1; + for (size_t l = len1; l > 0; l = l > size ? l - size : 0) { - memset (s1, '0', len1); - for (size_t i = 0; i < len2; ++i) - s1[len1 - len2 + i] = toupper (s2[i]); + size_t t = l > size ? size : l; + memcpy (ss1, input, t); + ss1 += t; } + + if (!fail) + memcpy (s1 + len1 - len2, s2, len2); s1[len1] = '\0'; - printf ("Length %4zd/%zd, alignment %2zd/%2zd, %s:", - len1, len2, align1, align2, fail ? "fail" : "found"); + /* Remove any accidental matches except for the last if !fail. */ + for (ss1 = simple_strcasestr (s1, s2); + ss1 != NULL; + ss1 = simple_strcasestr (ss1 + 1, s2)) + if (fail || ss1 != s1 + len1 - len2) + ++ss1[len2 / 2]; + + json_element_object_begin (json_ctx); + json_attr_uint (json_ctx, "len_haystack", len1); + json_attr_uint (json_ctx, "len_needle", len2); + json_attr_uint (json_ctx, "align_haystack", align1); + json_attr_uint (json_ctx, "align_needle", align2); + json_attr_uint (json_ctx, "fail", fail); + + json_array_begin (json_ctx, "timings"); FOR_EACH_IMPL (impl, 0) - do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2); + do_one_test (json_ctx, impl, s1, s2, fail ? NULL : s1 + len1 - len2); + + json_array_end (json_ctx); + json_element_object_end (json_ctx); - putchar ('\n'); +} + +/* Test needles which exhibit worst-case performance for naive quadradic + implementations. */ + +static void +test_hard_needle (json_ctx_t *json_ctx, size_t ne_len, size_t hs_len) +{ + char *ne = (char *) buf1; + char *hs = (char *) buf2; + + /* Hard needle for strstr algorithm using skip table. This results in many + memcmp calls comparing most of the needle. */ + { + memset (ne, 'a', ne_len); + ne[ne_len] = '\0'; + ne[ne_len - 14] = 'b'; + + memset (hs, 'a', hs_len); + for (size_t i = ne_len; i <= hs_len; i += ne_len) + { + hs[i - 5] = 'b'; + hs[i - 62] = 'b'; + } + + json_element_object_begin (json_ctx); + json_attr_uint (json_ctx, "len_haystack", hs_len); + json_attr_uint (json_ctx, "len_needle", ne_len); + json_attr_uint (json_ctx, "align_haystack", 0); + json_attr_uint (json_ctx, "align_needle", 0); + json_attr_uint (json_ctx, "fail", 1); + json_attr_string (json_ctx, "desc", "Difficult skiptable(0)"); + + json_array_begin (json_ctx, "timings"); + + FOR_EACH_IMPL (impl, 0) + do_one_test (json_ctx, impl, hs, ne, NULL); + + json_array_end (json_ctx); + json_element_object_end (json_ctx); + } + + /* 2nd hard needle for strstr algorithm using skip table. This results in + many memcmp calls comparing most of the needle. */ + { + memset (ne, 'a', ne_len); + ne[ne_len] = '\0'; + ne[ne_len - 6] = 'b'; + + memset (hs, 'a', hs_len); + for (size_t i = ne_len; i <= hs_len; i += ne_len) + { + hs[i - 5] = 'b'; + hs[i - 6] = 'b'; + } + + json_element_object_begin (json_ctx); + json_attr_uint (json_ctx, "len_haystack", hs_len); + json_attr_uint (json_ctx, "len_needle", ne_len); + json_attr_uint (json_ctx, "align_haystack", 0); + json_attr_uint (json_ctx, "align_needle", 0); + json_attr_uint (json_ctx, "fail", 1); + json_attr_string (json_ctx, "desc", "Difficult skiptable(1)"); + + json_array_begin (json_ctx, "timings"); + + FOR_EACH_IMPL (impl, 0) + do_one_test (json_ctx, impl, hs, ne, NULL); + + json_array_end (json_ctx); + json_element_object_end (json_ctx); + } + + /* Hard needle for Two-way algorithm - the random input causes a large number + of branch mispredictions which significantly reduces performance on modern + micro architectures. */ + { + for (int i = 0; i < hs_len; i++) + hs[i] = (rand () & 255) > 155 ? 'a' : 'b'; + hs[hs_len] = 0; + + memset (ne, 'a', ne_len); + ne[ne_len - 2] = 'b'; + ne[0] = 'b'; + ne[ne_len] = 0; + + json_element_object_begin (json_ctx); + json_attr_uint (json_ctx, "len_haystack", hs_len); + json_attr_uint (json_ctx, "len_needle", ne_len); + json_attr_uint (json_ctx, "align_haystack", 0); + json_attr_uint (json_ctx, "align_needle", 0); + json_attr_uint (json_ctx, "fail", 1); + json_attr_string (json_ctx, "desc", "Difficult 2-way"); + + json_array_begin (json_ctx, "timings"); + + FOR_EACH_IMPL (impl, 0) + do_one_test (json_ctx, impl, hs, ne, NULL); + + json_array_end (json_ctx); + json_element_object_end (json_ctx); + } + + /* Hard needle for standard algorithm testing first few characters of + * needle. */ + { + for (int i = 0; i < hs_len; i++) + hs[i] = (rand () & 255) >= 128 ? 'a' : 'b'; + hs[hs_len] = 0; + + for (int i = 0; i < ne_len; i++) + { + if (i % 3 == 0) + ne[i] = 'a'; + else if (i % 3 == 1) + ne[i] = 'b'; + else + ne[i] = 'c'; + } + ne[ne_len] = 0; + + json_element_object_begin (json_ctx); + json_attr_uint (json_ctx, "len_haystack", hs_len); + json_attr_uint (json_ctx, "len_needle", ne_len); + json_attr_uint (json_ctx, "align_haystack", 0); + json_attr_uint (json_ctx, "align_needle", 0); + json_attr_uint (json_ctx, "fail", 1); + json_attr_string (json_ctx, "desc", "Difficult testing first 2"); + + json_array_begin (json_ctx, "timings"); + + FOR_EACH_IMPL (impl, 0) + do_one_test (json_ctx, impl, hs, ne, NULL); + + json_array_end (json_ctx); + json_element_object_end (json_ctx); + } } static int test_main (void) { + json_ctx_t json_ctx; test_init (); - printf ("%23s", ""); + json_init (&json_ctx, 0, stdout); + + json_document_begin (&json_ctx); + json_attr_string (&json_ctx, "timing_type", TIMING_TYPE); + + json_attr_object_begin (&json_ctx, "functions"); + json_attr_object_begin (&json_ctx, TEST_NAME); + json_attr_string (&json_ctx, "bench-variant", ""); + + json_array_begin (&json_ctx, "ifuncs"); FOR_EACH_IMPL (impl, 0) - printf ("\t%s", impl->name); - putchar ('\n'); + json_element_string (&json_ctx, impl->name); + json_array_end (&json_ctx); - for (size_t klen = 2; klen < 32; ++klen) - for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen) + json_array_begin (&json_ctx, "results"); + + for (size_t hlen = 8; hlen <= 256;) + for (size_t klen = 1; klen <= 16; klen++) { - do_test (0, 0, hlen, klen, 0); - do_test (0, 0, hlen, klen, 1); - do_test (0, 3, hlen, klen, 0); - do_test (0, 3, hlen, klen, 1); - do_test (0, 9, hlen, klen, 0); - do_test (0, 9, hlen, klen, 1); - do_test (0, 15, hlen, klen, 0); - do_test (0, 15, hlen, klen, 1); - - do_test (3, 0, hlen, klen, 0); - do_test (3, 0, hlen, klen, 1); - do_test (3, 3, hlen, klen, 0); - do_test (3, 3, hlen, klen, 1); - do_test (3, 9, hlen, klen, 0); - do_test (3, 9, hlen, klen, 1); - do_test (3, 15, hlen, klen, 0); - do_test (3, 15, hlen, klen, 1); - - do_test (9, 0, hlen, klen, 0); - do_test (9, 0, hlen, klen, 1); - do_test (9, 3, hlen, klen, 0); - do_test (9, 3, hlen, klen, 1); - do_test (9, 9, hlen, klen, 0); - do_test (9, 9, hlen, klen, 1); - do_test (9, 15, hlen, klen, 0); - do_test (9, 15, hlen, klen, 1); - - do_test (15, 0, hlen, klen, 0); - do_test (15, 0, hlen, klen, 1); - do_test (15, 3, hlen, klen, 0); - do_test (15, 3, hlen, klen, 1); - do_test (15, 9, hlen, klen, 0); - do_test (15, 9, hlen, klen, 1); - do_test (15, 15, hlen, klen, 0); - do_test (15, 15, hlen, klen, 1); + do_test (&json_ctx, 1, 3, hlen, klen, 0); + do_test (&json_ctx, 0, 9, hlen, klen, 1); + + do_test (&json_ctx, 1, 3, hlen + 1, klen, 0); + do_test (&json_ctx, 0, 9, hlen + 1, klen, 1); + + do_test (&json_ctx, getpagesize () - 15, 9, hlen, klen, 1); + if (hlen < 64) + { + hlen += 8; + } + else + { + hlen += 32; + } + } + + for (size_t hlen = 256; hlen <= 65536; hlen *= 2) + for (size_t klen = 4; klen <= 256; klen *= 2) + { + do_test (&json_ctx, 1, 11, hlen, klen, 0); + do_test (&json_ctx, 14, 5, hlen, klen, 1); + + do_test (&json_ctx, 1, 11, hlen + 1, klen + 1, 0); + do_test (&json_ctx, 14, 5, hlen + 1, klen + 1, 1); + + do_test (&json_ctx, 1, 11, hlen + 1, klen, 0); + do_test (&json_ctx, 14, 5, hlen + 1, klen, 1); + + do_test (&json_ctx, getpagesize () - 15, 5, hlen + 1, klen, 1); } - do_test (0, 0, page_size - 1, 16, 0); - do_test (0, 0, page_size - 1, 16, 1); + test_hard_needle (&json_ctx, 64, 65536); + test_hard_needle (&json_ctx, 256, 65536); + test_hard_needle (&json_ctx, 1024, 65536); + + json_array_end (&json_ctx); + json_attr_object_end (&json_ctx); + json_attr_object_end (&json_ctx); + json_document_end (&json_ctx); return ret; }