]>
git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/examples/ec/ec_simple_example.c
1 /**********************************************************************
2 Copyright(c) 2011-2018 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
34 #include "erasure_code.h" // use <isa-l.h> instead when linking against installed
39 typedef unsigned char u8
;
44 "Usage: ec_simple_example [options]\n"
46 " -k <val> Number of source fragments\n"
47 " -p <val> Number of parity fragments\n"
48 " -l <val> Length of fragments\n"
49 " -e <val> Simulate erasure on frag index val. Zero based. Can be repeated.\n"
50 " -r <seed> Pick random (k, p) with seed\n");
54 static int gf_gen_decode_matrix_simple(u8
* encode_matrix
,
59 u8
* frag_err_list
, int nerrs
, int k
, int m
);
61 int main(int argc
, char *argv
[])
63 int i
, j
, m
, c
, e
, ret
;
64 int k
= 10, p
= 4, len
= 8 * 1024; // Default params
67 // Fragment buffer pointers
69 u8
*recover_srcs
[KMAX
];
70 u8
*recover_outp
[KMAX
];
71 u8 frag_err_list
[MMAX
];
73 // Coefficient matrices
74 u8
*encode_matrix
, *decode_matrix
;
75 u8
*invert_matrix
, *temp_matrix
;
77 u8 decode_index
[MMAX
];
80 for (i
= 0; i
< p
; i
++)
81 frag_err_list
[nerrs
++] = rand() % (k
+ p
);
83 while ((c
= getopt(argc
, argv
, "k:p:l:e:r:h")) != -1) {
98 frag_err_list
[nerrs
++] = e
;
102 k
= (rand() % (MMAX
- 1)) + 1; // Pick k {1 to MMAX - 1}
103 p
= (rand() % (MMAX
- k
)) + 1; // Pick p {1 to MMAX - k}
105 for (i
= 0; i
< k
+ p
&& nerrs
< p
; i
++)
107 frag_err_list
[nerrs
++] = i
;
117 // Check for valid parameters
118 if (m
> MMAX
|| k
> KMAX
|| m
< 0 || p
< 1 || k
< 1) {
119 printf(" Input test parameter error m=%d, k=%d, p=%d, erasures=%d\n",
124 printf(" Number of erasures chosen exceeds power of code erasures=%d p=%d\n",
128 for (i
= 0; i
< nerrs
; i
++) {
129 if (frag_err_list
[i
] >= m
) {
130 printf(" fragment %d not in range\n", frag_err_list
[i
]);
135 printf("ec_simple_example:\n");
137 // Allocate coding matrices
138 encode_matrix
= malloc(m
* k
);
139 decode_matrix
= malloc(m
* k
);
140 invert_matrix
= malloc(m
* k
);
141 temp_matrix
= malloc(m
* k
);
142 g_tbls
= malloc(k
* p
* 32);
144 if (encode_matrix
== NULL
|| decode_matrix
== NULL
145 || invert_matrix
== NULL
|| temp_matrix
== NULL
|| g_tbls
== NULL
) {
146 printf("Test failure! Error with malloc\n");
149 // Allocate the src & parity buffers
150 for (i
= 0; i
< m
; i
++) {
151 if (NULL
== (frag_ptrs
[i
] = malloc(len
))) {
152 printf("alloc error: Fail\n");
157 // Allocate buffers for recovered data
158 for (i
= 0; i
< p
; i
++) {
159 if (NULL
== (recover_outp
[i
] = malloc(len
))) {
160 printf("alloc error: Fail\n");
165 // Fill sources with random data
166 for (i
= 0; i
< k
; i
++)
167 for (j
= 0; j
< len
; j
++)
168 frag_ptrs
[i
][j
] = rand();
170 printf(" encode (m,k,p)=(%d,%d,%d) len=%d\n", m
, k
, p
, len
);
172 // Pick an encode matrix. A Cauchy matrix is a good choice as even
173 // large k are always invertable keeping the recovery rule simple.
174 gf_gen_cauchy1_matrix(encode_matrix
, m
, k
);
176 // Initialize g_tbls from encode matrix
177 ec_init_tables(k
, p
, &encode_matrix
[k
* k
], g_tbls
);
179 // Generate EC parity blocks from sources
180 ec_encode_data(len
, k
, p
, g_tbls
, frag_ptrs
, &frag_ptrs
[k
]);
185 printf(" recover %d fragments\n", nerrs
);
187 // Find a decode matrix to regenerate all erasures from remaining frags
188 ret
= gf_gen_decode_matrix_simple(encode_matrix
, decode_matrix
,
189 invert_matrix
, temp_matrix
, decode_index
,
190 frag_err_list
, nerrs
, k
, m
);
192 printf("Fail on generate decode matrix\n");
195 // Pack recovery array pointers as list of valid fragments
196 for (i
= 0; i
< k
; i
++)
197 recover_srcs
[i
] = frag_ptrs
[decode_index
[i
]];
200 ec_init_tables(k
, nerrs
, decode_matrix
, g_tbls
);
201 ec_encode_data(len
, k
, nerrs
, g_tbls
, recover_srcs
, recover_outp
);
203 // Check that recovered buffers are the same as original
204 printf(" check recovery of block {");
205 for (i
= 0; i
< nerrs
; i
++) {
206 printf(" %d", frag_err_list
[i
]);
207 if (memcmp(recover_outp
[i
], frag_ptrs
[frag_err_list
[i
]], len
)) {
208 printf(" Fail erasure recovery %d, frag %d\n", i
, frag_err_list
[i
]);
213 printf(" } done all: Pass\n");
218 * Generate decode matrix from encode matrix and erasure list
222 static int gf_gen_decode_matrix_simple(u8
* encode_matrix
,
226 u8
* decode_index
, u8
* frag_err_list
, int nerrs
, int k
,
231 u8 s
, *b
= temp_matrix
;
232 u8 frag_in_err
[MMAX
];
234 memset(frag_in_err
, 0, sizeof(frag_in_err
));
236 // Order the fragments in erasure for easier sorting
237 for (i
= 0; i
< nerrs
; i
++) {
238 if (frag_err_list
[i
] < k
)
240 frag_in_err
[frag_err_list
[i
]] = 1;
243 // Construct b (matrix that encoded remaining frags) by removing erased rows
244 for (i
= 0, r
= 0; i
< k
; i
++, r
++) {
245 while (frag_in_err
[r
])
247 for (j
= 0; j
< k
; j
++)
248 b
[k
* i
+ j
] = encode_matrix
[k
* r
+ j
];
252 // Invert matrix to get recovery matrix
253 if (gf_invert_matrix(b
, invert_matrix
, k
) < 0)
256 // Get decode matrix with only wanted recovery rows
257 for (i
= 0; i
< nerrs
; i
++) {
258 if (frag_err_list
[i
] < k
) // A src err
259 for (j
= 0; j
< k
; j
++)
260 decode_matrix
[k
* i
+ j
] =
261 invert_matrix
[k
* frag_err_list
[i
] + j
];
264 // For non-src (parity) erasures need to multiply encode matrix * invert
265 for (p
= 0; p
< nerrs
; p
++) {
266 if (frag_err_list
[p
] >= k
) { // A parity err
267 for (i
= 0; i
< k
; i
++) {
269 for (j
= 0; j
< k
; j
++)
270 s
^= gf_mul(invert_matrix
[j
* k
+ i
],
271 encode_matrix
[k
* frag_err_list
[p
] + j
]);
272 decode_matrix
[k
* p
+ i
] = s
;