From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F72E37204C for ; Tue, 12 May 2026 14:10:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778595024; cv=none; b=FmAXC0fkeBq4gyRFYQLDNwiRkm8n4SE/XXr1eaflFhJhsXKcKEDs7PZ6iaDXpLoCPV3vPY/WSshHlko0KM7LrtxDMjmPKHMAThi2ACWrSou2Esxvkc7SX+S4y/3I0GKmZrLt5cl+c7t1Ylx2+XapH/OsAqwWHXOLk2v3CZOYNJE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778595024; c=relaxed/simple; bh=txJBVGCj5lPXuzoOyE37eYL22tcWPxaCU6EO+05oHCM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UTFBBEg79lAh7emC05OvSEF/taolJtD/JS3fopOdO8nqD0RVjp7EgmX/SPEZZHpbI+EHhQPjQQdm1jJ6LeofPvqwyDZ69gfKJjGUJZb4pHgF0d7krcaMA99ubozvjNF48EYHf4f6X0CgUIVEBkuzsTOErLlJbF2+K2j39wHb5iY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=qxwqCILx; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qxwqCILx" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-48e56c1bf5dso34534935e9.3 for ; Tue, 12 May 2026 07:10:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778595022; x=1779199822; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=3QcDRa4FA3P0xEz4R3NFtiEh2UMv0DUcl8go6uXzNDQ=; b=qxwqCILx7FdgCjVs40s3LSw6jdNPH7jwtH23xbYH9OAjmKFF9vgwbq4iUhqwVeJrNU N0y6gAB8q1r7+8+CjRlHBC5CyA1bHQ8ayjfURzu0RD08ydxWKQTbBM85RHyvhY5YHJnC L3joyE97TPJ6V2xBgOyFZ/Dey0ClED3b7p9X0RWxwwq8SxZYopV03hGk68FRS1pagX8P V+xr+Jw77Mkg/BhKUg885juJ4GxQJNcrcqBa32U1car30scxl5ZzoizWLsxlSbKDPJ9b gY8ZDtAba/2LH/n4SE340Po1gOLz7GC27Yx4r+bMgyJRwJoAL3M+Yw+/zR/Cr7rzuVLL nHdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778595022; x=1779199822; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=3QcDRa4FA3P0xEz4R3NFtiEh2UMv0DUcl8go6uXzNDQ=; b=lDvo/6+YZLKangRlAVSTr+6oaWgpJT1//QDQVNLX2xwzSzbS3i8/kpHk+BnvLcvUjP zzFeEotOVVhkJeyfZN86ZiJTNlu6MWe3NWm2EpFUs1WytwivEpBfx7IwToAqG9iCz+1z LAiOlLlY7PvvWWo7pZoekzmCxDwoHkABnjXxAUP1RRyOeTG/eogHlfwSsrwBc5bmp8WP 9L1JwYhL7IEPs232KehN/aJZJ97t4cRjzNN9JjJbaE34nkJOCdPRMyXbMETKt3QxKssB zLC5LQflRTcz+/4muRds03887Va9WALXXX545eorHegz9DoJq3Bfe/H5HIBmqK0fHHv3 2PsA== X-Forwarded-Encrypted: i=1; AFNElJ8ytZURrAY6cckvitX1s8npkQJHxbyNjBWXPtSM/Pyy/sK9e7Uk83D7FDN0qynAF63kgY3cdgAqyUCxxuk=@vger.kernel.org X-Gm-Message-State: AOJu0YypvEfX3F/Y0xdZBkOnOAxALoHcR7q5nbWeNH+MTN2YtGAqEUbp dukbqpxkWW+cuH+qkcaH2SRUfKM7nEFPK0Ie0NU5jtIcNRcY4mqPIdQf X-Gm-Gg: Acq92OHHp9Cpd1Cv7em3KwiKHHH4mquLC8F29W+iQFL6I17MgP9tuQF6ehl21wn7yBe Tpj5GAIH9yTUXtDIGrm7Mdsrxzf1vPkyez8SpefuYpCkSRssQ39aM4Ff08ROq2c5jG11K8WzFF1 180DQhZP+5X16R0GQishZJVp2Z5LE58+YKwTcgg4AfIQz/ON5Zv66c8Esfg/8U63UhYPyKCaRq3 IrVyJDlQ77l0cFrLJVjMOmAZTSMWJGbxiV0esn0EIG1fy+2oBBqxdGkxkUvCR47sWBnHmztIlGO GpYcqzTbhdx5uObld+Rdl7SJou/NYk+66817XSo5WT/mKTKePjUtwlDzk5ueHf1lzNfGcwnoiH9 CW+V74cAOGu2rYka1QTjsZ+KUK2wWc3tCTlW6n8edngUpfZ2SmKv71CMvLAxO4cFhZQzt6fHuc1 siaUXgSbxYVKE0WWc/a0jgDNEInLwk8fl5c3IeNTgBLw== X-Received: by 2002:a05:600c:4c94:b0:48a:58ae:9933 with SMTP id 5b1f17b1804b1-48e96e69168mr21884325e9.18.1778595021278; Tue, 12 May 2026 07:10:21 -0700 (PDT) Received: from RTRKW671-LIN.domain.local ([77.243.27.125]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48e8e568b04sm40557965e9.0.2026.05.12.07.10.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 May 2026 07:10:20 -0700 (PDT) From: Milan Tripkovic To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Alexandre Ghiti , Dusan Stojkovic , Milan Tripkovic , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Milan Tripkovic Subject: [PATCH 2/2] lib/string_kunit: extend benchmarks and unit test to memcmp() Date: Tue, 12 May 2026 16:10:07 +0200 Message-ID: <20260512141007.1193033-2-milant2002@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260512141007.1193033-1-milant2002@gmail.com> References: <20260512141007.1193033-1-milant2002@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Milan Tripkovic Extend the string benchmarking suite to include memcmp(). Extend the string unit test to include memcmp(). Signed-off-by: Milan Tripkovic --- lib/tests/string_kunit.c | 102 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/lib/tests/string_kunit.c b/lib/tests/string_kunit.c index 0819ace5b..7b4aa6eee 100644 --- a/lib/tests/string_kunit.c +++ b/lib/tests/string_kunit.c @@ -880,6 +880,106 @@ static void string_bench_strrchr(struct kunit *test) { STRING_BENCH_BUF(test, buf, len, strrchr, buf, '\0'); } +static void string_test_memcmp(struct kunit *test) +{ + const int max_offset = 16; + const int max_len = 32; + const int buf_size = max_offset + max_len + 32; + u8 *buf1, *buf2; + int i, j, len, k; + + buf1 = kunit_kzalloc(test, buf_size, GFP_KERNEL); + buf2 = kunit_kzalloc(test, buf_size, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf1); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf2); + + for (i = 0; i < max_offset; i++) { + for (j = 0; j < max_offset; j++) { + for (len = 0; len <= max_len; len++) { + memset(buf1, 'A', buf_size); + memset(buf2, 'A', buf_size); + KUNIT_EXPECT_EQ_MSG(test, memcmp(buf1 + i, buf2 + j, len), 0, + "Should be equal: i:%d j:%d len:%d", i, j, len); + for (k = 0; k < len; k++) { + memset(buf1, 'A', buf_size); + memset(buf2, 'A', buf_size); + buf2[j + k] = 'B'; + int res = memcmp(buf1 + i, buf2 + j, len); + + KUNIT_EXPECT_NE_MSG(test, res, 0, + "Should detect difference at k:%d (i:%d j:%d len:%d)", + k, i, j, len); + + if (buf1[i + k] < buf2[j + k]) + KUNIT_EXPECT_LT(test, res, 0); + else + KUNIT_EXPECT_GT(test, res, 0); + } + } + } + } +} +#if IS_ENABLED(CONFIG_STRING_KUNIT_BENCH) +static void string_bench_memcmp(struct kunit *test) +{ + char *buf1, *buf2; + size_t lengths[] = { 1, 7, 8, 16, 32, 64, 128, 512, 1024, 4096}; + int offsets[] = {0, 1, 3, 7}; + const size_t max_len = 4096 + 64; + int res = 0; + + buf1 = vmalloc(max_len); + buf2 = vmalloc(max_len); + + if (!buf1 || !buf2) { + vfree(buf1); + vfree(buf2); + kunit_err(test, "vmalloc failed\n"); + return; + } + + memset(buf1, 'A', max_len); + memset(buf2, 'A', max_len); + + for (int i = 0; i < 100000; i++) + res += memcmp(buf1, buf2, 4096); + + for (int o = 0; o < ARRAY_SIZE(offsets); o++) { + int off = offsets[o]; + + for (int i = 0; i < ARRAY_SIZE(lengths); i++) { + size_t len = lengths[i]; + char *p1 = buf1; + char *p2 = buf2 + off; + + u32 iterations = (len < 512) ? 100000 : 10000; + + for (u32 j = 0; j < iterations; j++) { + asm volatile("" : : "g"(p1), "g"(p2), "g"(len) : "memory"); + int res = memcmp(p1, p2, len); + + asm volatile("" : : "g"(res) : "memory"); + } + + u64 elapsed = STRING_BENCH(iterations, memcmp, p1, p2, len); + u64 ns_per_call = div_u64(elapsed, iterations); + u64 mbps = len ? div_u64((u64)len * iterations * 1000, + elapsed) : 0; + + if (off == 0) { + kunit_info(test, "bench_memcmp_aligned: len=%-4zu: %llu MB/s (%llu ns/call)\n", + len, mbps, ns_per_call); + } else { + kunit_info(test, "bench_memcmp_unaligned(off=%d): len=%-4zu: %llu MB/s (%llu ns/call)\n", + off, len, mbps, ns_per_call); + } + } + } + + vfree(buf1); + vfree(buf2); +} +#endif static struct kunit_case string_test_cases[] = { KUNIT_CASE(string_test_memset16), @@ -910,6 +1010,8 @@ static struct kunit_case string_test_cases[] = { KUNIT_CASE(string_bench_strnlen), KUNIT_CASE(string_bench_strchr), KUNIT_CASE(string_bench_strrchr), + KUNIT_CASE(string_test_memcmp), + KUNIT_CASE_SLOW(string_bench_memcmp), {} }; -- 2.43.0