Skip to contentSkip to conversation

lib: test module for find_*_bit() functions

PATCH In review Yury Norov 6 participants 11 messages Nov 9, 2017 – Nov 24, 2017

Subsystems (2)
lore
Share & tools
lore

Yury Norov

author
#0 lore raw

PATCH 1/1 lib: test module for find_*_bit() functions

find_bit functions are widely used in the kernel, including hot paths. This module tests performance of that functions in 2 typical scenarios:
randomly filled bitmap with relatively equal distribution of set and cleared bits, and sparse bitmap which has 1 set bit for 500 cleared bits.

On ThunderX machine:

Start testing find_bit() with random-filled bitmap

[1032111.632383] find_next_bit:		240043 cycles,	164062 iterations
[1032111.647236] find_next_zero_bit:	312848 cycles,	163619 iterations
[1032111.661585] find_last_bit:		193748 cycles,	164062 iterations
[1032113.450517] find_first_bit:	177720874 cycles,	164062 iterations
[1032113.462930]
		 Start testing find_bit() with sparse bitmap
[1032113.477229] find_next_bit:		3633 cycles,	656 iterations
[1032113.494281] find_next_zero_bit:	620399 cycles,	327025 iterations
[1032113.506723] find_last_bit:		3038 cycles,	656 iterations
[1032113.524485] find_first_bit:	691407 cycles,	656 iterations

v2:
- pretty printing;
- increase length of bitmap to suppress noise;
- drop inlining _find_next_bit();

CC Alexey Dobriyan <[email protected]>
CC Andrew Morton <[email protected]>
CC Clement Courbet <[email protected]>
CC Matthew Wilcox <[email protected]>
CC Rasmus Villemoes <[email protected]>
---
 lib/Kconfig.debug   |   9 ++++
 lib/Makefile        |   1 +
 lib/test_find_bit.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 151 insertions(+)
 create mode 100644 lib/test_find_bit.c
lib/Kconfig.debug +9 -0
index dfdad67d8f6c..138034cc68a3 100644
@@ -1838,6 +1838,15 @@ config TEST_BPF
 
 	  If unsure, say N.
 
+config TEST_FIND_BIT
+	tristate "Test find_bit functions"
+	default n
+	help
+	  This builds the "test_find_bit" module that measure find_*_bit()
+	  functions performance.
+
+	  If unsure, say N.
+
 config TEST_FIRMWARE
 	tristate "Test firmware loading via userspace interface"
 	default n
lib/Makefile +1 -0
index b8f2c16fccaa..97ec69d843f2 100644
@@ -46,6 +46,7 @@ obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
 obj-y += kstrtox.o
 obj-$(CONFIG_TEST_BPF) += test_bpf.o
+obj-$(CONFIG_TEST_FIND_BIT) += test_find_bit.o
 obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
 obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
 obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o
lib/test_find_bit.c new file +141 -0

141 lines not on this page · raw message

Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/1 lib: test module for find_*_bit() functions

Thanks for the addition, Yury ! I've used a modified version of v1 for measuring improvements from find_next_and_bit() on x86 and arm and found it very useful.

maintainer
#2 lore raw
In reply to Yury Norov
Quoted 8 lines · On Thu, 9 Nov 2017 17:07:14 +0300 Yury Norov <[email protected]> wrote:

find_bit functions are widely used in the kernel, including hot paths. This module tests performance of that functions in 2 typical scenarios:
randomly filled bitmap with relatively equal distribution of set and cleared bits, and sparse bitmap which has 1 set bit for 500 cleared bits.

...

+config TEST_FIND_BIT

Well. It doesn't actually "test" the code. It measures its performance ;)

#3 lore raw
Quoted 8 lines · Andrew Morton
Quoted 5 lines · On Thu, 9 Nov 2017 17:07:14 +0300 Yury Norov <[email protected]> wrote:

find_bit functions are widely used in the kernel, including hot paths. This module tests performance of that functions in 2 typical scenarios:
randomly filled bitmap with relatively equal distribution of set and cleared bits, and sparse bitmap which has 1 set bit for 500 cleared bits.

Quoted 5 lines
Quoted 3 lines

...

+config TEST_FIND_BIT

Well. It doesn't actually "test" the code. It measures its performance ;)

Yes!

