927868810410915a09f82996be1b914c06d1724c
[obnox/samba/samba-obnox.git] / lib / tdb / tools / tdbtorture.c
1 /* this tests tdb by doing lots of ops from several simultaneous
2    writers - that stresses the locking code.
3 */
4
5 #include "replace.h"
6 #include "system/time.h"
7 #include "system/wait.h"
8 #include "system/filesys.h"
9 #include "tdb.h"
10
11 #ifdef HAVE_GETOPT_H
12 #include <getopt.h>
13 #endif
14
15
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
28
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 bool mutex = false;
37 static struct tdb_logging_context log_ctx;
38
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);
41 #endif
42 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
43 {
44         va_list ap;
45
46         /* trace level messages do not indicate an error */
47         if (level != TDB_DEBUG_TRACE) {
48                 error_count++;
49         }
50
51         va_start(ap, format);
52         vfprintf(stdout, format, ap);
53         va_end(ap);
54         fflush(stdout);
55 #if 0
56         if (level != TDB_DEBUG_TRACE) {
57                 char *ptr;
58                 signal(SIGUSR1, SIG_IGN);
59                 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
60                 system(ptr);
61                 free(ptr);
62         }
63 #endif
64 }
65
66 static void fatal(const char *why)
67 {
68         perror(why);
69         error_count++;
70 }
71
72 static char *randbuf(int len)
73 {
74         char *buf;
75         int i;
76         buf = (char *)malloc(len+1);
77
78         for (i=0;i<len;i++) {
79                 buf[i] = 'a' + (rand() % 26);
80         }
81         buf[i] = 0;
82         return buf;
83 }
84
85 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
86                          void *state)
87 {
88 #if CULL_PROB
89         if (random() % CULL_PROB == 0) {
90                 tdb_delete(tdb, key);
91         }
92 #endif
93         return 0;
94 }
95
96 static void addrec_db(void)
97 {
98         int klen, dlen;
99         char *k, *d;
100         TDB_DATA key, data;
101
102         klen = 1 + (rand() % KEYLEN);
103         dlen = 1 + (rand() % DATALEN);
104
105         k = randbuf(klen);
106         d = randbuf(dlen);
107
108         key.dptr = (unsigned char *)k;
109         key.dsize = klen+1;
110
111         data.dptr = (unsigned char *)d;
112         data.dsize = dlen+1;
113
114 #if REOPEN_PROB
115         if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
116                 tdb_reopen_all(0);
117                 goto next;
118         }
119 #endif
120
121 #if TRANSACTION_PROB
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");
127                 }
128                 in_transaction++;
129                 goto next;
130         }
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");
135                         }
136                 }
137                 if (tdb_transaction_commit(db) != 0) {
138                         fatal("tdb_transaction_commit failed");
139                 }
140                 in_transaction--;
141                 goto next;
142         }
143         if (in_transaction && random() % TRANSACTION_PROB == 0) {
144                 if (tdb_transaction_cancel(db) != 0) {
145                         fatal("tdb_transaction_cancel failed");
146                 }
147                 in_transaction--;
148                 goto next;
149         }
150 #endif
151
152 #if DELETE_PROB
153         if (random() % DELETE_PROB == 0) {
154                 tdb_delete(db, key);
155                 goto next;
156         }
157 #endif
158
159 #if STORE_PROB
160         if (random() % STORE_PROB == 0) {
161                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
162                         fatal("tdb_store failed");
163                 }
164                 goto next;
165         }
166 #endif
167
168 #if APPEND_PROB
169         if (random() % APPEND_PROB == 0) {
170                 if (tdb_append(db, key, data) != 0) {
171                         fatal("tdb_append failed");
172                 }
173                 goto next;
174         }
175 #endif
176
177 #if LOCKSTORE_PROB
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");
183                 }
184                 if (data.dptr) free(data.dptr);
185                 tdb_chainunlock(db, key);
186                 goto next;
187         } 
188 #endif
189
190 #if TRAVERSE_PROB
191         if (random() % TRAVERSE_PROB == 0) {
192                 tdb_traverse(db, cull_traverse, NULL);
193                 goto next;
194         }
195 #endif
196
197 #if TRAVERSE_READ_PROB
198         if (random() % TRAVERSE_READ_PROB == 0) {
199                 tdb_traverse_read(db, NULL, NULL);
200                 goto next;
201         }
202 #endif
203
204         data = tdb_fetch(db, key);
205         if (data.dptr) free(data.dptr);
206
207 next:
208         free(k);
209         free(d);
210 }
211
212 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
213                        void *state)
214 {
215         tdb_delete(tdb, key);
216         return 0;
217 }
218
219 static void usage(void)
220 {
221         printf("Usage: tdbtorture [-t] [-k] [-m] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
222         exit(0);
223 }
224
225 static void send_count_and_suicide(int sig)
226 {
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);
231 }
232
233 static int run_child(const char *filename, int i, int seed, unsigned num_loops, unsigned start)
234 {
235         int tdb_flags = TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
236
237         if (mutex) {
238                 tdb_flags |= TDB_MUTEX_LOCKING;
239         }
240
241         db = tdb_open_ex(filename, hash_size, tdb_flags,
242                          O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
243         if (!db) {
244                 fatal("db open failed");
245         }
246
247         srand(seed + i);
248         srandom(seed + i);
249
250         /* Set global, then we're ready to handle being killed. */
251         loopnum = start;
252         signal(SIGUSR1, send_count_and_suicide);
253
254         for (;loopnum<num_loops && error_count == 0;loopnum++) {
255                 addrec_db();
256         }
257
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);
263                                 in_transaction--;
264                         }
265                         if (tdb_transaction_start(db) != 0)
266                                 fatal("tdb_transaction_start failed");
267                 }
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");
273                 }
274         }
275
276         tdb_close(db);
277
278         return (error_count < 100 ? error_count : 100);
279 }
280
281 static char *test_path(const char *filename)
282 {
283         const char *prefix = getenv("TEST_DATA_PREFIX");
284
285         if (prefix) {
286                 char *path = NULL;
287                 int ret;
288
289                 ret = asprintf(&path, "%s/%s", prefix, filename);
290                 if (ret == -1) {
291                         return NULL;
292                 }
293                 return path;
294         }
295
296         return strdup(filename);
297 }
298
299 int main(int argc, char * const *argv)
300 {
301         int i, seed = -1;
302         int num_loops = 5000;
303         int num_procs = 3;
304         int c, pfds[2];
305         extern char *optarg;
306         pid_t *pids;
307         int kill_random = 0;
308         int *done;
309         char *test_tdb;
310
311         log_ctx.log_fn = tdb_log;
312
313         while ((c = getopt(argc, argv, "n:l:s:H:thkm")) != -1) {
314                 switch (c) {
315                 case 'n':
316                         num_procs = strtol(optarg, NULL, 0);
317                         break;
318                 case 'l':
319                         num_loops = strtol(optarg, NULL, 0);
320                         break;
321                 case 'H':
322                         hash_size = strtol(optarg, NULL, 0);
323                         break;
324                 case 's':
325                         seed = strtol(optarg, NULL, 0);
326                         break;
327                 case 't':
328                         always_transaction = 1;
329                         break;
330                 case 'k':
331                         kill_random = 1;
332                         break;
333                 case 'm':
334                         mutex = tdb_runtime_check_for_robust_mutexes();
335                         if (!mutex) {
336                                 printf("tdb_runtime_check_for_robust_mutexes() returned false\n");
337                                 exit(1);
338                         }
339                         break;
340                 default:
341                         usage();
342                 }
343         }
344
345         test_tdb = test_path("torture.tdb");
346
347         unlink(test_tdb);
348
349         if (seed == -1) {
350                 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
351         }
352
353         printf("Testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
354                num_procs, num_loops, hash_size, seed,
355                (always_transaction ? " (all within transactions)" : ""));
356
357         if (num_procs == 1 && !kill_random) {
358                 /* Don't fork for this case, makes debugging easier. */
359                 error_count = run_child(test_tdb, 0, seed, num_loops, 0);
360                 goto done;
361         }
362
363         pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
364         if (pids == NULL) {
365                 perror("Unable to allocate memory for pids");
366                 exit(1);
367         }
368         done = (int *)calloc(sizeof(int), num_procs);
369         if (done == NULL) {
370                 perror("Unable to allocate memory for done");
371                 exit(1);
372         }
373
374         if (pipe(pfds) != 0) {
375                 perror("Creating pipe");
376                 exit(1);
377         }
378         count_pipe = pfds[1];
379
380         for (i=0;i<num_procs;i++) {
381                 if ((pids[i]=fork()) == 0) {
382                         close(pfds[0]);
383                         exit(run_child(test_tdb, i, seed, num_loops, 0));
384                 }
385         }
386
387         while (num_procs) {
388                 int status, j;
389                 pid_t pid;
390
391                 if (error_count != 0) {
392                         /* try and stop the test on any failure */
393                         for (j=0;j<num_procs;j++) {
394                                 if (pids[j] != 0) {
395                                         kill(pids[j], SIGTERM);
396                                 }
397                         }
398                 }
399
400                 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
401                 if (pid == 0) {
402                         struct timeval tv;
403
404                         /* Sleep for 1/10 second. */
405                         tv.tv_sec = 0;
406                         tv.tv_usec = 100000;
407                         select(0, NULL, NULL, NULL, &tv);
408
409                         /* Kill someone. */
410                         kill(pids[random() % num_procs], SIGUSR1);
411                         continue;
412                 }
413
414                 if (pid == -1) {
415                         perror("failed to wait for child\n");
416                         exit(1);
417                 }
418
419                 for (j=0;j<num_procs;j++) {
420                         if (pids[j] == pid) break;
421                 }
422                 if (j == num_procs) {
423                         printf("unknown child %d exited!?\n", (int)pid);
424                         exit(1);
425                 }
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],
432                                              sizeof(done[j]));
433                                 }
434                                 pids[j] = fork();
435                                 if (pids[j] == 0)
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);
440                                 continue;
441                         }
442                         printf("child %d exited with signal %d\n",
443                                (int)pid, WTERMSIG(status));
444                         error_count++;
445                 } else {
446                         if (WEXITSTATUS(status) != 0) {
447                                 printf("child %d exited with status %d\n",
448                                        (int)pid, WEXITSTATUS(status));
449                                 error_count++;
450                         }
451                 }
452                 memmove(&pids[j], &pids[j+1],
453                         (num_procs - j - 1)*sizeof(pids[0]));
454                 num_procs--;
455         }
456
457         free(pids);
458
459 done:
460         if (error_count == 0) {
461                 int tdb_flags = TDB_DEFAULT;
462
463                 if (mutex) {
464                         tdb_flags |= TDB_NOLOCK;
465                 }
466
467                 db = tdb_open_ex(test_tdb, hash_size, tdb_flags,
468                                  O_RDWR, 0, &log_ctx, NULL);
469                 if (!db) {
470                         fatal("db open failed\n");
471                         exit(1);
472                 }
473                 if (tdb_check(db, NULL, NULL) == -1) {
474                         printf("db check failed\n");
475                         exit(1);
476                 }
477                 tdb_close(db);
478                 printf("OK\n");
479         }
480
481         free(test_tdb);
482         return error_count;
483 }