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 //#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 const char *message, void *data)
48 fputs(message, stdout);
53 signal(SIGUSR1, SIG_IGN);
54 sprintf(str,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
60 #include "../private.h"
62 static void segv_handler(int sig, siginfo_t *info, void *p)
66 sprintf(string, "%u: death at %p (map_ptr %p, map_size %zu)\n",
67 getpid(), info->si_addr, db->file->map_ptr,
68 (size_t)db->file->map_size);
69 if (write(2, string, strlen(string)) > 0)
74 static void fatal(struct tdb_context *tdb, const char *why)
76 fprintf(stderr, "%u:%s:%s\n", getpid(), why,
77 tdb ? tdb_errorstr(tdb_error(tdb)) : "(no tdb)");
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(db, "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(db, "tdb_transaction_prepare_commit failed");
157 if (tdb_transaction_commit(db) != 0) {
158 fatal(db, "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(db, "tdb_store failed");
188 if (random() % APPEND_PROB == 0) {
189 if (tdb_append(db, key, data) != 0) {
190 fatal(db, "tdb_append failed");
197 if (random() % LOCKSTORE_PROB == 0) {
198 tdb_chainlock(db, key);
199 if (tdb_fetch(db, key, &data) != TDB_SUCCESS) {
203 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
204 fatal(db, "tdb_store failed");
206 if (data.dptr) free(data.dptr);
207 tdb_chainunlock(db, key);
213 /* FIXME: recursive traverses break transactions? */
214 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
216 tdb_traverse(db, modify_traverse, NULL);
222 if (tdb_fetch(db, key, &data) == TDB_SUCCESS)
230 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
233 tdb_delete(tdb, key);
237 static void usage(void)
239 printf("Usage: tdbtorture"
243 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S]\n");
247 static void send_count_and_suicide(int sig)
249 /* This ensures our successor can continue where we left off. */
250 if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
252 /* This gives a unique signature. */
253 kill(getpid(), SIGUSR2);
256 static int run_child(int i, int seed, unsigned num_loops, unsigned start,
259 struct sigaction act = { .sa_sigaction = segv_handler,
260 .sa_flags = SA_SIGINFO };
261 sigaction(11, &act, NULL);
263 db = tdb_open("torture.tdb", tdb_flags, O_RDWR | O_CREAT, 0600,
266 fatal(NULL, "db open failed");
271 printf("pid %i\n", getpid());
280 /* Set global, then we're ready to handle being killed. */
282 signal(SIGUSR1, send_count_and_suicide);
284 for (;loopnum<num_loops && error_count == 0;loopnum++) {
288 if (error_count == 0) {
289 tdb_traverse(db, NULL, NULL);
291 if (always_transaction) {
292 while (in_transaction) {
293 tdb_transaction_cancel(db);
296 if (tdb_transaction_start(db) != 0)
297 fatal(db, "tdb_transaction_start failed");
300 tdb_traverse(db, traverse_fn, NULL);
301 tdb_traverse(db, traverse_fn, NULL);
304 if (always_transaction) {
305 if (tdb_transaction_commit(db) != 0)
306 fatal(db, "tdb_transaction_commit failed");
313 return (error_count < 100 ? error_count : 100);
316 int main(int argc, char * const *argv)
319 int num_loops = 5000;
326 int tdb_flags = TDB_DEFAULT;
328 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
329 log_attr.base.next = &seed_attr;
330 log_attr.log.fn = tdb_log;
331 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
333 while ((c = getopt(argc, argv, "n:l:s:thkS")) != -1) {
336 num_procs = strtol(optarg, NULL, 0);
339 num_loops = strtol(optarg, NULL, 0);
342 seed = strtol(optarg, NULL, 0);
345 tdb_flags = TDB_NOSYNC;
349 always_transaction = 1;
351 fprintf(stderr, "Transactions not supported\n");
363 unlink("torture.tdb");
366 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
368 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
370 if (num_procs == 1 && !kill_random) {
371 /* Don't fork for this case, makes debugging easier. */
372 error_count = run_child(0, seed, num_loops, 0, tdb_flags);
376 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
377 done = (int *)calloc(sizeof(int), num_procs);
379 if (pipe(pfds) != 0) {
380 perror("Creating pipe");
383 count_pipe = pfds[1];
385 for (i=0;i<num_procs;i++) {
386 if ((pids[i]=fork()) == 0) {
389 printf("testing with %d processes, %d loops, seed=%d%s\n",
390 num_procs, num_loops, seed,
392 always_transaction ? " (all within transactions)" : ""
398 exit(run_child(i, seed, num_loops, 0, tdb_flags));
406 if (error_count != 0) {
407 /* try and stop the test on any failure */
408 for (j=0;j<num_procs;j++) {
410 kill(pids[j], SIGTERM);
415 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
419 /* Sleep for 1/10 second. */
421 ts.tv_nsec = 100000000;
422 nanosleep(&ts, NULL);
425 kill(pids[random() % num_procs], SIGUSR1);
430 perror("failed to wait for child\n");
434 for (j=0;j<num_procs;j++) {
435 if (pids[j] == pid) break;
437 if (j == num_procs) {
438 printf("unknown child %d exited!?\n", (int)pid);
441 if (WIFSIGNALED(status)) {
442 if (WTERMSIG(status) == SIGUSR2
443 || WTERMSIG(status) == SIGUSR1) {
444 /* SIGUSR2 means they wrote to pipe. */
445 if (WTERMSIG(status) == SIGUSR2) {
446 if (read(pfds[0], &done[j],
450 "Short read from child?");
454 exit(run_child(j, seed, num_loops,
455 done[j], tdb_flags));
456 printf("Restarting child %i for %u-%u\n",
457 j, done[j], num_loops);
460 printf("child %d exited with signal %d\n",
461 (int)pid, WTERMSIG(status));
464 if (WEXITSTATUS(status) != 0) {
465 printf("child %d exited with status %d\n",
466 (int)pid, WEXITSTATUS(status));
470 memmove(&pids[j], &pids[j+1],
471 (num_procs - j - 1)*sizeof(pids[0]));
478 if (error_count == 0) {
479 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
482 fatal(db, "db open failed");
485 if (tdb_check(db, NULL, NULL) != 0) {
486 fatal(db, "db check failed");