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_opnum+1, filename[dep->satisfies_file],
506 dep->satisfies_opnum+1);
509 /* This could be any op, not just this one. */
514 static void do_post(char *filename[], unsigned int file,
515 const struct op op[], unsigned int i)
519 list_for_each(&op[i].post, dep, post_list) {
521 printf("%s:%u:sending to file %s:%u\n", filename[file], i+1,
522 filename[dep->needs_file], dep->needs_opnum+1);
524 if (write(pipes[dep->needs_file].fd[1], &dep, sizeof(dep))
526 err(1, "%s:%u failed to tell file %s",
527 filename[file], i+1, filename[dep->needs_file]);
531 static int get_len(TDB_DATA key, TDB_DATA data, void *private_data)
536 static unsigned run_ops(struct tdb_context *tdb,
541 unsigned int start, unsigned int stop);
543 struct traverse_info {
552 /* Trivial case: do whatever they did for this key. */
553 static int trivial_traverse(struct tdb_context *tdb,
554 TDB_DATA key, TDB_DATA data,
557 struct traverse_info *tinfo = _tinfo;
558 struct traverse *trav = tinfo->op[tinfo->start].trav;
559 unsigned int h = hash_key(&key) % (trav->num * 2);
561 while (trav->hash[h].index) {
562 if (key_eq(trav->hash[h].key, key)) {
563 run_ops(tdb, tinfo->pre_fd, tinfo->filename,
564 tinfo->file, tinfo->op, trav->hash[h].index,
569 h = (h + 1) % (trav->num * 2);
571 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
572 "unexpected traverse key");
575 /* More complex. Just do whatever's they did at the n'th entry. */
576 static int nontrivial_traverse(struct tdb_context *tdb,
577 TDB_DATA key, TDB_DATA data,
580 struct traverse_info *tinfo = _tinfo;
581 struct traverse *trav = tinfo->op[tinfo->start].trav;
583 if (tinfo->i == trav->end) {
584 /* This can happen if traverse expects to be empty. */
585 if (tinfo->start + 1 == trav->end)
587 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
588 "traverse did not terminate");
591 if (tinfo->op[tinfo->i].op != OP_TDB_TRAVERSE)
592 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
593 "%s:%u:traverse terminated early");
595 /* Run any normal ops. */
596 tinfo->i = run_ops(tdb, tinfo->pre_fd, tinfo->filename, tinfo->file,
597 tinfo->op, tinfo->i+1, trav->end);
599 if (tinfo->i == trav->end)
605 static unsigned op_traverse(struct tdb_context *tdb,
609 int (*traversefn)(struct tdb_context *,
610 tdb_traverse_func, void *),
614 struct traverse *trav = op[start].trav;
615 struct traverse_info tinfo = { op, filename, file, pre_fd,
620 int ret = traversefn(tdb, trivial_traverse, &tinfo);
621 if (ret != trav->num)
622 fail(filename[file], start+1,
623 "short traversal %i", ret);
627 traversefn(tdb, nontrivial_traverse, &tinfo);
629 /* Traversing in wrong order can have strange effects: eg. if
630 * original traverse went A (delete A), B, we might do B
631 * (delete A). So if we have ops left over, we do it now. */
632 while (tinfo.i != trav->end) {
633 if (op[tinfo.i].op == OP_TDB_TRAVERSE)
636 tinfo.i = run_ops(tdb, pre_fd, filename, file, op,
643 static void break_out(int sig)
647 static __attribute__((noinline))
648 unsigned run_ops(struct tdb_context *tdb,
652 struct op op[], unsigned int start, unsigned int stop)
657 sa.sa_handler = break_out;
660 sigaction(SIGALRM, &sa, NULL);
661 for (i = start; i < stop; i++) {
662 do_pre(filename, file, pre_fd, op, i);
666 try(tdb_lockall(tdb), op[i].ret);
668 case OP_TDB_LOCKALL_MARK:
669 try(tdb_lockall_mark(tdb), op[i].ret);
671 case OP_TDB_LOCKALL_UNMARK:
672 try(tdb_lockall_unmark(tdb), op[i].ret);
674 case OP_TDB_LOCKALL_NONBLOCK:
675 unreliable(tdb_lockall_nonblock(tdb), op[i].ret,
676 tdb_lockall(tdb), tdb_unlockall(tdb));
678 case OP_TDB_UNLOCKALL:
679 try(tdb_unlockall(tdb), op[i].ret);
681 case OP_TDB_LOCKALL_READ:
682 try(tdb_lockall_read(tdb), op[i].ret);
684 case OP_TDB_LOCKALL_READ_NONBLOCK:
685 unreliable(tdb_lockall_read_nonblock(tdb), op[i].ret,
686 tdb_lockall_read(tdb),
687 tdb_unlockall_read(tdb));
689 case OP_TDB_UNLOCKALL_READ:
690 try(tdb_unlockall_read(tdb), op[i].ret);
692 case OP_TDB_CHAINLOCK:
693 try(tdb_chainlock(tdb, op[i].key), op[i].ret);
695 case OP_TDB_CHAINLOCK_NONBLOCK:
696 unreliable(tdb_chainlock_nonblock(tdb, op[i].key),
698 tdb_chainlock(tdb, op[i].key),
699 tdb_chainunlock(tdb, op[i].key));
701 case OP_TDB_CHAINLOCK_MARK:
702 try(tdb_chainlock_mark(tdb, op[i].key), op[i].ret);
704 case OP_TDB_CHAINLOCK_UNMARK:
705 try(tdb_chainlock_unmark(tdb, op[i].key), op[i].ret);
707 case OP_TDB_CHAINUNLOCK:
708 try(tdb_chainunlock(tdb, op[i].key), op[i].ret);
710 case OP_TDB_CHAINLOCK_READ:
711 try(tdb_chainlock_read(tdb, op[i].key), op[i].ret);
713 case OP_TDB_CHAINUNLOCK_READ:
714 try(tdb_chainunlock_read(tdb, op[i].key), op[i].ret);
716 case OP_TDB_PARSE_RECORD:
717 try(tdb_parse_record(tdb, op[i].key, get_len, NULL),
721 try(tdb_exists(tdb, op[i].key), op[i].ret);
724 try(tdb_store(tdb, op[i].key, op[i].data, op[i].flag),
728 try(tdb_append(tdb, op[i].key, op[i].data), op[i].ret);
730 case OP_TDB_GET_SEQNUM:
731 try(tdb_get_seqnum(tdb), op[i].ret);
733 case OP_TDB_WIPE_ALL:
734 try(tdb_wipe_all(tdb), op[i].ret);
736 case OP_TDB_TRANSACTION_START:
737 try(tdb_transaction_start(tdb), op[i].ret);
739 case OP_TDB_TRANSACTION_CANCEL:
740 try(tdb_transaction_cancel(tdb), op[i].ret);
742 case OP_TDB_TRANSACTION_COMMIT:
743 try(tdb_transaction_commit(tdb), op[i].ret);
745 case OP_TDB_TRAVERSE_READ_START:
746 i = op_traverse(tdb, pre_fd, filename, file,
747 tdb_traverse_read, op, i);
749 case OP_TDB_TRAVERSE_START:
750 i = op_traverse(tdb, pre_fd, filename, file,
751 tdb_traverse, op, i);
753 case OP_TDB_TRAVERSE:
754 /* Terminate: we're in a traverse, and we've
757 case OP_TDB_TRAVERSE_END:
758 fail(filename[file], i+1, "unepxected end traverse");
759 /* FIXME: These must be treated like traverse. */
760 case OP_TDB_FIRSTKEY:
761 if (!key_eq(tdb_firstkey(tdb), op[i].data))
762 fail(filename[file], i+1, "bad firstkey");
765 if (!key_eq(tdb_nextkey(tdb, op[i].key), op[i].data))
766 fail(filename[file], i+1, "bad nextkey");
769 TDB_DATA f = tdb_fetch(tdb, op[i].key);
770 if (!key_eq(f, op[i].data))
771 fail(filename[file], i+1, "bad fetch %u",
776 try(tdb_delete(tdb, op[i].key), op[i].ret);
779 do_post(filename, file, op, i);
784 static struct op *load_tracefile(const char *filename, unsigned int *num,
785 unsigned int *hashsize,
786 unsigned int *tdb_flags,
787 unsigned int *open_flags)
790 struct op *op = talloc_array(NULL, struct op, 1);
795 file = grab_file(NULL, filename, NULL);
797 err(1, "Reading %s", filename);
799 lines = strsplit(file, file, "\n", NULL);
801 errx(1, "%s is empty", filename);
803 words = strsplit(lines, lines[0], " ", NULL);
804 if (!streq(words[1], "tdb_open"))
805 fail(filename, 1, "does not start with tdb_open");
807 *hashsize = atoi(words[2]);
808 *tdb_flags = strtoul(words[3], NULL, 0);
809 *open_flags = strtoul(words[4], NULL, 0);
811 for (i = 1; lines[i]; i++) {
812 const struct op_table *opt;
814 words = strsplit(lines, lines[i], " ", NULL);
815 if (!words[0] || !words[1])
816 fail(filename, i+1, "Expected serial number and op");
818 opt = find_keyword(words[1], strlen(words[1]));
820 if (streq(words[1], "tdb_close")) {
823 "lines after tdb_close");
828 fail(filename, i+1, "Unknown operation '%s'", words[1]);
831 add_op(filename, &op, i, atoi(words[0]), opt->type);
832 opt->enhance_op(filename, op, i, words);
835 fprintf(stderr, "%s:%u:last operation is not tdb_close: incomplete?",
842 /* We remember all the keys we've ever seen, and who has them. */
850 unsigned int num_users;
851 struct key_user *user;
854 static const TDB_DATA must_not_exist;
855 static const TDB_DATA must_exist;
856 static const TDB_DATA not_exists_or_empty;
858 /* NULL means doesn't care if it exists or not, &must_exist means
859 * it must exist but we don't care what, &must_not_exist means it must
860 * not exist, otherwise the data it needs. */
861 static const TDB_DATA *needs(const struct op *op)
864 /* FIXME: Pull forward deps, since we can deadlock */
865 case OP_TDB_CHAINLOCK:
866 case OP_TDB_CHAINLOCK_NONBLOCK:
867 case OP_TDB_CHAINLOCK_MARK:
868 case OP_TDB_CHAINLOCK_UNMARK:
869 case OP_TDB_CHAINUNLOCK:
870 case OP_TDB_CHAINLOCK_READ:
871 case OP_TDB_CHAINUNLOCK_READ:
875 if (op->append.pre.dsize == 0)
876 return ¬_exists_or_empty;
877 return &op->append.pre;
880 if (op->flag == TDB_INSERT) {
884 return &must_not_exist;
885 } else if (op->flag == TDB_MODIFY) {
887 return &must_not_exist;
891 /* No flags? Don't care */
898 return &must_not_exist;
900 case OP_TDB_PARSE_RECORD:
902 return &must_not_exist;
905 /* FIXME: handle these. */
906 case OP_TDB_WIPE_ALL:
907 case OP_TDB_FIRSTKEY:
909 case OP_TDB_GET_SEQNUM:
910 case OP_TDB_TRAVERSE:
911 case OP_TDB_TRANSACTION_COMMIT:
912 case OP_TDB_TRANSACTION_CANCEL:
913 case OP_TDB_TRANSACTION_START:
918 return &must_not_exist;
923 return &must_not_exist;
927 errx(1, "Unexpected op %i", op->op);
932 /* What's the data after this op? pre if nothing changed. */
933 static const TDB_DATA *gives(const struct op *op, const TDB_DATA *pre)
935 /* Failed ops don't change state of db. */
939 if (op->op == OP_TDB_DELETE || op->op == OP_TDB_WIPE_ALL)
942 if (op->op == OP_TDB_APPEND)
943 return &op->append.post;
945 if (op->op == OP_TDB_STORE)
951 static struct keyinfo *hash_ops(struct op *op[], unsigned int num_ops[],
954 unsigned int i, j, h;
955 struct keyinfo *hash;
957 hash = talloc_zero_array(op[0], struct keyinfo, total_keys*2);
958 for (i = 0; i < num; i++) {
959 for (j = 1; j < num_ops[i]; j++) {
960 /* We can't do this on allocation, due to realloc. */
961 list_head_init(&op[i][j].post);
962 list_head_init(&op[i][j].pre);
964 if (!op[i][j].key.dptr)
967 /* We don't wait for traverse keys */
968 /* FIXME: We should, for trivial traversals. */
969 if (op[i][j].op == OP_TDB_TRAVERSE)
972 h = hash_key(&op[i][j].key) % (total_keys * 2);
973 while (!key_eq(hash[h].key, op[i][j].key)) {
974 if (!hash[h].key.dptr) {
975 hash[h].key = op[i][j].key;
978 h = (h + 1) % (total_keys * 2);
980 /* Might as well save some memory if we can. */
981 if (op[i][j].key.dptr != hash[h].key.dptr) {
982 talloc_free(op[i][j].key.dptr);
983 op[i][j].key.dptr = hash[h].key.dptr;
985 hash[h].user = talloc_realloc(hash, hash[h].user,
987 hash[h].num_users+1);
988 hash[h].user[hash[h].num_users].op_num = j;
989 hash[h].user[hash[h].num_users].file = i;
997 static bool satisfies(const TDB_DATA *data, const TDB_DATA *need)
999 /* Don't need anything? Cool. */
1003 /* This should be tdb_null or a real value. */
1004 assert(data != &must_exist);
1005 assert(data != &must_not_exist);
1006 assert(data != ¬_exists_or_empty);
1008 /* must_not_exist == must_not_exist, must_exist == must_exist, or
1009 not_exists_or_empty == not_exists_or_empty. */
1010 if (data->dsize == need->dsize && data->dptr == need->dptr)
1013 /* Must not exist? data must not exist. */
1014 if (need == &must_not_exist)
1015 return data->dptr == NULL;
1018 if (need == &must_exist)
1019 return data->dptr != NULL;
1021 /* Either noexist or empty. */
1022 if (need == ¬_exists_or_empty)
1023 return data->dsize == 0;
1025 /* Needs something specific. */
1026 return key_eq(*data, *need);
1029 static void move_to_front(struct key_user res[], unsigned int elem)
1032 struct key_user tmp = res[elem];
1033 memmove(res + 1, res, elem*sizeof(res[0]));
1038 static void restore_to_pos(struct key_user res[], unsigned int elem)
1041 struct key_user tmp = res[0];
1042 memmove(res, res + 1, elem*sizeof(res[0]));
1047 static bool sort_deps(char *filename[], struct op *op[],
1048 struct key_user res[], unsigned num,
1049 const TDB_DATA *data, unsigned num_files)
1051 unsigned int i, files_done;
1053 bool done[num_files];
1055 /* Nothing left? We're sorted. */
1059 memset(done, 0, sizeof(done));
1061 /* Since ops within a trace file are ordered, we just need to figure
1062 * out which file to try next. Since we don't take into account
1063 * inter-key relationships (which exist by virtue of trace file order),
1064 * we minimize the chance of harm by trying to keep in serial order. */
1065 for (files_done = 0, i = 0; i < num && files_done < num_files; i++) {
1066 if (done[res[i].file])
1069 this_op = &op[res[i].file][res[i].op_num];
1070 /* Is what we have good enough for this op? */
1071 if (satisfies(data, needs(this_op))) {
1072 move_to_front(res, i);
1073 if (sort_deps(filename, op, res+1, num-1,
1074 gives(this_op, data), num_files))
1076 restore_to_pos(res, i);
1078 done[res[i].file] = true;
1082 /* No combination worked. */
1086 static void check_dep_sorting(struct key_user user[], unsigned num_users,
1091 unsigned minima[num_files];
1093 memset(minima, 0, sizeof(minima));
1094 for (i = 0; i < num_users; i++) {
1095 assert(minima[user[i].file] < user[i].op_num);
1096 minima[user[i].file] = user[i].op_num;
1101 /* All these ops have the same serial number. Which comes first?
1103 * This can happen both because read ops or failed write ops don't
1104 * change serial number, and also due to race since we access the
1105 * number unlocked (the race can cause less detectable ordering problems,
1106 * in which case we'll deadlock and report: fix manually in that case).
1108 static void figure_deps(char *filename[], struct op *op[],
1109 struct key_user user[], unsigned num_users,
1112 /* We assume database starts empty. */
1113 const struct TDB_DATA *data = &tdb_null;
1115 if (!sort_deps(filename, op, user, num_users, data, num_files))
1116 fail(filename[user[0].file], user[0].op_num+1,
1117 "Could not resolve inter-dependencies");
1119 check_dep_sorting(user, num_users, num_files);
1122 static void sort_ops(struct keyinfo hash[], char *filename[], struct op *op[],
1127 /* Gcc nexted function extension. How cool is this? */
1128 int compare_serial(const void *_a, const void *_b)
1130 const struct key_user *a = _a, *b = _b;
1132 /* First, maintain order within any trace file. */
1133 if (a->file == b->file)
1134 return a->op_num - b->op_num;
1136 /* Otherwise, arrange by serial order. */
1137 return op[a->file][a->op_num].serial
1138 - op[b->file][b->op_num].serial;
1141 /* Now sort into serial order. */
1142 for (h = 0; h < total_keys * 2; h++) {
1143 struct key_user *user = hash[h].user;
1145 qsort(user, hash[h].num_users, sizeof(user[0]), compare_serial);
1146 figure_deps(filename, op, user, hash[h].num_users, num);
1150 static int destroy_depend(struct depend *dep)
1152 list_del(&dep->pre_list);
1153 list_del(&dep->post_list);
1157 static void add_dependency(void *ctx,
1160 unsigned int needs_file,
1161 unsigned int needs_opnum,
1162 unsigned int satisfies_file,
1163 unsigned int satisfies_opnum)
1166 unsigned int needs_start, sat_start;
1168 /* We don't depend on ourselves. */
1169 if (needs_file == satisfies_file) {
1170 assert(satisfies_opnum < needs_opnum);
1175 printf("%s:%u: depends on %s:%u\n",
1176 filename[needs_file], needs_opnum+1,
1177 filename[satisfies_file], satisfies_opnum+1);
1180 needs_start = op[needs_file][needs_opnum].group_start;
1181 sat_start = op[satisfies_file][satisfies_opnum].group_start;
1183 /* If needs is in a transaction, we need it before start. */
1185 switch (op[needs_file][needs_start].op) {
1186 case OP_TDB_TRANSACTION_START:
1187 needs_opnum = needs_start;
1189 printf(" -> Back to %u\n", needs_start+1);
1198 /* If satisfies is in a transaction, we wait until after commit. */
1199 /* FIXME: If transaction is cancelled, don't need dependency. */
1201 if (op[satisfies_file][sat_start].op
1202 == OP_TDB_TRANSACTION_START) {
1204 = op[satisfies_file][sat_start].transaction_end;
1206 printf(" -> Depends on %u\n", satisfies_opnum+1);
1212 dep = talloc(ctx, struct depend);
1213 dep->needs_file = needs_file;
1214 dep->needs_opnum = needs_opnum;
1215 dep->satisfies_file = satisfies_file;
1216 dep->satisfies_opnum = satisfies_opnum;
1217 list_add(&op[satisfies_file][satisfies_opnum].post, &dep->post_list);
1218 list_add(&op[needs_file][needs_opnum].pre, &dep->pre_list);
1219 talloc_set_destructor(dep, destroy_depend);
1222 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1223 struct traverse_dep {
1225 unsigned int op_num;
1226 const struct op *op;
1229 /* Sort by which one runs first. */
1230 static int compare_traverse_dep(const void *_a, const void *_b)
1232 const struct traverse_dep *a = _a, *b = _b;
1233 const struct traverse *trava = a->op->trav, *travb = b->op->trav;
1235 if (a->op->serial != b->op->serial)
1236 return a->op->serial - b->op->serial;
1238 /* If they have same serial, it means one didn't make any changes.
1239 * Thus sort by end in that case. */
1240 return a->op[trava->end - a->op_num].serial
1241 - b->op[travb->end - b->op_num].serial;
1244 /* Traversals can deadlock against each other. Force order. */
1245 static void make_traverse_depends(char *filename[],
1246 struct op *op[], unsigned int num_ops[],
1249 unsigned int i, j, num_traversals = 0;
1250 struct traverse_dep *dep;
1252 dep = talloc_array(NULL, struct traverse_dep, 1);
1255 for (i = 0; i < num; i++) {
1256 for (j = 0; j < num_ops[i]; j++) {
1257 if (op[i][j].op == OP_TDB_TRAVERSE_START
1258 || op[i][j].op == OP_TDB_TRAVERSE_READ_START) {
1259 dep = talloc_realloc(NULL, dep,
1260 struct traverse_dep,
1262 dep[num_traversals].file = i;
1263 dep[num_traversals].op_num = j;
1264 dep[num_traversals].op = &op[i][j];
1269 qsort(dep, num_traversals, sizeof(dep[0]), compare_traverse_dep);
1270 for (i = 1; i < num_traversals; i++) {
1271 /* i depends on end of traverse i-1. */
1272 add_dependency(NULL, op, filename, dep[i].file, dep[i].op_num,
1273 dep[i-1].file, dep[i-1].op->trav->end);
1277 #endif /* TRAVERSALS_TAKE_TRANSACTION_LOCK */
1279 static bool changes_db(const struct op *op)
1281 return gives(op, NULL) != NULL;
1284 static void depend_on_previous(struct op *op[],
1287 struct key_user user[],
1297 if (prev == i - 1) {
1298 /* Just depend on previous. */
1299 add_dependency(NULL, op, filename,
1300 user[i].file, user[i].op_num,
1301 user[prev].file, user[prev].op_num);
1305 /* We have to wait for the readers. Find last one in *each* file. */
1306 memset(deps, 0, sizeof(deps));
1307 deps[user[i].file] = true;
1308 for (j = i - 1; j > prev; j--) {
1309 if (!deps[user[j].file]) {
1310 add_dependency(NULL, op, filename,
1311 user[i].file, user[i].op_num,
1312 user[j].file, user[j].op_num);
1313 deps[user[j].file] = true;
1318 /* This is simple, but not complete. We don't take into account
1319 * indirect dependencies. */
1320 static void optimize_dependencies(struct op *op[], unsigned int num_ops[],
1325 /* There can only be one real dependency on each file */
1326 for (i = 0; i < num; i++) {
1327 for (j = 1; j < num_ops[i]; j++) {
1328 struct depend *dep, *next;
1329 struct depend *prev[num];
1331 memset(prev, 0, sizeof(prev));
1333 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1334 if (!prev[dep->satisfies_file]) {
1335 prev[dep->satisfies_file] = dep;
1338 if (prev[dep->satisfies_file]->satisfies_opnum
1339 < dep->satisfies_opnum) {
1340 talloc_free(prev[dep->satisfies_file]);
1341 prev[dep->satisfies_file] = dep;
1348 for (i = 0; i < num; i++) {
1351 for (j = 0; j < num; j++)
1354 for (j = 1; j < num_ops[i]; j++) {
1355 struct depend *dep, *next;
1357 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1358 if (deps[dep->satisfies_file]
1359 >= (int)dep->satisfies_opnum)
1362 deps[dep->satisfies_file]
1363 = dep->satisfies_opnum;
1369 static void derive_dependencies(char *filename[],
1370 struct op *op[], unsigned int num_ops[],
1373 struct keyinfo *hash;
1376 /* Create hash table for faster key lookup. */
1377 hash = hash_ops(op, num_ops, num);
1379 /* Sort them by serial number. */
1380 sort_ops(hash, filename, op, num);
1382 /* Create dependencies back to the last change, rather than
1383 * creating false dependencies by naively making each one
1384 * depend on the previous. This has two purposes: it makes
1385 * later optimization simpler, and it also avoids deadlock with
1386 * same sequence number ops inside traversals (if one
1387 * traversal doesn't write anything, two ops can have the same
1388 * sequence number yet we can create a traversal dependency
1389 * the other way). */
1390 for (h = 0; h < total_keys * 2; h++) {
1393 if (hash[h].num_users < 2)
1396 for (i = 0; i < hash[h].num_users; i++) {
1397 if (changes_db(&op[hash[h].user[i].file]
1398 [hash[h].user[i].op_num])) {
1399 depend_on_previous(op, filename, num,
1400 hash[h].user, i, prev);
1402 } else if (prev >= 0)
1403 add_dependency(hash, op, filename,
1404 hash[h].user[i].file,
1405 hash[h].user[i].op_num,
1406 hash[h].user[prev].file,
1407 hash[h].user[prev].op_num);
1411 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1412 make_traverse_depends(filename, op, num_ops, num);
1415 optimize_dependencies(op, num_ops, num);
1418 int main(int argc, char *argv[])
1420 struct timeval start, end;
1421 unsigned int i, num_ops[argc], hashsize[argc], tdb_flags[argc], open_flags[argc];
1422 struct op *op[argc];
1428 errx(1, "Usage: %s <tdbfile> <tracefile>...", argv[0]);
1430 pipes = talloc_array(NULL, struct pipe, argc - 2);
1431 for (i = 0; i < argc - 2; i++) {
1432 printf("Loading tracefile %s...", argv[2+i]);
1434 op[i] = load_tracefile(argv[2+i], &num_ops[i], &hashsize[i],
1435 &tdb_flags[i], &open_flags[i]);
1436 if (pipe(pipes[i].fd) != 0)
1437 err(1, "creating pipe");
1441 printf("Calculating inter-dependencies...");
1443 derive_dependencies(argv+2, op, num_ops, i);
1446 /* Don't fork for single arg case: simple debugging. */
1448 struct tdb_context *tdb;
1449 tdb = tdb_open_ex(argv[1], hashsize[0], tdb_flags[0],
1450 open_flags[0], 0600,
1452 printf("Single threaded run...");
1455 run_ops(tdb, pipes[0].fd[0], argv+2, 0, op[0], 1, num_ops[0]);
1456 check_deps(argv[2], op[0], num_ops[0]);
1463 err(1, "creating pipe");
1465 for (i = 0; i < argc - 2; i++) {
1466 struct tdb_context *tdb;
1470 err(1, "fork failed");
1473 tdb = tdb_open_ex(argv[1], hashsize[i], tdb_flags[i],
1474 open_flags[i], 0600,
1477 err(1, "Opening tdb %s", argv[1]);
1479 /* This catches parent exiting. */
1480 if (read(fds[0], &c, 1) != 1)
1482 run_ops(tdb, pipes[i].fd[0], argv+2, i, op[i], 1,
1484 check_deps(argv[2+i], op[i], num_ops[i]);
1491 /* Let everything settle. */
1494 printf("Starting run...");
1496 gettimeofday(&start, NULL);
1497 /* Tell them all to go! Any write of sufficient length will do. */
1498 if (write(fds[1], hashsize, i) != i)
1499 err(1, "Writing to wakeup pipe");
1501 for (i = 0; i < argc - 2; i++) {
1504 if (!WIFEXITED(status)) {
1505 warnx("Child died with signal %i", WTERMSIG(status));
1507 } else if (WEXITSTATUS(status) != 0)
1508 /* Assume child spat out error. */
1514 gettimeofday(&end, NULL);
1517 end.tv_sec -= start.tv_sec;
1518 printf("Time replaying: %lu usec\n",
1519 end.tv_sec * 1000000UL + (end.tv_usec - start.tv_usec));