1 /* this tests ntdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
8 #include <ccan/err/err.h>
10 //#define REOPEN_PROB 30
14 #define TRANSACTION_PROB 10
15 #define TRANSACTION_PREPARE_PROB 2
16 #define LOCKSTORE_PROB 5
17 #define TRAVERSE_PROB 20
18 #define TRAVERSE_MOD_PROB 100
19 #define TRAVERSE_ABORT_PROB 500
24 static struct ntdb_context *db;
25 static int in_transaction;
26 static int in_traverse;
27 static int error_count;
29 static int always_transaction = 0;
32 static int count_pipe;
33 static union ntdb_attribute log_attr;
34 static union ntdb_attribute seed_attr;
35 static union ntdb_attribute hsize_attr;
37 static void ntdb_log(struct ntdb_context *ntdb,
38 enum ntdb_log_level level,
39 enum NTDB_ERROR ecode,
43 printf("ntdb:%s:%s:%s\n",
44 ntdb_name(ntdb), ntdb_errorstr(ecode), message);
49 signal(SIGUSR1, SIG_IGN);
50 sprintf(str,"xterm -e gdb /proc/%u/exe %u", (unsigned int)getpid(), (unsigned int)getpid());
56 #include "../private.h"
58 static void segv_handler(int sig, siginfo_t *info, void *p)
62 sprintf(string, "%u: death at %p (map_ptr %p, map_size %zu)\n",
63 (unsigned int)getpid(), info->si_addr, db->file->map_ptr,
64 (size_t)db->file->map_size);
65 if (write(2, string, strlen(string)) > 0)
70 static void warn_on_err(enum NTDB_ERROR e, struct ntdb_context *ntdb,
73 if (e != NTDB_SUCCESS) {
74 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), why,
75 ntdb ? ntdb_errorstr(e) : "(no ntdb)");
80 static char *randbuf(int len)
84 buf = (char *)malloc(len+1);
86 perror("randbuf: unable to allocate memory for buffer.\n");
91 buf[i] = 'a' + (rand() % 26);
97 static void addrec_db(void);
98 static int modify_traverse(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
102 if (random() % CULL_PROB == 0) {
103 ntdb_delete(ntdb, key);
107 #if TRAVERSE_MOD_PROB
108 if (random() % TRAVERSE_MOD_PROB == 0) {
113 #if TRAVERSE_ABORT_PROB
114 if (random() % TRAVERSE_ABORT_PROB == 0)
121 static void addrec_db(void)
128 klen = 1 + (rand() % KEYLEN);
129 dlen = 1 + (rand() % DATALEN);
134 key.dptr = (unsigned char *)k;
137 data.dptr = (unsigned char *)d;
141 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
148 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
149 e = ntdb_transaction_start(db);
150 warn_on_err(e, db, "ntdb_transaction_start failed");
154 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
155 if (random() % TRANSACTION_PREPARE_PROB == 0) {
156 e = ntdb_transaction_prepare_commit(db);
157 warn_on_err(e, db, "ntdb_transaction_prepare_commit failed");
159 e = ntdb_transaction_commit(db);
160 warn_on_err(e, db, "ntdb_transaction_commit failed");
165 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
166 ntdb_transaction_cancel(db);
173 if (random() % DELETE_PROB == 0) {
174 ntdb_delete(db, key);
180 if (random() % STORE_PROB == 0) {
181 e = ntdb_store(db, key, data, NTDB_REPLACE);
182 warn_on_err(e, db, "ntdb_store failed");
188 if (random() % APPEND_PROB == 0) {
189 e = ntdb_append(db, key, data);
190 warn_on_err(e, db, "ntdb_append failed");
196 if (random() % LOCKSTORE_PROB == 0) {
197 ntdb_chainlock(db, key);
198 if (ntdb_fetch(db, key, &data) != NTDB_SUCCESS) {
202 e = ntdb_store(db, key, data, NTDB_REPLACE);
203 warn_on_err(e, db, "ntdb_store failed");
204 if (data.dptr) free(data.dptr);
205 ntdb_chainunlock(db, key);
211 /* FIXME: recursive traverses break transactions? */
212 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
214 ntdb_traverse(db, modify_traverse, NULL);
220 if (ntdb_fetch(db, key, &data) == NTDB_SUCCESS)
228 static int traverse_fn(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
231 ntdb_delete(ntdb, key);
235 static void usage(void)
237 printf("Usage: ntdbtorture"
241 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S] [-H HASH_SIZE]\n");
245 static void send_count_and_suicide(int sig)
247 /* This ensures our successor can continue where we left off. */
248 if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
250 /* This gives a unique signature. */
251 kill(getpid(), SIGUSR2);
254 static int run_child(const char *filename, int i, int seed, unsigned num_loops,
255 unsigned start, int ntdb_flags)
257 struct sigaction act = { .sa_sigaction = segv_handler,
258 .sa_flags = SA_SIGINFO };
259 sigaction(11, &act, NULL);
261 db = ntdb_open(filename, ntdb_flags, O_RDWR | O_CREAT, 0600,
264 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), filename,
271 printf("pid %u\n", (unsigned int)getpid());
280 /* Set global, then we're ready to handle being killed. */
282 signal(SIGUSR1, send_count_and_suicide);
284 for (;loopnum<num_loops && error_count == 0;loopnum++) {
288 if (error_count == 0) {
291 ntdb_traverse(db, NULL, NULL);
293 if (always_transaction) {
294 while (in_transaction) {
295 ntdb_transaction_cancel(db);
298 e = ntdb_transaction_start(db);
301 "ntdb_transaction_start failed");
306 ntdb_traverse(db, traverse_fn, NULL);
307 ntdb_traverse(db, traverse_fn, NULL);
310 if (always_transaction) {
311 e = ntdb_transaction_commit(db);
312 warn_on_err(e, db, "ntdb_transaction_commit failed");
319 return (error_count < 100 ? error_count : 100);
322 static char *test_path(const char *filename)
324 const char *prefix = getenv("TEST_DATA_PREFIX");
330 ret = asprintf(&path, "%s/%s", prefix, filename);
337 return strdup(filename);
340 int main(int argc, char * const *argv)
343 int num_loops = 5000;
350 int ntdb_flags = NTDB_DEFAULT;
354 log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
355 log_attr.base.next = &seed_attr;
356 log_attr.log.fn = ntdb_log;
357 seed_attr.base.attr = NTDB_ATTRIBUTE_SEED;
358 seed_attr.base.next = &hsize_attr;
359 hsize_attr.base.attr = NTDB_ATTRIBUTE_HASHSIZE;
360 hsize_attr.base.next = NULL;
361 hsize_attr.hashsize.size = 2; /* stress it by default. */
363 while ((c = getopt(argc, argv, "n:l:s:thkSH:")) != -1) {
366 num_procs = strtol(optarg, NULL, 0);
369 num_loops = strtol(optarg, NULL, 0);
372 seed = strtol(optarg, NULL, 0);
375 ntdb_flags = NTDB_NOSYNC;
379 always_transaction = 1;
381 fprintf(stderr, "Transactions not supported\n");
389 hsize_attr.hashsize.size = strtol(optarg, NULL, 0);
396 test_ntdb = test_path("torture.ntdb");
401 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
403 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
405 if (num_procs == 1 && !kill_random) {
406 /* Don't fork for this case, makes debugging easier. */
407 error_count = run_child(test_ntdb, 0, seed, num_loops, 0,
412 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
413 done = (int *)calloc(sizeof(int), num_procs);
415 if (pipe(pfds) != 0) {
416 perror("Creating pipe");
419 count_pipe = pfds[1];
421 for (i=0;i<num_procs;i++) {
422 if ((pids[i]=fork()) == 0) {
425 printf("testing with %d processes, %d loops, seed=%d%s\n",
426 num_procs, num_loops, seed,
428 always_transaction ? " (all within transactions)" : ""
434 exit(run_child(test_ntdb, i, seed, num_loops, 0,
443 if (error_count != 0) {
444 /* try and stop the test on any failure */
445 for (j=0;j<num_procs;j++) {
447 kill(pids[j], SIGTERM);
452 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
456 /* Sleep for 1/10 second. */
458 ts.tv_nsec = 100000000;
459 nanosleep(&ts, NULL);
462 kill(pids[random() % num_procs], SIGUSR1);
467 perror("failed to wait for child\n");
471 for (j=0;j<num_procs;j++) {
472 if (pids[j] == pid) break;
474 if (j == num_procs) {
475 printf("unknown child %d exited!?\n", (int)pid);
478 if (WIFSIGNALED(status)) {
479 if (WTERMSIG(status) == SIGUSR2
480 || WTERMSIG(status) == SIGUSR1) {
481 /* SIGUSR2 means they wrote to pipe. */
482 if (WTERMSIG(status) == SIGUSR2) {
483 if (read(pfds[0], &done[j],
487 "Short read from child?");
491 exit(run_child(test_ntdb, j, seed,
494 printf("Restarting child %i for %u-%u\n",
495 j, done[j], num_loops);
498 printf("child %d exited with signal %d\n",
499 (int)pid, WTERMSIG(status));
502 if (WEXITSTATUS(status) != 0) {
503 printf("child %d exited with status %d\n",
504 (int)pid, WEXITSTATUS(status));
508 memmove(&pids[j], &pids[j+1],
509 (num_procs - j - 1)*sizeof(pids[0]));
516 if (error_count == 0) {
517 db = ntdb_open(test_ntdb, NTDB_DEFAULT, O_RDWR | O_CREAT,
520 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), test_ntdb,
524 e = ntdb_check(db, NULL, NULL);
526 warn_on_err(e, db, "db check failed");