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_MOD_PROB 100
28 #define TRAVERSE_ABORT_PROB 500
33 static struct tdb_context *db;
34 static int in_transaction;
35 static int in_traverse;
36 static int error_count;
38 static int always_transaction = 0;
41 static int count_pipe;
42 static union tdb_attribute log_attr;
43 static union tdb_attribute seed_attr;
45 static void tdb_log(struct tdb_context *tdb, enum tdb_log_level level,
46 void *private, const char *message)
48 fputs(message, stdout);
53 signal(SIGUSR1, SIG_IGN);
54 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
61 #include "../private.h"
63 static void segv_handler(int signal, siginfo_t *info, void *p)
67 sprintf(string, "%u: death at %p (map_ptr %p, map_size %llu)\n",
68 getpid(), info->si_addr, db->map_ptr, db->map_size);
69 write(2, string, strlen(string));
74 static void fatal(const char *why)
80 static char *randbuf(int len)
84 buf = (char *)malloc(len+1);
87 buf[i] = 'a' + (rand() % 26);
93 static void addrec_db(void);
94 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
98 if (random() % CULL_PROB == 0) {
103 #if TRAVERSE_MOD_PROB
104 if (random() % TRAVERSE_MOD_PROB == 0) {
109 #if TRAVERSE_ABORT_PROB
110 if (random() % TRAVERSE_ABORT_PROB == 0)
117 static void addrec_db(void)
123 klen = 1 + (rand() % KEYLEN);
124 dlen = 1 + (rand() % DATALEN);
129 key.dptr = (unsigned char *)k;
132 data.dptr = (unsigned char *)d;
136 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
143 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
144 if (tdb_transaction_start(db) != 0) {
145 fatal("tdb_transaction_start failed");
150 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
151 if (random() % TRANSACTION_PREPARE_PROB == 0) {
152 if (tdb_transaction_prepare_commit(db) != 0) {
153 fatal("tdb_transaction_prepare_commit failed");
156 if (tdb_transaction_commit(db) != 0) {
157 fatal("tdb_transaction_commit failed");
163 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
164 tdb_transaction_cancel(db);
171 if (random() % DELETE_PROB == 0) {
178 if (random() % STORE_PROB == 0) {
179 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
180 fatal("tdb_store failed");
187 if (random() % APPEND_PROB == 0) {
188 if (tdb_append(db, key, data) != 0) {
189 fatal("tdb_append failed");
196 if (random() % LOCKSTORE_PROB == 0) {
197 tdb_chainlock(db, key);
198 tdb_fetch(db, key, &data);
199 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
200 fatal("tdb_store failed");
202 if (data.dptr) free(data.dptr);
203 tdb_chainunlock(db, key);
209 /* FIXME: recursive traverses break transactions? */
210 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
212 tdb_traverse(db, modify_traverse, NULL);
218 if (tdb_fetch(db, key, &data) == TDB_SUCCESS)
226 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
229 tdb_delete(tdb, key);
233 static void usage(void)
235 printf("Usage: tdbtorture"
239 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n");
243 static void send_count_and_suicide(int sig)
245 /* This ensures our successor can continue where we left off. */
246 write(count_pipe, &loopnum, sizeof(loopnum));
247 /* This gives a unique signature. */
248 kill(getpid(), SIGUSR2);
251 static int run_child(int i, int seed, unsigned num_loops, unsigned start)
253 struct sigaction act = { .sa_sigaction = segv_handler,
254 .sa_flags = SA_SIGINFO };
255 sigaction(11, &act, NULL);
257 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600,
260 fatal("db open failed");
265 printf("pid %i\n", getpid());
274 /* Set global, then we're ready to handle being killed. */
276 signal(SIGUSR1, send_count_and_suicide);
278 for (;loopnum<num_loops && error_count == 0;loopnum++) {
282 if (error_count == 0) {
283 tdb_traverse(db, NULL, NULL);
285 if (always_transaction) {
286 while (in_transaction) {
287 tdb_transaction_cancel(db);
290 if (tdb_transaction_start(db) != 0)
291 fatal("tdb_transaction_start failed");
294 tdb_traverse(db, traverse_fn, NULL);
295 tdb_traverse(db, traverse_fn, NULL);
298 if (always_transaction) {
299 if (tdb_transaction_commit(db) != 0)
300 fatal("tdb_transaction_commit failed");
307 return (error_count < 100 ? error_count : 100);
310 int main(int argc, char * const *argv)
313 int num_loops = 5000;
321 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
322 log_attr.base.next = &seed_attr;
323 log_attr.log.log_fn = tdb_log;
324 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
326 while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) {
329 num_procs = strtol(optarg, NULL, 0);
332 num_loops = strtol(optarg, NULL, 0);
335 seed = strtol(optarg, NULL, 0);
339 always_transaction = 1;
341 fprintf(stderr, "Transactions not supported\n");
353 unlink("torture.tdb");
356 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
358 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
360 if (num_procs == 1 && !kill_random) {
361 /* Don't fork for this case, makes debugging easier. */
362 error_count = run_child(0, seed, num_loops, 0);
366 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
367 done = (int *)calloc(sizeof(int), num_procs);
369 if (pipe(pfds) != 0) {
370 perror("Creating pipe");
373 count_pipe = pfds[1];
375 for (i=0;i<num_procs;i++) {
376 if ((pids[i]=fork()) == 0) {
379 printf("testing with %d processes, %d loops, seed=%d%s\n",
380 num_procs, num_loops, seed,
382 always_transaction ? " (all within transactions)" : ""
388 exit(run_child(i, seed, num_loops, 0));
396 if (error_count != 0) {
397 /* try and stop the test on any failure */
398 for (j=0;j<num_procs;j++) {
400 kill(pids[j], SIGTERM);
405 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
409 /* Sleep for 1/10 second. */
411 ts.tv_nsec = 100000000;
412 nanosleep(&ts, NULL);
415 kill(pids[random() % num_procs], SIGUSR1);
420 perror("failed to wait for child\n");
424 for (j=0;j<num_procs;j++) {
425 if (pids[j] == pid) break;
427 if (j == num_procs) {
428 printf("unknown child %d exited!?\n", (int)pid);
431 if (WIFSIGNALED(status)) {
432 if (WTERMSIG(status) == SIGUSR2
433 || WTERMSIG(status) == SIGUSR1) {
434 /* SIGUSR2 means they wrote to pipe. */
435 if (WTERMSIG(status) == SIGUSR2) {
436 read(pfds[0], &done[j],
441 exit(run_child(j, seed, num_loops,
443 printf("Restarting child %i for %u-%u\n",
444 j, done[j], num_loops);
447 printf("child %d exited with signal %d\n",
448 (int)pid, WTERMSIG(status));
451 if (WEXITSTATUS(status) != 0) {
452 printf("child %d exited with status %d\n",
453 (int)pid, WEXITSTATUS(status));
457 memmove(&pids[j], &pids[j+1],
458 (num_procs - j - 1)*sizeof(pids[0]));
465 if (error_count == 0) {
466 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
469 fatal("db open failed");
471 if (tdb_check(db, NULL, NULL) == -1) {
472 printf("db check failed");