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
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");
149 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
150 if (tdb_transaction_cancel(db) != 0) {
151 fatal("tdb_transaction_cancel failed");
160 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
167 if (random() % DELETE_PROB == 0) {
174 if (random() % STORE_PROB == 0) {
175 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
176 fatal("tdb_store failed");
183 if (random() % APPEND_PROB == 0) {
184 if (tdb_append(db, key, data) != 0) {
185 fatal("tdb_append failed");
192 if (random() % LOCKSTORE_PROB == 0) {
193 tdb_chainlock(db, key);
194 data = tdb_fetch(db, key);
195 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
196 fatal("tdb_store failed");
198 if (data.dptr) free(data.dptr);
199 tdb_chainunlock(db, key);
205 /* FIXME: recursive traverses break transactions? */
206 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
208 tdb_traverse(db, modify_traverse, NULL);
214 #if TRAVERSE_READ_PROB
215 if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
217 tdb_traverse_read(db, NULL, NULL);
223 data = tdb_fetch(db, key);
224 if (data.dptr) free(data.dptr);
231 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
234 tdb_delete(tdb, key);
238 static void usage(void)
240 printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
244 int main(int argc, char * const *argv)
248 int num_loops = 5000;
254 struct tdb_logging_context log_ctx;
255 log_ctx.log_fn = tdb_log;
257 while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
260 num_procs = strtol(optarg, NULL, 0);
263 num_loops = strtol(optarg, NULL, 0);
266 hash_size = strtol(optarg, NULL, 0);
269 seed = strtol(optarg, NULL, 0);
276 unlink("torture.tdb");
278 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
281 for (i=0;i<num_procs-1;i++) {
282 if ((pids[i+1]=fork()) == 0) break;
285 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
286 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
288 fatal("db open failed");
292 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
296 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n",
297 num_procs, num_loops, hash_size, seed);
303 for (i=0;i<num_loops && error_count == 0;i++) {
307 if (error_count == 0) {
308 tdb_traverse_read(db, NULL, NULL);
309 tdb_traverse(db, traverse_fn, NULL);
310 tdb_traverse(db, traverse_fn, NULL);
315 if (getpid() != pids[0]) {
319 for (i=1;i<num_procs;i++) {
322 if (error_count != 0) {
323 /* try and stop the test on any failure */
324 for (j=1;j<num_procs;j++) {
326 kill(pids[j], SIGTERM);
330 pid = waitpid(-1, &status, 0);
332 perror("failed to wait for child\n");
335 for (j=1;j<num_procs;j++) {
336 if (pids[j] == pid) break;
338 if (j == num_procs) {
339 printf("unknown child %d exited!?\n", (int)pid);
342 if (WEXITSTATUS(status) != 0) {
343 printf("child %d exited with status %d\n",
344 (int)pid, WEXITSTATUS(status));
352 if (error_count == 0) {