From: Tamir Duberstein <tamird@kernel.org>
To: David Gow <davidgow@google.com>,
John Hubbard <jhubbard@nvidia.com>,
Andrew Morton <akpm@linux-foundation.org>,
Geert Uytterhoeven <geert@linux-m68k.org>,
Madhavan Srinivasan <maddy@linux.ibm.com>,
Michael Ellerman <mpe@ellerman.id.au>,
Nicholas Piggin <npiggin@gmail.com>,
Naveen N Rao <naveen@kernel.org>,
Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Shuah Khan <shuah@kernel.org>, Kees Cook <kees@kernel.org>,
Christophe Leroy <chleroy@kernel.org>
Cc: Muhammad Usama Anjum <usama.anjum@collabora.com>,
linux-kernel@vger.kernel.org, linux-m68k@lists.linux-m68k.org,
linuxppc-dev@lists.ozlabs.org, linux-kselftest@vger.kernel.org,
Tamir Duberstein <tamird@gmail.com>,
Alexander Potapenko <glider@google.com>
Subject: [PATCH v2 1/3] test_bitmap: extract benchmark module
Date: Mon, 22 Dec 2025 14:39:54 +0100 [thread overview]
Message-ID: <20251222-bitmap-kunit-convert-v2-1-6a61a5330eff@gmail.com> (raw)
In-Reply-To: <20251222-bitmap-kunit-convert-v2-0-6a61a5330eff@gmail.com>
From: Tamir Duberstein <tamird@gmail.com>
Add CONFIG_BITMAP_BENCHMARK to build benchmarks separately from unit
tests. The new module is marked authored by the author of commit
991e5583647d ("lib/test_bitmap: add tests for bitmap_{read,write}()")
which added these benchmarks.
Extract duplicated code between `test_bitmap_{read,write}_perf`.
Cc: Alexander Potapenko <glider@google.com>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
MAINTAINERS | 1 +
lib/Kconfig.debug | 8 +++++
lib/Makefile | 1 +
lib/bitmap_benchmark.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++
lib/test_bitmap.c | 50 ----------------------------
5 files changed, 99 insertions(+), 50 deletions(-)
diff --git a/MAINTAINERS b/MAINTAINERS
index 5b11839cba9d..4ddaef15b408 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4426,6 +4426,7 @@ F: include/uapi/linux/bits.h
F: include/vdso/bits.h
F: lib/bitmap-str.c
F: lib/bitmap.c
+F: lib/bitmap_benchmark.c
F: lib/cpumask.c
F: lib/find_bit.c
F: lib/find_bit_benchmark.c
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index ba36939fda79..60434b31a940 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2492,6 +2492,14 @@ config FFS_KUNIT_TEST
config TEST_KSTRTOX
tristate "Test kstrto*() family of functions at runtime"
+config BITMAP_BENCHMARK
+ tristate "Benchmark bitmap_*() family of functions"
+ help
+ This builds the "bitmap_benchmark_test" module that measure bitmap_*()
+ family of functions performance.
+
+ If unsure, say N.
+
config TEST_BITMAP
tristate "Test bitmap_*() family of functions at runtime"
help
diff --git a/lib/Makefile b/lib/Makefile
index aaf677cf4527..892cb3152b65 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -61,6 +61,7 @@ obj-y += string_helpers.o
obj-y += hexdump.o
obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
obj-y += kstrtox.o
+obj-$(CONFIG_BITMAP_BENCHMARK) += bitmap_benchmark.o
obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o
obj-$(CONFIG_FIND_BIT_BENCHMARK_RUST) += find_bit_benchmark_rust.o
obj-$(CONFIG_TEST_BPF) += test_bpf.o
diff --git a/lib/bitmap_benchmark.c b/lib/bitmap_benchmark.c
new file mode 100644
index 000000000000..47f4cea4e831
--- /dev/null
+++ b/lib/bitmap_benchmark.c
@@ -0,0 +1,89 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Benchmarks for bitmap API.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/bitmap.h>
+#include <linux/init.h>
+#include <linux/ktime.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+
+/*
+ * Test bitmap should be big enough to include the cases when start is not in
+ * the first word, and start+nbits lands in the following word.
+ */
+#define TEST_BIT_LEN (1000)
+
+typedef void (*bitmap_bench_fn)(unsigned long *bitmap, unsigned long i, unsigned long nbits);
+
+static void __init bench_bitmap(bitmap_bench_fn bench_fn, const char *name)
+{
+ DECLARE_BITMAP(bitmap, TEST_BIT_LEN);
+ unsigned int cnt, nbits, i;
+ ktime_t time;
+
+ bitmap_fill(bitmap, TEST_BIT_LEN);
+ time = ktime_get();
+ for (cnt = 0; cnt < 5; cnt++) {
+ for (nbits = 1; nbits <= BITS_PER_LONG; nbits++) {
+ for (i = 0; i < TEST_BIT_LEN; i++) {
+ if (i + nbits > TEST_BIT_LEN)
+ break;
+ bench_fn(bitmap, i, nbits);
+ }
+ }
+ }
+ time = ktime_get() - time;
+ pr_info("Time spent in %s:\t%llu\n", name, time);
+}
+
+#undef TEST_BIT_LEN
+
+static inline void bitmap_read_bench(unsigned long *bitmap, unsigned long i, unsigned long nbits)
+{
+ unsigned long val;
+ /*
+ * Prevent the compiler from optimizing away the
+ * bitmap_read() by using its value.
+ */
+ WRITE_ONCE(val, bitmap_read(bitmap, i, nbits));
+}
+
+static void __init test_bitmap_read_perf(void)
+{
+ bench_bitmap(bitmap_read_bench, __func__);
+}
+
+static inline void bitmap_write_bench(unsigned long *bitmap, unsigned long i, unsigned long nbits)
+{
+ unsigned long val = 0xfeedface;
+
+ bitmap_write(bitmap, val, i, nbits);
+}
+
+static void __init test_bitmap_write_perf(void)
+{
+ bench_bitmap(bitmap_write_bench, __func__);
+}
+
+static int __init bitmap_benchmark_init(void)
+{
+ test_bitmap_read_perf();
+ test_bitmap_write_perf();
+
+ return 0;
+}
+module_init(bitmap_benchmark_init);
+
+static void __exit bitmap_benchmark_exit(void)
+{
+ pr_info("Unloaded\n");
+}
+module_exit(bitmap_benchmark_exit);
+
+MODULE_AUTHOR("Alexander Potapenko <glider@google.com>");
+MODULE_DESCRIPTION("Benchmarks for bitmap API");
+MODULE_LICENSE("GPL");
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index c83829ef557f..f728b6485c88 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -1372,54 +1372,6 @@ static void __init test_bitmap_read_write(void)
test_bitmap_write_helper(pattern[pi]);
}
-static void __init test_bitmap_read_perf(void)
-{
- DECLARE_BITMAP(bitmap, TEST_BIT_LEN);
- unsigned int cnt, nbits, i;
- unsigned long val;
- ktime_t time;
-
- bitmap_fill(bitmap, TEST_BIT_LEN);
- time = ktime_get();
- for (cnt = 0; cnt < 5; cnt++) {
- for (nbits = 1; nbits <= BITS_PER_LONG; nbits++) {
- for (i = 0; i < TEST_BIT_LEN; i++) {
- if (i + nbits > TEST_BIT_LEN)
- break;
- /*
- * Prevent the compiler from optimizing away the
- * bitmap_read() by using its value.
- */
- WRITE_ONCE(val, bitmap_read(bitmap, i, nbits));
- }
- }
- }
- time = ktime_get() - time;
- pr_info("Time spent in %s:\t%llu\n", __func__, time);
-}
-
-static void __init test_bitmap_write_perf(void)
-{
- DECLARE_BITMAP(bitmap, TEST_BIT_LEN);
- unsigned int cnt, nbits, i;
- unsigned long val = 0xfeedface;
- ktime_t time;
-
- bitmap_zero(bitmap, TEST_BIT_LEN);
- time = ktime_get();
- for (cnt = 0; cnt < 5; cnt++) {
- for (nbits = 1; nbits <= BITS_PER_LONG; nbits++) {
- for (i = 0; i < TEST_BIT_LEN; i++) {
- if (i + nbits > TEST_BIT_LEN)
- break;
- bitmap_write(bitmap, val, i, nbits);
- }
- }
- }
- time = ktime_get() - time;
- pr_info("Time spent in %s:\t%llu\n", __func__, time);
-}
-
#undef TEST_BIT_LEN
static void __init selftest(void)
@@ -1440,8 +1392,6 @@ static void __init selftest(void)
test_bitmap_print_buf();
test_bitmap_const_eval();
test_bitmap_read_write();
- test_bitmap_read_perf();
- test_bitmap_write_perf();
test_find_nth_bit();
test_for_each_set_bit();
--
2.52.0
next prev parent reply other threads:[~2025-12-22 21:45 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-22 13:39 [PATCH v2 0/3] bitmap: convert self-test to KUnit Tamir Duberstein
2025-12-22 13:39 ` Tamir Duberstein [this message]
2025-12-22 13:39 ` [PATCH v2 2/3] " Tamir Duberstein
2025-12-22 13:39 ` [PATCH v2 3/3] bitmap: break kunit into test cases Tamir Duberstein
2025-12-23 13:22 ` kernel test robot
2025-12-23 14:18 ` kernel test robot
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=20251222-bitmap-kunit-convert-v2-1-6a61a5330eff@gmail.com \
--to=tamird@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=chleroy@kernel.org \
--cc=davidgow@google.com \
--cc=geert@linux-m68k.org \
--cc=glider@google.com \
--cc=jhubbard@nvidia.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-m68k@lists.linux-m68k.org \
--cc=linux@rasmusvillemoes.dk \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=mpe@ellerman.id.au \
--cc=naveen@kernel.org \
--cc=npiggin@gmail.com \
--cc=shuah@kernel.org \
--cc=tamird@gmail.com \
--cc=usama.anjum@collabora.com \
--cc=yury.norov@gmail.com \
/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 NNTP newsgroup(s).