Writing/checksumming

This commit is contained in:
Kent Overstreet 2010-05-23 10:14:49 -08:00
parent bbe6fe14d2
commit 71b4a29224

View File

@ -87,13 +87,19 @@ long getblocks(int fd)
return ret; return ret;
} }
struct pagestuff {
unsigned char csum[16];
int readcount;
int writecount;
};
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
bool walk = false, randsize = false, verbose = false, csum = false, destructive = false; bool walk = false, randsize = false, verbose = false, csum = false, destructive = false;
int fd1, fd2 = 0, direct = 0, nbytes = 4096, j; int fd1, fd2 = 0, direct = 0, nbytes = 4096, j;
unsigned long size, i, offset = 0; unsigned long size, i, offset = 0, done = 0;
void *buf1 = NULL, *buf2 = NULL; void *buf1 = NULL, *buf2 = NULL;
uint64_t *csums = NULL, *cp, c[2]; struct pagestuff *pages, *p;
RC4_KEY writedata; RC4_KEY writedata;
RC4_set_key(&writedata, 16, bcache_magic); RC4_set_key(&writedata, 16, bcache_magic);
@ -139,7 +145,7 @@ int main(int argc, char **argv)
size = MIN(size, getblocks(fd2)); size = MIN(size, getblocks(fd2));
size = size / 8 - 16; size = size / 8 - 16;
csums = calloc(size + 16, sizeof(*csums)); pages = calloc(size + 16, sizeof(*pages));
printf("size %li\n", size); printf("size %li\n", size);
if (posix_memalign(&buf1, 4096, 4096 * 16) || if (posix_memalign(&buf1, 4096, 4096 * 16) ||
@ -147,20 +153,21 @@ int main(int argc, char **argv)
printf("Could not allocate buffers\n"); printf("Could not allocate buffers\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
setvbuf(stdout, NULL, _IONBF, 0); //setvbuf(stdout, NULL, _IONBF, 0);
for (i = 0;; i++) { for (i = 0;; i++) {
bool writing = destructive && (i & 1); bool writing = destructive && (i & 1);
nbytes = randsize ? drand48() * 16 + 1 : 1; nbytes = randsize ? drand48() * 16 + 1 : 1;
nbytes <<= 12; nbytes <<= 12;
offset += walk ? normal() * 100 : random(); offset += walk ? normal() * 10 : random();
offset %= size; offset %= size;
offset <<= 12; offset <<= 12;
if (verbose || !(i % 100)) if (verbose || !(i % 100))
printf("Loop %li offset %li sectors %i\n", printf("Loop %6li offset %9li sectors %3i, %6lu mb done\n",
i, offset >> 9, nbytes >> 9); i, offset >> 9, nbytes >> 9, done >> 11);
done += nbytes >> 9;
if (!writing) if (!writing)
Pread(fd1, buf1, nbytes, offset); Pread(fd1, buf1, nbytes, offset);
@ -168,22 +175,27 @@ int main(int argc, char **argv)
Pread(fd2, buf2, nbytes, offset); Pread(fd2, buf2, nbytes, offset);
for (j = 0; j < nbytes; j += 4096) { for (j = 0; j < nbytes; j += 4096) {
p = &pages[(offset + j) / 4096];
if (writing) if (writing)
RC4(&writedata, 4096, zero, buf1 + j); RC4(&writedata, 4096, zero, buf1 + j);
if (csum) { if (csum) {
MD4(buf1 + j, 4096, (void*) c); unsigned char c[16];
cp = csums + (offset + j) / 4096; MD4(buf1 + j, 4096, &c[0]);
if (writing || !*cp) if (writing ||
*cp = c[0]; (!p->readcount && !p->writecount))
else if (*cp != c[0]) memcpy(&p->csum[0], c, 16);
else if (memcmp(&p->csum[0], c, 16))
goto bad; goto bad;
} else if (!writing && } else if (!writing &&
memcmp(buf1 + j, memcmp(buf1 + j,
buf2 + j, buf2 + j,
4096)) 4096))
goto bad; goto bad;
writing ? p->writecount++ : p->readcount++;
} }
if (writing) if (writing)
Pwrite(fd1, buf1, nbytes, offset); Pwrite(fd1, buf1, nbytes, offset);
@ -194,7 +206,7 @@ err:
perror("IO error"); perror("IO error");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
bad: bad:
printf("Bad read! loop %li offset %li sectors %i, sector %i\n", printf("Bad read! loop %li offset %li sectors %i, sector %i, readcount %i writecount %i\n",
i, offset >> 9, nbytes >> 9, j >> 9); i, offset >> 9, nbytes >> 9, j >> 9, p->readcount, p->writecount);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }