tdb2: update documentation
[ccan] / ccan / tdb2 / 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 <ccan/tdb2/tdb2.h>
6 #include <stdlib.h>
7 #include <err.h>
8 #include <getopt.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <fcntl.h>
16 #include <time.h>
17 #include <sys/wait.h>
18
19 //#define REOPEN_PROB 30
20 #define DELETE_PROB 8
21 #define STORE_PROB 4
22 #define APPEND_PROB 6
23 #define TRANSACTION_PROB 10
24 #define TRANSACTION_PREPARE_PROB 2
25 #define LOCKSTORE_PROB 5
26 #define TRAVERSE_PROB 20
27 #define TRAVERSE_READ_PROB 20
28 #define TRAVERSE_MOD_PROB 100
29 #define TRAVERSE_ABORT_PROB 500
30 #define CULL_PROB 100
31 #define KEYLEN 3
32 #define DATALEN 100
33
34 static struct tdb_context *db;
35 static int in_transaction;
36 static int in_traverse;
37 static int error_count;
38 #if TRANSACTION_PROB
39 static int always_transaction = 0;
40 #endif
41 static int loopnum;
42 static int count_pipe;
43 static union tdb_attribute log_attr;
44 static union tdb_attribute seed_attr;
45
46 #ifdef PRINTF_FMT
47 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...) PRINTF_FMT(4,5);
48 #endif
49 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...)
50 {
51         va_list ap;
52
53         if (level != TDB_DEBUG_TRACE)
54                 error_count++;
55
56         va_start(ap, format);
57         vfprintf(stdout, format, ap);
58         va_end(ap);
59         fflush(stdout);
60 #if 0
61         if (level != TDB_DEBUG_TRACE) {
62                 char *ptr;
63                 signal(SIGUSR1, SIG_IGN);
64                 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
65                 system(ptr);
66                 free(ptr);
67         }
68 #endif  
69 }
70
71 #include "../private.h"
72
73 static void segv_handler(int signal, siginfo_t *info, void *p)
74 {
75         char string[100];
76
77         sprintf(string, "%u: death at %p (map_ptr %p, map_size %llu)\n",
78                 getpid(), info->si_addr, db->map_ptr, db->map_size);
79         write(2, string, strlen(string));
80         sleep(60);
81         _exit(11);
82 }       
83
84 static void fatal(const char *why)
85 {
86         perror(why);
87         error_count++;
88 }
89
90 static char *randbuf(int len)
91 {
92         char *buf;
93         int i;
94         buf = (char *)malloc(len+1);
95
96         for (i=0;i<len;i++) {
97                 buf[i] = 'a' + (rand() % 26);
98         }
99         buf[i] = 0;
100         return buf;
101 }
102
103 static void addrec_db(void);
104 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
105                            void *state)
106 {
107 #if CULL_PROB
108         if (random() % CULL_PROB == 0) {
109                 tdb_delete(tdb, key);
110         }
111 #endif
112
113 #if TRAVERSE_MOD_PROB
114         if (random() % TRAVERSE_MOD_PROB == 0) {
115                 addrec_db();
116         }
117 #endif
118
119 #if TRAVERSE_ABORT_PROB
120         if (random() % TRAVERSE_ABORT_PROB == 0)
121                 return 1;
122 #endif
123
124         return 0;
125 }
126
127 static void addrec_db(void)
128 {
129         int klen, dlen;
130         char *k, *d;
131         TDB_DATA key, data;
132
133         klen = 1 + (rand() % KEYLEN);
134         dlen = 1 + (rand() % DATALEN);
135
136         k = randbuf(klen);
137         d = randbuf(dlen);
138
139         key.dptr = (unsigned char *)k;
140         key.dsize = klen+1;
141
142         data.dptr = (unsigned char *)d;
143         data.dsize = dlen+1;
144
145 #if REOPEN_PROB
146         if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
147                 tdb_reopen_all(0);
148                 goto next;
149         } 
150 #endif
151
152 #if TRANSACTION_PROB
153         if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
154                 if (tdb_transaction_start(db) != 0) {
155                         fatal("tdb_transaction_start failed");
156                 }
157                 in_transaction++;
158                 goto next;
159         }
160         if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
161                 if (random() % TRANSACTION_PREPARE_PROB == 0) {
162                         if (tdb_transaction_prepare_commit(db) != 0) {
163                                 fatal("tdb_transaction_prepare_commit failed");
164                         }
165                 }
166                 if (tdb_transaction_commit(db) != 0) {
167                         fatal("tdb_transaction_commit failed");
168                 }
169                 in_transaction--;
170                 goto next;
171         }
172
173         if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
174                 tdb_transaction_cancel(db);
175                 in_transaction--;
176                 goto next;
177         }
178 #endif
179
180 #if DELETE_PROB
181         if (random() % DELETE_PROB == 0) {
182                 tdb_delete(db, key);
183                 goto next;
184         }
185 #endif
186
187 #if STORE_PROB
188         if (random() % STORE_PROB == 0) {
189                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
190                         fatal("tdb_store failed");
191                 }
192                 goto next;
193         }
194 #endif
195
196 #if APPEND_PROB
197         if (random() % APPEND_PROB == 0) {
198                 if (tdb_append(db, key, data) != 0) {
199                         fatal("tdb_append failed");
200                 }
201                 goto next;
202         }
203 #endif
204
205 #if LOCKSTORE_PROB
206         if (random() % LOCKSTORE_PROB == 0) {
207                 tdb_chainlock(db, key);
208                 data = tdb_fetch(db, key);
209                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
210                         fatal("tdb_store failed");
211                 }
212                 if (data.dptr) free(data.dptr);
213                 tdb_chainunlock(db, key);
214                 goto next;
215         } 
216 #endif
217
218 #if TRAVERSE_PROB
219         /* FIXME: recursive traverses break transactions? */
220         if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
221                 in_traverse++;
222                 tdb_traverse(db, modify_traverse, NULL);
223                 in_traverse--;
224                 goto next;
225         }
226 #endif
227
228 #if TRAVERSE_READ_PROB
229         if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
230                 in_traverse++;
231                 tdb_traverse_read(db, NULL, NULL);
232                 in_traverse--;
233                 goto next;
234         }
235 #endif
236
237         data = tdb_fetch(db, key);
238         if (data.dptr) free(data.dptr);
239
240 next:
241         free(k);
242         free(d);
243 }
244
245 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
246                        void *state)
247 {
248         tdb_delete(tdb, key);
249         return 0;
250 }
251
252 static void usage(void)
253 {
254         printf("Usage: tdbtorture"
255 #if TRANSACTION_PROB
256                " [-t]"
257 #endif
258                " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n");
259         exit(0);
260 }
261
262 static void send_count_and_suicide(int sig)
263 {
264         /* This ensures our successor can continue where we left off. */
265         write(count_pipe, &loopnum, sizeof(loopnum));
266         /* This gives a unique signature. */
267         kill(getpid(), SIGUSR2);
268 }
269
270 static int run_child(int i, int seed, unsigned num_loops, unsigned start)
271 {
272         struct sigaction act = { .sa_sigaction = segv_handler,
273                                  .sa_flags = SA_SIGINFO };
274         sigaction(11, &act, NULL);      
275
276         db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600,
277                       &log_attr);
278         if (!db) {
279                 fatal("db open failed");
280         }
281
282 #if 0
283         if (i == 0) {
284                 printf("pid %i\n", getpid());
285                 sleep(9);
286         } else
287                 sleep(10);
288 #endif
289
290         srand(seed + i);
291         srandom(seed + i);
292
293         /* Set global, then we're ready to handle being killed. */
294         loopnum = start;
295         signal(SIGUSR1, send_count_and_suicide);
296
297         for (;loopnum<num_loops && error_count == 0;loopnum++) {
298                 addrec_db();
299         }
300
301         if (error_count == 0) {
302                 tdb_traverse_read(db, NULL, NULL);
303 #if TRANSACTION_PROB
304                 if (always_transaction) {
305                         while (in_transaction) {
306                                 tdb_transaction_cancel(db);
307                                 in_transaction--;
308                         }
309                         if (tdb_transaction_start(db) != 0)
310                                 fatal("tdb_transaction_start failed");
311                 }
312 #endif
313                 tdb_traverse(db, traverse_fn, NULL);
314                 tdb_traverse(db, traverse_fn, NULL);
315
316 #if TRANSACTION_PROB
317                 if (always_transaction) {
318                         if (tdb_transaction_commit(db) != 0)
319                                 fatal("tdb_transaction_commit failed");
320                 }
321 #endif
322         }
323
324         tdb_close(db);
325
326         return (error_count < 100 ? error_count : 100);
327 }
328
329 int main(int argc, char * const *argv)
330 {
331         int i, seed = -1;
332         int num_loops = 5000;
333         int num_procs = 3;
334         int c, pfds[2];
335         extern char *optarg;
336         pid_t *pids;
337         int kill_random = 0;
338         int *done;
339
340         log_attr.base.attr = TDB_ATTRIBUTE_LOG;
341         log_attr.base.next = &seed_attr;
342         log_attr.log.log_fn = tdb_log;
343         seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
344
345         while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) {
346                 switch (c) {
347                 case 'n':
348                         num_procs = strtol(optarg, NULL, 0);
349                         break;
350                 case 'l':
351                         num_loops = strtol(optarg, NULL, 0);
352                         break;
353                 case 's':
354                         seed = strtol(optarg, NULL, 0);
355                         break;
356                 case 't':
357 #if TRANSACTION_PROB
358                         always_transaction = 1;
359 #else
360                         fprintf(stderr, "Transactions not supported\n");
361                         usage();
362 #endif
363                         break;
364                 case 'k':
365                         kill_random = 1;
366                         break;
367                 default:
368                         usage();
369                 }
370         }
371
372         unlink("torture.tdb");
373
374         if (seed == -1) {
375                 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
376         }
377         seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed; 
378
379         if (num_procs == 1 && !kill_random) {
380                 /* Don't fork for this case, makes debugging easier. */
381                 error_count = run_child(0, seed, num_loops, 0);
382                 goto done;
383         }
384
385         pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
386         done = (int *)calloc(sizeof(int), num_procs);
387
388         if (pipe(pfds) != 0) {
389                 perror("Creating pipe");
390                 exit(1);
391         }
392         count_pipe = pfds[1];
393
394         for (i=0;i<num_procs;i++) {
395                 if ((pids[i]=fork()) == 0) {
396                         close(pfds[0]);
397                         if (i == 0) {
398                                 printf("testing with %d processes, %d loops, seed=%d%s\n", 
399                                        num_procs, num_loops, seed, 
400 #if TRANSACTION_PROB
401                                        always_transaction ? " (all within transactions)" : ""
402 #else
403                                        ""
404 #endif
405                                         );
406                         }
407                         exit(run_child(i, seed, num_loops, 0));
408                 }
409         }
410
411         while (num_procs) {
412                 int status, j;
413                 pid_t pid;
414
415                 if (error_count != 0) {
416                         /* try and stop the test on any failure */
417                         for (j=0;j<num_procs;j++) {
418                                 if (pids[j] != 0) {
419                                         kill(pids[j], SIGTERM);
420                                 }
421                         }
422                 }
423
424                 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
425                 if (pid == 0) {
426                         struct timespec ts;
427
428                         /* Sleep for 1/10 second. */
429                         ts.tv_sec = 0;
430                         ts.tv_nsec = 100000000;
431                         nanosleep(&ts, NULL);
432
433                         /* Kill someone. */
434                         kill(pids[random() % num_procs], SIGUSR1);
435                         continue;
436                 }
437
438                 if (pid == -1) {
439                         perror("failed to wait for child\n");
440                         exit(1);
441                 }
442
443                 for (j=0;j<num_procs;j++) {
444                         if (pids[j] == pid) break;
445                 }
446                 if (j == num_procs) {
447                         printf("unknown child %d exited!?\n", (int)pid);
448                         exit(1);
449                 }
450                 if (WIFSIGNALED(status)) {
451                         if (WTERMSIG(status) == SIGUSR2
452                             || WTERMSIG(status) == SIGUSR1) {
453                                 /* SIGUSR2 means they wrote to pipe. */
454                                 if (WTERMSIG(status) == SIGUSR2) {
455                                         read(pfds[0], &done[j],
456                                              sizeof(done[j]));
457                                 }
458                                 pids[j] = fork();
459                                 if (pids[j] == 0)
460                                         exit(run_child(j, seed, num_loops,
461                                                        done[j]));
462                                 printf("Restarting child %i for %u-%u\n",
463                                        j, done[j], num_loops);
464                                 continue;
465                         }
466                         printf("child %d exited with signal %d\n",
467                                (int)pid, WTERMSIG(status));
468                         error_count++;
469                 } else {
470                         if (WEXITSTATUS(status) != 0) {
471                                 printf("child %d exited with status %d\n",
472                                        (int)pid, WEXITSTATUS(status));
473                                 error_count++;
474                         }
475                 }
476                 memmove(&pids[j], &pids[j+1],
477                         (num_procs - j - 1)*sizeof(pids[0]));
478                 num_procs--;
479         }
480
481         free(pids);
482
483 done:
484         if (error_count == 0) {
485                 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
486                               0600, &log_attr);
487                 if (!db) {
488                         fatal("db open failed");
489                 }
490                 if (tdb_check(db, NULL, NULL) == -1) {
491                         printf("db check failed");
492                         exit(1);
493                 }
494                 tdb_close(db);
495                 printf("OK\n");
496         }
497
498         return error_count;
499 }