Fix up warnings. Make tdb_openXX() names const.
[kai/samba.git] / source3 / tdb / tdbtorture.c
1 #include <stdlib.h>
2 #include <time.h>
3 #include <stdio.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <fcntl.h>
8 #include <stdarg.h>
9 #include <sys/mman.h>
10 #include <sys/stat.h>
11 #include <sys/time.h>
12 #include <sys/wait.h>
13 #include "tdb.h"
14
15 /* this tests tdb by doing lots of ops from several simultaneous
16    writers - that stresses the locking code. Build with TDB_DEBUG=1
17    for best effect */
18
19
20
21 #define REOPEN_PROB 30
22 #define DELETE_PROB 8
23 #define STORE_PROB 4
24 #define LOCKSTORE_PROB 0
25 #define TRAVERSE_PROB 20
26 #define CULL_PROB 100
27 #define KEYLEN 3
28 #define DATALEN 100
29 #define LOCKLEN 20
30
31 static TDB_CONTEXT *db;
32
33 static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
34 {
35         va_list ap;
36     
37         va_start(ap, format);
38         vfprintf(stdout, format, ap);
39         va_end(ap);
40         fflush(stdout);
41 #if 0
42         {
43                 char *ptr;
44                 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
45                 system(ptr);
46                 free(ptr);
47         }
48 #endif  
49 }
50
51 static void fatal(char *why)
52 {
53         perror(why);
54         exit(1);
55 }
56
57 static char *randbuf(int len)
58 {
59         char *buf;
60         int i;
61         buf = (char *)malloc(len+1);
62
63         for (i=0;i<len;i++) {
64                 buf[i] = 'a' + (rand() % 26);
65         }
66         buf[i] = 0;
67         return buf;
68 }
69
70 static int cull_traverse(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf,
71                          void *state)
72 {
73         if (random() % CULL_PROB == 0) {
74                 tdb_delete(tdb, key);
75         }
76         return 0;
77 }
78
79 static void addrec_db(void)
80 {
81         int klen, dlen, slen;
82         char *k, *d, *s;
83         TDB_DATA key, data, lockkey;
84
85         klen = 1 + (rand() % KEYLEN);
86         dlen = 1 + (rand() % DATALEN);
87         slen = 1 + (rand() % LOCKLEN);
88
89         k = randbuf(klen);
90         d = randbuf(dlen);
91         s = randbuf(slen);
92
93         key.dptr = k;
94         key.dsize = klen+1;
95
96         data.dptr = d;
97         data.dsize = dlen+1;
98
99         lockkey.dptr = s;
100         lockkey.dsize = slen+1;
101
102 #if REOPEN_PROB
103         if (random() % REOPEN_PROB == 0) {
104                 tdb_reopen_all();
105                 goto next;
106         } 
107 #endif
108
109 #if DELETE_PROB
110         if (random() % DELETE_PROB == 0) {
111                 tdb_delete(db, key);
112                 goto next;
113         }
114 #endif
115
116 #if STORE_PROB
117         if (random() % STORE_PROB == 0) {
118                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
119                         fatal("tdb_store failed");
120                 }
121                 goto next;
122         }
123 #endif
124
125 #if LOCKSTORE_PROB
126         if (random() % LOCKSTORE_PROB == 0) {
127                 tdb_chainlock(db, lockkey);
128                 data = tdb_fetch(db, key);
129                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
130                         fatal("tdb_store failed");
131                 }
132                 if (data.dptr) free(data.dptr);
133                 tdb_chainunlock(db, lockkey);
134                 goto next;
135         } 
136 #endif
137
138 #if TRAVERSE_PROB
139         if (random() % TRAVERSE_PROB == 0) {
140                 tdb_traverse(db, cull_traverse, NULL);
141                 goto next;
142         }
143 #endif
144
145         data = tdb_fetch(db, key);
146         if (data.dptr) free(data.dptr);
147
148 next:
149         free(k);
150         free(d);
151         free(s);
152 }
153
154 static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf,
155                        void *state)
156 {
157         tdb_delete(tdb, key);
158         return 0;
159 }
160
161 #ifndef NPROC
162 #define NPROC 6
163 #endif
164
165 #ifndef NLOOPS
166 #define NLOOPS 200000
167 #endif
168
169 int main(int argc, char *argv[])
170 {
171         int i, seed=0;
172         int loops = NLOOPS;
173         pid_t pids[NPROC];
174
175         pids[0] = getpid();
176
177         for (i=0;i<NPROC-1;i++) {
178                 if ((pids[i+1]=fork()) == 0) break;
179         }
180
181         db = tdb_open("torture.tdb", 2, TDB_CLEAR_IF_FIRST, 
182                       O_RDWR | O_CREAT, 0600);
183         if (!db) {
184                 fatal("db open failed");
185         }
186         tdb_logging_function(db, tdb_log);
187
188         srand(seed + getpid());
189         srandom(seed + getpid() + time(NULL));
190         for (i=0;i<loops;i++) addrec_db();
191
192         tdb_traverse(db, NULL, NULL);
193         tdb_traverse(db, traverse_fn, NULL);
194         tdb_traverse(db, traverse_fn, NULL);
195
196         tdb_close(db);
197
198         if (getpid() == pids[0]) {
199                 for (i=0;i<NPROC-1;i++) {
200                         int status;
201                         if (waitpid(pids[i+1], &status, 0) != pids[i+1]) {
202                                 printf("failed to wait for %d\n",
203                                        (int)pids[i+1]);
204                                 exit(1);
205                         }
206                         if (WEXITSTATUS(status) != 0) {
207                                 printf("child %d exited with status %d\n",
208                                        (int)pids[i+1], WEXITSTATUS(status));
209                                 exit(1);
210                         }
211                 }
212                 printf("OK\n");
213         }
214
215         return 0;
216 }