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;
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 void addrec_db(void)
102 klen
= 1 + (rand() % KEYLEN
);
103 dlen
= 1 + (rand() % DATALEN
);
108 key
.dptr
= (unsigned char *)k
;
111 data
.dptr
= (unsigned char *)d
;
115 if (in_transaction
== 0 && random() % REOPEN_PROB
== 0) {
122 if (in_transaction
== 0 &&
123 ((tdb_get_flags(db
) & TDB_MUTEX_LOCKING
) == 0) &&
124 (always_transaction
|| random() % TRANSACTION_PROB
== 0)) {
125 if (tdb_transaction_start(db
) != 0) {
126 fatal("tdb_transaction_start failed");
131 if (in_transaction
&& random() % TRANSACTION_PROB
== 0) {
132 if (random() % TRANSACTION_PREPARE_PROB
== 0) {
133 if (tdb_transaction_prepare_commit(db
) != 0) {
134 fatal("tdb_transaction_prepare_commit failed");
137 if (tdb_transaction_commit(db
) != 0) {
138 fatal("tdb_transaction_commit failed");
143 if (in_transaction
&& random() % TRANSACTION_PROB
== 0) {
144 if (tdb_transaction_cancel(db
) != 0) {
145 fatal("tdb_transaction_cancel failed");
153 if (random() % DELETE_PROB
== 0) {
160 if (random() % STORE_PROB
== 0) {
161 if (tdb_store(db
, key
, data
, TDB_REPLACE
) != 0) {
162 fatal("tdb_store failed");
169 if (random() % APPEND_PROB
== 0) {
170 if (tdb_append(db
, key
, data
) != 0) {
171 fatal("tdb_append failed");
178 if (random() % LOCKSTORE_PROB
== 0) {
179 tdb_chainlock(db
, key
);
180 data
= tdb_fetch(db
, key
);
181 if (tdb_store(db
, key
, data
, TDB_REPLACE
) != 0) {
182 fatal("tdb_store failed");
184 if (data
.dptr
) free(data
.dptr
);
185 tdb_chainunlock(db
, key
);
191 if (random() % TRAVERSE_PROB
== 0) {
192 tdb_traverse(db
, cull_traverse
, NULL
);
197 #if TRAVERSE_READ_PROB
198 if (random() % TRAVERSE_READ_PROB
== 0) {
199 tdb_traverse_read(db
, NULL
, NULL
);
204 data
= tdb_fetch(db
, key
);
205 if (data
.dptr
) free(data
.dptr
);
212 static int traverse_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
215 tdb_delete(tdb
, key
);
219 static void usage(void)
221 printf("Usage: tdbtorture [-t] [-k] [-m] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
225 static void send_count_and_suicide(int sig
)
227 /* This ensures our successor can continue where we left off. */
228 write(count_pipe
, &loopnum
, sizeof(loopnum
));
229 /* This gives a unique signature. */
230 kill(getpid(), SIGUSR2
);
233 static int run_child(const char *filename
, int i
, int seed
, unsigned num_loops
, unsigned start
)
235 int tdb_flags
= TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
;
238 tdb_flags
|= TDB_MUTEX_LOCKING
;
241 db
= tdb_open_ex(filename
, hash_size
, tdb_flags
,
242 O_RDWR
| O_CREAT
, 0600, &log_ctx
, NULL
);
244 fatal("db open failed");
250 /* Set global, then we're ready to handle being killed. */
252 signal(SIGUSR1
, send_count_and_suicide
);
254 for (;loopnum
<num_loops
&& error_count
== 0;loopnum
++) {
258 if (error_count
== 0) {
259 tdb_traverse_read(db
, NULL
, NULL
);
260 if (always_transaction
) {
261 while (in_transaction
) {
262 tdb_transaction_cancel(db
);
265 if (tdb_transaction_start(db
) != 0)
266 fatal("tdb_transaction_start failed");
268 tdb_traverse(db
, traverse_fn
, NULL
);
269 tdb_traverse(db
, traverse_fn
, NULL
);
270 if (always_transaction
) {
271 if (tdb_transaction_commit(db
) != 0)
272 fatal("tdb_transaction_commit failed");
278 return (error_count
< 100 ? error_count
: 100);
281 static char *test_path(const char *filename
)
283 const char *prefix
= getenv("TEST_DATA_PREFIX");
289 ret
= asprintf(&path
, "%s/%s", prefix
, filename
);
296 return strdup(filename
);
299 int main(int argc
, char * const *argv
)
302 int num_loops
= 5000;
311 log_ctx
.log_fn
= tdb_log
;
313 while ((c
= getopt(argc
, argv
, "n:l:s:H:thkm")) != -1) {
316 num_procs
= strtol(optarg
, NULL
, 0);
319 num_loops
= strtol(optarg
, NULL
, 0);
322 hash_size
= strtol(optarg
, NULL
, 0);
325 seed
= strtol(optarg
, NULL
, 0);
328 always_transaction
= 1;
334 mutex
= tdb_runtime_check_for_robust_mutexes();
336 printf("tdb_runtime_check_for_robust_mutexes() returned false\n");
345 test_tdb
= test_path("torture.tdb");
350 seed
= (getpid() + time(NULL
)) & 0x7FFFFFFF;
353 if (num_procs
== 1 && !kill_random
) {
354 /* Don't fork for this case, makes debugging easier. */
355 error_count
= run_child(test_tdb
, 0, seed
, num_loops
, 0);
359 pids
= (pid_t
*)calloc(sizeof(pid_t
), num_procs
);
361 perror("Unable to allocate memory for pids");
364 done
= (int *)calloc(sizeof(int), num_procs
);
366 perror("Unable to allocate memory for done");
370 if (pipe(pfds
) != 0) {
371 perror("Creating pipe");
374 count_pipe
= pfds
[1];
376 for (i
=0;i
<num_procs
;i
++) {
377 if ((pids
[i
]=fork()) == 0) {
380 printf("Testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
381 num_procs
, num_loops
, hash_size
, seed
, always_transaction
? " (all within transactions)" : "");
383 exit(run_child(test_tdb
, i
, seed
, num_loops
, 0));
391 if (error_count
!= 0) {
392 /* try and stop the test on any failure */
393 for (j
=0;j
<num_procs
;j
++) {
395 kill(pids
[j
], SIGTERM
);
400 pid
= waitpid(-1, &status
, kill_random
? WNOHANG
: 0);
404 /* Sleep for 1/10 second. */
407 select(0, NULL
, NULL
, NULL
, &tv
);
410 kill(pids
[random() % num_procs
], SIGUSR1
);
415 perror("failed to wait for child\n");
419 for (j
=0;j
<num_procs
;j
++) {
420 if (pids
[j
] == pid
) break;
422 if (j
== num_procs
) {
423 printf("unknown child %d exited!?\n", (int)pid
);
426 if (WIFSIGNALED(status
)) {
427 if (WTERMSIG(status
) == SIGUSR2
428 || WTERMSIG(status
) == SIGUSR1
) {
429 /* SIGUSR2 means they wrote to pipe. */
430 if (WTERMSIG(status
) == SIGUSR2
) {
431 read(pfds
[0], &done
[j
],
436 exit(run_child(test_tdb
, j
, seed
,
437 num_loops
, done
[j
]));
438 printf("Restarting child %i for %u-%u\n",
439 j
, done
[j
], num_loops
);
442 printf("child %d exited with signal %d\n",
443 (int)pid
, WTERMSIG(status
));
446 if (WEXITSTATUS(status
) != 0) {
447 printf("child %d exited with status %d\n",
448 (int)pid
, WEXITSTATUS(status
));
452 memmove(&pids
[j
], &pids
[j
+1],
453 (num_procs
- j
- 1)*sizeof(pids
[0]));
460 if (error_count
== 0) {
461 int tdb_flags
= TDB_DEFAULT
;
464 tdb_flags
|= TDB_NOLOCK
;
467 db
= tdb_open_ex(test_tdb
, hash_size
, tdb_flags
,
468 O_RDWR
, 0, &log_ctx
, NULL
);
470 fatal("db open failed\n");
473 if (tdb_check(db
, NULL
, NULL
) == -1) {
474 printf("db check failed\n");