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,
46 enum tdb_log_level level,
51 printf("tdb:%s:%s:%s\n",
52 tdb_name(tdb), tdb_errorstr(ecode), message);
57 signal(SIGUSR1, SIG_IGN);
58 sprintf(str,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
64 #include "../private.h"
66 static void segv_handler(int sig, siginfo_t *info, void *p)
70 sprintf(string, "%u: death at %p (map_ptr %p, map_size %zu)\n",
71 getpid(), info->si_addr, db->file->map_ptr,
72 (size_t)db->file->map_size);
73 if (write(2, string, strlen(string)) > 0)
78 static void fatal(struct tdb_context *tdb, const char *why)
80 fprintf(stderr, "%u:%s:%s\n", getpid(), why,
81 tdb ? tdb_errorstr(tdb_error(tdb)) : "(no tdb)");
85 static char *randbuf(int len)
89 buf = (char *)malloc(len+1);
92 buf[i] = 'a' + (rand() % 26);
98 static void addrec_db(void);
99 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
103 if (random() % CULL_PROB == 0) {
104 tdb_delete(tdb, key);
108 #if TRAVERSE_MOD_PROB
109 if (random() % TRAVERSE_MOD_PROB == 0) {
114 #if TRAVERSE_ABORT_PROB
115 if (random() % TRAVERSE_ABORT_PROB == 0)
122 static void addrec_db(void)
128 klen = 1 + (rand() % KEYLEN);
129 dlen = 1 + (rand() % DATALEN);
134 key.dptr = (unsigned char *)k;
137 data.dptr = (unsigned char *)d;
141 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
148 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
149 if (tdb_transaction_start(db) != 0) {
150 fatal(db, "tdb_transaction_start failed");
155 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
156 if (random() % TRANSACTION_PREPARE_PROB == 0) {
157 if (tdb_transaction_prepare_commit(db) != 0) {
158 fatal(db, "tdb_transaction_prepare_commit failed");
161 if (tdb_transaction_commit(db) != 0) {
162 fatal(db, "tdb_transaction_commit failed");
168 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
169 tdb_transaction_cancel(db);
176 if (random() % DELETE_PROB == 0) {
183 if (random() % STORE_PROB == 0) {
184 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
185 fatal(db, "tdb_store failed");
192 if (random() % APPEND_PROB == 0) {
193 if (tdb_append(db, key, data) != 0) {
194 fatal(db, "tdb_append failed");
201 if (random() % LOCKSTORE_PROB == 0) {
202 tdb_chainlock(db, key);
203 if (tdb_fetch(db, key, &data) != TDB_SUCCESS) {
207 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
208 fatal(db, "tdb_store failed");
210 if (data.dptr) free(data.dptr);
211 tdb_chainunlock(db, key);
217 /* FIXME: recursive traverses break transactions? */
218 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
220 tdb_traverse(db, modify_traverse, NULL);
226 if (tdb_fetch(db, key, &data) == TDB_SUCCESS)
234 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
237 tdb_delete(tdb, key);
241 static void usage(void)
243 printf("Usage: tdbtorture"
247 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S]\n");
251 static void send_count_and_suicide(int sig)
253 /* This ensures our successor can continue where we left off. */
254 if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
256 /* This gives a unique signature. */
257 kill(getpid(), SIGUSR2);
260 static int run_child(int i, int seed, unsigned num_loops, unsigned start,
263 struct sigaction act = { .sa_sigaction = segv_handler,
264 .sa_flags = SA_SIGINFO };
265 sigaction(11, &act, NULL);
267 db = tdb_open("torture.tdb", tdb_flags, O_RDWR | O_CREAT, 0600,
270 fatal(NULL, "db open failed");
275 printf("pid %i\n", getpid());
284 /* Set global, then we're ready to handle being killed. */
286 signal(SIGUSR1, send_count_and_suicide);
288 for (;loopnum<num_loops && error_count == 0;loopnum++) {
292 if (error_count == 0) {
293 tdb_traverse(db, NULL, NULL);
295 if (always_transaction) {
296 while (in_transaction) {
297 tdb_transaction_cancel(db);
300 if (tdb_transaction_start(db) != 0)
301 fatal(db, "tdb_transaction_start failed");
304 tdb_traverse(db, traverse_fn, NULL);
305 tdb_traverse(db, traverse_fn, NULL);
308 if (always_transaction) {
309 if (tdb_transaction_commit(db) != 0)
310 fatal(db, "tdb_transaction_commit failed");
317 return (error_count < 100 ? error_count : 100);
320 int main(int argc, char * const *argv)
323 int num_loops = 5000;
330 int tdb_flags = TDB_DEFAULT;
332 log_attr.base.attr = TDB_ATTRIBUTE_LOG;
333 log_attr.base.next = &seed_attr;
334 log_attr.log.fn = tdb_log;
335 seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
337 while ((c = getopt(argc, argv, "n:l:s:thkS")) != -1) {
340 num_procs = strtol(optarg, NULL, 0);
343 num_loops = strtol(optarg, NULL, 0);
346 seed = strtol(optarg, NULL, 0);
349 tdb_flags = TDB_NOSYNC;
353 always_transaction = 1;
355 fprintf(stderr, "Transactions not supported\n");
367 unlink("torture.tdb");
370 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
372 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
374 if (num_procs == 1 && !kill_random) {
375 /* Don't fork for this case, makes debugging easier. */
376 error_count = run_child(0, seed, num_loops, 0, tdb_flags);
380 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
381 done = (int *)calloc(sizeof(int), num_procs);
383 if (pipe(pfds) != 0) {
384 perror("Creating pipe");
387 count_pipe = pfds[1];
389 for (i=0;i<num_procs;i++) {
390 if ((pids[i]=fork()) == 0) {
393 printf("testing with %d processes, %d loops, seed=%d%s\n",
394 num_procs, num_loops, seed,
396 always_transaction ? " (all within transactions)" : ""
402 exit(run_child(i, seed, num_loops, 0, tdb_flags));
410 if (error_count != 0) {
411 /* try and stop the test on any failure */
412 for (j=0;j<num_procs;j++) {
414 kill(pids[j], SIGTERM);
419 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
423 /* Sleep for 1/10 second. */
425 ts.tv_nsec = 100000000;
426 nanosleep(&ts, NULL);
429 kill(pids[random() % num_procs], SIGUSR1);
434 perror("failed to wait for child\n");
438 for (j=0;j<num_procs;j++) {
439 if (pids[j] == pid) break;
441 if (j == num_procs) {
442 printf("unknown child %d exited!?\n", (int)pid);
445 if (WIFSIGNALED(status)) {
446 if (WTERMSIG(status) == SIGUSR2
447 || WTERMSIG(status) == SIGUSR1) {
448 /* SIGUSR2 means they wrote to pipe. */
449 if (WTERMSIG(status) == SIGUSR2) {
450 if (read(pfds[0], &done[j],
454 "Short read from child?");
458 exit(run_child(j, seed, num_loops,
459 done[j], tdb_flags));
460 printf("Restarting child %i for %u-%u\n",
461 j, done[j], num_loops);
464 printf("child %d exited with signal %d\n",
465 (int)pid, WTERMSIG(status));
468 if (WEXITSTATUS(status) != 0) {
469 printf("child %d exited with status %d\n",
470 (int)pid, WEXITSTATUS(status));
474 memmove(&pids[j], &pids[j+1],
475 (num_procs - j - 1)*sizeof(pids[0]));
482 if (error_count == 0) {
483 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
486 fatal(db, "db open failed");
489 if (tdb_check(db, NULL, NULL) != 0) {
490 fatal(db, "db check failed");