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