raid6algos.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include <stdio.h>
  21. #endif
  22. struct raid6_calls raid6_call;
  23. /* Various routine sets */
  24. extern const struct raid6_calls raid6_intx1;
  25. extern const struct raid6_calls raid6_intx2;
  26. extern const struct raid6_calls raid6_intx4;
  27. extern const struct raid6_calls raid6_intx8;
  28. extern const struct raid6_calls raid6_intx16;
  29. extern const struct raid6_calls raid6_intx32;
  30. extern const struct raid6_calls raid6_mmxx1;
  31. extern const struct raid6_calls raid6_mmxx2;
  32. extern const struct raid6_calls raid6_sse1x1;
  33. extern const struct raid6_calls raid6_sse1x2;
  34. extern const struct raid6_calls raid6_sse2x1;
  35. extern const struct raid6_calls raid6_sse2x2;
  36. extern const struct raid6_calls raid6_sse2x4;
  37. extern const struct raid6_calls raid6_altivec1;
  38. extern const struct raid6_calls raid6_altivec2;
  39. extern const struct raid6_calls raid6_altivec4;
  40. extern const struct raid6_calls raid6_altivec8;
  41. const struct raid6_calls * const raid6_algos[] = {
  42. &raid6_intx1,
  43. &raid6_intx2,
  44. &raid6_intx4,
  45. &raid6_intx8,
  46. #if defined(__ia64__)
  47. &raid6_intx16,
  48. &raid6_intx32,
  49. #endif
  50. #if defined(__i386__)
  51. &raid6_mmxx1,
  52. &raid6_mmxx2,
  53. &raid6_sse1x1,
  54. &raid6_sse1x2,
  55. &raid6_sse2x1,
  56. &raid6_sse2x2,
  57. #endif
  58. #if defined(__x86_64__)
  59. &raid6_sse2x1,
  60. &raid6_sse2x2,
  61. &raid6_sse2x4,
  62. #endif
  63. #ifdef CONFIG_ALTIVEC
  64. &raid6_altivec1,
  65. &raid6_altivec2,
  66. &raid6_altivec4,
  67. &raid6_altivec8,
  68. #endif
  69. NULL
  70. };
  71. #ifdef __KERNEL__
  72. #define RAID6_TIME_JIFFIES_LG2 4
  73. #else
  74. /* Need more time to be stable in userspace */
  75. #define RAID6_TIME_JIFFIES_LG2 9
  76. #endif
  77. /* Try to pick the best algorithm */
  78. /* This code uses the gfmul table as convenient data set to abuse */
  79. int __init raid6_select_algo(void)
  80. {
  81. const struct raid6_calls * const * algo;
  82. const struct raid6_calls * best;
  83. char *syndromes;
  84. void *dptrs[(65536/PAGE_SIZE)+2];
  85. int i, disks;
  86. unsigned long perf, bestperf;
  87. int bestprefer;
  88. unsigned long j0, j1;
  89. disks = (65536/PAGE_SIZE)+2;
  90. for ( i = 0 ; i < disks-2 ; i++ ) {
  91. dptrs[i] = ((char *)raid6_gfmul) + PAGE_SIZE*i;
  92. }
  93. /* Normal code - use a 2-page allocation to avoid D$ conflict */
  94. syndromes = (void *) __get_free_pages(GFP_KERNEL, 1);
  95. if ( !syndromes ) {
  96. printk("raid6: Yikes! No memory available.\n");
  97. return -ENOMEM;
  98. }
  99. dptrs[disks-2] = syndromes;
  100. dptrs[disks-1] = syndromes + PAGE_SIZE;
  101. bestperf = 0; bestprefer = 0; best = NULL;
  102. for ( algo = raid6_algos ; *algo ; algo++ ) {
  103. if ( !(*algo)->valid || (*algo)->valid() ) {
  104. perf = 0;
  105. preempt_disable();
  106. j0 = jiffies;
  107. while ( (j1 = jiffies) == j0 )
  108. cpu_relax();
  109. while ( (jiffies-j1) < (1 << RAID6_TIME_JIFFIES_LG2) ) {
  110. (*algo)->gen_syndrome(disks, PAGE_SIZE, dptrs);
  111. perf++;
  112. }
  113. preempt_enable();
  114. if ( (*algo)->prefer > bestprefer ||
  115. ((*algo)->prefer == bestprefer &&
  116. perf > bestperf) ) {
  117. best = *algo;
  118. bestprefer = best->prefer;
  119. bestperf = perf;
  120. }
  121. printk("raid6: %-8s %5ld MB/s\n", (*algo)->name,
  122. (perf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  123. }
  124. }
  125. if ( best )
  126. printk("raid6: using algorithm %s (%ld MB/s)\n",
  127. best->name,
  128. (bestperf*HZ) >> (20-16+RAID6_TIME_JIFFIES_LG2));
  129. else
  130. printk("raid6: Yikes! No algorithm found!\n");
  131. raid6_call = *best;
  132. free_pages((unsigned long)syndromes, 1);
  133. return best ? 0 : -EINVAL;
  134. }