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;
47 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...) PRINTF_FMT(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 tdb_transaction_cancel(db);
181 if (random() % DELETE_PROB == 0) {
188 if (random() % STORE_PROB == 0) {
189 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
190 fatal("tdb_store failed");
197 if (random() % APPEND_PROB == 0) {
198 if (tdb_append(db, key, data) != 0) {
199 fatal("tdb_append failed");
206 if (random() % LOCKSTORE_PROB == 0) {
207 tdb_chainlock(db, key);
208 data = tdb_fetch(db, key);
209 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
210 fatal("tdb_store failed");
212 if (data.dptr) free(data.dptr);
213 tdb_chainunlock(db, key);
219 /* FIXME: recursive traverses break transactions? */
220 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
222 tdb_traverse(db, modify_traverse, NULL);
228 #if TRAVERSE_READ_PROB
229 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
231 tdb_traverse_read(db, NULL, NULL);
237 data = tdb_fetch(db, key);
238 if (data.dptr) free(data.dptr);
245 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
248 tdb_delete(tdb, key);
252 static void usage(void)
254 printf("Usage: tdbtorture"
258 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n");
262 static void send_count_and_suicide(int sig)
264 /* This ensures our successor can continue where we left off. */
265 write(count_pipe, &loopnum, sizeof(loopnum));
266 /* This gives a unique signature. */
267 kill(getpid(), SIGUSR2);
270 static int run_child(int i, int seed, unsigned num_loops, unsigned start)
272 struct sigaction act = { .sa_sigaction = segv_handler,
273 .sa_flags = SA_SIGINFO };
274 sigaction(11, &act, NULL);
276 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600,
279 fatal("db open failed");
284 printf("pid %i\n", getpid());
293 /* Set global, then we're ready to handle being killed. */
295 signal(SIGUSR1, send_count_and_suicide);
297 for (;loopnum<num_loops && error_count == 0;loopnum++) {
301 if (error_count == 0) {
302 tdb_traverse_read(db, NULL, NULL);
304 if (always_transaction) {
305 while (in_transaction) {
306 tdb_transaction_cancel(db);
309 if (tdb_transaction_start(db) != 0)
310 fatal("tdb_transaction_start failed");
313 tdb_traverse(db, traverse_fn, NULL);
314 tdb_traverse(db, traverse_fn, NULL);
317 if (always_transaction) {
318 if (tdb_transaction_commit(db) != 0)
319 fatal("tdb_transaction_commit failed");
326 return (error_count < 100 ? error_count : 100);
329 int main(int argc, char * const *argv)
332 int num_loops = 5000;
340 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
341 log_attr.base.next = &seed_attr;
342 log_attr.log.log_fn = tdb_log;
343 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
345 while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) {
348 num_procs = strtol(optarg, NULL, 0);
351 num_loops = strtol(optarg, NULL, 0);
354 seed = strtol(optarg, NULL, 0);
358 always_transaction = 1;
360 fprintf(stderr, "Transactions not supported\n");
372 unlink("torture.tdb");
375 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
377 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
379 if (num_procs == 1 && !kill_random) {
380 /* Don't fork for this case, makes debugging easier. */
381 error_count = run_child(0, seed, num_loops, 0);
385 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
386 done = (int *)calloc(sizeof(int), num_procs);
388 if (pipe(pfds) != 0) {
389 perror("Creating pipe");
392 count_pipe = pfds[1];
394 for (i=0;i<num_procs;i++) {
395 if ((pids[i]=fork()) == 0) {
398 printf("testing with %d processes, %d loops, seed=%d%s\n",
399 num_procs, num_loops, seed,
401 always_transaction ? " (all within transactions)" : ""
407 exit(run_child(i, seed, num_loops, 0));
415 if (error_count != 0) {
416 /* try and stop the test on any failure */
417 for (j=0;j<num_procs;j++) {
419 kill(pids[j], SIGTERM);
424 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
428 /* Sleep for 1/10 second. */
430 ts.tv_nsec = 100000000;
431 nanosleep(&ts, NULL);
434 kill(pids[random() % num_procs], SIGUSR1);
439 perror("failed to wait for child\n");
443 for (j=0;j<num_procs;j++) {
444 if (pids[j] == pid) break;
446 if (j == num_procs) {
447 printf("unknown child %d exited!?\n", (int)pid);
450 if (WIFSIGNALED(status)) {
451 if (WTERMSIG(status) == SIGUSR2
452 || WTERMSIG(status) == SIGUSR1) {
453 /* SIGUSR2 means they wrote to pipe. */
454 if (WTERMSIG(status) == SIGUSR2) {
455 read(pfds[0], &done[j],
460 exit(run_child(j, seed, num_loops,
462 printf("Restarting child %i for %u-%u\n",
463 j, done[j], num_loops);
466 printf("child %d exited with signal %d\n",
467 (int)pid, WTERMSIG(status));
470 if (WEXITSTATUS(status) != 0) {
471 printf("child %d exited with status %d\n",
472 (int)pid, WEXITSTATUS(status));
476 memmove(&pids[j], &pids[j+1],
477 (num_procs - j - 1)*sizeof(pids[0]));
484 if (error_count == 0) {
485 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
488 fatal("db open failed");
490 if (tdb_check(db, NULL, NULL) == -1) {
491 printf("db check failed");