Yyra, you can grab CONFIG_BENCHMARK_* namespace :-)

Another thing:

lib/test_find_bit.c line 139

Quoted 9 lines
lib/test_find_bit.c new file +9 -0
+
+       return 0;
+}
+module_init(find_bit_test);
+
+static void __exit test_find_bit_cleanup(void)
+{
+}
+module_exit(test_find_bit_cleanup);

module exit hook is entirely unnecessary as you can return -E from init hook. See lib/test-kstrtox.c

author
#4 lore raw

Hi Alexey, Andrew,

Thanks for comments.

Quoted 11 lines · Alexey Dobriyan
Quoted 10 lines · Andrew Morton
Quoted 7 lines · On Thu, 9 Nov 2017 17:07:14 +0300 Yury Norov <[email protected]> wrote:

find_bit functions are widely used in the kernel, including hot paths. This module tests performance of that functions in 2 typical scenarios:
randomly filled bitmap with relatively equal distribution of set and cleared bits, and sparse bitmap which has 1 set bit for 500 cleared bits.

...

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

+config TEST_FIND_BIT

Well. It doesn't actually "test" the code. It measures its performance ;)

Yes!

Yyra, you can grab CONFIG_BENCHMARK_* namespace :-)

There's no CONFIG_BENCHMARK_* namespace actually. The 'CONFIG_*_BENCHMARK' is referenced only 3 times in linux sources - CONFIG_RING_BUFFER_BENCHMARK, CONFIG_TRACEPOINT_BENCHMARK and CONFIG_GUP_BENCHMARK, so I simply didn't know about it. Some other tests like lib/rbtree_test.c also measure performance and use TEST namespace, but if you think it's better, I don't object to change it.

Quoted 14 lines

Another thing:

lib/test_find_bit.c line 139

Quoted 9 lines
lib/test_find_bit.c new file +9 -0
+
+       return 0;
+}
+module_init(find_bit_test);
+
+static void __exit test_find_bit_cleanup(void)
+{
+}
+module_exit(test_find_bit_cleanup);

module exit hook is entirely unnecessary as you can return -E from init hook. See lib/test-kstrtox.c

Ack.

I thought to send v3, but the patch is already in next tree, so I'll send fix in separated patch. OK?

Yury

maintainer
#5 lore raw
Quoted 3 lines · On Tue, 14 Nov 2017 13:07:30 +0300 Yury Norov <[email protected]> wrote:
Quoted 1 line

Yyra, you can grab CONFIG_BENCHMARK_* namespace :-)

There's no CONFIG_BENCHMARK_* namespace actually.

Alexey means you can be the first user of CONFIG_BENCHMARK_*.

Quoted 21 lines

The 'CONFIG_*_BENCHMARK' is
referenced only 3 times in linux sources - CONFIG_RING_BUFFER_BENCHMARK, CONFIG_TRACEPOINT_BENCHMARK and CONFIG_GUP_BENCHMARK, so I simply didn't know about it. Some other tests like lib/rbtree_test.c also measure performance and use TEST namespace, but if you think it's better, I don't object to change it.

Quoted 14 lines

Another thing:

lib/test_find_bit.c line 139

Quoted 9 lines
lib/test_find_bit.c new file +9 -0
+
+       return 0;
+}
+module_init(find_bit_test);
+
+static void __exit test_find_bit_cleanup(void)
+{
+}
+module_exit(test_find_bit_cleanup);

module exit hook is entirely unnecessary as you can return -E from init hook. See lib/test-kstrtox.c

Quoted 4 lines

Ack.

I thought to send v3, but the patch is already in next tree, so I'll send fix in separated patch. OK?

Either approach is OK.

#6 lore raw
In reply to Yury Norov
Quoted 8 lines · Yury Norov

find_bit functions are widely used in the kernel, including hot paths. This module tests performance of that functions in 2 typical scenarios:
randomly filled bitmap with relatively equal distribution of set and cleared bits, and sparse bitmap which has 1 set bit for 500 cleared bits.

On ThunderX machine:

Start testing find_bit() with random-filled bitmap

