scripts: maps: Cleaned up/moved header generation before rendering

Should've probably been two commits, but:

1. Cleaned up tracebd.py's header generation to be consistent with
   dbgbmap.py and other scripts.

   Percentage fields are now consistently floats in all scripts,
   allowing user-specified precision when punescaping.

2. Moved header generation up to where we still have the disk open (in
   dbgbmap[d3].py), to avoid issues with lazy Lfs attrs trying to access
   the disk after it's been closed.

   Found while testing with --title='cksum %(cksum)08x'. Lfs tries to
   validate the gcksum last minute and things break.
This commit is contained in:
Christopher Haster
2025-04-11 02:43:07 -05:00
parent f0b8d34230
commit 50f652d44f
3 changed files with 163 additions and 184 deletions
+59 -62
View File
@@ -4539,6 +4539,64 @@ def main_(ring, disk, mroots=None, *,
else:
bmap[b] = BmapBlock(b, type, child, usage)
# one last thing, build a title
if title:
title_ = punescape(title, {
'magic': 'littlefs%s' % (
'' if lfs.ckmagic() else '?'),
'version': 'v%s.%s' % (
lfs.version.major if lfs.version is not None else '?',
lfs.version.minor if lfs.version is not None else '?'),
'version_major':
lfs.version.major if lfs.version is not None else '?',
'version_minor':
lfs.version.minor if lfs.version is not None else '?',
'geometry': '%sx%s' % (
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?'),
'block_size':
lfs.block_size if lfs.block_size is not None else '?',
'block_count':
lfs.block_count if lfs.block_count is not None else '?',
'addr': lfs.addr(),
'weight': 'w%s.%s' % (lfs.mbweightrepr(), lfs.mrweightrepr()),
'mbweight': lfs.mbweightrepr(),
'mrweight': lfs.mrweightrepr(),
'rev': '%08x' % lfs.rev,
'cksum': '%08x%s' % (
lfs.cksum,
'' if lfs.ckgcksum() else '?'),
'total': total_count,
'total_percent': 100*total_count / max(len(bmap), 1),
'mdir': mdir_count,
'mdir_percent': 100*mdir_count / max(len(bmap), 1),
'btree': btree_count,
'btree_percent': 100*btree_count / max(len(bmap), 1),
'data': data_count,
'data_percent': 100*data_count / max(len(bmap), 1),
})
elif title_littlefs:
title_ = ('littlefs%s v%s.%s %sx%s %s w%s.%s, '
'rev %08x, '
'cksum %08x%s' % (
'' if lfs.ckmagic() else '?',
lfs.version.major if lfs.version is not None else '?',
lfs.version.minor if lfs.version is not None else '?',
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?',
lfs.addr(),
lfs.mbweightrepr(), lfs.mrweightrepr(),
lfs.rev,
lfs.cksum,
'' if lfs.ckgcksum() else '?'))
else:
title_ = ('bd %sx%s, %6s mdir, %6s btree, %6s data' % (
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?',
'%.1f%%' % (100*mdir_count / max(len(bmap), 1)),
'%.1f%%' % (100*btree_count / max(len(bmap), 1)),
'%.1f%%' % (100*data_count / max(len(bmap), 1))))
# scale width/height if requested
if (to_scale is not None
and (width is None or height is None)):
@@ -4754,68 +4812,7 @@ def main_(ring, disk, mroots=None, *,
# print some summary info
if not no_header:
if title:
ring.writeln(punescape(title, {
'magic': 'littlefs%s' % (
'' if lfs.ckmagic() else '?'),
'version': 'v%s.%s' % (
lfs.version.major
if lfs.version is not None else '?',
lfs.version.minor
if lfs.version is not None else '?'),
'version_major': lfs.version.major
if lfs.version is not None else '?',
'version_minor': lfs.version.minor
if lfs.version is not None else '?',
'geometry': '%sx%s' % (
lfs.block_size
if lfs.block_size is not None else '?',
lfs.block_count
if lfs.block_count is not None else '?'),
'block_size': lfs.block_size
if lfs.block_size is not None else '?',
'block_count': lfs.block_count
if lfs.block_count is not None else '?',
'addr': lfs.addr(),
'weight': 'w%s.%s' % (
lfs.mbweightrepr(),
lfs.mrweightrepr()),
'mbweight': lfs.mbweightrepr(),
'mrweight': lfs.mrweightrepr(),
'cksum': '%08x%s' % (
lfs.cksum,
'' if lfs.ckgcksum() else '?'),
'total': total_count,
'total_percent': '%.1f%%' % (
100*total_count / max(len(bmap), 1)),
'mdir': mdir_count,
'mdir_percent': '%.1f%%' % (
100*mdir_count / max(len(bmap), 1)),
'btree': btree_count,
'btree_percent': '%.1f%%' % (
100*btree_count / max(len(bmap), 1)),
'data': data_count,
'data_percent': '%.1f%%' % (
100*data_count / max(len(bmap), 1)),
}))
elif title_littlefs:
ring.writeln('littlefs%s v%s.%s %sx%s %s w%s.%s, cksum %08x%s' % (
'' if lfs.ckmagic() else '?',
lfs.version.major if lfs.version is not None else '?',
lfs.version.minor if lfs.version is not None else '?',
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?',
lfs.addr(),
lfs.mbweightrepr(), lfs.mrweightrepr(),
lfs.cksum,
'' if lfs.ckgcksum() else '?'))
else:
ring.writeln('bd %sx%s, %6s mdir, %6s btree, %6s data' % (
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?',
'%.1f%%' % (100*mdir_count / max(len(bmap), 1)),
'%.1f%%' % (100*btree_count / max(len(bmap), 1)),
'%.1f%%' % (100*data_count / max(len(bmap), 1))))
ring.writeln(title_)
# draw canvas
for row in range(canvas.height//canvas.yscale):
+59 -62
View File
@@ -4869,6 +4869,64 @@ def main(disk, output, mroots=None, *,
b_ for b_ in child.blocks
if b_ in bmap)
# one last thing, build a title
if title:
title_ = punescape(title, {
'magic': 'littlefs%s' % (
'' if lfs.ckmagic() else '?'),
'version': 'v%s.%s' % (
lfs.version.major if lfs.version is not None else '?',
lfs.version.minor if lfs.version is not None else '?'),
'version_major':
lfs.version.major if lfs.version is not None else '?',
'version_minor':
lfs.version.minor if lfs.version is not None else '?',
'geometry': '%sx%s' % (
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?'),
'block_size':
lfs.block_size if lfs.block_size is not None else '?',
'block_count':
lfs.block_count if lfs.block_count is not None else '?',
'addr': lfs.addr(),
'weight': 'w%s.%s' % (lfs.mbweightrepr(), lfs.mrweightrepr()),
'mbweight': lfs.mbweightrepr(),
'mrweight': lfs.mrweightrepr(),
'rev': '%08x' % lfs.rev,
'cksum': '%08x%s' % (
lfs.cksum,
'' if lfs.ckgcksum() else '?'),
'total': total_count,
'total_percent': 100*total_count / max(len(bmap), 1),
'mdir': mdir_count,
'mdir_percent': 100*mdir_count / max(len(bmap), 1),
'btree': btree_count,
'btree_percent': 100*btree_count / max(len(bmap), 1),
'data': data_count,
'data_percent': 100*data_count / max(len(bmap), 1),
})
elif not title_usage:
title_ = ('littlefs%s v%s.%s %sx%s %s w%s.%s, '
'rev %08x, '
'cksum %08x%s' % (
'' if lfs.ckmagic() else '?',
lfs.version.major if lfs.version is not None else '?',
lfs.version.minor if lfs.version is not None else '?',
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?',
lfs.addr(),
lfs.mbweightrepr(), lfs.mrweightrepr(),
lfs.rev,
lfs.cksum,
'' if lfs.ckgcksum() else '?'))
else:
title_ = ('bd %sx%s, %s mdir, %s btree, %s data' % (
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?',
'%.1f%%' % (100*mdir_count / max(len(bmap), 1)),
'%.1f%%' % (100*btree_count / max(len(bmap), 1)),
'%.1f%%' % (100*data_count / max(len(bmap), 1))))
# scale width/height if requested
if (to_scale is not None
and (width is None or height is None)):
@@ -5025,68 +5083,7 @@ def main(disk, output, mroots=None, *,
f.write('<text fill="%(color)s">' % dict(
color='#ffffff' if dark else '#000000'))
f.write('<tspan x="3" y="1.1em">')
if title:
f.write(punescape(title, {
'magic': 'littlefs%s' % (
'' if lfs.ckmagic() else '?'),
'version': 'v%s.%s' % (
lfs.version.major
if lfs.version is not None else '?',
lfs.version.minor
if lfs.version is not None else '?'),
'version_major': lfs.version.major
if lfs.version is not None else '?',
'version_minor': lfs.version.minor
if lfs.version is not None else '?',
'geometry': '%sx%s' % (
lfs.block_size
if lfs.block_size is not None else '?',
lfs.block_count
if lfs.block_count is not None else '?'),
'block_size': lfs.block_size
if lfs.block_size is not None else '?',
'block_count': lfs.block_count
if lfs.block_count is not None else '?',
'addr': lfs.addr(),
'weight': 'w%s.%s' % (
lfs.mbweightrepr(),
lfs.mrweightrepr()),
'mbweight': lfs.mbweightrepr(),
'mrweight': lfs.mrweightrepr(),
'cksum': '%08x%s' % (
lfs.cksum,
'' if lfs.ckgcksum() else '?'),
'total': total_count,
'total_percent': '%.1f%%' % (
100*total_count / max(len(bmap), 1)),
'mdir': mdir_count,
'mdir_percent': '%.1f%%' % (
100*mdir_count / max(len(bmap), 1)),
'btree': btree_count,
'btree_percent': '%.1f%%' % (
100*btree_count / max(len(bmap), 1)),
'data': data_count,
'data_percent': '%.1f%%' % (
100*data_count / max(len(bmap), 1)),
}))
elif not title_usage:
f.write('littlefs%s v%s.%s %sx%s %s w%s.%s, cksum %08x%s' % (
'' if lfs.ckmagic() else '?',
lfs.version.major if lfs.version is not None else '?',
lfs.version.minor if lfs.version is not None else '?',
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?',
lfs.addr(),
lfs.mbweightrepr(), lfs.mrweightrepr(),
lfs.cksum,
'' if lfs.ckgcksum() else '?'))
else:
f.writeln('bd %sx%s, %s mdir, %s btree, %s data' % (
lfs.block_size if lfs.block_size is not None else '?',
lfs.block_count if lfs.block_count is not None else '?',
'%.1f%%' % (100*mdir_count / max(len(bmap), 1)),
'%.1f%%' % (100*btree_count / max(len(bmap), 1)),
'%.1f%%' % (100*data_count / max(len(bmap), 1))))
f.write(title_)
f.write('</tspan>')
if not no_mode and not no_javascript:
f.write('<tspan id="mode" x="%(x)d" y="1.1em" '
+45 -60
View File
@@ -1722,6 +1722,50 @@ def main(path='-', *,
else:
block_cycles_ = wear_max
# build a title
if title:
title_ = punescape(title, {
'geometry': '%sx%s' % (block_size_, block_count_),
'block_size': block_size_,
'block_count': block_count_,
'total': total,
'read': readed,
'read_percent': 100*readed / max(total, 1),
'prog': proged,
'prog_percent': 100*proged / max(total, 1),
'erase': erased,
'erase_percent': 100*erased / max(total, 1),
'wear_min': wear_min if wear else '?',
'wear_min_percent':
100*wear_min / max(block_cycles_, 1) if wear else '?',
'wear_max': wear_max if wear else '?',
'wear_max_percent':
100*wear_max / max(block_cycles_, 1) if wear else '?',
'wear_avg': wear_avg if wear else '?',
'wear_avg_percent':
100*wear_avg / max(block_cycles_, 1) if wear else '?',
'wear_stddev': wear_stddev if wear else '?',
'wear_stddev_percent':
100*wear_stddev / max(block_cycles_, 1) if wear else '?',
})
else:
title_ = ('bd %dx%d%s%s%s%s' % (
block_size_, block_count_,
', %6s read' % (
'%.1f%%' % (100*readed / max(total, 1)))
if reads else '',
', %6s prog' % (
'%.1f%%' % (100*proged / max(total, 1)))
if progs else '',
', %6s erase' % (
'%.1f%%' % (100*erased / max(total, 1)))
if erases else '',
', %15s wear' % (
'%.1f%% +-%.1fσ' % (
100*wear_avg / max(block_cycles_, 1),
100*wear_stddev / max(block_cycles_, 1)))
if wear else ''))
# give ring a writeln function
def writeln(s=''):
ring.write(s)
@@ -1991,66 +2035,7 @@ def main(path='-', *,
# print some summary info
if not no_header:
# TODO
# # compute stddev of wear using our bmap, this is a bit different
# # from reads/progs/erases which ignores any bmap window, but it's
# # what we have
# if wear:
# mean = (sum(p.wear for p in bmap.pixels)
# / max(len(bmap.pixels), 1))
# stddev = mt.sqrt(sum((p.wear - mean)**2 for p in bmap.pixels)
# / max(len(bmap.pixels), 1))
# worst = max((p.wear for p in bmap.pixels), default=0)
if title:
ring.writeln(punescape(title, {
# TODO simplify this in other scripts
# geometry -> just block_size/block_count, don't eagerly
# format percents, etc
'block_size': block_size_,
'block_count': block_count_,
'total': total,
'read': readed,
# TODO if we're showing percentage of total ops here,
# should we should percentage of in-use blocks in
# dbgbmap.py?
'read_percent': 100*readed / max(total, 1),
'prog': proged,
'prog_percent': 100*proged / max(total, 1),
'erase': erased,
'erase_percent': 100*erased / max(total, 1),
'wear_min': wear_min if wear else '?',
'wear_min_percent': 100*wear_min / max(block_cycles_, 1)
if wear else '?',
'wear_max': wear_max if wear else '?',
'wear_max_percent': 100*wear_max / max(block_cycles_, 1)
if wear else '?',
'wear_avg': wear_avg if wear else '?',
'wear_avg_percent': 100*wear_avg / max(block_cycles_, 1)
if wear else '?',
'wear_stddev': wear_stddev if wear else '?',
'wear_stddev_percent':
100*wear_stddev / max(block_cycles_, 1)
if wear else '?',
}))
else:
# ring.writeln('curve: %s' % (curve.cache_info(),))
ring.writeln('bd %dx%d%s%s%s%s' % (
block_size_, block_count_,
', %6s read' % (
'%.1f%%' % (100*readed / max(total, 1)))
if reads else '',
', %6s prog' % (
'%.1f%%' % (100*proged / max(total, 1)))
if progs else '',
', %6s erase' % (
'%.1f%%' % (100*erased / max(total, 1)))
if erases else '',
', %15s wear' % (
'%.1f%% +-%.1fσ' % (
100*wear_avg / max(block_cycles_, 1),
100*wear_stddev / max(block_cycles_, 1)))
if wear else ''))
ring.writeln(title_)
# draw canvas
for row in range(canvas.height//canvas.yscale):