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