]> git.proxmox.com Git - ceph.git/blob - ceph/src/erasure-code/jerasure/jerasure/Examples/liberation_01.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / erasure-code / jerasure / jerasure / Examples / liberation_01.c
1 /* *
2 * Copyright (c) 2014, James S. Plank and Kevin Greenan
3 * All rights reserved.
4 *
5 * Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure
6 * Coding Techniques
7 *
8 * Revision 2.0: Galois Field backend now links to GF-Complete
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
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
20 * distribution.
21 *
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.
25 *
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.
38 */
39
40 /* Jerasure's authors:
41
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.
45 */
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <stdlib.h>
51 #include <gf_rand.h>
52 #include "jerasure.h"
53 #include "liberation.h"
54
55 #define talloc(type, num) (type *) malloc(sizeof(type)*(num))
56
57 usage(char *s)
58 {
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);
73 exit(1);
74 }
75
76 static print_array(char **ptrs, int ndevices, int size, int packetsize, char *label)
77 {
78 int i, j, x;
79 unsigned char *up;
80
81 printf("<center><table border=3 cellpadding=3><tr><td></td>\n");
82
83 for (i = 0; i < ndevices; i++) printf("<td align=center>%s%x</td>\n", label, i);
84 printf("</tr>\n");
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++) {
89 printf("<td><pre>");
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]);
95 }
96 printf("\n");
97 }
98 printf("</td>\n");
99 }
100 printf("</tr></table></center>\n");
101 }
102
103 int main(int argc, char **argv)
104 {
105 long l;
106 int k, w, i, j, m;
107 int *bitmatrix;
108 char **data, **coding, **ptrs;
109 int **dumb;
110 int *erasures, *erased;
111 double stats[3];
112 uint32_t seed;
113
114 if (argc != 4) usage("Wrong number of arguments");
115 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
116 if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
117 if (sscanf(argv[3], "%u", &seed) == 0) usage("Bad seed");
118 m = 2;
119 if (w < k) usage("k is too big");
120 for (i = 2; i*i <= w; i++) if (w%i == 0) usage("w isn't prime");
121
122 bitmatrix = liberation_coding_bitmatrix(k, w);
123 if (bitmatrix == NULL) {
124 usage("couldn't make coding matrix");
125 }
126
127 printf("<HTML><TITLE>liberation_01");
128 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
129 printf("</TITLE>\n");
130 printf("<h3>liberation_01");
131 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
132 printf("</h3>\n");
133 printf("<hr>\n");
134
135 printf("Coding Bit-Matrix:\n<pre>\n");
136 jerasure_print_bitmatrix(bitmatrix, w*m, w*k, w);
137 printf("</pre><hr>\n");
138
139 dumb = jerasure_dumb_bitmatrix_to_schedule(k, m, w, bitmatrix);
140
141 MOA_Seed(seed);
142 data = talloc(char *, k);
143 for (i = 0; i < k; i++) {
144 data[i] = talloc(char, sizeof(long)*w);
145 MOA_Fill_Random_Region(data[i], sizeof(long)*w);
146 }
147
148 coding = talloc(char *, m);
149 for (i = 0; i < m; i++) {
150 coding[i] = talloc(char, sizeof(long)*w);
151 }
152
153 jerasure_schedule_encode(k, m, w, dumb, data, coding, w*sizeof(long), sizeof(long));
154 jerasure_get_stats(stats);
155 printf("Smart Encoding Complete: - %.0lf XOR'd bytes. State of the system:\n\n", stats[0]);
156 printf("<p>\n");
157 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
158 printf("<p>\n");
159 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
160 printf("<hr>\n");
161
162 erasures = talloc(int, (m+1));
163 erased = talloc(int, (k+m));
164 for (i = 0; i < m+k; i++) erased[i] = 0;
165 for (i = 0; i < m; ) {
166 erasures[i] = MOA_Random_W(30,1)%(k+m);
167 if (erased[erasures[i]] == 0) {
168 erased[erasures[i]] = 1;
169 bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
170 i++;
171 }
172 }
173 erasures[i] = -1;
174
175 printf("Erased %d random devices:\n\n", m);
176 printf("<p>\n");
177 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
178 printf("<p>\n");
179 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
180 printf("<hr>\n");
181
182 jerasure_schedule_decode_lazy(k, m, w, bitmatrix, erasures, data, coding, w*sizeof(long), sizeof(long), 1);
183 jerasure_get_stats(stats);
184
185 printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
186 printf("<p>\n");
187 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
188 printf("<p>\n");
189 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
190 printf("<hr>\n");
191
192 return 0;
193 }