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, ...)
54 vfprintf(stdout, format, ap);
60 signal(SIGUSR1, SIG_IGN);
61 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
68 #include "../private.h"
70 static void segv_handler(int signal, siginfo_t *info, void *p)
74 sprintf(string, "%u: death at %p (map_ptr %p, map_size %llu)\n",
75 getpid(), info->si_addr, db->map_ptr, db->map_size);
76 write(2, string, strlen(string));
81 static void fatal(const char *why)
87 static char *randbuf(int len)
91 buf = (char *)malloc(len+1);
94 buf[i] = 'a' + (rand() % 26);
100 static void addrec_db(void);
101 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
105 if (random() % CULL_PROB == 0) {
106 tdb_delete(tdb, key);
110 #if TRAVERSE_MOD_PROB
111 if (random() % TRAVERSE_MOD_PROB == 0) {
116 #if TRAVERSE_ABORT_PROB
117 if (random() % TRAVERSE_ABORT_PROB == 0)
124 static void addrec_db(void)
130 klen = 1 + (rand() % KEYLEN);
131 dlen = 1 + (rand() % DATALEN);
136 key.dptr = (unsigned char *)k;
139 data.dptr = (unsigned char *)d;
143 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
150 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
151 if (tdb_transaction_start(db) != 0) {
152 fatal("tdb_transaction_start failed");
157 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
158 if (random() % TRANSACTION_PREPARE_PROB == 0) {
159 if (tdb_transaction_prepare_commit(db) != 0) {
160 fatal("tdb_transaction_prepare_commit failed");
163 if (tdb_transaction_commit(db) != 0) {
164 fatal("tdb_transaction_commit failed");
170 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
171 tdb_transaction_cancel(db);
178 if (random() % DELETE_PROB == 0) {
185 if (random() % STORE_PROB == 0) {
186 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
187 fatal("tdb_store failed");
194 if (random() % APPEND_PROB == 0) {
195 if (tdb_append(db, key, data) != 0) {
196 fatal("tdb_append failed");
203 if (random() % LOCKSTORE_PROB == 0) {
204 tdb_chainlock(db, key);
205 data = tdb_fetch(db, key);
206 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
207 fatal("tdb_store failed");
209 if (data.dptr) free(data.dptr);
210 tdb_chainunlock(db, key);
216 /* FIXME: recursive traverses break transactions? */
217 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
219 tdb_traverse(db, modify_traverse, NULL);
225 #if TRAVERSE_READ_PROB
226 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
228 tdb_traverse_read(db, NULL, NULL);
234 data = tdb_fetch(db, key);
235 if (data.dptr) free(data.dptr);
242 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
245 tdb_delete(tdb, key);
249 static void usage(void)
251 printf("Usage: tdbtorture"
255 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n");
259 static void send_count_and_suicide(int sig)
261 /* This ensures our successor can continue where we left off. */
262 write(count_pipe, &loopnum, sizeof(loopnum));
263 /* This gives a unique signature. */
264 kill(getpid(), SIGUSR2);
267 static int run_child(int i, int seed, unsigned num_loops, unsigned start)
269 struct sigaction act = { .sa_sigaction = segv_handler,
270 .sa_flags = SA_SIGINFO };
271 sigaction(11, &act, NULL);
273 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600,
276 fatal("db open failed");
281 printf("pid %i\n", getpid());
290 /* Set global, then we're ready to handle being killed. */
292 signal(SIGUSR1, send_count_and_suicide);
294 for (;loopnum<num_loops && error_count == 0;loopnum++) {
298 if (error_count == 0) {
299 tdb_traverse_read(db, NULL, NULL);
301 if (always_transaction) {
302 while (in_transaction) {
303 tdb_transaction_cancel(db);
306 if (tdb_transaction_start(db) != 0)
307 fatal("tdb_transaction_start failed");
310 tdb_traverse(db, traverse_fn, NULL);
311 tdb_traverse(db, traverse_fn, NULL);
314 if (always_transaction) {
315 if (tdb_transaction_commit(db) != 0)
316 fatal("tdb_transaction_commit failed");
323 return (error_count < 100 ? error_count : 100);
326 int main(int argc, char * const *argv)
329 int num_loops = 5000;
337 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
338 log_attr.base.next = &seed_attr;
339 log_attr.log.log_fn = tdb_log;
340 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
342 while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) {
345 num_procs = strtol(optarg, NULL, 0);
348 num_loops = strtol(optarg, NULL, 0);
351 seed = strtol(optarg, NULL, 0);
355 always_transaction = 1;
357 fprintf(stderr, "Transactions not supported\n");
369 unlink("torture.tdb");
372 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
374 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
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");