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;
44 static union tdb_attribute seed_attr;
46 #ifdef PRINTF_ATTRIBUTE
47 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
49 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...)
53 if (level != TDB_DEBUG_TRACE)
57 vfprintf(stdout, format, ap);
61 if (level != TDB_DEBUG_TRACE) {
63 signal(SIGUSR1, SIG_IGN);
64 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
71 #include "../private.h"
73 static void segv_handler(int signal, siginfo_t *info, void *p)
77 sprintf(string, "%u: death at %p (map_ptr %p, map_size %llu)\n",
78 getpid(), info->si_addr, db->map_ptr, db->map_size);
79 write(2, string, strlen(string));
84 static void fatal(const char *why)
90 static char *randbuf(int len)
94 buf = (char *)malloc(len+1);
97 buf[i] = 'a' + (rand() % 26);
103 static void addrec_db(void);
104 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
108 if (random() % CULL_PROB == 0) {
109 tdb_delete(tdb, key);
113 #if TRAVERSE_MOD_PROB
114 if (random() % TRAVERSE_MOD_PROB == 0) {
119 #if TRAVERSE_ABORT_PROB
120 if (random() % TRAVERSE_ABORT_PROB == 0)
127 static void addrec_db(void)
133 klen = 1 + (rand() % KEYLEN);
134 dlen = 1 + (rand() % DATALEN);
139 key.dptr = (unsigned char *)k;
142 data.dptr = (unsigned char *)d;
146 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
153 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
154 if (tdb_transaction_start(db) != 0) {
155 fatal("tdb_transaction_start failed");
160 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
161 if (random() % TRANSACTION_PREPARE_PROB == 0) {
162 if (tdb_transaction_prepare_commit(db) != 0) {
163 fatal("tdb_transaction_prepare_commit failed");
166 if (tdb_transaction_commit(db) != 0) {
167 fatal("tdb_transaction_commit failed");
173 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
174 if (tdb_transaction_cancel(db) != 0) {
175 fatal("tdb_transaction_cancel failed");
183 if (random() % DELETE_PROB == 0) {
190 if (random() % STORE_PROB == 0) {
191 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
192 fatal("tdb_store failed");
199 if (random() % APPEND_PROB == 0) {
200 if (tdb_append(db, key, data) != 0) {
201 fatal("tdb_append failed");
208 if (random() % LOCKSTORE_PROB == 0) {
209 tdb_chainlock(db, key);
210 data = tdb_fetch(db, key);
211 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
212 fatal("tdb_store failed");
214 if (data.dptr) free(data.dptr);
215 tdb_chainunlock(db, key);
221 /* FIXME: recursive traverses break transactions? */
222 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
224 tdb_traverse(db, modify_traverse, NULL);
230 #if TRAVERSE_READ_PROB
231 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
233 tdb_traverse_read(db, NULL, NULL);
239 data = tdb_fetch(db, key);
240 if (data.dptr) free(data.dptr);
247 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
250 tdb_delete(tdb, key);
254 static void usage(void)
256 printf("Usage: tdbtorture"
260 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n");
264 static void send_count_and_suicide(int sig)
266 /* This ensures our successor can continue where we left off. */
267 write(count_pipe, &loopnum, sizeof(loopnum));
268 /* This gives a unique signature. */
269 kill(getpid(), SIGUSR2);
272 static int run_child(int i, int seed, unsigned num_loops, unsigned start)
274 struct sigaction act = { .sa_sigaction = segv_handler,
275 .sa_flags = SA_SIGINFO };
276 sigaction(11, &act, NULL);
278 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600,
281 fatal("db open failed");
286 printf("pid %i\n", getpid());
295 /* Set global, then we're ready to handle being killed. */
297 signal(SIGUSR1, send_count_and_suicide);
299 for (;loopnum<num_loops && error_count == 0;loopnum++) {
303 if (error_count == 0) {
304 tdb_traverse_read(db, NULL, NULL);
306 if (always_transaction) {
307 while (in_transaction) {
308 tdb_transaction_cancel(db);
311 if (tdb_transaction_start(db) != 0)
312 fatal("tdb_transaction_start failed");
315 tdb_traverse(db, traverse_fn, NULL);
316 tdb_traverse(db, traverse_fn, NULL);
319 if (always_transaction) {
320 if (tdb_transaction_commit(db) != 0)
321 fatal("tdb_transaction_commit failed");
328 return (error_count < 100 ? error_count : 100);
331 int main(int argc, char * const *argv)
334 int num_loops = 5000;
342 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
343 log_attr.base.next = &seed_attr;
344 log_attr.log.log_fn = tdb_log;
345 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
347 while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) {
350 num_procs = strtol(optarg, NULL, 0);
353 num_loops = strtol(optarg, NULL, 0);
356 seed = strtol(optarg, NULL, 0);
360 always_transaction = 1;
362 fprintf(stderr, "Transactions not supported\n");
374 unlink("torture.tdb");
377 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
379 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
381 if (num_procs == 1 && !kill_random) {
382 /* Don't fork for this case, makes debugging easier. */
383 error_count = run_child(0, seed, num_loops, 0);
387 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
388 done = (int *)calloc(sizeof(int), num_procs);
390 if (pipe(pfds) != 0) {
391 perror("Creating pipe");
394 count_pipe = pfds[1];
396 for (i=0;i<num_procs;i++) {
397 if ((pids[i]=fork()) == 0) {
400 printf("testing with %d processes, %d loops, seed=%d%s\n",
401 num_procs, num_loops, seed,
403 always_transaction ? " (all within transactions)" : ""
409 exit(run_child(i, seed, num_loops, 0));
417 if (error_count != 0) {
418 /* try and stop the test on any failure */
419 for (j=0;j<num_procs;j++) {
421 kill(pids[j], SIGTERM);
426 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
430 /* Sleep for 1/10 second. */
432 ts.tv_nsec = 100000000;
433 nanosleep(&ts, NULL);
436 kill(pids[random() % num_procs], SIGUSR1);
441 perror("failed to wait for child\n");
445 for (j=0;j<num_procs;j++) {
446 if (pids[j] == pid) break;
448 if (j == num_procs) {
449 printf("unknown child %d exited!?\n", (int)pid);
452 if (WIFSIGNALED(status)) {
453 if (WTERMSIG(status) == SIGUSR2
454 || WTERMSIG(status) == SIGUSR1) {
455 /* SIGUSR2 means they wrote to pipe. */
456 if (WTERMSIG(status) == SIGUSR2) {
457 read(pfds[0], &done[j],
462 exit(run_child(j, seed, num_loops,
464 printf("Restarting child %i for %u-%u\n",
465 j, done[j], num_loops);
468 printf("child %d exited with signal %d\n",
469 (int)pid, WTERMSIG(status));
472 if (WEXITSTATUS(status) != 0) {
473 printf("child %d exited with status %d\n",
474 (int)pid, WEXITSTATUS(status));
478 memmove(&pids[j], &pids[j+1],
479 (num_procs - j - 1)*sizeof(pids[0]));
486 if (error_count == 0) {
487 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
490 fatal("db open failed");
492 if (tdb_check(db, NULL, NULL) == -1) {
493 printf("db check failed");