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