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