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);
345 done = (int *)calloc(sizeof(int), num_procs);
347 if (pipe(pfds) != 0) {
348 perror("Creating pipe");
351 count_pipe = pfds[1];
353 for (i=0;i<num_procs;i++) {
354 if ((pids[i]=fork()) == 0) {
357 printf("Testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
358 num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
360 exit(run_child(test_tdb, i, seed, num_loops, 0));
368 if (error_count != 0) {
369 /* try and stop the test on any failure */
370 for (j=0;j<num_procs;j++) {
372 kill(pids[j], SIGTERM);
377 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
381 /* Sleep for 1/10 second. */
384 select(0, NULL, NULL, NULL, &tv);
387 kill(pids[random() % num_procs], SIGUSR1);
392 perror("failed to wait for child\n");
396 for (j=0;j<num_procs;j++) {
397 if (pids[j] == pid) break;
399 if (j == num_procs) {
400 printf("unknown child %d exited!?\n", (int)pid);
403 if (WIFSIGNALED(status)) {
404 if (WTERMSIG(status) == SIGUSR2
405 || WTERMSIG(status) == SIGUSR1) {
406 /* SIGUSR2 means they wrote to pipe. */
407 if (WTERMSIG(status) == SIGUSR2) {
408 read(pfds[0], &done[j],
413 exit(run_child(test_tdb, j, seed,
414 num_loops, done[j]));
415 printf("Restarting child %i for %u-%u\n",
416 j, done[j], num_loops);
419 printf("child %d exited with signal %d\n",
420 (int)pid, WTERMSIG(status));
423 if (WEXITSTATUS(status) != 0) {
424 printf("child %d exited with status %d\n",
425 (int)pid, WEXITSTATUS(status));
429 memmove(&pids[j], &pids[j+1],
430 (num_procs - j - 1)*sizeof(pids[0]));
437 if (error_count == 0) {
438 db = tdb_open_ex(test_tdb, hash_size, TDB_DEFAULT,
439 O_RDWR, 0, &log_ctx, NULL);
441 fatal("db open failed");
443 if (tdb_check(db, NULL, NULL) == -1) {
444 printf("db check failed");