raid6algos.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright 2002 H. Peter Anvin - All Rights Reserved
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
  8. * Bostom MA 02111-1307, USA; either version 2 of the License, or
  9. * (at your option) any later version; incorporated herein by reference.
  10. *
  11. * ----------------------------------------------------------------------- */
  12. /*
  13. * raid6algos.c
  14. *
  15. * Algorithm list and algorithm selection for RAID-6
  16. */
  17. #include "raid6.h"
  18. #ifndef __KERNEL__
  19. #include <sys/mman.h>
  20. #endif
  21. struct raid6_calls raid6_call;
  22. /* Various routine sets */
  23. extern const struct raid6_calls raid6_intx1;
  24. extern const struct raid6_calls raid6_intx2;
  25. extern const struct raid6_calls raid6_intx4;
  26. extern const struct raid6_calls raid6_intx8;
  27. extern const struct raid6_calls raid6_intx16;
  28. extern const struct raid6_calls raid6_intx32;
  29. extern const struct raid6_calls raid6_mmxx1;
  30. extern const struct raid6_calls raid6_mmxx2;
  31. extern const struct raid6_calls raid6_sse1x1;
  32. extern const struct raid6_calls raid6_sse1x2;
  33. extern const struct raid6_calls raid6_sse2x1;
  34. extern const struct raid6_calls raid6_sse2x2;
  35. extern const struct raid6_calls raid6_sse2x4;
  36. extern const struct raid6_calls raid6_altivec1;
  37. extern const struct raid6_calls raid6_altivec2;
  38. extern const struct raid6_calls raid6_altivec4;
  39. extern const struct raid6_calls raid6_altivec8;
  40. const struct raid6_calls * const raid6_algos[] = {
  41. &raid6_intx1,
  42. &raid6_intx2,
  43. &raid6_intx4,
  44. &raid6_intx8,
  45. #if defined(__ia64__)
  46. &raid6_intx16,
  47. &raid6_intx32,
  48. #endif
  49. #if defined(__i386__)
  50. &raid6_mmxx1,
  51. &raid6_mmxx2,
  52. &raid6_sse1x1,
  53. &raid6_sse1x2,
  54. &raid6_sse2x1,
  55. &raid6_sse2x2,
  56. #endif
  57. #if defined(__x86_64__)
  58. &raid6_sse2x1,
  59. &raid6_sse2x2,
  60. &raid6_sse2x4,
  61. #endif
  62. #ifdef CONFIG_ALTIVEC
  63. &raid6_altivec1,
  64. &raid6_altivec2,
  65. &raid6_altivec4,
  66. &raid6_altivec8,
  67. #endif
  68. NULL
  69. };
  70. #ifdef __KERNEL__
  71. #define RAID6_TIME_JIFFIES_LG2 4
  72. #else
  73. /* Need more time to be stable in userspace */
  74. #define RAID6_TIME_JIFFIES_LG2 9
  75. #endif
  76. /* Try to pick the best algorithm */
  77. /* This code uses the gfmul table as convenient data set to abuse */
  78. int __init raid6_select_algo(void)
  79. {
  80. const struct raid6_calls * const * algo;
  81. const struct raid6_calls * best;
  82. char *syndromes;
  83. void *dptrs[(65536/PAGE_SIZE)+2];
  84. int i, disks;
  85. unsigned long perf, bestperf;
  86. int bestprefer;
  87. unsigned long j0, j1;
  88. disks = (65536/PAGE_SIZE)+2;
  89. for ( i = 0 ; i < disks-2 ; i++ ) {
  90. dptrs[i] = ((char *)raid6_gfmul) + PAGE_SIZE*i;
  91. }
  92. /* Normal code - use a 2-page allocation to avoid D$ conflict */
  93. syndromes = (void *) __get_free_pages(GFP_KERNEL, 1);
  94. if ( !syndromes ) {
  95. printk("raid6: Yikes! No memory available.\n");
  96. return -ENOMEM;
  97. }
  98. dptrs[disks-2] = syndromes;
  99. dptrs[disks-1] = syndromes + PAGE_SIZE;
  100. bestperf = 0; bestprefer = 0; best = NULL;
  101. for ( algo = raid6_algos ; *algo ; algo++ ) {
  102. if ( !(*algo)->valid || (*algo)->valid() ) {
  103. perf = 0;
  104. preempt_disable();
  105. j0 = jiffies;
  106. while ( (j1 = jiffies) == j0 )
  107. cpu_relax();
  108. while ( (jiffies-j1) < (1 << RAID6_TIME_JIFFIES_LG2) ) {
  109. (*algo)->gen_syndrome(disks, PAGE_SIZE, dptrs);
  110. perf++;
  111. }
  112. preempt_enable();
  113. if ( (*algo)->prefer > bestprefer ||
  114. ((*algo)->prefer == bestprefer &&
  115. perf > bestperf) ) {
  116. best = *algo;
  117. bestprefer = best->prefer;
  118. bestperf = perf;
  119. }
  120. printk("raid6: %-8s %5ld MB/s\n", (*algo)->name,
  121. (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  122. }
  123. }
  124. if ( best )
  125. printk("raid6: using algorithm %s (%ld MB/s)\n",
  126. best->name,
  127. (bestperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  128. else
  129. printk("raid6: Yikes! No algorithm found!\n");
  130. raid6_call = *best;
  131. free_pages((unsigned long)syndromes, 1);
  132. return best ? 0 : -EINVAL;
  133. }