1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
6 #include "system/time.h"
7 #include "system/wait.h"
8 #include "system/filesys.h"
16 #define REOPEN_PROB 30
20 #define TRANSACTION_PROB 10
21 #define TRANSACTION_PREPARE_PROB 2
22 #define LOCKSTORE_PROB 5
23 #define TRAVERSE_PROB 20
24 #define TRAVERSE_READ_PROB 20
29 static struct tdb_context *db;
30 static int in_transaction;
31 static int error_count;
32 static int always_transaction = 0;
33 static int hash_size = 2;
35 static int count_pipe;
36 static struct tdb_logging_context log_ctx;
38 #ifdef PRINTF_ATTRIBUTE
39 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
41 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
45 /* trace level messages do not indicate an error */
46 if (level != TDB_DEBUG_TRACE) {
51 vfprintf(stdout, format, ap);
55 if (level != TDB_DEBUG_TRACE) {
57 signal(SIGUSR1, SIG_IGN);
58 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
65 static void fatal(const char *why)
71 static char *randbuf(int len)
75 buf = (char *)malloc(len+1);
78 buf[i] = 'a' + (rand() % 26);
84 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
88 if (random() % CULL_PROB == 0) {
95 static void addrec_db(void)
101 klen = 1 + (rand() % KEYLEN);
102 dlen = 1 + (rand() % DATALEN);
107 key.dptr = (unsigned char *)k;
110 data.dptr = (unsigned char *)d;
114 if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
121 if (in_transaction == 0 &&
122 (always_transaction || random() % TRANSACTION_PROB == 0)) {
123 if (tdb_transaction_start(db) != 0) {
124 fatal("tdb_transaction_start failed");
129 if (in_transaction && random() % TRANSACTION_PROB == 0) {
130 if (random() % TRANSACTION_PREPARE_PROB == 0) {
131 if (tdb_transaction_prepare_commit(db) != 0) {
132 fatal("tdb_transaction_prepare_commit failed");
135 if (tdb_transaction_commit(db) != 0) {
136 fatal("tdb_transaction_commit failed");
141 if (in_transaction && random() % TRANSACTION_PROB == 0) {
142 if (tdb_transaction_cancel(db) != 0) {
143 fatal("tdb_transaction_cancel failed");
151 if (random() % DELETE_PROB == 0) {
158 if (random() % STORE_PROB == 0) {
159 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
160 fatal("tdb_store failed");
167 if (random() % APPEND_PROB == 0) {
168 if (tdb_append(db, key, data) != 0) {
169 fatal("tdb_append failed");
176 if (random() % LOCKSTORE_PROB == 0) {
177 tdb_chainlock(db, key);
178 data = tdb_fetch(db, key);
179 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
180 fatal("tdb_store failed");
182 if (data.dptr) free(data.dptr);
183 tdb_chainunlock(db, key);
189 if (random() % TRAVERSE_PROB == 0) {
190 tdb_traverse(db, cull_traverse, NULL);
195 #if TRAVERSE_READ_PROB
196 if (random() % TRAVERSE_READ_PROB == 0) {
197 tdb_traverse_read(db, NULL, NULL);
202 data = tdb_fetch(db, key);
203 if (data.dptr) free(data.dptr);
210 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
213 tdb_delete(tdb, key);
217 static void usage(void)
219 printf("Usage: tdbtorture [-t] [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
223 static void send_count_and_suicide(int sig)
225 /* This ensures our successor can continue where we left off. */
226 write(count_pipe, &loopnum, sizeof(loopnum));
227 /* This gives a unique signature. */
228 kill(getpid(), SIGUSR2);
231 static int run_child(const char *filename, int i, int seed, unsigned num_loops, unsigned start)
233 db = tdb_open_ex(filename, hash_size, TDB_DEFAULT,
234 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
236 fatal("db open failed");
242 /* Set global, then we're ready to handle being killed. */
244 signal(SIGUSR1, send_count_and_suicide);
246 for (;loopnum<num_loops && error_count == 0;loopnum++) {
250 if (error_count == 0) {
251 tdb_traverse_read(db, NULL, NULL);
252 if (always_transaction) {
253 while (in_transaction) {
254 tdb_transaction_cancel(db);
257 if (tdb_transaction_start(db) != 0)
258 fatal("tdb_transaction_start failed");
260 tdb_traverse(db, traverse_fn, NULL);
261 tdb_traverse(db, traverse_fn, NULL);
262 if (always_transaction) {
263 if (tdb_transaction_commit(db) != 0)
264 fatal("tdb_transaction_commit failed");
270 return (error_count < 100 ? error_count : 100);
273 static char *test_path(const char *filename)
275 const char *prefix = getenv("TEST_DATA_PREFIX");
281 ret = asprintf(&path, "%s/%s", prefix, filename);
288 return strdup(filename);
291 int main(int argc, char * const *argv)
294 int num_loops = 5000;
303 log_ctx.log_fn = tdb_log;
305 while ((c = getopt(argc, argv, "n:l:s:H:thk")) != -1) {
308 num_procs = strtol(optarg, NULL, 0);
311 num_loops = strtol(optarg, NULL, 0);
314 hash_size = strtol(optarg, NULL, 0);
317 seed = strtol(optarg, NULL, 0);
320 always_transaction = 1;
330 test_tdb = test_path("torture.tdb");
335 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
338 if (num_procs == 1 && !kill_random) {
339 /* Don't fork for this case, makes debugging easier. */
340 error_count = run_child(test_tdb, 0, seed, num_loops, 0);
344 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
346 perror("Unable to allocate memory for pids");
349 done = (int *)calloc(sizeof(int), num_procs);
351 perror("Unable to allocate memory for done");
355 if (pipe(pfds) != 0) {
356 perror("Creating pipe");
359 count_pipe = pfds[1];
361 for (i=0;i<num_procs;i++) {
362 if ((pids[i]=fork()) == 0) {
365 printf("Testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
366 num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
368 exit(run_child(test_tdb, i, seed, num_loops, 0));
376 if (error_count != 0) {
377 /* try and stop the test on any failure */
378 for (j=0;j<num_procs;j++) {
380 kill(pids[j], SIGTERM);
385 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
389 /* Sleep for 1/10 second. */
392 select(0, NULL, NULL, NULL, &tv);
395 kill(pids[random() % num_procs], SIGUSR1);
400 perror("failed to wait for child\n");
404 for (j=0;j<num_procs;j++) {
405 if (pids[j] == pid) break;
407 if (j == num_procs) {
408 printf("unknown child %d exited!?\n", (int)pid);
411 if (WIFSIGNALED(status)) {
412 if (WTERMSIG(status) == SIGUSR2
413 || WTERMSIG(status) == SIGUSR1) {
414 /* SIGUSR2 means they wrote to pipe. */
415 if (WTERMSIG(status) == SIGUSR2) {
416 read(pfds[0], &done[j],
421 exit(run_child(test_tdb, j, seed,
422 num_loops, done[j]));
423 printf("Restarting child %i for %u-%u\n",
424 j, done[j], num_loops);
427 printf("child %d exited with signal %d\n",
428 (int)pid, WTERMSIG(status));
431 if (WEXITSTATUS(status) != 0) {
432 printf("child %d exited with status %d\n",
433 (int)pid, WEXITSTATUS(status));
437 memmove(&pids[j], &pids[j+1],
438 (num_procs - j - 1)*sizeof(pids[0]));
445 if (error_count == 0) {
446 db = tdb_open_ex(test_tdb, hash_size, TDB_DEFAULT,
447 O_RDWR, 0, &log_ctx, NULL);
449 fatal("db open failed\n");
452 if (tdb_check(db, NULL, NULL) == -1) {
453 printf("db check failed\n");