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 /* transaction/traverse start/chainlock */
151 unsigned int group_len;
160 static unsigned char hex_char(const char *filename, unsigned int line, char c)
163 if (c >= 'A' && c <= 'F')
165 if (c >= '0' && c <= '9')
167 fail(filename, line, "invalid hex character '%c'", c);
170 /* TDB data is <size>:<%02x>* */
171 static TDB_DATA make_tdb_data(const void *ctx,
172 const char *filename, unsigned int line,
179 if (streq(word, "NULL"))
182 data.dsize = atoi(word);
183 data.dptr = talloc_array(ctx, unsigned char, data.dsize);
184 p = strchr(word, ':');
186 fail(filename, line, "invalid tdb data '%s'", word);
188 for (i = 0; i < data.dsize; i++)
189 data.dptr[i] = hex_char(filename, line, p[i*2])*16
190 + hex_char(filename, line, p[i*2+1]);
195 static void add_op(const char *filename, struct op **op, unsigned int i,
196 unsigned int seqnum, enum op_type type)
199 *op = talloc_realloc(NULL, *op, struct op, i+1);
202 new->seqnum = seqnum;
204 new->group_start = 0;
207 static void op_add_nothing(const char *filename,
208 struct op op[], unsigned int op_num, char *words[])
211 fail(filename, op_num+1, "Expected no arguments");
212 op[op_num].key = tdb_null;
215 static void op_add_key(const char *filename,
216 struct op op[], unsigned int op_num, char *words[])
218 if (words[2] == NULL || words[3])
219 fail(filename, op_num+1, "Expected just a key");
221 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
225 static void op_add_key_ret(const char *filename,
226 struct op op[], unsigned int op_num, char *words[])
228 if (!words[2] || !words[3] || !words[4] || words[5]
229 || !streq(words[3], "="))
230 fail(filename, op_num+1, "Expected <key> = <ret>");
231 op[op_num].ret = atoi(words[4]);
232 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
233 /* May only be a unique key if it fails */
234 if (op[op_num].ret != 0)
238 static void op_add_key_data(const char *filename,
239 struct op op[], unsigned int op_num, char *words[])
241 if (!words[2] || !words[3] || !words[4] || words[5]
242 || !streq(words[3], "="))
243 fail(filename, op_num+1, "Expected <key> = <data>");
244 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
245 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[4]);
246 /* May only be a unique key if it fails */
247 if (!op[op_num].data.dptr)
251 /* We don't record the keys or data for a traverse, as we don't use them. */
252 static void op_add_traverse(const char *filename,
253 struct op op[], unsigned int op_num, char *words[])
255 if (!words[2] || !words[3] || !words[4] || words[5]
256 || !streq(words[3], "="))
257 fail(filename, op_num+1, "Expected <key> = <data>");
258 op[op_num].key = tdb_null;
261 /* Full traverse info is useful for debugging, but changing it to
262 * "traversefn" without the data makes the traces *much* smaller! */
263 static void op_add_traversefn(const char *filename,
264 struct op op[], unsigned int op_num, char *words[])
267 fail(filename, op_num+1, "Expected no values");
268 op[op_num].key = tdb_null;
271 /* <seqnum> tdb_store <rec> <rec> <flag> = <ret> */
272 static void op_add_store(const char *filename,
273 struct op op[], unsigned int op_num, char *words[])
275 if (!words[2] || !words[3] || !words[4] || !words[5] || !words[6]
276 || words[7] || !streq(words[5], "="))
277 fail(filename, op_num+1, "Expect <key> <data> <flag> = <ret>");
279 op[op_num].flag = strtoul(words[4], NULL, 0);
280 op[op_num].ret = atoi(words[6]);
281 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
282 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
286 /* <seqnum> tdb_append <rec> <rec> = <rec> */
287 static void op_add_append(const char *filename,
288 struct op op[], unsigned int op_num, char *words[])
290 if (!words[2] || !words[3] || !words[4] || !words[5] || words[6]
291 || !streq(words[4], "="))
292 fail(filename, op_num+1, "Expect <key> <data> = <rec>");
294 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
295 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
297 op[op_num].append.post
298 = make_tdb_data(op, filename, op_num+1, words[5]);
300 /* By subtraction, figure out what previous data was. */
301 op[op_num].append.pre.dptr = op[op_num].append.post.dptr;
302 op[op_num].append.pre.dsize
303 = op[op_num].append.post.dsize - op[op_num].data.dsize;
307 /* <seqnum> tdb_get_seqnum = <ret> */
308 static void op_add_seqnum(const char *filename,
309 struct op op[], unsigned int op_num, char *words[])
311 if (!words[2] || !words[3] || words[4] || !streq(words[2], "="))
312 fail(filename, op_num+1, "Expect = <ret>");
314 op[op_num].key = tdb_null;
315 op[op_num].ret = atoi(words[3]);
318 static void op_add_traverse_start(const char *filename,
320 unsigned int op_num, char *words[])
323 fail(filename, op_num+1, "Expect no arguments");
325 op[op_num].key = tdb_null;
326 op[op_num].group_len = 0;
329 static void op_add_transaction(const char *filename, struct op op[],
330 unsigned int op_num, char *words[])
333 fail(filename, op_num+1, "Expect no arguments");
335 op[op_num].key = tdb_null;
336 op[op_num].group_len = 0;
339 static void op_add_chainlock(const char *filename,
340 struct op op[], unsigned int op_num, char *words[])
342 if (words[2] == NULL || words[3])
343 fail(filename, op_num+1, "Expected just a key");
345 /* A chainlock key isn't a key in the normal sense; it doesn't
346 * have to be in the db at all. Also, we don't want to hash this op. */
347 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[2]);
348 op[op_num].key = tdb_null;
349 op[op_num].group_len = 0;
352 static void op_add_chainlock_ret(const char *filename,
353 struct op op[], unsigned int op_num,
356 if (!words[2] || !words[3] || !words[4] || words[5]
357 || !streq(words[3], "="))
358 fail(filename, op_num+1, "Expected <key> = <ret>");
359 op[op_num].ret = atoi(words[4]);
360 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[2]);
361 op[op_num].key = tdb_null;
362 op[op_num].group_len = 0;
366 static int op_find_start(struct op op[], unsigned int op_num, enum op_type type)
370 for (i = op_num-1; i > 0; i--) {
371 if (op[i].type == type && !op[i].group_len)
377 static void op_analyze_transaction(const char *filename,
378 struct op op[], unsigned int op_num,
381 unsigned int start, i;
383 op[op_num].key = tdb_null;
386 fail(filename, op_num+1, "Expect no arguments");
388 start = op_find_start(op, op_num, OP_TDB_TRANSACTION_START);
390 fail(filename, op_num+1, "no transaction start found");
392 op[start].group_len = op_num - start;
394 /* This rolls in nested transactions. I think that's right. */
395 for (i = start; i <= op_num; i++)
396 op[i].group_start = start;
399 /* We treat chainlocks a lot like transactions, even though that's overkill */
400 static void op_analyze_chainlock(const char *filename,
401 struct op op[], unsigned int op_num,
404 unsigned int i, start;
406 if (words[2] == NULL || words[3])
407 fail(filename, op_num+1, "Expected just a key");
409 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[2]);
410 op[op_num].key = tdb_null;
413 start = op_find_start(op, op_num, OP_TDB_CHAINLOCK);
415 start = op_find_start(op, op_num, OP_TDB_CHAINLOCK_READ);
417 fail(filename, op_num+1, "no initial chainlock found");
419 /* FIXME: We'd have to do something clever to make this work
421 if (!key_eq(op[start].data, op[op_num].data))
422 fail(filename, op_num+1, "nested chainlock calls?");
424 op[start].group_len = op_num - start;
425 for (i = start; i <= op_num; i++)
426 op[i].group_start = start;
429 static void op_analyze_traverse(const char *filename,
430 struct op op[], unsigned int op_num,
435 op[op_num].key = tdb_null;
437 /* = %u means traverse function terminated. */
439 if (!streq(words[2], "=") || !words[3] || words[4])
440 fail(filename, op_num+1, "expect = <num>");
441 op[op_num].ret = atoi(words[3]);
445 start = op_find_start(op, op_num, OP_TDB_TRAVERSE_START);
447 start = op_find_start(op, op_num, OP_TDB_TRAVERSE_READ_START);
449 fail(filename, op_num+1, "no traversal start found");
451 op[start].group_len = op_num - start;
453 /* Don't roll in nested traverse/chainlock */
454 for (i = start; i <= op_num; i++)
455 if (!op[i].group_start)
456 op[i].group_start = start;
459 /* Keep -Wmissing-declarations happy: */
460 const struct op_table *
461 find_keyword (register const char *str, register unsigned int len);
463 #include "keywords.c"
466 /* We can have more than one */
467 struct list_node pre_list;
468 struct list_node post_list;
469 struct op_desc needs;
470 struct op_desc prereq;
473 static void check_deps(const char *filename, struct op op[], unsigned int num)
478 for (i = 1; i < num; i++)
479 if (!list_empty(&op[i].pre))
480 fail(filename, i+1, "Still has dependencies");
484 static void dump_pre(char *filename[], struct op *op[],
485 unsigned int file, unsigned int i)
489 printf("%s:%u (%u) still waiting for:\n", filename[file], i+1,
491 list_for_each(&op[file][i].pre, dep, pre_list)
492 printf(" %s:%u (%u)\n",
493 filename[dep->prereq.file], dep->prereq.op_num+1,
494 op[dep->prereq.file][dep->prereq.op_num].seqnum);
495 check_deps(filename[file], op[file], i);
498 /* We simply read/write pointers, since we all are children. */
499 static bool do_pre(struct tdb_context *tdb,
500 char *filename[], struct op *op[],
501 unsigned int file, int pre_fd, unsigned int i,
504 while (!list_empty(&op[file][i].pre)) {
508 printf("%s:%u:waiting for pre\n", filename[file], i+1);
515 while (read(pre_fd, &dep, sizeof(dep)) != sizeof(dep)) {
516 if (errno == EINTR) {
518 warnx("%s:%u:avoiding deadlock",
519 filename[file], i+1);
522 dump_pre(filename, op, file, i);
525 errx(1, "Reading from pipe");
530 printf("%s:%u:got pre %u from %s:%u\n", filename[file], i+1,
531 dep->needs.op_num+1, filename[dep->prereq.file],
532 dep->prereq.op_num+1);
535 /* This could be any op, not just this one. */
541 static void do_post(char *filename[], struct op *op[],
542 unsigned int file, unsigned int i)
546 list_for_each(&op[file][i].post, dep, post_list) {
548 printf("%s:%u:sending to file %s:%u\n", filename[file], i+1,
549 filename[dep->needs.file], dep->needs.op_num+1);
551 if (write(pipes[dep->needs.file].fd[1], &dep, sizeof(dep))
553 err(1, "%s:%u failed to tell file %s",
554 filename[file], i+1, filename[dep->needs.file]);
558 static int get_len(TDB_DATA key, TDB_DATA data, void *private_data)
563 static unsigned run_ops(struct tdb_context *tdb,
568 unsigned int start, unsigned int stop,
571 struct traverse_info {
580 /* More complex. Just do whatever's they did at the n'th entry. */
581 static int nontrivial_traverse(struct tdb_context *tdb,
582 TDB_DATA key, TDB_DATA data,
585 struct traverse_info *tinfo = _tinfo;
586 unsigned int trav_len = tinfo->op[tinfo->file][tinfo->start].group_len;
587 bool avoid_deadlock = false;
589 if (tinfo->i == tinfo->start + trav_len) {
590 /* This can happen if traverse expects to be empty. */
593 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
594 "traverse did not terminate");
597 if (tinfo->op[tinfo->file][tinfo->i].type != OP_TDB_TRAVERSE)
598 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
599 "%s:%u:traverse terminated early");
601 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
602 avoid_deadlock = true;
605 /* Run any normal ops. */
606 tinfo->i = run_ops(tdb, tinfo->pre_fd, tinfo->filename, tinfo->op,
607 tinfo->file, tinfo->i+1, tinfo->start + trav_len,
610 /* We backed off, or we hit OP_TDB_TRAVERSE_END. */
611 if (tinfo->op[tinfo->file][tinfo->i].type != OP_TDB_TRAVERSE)
617 static unsigned op_traverse(struct tdb_context *tdb,
621 int (*traversefn)(struct tdb_context *,
622 tdb_traverse_func, void *),
626 struct traverse_info tinfo = { op, filename, file, pre_fd,
629 traversefn(tdb, nontrivial_traverse, &tinfo);
631 /* Traversing in wrong order can have strange effects: eg. if
632 * original traverse went A (delete A), B, we might do B
633 * (delete A). So if we have ops left over, we do it now. */
634 while (tinfo.i != start + op[file][start].group_len) {
635 if (op[file][tinfo.i].type == OP_TDB_TRAVERSE)
638 tinfo.i = run_ops(tdb, pre_fd, filename, op, file,
640 start + op[file][start].group_len,
647 static void break_out(int sig)
651 static __attribute__((noinline))
652 unsigned run_ops(struct tdb_context *tdb,
657 unsigned int start, unsigned int stop,
663 sa.sa_handler = break_out;
666 sigaction(SIGALRM, &sa, NULL);
667 for (i = start; i < stop; i++) {
668 if (!do_pre(tdb, filename, op, file, pre_fd, i, backoff))
671 switch (op[file][i].type) {
673 try(tdb_lockall(tdb), op[file][i].ret);
675 case OP_TDB_LOCKALL_MARK:
676 try(tdb_lockall_mark(tdb), op[file][i].ret);
678 case OP_TDB_LOCKALL_UNMARK:
679 try(tdb_lockall_unmark(tdb), op[file][i].ret);
681 case OP_TDB_LOCKALL_NONBLOCK:
682 unreliable(tdb_lockall_nonblock(tdb), op[file][i].ret,
683 tdb_lockall(tdb), tdb_unlockall(tdb));
685 case OP_TDB_UNLOCKALL:
686 try(tdb_unlockall(tdb), op[file][i].ret);
688 case OP_TDB_LOCKALL_READ:
689 try(tdb_lockall_read(tdb), op[file][i].ret);
691 case OP_TDB_LOCKALL_READ_NONBLOCK:
692 unreliable(tdb_lockall_read_nonblock(tdb),
694 tdb_lockall_read(tdb),
695 tdb_unlockall_read(tdb));
697 case OP_TDB_UNLOCKALL_READ:
698 try(tdb_unlockall_read(tdb), op[file][i].ret);
700 case OP_TDB_CHAINLOCK:
701 try(tdb_chainlock(tdb, op[file][i].key),
704 case OP_TDB_CHAINLOCK_NONBLOCK:
705 unreliable(tdb_chainlock_nonblock(tdb, op[file][i].key),
707 tdb_chainlock(tdb, op[file][i].key),
708 tdb_chainunlock(tdb, op[file][i].key));
710 case OP_TDB_CHAINLOCK_MARK:
711 try(tdb_chainlock_mark(tdb, op[file][i].key),
714 case OP_TDB_CHAINLOCK_UNMARK:
715 try(tdb_chainlock_unmark(tdb, op[file][i].key),
718 case OP_TDB_CHAINUNLOCK:
719 try(tdb_chainunlock(tdb, op[file][i].key),
722 case OP_TDB_CHAINLOCK_READ:
723 try(tdb_chainlock_read(tdb, op[file][i].key),
726 case OP_TDB_CHAINUNLOCK_READ:
727 try(tdb_chainunlock_read(tdb, op[file][i].key),
730 case OP_TDB_PARSE_RECORD:
731 try(tdb_parse_record(tdb, op[file][i].key, get_len,
736 try(tdb_exists(tdb, op[file][i].key), op[file][i].ret);
739 try(tdb_store(tdb, op[file][i].key, op[file][i].data,
744 try(tdb_append(tdb, op[file][i].key, op[file][i].data),
747 case OP_TDB_GET_SEQNUM:
748 try(tdb_get_seqnum(tdb), op[file][i].ret);
750 case OP_TDB_WIPE_ALL:
751 try(tdb_wipe_all(tdb), op[file][i].ret);
753 case OP_TDB_TRANSACTION_START:
754 try(tdb_transaction_start(tdb), op[file][i].ret);
756 case OP_TDB_TRANSACTION_CANCEL:
757 try(tdb_transaction_cancel(tdb), op[file][i].ret);
759 case OP_TDB_TRANSACTION_COMMIT:
760 try(tdb_transaction_commit(tdb), op[file][i].ret);
762 case OP_TDB_TRAVERSE_READ_START:
763 i = op_traverse(tdb, pre_fd, filename, file,
764 tdb_traverse_read, op, i);
766 case OP_TDB_TRAVERSE_START:
767 i = op_traverse(tdb, pre_fd, filename, file,
768 tdb_traverse, op, i);
770 case OP_TDB_TRAVERSE:
771 /* Terminate: we're in a traverse, and we've
774 case OP_TDB_TRAVERSE_END:
775 fail(filename[file], i+1, "unexpected end traverse");
776 /* FIXME: These must be treated like traverse. */
777 case OP_TDB_FIRSTKEY:
778 if (!key_eq(tdb_firstkey(tdb), op[file][i].data))
779 fail(filename[file], i+1, "bad firstkey");
782 if (!key_eq(tdb_nextkey(tdb, op[file][i].key),
784 fail(filename[file], i+1, "bad nextkey");
787 TDB_DATA f = tdb_fetch(tdb, op[file][i].key);
788 if (!key_eq(f, op[file][i].data))
789 fail(filename[file], i+1, "bad fetch %u",
794 try(tdb_delete(tdb, op[file][i].key), op[file][i].ret);
797 do_post(filename, op, file, i);
802 /* tdbtorture, in particular, can do a tdb_close with a transaction in
804 static struct op *maybe_cancel_transaction(const char *filename,
805 struct op *op, unsigned int *num)
807 unsigned int start = op_find_start(op, *num, OP_TDB_TRANSACTION_START);
810 char *words[] = { "<unknown>", "tdb_close", NULL };
811 add_op(filename, &op, *num, op[start].seqnum,
812 OP_TDB_TRANSACTION_CANCEL);
813 op_analyze_transaction(filename, op, *num, words);
819 static struct op *load_tracefile(const char *filename, unsigned int *num,
820 unsigned int *hashsize,
821 unsigned int *tdb_flags,
822 unsigned int *open_flags)
825 struct op *op = talloc_array(NULL, struct op, 1);
830 file = grab_file(NULL, filename, NULL);
832 err(1, "Reading %s", filename);
834 lines = strsplit(file, file, "\n", NULL);
836 errx(1, "%s is empty", filename);
838 words = strsplit(lines, lines[0], " ", NULL);
839 if (!streq(words[1], "tdb_open"))
840 fail(filename, 1, "does not start with tdb_open");
842 *hashsize = atoi(words[2]);
843 *tdb_flags = strtoul(words[3], NULL, 0);
844 *open_flags = strtoul(words[4], NULL, 0);
846 for (i = 1; lines[i]; i++) {
847 const struct op_table *opt;
849 words = strsplit(lines, lines[i], " ", NULL);
850 if (!words[0] || !words[1])
851 fail(filename, i+1, "Expected seqnum number and op");
853 opt = find_keyword(words[1], strlen(words[1]));
855 if (streq(words[1], "tdb_close")) {
858 "lines after tdb_close");
861 return maybe_cancel_transaction(filename,
864 fail(filename, i+1, "Unknown operation '%s'", words[1]);
867 add_op(filename, &op, i, atoi(words[0]), opt->type);
868 opt->enhance_op(filename, op, i, words);
871 fprintf(stderr, "%s:%u:last operation is not tdb_close: incomplete?",
875 return maybe_cancel_transaction(filename, op, num);
878 /* We remember all the keys we've ever seen, and who has them. */
881 unsigned int num_users;
882 struct op_desc *user;
885 static const TDB_DATA must_not_exist;
886 static const TDB_DATA must_exist;
887 static const TDB_DATA not_exists_or_empty;
889 /* NULL means doesn't care if it exists or not, &must_exist means
890 * it must exist but we don't care what, &must_not_exist means it must
891 * not exist, otherwise the data it needs. */
892 static const TDB_DATA *needs(const struct op *op)
895 /* FIXME: Pull forward deps, since we can deadlock */
896 case OP_TDB_CHAINLOCK:
897 case OP_TDB_CHAINLOCK_NONBLOCK:
898 case OP_TDB_CHAINLOCK_MARK:
899 case OP_TDB_CHAINLOCK_UNMARK:
900 case OP_TDB_CHAINUNLOCK:
901 case OP_TDB_CHAINLOCK_READ:
902 case OP_TDB_CHAINUNLOCK_READ:
906 if (op->append.pre.dsize == 0)
907 return ¬_exists_or_empty;
908 return &op->append.pre;
911 if (op->flag == TDB_INSERT) {
915 return &must_not_exist;
916 } else if (op->flag == TDB_MODIFY) {
918 return &must_not_exist;
922 /* No flags? Don't care */
929 return &must_not_exist;
931 case OP_TDB_PARSE_RECORD:
933 return &must_not_exist;
936 /* FIXME: handle these. */
937 case OP_TDB_WIPE_ALL:
938 case OP_TDB_FIRSTKEY:
940 case OP_TDB_GET_SEQNUM:
941 case OP_TDB_TRAVERSE:
942 case OP_TDB_TRANSACTION_COMMIT:
943 case OP_TDB_TRANSACTION_CANCEL:
944 case OP_TDB_TRANSACTION_START:
949 return &must_not_exist;
954 return &must_not_exist;
958 errx(1, "Unexpected op type %i", op->type);
963 static bool starts_transaction(const struct op *op)
965 return op->type == OP_TDB_TRANSACTION_START;
968 static bool in_transaction(const struct op op[], unsigned int i)
970 return op[i].group_start && starts_transaction(&op[op[i].group_start]);
973 static bool successful_transaction(const struct op *op)
975 return starts_transaction(op)
976 && op[op->group_len].type == OP_TDB_TRANSACTION_COMMIT;
979 static bool starts_traverse(const struct op *op)
981 return op->type == OP_TDB_TRAVERSE_START
982 || op->type == OP_TDB_TRAVERSE_READ_START;
985 static bool in_traverse(const struct op op[], unsigned int i)
987 return op[i].group_start && starts_traverse(&op[op[i].group_start]);
990 static bool starts_chainlock(const struct op *op)
992 return op->type == OP_TDB_CHAINLOCK_READ
993 || op->type == OP_TDB_CHAINLOCK;
996 static bool in_chainlock(const struct op op[], unsigned int i)
998 return op[i].group_start && starts_chainlock(&op[op[i].group_start]);
1001 /* What's the data after this op? pre if nothing changed. */
1002 static const TDB_DATA *gives(const TDB_DATA *key, const TDB_DATA *pre,
1003 const struct op *op)
1005 if (starts_transaction(op) || starts_chainlock(op)) {
1008 /* Cancelled transactions don't change anything. */
1009 if (op[op->group_len].type == OP_TDB_TRANSACTION_CANCEL)
1011 assert(op[op->group_len].type == OP_TDB_TRANSACTION_COMMIT
1012 || op[op->group_len].type == OP_TDB_CHAINUNLOCK_READ
1013 || op[op->group_len].type == OP_TDB_CHAINUNLOCK);
1015 for (i = 1; i < op->group_len; i++) {
1016 /* This skips nested transactions, too */
1017 if (key_eq(op[i].key, *key))
1018 pre = gives(key, pre, &op[i]);
1023 /* Failed ops don't change state of db. */
1027 if (op->type == OP_TDB_DELETE || op->type == OP_TDB_WIPE_ALL)
1030 if (op->type == OP_TDB_APPEND)
1031 return &op->append.post;
1033 if (op->type == OP_TDB_STORE)
1039 static struct keyinfo *hash_ops(struct op *op[], unsigned int num_ops[],
1042 unsigned int i, j, h;
1043 struct keyinfo *hash;
1045 hash = talloc_zero_array(op[0], struct keyinfo, total_keys*2);
1046 for (i = 0; i < num; i++) {
1047 for (j = 1; j < num_ops[i]; j++) {
1048 /* We can't do this on allocation, due to realloc. */
1049 list_head_init(&op[i][j].post);
1050 list_head_init(&op[i][j].pre);
1052 if (!op[i][j].key.dptr)
1055 h = hash_key(&op[i][j].key) % (total_keys * 2);
1056 while (!key_eq(hash[h].key, op[i][j].key)) {
1057 if (!hash[h].key.dptr) {
1058 hash[h].key = op[i][j].key;
1061 h = (h + 1) % (total_keys * 2);
1063 /* Might as well save some memory if we can. */
1064 if (op[i][j].key.dptr != hash[h].key.dptr) {
1065 talloc_free(op[i][j].key.dptr);
1066 op[i][j].key.dptr = hash[h].key.dptr;
1068 hash[h].user = talloc_realloc(hash, hash[h].user,
1070 hash[h].num_users+1);
1072 /* If it's in a transaction, it's the transaction which
1073 * matters from an analysis POV. */
1074 if (in_transaction(op[i], j)
1075 || in_chainlock(op[i], j)) {
1076 unsigned start = op[i][j].group_start;
1078 /* Don't include twice. */
1079 if (hash[h].num_users
1080 && hash[h].user[hash[h].num_users-1].file
1082 && hash[h].user[hash[h].num_users-1].op_num
1086 hash[h].user[hash[h].num_users].op_num = start;
1088 hash[h].user[hash[h].num_users].op_num = j;
1089 hash[h].user[hash[h].num_users].file = i;
1090 hash[h].num_users++;
1097 static bool satisfies(const TDB_DATA *key, const TDB_DATA *data,
1098 const struct op *op)
1100 const TDB_DATA *need = NULL;
1102 if (starts_transaction(op) || starts_chainlock(op)) {
1105 /* Look through for an op in this transaction which
1106 * needs this key. */
1107 for (i = 1; i < op->group_len; i++) {
1108 if (key_eq(op[i].key, *key)) {
1109 need = needs(&op[i]);
1110 /* tdb_exists() is special: there might be
1111 * something in the transaction with more
1112 * specific requirements. Other ops don't have
1113 * specific requirements (eg. store or delete),
1114 * but they change the value so we can't get
1115 * more information from future ops. */
1116 if (op[i].type != OP_TDB_EXISTS)
1123 /* Don't need anything? Cool. */
1127 /* This should be tdb_null or a real value. */
1128 assert(data != &must_exist);
1129 assert(data != &must_not_exist);
1130 assert(data != ¬_exists_or_empty);
1132 /* Must not exist? data must not exist. */
1133 if (need == &must_not_exist)
1134 return data == &tdb_null;
1137 if (need == &must_exist)
1138 return data != &tdb_null;
1140 /* Either noexist or empty. */
1141 if (need == ¬_exists_or_empty)
1142 return data->dsize == 0;
1144 /* Needs something specific. */
1145 return key_eq(*data, *need);
1148 static void move_to_front(struct op_desc res[], unsigned off, unsigned elem)
1151 struct op_desc tmp = res[elem];
1152 memmove(res + off + 1, res + off, (elem - off)*sizeof(res[0]));
1157 static void restore_to_pos(struct op_desc res[], unsigned off, unsigned elem)
1160 struct op_desc tmp = res[off];
1161 memmove(res + off, res + off + 1, (elem - off)*sizeof(res[0]));
1166 static bool sort_deps(char *filename[], struct op *op[],
1167 struct op_desc res[],
1168 unsigned off, unsigned num,
1169 const TDB_DATA *key, const TDB_DATA *data,
1170 unsigned num_files, unsigned fuzz)
1172 unsigned int i, files_done;
1174 bool done[num_files];
1176 /* None left? We're sorted. */
1180 /* Does this make sequence number go backwards? Allow a little fuzz. */
1182 int seqnum1 = op[res[off-1].file][res[off-1].op_num].seqnum;
1183 int seqnum2 = op[res[off].file][res[off].op_num].seqnum;
1185 if (seqnum1 - seqnum2 > (int)fuzz) {
1187 printf("Seqnum jump too far (%u -> %u)\n",
1194 memset(done, 0, sizeof(done));
1196 /* Since ops within a trace file are ordered, we just need to figure
1197 * out which file to try next. Since we don't take into account
1198 * inter-key relationships (which exist by virtue of trace file order),
1199 * we minimize the chance of harm by trying to keep in seqnum order. */
1200 for (files_done = 0, i = off; i < num && files_done < num_files; i++) {
1201 if (done[res[i].file])
1204 this_op = &op[res[i].file][res[i].op_num];
1206 /* Is what we have good enough for this op? */
1207 if (satisfies(key, data, this_op)) {
1208 move_to_front(res, off, i);
1209 if (sort_deps(filename, op, res, off+1, num,
1210 key, gives(key, data, this_op),
1213 restore_to_pos(res, off, i);
1215 done[res[i].file] = true;
1219 /* No combination worked. */
1223 static void check_dep_sorting(struct op_desc user[], unsigned num_users,
1228 unsigned minima[num_files];
1230 memset(minima, 0, sizeof(minima));
1231 for (i = 0; i < num_users; i++) {
1232 assert(minima[user[i].file] < user[i].op_num);
1233 minima[user[i].file] = user[i].op_num;
1238 /* All these ops happen on the same key. Which comes first?
1240 * This can happen both because read ops or failed write ops don't
1241 * change sequence number, and also due to race since we access the
1242 * number unlocked (the race can cause less detectable ordering problems,
1243 * in which case we'll deadlock and report: fix manually in that case).
1245 static void figure_deps(char *filename[], struct op *op[],
1246 const TDB_DATA *key, struct op_desc user[],
1247 unsigned num_users, unsigned num_files)
1249 /* We assume database starts empty. */
1250 const struct TDB_DATA *data = &tdb_null;
1253 /* We prefer to keep strict seqnum order if possible: it's the
1254 * most likely. We get more lax if that fails. */
1255 for (fuzz = 0; fuzz < 100; fuzz = (fuzz + 1)*2) {
1256 if (sort_deps(filename, op, user, 0, num_users, key, data,
1262 fail(filename[user[0].file], user[0].op_num+1,
1263 "Could not resolve inter-dependencies");
1265 check_dep_sorting(user, num_users, num_files);
1268 static void sort_ops(struct keyinfo hash[], char *filename[], struct op *op[],
1273 /* Gcc nexted function extension. How cool is this? */
1274 int compare_seqnum(const void *_a, const void *_b)
1276 const struct op_desc *a = _a, *b = _b;
1278 /* First, maintain order within any trace file. */
1279 if (a->file == b->file)
1280 return a->op_num - b->op_num;
1282 /* Otherwise, arrange by seqnum order. */
1283 if (op[a->file][a->op_num].seqnum !=
1284 op[b->file][b->op_num].seqnum)
1285 return op[a->file][a->op_num].seqnum
1286 - op[b->file][b->op_num].seqnum;
1288 /* Cancelled transactions are assumed to happen first. */
1289 if (starts_transaction(&op[a->file][a->op_num])
1290 && !successful_transaction(&op[a->file][a->op_num]))
1292 if (starts_transaction(&op[b->file][b->op_num])
1293 && !successful_transaction(&op[b->file][b->op_num]))
1300 /* Now sort into seqnum order. */
1301 for (h = 0; h < total_keys * 2; h++) {
1302 struct op_desc *user = hash[h].user;
1304 qsort(user, hash[h].num_users, sizeof(user[0]), compare_seqnum);
1305 figure_deps(filename, op, &hash[h].key, user, hash[h].num_users,
1310 static int destroy_depend(struct depend *dep)
1312 list_del(&dep->pre_list);
1313 list_del(&dep->post_list);
1317 static void add_dependency(void *ctx,
1320 const struct op_desc *needs,
1321 const struct op_desc *prereq)
1325 /* We don't depend on ourselves. */
1326 if (needs->file == prereq->file) {
1327 assert(prereq->op_num < needs->op_num);
1332 printf("%s:%u: depends on %s:%u\n",
1333 filename[needs->file], needs->op_num+1,
1334 filename[prereq->file], prereq->op_num+1);
1337 dep = talloc(ctx, struct depend);
1338 dep->needs = *needs;
1339 dep->prereq = *prereq;
1341 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1342 /* If something in a traverse depends on something in another
1343 * traverse/transaction, it creates a dependency between the
1345 if ((in_traverse(op[prereq->file], prereq->op_num)
1346 && (starts_transaction(&op[needs->file][needs->op_num])
1347 || starts_traverse(&op[needs->file][needs->op_num])))
1348 || (in_traverse(op[needs->file], needs->op_num)
1349 && (starts_transaction(&op[prereq->file][prereq->op_num])
1350 || starts_traverse(&op[prereq->file][prereq->op_num])))) {
1353 /* We are satisfied by end of group. */
1354 start = op[prereq->file][prereq->op_num].group_start;
1355 dep->prereq.op_num = start + op[prereq->file][start].group_len;
1356 /* And we need that done by start of our group. */
1357 dep->needs.op_num = op[needs->file][needs->op_num].group_start;
1360 /* There is also this case:
1361 * <traverse> <read foo> ...
1362 * <transaction> ... </transaction> <create foo>
1363 * Where if we start the traverse then wait, we could block
1364 * the transaction and deadlock.
1366 * We try to address this by ensuring that where seqnum indicates it's
1367 * possible, we wait for <create foo> before *starting* traverse.
1369 else if (in_traverse(op[needs->file], needs->op_num)) {
1370 struct op *need = &op[needs->file][needs->op_num];
1371 if (op[needs->file][need->group_start].seqnum >
1372 op[prereq->file][prereq->op_num].seqnum) {
1373 dep->needs.op_num = need->group_start;
1378 /* If you depend on a transaction or chainlock, you actually
1379 * depend on it ending. */
1380 if (starts_transaction(&op[prereq->file][dep->prereq.op_num])
1381 || starts_chainlock(&op[prereq->file][dep->prereq.op_num])) {
1383 += op[dep->prereq.file][dep->prereq.op_num].group_len;
1385 printf("-> Actually end of transaction %s:%u\n",
1386 filename[dep->prereq->file], dep->prereq->op_num+1);
1389 /* We should never create a dependency from middle of
1391 assert(!in_transaction(op[prereq->file], dep->prereq.op_num)
1392 || op[prereq->file][dep->prereq.op_num].type
1393 == OP_TDB_TRANSACTION_COMMIT
1394 || op[prereq->file][dep->prereq.op_num].type
1395 == OP_TDB_TRANSACTION_CANCEL);
1397 list_add(&op[dep->prereq.file][dep->prereq.op_num].post,
1399 list_add(&op[dep->needs.file][dep->needs.op_num].pre,
1401 talloc_set_destructor(dep, destroy_depend);
1404 static bool changes_db(const TDB_DATA *key, const struct op *op)
1406 return gives(key, NULL, op) != NULL;
1409 static void depend_on_previous(struct op *op[],
1412 struct op_desc user[],
1422 if (prev == i - 1) {
1423 /* Just depend on previous. */
1424 add_dependency(NULL, op, filename, &user[i], &user[prev]);
1428 /* We have to wait for the readers. Find last one in *each* file. */
1429 memset(deps, 0, sizeof(deps));
1430 deps[user[i].file] = true;
1431 for (j = i - 1; j > prev; j--) {
1432 if (!deps[user[j].file]) {
1433 add_dependency(NULL, op, filename, &user[i], &user[j]);
1434 deps[user[j].file] = true;
1439 /* This is simple, but not complete. We don't take into account
1440 * indirect dependencies. */
1441 static void optimize_dependencies(struct op *op[], unsigned int num_ops[],
1446 /* There can only be one real dependency on each file */
1447 for (i = 0; i < num; i++) {
1448 for (j = 1; j < num_ops[i]; j++) {
1449 struct depend *dep, *next;
1450 struct depend *prev[num];
1452 memset(prev, 0, sizeof(prev));
1454 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1455 if (!prev[dep->prereq.file]) {
1456 prev[dep->prereq.file] = dep;
1459 if (prev[dep->prereq.file]->prereq.op_num
1460 < dep->prereq.op_num) {
1461 talloc_free(prev[dep->prereq.file]);
1462 prev[dep->prereq.file] = dep;
1469 for (i = 0; i < num; i++) {
1472 for (j = 0; j < num; j++)
1475 for (j = 1; j < num_ops[i]; j++) {
1476 struct depend *dep, *next;
1478 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1479 if (deps[dep->prereq.file]
1480 >= (int)dep->prereq.op_num)
1483 deps[dep->prereq.file]
1484 = dep->prereq.op_num;
1490 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1491 /* Force an order among the traversals, so they don't deadlock (as much) */
1492 static void make_traverse_depends(char *filename[],
1493 struct op *op[], unsigned int num_ops[],
1496 unsigned int i, num_traversals = 0;
1498 struct op_desc *desc;
1500 /* Sort by which one runs first. */
1501 int compare_traverse_desc(const void *_a, const void *_b)
1503 const struct op_desc *da = _a, *db = _b;
1504 const struct op *a = &op[da->file][da->op_num],
1505 *b = &op[db->file][db->op_num];
1507 if (a->seqnum != b->seqnum)
1508 return a->seqnum - b->seqnum;
1510 /* If they have same seqnum, it means one didn't make any
1511 * changes. Thus sort by end in that case. */
1512 return a[a->group_len].seqnum - b[b->group_len].seqnum;
1515 desc = talloc_array(NULL, struct op_desc, 1);
1518 for (i = 0; i < num; i++) {
1519 for (j = 1; j < num_ops[i]; j++) {
1520 /* Traverse start (ignore those in
1521 * transactions; they're already covered by
1522 * transaction dependencies). */
1523 if (starts_traverse(&op[i][j])
1524 && !in_transaction(op[i], j)) {
1525 desc = talloc_realloc(NULL, desc,
1528 desc[num_traversals].file = i;
1529 desc[num_traversals].op_num = j;
1534 qsort(desc, num_traversals, sizeof(desc[0]), compare_traverse_desc);
1536 for (i = 1; i < num_traversals; i++) {
1537 const struct op *prev = &op[desc[i-1].file][desc[i-1].op_num];
1538 const struct op *curr = &op[desc[i].file][desc[i].op_num];
1540 /* Read traverses don't depend on each other (read lock). */
1541 if (prev->type == OP_TDB_TRAVERSE_READ_START
1542 && curr->type == OP_TDB_TRAVERSE_READ_START)
1545 /* Only make dependency if it's clear. */
1546 if (compare_traverse_desc(&desc[i], &desc[i-1])) {
1547 /* i depends on end of traverse i-1. */
1548 struct op_desc end = desc[i-1];
1549 end.op_num += prev->group_len;
1550 add_dependency(NULL, op, filename, &desc[i], &end);
1557 static void derive_dependencies(char *filename[],
1558 struct op *op[], unsigned int num_ops[],
1561 struct keyinfo *hash;
1564 /* Create hash table for faster key lookup. */
1565 hash = hash_ops(op, num_ops, num);
1567 /* Sort them by sequence number. */
1568 sort_ops(hash, filename, op, num);
1570 /* Create dependencies back to the last change, rather than
1571 * creating false dependencies by naively making each one
1572 * depend on the previous. This has two purposes: it makes
1573 * later optimization simpler, and it also avoids deadlock with
1574 * same sequence number ops inside traversals (if one
1575 * traversal doesn't write anything, two ops can have the same
1576 * sequence number yet we can create a traversal dependency
1577 * the other way). */
1578 for (h = 0; h < total_keys * 2; h++) {
1581 if (hash[h].num_users < 2)
1584 for (i = 0; i < hash[h].num_users; i++) {
1585 if (changes_db(&hash[h].key, &op[hash[h].user[i].file]
1586 [hash[h].user[i].op_num])) {
1587 depend_on_previous(op, filename, num,
1588 hash[h].user, i, prev);
1590 } else if (prev >= 0)
1591 add_dependency(hash, op, filename,
1593 &hash[h].user[prev]);
1597 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1598 make_traverse_depends(filename, op, num_ops, num);
1601 optimize_dependencies(op, num_ops, num);
1604 int main(int argc, char *argv[])
1606 struct timeval start, end;
1607 unsigned int i, num_ops[argc], hashsize[argc], tdb_flags[argc], open_flags[argc];
1608 struct op *op[argc];
1614 errx(1, "Usage: %s <tdbfile> <tracefile>...", argv[0]);
1616 pipes = talloc_array(NULL, struct pipe, argc - 2);
1617 for (i = 0; i < argc - 2; i++) {
1618 printf("Loading tracefile %s...", argv[2+i]);
1620 op[i] = load_tracefile(argv[2+i], &num_ops[i], &hashsize[i],
1621 &tdb_flags[i], &open_flags[i]);
1622 if (pipe(pipes[i].fd) != 0)
1623 err(1, "creating pipe");
1627 printf("Calculating inter-dependencies...");
1629 derive_dependencies(argv+2, op, num_ops, i);
1632 /* Don't fork for single arg case: simple debugging. */
1634 struct tdb_context *tdb;
1635 tdb = tdb_open_ex(argv[1], hashsize[0], tdb_flags[0]|TDB_NOSYNC,
1636 open_flags[0], 0600, NULL, hash_key);
1637 printf("Single threaded run...");
1640 run_ops(tdb, pipes[0].fd[0], argv+2, op, 0, 1, num_ops[0],
1642 check_deps(argv[2], op[0], num_ops[0]);
1649 err(1, "creating pipe");
1651 for (i = 0; i < argc - 2; i++) {
1652 struct tdb_context *tdb;
1656 err(1, "fork failed");
1659 tdb = tdb_open_ex(argv[1], hashsize[i],
1660 tdb_flags[i]|TDB_NOSYNC,
1661 open_flags[i], 0600, NULL, hash_key);
1663 err(1, "Opening tdb %s", argv[1]);
1665 /* This catches parent exiting. */
1666 if (read(fds[0], &c, 1) != 1)
1668 run_ops(tdb, pipes[i].fd[0], argv+2, op, i, 1,
1670 check_deps(argv[2+i], op[i], num_ops[i]);
1677 /* Let everything settle. */
1680 printf("Starting run...");
1682 gettimeofday(&start, NULL);
1683 /* Tell them all to go! Any write of sufficient length will do. */
1684 if (write(fds[1], hashsize, i) != i)
1685 err(1, "Writing to wakeup pipe");
1687 for (i = 0; i < argc - 2; i++) {
1690 if (!WIFEXITED(status)) {
1691 warnx("Child died with signal %i", WTERMSIG(status));
1693 } else if (WEXITSTATUS(status) != 0)
1694 /* Assume child spat out error. */
1700 gettimeofday(&end, NULL);
1703 end.tv_sec -= start.tv_sec;
1704 printf("Time replaying: %lu usec\n",
1705 end.tv_sec * 1000000UL + (end.tv_usec - start.tv_usec));