Moves RAID tests in the raid/ dir.

This commit is contained in:
Andrea Mazzoleni 2013-12-29 22:13:08 +01:00
parent eb08995834
commit 03482b84a6
16 changed files with 943 additions and 612 deletions

158
raid/combo.h Normal file
View file

@ -0,0 +1,158 @@
/*
* Copyright (C) 2013 Andrea Mazzoleni
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __COMBO_H
#define __COMBO_H
#include <assert.h>
/**
* Get the first permutation with repetition of r of n elements.
*
* Typical use is with permutation_next() in the form :
*
* int i[R];
* permutation_first(R, N, i);
* do {
* code using i[0], i[1], ..., i[R-1]
* } while (permutation_next(R, N, i));
*
* It's equivalent at the code :
*
* for(i[0]=0;i[0]<N;++i[0])
* for(i[1]=0;i[1]<N;++i[1])
* ...
* for(i[R-2]=0;i[R-2]<N;++i[R-2])
* for(i[R-1]=0;i[R-1]<N;++i[R-1])
* code using i[0], i[1], ..., i[R-1]
*/
static inline void permutation_first(int r, int n, int *c)
{
int i;
(void)n; /* unused, but kept for clarity */
assert(0 < r && r <= n);
for (i = 0; i < r; ++i)
c[i] = 0;
}
/**
* Get the next permutation with repetition of r of n elements.
* Return ==0 when finished.
*/
static inline int permutation_next(int r, int n, int *c)
{
int i = r - 1; /* present position */
recurse:
/* next element at position i */
++c[i];
/* if the position has reached the max */
if (c[i] >= n) {
/* if we are at the first level, we have finished */
if (i == 0)
return 0;
/* increase the previous position */
--i;
goto recurse;
}
++i;
/* initialize all the next positions, if any */
while (i < r) {
c[i] = 0;
++i;
}
return 1;
}
/**
* Get the first combination without repetition of r of n elements.
*
* Typical use is with combination_next() in the form :
*
* int i[R];
* combination_first(R, N, i);
* do {
* code using i[0], i[1], ..., i[R-1]
* } while (combination_next(R, N, i));
*
* It's equivalent at the code :
*
* for(i[0]=0;i[0]<N-(R-1);++i[0])
* for(i[1]=i[0]+1;i[1]<N-(R-2);++i[1])
* ...
* for(i[R-2]=i[R-3]+1;i[R-2]<N-1;++i[R-2])
* for(i[R-1]=i[R-2]+1;i[R-1]<N;++i[R-1])
* code using i[0], i[1], ..., i[R-1]
*/
static inline void combination_first(int r, int n, int *c)
{
int i;
(void)n; /* unused, but kept for clarity */
assert(0 < r && r <= n);
for (i = 0; i < r; ++i)
c[i] = i;
}
/**
* Get the next combination without repetition of r of n elements.
* Return ==0 when finished.
*/
static inline int combination_next(int r, int n, int *c)
{
int i = r - 1; /* present position */
int h = n; /* high limit for this position */
recurse:
/* next element at position i */
++c[i];
/* if the position has reached the max */
if (c[i] >= h) {
/* if we are at the first level, we have finished */
if (i == 0)
return 0;
/* increase the previous position */
--i;
--h;
goto recurse;
}
++i;
/* initialize all the next positions, if any */
while (i < r) {
/* each position start at the next value of the previous one */
c[i] = c[i-1] + 1;
++i;
}
return 1;
}
#endif

View file

@ -24,12 +24,6 @@
* Basic range checks are implemented using BUG_ON().
*/
/*
* Generic assert() with inverted meaning.
*/
#define BUG_ON(a) assert(!(a))
/*
* GF a*b.
*/

View file

