Add some test and debug helpers

master
Harald Wolff 2022-08-21 22:58:34 +02:00
parent 7840ba3183
commit 0efdb551d8
3 changed files with 135 additions and 1 deletions

View File

@ -10,4 +10,6 @@ add_executable(mksparse src/mksparse.c)
add_executable(hashtest src/hashtest.c src/hash.c)
add_executable(test_merkle src/test_merkle.c src/merkle.c)
add_executable(test_mmap src/test_mmap.c)
add_executable(test_mmap src/test_mmap.c)
add_executable(fillblocks src/fillblocks.c)
add_executable(diffblocks src/diffblocks.c src/hash.c)

72
src/diffblocks.c 100644
View File

@ -0,0 +1,72 @@
//
// Created by haraldwolff on 21.08.22.
//
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <hash.h>
int main(int argc, char *argv[]){
int f1 = open(argv[1], O_RDWR);
if (f1 < 0)
{
printf("could not open %s (%s)\n", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
long size1 = lseek(f1, 0, SEEK_END);
void *mm1 = mmap(NULL, size1, PROT_READ | PROT_WRITE, MAP_SHARED, f1, 0);
if (mm1 == MAP_FAILED){
printf("could not mmap file.");
exit(EXIT_FAILURE);
}
int f2 = open(argv[2], O_RDWR);
if (f2 < 0)
{
printf("could not open %s (%s)\n", argv[2], strerror(errno));
exit(EXIT_FAILURE);
}
long size2 = lseek(f2, 0, SEEK_END);
void *mm2 = mmap(NULL, size2, PROT_READ | PROT_WRITE, MAP_SHARED, f2, 0);
if (mm2 == MAP_FAILED){
printf("could not mmap file.");
exit(EXIT_FAILURE);
}
long offset = 0;
u_int64_t *t1 = (u_int64_t *)mm1;
u_int64_t *t2 = (u_int64_t *)mm2;
while (offset < size1) {
uint64_t h1 = dhash( &t1[(offset >> 3)], 4096);
uint64_t h2 = dhash( &t2[(offset >> 3)], 4096);
if (h1 != h2) {
fprintf(stdout,"\r%0lx %0lx %0lx\n", offset, h1, h2);
fflush(stdout);
}
offset += 4096;
fprintf(stdout, "\roffset: 0x%llx %d%%", offset, (100 * offset / size1));
fflush(stdout);
}
munmap(mm1, size1);
munmap(mm2, size2);
close(f1);
close(f2);
}

60
src/fillblocks.c 100644
View File

@ -0,0 +1,60 @@
//
// Created by haraldwolff on 21.08.22.
//
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int ch;
char filename[1024] = "";
while ((ch = getopt(argc, argv, "f:"))!=-1){
switch (ch){
case 'f':
strncpy(filename, optarg, 1024);
break;
}
}
if (!filename[0]){
printf("need filename: blockfill -f <filename>\n");
exit(EXIT_FAILURE);
}
int f = open(filename, O_RDWR);
if (f < 0)
{
printf("could not open %s (%s)\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
long size = lseek(f, 0, SEEK_END);
void *mm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0);
if (mm == MAP_FAILED){
printf("could not mmap file.");
} else {
long offset = 0;
u_int64_t *t = (u_int64_t *)mm;
while (offset < size) {
for (int n = 0; n < 512; n++) {
t[ (offset >> 3) + n ] = offset;
}
offset += 4096;
printf("\roffset: 0x%llx %d%%", offset, (100 * offset / size));
}
printf("\n");
munmap(mm, size);
}
close(f);
}