find_bit_benchmark.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Test for find_*_bit functions.
  4. *
  5. * Copyright (c) 2017 Cavium.
  6. */
  7. /*
  8. * find_bit functions are widely used in kernel, so the successful boot
  9. * is good enough test for correctness.
  10. *
  11. * This test is focused on performance of traversing bitmaps. Two typical
  12. * scenarios are reproduced:
  13. * - randomly filled bitmap with approximately equal number of set and
  14. * cleared bits;
  15. * - sparse bitmap with few set bits at random positions.
  16. */
  17. #include <linux/bitops.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/printk.h>
  22. #include <linux/random.h>
  23. #define BITMAP_LEN (4096UL * 8 * 10)
  24. #define SPARSE 500
  25. static DECLARE_BITMAP(bitmap, BITMAP_LEN) __initdata;
  26. static DECLARE_BITMAP(bitmap2, BITMAP_LEN) __initdata;
  27. /*
  28. * This is Schlemiel the Painter's algorithm. It should be called after
  29. * all other tests for the same bitmap because it sets all bits of bitmap to 1.
  30. */
  31. static int __init test_find_first_bit(void *bitmap, unsigned long len)
  32. {
  33. unsigned long i, cnt;
  34. ktime_t time;
  35. time = ktime_get();
  36. for (cnt = i = 0; i < len; cnt++) {
  37. i = find_first_bit(bitmap, len);
  38. __clear_bit(i, bitmap);
  39. }
  40. time = ktime_get() - time;
  41. pr_err("find_first_bit: %18llu ns, %6ld iterations\n", time, cnt);
  42. return 0;
  43. }
  44. static int __init test_find_first_and_bit(void *bitmap, const void *bitmap2, unsigned long len)
  45. {
  46. static DECLARE_BITMAP(cp, BITMAP_LEN) __initdata;
  47. unsigned long i, cnt;
  48. ktime_t time;
  49. bitmap_copy(cp, bitmap, BITMAP_LEN);
  50. time = ktime_get();
  51. for (cnt = i = 0; i < len; cnt++) {
  52. i = find_first_and_bit(cp, bitmap2, len);
  53. __clear_bit(i, cp);
  54. }
  55. time = ktime_get() - time;
  56. pr_err("find_first_and_bit: %18llu ns, %6ld iterations\n", time, cnt);
  57. return 0;
  58. }
  59. static int __init test_find_next_bit(const void *bitmap, unsigned long len)
  60. {
  61. unsigned long i, cnt;
  62. ktime_t time;
  63. time = ktime_get();
  64. for (cnt = i = 0; i < BITMAP_LEN; cnt++)
  65. i = find_next_bit(bitmap, BITMAP_LEN, i) + 1;
  66. time = ktime_get() - time;
  67. pr_err("find_next_bit: %18llu ns, %6ld iterations\n", time, cnt);
  68. return 0;
  69. }
  70. static int __init test_find_next_zero_bit(const void *bitmap, unsigned long len)
  71. {
  72. unsigned long i, cnt;
  73. ktime_t time;
  74. time = ktime_get();
  75. for (cnt = i = 0; i < BITMAP_LEN; cnt++)
  76. i = find_next_zero_bit(bitmap, len, i) + 1;
  77. time = ktime_get() - time;
  78. pr_err("find_next_zero_bit: %18llu ns, %6ld iterations\n", time, cnt);
  79. return 0;
  80. }
  81. static int __init test_find_last_bit(const void *bitmap, unsigned long len)
  82. {
  83. unsigned long l, cnt = 0;
  84. ktime_t time;
  85. time = ktime_get();
  86. do {
  87. cnt++;
  88. l = find_last_bit(bitmap, len);
  89. if (l >= len)
  90. break;
  91. len = l;
  92. } while (len);
  93. time = ktime_get() - time;
  94. pr_err("find_last_bit: %18llu ns, %6ld iterations\n", time, cnt);
  95. return 0;
  96. }
  97. static int __init test_find_nth_bit(const unsigned long *bitmap, unsigned long len)
  98. {
  99. unsigned long l, n, w = bitmap_weight(bitmap, len);
  100. ktime_t time;
  101. time = ktime_get();
  102. for (n = 0; n < w; n++) {
  103. l = find_nth_bit(bitmap, len, n);
  104. WARN_ON(l >= len);
  105. }
  106. time = ktime_get() - time;
  107. pr_err("find_nth_bit: %18llu ns, %6ld iterations\n", time, w);
  108. return 0;
  109. }
  110. static int __init test_find_next_and_bit(const void *bitmap,
  111. const void *bitmap2, unsigned long len)
  112. {
  113. unsigned long i, cnt;
  114. ktime_t time;
  115. time = ktime_get();
  116. for (cnt = i = 0; i < BITMAP_LEN; cnt++)
  117. i = find_next_and_bit(bitmap, bitmap2, BITMAP_LEN, i + 1);
  118. time = ktime_get() - time;
  119. pr_err("find_next_and_bit: %18llu ns, %6ld iterations\n", time, cnt);
  120. return 0;
  121. }
  122. static int __init find_bit_test(void)
  123. {
  124. unsigned long nbits = BITMAP_LEN / SPARSE;
  125. pr_err("\nStart testing find_bit() with random-filled bitmap\n");
  126. get_random_bytes(bitmap, sizeof(bitmap));
  127. get_random_bytes(bitmap2, sizeof(bitmap2));
  128. test_find_next_bit(bitmap, BITMAP_LEN);
  129. test_find_next_zero_bit(bitmap, BITMAP_LEN);
  130. test_find_last_bit(bitmap, BITMAP_LEN);
  131. test_find_nth_bit(bitmap, BITMAP_LEN / 10);
  132. /*
  133. * test_find_first_bit() may take some time, so
  134. * traverse only part of bitmap to avoid soft lockup.
  135. */
  136. test_find_first_bit(bitmap, BITMAP_LEN / 10);
  137. test_find_first_and_bit(bitmap, bitmap2, BITMAP_LEN / 2);
  138. test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
  139. pr_err("\nStart testing find_bit() with sparse bitmap\n");
  140. bitmap_zero(bitmap, BITMAP_LEN);
  141. bitmap_zero(bitmap2, BITMAP_LEN);
  142. while (nbits--) {
  143. __set_bit(prandom_u32_max(BITMAP_LEN), bitmap);
  144. __set_bit(prandom_u32_max(BITMAP_LEN), bitmap2);
  145. }
  146. test_find_next_bit(bitmap, BITMAP_LEN);
  147. test_find_next_zero_bit(bitmap, BITMAP_LEN);
  148. test_find_last_bit(bitmap, BITMAP_LEN);
  149. test_find_nth_bit(bitmap, BITMAP_LEN);
  150. test_find_first_bit(bitmap, BITMAP_LEN);
  151. test_find_first_and_bit(bitmap, bitmap2, BITMAP_LEN);
  152. test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
  153. /*
  154. * Everything is OK. Return error just to let user run benchmark
  155. * again without annoying rmmod.
  156. */
  157. return -EINVAL;
  158. }
  159. module_init(find_bit_test);
  160. MODULE_LICENSE("GPL");