s3:smbd: add a fsp_persistent_id() function
[Samba.git] / lib / tdb / tools / tdbtorture.c
blob64c504344120708c4d015c10c0fe5811adb92e6e
1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
3 */
5 #include "replace.h"
6 #include "system/time.h"
7 #include "system/wait.h"
8 #include "system/filesys.h"
9 #include "tdb.h"
11 #ifdef HAVE_GETOPT_H
12 #include <getopt.h>
13 #endif
16 #define REOPEN_PROB 30
17 #define DELETE_PROB 8
18 #define STORE_PROB 4
19 #define APPEND_PROB 6
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
25 #define CULL_PROB 100
26 #define KEYLEN 3
27 #define DATALEN 100
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 int loopnum;
35 static int count_pipe;
36 static struct tdb_logging_context log_ctx;
38 #ifdef PRINTF_ATTRIBUTE
39 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
40 #endif
41 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
43 va_list ap;
45 /* trace level messages do not indicate an error */
46 if (level != TDB_DEBUG_TRACE) {
47 error_count++;
50 va_start(ap, format);
51 vfprintf(stdout, format, ap);
52 va_end(ap);
53 fflush(stdout);
54 #if 0
55 if (level != TDB_DEBUG_TRACE) {
56 char *ptr;
57 signal(SIGUSR1, SIG_IGN);
58 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
59 system(ptr);
60 free(ptr);
62 #endif
65 static void fatal(const char *why)
67 perror(why);
68 error_count++;
71 static char *randbuf(int len)
73 char *buf;
74 int i;
75 buf = (char *)malloc(len+1);
77 for (i=0;i<len;i++) {
78 buf[i] = 'a' + (rand() % 26);
80 buf[i] = 0;
81 return buf;
84 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
85 void *state)
87 #if CULL_PROB
88 if (random() % CULL_PROB == 0) {
89 tdb_delete(tdb, key);
91 #endif
92 return 0;
95 static void addrec_db(void)
97 int klen, dlen;
98 char *k, *d;
99 TDB_DATA key, data;
101 klen = 1 + (rand() % KEYLEN);
102 dlen = 1 + (rand() % DATALEN);
104 k = randbuf(klen);
105 d = randbuf(dlen);
107 key.dptr = (unsigned char *)k;
108 key.dsize = klen+1;
110 data.dptr = (unsigned char *)d;
111 data.dsize = dlen+1;
113 #if REOPEN_PROB
114 if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
115 tdb_reopen_all(0);
116 goto next;
118 #endif
120 #if TRANSACTION_PROB
121 if (in_transaction == 0 &&
122 (always_transaction || random() % TRANSACTION_PROB == 0)) {
123 if (tdb_transaction_start(db) != 0) {
124 fatal("tdb_transaction_start failed");
126 in_transaction++;
127 goto next;
129 if (in_transaction && random() % TRANSACTION_PROB == 0) {
130 if (random() % TRANSACTION_PREPARE_PROB == 0) {
131 if (tdb_transaction_prepare_commit(db) != 0) {
132 fatal("tdb_transaction_prepare_commit failed");
135 if (tdb_transaction_commit(db) != 0) {
136 fatal("tdb_transaction_commit failed");
138 in_transaction--;
139 goto next;
141 if (in_transaction && random() % TRANSACTION_PROB == 0) {
142 if (tdb_transaction_cancel(db) != 0) {
143 fatal("tdb_transaction_cancel failed");
145 in_transaction--;
146 goto next;
148 #endif
150 #if DELETE_PROB
151 if (random() % DELETE_PROB == 0) {
152 tdb_delete(db, key);
153 goto next;
155 #endif
157 #if STORE_PROB
158 if (random() % STORE_PROB == 0) {
159 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
160 fatal("tdb_store failed");
162 goto next;
164 #endif
166 #if APPEND_PROB
167 if (random() % APPEND_PROB == 0) {
168 if (tdb_append(db, key, data) != 0) {
169 fatal("tdb_append failed");
171 goto next;
173 #endif
175 #if LOCKSTORE_PROB
176 if (random() % LOCKSTORE_PROB == 0) {
177 tdb_chainlock(db, key);
178 data = tdb_fetch(db, key);
179 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
180 fatal("tdb_store failed");
182 if (data.dptr) free(data.dptr);
183 tdb_chainunlock(db, key);
184 goto next;
186 #endif
188 #if TRAVERSE_PROB
189 if (random() % TRAVERSE_PROB == 0) {
190 tdb_traverse(db, cull_traverse, NULL);
191 goto next;
193 #endif
195 #if TRAVERSE_READ_PROB
196 if (random() % TRAVERSE_READ_PROB == 0) {
197 tdb_traverse_read(db, NULL, NULL);
198 goto next;
200 #endif
202 data = tdb_fetch(db, key);
203 if (data.dptr) free(data.dptr);
205 next:
206 free(k);
207 free(d);
210 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
211 void *state)
213 tdb_delete(tdb, key);
214 return 0;
217 static void usage(void)
219 printf("Usage: tdbtorture [-t] [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
220 exit(0);
223 static void send_count_and_suicide(int sig)
225 /* This ensures our successor can continue where we left off. */
226 write(count_pipe, &loopnum, sizeof(loopnum));
227 /* This gives a unique signature. */
228 kill(getpid(), SIGUSR2);
231 static int run_child(const char *filename, int i, int seed, unsigned num_loops, unsigned start)
233 db = tdb_open_ex(filename, hash_size, TDB_DEFAULT,
234 O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
235 if (!db) {
236 fatal("db open failed");
239 srand(seed + i);
240 srandom(seed + i);
242 /* Set global, then we're ready to handle being killed. */
243 loopnum = start;
244 signal(SIGUSR1, send_count_and_suicide);
246 for (;loopnum<num_loops && error_count == 0;loopnum++) {
247 addrec_db();
250 if (error_count == 0) {
251 tdb_traverse_read(db, NULL, NULL);
252 if (always_transaction) {
253 while (in_transaction) {
254 tdb_transaction_cancel(db);
255 in_transaction--;
257 if (tdb_transaction_start(db) != 0)
258 fatal("tdb_transaction_start failed");
260 tdb_traverse(db, traverse_fn, NULL);
261 tdb_traverse(db, traverse_fn, NULL);
262 if (always_transaction) {
263 if (tdb_transaction_commit(db) != 0)
264 fatal("tdb_transaction_commit failed");
268 tdb_close(db);
270 return (error_count < 100 ? error_count : 100);
273 static char *test_path(const char *filename)
275 const char *prefix = getenv("TEST_DATA_PREFIX");
277 if (prefix) {
278 char *path = NULL;
279 int ret;
281 ret = asprintf(&path, "%s/%s", prefix, filename);
282 if (ret == -1) {
283 return NULL;
285 return path;
288 return strdup(filename);
291 int main(int argc, char * const *argv)
293 int i, seed = -1;
294 int num_loops = 5000;
295 int num_procs = 3;
296 int c, pfds[2];
297 extern char *optarg;
298 pid_t *pids;
299 int kill_random = 0;
300 int *done;
301 char *test_tdb;
303 log_ctx.log_fn = tdb_log;
305 while ((c = getopt(argc, argv, "n:l:s:H:thk")) != -1) {
306 switch (c) {
307 case 'n':
308 num_procs = strtol(optarg, NULL, 0);
309 break;
310 case 'l':
311 num_loops = strtol(optarg, NULL, 0);
312 break;
313 case 'H':
314 hash_size = strtol(optarg, NULL, 0);
315 break;
316 case 's':
317 seed = strtol(optarg, NULL, 0);
318 break;
319 case 't':
320 always_transaction = 1;
321 break;
322 case 'k':
323 kill_random = 1;
324 break;
325 default:
326 usage();
330 test_tdb = test_path("torture.tdb");
332 unlink(test_tdb);
334 if (seed == -1) {
335 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
338 if (num_procs == 1 && !kill_random) {
339 /* Don't fork for this case, makes debugging easier. */
340 error_count = run_child(test_tdb, 0, seed, num_loops, 0);
341 goto done;
344 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
345 done = (int *)calloc(sizeof(int), num_procs);
347 if (pipe(pfds) != 0) {
348 perror("Creating pipe");
349 exit(1);
351 count_pipe = pfds[1];
353 for (i=0;i<num_procs;i++) {
354 if ((pids[i]=fork()) == 0) {
355 close(pfds[0]);
356 if (i == 0) {
357 printf("Testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
358 num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
360 exit(run_child(test_tdb, i, seed, num_loops, 0));
364 while (num_procs) {
365 int status, j;
366 pid_t pid;
368 if (error_count != 0) {
369 /* try and stop the test on any failure */
370 for (j=0;j<num_procs;j++) {
371 if (pids[j] != 0) {
372 kill(pids[j], SIGTERM);
377 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
378 if (pid == 0) {
379 struct timeval tv;
381 /* Sleep for 1/10 second. */
382 tv.tv_sec = 0;
383 tv.tv_usec = 100000;
384 select(0, NULL, NULL, NULL, &tv);
386 /* Kill someone. */
387 kill(pids[random() % num_procs], SIGUSR1);
388 continue;
391 if (pid == -1) {
392 perror("failed to wait for child\n");
393 exit(1);
396 for (j=0;j<num_procs;j++) {
397 if (pids[j] == pid) break;
399 if (j == num_procs) {
400 printf("unknown child %d exited!?\n", (int)pid);
401 exit(1);
403 if (WIFSIGNALED(status)) {
404 if (WTERMSIG(status) == SIGUSR2
405 || WTERMSIG(status) == SIGUSR1) {
406 /* SIGUSR2 means they wrote to pipe. */
407 if (WTERMSIG(status) == SIGUSR2) {
408 read(pfds[0], &done[j],
409 sizeof(done[j]));
411 pids[j] = fork();
412 if (pids[j] == 0)
413 exit(run_child(test_tdb, j, seed,
414 num_loops, done[j]));
415 printf("Restarting child %i for %u-%u\n",
416 j, done[j], num_loops);
417 continue;
419 printf("child %d exited with signal %d\n",
420 (int)pid, WTERMSIG(status));
421 error_count++;
422 } else {
423 if (WEXITSTATUS(status) != 0) {
424 printf("child %d exited with status %d\n",
425 (int)pid, WEXITSTATUS(status));
426 error_count++;
429 memmove(&pids[j], &pids[j+1],
430 (num_procs - j - 1)*sizeof(pids[0]));
431 num_procs--;
434 free(pids);
436 done:
437 if (error_count == 0) {
438 db = tdb_open_ex(test_tdb, hash_size, TDB_DEFAULT,
439 O_RDWR, 0, &log_ctx, NULL);
440 if (!db) {
441 fatal("db open failed");
443 if (tdb_check(db, NULL, NULL) == -1) {
444 printf("db check failed");
445 exit(1);
447 tdb_close(db);
448 printf("OK\n");
451 free(test_tdb);
452 return error_count;