1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
5 #include <ccan/tdb2/tdb2.h>
14 #include <sys/types.h>
19 //#define REOPEN_PROB 30
23 //#define TRANSACTION_PROB 10
24 //#define TRANSACTION_PREPARE_PROB 2
25 #define LOCKSTORE_PROB 5
26 #define TRAVERSE_PROB 20
27 #define TRAVERSE_READ_PROB 20
28 #define TRAVERSE_MOD_PROB 100
29 #define TRAVERSE_ABORT_PROB 500
34 static struct tdb_context *db;
35 static int in_transaction;
36 static int in_traverse;
37 static int error_count;
39 static int always_transaction = 0;
42 static int count_pipe;
43 static union tdb_attribute log_attr;
45 #ifdef PRINTF_ATTRIBUTE
46 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
48 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...)
52 if (level != TDB_DEBUG_TRACE)
56 vfprintf(stdout, format, ap);
60 if (level != TDB_DEBUG_TRACE) {
62 signal(SIGUSR1, SIG_IGN);
63 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
70 #include "../private.h"
72 static void segv_handler(int signal, siginfo_t *info, void *p)
76 sprintf(string, "%u: death at %p (map_ptr %p, map_size %llu)\n",
77 getpid(), info->si_addr, db->map_ptr, db->map_size);
78 write(2, string, strlen(string));
83 static void fatal(const char *why)
89 static char *randbuf(int len)
93 buf = (char *)malloc(len+1);
96 buf[i] = 'a' + (rand() % 26);
102 static void addrec_db(void);
103 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
107 if (random() % CULL_PROB == 0) {
108 tdb_delete(tdb, key);
112 #if TRAVERSE_MOD_PROB
113 if (random() % TRAVERSE_MOD_PROB == 0) {
118 #if TRAVERSE_ABORT_PROB
119 if (random() % TRAVERSE_ABORT_PROB == 0)
126 static void addrec_db(void)
132 klen = 1 + (rand() % KEYLEN);
133 dlen = 1 + (rand() % DATALEN);
138 key.dptr = (unsigned char *)k;
141 data.dptr = (unsigned char *)d;
145 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
152 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
153 if (tdb_transaction_start(db) != 0) {
154 fatal("tdb_transaction_start failed");
159 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
160 if (random() % TRANSACTION_PREPARE_PROB == 0) {
161 if (tdb_transaction_prepare_commit(db) != 0) {
162 fatal("tdb_transaction_prepare_commit failed");
165 if (tdb_transaction_commit(db) != 0) {
166 fatal("tdb_transaction_commit failed");
172 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
173 if (tdb_transaction_cancel(db) != 0) {
174 fatal("tdb_transaction_cancel failed");
182 if (random() % DELETE_PROB == 0) {
189 if (random() % STORE_PROB == 0) {
190 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
191 fatal("tdb_store failed");
198 if (random() % APPEND_PROB == 0) {
199 if (tdb_append(db, key, data) != 0) {
200 fatal("tdb_append failed");
207 if (random() % LOCKSTORE_PROB == 0) {
208 tdb_chainlock(db, key);
209 data = tdb_fetch(db, key);
210 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
211 fatal("tdb_store failed");
213 if (data.dptr) free(data.dptr);
214 tdb_chainunlock(db, key);
220 /* FIXME: recursive traverses break transactions? */
221 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
223 tdb_traverse(db, modify_traverse, NULL);
229 #if TRAVERSE_READ_PROB
230 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
232 tdb_traverse_read(db, NULL, NULL);
238 data = tdb_fetch(db, key);
239 if (data.dptr) free(data.dptr);
246 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
249 tdb_delete(tdb, key);
253 static void usage(void)
255 printf("Usage: tdbtorture"
259 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n");
263 static void send_count_and_suicide(int sig)
265 /* This ensures our successor can continue where we left off. */
266 write(count_pipe, &loopnum, sizeof(loopnum));
267 /* This gives a unique signature. */
268 kill(getpid(), SIGUSR2);
271 static int run_child(int i, int seed, unsigned num_loops, unsigned start)
273 struct sigaction act = { .sa_sigaction = segv_handler,
274 .sa_flags = SA_SIGINFO };
275 sigaction(11, &act, NULL);
277 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600,
280 fatal("db open failed");
285 printf("pid %i\n", getpid());
294 /* Set global, then we're ready to handle being killed. */
296 signal(SIGUSR1, send_count_and_suicide);
298 for (;loopnum<num_loops && error_count == 0;loopnum++) {
302 if (error_count == 0) {
303 tdb_traverse_read(db, NULL, NULL);
305 if (always_transaction) {
306 while (in_transaction) {
307 tdb_transaction_cancel(db);
310 if (tdb_transaction_start(db) != 0)
311 fatal("tdb_transaction_start failed");
314 tdb_traverse(db, traverse_fn, NULL);
315 tdb_traverse(db, traverse_fn, NULL);
318 if (always_transaction) {
319 if (tdb_transaction_commit(db) != 0)
320 fatal("tdb_transaction_commit failed");
327 return (error_count < 100 ? error_count : 100);
330 int main(int argc, char * const *argv)
333 int num_loops = 5000;
341 log_attr.log.log_fn = tdb_log;
343 while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) {
346 num_procs = strtol(optarg, NULL, 0);
349 num_loops = strtol(optarg, NULL, 0);
352 seed = strtol(optarg, NULL, 0);
356 always_transaction = 1;
358 fprintf(stderr, "Transactions not supported\n");
370 unlink("torture.tdb");
373 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
376 if (num_procs == 1 && !kill_random) {
377 /* Don't fork for this case, makes debugging easier. */
378 error_count = run_child(0, seed, num_loops, 0);
382 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
383 done = (int *)calloc(sizeof(int), num_procs);
385 if (pipe(pfds) != 0) {
386 perror("Creating pipe");
389 count_pipe = pfds[1];
391 for (i=0;i<num_procs;i++) {
392 if ((pids[i]=fork()) == 0) {
395 printf("testing with %d processes, %d loops, seed=%d%s\n",
396 num_procs, num_loops, seed,
398 always_transaction ? " (all within transactions)" : ""
404 exit(run_child(i, seed, num_loops, 0));
412 if (error_count != 0) {
413 /* try and stop the test on any failure */
414 for (j=0;j<num_procs;j++) {
416 kill(pids[j], SIGTERM);
421 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
425 /* Sleep for 1/10 second. */
427 ts.tv_nsec = 100000000;
428 nanosleep(&ts, NULL);
431 kill(pids[random() % num_procs], SIGUSR1);
436 perror("failed to wait for child\n");
440 for (j=0;j<num_procs;j++) {
441 if (pids[j] == pid) break;
443 if (j == num_procs) {
444 printf("unknown child %d exited!?\n", (int)pid);
447 if (WIFSIGNALED(status)) {
448 if (WTERMSIG(status) == SIGUSR2
449 || WTERMSIG(status) == SIGUSR1) {
450 /* SIGUSR2 means they wrote to pipe. */
451 if (WTERMSIG(status) == SIGUSR2) {
452 read(pfds[0], &done[j],
457 exit(run_child(j, seed, num_loops,
459 printf("Restarting child %i for %u-%u\n",
460 j, done[j], num_loops);
463 printf("child %d exited with signal %d\n",
464 (int)pid, WTERMSIG(status));
467 if (WEXITSTATUS(status) != 0) {
468 printf("child %d exited with status %d\n",
469 (int)pid, WEXITSTATUS(status));
473 memmove(&pids[j], &pids[j+1],
474 (num_procs - j - 1)*sizeof(pids[0]));
481 if (error_count == 0) {
482 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
485 fatal("db open failed");
487 if (tdb_check(db, NULL, NULL) == -1) {
488 printf("db check failed");