lib/ntdb optimize includes in ntdb tools
[Samba.git] / lib / ntdb / tools / ntdbtorture.c
blob9fd25ca7c6c9bda38d6f09eafb6ade35c591ece3
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 "private.h"
8 #include <ccan/err/err.h>
10 //#define REOPEN_PROB 30
11 #define DELETE_PROB 8
12 #define STORE_PROB 4
13 #define APPEND_PROB 6
14 #define TRANSACTION_PROB 10
15 #define TRANSACTION_PREPARE_PROB 2
16 #define LOCKSTORE_PROB 5
17 #define TRAVERSE_PROB 20
18 #define TRAVERSE_MOD_PROB 100
19 #define TRAVERSE_ABORT_PROB 500
20 #define CULL_PROB 100
21 #define KEYLEN 3
22 #define DATALEN 100
24 static struct ntdb_context *db;
25 static int in_transaction;
26 static int in_traverse;
27 static int error_count;
28 #if TRANSACTION_PROB
29 static int always_transaction = 0;
30 #endif
31 static int loopnum;
32 static int count_pipe;
33 static union ntdb_attribute log_attr;
34 static union ntdb_attribute seed_attr;
35 static union ntdb_attribute hsize_attr;
37 static void ntdb_log(struct ntdb_context *ntdb,
38 enum ntdb_log_level level,
39 enum NTDB_ERROR ecode,
40 const char *message,
41 void *data)
43 printf("ntdb:%s:%s:%s\n",
44 ntdb_name(ntdb), ntdb_errorstr(ecode), message);
45 fflush(stdout);
46 #if 0
48 char str[200];
49 signal(SIGUSR1, SIG_IGN);
50 sprintf(str,"xterm -e gdb /proc/%u/exe %u", (unsigned int)getpid(), (unsigned int)getpid());
51 system(str);
53 #endif
56 #include "../private.h"
58 static void segv_handler(int sig, siginfo_t *info, void *p)
60 char string[100];
62 sprintf(string, "%u: death at %p (map_ptr %p, map_size %zu)\n",
63 (unsigned int)getpid(), info->si_addr, db->file->map_ptr,
64 (size_t)db->file->map_size);
65 if (write(2, string, strlen(string)) > 0)
66 sleep(60);
67 _exit(11);
70 static void warn_on_err(enum NTDB_ERROR e, struct ntdb_context *ntdb,
71 const char *why)
73 if (e != NTDB_SUCCESS) {
74 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), why,
75 ntdb ? ntdb_errorstr(e) : "(no ntdb)");
76 error_count++;
80 static char *randbuf(int len)
82 char *buf;
83 int i;
84 buf = (char *)malloc(len+1);
85 if (buf == NULL) {
86 perror("randbuf: unable to allocate memory for buffer.\n");
87 exit(1);
90 for (i=0;i<len;i++) {
91 buf[i] = 'a' + (rand() % 26);
93 buf[i] = 0;
94 return buf;
97 static void addrec_db(void);
98 static int modify_traverse(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
99 void *state)
101 #if CULL_PROB
102 if (random() % CULL_PROB == 0) {
103 ntdb_delete(ntdb, key);
105 #endif
107 #if TRAVERSE_MOD_PROB
108 if (random() % TRAVERSE_MOD_PROB == 0) {
109 addrec_db();
111 #endif
113 #if TRAVERSE_ABORT_PROB
114 if (random() % TRAVERSE_ABORT_PROB == 0)
115 return 1;
116 #endif
118 return 0;
121 static void addrec_db(void)
123 int klen, dlen;
124 char *k, *d;
125 NTDB_DATA key, data;
126 enum NTDB_ERROR e;
128 klen = 1 + (rand() % KEYLEN);
129 dlen = 1 + (rand() % DATALEN);
131 k = randbuf(klen);
132 d = randbuf(dlen);
134 key.dptr = (unsigned char *)k;
135 key.dsize = klen+1;
137 data.dptr = (unsigned char *)d;
138 data.dsize = dlen+1;
140 #if REOPEN_PROB
141 if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
142 ntdb_reopen_all(0);
143 goto next;
145 #endif
147 #if TRANSACTION_PROB
148 if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
149 e = ntdb_transaction_start(db);
150 warn_on_err(e, db, "ntdb_transaction_start failed");
151 in_transaction++;
152 goto next;
154 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
155 if (random() % TRANSACTION_PREPARE_PROB == 0) {
156 e = ntdb_transaction_prepare_commit(db);
157 warn_on_err(e, db, "ntdb_transaction_prepare_commit failed");
159 e = ntdb_transaction_commit(db);
160 warn_on_err(e, db, "ntdb_transaction_commit failed");
161 in_transaction--;
162 goto next;
165 if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
166 ntdb_transaction_cancel(db);
167 in_transaction--;
168 goto next;
170 #endif
172 #if DELETE_PROB
173 if (random() % DELETE_PROB == 0) {
174 ntdb_delete(db, key);
175 goto next;
177 #endif
179 #if STORE_PROB
180 if (random() % STORE_PROB == 0) {
181 e = ntdb_store(db, key, data, NTDB_REPLACE);
182 warn_on_err(e, db, "ntdb_store failed");
183 goto next;
185 #endif
187 #if APPEND_PROB
188 if (random() % APPEND_PROB == 0) {
189 e = ntdb_append(db, key, data);
190 warn_on_err(e, db, "ntdb_append failed");
191 goto next;
193 #endif
195 #if LOCKSTORE_PROB
196 if (random() % LOCKSTORE_PROB == 0) {
197 ntdb_chainlock(db, key);
198 if (ntdb_fetch(db, key, &data) != NTDB_SUCCESS) {
199 data.dsize = 0;
200 data.dptr = NULL;
202 e = ntdb_store(db, key, data, NTDB_REPLACE);
203 warn_on_err(e, db, "ntdb_store failed");
204 if (data.dptr) free(data.dptr);
205 ntdb_chainunlock(db, key);
206 goto next;
208 #endif
210 #if TRAVERSE_PROB
211 /* FIXME: recursive traverses break transactions? */
212 if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
213 in_traverse++;
214 ntdb_traverse(db, modify_traverse, NULL);
215 in_traverse--;
216 goto next;
218 #endif
220 if (ntdb_fetch(db, key, &data) == NTDB_SUCCESS)
221 free(data.dptr);
223 next:
224 free(k);
225 free(d);
228 static int traverse_fn(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf,
229 void *state)
231 ntdb_delete(ntdb, key);
232 return 0;
235 static void usage(void)
237 printf("Usage: ntdbtorture"
238 #if TRANSACTION_PROB
239 " [-t]"
240 #endif
241 " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S] [-H HASH_SIZE]\n");
242 exit(0);
245 static void send_count_and_suicide(int sig)
247 /* This ensures our successor can continue where we left off. */
248 if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
249 exit(2);
250 /* This gives a unique signature. */
251 kill(getpid(), SIGUSR2);
254 static int run_child(const char *filename, int i, int seed, unsigned num_loops,
255 unsigned start, int ntdb_flags)
257 struct sigaction act = { .sa_sigaction = segv_handler,
258 .sa_flags = SA_SIGINFO };
259 sigaction(11, &act, NULL);
261 db = ntdb_open(filename, ntdb_flags, O_RDWR | O_CREAT, 0600,
262 &log_attr);
263 if (!db) {
264 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), filename,
265 "db open failed");
266 exit(1);
269 #if 0
270 if (i == 0) {
271 printf("pid %u\n", (unsigned int)getpid());
272 sleep(9);
273 } else
274 sleep(10);
275 #endif
277 srand(seed + i);
278 srandom(seed + i);
280 /* Set global, then we're ready to handle being killed. */
281 loopnum = start;
282 signal(SIGUSR1, send_count_and_suicide);
284 for (;loopnum<num_loops && error_count == 0;loopnum++) {
285 addrec_db();
288 if (error_count == 0) {
289 enum NTDB_ERROR e;
291 ntdb_traverse(db, NULL, NULL);
292 #if TRANSACTION_PROB
293 if (always_transaction) {
294 while (in_transaction) {
295 ntdb_transaction_cancel(db);
296 in_transaction--;
298 e = ntdb_transaction_start(db);
299 if (e) {
300 warn_on_err(e, db,
301 "ntdb_transaction_start failed");
302 exit(1);
305 #endif
306 ntdb_traverse(db, traverse_fn, NULL);
307 ntdb_traverse(db, traverse_fn, NULL);
309 #if TRANSACTION_PROB
310 if (always_transaction) {
311 e = ntdb_transaction_commit(db);
312 warn_on_err(e, db, "ntdb_transaction_commit failed");
314 #endif
317 ntdb_close(db);
319 return (error_count < 100 ? error_count : 100);
322 static char *test_path(const char *filename)
324 const char *prefix = getenv("TEST_DATA_PREFIX");
326 if (prefix) {
327 char *path = NULL;
328 int ret;
330 ret = asprintf(&path, "%s/%s", prefix, filename);
331 if (ret == -1) {
332 return NULL;
334 return path;
337 return strdup(filename);
340 int main(int argc, char * const *argv)
342 int i, seed = -1;
343 int num_loops = 5000;
344 int num_procs = 3;
345 int c, pfds[2];
346 extern char *optarg;
347 pid_t *pids;
348 int kill_random = 0;
349 int *done;
350 int ntdb_flags = NTDB_DEFAULT;
351 char *test_ntdb;
352 enum NTDB_ERROR e;
354 log_attr.base.attr = NTDB_ATTRIBUTE_LOG;
355 log_attr.base.next = &seed_attr;
356 log_attr.log.fn = ntdb_log;
357 seed_attr.base.attr = NTDB_ATTRIBUTE_SEED;
358 seed_attr.base.next = &hsize_attr;
359 hsize_attr.base.attr = NTDB_ATTRIBUTE_HASHSIZE;
360 hsize_attr.base.next = NULL;
361 hsize_attr.hashsize.size = 2; /* stress it by default. */
363 while ((c = getopt(argc, argv, "n:l:s:thkSH:")) != -1) {
364 switch (c) {
365 case 'n':
366 num_procs = strtol(optarg, NULL, 0);
367 break;
368 case 'l':
369 num_loops = strtol(optarg, NULL, 0);
370 break;
371 case 's':
372 seed = strtol(optarg, NULL, 0);
373 break;
374 case 'S':
375 ntdb_flags = NTDB_NOSYNC;
376 break;
377 case 't':
378 #if TRANSACTION_PROB
379 always_transaction = 1;
380 #else
381 fprintf(stderr, "Transactions not supported\n");
382 usage();
383 #endif
384 break;
385 case 'k':
386 kill_random = 1;
387 break;
388 case 'H':
389 hsize_attr.hashsize.size = strtol(optarg, NULL, 0);
390 break;
391 default:
392 usage();
396 test_ntdb = test_path("torture.ntdb");
398 unlink(test_ntdb);
400 if (seed == -1) {
401 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
403 seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed;
405 if (num_procs == 1 && !kill_random) {
406 /* Don't fork for this case, makes debugging easier. */
407 error_count = run_child(test_ntdb, 0, seed, num_loops, 0,
408 ntdb_flags);
409 goto done;
412 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
413 done = (int *)calloc(sizeof(int), num_procs);
415 if (pipe(pfds) != 0) {
416 perror("Creating pipe");
417 exit(1);
419 count_pipe = pfds[1];
421 for (i=0;i<num_procs;i++) {
422 if ((pids[i]=fork()) == 0) {
423 close(pfds[0]);
424 if (i == 0) {
425 printf("testing with %d processes, %d loops, seed=%d%s\n",
426 num_procs, num_loops, seed,
427 #if TRANSACTION_PROB
428 always_transaction ? " (all within transactions)" : ""
429 #else
431 #endif
434 exit(run_child(test_ntdb, i, seed, num_loops, 0,
435 ntdb_flags));
439 while (num_procs) {
440 int status, j;
441 pid_t pid;
443 if (error_count != 0) {
444 /* try and stop the test on any failure */
445 for (j=0;j<num_procs;j++) {
446 if (pids[j] != 0) {
447 kill(pids[j], SIGTERM);
452 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
453 if (pid == 0) {
454 struct timespec ts;
456 /* Sleep for 1/10 second. */
457 ts.tv_sec = 0;
458 ts.tv_nsec = 100000000;
459 nanosleep(&ts, NULL);
461 /* Kill someone. */
462 kill(pids[random() % num_procs], SIGUSR1);
463 continue;
466 if (pid == -1) {
467 perror("failed to wait for child\n");
468 exit(1);
471 for (j=0;j<num_procs;j++) {
472 if (pids[j] == pid) break;
474 if (j == num_procs) {
475 printf("unknown child %d exited!?\n", (int)pid);
476 exit(1);
478 if (WIFSIGNALED(status)) {
479 if (WTERMSIG(status) == SIGUSR2
480 || WTERMSIG(status) == SIGUSR1) {
481 /* SIGUSR2 means they wrote to pipe. */
482 if (WTERMSIG(status) == SIGUSR2) {
483 if (read(pfds[0], &done[j],
484 sizeof(done[j]))
485 != sizeof(done[j]))
486 err(1,
487 "Short read from child?");
489 pids[j] = fork();
490 if (pids[j] == 0)
491 exit(run_child(test_ntdb, j, seed,
492 num_loops, done[j],
493 ntdb_flags));
494 printf("Restarting child %i for %u-%u\n",
495 j, done[j], num_loops);
496 continue;
498 printf("child %d exited with signal %d\n",
499 (int)pid, WTERMSIG(status));
500 error_count++;
501 } else {
502 if (WEXITSTATUS(status) != 0) {
503 printf("child %d exited with status %d\n",
504 (int)pid, WEXITSTATUS(status));
505 error_count++;
508 memmove(&pids[j], &pids[j+1],
509 (num_procs - j - 1)*sizeof(pids[0]));
510 num_procs--;
513 free(pids);
515 done:
516 if (error_count == 0) {
517 db = ntdb_open(test_ntdb, NTDB_DEFAULT, O_RDWR | O_CREAT,
518 0600, &log_attr);
519 if (!db) {
520 fprintf(stderr, "%u:%s:%s\n", (unsigned int)getpid(), test_ntdb,
521 "db open failed");
522 exit(1);
524 e = ntdb_check(db, NULL, NULL);
525 if (e) {
526 warn_on_err(e, db, "db check failed");
527 exit(1);
529 ntdb_close(db);
530 printf("OK\n");
533 free(test_ntdb);
534 return error_count;