Debug print dot

This commit is contained in:
2024-01-23 10:12:07 -08:00
parent 5bec5b178d
commit 836766132f

View File

@@ -882,9 +882,69 @@ void eraseChild(Node *self, uint8_t index) {
}
}
void debugPrintDot(FILE *file, Node *node) {
struct DebugDotPrinter {
explicit DebugDotPrinter(FILE *file) : file(file) {}
void print(Node *n) {
assert(n != nullptr);
if (n->entryPresent) {
fprintf(file, " k_%p [label=\"m=%d p=%d r=%d\"];\n", (void *)n,
int(n->maxVersion), int(n->entry.pointVersion),
int(n->entry.rangeVersion));
} else {
fprintf(file, " k_%p [label=\"m=%d\"];\n", (void *)n,
int(n->maxVersion));
}
for (int child = getChildGeq(n, 0); child >= 0;
child = getChildGeq(n, child + 1)) {
auto *c = getChildExists(n, child);
fprintf(file, " k_%p -> k_%p [label=\"'%c'\"];\n", (void *)n, (void *)c,
child);
print(c);
}
}
FILE *file;
};
fprintf(file, "digraph ConflictSet {\n");
assert(node != nullptr);
DebugDotPrinter printer{file};
printer.print(node);
fprintf(file, "}\n");
}
static Node *insert(Node *&self, std::span<const uint8_t> key,
int64_t writeVersion) {
self->maxVersion = writeVersion;
if (key.size() == 0) {
self->entryPresent = true;
self->entry.pointVersion = writeVersion;
// TODO set correct rangeVersion
return self;
}
auto &child = getOrCreateChild(self, key.front());
if (!child) {
child = newNode();
child->parent = self;
child->parentsIndex = key.front();
}
return insert(child, key.subspan(1, key.size() - 1), writeVersion);
}
struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
void check(const ReadRange *, Result *, int) const {}
void addWrites(const WriteRange *, int) {}
void addWrites(const WriteRange *writes, int count) {
for (int i = 0; i < count; ++i) {
const auto &w = writes[i];
// TODO support non-point writes
assert(w.end.len == 0);
insert(root, std::span<const uint8_t>(w.begin.p, w.begin.len),
w.writeVersion);
}
}
void setOldestVersion(int64_t oldestVersion) {
this->oldestVersion = oldestVersion;
}
@@ -906,7 +966,7 @@ struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
// Add all children to toFree
for (int child = getChildGeq(n, 0); child >= 0;
child = getChildGeq(n, child + 1)) {
auto *c = getChild(n, child);
auto *c = getChildExists(n, child);
assert(c != nullptr);
toFree.push_back(c);
}
@@ -914,7 +974,6 @@ struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
}
}
private:
Node *root;
int64_t oldestVersion;
};
@@ -998,12 +1057,12 @@ int main(void) {
auto *write = new (arena) ConflictSet::WriteRange[kNumKeys];
for (int i = 0; i < kNumKeys; ++i) {
write[i].begin = toKey(arena, i);
write[i].end = toKeyAfter(arena, i);
write[i].end.len = 0;
write[i].writeVersion = ++writeVersion;
}
cs.addWrites(write, kNumKeys);
refImpl.addWrites(write, kNumKeys);
debugPrintDot(stdout, cs.root);
return 0;
}
#endif