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