s3: Add tdb_data_equal
[ira/wip.git] / lib / 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 "replace.h"
6 #include "system/time.h"
7 #include "system/wait.h"
8 #include "system/filesys.h"
9 #include "tdb.h"
10
11 #ifdef HAVE_GETOPT_H
12 #include <getopt.h>
13 #endif
14
15
16 #define REOPEN_PROB 30
17 #define DELETE_PROB 8
18 #define STORE_PROB 4
19 #define APPEND_PROB 6
20 #define TRANSACTION_PROB 10
21 #define TRANSACTION_PREPARE_PROB 2
22 #define LOCKSTORE_PROB 5
23 #define TRAVERSE_PROB 20
24 #define TRAVERSE_READ_PROB 20
25 #define CULL_PROB 100
26 #define KEYLEN 3
27 #define DATALEN 100
28
29 static struct tdb_context *db;
30 static int in_transaction;
31 static int error_count;
32 static int always_transaction = 0;
33
34 #ifdef PRINTF_ATTRIBUTE
35 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
36 #endif
37 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
38 {
39         va_list ap;
40
41         /* trace level messages do not indicate an error */
42         if (level != TDB_DEBUG_TRACE) {
43                 error_count++;
44         }
45
46         va_start(ap, format);
47         vfprintf(stdout, format, ap);
48         va_end(ap);
49         fflush(stdout);
50 #if 0
51         {
52                 char *ptr;
53                 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
54                 system(ptr);
55                 free(ptr);
56         }
57 #endif  
58 }
59
60 static void fatal(const char *why)
61 {
62         perror(why);
63         error_count++;
64 }
65
66 static char *randbuf(int len)
67 {
68         char *buf;
69         int i;
70         buf = (char *)malloc(len+1);
71
72         for (i=0;i<len;i++) {
73                 buf[i] = 'a' + (rand() % 26);
74         }
75         buf[i] = 0;
76         return buf;
77 }
78
79 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
80                          void *state)
81 {
82 #if CULL_PROB
83         if (random() % CULL_PROB == 0) {
84                 tdb_delete(tdb, key);
85         }
86 #endif
87         return 0;
88 }
89
90 static void addrec_db(void)
91 {
92         int klen, dlen;
93         char *k, *d;
94         TDB_DATA key, data;
95
96         klen = 1 + (rand() % KEYLEN);
97         dlen = 1 + (rand() % DATALEN);
98
99         k = randbuf(klen);
100         d = randbuf(dlen);
101
102         key.dptr = (unsigned char *)k;
103         key.dsize = klen+1;
104
105         data.dptr = (unsigned char *)d;
106         data.dsize = dlen+1;
107
108 #if REOPEN_PROB
109         if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
110                 tdb_reopen_all(0);
111                 goto next;
112         }
113 #endif
114
115 #if TRANSACTION_PROB
116         if (in_transaction == 0 &&
117             (always_transaction || random() % TRANSACTION_PROB == 0)) {
118                 if (tdb_transaction_start(db) != 0) {
119                         fatal("tdb_transaction_start failed");
120                 }
121                 in_transaction++;
122                 goto next;
123         }
124         if (in_transaction && random() % TRANSACTION_PROB == 0) {
125                 if (random() % TRANSACTION_PREPARE_PROB == 0) {
126                         if (tdb_transaction_prepare_commit(db) != 0) {
127                                 fatal("tdb_transaction_prepare_commit failed");
128                         }
129                 }
130                 if (tdb_transaction_commit(db) != 0) {
131                         fatal("tdb_transaction_commit failed");
132                 }
133                 in_transaction--;
134                 goto next;
135         }
136         if (in_transaction && random() % TRANSACTION_PROB == 0) {
137                 if (tdb_transaction_cancel(db) != 0) {
138                         fatal("tdb_transaction_cancel failed");
139                 }
140                 in_transaction--;
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 [-t] [-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:th")) != -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                 case 't':
246                         always_transaction = 1;
247                         break;
248                 default:
249                         usage();
250                 }
251         }
252
253         unlink("torture.tdb");
254
255         pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
256         pids[0] = getpid();
257
258         for (i=0;i<num_procs-1;i++) {
259                 if ((pids[i+1]=fork()) == 0) break;
260         }
261
262         db = tdb_open_ex("torture.tdb", hash_size, TDB_CLEAR_IF_FIRST, 
263                          O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
264         if (!db) {
265                 fatal("db open failed");
266         }
267
268         if (seed == -1) {
269                 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
270         }
271
272         if (i == 0) {
273                 printf("testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
274                        num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
275         }
276
277         srand(seed + i);
278         srandom(seed + i);
279
280         for (i=0;i<num_loops && error_count == 0;i++) {
281                 addrec_db();
282         }
283
284         if (error_count == 0) {
285                 tdb_traverse_read(db, NULL, NULL);
286                 if (always_transaction) {
287                         while (in_transaction) {
288                                 tdb_transaction_cancel(db);
289                                 in_transaction--;
290                         }
291                         if (tdb_transaction_start(db) != 0)
292                                 fatal("tdb_transaction_start failed");
293                 }
294                 tdb_traverse(db, traverse_fn, NULL);
295                 tdb_traverse(db, traverse_fn, NULL);
296                 if (always_transaction) {
297                         if (tdb_transaction_commit(db) != 0)
298                                 fatal("tdb_transaction_commit failed");
299                 }
300         }
301
302         tdb_close(db);
303
304         if (getpid() != pids[0]) {
305                 return error_count;
306         }
307
308         for (i=1;i<num_procs;i++) {
309                 int status, j;
310                 pid_t pid;
311                 if (error_count != 0) {
312                         /* try and stop the test on any failure */
313                         for (j=1;j<num_procs;j++) {
314                                 if (pids[j] != 0) {
315                                         kill(pids[j], SIGTERM);
316                                 }
317                         }
318                 }
319                 pid = waitpid(-1, &status, 0);
320                 if (pid == -1) {
321                         perror("failed to wait for child\n");
322                         exit(1);
323                 }
324                 for (j=1;j<num_procs;j++) {
325                         if (pids[j] == pid) break;
326                 }
327                 if (j == num_procs) {
328                         printf("unknown child %d exited!?\n", (int)pid);
329                         exit(1);
330                 }
331                 if (WEXITSTATUS(status) != 0) {
332                         printf("child %d exited with status %d\n",
333                                (int)pid, WEXITSTATUS(status));
334                         error_count++;
335                 }
336                 pids[j] = 0;
337         }
338
339         free(pids);
340
341         if (error_count == 0) {
342                 printf("OK\n");
343         }
344
345         return error_count;
346 }