find_bit_benchmark.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_next_bit(const void *bitmap, unsigned long len)
  45. {
  46. unsigned long i, cnt;
  47. ktime_t time;
  48. time = ktime_get();
  49. for (cnt = i = 0; i < BITMAP_LEN; cnt++)
  50. i = find_next_bit(bitmap, BITMAP_LEN, i) + 1;
  51. time = ktime_get() - time;
  52. pr_err("find_next_bit: %18llu ns, %6ld iterations\n", time, cnt);
  53. return 0;
  54. }
  55. static int __init test_find_next_zero_bit(const void *bitmap, unsigned long len)
  56. {
  57. unsigned long i, cnt;
  58. ktime_t time;
  59. time = ktime_get();
  60. for (cnt = i = 0; i < BITMAP_LEN; cnt++)
  61. i = find_next_zero_bit(bitmap, len, i) + 1;
  62. time = ktime_get() - time;
  63. pr_err("find_next_zero_bit: %18llu ns, %6ld iterations\n", time, cnt);
  64. return 0;
  65. }
  66. static int __init test_find_last_bit(const void *bitmap, unsigned long len)
  67. {
  68. unsigned long l, cnt = 0;
  69. ktime_t time;
  70. time = ktime_get();
  71. do {
  72. cnt++;
  73. l = find_last_bit(bitmap, len);
  74. if (l >= len)
  75. break;
  76. len = l;
  77. } while (len);
  78. time = ktime_get() - time;
  79. pr_err("find_last_bit: %18llu ns, %6ld iterations\n", time, cnt);
  80. return 0;
  81. }
  82. static int __init test_find_next_and_bit(const void *bitmap,
  83. const void *bitmap2, unsigned long len)
  84. {
  85. unsigned long i, cnt;
  86. ktime_t time;
  87. time = ktime_get();
  88. for (cnt = i = 0; i < BITMAP_LEN; cnt++)
  89. i = find_next_and_bit(bitmap, bitmap2, BITMAP_LEN, i + 1);
  90. time = ktime_get() - time;
  91. pr_err("find_next_and_bit: %18llu ns, %6ld iterations\n", time, cnt);
  92. return 0;
  93. }
  94. static int __init find_bit_test(void)
  95. {
  96. unsigned long nbits = BITMAP_LEN / SPARSE;
  97. pr_err("\nStart testing find_bit() with random-filled bitmap\n");
  98. get_random_bytes(bitmap, sizeof(bitmap));
  99. get_random_bytes(bitmap2, sizeof(bitmap2));
  100. test_find_next_bit(bitmap, BITMAP_LEN);
  101. test_find_next_zero_bit(bitmap, BITMAP_LEN);
  102. test_find_last_bit(bitmap, BITMAP_LEN);
  103. /*
  104. * test_find_first_bit() may take some time, so
  105. * traverse only part of bitmap to avoid soft lockup.
  106. */
  107. test_find_first_bit(bitmap, BITMAP_LEN / 10);
  108. test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
  109. pr_err("\nStart testing find_bit() with sparse bitmap\n");
  110. bitmap_zero(bitmap, BITMAP_LEN);
  111. bitmap_zero(bitmap2, BITMAP_LEN);
  112. while (nbits--) {
  113. __set_bit(prandom_u32() % BITMAP_LEN, bitmap);
  114. __set_bit(prandom_u32() % BITMAP_LEN, bitmap2);
  115. }
  116. test_find_next_bit(bitmap, BITMAP_LEN);
  117. test_find_next_zero_bit(bitmap, BITMAP_LEN);
  118. test_find_last_bit(bitmap, BITMAP_LEN);
  119. test_find_first_bit(bitmap, BITMAP_LEN);
  120. test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
  121. /*
  122. * Everything is OK. Return error just to let user run benchmark
  123. * again without annoying rmmod.
  124. */
  125. return -EINVAL;
  126. }
  127. module_init(find_bit_test);
  128. MODULE_LICENSE("GPL");