1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
26 #include "lib/tdb/include/tdb.h"
27 #include "system/time.h"
28 #include "system/wait.h"
29 #include "system/filesys.h"
35 #define REOPEN_PROB 30
39 #define TRANSACTION_PROB 10
40 #define LOCKSTORE_PROB 5
41 #define TRAVERSE_PROB 20
47 static struct tdb_context *db;
48 static int in_transaction;
50 #ifdef PRINTF_ATTRIBUTE
51 static void tdb_log(struct tdb_context *tdb, int level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
53 static void tdb_log(struct tdb_context *tdb, int level, const char *format, ...)
58 vfprintf(stdout, format, ap);
64 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
71 static void fatal(const char *why)
77 static char *randbuf(int len)
81 buf = (char *)malloc(len+1);
84 buf[i] = 'a' + (rand() % 26);
90 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
94 if (random() % CULL_PROB == 0) {
101 static void addrec_db(void)
107 klen = 1 + (rand() % KEYLEN);
108 dlen = 1 + (rand() % DATALEN);
113 key.dptr = (unsigned char *)k;
116 data.dptr = (unsigned char *)d;
120 if (in_transaction == 0 && random() % TRANSACTION_PROB == 0) {
121 if (tdb_transaction_start(db) != 0) {
122 fatal("tdb_transaction_start failed");
127 if (in_transaction && random() % TRANSACTION_PROB == 0) {
128 if (tdb_transaction_commit(db) != 0) {
129 fatal("tdb_transaction_commit failed");
134 if (in_transaction && random() % TRANSACTION_PROB == 0) {
135 if (tdb_transaction_cancel(db) != 0) {
136 fatal("tdb_transaction_cancel failed");
144 if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
151 if (random() % DELETE_PROB == 0) {
158 if (random() % STORE_PROB == 0) {
159 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
160 fatal("tdb_store failed");
167 if (random() % APPEND_PROB == 0) {
168 if (tdb_append(db, key, data) != 0) {
169 fatal("tdb_append failed");
176 if (random() % LOCKSTORE_PROB == 0) {
177 tdb_chainlock(db, key);
178 data = tdb_fetch(db, key);
179 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
180 fatal("tdb_store failed");
182 if (data.dptr) free(data.dptr);
183 tdb_chainunlock(db, key);
189 if (random() % TRAVERSE_PROB == 0) {
190 tdb_traverse(db, cull_traverse, NULL);
195 data = tdb_fetch(db, key);
196 if (data.dptr) free(data.dptr);
203 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
206 tdb_delete(tdb, key);
210 static void usage(void)
212 printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
216 int main(int argc, char * const *argv)
220 int num_loops = 5000;
226 while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
229 num_procs = strtol(optarg, NULL, 0);
232 num_loops = strtol(optarg, NULL, 0);
235 hash_size = strtol(optarg, NULL, 0);
238 seed = strtol(optarg, NULL, 0);
245 unlink("torture.tdb");
247 pids = calloc(sizeof(pid_t), num_procs);
250 for (i=0;i<num_procs-1;i++) {
251 if ((pids[i+1]=fork()) == 0) break;
254 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
255 O_RDWR | O_CREAT, 0600, tdb_log, NULL);
257 fatal("db open failed");
261 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
265 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n",
266 num_procs, num_loops, hash_size, seed);
272 for (i=0;i<num_loops;i++) {
276 tdb_traverse(db, NULL, NULL);
277 tdb_traverse(db, traverse_fn, NULL);
278 tdb_traverse(db, traverse_fn, NULL);
282 if (getpid() == pids[0]) {
283 for (i=0;i<num_procs-1;i++) {
285 if (waitpid(pids[i+1], &status, 0) != pids[i+1]) {
286 printf("failed to wait for %d\n",
290 if (WEXITSTATUS(status) != 0) {
291 printf("child %d exited with status %d\n",
292 (int)pids[i+1], WEXITSTATUS(status));