1 #include <ccan/tdb/tdb.h>
2 #include <ccan/grab_file/grab_file.h>
3 #include <ccan/hash/hash.h>
4 #include <ccan/talloc/talloc.h>
5 #include <ccan/str_talloc/str_talloc.h>
6 #include <ccan/str/str.h>
7 #include <ccan/list/list.h>
12 #include <sys/types.h>
20 #define STRINGIFY2(x) #x
21 #define STRINGIFY(x) STRINGIFY2(x)
23 static bool quiet = false;
25 /* Avoid mod by zero */
26 static unsigned int total_keys = 1;
28 /* All the wipe_all ops. */
29 static struct op_desc *wipe_alls = NULL;
30 static unsigned int num_wipe_alls = 0;
32 /* #define DEBUG_DEPS 1 */
34 /* Traversals block transactions in the current implementation. */
35 #define TRAVERSALS_TAKE_TRANSACTION_LOCK 1
40 static struct pipe *pipes;
41 static int backoff_fd = -1;
43 static void __attribute__((noreturn)) fail(const char *filename,
50 fprintf(stderr, "%s:%u: FAIL: ", filename, line);
51 vfprintf(stderr, fmt, ap);
52 fprintf(stderr, "\n");
58 #define try(expr, expect) \
61 if (ret != (expect)) \
62 fail(filename[file], i+1, \
63 STRINGIFY(expr) "= %i", ret); \
66 /* Try or imitate results. */
67 #define unreliable(expr, expect, force, undo) \
70 if (ret != expect) { \
71 fprintf(stderr, "%s:%u: %s gave %i not %i", \
72 filename[file], i+1, STRINGIFY(expr), \
81 static bool key_eq(TDB_DATA a, TDB_DATA b)
83 if (a.dsize != b.dsize)
85 return memcmp(a.dptr, b.dptr, a.dsize) == 0;
88 /* This is based on the hash algorithm from gdbm */
89 static unsigned int hash_key(TDB_DATA *key)
91 uint32_t value; /* Used to compute the hash value. */
92 uint32_t i; /* Used to cycle through random values. */
94 /* Set the initial value from the key size. */
95 for (value = 0x238F13AF ^ key->dsize, i=0; i < key->dsize; i++)
96 value = (value + (key->dptr[i] << (i*5 % 24)));
98 return (1103515243 * value + 12345);
104 OP_TDB_LOCKALL_UNMARK,
105 OP_TDB_LOCKALL_NONBLOCK,
108 OP_TDB_LOCKALL_READ_NONBLOCK,
109 OP_TDB_UNLOCKALL_READ,
111 OP_TDB_CHAINLOCK_NONBLOCK,
112 OP_TDB_CHAINLOCK_MARK,
113 OP_TDB_CHAINLOCK_UNMARK,
115 OP_TDB_CHAINLOCK_READ,
116 OP_TDB_CHAINUNLOCK_READ,
123 OP_TDB_TRANSACTION_START,
124 OP_TDB_TRANSACTION_CANCEL,
125 OP_TDB_TRANSACTION_PREPARE_COMMIT,
126 OP_TDB_TRANSACTION_COMMIT,
127 OP_TDB_TRAVERSE_READ_START,
128 OP_TDB_TRAVERSE_START,
131 OP_TDB_TRAVERSE_END_EARLY,
146 /* Who is waiting for us? */
147 struct list_head post;
148 /* What are we waiting for? */
149 struct list_head pre;
151 /* If I'm part of a group (traverse/transaction) where is
152 * start? (Otherwise, 0) */
153 unsigned int group_start;
156 int flag; /* open and store */
157 struct { /* append */
161 /* transaction/traverse start/chainlock */
162 unsigned int group_len;
171 static unsigned char hex_char(const char *filename, unsigned int line, char c)
174 if (c >= 'A' && c <= 'F')
176 if (c >= '0' && c <= '9')
178 fail(filename, line, "invalid hex character '%c'", c);
181 /* TDB data is <size>:<%02x>* */
182 static TDB_DATA make_tdb_data(const void *ctx,
183 const char *filename, unsigned int line,
190 if (streq(word, "NULL"))
193 data.dsize = atoi(word);
194 data.dptr = talloc_array(ctx, unsigned char, data.dsize);
195 p = strchr(word, ':');
197 fail(filename, line, "invalid tdb data '%s'", word);
199 for (i = 0; i < data.dsize; i++)
200 data.dptr[i] = hex_char(filename, line, p[i*2])*16
201 + hex_char(filename, line, p[i*2+1]);
206 static void add_op(const char *filename, struct op **op, unsigned int i,
207 unsigned int seqnum, enum op_type type)
210 *op = talloc_realloc(NULL, *op, struct op, i+1);
213 new->seqnum = seqnum;
215 new->group_start = 0;
218 static void op_add_nothing(char *filename[], struct op op[],
219 unsigned file, unsigned op_num, char *words[])
222 fail(filename[file], op_num+1, "Expected no arguments");
223 op[op_num].key = tdb_null;
226 static void op_add_key(char *filename[], struct op op[],
227 unsigned file, unsigned op_num, char *words[])
229 if (words[2] == NULL || words[3])
230 fail(filename[file], op_num+1, "Expected just a key");
232 op[op_num].key = make_tdb_data(op, filename[file], op_num+1, words[2]);
236 static void op_add_key_ret(char *filename[], struct op op[],
237 unsigned file, unsigned op_num, char *words[])
239 if (!words[2] || !words[3] || !words[4] || words[5]
240 || !streq(words[3], "="))
241 fail(filename[file], op_num+1, "Expected <key> = <ret>");
242 op[op_num].ret = atoi(words[4]);
243 op[op_num].key = make_tdb_data(op, filename[file], op_num+1, words[2]);
244 /* May only be a unique key if it fails */
245 if (op[op_num].ret != 0)
249 static void op_add_key_data(char *filename[], struct op op[],
250 unsigned file, unsigned op_num, char *words[])
252 if (!words[2] || !words[3] || !words[4] || words[5]
253 || !streq(words[3], "="))
254 fail(filename[file], op_num+1, "Expected <key> = <data>");
255 op[op_num].key = make_tdb_data(op, filename[file], op_num+1, words[2]);
256 op[op_num].data = make_tdb_data(op, filename[file], op_num+1, words[4]);
257 /* Likely only be a unique key if it fails */
258 if (!op[op_num].data.dptr)
260 else if (random() % 2)
264 /* We don't record the keys or data for a traverse, as we don't use them. */
265 static void op_add_traverse(char *filename[], struct op op[],
266 unsigned file, unsigned op_num, char *words[])
268 if (!words[2] || !words[3] || !words[4] || words[5]
269 || !streq(words[3], "="))
270 fail(filename[file], op_num+1, "Expected <key> = <data>");
271 op[op_num].key = tdb_null;
274 /* Full traverse info is useful for debugging, but changing it to
275 * "traversefn" without the data makes the traces *much* smaller! */
276 static void op_add_traversefn(char *filename[], struct op op[],
277 unsigned file, unsigned op_num, char *words[])
280 fail(filename[file], op_num+1, "Expected no values");
281 op[op_num].key = tdb_null;
284 /* <seqnum> tdb_store <rec> <rec> <flag> = <ret> */
285 static void op_add_store(char *filename[], struct op op[],
286 unsigned file, unsigned op_num, char *words[])
288 if (!words[2] || !words[3] || !words[4] || !words[5] || !words[6]
289 || words[7] || !streq(words[5], "="))
290 fail(filename[file], op_num+1, "Expect <key> <data> <flag> = <ret>");
292 op[op_num].flag = strtoul(words[4], NULL, 0);
293 op[op_num].ret = atoi(words[6]);
294 op[op_num].key = make_tdb_data(op, filename[file], op_num+1, words[2]);
295 op[op_num].data = make_tdb_data(op, filename[file], op_num+1, words[3]);
299 /* <seqnum> tdb_append <rec> <rec> = <rec> */
300 static void op_add_append(char *filename[], struct op op[],
301 unsigned file, unsigned op_num, char *words[])
303 if (!words[2] || !words[3] || !words[4] || !words[5] || words[6]
304 || !streq(words[4], "="))
305 fail(filename[file], op_num+1, "Expect <key> <data> = <rec>");
307 op[op_num].key = make_tdb_data(op, filename[file], op_num+1, words[2]);
308 op[op_num].data = make_tdb_data(op, filename[file], op_num+1, words[3]);
310 op[op_num].append.post
311 = make_tdb_data(op, filename[file], op_num+1, words[5]);
313 /* By subtraction, figure out what previous data was. */
314 op[op_num].append.pre.dptr = op[op_num].append.post.dptr;
315 op[op_num].append.pre.dsize
316 = op[op_num].append.post.dsize - op[op_num].data.dsize;
320 /* <seqnum> tdb_get_seqnum = <ret> */
321 static void op_add_seqnum(char *filename[], struct op op[],
322 unsigned file, unsigned op_num, char *words[])
324 if (!words[2] || !words[3] || words[4] || !streq(words[2], "="))
325 fail(filename[file], op_num+1, "Expect = <ret>");
327 op[op_num].key = tdb_null;
328 op[op_num].ret = atoi(words[3]);
331 static void op_add_traverse_start(char *filename[], struct op op[],
332 unsigned file, unsigned op_num, char *words[])
335 fail(filename[file], op_num+1, "Expect no arguments");
337 op[op_num].key = tdb_null;
338 op[op_num].group_len = 0;
341 static void op_add_transaction(char *filename[], struct op op[],
342 unsigned file, unsigned op_num, char *words[])
345 fail(filename[file], op_num+1, "Expect no arguments");
347 op[op_num].key = tdb_null;
348 op[op_num].group_len = 0;
351 static void op_add_chainlock(char *filename[], struct op op[],
352 unsigned file, unsigned op_num, char *words[])
354 if (words[2] == NULL || words[3])
355 fail(filename[file], op_num+1, "Expected just a key");
357 /* A chainlock key isn't a key in the normal sense; it doesn't
358 * have to be in the db at all. Also, we don't want to hash this op. */
359 op[op_num].data = make_tdb_data(op, filename[file], op_num+1, words[2]);
360 op[op_num].key = tdb_null;
361 op[op_num].group_len = 0;
364 static void op_add_chainlock_ret(char *filename[], struct op op[],
365 unsigned file, unsigned op_num, char *words[])
367 if (!words[2] || !words[3] || !words[4] || words[5]
368 || !streq(words[3], "="))
369 fail(filename[file], op_num+1, "Expected <key> = <ret>");
370 op[op_num].ret = atoi(words[4]);
371 op[op_num].data = make_tdb_data(op, filename[file], op_num+1, words[2]);
372 op[op_num].key = tdb_null;
373 op[op_num].group_len = 0;
377 static void op_add_wipe_all(char *filename[], struct op op[],
378 unsigned file, unsigned op_num, char *words[])
381 fail(filename[file], op_num+1, "Expected no arguments");
382 op[op_num].key = tdb_null;
383 wipe_alls = talloc_realloc(NULL, wipe_alls, struct op_desc,
385 wipe_alls[num_wipe_alls].file = file;
386 wipe_alls[num_wipe_alls].op_num = op_num;
390 static int op_find_start(struct op op[], unsigned int op_num, enum op_type type)
394 for (i = op_num-1; i > 0; i--) {
395 if (op[i].type == type && !op[i].group_len)
401 static void op_analyze_transaction(char *filename[], struct op op[],
402 unsigned file, unsigned op_num,
405 unsigned int start, i;
407 op[op_num].key = tdb_null;
410 fail(filename[file], op_num+1, "Expect no arguments");
412 start = op_find_start(op, op_num, OP_TDB_TRANSACTION_START);
414 fail(filename[file], op_num+1, "no transaction start found");
416 op[start].group_len = op_num - start;
418 /* This rolls in nested transactions. I think that's right. */
419 for (i = start; i <= op_num; i++)
420 op[i].group_start = start;
423 /* We treat chainlocks a lot like transactions, even though that's overkill */
424 static void op_analyze_chainlock(char *filename[], struct op op[],
425 unsigned file, unsigned op_num, char *words[])
427 unsigned int i, start;
429 if (words[2] == NULL || words[3])
430 fail(filename[file], op_num+1, "Expected just a key");
432 op[op_num].data = make_tdb_data(op, filename[file], op_num+1, words[2]);
433 op[op_num].key = tdb_null;
436 start = op_find_start(op, op_num, OP_TDB_CHAINLOCK);
438 start = op_find_start(op, op_num, OP_TDB_CHAINLOCK_READ);
440 fail(filename[file], op_num+1, "no initial chainlock found");
442 /* FIXME: We'd have to do something clever to make this work
444 if (!key_eq(op[start].data, op[op_num].data))
445 fail(filename[file], op_num+1, "nested chainlock calls?");
447 op[start].group_len = op_num - start;
448 for (i = start; i <= op_num; i++)
449 op[i].group_start = start;
452 static void op_analyze_traverse(char *filename[], struct op op[],
453 unsigned file, unsigned op_num, char *words[])
457 op[op_num].key = tdb_null;
459 /* = %u means traverse function terminated. */
461 if (!streq(words[2], "=") || !words[3] || words[4])
462 fail(filename[file], op_num+1, "expect = <num>");
463 op[op_num].ret = atoi(words[3]);
467 start = op_find_start(op, op_num, OP_TDB_TRAVERSE_START);
469 start = op_find_start(op, op_num, OP_TDB_TRAVERSE_READ_START);
471 fail(filename[file], op_num+1, "no traversal start found");
473 op[start].group_len = op_num - start;
475 /* Don't roll in nested traverse/chainlock */
476 for (i = start; i <= op_num; i++)
477 if (!op[i].group_start)
478 op[i].group_start = start;
481 /* Keep -Wmissing-declarations happy: */
482 const struct op_table *
483 find_keyword (register const char *str, register unsigned int len);
485 #include "keywords.c"
488 /* We can have more than one */
489 struct list_node pre_list;
490 struct list_node post_list;
491 struct op_desc needs;
492 struct op_desc prereq;
495 static void check_deps(const char *filename, struct op op[], unsigned int num)
500 for (i = 1; i < num; i++)
501 if (!list_empty(&op[i].pre))
502 fail(filename, i+1, "Still has dependencies");
506 static void dump_pre(char *filename[], struct op *op[],
507 unsigned int file, unsigned int i)
512 printf("%s:%u (%u) still waiting for:\n", filename[file], i+1,
514 list_for_each(&op[file][i].pre, dep, pre_list)
515 printf(" %s:%u (%u)\n",
516 filename[dep->prereq.file], dep->prereq.op_num+1,
517 op[dep->prereq.file][dep->prereq.op_num].seqnum);
519 check_deps(filename[file], op[file], i);
522 /* We simply read/write pointers, since we all are children. */
523 static bool do_pre(struct tdb_context *tdb,
524 char *filename[], struct op *op[],
525 unsigned int file, int pre_fd, unsigned int i,
528 while (!list_empty(&op[file][i].pre)) {
532 printf("%s:%u:waiting for pre\n", filename[file], i+1);
539 while (read(pre_fd, &dep, sizeof(dep)) != sizeof(dep)) {
540 if (errno == EINTR) {
542 struct op_desc desc = { file,i };
543 warnx("%s:%u:avoiding deadlock",
544 filename[file], i+1);
545 if (write(backoff_fd, &desc,
546 sizeof(desc)) != sizeof(desc))
547 err(1, "writing backoff_fd");
550 dump_pre(filename, op, file, i);
553 errx(1, "Reading from pipe");
558 printf("%s:%u:got pre %u from %s:%u\n", filename[file], i+1,
559 dep->needs.op_num+1, filename[dep->prereq.file],
560 dep->prereq.op_num+1);
563 /* This could be any op, not just this one. */
569 static void do_post(char *filename[], struct op *op[],
570 unsigned int file, unsigned int i)
574 list_for_each(&op[file][i].post, dep, post_list) {
576 printf("%s:%u:sending to file %s:%u\n", filename[file], i+1,
577 filename[dep->needs.file], dep->needs.op_num+1);
579 if (write(pipes[dep->needs.file].fd[1], &dep, sizeof(dep))
581 err(1, "%s:%u failed to tell file %s",
582 filename[file], i+1, filename[dep->needs.file]);
586 static int get_len(TDB_DATA key, TDB_DATA data, void *private_data)
591 static unsigned run_ops(struct tdb_context *tdb,
596 unsigned int start, unsigned int stop,
599 struct traverse_info {
608 /* More complex. Just do whatever's they did at the n'th entry. */
609 static int nontrivial_traverse(struct tdb_context *tdb,
610 TDB_DATA key, TDB_DATA data,
613 struct traverse_info *tinfo = _tinfo;
614 unsigned int trav_len = tinfo->op[tinfo->file][tinfo->start].group_len;
615 bool avoid_deadlock = false;
617 if (tinfo->i == tinfo->start + trav_len) {
618 /* This can happen if traverse expects to be empty. */
621 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
622 "traverse did not terminate");
625 if (tinfo->op[tinfo->file][tinfo->i].type != OP_TDB_TRAVERSE)
626 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
627 "%s:%u:traverse terminated early");
629 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
630 avoid_deadlock = true;
633 /* Run any normal ops. */
634 tinfo->i = run_ops(tdb, tinfo->pre_fd, tinfo->filename, tinfo->op,
635 tinfo->file, tinfo->i+1, tinfo->start + trav_len,
638 /* We backed off, or we hit OP_TDB_TRAVERSE_END/EARLY. */
639 if (tinfo->op[tinfo->file][tinfo->i].type != OP_TDB_TRAVERSE)
645 static unsigned op_traverse(struct tdb_context *tdb,
649 int (*traversefn)(struct tdb_context *,
650 tdb_traverse_func, void *),
654 struct traverse_info tinfo = { op, filename, file, pre_fd,
657 traversefn(tdb, nontrivial_traverse, &tinfo);
659 /* Traversing in wrong order can have strange effects: eg. if
660 * original traverse went A (delete A), B, we might do B
661 * (delete A). So if we have ops left over, we do it now. */
662 while (tinfo.i != start + op[file][start].group_len) {
663 if (op[file][tinfo.i].type == OP_TDB_TRAVERSE
664 || op[file][tinfo.i].type == OP_TDB_TRAVERSE_END_EARLY)
667 tinfo.i = run_ops(tdb, pre_fd, filename, op, file,
669 start + op[file][start].group_len,
676 static void break_out(int sig)
680 static __attribute__((noinline))
681 unsigned run_ops(struct tdb_context *tdb,
686 unsigned int start, unsigned int stop,
692 sa.sa_handler = break_out;
695 sigaction(SIGALRM, &sa, NULL);
696 for (i = start; i < stop; i++) {
697 if (!do_pre(tdb, filename, op, file, pre_fd, i, backoff))
700 switch (op[file][i].type) {
702 try(tdb_lockall(tdb), op[file][i].ret);
704 case OP_TDB_LOCKALL_MARK:
705 try(tdb_lockall_mark(tdb), op[file][i].ret);
707 case OP_TDB_LOCKALL_UNMARK:
708 try(tdb_lockall_unmark(tdb), op[file][i].ret);
710 case OP_TDB_LOCKALL_NONBLOCK:
711 unreliable(tdb_lockall_nonblock(tdb), op[file][i].ret,
712 tdb_lockall(tdb), tdb_unlockall(tdb));
714 case OP_TDB_UNLOCKALL:
715 try(tdb_unlockall(tdb), op[file][i].ret);
717 case OP_TDB_LOCKALL_READ:
718 try(tdb_lockall_read(tdb), op[file][i].ret);
720 case OP_TDB_LOCKALL_READ_NONBLOCK:
721 unreliable(tdb_lockall_read_nonblock(tdb),
723 tdb_lockall_read(tdb),
724 tdb_unlockall_read(tdb));
726 case OP_TDB_UNLOCKALL_READ:
727 try(tdb_unlockall_read(tdb), op[file][i].ret);
729 case OP_TDB_CHAINLOCK:
730 try(tdb_chainlock(tdb, op[file][i].key),
733 case OP_TDB_CHAINLOCK_NONBLOCK:
734 unreliable(tdb_chainlock_nonblock(tdb, op[file][i].key),
736 tdb_chainlock(tdb, op[file][i].key),
737 tdb_chainunlock(tdb, op[file][i].key));
739 case OP_TDB_CHAINLOCK_MARK:
740 try(tdb_chainlock_mark(tdb, op[file][i].key),
743 case OP_TDB_CHAINLOCK_UNMARK:
744 try(tdb_chainlock_unmark(tdb, op[file][i].key),
747 case OP_TDB_CHAINUNLOCK:
748 try(tdb_chainunlock(tdb, op[file][i].key),
751 case OP_TDB_CHAINLOCK_READ:
752 try(tdb_chainlock_read(tdb, op[file][i].key),
755 case OP_TDB_CHAINUNLOCK_READ:
756 try(tdb_chainunlock_read(tdb, op[file][i].key),
759 case OP_TDB_PARSE_RECORD:
760 try(tdb_parse_record(tdb, op[file][i].key, get_len,
765 try(tdb_exists(tdb, op[file][i].key), op[file][i].ret);
768 try(tdb_store(tdb, op[file][i].key, op[file][i].data,
773 try(tdb_append(tdb, op[file][i].key, op[file][i].data),
776 case OP_TDB_GET_SEQNUM:
777 try(tdb_get_seqnum(tdb), op[file][i].ret);
779 case OP_TDB_WIPE_ALL:
780 try(tdb_wipe_all(tdb), op[file][i].ret);
782 case OP_TDB_TRANSACTION_START:
783 try(tdb_transaction_start(tdb), op[file][i].ret);
785 case OP_TDB_TRANSACTION_CANCEL:
786 try(tdb_transaction_cancel(tdb), op[file][i].ret);
788 case OP_TDB_TRANSACTION_PREPARE_COMMIT:
789 try(tdb_transaction_prepare_commit(tdb),
792 case OP_TDB_TRANSACTION_COMMIT:
793 try(tdb_transaction_commit(tdb), op[file][i].ret);
795 case OP_TDB_TRAVERSE_READ_START:
796 i = op_traverse(tdb, pre_fd, filename, file,
797 tdb_traverse_read, op, i);
799 case OP_TDB_TRAVERSE_START:
800 i = op_traverse(tdb, pre_fd, filename, file,
801 tdb_traverse, op, i);
803 case OP_TDB_TRAVERSE:
804 case OP_TDB_TRAVERSE_END_EARLY:
805 /* Terminate: we're in a traverse, and we've
808 case OP_TDB_TRAVERSE_END:
809 fail(filename[file], i+1, "unexpected end traverse");
810 /* FIXME: These must be treated like traverse. */
811 case OP_TDB_FIRSTKEY:
812 if (!key_eq(tdb_firstkey(tdb), op[file][i].data))
813 fail(filename[file], i+1, "bad firstkey");
816 if (!key_eq(tdb_nextkey(tdb, op[file][i].key),
818 fail(filename[file], i+1, "bad nextkey");
821 TDB_DATA f = tdb_fetch(tdb, op[file][i].key);
822 if (!key_eq(f, op[file][i].data))
823 fail(filename[file], i+1, "bad fetch %u",
828 try(tdb_delete(tdb, op[file][i].key), op[file][i].ret);
831 /* We do nothing here: the transaction and traverse are
832 * traced. It's in the trace to mark it, since it
833 * may become unnecessary in future. */
836 do_post(filename, op, file, i);
841 /* tdbtorture, in particular, can do a tdb_close with a transaction in
843 static struct op *maybe_cancel_transaction(char *filename[], unsigned int file,
844 struct op *op, unsigned int *num)
846 unsigned int start = op_find_start(op, *num, OP_TDB_TRANSACTION_START);
849 char *words[] = { "<unknown>", "tdb_close", NULL };
850 add_op(filename[file], &op, *num, op[start].seqnum,
851 OP_TDB_TRANSACTION_CANCEL);
852 op_analyze_transaction(filename, op, file, *num, words);
858 static struct op *load_tracefile(char *filename[],
861 unsigned int *hashsize,
862 unsigned int *tdb_flags,
863 unsigned int *open_flags)
866 struct op *op = talloc_array(NULL, struct op, 1);
871 contents = grab_file(NULL, filename[file], NULL);
873 err(1, "Reading %s", filename[file]);
875 lines = strsplit(contents, contents, "\n", NULL);
877 errx(1, "%s is empty", filename[file]);
879 words = strsplit(lines, lines[0], " ", NULL);
880 if (!streq(words[1], "tdb_open"))
881 fail(filename[file], 1, "does not start with tdb_open");
883 *hashsize = atoi(words[2]);
884 *tdb_flags = strtoul(words[3], NULL, 0);
885 *open_flags = strtoul(words[4], NULL, 0);
887 for (i = 1; lines[i]; i++) {
888 const struct op_table *opt;
890 words = strsplit(lines, lines[i], " ", NULL);
891 if (!words[0] || !words[1])
892 fail(filename[file], i+1,
893 "Expected seqnum number and op");
895 opt = find_keyword(words[1], strlen(words[1]));
897 if (streq(words[1], "tdb_close")) {
899 fail(filename[file], i+2,
900 "lines after tdb_close");
903 return maybe_cancel_transaction(filename, file,
906 fail(filename[file], i+1,
907 "Unknown operation '%s'", words[1]);
910 add_op(filename[file], &op, i, atoi(words[0]), opt->type);
911 opt->enhance_op(filename, op, file, i, words);
916 "%s:%u:last operation is not tdb_close: incomplete?",
919 talloc_free(contents);
921 return maybe_cancel_transaction(filename, file, op, num);
924 /* We remember all the keys we've ever seen, and who has them. */
927 unsigned int num_users;
928 struct op_desc *user;
931 static bool starts_transaction(const struct op *op)
933 return op->type == OP_TDB_TRANSACTION_START;
936 static bool in_transaction(const struct op op[], unsigned int i)
938 return op[i].group_start && starts_transaction(&op[op[i].group_start]);
941 static bool successful_transaction(const struct op *op)
943 return starts_transaction(op)
944 && op[op->group_len].type == OP_TDB_TRANSACTION_COMMIT;
947 static bool starts_traverse(const struct op *op)
949 return op->type == OP_TDB_TRAVERSE_START
950 || op->type == OP_TDB_TRAVERSE_READ_START;
953 static bool in_traverse(const struct op op[], unsigned int i)
955 return op[i].group_start && starts_traverse(&op[op[i].group_start]);
958 static bool starts_chainlock(const struct op *op)
960 return op->type == OP_TDB_CHAINLOCK_READ
961 || op->type == OP_TDB_CHAINLOCK;
964 static bool in_chainlock(const struct op op[], unsigned int i)
966 return op[i].group_start && starts_chainlock(&op[op[i].group_start]);
969 static const TDB_DATA must_not_exist;
970 static const TDB_DATA must_exist;
971 static const TDB_DATA not_exists_or_empty;
973 /* NULL means doesn't care if it exists or not, &must_exist means
974 * it must exist but we don't care what, &must_not_exist means it must
975 * not exist, otherwise the data it needs. */
976 static const TDB_DATA *needs(const TDB_DATA *key, const struct op *op)
978 /* Look through for an op in this transaction which needs this key. */
979 if (starts_transaction(op) || starts_chainlock(op)) {
981 const TDB_DATA *need = NULL;
983 for (i = 1; i < op->group_len; i++) {
984 if (key_eq(op[i].key, *key)
985 || op[i].type == OP_TDB_WIPE_ALL) {
986 need = needs(key, &op[i]);
987 /* tdb_exists() is special: there might be
988 * something in the transaction with more
989 * specific requirements. Other ops don't have
990 * specific requirements (eg. store or delete),
991 * but they change the value so we can't get
992 * more information from future ops. */
993 if (op[i].type != OP_TDB_EXISTS)
1002 /* FIXME: Pull forward deps, since we can deadlock */
1003 case OP_TDB_CHAINLOCK:
1004 case OP_TDB_CHAINLOCK_NONBLOCK:
1005 case OP_TDB_CHAINLOCK_MARK:
1006 case OP_TDB_CHAINLOCK_UNMARK:
1007 case OP_TDB_CHAINUNLOCK:
1008 case OP_TDB_CHAINLOCK_READ:
1009 case OP_TDB_CHAINUNLOCK_READ:
1013 if (op->append.pre.dsize == 0)
1014 return ¬_exists_or_empty;
1015 return &op->append.pre;
1018 if (op->flag == TDB_INSERT) {
1022 return &must_not_exist;
1023 } else if (op->flag == TDB_MODIFY) {
1025 return &must_not_exist;
1029 /* No flags? Don't care */
1036 return &must_not_exist;
1038 case OP_TDB_PARSE_RECORD:
1040 return &must_not_exist;
1043 /* FIXME: handle these. */
1044 case OP_TDB_WIPE_ALL:
1045 case OP_TDB_FIRSTKEY:
1046 case OP_TDB_NEXTKEY:
1047 case OP_TDB_GET_SEQNUM:
1048 case OP_TDB_TRAVERSE:
1049 case OP_TDB_TRANSACTION_COMMIT:
1050 case OP_TDB_TRANSACTION_CANCEL:
1051 case OP_TDB_TRANSACTION_START:
1056 return &must_not_exist;
1061 return &must_not_exist;
1065 errx(1, "Unexpected op type %i", op->type);
1070 /* What's the data after this op? pre if nothing changed. */
1071 static const TDB_DATA *gives(const TDB_DATA *key, const TDB_DATA *pre,
1072 const struct op *op)
1074 if (starts_transaction(op) || starts_chainlock(op)) {
1077 /* Cancelled transactions don't change anything. */
1078 if (op[op->group_len].type == OP_TDB_TRANSACTION_CANCEL)
1080 assert(op[op->group_len].type == OP_TDB_TRANSACTION_COMMIT
1081 || op[op->group_len].type == OP_TDB_CHAINUNLOCK_READ
1082 || op[op->group_len].type == OP_TDB_CHAINUNLOCK);
1084 for (i = 1; i < op->group_len; i++) {
1085 /* This skips nested transactions, too */
1086 if (key_eq(op[i].key, *key)
1087 || op[i].type == OP_TDB_WIPE_ALL)
1088 pre = gives(key, pre, &op[i]);
1093 /* Failed ops don't change state of db. */
1097 if (op->type == OP_TDB_DELETE || op->type == OP_TDB_WIPE_ALL)
1100 if (op->type == OP_TDB_APPEND)
1101 return &op->append.post;
1103 if (op->type == OP_TDB_STORE)
1109 static void add_hash_user(struct keyinfo *hash,
1113 unsigned int op_num)
1115 hash[h].user = talloc_realloc(hash, hash[h].user,
1116 struct op_desc, hash[h].num_users+1);
1118 /* If it's in a transaction, it's the transaction which
1119 * matters from an analysis POV. */
1120 if (in_transaction(op[file], op_num)
1121 || in_chainlock(op[file], op_num)) {
1124 op_num = op[file][op_num].group_start;
1126 /* Don't include twice. */
1127 for (i = 0; i < hash[h].num_users; i++) {
1128 if (hash[h].user[i].file == file
1129 && hash[h].user[i].op_num == op_num)
1133 hash[h].user[hash[h].num_users].op_num = op_num;
1134 hash[h].user[hash[h].num_users].file = file;
1135 hash[h].num_users++;
1138 static struct keyinfo *hash_ops(struct op *op[], unsigned int num_ops[],
1141 unsigned int i, j, h;
1142 struct keyinfo *hash;
1144 hash = talloc_zero_array(op[0], struct keyinfo, total_keys*2);
1145 for (i = 0; i < num; i++) {
1146 for (j = 1; j < num_ops[i]; j++) {
1147 /* We can't do this on allocation, due to realloc. */
1148 list_head_init(&op[i][j].post);
1149 list_head_init(&op[i][j].pre);
1151 if (!op[i][j].key.dptr)
1154 h = hash_key(&op[i][j].key) % (total_keys * 2);
1155 while (!key_eq(hash[h].key, op[i][j].key)) {
1156 if (!hash[h].key.dptr) {
1157 hash[h].key = op[i][j].key;
1160 h = (h + 1) % (total_keys * 2);
1162 /* Might as well save some memory if we can. */
1163 if (op[i][j].key.dptr != hash[h].key.dptr) {
1164 talloc_free(op[i][j].key.dptr);
1165 op[i][j].key.dptr = hash[h].key.dptr;
1168 add_hash_user(hash, h, op, i, j);
1172 /* Any wipe all entries need adding to all hash entries. */
1173 for (h = 0; h < total_keys*2; h++) {
1174 if (!hash[h].num_users)
1177 for (i = 0; i < num_wipe_alls; i++)
1178 add_hash_user(hash, h, op,
1179 wipe_alls[i].file, wipe_alls[i].op_num);
1185 static bool satisfies(const TDB_DATA *key, const TDB_DATA *data,
1186 const struct op *op)
1188 const TDB_DATA *need = needs(key, op);
1190 /* Don't need anything? Cool. */
1194 /* This should be tdb_null or a real value. */
1195 assert(data != &must_exist);
1196 assert(data != &must_not_exist);
1197 assert(data != ¬_exists_or_empty);
1199 /* Must not exist? data must not exist. */
1200 if (need == &must_not_exist)
1201 return data == &tdb_null;
1204 if (need == &must_exist)
1205 return data != &tdb_null;
1207 /* Either noexist or empty. */
1208 if (need == ¬_exists_or_empty)
1209 return data->dsize == 0;
1211 /* Needs something specific. */
1212 return key_eq(*data, *need);
1215 static void move_to_front(struct op_desc res[], unsigned off, unsigned elem)
1218 struct op_desc tmp = res[elem];
1219 memmove(res + off + 1, res + off, (elem - off)*sizeof(res[0]));
1224 static void restore_to_pos(struct op_desc res[], unsigned off, unsigned elem)
1227 struct op_desc tmp = res[off];
1228 memmove(res + off, res + off + 1, (elem - off)*sizeof(res[0]));
1233 static bool sort_deps(char *filename[], struct op *op[],
1234 struct op_desc res[],
1235 unsigned off, unsigned num,
1236 const TDB_DATA *key, const TDB_DATA *data,
1237 unsigned num_files, unsigned fuzz)
1239 unsigned int i, files_done;
1241 bool done[num_files];
1243 /* None left? We're sorted. */
1247 /* Does this make sequence number go backwards? Allow a little fuzz. */
1249 int seqnum1 = op[res[off-1].file][res[off-1].op_num].seqnum;
1250 int seqnum2 = op[res[off].file][res[off].op_num].seqnum;
1252 if (seqnum1 - seqnum2 > (int)fuzz) {
1254 printf("Seqnum jump too far (%u -> %u)\n",
1261 memset(done, 0, sizeof(done));
1263 /* Since ops within a trace file are ordered, we just need to figure
1264 * out which file to try next. Since we don't take into account
1265 * inter-key relationships (which exist by virtue of trace file order),
1266 * we minimize the chance of harm by trying to keep in seqnum order. */
1267 for (files_done = 0, i = off; i < num && files_done < num_files; i++) {
1268 if (done[res[i].file])
1271 this_op = &op[res[i].file][res[i].op_num];
1273 /* Is what we have good enough for this op? */
1274 if (satisfies(key, data, this_op)) {
1275 move_to_front(res, off, i);
1276 if (sort_deps(filename, op, res, off+1, num,
1277 key, gives(key, data, this_op),
1280 restore_to_pos(res, off, i);
1282 done[res[i].file] = true;
1286 /* No combination worked. */
1290 static void check_dep_sorting(struct op_desc user[], unsigned num_users,
1295 unsigned minima[num_files];
1297 memset(minima, 0, sizeof(minima));
1298 for (i = 0; i < num_users; i++) {
1299 assert(minima[user[i].file] < user[i].op_num);
1300 minima[user[i].file] = user[i].op_num;
1305 /* All these ops happen on the same key. Which comes first?
1307 * This can happen both because read ops or failed write ops don't
1308 * change sequence number, and also due to race since we access the
1309 * number unlocked (the race can cause less detectable ordering problems,
1310 * in which case we'll deadlock and report: fix manually in that case).
1312 static bool figure_deps(char *filename[], struct op *op[],
1313 const TDB_DATA *key, const TDB_DATA *data,
1314 struct op_desc user[],
1315 unsigned num_users, unsigned num_files)
1319 /* We prefer to keep strict seqnum order if possible: it's the
1320 * most likely. We get more lax if that fails. */
1321 for (fuzz = 0; fuzz < 100; fuzz = (fuzz + 1)*2) {
1322 if (sort_deps(filename, op, user, 0, num_users, key, data,
1330 check_dep_sorting(user, num_users, num_files);
1334 /* We're having trouble sorting out dependencies for this key. Assume that it's
1335 * a pre-existing record in the db, so determine a likely value. */
1336 static const TDB_DATA *preexisting_data(char *filename[], struct op *op[],
1337 const TDB_DATA *key,
1338 struct op_desc *user,
1339 unsigned int num_users)
1342 const TDB_DATA *data;
1344 for (i = 0; i < num_users; i++) {
1345 data = needs(key, &op[user->file][user->op_num]);
1346 if (data && data != &must_not_exist) {
1348 printf("%s:%u: needs pre-existing record\n",
1349 filename[user->file], user->op_num+1);
1356 static void sort_ops(struct tdb_context *tdb,
1357 struct keyinfo hash[], char *filename[], struct op *op[],
1362 /* Gcc nexted function extension. How cool is this? */
1363 int compare_seqnum(const void *_a, const void *_b)
1365 const struct op_desc *a = _a, *b = _b;
1367 /* First, maintain order within any trace file. */
1368 if (a->file == b->file)
1369 return a->op_num - b->op_num;
1371 /* Otherwise, arrange by seqnum order. */
1372 if (op[a->file][a->op_num].seqnum !=
1373 op[b->file][b->op_num].seqnum)
1374 return op[a->file][a->op_num].seqnum
1375 - op[b->file][b->op_num].seqnum;
1377 /* Cancelled transactions are assumed to happen first. */
1378 if (starts_transaction(&op[a->file][a->op_num])
1379 && !successful_transaction(&op[a->file][a->op_num]))
1381 if (starts_transaction(&op[b->file][b->op_num])
1382 && !successful_transaction(&op[b->file][b->op_num]))
1389 /* Now sort into seqnum order. */
1390 for (h = 0; h < total_keys * 2; h++) {
1391 struct op_desc *user = hash[h].user;
1393 qsort(user, hash[h].num_users, sizeof(user[0]), compare_seqnum);
1394 if (!figure_deps(filename, op, &hash[h].key, &tdb_null, user,
1395 hash[h].num_users, num)) {
1396 const TDB_DATA *data;
1398 data = preexisting_data(filename, op, &hash[h].key,
1399 user, hash[h].num_users);
1400 /* Give the first op what it wants: does that help? */
1401 if (!figure_deps(filename, op, &hash[h].key, data, user,
1402 hash[h].num_users, num))
1403 fail(filename[user[0].file], user[0].op_num+1,
1404 "Could not resolve inter-dependencies");
1405 if (tdb_store(tdb, hash[h].key, *data, TDB_INSERT) != 0)
1406 errx(1, "Could not store initial value");
1411 static int destroy_depend(struct depend *dep)
1413 list_del(&dep->pre_list);
1414 list_del(&dep->post_list);
1418 static void add_dependency(void *ctx,
1421 const struct op_desc *needs,
1422 const struct op_desc *prereq)
1426 /* We don't depend on ourselves. */
1427 if (needs->file == prereq->file) {
1428 assert(prereq->op_num < needs->op_num);
1433 printf("%s:%u: depends on %s:%u\n",
1434 filename[needs->file], needs->op_num+1,
1435 filename[prereq->file], prereq->op_num+1);
1438 dep = talloc(ctx, struct depend);
1439 dep->needs = *needs;
1440 dep->prereq = *prereq;
1442 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1443 /* If something in a traverse depends on something in another
1444 * traverse/transaction, it creates a dependency between the
1446 if ((in_traverse(op[prereq->file], prereq->op_num)
1447 && (starts_transaction(&op[needs->file][needs->op_num])
1448 || starts_traverse(&op[needs->file][needs->op_num])))
1449 || (in_traverse(op[needs->file], needs->op_num)
1450 && (starts_transaction(&op[prereq->file][prereq->op_num])
1451 || starts_traverse(&op[prereq->file][prereq->op_num])))) {
1454 /* We are satisfied by end of group. */
1455 start = op[prereq->file][prereq->op_num].group_start;
1456 dep->prereq.op_num = start + op[prereq->file][start].group_len;
1457 /* And we need that done by start of our group. */
1458 dep->needs.op_num = op[needs->file][needs->op_num].group_start;
1461 /* There is also this case:
1462 * <traverse> <read foo> ...
1463 * <transaction> ... </transaction> <create foo>
1464 * Where if we start the traverse then wait, we could block
1465 * the transaction and deadlock.
1467 * We try to address this by ensuring that where seqnum indicates it's
1468 * possible, we wait for <create foo> before *starting* traverse.
1470 else if (in_traverse(op[needs->file], needs->op_num)) {
1471 struct op *need = &op[needs->file][needs->op_num];
1472 if (op[needs->file][need->group_start].seqnum >
1473 op[prereq->file][prereq->op_num].seqnum) {
1474 dep->needs.op_num = need->group_start;
1479 /* If you depend on a transaction or chainlock, you actually
1480 * depend on it ending. */
1481 if (starts_transaction(&op[prereq->file][dep->prereq.op_num])
1482 || starts_chainlock(&op[prereq->file][dep->prereq.op_num])) {
1484 += op[dep->prereq.file][dep->prereq.op_num].group_len;
1486 printf("-> Actually end of transaction %s:%u\n",
1487 filename[dep->prereq->file], dep->prereq->op_num+1);
1490 /* We should never create a dependency from middle of
1492 assert(!in_transaction(op[prereq->file], dep->prereq.op_num)
1493 || op[prereq->file][dep->prereq.op_num].type
1494 == OP_TDB_TRANSACTION_COMMIT
1495 || op[prereq->file][dep->prereq.op_num].type
1496 == OP_TDB_TRANSACTION_CANCEL);
1498 list_add(&op[dep->prereq.file][dep->prereq.op_num].post,
1500 list_add(&op[dep->needs.file][dep->needs.op_num].pre,
1502 talloc_set_destructor(dep, destroy_depend);
1505 static bool changes_db(const TDB_DATA *key, const struct op *op)
1507 return gives(key, NULL, op) != NULL;
1510 static void depend_on_previous(struct op *op[],
1513 struct op_desc user[],
1523 if (prev == i - 1) {
1524 /* Just depend on previous. */
1525 add_dependency(NULL, op, filename, &user[i], &user[prev]);
1529 /* We have to wait for the readers. Find last one in *each* file. */
1530 memset(deps, 0, sizeof(deps));
1531 deps[user[i].file] = true;
1532 for (j = i - 1; j > prev; j--) {
1533 if (!deps[user[j].file]) {
1534 add_dependency(NULL, op, filename, &user[i], &user[j]);
1535 deps[user[j].file] = true;
1540 /* This is simple, but not complete. We don't take into account
1541 * indirect dependencies. */
1542 static void optimize_dependencies(struct op *op[], unsigned int num_ops[],
1547 /* There can only be one real dependency on each file */
1548 for (i = 0; i < num; i++) {
1549 for (j = 1; j < num_ops[i]; j++) {
1550 struct depend *dep, *next;
1551 struct depend *prev[num];
1553 memset(prev, 0, sizeof(prev));
1555 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1556 if (!prev[dep->prereq.file]) {
1557 prev[dep->prereq.file] = dep;
1560 if (prev[dep->prereq.file]->prereq.op_num
1561 < dep->prereq.op_num) {
1562 talloc_free(prev[dep->prereq.file]);
1563 prev[dep->prereq.file] = dep;
1570 for (i = 0; i < num; i++) {
1573 for (j = 0; j < num; j++)
1576 for (j = 1; j < num_ops[i]; j++) {
1577 struct depend *dep, *next;
1579 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1580 if (deps[dep->prereq.file]
1581 >= (int)dep->prereq.op_num)
1584 deps[dep->prereq.file]
1585 = dep->prereq.op_num;
1591 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1592 /* Force an order among the traversals, so they don't deadlock (as much) */
1593 static void make_traverse_depends(char *filename[],
1594 struct op *op[], unsigned int num_ops[],
1597 unsigned int i, num_traversals = 0;
1599 struct op_desc *desc;
1601 /* Sort by which one runs first. */
1602 int compare_traverse_desc(const void *_a, const void *_b)
1604 const struct op_desc *da = _a, *db = _b;
1605 const struct op *a = &op[da->file][da->op_num],
1606 *b = &op[db->file][db->op_num];
1608 if (a->seqnum != b->seqnum)
1609 return a->seqnum - b->seqnum;
1611 /* If they have same seqnum, it means one didn't make any
1612 * changes. Thus sort by end in that case. */
1613 return a[a->group_len].seqnum - b[b->group_len].seqnum;
1616 desc = talloc_array(NULL, struct op_desc, 1);
1619 for (i = 0; i < num; i++) {
1620 for (j = 1; j < num_ops[i]; j++) {
1621 /* Traverse start (ignore those in
1622 * transactions; they're already covered by
1623 * transaction dependencies). */
1624 if (starts_traverse(&op[i][j])
1625 && !in_transaction(op[i], j)) {
1626 desc = talloc_realloc(NULL, desc,
1629 desc[num_traversals].file = i;
1630 desc[num_traversals].op_num = j;
1635 qsort(desc, num_traversals, sizeof(desc[0]), compare_traverse_desc);
1637 for (i = 1; i < num_traversals; i++) {
1638 const struct op *prev = &op[desc[i-1].file][desc[i-1].op_num];
1639 const struct op *curr = &op[desc[i].file][desc[i].op_num];
1641 /* Read traverses don't depend on each other (read lock). */
1642 if (prev->type == OP_TDB_TRAVERSE_READ_START
1643 && curr->type == OP_TDB_TRAVERSE_READ_START)
1646 /* Only make dependency if it's clear. */
1647 if (compare_traverse_desc(&desc[i], &desc[i-1])) {
1648 /* i depends on end of traverse i-1. */
1649 struct op_desc end = desc[i-1];
1650 end.op_num += prev->group_len;
1651 add_dependency(NULL, op, filename, &desc[i], &end);
1657 static void set_nonblock(int fd)
1659 if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK) != 0)
1660 err(1, "Setting pipe nonblocking");
1663 static bool handle_backoff(struct op *op[], int fd)
1665 struct op_desc desc;
1666 bool handled = false;
1668 /* Sloppy coding: we assume PIPEBUF never fills. */
1669 while (read(fd, &desc, sizeof(desc)) != -1) {
1672 for (i = desc.op_num; i > 0; i--) {
1673 if (op[desc.file][i].type == OP_TDB_TRAVERSE) {
1674 /* We insert a fake end here. */
1675 op[desc.file][i].type
1676 = OP_TDB_TRAVERSE_END_EARLY;
1678 } else if (starts_traverse(&op[desc.file][i])) {
1679 unsigned int start = i;
1680 struct op tmp = op[desc.file][i];
1681 /* Move the ops outside traverse. */
1682 memmove(&op[desc.file][i],
1683 &op[desc.file][i+1],
1684 (desc.op_num-i-1) * sizeof(op[0][0]));
1685 op[desc.file][desc.op_num] = tmp;
1686 while (op[desc.file][i].group_start == start) {
1687 op[desc.file][i++].group_start
1697 #else /* !TRAVERSALS_TAKE_TRANSACTION_LOCK */
1698 static bool handle_backoff(struct op *op[], int fd)
1704 static void derive_dependencies(struct tdb_context *tdb,
1706 struct op *op[], unsigned int num_ops[],
1709 struct keyinfo *hash;
1712 /* Create hash table for faster key lookup. */
1713 hash = hash_ops(op, num_ops, num);
1715 /* Sort them by sequence number. */
1716 sort_ops(tdb, hash, filename, op, num);
1718 /* Create dependencies back to the last change, rather than
1719 * creating false dependencies by naively making each one
1720 * depend on the previous. This has two purposes: it makes
1721 * later optimization simpler, and it also avoids deadlock with
1722 * same sequence number ops inside traversals (if one
1723 * traversal doesn't write anything, two ops can have the same
1724 * sequence number yet we can create a traversal dependency
1725 * the other way). */
1726 for (h = 0; h < total_keys * 2; h++) {
1729 if (hash[h].num_users < 2)
1732 for (i = 0; i < hash[h].num_users; i++) {
1733 if (changes_db(&hash[h].key, &op[hash[h].user[i].file]
1734 [hash[h].user[i].op_num])) {
1735 depend_on_previous(op, filename, num,
1736 hash[h].user, i, prev);
1738 } else if (prev >= 0)
1739 add_dependency(hash, op, filename,
1741 &hash[h].user[prev]);
1745 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1746 make_traverse_depends(filename, op, num_ops, num);
1749 optimize_dependencies(op, num_ops, num);
1752 static struct timeval run_test(char *argv[],
1753 unsigned int num_ops[],
1754 unsigned int hashsize[],
1755 unsigned int tdb_flags[],
1756 unsigned int open_flags[],
1761 struct timeval start, end, diff;
1764 for (i = 0; argv[i+2]; i++) {
1765 struct tdb_context *tdb;
1770 err(1, "fork failed");
1773 tdb = tdb_open(argv[1], hashsize[i],
1774 tdb_flags[i], open_flags[i], 0600);
1776 err(1, "Opening tdb %s", argv[1]);
1778 /* This catches parent exiting. */
1779 if (read(fds[0], &c, 1) != 1)
1781 run_ops(tdb, pipes[i].fd[0], argv+2, op, i, 1,
1783 check_deps(argv[2+i], op[i], num_ops[i]);
1790 /* Let everything settle. */
1794 printf("Starting run...");
1796 gettimeofday(&start, NULL);
1797 /* Tell them all to go! Any write of sufficient length will do. */
1798 if (write(fds[1], hashsize, i) != i)
1799 err(1, "Writing to wakeup pipe");
1801 for (i = 0; argv[i + 2]; i++) {
1804 if (!WIFEXITED(status)) {
1805 warnx("Child died with signal %i", WTERMSIG(status));
1807 } else if (WEXITSTATUS(status) != 0)
1808 /* Assume child spat out error. */
1814 gettimeofday(&end, NULL);
1818 if (end.tv_usec < start.tv_usec) {
1819 end.tv_usec += 1000000;
1822 diff.tv_sec = end.tv_sec - start.tv_sec;
1823 diff.tv_usec = end.tv_usec - start.tv_usec;
1827 static void init_tdb(struct tdb_context *master_tdb,
1828 const char *name, unsigned int hashsize)
1831 struct tdb_context *tdb;
1833 tdb = tdb_open(name, hashsize, TDB_CLEAR_IF_FIRST|TDB_NOSYNC,
1834 O_CREAT|O_TRUNC|O_RDWR, 0600);
1836 errx(1, "opening tdb %s", name);
1838 for (key = tdb_firstkey(master_tdb);
1840 key = tdb_nextkey(master_tdb, key)) {
1841 data = tdb_fetch(master_tdb, key);
1842 if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
1843 errx(1, "Failed to store initial key");
1848 int main(int argc, char *argv[])
1850 struct timeval diff;
1851 unsigned int i, num_ops[argc], hashsize[argc], tdb_flags[argc], open_flags[argc];
1852 struct op *op[argc];
1854 struct tdb_context *master;
1855 unsigned int runs = 1;
1858 errx(1, "Usage: %s [--quiet] [-n <number>] <tdbfile> <tracefile>...", argv[0]);
1860 if (streq(argv[1], "--quiet")) {
1865 if (streq(argv[1], "-n")) {
1866 runs = atoi(argv[2]);
1871 pipes = talloc_array(NULL, struct pipe, argc - 1);
1872 for (i = 0; i < argc - 2; i++) {
1874 printf("Loading tracefile %s...", argv[2+i]);
1876 op[i] = load_tracefile(argv+2, i, &num_ops[i], &hashsize[i],
1877 &tdb_flags[i], &open_flags[i]);
1878 if (pipe(pipes[i].fd) != 0)
1879 err(1, "creating pipe");
1880 /* Don't truncate, or clear if first: we do that. */
1881 open_flags[i] &= ~(O_TRUNC);
1882 tdb_flags[i] &= ~(TDB_CLEAR_IF_FIRST);
1883 /* Open NOSYNC, to save time. */
1884 tdb_flags[i] |= TDB_NOSYNC;
1889 /* Dependency may figure we need to create seed records. */
1890 master = tdb_open(NULL, 0, TDB_INTERNAL, O_RDWR, 0);
1892 printf("Calculating inter-dependencies...");
1895 derive_dependencies(master, argv+2, op, num_ops, i);
1899 for (i = 0; i < runs; i++) {
1900 init_tdb(master, argv[1], hashsize[0]);
1902 /* Don't fork for single arg case: simple debugging. */
1904 struct timeval start, end;
1905 struct tdb_context *tdb;
1907 tdb = tdb_open(argv[1], hashsize[0], tdb_flags[0],
1908 open_flags[0], 0600);
1910 printf("Single threaded run...");
1913 gettimeofday(&start, NULL);
1915 run_ops(tdb, pipes[0].fd[0], argv+2, op, 0, 1,
1917 gettimeofday(&end, NULL);
1922 check_deps(argv[2], op[0], num_ops[0]);
1923 if (end.tv_usec < start.tv_usec) {
1924 end.tv_usec += 1000000;
1927 diff.tv_sec = end.tv_sec - start.tv_sec;
1928 diff.tv_usec = end.tv_usec - start.tv_usec;
1933 err(1, "creating pipe");
1935 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1936 if (pipe(pipes[argc-2].fd) != 0)
1937 err(1, "creating pipe");
1938 backoff_fd = pipes[argc-2].fd[1];
1939 set_nonblock(pipes[argc-2].fd[1]);
1940 set_nonblock(pipes[argc-2].fd[0]);
1944 diff = run_test(argv, num_ops, hashsize, tdb_flags,
1945 open_flags, op, fds);
1946 } while (handle_backoff(op, pipes[argc-2].fd[0]));
1950 printf("Time replaying: ");
1951 printf("%lu usec\n", diff.tv_sec * 1000000UL + diff.tv_usec);