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