Remove unused variable.
[samba.git] / 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 #include "replace.h"
6 #include "system/time.h"
7 #include "system/wait.h"
8 #include "system/filesys.h"
9 #include "tdb.h"
10
11 #ifdef HAVE_GETOPT_H
12 #include <getopt.h>
13 #endif
14
15
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
28
29 static struct tdb_context *db;
30 static int in_transaction;
31 static int error_count;
32 static int always_transaction = 0;
33 static int hash_size = 2;
34 static int loopnum;
35 static int count_pipe;
36 static struct tdb_logging_context log_ctx;
37
38 #ifdef PRINTF_ATTRIBUTE
39 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...) PRINTF_ATTRIBUTE(3,4);
40 #endif
41 static void tdb_log(struct tdb_context *tdb, enum tdb_debug_level level, const char *format, ...)
42 {
43         va_list ap;
44
45         /* trace level messages do not indicate an error */
46         if (level != TDB_DEBUG_TRACE) {
47                 error_count++;
48         }
49
50         va_start(ap, format);
51         vfprintf(stdout, format, ap);
52         va_end(ap);
53         fflush(stdout);
54 #if 0
55         if (level != TDB_DEBUG_TRACE) {
56                 char *ptr;
57                 signal(SIGUSR1, SIG_IGN);
58                 asprintf(&ptr,"xterm -e gdb /proc/%d/exe %d", getpid(), getpid());
59                 system(ptr);
60                 free(ptr);
61         }
62 #endif  
63 }
64
65 static void fatal(const char *why)
66 {
67         perror(why);
68         error_count++;
69 }
70
71 static char *randbuf(int len)
72 {
73         char *buf;
74         int i;
75         buf = (char *)malloc(len+1);
76
77         for (i=0;i<len;i++) {
78                 buf[i] = 'a' + (rand() % 26);
79         }
80         buf[i] = 0;
81         return buf;
82 }
83
84 static int cull_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
85                          void *state)
86 {
87 #if CULL_PROB
88         if (random() % CULL_PROB == 0) {
89                 tdb_delete(tdb, key);
90         }
91 #endif
92         return 0;
93 }
94
95 static void addrec_db(void)
96 {
97         int klen, dlen;
98         char *k, *d;
99         TDB_DATA key, data;
100
101         klen = 1 + (rand() % KEYLEN);
102         dlen = 1 + (rand() % DATALEN);
103
104         k = randbuf(klen);
105         d = randbuf(dlen);
106
107         key.dptr = (unsigned char *)k;
108         key.dsize = klen+1;
109
110         data.dptr = (unsigned char *)d;
111         data.dsize = dlen+1;
112
113 #if REOPEN_PROB
114         if (in_transaction == 0 && random() % REOPEN_PROB == 0) {
115                 tdb_reopen_all(0);
116                 goto next;
117         }
118 #endif
119
120 #if TRANSACTION_PROB
121         if (in_transaction == 0 &&
122             (always_transaction || random() % TRANSACTION_PROB == 0)) {
123                 if (tdb_transaction_start(db) != 0) {
124                         fatal("tdb_transaction_start failed");
125                 }
126                 in_transaction++;
127                 goto next;
128         }
129         if (in_transaction && random() % TRANSACTION_PROB == 0) {
130                 if (random() % TRANSACTION_PREPARE_PROB == 0) {
131                         if (tdb_transaction_prepare_commit(db) != 0) {
132                                 fatal("tdb_transaction_prepare_commit failed");
133                         }
134                 }
135                 if (tdb_transaction_commit(db) != 0) {
136                         fatal("tdb_transaction_commit failed");
137                 }
138                 in_transaction--;
139                 goto next;
140         }
141         if (in_transaction && random() % TRANSACTION_PROB == 0) {
142                 if (tdb_transaction_cancel(db) != 0) {
143                         fatal("tdb_transaction_cancel failed");
144                 }
145                 in_transaction--;
146                 goto next;
147         }
148 #endif
149
150 #if DELETE_PROB
151         if (random() % DELETE_PROB == 0) {
152                 tdb_delete(db, key);
153                 goto next;
154         }
155 #endif
156
157 #if STORE_PROB
158         if (random() % STORE_PROB == 0) {
159                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
160                         fatal("tdb_store failed");
161                 }
162                 goto next;
163         }
164 #endif
165
166 #if APPEND_PROB
167         if (random() % APPEND_PROB == 0) {
168                 if (tdb_append(db, key, data) != 0) {
169                         fatal("tdb_append failed");
170                 }
171                 goto next;
172         }
173 #endif
174
175 #if LOCKSTORE_PROB
176         if (random() % LOCKSTORE_PROB == 0) {
177                 tdb_chainlock(db, key);
178                 data = tdb_fetch(db, key);
179                 if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
180                         fatal("tdb_store failed");
181                 }
182                 if (data.dptr) free(data.dptr);
183                 tdb_chainunlock(db, key);
184                 goto next;
185         } 
186 #endif
187
188 #if TRAVERSE_PROB
189         if (random() % TRAVERSE_PROB == 0) {
190                 tdb_traverse(db, cull_traverse, NULL);
191                 goto next;
192         }
193 #endif
194
195 #if TRAVERSE_READ_PROB
196         if (random() % TRAVERSE_READ_PROB == 0) {
197                 tdb_traverse_read(db, NULL, NULL);
198                 goto next;
199         }
200 #endif
201
202         data = tdb_fetch(db, key);
203         if (data.dptr) free(data.dptr);
204
205 next:
206         free(k);
207         free(d);
208 }
209
210 static int traverse_fn(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
211                        void *state)
212 {
213         tdb_delete(tdb, key);
214         return 0;
215 }
216
217 static void usage(void)
218 {
219         printf("Usage: tdbtorture [-t] [-k] [-n NUM_PROCS] [-l NUM_LOOPS] [-s SEED] [-H HASH_SIZE]\n");
220         exit(0);
221 }
222
223 static void send_count_and_suicide(int sig)
224 {
225         /* This ensures our successor can continue where we left off. */
226         write(count_pipe, &loopnum, sizeof(loopnum));
227         /* This gives a unique signature. */
228         kill(getpid(), SIGUSR2);
229 }
230
231 static int run_child(const char *filename, int i, int seed, unsigned num_loops, unsigned start)
232 {
233         db = tdb_open_ex(filename, hash_size, TDB_DEFAULT,
234                          O_RDWR | O_CREAT, 0600, &log_ctx, NULL);
235         if (!db) {
236                 fatal("db open failed");
237         }
238
239         srand(seed + i);
240         srandom(seed + i);
241
242         /* Set global, then we're ready to handle being killed. */
243         loopnum = start;
244         signal(SIGUSR1, send_count_and_suicide);
245
246         for (;loopnum<num_loops && error_count == 0;loopnum++) {
247                 addrec_db();
248         }
249
250         if (error_count == 0) {
251                 tdb_traverse_read(db, NULL, NULL);
252                 if (always_transaction) {
253                         while (in_transaction) {
254                                 tdb_transaction_cancel(db);
255                                 in_transaction--;
256                         }
257                         if (tdb_transaction_start(db) != 0)
258                                 fatal("tdb_transaction_start failed");
259                 }
260                 tdb_traverse(db, traverse_fn, NULL);
261                 tdb_traverse(db, traverse_fn, NULL);
262                 if (always_transaction) {
263                         if (tdb_transaction_commit(db) != 0)
264                                 fatal("tdb_transaction_commit failed");
265                 }
266         }
267
268         tdb_close(db);
269
270         return (error_count < 100 ? error_count : 100);
271 }
272
273 static char *test_path(const char *filename)
274 {
275         const char *prefix = getenv("TEST_DATA_PREFIX");
276
277         if (prefix) {
278                 char *path = NULL;
279                 int ret;
280
281                 ret = asprintf(&path, "%s/%s", prefix, filename);
282                 if (ret == -1) {
283                         return NULL;
284                 }
285                 return path;
286         }
287
288         return strdup(filename);
289 }
290
291 int main(int argc, char * const *argv)
292 {
293         int i, seed = -1;
294         int num_loops = 5000;
295         int num_procs = 3;
296         int c, pfds[2];
297         extern char *optarg;
298         pid_t *pids;
299         int kill_random = 0;
300         int *done;
301         char *test_tdb;
302
303         log_ctx.log_fn = tdb_log;
304
305         while ((c = getopt(argc, argv, "n:l:s:H:thk")) != -1) {
306                 switch (c) {
307                 case 'n':
308                         num_procs = strtol(optarg, NULL, 0);
309                         break;
310                 case 'l':
311                         num_loops = strtol(optarg, NULL, 0);
312                         break;
313                 case 'H':
314                         hash_size = strtol(optarg, NULL, 0);
315                         break;
316                 case 's':
317                         seed = strtol(optarg, NULL, 0);
318                         break;
319                 case 't':
320                         always_transaction = 1;
321                         break;
322                 case 'k':
323                         kill_random = 1;
324                         break;
325                 default:
326                         usage();
327                 }
328         }
329
330         test_tdb = test_path("torture.tdb");
331
332         unlink(test_tdb);
333
334         if (seed == -1) {
335                 seed = (getpid() + time(NULL)) & 0x7FFFFFFF;
336         }
337
338         if (num_procs == 1 && !kill_random) {
339                 /* Don't fork for this case, makes debugging easier. */
340                 error_count = run_child(test_tdb, 0, seed, num_loops, 0);
341                 goto done;
342         }
343
344         pids = (pid_t *)calloc(sizeof(pid_t), num_procs);
345         done = (int *)calloc(sizeof(int), num_procs);
346
347         if (pipe(pfds) != 0) {
348                 perror("Creating pipe");
349                 exit(1);
350         }
351         count_pipe = pfds[1];
352
353         for (i=0;i<num_procs;i++) {
354                 if ((pids[i]=fork()) == 0) {
355                         close(pfds[0]);
356                         if (i == 0) {
357                                 printf("Testing with %d processes, %d loops, %d hash_size, seed=%d%s\n",
358                                        num_procs, num_loops, hash_size, seed, always_transaction ? " (all within transactions)" : "");
359                         }
360                         exit(run_child(test_tdb, i, seed, num_loops, 0));
361                 }
362         }
363
364         while (num_procs) {
365                 int status, j;
366                 pid_t pid;
367
368                 if (error_count != 0) {
369                         /* try and stop the test on any failure */
370                         for (j=0;j<num_procs;j++) {
371                                 if (pids[j] != 0) {
372                                         kill(pids[j], SIGTERM);
373                                 }
374                         }
375                 }
376
377                 pid = waitpid(-1, &status, kill_random ? WNOHANG : 0);
378                 if (pid == 0) {
379                         struct timeval tv;
380
381                         /* Sleep for 1/10 second. */
382                         tv.tv_sec = 0;
383                         tv.tv_usec = 100000;
384                         select(0, NULL, NULL, NULL, &tv);
385
386                         /* Kill someone. */
387                         kill(pids[random() % num_procs], SIGUSR1);
388                         continue;
389                 }
390
391                 if (pid == -1) {
392                         perror("failed to wait for child\n");
393                         exit(1);
394                 }
395
396                 for (j=0;j<num_procs;j++) {
397                         if (pids[j] == pid) break;
398                 }
399                 if (j == num_procs) {
400                         printf("unknown child %d exited!?\n", (int)pid);
401                         exit(1);
402                 }
403                 if (WIFSIGNALED(status)) {
404                         if (WTERMSIG(status) == SIGUSR2
405                             || WTERMSIG(status) == SIGUSR1) {
406                                 /* SIGUSR2 means they wrote to pipe. */
407                                 if (WTERMSIG(status) == SIGUSR2) {
408                                         read(pfds[0], &done[j],
409                                              sizeof(done[j]));
410                                 }
411                                 pids[j] = fork();
412                                 if (pids[j] == 0)
413                                         exit(run_child(test_tdb, j, seed,
414                                                        num_loops, done[j]));
415                                 printf("Restarting child %i for %u-%u\n",
416                                        j, done[j], num_loops);
417                                 continue;
418                         }
419                         printf("child %d exited with signal %d\n",
420                                (int)pid, WTERMSIG(status));
421                         error_count++;
422                 } else {
423                         if (WEXITSTATUS(status) != 0) {
424                                 printf("child %d exited with status %d\n",
425                                        (int)pid, WEXITSTATUS(status));
426                                 error_count++;
427                         }
428                 }
429                 memmove(&pids[j], &pids[j+1],
430                         (num_procs - j - 1)*sizeof(pids[0]));
431                 num_procs--;
432         }
433
434         free(pids);
435
436 done:
437         if (error_count == 0) {
438                 db = tdb_open_ex(test_tdb, hash_size, TDB_DEFAULT,
439                                  O_RDWR, 0, &log_ctx, NULL);
440                 if (!db) {
441                         fatal("db open failed");
442                 }
443                 if (tdb_check(db, NULL, NULL) == -1) {
444                         printf("db check failed");
445                         exit(1);
446                 }
447                 tdb_close(db);
448                 printf("OK\n");
449         }
450
451         free(test_tdb);
452         return error_count;
453 }