librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/gebeck_regimport.git] / lib / ntdb / tools / ntdbtorture.c
blob3bcf3200f2c153c20acc04320e91d0ebd5763c96
1 /* this tests ntdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
3 */
5 #include "config.h"
6 #include "ntdb.h"
7 #include <ccan/err/err.h>
8 #ifdef HAVE_LIBREPLACE
9 #include <replace.h>
10 #else
11 #include <stdlib.h>
12 #include <getopt.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <fcntl.h>
20 #include <time.h>
21 #include <sys/wait.h>
22 #endif
24 //#define REOPEN_PROB 30
25 #define DELETE_PROB 8
26 #define STORE_PROB 4
27 #define APPEND_PROB 6
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
34 #define CULL_PROB 100
35 #define KEYLEN 3
36 #define DATALEN 100
38 static struct ntdb_context *db;
39 static int in_transaction;
40 static int in_traverse;
41 static int error_count;
42 #if TRANSACTION_PROB
43 static int always_transaction = 0;
44 #endif
45 static int loopnum;
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,
54 const char *message,
55 void *data)
57 printf("ntdb:%s:%s:%s\n",
58 ntdb_name(ntdb), ntdb_errorstr(ecode), message);
59 fflush(stdout);
60 #if 0
62 char str[200];
63 signal(SIGUSR1, SIG_IGN);
64 sprintf(str,"xterm -e gdb /proc/%u/exe %u", (unsigned int)getpid(), (unsigned int)getpid());
65 system(str);
67 #endif
70 #include "../private.h"
72 static void segv_handler(int sig, siginfo_t *info, void *p)
74 char string[100];
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)
80 sleep(60);
81 _exit(11);
84 static void warn_on_err(enum NTDB_ERROR e, struct ntdb_context *ntdb,
85 const char *why)
87 if (e != NTDB_SUCCESS) {
88 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), why,
89 ntdb ? ntdb_errorstr(e) : "(no ntdb)");
90 error_count++;
94 static char *randbuf(int len)
96 char *buf;
97 int i;
98 buf = (char *)malloc(len+1);
100 for (i=0;i<len;i++) {
101 buf[i] = 'a' + (rand() % 26);
103 buf[i] = 0;
104 return buf;
107 static void addrec_db(void);
108 static int modify_traverse(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
109 void *state)
111 #if CULL_PROB
112 if (random() % CULL_PROB == 0) {
113 ntdb_delete(ntdb, key);
115 #endif
117 #if TRAVERSE_MOD_PROB
118 if (random() % TRAVERSE_MOD_PROB == 0) {
119 addrec_db();
121 #endif
123 #if TRAVERSE_ABORT_PROB
124 if (random() % TRAVERSE_ABORT_PROB == 0)
125 return 1;
126 #endif
128 return 0;
131 static void addrec_db(void)
133 int klen, dlen;
134 char *k, *d;
135 NTDB_DATA key, data;
136 enum NTDB_ERROR e;
138 klen = 1 + (rand() % KEYLEN);
139 dlen = 1 + (rand() % DATALEN);
141 k = randbuf(klen);
142 d = randbuf(dlen);
144 key.dptr = (unsigned char *)k;
145 key.dsize = klen+1;
147 data.dptr = (unsigned char *)d;
148 data.dsize = dlen+1;
150 #if REOPEN_PROB
151 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
152 ntdb_reopen_all(0);
153 goto next;
155 #endif
157 #if TRANSACTION_PROB
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");
161 in_transaction++;
162 goto next;
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");
171 in_transaction--;
172 goto next;
175 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
176 ntdb_transaction_cancel(db);
177 in_transaction--;
178 goto next;
180 #endif
182 #if DELETE_PROB
183 if (random() % DELETE_PROB == 0) {
184 ntdb_delete(db, key);
185 goto next;
187 #endif
189 #if STORE_PROB
190 if (random() % STORE_PROB == 0) {
191 e = ntdb_store(db, key, data, NTDB_REPLACE);
192 warn_on_err(e, db, "ntdb_store failed");
193 goto next;
195 #endif
197 #if APPEND_PROB
198 if (random() % APPEND_PROB == 0) {
199 e = ntdb_append(db, key, data);
200 warn_on_err(e, db, "ntdb_append failed");
201 goto next;
203 #endif
205 #if LOCKSTORE_PROB
206 if (random() % LOCKSTORE_PROB == 0) {
207 ntdb_chainlock(db, key);
208 if (ntdb_fetch(db, key, &data) != NTDB_SUCCESS) {
209 data.dsize = 0;
210 data.dptr = NULL;
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);
216 goto next;
218 #endif
220 #if TRAVERSE_PROB
221 /* FIXME: recursive traverses break transactions? */
222 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
223 in_traverse++;
224 ntdb_traverse(db, modify_traverse, NULL);
225 in_traverse--;
226 goto next;
228 #endif
230 if (ntdb_fetch(db, key, &data) == NTDB_SUCCESS)
231 free(data.dptr);
233 next:
234 free(k);
235 free(d);
238 static int traverse_fn(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
239 void *state)
241 ntdb_delete(ntdb, key);
242 return 0;
245 static void usage(void)
247 printf("Usage: ntdbtorture"
248 #if TRANSACTION_PROB
249 " [-t]"
250 #endif
251 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S] [-H HASH_SIZE]\n");
252 exit(0);
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))
259 exit(2);
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,
272 &log_attr);
273 if (!db) {
274 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), filename,
275 "db open failed");
276 exit(1);
279 #if 0
280 if (i == 0) {
281 printf("pid %u\n", (unsigned int)getpid());
282 sleep(9);
283 } else
284 sleep(10);
285 #endif
287 srand(seed + i);
288 srandom(seed + i);
290 /* Set global, then we're ready to handle being killed. */
291 loopnum = start;
292 signal(SIGUSR1, send_count_and_suicide);
294 for (;loopnum<num_loops && error_count == 0;loopnum++) {
295 addrec_db();
298 if (error_count == 0) {
299 enum NTDB_ERROR e;
301 ntdb_traverse(db, NULL, NULL);
302 #if TRANSACTION_PROB
303 if (always_transaction) {
304 while (in_transaction) {
305 ntdb_transaction_cancel(db);
306 in_transaction--;
308 e = ntdb_transaction_start(db);
309 if (e) {
310 warn_on_err(e, db,
311 "ntdb_transaction_start failed");
312 exit(1);
315 #endif
316 ntdb_traverse(db, traverse_fn, NULL);
317 ntdb_traverse(db, traverse_fn, NULL);
319 #if TRANSACTION_PROB
320 if (always_transaction) {
321 e = ntdb_transaction_commit(db);
322 warn_on_err(e, db, "ntdb_transaction_commit failed");
324 #endif
327 ntdb_close(db);
329 return (error_count < 100 ? error_count : 100);
332 static char *test_path(const char *filename)
334 const char *prefix = getenv("TEST_DATA_PREFIX");
336 if (prefix) {
337 char *path = NULL;
338 int ret;
340 ret = asprintf(&path, "%s/%s", prefix, filename);
341 if (ret == -1) {
342 return NULL;
344 return path;
347 return strdup(filename);
350 int main(int argc, char * const *argv)
352 int i, seed = -1;
353 int num_loops = 5000;
354 int num_procs = 3;
355 int c, pfds[2];
356 extern char *optarg;
357 pid_t *pids;
358 int kill_random = 0;
359 int *done;
360 int ntdb_flags = NTDB_DEFAULT;
361 char *test_ntdb;
362 enum NTDB_ERROR e;
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) {
374 switch (c) {
375 case 'n':
376 num_procs = strtol(optarg, NULL, 0);
377 break;
378 case 'l':
379 num_loops = strtol(optarg, NULL, 0);
380 break;
381 case 's':
382 seed = strtol(optarg, NULL, 0);
383 break;
384 case 'S':
385 ntdb_flags = NTDB_NOSYNC;
386 break;
387 case 't':
388 #if TRANSACTION_PROB
389 always_transaction = 1;
390 #else
391 fprintf(stderr, "Transactions not supported\n");
392 usage();
393 #endif
394 break;
395 case 'k':
396 kill_random = 1;
397 break;
398 case 'H':
399 hsize_attr.hashsize.size = strtol(optarg, NULL, 0);
400 break;
401 default:
402 usage();
406 test_ntdb = test_path("torture.ntdb");
408 unlink(test_ntdb);
410 if (seed == -1) {
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,
418 ntdb_flags);
419 goto done;
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");
427 exit(1);
429 count_pipe = pfds[1];
431 for (i=0;i<num_procs;i++) {
432 if ((pids[i]=fork()) == 0) {
433 close(pfds[0]);
434 if (i == 0) {
435 printf("testing with %d processes, %d loops, seed=%d%s\n",
436 num_procs, num_loops, seed,
437 #if TRANSACTION_PROB
438 always_transaction ? " (all within transactions)" : ""
439 #else
441 #endif
444 exit(run_child(test_ntdb, i, seed, num_loops, 0,
445 ntdb_flags));
449 while (num_procs) {
450 int status, j;
451 pid_t pid;
453 if (error_count != 0) {
454 /* try and stop the test on any failure */
455 for (j=0;j<num_procs;j++) {
456 if (pids[j] != 0) {
457 kill(pids[j], SIGTERM);
462 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
463 if (pid == 0) {
464 struct timespec ts;
466 /* Sleep for 1/10 second. */
467 ts.tv_sec = 0;
468 ts.tv_nsec = 100000000;
469 nanosleep(&ts, NULL);
471 /* Kill someone. */
472 kill(pids[random() % num_procs], SIGUSR1);
473 continue;
476 if (pid == -1) {
477 perror("failed to wait for child\n");
478 exit(1);
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);
486 exit(1);
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],
494 sizeof(done[j]))
495 != sizeof(done[j]))
496 err(1,
497 "Short read from child?");
499 pids[j] = fork();
500 if (pids[j] == 0)
501 exit(run_child(test_ntdb, j, seed,
502 num_loops, done[j],
503 ntdb_flags));
504 printf("Restarting child %i for %u-%u\n",
505 j, done[j], num_loops);
506 continue;
508 printf("child %d exited with signal %d\n",
509 (int)pid, WTERMSIG(status));
510 error_count++;
511 } else {
512 if (WEXITSTATUS(status) != 0) {
513 printf("child %d exited with status %d\n",
514 (int)pid, WEXITSTATUS(status));
515 error_count++;
518 memmove(&pids[j], &pids[j+1],
519 (num_procs - j - 1)*sizeof(pids[0]));
520 num_procs--;
523 free(pids);
525 done:
526 if (error_count == 0) {
527 db = ntdb_open(test_ntdb, NTDB_DEFAULT, O_RDWR | O_CREAT,
528 0600, &log_attr);
529 if (!db) {
530 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), test_ntdb,
531 "db open failed");
532 exit(1);
534 e = ntdb_check(db, NULL, NULL);
535 if (e) {
536 warn_on_err(e, db, "db check failed");
537 exit(1);
539 ntdb_close(db);
540 printf("OK\n");
543 free(test_ntdb);
544 return error_count;