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;
38 static int always_transaction = 0;
40 #ifdef PRINTF_ATTRIBUTE
41 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
43 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
50 vfprintf(stdout, format, ap);
56 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
63 static void fatal(const char *why)
69 static char *randbuf(int len)
73 buf = (char *)malloc(len+1);
76 buf[i] = 'a' + (rand() % 26);
82 static void addrec_db(void);
83 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
87 if (random() % CULL_PROB == 0) {
93 if (random() % TRAVERSE_MOD_PROB == 0) {
98 #if TRAVERSE_ABORT_PROB
99 if (random() % TRAVERSE_ABORT_PROB == 0)
106 static void addrec_db(void)
112 klen = 1 + (rand() % KEYLEN);
113 dlen = 1 + (rand() % DATALEN);
118 key.dptr = (unsigned char *)k;
121 data.dptr = (unsigned char *)d;
125 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
132 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
133 if (tdb_transaction_start(db) != 0) {
134 fatal("tdb_transaction_start failed");
139 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
140 if (random() % TRANSACTION_PREPARE_PROB == 0) {
141 if (tdb_transaction_prepare_commit(db) != 0) {
142 fatal("tdb_transaction_prepare_commit failed");
145 if (tdb_transaction_commit(db) != 0) {
146 fatal("tdb_transaction_commit failed");
152 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
153 if (tdb_transaction_cancel(db) != 0) {
154 fatal("tdb_transaction_cancel failed");
162 if (random() % DELETE_PROB == 0) {
169 if (random() % STORE_PROB == 0) {
170 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
171 fatal("tdb_store failed");
178 if (random() % APPEND_PROB == 0) {
179 if (tdb_append(db, key, data) != 0) {
180 fatal("tdb_append failed");
187 if (random() % LOCKSTORE_PROB == 0) {
188 tdb_chainlock(db, key);
189 data = tdb_fetch(db, key);
190 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
191 fatal("tdb_store failed");
193 if (data.dptr) free(data.dptr);
194 tdb_chainunlock(db, key);
200 /* FIXME: recursive traverses break transactions? */
201 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
203 tdb_traverse(db, modify_traverse, NULL);
209 #if TRAVERSE_READ_PROB
210 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
212 tdb_traverse_read(db, NULL, NULL);
218 data = tdb_fetch(db, key);
219 if (data.dptr) free(data.dptr);
226 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
229 tdb_delete(tdb, key);
233 static void usage(void)
235 printf("Usage: tdbtorture [-t] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
239 int main(int argc, char * const *argv)
243 int num_loops = 5000;
249 struct tdb_logging_context log_ctx;
250 log_ctx.log_fn = tdb_log;
252 while ((c = getopt(argc, argv, "n:l:s:H:th")) != -1) {
255 num_procs = strtol(optarg, NULL, 0);
258 num_loops = strtol(optarg, NULL, 0);
261 hash_size = strtol(optarg, NULL, 0);
264 seed = strtol(optarg, NULL, 0);
267 always_transaction = 1;
274 unlink("torture.tdb");
276 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
279 for (i=0;i<num_procs-1;i++) {
280 if ((pids[i+1]=fork()) == 0) break;
283 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
284 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
286 fatal("db open failed");
290 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
294 printf("testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
295 num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
301 for (i=0;i<num_loops && error_count == 0;i++) {
305 if (error_count == 0) {
306 tdb_traverse_read(db, NULL, NULL);
307 if (always_transaction) {
308 while (in_transaction) {
309 tdb_transaction_cancel(db);
312 if (tdb_transaction_start(db) != 0)
313 fatal("tdb_transaction_start failed");
315 tdb_traverse(db, traverse_fn, NULL);
316 tdb_traverse(db, traverse_fn, NULL);
317 if (always_transaction) {
318 if (tdb_transaction_commit(db) != 0)
319 fatal("tdb_transaction_commit failed");
325 if (getpid() != pids[0]) {
329 for (i=1;i<num_procs;i++) {
332 if (error_count != 0) {
333 /* try and stop the test on any failure */
334 for (j=1;j<num_procs;j++) {
336 kill(pids[j], SIGTERM);
340 pid = waitpid(-1, &status, 0);
342 perror("failed to wait for child\n");
345 for (j=1;j<num_procs;j++) {
346 if (pids[j] == pid) break;
348 if (j == num_procs) {
349 printf("unknown child %d exited!?\n", (int)pid);
352 if (WEXITSTATUS(status) != 0) {
353 printf("child %d exited with status %d\n",
354 (int)pid, WEXITSTATUS(status));
362 if (error_count == 0) {