]> git.ozlabs.org Git - ccan/blob - ccan/tdb/tools/tdbtorture.c
Torture harder: random ops inside traverses.
[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
39 #ifdef PRINTF_ATTRIBUTE
40 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
41 #endif
42 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
43 {
44         va_list ap;
45     
46         error_count++;
47
48         va_start(ap, format);
49         vfprintf(stdout, format, ap);
50         va_end(ap);
51         fflush(stdout);
52 #if 0
53         {
54                 char *ptr;
55                 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
56                 system(ptr);
57                 free(ptr);
58         }
59 #endif  
60 }
61
62 static void fatal(const char *why)
63 {
64         perror(why);
65         error_count++;
66 }
67
68 static char *randbuf(int len)
69 {
70         char *buf;
71         int i;
72         buf = (char *)malloc(len+1);
73
74         for (i=0;i<len;i++) {
75                 buf[i] = 'a' + (rand() % 26);
76         }
77         buf[i] = 0;
78         return buf;
79 }
80
81 static void addrec_db(void);
82 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
83                            void *state)
84 {
85 #if CULL_PROB
86         if (random() % CULL_PROB == 0) {
87                 tdb_delete(tdb, key);
88         }
89 #endif
90
91 #if TRAVERSE_MOD_PROB
92         if (random() % TRAVERSE_MOD_PROB == 0) {
93                 addrec_db();
94         }
95 #endif
96
97 #if TRAVERSE_ABORT_PROB
98         if (random() % TRAVERSE_ABORT_PROB == 0)
99                 return 1;
100 #endif
101
102         return 0;
103 }
104
105 static void addrec_db(void)
106 {
107         int klen, dlen;
108         char *k, *d;
109         TDB_DATA key, data;
110
111         klen = 1 + (rand() % KEYLEN);
112         dlen = 1 + (rand() % DATALEN);
113
114         k = randbuf(klen);
115         d = randbuf(dlen);
116
117         key.dptr = (unsigned char *)k;
118         key.dsize = klen+1;
119
120         data.dptr = (unsigned char *)d;
121         data.dsize = dlen+1;
122
123 #if TRANSACTION_PROB
124         if (in_traverse == 0 && in_transaction == 0 && random() % TRANSACTION_PROB == 0) {
125                 if (tdb_transaction_start(db) != 0) {
126                         fatal("tdb_transaction_start failed");
127                 }
128                 in_transaction++;
129                 goto next;
130         }
131         if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
132 #if 0
133                 if (random() % TRANSACTION_PREPARE_PROB == 0) {
134                         if (tdb_transaction_prepare_commit(db) != 0) {
135                                 fatal("tdb_transaction_prepare_commit failed");
136                         }
137                 }
138 #endif
139                 if (tdb_transaction_commit(db) != 0) {
140                         fatal("tdb_transaction_commit failed");
141                 }
142                 in_transaction--;
143                 goto next;
144         }
145         if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
146                 if (tdb_transaction_cancel(db) != 0) {
147                         fatal("tdb_transaction_cancel failed");
148                 }
149                 in_transaction--;
150                 goto next;
151         }
152 #endif
153
154 #if REOPEN_PROB
155         if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
156                 tdb_reopen_all(0);
157                 goto next;
158         } 
159 #endif
160
161 #if DELETE_PROB
162         if (random() % DELETE_PROB == 0) {
163                 tdb_delete(db, key);
164                 goto next;
165         }
166 #endif
167
168 #if STORE_PROB
169         if (random() % STORE_PROB == 0) {
170                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
171                         fatal("tdb_store failed");
172                 }
173                 goto next;
174         }
175 #endif
176
177 #if APPEND_PROB
178         if (random() % APPEND_PROB == 0) {
179                 if (tdb_append(db, key, data) != 0) {
180                         fatal("tdb_append failed");
181                 }
182                 goto next;
183         }
184 #endif
185
186 #if LOCKSTORE_PROB
187         if (random() % LOCKSTORE_PROB == 0) {
188                 tdb_chainlock(db, key);
189                 data = tdb_fetch(db, key);
190                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
191                         fatal("tdb_store failed");
192                 }
193                 if (data.dptr) free(data.dptr);
194                 tdb_chainunlock(db, key);
195                 goto next;
196         } 
197 #endif
198
199 #if TRAVERSE_PROB
200         /* FIXME: recursive traverses break transactions? */
201         if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
202                 in_traverse++;
203                 tdb_traverse(db, modify_traverse, NULL);
204                 in_traverse--;
205                 goto next;
206         }
207 #endif
208
209 #if TRAVERSE_READ_PROB
210         if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
211                 in_traverse++;
212                 tdb_traverse_read(db, NULL, NULL);
213                 in_traverse--;
214                 goto next;
215         }
216 #endif
217
218         data = tdb_fetch(db, key);
219         if (data.dptr) free(data.dptr);
220
221 next:
222         free(k);
223         free(d);
224 }
225
226 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
227                        void *state)
228 {
229         tdb_delete(tdb, key);
230         return 0;
231 }
232
233 static void usage(void)
234 {
235         printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
236         exit(0);
237 }
238
239 int main(int argc, char * const *argv)
240 {
241         int i, seed = -1;
242         int num_procs = 3;
243         int num_loops = 5000;
244         int hash_size = 2;
245         int c;
246         extern char *optarg;
247         pid_t *pids;
248
249         struct tdb_logging_context log_ctx;
250         log_ctx.log_fn = tdb_log;
251
252         while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
253                 switch (c) {
254                 case 'n':
255                         num_procs = strtol(optarg, NULL, 0);
256                         break;
257                 case 'l':
258                         num_loops = strtol(optarg, NULL, 0);
259                         break;
260                 case 'H':
261                         hash_size = strtol(optarg, NULL, 0);
262                         break;
263                 case 's':
264                         seed = strtol(optarg, NULL, 0);
265                         break;
266                 default:
267                         usage();
268                 }
269         }
270
271         unlink("torture.tdb");
272
273         pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
274         pids[0] = getpid();
275
276         for (i=0;i<num_procs-1;i++) {
277                 if ((pids[i+1]=fork()) == 0) break;
278         }
279
280         db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST, 
281                          O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
282         if (!db) {
283                 fatal("db open failed");
284         }
285
286         if (seed == -1) {
287                 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
288         }
289
290         if (i == 0) {
291                 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n", 
292                        num_procs, num_loops, hash_size, seed);
293         }
294
295         srand(seed + i);
296         srandom(seed + i);
297
298         for (i=0;i<num_loops && error_count == 0;i++) {
299                 addrec_db();
300         }
301
302         if (error_count == 0) {
303                 tdb_traverse_read(db, NULL, NULL);
304                 tdb_traverse(db, traverse_fn, NULL);
305                 tdb_traverse(db, traverse_fn, NULL);
306         }
307
308         tdb_close(db);
309
310         if (getpid() != pids[0]) {
311                 return error_count;
312         }
313
314         for (i=1;i<num_procs;i++) {
315                 int status, j;
316                 pid_t pid;
317                 if (error_count != 0) {
318                         /* try and stop the test on any failure */
319                         for (j=1;j<num_procs;j++) {
320                                 if (pids[j] != 0) {
321                                         kill(pids[j], SIGTERM);
322                                 }
323                         }
324                 }
325                 pid = waitpid(-1, &status, 0);
326                 if (pid == -1) {
327                         perror("failed to wait for child\n");
328                         exit(1);
329                 }
330                 for (j=1;j<num_procs;j++) {
331                         if (pids[j] == pid) break;
332                 }
333                 if (j == num_procs) {
334                         printf("unknown child %d exited!?\n", (int)pid);
335                         exit(1);
336                 }
337                 if (WEXITSTATUS(status) != 0) {
338                         printf("child %d exited with status %d\n",
339                                (int)pid, WEXITSTATUS(status));
340                         error_count++;
341                 }
342                 pids[j] = 0;
343         }
344
345         free(pids);
346
347         if (error_count == 0) {
348                 printf("OK\n");
349         }
350
351         return error_count;
352 }