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>
16 #define STRINGIFY2(x) #x
17 #define STRINGIFY(x) STRINGIFY2(x)
19 /* Avoid mod by zero */
20 static unsigned int total_keys = 1;
24 /* Traversals block transactions in the current implementation. */
25 #define TRAVERSALS_TAKE_TRANSACTION_LOCK 1
30 static struct pipe *pipes;
32 static void __attribute__((noreturn)) fail(const char *filename,
39 fprintf(stderr, "%s:%u: FAIL: ", filename, line);
40 vfprintf(stderr, fmt, ap);
41 fprintf(stderr, "\n");
47 #define try(expr, expect) \
50 if (ret != (expect)) \
51 fail(filename, i+1, STRINGIFY(expr) "= %i", ret); \
54 /* Try or imitate results. */
55 #define unreliable(expr, expect, force, undo) \
58 if (ret != expect) { \
59 fprintf(stderr, "%s:%u: %s gave %i not %i", \
60 filename, i+1, STRINGIFY(expr), ret, expect); \
68 static bool key_eq(TDB_DATA a, TDB_DATA b)
70 if (a.dsize != b.dsize)
72 return memcmp(a.dptr, b.dptr, a.dsize) == 0;
75 /* This is based on the hash algorithm from gdbm */
76 static unsigned int hash_key(TDB_DATA *key)
78 uint32_t value; /* Used to compute the hash value. */
79 uint32_t i; /* Used to cycle through random values. */
81 /* Set the initial value from the key size. */
82 for (value = 0x238F13AF ^ key->dsize, i=0; i < key->dsize; i++)
83 value = (value + (key->dptr[i] << (i*5 % 24)));
85 return (1103515243 * value + 12345);
91 OP_TDB_LOCKALL_UNMARK,
92 OP_TDB_LOCKALL_NONBLOCK,
95 OP_TDB_LOCKALL_READ_NONBLOCK,
96 OP_TDB_UNLOCKALL_READ,
98 OP_TDB_CHAINLOCK_NONBLOCK,
99 OP_TDB_CHAINLOCK_MARK,
100 OP_TDB_CHAINLOCK_UNMARK,
102 OP_TDB_CHAINLOCK_READ,
103 OP_TDB_CHAINUNLOCK_READ,
110 OP_TDB_TRANSACTION_START,
111 OP_TDB_TRANSACTION_CANCEL,
112 OP_TDB_TRANSACTION_COMMIT,
113 OP_TDB_TRAVERSE_READ_START,
114 OP_TDB_TRAVERSE_START,
130 /* Who is waiting for us? */
131 struct list_head post;
132 /* How many are we waiting for? */
135 /* If I'm part of a group (traverse/transaction) where is
136 * start? (Otherwise, 0) */
137 unsigned int group_start;
140 int flag; /* open and store */
141 struct traverse *trav; /* traverse start */
142 TDB_DATA post_append; /* append */
143 unsigned int transaction_end; /* transaction start */
147 static unsigned char hex_char(const char *filename, unsigned int line, char c)
150 if (c >= 'A' && c <= 'F')
152 if (c >= '0' && c <= '9')
154 fail(filename, line, "invalid hex character '%c'", c);
157 /* TDB data is <size>:<%02x>* */
158 static TDB_DATA make_tdb_data(const void *ctx,
159 const char *filename, unsigned int line,
166 if (streq(word, "NULL"))
169 data.dsize = atoi(word);
170 data.dptr = talloc_array(ctx, unsigned char, data.dsize);
171 p = strchr(word, ':');
173 fail(filename, line, "invalid tdb data '%s'", word);
175 for (i = 0; i < data.dsize; i++)
176 data.dptr[i] = hex_char(filename, line, p[i*2])*16
177 + hex_char(filename, line, p[i*2+1]);
182 static void add_op(const char *filename, struct op **op, unsigned int i,
183 unsigned int serial, enum op_type type)
186 *op = talloc_realloc(NULL, *op, struct op, i+1);
189 new->serial = serial;
192 new->group_start = 0;
195 static void op_add_nothing(const char *filename,
196 struct op op[], unsigned int op_num, char *words[])
199 fail(filename, op_num+1, "Expected no arguments");
200 op[op_num].key = tdb_null;
203 static void op_add_key(const char *filename,
204 struct op op[], unsigned int op_num, char *words[])
206 if (words[2] == NULL || words[3])
207 fail(filename, op_num+1, "Expected just a key");
209 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
210 if (op[op_num].op != OP_TDB_TRAVERSE)
214 static void op_add_key_ret(const char *filename,
215 struct op op[], unsigned int op_num, char *words[])
217 if (!words[2] || !words[3] || !words[4] || words[5]
218 || !streq(words[3], "="))
219 fail(filename, op_num+1, "Expected <key> = <ret>");
220 op[op_num].ret = atoi(words[4]);
221 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
222 /* May only be a unique key if it fails */
223 if (op[op_num].ret != 0)
227 static void op_add_key_data(const char *filename,
228 struct op op[], unsigned int op_num, char *words[])
230 if (!words[2] || !words[3] || !words[4] || words[5]
231 || !streq(words[3], "="))
232 fail(filename, op_num+1, "Expected <key> = <data>");
233 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
234 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[4]);
235 /* May only be a unique key if it fails */
236 if (!op[op_num].data.dptr)
240 /* <serial> tdb_store <rec> <rec> <flag> = <ret> */
241 static void op_add_store(const char *filename,
242 struct op op[], unsigned int op_num, char *words[])
244 if (!words[2] || !words[3] || !words[4] || !words[5] || !words[6]
245 || words[7] || !streq(words[5], "="))
246 fail(filename, op_num+1, "Expect <key> <data> <flag> = <ret>");
248 op[op_num].flag = strtoul(words[4], NULL, 0);
249 op[op_num].ret = atoi(words[6]);
250 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
251 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
255 /* <serial> tdb_append <rec> <rec> = <rec> */
256 static void op_add_append(const char *filename,
257 struct op op[], unsigned int op_num, char *words[])
259 if (!words[2] || !words[3] || !words[4] || !words[5] || words[6]
260 || !streq(words[4], "="))
261 fail(filename, op_num+1, "Expect <key> <data> = <rec>");
263 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
264 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
265 op[op_num].post_append
266 = make_tdb_data(op, filename, op_num+1, words[5]);
270 /* <serial> tdb_get_seqnum = <ret> */
271 static void op_add_seqnum(const char *filename,
272 struct op op[], unsigned int op_num, char *words[])
274 if (!words[2] || !words[3] || words[4] || !streq(words[2], "="))
275 fail(filename, op_num+1, "Expect = <ret>");
277 op[op_num].key = tdb_null;
278 op[op_num].ret = atoi(words[3]);
281 static void op_add_traverse(const char *filename,
282 struct op op[], unsigned int op_num, char *words[])
285 fail(filename, op_num+1, "Expect no arguments");
287 op[op_num].key = tdb_null;
288 op[op_num].trav = NULL;
291 static void op_add_transaction(const char *filename, struct op op[],
292 unsigned int op_num, char *words[])
295 fail(filename, op_num+1, "Expect no arguments");
297 op[op_num].key = tdb_null;
298 op[op_num].transaction_end = 0;
301 static void op_analyze_transaction(const char *filename,
302 struct op op[], unsigned int op_num,
307 op[op_num].key = tdb_null;
310 fail(filename, op_num+1, "Expect no arguments");
312 for (i = op_num-1; i >= 0; i--) {
313 if (op[i].op == OP_TDB_TRANSACTION_START &&
314 !op[i].transaction_end)
319 fail(filename, op_num+1, "no transaction start found");
322 op[start].transaction_end = op_num;
324 /* This rolls in nested transactions. I think that's right. */
325 for (i++; i <= op_num; i++)
326 op[i].group_start = start;
329 struct traverse_hash {
334 /* A traverse is a hash of keys, each one associated with ops. */
336 /* How many traversal callouts should I do? */
339 /* Where is traversal end op? */
342 /* For trivial traversals. */
343 struct traverse_hash *hash;
346 /* A trivial traversal is one which doesn't terminate early and only
347 * plays with its own record. We can reliably replay these even if
348 * traverse order changes. */
349 static bool is_trivial_traverse(struct op op[], unsigned int end)
353 TDB_DATA cur = tdb_null;
355 if (op[end].ret != 0)
358 for (i = 0; i < end; i++) {
361 if (op[i].op == OP_TDB_TRAVERSE)
363 if (!key_eq(cur, op[i].key))
368 /* With multiple things happening at once, no traverse is trivial. */
372 static void op_analyze_traverse(const char *filename,
373 struct op op[], unsigned int op_num,
377 struct traverse *trav = talloc(op, struct traverse);
379 op[op_num].key = tdb_null;
381 /* = %u means traverse function terminated. */
383 if (!streq(words[2], "=") || !words[3] || words[4])
384 fail(filename, op_num+1, "expect = <num>");
385 op[op_num].ret = atoi(words[3]);
391 for (i = op_num-1; i >= 0; i--) {
392 if (op[i].op == OP_TDB_TRAVERSE)
394 if (op[i].op != OP_TDB_TRAVERSE_READ_START
395 && op[i].op != OP_TDB_TRAVERSE_START)
403 fail(filename, op_num+1, "no traversal start found");
406 op[start].trav = trav;
408 for (i = start; i <= op_num; i++)
409 op[i].group_start = start;
411 if (is_trivial_traverse(op+i, op_num-i)) {
412 /* Fill in a plentiful hash table. */
413 op[start].trav->hash = talloc_zero_array(op[i].trav,
414 struct traverse_hash,
416 for (i = start; i < op_num; i++) {
418 if (op[i].op != OP_TDB_TRAVERSE)
420 h = hash_key(&op[i].key) % (trav->num * 2);
421 while (trav->hash[h].index)
422 h = (h + 1) % (trav->num * 2);
423 trav->hash[h].index = i+1;
424 trav->hash[h].key = op[i].key;
430 /* Keep -Wmissing-declarations happy: */
431 const struct op_table *
432 find_keyword (register const char *str, register unsigned int len);
434 #include "keywords.c"
436 static int get_len(TDB_DATA key, TDB_DATA data, void *private_data)
441 static unsigned run_ops(struct tdb_context *tdb,
443 const char *filename,
445 unsigned int start, unsigned int stop);
447 struct traverse_info {
449 const char *filename;
455 /* Trivial case: do whatever they did for this key. */
456 static int trivial_traverse(struct tdb_context *tdb,
457 TDB_DATA key, TDB_DATA data,
460 struct traverse_info *tinfo = _tinfo;
461 struct traverse *trav = tinfo->op[tinfo->start].trav;
462 unsigned int h = hash_key(&key) % (trav->num * 2);
464 while (trav->hash[h].index) {
465 if (key_eq(trav->hash[h].key, key)) {
466 run_ops(tdb, tinfo->pre_fd, tinfo->filename, tinfo->op,
467 trav->hash[h].index, trav->end);
471 h = (h + 1) % (trav->num * 2);
473 fail(tinfo->filename, tinfo->start + 1, "unexpected traverse key");
476 /* More complex. Just do whatever's they did at the n'th entry. */
477 static int nontrivial_traverse(struct tdb_context *tdb,
478 TDB_DATA key, TDB_DATA data,
481 struct traverse_info *tinfo = _tinfo;
482 struct traverse *trav = tinfo->op[tinfo->start].trav;
484 if (tinfo->i == trav->end) {
485 /* This can happen if traverse expects to be empty. */
486 if (tinfo->start + 1 == trav->end)
488 fail(tinfo->filename, tinfo->start + 1,
489 "traverse did not terminate");
492 if (tinfo->op[tinfo->i].op != OP_TDB_TRAVERSE)
493 fail(tinfo->filename, tinfo->start + 1,
494 "%s:%u:traverse terminated early");
496 /* Run any normal ops. */
497 tinfo->i = run_ops(tdb, tinfo->pre_fd, tinfo->filename, tinfo->op,
498 tinfo->i+1, trav->end);
500 if (tinfo->i == trav->end)
506 static unsigned op_traverse(struct tdb_context *tdb,
508 const char *filename,
509 int (*traversefn)(struct tdb_context *,
510 tdb_traverse_func, void *),
514 struct traverse *trav = op[start].trav;
515 struct traverse_info tinfo = { op, filename, pre_fd, start, start+1 };
519 int ret = traversefn(tdb, trivial_traverse, &tinfo);
520 if (ret != trav->num)
521 fail(filename, start+1, "short traversal %i", ret);
525 traversefn(tdb, nontrivial_traverse, &tinfo);
527 /* Traversing in wrong order can have strange effects: eg. if
528 * original traverse went A (delete A), B, we might do B
529 * (delete A). So if we have ops left over, we do it now. */
530 while (tinfo.i != trav->end) {
531 if (op[tinfo.i].op == OP_TDB_TRAVERSE)
534 tinfo.i = run_ops(tdb, pre_fd, filename, op,
541 /* We can have more than one */
542 struct list_node list;
547 static void do_pre(const char *filename, int pre_fd,
548 struct op op[], unsigned int i)
550 while (op[i].pre != 0) {
554 printf("%s:%u:waiting for pre\n", filename, i+1);
557 if (read(pre_fd, &opnum, sizeof(opnum)) != sizeof(opnum))
558 errx(1, "Reading from pipe");
561 printf("%s:%u:got pre %u (%u)\n", filename, i+1, opnum+1,
565 /* This could be any op, not just this one. */
566 if (op[opnum].pre == 0)
567 errx(1, "Got extra notification for op line %u",
570 errx(1, "%s:%u: got late notification for line %u",
571 filename, i + 1, opnum + 1);
576 static void do_post(const char *filename, const struct op op[], unsigned int i)
580 list_for_each(&op[i].post, dep, list) {
582 printf("%s:%u:sending %u to file %u\n", filename, i+1,
583 dep->op+1, dep->file);
585 if (write(pipes[dep->file].fd[1], &dep->op, sizeof(dep->op))
587 err(1, "Failed to tell file %u", dep->file);
591 static __attribute__((noinline))
592 unsigned run_ops(struct tdb_context *tdb,
594 const char *filename,
595 struct op op[], unsigned int start, unsigned int stop)
599 for (i = start; i < stop; i++) {
600 do_pre(filename, pre_fd, op, i);
604 try(tdb_lockall(tdb), op[i].ret);
606 case OP_TDB_LOCKALL_MARK:
607 try(tdb_lockall_mark(tdb), op[i].ret);
609 case OP_TDB_LOCKALL_UNMARK:
610 try(tdb_lockall_unmark(tdb), op[i].ret);
612 case OP_TDB_LOCKALL_NONBLOCK:
613 unreliable(tdb_lockall_nonblock(tdb), op[i].ret,
614 tdb_lockall(tdb), tdb_unlockall(tdb));
616 case OP_TDB_UNLOCKALL:
617 try(tdb_unlockall(tdb), op[i].ret);
619 case OP_TDB_LOCKALL_READ:
620 try(tdb_lockall_read(tdb), op[i].ret);
622 case OP_TDB_LOCKALL_READ_NONBLOCK:
623 unreliable(tdb_lockall_read_nonblock(tdb), op[i].ret,
624 tdb_lockall_read(tdb),
625 tdb_unlockall_read(tdb));
627 case OP_TDB_UNLOCKALL_READ:
628 try(tdb_unlockall_read(tdb), op[i].ret);
630 case OP_TDB_CHAINLOCK:
631 try(tdb_chainlock(tdb, op[i].key), op[i].ret);
633 case OP_TDB_CHAINLOCK_NONBLOCK:
634 unreliable(tdb_chainlock_nonblock(tdb, op[i].key),
636 tdb_chainlock(tdb, op[i].key),
637 tdb_chainunlock(tdb, op[i].key));
639 case OP_TDB_CHAINLOCK_MARK:
640 try(tdb_chainlock_mark(tdb, op[i].key), op[i].ret);
642 case OP_TDB_CHAINLOCK_UNMARK:
643 try(tdb_chainlock_unmark(tdb, op[i].key), op[i].ret);
645 case OP_TDB_CHAINUNLOCK:
646 try(tdb_chainunlock(tdb, op[i].key), op[i].ret);
648 case OP_TDB_CHAINLOCK_READ:
649 try(tdb_chainlock_read(tdb, op[i].key), op[i].ret);
651 case OP_TDB_CHAINUNLOCK_READ:
652 try(tdb_chainunlock_read(tdb, op[i].key), op[i].ret);
654 case OP_TDB_PARSE_RECORD:
655 try(tdb_parse_record(tdb, op[i].key, get_len, NULL),
659 try(tdb_exists(tdb, op[i].key), op[i].ret);
662 try(tdb_store(tdb, op[i].key, op[i].data, op[i].flag),
663 op[i].ret < 0 ? op[i].ret : 0);
666 try(tdb_append(tdb, op[i].key, op[i].data),
667 op[i].ret < 0 ? op[i].ret : 0);
669 case OP_TDB_GET_SEQNUM:
670 try(tdb_get_seqnum(tdb), op[i].ret);
672 case OP_TDB_WIPE_ALL:
673 try(tdb_wipe_all(tdb), op[i].ret);
675 case OP_TDB_TRANSACTION_START:
676 try(tdb_transaction_start(tdb), op[i].ret);
678 case OP_TDB_TRANSACTION_CANCEL:
679 try(tdb_transaction_cancel(tdb), op[i].ret);
681 case OP_TDB_TRANSACTION_COMMIT:
682 try(tdb_transaction_commit(tdb), op[i].ret);
684 case OP_TDB_TRAVERSE_READ_START:
685 i = op_traverse(tdb, pre_fd, filename,
686 tdb_traverse_read, op, i);
688 case OP_TDB_TRAVERSE_START:
689 i = op_traverse(tdb, pre_fd, filename,
690 tdb_traverse, op, i);
692 case OP_TDB_TRAVERSE:
693 /* Terminate: we're in a traverse, and we've
696 case OP_TDB_TRAVERSE_END:
697 fail(filename, i+1, "unepxected end traverse");
698 /* FIXME: These must be treated like traverse. */
699 case OP_TDB_FIRSTKEY:
700 if (!key_eq(tdb_firstkey(tdb), op[i].data))
701 fail(filename, i+1, "bad firstkey");
704 if (!key_eq(tdb_nextkey(tdb, op[i].key), op[i].data))
705 fail(filename, i+1, "bad nextkey");
708 TDB_DATA f = tdb_fetch(tdb, op[i].key);
709 if (!key_eq(f, op[i].data))
710 fail(filename, i+1, "bad fetch %u", f.dsize);
714 try(tdb_delete(tdb, op[i].key), op[i].ret);
717 do_post(filename, op, i);
722 static struct op *load_tracefile(const char *filename, unsigned int *num,
723 unsigned int *hashsize,
724 unsigned int *tdb_flags,
725 unsigned int *open_flags)
728 struct op *op = talloc_array(NULL, struct op, 1);
733 file = grab_file(NULL, filename, NULL);
735 err(1, "Reading %s", filename);
737 lines = strsplit(file, file, "\n", NULL);
739 errx(1, "%s is empty", filename);
741 words = strsplit(lines, lines[0], " ", NULL);
742 if (!streq(words[1], "tdb_open"))
743 fail(filename, 1, "does not start with tdb_open");
745 *hashsize = atoi(words[2]);
746 *tdb_flags = strtoul(words[3], NULL, 0);
747 *open_flags = strtoul(words[4], NULL, 0);
749 for (i = 1; lines[i]; i++) {
750 const struct op_table *opt;
752 words = strsplit(lines, lines[i], " ", NULL);
753 if (!words[0] || !words[1])
754 fail(filename, i+1, "Expected serial number and op");
756 opt = find_keyword(words[1], strlen(words[1]));
758 if (streq(words[1], "tdb_close")) {
761 "lines after tdb_close");
766 fail(filename, i+1, "Unknown operation '%s'", words[1]);
769 add_op(filename, &op, i, atoi(words[0]), opt->type);
770 opt->enhance_op(filename, op, i, words);
773 fprintf(stderr, "%s:%u:last operation is not tdb_close: incomplete?",
780 /* We remember all the keys we've ever seen, and who has them. */
788 unsigned int num_users;
789 struct key_user *user;
792 static bool changes_db(const struct op *op)
797 return op->op == OP_TDB_STORE
798 || op->op == OP_TDB_APPEND
799 || op->op == OP_TDB_WIPE_ALL
800 || op->op == OP_TDB_TRANSACTION_COMMIT
801 || op->op == OP_TDB_DELETE;
804 static struct keyinfo *hash_ops(struct op *op[], unsigned int num_ops[],
807 unsigned int i, j, h;
808 struct keyinfo *hash;
810 /* Gcc nexted function extension. How cool is this? */
811 int compare_user_serial(const void *_a, const void *_b)
813 const struct key_user *a = _a, *b = _b;
814 int ret = op[a->file][a->op_num].serial
815 - op[b->file][b->op_num].serial;
817 /* Fetches don't inc serial, so we put changes first. */
819 if (changes_db(&op[a->file][a->op_num])
820 && !changes_db(&op[b->file][b->op_num]))
822 if (changes_db(&op[b->file][b->op_num])
823 && !changes_db(&op[a->file][a->op_num]))
829 hash = talloc_zero_array(op[0], struct keyinfo, total_keys*2);
830 for (i = 0; i < num; i++) {
831 for (j = 1; j < num_ops[i]; j++) {
832 /* We can't do this on allocation, due to realloc. */
833 list_head_init(&op[i][j].post);
835 if (!op[i][j].key.dptr)
838 /* We don't wait for traverse keys */
839 /* FIXME: We should, for trivial traversals. */
840 if (op[i][j].op == OP_TDB_TRAVERSE)
843 h = hash_key(&op[i][j].key) % (total_keys * 2);
844 while (!key_eq(hash[h].key, op[i][j].key)) {
845 if (!hash[h].key.dptr) {
846 hash[h].key = op[i][j].key;
849 h = (h + 1) % (total_keys * 2);
851 /* Might as well save some memory if we can. */
852 if (op[i][j].key.dptr != hash[h].key.dptr) {
853 talloc_free(op[i][j].key.dptr);
854 op[i][j].key.dptr = hash[h].key.dptr;
856 hash[h].user = talloc_realloc(hash, hash[h].user,
858 hash[h].num_users+1);
859 hash[h].user[hash[h].num_users].op_num = j;
860 hash[h].user[hash[h].num_users].file = i;
865 /* Now sort into seqnum order. */
866 for (h = 0; h < total_keys * 2; h++)
867 qsort(hash[h].user, hash[h].num_users, sizeof(hash[h].user[0]),
868 compare_user_serial);
873 static void add_dependency(void *ctx,
875 unsigned int needs_file,
876 unsigned int needs_opnum,
877 unsigned int satisfies_file,
878 unsigned int satisfies_opnum)
881 unsigned int needs_start, sat_start;
883 needs_start = op[needs_file][needs_opnum].group_start;
884 sat_start = op[satisfies_file][satisfies_opnum].group_start;
886 /* If needs is in a transaction, we need it before start. */
888 switch (op[needs_file][needs_start].op) {
889 case OP_TDB_TRANSACTION_START:
890 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
891 case OP_TDB_TRAVERSE_START:
892 case OP_TDB_TRAVERSE_READ_START:
894 needs_opnum = needs_start;
896 printf(" -> Back to %u\n", needs_start+1);
905 /* If satisfies is in a transaction, we wait until after commit. */
906 /* FIXME: If transaction is cancelled, don't need dependency. */
908 if (op[satisfies_file][sat_start].op
909 == OP_TDB_TRANSACTION_START) {
911 = op[satisfies_file][sat_start].transaction_end;
913 printf(" -> Depends on %u\n", satisfies_opnum+1);
919 post = talloc(ctx, struct depend);
920 post->file = needs_file;
921 post->op = needs_opnum;
922 list_add(&op[satisfies_file][satisfies_opnum].post, &post->list);
924 op[needs_file][needs_opnum].pre++;
926 if (op[needs_file][needs_opnum].pre > 1) {
927 printf(" (File %u opnum %u hash %u needs)\n",
928 needs_file, needs_opnum+1,
929 op[needs_file][needs_opnum].pre);
935 static void derive_dependencies(char *filename[],
936 struct op *op[], unsigned int num_ops[],
939 struct keyinfo *hash;
942 /* Create hash table for faster key lookup. */
943 hash = hash_ops(op, num_ops, num);
945 /* We make the naive assumption that two ops on the same key
946 * have to be ordered; it's overkill. */
947 for (i = 0; i < total_keys * 2; i++) {
950 for (j = 1; j < hash[i].num_users; j++) {
951 /* We don't depend on ourselves. */
952 if (hash[i].user[j].file == hash[i].user[j-1].file)
955 printf("%s:%u: depends on %s:%u\n",
956 filename[hash[i].user[j].file],
957 hash[i].user[j].op_num+1,
958 filename[hash[i].user[j-1].file],
959 hash[i].user[j-1].op_num+1);
962 add_dependency(hash, op,
963 hash[i].user[j].file,
964 hash[i].user[j].op_num,
965 hash[i].user[j-1].file,
966 hash[i].user[j-1].op_num);
971 int main(int argc, char *argv[])
973 struct timeval start, end;
974 unsigned int i, num_ops[argc], hashsize[argc], tdb_flags[argc], open_flags[argc];
980 errx(1, "Usage: %s <tdbfile> <tracefile>...", argv[0]);
982 pipes = talloc_array(NULL, struct pipe, argc - 2);
983 for (i = 0; i < argc - 2; i++) {
984 op[i] = load_tracefile(argv[2+i], &num_ops[i], &hashsize[i],
985 &tdb_flags[i], &open_flags[i]);
986 if (pipe(pipes[i].fd) != 0)
987 err(1, "creating pipe");
990 derive_dependencies(argv+2, op, num_ops, i);
992 /* Don't fork for single arg case: simple debugging. */
994 struct tdb_context *tdb;
995 tdb = tdb_open_ex(argv[1], hashsize[0], tdb_flags[0],
998 run_ops(tdb, pipes[0].fd[0], argv[2],
999 op[0], 1, num_ops[0]);
1004 err(1, "creating pipe");
1006 for (i = 0; i < argc - 2; i++) {
1007 struct tdb_context *tdb;
1011 err(1, "fork failed");
1014 tdb = tdb_open_ex(argv[1], hashsize[i], tdb_flags[i],
1015 open_flags[i], 0600,
1018 err(1, "Opening tdb %s", argv[1]);
1020 /* This catches parent exiting. */
1021 if (read(fds[0], &c, 1) != 1)
1023 run_ops(tdb, pipes[i].fd[0], argv[2+i],
1024 op[i], 1, num_ops[i]);
1031 /* Let everything settle. */
1034 gettimeofday(&start, NULL);
1035 /* Tell them all to go! Any write of sufficient length will do. */
1036 if (write(fds[1], hashsize, i) != i)
1037 err(1, "Writing to wakeup pipe");
1039 for (i = 0; i < argc - 2; i++) {
1042 if (!WIFEXITED(status))
1043 errx(1, "Child died with signal %i", WTERMSIG(status));
1044 if (WEXITSTATUS(status) != 0)
1045 errx(1, "Child died with error code");
1047 gettimeofday(&end, NULL);
1049 end.tv_sec -= start.tv_sec;
1050 printf("Time replaying: %lu usec\n",
1051 end.tv_sec * 1000000UL + (end.tv_usec - start.tv_usec));