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