Commit 788c3a4f authored by Rusty Russell's avatar Rusty Russell

tdb2: cleanups for tdbtorture, add more debugging and -S flag.

parent 5e7b20d0
...@@ -49,11 +49,10 @@ static void tdb_log(struct tdb_context *tdb, enum tdb_log_level level, ...@@ -49,11 +49,10 @@ static void tdb_log(struct tdb_context *tdb, enum tdb_log_level level,
fflush(stdout); fflush(stdout);
#if 0 #if 0
{ {
char *ptr; char str[200];
signal(SIGUSR1, SIG_IGN); signal(SIGUSR1, SIG_IGN);
asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid()); sprintf(str,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
system(ptr); system(str);
free(ptr);
} }
#endif #endif
} }
...@@ -72,9 +71,10 @@ static void segv_handler(int sig, siginfo_t *info, void *p) ...@@ -72,9 +71,10 @@ static void segv_handler(int sig, siginfo_t *info, void *p)
_exit(11); _exit(11);
} }
static void fatal(const char *why) static void fatal(struct tdb_context *tdb, const char *why)
{ {
perror(why); fprintf(stderr, "%u:%s:%s\n", getpid(), why,
tdb ? tdb_errorstr(tdb_error(tdb)) : "(no tdb)");
error_count++; error_count++;
} }
...@@ -143,7 +143,7 @@ static void addrec_db(void) ...@@ -143,7 +143,7 @@ static void addrec_db(void)
#if TRANSACTION_PROB #if TRANSACTION_PROB
if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) { if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
if (tdb_transaction_start(db) != 0) { if (tdb_transaction_start(db) != 0) {
fatal("tdb_transaction_start failed"); fatal(db, "tdb_transaction_start failed");
} }
in_transaction++; in_transaction++;
goto next; goto next;
...@@ -151,11 +151,11 @@ static void addrec_db(void) ...@@ -151,11 +151,11 @@ static void addrec_db(void)
if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) { if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
if (random() % TRANSACTION_PREPARE_PROB == 0) { if (random() % TRANSACTION_PREPARE_PROB == 0) {
if (tdb_transaction_prepare_commit(db) != 0) { if (tdb_transaction_prepare_commit(db) != 0) {
fatal("tdb_transaction_prepare_commit failed"); fatal(db, "tdb_transaction_prepare_commit failed");
} }
} }
if (tdb_transaction_commit(db) != 0) { if (tdb_transaction_commit(db) != 0) {
fatal("tdb_transaction_commit failed"); fatal(db, "tdb_transaction_commit failed");
} }
in_transaction--; in_transaction--;
goto next; goto next;
...@@ -178,7 +178,7 @@ static void addrec_db(void) ...@@ -178,7 +178,7 @@ static void addrec_db(void)
#if STORE_PROB #if STORE_PROB
if (random() % STORE_PROB == 0) { if (random() % STORE_PROB == 0) {
if (tdb_store(db, key, data, TDB_REPLACE) != 0) { if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
fatal("tdb_store failed"); fatal(db, "tdb_store failed");
} }
goto next; goto next;
} }
...@@ -187,7 +187,7 @@ static void addrec_db(void) ...@@ -187,7 +187,7 @@ static void addrec_db(void)
#if APPEND_PROB #if APPEND_PROB
if (random() % APPEND_PROB == 0) { if (random() % APPEND_PROB == 0) {
if (tdb_append(db, key, data) != 0) { if (tdb_append(db, key, data) != 0) {
fatal("tdb_append failed"); fatal(db, "tdb_append failed");
} }
goto next; goto next;
} }
...@@ -196,9 +196,12 @@ static void addrec_db(void) ...@@ -196,9 +196,12 @@ static void addrec_db(void)
#if LOCKSTORE_PROB #if LOCKSTORE_PROB
if (random() % LOCKSTORE_PROB == 0) { if (random() % LOCKSTORE_PROB == 0) {
tdb_chainlock(db, key); tdb_chainlock(db, key);
tdb_fetch(db, key, &data); if (tdb_fetch(db, key, &data) != TDB_SUCCESS) {
data.dsize = 0;
data.dptr = NULL;
}
if (tdb_store(db, key, data, TDB_REPLACE) != 0) { if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
fatal("tdb_store failed"); fatal(db, "tdb_store failed");
} }
if (data.dptr) free(data.dptr); if (data.dptr) free(data.dptr);
tdb_chainunlock(db, key); tdb_chainunlock(db, key);
...@@ -237,7 +240,7 @@ static void usage(void) ...@@ -237,7 +240,7 @@ static void usage(void)
#if TRANSACTION_PROB #if TRANSACTION_PROB
" [-t]" " [-t]"
#endif #endif
" [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n"); " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S]\n");
exit(0); exit(0);
} }
...@@ -250,16 +253,17 @@ static void send_count_and_suicide(int sig) ...@@ -250,16 +253,17 @@ static void send_count_and_suicide(int sig)
kill(getpid(), SIGUSR2); kill(getpid(), SIGUSR2);
} }
static int run_child(int i, int seed, unsigned num_loops, unsigned start) static int run_child(int i, int seed, unsigned num_loops, unsigned start,
int tdb_flags)
{ {
struct sigaction act = { .sa_sigaction = segv_handler, struct sigaction act = { .sa_sigaction = segv_handler,
.sa_flags = SA_SIGINFO }; .sa_flags = SA_SIGINFO };
sigaction(11, &act, NULL); sigaction(11, &act, NULL);
db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600, db = tdb_open("torture.tdb", tdb_flags, O_RDWR | O_CREAT, 0600,
&log_attr); &log_attr);
if (!db) { if (!db) {
fatal("db open failed"); fatal(NULL, "db open failed");
} }
#if 0 #if 0
...@@ -290,7 +294,7 @@ static int run_child(int i, int seed, unsigned num_loops, unsigned start) ...@@ -290,7 +294,7 @@ static int run_child(int i, int seed, unsigned num_loops, unsigned start)
in_transaction--; in_transaction--;
} }
if (tdb_transaction_start(db) != 0) if (tdb_transaction_start(db) != 0)
fatal("tdb_transaction_start failed"); fatal(db, "tdb_transaction_start failed");
} }
#endif #endif
tdb_traverse(db, traverse_fn, NULL); tdb_traverse(db, traverse_fn, NULL);
...@@ -299,7 +303,7 @@ static int run_child(int i, int seed, unsigned num_loops, unsigned start) ...@@ -299,7 +303,7 @@ static int run_child(int i, int seed, unsigned num_loops, unsigned start)
#if TRANSACTION_PROB #if TRANSACTION_PROB
if (always_transaction) { if (always_transaction) {
if (tdb_transaction_commit(db) != 0) if (tdb_transaction_commit(db) != 0)
fatal("tdb_transaction_commit failed"); fatal(db, "tdb_transaction_commit failed");
} }
#endif #endif
} }
...@@ -319,13 +323,14 @@ int main(int argc, char * const *argv) ...@@ -319,13 +323,14 @@ int main(int argc, char * const *argv)
pid_t *pids; pid_t *pids;
int kill_random = 0; int kill_random = 0;
int *done; int *done;
int tdb_flags = TDB_DEFAULT;
log_attr.base.attr = TDB_ATTRIBUTE_LOG; log_attr.base.attr = TDB_ATTRIBUTE_LOG;
log_attr.base.next = &seed_attr; log_attr.base.next = &seed_attr;
log_attr.log.log_fn = tdb_log; log_attr.log.log_fn = tdb_log;
seed_attr.base.attr = TDB_ATTRIBUTE_SEED; seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) { while ((c = getopt(argc, argv, "n:l:s:thkS")) != -1) {
switch (c) { switch (c) {
case 'n': case 'n':
num_procs = strtol(optarg, NULL, 0); num_procs = strtol(optarg, NULL, 0);
...@@ -336,6 +341,9 @@ int main(int argc, char * const *argv) ...@@ -336,6 +341,9 @@ int main(int argc, char * const *argv)
case 's': case 's':
seed = strtol(optarg, NULL, 0); seed = strtol(optarg, NULL, 0);
break; break;
case 'S':
tdb_flags = TDB_NOSYNC;
break;
case 't': case 't':
#if TRANSACTION_PROB #if TRANSACTION_PROB
always_transaction = 1; always_transaction = 1;
...@@ -361,7 +369,7 @@ int main(int argc, char * const *argv) ...@@ -361,7 +369,7 @@ int main(int argc, char * const *argv)
if (num_procs == 1 && !kill_random) { if (num_procs == 1 && !kill_random) {
/* Don't fork for this case, makes debugging easier. */ /* Don't fork for this case, makes debugging easier. */
error_count = run_child(0, seed, num_loops, 0); error_count = run_child(0, seed, num_loops, 0, tdb_flags);
goto done; goto done;
} }
...@@ -387,7 +395,7 @@ int main(int argc, char * const *argv) ...@@ -387,7 +395,7 @@ int main(int argc, char * const *argv)
#endif #endif
); );
} }
exit(run_child(i, seed, num_loops, 0)); exit(run_child(i, seed, num_loops, 0, tdb_flags));
} }
} }
...@@ -444,7 +452,7 @@ int main(int argc, char * const *argv) ...@@ -444,7 +452,7 @@ int main(int argc, char * const *argv)
pids[j] = fork(); pids[j] = fork();
if (pids[j] == 0) if (pids[j] == 0)
exit(run_child(j, seed, num_loops, exit(run_child(j, seed, num_loops,
done[j])); done[j], tdb_flags));
printf("Restarting child %i for %u-%u\n", printf("Restarting child %i for %u-%u\n",
j, done[j], num_loops); j, done[j], num_loops);
continue; continue;
...@@ -471,10 +479,11 @@ done: ...@@ -471,10 +479,11 @@ done:
db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
0600, &log_attr); 0600, &log_attr);
if (!db) { if (!db) {
fatal("db open failed"); fatal(db, "db open failed");
exit(1);
} }
if (tdb_check(db, NULL, NULL) == -1) { if (tdb_check(db, NULL, NULL) != 0) {
printf("db check failed"); fatal(db, "db check failed");
exit(1); exit(1);
} }
tdb_close(db); tdb_close(db);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment