]> git.proxmox.com Git - ceph.git/blob - ceph/src/erasure-code/jerasure/jerasure/Examples/jerasure_08.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / erasure-code / jerasure / jerasure / Examples / jerasure_08.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 <string.h>
49 #include <stdlib.h>
50 #include <stdint.h>
51 #include <gf_rand.h>
52 #include "jerasure.h"
53
54 #define talloc(type, num) (type *) malloc(sizeof(type)*(num))
55
56 usage(char *s)
57 {
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);
75 exit(1);
76 }
77
78 static print_array(char **ptrs, int ndevices, int size, int packetsize, char *label)
79 {
80 int i, j, x;
81 unsigned char *up;
82
83 printf("<center><table border=3 cellpadding=3><tr><td></td>\n");
84
85 for (i = 0; i < ndevices; i++) printf("<td align=center>%s%x</td>\n", label, i);
86 printf("</tr>\n");
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++) {
91 printf("<td><pre>");
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]);
97 }
98 printf("\n");
99 }
100 printf("</td>\n");
101 }
102 printf("</tr></table></center>\n");
103 }
104
105 int main(int argc, char **argv)
106 {
107 long l;
108 int k, w, i, j, m;
109 int *matrix, *bitmatrix;
110 char **data, **coding, **ptrs;
111 int **smart, ***cache;
112 int *erasures, *erased;
113 double stats[3];
114 uint32_t seed;
115
116 if (argc != 4) usage("Wrong number of arguments");
117 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
118 if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad m");
119 if (sscanf(argv[3], "%d", &seed) == 0) usage("Bad seed");
120 m = 2;
121 if (w < 30 && (k+m) > (1 << w)) usage("k + m is too big");
122
123 MOA_Seed(seed);
124
125 matrix = talloc(int, m*k);
126 for (j = 0; j < k; j++) matrix[j] = 1;
127 i = 1;
128 for (j = 0; j < k; j++) {
129 matrix[k+j] = i;
130 i = galois_single_multiply(i, 2, w);
131 }
132 bitmatrix = jerasure_matrix_to_bitmatrix(k, m, w, matrix);
133
134 smart = jerasure_smart_bitmatrix_to_schedule(k, m, w, bitmatrix);
135 cache = jerasure_generate_schedule_cache(k, m, w, bitmatrix, 1);
136
137 data = talloc(char *, k);
138 for (i = 0; i < k; i++) {
139 data[i] = talloc(char, sizeof(long)*w);
140 MOA_Fill_Random_Region(data[i], sizeof(long)*w);
141 }
142
143 coding = talloc(char *, m);
144 for (i = 0; i < m; i++) {
145 coding[i] = talloc(char, sizeof(long)*w);
146 }
147
148 jerasure_schedule_encode(k, m, w, smart, data, coding, w*sizeof(long), sizeof(long));
149 jerasure_get_stats(stats);
150
151 printf("<HTML><TITLE>jerasure_08");
152 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
153 printf("</TITLE>\n");
154 printf("<h3>jerasure_08");
155 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
156 printf("</h3>\n");
157 printf("<hr>\n");
158
159 printf("Encoding Complete: - %.0lf XOR'd bytes. Here is the state of the system:\n<p>\n", stats[0]);
160 printf("<p>\n");
161 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
162 printf("<p>\n");
163 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
164 printf("<hr>\n");
165
166 erasures = talloc(int, (m+1));
167 erasures[0] = k;
168 erasures[1] = k+1;
169 erasures[2] = -1;
170 for (j = 0; j < m; j++) bzero(coding[j], sizeof(long)*w);
171
172 jerasure_schedule_decode_cache(k, m, w, cache, erasures, data, coding, w*sizeof(long), sizeof(long));
173 jerasure_get_stats(stats);
174 printf("Encoding Using the Schedule Cache: - %.0lf XOR'd bytes\n\n", stats[0]);
175 printf("<p>\n");
176 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
177 printf("<p>\n");
178 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
179 printf("<hr>\n");
180
181 erased = talloc(int, (k+m));
182 for (i = 0; i < m+k; i++) erased[i] = 0;
183 for (i = 0; i < m; ) {
184 erasures[i] = MOA_Random_W(w, 1)%(k+m);
185 if (erased[erasures[i]] == 0) {
186 erased[erasures[i]] = 1;
187 bzero((erasures[i] < k) ? data[erasures[i]] : coding[erasures[i]-k], sizeof(long)*w);
188 i++;
189 }
190 }
191 erasures[i] = -1;
192
193 printf("Erased %d random devices:\n\n", m);
194 printf("<p>\n");
195 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
196 printf("<p>\n");
197 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
198 printf("<hr>\n");
199
200 jerasure_schedule_decode_cache(k, m, w, cache, erasures, data, coding, w*sizeof(long), sizeof(long));
201 jerasure_get_stats(stats);
202
203 printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats[0]);
204 printf("<p>\n");
205 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
206 printf("<p>\n");
207 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
208 printf("<hr>\n");
209
210 bzero(coding[0], sizeof(long)*w);
211 printf("Erased the first coding device:\n\n", m);
212 printf("<p>\n");
213 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
214 printf("<p>\n");
215 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
216 printf("<hr>\n");
217
218 jerasure_do_parity(k, data, coding[0], sizeof(long)*w);
219 printf("State of the system after using\n");
220 printf("<b>jerasure_do_parity()</b> to re-encode it:\n\n");
221 printf("<p>\n");
222 print_array(data, k, sizeof(long)*w, sizeof(long), "D");
223 printf("<p>\n");
224 print_array(coding, m, sizeof(long)*w, sizeof(long), "C");
225 printf("<hr>\n");
226
227 jerasure_free_schedule(smart);
228 jerasure_free_schedule_cache(k, m, cache);
229
230 printf("Smart schedule and cache freed.\n\n");
231
232 return 0;
233 }