162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Test for find_*_bit functions. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2017 Cavium. 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci/* 962306a36Sopenharmony_ci * find_bit functions are widely used in kernel, so the successful boot 1062306a36Sopenharmony_ci * is good enough test for correctness. 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * This test is focused on performance of traversing bitmaps. Two typical 1362306a36Sopenharmony_ci * scenarios are reproduced: 1462306a36Sopenharmony_ci * - randomly filled bitmap with approximately equal number of set and 1562306a36Sopenharmony_ci * cleared bits; 1662306a36Sopenharmony_ci * - sparse bitmap with few set bits at random positions. 1762306a36Sopenharmony_ci */ 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#include <linux/bitops.h> 2062306a36Sopenharmony_ci#include <linux/kernel.h> 2162306a36Sopenharmony_ci#include <linux/list.h> 2262306a36Sopenharmony_ci#include <linux/module.h> 2362306a36Sopenharmony_ci#include <linux/printk.h> 2462306a36Sopenharmony_ci#include <linux/random.h> 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci#define BITMAP_LEN (4096UL * 8 * 10) 2762306a36Sopenharmony_ci#define SPARSE 500 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_cistatic DECLARE_BITMAP(bitmap, BITMAP_LEN) __initdata; 3062306a36Sopenharmony_cistatic DECLARE_BITMAP(bitmap2, BITMAP_LEN) __initdata; 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci/* 3362306a36Sopenharmony_ci * This is Schlemiel the Painter's algorithm. It should be called after 3462306a36Sopenharmony_ci * all other tests for the same bitmap because it sets all bits of bitmap to 1. 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_cistatic int __init test_find_first_bit(void *bitmap, unsigned long len) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci unsigned long i, cnt; 3962306a36Sopenharmony_ci ktime_t time; 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci time = ktime_get(); 4262306a36Sopenharmony_ci for (cnt = i = 0; i < len; cnt++) { 4362306a36Sopenharmony_ci i = find_first_bit(bitmap, len); 4462306a36Sopenharmony_ci __clear_bit(i, bitmap); 4562306a36Sopenharmony_ci } 4662306a36Sopenharmony_ci time = ktime_get() - time; 4762306a36Sopenharmony_ci pr_err("find_first_bit: %18llu ns, %6ld iterations\n", time, cnt); 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci return 0; 5062306a36Sopenharmony_ci} 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_cistatic int __init test_find_first_and_bit(void *bitmap, const void *bitmap2, unsigned long len) 5362306a36Sopenharmony_ci{ 5462306a36Sopenharmony_ci static DECLARE_BITMAP(cp, BITMAP_LEN) __initdata; 5562306a36Sopenharmony_ci unsigned long i, cnt; 5662306a36Sopenharmony_ci ktime_t time; 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci bitmap_copy(cp, bitmap, BITMAP_LEN); 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci time = ktime_get(); 6162306a36Sopenharmony_ci for (cnt = i = 0; i < len; cnt++) { 6262306a36Sopenharmony_ci i = find_first_and_bit(cp, bitmap2, len); 6362306a36Sopenharmony_ci __clear_bit(i, cp); 6462306a36Sopenharmony_ci } 6562306a36Sopenharmony_ci time = ktime_get() - time; 6662306a36Sopenharmony_ci pr_err("find_first_and_bit: %18llu ns, %6ld iterations\n", time, cnt); 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci return 0; 6962306a36Sopenharmony_ci} 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_cistatic int __init test_find_next_bit(const void *bitmap, unsigned long len) 7262306a36Sopenharmony_ci{ 7362306a36Sopenharmony_ci unsigned long i, cnt; 7462306a36Sopenharmony_ci ktime_t time; 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci time = ktime_get(); 7762306a36Sopenharmony_ci for (cnt = i = 0; i < BITMAP_LEN; cnt++) 7862306a36Sopenharmony_ci i = find_next_bit(bitmap, BITMAP_LEN, i) + 1; 7962306a36Sopenharmony_ci time = ktime_get() - time; 8062306a36Sopenharmony_ci pr_err("find_next_bit: %18llu ns, %6ld iterations\n", time, cnt); 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci return 0; 8362306a36Sopenharmony_ci} 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_cistatic int __init test_find_next_zero_bit(const void *bitmap, unsigned long len) 8662306a36Sopenharmony_ci{ 8762306a36Sopenharmony_ci unsigned long i, cnt; 8862306a36Sopenharmony_ci ktime_t time; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci time = ktime_get(); 9162306a36Sopenharmony_ci for (cnt = i = 0; i < BITMAP_LEN; cnt++) 9262306a36Sopenharmony_ci i = find_next_zero_bit(bitmap, len, i) + 1; 9362306a36Sopenharmony_ci time = ktime_get() - time; 9462306a36Sopenharmony_ci pr_err("find_next_zero_bit: %18llu ns, %6ld iterations\n", time, cnt); 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci return 0; 9762306a36Sopenharmony_ci} 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_cistatic int __init test_find_last_bit(const void *bitmap, unsigned long len) 10062306a36Sopenharmony_ci{ 10162306a36Sopenharmony_ci unsigned long l, cnt = 0; 10262306a36Sopenharmony_ci ktime_t time; 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci time = ktime_get(); 10562306a36Sopenharmony_ci do { 10662306a36Sopenharmony_ci cnt++; 10762306a36Sopenharmony_ci l = find_last_bit(bitmap, len); 10862306a36Sopenharmony_ci if (l >= len) 10962306a36Sopenharmony_ci break; 11062306a36Sopenharmony_ci len = l; 11162306a36Sopenharmony_ci } while (len); 11262306a36Sopenharmony_ci time = ktime_get() - time; 11362306a36Sopenharmony_ci pr_err("find_last_bit: %18llu ns, %6ld iterations\n", time, cnt); 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci return 0; 11662306a36Sopenharmony_ci} 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_cistatic int __init test_find_nth_bit(const unsigned long *bitmap, unsigned long len) 11962306a36Sopenharmony_ci{ 12062306a36Sopenharmony_ci unsigned long l, n, w = bitmap_weight(bitmap, len); 12162306a36Sopenharmony_ci ktime_t time; 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci time = ktime_get(); 12462306a36Sopenharmony_ci for (n = 0; n < w; n++) { 12562306a36Sopenharmony_ci l = find_nth_bit(bitmap, len, n); 12662306a36Sopenharmony_ci WARN_ON(l >= len); 12762306a36Sopenharmony_ci } 12862306a36Sopenharmony_ci time = ktime_get() - time; 12962306a36Sopenharmony_ci pr_err("find_nth_bit: %18llu ns, %6ld iterations\n", time, w); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci return 0; 13262306a36Sopenharmony_ci} 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_cistatic int __init test_find_next_and_bit(const void *bitmap, 13562306a36Sopenharmony_ci const void *bitmap2, unsigned long len) 13662306a36Sopenharmony_ci{ 13762306a36Sopenharmony_ci unsigned long i, cnt; 13862306a36Sopenharmony_ci ktime_t time; 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci time = ktime_get(); 14162306a36Sopenharmony_ci for (cnt = i = 0; i < BITMAP_LEN; cnt++) 14262306a36Sopenharmony_ci i = find_next_and_bit(bitmap, bitmap2, BITMAP_LEN, i + 1); 14362306a36Sopenharmony_ci time = ktime_get() - time; 14462306a36Sopenharmony_ci pr_err("find_next_and_bit: %18llu ns, %6ld iterations\n", time, cnt); 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci return 0; 14762306a36Sopenharmony_ci} 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_cistatic int __init find_bit_test(void) 15062306a36Sopenharmony_ci{ 15162306a36Sopenharmony_ci unsigned long nbits = BITMAP_LEN / SPARSE; 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci pr_err("\nStart testing find_bit() with random-filled bitmap\n"); 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci get_random_bytes(bitmap, sizeof(bitmap)); 15662306a36Sopenharmony_ci get_random_bytes(bitmap2, sizeof(bitmap2)); 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci test_find_next_bit(bitmap, BITMAP_LEN); 15962306a36Sopenharmony_ci test_find_next_zero_bit(bitmap, BITMAP_LEN); 16062306a36Sopenharmony_ci test_find_last_bit(bitmap, BITMAP_LEN); 16162306a36Sopenharmony_ci test_find_nth_bit(bitmap, BITMAP_LEN / 10); 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci /* 16462306a36Sopenharmony_ci * test_find_first_bit() may take some time, so 16562306a36Sopenharmony_ci * traverse only part of bitmap to avoid soft lockup. 16662306a36Sopenharmony_ci */ 16762306a36Sopenharmony_ci test_find_first_bit(bitmap, BITMAP_LEN / 10); 16862306a36Sopenharmony_ci test_find_first_and_bit(bitmap, bitmap2, BITMAP_LEN / 2); 16962306a36Sopenharmony_ci test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN); 17062306a36Sopenharmony_ci 17162306a36Sopenharmony_ci pr_err("\nStart testing find_bit() with sparse bitmap\n"); 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci bitmap_zero(bitmap, BITMAP_LEN); 17462306a36Sopenharmony_ci bitmap_zero(bitmap2, BITMAP_LEN); 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci while (nbits--) { 17762306a36Sopenharmony_ci __set_bit(get_random_u32_below(BITMAP_LEN), bitmap); 17862306a36Sopenharmony_ci __set_bit(get_random_u32_below(BITMAP_LEN), bitmap2); 17962306a36Sopenharmony_ci } 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci test_find_next_bit(bitmap, BITMAP_LEN); 18262306a36Sopenharmony_ci test_find_next_zero_bit(bitmap, BITMAP_LEN); 18362306a36Sopenharmony_ci test_find_last_bit(bitmap, BITMAP_LEN); 18462306a36Sopenharmony_ci test_find_nth_bit(bitmap, BITMAP_LEN); 18562306a36Sopenharmony_ci test_find_first_bit(bitmap, BITMAP_LEN); 18662306a36Sopenharmony_ci test_find_first_and_bit(bitmap, bitmap2, BITMAP_LEN); 18762306a36Sopenharmony_ci test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN); 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_ci /* 19062306a36Sopenharmony_ci * Everything is OK. Return error just to let user run benchmark 19162306a36Sopenharmony_ci * again without annoying rmmod. 19262306a36Sopenharmony_ci */ 19362306a36Sopenharmony_ci return -EINVAL; 19462306a36Sopenharmony_ci} 19562306a36Sopenharmony_cimodule_init(find_bit_test); 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 198