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