]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb/tools/tdbtorture.c
Use raw .o files rather than -lccan
[ccan] / ccan / tdb / tools / tdbtorture.c
index 9bc799530851a239538d8ca5aeeb7725550ec184..e9c793ac89f08d2b7c34898f5f1e1820efb5fa38 100644 (file)
 #define LOCKSTORE_PROB 5
 #define TRAVERSE_PROB 20
 #define TRAVERSE_READ_PROB 20
+#define TRAVERSE_MOD_PROB 100
+#define TRAVERSE_ABORT_PROB 500
 #define CULL_PROB 100
 #define KEYLEN 3
 #define DATALEN 100
 
 static struct tdb_context *db;
 static int in_transaction;
+static int in_traverse;
 static int error_count;
+static int always_transaction = 0;
 
 #ifdef PRINTF_ATTRIBUTE
 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
@@ -75,14 +79,27 @@ static char *randbuf(int len)
        return buf;
 }
 
-static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
-                        void *state)
+static void addrec_db(void);
+static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
+                          void *state)
 {
 #if CULL_PROB
        if (random() % CULL_PROB == 0) {
                tdb_delete(tdb, key);
        }
 #endif
+
+#if TRAVERSE_MOD_PROB
+       if (random() % TRAVERSE_MOD_PROB == 0) {
+               addrec_db();
+       }
+#endif
+
+#if TRAVERSE_ABORT_PROB
+       if (random() % TRAVERSE_ABORT_PROB == 0)
+               return 1;
+#endif
+
        return 0;
 }
 
@@ -104,29 +121,35 @@ static void addrec_db(void)
        data.dptr = (unsigned char *)d;
        data.dsize = dlen+1;
 
+#if REOPEN_PROB
+       if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
+               tdb_reopen_all(0);
+               goto next;
+       } 
+#endif
+
 #if TRANSACTION_PROB
-       if (in_transaction == 0 && random() % TRANSACTION_PROB == 0) {
+       if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
                if (tdb_transaction_start(db) != 0) {
                        fatal("tdb_transaction_start failed");
                }
                in_transaction++;
                goto next;
        }
-       if (in_transaction && random() % TRANSACTION_PROB == 0) {
-#if 0
+       if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
                if (random() % TRANSACTION_PREPARE_PROB == 0) {
                        if (tdb_transaction_prepare_commit(db) != 0) {
                                fatal("tdb_transaction_prepare_commit failed");
                        }
                }
-#endif
                if (tdb_transaction_commit(db) != 0) {
                        fatal("tdb_transaction_commit failed");
                }
                in_transaction--;
                goto next;
        }
-       if (in_transaction && random() % TRANSACTION_PROB == 0) {
+
+       if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
                if (tdb_transaction_cancel(db) != 0) {
                        fatal("tdb_transaction_cancel failed");
                }
@@ -135,13 +158,6 @@ static void addrec_db(void)
        }
 #endif
 
-#if REOPEN_PROB
-       if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
-               tdb_reopen_all(0);
-               goto next;
-       } 
-#endif
-
 #if DELETE_PROB
        if (random() % DELETE_PROB == 0) {
                tdb_delete(db, key);
@@ -181,15 +197,20 @@ static void addrec_db(void)
 #endif
 
 #if TRAVERSE_PROB
-       if (random() % TRAVERSE_PROB == 0) {
-               tdb_traverse(db, cull_traverse, NULL);
+       /* FIXME: recursive traverses break transactions? */
+       if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
+               in_traverse++;
+               tdb_traverse(db, modify_traverse, NULL);
+               in_traverse--;
                goto next;
        }
 #endif
 
 #if TRAVERSE_READ_PROB
-       if (random() % TRAVERSE_READ_PROB == 0) {
+       if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
+               in_traverse++;
                tdb_traverse_read(db, NULL, NULL);
+               in_traverse--;
                goto next;
        }
 #endif
@@ -211,7 +232,7 @@ static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
 
 static void usage(void)
 {
-       printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
+       printf("Usage: tdbtorture [-t] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
        exit(0);
 }
 
@@ -228,7 +249,7 @@ int main(int argc, char * const *argv)
        struct tdb_logging_context log_ctx;
        log_ctx.log_fn = tdb_log;
 
-       while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
+       while ((c = getopt(argc, argv, "n:l:s:H:th")) != -1) {
                switch (c) {
                case 'n':
                        num_procs = strtol(optarg, NULL, 0);
@@ -242,6 +263,9 @@ int main(int argc, char * const *argv)
                case 's':
                        seed = strtol(optarg, NULL, 0);
                        break;
+               case 't':
+                       always_transaction = 1;
+                       break;
                default:
                        usage();
                }
@@ -267,8 +291,8 @@ int main(int argc, char * const *argv)
        }
 
        if (i == 0) {
-               printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n", 
-                      num_procs, num_loops, hash_size, seed);
+               printf("testing with %d processes, %d loops, %d hash_size, seed=%d%s\n", 
+                      num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
        }
 
        srand(seed + i);
@@ -280,8 +304,20 @@ int main(int argc, char * const *argv)
 
        if (error_count == 0) {
                tdb_traverse_read(db, NULL, NULL);
+               if (always_transaction) {
+                       while (in_transaction) {
+                               tdb_transaction_cancel(db);
+                               in_transaction--;
+                       }
+                       if (tdb_transaction_start(db) != 0)
+                               fatal("tdb_transaction_start failed");
+               }
                tdb_traverse(db, traverse_fn, NULL);
                tdb_traverse(db, traverse_fn, NULL);
+               if (always_transaction) {
+                       if (tdb_transaction_commit(db) != 0)
+                               fatal("tdb_transaction_commit failed");
+               }
        }
 
        tdb_close(db);