Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp600424imm; Wed, 1 Aug 2018 02:02:18 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdfO46+68dL9WF+ufD63ERIKMQlis8W1Zd3so7mLF26aoLMRGtoI0AKbvXKaScz2I2MHyVn X-Received: by 2002:a63:ee56:: with SMTP id n22-v6mr4520574pgk.402.1533114138250; Wed, 01 Aug 2018 02:02:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533114138; cv=none; d=google.com; s=arc-20160816; b=a3VNU4GS6shuW8wEN5cp6aBk3fwO0RzLz3Hfdb/xS/+N3aGgXwjnU+R1seRf3g0703 9JYRNnX2ZgAyXqANOLwwxz8stmmbxQgpq5Kzz6x/j8ul0vJxVmskEYGpA2XQ6Gi7Wdy3 it3afS3fSbCbyTCAlooOBRQzBLg4R8zlBsP+98HUXt+F0POiHjDXglQEQdA/B9lQDGjI 17907+oAYiZUVtmLm4P8vdEMs4qwuKEy4NQtgS2LXTU5VwW70W7O8MG6XXWOm3XRFofS 8qZ4afyvPmG56G89dtD7pUu0TSn4Wni/MoTupSQKrvO4TYsFPtviKwTEAPLMS5169gfe nn2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:cc:to:subject:from:references :in-reply-to:message-id:arc-authentication-results; bh=Zqmd1CYgobyq1ijnGcP+PooAedYMDNkqPv7RA4jRy2A=; b=C35YDCuXMZgFa+TtHlHzF4yLBMYs+NQZIFypfpm4FcK2Jhpxs27uKHMAlf96M9U5ar 411ibUtf9//EVlWCaVF9g1MZPaIj223yir9ePDyeECAxNhHTUCAPvewMiwhnRCQbcPp4 CyU1Y0xpFcvB/fcxSUid6olqx44Mibc5Uht8NhM6FH0xZ4S42dCGa7o4AVBry3PyBjD7 dOJ6wtQAdWKaS406KWVzpYLh+qnW/KjzHGNkAm2jAfas1h9EFrWJqraxSQnb8lEHFj0W 8SYnZ09yQ/OL2taWz/SQ0WArH0jKsvD9/axwJ/su7hx3XuQWvGlN9PklT7qC6NFK8GO4 Cglw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u7-v6si17476033pfi.96.2018.08.01.02.02.03; Wed, 01 Aug 2018 02:02:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388833AbeHAKp5 (ORCPT + 99 others); Wed, 1 Aug 2018 06:45:57 -0400 Received: from pegase1.c-s.fr ([93.17.236.30]:9178 "EHLO pegase1.c-s.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388250AbeHAKp5 (ORCPT ); Wed, 1 Aug 2018 06:45:57 -0400 Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 41gS2h2ZVVz9ttgK; Wed, 1 Aug 2018 11:01:00 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id I2TFBq9b32LC; Wed, 1 Aug 2018 11:01:00 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 41gS2h1GjMz9ttg5; Wed, 1 Aug 2018 11:01:00 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id C21E68B831; Wed, 1 Aug 2018 11:01:12 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id h4DBh4aprSyZ; Wed, 1 Aug 2018 11:01:12 +0200 (CEST) Received: from po14934vm.idsi0.si.c-s.fr (po15451.idsi0.si.c-s.fr [172.25.231.4]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 8795C8B756; Wed, 1 Aug 2018 11:01:12 +0200 (CEST) Received: by po14934vm.idsi0.si.c-s.fr (Postfix, from userid 0) id 804A67120B; Wed, 1 Aug 2018 09:01:12 +0000 (UTC) Message-Id: In-Reply-To: References: From: Christophe Leroy Subject: [PATCH v8 2/4] selftests/powerpc: Add test for strlen() To: Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , wei.guo.simon@gmail.com, segher@kernel.crashing.org Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Date: Wed, 1 Aug 2018 09:01:12 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds a test for strlen() string.c contains a copy of strlen() from lib/string.c The test first tests the correctness of strlen() by comparing the result with libc strlen(). It tests all cases of alignment. It them tests the duration of an aligned strlen() on a 4 bytes string, on a 16 bytes string and on a 256 bytes string. Signed-off-by: Christophe Leroy --- v8: no change v7: no change v6: refactorised the benchmark test v5: no change v4: new .../testing/selftests/powerpc/stringloops/Makefile | 5 +- .../testing/selftests/powerpc/stringloops/string.c | 36 ++++++ .../testing/selftests/powerpc/stringloops/strlen.c | 127 +++++++++++++++++++++ 3 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/powerpc/stringloops/string.c create mode 100644 tools/testing/selftests/powerpc/stringloops/strlen.c diff --git a/tools/testing/selftests/powerpc/stringloops/Makefile b/tools/testing/selftests/powerpc/stringloops/Makefile index 3862256c2b7d..779b644461c4 100644 --- a/tools/testing/selftests/powerpc/stringloops/Makefile +++ b/tools/testing/selftests/powerpc/stringloops/Makefile @@ -10,9 +10,12 @@ $(OUTPUT)/memcmp_64: CFLAGS += -m64 -maltivec $(OUTPUT)/memcmp_32: memcmp.c $(OUTPUT)/memcmp_32: CFLAGS += -m32 +$(OUTPUT)/strlen: strlen.c string.o +$(OUTPUT)/string.o: string.c + ASFLAGS = $(CFLAGS) -TEST_GEN_PROGS := memcmp_32 memcmp_64 +TEST_GEN_PROGS := memcmp_32 memcmp_64 strlen include ../../lib.mk diff --git a/tools/testing/selftests/powerpc/stringloops/string.c b/tools/testing/selftests/powerpc/stringloops/string.c new file mode 100644 index 000000000000..d05200481017 --- /dev/null +++ b/tools/testing/selftests/powerpc/stringloops/string.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * linux/lib/string.c + * + * Copyright (C) 1991, 1992 Linus Torvalds + */ + +/* + * stupid library routines.. The optimized versions should generally be found + * as inline code in + * + * These are buggy as well.. + * + * * Fri Jun 25 1999, Ingo Oeser + * - Added strsep() which will replace strtok() soon (because strsep() is + * reentrant and should be faster). Use only strsep() in new code, please. + * + * * Sat Feb 09 2002, Jason Thomas , + * Matthew Hawkins + * - Kissed strtok() goodbye + */ + +#include + +/** + * strlen - Find the length of a string + * @s: The string to be sized + */ +size_t test_strlen(const char *s) +{ + const char *sc; + + for (sc = s; *sc != '\0'; ++sc) + /* nothing */; + return sc - s; +} diff --git a/tools/testing/selftests/powerpc/stringloops/strlen.c b/tools/testing/selftests/powerpc/stringloops/strlen.c new file mode 100644 index 000000000000..9055ebc484d0 --- /dev/null +++ b/tools/testing/selftests/powerpc/stringloops/strlen.c @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include "utils.h" + +#define SIZE 256 +#define ITERATIONS 1000 +#define ITERATIONS_BENCH 100000 + +int test_strlen(const void *s); + +/* test all offsets and lengths */ +static void test_one(char *s) +{ + unsigned long offset; + + for (offset = 0; offset < SIZE; offset++) { + int x, y; + unsigned long i; + + y = strlen(s + offset); + x = test_strlen(s + offset); + + if (x != y) { + printf("strlen() returned %d, should have returned %d (%p offset %ld)\n", x, y, s, offset); + + for (i = offset; i < SIZE; i++) + printf("%02x ", s[i]); + printf("\n"); + } + } +} + +static void bench_test(char *s) +{ + struct timespec ts_start, ts_end; + int i; + + clock_gettime(CLOCK_MONOTONIC, &ts_start); + + for (i = 0; i < ITERATIONS_BENCH; i++) + test_strlen(s); + + clock_gettime(CLOCK_MONOTONIC, &ts_end); + + printf("len %3.3d : time = %.6f\n", test_strlen(s), ts_end.tv_sec - ts_start.tv_sec + (ts_end.tv_nsec - ts_start.tv_nsec) / 1e9); +} + +static int testcase(void) +{ + char *s; + unsigned long i; + + s = memalign(128, SIZE); + if (!s) { + perror("memalign"); + exit(1); + } + + srandom(1); + + memset(s, 0, SIZE); + for (i = 0; i < SIZE; i++) { + char c; + + do { + c = random() & 0x7f; + } while (!c); + s[i] = c; + test_one(s); + } + + for (i = 0; i < ITERATIONS; i++) { + unsigned long j; + + for (j = 0; j < SIZE; j++) { + char c; + + do { + c = random() & 0x7f; + } while (!c); + s[j] = c; + } + for (j = 0; j < sizeof(long); j++) { + s[SIZE - 1 - j] = 0; + test_one(s); + } + } + + for (i = 0; i < SIZE; i++) { + char c; + + do { + c = random() & 0x7f; + } while (!c); + s[i] = c; + } + + bench_test(s); + + s[16] = 0; + bench_test(s); + + s[8] = 0; + bench_test(s); + + s[4] = 0; + bench_test(s); + + s[3] = 0; + bench_test(s); + + s[2] = 0; + bench_test(s); + + s[1] = 0; + bench_test(s); + + return 0; +} + +int main(void) +{ + return test_harness(testcase, "strlen"); +} -- 2.13.3