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
24 #define TRANSACTION_PROB 10
25 #define TRANSACTION_PREPARE_PROB 2
26 #define LOCKSTORE_PROB 5
28 #define TRAVERSE_PROB 20
29 #define TRAVERSE_READ_PROB 20
30 #define TRAVERSE_MOD_PROB 100
31 #define TRAVERSE_ABORT_PROB 500
36 static struct tdb_context *db;
37 static int in_transaction;
38 static int in_traverse;
39 static int error_count;
41 #ifdef PRINTF_ATTRIBUTE
42 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
44 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
51 vfprintf(stdout, format, ap);
57 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
64 static void fatal(const char *why)
70 static char *randbuf(int len)
74 buf = (char *)malloc(len+1);
77 buf[i] = 'a' + (rand() % 26);
83 static void addrec_db(void);
84 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
88 if (random() % CULL_PROB == 0) {
94 if (random() % TRAVERSE_MOD_PROB == 0) {
99 #if TRAVERSE_ABORT_PROB
100 if (random() % TRAVERSE_ABORT_PROB == 0)
107 static void addrec_db(void)
113 klen = 1 + (rand() % KEYLEN);
114 dlen = 1 + (rand() % DATALEN);
119 key.dptr = (unsigned char *)k;
122 data.dptr = (unsigned char *)d;
126 if (in_traverse == 0 && in_transaction == 0 && random() % TRANSACTION_PROB == 0) {
127 if (tdb_transaction_start(db) != 0) {
128 fatal("tdb_transaction_start failed");
133 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
135 if (random() % TRANSACTION_PREPARE_PROB == 0) {
136 if (tdb_transaction_prepare_commit(db) != 0) {
137 fatal("tdb_transaction_prepare_commit failed");
141 if (tdb_transaction_commit(db) != 0) {
142 fatal("tdb_transaction_commit failed");
147 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
148 if (tdb_transaction_cancel(db) != 0) {
149 fatal("tdb_transaction_cancel failed");
157 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
164 if (random() % DELETE_PROB == 0) {
171 if (random() % STORE_PROB == 0) {
172 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
173 fatal("tdb_store failed");
180 if (random() % APPEND_PROB == 0) {
181 if (tdb_append(db, key, data) != 0) {
182 fatal("tdb_append failed");
189 if (random() % LOCKSTORE_PROB == 0) {
190 tdb_chainlock(db, key);
191 data = tdb_fetch(db, key);
192 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
193 fatal("tdb_store failed");
195 if (data.dptr) free(data.dptr);
196 tdb_chainunlock(db, key);
202 /* FIXME: recursive traverses break transactions? */
203 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
205 tdb_traverse(db, modify_traverse, NULL);
211 #if TRAVERSE_READ_PROB
212 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
214 tdb_traverse_read(db, NULL, NULL);
220 data = tdb_fetch(db, key);
221 if (data.dptr) free(data.dptr);
228 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
231 tdb_delete(tdb, key);
235 static void usage(void)
237 printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
241 int main(int argc, char * const *argv)
245 int num_loops = 5000;
251 struct tdb_logging_context log_ctx;
252 log_ctx.log_fn = tdb_log;
254 while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
257 num_procs = strtol(optarg, NULL, 0);
260 num_loops = strtol(optarg, NULL, 0);
263 hash_size = strtol(optarg, NULL, 0);
266 seed = strtol(optarg, NULL, 0);
273 unlink("torture.tdb");
275 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
278 for (i=0;i<num_procs-1;i++) {
279 if ((pids[i+1]=fork()) == 0) break;
282 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
283 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
285 fatal("db open failed");
289 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
293 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n",
294 num_procs, num_loops, hash_size, seed);
300 for (i=0;i<num_loops && error_count == 0;i++) {
304 if (error_count == 0) {
305 tdb_traverse_read(db, NULL, NULL);
306 tdb_traverse(db, traverse_fn, NULL);
307 tdb_traverse(db, traverse_fn, NULL);
312 if (getpid() != pids[0]) {
316 for (i=1;i<num_procs;i++) {
319 if (error_count != 0) {
320 /* try and stop the test on any failure */
321 for (j=1;j<num_procs;j++) {
323 kill(pids[j], SIGTERM);
327 pid = waitpid(-1, &status, 0);
329 perror("failed to wait for child\n");
332 for (j=1;j<num_procs;j++) {
333 if (pids[j] == pid) break;
335 if (j == num_procs) {
336 printf("unknown child %d exited!?\n", (int)pid);
339 if (WEXITSTATUS(status) != 0) {
340 printf("child %d exited with status %d\n",
341 (int)pid, WEXITSTATUS(status));
349 if (error_count == 0) {