Quoted 10 lines
[1032111.632383] find_next_bit:		240043 cycles,	164062 iterations
[1032111.647236] find_next_zero_bit:	312848 cycles,	163619 iterations
[1032111.661585] find_last_bit:		193748 cycles,	164062 iterations
[1032113.450517] find_first_bit:	177720874 cycles,	164062 iterations
[1032113.462930]
		 Start testing find_bit() with sparse bitmap
[1032113.477229] find_next_bit:		3633 cycles,	656 iterations
[1032113.494281] find_next_zero_bit:	620399 cycles,	327025 iterations
[1032113.506723] find_last_bit:		3038 cycles,	656 iterations
[1032113.524485] find_first_bit:	691407 cycles,	656 iterations

Have you thought about timing it rather than using get_cycles()?

get_cycles() has the downside that it can't be compared across different architectures or even platforms within an architecture.

cheers

author
#7 lore raw

Hi Michael,

Quoted 21 lines · Michael Ellerman
Quoted 8 lines · Yury Norov

find_bit functions are widely used in the kernel, including hot paths. This module tests performance of that functions in 2 typical scenarios:
randomly filled bitmap with relatively equal distribution of set and cleared bits, and sparse bitmap which has 1 set bit for 500 cleared bits.

On ThunderX machine:

Start testing find_bit() with random-filled bitmap

Quoted 10 lines
[1032111.632383] find_next_bit:		240043 cycles,	164062 iterations
[1032111.647236] find_next_zero_bit:	312848 cycles,	163619 iterations
[1032111.661585] find_last_bit:		193748 cycles,	164062 iterations
[1032113.450517] find_first_bit:	177720874 cycles,	164062 iterations
[1032113.462930]
		 Start testing find_bit() with sparse bitmap
[1032113.477229] find_next_bit:		3633 cycles,	656 iterations
[1032113.494281] find_next_zero_bit:	620399 cycles,	327025 iterations
[1032113.506723] find_last_bit:		3038 cycles,	656 iterations
[1032113.524485] find_first_bit:	691407 cycles,	656 iterations
Quoted 4 lines

Have you thought about timing it rather than using get_cycles()?

get_cycles() has the downside that it can't be compared across different architectures or even platforms within an architecture.

This test is written to benchmark find_bit() on the same target if algorithm is changed. Comparing different architectures looks problematic anyway.

Different architectures may have different clock rates, and even implementations of the function, like ARM. And many CPUs support dynamic changing of CPU speed which will directly affect time of execution. So I don't think that direct comparison of time across platforms would be informative without additional precautions.

Also, other tests, like lib/interval_tree_test.c or lib/rbtree_test.c print get_cycles() where they need to estimate performance, and it looks like common practice.

Do you have real usecase for it?

Yury

#8 lore raw
In reply to Yury Norov

Hi Yury,

Quoted 4 lines · Yury Norov

find_bit functions are widely used in the kernel, including hot paths. This module tests performance of that functions in 2 typical scenarios:
randomly filled bitmap with relatively equal distribution of set and cleared bits, and sparse bitmap which has 1 set bit for 500 cleared bits.

Thanks for your patch!

Quoted 13 lines

On ThunderX machine:

Start testing find_bit() with random-filled bitmap

[1032111.632383] find_next_bit:         240043 cycles,  164062 iterations
[1032111.647236] find_next_zero_bit:    312848 cycles,  163619 iterations
[1032111.661585] find_last_bit:         193748 cycles,  164062 iterations
[1032113.450517] find_first_bit:        177720874 cycles,       164062 iterations
[1032113.462930]
                 Start testing find_bit() with sparse bitmap
[1032113.477229] find_next_bit:         3633 cycles,    656 iterations
[1032113.494281] find_next_zero_bit:    620399 cycles,  327025 iterations
[1032113.506723] find_last_bit:         3038 cycles,    656 iterations
[1032113.524485] find_first_bit:        691407 cycles,  656 iterations

That's what ends up in dmesg, but that's not what it prints on the console:
"\t" prints an ugly placeholder, as TABs are not supported.

lib/test_find_bit.c line 48

Quoted 10 lines
lib/test_find_bit.c new file +6 -0
@@ -0,0 +1,141 @@
 
