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.
54 #define talloc(type, num) (type *) malloc(sizeof(type)*(num))
56 static void usage(char *s)
58 fprintf(stderr, "usage: jerasure_08 k w seed - Example schedule cache usage with RAID-6\n");
59 fprintf(stderr, " \n");
60 fprintf(stderr, " m=2. k+m must be <= 2^w. It sets up a RAID-6 generator matrix and encodes\n");
61 fprintf(stderr, " k sets of w*%ld bytes. It creates a schedule cache for decoding.\n", sizeof(long));
62 fprintf(stderr, " It demonstrates using the schedule cache for both encoding and decoding.\n");
63 fprintf(stderr, " Then it demonstrates using jerasure_do_parity() to re-encode the first.\n");
64 fprintf(stderr, " coding device\n");
65 fprintf(stderr, " \n");
66 fprintf(stderr, "This demonstrates: jerasure_generate_schedule_cache()\n");
67 fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
68 fprintf(stderr, " jerasure_schedule_encode()\n");
69 fprintf(stderr, " jerasure_schedule_decode_cache()\n");
70 fprintf(stderr, " jerasure_free_schedule()\n");
71 fprintf(stderr, " jerasure_free_schedule_cache()\n");
72 fprintf(stderr, " jerasure_get_stats()\n");
73 fprintf(stderr, " jerasure_do_parity()\n");
74 if (s != NULL) fprintf(stderr, "%s\n", s);
78 static void print_array(char **ptrs, int ndevices, int size, int packetsize, char *label)
83 printf("<center><table border=3 cellpadding=3><tr><td></td>\n");
85 for (i = 0; i < ndevices; i++) printf("<td align=center>%s%x</td>\n", label, i);
87 printf("<td align=right><pre>");
88 for (j = 0; j < size/packetsize; j++) printf("Packet %d\n", j);
89 printf("</pre></td>\n");
90 for (i = 0; i < ndevices; i++) {
92 up = (unsigned char *) ptrs[i];
93 for (j = 0; j < size/packetsize; j++) {
94 for (x = 0; x < packetsize; x++) {
95 if (x > 0 && x%4 == 0) printf(" ");
96 printf("%02x", up[j*packetsize+x]);
102 printf("</tr></table></center>\n");
105 int main(int argc, char **argv)
108 int *matrix, *bitmatrix;
109 char **data, **coding;
110 int **smart, ***cache;
111 int *erasures, *erased;
115 if (argc != 4) usage("Wrong number of arguments");
116 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
117 if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad m");
118 if (sscanf(argv[3], "%d", &seed) == 0) usage("Bad seed");
120 if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
124 matrix = talloc(int, m*k);
125 for (j = 0; j < k; j++) matrix[j] = 1;
127 for (j = 0; j < k; j++) {
129 i = galois_single_multiply(i, 2, w);
131 bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
133 smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
134 cache = jerasure_generate_schedule_cache(k, m, w, bitmatrix, 1);
136 data = talloc(char *, k);
137 for (i = 0; i < k; i++) {
138 data[i] = talloc(char, sizeof(long)*w);
139 MOA_Fill_Random_Region(data[i], sizeof(long)*w);
142 coding = talloc(char *, m);
143 for (i = 0; i < m; i++) {
144 coding[i] = talloc(char, sizeof(long)*w);
147 jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
148 jerasure_get_stats(stats);
150 printf("<HTML><TITLE>jerasure_08");
151 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
152 printf("</TITLE>\n");
153 printf("<h3>jerasure_08");
154 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
158 printf("Encoding Complete: - %.0lf XOR'd bytes. Here is the state of the system:\n<p>\n", stats[0]);
160 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
162 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
165 erasures = talloc(int, (m+1));
169 for (j = 0; j < m; j++) bzero(coding[j], sizeof(long)*w);
171 jerasure_schedule_decode_cache(k, m, w, cache, erasures, data, coding, w*sizeof(long), sizeof(long));
172 jerasure_get_stats(stats);
173 printf("Encoding Using the Schedule Cache: - %.0lf XOR'd bytes\n\n", stats[0]);
175 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
177 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
180 erased = talloc(int, (k+m));
181 for (i = 0; i < m+k; i++) erased[i] = 0;
182 for (i = 0; i < m; ) {
183 erasures[i] = MOA_Random_W(w, 1)%(k+m);
184 if (erased[erasures[i]] == 0) {
185 erased[erasures[i]] = 1;
186 bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
192 printf("Erased %d random devices:\n\n", m);
194 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
196 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
199 jerasure_schedule_decode_cache(k, m, w, cache, erasures, data, coding, w*sizeof(long), sizeof(long));
200 jerasure_get_stats(stats);
202 printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
204 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
206 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
209 bzero(coding[0], sizeof(long)*w);
210 printf("Erased the first coding device:\n\n");
212 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
214 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
217 jerasure_do_parity(k, data, coding[0], sizeof(long)*w);
218 printf("State of the system after using\n");
219 printf("<b>jerasure_do_parity()</b> to re-encode it:\n\n");
221 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
223 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
226 jerasure_free_schedule(smart);
227 jerasure_free_schedule_cache(k, m, cache);
229 printf("Smart schedule and cache freed.\n\n");