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;
33 static int hash_size
= 2;
34 static unsigned loopnum
;
35 static int count_pipe
;
36 static bool mutex
= false;
37 static struct tdb_logging_context log_ctx
;
39 #ifdef PRINTF_ATTRIBUTE
40 static void tdb_log(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
42 static void tdb_log(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *format
, ...)
46 /* trace level messages do not indicate an error */
47 if (level
!= TDB_DEBUG_TRACE
) {
52 vfprintf(stdout
, format
, ap
);
56 if (level
!= TDB_DEBUG_TRACE
) {
58 signal(SIGUSR1
, SIG_IGN
);
59 asprintf(&ptr
,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
66 static void fatal(const char *why
)
72 static char *randbuf(int len
)
76 buf
= (char *)malloc(len
+1);
79 buf
[i
] = 'a' + (rand() % 26);
85 static int cull_traverse(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
89 if (random() % CULL_PROB
== 0) {
96 static bool do_transaction(void)
102 if (random() % TRANSACTION_PROB
== 0) {
109 static void addrec_db(void)
115 klen
= 1 + (rand() % KEYLEN
);
116 dlen
= 1 + (rand() % DATALEN
);
121 key
.dptr
= (unsigned char *)k
;
124 data
.dptr
= (unsigned char *)d
;
128 if (in_transaction
== 0 && random() % REOPEN_PROB
== 0) {
134 if (in_transaction
== 0 &&
135 (always_transaction
|| do_transaction())) {
136 if (tdb_transaction_start(db
) != 0) {
137 fatal("tdb_transaction_start failed");
142 if (in_transaction
&& do_transaction()) {
143 if (random() % TRANSACTION_PREPARE_PROB
== 0) {
144 if (tdb_transaction_prepare_commit(db
) != 0) {
145 fatal("tdb_transaction_prepare_commit failed");
148 if (tdb_transaction_commit(db
) != 0) {
149 fatal("tdb_transaction_commit failed");
154 if (in_transaction
&& do_transaction()) {
155 if (tdb_transaction_cancel(db
) != 0) {
156 fatal("tdb_transaction_cancel failed");
163 if (random() % DELETE_PROB
== 0) {
170 if (random() % STORE_PROB
== 0) {
171 if (tdb_store(db
, key
, data
, TDB_REPLACE
) != 0) {
172 fatal("tdb_store failed");
179 if (random() % APPEND_PROB
== 0) {
180 if (tdb_append(db
, key
, data
) != 0) {
181 fatal("tdb_append failed");
188 if (random() % LOCKSTORE_PROB
== 0) {
189 tdb_chainlock(db
, key
);
190 data
= tdb_fetch(db
, key
);
191 if (tdb_store(db
, key
, data
, TDB_REPLACE
) != 0) {
192 fatal("tdb_store failed");
194 if (data
.dptr
) free(data
.dptr
);
195 tdb_chainunlock(db
, key
);
201 if (random() % TRAVERSE_PROB
== 0) {
202 tdb_traverse(db
, cull_traverse
, NULL
);
207 #if TRAVERSE_READ_PROB
208 if (random() % TRAVERSE_READ_PROB
== 0) {
209 tdb_traverse_read(db
, NULL
, NULL
);
214 data
= tdb_fetch(db
, key
);
215 if (data
.dptr
) free(data
.dptr
);
222 static int traverse_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
225 tdb_delete(tdb
, key
);
229 static void usage(void)
231 printf("Usage: tdbtorture [-t] [-k] [-m] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
235 static void send_count_and_suicide(int sig
)
239 /* This ensures our successor can continue where we left off. */
241 ret
= write(count_pipe
, &loopnum
, sizeof(loopnum
));
242 } while (ret
== -1 && errno
== EINTR
);
243 /* This gives a unique signature. */
244 kill(getpid(), SIGUSR2
);
247 static int run_child(const char *filename
, int i
, int seed
, unsigned num_loops
, unsigned start
)
249 int tdb_flags
= TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
;
252 tdb_flags
|= TDB_MUTEX_LOCKING
;
255 db
= tdb_open_ex(filename
, hash_size
, tdb_flags
,
256 O_RDWR
| O_CREAT
, 0600, &log_ctx
, NULL
);
258 fatal("db open failed");
264 /* Set global, then we're ready to handle being killed. */
266 signal(SIGUSR1
, send_count_and_suicide
);
268 for (;loopnum
<num_loops
&& error_count
== 0;loopnum
++) {
272 if (error_count
== 0) {
273 tdb_traverse_read(db
, NULL
, NULL
);
274 if (always_transaction
) {
275 while (in_transaction
) {
276 tdb_transaction_cancel(db
);
279 if (tdb_transaction_start(db
) != 0)
280 fatal("tdb_transaction_start failed");
282 tdb_traverse(db
, traverse_fn
, NULL
);
283 tdb_traverse(db
, traverse_fn
, NULL
);
284 if (always_transaction
) {
285 if (tdb_transaction_commit(db
) != 0)
286 fatal("tdb_transaction_commit failed");
292 return (error_count
< 100 ? error_count
: 100);
295 static char *test_path(const char *filename
)
297 const char *prefix
= getenv("TEST_DATA_PREFIX");
303 ret
= asprintf(&path
, "%s/%s", prefix
, filename
);
310 return strdup(filename
);
313 int main(int argc
, char * const *argv
)
316 int num_loops
= 5000;
325 log_ctx
.log_fn
= tdb_log
;
327 while ((c
= getopt(argc
, argv
, "n:l:s:H:thkm")) != -1) {
330 num_procs
= strtol(optarg
, NULL
, 0);
333 num_loops
= strtol(optarg
, NULL
, 0);
336 hash_size
= strtol(optarg
, NULL
, 0);
339 seed
= strtol(optarg
, NULL
, 0);
342 always_transaction
= 1;
348 mutex
= tdb_runtime_check_for_robust_mutexes();
350 printf("tdb_runtime_check_for_robust_mutexes() returned false\n");
359 test_tdb
= test_path("torture.tdb");
364 seed
= (getpid() + time(NULL
)) & 0x7FFFFFFF;
367 printf("Testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
368 num_procs
, num_loops
, hash_size
, seed
,
369 (always_transaction
? " (all within transactions)" : ""));
371 if (num_procs
== 1 && !kill_random
) {
372 /* Don't fork for this case, makes debugging easier. */
373 error_count
= run_child(test_tdb
, 0, seed
, num_loops
, 0);
377 pids
= (pid_t
*)calloc(sizeof(pid_t
), num_procs
);
379 perror("Unable to allocate memory for pids");
382 done
= (int *)calloc(sizeof(int), num_procs
);
384 perror("Unable to allocate memory for done");
388 if (pipe(pfds
) != 0) {
389 perror("Creating pipe");
392 count_pipe
= pfds
[1];
394 for (i
=0;i
<num_procs
;i
++) {
395 if ((pids
[i
]=fork()) == 0) {
397 exit(run_child(test_tdb
, i
, seed
, num_loops
, 0));
405 if (error_count
!= 0) {
406 /* try and stop the test on any failure */
407 for (j
=0;j
<num_procs
;j
++) {
409 kill(pids
[j
], SIGTERM
);
414 pid
= waitpid(-1, &status
, kill_random
? WNOHANG
: 0);
418 /* Sleep for 1/10 second. */
421 select(0, NULL
, NULL
, NULL
, &tv
);
424 kill(pids
[random() % num_procs
], SIGUSR1
);
429 perror("failed to wait for child\n");
433 for (j
=0;j
<num_procs
;j
++) {
434 if (pids
[j
] == pid
) break;
436 if (j
== num_procs
) {
437 printf("unknown child %d exited!?\n", (int)pid
);
440 if (WIFSIGNALED(status
)) {
441 if (WTERMSIG(status
) == SIGUSR2
442 || WTERMSIG(status
) == SIGUSR1
) {
443 /* SIGUSR2 means they wrote to pipe. */
444 if (WTERMSIG(status
) == SIGUSR2
) {
448 ret
= read(pfds
[0], &done
[j
],
450 } while (ret
== -1 && errno
== EINTR
);
454 exit(run_child(test_tdb
, j
, seed
,
455 num_loops
, done
[j
]));
456 printf("Restarting child %i for %u-%u\n",
457 j
, done
[j
], num_loops
);
460 printf("child %d exited with signal %d\n",
461 (int)pid
, WTERMSIG(status
));
464 if (WEXITSTATUS(status
) != 0) {
465 printf("child %d exited with status %d\n",
466 (int)pid
, WEXITSTATUS(status
));
470 ARRAY_DEL_ELEMENT(pids
, j
, num_procs
);
477 if (error_count
== 0) {
478 int tdb_flags
= TDB_DEFAULT
;
481 tdb_flags
|= TDB_NOLOCK
;
484 db
= tdb_open_ex(test_tdb
, hash_size
, tdb_flags
,
485 O_RDWR
, 0, &log_ctx
, NULL
);
487 fatal("db open failed\n");
490 if (tdb_check(db
, NULL
, NULL
) == -1) {
491 printf("db check failed\n");