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 perror("randbuf: unable to allocate memory for buffer.\n");
104 for (i
=0;i
<len
;i
++) {
105 buf
[i
] = 'a' + (rand() % 26);
111 static void addrec_db(void);
112 static int modify_traverse(struct ntdb_context
*ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
,
116 if (random() % CULL_PROB
== 0) {
117 ntdb_delete(ntdb
, key
);
121 #if TRAVERSE_MOD_PROB
122 if (random() % TRAVERSE_MOD_PROB
== 0) {
127 #if TRAVERSE_ABORT_PROB
128 if (random() % TRAVERSE_ABORT_PROB
== 0)
135 static void addrec_db(void)
142 klen
= 1 + (rand() % KEYLEN
);
143 dlen
= 1 + (rand() % DATALEN
);
148 key
.dptr
= (unsigned char *)k
;
151 data
.dptr
= (unsigned char *)d
;
155 if (in_traverse
== 0 && in_transaction
== 0 && random() % REOPEN_PROB
== 0) {
162 if (in_traverse
== 0 && in_transaction
== 0 && (always_transaction
|| random() % TRANSACTION_PROB
== 0)) {
163 e
= ntdb_transaction_start(db
);
164 warn_on_err(e
, db
, "ntdb_transaction_start failed");
168 if (in_traverse
== 0 && in_transaction
&& random() % TRANSACTION_PROB
== 0) {
169 if (random() % TRANSACTION_PREPARE_PROB
== 0) {
170 e
= ntdb_transaction_prepare_commit(db
);
171 warn_on_err(e
, db
, "ntdb_transaction_prepare_commit failed");
173 e
= ntdb_transaction_commit(db
);
174 warn_on_err(e
, db
, "ntdb_transaction_commit failed");
179 if (in_traverse
== 0 && in_transaction
&& random() % TRANSACTION_PROB
== 0) {
180 ntdb_transaction_cancel(db
);
187 if (random() % DELETE_PROB
== 0) {
188 ntdb_delete(db
, key
);
194 if (random() % STORE_PROB
== 0) {
195 e
= ntdb_store(db
, key
, data
, NTDB_REPLACE
);
196 warn_on_err(e
, db
, "ntdb_store failed");
202 if (random() % APPEND_PROB
== 0) {
203 e
= ntdb_append(db
, key
, data
);
204 warn_on_err(e
, db
, "ntdb_append failed");
210 if (random() % LOCKSTORE_PROB
== 0) {
211 ntdb_chainlock(db
, key
);
212 if (ntdb_fetch(db
, key
, &data
) != NTDB_SUCCESS
) {
216 e
= ntdb_store(db
, key
, data
, NTDB_REPLACE
);
217 warn_on_err(e
, db
, "ntdb_store failed");
218 if (data
.dptr
) free(data
.dptr
);
219 ntdb_chainunlock(db
, key
);
225 /* FIXME: recursive traverses break transactions? */
226 if (in_traverse
== 0 && random() % TRAVERSE_PROB
== 0) {
228 ntdb_traverse(db
, modify_traverse
, NULL
);
234 if (ntdb_fetch(db
, key
, &data
) == NTDB_SUCCESS
)
242 static int traverse_fn(struct ntdb_context
*ntdb
, NTDB_DATA key
, NTDB_DATA dbuf
,
245 ntdb_delete(ntdb
, key
);
249 static void usage(void)
251 printf("Usage: ntdbtorture"
255 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S] [-H HASH_SIZE]\n");
259 static void send_count_and_suicide(int sig
)
261 /* This ensures our successor can continue where we left off. */
262 if (write(count_pipe
, &loopnum
, sizeof(loopnum
)) != sizeof(loopnum
))
264 /* This gives a unique signature. */
265 kill(getpid(), SIGUSR2
);
268 static int run_child(const char *filename
, int i
, int seed
, unsigned num_loops
,
269 unsigned start
, int ntdb_flags
)
271 struct sigaction act
= { .sa_sigaction
= segv_handler
,
272 .sa_flags
= SA_SIGINFO
};
273 sigaction(11, &act
, NULL
);
275 db
= ntdb_open(filename
, ntdb_flags
, O_RDWR
| O_CREAT
, 0600,
278 fprintf(stderr
, "%u:%s:%s\n", (unsigned int)getpid(), filename
,
285 printf("pid %u\n", (unsigned int)getpid());
294 /* Set global, then we're ready to handle being killed. */
296 signal(SIGUSR1
, send_count_and_suicide
);
298 for (;loopnum
<num_loops
&& error_count
== 0;loopnum
++) {
302 if (error_count
== 0) {
305 ntdb_traverse(db
, NULL
, NULL
);
307 if (always_transaction
) {
308 while (in_transaction
) {
309 ntdb_transaction_cancel(db
);
312 e
= ntdb_transaction_start(db
);
315 "ntdb_transaction_start failed");
320 ntdb_traverse(db
, traverse_fn
, NULL
);
321 ntdb_traverse(db
, traverse_fn
, NULL
);
324 if (always_transaction
) {
325 e
= ntdb_transaction_commit(db
);
326 warn_on_err(e
, db
, "ntdb_transaction_commit failed");
333 return (error_count
< 100 ? error_count
: 100);
336 static char *test_path(const char *filename
)
338 const char *prefix
= getenv("TEST_DATA_PREFIX");
344 ret
= asprintf(&path
, "%s/%s", prefix
, filename
);
351 return strdup(filename
);
354 int main(int argc
, char * const *argv
)
357 int num_loops
= 5000;
364 int ntdb_flags
= NTDB_DEFAULT
;
368 log_attr
.base
.attr
= NTDB_ATTRIBUTE_LOG
;
369 log_attr
.base
.next
= &seed_attr
;
370 log_attr
.log
.fn
= ntdb_log
;
371 seed_attr
.base
.attr
= NTDB_ATTRIBUTE_SEED
;
372 seed_attr
.base
.next
= &hsize_attr
;
373 hsize_attr
.base
.attr
= NTDB_ATTRIBUTE_HASHSIZE
;
374 hsize_attr
.base
.next
= NULL
;
375 hsize_attr
.hashsize
.size
= 2; /* stress it by default. */
377 while ((c
= getopt(argc
, argv
, "n:l:s:thkSH:")) != -1) {
380 num_procs
= strtol(optarg
, NULL
, 0);
383 num_loops
= strtol(optarg
, NULL
, 0);
386 seed
= strtol(optarg
, NULL
, 0);
389 ntdb_flags
= NTDB_NOSYNC
;
393 always_transaction
= 1;
395 fprintf(stderr
, "Transactions not supported\n");
403 hsize_attr
.hashsize
.size
= strtol(optarg
, NULL
, 0);
410 test_ntdb
= test_path("torture.ntdb");
415 seed
= (getpid() + time(NULL
)) & 0x7FFFFFFF;
417 seed_attr
.seed
.seed
= (((uint64_t)seed
) << 32) | seed
;
419 if (num_procs
== 1 && !kill_random
) {
420 /* Don't fork for this case, makes debugging easier. */
421 error_count
= run_child(test_ntdb
, 0, seed
, num_loops
, 0,
426 pids
= (pid_t
*)calloc(sizeof(pid_t
), num_procs
);
427 done
= (int *)calloc(sizeof(int), num_procs
);
429 if (pipe(pfds
) != 0) {
430 perror("Creating pipe");
433 count_pipe
= pfds
[1];
435 for (i
=0;i
<num_procs
;i
++) {
436 if ((pids
[i
]=fork()) == 0) {
439 printf("testing with %d processes, %d loops, seed=%d%s\n",
440 num_procs
, num_loops
, seed
,
442 always_transaction
? " (all within transactions)" : ""
448 exit(run_child(test_ntdb
, i
, seed
, num_loops
, 0,
457 if (error_count
!= 0) {
458 /* try and stop the test on any failure */
459 for (j
=0;j
<num_procs
;j
++) {
461 kill(pids
[j
], SIGTERM
);
466 pid
= waitpid(-1, &status
, kill_random
? WNOHANG
: 0);
470 /* Sleep for 1/10 second. */
472 ts
.tv_nsec
= 100000000;
473 nanosleep(&ts
, NULL
);
476 kill(pids
[random() % num_procs
], SIGUSR1
);
481 perror("failed to wait for child\n");
485 for (j
=0;j
<num_procs
;j
++) {
486 if (pids
[j
] == pid
) break;
488 if (j
== num_procs
) {
489 printf("unknown child %d exited!?\n", (int)pid
);
492 if (WIFSIGNALED(status
)) {
493 if (WTERMSIG(status
) == SIGUSR2
494 || WTERMSIG(status
) == SIGUSR1
) {
495 /* SIGUSR2 means they wrote to pipe. */
496 if (WTERMSIG(status
) == SIGUSR2
) {
497 if (read(pfds
[0], &done
[j
],
501 "Short read from child?");
505 exit(run_child(test_ntdb
, j
, seed
,
508 printf("Restarting child %i for %u-%u\n",
509 j
, done
[j
], num_loops
);
512 printf("child %d exited with signal %d\n",
513 (int)pid
, WTERMSIG(status
));
516 if (WEXITSTATUS(status
) != 0) {
517 printf("child %d exited with status %d\n",
518 (int)pid
, WEXITSTATUS(status
));
522 memmove(&pids
[j
], &pids
[j
+1],
523 (num_procs
- j
- 1)*sizeof(pids
[0]));
530 if (error_count
== 0) {
531 db
= ntdb_open(test_ntdb
, NTDB_DEFAULT
, O_RDWR
| O_CREAT
,
534 fprintf(stderr
, "%u:%s:%s\n", (unsigned int)getpid(), test_ntdb
,
538 e
= ntdb_check(db
, NULL
, NULL
);
540 warn_on_err(e
, db
, "db check failed");