18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Test for find_*_bit functions.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2017 Cavium.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * find_bit functions are widely used in kernel, so the successful boot
108c2ecf20Sopenharmony_ci * is good enough test for correctness.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * This test is focused on performance of traversing bitmaps. Two typical
138c2ecf20Sopenharmony_ci * scenarios are reproduced:
148c2ecf20Sopenharmony_ci * - randomly filled bitmap with approximately equal number of set and
158c2ecf20Sopenharmony_ci *   cleared bits;
168c2ecf20Sopenharmony_ci * - sparse bitmap with few set bits at random positions.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/bitops.h>
208c2ecf20Sopenharmony_ci#include <linux/kernel.h>
218c2ecf20Sopenharmony_ci#include <linux/list.h>
228c2ecf20Sopenharmony_ci#include <linux/module.h>
238c2ecf20Sopenharmony_ci#include <linux/printk.h>
248c2ecf20Sopenharmony_ci#include <linux/random.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define BITMAP_LEN	(4096UL * 8 * 10)
278c2ecf20Sopenharmony_ci#define SPARSE		500
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic DECLARE_BITMAP(bitmap, BITMAP_LEN) __initdata;
308c2ecf20Sopenharmony_cistatic DECLARE_BITMAP(bitmap2, BITMAP_LEN) __initdata;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * This is Schlemiel the Painter's algorithm. It should be called after
348c2ecf20Sopenharmony_ci * all other tests for the same bitmap because it sets all bits of bitmap to 1.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_cistatic int __init test_find_first_bit(void *bitmap, unsigned long len)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	unsigned long i, cnt;
398c2ecf20Sopenharmony_ci	ktime_t time;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	time = ktime_get();
428c2ecf20Sopenharmony_ci	for (cnt = i = 0; i < len; cnt++) {
438c2ecf20Sopenharmony_ci		i = find_first_bit(bitmap, len);
448c2ecf20Sopenharmony_ci		__clear_bit(i, bitmap);
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci	time = ktime_get() - time;
478c2ecf20Sopenharmony_ci	pr_err("find_first_bit:     %18llu ns, %6ld iterations\n", time, cnt);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	return 0;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int __init test_find_next_bit(const void *bitmap, unsigned long len)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	unsigned long i, cnt;
558c2ecf20Sopenharmony_ci	ktime_t time;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	time = ktime_get();
588c2ecf20Sopenharmony_ci	for (cnt = i = 0; i < BITMAP_LEN; cnt++)
598c2ecf20Sopenharmony_ci		i = find_next_bit(bitmap, BITMAP_LEN, i) + 1;
608c2ecf20Sopenharmony_ci	time = ktime_get() - time;
618c2ecf20Sopenharmony_ci	pr_err("find_next_bit:      %18llu ns, %6ld iterations\n", time, cnt);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return 0;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int __init test_find_next_zero_bit(const void *bitmap, unsigned long len)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	unsigned long i, cnt;
698c2ecf20Sopenharmony_ci	ktime_t time;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	time = ktime_get();
728c2ecf20Sopenharmony_ci	for (cnt = i = 0; i < BITMAP_LEN; cnt++)
738c2ecf20Sopenharmony_ci		i = find_next_zero_bit(bitmap, len, i) + 1;
748c2ecf20Sopenharmony_ci	time = ktime_get() - time;
758c2ecf20Sopenharmony_ci	pr_err("find_next_zero_bit: %18llu ns, %6ld iterations\n", time, cnt);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	return 0;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic int __init test_find_last_bit(const void *bitmap, unsigned long len)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	unsigned long l, cnt = 0;
838c2ecf20Sopenharmony_ci	ktime_t time;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	time = ktime_get();
868c2ecf20Sopenharmony_ci	do {
878c2ecf20Sopenharmony_ci		cnt++;
888c2ecf20Sopenharmony_ci		l = find_last_bit(bitmap, len);
898c2ecf20Sopenharmony_ci		if (l >= len)
908c2ecf20Sopenharmony_ci			break;
918c2ecf20Sopenharmony_ci		len = l;
928c2ecf20Sopenharmony_ci	} while (len);
938c2ecf20Sopenharmony_ci	time = ktime_get() - time;
948c2ecf20Sopenharmony_ci	pr_err("find_last_bit:      %18llu ns, %6ld iterations\n", time, cnt);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	return 0;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic int __init test_find_next_and_bit(const void *bitmap,
1008c2ecf20Sopenharmony_ci		const void *bitmap2, unsigned long len)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	unsigned long i, cnt;
1038c2ecf20Sopenharmony_ci	ktime_t time;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	time = ktime_get();
1068c2ecf20Sopenharmony_ci	for (cnt = i = 0; i < BITMAP_LEN; cnt++)
1078c2ecf20Sopenharmony_ci		i = find_next_and_bit(bitmap, bitmap2, BITMAP_LEN, i + 1);
1088c2ecf20Sopenharmony_ci	time = ktime_get() - time;
1098c2ecf20Sopenharmony_ci	pr_err("find_next_and_bit:  %18llu ns, %6ld iterations\n", time, cnt);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	return 0;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic int __init find_bit_test(void)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	unsigned long nbits = BITMAP_LEN / SPARSE;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	pr_err("\nStart testing find_bit() with random-filled bitmap\n");
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	get_random_bytes(bitmap, sizeof(bitmap));
1218c2ecf20Sopenharmony_ci	get_random_bytes(bitmap2, sizeof(bitmap2));
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	test_find_next_bit(bitmap, BITMAP_LEN);
1248c2ecf20Sopenharmony_ci	test_find_next_zero_bit(bitmap, BITMAP_LEN);
1258c2ecf20Sopenharmony_ci	test_find_last_bit(bitmap, BITMAP_LEN);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/*
1288c2ecf20Sopenharmony_ci	 * test_find_first_bit() may take some time, so
1298c2ecf20Sopenharmony_ci	 * traverse only part of bitmap to avoid soft lockup.
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	test_find_first_bit(bitmap, BITMAP_LEN / 10);
1328c2ecf20Sopenharmony_ci	test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	pr_err("\nStart testing find_bit() with sparse bitmap\n");
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	bitmap_zero(bitmap, BITMAP_LEN);
1378c2ecf20Sopenharmony_ci	bitmap_zero(bitmap2, BITMAP_LEN);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	while (nbits--) {
1408c2ecf20Sopenharmony_ci		__set_bit(prandom_u32() % BITMAP_LEN, bitmap);
1418c2ecf20Sopenharmony_ci		__set_bit(prandom_u32() % BITMAP_LEN, bitmap2);
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	test_find_next_bit(bitmap, BITMAP_LEN);
1458c2ecf20Sopenharmony_ci	test_find_next_zero_bit(bitmap, BITMAP_LEN);
1468c2ecf20Sopenharmony_ci	test_find_last_bit(bitmap, BITMAP_LEN);
1478c2ecf20Sopenharmony_ci	test_find_first_bit(bitmap, BITMAP_LEN);
1488c2ecf20Sopenharmony_ci	test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	/*
1518c2ecf20Sopenharmony_ci	 * Everything is OK. Return error just to let user run benchmark
1528c2ecf20Sopenharmony_ci	 * again without annoying rmmod.
1538c2ecf20Sopenharmony_ci	 */
1548c2ecf20Sopenharmony_ci	return -EINVAL;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_cimodule_init(find_bit_test);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
159