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_07 k m w seed - Scheduled Cauchy Reed-Solomon coding example in GF(2^w).\n");
59 fprintf(stderr, " \n");
60 fprintf(stderr, " k+m must be <= 2^w. It sets up a Cauchy generator matrix and encodes\n");
61 fprintf(stderr, " k sets of w*%ld bytes. It uses bit-matrix scheduling, both smart and dumb.\n", sizeof(long));
62 fprintf(stderr, " It decodes using bit-matrix scheduling, then shows an example of\n");
63 fprintf(stderr, " using jerasure_do_scheduled_operations().\n");
64 fprintf(stderr, " \n");
65 fprintf(stderr, "This demonstrates: jerasure_dumb_bitmatrix_to_schedule()\n");
66 fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
67 fprintf(stderr, " jerasure_schedule_encode()\n");
68 fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
69 fprintf(stderr, " jerasure_do_scheduled_operations()\n");
70 fprintf(stderr, " jerasure_get_stats()\n");
71 if (s != NULL) fprintf(stderr, "%s\n", s);
75 static void print_array(char **ptrs, int ndevices, int size, int packetsize, char *label)
80 printf("<center><table border=3 cellpadding=3><tr><td></td>\n");
82 for (i = 0; i < ndevices; i++) printf("<td align=center>%s%x</td>\n", label, i);
84 printf("<td align=right><pre>");
85 for (j = 0; j < size/packetsize; j++) printf("Packet %d\n", j);
86 printf("</pre></td>\n");
87 for (i = 0; i < ndevices; i++) {
89 up = (unsigned char *) ptrs[i];
90 for (j = 0; j < size/packetsize; j++) {
91 for (x = 0; x < packetsize; x++) {
92 if (x > 0 && x%4 == 0) printf(" ");
93 printf("%02x", up[j*packetsize+x]);
99 printf("</tr></table></center>\n");
102 int main(int argc, char **argv)
105 int *matrix, *bitmatrix;
106 char **data, **coding, **ptrs;
108 int *erasures, *erased;
112 if (argc != 5) usage("Wrong number of arguments");
113 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
114 if (sscanf(argv[2], "%d", &m) == 0 || m <= 0) usage("Bad m");
115 if (sscanf(argv[3], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
116 if (sscanf(argv[4], "%d", &seed) == 0) usage("Bad seed");
117 if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
119 matrix = talloc(int, m*k);
120 for (i = 0; i < m; i++) {
121 for (j = 0; j < k; j++) {
122 matrix[i*k+j] = galois_single_divide(1, i ^ (m + j), w);
125 bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
127 printf("<HTML><TITLE>jerasure_07");
128 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
129 printf("</TITLE>\n");
130 printf("<h3>jerasure_07");
131 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
135 printf("Last m*w rows of the generator matrix (G^T):\n<pre>\n");
136 jerasure_print_bitmatrix(bitmatrix, w*m, w*k, w);
137 printf("</pre><hr>\n");
139 dumb = jerasure_dumb_bitmatrix_to_schedule(k, m, w, bitmatrix);
140 smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
143 data = talloc(char *, k);
144 for (i = 0; i < k; i++) {
145 data[i] = talloc(char, sizeof(long)*w);
146 MOA_Fill_Random_Region(data[i], sizeof(long)*w);
149 coding = talloc(char *, m);
150 for (i = 0; i < m; i++) {
151 coding[i] = talloc(char, sizeof(long)*w);
154 jerasure_schedule_encode(k, m, w, dumb, data, coding, w*sizeof(long), sizeof(long));
155 jerasure_get_stats(stats);
156 printf("Dumb Encoding Complete: - %.0lf XOR'd bytes. State of the system:\n\n", stats[0]);
158 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
160 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
163 jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
164 jerasure_get_stats(stats);
165 printf("Smart Encoding Complete: - %.0lf XOR'd bytes\n\n", stats[0]);
167 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
169 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
172 erasures = talloc(int, (m+1));
173 erased = talloc(int, (k+m));
174 for (i = 0; i < m+k; i++) erased[i] = 0;
175 for (i = 0; i < m; ) {
176 erasures[i] = MOA_Random_W(w, 1)%(k+m);
177 if (erased[erasures[i]] == 0) {
178 erased[erasures[i]] = 1;
179 bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
185 printf("Erased %d random devices:\n\n", m);
187 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
189 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
192 jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
193 jerasure_get_stats(stats);
195 printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
197 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
199 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
202 ptrs = talloc(char *, (k+m));
203 for (i = 0; i < k; i++) ptrs[i] = data[i];
204 for (i = 0; i < m; i++) ptrs[k+i] = coding[i];
206 for (j = 0; j < m; j++) bzero(coding[j], sizeof(long)*w);
207 printf("State of the system after erasing the coding devices:\n");
209 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
211 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
214 jerasure_do_scheduled_operations(ptrs, smart, sizeof(long));
215 printf("And using <b>jerasure_do_scheduled_operations()</b>: %.0lf XOR'd bytes\n\n", stats[0]);
217 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
219 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");