Changed to a shifted mid=bid.rid representation for debugging

This only matters for developers, not users, but it still helps a lot to
get debug representations right.

Since the exact mid encoding depends on the block_size in an unintuitive
manner, it's tricky to render in a debug-friendly way that is useful
both with and without tools.

Previously, I avoided shifting the bid representation, since this would
be closer to the value in the device, but this hides the actual
structure of the mtree. Now the bid is shifted, showing the underlying
mtree/mdir structure, at the cost of needing to know the number of mbits
to encode the mid back into an integer.

So for example, on a device with 4KiB blocks, or 8 mbits:

  mid=1
  mid=258
  mid=515

Becomes:

  mid=0.1
  mid=1.2
  mid=2.3

This continues to make the mbits a more fundamental part of littlefs,
but that's probably just how that's going to be.
This commit is contained in:
Christopher Haster
2023-09-11 01:59:19 -05:00
parent f9bd2c56e3
commit 900ea807ae
4 changed files with 81 additions and 82 deletions

View File

@@ -561,13 +561,13 @@ class Rbyd:
mtree = Rbyd.fetch(f, block_size, block, trunk)
# corrupted?
if not mtree:
return False, -1, None, -1, 0, 0
return False, -1, 0, None, -1, 0, 0
# lookup our name in the mtree
mbid, tag_, mw, data = mtree.btree_namelookup(
f, block_size, did, name)
if tag_ != TAG_MDIR:
return False, -1, None, -1, 0, 0
return False, -1, 0, None, -1, 0, 0
# fetch the mdir
blocks = frommdir(data)
@@ -653,10 +653,10 @@ def superconfig(mroot):
# collect gstate
class GState:
def __init__(self, block_size):
def __init__(self, mbits):
self.gstate = {}
self.gdelta = {}
self.block_size = block_size
self.mbits = mbits
def xor(self, mbid, mw, mdir):
tag = TAG_GSTATE-0x1
@@ -682,10 +682,6 @@ class GState:
if TAG_GRM not in self.gstate:
return []
# derive the mbid/mrid masks from the block_size
mridmask = (1 << m.ceil(m.log2(self.block_size // 16)))-1
mbidmask = ~mridmask
data = self.gstate[TAG_GRM]
d = 0
count, d_ = fromleb128(data[d:]); d += d_
@@ -693,26 +689,27 @@ class GState:
if count <= 2:
for _ in range(count):
mid, d_ = fromleb128(data[d:]); d += d_
rms.append((mid & mbidmask, mid & mridmask))
rms.append((
mid & ~((1 << self.mbits)-1),
mid & ((1 << self.mbits)-1)))
return rms
def grepr(tag, data, block_size):
def grepr(tag, data, mbits):
if tag == TAG_GRM:
# derive the mbid/mrid masks from the block_size
mridmask = (1 << m.ceil(m.log2(block_size // 16)))-1
mbidmask = ~mridmask
d = 0
count, d_ = fromleb128(data[d:]); d += d_
rms = []
if count <= 2:
for _ in range(count):
mid, d_ = fromleb128(data[d:]); d += d_
rms.append((mid & mbidmask, mid & mridmask))
rms.append((
mid & ~((1 << mbits)-1),
mid & ((1 << mbits)-1)))
return 'grm %s' % (
'none' if count == 0
else ' '.join('%d.%d' % (mbid, rid) for mbid, rid in rms)
if count <= 2
else ' '.join('%d.%d' % (mbid >> mbits, rid)
for mbid, rid in rms)
if count <= 2
else '0x%x' % count)
else:
return 'gstate 0x%02x %d' % (tag, len(data))
@@ -762,6 +759,10 @@ def main(disk, mroots=None, *,
f.seek(0, os.SEEK_END)
block_size = f.tell()
# determine the number of mbits from the block_size, this is just
# for printing purposes
mbits = m.ceil(m.log2(block_size // 16))
# before we print, we need to do a pass for a few things:
# - find the actual mroot
# - find the total weight
@@ -772,7 +773,7 @@ def main(disk, mroots=None, *,
mweight = 0
rweight = 0
corrupted = False
gstate = GState(block_size)
gstate = GState(mbits)
config = {}
dir_dids = [(0, b'', -1, 0, None, -1, TAG_DID, 0)]
bookmark_dids = []
@@ -937,13 +938,13 @@ def main(disk, mroots=None, *,
#### actual debugging begins here
# print some information about the filesystem
print('littlefs v%s.%s %s, rev %d, weight %d' % (
print('littlefs v%s.%s %s, rev %d, weight %d.%d' % (
config.get('major_version', ('?',))[0],
config.get('minor_version', ('?',))[0],
mroot.addr(), mroot.rev, mweight))
mroot.addr(), mroot.rev, mweight >> mbits, 1 << mbits))
# print header
w_width = (m.ceil(m.log10(max(1, mweight)+1))
w_width = (m.ceil(m.log10(max(1, mweight >> mbits)+1))
+ 2*m.ceil(m.log10(max(1, rweight)+1))
+ 2)
if dtree:
@@ -998,7 +999,7 @@ def main(disk, mroots=None, *,
print('%12s %-*s %s' % (
'gstate:' if i == 0 else '',
w_width + 23,
grepr(tag, data, block_size),
grepr(tag, data, mbits),
next(xxd(data, 8), '')
if not args.get('no_truncate') else ''))
@@ -1033,7 +1034,7 @@ def main(disk, mroots=None, *,
','.join('%04x' % block
for block in it.chain([mdir.block],
mdir.redund_blocks)),
w_width, mbid-max(mw-1, 0),
w_width, mbid >> mbits,
tagrepr(tag, 0, len(data)),
next(xxd(data, 8), '')
if not args.get('no_truncate') else '',
@@ -1136,8 +1137,8 @@ def main(disk, mroots=None, *,
mdir.redund_blocks))
if mbid != pmbid else '',
w_width, '%d.%d-%d' % (
mbid-max(mw-1, 0), rid-(w-1), rid)
if w > 1 else '%d.%d' % (mbid-max(mw-1, 0), rid)
mbid >> mbits, rid-(w-1), rid)
if w > 1 else '%d.%d' % (mbid >> mbits, rid)
if w > 0 else '',
f_width, '%s%s' % (
prefixes[0+(i==len(dir)-1)],