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;
39 #ifdef PRINTF_ATTRIBUTE
40 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
42 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
49 vfprintf(stdout, format, ap);
55 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
62 static void fatal(const char *why)
68 static char *randbuf(int len)
72 buf = (char *)malloc(len+1);
75 buf[i] = 'a' + (rand() % 26);
81 static void addrec_db(void);
82 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
86 if (random() % CULL_PROB == 0) {
92 if (random() % TRAVERSE_MOD_PROB == 0) {
97 #if TRAVERSE_ABORT_PROB
98 if (random() % TRAVERSE_ABORT_PROB == 0)
105 static void addrec_db(void)
111 klen = 1 + (rand() % KEYLEN);
112 dlen = 1 + (rand() % DATALEN);
117 key.dptr = (unsigned char *)k;
120 data.dptr = (unsigned char *)d;
124 if (in_traverse == 0 && in_transaction == 0 && random() % TRANSACTION_PROB == 0) {
125 if (tdb_transaction_start(db) != 0) {
126 fatal("tdb_transaction_start failed");
131 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
132 if (random() % TRANSACTION_PREPARE_PROB == 0) {
133 if (tdb_transaction_prepare_commit(db) != 0) {
134 fatal("tdb_transaction_prepare_commit failed");
137 if (tdb_transaction_commit(db) != 0) {
138 fatal("tdb_transaction_commit failed");
144 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
145 if (tdb_transaction_cancel(db) != 0) {
146 fatal("tdb_transaction_cancel failed");
154 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
161 if (random() % DELETE_PROB == 0) {
168 if (random() % STORE_PROB == 0) {
169 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
170 fatal("tdb_store failed");
177 if (random() % APPEND_PROB == 0) {
178 if (tdb_append(db, key, data) != 0) {
179 fatal("tdb_append failed");
186 if (random() % LOCKSTORE_PROB == 0) {
187 tdb_chainlock(db, key);
188 data = tdb_fetch(db, key);
189 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
190 fatal("tdb_store failed");
192 if (data.dptr) free(data.dptr);
193 tdb_chainunlock(db, key);
199 /* FIXME: recursive traverses break transactions? */
200 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
202 tdb_traverse(db, modify_traverse, NULL);
208 #if TRAVERSE_READ_PROB
209 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
211 tdb_traverse_read(db, NULL, NULL);
217 data = tdb_fetch(db, key);
218 if (data.dptr) free(data.dptr);
225 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
228 tdb_delete(tdb, key);
232 static void usage(void)
234 printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
238 int main(int argc, char * const *argv)
242 int num_loops = 5000;
248 struct tdb_logging_context log_ctx;
249 log_ctx.log_fn = tdb_log;
251 while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
254 num_procs = strtol(optarg, NULL, 0);
257 num_loops = strtol(optarg, NULL, 0);
260 hash_size = strtol(optarg, NULL, 0);
263 seed = strtol(optarg, NULL, 0);
270 unlink("torture.tdb");
272 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
275 for (i=0;i<num_procs-1;i++) {
276 if ((pids[i+1]=fork()) == 0) break;
279 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
280 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
282 fatal("db open failed");
286 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
290 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n",
291 num_procs, num_loops, hash_size, seed);
297 for (i=0;i<num_loops && error_count == 0;i++) {
301 if (error_count == 0) {
302 tdb_traverse_read(db, NULL, NULL);
303 tdb_traverse(db, traverse_fn, NULL);
304 tdb_traverse(db, traverse_fn, NULL);
309 if (getpid() != pids[0]) {
313 for (i=1;i<num_procs;i++) {
316 if (error_count != 0) {
317 /* try and stop the test on any failure */
318 for (j=1;j<num_procs;j++) {
320 kill(pids[j], SIGTERM);
324 pid = waitpid(-1, &status, 0);
326 perror("failed to wait for child\n");
329 for (j=1;j<num_procs;j++) {
330 if (pids[j] == pid) break;
332 if (j == num_procs) {
333 printf("unknown child %d exited!?\n", (int)pid);
336 if (WEXITSTATUS(status) != 0) {
337 printf("child %d exited with status %d\n",
338 (int)pid, WEXITSTATUS(status));
346 if (error_count == 0) {