1 /* this tests ntdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
7 #include <ccan/err/err.h>
18 #include <sys/types.h>
24 //#define REOPEN_PROB 30
28 #define TRANSACTION_PROB 10
29 #define TRANSACTION_PREPARE_PROB 2
30 #define LOCKSTORE_PROB 5
31 #define TRAVERSE_PROB 20
32 #define TRAVERSE_MOD_PROB 100
33 #define TRAVERSE_ABORT_PROB 500
38 static struct ntdb_context
*db
;
39 static int in_transaction
;
40 static int in_traverse
;
41 static int error_count
;
43 static int always_transaction
= 0;
46 static int count_pipe
;
47 static union ntdb_attribute log_attr
;
48 static union ntdb_attribute seed_attr
;
49 static union ntdb_attribute hsize_attr
;
51 static void ntdb_log(struct ntdb_context
*ntdb
,
52 enum ntdb_log_level level
,
53 enum NTDB_ERROR ecode
,
57 printf("ntdb:%s:%s:%s\n",
58 ntdb_name(ntdb
), ntdb_errorstr(ecode
), message
);
63 signal(SIGUSR1
, SIG_IGN
);
64 sprintf(str
,"xterm -e gdb /proc/%u/exe %u", (unsigned int)getpid(), (unsigned int)getpid());
70 #include "../private.h"
72 static void segv_handler(int sig
, siginfo_t
*info
, void *p
)
76 sprintf(string
, "%u: death at %p (map_ptr %p, map_size %zu)\n",
77 (unsigned int)getpid(), info
->si_addr
, db
->file
->map_ptr
,
78 (size_t)db
->file
->map_size
);
79 if (write(2, string
, strlen(string
)) > 0)
84 static void warn_on_err(enum NTDB_ERROR e
, struct ntdb_context
*ntdb
,
87 if (e
!= NTDB_SUCCESS
) {
88 fprintf(stderr
, "%u:%s:%s\n", (unsigned int)getpid(), why
,
89 ntdb
? ntdb_errorstr(e
) : "(no ntdb)");
94 static char *randbuf(int len
)
98 buf
= (char *)malloc(len
+1);
100 for (i
=0;i
<len
;i
++) {
101 buf
[i
] = 'a' + (rand() % 26);
107 static void addrec_db(void);
108 static int modify_traverse(struct ntdb_context
*ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
,
112 if (random() % CULL_PROB
== 0) {
113 ntdb_delete(ntdb
, key
);
117 #if TRAVERSE_MOD_PROB
118 if (random() % TRAVERSE_MOD_PROB
== 0) {
123 #if TRAVERSE_ABORT_PROB
124 if (random() % TRAVERSE_ABORT_PROB
== 0)
131 static void addrec_db(void)
138 klen
= 1 + (rand() % KEYLEN
);
139 dlen
= 1 + (rand() % DATALEN
);
144 key
.dptr
= (unsigned char *)k
;
147 data
.dptr
= (unsigned char *)d
;
151 if (in_traverse
== 0 && in_transaction
== 0 && random() % REOPEN_PROB
== 0) {
158 if (in_traverse
== 0 && in_transaction
== 0 && (always_transaction
|| random() % TRANSACTION_PROB
== 0)) {
159 e
= ntdb_transaction_start(db
);
160 warn_on_err(e
, db
, "ntdb_transaction_start failed");
164 if (in_traverse
== 0 && in_transaction
&& random() % TRANSACTION_PROB
== 0) {
165 if (random() % TRANSACTION_PREPARE_PROB
== 0) {
166 e
= ntdb_transaction_prepare_commit(db
);
167 warn_on_err(e
, db
, "ntdb_transaction_prepare_commit failed");
169 e
= ntdb_transaction_commit(db
);
170 warn_on_err(e
, db
, "ntdb_transaction_commit failed");
175 if (in_traverse
== 0 && in_transaction
&& random() % TRANSACTION_PROB
== 0) {
176 ntdb_transaction_cancel(db
);
183 if (random() % DELETE_PROB
== 0) {
184 ntdb_delete(db
, key
);
190 if (random() % STORE_PROB
== 0) {
191 e
= ntdb_store(db
, key
, data
, NTDB_REPLACE
);
192 warn_on_err(e
, db
, "ntdb_store failed");
198 if (random() % APPEND_PROB
== 0) {
199 e
= ntdb_append(db
, key
, data
);
200 warn_on_err(e
, db
, "ntdb_append failed");
206 if (random() % LOCKSTORE_PROB
== 0) {
207 ntdb_chainlock(db
, key
);
208 if (ntdb_fetch(db
, key
, &data
) != NTDB_SUCCESS
) {
212 e
= ntdb_store(db
, key
, data
, NTDB_REPLACE
);
213 warn_on_err(e
, db
, "ntdb_store failed");
214 if (data
.dptr
) free(data
.dptr
);
215 ntdb_chainunlock(db
, key
);
221 /* FIXME: recursive traverses break transactions? */
222 if (in_traverse
== 0 && random() % TRAVERSE_PROB
== 0) {
224 ntdb_traverse(db
, modify_traverse
, NULL
);
230 if (ntdb_fetch(db
, key
, &data
) == NTDB_SUCCESS
)
238 static int traverse_fn(struct ntdb_context
*ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
,
241 ntdb_delete(ntdb
, key
);
245 static void usage(void)
247 printf("Usage: ntdbtorture"
251 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S] [-H HASH_SIZE]\n");
255 static void send_count_and_suicide(int sig
)
257 /* This ensures our successor can continue where we left off. */
258 if (write(count_pipe
, &loopnum
, sizeof(loopnum
)) != sizeof(loopnum
))
260 /* This gives a unique signature. */
261 kill(getpid(), SIGUSR2
);
264 static int run_child(const char *filename
, int i
, int seed
, unsigned num_loops
,
265 unsigned start
, int ntdb_flags
)
267 struct sigaction act
= { .sa_sigaction
= segv_handler
,
268 .sa_flags
= SA_SIGINFO
};
269 sigaction(11, &act
, NULL
);
271 db
= ntdb_open(filename
, ntdb_flags
, O_RDWR
| O_CREAT
, 0600,
274 fprintf(stderr
, "%u:%s:%s\n", (unsigned int)getpid(), filename
,
281 printf("pid %u\n", (unsigned int)getpid());
290 /* Set global, then we're ready to handle being killed. */
292 signal(SIGUSR1
, send_count_and_suicide
);
294 for (;loopnum
<num_loops
&& error_count
== 0;loopnum
++) {
298 if (error_count
== 0) {
301 ntdb_traverse(db
, NULL
, NULL
);
303 if (always_transaction
) {
304 while (in_transaction
) {
305 ntdb_transaction_cancel(db
);
308 e
= ntdb_transaction_start(db
);
311 "ntdb_transaction_start failed");
316 ntdb_traverse(db
, traverse_fn
, NULL
);
317 ntdb_traverse(db
, traverse_fn
, NULL
);
320 if (always_transaction
) {
321 e
= ntdb_transaction_commit(db
);
322 warn_on_err(e
, db
, "ntdb_transaction_commit failed");
329 return (error_count
< 100 ? error_count
: 100);
332 static char *test_path(const char *filename
)
334 const char *prefix
= getenv("TEST_DATA_PREFIX");
340 ret
= asprintf(&path
, "%s/%s", prefix
, filename
);
347 return strdup(filename
);
350 int main(int argc
, char * const *argv
)
353 int num_loops
= 5000;
360 int ntdb_flags
= NTDB_DEFAULT
;
364 log_attr
.base
.attr
= NTDB_ATTRIBUTE_LOG
;
365 log_attr
.base
.next
= &seed_attr
;
366 log_attr
.log
.fn
= ntdb_log
;
367 seed_attr
.base
.attr
= NTDB_ATTRIBUTE_SEED
;
368 seed_attr
.base
.next
= &hsize_attr
;
369 hsize_attr
.base
.attr
= NTDB_ATTRIBUTE_HASHSIZE
;
370 hsize_attr
.base
.next
= NULL
;
371 hsize_attr
.hashsize
.size
= 2; /* stress it by default. */
373 while ((c
= getopt(argc
, argv
, "n:l:s:thkSH:")) != -1) {
376 num_procs
= strtol(optarg
, NULL
, 0);
379 num_loops
= strtol(optarg
, NULL
, 0);
382 seed
= strtol(optarg
, NULL
, 0);
385 ntdb_flags
= NTDB_NOSYNC
;
389 always_transaction
= 1;
391 fprintf(stderr
, "Transactions not supported\n");
399 hsize_attr
.hashsize
.size
= strtol(optarg
, NULL
, 0);
406 test_ntdb
= test_path("torture.ntdb");
411 seed
= (getpid() + time(NULL
)) & 0x7FFFFFFF;
413 seed_attr
.seed
.seed
= (((uint64_t)seed
) << 32) | seed
;
415 if (num_procs
== 1 && !kill_random
) {
416 /* Don't fork for this case, makes debugging easier. */
417 error_count
= run_child(test_ntdb
, 0, seed
, num_loops
, 0,
422 pids
= (pid_t
*)calloc(sizeof(pid_t
), num_procs
);
423 done
= (int *)calloc(sizeof(int), num_procs
);
425 if (pipe(pfds
) != 0) {
426 perror("Creating pipe");
429 count_pipe
= pfds
[1];
431 for (i
=0;i
<num_procs
;i
++) {
432 if ((pids
[i
]=fork()) == 0) {
435 printf("testing with %d processes, %d loops, seed=%d%s\n",
436 num_procs
, num_loops
, seed
,
438 always_transaction
? " (all within transactions)" : ""
444 exit(run_child(test_ntdb
, i
, seed
, num_loops
, 0,
453 if (error_count
!= 0) {
454 /* try and stop the test on any failure */
455 for (j
=0;j
<num_procs
;j
++) {
457 kill(pids
[j
], SIGTERM
);
462 pid
= waitpid(-1, &status
, kill_random
? WNOHANG
: 0);
466 /* Sleep for 1/10 second. */
468 ts
.tv_nsec
= 100000000;
469 nanosleep(&ts
, NULL
);
472 kill(pids
[random() % num_procs
], SIGUSR1
);
477 perror("failed to wait for child\n");
481 for (j
=0;j
<num_procs
;j
++) {
482 if (pids
[j
] == pid
) break;
484 if (j
== num_procs
) {
485 printf("unknown child %d exited!?\n", (int)pid
);
488 if (WIFSIGNALED(status
)) {
489 if (WTERMSIG(status
) == SIGUSR2
490 || WTERMSIG(status
) == SIGUSR1
) {
491 /* SIGUSR2 means they wrote to pipe. */
492 if (WTERMSIG(status
) == SIGUSR2
) {
493 if (read(pfds
[0], &done
[j
],
497 "Short read from child?");
501 exit(run_child(test_ntdb
, j
, seed
,
504 printf("Restarting child %i for %u-%u\n",
505 j
, done
[j
], num_loops
);
508 printf("child %d exited with signal %d\n",
509 (int)pid
, WTERMSIG(status
));
512 if (WEXITSTATUS(status
) != 0) {
513 printf("child %d exited with status %d\n",
514 (int)pid
, WEXITSTATUS(status
));
518 memmove(&pids
[j
], &pids
[j
+1],
519 (num_procs
- j
- 1)*sizeof(pids
[0]));
526 if (error_count
== 0) {
527 db
= ntdb_open(test_ntdb
, NTDB_DEFAULT
, O_RDWR
| O_CREAT
,
530 fprintf(stderr
, "%u:%s:%s\n", (unsigned int)getpid(), test_ntdb
,
534 e
= ntdb_check(db
, NULL
, NULL
);
536 warn_on_err(e
, db
, "db check failed");