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.
53 #include "liberation.h"
55 #define talloc(type, num) (type *) malloc(sizeof(type)*(num))
57 static void usage(char *s)
59 fprintf(stderr, "usage: liberation_01 k w seed - Liberation RAID-6 coding/decoding example in GF(2^w).\n");
60 fprintf(stderr, " \n");
61 fprintf(stderr, " w must be prime and k <= w. It sets up a Liberation bit-matrix\n");
62 fprintf(stderr, " then it encodes k devices of w*%ld bytes using dumb bit-matrix scheduling.\n", sizeof(long));
63 fprintf(stderr, " It decodes using smart bit-matrix scheduling.\n");
64 fprintf(stderr, " \n");
65 fprintf(stderr, "This demonstrates: liberation_coding_bitmatrix()\n");
66 fprintf(stderr, " jerasure_smart_bitmatrix_to_schedule()\n");
67 fprintf(stderr, " jerasure_dumb_bitmatrix_to_schedule()\n");
68 fprintf(stderr, " jerasure_schedule_encode()\n");
69 fprintf(stderr, " jerasure_schedule_decode_lazy()\n");
70 fprintf(stderr, " jerasure_print_bitmatrix()\n");
71 fprintf(stderr, " jerasure_get_stats()\n");
72 if (s != NULL) fprintf(stderr, "%s\n", s);
76 static void print_array(char **ptrs, int ndevices, int size, int packetsize, char *label)
81 printf("<center><table border=3 cellpadding=3><tr><td></td>\n");
83 for (i = 0; i < ndevices; i++) printf("<td align=center>%s%x</td>\n", label, i);
85 printf("<td align=right><pre>");
86 for (j = 0; j < size/packetsize; j++) printf("Packet %d\n", j);
87 printf("</pre></td>\n");
88 for (i = 0; i < ndevices; i++) {
90 up = (unsigned char *) ptrs[i];
91 for (j = 0; j < size/packetsize; j++) {
92 for (x = 0; x < packetsize; x++) {
93 if (x > 0 && x%4 == 0) printf(" ");
94 printf("%02x", up[j*packetsize+x]);
100 printf("</tr></table></center>\n");
103 int main(int argc, char **argv)
107 char **data, **coding;
109 int *erasures, *erased;
113 if (argc != 4) usage("Wrong number of arguments");
114 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
115 if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
116 if (sscanf(argv[3], "%u", &seed) == 0) usage("Bad seed");
118 if (w < k) usage("k is too big");
119 for (i = 2; i*i <= w; i++) if (w%i == 0) usage("w isn't prime");
121 bitmatrix = liberation_coding_bitmatrix(k, w);
122 if (bitmatrix == NULL) {
123 usage("couldn't make coding matrix");
126 printf("<HTML><TITLE>liberation_01");
127 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
128 printf("</TITLE>\n");
129 printf("<h3>liberation_01");
130 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
134 printf("Coding Bit-Matrix:\n<pre>\n");
135 jerasure_print_bitmatrix(bitmatrix, w*m, w*k, w);
136 printf("</pre><hr>\n");
138 dumb = jerasure_dumb_bitmatrix_to_schedule(k, m, w, bitmatrix);
141 data = talloc(char *, k);
142 for (i = 0; i < k; i++) {
143 data[i] = talloc(char, sizeof(long)*w);
144 MOA_Fill_Random_Region(data[i], sizeof(long)*w);
147 coding = talloc(char *, m);
148 for (i = 0; i < m; i++) {
149 coding[i] = talloc(char, sizeof(long)*w);
152 jerasure_schedule_encode(k, m, w, dumb, data, coding, w*sizeof(long), sizeof(long));
153 jerasure_get_stats(stats);
154 printf("Smart Encoding Complete: - %.0lf XOR'd bytes. State of the system:\n\n", stats[0]);
156 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
158 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
161 erasures = talloc(int, (m+1));
162 erased = talloc(int, (k+m));
163 for (i = 0; i < m+k; i++) erased[i] = 0;
164 for (i = 0; i < m; ) {
165 erasures[i] = MOA_Random_W(30,1)%(k+m);
166 if (erased[erasures[i]] == 0) {
167 erased[erasures[i]] = 1;
168 bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
174 printf("Erased %d random devices:\n\n", m);
176 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
178 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
181 jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
182 jerasure_get_stats(stats);
184 printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
186 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
188 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");