1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
14 #include <sys/types.h>
19 /* Currently we default to creating a tdb1. This will change! */
20 #define TDB2_IS_DEFAULT false
22 //#define REOPEN_PROB 30
26 #define TRANSACTION_PROB 10
27 #define TRANSACTION_PREPARE_PROB 2
28 #define LOCKSTORE_PROB 5
29 #define TRAVERSE_PROB 20
30 #define TRAVERSE_MOD_PROB 100
31 #define TRAVERSE_ABORT_PROB 500
36 static struct tdb_context *db;
37 static int in_transaction;
38 static int in_traverse;
39 static int error_count;
41 static int always_transaction = 0;
44 static int count_pipe;
45 static union tdb_attribute log_attr;
46 static union tdb_attribute seed_attr;
48 static void tdb_log(struct tdb_context *tdb,
49 enum tdb_log_level level,
54 printf("tdb:%s:%s:%s\n",
55 tdb_name(tdb), tdb_errorstr(ecode), message);
60 signal(SIGUSR1, SIG_IGN);
61 sprintf(str,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
67 #include "../private.h"
69 static void segv_handler(int sig, siginfo_t *info, void *p)
73 sprintf(string, "%u: death at %p (map_ptr %p, map_size %zu)\n",
74 getpid(), info->si_addr, db->file->map_ptr,
75 (size_t)db->file->map_size);
76 if (write(2, string, strlen(string)) > 0)
81 static void fatal(struct tdb_context *tdb, const char *why)
83 fprintf(stderr, "%u:%s:%s\n", getpid(), why,
84 tdb ? tdb_errorstr(tdb_error(tdb)) : "(no tdb)");
88 static char *randbuf(int len)
92 buf = (char *)malloc(len+1);
95 buf[i] = 'a' + (rand() % 26);
101 static void addrec_db(void);
102 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
106 if (random() % CULL_PROB == 0) {
107 tdb_delete(tdb, key);
111 #if TRAVERSE_MOD_PROB
112 if (random() % TRAVERSE_MOD_PROB == 0) {
117 #if TRAVERSE_ABORT_PROB
118 if (random() % TRAVERSE_ABORT_PROB == 0)
125 static void addrec_db(void)
131 klen = 1 + (rand() % KEYLEN);
132 dlen = 1 + (rand() % DATALEN);
137 key.dptr = (unsigned char *)k;
140 data.dptr = (unsigned char *)d;
144 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
151 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
152 if (tdb_transaction_start(db) != 0) {
153 fatal(db, "tdb_transaction_start failed");
158 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
159 if (random() % TRANSACTION_PREPARE_PROB == 0) {
160 if (tdb_transaction_prepare_commit(db) != 0) {
161 fatal(db, "tdb_transaction_prepare_commit failed");
164 if (tdb_transaction_commit(db) != 0) {
165 fatal(db, "tdb_transaction_commit failed");
171 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
172 tdb_transaction_cancel(db);
179 if (random() % DELETE_PROB == 0) {
186 if (random() % STORE_PROB == 0) {
187 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
188 fatal(db, "tdb_store failed");
195 if (random() % APPEND_PROB == 0) {
196 if (tdb_append(db, key, data) != 0) {
197 fatal(db, "tdb_append failed");
204 if (random() % LOCKSTORE_PROB == 0) {
205 tdb_chainlock(db, key);
206 if (tdb_fetch(db, key, &data) != TDB_SUCCESS) {
210 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
211 fatal(db, "tdb_store failed");
213 if (data.dptr) free(data.dptr);
214 tdb_chainunlock(db, key);
220 /* FIXME: recursive traverses break transactions? */
221 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
223 tdb_traverse(db, modify_traverse, NULL);
229 if (tdb_fetch(db, key, &data) == TDB_SUCCESS)
237 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
240 tdb_delete(tdb, key);
244 static void usage(void)
246 printf("Usage: tdbtorture"
250 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S]\n");
254 static void send_count_and_suicide(int sig)
256 /* This ensures our successor can continue where we left off. */
257 if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
259 /* This gives a unique signature. */
260 kill(getpid(), SIGUSR2);
263 static int run_child(const char *filename, int i, int seed, unsigned num_loops,
264 unsigned start, int tdb_flags)
266 struct sigaction act = { .sa_sigaction = segv_handler,
267 .sa_flags = SA_SIGINFO };
268 sigaction(11, &act, NULL);
270 db = tdb_open(filename, tdb_flags, O_RDWR | O_CREAT, 0600,
273 fatal(NULL, "db open failed");
278 printf("pid %i\n", getpid());
287 /* Set global, then we're ready to handle being killed. */
289 signal(SIGUSR1, send_count_and_suicide);
291 for (;loopnum<num_loops && error_count == 0;loopnum++) {
295 if (error_count == 0) {
296 tdb_traverse(db, NULL, NULL);
298 if (always_transaction) {
299 while (in_transaction) {
300 tdb_transaction_cancel(db);
303 if (tdb_transaction_start(db) != 0)
304 fatal(db, "tdb_transaction_start failed");
307 tdb_traverse(db, traverse_fn, NULL);
308 tdb_traverse(db, traverse_fn, NULL);
311 if (always_transaction) {
312 if (tdb_transaction_commit(db) != 0)
313 fatal(db, "tdb_transaction_commit failed");
320 return (error_count < 100 ? error_count : 100);
323 static char *test_path(const char *filename)
325 const char *prefix = getenv("TEST_DATA_PREFIX");
331 ret = asprintf(&path, "%s/%s", prefix, filename);
338 return strdup(filename);
341 int main(int argc, char * const *argv)
344 int num_loops = 5000;
351 int tdb_flags = TDB_DEFAULT;
352 bool tdb2 = TDB2_IS_DEFAULT;
355 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
356 log_attr.base.next = &seed_attr;
357 log_attr.log.fn = tdb_log;
358 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
359 seed_attr.base.next = NULL;
361 while ((c = getopt(argc, argv, "n:l:s:thkS12")) != -1) {
364 num_procs = strtol(optarg, NULL, 0);
367 num_loops = strtol(optarg, NULL, 0);
370 seed = strtol(optarg, NULL, 0);
373 tdb_flags = TDB_NOSYNC;
377 always_transaction = 1;
379 fprintf(stderr, "Transactions not supported\n");
398 tdb_flags |= TDB_VERSION1;
399 /* TDB1 tdbs don't use seed. */
400 log_attr.base.next = NULL;
403 test_tdb = test_path("torture.tdb");
408 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
410 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
412 if (num_procs == 1 && !kill_random) {
413 /* Don't fork for this case, makes debugging easier. */
414 error_count = run_child(test_tdb, 0, seed, num_loops, 0,
419 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
420 done = (int *)calloc(sizeof(int), num_procs);
422 if (pipe(pfds) != 0) {
423 perror("Creating pipe");
426 count_pipe = pfds[1];
428 for (i=0;i<num_procs;i++) {
429 if ((pids[i]=fork()) == 0) {
432 printf("testing with %d processes, %d loops, seed=%d%s\n",
433 num_procs, num_loops, seed,
435 always_transaction ? " (all within transactions)" : ""
441 exit(run_child(test_tdb, i, seed, num_loops, 0,
450 if (error_count != 0) {
451 /* try and stop the test on any failure */
452 for (j=0;j<num_procs;j++) {
454 kill(pids[j], SIGTERM);
459 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
463 /* Sleep for 1/10 second. */
465 ts.tv_nsec = 100000000;
466 nanosleep(&ts, NULL);
469 kill(pids[random() % num_procs], SIGUSR1);
474 perror("failed to wait for child\n");
478 for (j=0;j<num_procs;j++) {
479 if (pids[j] == pid) break;
481 if (j == num_procs) {
482 printf("unknown child %d exited!?\n", (int)pid);
485 if (WIFSIGNALED(status)) {
486 if (WTERMSIG(status) == SIGUSR2
487 || WTERMSIG(status) == SIGUSR1) {
488 /* SIGUSR2 means they wrote to pipe. */
489 if (WTERMSIG(status) == SIGUSR2) {
490 if (read(pfds[0], &done[j],
494 "Short read from child?");
498 exit(run_child(test_tdb, j, seed,
501 printf("Restarting child %i for %u-%u\n",
502 j, done[j], num_loops);
505 printf("child %d exited with signal %d\n",
506 (int)pid, WTERMSIG(status));
509 if (WEXITSTATUS(status) != 0) {
510 printf("child %d exited with status %d\n",
511 (int)pid, WEXITSTATUS(status));
515 memmove(&pids[j], &pids[j+1],
516 (num_procs - j - 1)*sizeof(pids[0]));
523 if (error_count == 0) {
524 db = tdb_open(test_tdb, TDB_DEFAULT, O_RDWR | O_CREAT,
527 fatal(db, "db open failed");
530 if (tdb_check(db, NULL, NULL) != 0) {
531 fatal(db, "db check failed");