1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
5 #include <ccan/tdb/tdb.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_READ_PROB 20
28 #define TRAVERSE_MOD_PROB 100
29 #define TRAVERSE_ABORT_PROB 500
34 static struct tdb_context *db;
35 static int in_transaction;
36 static int in_traverse;
37 static int error_count;
38 static int always_transaction = 0;
39 static int hash_size = 2;
41 static int count_pipe;
42 static struct tdb_logging_context log_ctx;
44 #ifdef PRINTF_ATTRIBUTE
45 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
47 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
51 if (level != TDB_DEBUG_TRACE)
55 vfprintf(stdout, format, ap);
59 if (level != TDB_DEBUG_TRACE) {
61 signal(SIGUSR1, SIG_IGN);
62 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
69 static void fatal(const char *why)
75 static char *randbuf(int len)
79 buf = (char *)malloc(len+1);
82 buf[i] = 'a' + (rand() % 26);
88 static void addrec_db(void);
89 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
93 if (random() % CULL_PROB == 0) {
99 if (random() % TRAVERSE_MOD_PROB == 0) {
104 #if TRAVERSE_ABORT_PROB
105 if (random() % TRAVERSE_ABORT_PROB == 0)
112 static void addrec_db(void)
118 klen = 1 + (rand() % KEYLEN);
119 dlen = 1 + (rand() % DATALEN);
124 key.dptr = (unsigned char *)k;
127 data.dptr = (unsigned char *)d;
131 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
138 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
139 if (tdb_transaction_start(db) != 0) {
140 fatal("tdb_transaction_start failed");
145 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
146 if (random() % TRANSACTION_PREPARE_PROB == 0) {
147 if (tdb_transaction_prepare_commit(db) != 0) {
148 fatal("tdb_transaction_prepare_commit failed");
151 if (tdb_transaction_commit(db) != 0) {
152 fatal("tdb_transaction_commit failed");
158 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
159 if (tdb_transaction_cancel(db) != 0) {
160 fatal("tdb_transaction_cancel failed");
168 if (random() % DELETE_PROB == 0) {
175 if (random() % STORE_PROB == 0) {
176 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
177 fatal("tdb_store failed");
184 if (random() % APPEND_PROB == 0) {
185 if (tdb_append(db, key, data) != 0) {
186 fatal("tdb_append failed");
193 if (random() % LOCKSTORE_PROB == 0) {
194 tdb_chainlock(db, key);
195 data = tdb_fetch(db, key);
196 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
197 fatal("tdb_store failed");
199 if (data.dptr) free(data.dptr);
200 tdb_chainunlock(db, key);
206 /* FIXME: recursive traverses break transactions? */
207 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
209 tdb_traverse(db, modify_traverse, NULL);
215 #if TRAVERSE_READ_PROB
216 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
218 tdb_traverse_read(db, NULL, NULL);
224 data = tdb_fetch(db, key);
225 if (data.dptr) free(data.dptr);
232 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
235 tdb_delete(tdb, key);
239 static void usage(void)
241 printf("Usage: tdbtorture [-t] [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
245 static void send_count_and_suicide(int sig)
247 /* This ensures our successor can continue where we left off. */
248 write(count_pipe, &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 db = tdb_open_ex("torture.tdb", hash_size, TDB_DEFAULT,
256 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
258 fatal("db open failed");
264 /* Set global, then we're ready to handle being killed. */
266 signal(SIGUSR1, send_count_and_suicide);
268 for (;loopnum<num_loops && error_count == 0;loopnum++) {
272 if (error_count == 0) {
273 tdb_traverse_read(db, NULL, NULL);
274 if (always_transaction) {
275 while (in_transaction) {
276 tdb_transaction_cancel(db);
279 if (tdb_transaction_start(db) != 0)
280 fatal("tdb_transaction_start failed");
282 tdb_traverse(db, traverse_fn, NULL);
283 tdb_traverse(db, traverse_fn, NULL);
284 if (always_transaction) {
285 if (tdb_transaction_commit(db) != 0)
286 fatal("tdb_transaction_commit failed");
292 return (error_count < 100 ? error_count : 100);
295 int main(int argc, char * const *argv)
298 int num_loops = 5000;
306 log_ctx.log_fn = tdb_log;
308 while ((c = getopt(argc, argv, "n:l:s:H:thk")) != -1) {
311 num_procs = strtol(optarg, NULL, 0);
314 num_loops = strtol(optarg, NULL, 0);
317 hash_size = strtol(optarg, NULL, 0);
320 seed = strtol(optarg, NULL, 0);
323 always_transaction = 1;
333 unlink("torture.tdb");
336 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
339 if (num_procs == 1 && !kill_random) {
340 /* Don't fork for this case, makes debugging easier. */
341 error_count = run_child(0, seed, num_loops, 0);
345 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
346 done = (int *)calloc(sizeof(int), num_procs);
348 if (pipe(pfds) != 0) {
349 perror("Creating pipe");
352 count_pipe = pfds[1];
354 for (i=0;i<num_procs;i++) {
355 if ((pids[i]=fork()) == 0) {
358 printf("testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
359 num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
361 exit(run_child(i, seed, num_loops, 0));
369 if (error_count != 0) {
370 /* try and stop the test on any failure */
371 for (j=0;j<num_procs;j++) {
373 kill(pids[j], SIGTERM);
378 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
382 /* Sleep for 1/10 second. */
384 ts.tv_nsec = 100000000;
385 nanosleep(&ts, NULL);
388 kill(pids[random() % num_procs], SIGUSR1);
393 perror("failed to wait for child\n");
397 for (j=0;j<num_procs;j++) {
398 if (pids[j] == pid) break;
400 if (j == num_procs) {
401 printf("unknown child %d exited!?\n", (int)pid);
404 if (WIFSIGNALED(status)) {
405 if (WTERMSIG(status) == SIGUSR2
406 || WTERMSIG(status) == SIGUSR1) {
407 /* SIGUSR2 means they wrote to pipe. */
408 if (WTERMSIG(status) == SIGUSR2) {
409 read(pfds[0], &done[j],
414 exit(run_child(j, seed, num_loops,
416 printf("Restarting child %i for %u-%u\n",
417 j, done[j], num_loops);
420 printf("child %d exited with signal %d\n",
421 (int)pid, WTERMSIG(status));
424 if (WEXITSTATUS(status) != 0) {
425 printf("child %d exited with status %d\n",
426 (int)pid, WEXITSTATUS(status));
430 memmove(&pids[j], &pids[j+1],
431 (num_procs - j - 1)*sizeof(pids[0]));
438 if (error_count == 0) {
439 db = tdb_open_ex("torture.tdb", hash_size, TDB_DEFAULT,
440 O_RDWR, 0, &log_ctx, NULL);
442 fatal("db open failed");
444 if (tdb_check(db, NULL, NULL) == -1) {
445 printf("db check failed");