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