find_bit_benchmark.c 4.3 KB

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