tdb: add -t (always transactional) to tdbtorture
[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 static int always_transaction = 0;
39
40 #ifdef PRINTF_ATTRIBUTE
41 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
42 #endif
43 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
44 {
45         va_list ap;
46     
47         error_count++;
48
49         va_start(ap, format);
50         vfprintf(stdout, format, ap);
51         va_end(ap);
52         fflush(stdout);
53 #if 0
54         {
55                 char *ptr;
56                 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
57                 system(ptr);
58                 free(ptr);
59         }
60 #endif  
61 }
62
63 static void fatal(const char *why)
64 {
65         perror(why);
66         error_count++;
67 }
68
69 static char *randbuf(int len)
70 {
71         char *buf;
72         int i;
73         buf = (char *)malloc(len+1);
74
75         for (i=0;i<len;i++) {
76                 buf[i] = 'a' + (rand() % 26);
77         }
78         buf[i] = 0;
79         return buf;
80 }
81
82 static void addrec_db(void);
83 static int modify_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
84                            void *state)
85 {
86 #if CULL_PROB
87         if (random() % CULL_PROB == 0) {
88                 tdb_delete(tdb, key);
89         }
90 #endif
91
92 #if TRAVERSE_MOD_PROB
93         if (random() % TRAVERSE_MOD_PROB == 0) {
94                 addrec_db();
95         }
96 #endif
97
98 #if TRAVERSE_ABORT_PROB
99         if (random() % TRAVERSE_ABORT_PROB == 0)
100                 return 1;
101 #endif
102
103         return 0;
104 }
105
106 static void addrec_db(void)
107 {
108         int klen, dlen;
109         char *k, *d;
110         TDB_DATA key, data;
111
112         klen = 1 + (rand() % KEYLEN);
113         dlen = 1 + (rand() % DATALEN);
114
115         k = randbuf(klen);
116         d = randbuf(dlen);
117
118         key.dptr = (unsigned char *)k;
119         key.dsize = klen+1;
120
121         data.dptr = (unsigned char *)d;
122         data.dsize = dlen+1;
123
124 #if REOPEN_PROB
125         if (in_traverse == 0 && in_transaction == 0 && random() % REOPEN_PROB == 0) {
126                 tdb_reopen_all(0);
127                 goto next;
128         } 
129 #endif
130
131 #if TRANSACTION_PROB
132         if (in_traverse == 0 && in_transaction == 0 && (always_transaction || random() % TRANSACTION_PROB == 0)) {
133                 if (tdb_transaction_start(db) != 0) {
134                         fatal("tdb_transaction_start failed");
135                 }
136                 in_transaction++;
137                 goto next;
138         }
139         if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
140                 if (random() % TRANSACTION_PREPARE_PROB == 0) {
141                         if (tdb_transaction_prepare_commit(db) != 0) {
142                                 fatal("tdb_transaction_prepare_commit failed");
143                         }
144                 }
145                 if (tdb_transaction_commit(db) != 0) {
146                         fatal("tdb_transaction_commit failed");
147                 }
148                 in_transaction--;
149                 goto next;
150         }
151
152         if (in_traverse == 0 && in_transaction && random() % TRANSACTION_PROB == 0) {
153                 if (tdb_transaction_cancel(db) != 0) {
154                         fatal("tdb_transaction_cancel failed");
155                 }
156                 in_transaction--;
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 [-t] [-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:th")) != -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                 case 't':
267                         always_transaction = 1;
268                         break;
269                 default:
270                         usage();
271                 }
272         }
273
274         unlink("torture.tdb");
275
276         pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
277         pids[0] = getpid();
278
279         for (i=0;i<num_procs-1;i++) {
280                 if ((pids[i+1]=fork()) == 0) break;
281         }
282
283         db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST, 
284                          O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
285         if (!db) {
286                 fatal("db open failed");
287         }
288
289         if (seed == -1) {
290                 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
291         }
292
293         if (i == 0) {
294                 printf("testing with %d processes, %d loops, %d hash_size, seed=%d%s\n", 
295                        num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
296         }
297
298         srand(seed + i);
299         srandom(seed + i);
300
301         for (i=0;i<num_loops && error_count == 0;i++) {
302                 addrec_db();
303         }
304
305         if (error_count == 0) {
306                 tdb_traverse_read(db, NULL, NULL);
307                 if (always_transaction) {
308                         while (in_transaction) {
309                                 tdb_transaction_cancel(db);
310                                 in_transaction--;
311                         }
312                         if (tdb_transaction_start(db) != 0)
313                                 fatal("tdb_transaction_start failed");
314                 }
315                 tdb_traverse(db, traverse_fn, NULL);
316                 tdb_traverse(db, traverse_fn, NULL);
317                 if (always_transaction) {
318                         if (tdb_transaction_commit(db) != 0)
319                                 fatal("tdb_transaction_commit failed");
320                 }
321         }
322
323         tdb_close(db);
324
325         if (getpid() != pids[0]) {
326                 return error_count;
327         }
328
329         for (i=1;i<num_procs;i++) {
330                 int status, j;
331                 pid_t pid;
332                 if (error_count != 0) {
333                         /* try and stop the test on any failure */
334                         for (j=1;j<num_procs;j++) {
335                                 if (pids[j] != 0) {
336                                         kill(pids[j], SIGTERM);
337                                 }
338                         }
339                 }
340                 pid = waitpid(-1, &status, 0);
341                 if (pid == -1) {
342                         perror("failed to wait for child\n");
343                         exit(1);
344                 }
345                 for (j=1;j<num_procs;j++) {
346                         if (pids[j] == pid) break;
347                 }
348                 if (j == num_procs) {
349                         printf("unknown child %d exited!?\n", (int)pid);
350                         exit(1);
351                 }
352                 if (WEXITSTATUS(status) != 0) {
353                         printf("child %d exited with status %d\n",
354                                (int)pid, WEXITSTATUS(status));
355                         error_count++;
356                 }
357                 pids[j] = 0;
358         }
359
360         free(pids);
361
362         if (error_count == 0) {
363                 printf("OK\n");
364         }
365
366         return error_count;
367 }