+static int __init test_find_first_bit(void *bitmap, unsigned long len)
+{
+       unsigned long i, cnt;
+       cycles_t cycles;
+
+       cycles = get_cycles();

get_cycles() returns zero on half of the architectures.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author
#9 lore raw

Hi Geert, all

Below the updates proposed in this thread.

Yury

Quoted 1 line

From 959700bd7e7f586171c15a4130a9888acac02daf Mon Sep 17 00:00:00 2001

From: Yury Norov <[email protected]>
Date: Wed, 22 Nov 2017 17:21:40 +0300
Subject: [PATCH] improve lib/test_find_bit

As suggested in review comments:
* printk: align numbers using whitespaces instead of tabs;
* return error value from init() to avoid calling rmmod if testing again;
* use ktime_get instead of get_cycles as some arches don't support it;
* rename test_find_bit.c to find_bit_benchmark.c.

The output in dmesg (on QEMU arm64):

[   38.823430] Start testing find_bit() with random-filled bitmap
[   38.845358] find_next_bit:                20138448 ns, 163968 iterations
[   38.856217] find_next_zero_bit:           10615328 ns, 163713 iterations
[   38.863564] find_last_bit:                 7111888 ns, 163967 iterations
[   40.944796] find_first_bit:             2081007216 ns, 163968 iterations
[   40.944975]
[   40.944975] Start testing find_bit() with sparse bitmap
[   40.945268] find_next_bit:                   73216 ns,    656 iterations
[   40.967858] find_next_zero_bit:           22461008 ns, 327025 iterations
[   40.968047] find_last_bit:                   62320 ns,    656 iterations
[   40.978060] find_first_bit:                9889360 ns,    656 iterations
---
 lib/Kconfig.debug                             |  2 +-
 lib/Makefile                                  |  2 +-
 lib/{test_find_bit.c => find_bit_benchmark.c} | 47 ++++++++++++---------------
 3 files changed, 23 insertions(+), 28 deletions(-)
 rename lib/{test_find_bit.c => find_bit_benchmark.c} (79%)
lib/Kconfig.debug +1 -1
index 138034cc68a3..1c57c36ad79a 100644
@@ -1838,7 +1838,7 @@ config TEST_BPF
 
 	  If unsure, say N.
 
-config TEST_FIND_BIT
+config FIND_BIT_BENCHMARK
 	tristate "Test find_bit functions"
 	default n
 	help
lib/Makefile +1 -1
index 97ec69d843f2..1688cc84536e 100644
@@ -45,8 +45,8 @@ obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
 obj-y += hexdump.o
 obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
 obj-y += kstrtox.o
+obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o
 obj-$(CONFIG_TEST_BPF) += test_bpf.o
-obj-$(CONFIG_TEST_FIND_BIT) += test_find_bit.o
 obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
 obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
 obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o
lib/find_bit_benchmark.c renamed from lib/test_find_bit.c +21 -26

96 lines not on this page · raw message

Signature · 1 line
2.11.0
#10 lore raw

Hi Yury,

Quoted 1 line · Yury Norov

Below the updates proposed in this thread.

Thank you!

Quoted 24 lines

From 959700bd7e7f586171c15a4130a9888acac02daf Mon Sep 17 00:00:00 2001
From: Yury Norov <[email protected]>
Date: Wed, 22 Nov 2017 17:21:40 +0300
Subject: [PATCH] improve lib/test_find_bit

As suggested in review comments:
* printk: align numbers using whitespaces instead of tabs;
* return error value from init() to avoid calling rmmod if testing again;
* use ktime_get instead of get_cycles as some arches don't support it;
* rename test_find_bit.c to find_bit_benchmark.c.

The output in dmesg (on QEMU arm64):

[   38.823430] Start testing find_bit() with random-filled bitmap
[   38.845358] find_next_bit:                20138448 ns, 163968 iterations
[   38.856217] find_next_zero_bit:           10615328 ns, 163713 iterations
[   38.863564] find_last_bit:                 7111888 ns, 163967 iterations
[   40.944796] find_first_bit:             2081007216 ns, 163968 iterations
[   40.944975]
[   40.944975] Start testing find_bit() with sparse bitmap
[   40.945268] find_next_bit:                   73216 ns,    656 iterations
[   40.967858] find_next_zero_bit:           22461008 ns, 327025 iterations
[   40.968047] find_last_bit:                   62320 ns,    656 iterations
[   40.978060] find_first_bit:                9889360 ns,    656 iterations
Quoted 1 line

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds