erasure_code_base_perf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**********************************************************************
  2. Copyright(c) 2011-2015 Intel Corporation All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the
  11. distribution.
  12. * Neither the name of Intel Corporation nor the names of its
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. **********************************************************************/
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h> // for memset, memcmp
  30. #include <assert.h>
  31. #include "erasure_code.h"
  32. #include "test.h"
  33. #ifndef GT_L3_CACHE
  34. # define GT_L3_CACHE 32*1024*1024 /* some number > last level cache */
  35. #endif
  36. #if !defined(COLD_TEST) && !defined(TEST_CUSTOM)
  37. // Cached test, loop many times over small dataset
  38. # define TEST_SOURCES 32
  39. # define TEST_LEN(m) ((128*1024 / m) & ~(64-1))
  40. # define TEST_TYPE_STR "_warm"
  41. #elif defined (COLD_TEST)
  42. // Uncached test. Pull from large mem base.
  43. # define TEST_SOURCES 32
  44. # define TEST_LEN(m) ((GT_L3_CACHE / m) & ~(64-1))
  45. # define TEST_TYPE_STR "_cold"
  46. #elif defined (TEST_CUSTOM)
  47. # define TEST_TYPE_STR "_cus"
  48. #endif
  49. #define MMAX TEST_SOURCES
  50. #define KMAX TEST_SOURCES
  51. #define BAD_MATRIX -1
  52. typedef unsigned char u8;
  53. void ec_encode_perf(int m, int k, u8 * a, u8 * g_tbls, u8 ** buffs)
  54. {
  55. ec_init_tables_base(k, m - k, &a[k * k], g_tbls);
  56. ec_encode_data_base(TEST_LEN(m), k, m - k, g_tbls, buffs, &buffs[k]);
  57. }
  58. int ec_decode_perf(int m, int k, u8 * a, u8 * g_tbls, u8 ** buffs, u8 * src_in_err,
  59. u8 * src_err_list, int nerrs, u8 ** temp_buffs)
  60. {
  61. int i, j, r;
  62. u8 b[MMAX * KMAX], c[MMAX * KMAX], d[MMAX * KMAX];
  63. u8 *recov[TEST_SOURCES];
  64. // Construct b by removing error rows
  65. for (i = 0, r = 0; i < k; i++, r++) {
  66. while (src_in_err[r])
  67. r++;
  68. recov[i] = buffs[r];
  69. for (j = 0; j < k; j++)
  70. b[k * i + j] = a[k * r + j];
  71. }
  72. if (gf_invert_matrix(b, d, k) < 0)
  73. return BAD_MATRIX;
  74. for (i = 0; i < nerrs; i++)
  75. for (j = 0; j < k; j++)
  76. c[k * i + j] = d[k * src_err_list[i] + j];
  77. // Recover data
  78. ec_init_tables_base(k, nerrs, c, g_tbls);
  79. ec_encode_data_base(TEST_LEN(m), k, nerrs, g_tbls, recov, temp_buffs);
  80. return 0;
  81. }
  82. int main(int argc, char *argv[])
  83. {
  84. int i, j, m, k, nerrs, check;
  85. void *buf;
  86. u8 *temp_buffs[TEST_SOURCES], *buffs[TEST_SOURCES];
  87. u8 a[MMAX * KMAX];
  88. u8 g_tbls[KMAX * TEST_SOURCES * 32], src_in_err[TEST_SOURCES];
  89. u8 src_err_list[TEST_SOURCES];
  90. struct perf start;
  91. // Pick test parameters
  92. m = 14;
  93. k = 10;
  94. nerrs = 4;
  95. const u8 err_list[] = { 2, 4, 5, 7 };
  96. printf("erasure_code_base_perf: %dx%d %d\n", m, TEST_LEN(m), nerrs);
  97. // check input parameters
  98. assert(!(m > MMAX || k > KMAX || nerrs > (m - k)));
  99. memcpy(src_err_list, err_list, nerrs);
  100. memset(src_in_err, 0, TEST_SOURCES);
  101. for (i = 0; i < nerrs; i++)
  102. src_in_err[src_err_list[i]] = 1;
  103. // Allocate the arrays
  104. for (i = 0; i < m; i++) {
  105. if (posix_memalign(&buf, 64, TEST_LEN(m))) {
  106. printf("alloc error: Fail\n");
  107. return -1;
  108. }
  109. buffs[i] = buf;
  110. }
  111. for (i = 0; i < (m - k); i++) {
  112. if (posix_memalign(&buf, 64, TEST_LEN(m))) {
  113. printf("alloc error: Fail\n");
  114. return -1;
  115. }
  116. temp_buffs[i] = buf;
  117. }
  118. // Make random data
  119. for (i = 0; i < k; i++)
  120. for (j = 0; j < TEST_LEN(m); j++)
  121. buffs[i][j] = rand();
  122. gf_gen_rs_matrix(a, m, k);
  123. // Start encode test
  124. BENCHMARK(&start, BENCHMARK_TIME, ec_encode_perf(m, k, a, g_tbls, buffs));
  125. printf("erasure_code_base_encode" TEST_TYPE_STR ": ");
  126. perf_print(start, (long long)(TEST_LEN(m)) * (m));
  127. // Start decode test
  128. BENCHMARK(&start, BENCHMARK_TIME, check =
  129. ec_decode_perf(m, k, a, g_tbls, buffs, src_in_err, src_err_list, nerrs,
  130. temp_buffs));
  131. if (check == BAD_MATRIX) {
  132. printf("BAD MATRIX\n");
  133. return check;
  134. }
  135. for (i = 0; i < nerrs; i++) {
  136. if (0 != memcmp(temp_buffs[i], buffs[src_err_list[i]], TEST_LEN(m))) {
  137. printf("Fail error recovery (%d, %d, %d) - ", m, k, nerrs);
  138. return -1;
  139. }
  140. }
  141. printf("erasure_code_base_decode" TEST_TYPE_STR ": ");
  142. perf_print(start, (long long)(TEST_LEN(m)) * (k + nerrs));
  143. printf("done all: Pass\n");
  144. return 0;
  145. }