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 %zu)\n",
68 getpid(), info->si_addr, db->file->map_ptr,
69 (size_t)db->file->map_size);
70 if (write(2, string, strlen(string)) > 0)
75 static void fatal(const char *why)
81 static char *randbuf(int len)
85 buf = (char *)malloc(len+1);
88 buf[i] = 'a' + (rand() % 26);
94 static void addrec_db(void);
95 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
99 if (random() % CULL_PROB == 0) {
100 tdb_delete(tdb, key);
104 #if TRAVERSE_MOD_PROB
105 if (random() % TRAVERSE_MOD_PROB == 0) {
110 #if TRAVERSE_ABORT_PROB
111 if (random() % TRAVERSE_ABORT_PROB == 0)
118 static void addrec_db(void)
124 klen = 1 + (rand() % KEYLEN);
125 dlen = 1 + (rand() % DATALEN);
130 key.dptr = (unsigned char *)k;
133 data.dptr = (unsigned char *)d;
137 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
144 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
145 if (tdb_transaction_start(db) != 0) {
146 fatal("tdb_transaction_start failed");
151 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
152 if (random() % TRANSACTION_PREPARE_PROB == 0) {
153 if (tdb_transaction_prepare_commit(db) != 0) {
154 fatal("tdb_transaction_prepare_commit failed");
157 if (tdb_transaction_commit(db) != 0) {
158 fatal("tdb_transaction_commit failed");
164 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
165 tdb_transaction_cancel(db);
172 if (random() % DELETE_PROB == 0) {
179 if (random() % STORE_PROB == 0) {
180 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
181 fatal("tdb_store failed");
188 if (random() % APPEND_PROB == 0) {
189 if (tdb_append(db, key, data) != 0) {
190 fatal("tdb_append failed");
197 if (random() % LOCKSTORE_PROB == 0) {
198 tdb_chainlock(db, key);
199 tdb_fetch(db, key, &data);
200 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
201 fatal("tdb_store failed");
203 if (data.dptr) free(data.dptr);
204 tdb_chainunlock(db, key);
210 /* FIXME: recursive traverses break transactions? */
211 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
213 tdb_traverse(db, modify_traverse, NULL);
219 if (tdb_fetch(db, key, &data) == TDB_SUCCESS)
227 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
230 tdb_delete(tdb, key);
234 static void usage(void)
236 printf("Usage: tdbtorture"
240 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n");
244 static void send_count_and_suicide(int sig)
246 /* This ensures our successor can continue where we left off. */
247 if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
249 /* This gives a unique signature. */
250 kill(getpid(), SIGUSR2);
253 static int run_child(int i, int seed, unsigned num_loops, unsigned start)
255 struct sigaction act = { .sa_sigaction = segv_handler,
256 .sa_flags = SA_SIGINFO };
257 sigaction(11, &act, NULL);
259 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600,
262 fatal("db open failed");
267 printf("pid %i\n", getpid());
276 /* Set global, then we're ready to handle being killed. */
278 signal(SIGUSR1, send_count_and_suicide);
280 for (;loopnum<num_loops && error_count == 0;loopnum++) {
284 if (error_count == 0) {
285 tdb_traverse(db, NULL, NULL);
287 if (always_transaction) {
288 while (in_transaction) {
289 tdb_transaction_cancel(db);
292 if (tdb_transaction_start(db) != 0)
293 fatal("tdb_transaction_start failed");
296 tdb_traverse(db, traverse_fn, NULL);
297 tdb_traverse(db, traverse_fn, NULL);
300 if (always_transaction) {
301 if (tdb_transaction_commit(db) != 0)
302 fatal("tdb_transaction_commit failed");
309 return (error_count < 100 ? error_count : 100);
312 int main(int argc, char * const *argv)
315 int num_loops = 5000;
323 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
324 log_attr.base.next = &seed_attr;
325 log_attr.log.log_fn = tdb_log;
326 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
328 while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) {
331 num_procs = strtol(optarg, NULL, 0);
334 num_loops = strtol(optarg, NULL, 0);
337 seed = strtol(optarg, NULL, 0);
341 always_transaction = 1;
343 fprintf(stderr, "Transactions not supported\n");
355 unlink("torture.tdb");
358 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
360 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
362 if (num_procs == 1 && !kill_random) {
363 /* Don't fork for this case, makes debugging easier. */
364 error_count = run_child(0, seed, num_loops, 0);
368 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
369 done = (int *)calloc(sizeof(int), num_procs);
371 if (pipe(pfds) != 0) {
372 perror("Creating pipe");
375 count_pipe = pfds[1];
377 for (i=0;i<num_procs;i++) {
378 if ((pids[i]=fork()) == 0) {
381 printf("testing with %d processes, %d loops, seed=%d%s\n",
382 num_procs, num_loops, seed,
384 always_transaction ? " (all within transactions)" : ""
390 exit(run_child(i, seed, num_loops, 0));
398 if (error_count != 0) {
399 /* try and stop the test on any failure */
400 for (j=0;j<num_procs;j++) {
402 kill(pids[j], SIGTERM);
407 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
411 /* Sleep for 1/10 second. */
413 ts.tv_nsec = 100000000;
414 nanosleep(&ts, NULL);
417 kill(pids[random() % num_procs], SIGUSR1);
422 perror("failed to wait for child\n");
426 for (j=0;j<num_procs;j++) {
427 if (pids[j] == pid) break;
429 if (j == num_procs) {
430 printf("unknown child %d exited!?\n", (int)pid);
433 if (WIFSIGNALED(status)) {
434 if (WTERMSIG(status) == SIGUSR2
435 || WTERMSIG(status) == SIGUSR1) {
436 /* SIGUSR2 means they wrote to pipe. */
437 if (WTERMSIG(status) == SIGUSR2) {
438 if (read(pfds[0], &done[j],
442 "Short read from child?");
446 exit(run_child(j, seed, num_loops,
448 printf("Restarting child %i for %u-%u\n",
449 j, done[j], num_loops);
452 printf("child %d exited with signal %d\n",
453 (int)pid, WTERMSIG(status));
456 if (WEXITSTATUS(status) != 0) {
457 printf("child %d exited with status %d\n",
458 (int)pid, WEXITSTATUS(status));
462 memmove(&pids[j], &pids[j+1],
463 (num_procs - j - 1)*sizeof(pids[0]));
470 if (error_count == 0) {
471 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
474 fatal("db open failed");
476 if (tdb_check(db, NULL, NULL) == -1) {
477 printf("db check failed");