]>
git.proxmox.com Git - ceph.git/blob - ceph/src/erasure-code/jerasure/jerasure/Examples/liberation_01.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.
53 #include "liberation.h"
55 #define talloc(type, num) (type *) malloc(sizeof(type)*(num))
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 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
)
108 char **data
, **coding
, **ptrs
;
110 int *erasures
, *erased
;
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");
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");
122 bitmatrix
= liberation_coding_bitmatrix(k
, w
);
123 if (bitmatrix
== NULL
) {
124 usage("couldn't make coding matrix");
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
]);
135 printf("Coding Bit-Matrix:\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
);
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
);
148 coding
= talloc(char *, m
);
149 for (i
= 0; i
< m
; i
++) {
150 coding
[i
] = talloc(char, sizeof(long)*w
);
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]);
157 print_array(data
, k
, sizeof(long)*w
, sizeof(long), "D");
159 print_array(coding
, m
, sizeof(long)*w
, sizeof(long), "C");
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
);
175 printf("Erased %d random devices:\n\n", m
);
177 print_array(data
, k
, sizeof(long)*w
, sizeof(long), "D");
179 print_array(coding
, m
, sizeof(long)*w
, sizeof(long), "C");
182 jerasure_schedule_decode_lazy(k
, m
, w
, bitmatrix
, erasures
, data
, coding
, w
*sizeof(long), sizeof(long), 1);
183 jerasure_get_stats(stats
);
185 printf("State of the system after decoding: %.0lf XOR'd bytes\n\n", stats
[0]);
187 print_array(data
, k
, sizeof(long)*w
, sizeof(long), "D");
189 print_array(coding
, m
, sizeof(long)*w
, sizeof(long), "C");