r10421: following on discussions with simo, I have worked out a way of
[ira/wip.git] / source / 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 #define _GNU_SOURCE
6
7 #ifndef _SAMBA_BUILD_
8 #include <stdlib.h>
9 #include <time.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <stdarg.h>
17 #include <sys/mman.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <sys/wait.h>
21 #include "tdb.h"
22
23 #else
24
25 #include "includes.h"
26 #include "lib/tdb/include/tdb.h"
27 #include "system/time.h"
28 #include "system/wait.h"
29 #include "system/filesys.h"
30
31 #endif
32
33 #include <getopt.h>
34
35 #define REOPEN_PROB 30
36 #define DELETE_PROB 8
37 #define STORE_PROB 4
38 #define APPEND_PROB 6
39 #define TRANSACTION_PROB 10
40 #define LOCKSTORE_PROB 5
41 #define TRAVERSE_PROB 20
42 #define TRAVERSE_READ_PROB 20
43 #define CULL_PROB 100
44 #define KEYLEN 3
45 #define DATALEN 100
46 #define LOCKLEN 20
47
48 static struct tdb_context *db;
49 static int in_transaction;
50
51 #ifdef PRINTF_ATTRIBUTE
52 static void tdb_log(struct tdb_context *tdb, int level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
53 #endif
54 static void tdb_log(struct tdb_context *tdb, int level, const char *format, ...)
55 {
56         va_list ap;
57     
58         va_start(ap, format);
59         vfprintf(stdout, format, ap);
60         va_end(ap);
61         fflush(stdout);
62 #if 0
63         {
64                 char *ptr;
65                 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
66                 system(ptr);
67                 free(ptr);
68         }
69 #endif  
70 }
71
72 static void fatal(const char *why)
73 {
74         perror(why);
75         exit(1);
76 }
77
78 static char *randbuf(int len)
79 {
80         char *buf;
81         int i;
82         buf = (char *)malloc(len+1);
83
84         for (i=0;i<len;i++) {
85                 buf[i] = 'a' + (rand() % 26);
86         }
87         buf[i] = 0;
88         return buf;
89 }
90
91 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
92                          void *state)
93 {
94 #if CULL_PROB
95         if (random() % CULL_PROB == 0) {
96                 tdb_delete(tdb, key);
97         }
98 #endif
99         return 0;
100 }
101
102 static void addrec_db(void)
103 {
104         int klen, dlen;
105         char *k, *d;
106         TDB_DATA key, data;
107
108         klen = 1 + (rand() % KEYLEN);
109         dlen = 1 + (rand() % DATALEN);
110
111         k = randbuf(klen);
112         d = randbuf(dlen);
113
114         key.dptr = (unsigned char *)k;
115         key.dsize = klen+1;
116
117         data.dptr = (unsigned char *)d;
118         data.dsize = dlen+1;
119
120 #if TRANSACTION_PROB
121         if (in_transaction == 0 && random() % TRANSACTION_PROB == 0) {
122                 if (tdb_transaction_start(db) != 0) {
123                         fatal("tdb_transaction_start failed");
124                 }
125                 in_transaction++;
126                 goto next;
127         }
128         if (in_transaction && random() % TRANSACTION_PROB == 0) {
129                 if (tdb_transaction_commit(db) != 0) {
130                         fatal("tdb_transaction_commit failed");
131                 }
132                 in_transaction--;
133                 goto next;
134         }
135         if (in_transaction && random() % TRANSACTION_PROB == 0) {
136                 if (tdb_transaction_cancel(db) != 0) {
137                         fatal("tdb_transaction_cancel failed");
138                 }
139                 in_transaction--;
140                 goto next;
141         }
142 #endif
143
144 #if REOPEN_PROB
145         if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
146                 tdb_reopen_all();
147                 goto next;
148         } 
149 #endif
150
151 #if DELETE_PROB
152         if (random() % DELETE_PROB == 0) {
153                 tdb_delete(db, key);
154                 goto next;
155         }
156 #endif
157
158 #if STORE_PROB
159         if (random() % STORE_PROB == 0) {
160                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
161                         fatal("tdb_store failed");
162                 }
163                 goto next;
164         }
165 #endif
166
167 #if APPEND_PROB
168         if (random() % APPEND_PROB == 0) {
169                 if (tdb_append(db, key, data) != 0) {
170                         fatal("tdb_append failed");
171                 }
172                 goto next;
173         }
174 #endif
175
176 #if LOCKSTORE_PROB
177         if (random() % LOCKSTORE_PROB == 0) {
178                 tdb_chainlock(db, key);
179                 data = tdb_fetch(db, key);
180                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
181                         fatal("tdb_store failed");
182                 }
183                 if (data.dptr) free(data.dptr);
184                 tdb_chainunlock(db, key);
185                 goto next;
186         } 
187 #endif
188
189 #if TRAVERSE_PROB
190         if (random() % TRAVERSE_PROB == 0) {
191                 tdb_traverse(db, cull_traverse, NULL);
192                 goto next;
193         }
194 #endif
195
196 #if TRAVERSE_READ_PROB
197         if (random() % TRAVERSE_READ_PROB == 0) {
198                 tdb_traverse_read(db, NULL, NULL);
199                 goto next;
200         }
201 #endif
202
203         data = tdb_fetch(db, key);
204         if (data.dptr) free(data.dptr);
205
206 next:
207         free(k);
208         free(d);
209 }
210
211 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
212                        void *state)
213 {
214         tdb_delete(tdb, key);
215         return 0;
216 }
217
218 static void usage(void)
219 {
220         printf("Usage: tdbtorture [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
221         exit(0);
222 }
223
224  int main(int argc, char * const *argv)
225 {
226         int i, seed = -1;
227         int num_procs = 2;
228         int num_loops = 5000;
229         int hash_size = 2;
230         int c;
231         extern char *optarg;
232         pid_t *pids;
233
234         while ((c = getopt(argc, argv, "n:l:s:H:h")) != -1) {
235                 switch (c) {
236                 case 'n':
237                         num_procs = strtol(optarg, NULL, 0);
238                         break;
239                 case 'l':
240                         num_loops = strtol(optarg, NULL, 0);
241                         break;
242                 case 'H':
243                         hash_size = strtol(optarg, NULL, 0);
244                         break;
245                 case 's':
246                         seed = strtol(optarg, NULL, 0);
247                         break;
248                 default:
249                         usage();
250                 }
251         }
252
253         unlink("torture.tdb");
254
255         pids = 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, tdb_log, 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\n", 
274                        num_procs, num_loops, hash_size, seed);
275         }
276
277         srand(seed + i);
278         srandom(seed + i);
279
280         for (i=0;i<num_loops;i++) {
281                 addrec_db();
282         }
283
284         tdb_traverse_read(db, NULL, NULL);
285         tdb_traverse(db, traverse_fn, NULL);
286         tdb_traverse(db, traverse_fn, NULL);
287
288         tdb_close(db);
289
290         if (getpid() == pids[0]) {
291                 for (i=0;i<num_procs-1;i++) {
292                         int status;
293                         if (waitpid(pids[i+1], &status, 0) != pids[i+1]) {
294                                 printf("failed to wait for %d\n",
295                                        (int)pids[i+1]);
296                                 exit(1);
297                         }
298                         if (WEXITSTATUS(status) != 0) {
299                                 printf("child %d exited with status %d\n",
300                                        (int)pids[i+1], WEXITSTATUS(status));
301                                 exit(1);
302                         }
303                 }
304                 printf("OK\n");
305         }
306
307         return 0;
308 }