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 (always_transaction
|| random() % TRANSACTION_PROB
== 0)) {
124 if (tdb_transaction_start(db
) != 0) {
125 fatal("tdb_transaction_start failed");
130 if (in_transaction
&& random() % TRANSACTION_PROB
== 0) {
131 if (random() % TRANSACTION_PREPARE_PROB
== 0) {
132 if (tdb_transaction_prepare_commit(db
) != 0) {
133 fatal("tdb_transaction_prepare_commit failed");
136 if (tdb_transaction_commit(db
) != 0) {
137 fatal("tdb_transaction_commit failed");
142 if (in_transaction
&& random() % TRANSACTION_PROB
== 0) {
143 if (tdb_transaction_cancel(db
) != 0) {
144 fatal("tdb_transaction_cancel failed");
152 if (random() % DELETE_PROB
== 0) {
159 if (random() % STORE_PROB
== 0) {
160 if (tdb_store(db
, key
, data
, TDB_REPLACE
) != 0) {
161 fatal("tdb_store failed");
168 if (random() % APPEND_PROB
== 0) {
169 if (tdb_append(db
, key
, data
) != 0) {
170 fatal("tdb_append failed");
177 if (random() % LOCKSTORE_PROB
== 0) {
178 tdb_chainlock(db
, key
);
179 data
= tdb_fetch(db
, key
);
180 if (tdb_store(db
, key
, data
, TDB_REPLACE
) != 0) {
181 fatal("tdb_store failed");
183 if (data
.dptr
) free(data
.dptr
);
184 tdb_chainunlock(db
, key
);
190 if (random() % TRAVERSE_PROB
== 0) {
191 tdb_traverse(db
, cull_traverse
, NULL
);
196 #if TRAVERSE_READ_PROB
197 if (random() % TRAVERSE_READ_PROB
== 0) {
198 tdb_traverse_read(db
, NULL
, NULL
);
203 data
= tdb_fetch(db
, key
);
204 if (data
.dptr
) free(data
.dptr
);
211 static int traverse_fn(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
214 tdb_delete(tdb
, key
);
218 static void usage(void)
220 printf("Usage: tdbtorture [-t] [-k] [-m] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
224 static void send_count_and_suicide(int sig
)
226 /* This ensures our successor can continue where we left off. */
227 write(count_pipe
, &loopnum
, sizeof(loopnum
));
228 /* This gives a unique signature. */
229 kill(getpid(), SIGUSR2
);
232 static int run_child(const char *filename
, int i
, int seed
, unsigned num_loops
, unsigned start
)
234 int tdb_flags
= TDB_DEFAULT
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
;
237 tdb_flags
|= TDB_MUTEX_LOCKING
;
240 db
= tdb_open_ex(filename
, hash_size
, tdb_flags
,
241 O_RDWR
| O_CREAT
, 0600, &log_ctx
, NULL
);
243 fatal("db open failed");
249 /* Set global, then we're ready to handle being killed. */
251 signal(SIGUSR1
, send_count_and_suicide
);
253 for (;loopnum
<num_loops
&& error_count
== 0;loopnum
++) {
257 if (error_count
== 0) {
258 tdb_traverse_read(db
, NULL
, NULL
);
259 if (always_transaction
) {
260 while (in_transaction
) {
261 tdb_transaction_cancel(db
);
264 if (tdb_transaction_start(db
) != 0)
265 fatal("tdb_transaction_start failed");
267 tdb_traverse(db
, traverse_fn
, NULL
);
268 tdb_traverse(db
, traverse_fn
, NULL
);
269 if (always_transaction
) {
270 if (tdb_transaction_commit(db
) != 0)
271 fatal("tdb_transaction_commit failed");
277 return (error_count
< 100 ? error_count
: 100);
280 static char *test_path(const char *filename
)
282 const char *prefix
= getenv("TEST_DATA_PREFIX");
288 ret
= asprintf(&path
, "%s/%s", prefix
, filename
);
295 return strdup(filename
);
298 int main(int argc
, char * const *argv
)
301 int num_loops
= 5000;
310 log_ctx
.log_fn
= tdb_log
;
312 while ((c
= getopt(argc
, argv
, "n:l:s:H:thkm")) != -1) {
315 num_procs
= strtol(optarg
, NULL
, 0);
318 num_loops
= strtol(optarg
, NULL
, 0);
321 hash_size
= strtol(optarg
, NULL
, 0);
324 seed
= strtol(optarg
, NULL
, 0);
327 always_transaction
= 1;
333 mutex
= tdb_runtime_check_for_robust_mutexes();
335 printf("tdb_runtime_check_for_robust_mutexes() returned false\n");
344 test_tdb
= test_path("torture.tdb");
349 seed
= (getpid() + time(NULL
)) & 0x7FFFFFFF;
352 printf("Testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
353 num_procs
, num_loops
, hash_size
, seed
,
354 (always_transaction
? " (all within transactions)" : ""));
356 if (num_procs
== 1 && !kill_random
) {
357 /* Don't fork for this case, makes debugging easier. */
358 error_count
= run_child(test_tdb
, 0, seed
, num_loops
, 0);
362 pids
= (pid_t
*)calloc(sizeof(pid_t
), num_procs
);
364 perror("Unable to allocate memory for pids");
367 done
= (int *)calloc(sizeof(int), num_procs
);
369 perror("Unable to allocate memory for done");
373 if (pipe(pfds
) != 0) {
374 perror("Creating pipe");
377 count_pipe
= pfds
[1];
379 for (i
=0;i
<num_procs
;i
++) {
380 if ((pids
[i
]=fork()) == 0) {
382 exit(run_child(test_tdb
, i
, seed
, num_loops
, 0));
390 if (error_count
!= 0) {
391 /* try and stop the test on any failure */
392 for (j
=0;j
<num_procs
;j
++) {
394 kill(pids
[j
], SIGTERM
);
399 pid
= waitpid(-1, &status
, kill_random
? WNOHANG
: 0);
403 /* Sleep for 1/10 second. */
406 select(0, NULL
, NULL
, NULL
, &tv
);
409 kill(pids
[random() % num_procs
], SIGUSR1
);
414 perror("failed to wait for child\n");
418 for (j
=0;j
<num_procs
;j
++) {
419 if (pids
[j
] == pid
) break;
421 if (j
== num_procs
) {
422 printf("unknown child %d exited!?\n", (int)pid
);
425 if (WIFSIGNALED(status
)) {
426 if (WTERMSIG(status
) == SIGUSR2
427 || WTERMSIG(status
) == SIGUSR1
) {
428 /* SIGUSR2 means they wrote to pipe. */
429 if (WTERMSIG(status
) == SIGUSR2
) {
430 read(pfds
[0], &done
[j
],
435 exit(run_child(test_tdb
, j
, seed
,
436 num_loops
, done
[j
]));
437 printf("Restarting child %i for %u-%u\n",
438 j
, done
[j
], num_loops
);
441 printf("child %d exited with signal %d\n",
442 (int)pid
, WTERMSIG(status
));
445 if (WEXITSTATUS(status
) != 0) {
446 printf("child %d exited with status %d\n",
447 (int)pid
, WEXITSTATUS(status
));
451 memmove(&pids
[j
], &pids
[j
+1],
452 (num_procs
- j
- 1)*sizeof(pids
[0]));
459 if (error_count
== 0) {
460 int tdb_flags
= TDB_DEFAULT
;
463 tdb_flags
|= TDB_NOLOCK
;
466 db
= tdb_open_ex(test_tdb
, hash_size
, tdb_flags
,
467 O_RDWR
, 0, &log_ctx
, NULL
);
469 fatal("db open failed\n");
472 if (tdb_check(db
, NULL
, NULL
) == -1) {
473 printf("db check failed\n");