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;
155 static unsigned char hex_char(const char *filename, unsigned int line, char c)
158 if (c >= 'A' && c <= 'F')
160 if (c >= '0' && c <= '9')
162 fail(filename, line, "invalid hex character '%c'", c);
165 /* TDB data is <size>:<%02x>* */
166 static TDB_DATA make_tdb_data(const void *ctx,
167 const char *filename, unsigned int line,
174 if (streq(word, "NULL"))
177 data.dsize = atoi(word);
178 data.dptr = talloc_array(ctx, unsigned char, data.dsize);
179 p = strchr(word, ':');
181 fail(filename, line, "invalid tdb data '%s'", word);
183 for (i = 0; i < data.dsize; i++)
184 data.dptr[i] = hex_char(filename, line, p[i*2])*16
185 + hex_char(filename, line, p[i*2+1]);
190 static void add_op(const char *filename, struct op **op, unsigned int i,
191 unsigned int serial, enum op_type type)
194 *op = talloc_realloc(NULL, *op, struct op, i+1);
197 new->serial = serial;
199 new->group_start = 0;
202 static void op_add_nothing(const char *filename,
203 struct op op[], unsigned int op_num, char *words[])
206 fail(filename, op_num+1, "Expected no arguments");
207 op[op_num].key = tdb_null;
210 static void op_add_key(const char *filename,
211 struct op op[], unsigned int op_num, char *words[])
213 if (words[2] == NULL || words[3])
214 fail(filename, op_num+1, "Expected just a key");
216 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
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 /* We don't record the keys or data for a traverse, as we don't use them. */
247 static void op_add_traverse(const char *filename,
248 struct op op[], unsigned int op_num, char *words[])
250 if (!words[2] || !words[3] || !words[4] || words[5]
251 || !streq(words[3], "="))
252 fail(filename, op_num+1, "Expected <key> = <data>");
253 op[op_num].key = tdb_null;
256 /* <serial> tdb_store <rec> <rec> <flag> = <ret> */
257 static void op_add_store(const char *filename,
258 struct op op[], unsigned int op_num, char *words[])
260 if (!words[2] || !words[3] || !words[4] || !words[5] || !words[6]
261 || words[7] || !streq(words[5], "="))
262 fail(filename, op_num+1, "Expect <key> <data> <flag> = <ret>");
264 op[op_num].flag = strtoul(words[4], NULL, 0);
265 op[op_num].ret = atoi(words[6]);
266 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
267 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
271 /* <serial> tdb_append <rec> <rec> = <rec> */
272 static void op_add_append(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 || !streq(words[4], "="))
277 fail(filename, op_num+1, "Expect <key> <data> = <rec>");
279 op[op_num].key = make_tdb_data(op, filename, op_num+1, words[2]);
280 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[3]);
282 op[op_num].append.post
283 = make_tdb_data(op, filename, op_num+1, words[5]);
285 /* By subtraction, figure out what previous data was. */
286 op[op_num].append.pre.dptr = op[op_num].append.post.dptr;
287 op[op_num].append.pre.dsize
288 = op[op_num].append.post.dsize - op[op_num].data.dsize;
292 /* <serial> tdb_get_seqnum = <ret> */
293 static void op_add_seqnum(const char *filename,
294 struct op op[], unsigned int op_num, char *words[])
296 if (!words[2] || !words[3] || words[4] || !streq(words[2], "="))
297 fail(filename, op_num+1, "Expect = <ret>");
299 op[op_num].key = tdb_null;
300 op[op_num].ret = atoi(words[3]);
303 static void op_add_traverse_start(const char *filename,
305 unsigned int op_num, char *words[])
308 fail(filename, op_num+1, "Expect no arguments");
310 op[op_num].key = tdb_null;
311 op[op_num].group_len = 0;
314 static void op_add_transaction(const char *filename, struct op op[],
315 unsigned int op_num, char *words[])
318 fail(filename, op_num+1, "Expect no arguments");
320 op[op_num].key = tdb_null;
321 op[op_num].group_len = 0;
324 static void op_add_chainlock(const char *filename,
325 struct op op[], unsigned int op_num, char *words[])
327 if (words[2] == NULL || words[3])
328 fail(filename, op_num+1, "Expected just a key");
330 /* A chainlock key isn't a key in the normal sense; it doesn't
331 * have to be in the db at all. Also, we don't want to hash this op. */
332 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[2]);
333 op[op_num].key = tdb_null;
334 op[op_num].group_len = 0;
337 static void op_add_chainlock_ret(const char *filename,
338 struct op op[], unsigned int op_num,
341 if (!words[2] || !words[3] || !words[4] || words[5]
342 || !streq(words[3], "="))
343 fail(filename, op_num+1, "Expected <key> = <ret>");
344 op[op_num].ret = atoi(words[4]);
345 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[2]);
346 op[op_num].key = tdb_null;
347 op[op_num].group_len = 0;
351 static int op_find_start(struct op op[], unsigned int op_num, enum op_type type)
355 for (i = op_num-1; i > 0; i--) {
356 if (op[i].op == type && !op[i].group_len)
362 static void op_analyze_transaction(const char *filename,
363 struct op op[], unsigned int op_num,
366 unsigned int start, i;
368 op[op_num].key = tdb_null;
371 fail(filename, op_num+1, "Expect no arguments");
373 start = op_find_start(op, op_num, OP_TDB_TRANSACTION_START);
375 fail(filename, op_num+1, "no transaction start found");
377 op[start].group_len = op_num - start;
379 /* This rolls in nested transactions. I think that's right. */
380 for (i = start; i <= op_num; i++)
381 op[i].group_start = start;
384 /* We treat chainlocks a lot like transactions, even though that's overkill */
385 static void op_analyze_chainlock(const char *filename,
386 struct op op[], unsigned int op_num,
389 unsigned int i, start;
391 if (words[2] == NULL || words[3])
392 fail(filename, op_num+1, "Expected just a key");
394 op[op_num].data = make_tdb_data(op, filename, op_num+1, words[2]);
395 op[op_num].key = tdb_null;
398 start = op_find_start(op, op_num, OP_TDB_CHAINLOCK);
400 start = op_find_start(op, op_num, OP_TDB_CHAINLOCK_READ);
402 fail(filename, op_num+1, "no initial chainlock found");
404 /* FIXME: We'd have to do something clever to make this work
406 if (!key_eq(op[start].data, op[op_num].data))
407 fail(filename, op_num+1, "nested chainlock calls?");
409 op[start].group_len = op_num - start;
410 for (i = start; i <= op_num; i++)
411 op[i].group_start = start;
414 static void op_analyze_traverse(const char *filename,
415 struct op op[], unsigned int op_num,
420 op[op_num].key = tdb_null;
422 /* = %u means traverse function terminated. */
424 if (!streq(words[2], "=") || !words[3] || words[4])
425 fail(filename, op_num+1, "expect = <num>");
426 op[op_num].ret = atoi(words[3]);
430 start = op_find_start(op, op_num, OP_TDB_TRAVERSE_START);
432 start = op_find_start(op, op_num, OP_TDB_TRAVERSE_READ_START);
434 fail(filename, op_num+1, "no traversal start found");
436 op[start].group_len = op_num - start;
438 /* Don't roll in nested traverse/chainlock */
439 for (i = start; i <= op_num; i++)
440 if (!op[i].group_start)
441 op[i].group_start = start;
444 /* Keep -Wmissing-declarations happy: */
445 const struct op_table *
446 find_keyword (register const char *str, register unsigned int len);
448 #include "keywords.c"
451 /* We can have more than one */
452 struct list_node pre_list;
453 struct list_node post_list;
454 unsigned int needs_file;
455 unsigned int needs_opnum;
456 unsigned int satisfies_file;
457 unsigned int satisfies_opnum;
460 static void check_deps(const char *filename, struct op op[], unsigned int num)
465 for (i = 1; i < num; i++)
466 if (!list_empty(&op[i].pre))
467 fail(filename, i+1, "Still has dependencies");
471 static void dump_pre(char *filename[], struct op *op[],
472 unsigned int file, unsigned int i)
476 printf("%s:%u (%u) still waiting for:\n", filename[file], i+1,
478 list_for_each(&op[file][i].pre, dep, pre_list)
479 printf(" %s:%u (%u)\n",
480 filename[dep->satisfies_file], dep->satisfies_opnum+1,
481 op[dep->satisfies_file][dep->satisfies_opnum].serial);
482 check_deps(filename[file], op[file], i);
485 /* We simply read/write pointers, since we all are children. */
486 static bool do_pre(struct tdb_context *tdb,
487 char *filename[], struct op *op[],
488 unsigned int file, int pre_fd, unsigned int i,
491 while (!list_empty(&op[file][i].pre)) {
495 printf("%s:%u:waiting for pre\n", filename[file], i+1);
502 while (read(pre_fd, &dep, sizeof(dep)) != sizeof(dep)) {
503 if (errno == EINTR) {
505 warnx("%s:%u:avoiding deadlock",
506 filename[file], i+1);
509 dump_pre(filename, op, file, i);
512 errx(1, "Reading from pipe");
517 printf("%s:%u:got pre %u from %s:%u\n", filename[file], i+1,
518 dep->needs_opnum+1, filename[dep->satisfies_file],
519 dep->satisfies_opnum+1);
522 /* This could be any op, not just this one. */
528 static void do_post(char *filename[], struct op *op[],
529 unsigned int file, unsigned int i)
533 list_for_each(&op[file][i].post, dep, post_list) {
535 printf("%s:%u:sending to file %s:%u\n", filename[file], i+1,
536 filename[dep->needs_file], dep->needs_opnum+1);
538 if (write(pipes[dep->needs_file].fd[1], &dep, sizeof(dep))
540 err(1, "%s:%u failed to tell file %s",
541 filename[file], i+1, filename[dep->needs_file]);
545 static int get_len(TDB_DATA key, TDB_DATA data, void *private_data)
550 static unsigned run_ops(struct tdb_context *tdb,
555 unsigned int start, unsigned int stop,
558 struct traverse_info {
567 /* More complex. Just do whatever's they did at the n'th entry. */
568 static int nontrivial_traverse(struct tdb_context *tdb,
569 TDB_DATA key, TDB_DATA data,
572 struct traverse_info *tinfo = _tinfo;
573 unsigned int trav_len = tinfo->op[tinfo->file][tinfo->start].group_len;
574 bool avoid_deadlock = false;
576 if (tinfo->i == tinfo->start + trav_len) {
577 /* This can happen if traverse expects to be empty. */
580 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
581 "traverse did not terminate");
584 if (tinfo->op[tinfo->file][tinfo->i].op != OP_TDB_TRAVERSE)
585 fail(tinfo->filename[tinfo->file], tinfo->start + 1,
586 "%s:%u:traverse terminated early");
588 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
589 avoid_deadlock = true;
592 /* Run any normal ops. */
593 tinfo->i = run_ops(tdb, tinfo->pre_fd, tinfo->filename, tinfo->op,
594 tinfo->file, tinfo->i+1, tinfo->start + trav_len,
597 /* We backed off, or we hit OP_TDB_TRAVERSE_END. */
598 if (tinfo->op[tinfo->file][tinfo->i].op != OP_TDB_TRAVERSE)
604 static unsigned op_traverse(struct tdb_context *tdb,
608 int (*traversefn)(struct tdb_context *,
609 tdb_traverse_func, void *),
613 struct traverse_info tinfo = { op, filename, file, pre_fd,
616 traversefn(tdb, nontrivial_traverse, &tinfo);
618 /* Traversing in wrong order can have strange effects: eg. if
619 * original traverse went A (delete A), B, we might do B
620 * (delete A). So if we have ops left over, we do it now. */
621 while (tinfo.i != start + op[file][start].group_len) {
622 if (op[file][tinfo.i].op == OP_TDB_TRAVERSE)
625 tinfo.i = run_ops(tdb, pre_fd, filename, op, file,
627 start + op[file][start].group_len,
634 static void break_out(int sig)
638 static __attribute__((noinline))
639 unsigned run_ops(struct tdb_context *tdb,
644 unsigned int start, unsigned int stop,
650 sa.sa_handler = break_out;
653 sigaction(SIGALRM, &sa, NULL);
654 for (i = start; i < stop; i++) {
655 if (!do_pre(tdb, filename, op, file, pre_fd, i, backoff))
658 switch (op[file][i].op) {
660 try(tdb_lockall(tdb), op[file][i].ret);
662 case OP_TDB_LOCKALL_MARK:
663 try(tdb_lockall_mark(tdb), op[file][i].ret);
665 case OP_TDB_LOCKALL_UNMARK:
666 try(tdb_lockall_unmark(tdb), op[file][i].ret);
668 case OP_TDB_LOCKALL_NONBLOCK:
669 unreliable(tdb_lockall_nonblock(tdb), op[file][i].ret,
670 tdb_lockall(tdb), tdb_unlockall(tdb));
672 case OP_TDB_UNLOCKALL:
673 try(tdb_unlockall(tdb), op[file][i].ret);
675 case OP_TDB_LOCKALL_READ:
676 try(tdb_lockall_read(tdb), op[file][i].ret);
678 case OP_TDB_LOCKALL_READ_NONBLOCK:
679 unreliable(tdb_lockall_read_nonblock(tdb),
681 tdb_lockall_read(tdb),
682 tdb_unlockall_read(tdb));
684 case OP_TDB_UNLOCKALL_READ:
685 try(tdb_unlockall_read(tdb), op[file][i].ret);
687 case OP_TDB_CHAINLOCK:
688 try(tdb_chainlock(tdb, op[file][i].key),
691 case OP_TDB_CHAINLOCK_NONBLOCK:
692 unreliable(tdb_chainlock_nonblock(tdb, op[file][i].key),
694 tdb_chainlock(tdb, op[file][i].key),
695 tdb_chainunlock(tdb, op[file][i].key));
697 case OP_TDB_CHAINLOCK_MARK:
698 try(tdb_chainlock_mark(tdb, op[file][i].key),
701 case OP_TDB_CHAINLOCK_UNMARK:
702 try(tdb_chainlock_unmark(tdb, op[file][i].key),
705 case OP_TDB_CHAINUNLOCK:
706 try(tdb_chainunlock(tdb, op[file][i].key),
709 case OP_TDB_CHAINLOCK_READ:
710 try(tdb_chainlock_read(tdb, op[file][i].key),
713 case OP_TDB_CHAINUNLOCK_READ:
714 try(tdb_chainunlock_read(tdb, op[file][i].key),
717 case OP_TDB_PARSE_RECORD:
718 try(tdb_parse_record(tdb, op[file][i].key, get_len,
723 try(tdb_exists(tdb, op[file][i].key), op[file][i].ret);
726 try(tdb_store(tdb, op[file][i].key, op[file][i].data,
731 try(tdb_append(tdb, op[file][i].key, op[file][i].data),
734 case OP_TDB_GET_SEQNUM:
735 try(tdb_get_seqnum(tdb), op[file][i].ret);
737 case OP_TDB_WIPE_ALL:
738 try(tdb_wipe_all(tdb), op[file][i].ret);
740 case OP_TDB_TRANSACTION_START:
741 try(tdb_transaction_start(tdb), op[file][i].ret);
743 case OP_TDB_TRANSACTION_CANCEL:
744 try(tdb_transaction_cancel(tdb), op[file][i].ret);
746 case OP_TDB_TRANSACTION_COMMIT:
747 try(tdb_transaction_commit(tdb), op[file][i].ret);
749 case OP_TDB_TRAVERSE_READ_START:
750 i = op_traverse(tdb, pre_fd, filename, file,
751 tdb_traverse_read, op, i);
753 case OP_TDB_TRAVERSE_START:
754 i = op_traverse(tdb, pre_fd, filename, file,
755 tdb_traverse, op, i);
757 case OP_TDB_TRAVERSE:
758 /* Terminate: we're in a traverse, and we've
761 case OP_TDB_TRAVERSE_END:
762 fail(filename[file], i+1, "unexpected end traverse");
763 /* FIXME: These must be treated like traverse. */
764 case OP_TDB_FIRSTKEY:
765 if (!key_eq(tdb_firstkey(tdb), op[file][i].data))
766 fail(filename[file], i+1, "bad firstkey");
769 if (!key_eq(tdb_nextkey(tdb, op[file][i].key),
771 fail(filename[file], i+1, "bad nextkey");
774 TDB_DATA f = tdb_fetch(tdb, op[file][i].key);
775 if (!key_eq(f, op[file][i].data))
776 fail(filename[file], i+1, "bad fetch %u",
781 try(tdb_delete(tdb, op[file][i].key), op[file][i].ret);
784 do_post(filename, op, file, i);
789 /* tdbtorture, in particular, can do a tdb_close with a transaction in
791 static struct op *maybe_cancel_transaction(const char *filename,
792 struct op *op, unsigned int *num)
794 unsigned int start = op_find_start(op, *num, OP_TDB_TRANSACTION_START);
797 char *words[] = { "<unknown>", "tdb_close", NULL };
798 add_op(filename, &op, *num, op[start].serial,
799 OP_TDB_TRANSACTION_CANCEL);
800 op_analyze_transaction(filename, op, *num, words);
806 static struct op *load_tracefile(const char *filename, unsigned int *num,
807 unsigned int *hashsize,
808 unsigned int *tdb_flags,
809 unsigned int *open_flags)
812 struct op *op = talloc_array(NULL, struct op, 1);
817 file = grab_file(NULL, filename, NULL);
819 err(1, "Reading %s", filename);
821 lines = strsplit(file, file, "\n", NULL);
823 errx(1, "%s is empty", filename);
825 words = strsplit(lines, lines[0], " ", NULL);
826 if (!streq(words[1], "tdb_open"))
827 fail(filename, 1, "does not start with tdb_open");
829 *hashsize = atoi(words[2]);
830 *tdb_flags = strtoul(words[3], NULL, 0);
831 *open_flags = strtoul(words[4], NULL, 0);
833 for (i = 1; lines[i]; i++) {
834 const struct op_table *opt;
836 words = strsplit(lines, lines[i], " ", NULL);
837 if (!words[0] || !words[1])
838 fail(filename, i+1, "Expected serial number and op");
840 opt = find_keyword(words[1], strlen(words[1]));
842 if (streq(words[1], "tdb_close")) {
845 "lines after tdb_close");
848 return maybe_cancel_transaction(filename,
851 fail(filename, i+1, "Unknown operation '%s'", words[1]);
854 add_op(filename, &op, i, atoi(words[0]), opt->type);
855 opt->enhance_op(filename, op, i, words);
858 fprintf(stderr, "%s:%u:last operation is not tdb_close: incomplete?",
862 return maybe_cancel_transaction(filename, op, num);
865 /* We remember all the keys we've ever seen, and who has them. */
873 unsigned int num_users;
874 struct key_user *user;
877 static const TDB_DATA must_not_exist;
878 static const TDB_DATA must_exist;
879 static const TDB_DATA not_exists_or_empty;
881 /* NULL means doesn't care if it exists or not, &must_exist means
882 * it must exist but we don't care what, &must_not_exist means it must
883 * not exist, otherwise the data it needs. */
884 static const TDB_DATA *needs(const struct op *op)
887 /* FIXME: Pull forward deps, since we can deadlock */
888 case OP_TDB_CHAINLOCK:
889 case OP_TDB_CHAINLOCK_NONBLOCK:
890 case OP_TDB_CHAINLOCK_MARK:
891 case OP_TDB_CHAINLOCK_UNMARK:
892 case OP_TDB_CHAINUNLOCK:
893 case OP_TDB_CHAINLOCK_READ:
894 case OP_TDB_CHAINUNLOCK_READ:
898 if (op->append.pre.dsize == 0)
899 return ¬_exists_or_empty;
900 return &op->append.pre;
903 if (op->flag == TDB_INSERT) {
907 return &must_not_exist;
908 } else if (op->flag == TDB_MODIFY) {
910 return &must_not_exist;
914 /* No flags? Don't care */
921 return &must_not_exist;
923 case OP_TDB_PARSE_RECORD:
925 return &must_not_exist;
928 /* FIXME: handle these. */
929 case OP_TDB_WIPE_ALL:
930 case OP_TDB_FIRSTKEY:
932 case OP_TDB_GET_SEQNUM:
933 case OP_TDB_TRAVERSE:
934 case OP_TDB_TRANSACTION_COMMIT:
935 case OP_TDB_TRANSACTION_CANCEL:
936 case OP_TDB_TRANSACTION_START:
941 return &must_not_exist;
946 return &must_not_exist;
950 errx(1, "Unexpected op %i", op->op);
955 static bool starts_transaction(const struct op *op)
957 return op->op == OP_TDB_TRANSACTION_START;
960 static bool in_transaction(const struct op op[], unsigned int i)
962 return op[i].group_start && starts_transaction(&op[op[i].group_start]);
965 static bool successful_transaction(const struct op *op)
967 return starts_transaction(op)
968 && op[op->group_len].op == OP_TDB_TRANSACTION_COMMIT;
971 static bool starts_traverse(const struct op *op)
973 return op->op == OP_TDB_TRAVERSE_START
974 || op->op == OP_TDB_TRAVERSE_READ_START;
977 static bool in_traverse(const struct op op[], unsigned int i)
979 return op[i].group_start && starts_traverse(&op[op[i].group_start]);
982 static bool starts_chainlock(const struct op *op)
984 return op->op == OP_TDB_CHAINLOCK_READ || op->op == OP_TDB_CHAINLOCK;
987 static bool in_chainlock(const struct op op[], unsigned int i)
989 return op[i].group_start && starts_chainlock(&op[op[i].group_start]);
992 /* What's the data after this op? pre if nothing changed. */
993 static const TDB_DATA *gives(const TDB_DATA *key, const TDB_DATA *pre,
996 if (starts_transaction(op) || starts_chainlock(op)) {
999 /* Cancelled transactions don't change anything. */
1000 if (op[op->group_len].op == OP_TDB_TRANSACTION_CANCEL)
1002 assert(op[op->group_len].op == OP_TDB_TRANSACTION_COMMIT
1003 || op[op->group_len].op == OP_TDB_CHAINUNLOCK_READ
1004 || op[op->group_len].op == OP_TDB_CHAINUNLOCK);
1006 for (i = 1; i < op->group_len; i++) {
1007 /* This skips nested transactions, too */
1008 if (key_eq(op[i].key, *key))
1009 pre = gives(key, pre, &op[i]);
1014 /* Failed ops don't change state of db. */
1018 if (op->op == OP_TDB_DELETE || op->op == OP_TDB_WIPE_ALL)
1021 if (op->op == OP_TDB_APPEND)
1022 return &op->append.post;
1024 if (op->op == OP_TDB_STORE)
1030 static struct keyinfo *hash_ops(struct op *op[], unsigned int num_ops[],
1033 unsigned int i, j, h;
1034 struct keyinfo *hash;
1036 hash = talloc_zero_array(op[0], struct keyinfo, total_keys*2);
1037 for (i = 0; i < num; i++) {
1038 for (j = 1; j < num_ops[i]; j++) {
1039 /* We can't do this on allocation, due to realloc. */
1040 list_head_init(&op[i][j].post);
1041 list_head_init(&op[i][j].pre);
1043 if (!op[i][j].key.dptr)
1046 h = hash_key(&op[i][j].key) % (total_keys * 2);
1047 while (!key_eq(hash[h].key, op[i][j].key)) {
1048 if (!hash[h].key.dptr) {
1049 hash[h].key = op[i][j].key;
1052 h = (h + 1) % (total_keys * 2);
1054 /* Might as well save some memory if we can. */
1055 if (op[i][j].key.dptr != hash[h].key.dptr) {
1056 talloc_free(op[i][j].key.dptr);
1057 op[i][j].key.dptr = hash[h].key.dptr;
1059 hash[h].user = talloc_realloc(hash, hash[h].user,
1061 hash[h].num_users+1);
1063 /* If it's in a transaction, it's the transaction which
1064 * matters from an analysis POV. */
1065 if (in_transaction(op[i], j)
1066 || in_chainlock(op[i], j)) {
1067 unsigned start = op[i][j].group_start;
1069 /* Don't include twice. */
1070 if (hash[h].num_users
1071 && hash[h].user[hash[h].num_users-1].file
1073 && hash[h].user[hash[h].num_users-1].op_num
1077 hash[h].user[hash[h].num_users].op_num = start;
1079 hash[h].user[hash[h].num_users].op_num = j;
1080 hash[h].user[hash[h].num_users].file = i;
1081 hash[h].num_users++;
1088 static bool satisfies(const TDB_DATA *key, const TDB_DATA *data,
1089 const struct op *op)
1091 const TDB_DATA *need = NULL;
1093 if (starts_transaction(op) || starts_chainlock(op)) {
1096 /* Look through for an op in this transaction which
1097 * needs this key. */
1098 for (i = 1; i < op->group_len; i++) {
1099 if (key_eq(op[i].key, *key)) {
1100 need = needs(&op[i]);
1101 /* tdb_exists() is special: there might be
1102 * something in the transaction with more
1103 * specific requirements. Other ops don't have
1104 * specific requirements (eg. store or delete),
1105 * but they change the value so we can't get
1106 * more information from future ops. */
1107 if (op[i].op != OP_TDB_EXISTS)
1114 /* Don't need anything? Cool. */
1118 /* This should be tdb_null or a real value. */
1119 assert(data != &must_exist);
1120 assert(data != &must_not_exist);
1121 assert(data != ¬_exists_or_empty);
1123 /* Must not exist? data must not exist. */
1124 if (need == &must_not_exist)
1125 return data == &tdb_null;
1128 if (need == &must_exist)
1129 return data != &tdb_null;
1131 /* Either noexist or empty. */
1132 if (need == ¬_exists_or_empty)
1133 return data->dsize == 0;
1135 /* Needs something specific. */
1136 return key_eq(*data, *need);
1139 static void move_to_front(struct key_user res[], unsigned off, unsigned elem)
1142 struct key_user tmp = res[elem];
1143 memmove(res + off + 1, res + off, (elem - off)*sizeof(res[0]));
1148 static void restore_to_pos(struct key_user res[], unsigned off, unsigned elem)
1151 struct key_user tmp = res[off];
1152 memmove(res + off, res + off + 1, (elem - off)*sizeof(res[0]));
1157 static bool sort_deps(char *filename[], struct op *op[],
1158 struct key_user res[],
1159 unsigned off, unsigned num,
1160 const TDB_DATA *key, const TDB_DATA *data,
1161 unsigned num_files, unsigned fuzz)
1163 unsigned int i, files_done;
1165 bool done[num_files];
1167 /* None left? We're sorted. */
1171 /* Does this make serial numbers go backwards? Allow a little fuzz. */
1173 int serial1 = op[res[off-1].file][res[off-1].op_num].serial;
1174 int serial2 = op[res[off].file][res[off].op_num].serial;
1176 if (serial1 - serial2 > (int)fuzz) {
1178 printf("Serial jump too far (%u -> %u)\n",
1185 memset(done, 0, sizeof(done));
1187 /* Since ops within a trace file are ordered, we just need to figure
1188 * out which file to try next. Since we don't take into account
1189 * inter-key relationships (which exist by virtue of trace file order),
1190 * we minimize the chance of harm by trying to keep in serial order. */
1191 for (files_done = 0, i = off; i < num && files_done < num_files; i++) {
1192 if (done[res[i].file])
1195 this_op = &op[res[i].file][res[i].op_num];
1197 /* Is what we have good enough for this op? */
1198 if (satisfies(key, data, this_op)) {
1199 move_to_front(res, off, i);
1200 if (sort_deps(filename, op, res, off+1, num,
1201 key, gives(key, data, this_op),
1204 restore_to_pos(res, off, i);
1206 done[res[i].file] = true;
1210 /* No combination worked. */
1214 static void check_dep_sorting(struct key_user user[], unsigned num_users,
1219 unsigned minima[num_files];
1221 memset(minima, 0, sizeof(minima));
1222 for (i = 0; i < num_users; i++) {
1223 assert(minima[user[i].file] < user[i].op_num);
1224 minima[user[i].file] = user[i].op_num;
1229 /* All these ops happen on the same key. Which comes first?
1231 * This can happen both because read ops or failed write ops don't
1232 * change serial number, and also due to race since we access the
1233 * number unlocked (the race can cause less detectable ordering problems,
1234 * in which case we'll deadlock and report: fix manually in that case).
1236 static void figure_deps(char *filename[], struct op *op[],
1237 const TDB_DATA *key, struct key_user user[],
1238 unsigned num_users, unsigned num_files)
1240 /* We assume database starts empty. */
1241 const struct TDB_DATA *data = &tdb_null;
1244 /* We prefer to keep strict serial order if possible: it's the
1245 * most likely. We get more lax if that fails. */
1246 for (fuzz = 0; fuzz < 100; fuzz = (fuzz + 1)*2) {
1247 if (sort_deps(filename, op, user, 0, num_users, key, data,
1253 fail(filename[user[0].file], user[0].op_num+1,
1254 "Could not resolve inter-dependencies");
1256 check_dep_sorting(user, num_users, num_files);
1259 static void sort_ops(struct keyinfo hash[], char *filename[], struct op *op[],
1264 /* Gcc nexted function extension. How cool is this? */
1265 int compare_serial(const void *_a, const void *_b)
1267 const struct key_user *a = _a, *b = _b;
1269 /* First, maintain order within any trace file. */
1270 if (a->file == b->file)
1271 return a->op_num - b->op_num;
1273 /* Otherwise, arrange by serial order. */
1274 if (op[a->file][a->op_num].serial !=
1275 op[b->file][b->op_num].serial)
1276 return op[a->file][a->op_num].serial
1277 - op[b->file][b->op_num].serial;
1279 /* Cancelled transactions are assumed to happen first. */
1280 if (starts_transaction(&op[a->file][a->op_num])
1281 && !successful_transaction(&op[a->file][a->op_num]))
1283 if (starts_transaction(&op[b->file][b->op_num])
1284 && !successful_transaction(&op[b->file][b->op_num]))
1291 /* Now sort into serial order. */
1292 for (h = 0; h < total_keys * 2; h++) {
1293 struct key_user *user = hash[h].user;
1295 qsort(user, hash[h].num_users, sizeof(user[0]), compare_serial);
1296 figure_deps(filename, op, &hash[h].key, user, hash[h].num_users,
1301 static int destroy_depend(struct depend *dep)
1303 list_del(&dep->pre_list);
1304 list_del(&dep->post_list);
1308 static void add_dependency(void *ctx,
1311 unsigned int needs_file,
1312 unsigned int needs_opnum,
1313 unsigned int satisfies_file,
1314 unsigned int satisfies_opnum)
1318 /* We don't depend on ourselves. */
1319 if (needs_file == satisfies_file) {
1320 assert(satisfies_opnum < needs_opnum);
1325 printf("%s:%u: depends on %s:%u\n",
1326 filename[needs_file], needs_opnum+1,
1327 filename[satisfies_file], satisfies_opnum+1);
1330 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1331 /* If something in a traverse depends on something in another
1332 * traverse/transaction, it creates a dependency between the
1334 if ((in_traverse(op[satisfies_file], satisfies_opnum)
1335 && (starts_transaction(&op[needs_file][needs_opnum])
1336 || starts_traverse(&op[needs_file][needs_opnum])))
1337 || (in_traverse(op[needs_file], needs_opnum)
1338 && (starts_transaction(&op[satisfies_file][satisfies_opnum])
1339 || starts_traverse(&op[satisfies_file][satisfies_opnum])))){
1342 /* We are satisfied by end of group. */
1343 sat = op[satisfies_file][satisfies_opnum].group_start;
1344 satisfies_opnum = sat + op[satisfies_file][sat].group_len;
1345 /* And we need that done by start of our group. */
1346 needs_opnum = op[needs_file][needs_opnum].group_start;
1349 /* There is also this case:
1350 * <traverse> <read foo> ...
1351 * <transaction> ... </transaction> <create foo>
1352 * Where if we start the traverse then wait, we could block
1353 * the transaction and deadlock.
1355 * We try to address this by ensuring that where seqnum indicates it's
1356 * possible, we wait for <create foo> before *starting* traverse.
1358 else if (in_traverse(op[needs_file], needs_opnum)) {
1359 struct op *need = &op[needs_file][needs_opnum];
1360 if (op[needs_file][need->group_start].serial >
1361 op[satisfies_file][satisfies_opnum].serial) {
1362 needs_opnum = need->group_start;
1367 /* If you depend on a transaction or chainlock, you actually
1368 * depend on it ending. */
1369 if (starts_transaction(&op[satisfies_file][satisfies_opnum])
1370 || starts_chainlock(&op[satisfies_file][satisfies_opnum])) {
1372 += op[satisfies_file][satisfies_opnum].group_len;
1374 printf("-> Actually end of transaction %s:%u\n",
1375 filename[satisfies_file], satisfies_opnum+1);
1378 /* We should never create a dependency from middle of
1380 assert(!in_transaction(op[satisfies_file], satisfies_opnum)
1381 || op[satisfies_file][satisfies_opnum].op
1382 == OP_TDB_TRANSACTION_COMMIT
1383 || op[satisfies_file][satisfies_opnum].op
1384 == OP_TDB_TRANSACTION_CANCEL);
1386 assert(op[needs_file][needs_opnum].op != OP_TDB_TRAVERSE);
1387 assert(op[satisfies_file][satisfies_opnum].op != OP_TDB_TRAVERSE);
1389 dep = talloc(ctx, struct depend);
1390 dep->needs_file = needs_file;
1391 dep->needs_opnum = needs_opnum;
1392 dep->satisfies_file = satisfies_file;
1393 dep->satisfies_opnum = satisfies_opnum;
1394 list_add(&op[satisfies_file][satisfies_opnum].post, &dep->post_list);
1395 list_add(&op[needs_file][needs_opnum].pre, &dep->pre_list);
1396 talloc_set_destructor(dep, destroy_depend);
1399 static bool changes_db(const TDB_DATA *key, const struct op *op)
1401 return gives(key, NULL, op) != NULL;
1404 static void depend_on_previous(struct op *op[],
1407 struct key_user user[],
1417 if (prev == i - 1) {
1418 /* Just depend on previous. */
1419 add_dependency(NULL, op, filename,
1420 user[i].file, user[i].op_num,
1421 user[prev].file, user[prev].op_num);
1425 /* We have to wait for the readers. Find last one in *each* file. */
1426 memset(deps, 0, sizeof(deps));
1427 deps[user[i].file] = true;
1428 for (j = i - 1; j > prev; j--) {
1429 if (!deps[user[j].file]) {
1430 add_dependency(NULL, op, filename,
1431 user[i].file, user[i].op_num,
1432 user[j].file, user[j].op_num);
1433 deps[user[j].file] = true;
1438 /* This is simple, but not complete. We don't take into account
1439 * indirect dependencies. */
1440 static void optimize_dependencies(struct op *op[], unsigned int num_ops[],
1445 /* There can only be one real dependency on each file */
1446 for (i = 0; i < num; i++) {
1447 for (j = 1; j < num_ops[i]; j++) {
1448 struct depend *dep, *next;
1449 struct depend *prev[num];
1451 memset(prev, 0, sizeof(prev));
1453 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1454 if (!prev[dep->satisfies_file]) {
1455 prev[dep->satisfies_file] = dep;
1458 if (prev[dep->satisfies_file]->satisfies_opnum
1459 < dep->satisfies_opnum) {
1460 talloc_free(prev[dep->satisfies_file]);
1461 prev[dep->satisfies_file] = dep;
1468 for (i = 0; i < num; i++) {
1471 for (j = 0; j < num; j++)
1474 for (j = 1; j < num_ops[i]; j++) {
1475 struct depend *dep, *next;
1477 list_for_each_safe(&op[i][j].pre, dep, next, pre_list) {
1478 if (deps[dep->satisfies_file]
1479 >= (int)dep->satisfies_opnum)
1482 deps[dep->satisfies_file]
1483 = dep->satisfies_opnum;
1489 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1490 struct traverse_dep {
1492 unsigned int op_num;
1495 /* Force an order among the traversals, so they don't deadlock (as much) */
1496 static void make_traverse_depends(char *filename[],
1497 struct op *op[], unsigned int num_ops[],
1500 unsigned int i, num_traversals = 0;
1502 struct traverse_dep *dep;
1504 /* Sort by which one runs first. */
1505 int compare_traverse_dep(const void *_a, const void *_b)
1507 const struct traverse_dep *ta = _a, *tb = _b;
1508 const struct op *a = &op[ta->file][ta->op_num],
1509 *b = &op[tb->file][tb->op_num];
1511 if (a->serial != b->serial)
1512 return a->serial - b->serial;
1514 /* If they have same serial, it means one didn't make any
1515 * changes. Thus sort by end in that case. */
1516 return a[a->group_len].serial - b[b->group_len].serial;
1519 dep = talloc_array(NULL, struct traverse_dep, 1);
1522 for (i = 0; i < num; i++) {
1523 for (j = 1; j < num_ops[i]; j++) {
1524 /* Traverse start (ignore those in
1525 * transactions; they're already covered by
1526 * transaction dependencies). */
1527 if (starts_traverse(&op[i][j])
1528 && !in_transaction(op[i], j)) {
1529 dep = talloc_realloc(NULL, dep,
1530 struct traverse_dep,
1532 dep[num_traversals].file = i;
1533 dep[num_traversals].op_num = j;
1538 qsort(dep, num_traversals, sizeof(dep[0]), compare_traverse_dep);
1540 for (i = 1; i < num_traversals; i++) {
1541 const struct op *prev = &op[dep[i-1].file][dep[i-1].op_num];
1542 const struct op *curr = &op[dep[i].file][dep[i].op_num];
1544 /* Read traverses don't depend on each other (read lock). */
1545 if (prev->op == OP_TDB_TRAVERSE_READ_START
1546 && curr->op == OP_TDB_TRAVERSE_READ_START)
1549 /* Only make dependency if it's clear. */
1550 if (compare_traverse_dep(&dep[i], &dep[i-1])) {
1551 /* i depends on end of traverse i-1. */
1552 add_dependency(NULL, op, filename,
1553 dep[i].file, dep[i].op_num,
1554 dep[i-1].file, dep[i-1].op_num
1562 static void derive_dependencies(char *filename[],
1563 struct op *op[], unsigned int num_ops[],
1566 struct keyinfo *hash;
1569 /* Create hash table for faster key lookup. */
1570 hash = hash_ops(op, num_ops, num);
1572 /* Sort them by serial number. */
1573 sort_ops(hash, filename, op, num);
1575 /* Create dependencies back to the last change, rather than
1576 * creating false dependencies by naively making each one
1577 * depend on the previous. This has two purposes: it makes
1578 * later optimization simpler, and it also avoids deadlock with
1579 * same sequence number ops inside traversals (if one
1580 * traversal doesn't write anything, two ops can have the same
1581 * sequence number yet we can create a traversal dependency
1582 * the other way). */
1583 for (h = 0; h < total_keys * 2; h++) {
1586 if (hash[h].num_users < 2)
1589 for (i = 0; i < hash[h].num_users; i++) {
1590 if (changes_db(&hash[h].key, &op[hash[h].user[i].file]
1591 [hash[h].user[i].op_num])) {
1592 depend_on_previous(op, filename, num,
1593 hash[h].user, i, prev);
1595 } else if (prev >= 0)
1596 add_dependency(hash, op, filename,
1597 hash[h].user[i].file,
1598 hash[h].user[i].op_num,
1599 hash[h].user[prev].file,
1600 hash[h].user[prev].op_num);
1604 #if TRAVERSALS_TAKE_TRANSACTION_LOCK
1605 make_traverse_depends(filename, op, num_ops, num);
1608 optimize_dependencies(op, num_ops, num);
1611 int main(int argc, char *argv[])
1613 struct timeval start, end;
1614 unsigned int i, num_ops[argc], hashsize[argc], tdb_flags[argc], open_flags[argc];
1615 struct op *op[argc];
1621 errx(1, "Usage: %s <tdbfile> <tracefile>...", argv[0]);
1623 pipes = talloc_array(NULL, struct pipe, argc - 2);
1624 for (i = 0; i < argc - 2; i++) {
1625 printf("Loading tracefile %s...", argv[2+i]);
1627 op[i] = load_tracefile(argv[2+i], &num_ops[i], &hashsize[i],
1628 &tdb_flags[i], &open_flags[i]);
1629 if (pipe(pipes[i].fd) != 0)
1630 err(1, "creating pipe");
1634 printf("Calculating inter-dependencies...");
1636 derive_dependencies(argv+2, op, num_ops, i);
1639 /* Don't fork for single arg case: simple debugging. */
1641 struct tdb_context *tdb;
1642 tdb = tdb_open_ex(argv[1], hashsize[0], tdb_flags[0]|TDB_NOSYNC,
1643 open_flags[0], 0600, NULL, hash_key);
1644 printf("Single threaded run...");
1647 run_ops(tdb, pipes[0].fd[0], argv+2, op, 0, 1, num_ops[0],
1649 check_deps(argv[2], op[0], num_ops[0]);
1656 err(1, "creating pipe");
1658 for (i = 0; i < argc - 2; i++) {
1659 struct tdb_context *tdb;
1663 err(1, "fork failed");
1666 tdb = tdb_open_ex(argv[1], hashsize[i],
1667 tdb_flags[i]|TDB_NOSYNC,
1668 open_flags[i], 0600, NULL, hash_key);
1670 err(1, "Opening tdb %s", argv[1]);
1672 /* This catches parent exiting. */
1673 if (read(fds[0], &c, 1) != 1)
1675 run_ops(tdb, pipes[i].fd[0], argv+2, op, i, 1,
1677 check_deps(argv[2+i], op[i], num_ops[i]);
1684 /* Let everything settle. */
1687 printf("Starting run...");
1689 gettimeofday(&start, NULL);
1690 /* Tell them all to go! Any write of sufficient length will do. */
1691 if (write(fds[1], hashsize, i) != i)
1692 err(1, "Writing to wakeup pipe");
1694 for (i = 0; i < argc - 2; i++) {
1697 if (!WIFEXITED(status)) {
1698 warnx("Child died with signal %i", WTERMSIG(status));
1700 } else if (WEXITSTATUS(status) != 0)
1701 /* Assume child spat out error. */
1707 gettimeofday(&end, NULL);
1710 end.tv_sec -= start.tv_sec;
1711 printf("Time replaying: %lu usec\n",
1712 end.tv_sec * 1000000UL + (end.tv_usec - start.tv_usec));