@ -402,7 +402,7 @@ void raid_rec1_int8(int nr, const int *id, const int *ip, int nd, size_t size, v
(void)nr; /* unused, it's always 1 */
/* if it's RAID5 uses the dedicated and faster function */
/* if it's RAID5 uses the faster function */
if (ip[0] == 0) {
raid_rec1_par1(id, nd, size, vv);
return;
@ -459,7 +459,7 @@ void raid_rec2_int8(int nr, const int *id, const int *ip, int nd, size_t size, v
(void)nr; /* unused, it's always 2 */
/* if it's RAID6 recovering with P and Q uses the dedicated and faster function */
/* if it's RAID6 recovering with P and Q uses the faster function */
if (ip[0] == 0 && ip[1] == 1) {
raid_rec2_par2(id, ip, nd, size, vv);
return;

View file

@ -18,8 +18,15 @@
#ifndef __RAID_INTERNAL_H
#define __RAID_INTERNAL_H
/*
* Includes anything required for compatibility.
*/
#include "../portable.h"
#define BUG_ON(a) assert(!(a))
/*
* Include the main header.
*/
#include "raid.h"
/*

125
raid/memory.c Normal file
View file

@ -0,0 +1,125 @@
/*
* Copyright (C) 2013 Andrea Mazzoleni
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "internal.h"
#include "memory.h"
void *raid_malloc_align(size_t size, void **freeptr)
{
unsigned char *ptr;
uintptr_t offset;
ptr = malloc(size + RAID_MALLOC_ALIGN);
if (!ptr)
return 0;
*freeptr = ptr;
offset = ((uintptr_t)ptr) % RAID_MALLOC_ALIGN;
if (offset != 0)
ptr += RAID_MALLOC_ALIGN - offset;
return ptr;
}
void **raid_malloc_vector(size_t reverse, size_t count, size_t size, void **freeptr)
{
void **v;
unsigned char *va;
size_t i;
assert(reverse <= count);
v = malloc(count * sizeof(void *));
if (!v)
return 0;
va = raid_malloc_align(count * (size + RAID_MALLOC_DISPLACEMENT), freeptr);
if (!va) {
free(v);
return 0;
}
for (i = 0; i < count; ++i)
v[i] = va + i * (size + RAID_MALLOC_DISPLACEMENT);
/* reverse order for the initial ones */
for (i = 0; i < reverse/2; ++i) {
void *ptr = v[i];
v[i] = v[reverse - 1 - i];
v[reverse - 1 - i] = ptr;
}
return v;
}
int raid_mtest_vector(void **vv, size_t count, size_t size)
{
unsigned char **v = (unsigned char **)vv;
size_t i;
size_t j;
unsigned k;
unsigned char d;
unsigned char p;
/* fill with 0 */
d = 0;
for (i = 0; i < count; ++i)
for (j = 0; j < size; ++j)
v[i][j] = d;
/* test with all the byte patterns */
for (k = 1; k < 256; ++k) {
p = d;
d = k;
/* forward fill */
for (i = 0; i < count; ++i) {
for (j = 0; j < size; ++j) {
if (v[i][j] != p)
return -1;
v[i][j] = d;
}
}
p = d;
d = ~p;
/* backward fill with complement */
for (i = 0; i < count; ++i) {
for (j = size; j > 0; --j) {
if (v[i][j-1] != p)
return -1;
v[i][j-1] = d;
}
}
}
return 0;
}
void raid_mrand_vector(void **vv, size_t count, size_t size)
{
unsigned char **v = (unsigned char **)vv;
size_t i;
size_t j;
for (i = 0; i < count; ++i)
for (j = 0; j < size; ++j)
v[i][j] = rand();
}

80
raid/memory.h Normal file
View file

@ -0,0 +1,80 @@
/*
* Copyright (C) 2013 Andrea Mazzoleni
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RAID_MEMORY_H
#define __RAID_MEMORY_H
/**
* Memory alignment provided by raid_malloc_align().
*
* It should guarantee good cache performance everywhere.
*/
#define RAID_MALLOC_ALIGN 256
/**
* Memory displacement to avoid cache collisions on contiguous blocks,
* ued by raid_malloc_vector().
*
* When allocating a sequence of blocks with a size of power of 2,
* there is the risk that the start of each block is mapped into the same
* cache line, resulting in cache collisions if you access all the blocks
* in parallel, from the start to the end.
*
* The selected value was choosen empirically with some speed tests
* with 8/12/16/20/24 data buffers and 3 parity buffers
* for RAID-5/6/TP computation.
*
* With displacement (8 buffers, icore5, 32 bit):
*
* PAR1 sse2x4 21936 [MB/s]
* PAR2 sse2x2 11902 [MB/s]
* PARz sse2x1 5838 [MB/s]
*
* Without displacement:
*
* PAR1 sse2x4 15368 [MB/s]
* PAR2 sse2x2 6814 [MB/s]
* PARz sse2x1 3033 [MB/s]
*/
#define RAID_MALLOC_DISPLACEMENT (7*256)
/**
* Aligned malloc.
*/
void *raid_malloc_align(size_t size, void **freeptr);
/**
* Aligned vector allocation.
* Returns a vector of "count" pointers, each one pointing to a block of the
* specified "size".
* The first "reverse" pointers are put in reverse memory offset order.
*/
void **raid_malloc_vector(size_t reverse, size_t count, size_t size, void **freeptr);
/**
* Tests the memory vector for RAM problems.
* If a problem is found, it crashes.
*/
int raid_mtest_vector(void **vv, size_t count, size_t size);
/**
* Fills the memory vector with random data.
*/
void raid_mrand_vector(void **vv, size_t count, size_t size);
#endif

View file

@ -44,7 +44,6 @@
/**
* Maximum number of data disks.
* Limit of the parity generator matrix used.
*/
#define RAID_DATA_MAX 251

331
raid/test.c Normal file
View file

@ -0,0 +1,331 @@
/*
* Copyright (C) 2013 Andrea Mazzoleni
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "internal.h"
#include "cpu.h"
#include "combo.h"
#include "memory.h"
/**
* Binomial coefficient of n over r.
*/
static int ibc(int n, int r)
{
if (r == 0 || n == r)
return 1;
else
return ibc(n - 1, r - 1) + ibc(n - 1, r);
}
/**
* Power n ^ r;
*/
static int ipow(int n, int r)
{
int v = 1;
while (r) {
v *= n;
--r;
}
return v;
}
int raid_test_combo(void)
{
int r;
int count;
int p[RAID_PARITY_MAX];
for (r = 1; r <= RAID_PARITY_MAX; ++r) {
/* count combination (r of RAID_PARITY_MAX) elements */
count = 0;
combination_first(r, RAID_PARITY_MAX, p);
do {
++count;
} while (combination_next(r, RAID_PARITY_MAX, p));
if (count != ibc(RAID_PARITY_MAX, r))
return -1;
}
for (r = 1; r <= RAID_PARITY_MAX; ++r) {
/* count permutation (r of RAID_PARITY_MAX) elements */
count = 0;
permutation_first(r, RAID_PARITY_MAX, p);
do {
++count;
} while (permutation_next(r, RAID_PARITY_MAX, p));
if (count != ipow(RAID_PARITY_MAX, r))
return -1;
}
return 0;
}
int raid_test_sort(void)
{
int p[RAID_PARITY_MAX];
int r;
for (r = 1; r <= RAID_PARITY_MAX; ++r) {
permutation_first(r, RAID_PARITY_MAX, p);
do {
int i[RAID_PARITY_MAX];
int j;
/* make a copy */
for (j = 0; j < r; ++j)
i[j] = p[j];
raid_sort(r, i);
/* check order */
for (j = 1; j < r; ++j)
if (i[j-1] > i[j])
return -1;
} while (permutation_next(r, RAID_PARITY_MAX, p));
}
return 0;
}
int raid_test_rec(int mode, int nd, size_t size)
{
void *buffer_alloc;
void **buffer;
void **data;
void **parity;
void **test;
void *data_save[RAID_PARITY_MAX];
void *parity_save[RAID_PARITY_MAX];
void *zero;
void *waste;
int buffermax;
int id[RAID_PARITY_MAX];
int ip[RAID_PARITY_MAX];
int i;
int j;
int nr;
void (*map[RAID_PARITY_MAX][4])(int nr, const int *id, const int *ip, int nd, size_t size, void **vbuf);
int mac[RAID_PARITY_MAX];
int np;
raid_mode(mode);
if (mode == RAID_MODE_CAUCHY)
np = RAID_PARITY_MAX;
else
np = 3;
buffermax = nd + np * 2 + 2;
buffer = raid_malloc_vector(nd, buffermax, size, &buffer_alloc);
if (!buffer)
return -1;
data = buffer;
parity = buffer + nd;
test = buffer + nd + np;
for (i = 0; i < np; ++i)
parity_save[i] = parity[i];
zero = buffer[buffermax-2];
memset(zero, 0, size);
raid_zero(zero);
waste = buffer[buffermax-1];
/* fill data disk with random */
raid_mrand_vector(buffer, nd, size);
/* setup recov functions */
for (i = 0; i < np; ++i) {
mac[i] = 0;
if (i == 0) {
map[i][mac[i]++] = raid_rec1_int8;
#if defined(__i386__) || defined(__x86_64__)
if (raid_cpu_has_ssse3())
map[i][mac[i]++] = raid_rec1_ssse3;
#endif
} else if (i == 1) {
map[i][mac[i]++] = raid_rec2_int8;
#if defined(__i386__) || defined(__x86_64__)
if (raid_cpu_has_ssse3())
map[i][mac[i]++] = raid_rec2_ssse3;
#endif
} else {
map[i][mac[i]++] = raid_recX_int8;
#if defined(__i386__) || defined(__x86_64__)
if (raid_cpu_has_ssse3())
map[i][mac[i]++] = raid_recX_ssse3;
#endif
}
}
/* compute the parity */
raid_par(nd, np, size, buffer);
/* set all the parity to the waste buffer */
for (i = 0; i < np; ++i)
parity[i] = waste;
/* all parity levels */
for (nr = 1; nr <= np; ++nr) {
/* all combinations (nr of nd) disks */
combination_first(nr, nd, id);
do {
/* all combinations (nr of np) parities */
combination_first(nr, np, ip);
do {
/* for each recover function */
for (j = 0; j < mac[nr-1]; ++j) {
/* set */
for (i = 0; i < nr; ++i) {
/* remove the missing data */
data_save[i] = data[id[i]];
data[id[i]] = test[i];
/* set the parity to use */
parity[ip[i]] = parity_save[ip[i]];
}
/* recover */
map[nr-1][j](nr, id, ip, nd, size, buffer);
/* check */
for (i = 0; i < nr; ++i)
if (memcmp(test[i], data_save[i], size) != 0)
return -1;
/* restore */
for (i = 0; i < nr; ++i) {
/* restore the data */
data[id[i]] = data_save[i];
/* restore the parity */
parity[ip[i]] = waste;
}
}
} while (combination_next(nr, np, ip));
} while (combination_next(nr, nd, id));
}
free(buffer_alloc);
free(buffer);
return 0;
}
int raid_test_par(int mode, int nd, size_t size)
{
void *buffer_alloc;
void **buffer;
int buffermax;
int i, j;
void (*map[64])(int nd, size_t size, void **vbuf);
int mac;
int np;
raid_mode(mode);
if (mode == RAID_MODE_CAUCHY)
np = RAID_PARITY_MAX;
else
np = 3;
buffermax = nd + np * 2;
buffer = raid_malloc_vector(nd, buffermax, size, &buffer_alloc);
if (!buffer)
return -1;
/* fill with random */
raid_mrand_vector(buffer, buffermax, size);
/* compute the parity */
raid_par(nd, np, size, buffer);
/* copy in back buffers */
for (i = 0; i < np; ++i)
memcpy(buffer[nd + np + i], buffer[nd + i], size);
/* load all the available functions */
mac = 0;
map[mac++] = raid_par1_int32;
map[mac++] = raid_par1_int64;
map[mac++] = raid_par2_int32;
map[mac++] = raid_par2_int64;
#if defined(__i386__) || defined(__x86_64__)
if (raid_cpu_has_sse2()) {
map[mac++] = raid_par1_sse2;
map[mac++] = raid_par2_sse2;
#if defined(__x86_64__)
map[mac++] = raid_par2_sse2ext;
#endif
}
if (mode == RAID_MODE_CAUCHY) {
map[mac++] = raid_par3_int8;
map[mac++] = raid_par4_int8;
map[mac++] = raid_par5_int8;
map[mac++] = raid_par6_int8;
if (raid_cpu_has_ssse3()) {
map[mac++] = raid_par3_ssse3;
map[mac++] = raid_par4_ssse3;
map[mac++] = raid_par5_ssse3;
map[mac++] = raid_par6_ssse3;
#if defined(__x86_64__)
map[mac++] = raid_par3_ssse3ext;
map[mac++] = raid_par4_ssse3ext;
map[mac++] = raid_par5_ssse3ext;
map[mac++] = raid_par6_ssse3ext;
#endif
}
#endif
} else {
#if defined(__i386__) || defined(__x86_64__)
map[mac++] = raid_parz_int32;
map[mac++] = raid_parz_int64;
if (raid_cpu_has_sse2()) {
map[mac++] = raid_parz_sse2;
#if defined(__x86_64__)
map[mac++] = raid_parz_sse2ext;
#endif
}
#endif
}
/* check all the functions */
for (j = 0; j < mac; ++j) {
/* compute parity */
map[j](nd, size, buffer);
/* check it */
for (i = 0; i < np; ++i)
if (memcmp(buffer[nd + np + i], buffer[nd + i], size) != 0)
return -1;
}
free(buffer_alloc);
free(buffer);
return 0;
}

62
raid/test.h Normal file
View file

@ -0,0 +1,62 @@
/*
* Copyright (C) 2013 Andrea Mazzoleni
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RAID_TEST_H
#define __RAID_TEST_H
/**
* Tests sorting functions.
*
* Test raid_sort() with all the possible combinations of elements to sort.
*
* Returns 0 on success.
*/
int raid_test_sort(void);
/**
* Tests combinations functions.
*
* Tests combination_first() and combination_next() for all the parity levels.
*
* Returns 0 on success.
*/
int raid_test_combo(void);
/**
* Tests recovering functions.
*
* All the recovering functions are tested with all the combinations
* of failing disks and recovering parities.
*
* Take care that the test time grows exponentially with the number of disks.
*
* Returns 0 on success.
*/
int raid_test_rec(unsigned mode, int nd, size_t size);
/**
* Tests parity generation functions.
*
* All the parity generation functions are tested with the specified
* number of disks.
*
* Returns 0 on success.
*/
int raid_test_par(unsigned mode, int nd, size_t size);
#endif

View file

@ -1338,7 +1338,7 @@ void raid_rec1_ssse3(int nr, const int *id, const int *ip, int nd, size_t size,
(void)nr; /* unused, it's always 1 */
/* if it's RAID5 uses the dedicated and faster function */
/* if it's RAID5 uses the faster function */
if (ip[0] == 0) {
raid_rec1_par1(id, nd, size, vv);
return;