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