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