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) {
133 if (random() % TRANSACTION_PREPARE_PROB == 0) {
134 if (tdb_transaction_prepare_commit(db) != 0) {
135 fatal("tdb_transaction_prepare_commit failed");
139 if (tdb_transaction_commit(db) != 0) {
140 fatal("tdb_transaction_commit failed");
146 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
147 if (tdb_transaction_cancel(db) != 0) {
148 fatal("tdb_transaction_cancel failed");
156 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
163 if (random() % DELETE_PROB == 0) {
170 if (random() % STORE_PROB == 0) {
171 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
172 fatal("tdb_store failed");
179 if (random() % APPEND_PROB == 0) {
180 if (tdb_append(db, key, data) != 0) {
181 fatal("tdb_append failed");
188 if (random() % LOCKSTORE_PROB == 0) {
189 tdb_chainlock(db, key);
190 data = tdb_fetch(db, key);
191 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
192 fatal("tdb_store failed");
194 if (data.dptr) free(data.dptr);
195 tdb_chainunlock(db, key);
201 /* FIXME: recursive traverses break transactions? */
202 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
204 tdb_traverse(db, modify_traverse, NULL);
210 #if TRAVERSE_READ_PROB
211 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
213 tdb_traverse_read(db, NULL, NULL);
219 data = tdb_fetch(db, key);
220 if (data.dptr) free(data.dptr);
227 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
230 tdb_delete(tdb, key);
234 static void usage(void)
236 printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
240 int main(int argc, char * const *argv)
244 int num_loops = 5000;
250 struct tdb_logging_context log_ctx;
251 log_ctx.log_fn = tdb_log;
253 while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
256 num_procs = strtol(optarg, NULL, 0);
259 num_loops = strtol(optarg, NULL, 0);
262 hash_size = strtol(optarg, NULL, 0);
265 seed = strtol(optarg, NULL, 0);
272 unlink("torture.tdb");
274 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
277 for (i=0;i<num_procs-1;i++) {
278 if ((pids[i+1]=fork()) == 0) break;
281 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
282 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
284 fatal("db open failed");
288 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
292 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n",
293 num_procs, num_loops, hash_size, seed);
299 for (i=0;i<num_loops && error_count == 0;i++) {
303 if (error_count == 0) {
304 tdb_traverse_read(db, NULL, NULL);
305 tdb_traverse(db, traverse_fn, NULL);
306 tdb_traverse(db, traverse_fn, NULL);
311 if (getpid() != pids[0]) {
315 for (i=1;i<num_procs;i++) {
318 if (error_count != 0) {
319 /* try and stop the test on any failure */
320 for (j=1;j<num_procs;j++) {
322 kill(pids[j], SIGTERM);
326 pid = waitpid(-1, &status, 0);
328 perror("failed to wait for child\n");
331 for (j=1;j<num_procs;j++) {
332 if (pids[j] == pid) break;
334 if (j == num_procs) {
335 printf("unknown child %d exited!?\n", (int)pid);
338 if (WEXITSTATUS(status) != 0) {
339 printf("child %d exited with status %d\n",
340 (int)pid, WEXITSTATUS(status));
348 if (error_count == 0) {