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