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>
19 #define STRINGIFY2(x) #x
20 #define STRINGIFY(x) STRINGIFY2(x)
22 /* Avoid mod by zero */
23 static unsigned int total_keys = 1;
25 /* #define DEBUG_DEPS 1 */
27 /* Traversals block transactions in the current implementation. */
28 #define TRAVERSALS_TAKE_TRANSACTION_LOCK 1
33 static struct pipe *pipes;
35 static void __attribute__((noreturn)) fail(const char *filename,
42 fprintf(stderr, "%s:%u: FAIL: ", filename, line);
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
50 #define try(expr, expect) \
53 if (ret != (expect)) \
54 fail(filename[file], i+1, \
55 STRINGIFY(expr) "= %i", ret); \
58 /* Try or imitate results. */
59 #define unreliable(expr, expect, force, undo) \
62 if (ret != expect) { \
63 fprintf(stderr, "%s:%u: %s gave %i not %i", \
64 filename[file], i+1, STRINGIFY(expr), \
73 static bool key_eq(TDB_DATA a, TDB_DATA b)
75 if (a.dsize != b.dsize)
77 return memcmp(a.dptr, b.dptr, a.dsize) == 0;
80 /* This is based on the hash algorithm from gdbm */
81 static unsigned int hash_key(TDB_DATA *key)
83 uint32_t value; /* Used to compute the hash value. */
84 uint32_t i; /* Used to cycle through random values. */
86 /* Set the initial value from the key size. */
87 for (value = 0x238F13AF ^ key->dsize, i=0; i < key->dsize; i++)
88 value = (value + (key->dptr[i] << (i*5 % 24)));
90 return (1103515243 * value + 12345);
96 OP_TDB_LOCKALL_UNMARK,
97 OP_TDB_LOCKALL_NONBLOCK,
100 OP_TDB_LOCKALL_READ_NONBLOCK,
101 OP_TDB_UNLOCKALL_READ,
103 OP_TDB_CHAINLOCK_NONBLOCK,
104 OP_TDB_CHAINLOCK_MARK,
105 OP_TDB_CHAINLOCK_UNMARK,
107 OP_TDB_CHAINLOCK_READ,
108 OP_TDB_CHAINUNLOCK_READ,
115 OP_TDB_TRANSACTION_START,
116 OP_TDB_TRANSACTION_CANCEL,
117 OP_TDB_TRANSACTION_COMMIT,
118 OP_TDB_TRAVERSE_READ_START,
119 OP_TDB_TRAVERSE_START,
135 /* Who is waiting for us? */
136 struct list_head post;
137 /* What are we waiting for? */
138 struct list_head pre;
140 /* If I'm part of a group (traverse/transaction) where is
141 * start? (Otherwise, 0) */
142 unsigned int group_start;
145 int flag; /* open and store */
146 struct traverse *trav; /* traverse start */
147 struct { /* append */
151 unsigned int transaction_end; /* transaction start */
155 static unsigned char hex_char(const char *filename, unsigned int line, char c)
158 if (c >= 'A' && c <= 'F')
160 if (c >= '0' && c <= '9')
162 fail(filename, line, "invalid hex character '%c'", c);
165 /* TDB data is <size>:<%02x>* */
166 static TDB_DATA make_tdb_data(const void *ctx,
167 const char *filename, unsigned int line,
174 if (streq(word, "NULL"))
177 data.dsize = atoi(word);
178 data.dptr = talloc_array(ctx, unsigned char, data.dsize);
179 p = strchr(word, ':');
181 fail(filename, line, "invalid tdb data '%s'", word);
183 for (i = 0; i < data.dsize; i++)
184 data.dptr[i] = hex_char(filename, line, p[i*2])*16
185 + hex_char(filename, line, p[i*2+1]);
190 static void add_op(const char *filename, struct op **op, unsigned int i,
191 unsigned int serial, enum op_type type)
194 *op = talloc_realloc(NULL, *op, struct op, i+1);
197 new->serial = serial;
199 new->group_start = 0;
202 static void op_add_nothing(const char *filename,
203 struct op op[], unsigned int op_num, char *words[])
206 fail(filename, op_num+1, "Expected no arguments");
207 op[op_num].key = tdb_null;
210 static void op_add_key(const char *filename,
211 struct op op[], unsigned int op_num, char *words[])
213 if (words[2] == NULL || words[3])
214 fail(filename, op_num+1, "Expected just a key");
216 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
217 if (op[op_num].op != OP_TDB_TRAVERSE)
221 static void op_add_key_ret(const char *filename,
222 struct op op[], unsigned int op_num, char *words[])
224 if (!words[2] || !words[3] || !words[4] || words[5]
225 || !streq(words[3], "="))
226 fail(filename, op_num+1, "Expected <key> = <ret>");
227 op[op_num].ret = atoi(words[4]);
228 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
229 /* May only be a unique key if it fails */
230 if (op[op_num].ret != 0)
234 static void op_add_key_data(const char *filename,
235 struct op op[], unsigned int op_num, char *words[])
237 if (!words[2] || !words[3] || !words[4] || words[5]
238 || !streq(words[3], "="))
239 fail(filename, op_num+1, "Expected <key> = <data>");
240 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
241 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[4]);
242 /* May only be a unique key if it fails */
243 if (!op[op_num].data.dptr)
247 /* <serial> tdb_store <rec> <rec> <flag> = <ret> */
248 static void op_add_store(const char *filename,
249 struct op op[], unsigned int op_num, char *words[])
251 if (!words[2] || !words[3] || !words[4] || !words[5] || !words[6]
252 || words[7] || !streq(words[5], "="))
253 fail(filename, op_num+1, "Expect <key> <data> <flag> = <ret>");
255 op[op_num].flag = strtoul(words[4], NULL, 0);
256 op[op_num].ret = atoi(words[6]);
257 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
258 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
262 /* <serial> tdb_append <rec> <rec> = <rec> */
263 static void op_add_append(const char *filename,
264 struct op op[], unsigned int op_num, char *words[])
266 if (!words[2] || !words[3] || !words[4] || !words[5] || words[6]
267 || !streq(words[4], "="))
268 fail(filename, op_num+1, "Expect <key> <data> = <rec>");
270 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
271 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
273 op[op_num].append.post
274 = make_tdb_data(op, filename, op_num+1, words[5]);
276 /* By subtraction, figure out what previous data was. */
277 op[op_num].append.pre.dptr = op[op_num].append.post.dptr;
278 op[op_num].append.pre.dsize
279 = op[op_num].append.post.dsize - op[op_num].data.dsize;
283 /* <serial> tdb_get_seqnum = <ret> */
284 static void op_add_seqnum(const char *filename,
285 struct op op[], unsigned int op_num, char *words[])
287 if (!words[2] || !words[3] || words[4] || !streq(words[2], "="))
288 fail(filename, op_num+1, "Expect = <ret>");
290 op[op_num].key = tdb_null;
291 op[op_num].ret = atoi(words[3]);
294 static void op_add_traverse(const char *filename,
295 struct op op[], unsigned int op_num, char *words[])
298 fail(filename, op_num+1, "Expect no arguments");
300 op[op_num].key = tdb_null;
301 op[op_num].trav = NULL;
304 static void op_add_transaction(const char *filename, struct op op[],
305 unsigned int op_num, char *words[])
308 fail(filename, op_num+1, "Expect no arguments");
310 op[op_num].key = tdb_null;
311 op[op_num].transaction_end = 0;
314 static void op_analyze_transaction(const char *filename,
315 struct op op[], unsigned int op_num,
320 op[op_num].key = tdb_null;
323 fail(filename, op_num+1, "Expect no arguments");
325 for (i = op_num-1; i >= 0; i--) {
326 if (op[i].op == OP_TDB_TRANSACTION_START &&
327 !op[i].transaction_end)
332 fail(filename, op_num+1, "no transaction start found");
335 op[start].transaction_end = op_num;
337 /* This rolls in nested transactions. I think that's right. */
338 for (i++; i <= op_num; i++)
339 op[i].group_start = start;
342 struct traverse_hash {
347 /* A traverse is a hash of keys, each one associated with ops. */
349 /* How many traversal callouts should I do? */
352 /* Where is traversal end op? */
355 /* For trivial traversals. */
356 struct traverse_hash *hash;
359 /* A trivial traversal is one which doesn't terminate early and only
360 * plays with its own record. We can reliably replay these even if
361 * traverse order changes. */
362 static bool is_trivial_traverse(struct op op[], unsigned int end)
366 TDB_DATA cur = tdb_null;
368 if (op[end].ret != 0)
371 for (i = 0; i < end; i++) {
374 if (op[i].op == OP_TDB_TRAVERSE)
376 if (!key_eq(cur, op[i].key))
381 /* With multiple things happening at once, no traverse is trivial. */
385 static void op_analyze_traverse(const char *filename,
386 struct op op[], unsigned int op_num,
390 struct traverse *trav = talloc(op, struct traverse);
392 op[op_num].key = tdb_null;
394 /* = %u means traverse function terminated. */
396 if (!streq(words[2], "=") || !words[3] || words[4])
397 fail(filename, op_num+1, "expect = <num>");
398 op[op_num].ret = atoi(words[3]);
404 for (i = op_num-1; i >= 0; i--) {
405 if (op[i].op == OP_TDB_TRAVERSE)
407 if (op[i].op != OP_TDB_TRAVERSE_READ_START
408 && op[i].op != OP_TDB_TRAVERSE_START)
416 fail(filename, op_num+1, "no traversal start found");
419 op[start].trav = trav;
421 for (i = start; i <= op_num; i++)
422 op[i].group_start = start;
424 if (is_trivial_traverse(op+i, op_num-i)) {
425 /* Fill in a plentiful hash table. */
426 op[start].trav->hash = talloc_zero_array(op[i].trav,
427 struct traverse_hash,
429 for (i = start; i < op_num; i++) {
431 if (op[i].op != OP_TDB_TRAVERSE)
433 h = hash_key(&op[i].key) % (trav->num * 2);
434 while (trav->hash[h].index)
435 h = (h + 1) % (trav->num * 2);
436 trav->hash[h].index = i+1;
437 trav->hash[h].key = op[i].key;
443 /* Keep -Wmissing-declarations happy: */
444 const struct op_table *
445 find_keyword (register const char *str, register unsigned int len);
447 #include "keywords.c"
450 /* We can have more than one */
451 struct list_node pre_list;
452 struct list_node post_list;
453 unsigned int needs_file;
454 unsigned int needs_opnum;
455 unsigned int satisfies_file;
456 unsigned int satisfies_opnum;
459 static void check_deps(const char *filename, struct op op[], unsigned int num)
464 for (i = 1; i < num; i++)
465 if (!list_empty(&op[i].pre))
466 fail(filename, i+1, "Still has dependencies");
470 static void dump_pre(char *filename[], unsigned int file,
471 struct op op[], unsigned int i)
475 printf("%s:%u still waiting for:\n", filename[file], i+1);
476 list_for_each(&op[i].pre, dep, pre_list)
478 filename[dep->satisfies_file], dep->satisfies_opnum+1);
479 check_deps(filename[file], op, i);
482 /* We simply read/write pointers, since we all are children. */
483 static void do_pre(char *filename[], unsigned int file, int pre_fd,
484 struct op op[], unsigned int i)
486 while (!list_empty(&op[i].pre)) {
490 printf("%s:%u:waiting for pre\n", filename[file], i+1);
494 while (read(pre_fd, &dep, sizeof(dep)) != sizeof(dep)) {
495 if (errno == EINTR) {
496 dump_pre(filename, file, op, i);
499 errx(1, "Reading from pipe");
504 printf("%s:%u:got pre %u from %s:%u\n", filename[file], i+1,
505 dep->needs_op, dep->satisfies_file, dep->satisfies_op+1);
508 /* This could be any op, not just this one. */
513 static void do_post(char *filename[], unsigned int file,
514 const struct op op[], unsigned int i)
518 list_for_each(&op[i].post, dep, post_list) {
520 printf("%s:%u:sending to file %s:%u\n", filename[file], i+1,
521 filename[dep->needs_file], dep->needs_opnum+1);
523 if (write(pipes[dep->needs_file].fd[1], &dep, sizeof(dep))
525 err(1, "%s:%u failed to tell file %s",
526 filename[file], i+1, filename[dep->needs_file]);
530 static int get_len(TDB_DATA key, TDB_DATA data, void *private_data)
535 static unsigned run_ops(struct tdb_context *tdb,
540 unsigned int start, unsigned int stop);
542 struct traverse_info {
551 /* Trivial case: do whatever they did for this key. */
552 static int trivial_traverse(struct tdb_context *tdb,
553 TDB_DATA key, TDB_DATA data,
556 struct traverse_info *tinfo = _tinfo;
557 struct traverse *trav = tinfo->op[tinfo->start].trav;
558 unsigned int h = hash_key(&key) % (trav->num * 2);
560 while (trav->hash[h].index) {
561 if (key_eq(trav->hash[h].key, key)) {
562 run_ops(tdb, tinfo->pre_fd, tinfo->filename,
563 tinfo->file, tinfo->op, trav->hash[h].index,
568 h = (h + 1) % (trav->num * 2);
570 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
571 "unexpected traverse key");
574 /* More complex. Just do whatever's they did at the n'th entry. */
575 static int nontrivial_traverse(struct tdb_context *tdb,
576 TDB_DATA key, TDB_DATA data,
579 struct traverse_info *tinfo = _tinfo;
580 struct traverse *trav = tinfo->op[tinfo->start].trav;
582 if (tinfo->i == trav->end) {
583 /* This can happen if traverse expects to be empty. */
584 if (tinfo->start + 1 == trav->end)
586 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
587 "traverse did not terminate");
590 if (tinfo->op[tinfo->i].op != OP_TDB_TRAVERSE)
591 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
592 "%s:%u:traverse terminated early");
594 /* Run any normal ops. */
595 tinfo->i = run_ops(tdb, tinfo->pre_fd, tinfo->filename, tinfo->file,
596 tinfo->op, tinfo->i+1, trav->end);
598 if (tinfo->i == trav->end)
604 static unsigned op_traverse(struct tdb_context *tdb,
608 int (*traversefn)(struct tdb_context *,
609 tdb_traverse_func, void *),
613 struct traverse *trav = op[start].trav;
614 struct traverse_info tinfo = { op, filename, file, pre_fd,
619 int ret = traversefn(tdb, trivial_traverse, &tinfo);
620 if (ret != trav->num)
621 fail(filename[file], start+1,
622 "short traversal %i", ret);
626 traversefn(tdb, nontrivial_traverse, &tinfo);
628 /* Traversing in wrong order can have strange effects: eg. if
629 * original traverse went A (delete A), B, we might do B
630 * (delete A). So if we have ops left over, we do it now. */
631 while (tinfo.i != trav->end) {
632 if (op[tinfo.i].op == OP_TDB_TRAVERSE)
635 tinfo.i = run_ops(tdb, pre_fd, filename, file, op,
642 static void break_out(int sig)
646 static __attribute__((noinline))
647 unsigned run_ops(struct tdb_context *tdb,
651 struct op op[], unsigned int start, unsigned int stop)
656 sa.sa_handler = break_out;
659 sigaction(SIGALRM, &sa, NULL);
660 for (i = start; i < stop; i++) {
661 do_pre(filename, file, pre_fd, op, i);
665 try(tdb_lockall(tdb), op[i].ret);
667 case OP_TDB_LOCKALL_MARK:
668 try(tdb_lockall_mark(tdb), op[i].ret);
670 case OP_TDB_LOCKALL_UNMARK:
671 try(tdb_lockall_unmark(tdb), op[i].ret);
673 case OP_TDB_LOCKALL_NONBLOCK:
674 unreliable(tdb_lockall_nonblock(tdb), op[i].ret,
675 tdb_lockall(tdb), tdb_unlockall(tdb));
677 case OP_TDB_UNLOCKALL:
678 try(tdb_unlockall(tdb), op[i].ret);
680 case OP_TDB_LOCKALL_READ:
681 try(tdb_lockall_read(tdb), op[i].ret);
683 case OP_TDB_LOCKALL_READ_NONBLOCK:
684 unreliable(tdb_lockall_read_nonblock(tdb), op[i].ret,
685 tdb_lockall_read(tdb),
686 tdb_unlockall_read(tdb));
688 case OP_TDB_UNLOCKALL_READ:
689 try(tdb_unlockall_read(tdb), op[i].ret);
691 case OP_TDB_CHAINLOCK:
692 try(tdb_chainlock(tdb, op[i].key), op[i].ret);
694 case OP_TDB_CHAINLOCK_NONBLOCK:
695 unreliable(tdb_chainlock_nonblock(tdb, op[i].key),
697 tdb_chainlock(tdb, op[i].key),
698 tdb_chainunlock(tdb, op[i].key));
700 case OP_TDB_CHAINLOCK_MARK:
701 try(tdb_chainlock_mark(tdb, op[i].key), op[i].ret);
703 case OP_TDB_CHAINLOCK_UNMARK:
704 try(tdb_chainlock_unmark(tdb, op[i].key), op[i].ret);
706 case OP_TDB_CHAINUNLOCK:
707 try(tdb_chainunlock(tdb, op[i].key), op[i].ret);
709 case OP_TDB_CHAINLOCK_READ:
710 try(tdb_chainlock_read(tdb, op[i].key), op[i].ret);
712 case OP_TDB_CHAINUNLOCK_READ:
713 try(tdb_chainunlock_read(tdb, op[i].key), op[i].ret);
715 case OP_TDB_PARSE_RECORD:
716 try(tdb_parse_record(tdb, op[i].key, get_len, NULL),
720 try(tdb_exists(tdb, op[i].key), op[i].ret);
723 try(tdb_store(tdb, op[i].key, op[i].data, op[i].flag),
727 try(tdb_append(tdb, op[i].key, op[i].data), op[i].ret);
729 case OP_TDB_GET_SEQNUM:
730 try(tdb_get_seqnum(tdb), op[i].ret);
732 case OP_TDB_WIPE_ALL:
733 try(tdb_wipe_all(tdb), op[i].ret);
735 case OP_TDB_TRANSACTION_START:
736 try(tdb_transaction_start(tdb), op[i].ret);
738 case OP_TDB_TRANSACTION_CANCEL:
739 try(tdb_transaction_cancel(tdb), op[i].ret);
741 case OP_TDB_TRANSACTION_COMMIT:
742 try(tdb_transaction_commit(tdb), op[i].ret);
744 case OP_TDB_TRAVERSE_READ_START:
745 i = op_traverse(tdb, pre_fd, filename, file,
746 tdb_traverse_read, op, i);
748 case OP_TDB_TRAVERSE_START:
749 i = op_traverse(tdb, pre_fd, filename, file,
750 tdb_traverse, op, i);
752 case OP_TDB_TRAVERSE:
753 /* Terminate: we're in a traverse, and we've
756 case OP_TDB_TRAVERSE_END:
757 fail(filename[file], i+1, "unepxected end traverse");
758 /* FIXME: These must be treated like traverse. */
759 case OP_TDB_FIRSTKEY:
760 if (!key_eq(tdb_firstkey(tdb), op[i].data))
761 fail(filename[file], i+1, "bad firstkey");
764 if (!key_eq(tdb_nextkey(tdb, op[i].key), op[i].data))
765 fail(filename[file], i+1, "bad nextkey");
768 TDB_DATA f = tdb_fetch(tdb, op[i].key);
769 if (!key_eq(f, op[i].data))
770 fail(filename[file], i+1, "bad fetch %u",
775 try(tdb_delete(tdb, op[i].key), op[i].ret);
778 do_post(filename, file, op, i);
783 static struct op *load_tracefile(const char *filename, unsigned int *num,
784 unsigned int *hashsize,
785 unsigned int *tdb_flags,
786 unsigned int *open_flags)
789 struct op *op = talloc_array(NULL, struct op, 1);
794 file = grab_file(NULL, filename, NULL);
796 err(1, "Reading %s", filename);
798 lines = strsplit(file, file, "\n", NULL);
800 errx(1, "%s is empty", filename);
802 words = strsplit(lines, lines[0], " ", NULL);
803 if (!streq(words[1], "tdb_open"))
804 fail(filename, 1, "does not start with tdb_open");
806 *hashsize = atoi(words[2]);
807 *tdb_flags = strtoul(words[3], NULL, 0);
808 *open_flags = strtoul(words[4], NULL, 0);
810 for (i = 1; lines[i]; i++) {
811 const struct op_table *opt;
813 words = strsplit(lines, lines[i], " ", NULL);
814 if (!words[0] || !words[1])
815 fail(filename, i+1, "Expected serial number and op");
817 opt = find_keyword(words[1], strlen(words[1]));
819 if (streq(words[1], "tdb_close")) {
822 "lines after tdb_close");
827 fail(filename, i+1, "Unknown operation '%s'", words[1]);
830 add_op(filename, &op, i, atoi(words[0]), opt->type);
831 opt->enhance_op(filename, op, i, words);
834 fprintf(stderr, "%s:%u:last operation is not tdb_close: incomplete?",
841 /* We remember all the keys we've ever seen, and who has them. */
849 unsigned int num_users;
850 struct key_user *user;
853 static const TDB_DATA must_not_exist;
854 static const TDB_DATA must_exist;
855 static const TDB_DATA not_exists_or_empty;
857 /* NULL means doesn't care if it exists or not, &must_exist means
858 * it must exist but we don't care what, &must_not_exist means it must
859 * not exist, otherwise the data it needs. */
860 static const TDB_DATA *needs(const struct op *op)
863 /* FIXME: Pull forward deps, since we can deadlock */
864 case OP_TDB_CHAINLOCK:
865 case OP_TDB_CHAINLOCK_NONBLOCK:
866 case OP_TDB_CHAINLOCK_MARK:
867 case OP_TDB_CHAINLOCK_UNMARK:
868 case OP_TDB_CHAINUNLOCK:
869 case OP_TDB_CHAINLOCK_READ:
870 case OP_TDB_CHAINUNLOCK_READ:
874 if (op->append.pre.dsize == 0)
875 return ¬_exists_or_empty;
876 return &op->append.pre;
879 if (op->flag == TDB_INSERT) {
883 return &must_not_exist;
884 } else if (op->flag == TDB_MODIFY) {
886 return &must_not_exist;
890 /* No flags? Don't care */
897 return &must_not_exist;
899 case OP_TDB_PARSE_RECORD:
901 return &must_not_exist;
904 /* FIXME: handle these. */
905 case OP_TDB_WIPE_ALL:
906 case OP_TDB_FIRSTKEY:
908 case OP_TDB_GET_SEQNUM:
909 case OP_TDB_TRAVERSE:
910 case OP_TDB_TRANSACTION_COMMIT:
911 case OP_TDB_TRANSACTION_CANCEL:
912 case OP_TDB_TRANSACTION_START:
917 return &must_not_exist;
922 return &must_not_exist;
926 errx(1, "Unexpected op %i", op->op);
931 /* What's the data after this op? pre if nothing changed. */
932 static const TDB_DATA *gives(const struct op *op, const TDB_DATA *pre)
934 /* Failed ops don't change state of db. */
938 if (op->op == OP_TDB_DELETE || op->op == OP_TDB_WIPE_ALL)
941 if (op->op == OP_TDB_APPEND)
942 return &op->append.post;
944 if (op->op == OP_TDB_STORE)
950 static struct keyinfo *hash_ops(struct op *op[], unsigned int num_ops[],
953 unsigned int i, j, h;
954 struct keyinfo *hash;
956 hash = talloc_zero_array(op[0], struct keyinfo, total_keys*2);
957 for (i = 0; i < num; i++) {
958 for (j = 1; j < num_ops[i]; j++) {
959 /* We can't do this on allocation, due to realloc. */
960 list_head_init(&op[i][j].post);
961 list_head_init(&op[i][j].pre);
963 if (!op[i][j].key.dptr)
966 /* We don't wait for traverse keys */
967 /* FIXME: We should, for trivial traversals. */
968 if (op[i][j].op == OP_TDB_TRAVERSE)
971 h = hash_key(&op[i][j].key) % (total_keys * 2);
972 while (!key_eq(hash[h].key, op[i][j].key)) {
973 if (!hash[h].key.dptr) {
974 hash[h].key = op[i][j].key;
977 h = (h + 1) % (total_keys * 2);
979 /* Might as well save some memory if we can. */
980 if (op[i][j].key.dptr != hash[h].key.dptr) {
981 talloc_free(op[i][j].key.dptr);
982 op[i][j].key.dptr = hash[h].key.dptr;
984 hash[h].user = talloc_realloc(hash, hash[h].user,
986 hash[h].num_users+1);
987 hash[h].user[hash[h].num_users].op_num = j;
988 hash[h].user[hash[h].num_users].file = i;
996 static bool satisfies(const TDB_DATA *data, const TDB_DATA *need)
998 /* Don't need anything? Cool. */
1002 /* This should be tdb_null or a real value. */
1003 assert(data != &must_exist);
1004 assert(data != &must_not_exist);
1005 assert(data != ¬_exists_or_empty);
1007 /* must_not_exist == must_not_exist, must_exist == must_exist, or
1008 not_exists_or_empty == not_exists_or_empty. */
1009 if (data->dsize == need->dsize && data->dptr == need->dptr)
1012 /* Must not exist? data must not exist. */
1013 if (need == &must_not_exist)
1014 return data->dptr == NULL;
1017 if (need == &must_exist)
1018 return data->dptr != NULL;
1020 /* Either noexist or empty. */
1021 if (need == ¬_exists_or_empty)
1022 return data->dsize == 0;
1024 /* Needs something specific. */
1025 return key_eq(*data, *need);
1028 static bool sort_deps(char *filename[], struct op *op[],
1029 struct key_user res[], unsigned num,
1030 const TDB_DATA *data)
1034 /* Nothing left? We're sorted. */
1038 for (i = 0; i < num; i++) {
1039 struct op *this_op = &op[res[i].file][res[i].op_num];
1041 /* Is what we have good enough for this op? */
1042 if (satisfies(data, needs(this_op))) {
1043 /* Try this one next. */
1044 struct key_user tmp = res[0];
1047 if (sort_deps(filename, op, res+1, num-1,
1048 gives(this_op, data)))
1052 /* No combination worked. */
1056 /* All these ops have the same serial number. Which comes first?
1058 * This can happen both because read ops or failed write ops don't
1059 * change serial number, and also due to race since we access the
1060 * number unlocked (the race can cause less detectable ordering problems,
1061 * in which case we'll deadlock and report: fix manually in that case).
1063 static void figure_deps(char *filename[], struct op *op[],
1064 struct key_user user[], unsigned start, unsigned end)
1067 /* We assume database starts empty. */
1068 const struct TDB_DATA *data = &tdb_null;
1070 /* What do we have to start with? */
1071 for (i = 0; i < start; i++)
1072 data = gives(&op[user[i].file][user[i].op_num], data);
1074 if (!sort_deps(filename, op, user + start, end - start, data))
1075 fail(filename[user[start].file], user[start].op_num+1,
1076 "Could not resolve inter-dependencies");
1079 static void sort_ops(struct keyinfo hash[], char *filename[], struct op *op[])
1083 /* Gcc nexted function extension. How cool is this? */
1084 int compare_serial(const void *_a, const void *_b)
1086 const struct key_user *a = _a, *b = _b;
1087 return op[a->file][a->op_num].serial
1088 - op[b->file][b->op_num].serial;
1091 /* Now sort into seqnum order. */
1092 for (h = 0; h < total_keys * 2; h++) {
1093 unsigned int i, same;
1094 struct key_user *user = hash[h].user;
1096 qsort(user, hash[h].num_users, sizeof(user[0]), compare_serial);
1098 /* Try to deal with same serial numbers. */
1099 for (i = 1, same = 0; i < hash[h].num_users; i++) {
1100 if (op[user[i].file][user[i].op_num].serial
1101 == op[user[i-1].file][user[i-1].op_num].serial) {
1107 figure_deps(filename, op, user, i-same-1, i);
1112 figure_deps(filename, op, user, i-same-1, i);
1116 static int destroy_depend(struct depend *dep)
1118 list_del(&dep->pre_list);
1119 list_del(&dep->post_list);
1123 static void add_dependency(void *ctx,
1126 unsigned int needs_file,
1127 unsigned int needs_opnum,
1128 unsigned int satisfies_file,
1129 unsigned int satisfies_opnum)
1132 unsigned int needs_start, sat_start;
1134 /* We don't depend on ourselves. */
1135 if (needs_file == satisfies_file)
1139 printf("%s:%u: depends on %s:%u\n",
1140 filename[needs_file], needs_opnum+1,
1141 filename[satisfies_file], satisfies_opnum+1);
1144 needs_start = op[needs_file][needs_opnum].group_start;
1145 sat_start = op[satisfies_file][satisfies_opnum].group_start;
1147 /* If needs is in a transaction, we need it before start. */
1149 switch (op[needs_file][needs_start].op) {
1150 case OP_TDB_TRANSACTION_START:
1151 needs_opnum = needs_start;
1153 printf(" -> Back to %u\n", needs_start+1);
1162 /* If satisfies is in a transaction, we wait until after commit. */
1163 /* FIXME: If transaction is cancelled, don't need dependency. */
1165 if (op[satisfies_file][sat_start].op
1166 == OP_TDB_TRANSACTION_START) {
1168 = op[satisfies_file][sat_start].transaction_end;
1170 printf(" -> Depends on %u\n", satisfies_opnum+1);
1176 dep = talloc(ctx, struct depend);
1177 dep->needs_file = needs_file;
1178 dep->needs_opnum = needs_opnum;
1179 dep->satisfies_file = satisfies_file;
1180 dep->satisfies_opnum = satisfies_opnum;
1181 list_add(&op[satisfies_file][satisfies_opnum].post, &dep->post_list);
1182 list_add(&op[needs_file][needs_opnum].pre, &dep->pre_list);
1183 talloc_set_destructor(dep, destroy_depend);
1186 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1187 struct traverse_dep {
1189 unsigned int op_num;
1190 const struct op *op;
1193 /* Sort by which one runs first. */
1194 static int compare_traverse_dep(const void *_a, const void *_b)
1196 const struct traverse_dep *a = _a, *b = _b;
1197 const struct traverse *trava = a->op->trav, *travb = b->op->trav;
1199 if (a->op->serial != b->op->serial)
1200 return a->op->serial - b->op->serial;
1202 /* If they have same serial, it means one didn't make any changes.
1203 * Thus sort by end in that case. */
1204 return a->op[trava->end - a->op_num].serial
1205 - b->op[travb->end - b->op_num].serial;
1208 /* Traversals can deadlock against each other. Force order. */
1209 static void make_traverse_depends(char *filename[],
1210 struct op *op[], unsigned int num_ops[],
1213 unsigned int i, j, num_traversals = 0;
1214 struct traverse_dep *dep;
1216 dep = talloc_array(NULL, struct traverse_dep, 1);
1219 for (i = 0; i < num; i++) {
1220 for (j = 0; j < num_ops[i]; j++) {
1221 if (op[i][j].op == OP_TDB_TRAVERSE_START
1222 || op[i][j].op == OP_TDB_TRAVERSE_READ_START) {
1223 dep = talloc_realloc(NULL, dep,
1224 struct traverse_dep,
1226 dep[num_traversals].file = i;
1227 dep[num_traversals].op_num = j;
1228 dep[num_traversals].op = &op[i][j];
1233 qsort(dep, num_traversals, sizeof(dep[0]), compare_traverse_dep);
1234 for (i = 1; i < num_traversals; i++) {
1235 /* i depends on end of traverse i-1. */
1236 add_dependency(NULL, op, filename, dep[i].file, dep[i].op_num,
1237 dep[i-1].file, dep[i-1].op->trav->end);
1241 #endif /* TRAVERSALS_TAKE_TRANSACTION_LOCK */
1243 static bool changes_db(const struct op *op)
1245 return gives(op, NULL) != NULL;
1248 static void depend_on_previous(struct op *op[],
1251 struct key_user user[],
1261 if (prev == i - 1) {
1262 /* Just depend on previous. */
1263 add_dependency(NULL, op, filename,
1264 user[i].file, user[i].op_num,
1265 user[prev].file, user[prev].op_num);
1269 /* We have to wait for the readers. Find last one in *each* file. */
1270 memset(deps, 0, sizeof(deps));
1271 deps[user[i].file] = true;
1272 for (j = i - 1; j > prev; j--) {
1273 if (!deps[user[j].file]) {
1274 add_dependency(NULL, op, filename,
1275 user[i].file, user[i].op_num,
1276 user[j].file, user[j].op_num);
1277 deps[user[j].file] = true;
1282 /* This is simple, but not complete. We don't take into account
1283 * indirect dependencies. */
1284 static void optimize_dependencies(struct op *op[], unsigned int num_ops[],
1289 for (i = 0; i < num; i++) {
1292 for (j = 0; j < num; j++)
1295 for (j = 1; j < num_ops[i]; j++) {
1296 struct depend *dep, *next;
1298 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1299 if (deps[dep->satisfies_file]
1300 >= (int)dep->satisfies_opnum)
1303 deps[dep->satisfies_file]
1304 = dep->satisfies_opnum;
1310 static void derive_dependencies(char *filename[],
1311 struct op *op[], unsigned int num_ops[],
1314 struct keyinfo *hash;
1317 /* Create hash table for faster key lookup. */
1318 hash = hash_ops(op, num_ops, num);
1320 /* Now handle the hard cases: same serial number. */
1321 sort_ops(hash, filename, op);
1323 /* Create dependencies back to the last change, rather than
1324 * creating false dependencies by naively making each one
1325 * depend on the previous. This has two purposes: it makes
1326 * later optimization simpler, and it also avoids deadlock with
1327 * same sequence number ops inside traversals (if one
1328 * traversal doesn't write anything, two ops can have the same
1329 * sequence number yet we can create a traversal dependency
1330 * the other way). */
1331 for (h = 0; h < total_keys * 2; h++) {
1334 if (hash[h].num_users < 2)
1337 for (i = 0; i < hash[h].num_users; i++) {
1338 if (changes_db(&op[hash[h].user[i].file]
1339 [hash[h].user[i].op_num])) {
1340 depend_on_previous(op, filename, num,
1341 hash[h].user, i, prev);
1343 } else if (prev >= 0)
1344 add_dependency(hash, op, filename,
1345 hash[h].user[i].file,
1346 hash[h].user[i].op_num,
1347 hash[h].user[prev].file,
1348 hash[h].user[prev].op_num);
1352 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1353 make_traverse_depends(filename, op, num_ops, num);
1356 optimize_dependencies(op, num_ops, num);
1359 int main(int argc, char *argv[])
1361 struct timeval start, end;
1362 unsigned int i, num_ops[argc], hashsize[argc], tdb_flags[argc], open_flags[argc];
1363 struct op *op[argc];
1369 errx(1, "Usage: %s <tdbfile> <tracefile>...", argv[0]);
1371 pipes = talloc_array(NULL, struct pipe, argc - 2);
1372 for (i = 0; i < argc - 2; i++) {
1373 printf("Loading tracefile %s...", argv[2+i]);
1375 op[i] = load_tracefile(argv[2+i], &num_ops[i], &hashsize[i],
1376 &tdb_flags[i], &open_flags[i]);
1377 if (pipe(pipes[i].fd) != 0)
1378 err(1, "creating pipe");
1382 printf("Calculating inter-dependencies...");
1384 derive_dependencies(argv+2, op, num_ops, i);
1387 /* Don't fork for single arg case: simple debugging. */
1389 struct tdb_context *tdb;
1390 tdb = tdb_open_ex(argv[1], hashsize[0], tdb_flags[0],
1391 open_flags[0], 0600,
1393 printf("Single threaded run...");
1396 run_ops(tdb, pipes[0].fd[0], argv+2, 0, op[0], 1, num_ops[0]);
1397 check_deps(argv[2], op[0], num_ops[0]);
1404 err(1, "creating pipe");
1406 for (i = 0; i < argc - 2; i++) {
1407 struct tdb_context *tdb;
1411 err(1, "fork failed");
1414 tdb = tdb_open_ex(argv[1], hashsize[i], tdb_flags[i],
1415 open_flags[i], 0600,
1418 err(1, "Opening tdb %s", argv[1]);
1420 /* This catches parent exiting. */
1421 if (read(fds[0], &c, 1) != 1)
1423 run_ops(tdb, pipes[i].fd[0], argv+2, i, op[i], 1,
1425 check_deps(argv[2+i], op[i], num_ops[i]);
1432 /* Let everything settle. */
1435 printf("Starting run...");
1437 gettimeofday(&start, NULL);
1438 /* Tell them all to go! Any write of sufficient length will do. */
1439 if (write(fds[1], hashsize, i) != i)
1440 err(1, "Writing to wakeup pipe");
1442 for (i = 0; i < argc - 2; i++) {
1445 if (!WIFEXITED(status)) {
1446 warnx("Child died with signal %i", WTERMSIG(status));
1448 } else if (WEXITSTATUS(status) != 0)
1449 /* Assume child spat out error. */
1455 gettimeofday(&end, NULL);
1458 end.tv_sec -= start.tv_sec;
1459 printf("Time replaying: %lu usec\n",
1460 end.tv_sec * 1000000UL + (end.tv_usec - start.tv_usec));