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
32 static struct tdb_context *db;
33 static int in_transaction;
34 static int error_count;
36 #ifdef PRINTF_ATTRIBUTE
37 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
39 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
46 vfprintf(stdout, format, ap);
52 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
59 static void fatal(const char *why)
65 static char *randbuf(int len)
69 buf = (char *)malloc(len+1);
72 buf[i] = 'a' + (rand() % 26);
78 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
82 if (random() % CULL_PROB == 0) {
89 static void addrec_db(void)
95 klen = 1 + (rand() % KEYLEN);
96 dlen = 1 + (rand() % DATALEN);
101 key.dptr = (unsigned char *)k;
104 data.dptr = (unsigned char *)d;
108 if (in_transaction == 0 && random() % TRANSACTION_PROB == 0) {
109 if (tdb_transaction_start(db) != 0) {
110 fatal("tdb_transaction_start failed");
115 if (in_transaction && random() % TRANSACTION_PROB == 0) {
117 if (random() % TRANSACTION_PREPARE_PROB == 0) {
118 if (tdb_transaction_prepare_commit(db) != 0) {
119 fatal("tdb_transaction_prepare_commit failed");
123 if (tdb_transaction_commit(db) != 0) {
124 fatal("tdb_transaction_commit failed");
129 if (in_transaction && random() % TRANSACTION_PROB == 0) {
130 if (tdb_transaction_cancel(db) != 0) {
131 fatal("tdb_transaction_cancel failed");
139 if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
146 if (random() % DELETE_PROB == 0) {
153 if (random() % STORE_PROB == 0) {
154 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
155 fatal("tdb_store failed");
162 if (random() % APPEND_PROB == 0) {
163 if (tdb_append(db, key, data) != 0) {
164 fatal("tdb_append failed");
171 if (random() % LOCKSTORE_PROB == 0) {
172 tdb_chainlock(db, key);
173 data = tdb_fetch(db, key);
174 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
175 fatal("tdb_store failed");
177 if (data.dptr) free(data.dptr);
178 tdb_chainunlock(db, key);
184 if (random() % TRAVERSE_PROB == 0) {
185 tdb_traverse(db, cull_traverse, NULL);
190 #if TRAVERSE_READ_PROB
191 if (random() % TRAVERSE_READ_PROB == 0) {
192 tdb_traverse_read(db, NULL, NULL);
197 data = tdb_fetch(db, key);
198 if (data.dptr) free(data.dptr);
205 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
208 tdb_delete(tdb, key);
212 static void usage(void)
214 printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
218 int main(int argc, char * const *argv)
222 int num_loops = 5000;
228 struct tdb_logging_context log_ctx;
229 log_ctx.log_fn = tdb_log;
231 while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
234 num_procs = strtol(optarg, NULL, 0);
237 num_loops = strtol(optarg, NULL, 0);
240 hash_size = strtol(optarg, NULL, 0);
243 seed = strtol(optarg, NULL, 0);
250 unlink("torture.tdb");
252 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
255 for (i=0;i<num_procs-1;i++) {
256 if ((pids[i+1]=fork()) == 0) break;
259 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
260 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
262 fatal("db open failed");
266 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
270 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n",
271 num_procs, num_loops, hash_size, seed);
277 for (i=0;i<num_loops && error_count == 0;i++) {
281 if (error_count == 0) {
282 tdb_traverse_read(db, NULL, NULL);
283 tdb_traverse(db, traverse_fn, NULL);
284 tdb_traverse(db, traverse_fn, NULL);
289 if (getpid() != pids[0]) {
293 for (i=1;i<num_procs;i++) {
296 if (error_count != 0) {
297 /* try and stop the test on any failure */
298 for (j=1;j<num_procs;j++) {
300 kill(pids[j], SIGTERM);
304 pid = waitpid(-1, &status, 0);
306 perror("failed to wait for child\n");
309 for (j=1;j<num_procs;j++) {
310 if (pids[j] == pid) break;
312 if (j == num_procs) {
313 printf("unknown child %d exited!?\n", (int)pid);
316 if (WEXITSTATUS(status) != 0) {
317 printf("child %d exited with status %d\n",
318 (int)pid, WEXITSTATUS(status));
326 if (error_count == 0) {