bcachefs-tools/bcache-test.c

187 lines
4.2 KiB
C
Raw Normal View History

2010-05-05 05:58:22 +04:00
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/fs.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
2010-05-14 20:54:26 +04:00
#include <stdint.h>
2010-05-05 05:58:22 +04:00
#define Pread(fd, buf, size, offset) do { \
int _read = 0, _r; \
while (_read < size) { \
_r = pread(fd, buf, (size) - _read, (offset) + _read); \
if (_r <= 0) \
goto err; \
_read += _r; \
} \
} while (0)
/* Marsaglia polar method
*/
double normal()
{
double x, y, s;
static double n = 0 / (double) 0;
if (n == n) {
x = n;
n = 0 / (double) 0;
return x;
}
do {
x = random() / (double) (RAND_MAX / 2) - 1;
y = random() / (double) (RAND_MAX / 2) - 1;
s = x * x + y * y;
} while (s >= 1);
s = sqrt(-2 * log(s) / s);
n = y * s;
return x * s;
}
2010-05-14 20:54:26 +04:00
uint32_t fletcher32(uint16_t *data, size_t len)
{
uint32_t sum1 = 0xffff, sum2 = 0xffff;
while (len) {
unsigned tlen = len > 360 ? 360 : len;
len -= tlen;
do {
sum1 += *data++;
sum2 += sum1;
} while (--tlen);
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
}
/* Second reduction step to reduce sums to 16 bits */
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
return sum2 << 16 | sum1;
}
2010-05-05 05:58:22 +04:00
long getblocks(int fd)
{
long ret;
struct stat statbuf;
if (fstat(fd, &statbuf)) {
2010-05-14 20:54:26 +04:00
perror("stat error");
2010-05-05 05:58:22 +04:00
exit(EXIT_FAILURE);
}
2010-05-14 20:54:26 +04:00
ret = statbuf.st_size / 512;
2010-05-05 05:58:22 +04:00
if (S_ISBLK(statbuf.st_mode))
if (ioctl(fd, BLKGETSIZE, &ret)) {
perror("ioctl error");
exit(EXIT_FAILURE);
}
return ret;
}
int main(int argc, char **argv)
{
2010-05-14 20:54:26 +04:00
bool walk = false, randsize = false, verbose = false, csum = false;
int fd1, fd2 = 0, direct = 0, nbytes = 4096, j;
unsigned long size, i, offset = 0;
2010-05-08 22:31:53 +04:00
void *buf1 = NULL, *buf2 = NULL;
2010-05-14 20:54:26 +04:00
uint32_t *csums = NULL;
2010-05-05 05:58:22 +04:00
if (argc < 3) {
printf("Please enter a cache device and raw device\n");
exit(EXIT_FAILURE);
}
2010-05-14 20:54:26 +04:00
for (i = 1; i < argc; i++) {
2010-05-05 05:58:22 +04:00
if (strcmp(argv[i], "direct") == 0)
direct = O_DIRECT;
2010-05-08 22:31:53 +04:00
else if (strcmp(argv[i], "walk") == 0)
2010-05-05 05:58:22 +04:00
walk = true;
2010-05-08 22:31:53 +04:00
else if (strcmp(argv[i], "verbose") == 0)
2010-05-05 05:58:22 +04:00
verbose = true;
2010-05-08 22:31:53 +04:00
else if (strcmp(argv[i], "size") == 0)
2010-05-05 05:58:22 +04:00
randsize = true;
2010-05-14 20:54:26 +04:00
else if (strcmp(argv[i], "csum") == 0)
csum= true;
else
break;
2010-05-05 05:58:22 +04:00
}
2010-05-14 20:54:26 +04:00
fd1 = open(argv[i], O_RDONLY|direct);
size = getblocks(fd1);
if (!csum) {
fd2 = open(argv[2], O_RDONLY|direct);
size = MIN(size, getblocks(fd2));
} else
csums = calloc((size / 8 + 1), sizeof(*csums));
2010-05-05 05:58:22 +04:00
if (fd1 == -1 || fd2 == -1) {
perror("Error opening device");
exit(EXIT_FAILURE);
}
2010-05-14 20:54:26 +04:00
size = size / 8 - 16;
2010-05-05 05:58:22 +04:00
printf("size %li\n", size);
2010-05-08 22:31:53 +04:00
if (posix_memalign(&buf1, 4096, 4096 * 16) ||
posix_memalign(&buf2, 4096, 4096 * 16)) {
printf("Could not allocate buffers\n");
exit(EXIT_FAILURE);
}
2010-05-14 20:54:26 +04:00
setvbuf(stdout, NULL, _IONBF, 0);
2010-05-05 05:58:22 +04:00
2010-05-14 20:54:26 +04:00
for (i = 0;; i++) {
2010-05-08 22:31:53 +04:00
if (randsize)
nbytes = 4096 * (int) (drand48() * 16 + 1);
2010-05-05 05:58:22 +04:00
2010-05-14 20:54:26 +04:00
offset += walk ? normal() * 100 : random();
2010-05-08 22:31:53 +04:00
offset %= size;
assert(offset < size);
2010-05-14 20:54:26 +04:00
if (verbose)
printf("Loop %li offset %li sectors %i\n",
i, offset << 3, nbytes >> 9);
else if (!(i % 100))
printf("Loop %li\n", i);
Pread(fd1, buf1, nbytes, offset << 12);
2010-05-08 22:31:53 +04:00
2010-05-14 20:54:26 +04:00
if (!csum) {
2010-05-08 22:31:53 +04:00
Pread(fd2, buf2, nbytes, offset << 12);
for (j = 0; j < nbytes; j += 512)
if (memcmp(buf1 + j,
buf2 + j,
512)) {
2010-05-14 20:54:26 +04:00
printf("Bad read! loop %li offset %li sectors %i, sector %i\n",
i, offset << 3, nbytes >> 9, j >> 9);
exit(EXIT_FAILURE);
}
} else
for (j = 0; j < nbytes / 4096; j++) {
int c = fletcher32(buf1 + j * 4096, 4096);
if (!csums[offset + j])
csums[offset + j] = c;
else if (csums[offset + j] != c) {
printf("Bad read! loop %li offset %li sectors %i, sector %i\n",
i, offset << 3, nbytes >> 9, j << 3);
2010-05-08 22:31:53 +04:00
exit(EXIT_FAILURE);
}
2010-05-14 20:54:26 +04:00
}
2010-05-05 05:58:22 +04:00
}
err:
perror("Read error");
exit(EXIT_FAILURE);
}