1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
18 #include <sys/types.h>
24 /* Currently we default to creating a tdb1. This will change! */
25 #define TDB2_IS_DEFAULT false
27 //#define REOPEN_PROB 30
31 #define TRANSACTION_PROB 10
32 #define TRANSACTION_PREPARE_PROB 2
33 #define LOCKSTORE_PROB 5
34 #define TRAVERSE_PROB 20
35 #define TRAVERSE_MOD_PROB 100
36 #define TRAVERSE_ABORT_PROB 500
41 static struct tdb_context *db;
42 static int in_transaction;
43 static int in_traverse;
44 static int error_count;
46 static int always_transaction = 0;
49 static int count_pipe;
50 static union tdb_attribute log_attr;
51 static union tdb_attribute seed_attr;
53 static void tdb_log(struct tdb_context *tdb,
54 enum tdb_log_level level,
59 printf("tdb:%s:%s:%s\n",
60 tdb_name(tdb), tdb_errorstr(ecode), message);
65 signal(SIGUSR1, SIG_IGN);
66 sprintf(str,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
72 #include "../private.h"
74 static void segv_handler(int sig, siginfo_t *info, void *p)
78 sprintf(string, "%u: death at %p (map_ptr %p, map_size %zu)\n",
79 getpid(), info->si_addr, db->file->map_ptr,
80 (size_t)db->file->map_size);
81 if (write(2, string, strlen(string)) > 0)
86 static void fatal(struct tdb_context *tdb, const char *why)
88 fprintf(stderr, "%u:%s:%s\n", getpid(), why,
89 tdb ? tdb_errorstr(tdb_error(tdb)) : "(no tdb)");
93 static char *randbuf(int len)
97 buf = (char *)malloc(len+1);
100 buf[i] = 'a' + (rand() % 26);
106 static void addrec_db(void);
107 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
111 if (random() % CULL_PROB == 0) {
112 tdb_delete(tdb, key);
116 #if TRAVERSE_MOD_PROB
117 if (random() % TRAVERSE_MOD_PROB == 0) {
122 #if TRAVERSE_ABORT_PROB
123 if (random() % TRAVERSE_ABORT_PROB == 0)
130 static void addrec_db(void)
136 klen = 1 + (rand() % KEYLEN);
137 dlen = 1 + (rand() % DATALEN);
142 key.dptr = (unsigned char *)k;
145 data.dptr = (unsigned char *)d;
149 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
156 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
157 if (tdb_transaction_start(db) != 0) {
158 fatal(db, "tdb_transaction_start failed");
163 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
164 if (random() % TRANSACTION_PREPARE_PROB == 0) {
165 if (tdb_transaction_prepare_commit(db) != 0) {
166 fatal(db, "tdb_transaction_prepare_commit failed");
169 if (tdb_transaction_commit(db) != 0) {
170 fatal(db, "tdb_transaction_commit failed");
176 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
177 tdb_transaction_cancel(db);
184 if (random() % DELETE_PROB == 0) {
191 if (random() % STORE_PROB == 0) {
192 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
193 fatal(db, "tdb_store failed");
200 if (random() % APPEND_PROB == 0) {
201 if (tdb_append(db, key, data) != 0) {
202 fatal(db, "tdb_append failed");
209 if (random() % LOCKSTORE_PROB == 0) {
210 tdb_chainlock(db, key);
211 if (tdb_fetch(db, key, &data) != TDB_SUCCESS) {
215 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
216 fatal(db, "tdb_store failed");
218 if (data.dptr) free(data.dptr);
219 tdb_chainunlock(db, key);
225 /* FIXME: recursive traverses break transactions? */
226 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
228 tdb_traverse(db, modify_traverse, NULL);
234 if (tdb_fetch(db, key, &data) == TDB_SUCCESS)
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] [-S]\n");
259 static void send_count_and_suicide(int sig)
261 /* This ensures our successor can continue where we left off. */
262 if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
264 /* This gives a unique signature. */
265 kill(getpid(), SIGUSR2);
268 static int run_child(const char *filename, int i, int seed, unsigned num_loops,
269 unsigned start, int tdb_flags)
271 struct sigaction act = { .sa_sigaction = segv_handler,
272 .sa_flags = SA_SIGINFO };
273 sigaction(11, &act, NULL);
275 db = tdb_open(filename, tdb_flags, O_RDWR | O_CREAT, 0600,
278 fatal(NULL, "db open failed");
283 printf("pid %i\n", getpid());
292 /* Set global, then we're ready to handle being killed. */
294 signal(SIGUSR1, send_count_and_suicide);
296 for (;loopnum<num_loops && error_count == 0;loopnum++) {
300 if (error_count == 0) {
301 tdb_traverse(db, NULL, NULL);
303 if (always_transaction) {
304 while (in_transaction) {
305 tdb_transaction_cancel(db);
308 if (tdb_transaction_start(db) != 0)
309 fatal(db, "tdb_transaction_start failed");
312 tdb_traverse(db, traverse_fn, NULL);
313 tdb_traverse(db, traverse_fn, NULL);
316 if (always_transaction) {
317 if (tdb_transaction_commit(db) != 0)
318 fatal(db, "tdb_transaction_commit failed");
325 return (error_count < 100 ? error_count : 100);
328 static char *test_path(const char *filename)
330 const char *prefix = getenv("TEST_DATA_PREFIX");
336 ret = asprintf(&path, "%s/%s", prefix, filename);
343 return strdup(filename);
346 int main(int argc, char * const *argv)
349 int num_loops = 5000;
356 int tdb_flags = TDB_DEFAULT;
357 bool tdb2 = TDB2_IS_DEFAULT;
360 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
361 log_attr.base.next = &seed_attr;
362 log_attr.log.fn = tdb_log;
363 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
364 seed_attr.base.next = NULL;
366 while ((c = getopt(argc, argv, "n:l:s:thkS12")) != -1) {
369 num_procs = strtol(optarg, NULL, 0);
372 num_loops = strtol(optarg, NULL, 0);
375 seed = strtol(optarg, NULL, 0);
378 tdb_flags = TDB_NOSYNC;
382 always_transaction = 1;
384 fprintf(stderr, "Transactions not supported\n");
403 tdb_flags |= TDB_VERSION1;
404 /* TDB1 tdbs don't use seed. */
405 log_attr.base.next = NULL;
408 test_tdb = test_path("torture.tdb");
413 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
415 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
417 if (num_procs == 1 && !kill_random) {
418 /* Don't fork for this case, makes debugging easier. */
419 error_count = run_child(test_tdb, 0, seed, num_loops, 0,
424 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
425 done = (int *)calloc(sizeof(int), num_procs);
427 if (pipe(pfds) != 0) {
428 perror("Creating pipe");
431 count_pipe = pfds[1];
433 for (i=0;i<num_procs;i++) {
434 if ((pids[i]=fork()) == 0) {
437 printf("testing with %d processes, %d loops, seed=%d%s\n",
438 num_procs, num_loops, seed,
440 always_transaction ? " (all within transactions)" : ""
446 exit(run_child(test_tdb, i, seed, num_loops, 0,
455 if (error_count != 0) {
456 /* try and stop the test on any failure */
457 for (j=0;j<num_procs;j++) {
459 kill(pids[j], SIGTERM);
464 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
468 /* Sleep for 1/10 second. */
470 ts.tv_nsec = 100000000;
471 nanosleep(&ts, NULL);
474 kill(pids[random() % num_procs], SIGUSR1);
479 perror("failed to wait for child\n");
483 for (j=0;j<num_procs;j++) {
484 if (pids[j] == pid) break;
486 if (j == num_procs) {
487 printf("unknown child %d exited!?\n", (int)pid);
490 if (WIFSIGNALED(status)) {
491 if (WTERMSIG(status) == SIGUSR2
492 || WTERMSIG(status) == SIGUSR1) {
493 /* SIGUSR2 means they wrote to pipe. */
494 if (WTERMSIG(status) == SIGUSR2) {
495 if (read(pfds[0], &done[j],
499 "Short read from child?");
503 exit(run_child(test_tdb, j, seed,
506 printf("Restarting child %i for %u-%u\n",
507 j, done[j], num_loops);
510 printf("child %d exited with signal %d\n",
511 (int)pid, WTERMSIG(status));
514 if (WEXITSTATUS(status) != 0) {
515 printf("child %d exited with status %d\n",
516 (int)pid, WEXITSTATUS(status));
520 memmove(&pids[j], &pids[j+1],
521 (num_procs - j - 1)*sizeof(pids[0]));
528 if (error_count == 0) {
529 db = tdb_open(test_tdb, TDB_DEFAULT, O_RDWR | O_CREAT,
532 fatal(db, "db open failed");
535 if (tdb_check(db, NULL, NULL) != 0) {
536 fatal(db, "db check failed");