]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tools/tdbtorture.c
84223aa32a0ec2ac8ff2aa8ee11d47ff06f44d16
[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
45 #ifdef PRINTF_ATTRIBUTE
46 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...) PRINTF_ATTRIBUTE(4,5);
47 #endif
48 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, void *private, const char *format, ...)
49 {
50         va_list ap;
51
52         if (level != TDB_DEBUG_TRACE)
53                 error_count++;
54
55         va_start(ap, format);
56         vfprintf(stdout, format, ap);
57         va_end(ap);
58         fflush(stdout);
59 #if 0
60         if (level != TDB_DEBUG_TRACE) {
61                 char *ptr;
62                 signal(SIGUSR1, SIG_IGN);
63                 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
64                 system(ptr);
65                 free(ptr);
66         }
67 #endif  
68 }
69
70 #include "../private.h"
71
72 static void segv_handler(int signal, siginfo_t *info, void *p)
73 {
74         char string[100];
75
76         sprintf(string, "%u: death at %p (map_ptr %p, map_size %llu)\n",
77                 getpid(), info->si_addr, db->map_ptr, db->map_size);
78         write(2, string, strlen(string));
79         sleep(60);
80         _exit(11);
81 }       
82
83 static void fatal(const char *why)
84 {
85         perror(why);
86         error_count++;
87 }
88
89 static char *randbuf(int len)
90 {
91         char *buf;
92         int i;
93         buf = (char *)malloc(len+1);
94
95         for (i=0;i<len;i++) {
96                 buf[i] = 'a' + (rand() % 26);
97         }
98         buf[i] = 0;
99         return buf;
100 }
101
102 static void addrec_db(void);
103 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
104                            void *state)
105 {
106 #if CULL_PROB
107         if (random() % CULL_PROB == 0) {
108                 tdb_delete(tdb, key);
109         }
110 #endif
111
112 #if TRAVERSE_MOD_PROB
113         if (random() % TRAVERSE_MOD_PROB == 0) {
114                 addrec_db();
115         }
116 #endif
117
118 #if TRAVERSE_ABORT_PROB
119         if (random() % TRAVERSE_ABORT_PROB == 0)
120                 return 1;
121 #endif
122
123         return 0;
124 }
125
126 static void addrec_db(void)
127 {
128         int klen, dlen;
129         char *k, *d;
130         TDB_DATA key, data;
131
132         klen = 1 + (rand() % KEYLEN);
133         dlen = 1 + (rand() % DATALEN);
134
135         k = randbuf(klen);
136         d = randbuf(dlen);
137
138         key.dptr = (unsigned char *)k;
139         key.dsize = klen+1;
140
141         data.dptr = (unsigned char *)d;
142         data.dsize = dlen+1;
143
144 #if REOPEN_PROB
145         if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
146                 tdb_reopen_all(0);
147                 goto next;
148         } 
149 #endif
150
151 #if TRANSACTION_PROB
152         if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
153                 if (tdb_transaction_start(db) != 0) {
154                         fatal("tdb_transaction_start failed");
155                 }
156                 in_transaction++;
157                 goto next;
158         }
159         if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
160                 if (random() % TRANSACTION_PREPARE_PROB == 0) {
161                         if (tdb_transaction_prepare_commit(db) != 0) {
162                                 fatal("tdb_transaction_prepare_commit failed");
163                         }
164                 }
165                 if (tdb_transaction_commit(db) != 0) {
166                         fatal("tdb_transaction_commit failed");
167                 }
168                 in_transaction--;
169                 goto next;
170         }
171
172         if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
173                 if (tdb_transaction_cancel(db) != 0) {
174                         fatal("tdb_transaction_cancel failed");
175                 }
176                 in_transaction--;
177                 goto next;
178         }
179 #endif
180
181 #if DELETE_PROB
182         if (random() % DELETE_PROB == 0) {
183                 tdb_delete(db, key);
184                 goto next;
185         }
186 #endif
187
188 #if STORE_PROB
189         if (random() % STORE_PROB == 0) {
190                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
191                         fatal("tdb_store failed");
192                 }
193                 goto next;
194         }
195 #endif
196
197 #if APPEND_PROB
198         if (random() % APPEND_PROB == 0) {
199                 if (tdb_append(db, key, data) != 0) {
200                         fatal("tdb_append failed");
201                 }
202                 goto next;
203         }
204 #endif
205
206 #if LOCKSTORE_PROB
207         if (random() % LOCKSTORE_PROB == 0) {
208                 tdb_chainlock(db, key);
209                 data = tdb_fetch(db, key);
210                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
211                         fatal("tdb_store failed");
212                 }
213                 if (data.dptr) free(data.dptr);
214                 tdb_chainunlock(db, key);
215                 goto next;
216         } 
217 #endif
218
219 #if TRAVERSE_PROB
220         /* FIXME: recursive traverses break transactions? */
221         if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
222                 in_traverse++;
223                 tdb_traverse(db, modify_traverse, NULL);
224                 in_traverse--;
225                 goto next;
226         }
227 #endif
228
229 #if TRAVERSE_READ_PROB
230         if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
231                 in_traverse++;
232                 tdb_traverse_read(db, NULL, NULL);
233                 in_traverse--;
234                 goto next;
235         }
236 #endif
237
238         data = tdb_fetch(db, key);
239         if (data.dptr) free(data.dptr);
240
241 next:
242         free(k);
243         free(d);
244 }
245
246 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
247                        void *state)
248 {
249         tdb_delete(tdb, key);
250         return 0;
251 }
252
253 static void usage(void)
254 {
255         printf("Usage: tdbtorture"
256 #if TRANSACTION_PROB
257                " [-t]"
258 #endif
259                " [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED]\n");
260         exit(0);
261 }
262
263 static void send_count_and_suicide(int sig)
264 {
265         /* This ensures our successor can continue where we left off. */
266         write(count_pipe, &loopnum, sizeof(loopnum));
267         /* This gives a unique signature. */
268         kill(getpid(), SIGUSR2);
269 }
270
271 static int run_child(int i, int seed, unsigned num_loops, unsigned start)
272 {
273         struct sigaction act = { .sa_sigaction = segv_handler,
274                                  .sa_flags = SA_SIGINFO };
275         sigaction(11, &act, NULL);      
276
277         db = tdb_open("torture.tdb", TDB_DEFAULT, O_RDWR | O_CREAT, 0600,
278                       &log_attr);
279         if (!db) {
280                 fatal("db open failed");
281         }
282
283 #if 0
284         if (i == 0) {
285                 printf("pid %i\n", getpid());
286                 sleep(9);
287         } else
288                 sleep(10);
289 #endif
290
291         srand(seed + i);
292         srandom(seed + i);
293
294         /* Set global, then we're ready to handle being killed. */
295         loopnum = start;
296         signal(SIGUSR1, send_count_and_suicide);
297
298         for (;loopnum<num_loops && error_count == 0;loopnum++) {
299                 addrec_db();
300         }
301
302         if (error_count == 0) {
303                 tdb_traverse_read(db, NULL, NULL);
304 #if TRANSACTION_PROB
305                 if (always_transaction) {
306                         while (in_transaction) {
307                                 tdb_transaction_cancel(db);
308                                 in_transaction--;
309                         }
310                         if (tdb_transaction_start(db) != 0)
311                                 fatal("tdb_transaction_start failed");
312                 }
313 #endif
314                 tdb_traverse(db, traverse_fn, NULL);
315                 tdb_traverse(db, traverse_fn, NULL);
316
317 #if TRANSACTION_PROB
318                 if (always_transaction) {
319                         if (tdb_transaction_commit(db) != 0)
320                                 fatal("tdb_transaction_commit failed");
321                 }
322 #endif
323         }
324
325         tdb_close(db);
326
327         return (error_count < 100 ? error_count : 100);
328 }
329
330 int main(int argc, char * const *argv)
331 {
332         int i, seed = -1;
333         int num_loops = 5000;
334         int num_procs = 3;
335         int c, pfds[2];
336         extern char *optarg;
337         pid_t *pids;
338         int kill_random = 0;
339         int *done;
340
341         log_attr.log.log_fn = tdb_log;
342
343         while ((c = getopt(argc, argv, "n:l:s:thk")) != -1) {
344                 switch (c) {
345                 case 'n':
346                         num_procs = strtol(optarg, NULL, 0);
347                         break;
348                 case 'l':
349                         num_loops = strtol(optarg, NULL, 0);
350                         break;
351                 case 's':
352                         seed = strtol(optarg, NULL, 0);
353                         break;
354                 case 't':
355 #if TRANSACTION_PROB
356                         always_transaction = 1;
357 #else
358                         fprintf(stderr, "Transactions not supported\n");
359                         usage();
360 #endif
361                         break;
362                 case 'k':
363                         kill_random = 1;
364                         break;
365                 default:
366                         usage();
367                 }
368         }
369
370         unlink("torture.tdb");
371
372         if (seed == -1) {
373                 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
374         }
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 }