Merge a trivial fix across from HEAD. Not that this
[Samba.git] / source / tdb / tdbtorture.c
blobe27bbff9909bbf478214f3f72885ab9a00d02ff1
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"
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 */
21 #define REOPEN_PROB 30
22 #define DELETE_PROB 8
23 #define STORE_PROB 4
24 #define APPEND_PROB 6
25 #define LOCKSTORE_PROB 0
26 #define TRAVERSE_PROB 20
27 #define CULL_PROB 100
28 #define KEYLEN 3
29 #define DATALEN 100
30 #define LOCKLEN 20
32 static TDB_CONTEXT *db;
34 static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
36 va_list ap;
38 va_start(ap, format);
39 vfprintf(stdout, format, ap);
40 va_end(ap);
41 fflush(stdout);
42 #if 0
44 char *ptr;
45 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
46 system(ptr);
47 free(ptr);
49 #endif
52 static void fatal(char *why)
54 perror(why);
55 exit(1);
58 static char *randbuf(int len)
60 char *buf;
61 int i;
62 buf = (char *)malloc(len+1);
64 for (i=0;i<len;i++) {
65 buf[i] = 'a' + (rand() % 26);
67 buf[i] = 0;
68 return buf;
71 static int cull_traverse(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf,
72 void *state)
74 if (random() % CULL_PROB == 0) {
75 tdb_delete(tdb, key);
77 return 0;
80 static void addrec_db(void)
82 int klen, dlen, slen;
83 char *k, *d, *s;
84 TDB_DATA key, data, lockkey;
86 klen = 1 + (rand() % KEYLEN);
87 dlen = 1 + (rand() % DATALEN);
88 slen = 1 + (rand() % LOCKLEN);
90 k = randbuf(klen);
91 d = randbuf(dlen);
92 s = randbuf(slen);
94 key.dptr = k;
95 key.dsize = klen+1;
97 data.dptr = d;
98 data.dsize = dlen+1;
100 lockkey.dptr = s;
101 lockkey.dsize = slen+1;
103 #if REOPEN_PROB
104 if (random() % REOPEN_PROB == 0) {
105 tdb_reopen_all();
106 goto next;
108 #endif
110 #if DELETE_PROB
111 if (random() % DELETE_PROB == 0) {
112 tdb_delete(db, key);
113 goto next;
115 #endif
117 #if STORE_PROB
118 if (random() % STORE_PROB == 0) {
119 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
120 fatal("tdb_store failed");
122 goto next;
124 #endif
126 #if APPEND_PROB
127 if (random() % APPEND_PROB == 0) {
128 if (tdb_append(db, key, data) != 0) {
129 fatal("tdb_append failed");
131 goto next;
133 #endif
135 #if LOCKSTORE_PROB
136 if (random() % LOCKSTORE_PROB == 0) {
137 tdb_chainlock(db, lockkey);
138 data = tdb_fetch(db, key);
139 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
140 fatal("tdb_store failed");
142 if (data.dptr) free(data.dptr);
143 tdb_chainunlock(db, lockkey);
144 goto next;
146 #endif
148 #if TRAVERSE_PROB
149 if (random() % TRAVERSE_PROB == 0) {
150 tdb_traverse(db, cull_traverse, NULL);
151 goto next;
153 #endif
155 data = tdb_fetch(db, key);
156 if (data.dptr) free(data.dptr);
158 next:
159 free(k);
160 free(d);
161 free(s);
164 static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf,
165 void *state)
167 tdb_delete(tdb, key);
168 return 0;
171 #ifndef NPROC
172 #define NPROC 6
173 #endif
175 #ifndef NLOOPS
176 #define NLOOPS 200000
177 #endif
179 int main(int argc, char *argv[])
181 int i, seed=0;
182 int loops = NLOOPS;
183 pid_t pids[NPROC];
185 pids[0] = getpid();
187 for (i=0;i<NPROC-1;i++) {
188 if ((pids[i+1]=fork()) == 0) break;
191 db = tdb_open("torture.tdb", 2, TDB_CLEAR_IF_FIRST,
192 O_RDWR | O_CREAT, 0600);
193 if (!db) {
194 fatal("db open failed");
196 tdb_logging_function(db, tdb_log);
198 srand(seed + getpid());
199 srandom(seed + getpid() + time(NULL));
200 for (i=0;i<loops;i++) addrec_db();
202 tdb_traverse(db, NULL, NULL);
203 tdb_traverse(db, traverse_fn, NULL);
204 tdb_traverse(db, traverse_fn, NULL);
206 tdb_close(db);
208 if (getpid() == pids[0]) {
209 for (i=0;i<NPROC-1;i++) {
210 int status;
211 if (waitpid(pids[i+1], &status, 0) != pids[i+1]) {
212 printf("failed to wait for %d\n",
213 (int)pids[i+1]);
214 exit(1);
216 if (WEXITSTATUS(status) != 0) {
217 printf("child %d exited with status %d\n",
218 (int)pids[i+1], WEXITSTATUS(status));
219 exit(1);
222 printf("OK\n");
225 return 0;