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 LOCKSTORE_PROB 5
22 #define TRAVERSE_PROB 20
23 #define TRAVERSE_READ_PROB 20
28 static struct tdb_context *db;
29 static int in_transaction;
30 static int error_count;
31 static int always_transaction = 0;
33 #ifdef PRINTF_ATTRIBUTE
34 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
36 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
40 /* trace level messages do not indicate an error */
41 if (level != TDB_DEBUG_TRACE) {
46 vfprintf(stdout, format, ap);
52 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
59 static void fatal(const char *why)
65 static char *randbuf(int len)
69 buf = (char *)malloc(len+1);
72 buf[i] = 'a' + (rand() % 26);
78 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
82 if (random() % CULL_PROB == 0) {
89 static void addrec_db(void)
95 klen = 1 + (rand() % KEYLEN);
96 dlen = 1 + (rand() % DATALEN);
101 key.dptr = (unsigned char *)k;
104 data.dptr = (unsigned char *)d;
108 if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
115 if (in_transaction == 0 &&
116 (always_transaction || random() % TRANSACTION_PROB == 0)) {
117 if (tdb_transaction_start(db) != 0) {
118 fatal("tdb_transaction_start failed");
123 if (in_transaction && random() % TRANSACTION_PROB == 0) {
124 if (tdb_transaction_commit(db) != 0) {
125 fatal("tdb_transaction_commit failed");
130 if (in_transaction && random() % TRANSACTION_PROB == 0) {
131 if (tdb_transaction_cancel(db) != 0) {
132 fatal("tdb_transaction_cancel failed");
140 if (random() % DELETE_PROB == 0) {
147 if (random() % STORE_PROB == 0) {
148 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
149 fatal("tdb_store failed");
156 if (random() % APPEND_PROB == 0) {
157 if (tdb_append(db, key, data) != 0) {
158 fatal("tdb_append failed");
165 if (random() % LOCKSTORE_PROB == 0) {
166 tdb_chainlock(db, key);
167 data = tdb_fetch(db, key);
168 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
169 fatal("tdb_store failed");
171 if (data.dptr) free(data.dptr);
172 tdb_chainunlock(db, key);
178 if (random() % TRAVERSE_PROB == 0) {
179 tdb_traverse(db, cull_traverse, NULL);
184 #if TRAVERSE_READ_PROB
185 if (random() % TRAVERSE_READ_PROB == 0) {
186 tdb_traverse_read(db, NULL, NULL);
191 data = tdb_fetch(db, key);
192 if (data.dptr) free(data.dptr);
199 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
202 tdb_delete(tdb, key);
206 static void usage(void)
208 printf("Usage: tdbtorture [-t] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
212 int main(int argc, char * const *argv)
216 int num_loops = 5000;
222 struct tdb_logging_context log_ctx;
223 log_ctx.log_fn = tdb_log;
225 while ((c = getopt(argc, argv, "n:l:s:H:th")) != -1) {
228 num_procs = strtol(optarg, NULL, 0);
231 num_loops = strtol(optarg, NULL, 0);
234 hash_size = strtol(optarg, NULL, 0);
237 seed = strtol(optarg, NULL, 0);
240 always_transaction = 1;
247 unlink("torture.tdb");
249 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
252 for (i=0;i<num_procs-1;i++) {
253 if ((pids[i+1]=fork()) == 0) break;
256 db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST,
257 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
259 fatal("db open failed");
263 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
267 printf("testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
268 num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
274 for (i=0;i<num_loops && error_count == 0;i++) {
278 if (error_count == 0) {
279 tdb_traverse_read(db, NULL, NULL);
280 if (always_transaction) {
281 while (in_transaction) {
282 tdb_transaction_cancel(db);
285 if (tdb_transaction_start(db) != 0)
286 fatal("tdb_transaction_start failed");
288 tdb_traverse(db, traverse_fn, NULL);
289 tdb_traverse(db, traverse_fn, NULL);
290 if (always_transaction) {
291 if (tdb_transaction_commit(db) != 0)
292 fatal("tdb_transaction_commit failed");
298 if (getpid() != pids[0]) {
302 for (i=1;i<num_procs;i++) {
305 if (error_count != 0) {
306 /* try and stop the test on any failure */
307 for (j=1;j<num_procs;j++) {
309 kill(pids[j], SIGTERM);
313 pid = waitpid(-1, &status, 0);
315 perror("failed to wait for child\n");
318 for (j=1;j<num_procs;j++) {
319 if (pids[j] == pid) break;
321 if (j == num_procs) {
322 printf("unknown child %d exited!?\n", (int)pid);
325 if (WEXITSTATUS(status) != 0) {
326 printf("child %d exited with status %d\n",
327 (int)pid, WEXITSTATUS(status));
333 if (error_count == 0) {