]>
git.proxmox.com Git - ceph.git/blob - ceph/src/erasure-code/jerasure/jerasure/Examples/jerasure_08.c
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))
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 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
)
109 int *matrix
, *bitmatrix
;
110 char **data
, **coding
, **ptrs
;
111 int **smart
, ***cache
;
112 int *erasures
, *erased
;
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");
121 if (w
< 30 && (k
+m
) > (1 << w
)) usage("k + m is too big");
125 matrix
= talloc(int, m
*k
);
126 for (j
= 0; j
< k
; j
++) matrix
[j
] = 1;
128 for (j
= 0; j
< k
; j
++) {
130 i
= galois_single_multiply(i
, 2, w
);
132 bitmatrix
= jerasure_matrix_to_bitmatrix(k
, m
, w
, matrix
);
134 smart
= jerasure_smart_bitmatrix_to_schedule(k
, m
, w
, bitmatrix
);
135 cache
= jerasure_generate_schedule_cache(k
, m
, w
, bitmatrix
, 1);
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
);
143 coding
= talloc(char *, m
);
144 for (i
= 0; i
< m
; i
++) {
145 coding
[i
] = talloc(char, sizeof(long)*w
);
148 jerasure_schedule_encode(k
, m
, w
, smart
, data
, coding
, w
*sizeof(long), sizeof(long));
149 jerasure_get_stats(stats
);
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
]);
159 printf("Encoding Complete: - %.0lf XOR'd bytes. Here is the state of the system:\n<p>\n", stats
[0]);
161 print_array(data
, k
, sizeof(long)*w
, sizeof(long), "D");
163 print_array(coding
, m
, sizeof(long)*w
, sizeof(long), "C");
166 erasures
= talloc(int, (m
+1));
170 for (j
= 0; j
< m
; j
++) bzero(coding
[j
], sizeof(long)*w
);
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]);
176 print_array(data
, k
, sizeof(long)*w
, sizeof(long), "D");
178 print_array(coding
, m
, sizeof(long)*w
, sizeof(long), "C");
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
);
193 printf("Erased %d random devices:\n\n", m
);
195 print_array(data
, k
, sizeof(long)*w
, sizeof(long), "D");
197 print_array(coding
, m
, sizeof(long)*w
, sizeof(long), "C");
200 jerasure_schedule_decode_cache(k
, m
, w
, cache
, erasures
, data
, coding
, w
*sizeof(long), sizeof(long));
201 jerasure_get_stats(stats
);
203 printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats
[0]);
205 print_array(data
, k
, sizeof(long)*w
, sizeof(long), "D");
207 print_array(coding
, m
, sizeof(long)*w
, sizeof(long), "C");
210 bzero(coding
[0], sizeof(long)*w
);
211 printf("Erased the first coding device:\n\n", m
);
213 print_array(data
, k
, sizeof(long)*w
, sizeof(long), "D");
215 print_array(coding
, m
, sizeof(long)*w
, sizeof(long), "C");
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");
222 print_array(data
, k
, sizeof(long)*w
, sizeof(long), "D");
224 print_array(coding
, m
, sizeof(long)*w
, sizeof(long), "C");
227 jerasure_free_schedule(smart
);
228 jerasure_free_schedule_cache(k
, m
, cache
);
230 printf("Smart schedule and cache freed.\n\n");