2 * Copyright (c) 2014, James S. Plank and Kevin Greenan
5 * Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure
8 * Revision 2.0: Galois Field backend now links to GF-Complete
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * - Neither the name of the University of Tennessee nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
33 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
36 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 /* Jerasure's authors:
42 Revision 2.x - 2014: James S. Plank and Kevin M. Greenan.
43 Revision 1.2 - 2008: James S. Plank, Scott Simmerman and Catherine D. Schuman.
44 Revision 1.0 - 2007: James S. Plank.
51 #include <gf_complete.h>
53 #include <gf_method.h>
59 static void *malloc16(int size) {
60 void *mem = malloc(size+16+sizeof(void*));
61 void **ptr = (void**)((long)(mem+16+sizeof(void*)) & ~(15));
68 static void free16(void *ptr) {
69 free(((void**)ptr)[-1]);
73 #define talloc(type, num) (type *) malloc16(sizeof(type)*(num))
75 static void usage(char *s)
77 fprintf(stderr, "usage: reed_sol_time_gf k m w seed iterations bufsize (additional GF args) - Test and time Reed-Solomon in a particular GF(2^w).\n");
78 fprintf(stderr, " \n");
79 fprintf(stderr, " w must be 8, 16 or 32. k+m must be <= 2^w.\n");
80 fprintf(stderr, " See the README for information on the additional GF args.\n");
81 fprintf(stderr, " Set up a Vandermonde-based distribution matrix and encodes k devices of\n");
82 fprintf(stderr, " bufsize bytes each with it. Then it decodes.\n");
83 fprintf(stderr, " \n");
84 fprintf(stderr, "This tests: jerasure_matrix_encode()\n");
85 fprintf(stderr, " jerasure_matrix_decode()\n");
86 fprintf(stderr, " jerasure_print_matrix()\n");
87 fprintf(stderr, " galois_change_technique()\n");
88 fprintf(stderr, " reed_sol_vandermonde_coding_matrix()\n");
89 if (s != NULL) fprintf(stderr, "%s\n", s);
93 gf_t* get_gf(int w, int argc, char **argv, int starting)
95 gf_t *gf = (gf_t*)malloc(sizeof(gf_t));
96 if (create_gf_from_argv(gf, w, argc, argv, starting) == 0) {
103 int main(int argc, char **argv)
105 int k, w, i, m, iterations, bufsize;
107 char **data, **coding, **old_values;
108 int *erasures, *erased;
110 double t = 0, total_time = 0;
113 if (argc < 8) usage(NULL);
114 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
115 if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
116 if (sscanf(argv[3], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage("Bad w");
117 if (sscanf(argv[4], "%d", &seed) == 0) usage("Bad seed");
118 if (sscanf(argv[5], "%d", &iterations) == 0) usage("Bad iterations");
119 if (sscanf(argv[6], "%d", &bufsize) == 0) usage("Bad bufsize");
120 if (w <= 16 && k + m > (1 << w)) usage("k + m is too big");
124 gf = get_gf(w, argc, argv, 7);
127 usage("Invalid arguments given for GF!\n");
130 galois_change_technique(gf, w);
132 matrix = reed_sol_vandermonde_coding_matrix(k, m, w);
134 printf("<HTML><TITLE>reed_sol_time_gf");
135 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
136 printf("</TITLE>\n");
137 printf("<h3>reed_sol_time_gf");
138 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
142 printf("Last m rows of the generator matrix (G^T):\n\n");
143 jerasure_print_matrix(matrix, m, k, w);
146 data = talloc(char *, k);
147 for (i = 0; i < k; i++) {
148 data[i] = talloc(char, bufsize);
149 MOA_Fill_Random_Region(data[i], bufsize);
152 coding = talloc(char *, m);
153 old_values = talloc(char *, m);
154 for (i = 0; i < m; i++) {
155 coding[i] = talloc(char, bufsize);
156 old_values[i] = talloc(char, bufsize);
159 for (i = 0; i < iterations; i++) {
161 jerasure_matrix_encode(k, m, w, matrix, data, coding, bufsize);
162 total_time += timing_now() - t;
165 printf("Encode throughput for %d iterations: %.2f MB/s (%.2f sec)\n", iterations, (double)(k*iterations*bufsize/1024/1024) / total_time, total_time);
167 erasures = talloc(int, (m+1));
168 erased = talloc(int, (k+m));
169 for (i = 0; i < m+k; i++) erased[i] = 0;
170 for (i = 0; i < m; ) {
171 erasures[i] = ((unsigned int)MOA_Random_W(w, 1))%(k+m);
172 if (erased[erasures[i]] == 0) {
173 erased[erasures[i]] = 1;
174 memcpy(old_values[i], (erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], bufsize);
175 bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], bufsize);
181 for (i = 0; i < iterations; i++) {
183 jerasure_matrix_decode(k, m, w, matrix, 1, erasures, data, coding, bufsize);
184 total_time += timing_now() - t;
187 printf("Decode throughput for %d iterations: %.2f MB/s (%.2f sec)\n", iterations, (double)(k*iterations*bufsize/1024/1024) / total_time, total_time);
189 for (i = 0; i < m; i++) {
190 if (erasures[i] < k) {
191 if (memcmp(data[erasures[i]], old_values[i], bufsize)) {
192 fprintf(stderr, "Decoding failed for %d!\n", erasures[i]);
196 if (memcmp(coding[erasures[i]-k], old_values[i], bufsize)) {
197 fprintf(stderr, "Decoding failed for %d!\n", erasures[i]);