]> git.ozlabs.org Git - ccan/blob - ccan/tdb/tools/tdbtorture.c
Merge.
[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
146         if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
147                 if (tdb_transaction_cancel(db) != 0) {
148                         fatal("tdb_transaction_cancel failed");
149                 }
150                 in_transaction--;
151                 goto next;
152         }
153 #endif
154
155 #if REOPEN_PROB
156         if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
157                 tdb_reopen_all(0);
158                 goto next;
159         } 
160 #endif
161
162 #if DELETE_PROB
163         if (random() % DELETE_PROB == 0) {
164                 tdb_delete(db, key);
165                 goto next;
166         }
167 #endif
168
169 #if STORE_PROB
170         if (random() % STORE_PROB == 0) {
171                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
172                         fatal("tdb_store failed");
173                 }
174                 goto next;
175         }
176 #endif
177
178 #if APPEND_PROB
179         if (random() % APPEND_PROB == 0) {
180                 if (tdb_append(db, key, data) != 0) {
181                         fatal("tdb_append failed");
182                 }
183                 goto next;
184         }
185 #endif
186
187 #if LOCKSTORE_PROB
188         if (random() % LOCKSTORE_PROB == 0) {
189                 tdb_chainlock(db, key);
190                 data = tdb_fetch(db, key);
191                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
192                         fatal("tdb_store failed");
193                 }
194                 if (data.dptr) free(data.dptr);
195                 tdb_chainunlock(db, key);
196                 goto next;
197         } 
198 #endif
199
200 #if TRAVERSE_PROB
201         /* FIXME: recursive traverses break transactions? */
202         if (in_traverse == 0 && random() % TRAVERSE_PROB == 0) {
203                 in_traverse++;
204                 tdb_traverse(db, modify_traverse, NULL);
205                 in_traverse--;
206                 goto next;
207         }
208 #endif
209
210 #if TRAVERSE_READ_PROB
211         if (in_traverse == 0 && random() % TRAVERSE_READ_PROB == 0) {
212                 in_traverse++;
213                 tdb_traverse_read(db, NULL, NULL);
214                 in_traverse--;
215                 goto next;
216         }
217 #endif
218
219         data = tdb_fetch(db, key);
220         if (data.dptr) free(data.dptr);
221
222 next:
223         free(k);
224         free(d);
225 }
226
227 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
228                        void *state)
229 {
230         tdb_delete(tdb, key);
231         return 0;
232 }
233
234 static void usage(void)
235 {
236         printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
237         exit(0);
238 }
239
240 int main(int argc, char * const *argv)
241 {
242         int i, seed = -1;
243         int num_procs = 3;
244         int num_loops = 5000;
245         int hash_size = 2;
246         int c;
247         extern char *optarg;
248         pid_t *pids;
249
250         struct tdb_logging_context log_ctx;
251         log_ctx.log_fn = tdb_log;
252
253         while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
254                 switch (c) {
255                 case 'n':
256                         num_procs = strtol(optarg, NULL, 0);
257                         break;
258                 case 'l':
259                         num_loops = strtol(optarg, NULL, 0);
260                         break;
261                 case 'H':
262                         hash_size = strtol(optarg, NULL, 0);
263                         break;
264                 case 's':
265                         seed = strtol(optarg, NULL, 0);
266                         break;
267                 default:
268                         usage();
269                 }
270         }
271
272         unlink("torture.tdb");
273
274         pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
275         pids[0] = getpid();
276
277         for (i=0;i<num_procs-1;i++) {
278                 if ((pids[i+1]=fork()) == 0) break;
279         }
280
281         db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST, 
282                          O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
283         if (!db) {
284                 fatal("db open failed");
285         }
286
287         if (seed == -1) {
288                 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
289         }
290
291         if (i == 0) {
292                 printf("testing with %d processes, %d loops, %d hash_size, seed=%d\n", 
293                        num_procs, num_loops, hash_size, seed);
294         }
295
296         srand(seed + i);
297         srandom(seed + i);
298
299         for (i=0;i<num_loops && error_count == 0;i++) {
300                 addrec_db();
301         }
302
303         if (error_count == 0) {
304                 tdb_traverse_read(db, NULL, NULL);
305                 tdb_traverse(db, traverse_fn, NULL);
306                 tdb_traverse(db, traverse_fn, NULL);
307         }
308
309         tdb_close(db);
310
311         if (getpid() != pids[0]) {
312                 return error_count;
313         }
314
315         for (i=1;i<num_procs;i++) {
316                 int status, j;
317                 pid_t pid;
318                 if (error_count != 0) {
319                         /* try and stop the test on any failure */
320                         for (j=1;j<num_procs;j++) {
321                                 if (pids[j] != 0) {
322                                         kill(pids[j], SIGTERM);
323                                 }
324                         }
325                 }
326                 pid = waitpid(-1, &status, 0);
327                 if (pid == -1) {
328                         perror("failed to wait for child\n");
329                         exit(1);
330                 }
331                 for (j=1;j<num_procs;j++) {
332                         if (pids[j] == pid) break;
333                 }
334                 if (j == num_procs) {
335                         printf("unknown child %d exited!?\n", (int)pid);
336                         exit(1);
337                 }
338                 if (WEXITSTATUS(status) != 0) {
339                         printf("child %d exited with status %d\n",
340                                (int)pid, WEXITSTATUS(status));
341                         error_count++;
342                 }
343                 pids[j] = 0;
344         }
345
346         free(pids);
347
348         if (error_count == 0) {
349                 printf("OK\n");
350         }
351
352         return error_count;
353 }