test_find_bit.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /*
  35. * This is Schlemiel the Painter's algorithm. It should be called after
  36. * all other tests for the same bitmap because it sets all bits of bitmap to 1.
  37. */
  38. static int __init test_find_first_bit(void *bitmap, unsigned long len)
  39. {
  40. unsigned long i, cnt;
  41. cycles_t cycles;
  42. cycles = get_cycles();
  43. for (cnt = i = 0; i < len; cnt++) {
  44. i = find_first_bit(bitmap, len);
  45. __clear_bit(i, bitmap);
  46. }
  47. cycles = get_cycles() - cycles;
  48. pr_err("find_first_bit:\t\t%llu cycles,\t%ld iterations\n",
  49. (u64)cycles, 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. cycles_t cycles;
  56. cycles = get_cycles();
  57. for (cnt = i = 0; i < BITMAP_LEN; cnt++)
  58. i = find_next_bit(bitmap, BITMAP_LEN, i) + 1;
  59. cycles = get_cycles() - cycles;
  60. pr_err("find_next_bit:\t\t%llu cycles,\t%ld iterations\n",
  61. (u64)cycles, cnt);
  62. return 0;
  63. }
  64. static int __init test_find_next_zero_bit(const void *bitmap, unsigned long len)
  65. {
  66. unsigned long i, cnt;
  67. cycles_t cycles;
  68. cycles = get_cycles();
  69. for (cnt = i = 0; i < BITMAP_LEN; cnt++)
  70. i = find_next_zero_bit(bitmap, len, i) + 1;
  71. cycles = get_cycles() - cycles;
  72. pr_err("find_next_zero_bit:\t%llu cycles,\t%ld iterations\n",
  73. (u64)cycles, cnt);
  74. return 0;
  75. }
  76. static int __init test_find_last_bit(const void *bitmap, unsigned long len)
  77. {
  78. unsigned long l, cnt = 0;
  79. cycles_t cycles;
  80. cycles = get_cycles();
  81. do {
  82. cnt++;
  83. l = find_last_bit(bitmap, len);
  84. if (l >= len)
  85. break;
  86. len = l;
  87. } while (len);
  88. cycles = get_cycles() - cycles;
  89. pr_err("find_last_bit:\t\t%llu cycles,\t%ld iterations\n",
  90. (u64)cycles, cnt);
  91. return 0;
  92. }
  93. static int __init find_bit_test(void)
  94. {
  95. unsigned long nbits = BITMAP_LEN / SPARSE;
  96. pr_err("\nStart testing find_bit() with random-filled bitmap\n");
  97. get_random_bytes(bitmap, sizeof(bitmap));
  98. test_find_next_bit(bitmap, BITMAP_LEN);
  99. test_find_next_zero_bit(bitmap, BITMAP_LEN);
  100. test_find_last_bit(bitmap, BITMAP_LEN);
  101. test_find_first_bit(bitmap, BITMAP_LEN);
  102. pr_err("\nStart testing find_bit() with sparse bitmap\n");
  103. bitmap_zero(bitmap, BITMAP_LEN);
  104. while (nbits--)
  105. __set_bit(prandom_u32() % BITMAP_LEN, bitmap);
  106. test_find_next_bit(bitmap, BITMAP_LEN);
  107. test_find_next_zero_bit(bitmap, BITMAP_LEN);
  108. test_find_last_bit(bitmap, BITMAP_LEN);
  109. test_find_first_bit(bitmap, BITMAP_LEN);
  110. return 0;
  111. }
  112. module_init(find_bit_test);
  113. static void __exit test_find_bit_cleanup(void)
  114. {
  115. }
  116. module_exit(test_find_bit_cleanup);
  117. MODULE_LICENSE("GPL");