VERSION: Set version to 3.5.0pre1.
[Samba/gbeck.git] / lib / tdb / tools / tdbtorture.c
blobb0221a2503227a718142738fda6ddbea5916b1a9
1 /* this tests tdb by doing lots of ops from several simultaneous
2 writers - that stresses the locking code.
3 */
5 #include "replace.h"
6 #include "system/time.h"
7 #include "system/wait.h"
8 #include "system/filesys.h"
9 #include "tdb.h"
11 #ifdef HAVE_GETOPT_H
12 #include <getopt.h>
13 #endif
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
29 static struct tdb_context *db;
30 static int in_transaction;
31 static int error_count;
32 static int always_transaction = 0;
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, ...)
39 va_list ap;
41 /* trace level messages do not indicate an error */
42 if (level != TDB_DEBUG_TRACE) {
43 error_count++;
46 va_start(ap, format);
47 vfprintf(stdout, format, ap);
48 va_end(ap);
49 fflush(stdout);
50 #if 0
52 char *ptr;
53 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
54 system(ptr);
55 free(ptr);
57 #endif
60 static void fatal(const char *why)
62 perror(why);
63 error_count++;
66 static char *randbuf(int len)
68 char *buf;
69 int i;
70 buf = (char *)malloc(len+1);
72 for (i=0;i<len;i++) {
73 buf[i] = 'a' + (rand() % 26);
75 buf[i] = 0;
76 return buf;
79 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
80 void *state)
82 #if CULL_PROB
83 if (random() % CULL_PROB == 0) {
84 tdb_delete(tdb, key);
86 #endif
87 return 0;
90 static void addrec_db(void)
92 int klen, dlen;
93 char *k, *d;
94 TDB_DATA key, data;
96 klen = 1 + (rand() % KEYLEN);
97 dlen = 1 + (rand() % DATALEN);
99 k = randbuf(klen);
100 d = randbuf(dlen);
102 key.dptr = (unsigned char *)k;
103 key.dsize = klen+1;
105 data.dptr = (unsigned char *)d;
106 data.dsize = dlen+1;
108 #if REOPEN_PROB
109 if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
110 tdb_reopen_all(0);
111 goto next;
113 #endif
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");
121 in_transaction++;
122 goto next;
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");
130 if (tdb_transaction_commit(db) != 0) {
131 fatal("tdb_transaction_commit failed");
133 in_transaction--;
134 goto next;
136 if (in_transaction && random() % TRANSACTION_PROB == 0) {
137 if (tdb_transaction_cancel(db) != 0) {
138 fatal("tdb_transaction_cancel failed");
140 in_transaction--;
141 goto next;
143 #endif
145 #if DELETE_PROB
146 if (random() % DELETE_PROB == 0) {
147 tdb_delete(db, key);
148 goto next;
150 #endif
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");
157 goto next;
159 #endif
161 #if APPEND_PROB
162 if (random() % APPEND_PROB == 0) {
163 if (tdb_append(db, key, data) != 0) {
164 fatal("tdb_append failed");
166 goto next;
168 #endif
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");
177 if (data.dptr) free(data.dptr);
178 tdb_chainunlock(db, key);
179 goto next;
181 #endif
183 #if TRAVERSE_PROB
184 if (random() % TRAVERSE_PROB == 0) {
185 tdb_traverse(db, cull_traverse, NULL);
186 goto next;
188 #endif
190 #if TRAVERSE_READ_PROB
191 if (random() % TRAVERSE_READ_PROB == 0) {
192 tdb_traverse_read(db, NULL, NULL);
193 goto next;
195 #endif
197 data = tdb_fetch(db, key);
198 if (data.dptr) free(data.dptr);
200 next:
201 free(k);
202 free(d);
205 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
206 void *state)
208 tdb_delete(tdb, key);
209 return 0;
212 static void usage(void)
214 printf("Usage: tdbtorture [-t] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
215 exit(0);
218 int main(int argc, char * const *argv)
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;
228 struct tdb_logging_context log_ctx;
229 log_ctx.log_fn = tdb_log;
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();
253 unlink("torture.tdb");
255 pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
256 pids[0] = getpid();
258 for (i=0;i<num_procs-1;i++) {
259 if ((pids[i+1]=fork()) == 0) break;
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");
268 if (seed == -1) {
269 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
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)" : "");
277 srand(seed + i);
278 srandom(seed + i);
280 for (i=0;i<num_loops && error_count == 0;i++) {
281 addrec_db();
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--;
291 if (tdb_transaction_start(db) != 0)
292 fatal("tdb_transaction_start failed");
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");
302 tdb_close(db);
304 if (getpid() != pids[0]) {
305 return error_count;
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);
319 pid = waitpid(-1, &status, 0);
320 if (pid == -1) {
321 perror("failed to wait for child\n");
322 exit(1);
324 for (j=1;j<num_procs;j++) {
325 if (pids[j] == pid) break;
327 if (j == num_procs) {
328 printf("unknown child %d exited!?\n", (int)pid);
329 exit(1);
331 if (WEXITSTATUS(status) != 0) {
332 printf("child %d exited with status %d\n",
333 (int)pid, WEXITSTATUS(status));
334 error_count++;
336 pids[j] = 0;
339 free(pids);
341 if (error_count == 0) {
342 printf("OK\n");
345 return error_count;