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 void move_to_front(struct key_user res[], unsigned int elem)
1031 struct key_user tmp = res[elem];
1032 memmove(res + 1, res, elem*sizeof(res[0]));
1037 static void restore_to_pos(struct key_user res[], unsigned int elem)
1040 struct key_user tmp = res[0];
1041 memmove(res, res + 1, elem*sizeof(res[0]));
1046 static bool sort_deps(char *filename[], struct op *op[],
1047 struct key_user res[], unsigned num,
1048 const TDB_DATA *data, unsigned num_files)
1050 unsigned int i, files_done;
1052 bool done[num_files];
1054 /* Nothing left? We're sorted. */
1058 memset(done, 0, sizeof(done));
1060 /* Since ops within a trace file are ordered, we just need to figure
1061 * out which file to try next. Since we don't take into account
1062 * inter-key relationships (which exist by virtue of trace file order),
1063 * we minimize the chance of harm by trying to keep in serial order. */
1064 for (files_done = 0, i = 0; i < num && files_done < num_files; i++) {
1065 if (done[res[i].file])
1068 this_op = &op[res[i].file][res[i].op_num];
1069 /* Is what we have good enough for this op? */
1070 if (satisfies(data, needs(this_op))) {
1071 move_to_front(res, i);
1072 if (sort_deps(filename, op, res+1, num-1,
1073 gives(this_op, data), num_files))
1075 restore_to_pos(res, i);
1077 done[res[i].file] = true;
1081 /* No combination worked. */
1085 static void check_dep_sorting(struct key_user user[], unsigned num_users,
1090 unsigned minima[num_files];
1092 memset(minima, 0, sizeof(minima));
1093 for (i = 0; i < num_users; i++) {
1094 assert(minima[user[i].file] < user[i].op_num);
1095 minima[user[i].file] = user[i].op_num;
1100 /* All these ops have the same serial number. Which comes first?
1102 * This can happen both because read ops or failed write ops don't
1103 * change serial number, and also due to race since we access the
1104 * number unlocked (the race can cause less detectable ordering problems,
1105 * in which case we'll deadlock and report: fix manually in that case).
1107 static void figure_deps(char *filename[], struct op *op[],
1108 struct key_user user[], unsigned num_users,
1111 /* We assume database starts empty. */
1112 const struct TDB_DATA *data = &tdb_null;
1114 if (!sort_deps(filename, op, user, num_users, data, num_files))
1115 fail(filename[user[0].file], user[0].op_num+1,
1116 "Could not resolve inter-dependencies");
1118 check_dep_sorting(user, num_users, num_files);
1121 static void sort_ops(struct keyinfo hash[], char *filename[], struct op *op[],
1126 /* Gcc nexted function extension. How cool is this? */
1127 int compare_serial(const void *_a, const void *_b)
1129 const struct key_user *a = _a, *b = _b;
1131 /* First, maintain order within any trace file. */
1132 if (a->file == b->file)
1133 return a->op_num - b->op_num;
1135 /* Otherwise, arrange by serial order. */
1136 return op[a->file][a->op_num].serial
1137 - op[b->file][b->op_num].serial;
1140 /* Now sort into serial order. */
1141 for (h = 0; h < total_keys * 2; h++) {
1142 struct key_user *user = hash[h].user;
1144 qsort(user, hash[h].num_users, sizeof(user[0]), compare_serial);
1145 figure_deps(filename, op, user, hash[h].num_users, num);
1149 static int destroy_depend(struct depend *dep)
1151 list_del(&dep->pre_list);
1152 list_del(&dep->post_list);
1156 static void add_dependency(void *ctx,
1159 unsigned int needs_file,
1160 unsigned int needs_opnum,
1161 unsigned int satisfies_file,
1162 unsigned int satisfies_opnum)
1165 unsigned int needs_start, sat_start;
1167 /* We don't depend on ourselves. */
1168 if (needs_file == satisfies_file) {
1169 assert(satisfies_opnum < needs_opnum);
1174 printf("%s:%u: depends on %s:%u\n",
1175 filename[needs_file], needs_opnum+1,
1176 filename[satisfies_file], satisfies_opnum+1);
1179 needs_start = op[needs_file][needs_opnum].group_start;
1180 sat_start = op[satisfies_file][satisfies_opnum].group_start;
1182 /* If needs is in a transaction, we need it before start. */
1184 switch (op[needs_file][needs_start].op) {
1185 case OP_TDB_TRANSACTION_START:
1186 needs_opnum = needs_start;
1188 printf(" -> Back to %u\n", needs_start+1);
1197 /* If satisfies is in a transaction, we wait until after commit. */
1198 /* FIXME: If transaction is cancelled, don't need dependency. */
1200 if (op[satisfies_file][sat_start].op
1201 == OP_TDB_TRANSACTION_START) {
1203 = op[satisfies_file][sat_start].transaction_end;
1205 printf(" -> Depends on %u\n", satisfies_opnum+1);
1211 dep = talloc(ctx, struct depend);
1212 dep->needs_file = needs_file;
1213 dep->needs_opnum = needs_opnum;
1214 dep->satisfies_file = satisfies_file;
1215 dep->satisfies_opnum = satisfies_opnum;
1216 list_add(&op[satisfies_file][satisfies_opnum].post, &dep->post_list);
1217 list_add(&op[needs_file][needs_opnum].pre, &dep->pre_list);
1218 talloc_set_destructor(dep, destroy_depend);
1221 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1222 struct traverse_dep {
1224 unsigned int op_num;
1225 const struct op *op;
1228 /* Sort by which one runs first. */
1229 static int compare_traverse_dep(const void *_a, const void *_b)
1231 const struct traverse_dep *a = _a, *b = _b;
1232 const struct traverse *trava = a->op->trav, *travb = b->op->trav;
1234 if (a->op->serial != b->op->serial)
1235 return a->op->serial - b->op->serial;
1237 /* If they have same serial, it means one didn't make any changes.
1238 * Thus sort by end in that case. */
1239 return a->op[trava->end - a->op_num].serial
1240 - b->op[travb->end - b->op_num].serial;
1243 /* Traversals can deadlock against each other. Force order. */
1244 static void make_traverse_depends(char *filename[],
1245 struct op *op[], unsigned int num_ops[],
1248 unsigned int i, j, num_traversals = 0;
1249 struct traverse_dep *dep;
1251 dep = talloc_array(NULL, struct traverse_dep, 1);
1254 for (i = 0; i < num; i++) {
1255 for (j = 0; j < num_ops[i]; j++) {
1256 if (op[i][j].op == OP_TDB_TRAVERSE_START
1257 || op[i][j].op == OP_TDB_TRAVERSE_READ_START) {
1258 dep = talloc_realloc(NULL, dep,
1259 struct traverse_dep,
1261 dep[num_traversals].file = i;
1262 dep[num_traversals].op_num = j;
1263 dep[num_traversals].op = &op[i][j];
1268 qsort(dep, num_traversals, sizeof(dep[0]), compare_traverse_dep);
1269 for (i = 1; i < num_traversals; i++) {
1270 /* i depends on end of traverse i-1. */
1271 add_dependency(NULL, op, filename, dep[i].file, dep[i].op_num,
1272 dep[i-1].file, dep[i-1].op->trav->end);
1276 #endif /* TRAVERSALS_TAKE_TRANSACTION_LOCK */
1278 static bool changes_db(const struct op *op)
1280 return gives(op, NULL) != NULL;
1283 static void depend_on_previous(struct op *op[],
1286 struct key_user user[],
1296 if (prev == i - 1) {
1297 /* Just depend on previous. */
1298 add_dependency(NULL, op, filename,
1299 user[i].file, user[i].op_num,
1300 user[prev].file, user[prev].op_num);
1304 /* We have to wait for the readers. Find last one in *each* file. */
1305 memset(deps, 0, sizeof(deps));
1306 deps[user[i].file] = true;
1307 for (j = i - 1; j > prev; j--) {
1308 if (!deps[user[j].file]) {
1309 add_dependency(NULL, op, filename,
1310 user[i].file, user[i].op_num,
1311 user[j].file, user[j].op_num);
1312 deps[user[j].file] = true;
1317 /* This is simple, but not complete. We don't take into account
1318 * indirect dependencies. */
1319 static void optimize_dependencies(struct op *op[], unsigned int num_ops[],
1324 /* There can only be one real dependency on each file */
1325 for (i = 0; i < num; i++) {
1326 for (j = 1; j < num_ops[i]; j++) {
1327 struct depend *dep, *next;
1328 struct depend *prev[num];
1330 memset(prev, 0, sizeof(prev));
1332 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1333 if (!prev[dep->satisfies_file]) {
1334 prev[dep->satisfies_file] = dep;
1337 if (prev[dep->satisfies_file]->satisfies_opnum
1338 > dep->satisfies_opnum) {
1339 talloc_free(prev[dep->satisfies_file]);
1340 prev[dep->satisfies_file] = dep;
1347 for (i = 0; i < num; i++) {
1350 for (j = 0; j < num; j++)
1353 for (j = 1; j < num_ops[i]; j++) {
1354 struct depend *dep, *next;
1356 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1357 if (deps[dep->satisfies_file]
1358 >= (int)dep->satisfies_opnum)
1361 deps[dep->satisfies_file]
1362 = dep->satisfies_opnum;
1368 static void derive_dependencies(char *filename[],
1369 struct op *op[], unsigned int num_ops[],
1372 struct keyinfo *hash;
1375 /* Create hash table for faster key lookup. */
1376 hash = hash_ops(op, num_ops, num);
1378 /* Sort them by serial number. */
1379 sort_ops(hash, filename, op, num);
1381 /* Create dependencies back to the last change, rather than
1382 * creating false dependencies by naively making each one
1383 * depend on the previous. This has two purposes: it makes
1384 * later optimization simpler, and it also avoids deadlock with
1385 * same sequence number ops inside traversals (if one
1386 * traversal doesn't write anything, two ops can have the same
1387 * sequence number yet we can create a traversal dependency
1388 * the other way). */
1389 for (h = 0; h < total_keys * 2; h++) {
1392 if (hash[h].num_users < 2)
1395 for (i = 0; i < hash[h].num_users; i++) {
1396 if (changes_db(&op[hash[h].user[i].file]
1397 [hash[h].user[i].op_num])) {
1398 depend_on_previous(op, filename, num,
1399 hash[h].user, i, prev);
1401 } else if (prev >= 0)
1402 add_dependency(hash, op, filename,
1403 hash[h].user[i].file,
1404 hash[h].user[i].op_num,
1405 hash[h].user[prev].file,
1406 hash[h].user[prev].op_num);
1410 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1411 make_traverse_depends(filename, op, num_ops, num);
1414 optimize_dependencies(op, num_ops, num);
1417 int main(int argc, char *argv[])
1419 struct timeval start, end;
1420 unsigned int i, num_ops[argc], hashsize[argc], tdb_flags[argc], open_flags[argc];
1421 struct op *op[argc];
1427 errx(1, "Usage: %s <tdbfile> <tracefile>...", argv[0]);
1429 pipes = talloc_array(NULL, struct pipe, argc - 2);
1430 for (i = 0; i < argc - 2; i++) {
1431 printf("Loading tracefile %s...", argv[2+i]);
1433 op[i] = load_tracefile(argv[2+i], &num_ops[i], &hashsize[i],
1434 &tdb_flags[i], &open_flags[i]);
1435 if (pipe(pipes[i].fd) != 0)
1436 err(1, "creating pipe");
1440 printf("Calculating inter-dependencies...");
1442 derive_dependencies(argv+2, op, num_ops, i);
1445 /* Don't fork for single arg case: simple debugging. */
1447 struct tdb_context *tdb;
1448 tdb = tdb_open_ex(argv[1], hashsize[0], tdb_flags[0],
1449 open_flags[0], 0600,
1451 printf("Single threaded run...");
1454 run_ops(tdb, pipes[0].fd[0], argv+2, 0, op[0], 1, num_ops[0]);
1455 check_deps(argv[2], op[0], num_ops[0]);
1462 err(1, "creating pipe");
1464 for (i = 0; i < argc - 2; i++) {
1465 struct tdb_context *tdb;
1469 err(1, "fork failed");
1472 tdb = tdb_open_ex(argv[1], hashsize[i], tdb_flags[i],
1473 open_flags[i], 0600,
1476 err(1, "Opening tdb %s", argv[1]);
1478 /* This catches parent exiting. */
1479 if (read(fds[0], &c, 1) != 1)
1481 run_ops(tdb, pipes[i].fd[0], argv+2, i, op[i], 1,
1483 check_deps(argv[2+i], op[i], num_ops[i]);
1490 /* Let everything settle. */
1493 printf("Starting run...");
1495 gettimeofday(&start, NULL);
1496 /* Tell them all to go! Any write of sufficient length will do. */
1497 if (write(fds[1], hashsize, i) != i)
1498 err(1, "Writing to wakeup pipe");
1500 for (i = 0; i < argc - 2; i++) {
1503 if (!WIFEXITED(status)) {
1504 warnx("Child died with signal %i", WTERMSIG(status));
1506 } else if (WEXITSTATUS(status) != 0)
1507 /* Assume child spat out error. */
1513 gettimeofday(&end, NULL);
1516 end.tv_sec -= start.tv_sec;
1517 printf("Time replaying: %lu usec\n",
1518 end.tv_sec * 1000000UL + (end.tv_usec - start.tv_usec));