me stupid.
[Samba.git] / source / tdb / tdbtorture.c
blob1a3b715402d941a89fcb92eae5e8440beac800a2
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <fcntl.h>
7 #include <stdarg.h>
8 #include <sys/mman.h>
9 #include <sys/stat.h>
10 #include <sys/time.h>
11 #include <sys/wait.h>
12 #include "tdb.h"
14 /* this tests tdb by doing lots of ops from several simultaneous
15 writers - that stresses the locking code. Build with TDB_DEBUG=1
16 for best effect */
20 #define DELETE_PROB 10
21 #define STORE_PROB 3
22 #define TRAVERSE_PROB 8
23 #define CULL_PROB 60
24 #define KEYLEN 3
25 #define DATALEN 100
27 static TDB_CONTEXT *db;
29 static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
31 va_list ap;
33 va_start(ap, format);
34 vfprintf(stdout, format, ap);
35 va_end(ap);
36 #if 0
38 char *ptr;
39 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
40 system(ptr);
41 free(ptr);
43 #endif
46 static void fatal(char *why)
48 perror(why);
49 exit(1);
52 static char *randbuf(int len)
54 char *buf;
55 int i;
56 buf = (char *)malloc(len+1);
58 for (i=0;i<len;i++) {
59 buf[i] = 'a' + (rand() % 26);
61 buf[i] = 0;
62 return buf;
65 static int cull_traverse(TDB_CONTEXT *db, TDB_DATA key, TDB_DATA dbuf,
66 void *state)
68 if (random() % CULL_PROB == 0) {
69 tdb_delete(db, key);
71 return 0;
74 static void addrec_db(void)
76 int klen, dlen;
77 char *k, *d;
78 TDB_DATA key, data;
80 klen = 1 + (rand() % KEYLEN);
81 dlen = 1 + (rand() % DATALEN);
83 k = randbuf(klen);
84 d = randbuf(dlen);
86 key.dptr = k;
87 key.dsize = klen+1;
89 data.dptr = d;
90 data.dsize = dlen+1;
92 if (random() % DELETE_PROB == 0) {
93 tdb_delete(db, key);
94 } else if (random() % STORE_PROB == 0) {
95 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
96 fatal("tdb_store failed");
98 } else if (random() % TRAVERSE_PROB == 0) {
99 tdb_traverse(db, cull_traverse, NULL);
100 } else {
101 data = tdb_fetch(db, key);
102 if (data.dptr) free(data.dptr);
105 free(k);
106 free(d);
109 static int traverse_fn(TDB_CONTEXT *db, TDB_DATA key, TDB_DATA dbuf,
110 void *state)
112 tdb_delete(db, key);
113 return 0;
116 #ifndef NPROC
117 #define NPROC 6
118 #endif
120 #ifndef NLOOPS
121 #define NLOOPS 200000
122 #endif
124 int main(int argc, char *argv[])
126 int i, seed=0;
127 int loops = NLOOPS;
128 pid_t pids[NPROC];
130 pids[0] = getpid();
132 for (i=0;i<NPROC-1;i++) {
133 if ((pids[i+1]=fork()) == 0) break;
136 db = tdb_open("test.tdb", 0, TDB_CLEAR_IF_FIRST,
137 O_RDWR | O_CREAT, 0600);
138 if (!db) {
139 fatal("db open failed");
141 tdb_logging_function(db, tdb_log);
143 srand(seed + getpid());
144 srandom(seed + getpid() + time(NULL));
145 for (i=0;i<loops;i++) addrec_db();
147 tdb_traverse(db, NULL, NULL);
148 tdb_traverse(db, traverse_fn, NULL);
149 tdb_traverse(db, traverse_fn, NULL);
151 tdb_close(db);
153 if (getpid() == pids[0]) {
154 for (i=0;i<NPROC-1;i++) {
155 int status;
156 if (waitpid(pids[i+1], &status, 0) != pids[i+1]) {
157 printf("failed to wait for %d\n",
158 (int)pids[i+1]);
159 exit(1);
161 if (WEXITSTATUS(status) != 0) {
162 printf("child %d exited with status %d\n",
163 (int)pids[i+1], WEXITSTATUS(status));
164 exit(1);
167 printf("OK\n");
170 return 0;