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 { /* append */
150 unsigned int group_len; /* transaction/traverse start */
154 static unsigned char hex_char(const char *filename, unsigned int line, char c)
157 if (c >= 'A' && c <= 'F')
159 if (c >= '0' && c <= '9')
161 fail(filename, line, "invalid hex character '%c'", c);
164 /* TDB data is <size>:<%02x>* */
165 static TDB_DATA make_tdb_data(const void *ctx,
166 const char *filename, unsigned int line,
173 if (streq(word, "NULL"))
176 data.dsize = atoi(word);
177 data.dptr = talloc_array(ctx, unsigned char, data.dsize);
178 p = strchr(word, ':');
180 fail(filename, line, "invalid tdb data '%s'", word);
182 for (i = 0; i < data.dsize; i++)
183 data.dptr[i] = hex_char(filename, line, p[i*2])*16
184 + hex_char(filename, line, p[i*2+1]);
189 static void add_op(const char *filename, struct op **op, unsigned int i,
190 unsigned int serial, enum op_type type)
193 *op = talloc_realloc(NULL, *op, struct op, i+1);
196 new->serial = serial;
198 new->group_start = 0;
201 static void op_add_nothing(const char *filename,
202 struct op op[], unsigned int op_num, char *words[])
205 fail(filename, op_num+1, "Expected no arguments");
206 op[op_num].key = tdb_null;
209 static void op_add_key(const char *filename,
210 struct op op[], unsigned int op_num, char *words[])
212 if (words[2] == NULL || words[3])
213 fail(filename, op_num+1, "Expected just a key");
215 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
216 if (op[op_num].op != OP_TDB_TRAVERSE)
220 static void op_add_key_ret(const char *filename,
221 struct op op[], unsigned int op_num, char *words[])
223 if (!words[2] || !words[3] || !words[4] || words[5]
224 || !streq(words[3], "="))
225 fail(filename, op_num+1, "Expected <key> = <ret>");
226 op[op_num].ret = atoi(words[4]);
227 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
228 /* May only be a unique key if it fails */
229 if (op[op_num].ret != 0)
233 static void op_add_key_data(const char *filename,
234 struct op op[], unsigned int op_num, char *words[])
236 if (!words[2] || !words[3] || !words[4] || words[5]
237 || !streq(words[3], "="))
238 fail(filename, op_num+1, "Expected <key> = <data>");
239 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
240 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[4]);
241 /* May only be a unique key if it fails */
242 if (!op[op_num].data.dptr)
246 /* <serial> tdb_store <rec> <rec> <flag> = <ret> */
247 static void op_add_store(const char *filename,
248 struct op op[], unsigned int op_num, char *words[])
250 if (!words[2] || !words[3] || !words[4] || !words[5] || !words[6]
251 || words[7] || !streq(words[5], "="))
252 fail(filename, op_num+1, "Expect <key> <data> <flag> = <ret>");
254 op[op_num].flag = strtoul(words[4], NULL, 0);
255 op[op_num].ret = atoi(words[6]);
256 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
257 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
261 /* <serial> tdb_append <rec> <rec> = <rec> */
262 static void op_add_append(const char *filename,
263 struct op op[], unsigned int op_num, char *words[])
265 if (!words[2] || !words[3] || !words[4] || !words[5] || words[6]
266 || !streq(words[4], "="))
267 fail(filename, op_num+1, "Expect <key> <data> = <rec>");
269 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
270 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
272 op[op_num].append.post
273 = make_tdb_data(op, filename, op_num+1, words[5]);
275 /* By subtraction, figure out what previous data was. */
276 op[op_num].append.pre.dptr = op[op_num].append.post.dptr;
277 op[op_num].append.pre.dsize
278 = op[op_num].append.post.dsize - op[op_num].data.dsize;
282 /* <serial> tdb_get_seqnum = <ret> */
283 static void op_add_seqnum(const char *filename,
284 struct op op[], unsigned int op_num, char *words[])
286 if (!words[2] || !words[3] || words[4] || !streq(words[2], "="))
287 fail(filename, op_num+1, "Expect = <ret>");
289 op[op_num].key = tdb_null;
290 op[op_num].ret = atoi(words[3]);
293 static void op_add_traverse(const char *filename,
294 struct op op[], unsigned int op_num, char *words[])
297 fail(filename, op_num+1, "Expect no arguments");
299 op[op_num].key = tdb_null;
300 op[op_num].group_len = 0;
303 static void op_add_transaction(const char *filename, struct op op[],
304 unsigned int op_num, char *words[])
307 fail(filename, op_num+1, "Expect no arguments");
309 op[op_num].key = tdb_null;
310 op[op_num].group_len = 0;
313 static void op_analyze_transaction(const char *filename,
314 struct op op[], unsigned int op_num,
319 op[op_num].key = tdb_null;
322 fail(filename, op_num+1, "Expect no arguments");
324 for (i = op_num-1; i >= 0; i--) {
325 if (op[i].op == OP_TDB_TRANSACTION_START && !op[i].group_len)
330 fail(filename, op_num+1, "no transaction start found");
333 op[start].group_len = op_num - i;
335 /* This rolls in nested transactions. I think that's right. */
336 for (i++; i <= op_num; i++)
337 op[i].group_start = start;
340 struct traverse_hash {
345 static void op_analyze_traverse(const char *filename,
346 struct op op[], unsigned int op_num,
351 op[op_num].key = tdb_null;
353 /* = %u means traverse function terminated. */
355 if (!streq(words[2], "=") || !words[3] || words[4])
356 fail(filename, op_num+1, "expect = <num>");
357 op[op_num].ret = atoi(words[3]);
361 for (i = op_num-1; i >= 0; i--) {
362 if (op[i].op != OP_TDB_TRAVERSE_READ_START
363 && op[i].op != OP_TDB_TRAVERSE_START)
371 fail(filename, op_num+1, "no traversal start found");
374 op[start].group_len = op_num - start;
376 for (i = start; i <= op_num; i++)
377 op[i].group_start = start;
380 /* Keep -Wmissing-declarations happy: */
381 const struct op_table *
382 find_keyword (register const char *str, register unsigned int len);
384 #include "keywords.c"
387 /* We can have more than one */
388 struct list_node pre_list;
389 struct list_node post_list;
390 unsigned int needs_file;
391 unsigned int needs_opnum;
392 unsigned int satisfies_file;
393 unsigned int satisfies_opnum;
396 static void check_deps(const char *filename, struct op op[], unsigned int num)
401 for (i = 1; i < num; i++)
402 if (!list_empty(&op[i].pre))
403 fail(filename, i+1, "Still has dependencies");
407 static void dump_pre(char *filename[], unsigned int file,
408 struct op op[], unsigned int i)
412 printf("%s:%u still waiting for:\n", filename[file], i+1);
413 list_for_each(&op[i].pre, dep, pre_list)
415 filename[dep->satisfies_file], dep->satisfies_opnum+1);
416 check_deps(filename[file], op, i);
419 /* We simply read/write pointers, since we all are children. */
420 static void do_pre(char *filename[], unsigned int file, int pre_fd,
421 struct op op[], unsigned int i)
423 while (!list_empty(&op[i].pre)) {
427 printf("%s:%u:waiting for pre\n", filename[file], i+1);
431 while (read(pre_fd, &dep, sizeof(dep)) != sizeof(dep)) {
432 if (errno == EINTR) {
433 dump_pre(filename, file, op, i);
436 errx(1, "Reading from pipe");
441 printf("%s:%u:got pre %u from %s:%u\n", filename[file], i+1,
442 dep->needs_opnum+1, filename[dep->satisfies_file],
443 dep->satisfies_opnum+1);
446 /* This could be any op, not just this one. */
451 static void do_post(char *filename[], unsigned int file,
452 const struct op op[], unsigned int i)
456 list_for_each(&op[i].post, dep, post_list) {
458 printf("%s:%u:sending to file %s:%u\n", filename[file], i+1,
459 filename[dep->needs_file], dep->needs_opnum+1);
461 if (write(pipes[dep->needs_file].fd[1], &dep, sizeof(dep))
463 err(1, "%s:%u failed to tell file %s",
464 filename[file], i+1, filename[dep->needs_file]);
468 static int get_len(TDB_DATA key, TDB_DATA data, void *private_data)
473 static unsigned run_ops(struct tdb_context *tdb,
478 unsigned int start, unsigned int stop);
480 struct traverse_info {
489 /* More complex. Just do whatever's they did at the n'th entry. */
490 static int nontrivial_traverse(struct tdb_context *tdb,
491 TDB_DATA key, TDB_DATA data,
494 struct traverse_info *tinfo = _tinfo;
495 unsigned int trav_len = tinfo->op[tinfo->start].group_len;
497 if (tinfo->i == tinfo->start + trav_len) {
498 /* This can happen if traverse expects to be empty. */
501 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
502 "traverse did not terminate");
505 if (tinfo->op[tinfo->i].op != OP_TDB_TRAVERSE)
506 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
507 "%s:%u:traverse terminated early");
509 /* Run any normal ops. */
510 tinfo->i = run_ops(tdb, tinfo->pre_fd, tinfo->filename, tinfo->file,
511 tinfo->op, tinfo->i+1, tinfo->start + trav_len);
513 if (tinfo->i == tinfo->start + trav_len)
519 static unsigned op_traverse(struct tdb_context *tdb,
523 int (*traversefn)(struct tdb_context *,
524 tdb_traverse_func, void *),
528 struct traverse_info tinfo = { op, filename, file, pre_fd,
531 traversefn(tdb, nontrivial_traverse, &tinfo);
533 /* Traversing in wrong order can have strange effects: eg. if
534 * original traverse went A (delete A), B, we might do B
535 * (delete A). So if we have ops left over, we do it now. */
536 while (tinfo.i != start + op[start].group_len) {
537 if (op[tinfo.i].op == OP_TDB_TRAVERSE)
540 tinfo.i = run_ops(tdb, pre_fd, filename, file, op,
541 tinfo.i, start + op[start].group_len);
547 static void break_out(int sig)
551 static __attribute__((noinline))
552 unsigned run_ops(struct tdb_context *tdb,
556 struct op op[], unsigned int start, unsigned int stop)
561 sa.sa_handler = break_out;
564 sigaction(SIGALRM, &sa, NULL);
565 for (i = start; i < stop; i++) {
566 do_pre(filename, file, pre_fd, op, i);
570 try(tdb_lockall(tdb), op[i].ret);
572 case OP_TDB_LOCKALL_MARK:
573 try(tdb_lockall_mark(tdb), op[i].ret);
575 case OP_TDB_LOCKALL_UNMARK:
576 try(tdb_lockall_unmark(tdb), op[i].ret);
578 case OP_TDB_LOCKALL_NONBLOCK:
579 unreliable(tdb_lockall_nonblock(tdb), op[i].ret,
580 tdb_lockall(tdb), tdb_unlockall(tdb));
582 case OP_TDB_UNLOCKALL:
583 try(tdb_unlockall(tdb), op[i].ret);
585 case OP_TDB_LOCKALL_READ:
586 try(tdb_lockall_read(tdb), op[i].ret);
588 case OP_TDB_LOCKALL_READ_NONBLOCK:
589 unreliable(tdb_lockall_read_nonblock(tdb), op[i].ret,
590 tdb_lockall_read(tdb),
591 tdb_unlockall_read(tdb));
593 case OP_TDB_UNLOCKALL_READ:
594 try(tdb_unlockall_read(tdb), op[i].ret);
596 case OP_TDB_CHAINLOCK:
597 try(tdb_chainlock(tdb, op[i].key), op[i].ret);
599 case OP_TDB_CHAINLOCK_NONBLOCK:
600 unreliable(tdb_chainlock_nonblock(tdb, op[i].key),
602 tdb_chainlock(tdb, op[i].key),
603 tdb_chainunlock(tdb, op[i].key));
605 case OP_TDB_CHAINLOCK_MARK:
606 try(tdb_chainlock_mark(tdb, op[i].key), op[i].ret);
608 case OP_TDB_CHAINLOCK_UNMARK:
609 try(tdb_chainlock_unmark(tdb, op[i].key), op[i].ret);
611 case OP_TDB_CHAINUNLOCK:
612 try(tdb_chainunlock(tdb, op[i].key), op[i].ret);
614 case OP_TDB_CHAINLOCK_READ:
615 try(tdb_chainlock_read(tdb, op[i].key), op[i].ret);
617 case OP_TDB_CHAINUNLOCK_READ:
618 try(tdb_chainunlock_read(tdb, op[i].key), op[i].ret);
620 case OP_TDB_PARSE_RECORD:
621 try(tdb_parse_record(tdb, op[i].key, get_len, NULL),
625 try(tdb_exists(tdb, op[i].key), op[i].ret);
628 try(tdb_store(tdb, op[i].key, op[i].data, op[i].flag),
632 try(tdb_append(tdb, op[i].key, op[i].data), op[i].ret);
634 case OP_TDB_GET_SEQNUM:
635 try(tdb_get_seqnum(tdb), op[i].ret);
637 case OP_TDB_WIPE_ALL:
638 try(tdb_wipe_all(tdb), op[i].ret);
640 case OP_TDB_TRANSACTION_START:
641 try(tdb_transaction_start(tdb), op[i].ret);
643 case OP_TDB_TRANSACTION_CANCEL:
644 try(tdb_transaction_cancel(tdb), op[i].ret);
646 case OP_TDB_TRANSACTION_COMMIT:
647 try(tdb_transaction_commit(tdb), op[i].ret);
649 case OP_TDB_TRAVERSE_READ_START:
650 i = op_traverse(tdb, pre_fd, filename, file,
651 tdb_traverse_read, op, i);
653 case OP_TDB_TRAVERSE_START:
654 i = op_traverse(tdb, pre_fd, filename, file,
655 tdb_traverse, op, i);
657 case OP_TDB_TRAVERSE:
658 /* Terminate: we're in a traverse, and we've
661 case OP_TDB_TRAVERSE_END:
662 fail(filename[file], i+1, "unexpected end traverse");
663 /* FIXME: These must be treated like traverse. */
664 case OP_TDB_FIRSTKEY:
665 if (!key_eq(tdb_firstkey(tdb), op[i].data))
666 fail(filename[file], i+1, "bad firstkey");
669 if (!key_eq(tdb_nextkey(tdb, op[i].key), op[i].data))
670 fail(filename[file], i+1, "bad nextkey");
673 TDB_DATA f = tdb_fetch(tdb, op[i].key);
674 if (!key_eq(f, op[i].data))
675 fail(filename[file], i+1, "bad fetch %u",
680 try(tdb_delete(tdb, op[i].key), op[i].ret);
683 do_post(filename, file, op, i);
688 static struct op *load_tracefile(const char *filename, unsigned int *num,
689 unsigned int *hashsize,
690 unsigned int *tdb_flags,
691 unsigned int *open_flags)
694 struct op *op = talloc_array(NULL, struct op, 1);
699 file = grab_file(NULL, filename, NULL);
701 err(1, "Reading %s", filename);
703 lines = strsplit(file, file, "\n", NULL);
705 errx(1, "%s is empty", filename);
707 words = strsplit(lines, lines[0], " ", NULL);
708 if (!streq(words[1], "tdb_open"))
709 fail(filename, 1, "does not start with tdb_open");
711 *hashsize = atoi(words[2]);
712 *tdb_flags = strtoul(words[3], NULL, 0);
713 *open_flags = strtoul(words[4], NULL, 0);
715 for (i = 1; lines[i]; i++) {
716 const struct op_table *opt;
718 words = strsplit(lines, lines[i], " ", NULL);
719 if (!words[0] || !words[1])
720 fail(filename, i+1, "Expected serial number and op");
722 opt = find_keyword(words[1], strlen(words[1]));
724 if (streq(words[1], "tdb_close")) {
727 "lines after tdb_close");
732 fail(filename, i+1, "Unknown operation '%s'", words[1]);
735 add_op(filename, &op, i, atoi(words[0]), opt->type);
736 opt->enhance_op(filename, op, i, words);
739 fprintf(stderr, "%s:%u:last operation is not tdb_close: incomplete?",
746 /* We remember all the keys we've ever seen, and who has them. */
754 unsigned int num_users;
755 struct key_user *user;
758 static const TDB_DATA must_not_exist;
759 static const TDB_DATA must_exist;
760 static const TDB_DATA not_exists_or_empty;
762 /* NULL means doesn't care if it exists or not, &must_exist means
763 * it must exist but we don't care what, &must_not_exist means it must
764 * not exist, otherwise the data it needs. */
765 static const TDB_DATA *needs(const struct op *op)
768 /* FIXME: Pull forward deps, since we can deadlock */
769 case OP_TDB_CHAINLOCK:
770 case OP_TDB_CHAINLOCK_NONBLOCK:
771 case OP_TDB_CHAINLOCK_MARK:
772 case OP_TDB_CHAINLOCK_UNMARK:
773 case OP_TDB_CHAINUNLOCK:
774 case OP_TDB_CHAINLOCK_READ:
775 case OP_TDB_CHAINUNLOCK_READ:
779 if (op->append.pre.dsize == 0)
780 return ¬_exists_or_empty;
781 return &op->append.pre;
784 if (op->flag == TDB_INSERT) {
788 return &must_not_exist;
789 } else if (op->flag == TDB_MODIFY) {
791 return &must_not_exist;
795 /* No flags? Don't care */
802 return &must_not_exist;
804 case OP_TDB_PARSE_RECORD:
806 return &must_not_exist;
809 /* FIXME: handle these. */
810 case OP_TDB_WIPE_ALL:
811 case OP_TDB_FIRSTKEY:
813 case OP_TDB_GET_SEQNUM:
814 case OP_TDB_TRAVERSE:
815 case OP_TDB_TRANSACTION_COMMIT:
816 case OP_TDB_TRANSACTION_CANCEL:
817 case OP_TDB_TRANSACTION_START:
822 return &must_not_exist;
827 return &must_not_exist;
831 errx(1, "Unexpected op %i", op->op);
836 /* What's the data after this op? pre if nothing changed. */
837 static const TDB_DATA *gives(const struct op *op, const TDB_DATA *pre)
839 /* Failed ops don't change state of db. */
843 if (op->op == OP_TDB_DELETE || op->op == OP_TDB_WIPE_ALL)
846 if (op->op == OP_TDB_APPEND)
847 return &op->append.post;
849 if (op->op == OP_TDB_STORE)
855 static struct keyinfo *hash_ops(struct op *op[], unsigned int num_ops[],
858 unsigned int i, j, h;
859 struct keyinfo *hash;
861 hash = talloc_zero_array(op[0], struct keyinfo, total_keys*2);
862 for (i = 0; i < num; i++) {
863 for (j = 1; j < num_ops[i]; j++) {
864 /* We can't do this on allocation, due to realloc. */
865 list_head_init(&op[i][j].post);
866 list_head_init(&op[i][j].pre);
868 if (!op[i][j].key.dptr)
871 /* We don't wait for traverse keys */
872 /* FIXME: We should, for trivial traversals. */
873 if (op[i][j].op == OP_TDB_TRAVERSE)
876 h = hash_key(&op[i][j].key) % (total_keys * 2);
877 while (!key_eq(hash[h].key, op[i][j].key)) {
878 if (!hash[h].key.dptr) {
879 hash[h].key = op[i][j].key;
882 h = (h + 1) % (total_keys * 2);
884 /* Might as well save some memory if we can. */
885 if (op[i][j].key.dptr != hash[h].key.dptr) {
886 talloc_free(op[i][j].key.dptr);
887 op[i][j].key.dptr = hash[h].key.dptr;
889 hash[h].user = talloc_realloc(hash, hash[h].user,
891 hash[h].num_users+1);
892 hash[h].user[hash[h].num_users].op_num = j;
893 hash[h].user[hash[h].num_users].file = i;
901 static bool satisfies(const TDB_DATA *data, const TDB_DATA *need)
903 /* Don't need anything? Cool. */
907 /* This should be tdb_null or a real value. */
908 assert(data != &must_exist);
909 assert(data != &must_not_exist);
910 assert(data != ¬_exists_or_empty);
912 /* must_not_exist == must_not_exist, must_exist == must_exist, or
913 not_exists_or_empty == not_exists_or_empty. */
914 if (data->dsize == need->dsize && data->dptr == need->dptr)
917 /* Must not exist? data must not exist. */
918 if (need == &must_not_exist)
919 return data->dptr == NULL;
922 if (need == &must_exist)
923 return data->dptr != NULL;
925 /* Either noexist or empty. */
926 if (need == ¬_exists_or_empty)
927 return data->dsize == 0;
929 /* Needs something specific. */
930 return key_eq(*data, *need);
933 static void move_to_front(struct key_user res[], unsigned int elem)
936 struct key_user tmp = res[elem];
937 memmove(res + 1, res, elem*sizeof(res[0]));
942 static void restore_to_pos(struct key_user res[], unsigned int elem)
945 struct key_user tmp = res[0];
946 memmove(res, res + 1, elem*sizeof(res[0]));
951 static bool sort_deps(char *filename[], struct op *op[],
952 struct key_user res[], unsigned num,
953 const TDB_DATA *data, unsigned num_files)
955 unsigned int i, files_done;
957 bool done[num_files];
959 /* Nothing left? We're sorted. */
963 memset(done, 0, sizeof(done));
965 /* Since ops within a trace file are ordered, we just need to figure
966 * out which file to try next. Since we don't take into account
967 * inter-key relationships (which exist by virtue of trace file order),
968 * we minimize the chance of harm by trying to keep in serial order. */
969 for (files_done = 0, i = 0; i < num && files_done < num_files; i++) {
970 if (done[res[i].file])
973 this_op = &op[res[i].file][res[i].op_num];
974 /* Is what we have good enough for this op? */
975 if (satisfies(data, needs(this_op))) {
976 move_to_front(res, i);
977 if (sort_deps(filename, op, res+1, num-1,
978 gives(this_op, data), num_files))
980 restore_to_pos(res, i);
982 done[res[i].file] = true;
986 /* No combination worked. */
990 static void check_dep_sorting(struct key_user user[], unsigned num_users,
995 unsigned minima[num_files];
997 memset(minima, 0, sizeof(minima));
998 for (i = 0; i < num_users; i++) {
999 assert(minima[user[i].file] < user[i].op_num);
1000 minima[user[i].file] = user[i].op_num;
1005 /* All these ops have the same serial number. Which comes first?
1007 * This can happen both because read ops or failed write ops don't
1008 * change serial number, and also due to race since we access the
1009 * number unlocked (the race can cause less detectable ordering problems,
1010 * in which case we'll deadlock and report: fix manually in that case).
1012 static void figure_deps(char *filename[], struct op *op[],
1013 struct key_user user[], unsigned num_users,
1016 /* We assume database starts empty. */
1017 const struct TDB_DATA *data = &tdb_null;
1019 if (!sort_deps(filename, op, user, num_users, data, num_files))
1020 fail(filename[user[0].file], user[0].op_num+1,
1021 "Could not resolve inter-dependencies");
1023 check_dep_sorting(user, num_users, num_files);
1026 static void sort_ops(struct keyinfo hash[], char *filename[], struct op *op[],
1031 /* Gcc nexted function extension. How cool is this? */
1032 int compare_serial(const void *_a, const void *_b)
1034 const struct key_user *a = _a, *b = _b;
1036 /* First, maintain order within any trace file. */
1037 if (a->file == b->file)
1038 return a->op_num - b->op_num;
1040 /* Otherwise, arrange by serial order. */
1041 return op[a->file][a->op_num].serial
1042 - op[b->file][b->op_num].serial;
1045 /* Now sort into serial order. */
1046 for (h = 0; h < total_keys * 2; h++) {
1047 struct key_user *user = hash[h].user;
1049 qsort(user, hash[h].num_users, sizeof(user[0]), compare_serial);
1050 figure_deps(filename, op, user, hash[h].num_users, num);
1054 static int destroy_depend(struct depend *dep)
1056 list_del(&dep->pre_list);
1057 list_del(&dep->post_list);
1061 static void add_dependency(void *ctx,
1064 unsigned int needs_file,
1065 unsigned int needs_opnum,
1066 unsigned int satisfies_file,
1067 unsigned int satisfies_opnum)
1070 unsigned int needs_start, sat_start;
1072 /* We don't depend on ourselves. */
1073 if (needs_file == satisfies_file) {
1074 assert(satisfies_opnum < needs_opnum);
1079 printf("%s:%u: depends on %s:%u\n",
1080 filename[needs_file], needs_opnum+1,
1081 filename[satisfies_file], satisfies_opnum+1);
1084 needs_start = op[needs_file][needs_opnum].group_start;
1085 sat_start = op[satisfies_file][satisfies_opnum].group_start;
1087 /* If needs is in a transaction, we need it before start. */
1089 switch (op[needs_file][needs_start].op) {
1090 case OP_TDB_TRANSACTION_START:
1091 needs_opnum = needs_start;
1093 printf(" -> Back to %u\n", needs_start+1);
1102 /* If satisfies is in a transaction, we wait until after commit. */
1103 /* FIXME: If transaction is cancelled, don't need dependency. */
1105 if (op[satisfies_file][sat_start].op
1106 == OP_TDB_TRANSACTION_START) {
1107 satisfies_opnum = sat_start
1108 + op[satisfies_file][sat_start].group_len;
1110 printf(" -> Depends on %u\n", satisfies_opnum+1);
1116 assert(op[needs_file][needs_opnum].op != OP_TDB_TRAVERSE);
1117 assert(op[satisfies_file][satisfies_opnum].op != OP_TDB_TRAVERSE);
1119 dep = talloc(ctx, struct depend);
1120 dep->needs_file = needs_file;
1121 dep->needs_opnum = needs_opnum;
1122 dep->satisfies_file = satisfies_file;
1123 dep->satisfies_opnum = satisfies_opnum;
1124 list_add(&op[satisfies_file][satisfies_opnum].post, &dep->post_list);
1125 list_add(&op[needs_file][needs_opnum].pre, &dep->pre_list);
1126 talloc_set_destructor(dep, destroy_depend);
1129 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1130 struct traverse_dep {
1132 unsigned int op_num;
1135 /* Traversals can deadlock against each other. Force order. */
1136 static void make_traverse_depends(char *filename[],
1137 struct op *op[], unsigned int num_ops[],
1140 unsigned int i, j, num_traversals = 0;
1141 struct traverse_dep *dep;
1143 /* Sort by which one runs first. */
1144 int compare_traverse_dep(const void *_a, const void *_b)
1146 const struct traverse_dep *ta = _a, *tb = _b;
1147 const struct op *a = &op[ta->file][ta->op_num],
1148 *b = &op[tb->file][tb->op_num];
1150 if (a->serial != b->serial)
1151 return a->serial - b->serial;
1153 /* If they have same serial, it means one didn't make any
1154 * changes. Thus sort by end in that case. */
1155 return a[a->group_len].serial - b[b->group_len].serial;
1158 dep = talloc_array(NULL, struct traverse_dep, 1);
1161 for (i = 0; i < num; i++) {
1162 for (j = 1; j < num_ops[i]; j++) {
1163 /* Transaction on traverse start. */
1164 if (op[i][j].group_start == j) {
1165 dep = talloc_realloc(NULL, dep,
1166 struct traverse_dep,
1168 dep[num_traversals].file = i;
1169 dep[num_traversals].op_num = j;
1174 qsort(dep, num_traversals, sizeof(dep[0]), compare_traverse_dep);
1175 for (i = 1; i < num_traversals; i++) {
1176 /* i depends on end of traverse i-1. */
1177 add_dependency(NULL, op, filename, dep[i].file, dep[i].op_num,
1178 dep[i-1].file, dep[i-1].op_num
1179 + op[dep[i-1].file][dep[i-1].op_num].group_len);
1183 #endif /* TRAVERSALS_TAKE_TRANSACTION_LOCK */
1185 static bool changes_db(const struct op *op)
1187 return gives(op, NULL) != NULL;
1190 static void depend_on_previous(struct op *op[],
1193 struct key_user user[],
1203 if (prev == i - 1) {
1204 /* Just depend on previous. */
1205 add_dependency(NULL, op, filename,
1206 user[i].file, user[i].op_num,
1207 user[prev].file, user[prev].op_num);
1211 /* We have to wait for the readers. Find last one in *each* file. */
1212 memset(deps, 0, sizeof(deps));
1213 deps[user[i].file] = true;
1214 for (j = i - 1; j > prev; j--) {
1215 if (!deps[user[j].file]) {
1216 add_dependency(NULL, op, filename,
1217 user[i].file, user[i].op_num,
1218 user[j].file, user[j].op_num);
1219 deps[user[j].file] = true;
1224 /* This is simple, but not complete. We don't take into account
1225 * indirect dependencies. */
1226 static void optimize_dependencies(struct op *op[], unsigned int num_ops[],
1231 /* There can only be one real dependency on each file */
1232 for (i = 0; i < num; i++) {
1233 for (j = 1; j < num_ops[i]; j++) {
1234 struct depend *dep, *next;
1235 struct depend *prev[num];
1237 memset(prev, 0, sizeof(prev));
1239 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1240 if (!prev[dep->satisfies_file]) {
1241 prev[dep->satisfies_file] = dep;
1244 if (prev[dep->satisfies_file]->satisfies_opnum
1245 < dep->satisfies_opnum) {
1246 talloc_free(prev[dep->satisfies_file]);
1247 prev[dep->satisfies_file] = dep;
1254 for (i = 0; i < num; i++) {
1257 for (j = 0; j < num; j++)
1260 for (j = 1; j < num_ops[i]; j++) {
1261 struct depend *dep, *next;
1263 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1264 if (deps[dep->satisfies_file]
1265 >= (int)dep->satisfies_opnum)
1268 deps[dep->satisfies_file]
1269 = dep->satisfies_opnum;
1275 static void derive_dependencies(char *filename[],
1276 struct op *op[], unsigned int num_ops[],
1279 struct keyinfo *hash;
1282 /* Create hash table for faster key lookup. */
1283 hash = hash_ops(op, num_ops, num);
1285 /* Sort them by serial number. */
1286 sort_ops(hash, filename, op, num);
1288 /* Create dependencies back to the last change, rather than
1289 * creating false dependencies by naively making each one
1290 * depend on the previous. This has two purposes: it makes
1291 * later optimization simpler, and it also avoids deadlock with
1292 * same sequence number ops inside traversals (if one
1293 * traversal doesn't write anything, two ops can have the same
1294 * sequence number yet we can create a traversal dependency
1295 * the other way). */
1296 for (h = 0; h < total_keys * 2; h++) {
1299 if (hash[h].num_users < 2)
1302 for (i = 0; i < hash[h].num_users; i++) {
1303 if (changes_db(&op[hash[h].user[i].file]
1304 [hash[h].user[i].op_num])) {
1305 depend_on_previous(op, filename, num,
1306 hash[h].user, i, prev);
1308 } else if (prev >= 0)
1309 add_dependency(hash, op, filename,
1310 hash[h].user[i].file,
1311 hash[h].user[i].op_num,
1312 hash[h].user[prev].file,
1313 hash[h].user[prev].op_num);
1317 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1318 make_traverse_depends(filename, op, num_ops, num);
1321 optimize_dependencies(op, num_ops, num);
1324 int main(int argc, char *argv[])
1326 struct timeval start, end;
1327 unsigned int i, num_ops[argc], hashsize[argc], tdb_flags[argc], open_flags[argc];
1328 struct op *op[argc];
1334 errx(1, "Usage: %s <tdbfile> <tracefile>...", argv[0]);
1336 pipes = talloc_array(NULL, struct pipe, argc - 2);
1337 for (i = 0; i < argc - 2; i++) {
1338 printf("Loading tracefile %s...", argv[2+i]);
1340 op[i] = load_tracefile(argv[2+i], &num_ops[i], &hashsize[i],
1341 &tdb_flags[i], &open_flags[i]);
1342 if (pipe(pipes[i].fd) != 0)
1343 err(1, "creating pipe");
1347 printf("Calculating inter-dependencies...");
1349 derive_dependencies(argv+2, op, num_ops, i);
1352 /* Don't fork for single arg case: simple debugging. */
1354 struct tdb_context *tdb;
1355 tdb = tdb_open_ex(argv[1], hashsize[0], tdb_flags[0],
1356 open_flags[0], 0600,
1358 printf("Single threaded run...");
1361 run_ops(tdb, pipes[0].fd[0], argv+2, 0, op[0], 1, num_ops[0]);
1362 check_deps(argv[2], op[0], num_ops[0]);
1369 err(1, "creating pipe");
1371 for (i = 0; i < argc - 2; i++) {
1372 struct tdb_context *tdb;
1376 err(1, "fork failed");
1379 tdb = tdb_open_ex(argv[1], hashsize[i], tdb_flags[i],
1380 open_flags[i], 0600,
1383 err(1, "Opening tdb %s", argv[1]);
1385 /* This catches parent exiting. */
1386 if (read(fds[0], &c, 1) != 1)
1388 run_ops(tdb, pipes[i].fd[0], argv+2, i, op[i], 1,
1390 check_deps(argv[2+i], op[i], num_ops[i]);
1397 /* Let everything settle. */
1400 printf("Starting run...");
1402 gettimeofday(&start, NULL);
1403 /* Tell them all to go! Any write of sufficient length will do. */
1404 if (write(fds[1], hashsize, i) != i)
1405 err(1, "Writing to wakeup pipe");
1407 for (i = 0; i < argc - 2; i++) {
1410 if (!WIFEXITED(status)) {
1411 warnx("Child died with signal %i", WTERMSIG(status));
1413 } else if (WEXITSTATUS(status) != 0)
1414 /* Assume child spat out error. */
1420 gettimeofday(&end, NULL);
1423 end.tv_sec -= start.tv_sec;
1424 printf("Time replaying: %lu usec\n",
1425 end.tv_sec * 1000000UL + (end.tv_usec - start.tv_usec));