1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
6 #include "system/time.h"
7 #include "system/wait.h"
8 #include "system/filesys.h"
16 #define REOPEN_PROB 30
20 #define TRANSACTION_PROB 10
21 #define TRANSACTION_PREPARE_PROB 2
22 #define LOCKSTORE_PROB 5
23 #define TRAVERSE_PROB 20
24 #define TRAVERSE_READ_PROB 20
29 static struct tdb_context *db;
30 static int in_transaction;
31 static int error_count;
32 static int always_transaction = 0;
34 #ifdef PRINTF_ATTRIBUTE
35 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
37 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
41 /* trace level messages do not indicate an error */
42 if (level != TDB_DEBUG_TRACE) {
47 vfprintf(stdout, format, ap);
53 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
60 static void fatal(const char *why)
66 static char *randbuf(int len)
70 buf = (char *)malloc(len+1);
73 buf[i] = 'a' + (rand() % 26);
79 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
83 if (random() % CULL_PROB == 0) {
90 static void addrec_db(void)
96 klen = 1 + (rand() % KEYLEN);
97 dlen = 1 + (rand() % DATALEN);
102 key.dptr = (unsigned char *)k;
105 data.dptr = (unsigned char *)d;
109 if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
116 if (in_transaction == 0 &&
117 (always_transaction || random() % TRANSACTION_PROB == 0)) {
118 if (tdb_transaction_start(db) != 0) {
119 fatal("tdb_transaction_start failed");
124 if (in_transaction && random() % TRANSACTION_PROB == 0) {
125 if (random() % TRANSACTION_PREPARE_PROB == 0) {
126 if (tdb_transaction_prepare_commit(db) != 0) {
127 fatal("tdb_transaction_prepare_commit failed");
130 if (tdb_transaction_commit(db) != 0) {
131 fatal("tdb_transaction_commit failed");
136 if (in_transaction && random() % TRANSACTION_PROB == 0) {
137 if (tdb_transaction_cancel(db) != 0) {
138 fatal("tdb_transaction_cancel failed");
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 [-t] [-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:th")) != -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);
246 always_transaction = 1;
253 unlink("torture.tdb");
255 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
258 for (i=0;i<num_procs-1;i++) {
259 if ((pids[i+1]=fork()) == 0) break;
262 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
263 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
265 fatal("db open failed");
269 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
273 printf("testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
274 num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
280 for (i=0;i<num_loops && error_count == 0;i++) {
284 if (error_count == 0) {
285 tdb_traverse_read(db, NULL, NULL);
286 if (always_transaction) {
287 while (in_transaction) {
288 tdb_transaction_cancel(db);
291 if (tdb_transaction_start(db) != 0)
292 fatal("tdb_transaction_start failed");
294 tdb_traverse(db, traverse_fn, NULL);
295 tdb_traverse(db, traverse_fn, NULL);
296 if (always_transaction) {
297 if (tdb_transaction_commit(db) != 0)
298 fatal("tdb_transaction_commit failed");
304 if (getpid() != pids[0]) {
308 for (i=1;i<num_procs;i++) {
311 if (error_count != 0) {
312 /* try and stop the test on any failure */
313 for (j=1;j<num_procs;j++) {
315 kill(pids[j], SIGTERM);
319 pid = waitpid(-1, &status, 0);
321 perror("failed to wait for child\n");
324 for (j=1;j<num_procs;j++) {
325 if (pids[j] == pid) break;
327 if (j == num_procs) {
328 printf("unknown child %d exited!?\n", (int)pid);
331 if (WEXITSTATUS(status) != 0) {
332 printf("child %d exited with status %d\n",
333 (int)pid, WEXITSTATUS(status));
341 if (error_count == 0) {