* [PATCH] lib/find_bit_benchmark: avoid clearing randomly filled bitmap in test_find_first_bit()
@ 2026-03-10 15:21 Akinobu Mita
2026-03-10 16:57 ` Yury Norov
0 siblings, 1 reply; 2+ messages in thread
From: Akinobu Mita @ 2026-03-10 15:21 UTC (permalink / raw)
To: linux-kernel; +Cc: akpm, Akinobu Mita, Yury Norov, Rasmus Villemoes
test_find_first_bit() searches for a set bit from the beginning of the
test bitmap and clears it repeatedly, eventually clearing the entire
bitmap.
After test_find_first_bit() is executed, test_find_first_and_bit() and
test_find_next_and_bit() are executed without randomly reinitializing the
cleared bitmap.
In the first phase (testing find_bit() with a random-filled bitmap),
test_find_first_bit() only operates on 1/10 of the entire size of the
testing bitmap, so this isn't a big problem.
However, in the second phase (testing find_bit() with a sparse bitmap),
test_find_first_bit() clears the entire test bitmap, so the subsequent
test_find_first_and_bit() and test_find_next_and_bit() will not find any
set bits. This is probably not the intended benchmark.
To fix this issue, test_find_first_bit() operates on a duplicated bitmap
and does not clear the original test bitmap.
The same is already done in test_find_first_and_bit().
While we're at it, add const qualifiers to the bitmap pointer arguments
in the test functions.
Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
Cc: Yury Norov <yury.norov@gmail.com>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
lib/find_bit_benchmark.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/lib/find_bit_benchmark.c b/lib/find_bit_benchmark.c
index 402e160e7186..00d9dc61cd46 100644
--- a/lib/find_bit_benchmark.c
+++ b/lib/find_bit_benchmark.c
@@ -30,18 +30,20 @@ static DECLARE_BITMAP(bitmap, BITMAP_LEN) __initdata;
static DECLARE_BITMAP(bitmap2, BITMAP_LEN) __initdata;
/*
- * This is Schlemiel the Painter's algorithm. It should be called after
- * all other tests for the same bitmap because it sets all bits of bitmap to 1.
+ * This is Schlemiel the Painter's algorithm.
*/
-static int __init test_find_first_bit(void *bitmap, unsigned long len)
+static int __init test_find_first_bit(const void *bitmap, unsigned long len)
{
+ static DECLARE_BITMAP(cp, BITMAP_LEN) __initdata;
unsigned long i, cnt;
ktime_t time;
+ bitmap_copy(cp, bitmap, BITMAP_LEN);
+
time = ktime_get();
for (cnt = i = 0; i < len; cnt++) {
- i = find_first_bit(bitmap, len);
- __clear_bit(i, bitmap);
+ i = find_first_bit(cp, len);
+ __clear_bit(i, cp);
}
time = ktime_get() - time;
pr_err("find_first_bit: %18llu ns, %6ld iterations\n", time, cnt);
@@ -49,7 +51,8 @@ static int __init test_find_first_bit(void *bitmap, unsigned long len)
return 0;
}
-static int __init test_find_first_and_bit(void *bitmap, const void *bitmap2, unsigned long len)
+static int __init test_find_first_and_bit(const void *bitmap, const void *bitmap2,
+ unsigned long len)
{
static DECLARE_BITMAP(cp, BITMAP_LEN) __initdata;
unsigned long i, cnt;
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] lib/find_bit_benchmark: avoid clearing randomly filled bitmap in test_find_first_bit()
2026-03-10 15:21 [PATCH] lib/find_bit_benchmark: avoid clearing randomly filled bitmap in test_find_first_bit() Akinobu Mita
@ 2026-03-10 16:57 ` Yury Norov
0 siblings, 0 replies; 2+ messages in thread
From: Yury Norov @ 2026-03-10 16:57 UTC (permalink / raw)
To: Akinobu Mita; +Cc: linux-kernel, akpm, Yury Norov, Rasmus Villemoes
On Wed, Mar 11, 2026 at 12:21:26AM +0900, Akinobu Mita wrote:
> test_find_first_bit() searches for a set bit from the beginning of the
> test bitmap and clears it repeatedly, eventually clearing the entire
> bitmap.
>
> After test_find_first_bit() is executed, test_find_first_and_bit() and
> test_find_next_and_bit() are executed without randomly reinitializing the
> cleared bitmap.
>
> In the first phase (testing find_bit() with a random-filled bitmap),
> test_find_first_bit() only operates on 1/10 of the entire size of the
> testing bitmap, so this isn't a big problem.
>
> However, in the second phase (testing find_bit() with a sparse bitmap),
> test_find_first_bit() clears the entire test bitmap, so the subsequent
> test_find_first_and_bit() and test_find_next_and_bit() will not find any
> set bits. This is probably not the intended benchmark.
>
> To fix this issue, test_find_first_bit() operates on a duplicated bitmap
> and does not clear the original test bitmap.
> The same is already done in test_find_first_and_bit().
>
> While we're at it, add const qualifiers to the bitmap pointer arguments
> in the test functions.
>
> Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
> Cc: Yury Norov <yury.norov@gmail.com>
> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Applied, thanks!
> ---
> lib/find_bit_benchmark.c | 15 +++++++++------
> 1 file changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/lib/find_bit_benchmark.c b/lib/find_bit_benchmark.c
> index 402e160e7186..00d9dc61cd46 100644
> --- a/lib/find_bit_benchmark.c
> +++ b/lib/find_bit_benchmark.c
> @@ -30,18 +30,20 @@ static DECLARE_BITMAP(bitmap, BITMAP_LEN) __initdata;
> static DECLARE_BITMAP(bitmap2, BITMAP_LEN) __initdata;
>
> /*
> - * This is Schlemiel the Painter's algorithm. It should be called after
> - * all other tests for the same bitmap because it sets all bits of bitmap to 1.
> + * This is Schlemiel the Painter's algorithm.
> */
> -static int __init test_find_first_bit(void *bitmap, unsigned long len)
> +static int __init test_find_first_bit(const void *bitmap, unsigned long len)
> {
> + static DECLARE_BITMAP(cp, BITMAP_LEN) __initdata;
> unsigned long i, cnt;
> ktime_t time;
>
> + bitmap_copy(cp, bitmap, BITMAP_LEN);
> +
> time = ktime_get();
> for (cnt = i = 0; i < len; cnt++) {
> - i = find_first_bit(bitmap, len);
> - __clear_bit(i, bitmap);
> + i = find_first_bit(cp, len);
> + __clear_bit(i, cp);
> }
> time = ktime_get() - time;
> pr_err("find_first_bit: %18llu ns, %6ld iterations\n", time, cnt);
> @@ -49,7 +51,8 @@ static int __init test_find_first_bit(void *bitmap, unsigned long len)
> return 0;
> }
>
> -static int __init test_find_first_and_bit(void *bitmap, const void *bitmap2, unsigned long len)
> +static int __init test_find_first_and_bit(const void *bitmap, const void *bitmap2,
> + unsigned long len)
> {
> static DECLARE_BITMAP(cp, BITMAP_LEN) __initdata;
> unsigned long i, cnt;
> --
> 2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-03-10 16:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-10 15:21 [PATCH] lib/find_bit_benchmark: avoid clearing randomly filled bitmap in test_find_first_bit() Akinobu Mita
2026-03-10 16:57 ` Yury Norov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox