1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
25 #include "lib/tdb/include/tdb.h"
26 #include "system/time.h"
27 #include "system/wait.h"
28 #include "system/filesys.h"
37 #define REOPEN_PROB 30
41 #define TRANSACTION_PROB 10
42 #define LOCKSTORE_PROB 5
43 #define TRAVERSE_PROB 20
44 #define TRAVERSE_READ_PROB 20
49 static struct tdb_context *db;
50 static int in_transaction;
51 static int error_count;
53 #ifdef PRINTF_ATTRIBUTE
54 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
56 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
63 vfprintf(stdout, format, ap);
69 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
76 static void fatal(const char *why)
82 static char *randbuf(int len)
86 buf = (char *)malloc(len+1);
89 buf[i] = 'a' + (rand() % 26);
95 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
99 if (random() % CULL_PROB == 0) {
100 tdb_delete(tdb, key);
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_transaction == 0 && random() % TRANSACTION_PROB == 0) {
126 if (tdb_transaction_start(db) != 0) {
127 fatal("tdb_transaction_start failed");
132 if (in_transaction && random() % TRANSACTION_PROB == 0) {
133 if (tdb_transaction_commit(db) != 0) {
134 fatal("tdb_transaction_commit failed");
139 if (in_transaction && random() % TRANSACTION_PROB == 0) {
140 if (tdb_transaction_cancel(db) != 0) {
141 fatal("tdb_transaction_cancel failed");
149 if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
156 if (random() % DELETE_PROB == 0) {
163 if (random() % STORE_PROB == 0) {
164 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
165 fatal("tdb_store failed");
172 if (random() % APPEND_PROB == 0) {
173 if (tdb_append(db, key, data) != 0) {
174 fatal("tdb_append failed");
181 if (random() % LOCKSTORE_PROB == 0) {
182 tdb_chainlock(db, key);
183 data = tdb_fetch(db, key);
184 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
185 fatal("tdb_store failed");
187 if (data.dptr) free(data.dptr);
188 tdb_chainunlock(db, key);
194 if (random() % TRAVERSE_PROB == 0) {
195 tdb_traverse(db, cull_traverse, NULL);
200 #if TRAVERSE_READ_PROB
201 if (random() % TRAVERSE_READ_PROB == 0) {
202 tdb_traverse_read(db, NULL, NULL);
207 data = tdb_fetch(db, key);
208 if (data.dptr) free(data.dptr);
215 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
218 tdb_delete(tdb, key);
222 static void usage(void)
224 printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
228 int main(int argc, char * const *argv)
232 int num_loops = 5000;
238 struct tdb_logging_context log_ctx;
239 log_ctx.log_fn = tdb_log;
241 while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
244 num_procs = strtol(optarg, NULL, 0);
247 num_loops = strtol(optarg, NULL, 0);
250 hash_size = strtol(optarg, NULL, 0);
253 seed = strtol(optarg, NULL, 0);
260 unlink("torture.tdb");
262 pids = calloc(sizeof(pid_t), num_procs);
265 for (i=0;i<num_procs-1;i++) {
266 if ((pids[i+1]=fork()) == 0) break;
269 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
270 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
272 fatal("db open failed");
276 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
280 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n",
281 num_procs, num_loops, hash_size, seed);
287 for (i=0;i<num_loops && error_count == 0;i++) {
291 if (error_count == 0) {
292 tdb_traverse_read(db, NULL, NULL);
293 tdb_traverse(db, traverse_fn, NULL);
294 tdb_traverse(db, traverse_fn, NULL);
299 if (getpid() != pids[0]) {
303 for (i=1;i<num_procs;i++) {
306 if (error_count != 0) {
307 /* try and stop the test on any failure */
308 for (j=1;j<num_procs;j++) {
310 kill(pids[j], SIGTERM);
314 pid = waitpid(-1, &status, 0);
316 perror("failed to wait for child\n");
319 for (j=1;j<num_procs;j++) {
320 if (pids[j] == pid) break;
322 if (j == num_procs) {
323 printf("unknown child %d exited!?\n", (int)pid);
326 if (WEXITSTATUS(status) != 0) {
327 printf("child %d exited with status %d\n",
328 (int)pid, WEXITSTATUS(status));
334 if (error_count == 0) {