2 * Test for find_*_bit functions.
4 * Copyright (c) 2017 Cavium.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
17 * find_bit functions are widely used in kernel, so the successful boot
18 * is good enough test for correctness.
20 * This test is focused on performance of traversing bitmaps. Two typical
21 * scenarios are reproduced:
22 * - randomly filled bitmap with approximately equal number of set and
24 * - sparse bitmap with few set bits at random positions.
27 #include <linux/bitops.h>
28 #include <linux/kernel.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/printk.h>
32 #include <linux/random.h>
34 #define BITMAP_LEN (4096UL * 8 * 10)
37 static DECLARE_BITMAP(bitmap
, BITMAP_LEN
) __initdata
;
40 * This is Schlemiel the Painter's algorithm. It should be called after
41 * all other tests for the same bitmap because it sets all bits of bitmap to 1.
43 static int __init
test_find_first_bit(void *bitmap
, unsigned long len
)
48 cycles
= get_cycles();
49 for (cnt
= i
= 0; i
< len
; cnt
++) {
50 i
= find_first_bit(bitmap
, len
);
51 __clear_bit(i
, bitmap
);
53 cycles
= get_cycles() - cycles
;
54 pr_err("find_first_bit:\t\t%llu cycles,\t%ld iterations\n",
60 static int __init
test_find_next_bit(const void *bitmap
, unsigned long len
)
65 cycles
= get_cycles();
66 for (cnt
= i
= 0; i
< BITMAP_LEN
; cnt
++)
67 i
= find_next_bit(bitmap
, BITMAP_LEN
, i
) + 1;
68 cycles
= get_cycles() - cycles
;
69 pr_err("find_next_bit:\t\t%llu cycles,\t%ld iterations\n",
75 static int __init
test_find_next_zero_bit(const void *bitmap
, unsigned long len
)
80 cycles
= get_cycles();
81 for (cnt
= i
= 0; i
< BITMAP_LEN
; cnt
++)
82 i
= find_next_zero_bit(bitmap
, len
, i
) + 1;
83 cycles
= get_cycles() - cycles
;
84 pr_err("find_next_zero_bit:\t%llu cycles,\t%ld iterations\n",
90 static int __init
test_find_last_bit(const void *bitmap
, unsigned long len
)
92 unsigned long l
, cnt
= 0;
95 cycles
= get_cycles();
98 l
= find_last_bit(bitmap
, len
);
103 cycles
= get_cycles() - cycles
;
104 pr_err("find_last_bit:\t\t%llu cycles,\t%ld iterations\n",
110 static int __init
find_bit_test(void)
112 unsigned long nbits
= BITMAP_LEN
/ SPARSE
;
114 pr_err("\nStart testing find_bit() with random-filled bitmap\n");
116 get_random_bytes(bitmap
, sizeof(bitmap
));
118 test_find_next_bit(bitmap
, BITMAP_LEN
);
119 test_find_next_zero_bit(bitmap
, BITMAP_LEN
);
120 test_find_last_bit(bitmap
, BITMAP_LEN
);
121 test_find_first_bit(bitmap
, BITMAP_LEN
);
123 pr_err("\nStart testing find_bit() with sparse bitmap\n");
125 bitmap_zero(bitmap
, BITMAP_LEN
);
128 __set_bit(prandom_u32() % BITMAP_LEN
, bitmap
);
130 test_find_next_bit(bitmap
, BITMAP_LEN
);
131 test_find_next_zero_bit(bitmap
, BITMAP_LEN
);
132 test_find_last_bit(bitmap
, BITMAP_LEN
);
133 test_find_first_bit(bitmap
, BITMAP_LEN
);
137 module_init(find_bit_test
);
139 static void __exit
test_find_bit_cleanup(void)
142 module_exit(test_find_bit_cleanup
);
144 MODULE_LICENSE("GPL");