From: Levi Yun <ppbuk5246@gmail.com>
To: akpm@linux-foundation.org, yury.norov@gmail.com,
andriy.shevchenko@linux.intel.com,
richard.weiyang@linux.alibaba.com, christian.brauner@ubuntu.com,
arnd@arndb.de, jpoimboe@redhat.com, changbin.du@intel.com,
rdunlap@infradead.org, masahiroy@kernel.org,
gregkh@linuxfoundation.org, peterz@infradead.org,
peter.enderborg@sony.com, krzk@kernel.org,
brendanhiggins@google.com, keescook@chromium.org,
broonie@kernel.org, matti.vaittinen@fi.rohmeurope.com,
mhiramat@kernel.org, jpa@git.mail.kapsi.fi,
nivedita@alum.mit.edu, glider@google.com, orson.zhai@unisoc.com,
takahiro.akashi@linaro.org, clm@fb.com, josef@toxicpanda.com,
dsterba@suse.com, dushistov@mail.ru
Cc: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org,
linux-btrfs@vger.kernel.org
Subject: [PATCH v2 6/8] lib/find_bit: Add test-module,find_last_zero_bit
Date: Sun, 6 Dec 2020 15:49:35 +0900 [thread overview]
Message-ID: <20201206064935.GA6252@ubuntu> (raw)
Add test module to test find_last_zero_bit and
find_each_*_bit_revserse.
Signed-off-by: Levi Yun <ppbuk5246@gmail.com>
---
lib/Kconfig.debug | 8 ++
lib/Makefile | 1 +
lib/test_find_last_bit.c | 235 +++++++++++++++++++++++++++++++++++++++
3 files changed, 244 insertions(+)
create mode 100644 lib/test_find_last_bit.c
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index c789b39ed527..481e366d0e82 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2188,6 +2188,14 @@ config FIND_BIT_BENCHMARK
If unsure, say N.
+config TEST_FIND_LAST_BIT
+ tristate "Test find_last_*_bit functions"
+ help
+ This builds the "test_find_last_bit" module that measure find_last_*_bit()
+ functions performance.
+
+ If unsure, say N.
+
config TEST_FIRMWARE
tristate "Test firmware loading via userspace interface"
depends on FW_LOADER
diff --git a/lib/Makefile b/lib/Makefile
index ce45af50983a..310719df6207 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -58,6 +58,7 @@ 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_FIND_LAST_BIT) += test_find_last_bit.o
obj-$(CONFIG_TEST_BPF) += test_bpf.o
obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
obj-$(CONFIG_TEST_BITOPS) += test_bitops.o
diff --git a/lib/test_find_last_bit.c b/lib/test_find_last_bit.c
new file mode 100644
index 000000000000..579866c476d1
--- /dev/null
+++ b/lib/test_find_last_bit.c
@@ -0,0 +1,235 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test for find_prev_*_bit functions.
+ *
+ * Copyright (c) 2020 Levi Yun.
+ */
+
+#include <linux/bitops.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/random.h>
+
+
+#define BITMAP_LEN (4096UL * 8 * 10)
+#define BITMAP_LAST_IDX BITMAP_LEN - 1
+
+
+static DECLARE_BITMAP(zero_bitmap, BITMAP_LEN) __initdata;
+static DECLARE_BITMAP(full_bitmap, BITMAP_LEN) __initdata;
+
+static int __init test_for_each_clear_rev(const void *bitmap, unsigned long len)
+{
+ unsigned long cur;
+ unsigned long expect = BITMAP_LAST_IDX;
+
+ pr_err("[%s:%d] Start test clear full iteration\n", __func__, __LINE__);
+
+ len = BITMAP_LEN;
+
+ for_each_clear_bit_reverse(cur, bitmap, len) {
+ if (cur != expect) {
+ pr_err("[%s:%d] full iteration fail!, expect: %lu, cur: %lu\n",
+ __func__, __LINE__, expect, cur);
+
+ return 0;
+ }
+
+ expect--;
+ }
+
+ len = BITMAP_LEN;
+
+ cur = len / 3;
+ expect = cur;
+ pr_err("[%s:%d] Success\n", __func__, __LINE__);
+
+ pr_err("[%s:%d] Start test clear iteration with starting point\n", __func__, __LINE__);
+ for_each_clear_bit_from_reverse(cur, bitmap, len) {
+ if (cur != expect) {
+ pr_err("[%s:%d] full iteration fail!, expect: %lu, cur: %lu\n",
+ __func__, __LINE__, expect, cur);
+
+ return 0;
+ }
+
+ expect--;
+ }
+
+ pr_err("[%s:%d] Success\n", __func__, __LINE__);
+
+ return 0;
+}
+
+static int __init test_for_each_set_rev(const void *bitmap, unsigned long len)
+{
+ unsigned long cur;
+ unsigned long expect = BITMAP_LAST_IDX;
+
+ len = BITMAP_LEN;
+ pr_err("[%s:%d] Start test set full iteration\n", __func__, __LINE__);
+ for_each_set_bit_reverse(cur, bitmap, len) {
+ if (cur != expect) {
+ pr_err("[%s:%d] full iteration fail!, expect: %lu, cur: %lu\n",
+ __func__, __LINE__, expect, cur);
+
+ return 0;
+ }
+
+ expect--;
+ }
+ pr_err("[%s:%d] Success\n", __func__, __LINE__);
+
+
+ len = BITMAP_LEN;
+ cur = len / 3;
+ expect = cur;
+
+ pr_err("[%s:%d] Start test set iteration with starting point\n", __func__, __LINE__);
+ for_each_set_bit_from_reverse(cur, bitmap, len) {
+ if (cur != expect) {
+ pr_err("[%s:%d] full iteration fail!, expect: %lu, cur: %lu\n",
+ __func__, __LINE__, expect, cur);
+
+ return 0;
+ }
+
+ expect--;
+ }
+ pr_err("[%s:%d] Success\n", __func__, __LINE__);
+
+ return 0;
+}
+
+static int __init test_find_last_bit_clear_middle(void *bitmap)
+{
+ unsigned long idx;
+ unsigned long len = 4096;
+
+ pr_err("[%s] Start test\n", __func__);
+
+ clear_bit(1, bitmap);
+ clear_bit(65, bitmap);
+ clear_bit(136, bitmap);
+ clear_bit(4095, bitmap);
+
+ idx = find_last_zero_bit(bitmap, len);
+ if (idx != 4095) {
+ pr_err("[%s] Fail! expect: %u, idx: %lu\n", __func__, 4095, idx);
+
+ goto teardown;
+ }
+
+ idx = find_last_zero_bit(bitmap, idx);
+ if (idx != 136) {
+ pr_err("[%s] Fail! expect: %u, idx: %lu\n", __func__, 127, idx);
+
+ goto teardown;
+ }
+
+ idx = find_last_zero_bit(bitmap, idx);
+ if (idx != 65) {
+ pr_err("[%s] Fail! expect: %u, idx: %lu\n", __func__, 65, idx);
+
+ goto teardown;
+ }
+
+ idx = find_last_zero_bit(bitmap, idx);
+ if (idx != 1) {
+ pr_err("[%s] Fail! expect: %u, idx: %lu\n", __func__, 1, idx);
+
+ goto teardown;
+ }
+
+ idx = find_last_zero_bit(bitmap, idx);
+ if (idx != 1) {
+ pr_err("[%s] Fail (find no bit clear)! expect: %u, idx: %lu\n", __func__, 1, idx);
+
+ goto teardown;
+ }
+
+ pr_err("[%s] Success\n", __func__);
+
+teardown:
+ set_bit(4095, bitmap);
+ set_bit(136, bitmap);
+ set_bit(65, bitmap);
+ set_bit(1, bitmap);
+
+ return 0;
+}
+
+static int __init test_find_last_zero_bit(void *bitmap, unsigned long len)
+{
+ unsigned long idx;
+ pr_err("[%s] Start test\n", __func__);
+
+ idx = find_last_zero_bit(bitmap, len);
+ if (idx != len) {
+ pr_err("[%s] Fail! expect: %lu, idx: %lu\n", __func__, len, idx);
+
+ goto teardown;
+ }
+
+ clear_bit(4095, bitmap);
+ clear_bit(2032, bitmap);
+ clear_bit(0, bitmap);
+
+ idx = find_last_zero_bit(bitmap, len);
+ if (idx != 4095) {
+ pr_err("[%s] Fail! expect: %u, idx: %lu\n", __func__, 4095, idx);
+
+ goto teardown;
+ }
+
+ idx = find_last_zero_bit(bitmap, idx);
+ if (idx != 2032) {
+ pr_err("[%s] Fail! expect: %u, idx: %lu\n", __func__, 2032, idx);
+
+ goto teardown;
+ }
+
+ idx = find_last_zero_bit(bitmap, idx);
+ if (idx != 0) {
+ pr_err("[%s] Fail! expect: %u, idx: %lu\n", __func__, 0, idx);
+
+ goto teardown;
+ }
+
+ pr_err("[%s] Success\n", __func__);
+
+teardown:
+ set_bit(0, bitmap);
+ set_bit(2032, bitmap);
+ set_bit(4095, bitmap);
+
+ return 0;
+}
+
+static int __init find_last_bit_test(void)
+{
+ bitmap_zero(zero_bitmap, BITMAP_LEN);
+ bitmap_fill(full_bitmap, BITMAP_LEN);
+ pr_err("\nStart testing find_last_*_bit()\n");
+
+ test_for_each_clear_rev(zero_bitmap, BITMAP_LEN);
+ test_for_each_set_rev(full_bitmap, BITMAP_LEN);
+ test_find_last_bit_clear_middle(full_bitmap);
+ test_find_last_zero_bit(full_bitmap, BITMAP_LEN);
+
+ pr_err("\nFinish testing find_last_*_bit()\n");
+
+ /*
+ * Everything is OK. Return error just to let user run benchmark
+ * again without annoying rmmod.
+ */
+ return -EINVAL;
+}
+
+
+module_init(find_last_bit_test);
+
+
+MODULE_LICENSE("GPL");
--
2.27.0
reply other threads:[~2020-12-06 6:50 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20201206064935.GA6252@ubuntu \
--to=ppbuk5246@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=arnd@arndb.de \
--cc=brendanhiggins@google.com \
--cc=broonie@kernel.org \
--cc=changbin.du@intel.com \
--cc=christian.brauner@ubuntu.com \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=dushistov@mail.ru \
--cc=glider@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=josef@toxicpanda.com \
--cc=jpa@git.mail.kapsi.fi \
--cc=jpoimboe@redhat.com \
--cc=keescook@chromium.org \
--cc=krzk@kernel.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=masahiroy@kernel.org \
--cc=matti.vaittinen@fi.rohmeurope.com \
--cc=mhiramat@kernel.org \
--cc=nivedita@alum.mit.edu \
--cc=orson.zhai@unisoc.com \
--cc=peter.enderborg@sony.com \
--cc=peterz@infradead.org \
--cc=rdunlap@infradead.org \
--cc=richard.weiyang@linux.alibaba.com \
--cc=takahiro.akashi@linaro.org \
--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