tdb2: rearrange log function to put data arg at the end.
[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                     const char *message, void *data)
47 {
48         fputs(message, stdout);
49         fflush(stdout);
50 #if 0
51         {
52                 char str[200];
53                 signal(SIGUSR1, SIG_IGN);
54                 sprintf(str,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
55                 system(str);
56         }
57 #endif  
58 }
59
60 #include "../private.h"
61
62 static void segv_handler(int sig, siginfo_t *info, void *p)
63 {
64         char string[100];
65
66         sprintf(string, "%u: death at %p (map_ptr %p, map_size %zu)\n",
67                 getpid(), info->si_addr, db->file->map_ptr,
68                 (size_t)db->file->map_size);
69         if (write(2, string, strlen(string)) > 0)
70                 sleep(60);
71         _exit(11);
72 }       
73
74 static void fatal(struct tdb_context *tdb, const char *why)
75 {
76         fprintf(stderr, "%u:%s:%s\n", getpid(), why,
77                 tdb ? tdb_errorstr(tdb_error(tdb)) : "(no tdb)");
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(db, "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(db, "tdb_transaction_prepare_commit failed");
155                         }
156                 }
157                 if (tdb_transaction_commit(db) != 0) {
158                         fatal(db, "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(db, "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(db, "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                 if (tdb_fetch(db, key, &data) != TDB_SUCCESS) {
200                         data.dsize = 0;
201                         data.dptr = NULL;
202                 }
203                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
204                         fatal(db, "tdb_store failed");
205                 }
206                 if (data.dptr) free(data.dptr);
207                 tdb_chainunlock(db, key);
208                 goto next;
209         } 
210 #endif
211
212 #if TRAVERSE_PROB
213         /* FIXME: recursive traverses break transactions? */
214         if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
215                 in_traverse++;
216                 tdb_traverse(db, modify_traverse, NULL);
217                 in_traverse--;
218                 goto next;
219         }
220 #endif
221
222         if (tdb_fetch(db, key, &data) == TDB_SUCCESS)
223                 free(data.dptr);
224
225 next:
226         free(k);
227         free(d);
228 }
229
230 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
231                        void *state)
232 {
233         tdb_delete(tdb, key);
234         return 0;
235 }
236
237 static void usage(void)
238 {
239         printf("Usage: tdbtorture"
240 #if TRANSACTION_PROB
241                " [-t]"
242 #endif
243                " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-S]\n");
244         exit(0);
245 }
246
247 static void send_count_and_suicide(int sig)
248 {
249         /* This ensures our successor can continue where we left off. */
250         if (write(count_pipe, &loopnum, sizeof(loopnum)) != sizeof(loopnum))
251                 exit(2);
252         /* This gives a unique signature. */
253         kill(getpid(), SIGUSR2);
254 }
255
256 static int run_child(int i, int seed, unsigned num_loops, unsigned start,
257                      int tdb_flags)
258 {
259         struct sigaction act = { .sa_sigaction = segv_handler,
260                                  .sa_flags = SA_SIGINFO };
261         sigaction(11, &act, NULL);      
262
263         db = tdb_open("torture.tdb", tdb_flags, O_RDWR | O_CREAT, 0600,
264                       &log_attr);
265         if (!db) {
266                 fatal(NULL, "db open failed");
267         }
268
269 #if 0
270         if (i == 0) {
271                 printf("pid %i\n", getpid());
272                 sleep(9);
273         } else
274                 sleep(10);
275 #endif
276
277         srand(seed + i);
278         srandom(seed + i);
279
280         /* Set global, then we're ready to handle being killed. */
281         loopnum = start;
282         signal(SIGUSR1, send_count_and_suicide);
283
284         for (;loopnum<num_loops && error_count == 0;loopnum++) {
285                 addrec_db();
286         }
287
288         if (error_count == 0) {
289                 tdb_traverse(db, NULL, NULL);
290 #if TRANSACTION_PROB
291                 if (always_transaction) {
292                         while (in_transaction) {
293                                 tdb_transaction_cancel(db);
294                                 in_transaction--;
295                         }
296                         if (tdb_transaction_start(db) != 0)
297                                 fatal(db, "tdb_transaction_start failed");
298                 }
299 #endif
300                 tdb_traverse(db, traverse_fn, NULL);
301                 tdb_traverse(db, traverse_fn, NULL);
302
303 #if TRANSACTION_PROB
304                 if (always_transaction) {
305                         if (tdb_transaction_commit(db) != 0)
306                                 fatal(db, "tdb_transaction_commit failed");
307                 }
308 #endif
309         }
310
311         tdb_close(db);
312
313         return (error_count < 100 ? error_count : 100);
314 }
315
316 int main(int argc, char * const *argv)
317 {
318         int i, seed = -1;
319         int num_loops = 5000;
320         int num_procs = 3;
321         int c, pfds[2];
322         extern char *optarg;
323         pid_t *pids;
324         int kill_random = 0;
325         int *done;
326         int tdb_flags = TDB_DEFAULT;
327
328         log_attr.base.attr = TDB_ATTRIBUTE_LOG;
329         log_attr.base.next = &seed_attr;
330         log_attr.log.fn = tdb_log;
331         seed_attr.base.attr = TDB_ATTRIBUTE_SEED;
332
333         while ((c = getopt(argc, argv, "n:l:s:thkS")) != -1) {
334                 switch (c) {
335                 case 'n':
336                         num_procs = strtol(optarg, NULL, 0);
337                         break;
338                 case 'l':
339                         num_loops = strtol(optarg, NULL, 0);
340                         break;
341                 case 's':
342                         seed = strtol(optarg, NULL, 0);
343                         break;
344                 case 'S':
345                         tdb_flags = TDB_NOSYNC;
346                         break;
347                 case 't':
348 #if TRANSACTION_PROB
349                         always_transaction = 1;
350 #else
351                         fprintf(stderr, "Transactions not supported\n");
352                         usage();
353 #endif
354                         break;
355                 case 'k':
356                         kill_random = 1;
357                         break;
358                 default:
359                         usage();
360                 }
361         }
362
363         unlink("torture.tdb");
364
365         if (seed == -1) {
366                 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
367         }
368         seed_attr.seed.seed = (((uint64_t)seed) << 32) | seed; 
369
370         if (num_procs == 1 && !kill_random) {
371                 /* Don't fork for this case, makes debugging easier. */
372                 error_count = run_child(0, seed, num_loops, 0, tdb_flags);
373                 goto done;
374         }
375
376         pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
377         done = (int *)calloc(sizeof(int), num_procs);
378
379         if (pipe(pfds) != 0) {
380                 perror("Creating pipe");
381                 exit(1);
382         }
383         count_pipe = pfds[1];
384
385         for (i=0;i<num_procs;i++) {
386                 if ((pids[i]=fork()) == 0) {
387                         close(pfds[0]);
388                         if (i == 0) {
389                                 printf("testing with %d processes, %d loops, seed=%d%s\n", 
390                                        num_procs, num_loops, seed, 
391 #if TRANSACTION_PROB
392                                        always_transaction ? " (all within transactions)" : ""
393 #else
394                                        ""
395 #endif
396                                         );
397                         }
398                         exit(run_child(i, seed, num_loops, 0, tdb_flags));
399                 }
400         }
401
402         while (num_procs) {
403                 int status, j;
404                 pid_t pid;
405
406                 if (error_count != 0) {
407                         /* try and stop the test on any failure */
408                         for (j=0;j<num_procs;j++) {
409                                 if (pids[j] != 0) {
410                                         kill(pids[j], SIGTERM);
411                                 }
412                         }
413                 }
414
415                 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
416                 if (pid == 0) {
417                         struct timespec ts;
418
419                         /* Sleep for 1/10 second. */
420                         ts.tv_sec = 0;
421                         ts.tv_nsec = 100000000;
422                         nanosleep(&ts, NULL);
423
424                         /* Kill someone. */
425                         kill(pids[random() % num_procs], SIGUSR1);
426                         continue;
427                 }
428
429                 if (pid == -1) {
430                         perror("failed to wait for child\n");
431                         exit(1);
432                 }
433
434                 for (j=0;j<num_procs;j++) {
435                         if (pids[j] == pid) break;
436                 }
437                 if (j == num_procs) {
438                         printf("unknown child %d exited!?\n", (int)pid);
439                         exit(1);
440                 }
441                 if (WIFSIGNALED(status)) {
442                         if (WTERMSIG(status) == SIGUSR2
443                             || WTERMSIG(status) == SIGUSR1) {
444                                 /* SIGUSR2 means they wrote to pipe. */
445                                 if (WTERMSIG(status) == SIGUSR2) {
446                                         if (read(pfds[0], &done[j],
447                                                  sizeof(done[j]))
448                                             != sizeof(done[j]))
449                                                 err(1,
450                                                     "Short read from child?");
451                                 }
452                                 pids[j] = fork();
453                                 if (pids[j] == 0)
454                                         exit(run_child(j, seed, num_loops,
455                                                        done[j], tdb_flags));
456                                 printf("Restarting child %i for %u-%u\n",
457                                        j, done[j], num_loops);
458                                 continue;
459                         }
460                         printf("child %d exited with signal %d\n",
461                                (int)pid, WTERMSIG(status));
462                         error_count++;
463                 } else {
464                         if (WEXITSTATUS(status) != 0) {
465                                 printf("child %d exited with status %d\n",
466                                        (int)pid, WEXITSTATUS(status));
467                                 error_count++;
468                         }
469                 }
470                 memmove(&pids[j], &pids[j+1],
471                         (num_procs - j - 1)*sizeof(pids[0]));
472                 num_procs--;
473         }
474
475         free(pids);
476
477 done:
478         if (error_count == 0) {
479                 db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT,
480                               0600, &log_attr);
481                 if (!db) {
482                         fatal(db, "db open failed");
483                         exit(1);
484                 }
485                 if (tdb_check(db, NULL, NULL) != 0) {
486                         fatal(db, "db check failed");
487                         exit(1);
488                 }
489                 tdb_close(db);
490                 printf("OK\n");
491         }
492
493         return error_count;
494 }