forked from Imagelibrary/littlefs
Enabled both pruning/non-pruning dbg reprs, -t/--tree and -R/--rbyd
Now that altns/altas are more important structurally, including them in
our dbg script's tree renderers is valuable for debugging. On the other
hand, they do add quite a bit of visual noise when looking at large
multi-rbyd trees topologically.
This commit gives us the best of both worlds by making both tree
renderings available under different options:
-t/--tree, a simplified rbyd tree renderer with altn/alta pruning:
.-> 0 reg w1 4
.-+-> uattr 0x01 2
| .-> uattr 0x02 2
.---+-+-> uattr 0x03 2
| .-> uattr 0x04 2
| .-+-> uattr 0x05 2
| .-+---> uattr 0x06 2
+-+-+-+-+-> 1 reg w1 4
| | '-> 2 reg w1 4
| '---> uattr 0x01 2
'---+-+-+-> uattr 0x02 2
| | '-> uattr 0x03 2
| '-+-> uattr 0x04 2
| '-> uattr 0x05 2
| .-> uattr 0x06 2
| .-+-> uattr 0x07 2
| | .-> uattr 0x08 2
'-+-+-> uattr 0x09 2
-R/--rbyd, a full rbyd tree renderer:
.---> 0 reg w1 4
.---+-+-> uattr 0x01 2
| .---> uattr 0x02 2
.-+-+-+-+-> uattr 0x03 2
| .---> uattr 0x04 2
| .-+-+-> uattr 0x05 2
| .-+---+-> uattr 0x06 2
+---+-+-+-+-+-> 1 reg w1 4
| | '-> 2 reg w1 4
| '-----> uattr 0x01 2
'-+-+-+-+-+-+-> uattr 0x02 2
| | '---> uattr 0x03 2
| '---+-+-> uattr 0x04 2
| '---> uattr 0x05 2
| .---> uattr 0x06 2
| .-+-+-> uattr 0x07 2
| | .-> uattr 0x08 2
'-----+---+-> uattr 0x09 2
And of course -B/--btree, a simplified B-tree renderer (more useful for
multi-rbyds):
+-> 0 reg w1 4
| uattr 0x01 2
| uattr 0x02 2
| uattr 0x03 2
| uattr 0x04 2
| uattr 0x05 2
| uattr 0x06 2
|-> 1 reg w1 4
'-> 2 reg w1 4
uattr 0x01 2
uattr 0x02 2
uattr 0x03 2
uattr 0x04 2
uattr 0x05 2
uattr 0x06 2
uattr 0x07 2
uattr 0x08 2
uattr 0x09 2
This commit is contained in:
@@ -792,85 +792,6 @@ class Rbyd:
|
||||
|
||||
yield rid, tag, w, j, d, data
|
||||
|
||||
# create tree representation for debugging
|
||||
def tree(self):
|
||||
trunks = co.defaultdict(lambda: (-1, 0))
|
||||
alts = co.defaultdict(lambda: {})
|
||||
|
||||
rid, tag = -1, 0
|
||||
while True:
|
||||
done, rid, tag, w, j, d, data, path = self.lookup(rid, tag+0x1)
|
||||
# found end of tree?
|
||||
if done:
|
||||
break
|
||||
|
||||
# keep track of trunks/alts
|
||||
trunks[j] = (rid, tag)
|
||||
|
||||
for j_, j__, followed, c in path:
|
||||
if followed:
|
||||
alts[j_] |= {'f': j__, 'c': c}
|
||||
else:
|
||||
alts[j_] |= {'nf': j__, 'c': c}
|
||||
|
||||
# treat unreachable alts as converging paths
|
||||
for j_, alt in alts.items():
|
||||
if 'f' not in alt:
|
||||
alt['f'] = alt['nf']
|
||||
elif 'nf' not in alt:
|
||||
alt['nf'] = alt['f']
|
||||
|
||||
# find the trunk and depth of each alt
|
||||
def rec_trunk(j_):
|
||||
if j_ not in alts:
|
||||
return trunks[j_]
|
||||
else:
|
||||
if 'nft' not in alts[j_]:
|
||||
alts[j_]['nft'] = rec_trunk(alts[j_]['nf'])
|
||||
return alts[j_]['nft']
|
||||
|
||||
for j_ in alts.keys():
|
||||
rec_trunk(j_)
|
||||
for j_, alt in alts.items():
|
||||
if alt['f'] in alts:
|
||||
alt['ft'] = alts[alt['f']]['nft']
|
||||
else:
|
||||
alt['ft'] = trunks[alt['f']]
|
||||
|
||||
def rec_height(j_):
|
||||
if j_ not in alts:
|
||||
return 0
|
||||
else:
|
||||
if 'h' not in alts[j_]:
|
||||
alts[j_]['h'] = max(
|
||||
rec_height(alts[j_]['f']),
|
||||
rec_height(alts[j_]['nf'])) + 1
|
||||
return alts[j_]['h']
|
||||
|
||||
for j_ in alts.keys():
|
||||
rec_height(j_)
|
||||
|
||||
t_depth = max((alt['h']+1 for alt in alts.values()), default=0)
|
||||
|
||||
# convert to more general tree representation
|
||||
tree = set()
|
||||
for j, alt in alts.items():
|
||||
# note all non-trunk edges should be black
|
||||
tree.add(TBranch(
|
||||
a=alt['nft'],
|
||||
b=alt['nft'],
|
||||
d=t_depth-1 - alt['h'],
|
||||
c=alt['c'],
|
||||
))
|
||||
tree.add(TBranch(
|
||||
a=alt['nft'],
|
||||
b=alt['ft'],
|
||||
d=t_depth-1 - alt['h'],
|
||||
c='b',
|
||||
))
|
||||
|
||||
return tree, t_depth
|
||||
|
||||
# btree lookup with this rbyd as the root
|
||||
def btree_lookup(self, f, block_size, bid, *,
|
||||
depth=None):
|
||||
@@ -927,201 +848,6 @@ class Rbyd:
|
||||
else:
|
||||
return not tags, bid + (rid_-rid), w, rbyd, rid_, tags, path
|
||||
|
||||
# btree rbyd-tree generation for debugging
|
||||
def btree_tree(self, f, block_size, *,
|
||||
depth=None,
|
||||
inner=False):
|
||||
# find the max depth of each layer to nicely align trees
|
||||
bdepths = {}
|
||||
bid = -1
|
||||
while True:
|
||||
done, bid, w, rbyd, rid, tags, path = self.btree_lookup(
|
||||
f, block_size, bid+1, depth=depth)
|
||||
if done:
|
||||
break
|
||||
|
||||
for d, (bid, w, rbyd, rid, tags) in enumerate(path):
|
||||
_, rdepth = rbyd.tree()
|
||||
bdepths[d] = max(bdepths.get(d, 0), rdepth)
|
||||
|
||||
# find all branches
|
||||
tree = set()
|
||||
root = None
|
||||
branches = {}
|
||||
bid = -1
|
||||
while True:
|
||||
done, bid, w, rbyd, rid, tags, path = self.btree_lookup(
|
||||
f, block_size, bid+1, depth=depth)
|
||||
if done:
|
||||
break
|
||||
|
||||
d_ = 0
|
||||
leaf = None
|
||||
for d, (bid, w, rbyd, rid, tags) in enumerate(path):
|
||||
if not tags:
|
||||
continue
|
||||
|
||||
# map rbyd tree into B-tree space
|
||||
rtree, rdepth = rbyd.tree()
|
||||
|
||||
# note we adjust our bid/rids to be left-leaning,
|
||||
# this allows a global order and make tree rendering quite
|
||||
# a bit easier
|
||||
rtree_ = set()
|
||||
for branch in rtree:
|
||||
a_rid, a_tag = branch.a
|
||||
b_rid, b_tag = branch.b
|
||||
_, _, _, a_w, _, _, _, _ = rbyd.lookup(a_rid, 0)
|
||||
_, _, _, b_w, _, _, _, _ = rbyd.lookup(b_rid, 0)
|
||||
rtree_.add(TBranch(
|
||||
a=(a_rid-(a_w-1), a_tag),
|
||||
b=(b_rid-(b_w-1), b_tag),
|
||||
d=branch.d,
|
||||
c=branch.c,
|
||||
))
|
||||
rtree = rtree_
|
||||
|
||||
# connect our branch to the rbyd's root
|
||||
if leaf is not None:
|
||||
root = min(rtree,
|
||||
key=lambda branch: branch.d,
|
||||
default=None)
|
||||
|
||||
if root is not None:
|
||||
r_rid, r_tag = root.a
|
||||
else:
|
||||
r_rid, r_tag = rid-(w-1), tags[0][0]
|
||||
tree.add(TBranch(
|
||||
a=leaf,
|
||||
b=(bid-rid+r_rid, d, r_rid, r_tag),
|
||||
d=d_-1,
|
||||
c='b',
|
||||
))
|
||||
|
||||
for branch in rtree:
|
||||
# map rbyd branches into our btree space
|
||||
a_rid, a_tag = branch.a
|
||||
b_rid, b_tag = branch.b
|
||||
tree.add(TBranch(
|
||||
a=(bid-rid+a_rid, d, a_rid, a_tag),
|
||||
b=(bid-rid+b_rid, d, b_rid, b_tag),
|
||||
d=branch.d + d_ + bdepths.get(d, 0)-rdepth,
|
||||
c=branch.c,
|
||||
))
|
||||
|
||||
d_ += max(bdepths.get(d, 0), 1)
|
||||
leaf = (bid-(w-1), d, rid-(w-1),
|
||||
next((tag for tag, _, _, _ in tags
|
||||
if tag & 0xfff == TAG_BRANCH),
|
||||
TAG_BRANCH))
|
||||
|
||||
# remap branches to leaves if we aren't showing inner branches
|
||||
if not inner:
|
||||
# step through each layer backwards
|
||||
b_depth = max((branch.a[1]+1 for branch in tree), default=0)
|
||||
|
||||
# keep track of the original bids, unfortunately because we
|
||||
# store the bids in the branches we overwrite these
|
||||
tree = {(branch.b[0] - branch.b[2], branch) for branch in tree}
|
||||
|
||||
for bd in reversed(range(b_depth-1)):
|
||||
# find leaf-roots at this level
|
||||
roots = {}
|
||||
for bid, branch in tree:
|
||||
# choose the highest node as the root
|
||||
if (branch.b[1] == b_depth-1
|
||||
and (bid not in roots
|
||||
or branch.d < roots[bid].d)):
|
||||
roots[bid] = branch
|
||||
|
||||
# remap branches to leaf-roots
|
||||
tree_ = set()
|
||||
for bid, branch in tree:
|
||||
if branch.a[1] == bd and branch.a[0] in roots:
|
||||
branch = TBranch(
|
||||
a=roots[branch.a[0]].b,
|
||||
b=branch.b,
|
||||
d=branch.d,
|
||||
c=branch.c,
|
||||
)
|
||||
if branch.b[1] == bd and branch.b[0] in roots:
|
||||
branch = TBranch(
|
||||
a=branch.a,
|
||||
b=roots[branch.b[0]].b,
|
||||
d=branch.d,
|
||||
c=branch.c,
|
||||
)
|
||||
tree_.add((bid, branch))
|
||||
tree = tree_
|
||||
|
||||
# strip out bids
|
||||
tree = {branch for _, branch in tree}
|
||||
|
||||
return tree, max((branch.d+1 for branch in tree), default=0)
|
||||
|
||||
# btree B-tree generation for debugging
|
||||
def btree_btree(self, f, block_size, *,
|
||||
depth=None,
|
||||
inner=False):
|
||||
# find all branches
|
||||
tree = set()
|
||||
root = None
|
||||
branches = {}
|
||||
bid = -1
|
||||
while True:
|
||||
done, bid, w, rbyd, rid, tags, path = self.btree_lookup(
|
||||
f, block_size, bid+1, depth=depth)
|
||||
if done:
|
||||
break
|
||||
|
||||
# if we're not showing inner nodes, prefer names higher in
|
||||
# the tree since this avoids showing vestigial names
|
||||
name = None
|
||||
if not inner:
|
||||
name = None
|
||||
for bid_, w_, rbyd_, rid_, tags_ in reversed(path):
|
||||
for tag_, j_, d_, data_ in tags_:
|
||||
if tag_ & 0x7f00 == TAG_NAME:
|
||||
name = (tag_, j_, d_, data_)
|
||||
|
||||
if rid_-(w_-1) != 0:
|
||||
break
|
||||
|
||||
a = root
|
||||
for d, (bid, w, rbyd, rid, tags) in enumerate(path):
|
||||
if not tags:
|
||||
continue
|
||||
|
||||
b = (bid-(w-1), d, rid-(w-1),
|
||||
(name if name else tags[0])[0])
|
||||
|
||||
# remap branches to leaves if we aren't showing
|
||||
# inner branches
|
||||
if not inner:
|
||||
if b not in branches:
|
||||
bid, w, rbyd, rid, tags = path[-1]
|
||||
if not tags:
|
||||
continue
|
||||
branches[b] = (
|
||||
bid-(w-1), len(path)-1, rid-(w-1),
|
||||
(name if name else tags[0])[0])
|
||||
b = branches[b]
|
||||
|
||||
# found entry point?
|
||||
if root is None:
|
||||
root = b
|
||||
a = root
|
||||
|
||||
tree.add(TBranch(
|
||||
a=a,
|
||||
b=b,
|
||||
d=d,
|
||||
c='b',
|
||||
))
|
||||
a = b
|
||||
|
||||
return tree, max((branch.d+1 for branch in tree), default=0)
|
||||
|
||||
# mtree lookup with this rbyd as the mroot
|
||||
def mtree_lookup(self, f, block_size, mbid):
|
||||
# have mtree?
|
||||
|
||||
Reference in New Issue
Block a user