scripts: Adopted ring name for stdout substitution

This commit is contained in:
Christopher Haster
2025-04-11 01:30:55 -05:00
parent 5952431660
commit cffa9ec67e
6 changed files with 75 additions and 77 deletions
+29 -29
View File
@@ -897,7 +897,7 @@ def collect_ctx(obj_paths, *,
return ctx return ctx
def main_(f, paths, *, def main_(ring, paths, *,
namespace_depth=2, namespace_depth=2,
labels=[], labels=[],
chars=[], chars=[],
@@ -916,11 +916,11 @@ def main_(f, paths, *,
label=False, label=False,
no_label=False, no_label=False,
**args): **args):
# give f an writeln function # give ring an writeln function
def writeln(s=''): def writeln(s=''):
f.write(s) ring.write(s)
f.write('\n') ring.write('\n')
f.writeln = writeln ring.writeln = writeln
# figure out what color should be # figure out what color should be
if color == 'auto': if color == 'auto':
@@ -971,25 +971,25 @@ def main_(f, paths, *,
# elf/callgraph files # elf/callgraph files
fs = [] fs = []
for path in paths: for path in paths:
f_ = openio(path) f = openio(path)
if f_.buffer.peek(4)[:4] == b'\x7fELF': if f.buffer.peek(4)[:4] == b'\x7fELF':
for f_ in fs: for f in fs:
f_.close() f.close()
raise StopIteration() raise StopIteration()
fs.append(f_) fs.append(f)
for f_ in fs: for f in fs:
with f_: with f:
# csv or json? assume json starts with [ # csv or json? assume json starts with [
is_json = (f_.buffer.peek(1)[:1] == b'[') is_json = (f.buffer.peek(1)[:1] == b'[')
# read csv? # read csv?
if not is_json: if not is_json:
results.extend(csv.DictReader(f_, restval='')) results.extend(csv.DictReader(f, restval=''))
# read json? # read json?
else: else:
results.extend(json.load(f_)) results.extend(json.load(f))
# fall back to extracting code/stack/ctx info from elf/callgraph files # fall back to extracting code/stack/ctx info from elf/callgraph files
except StopIteration: except StopIteration:
@@ -1069,29 +1069,29 @@ def main_(f, paths, *,
return f['frame'] + limit return f['frame'] + limit
for k, f_ in functions.items(): for k, f in functions.items():
if 'stack' in f_: if 'stack' in f:
if mt.isinf(f_['stack']): if mt.isinf(f['stack']):
f_['limit'] = limitof(k, f_) f['limit'] = limitof(k, f)
else: else:
f_['limit'] = f_['stack'] f['limit'] = f['stack']
# organize into subsystems # organize into subsystems
namespace_pattern = re.compile('_*[^_]+(?:_*$)?') namespace_pattern = re.compile('_*[^_]+(?:_*$)?')
namespace_slice = slice(namespace_depth if namespace_depth else None) namespace_slice = slice(namespace_depth if namespace_depth else None)
subsystems = {} subsystems = {}
for k, f_ in functions.items(): for k, f in functions.items():
# ignore leading/trailing underscores # ignore leading/trailing underscores
f_['subsystem'] = ''.join( f['subsystem'] = ''.join(
namespace_pattern.findall(k)[ namespace_pattern.findall(k)[
namespace_slice]) namespace_slice])
if f_['subsystem'] not in subsystems: if f['subsystem'] not in subsystems:
subsystems[f_['subsystem']] = {'name': f_['subsystem']} subsystems[f['subsystem']] = {'name': f['subsystem']}
# include ctx in subsystems to give them different colors # include ctx in subsystems to give them different colors
for _, f_ in functions.items(): for _, f in functions.items():
for a in f_.get('args', []): for a in f.get('args', []):
a['subsystem'] = a['name'] a['subsystem'] = a['name']
if a['subsystem'] not in subsystems: if a['subsystem'] not in subsystems:
@@ -1339,9 +1339,9 @@ def main_(f, paths, *,
# print some summary info # print some summary info
if not no_header: if not no_header:
if title: if title:
f.writeln(punescape(title, totals['attrs'] | totals)) ring.writeln(punescape(title, totals['attrs'] | totals))
else: else:
f.writeln('code %d stack %s ctx %d' % ( ring.writeln('code %d stack %s ctx %d' % (
totals.get('code', 0), totals.get('code', 0),
(lambda s: '' if mt.isinf(s) else s)( (lambda s: '' if mt.isinf(s) else s)(
totals.get('stack', 0)), totals.get('stack', 0)),
@@ -1350,7 +1350,7 @@ def main_(f, paths, *,
# draw canvas # draw canvas
for row in range(canvas.height//canvas.yscale): for row in range(canvas.height//canvas.yscale):
line = canvas.draw(row) line = canvas.draw(row)
f.writeln(line) ring.writeln(line)
if (args.get('error_on_recursion') if (args.get('error_on_recursion')
and mt.isinf(totals.get('stack', 0))): and mt.isinf(totals.get('stack', 0))):
+15 -15
View File
@@ -4327,7 +4327,7 @@ class BmapBlock:
} }
def main_(f, disk, mroots=None, *, def main_(ring, disk, mroots=None, *,
trunk=None, trunk=None,
block_size=None, block_size=None,
block_count=None, block_count=None,
@@ -4357,11 +4357,11 @@ def main_(f, disk, mroots=None, *,
title_usage=False, title_usage=False,
padding=0, padding=0,
**args): **args):
# give f an writeln function # give ring an writeln function
def writeln(s=''): def writeln(s=''):
f.write(s) ring.write(s)
f.write('\n') ring.write('\n')
f.writeln = writeln ring.writeln = writeln
# figure out what color should be # figure out what color should be
if color == 'auto': if color == 'auto':
@@ -4435,14 +4435,14 @@ def main_(f, disk, mroots=None, *,
None)) None))
# we seek around a bunch, so just keep the disk open # we seek around a bunch, so just keep the disk open
with open(disk, 'rb') as f_: with open(disk, 'rb') as f:
# if block_size is omitted, assume the block device is one big block # if block_size is omitted, assume the block device is one big block
if block_size is None: if block_size is None:
f_.seek(0, os.SEEK_END) f.seek(0, os.SEEK_END)
block_size = f_.tell() block_size = f.tell()
# fetch the filesystem # fetch the filesystem
bd = Bd(f_, block_size, block_count) bd = Bd(f, block_size, block_count)
lfs = Lfs.fetch(bd, mroots, trunk, lfs = Lfs.fetch(bd, mroots, trunk,
# don't bother to check things if we're not reporting errors # don't bother to check things if we're not reporting errors
no_ck=not args.get('error_on_corrupt')) no_ck=not args.get('error_on_corrupt'))
@@ -4455,8 +4455,8 @@ def main_(f, disk, mroots=None, *,
if lfs.config.geometry is not None: if lfs.config.geometry is not None:
block_count_ = lfs.config.geometry.block_count block_count_ = lfs.config.geometry.block_count
else: else:
f_.seek(0, os.SEEK_END) f.seek(0, os.SEEK_END)
block_count_ = mt.ceil(f_.tell() / block_size) block_count_ = mt.ceil(f.tell() / block_size)
# flatten blocks, default to all blocks # flatten blocks, default to all blocks
blocks_ = list( blocks_ = list(
@@ -4755,7 +4755,7 @@ def main_(f, disk, mroots=None, *,
# print some summary info # print some summary info
if not no_header: if not no_header:
if title: if title:
f.writeln(punescape(title, { ring.writeln(punescape(title, {
'magic': 'littlefs%s' % ( 'magic': 'littlefs%s' % (
'' if lfs.ckmagic() else '?'), '' if lfs.ckmagic() else '?'),
'version': 'v%s.%s' % ( 'version': 'v%s.%s' % (
@@ -4799,7 +4799,7 @@ def main_(f, disk, mroots=None, *,
100*data_count / max(len(bmap), 1)), 100*data_count / max(len(bmap), 1)),
})) }))
elif title_littlefs: elif title_littlefs:
f.writeln('littlefs%s v%s.%s %sx%s %s w%s.%s, cksum %08x%s' % ( ring.writeln('littlefs%s v%s.%s %sx%s %s w%s.%s, cksum %08x%s' % (
'' if lfs.ckmagic() else '?', '' if lfs.ckmagic() else '?',
lfs.version.major if lfs.version is not None else '?', lfs.version.major if lfs.version is not None else '?',
lfs.version.minor if lfs.version is not None else '?', lfs.version.minor if lfs.version is not None else '?',
@@ -4810,7 +4810,7 @@ def main_(f, disk, mroots=None, *,
lfs.cksum, lfs.cksum,
'' if lfs.ckgcksum() else '?')) '' if lfs.ckgcksum() else '?'))
else: else:
f.writeln('bd %sx%s, %6s mdir, %6s btree, %6s data' % ( ring.writeln('bd %sx%s, %6s mdir, %6s btree, %6s data' % (
lfs.block_size if lfs.block_size 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.block_count if lfs.block_count is not None else '?',
'%.1f%%' % (100*mdir_count / max(len(bmap), 1)), '%.1f%%' % (100*mdir_count / max(len(bmap), 1)),
@@ -4820,7 +4820,7 @@ def main_(f, disk, mroots=None, *,
# draw canvas # draw canvas
for row in range(canvas.height//canvas.yscale): for row in range(canvas.height//canvas.yscale):
line = canvas.draw(row) line = canvas.draw(row)
f.writeln(line) ring.writeln(line)
if args.get('error_on_corrupt') and corrupted: if args.get('error_on_corrupt') and corrupted:
sys.exit(2) sys.exit(2)
+22 -22
View File
@@ -1152,7 +1152,7 @@ class Grid:
return grid return grid
def main_(f, csv_paths, *, def main_(ring, csv_paths, *,
by=None, by=None,
x=None, x=None,
y=None, y=None,
@@ -1187,11 +1187,11 @@ def main_(f, csv_paths, *,
subplot={}, subplot={},
subplots=[], subplots=[],
**args): **args):
# give f an writeln function # give ring an writeln function
def writeln(s=''): def writeln(s=''):
f.write(s) ring.write(s)
f.write('\n') ring.write('\n')
f.writeln = writeln ring.writeln = writeln
# figure out what color should be # figure out what color should be
if color == 'auto': if color == 'auto':
@@ -1643,14 +1643,14 @@ def main_(f, csv_paths, *,
# draw title? # draw title?
for line in title_: for line in title_:
f.writeln('%*s%s' % ( ring.writeln('%*s%s' % (
sum(xmargin[:2]), '', sum(xmargin[:2]), '',
line.center(width_-xmargin[1]))) line.center(width_-xmargin[1])))
# draw legend_above? # draw legend_above?
if legend_above and legend_: if legend_above and legend_:
for i in range(0, len(legend_), legend_cols): for i in range(0, len(legend_), legend_cols):
f.writeln('%*s%s' % ( ring.writeln('%*s%s' % (
max( max(
sum(xmargin[:2]) sum(xmargin[:2])
+ (width_-xmargin[1] + (width_-xmargin[1]
@@ -1665,7 +1665,7 @@ def main_(f, csv_paths, *,
for row in range(height_): for row in range(height_):
# draw ylabel? # draw ylabel?
f.write('%s ' % ''.join( ring.write('%s ' % ''.join(
('%*s%s%*s' % ( ('%*s%s%*s' % (
ymargin[-1], '', ymargin[-1], '',
line.center(height_-sum(ymargin)), line.center(height_-sum(ymargin)),
@@ -1688,11 +1688,11 @@ def main_(f, csv_paths, *,
if subrow < s.ymargin[-1]: if subrow < s.ymargin[-1]:
# draw subtitle? # draw subtitle?
if subrow < len(s.title_): if subrow < len(s.title_):
f.write('%*s%s' % ( ring.write('%*s%s' % (
sum(s.xmargin[:2]), '', sum(s.xmargin[:2]), '',
s.title_[subrow].center(s.width_))) s.title_[subrow].center(s.width_)))
else: else:
f.write('%*s%*s' % ( ring.write('%*s%*s' % (
sum(s.xmargin[:2]), '', sum(s.xmargin[:2]), '',
s.width_, '')) s.width_, ''))
# draw plot? # draw plot?
@@ -1700,7 +1700,7 @@ def main_(f, csv_paths, *,
subrow = subrow-s.ymargin[-1] subrow = subrow-s.ymargin[-1]
# draw ysublabel? # draw ysublabel?
f.write('%-*s' % ( ring.write('%-*s' % (
s.xmargin[0], s.xmargin[0],
'%s ' % ''.join( '%s ' % ''.join(
line.center(s.height_)[subrow] line.center(s.height_)[subrow]
@@ -1709,25 +1709,25 @@ def main_(f, csv_paths, *,
# draw yunits? # draw yunits?
if subrow == 0 and s.yticklabels_ != []: if subrow == 0 and s.yticklabels_ != []:
f.write('%*s' % ( ring.write('%*s' % (
s.xmargin[1], s.xmargin[1],
((si2 if s.y2 else si)(s.ylim_[1]) + s.yunits ((si2 if s.y2 else si)(s.ylim_[1]) + s.yunits
if s.yticklabels_ is None if s.yticklabels_ is None
else s.yticklabels_[1]) else s.yticklabels_[1])
+ ' ')) + ' '))
elif subrow == s.height_-1 and s.yticklabels_ != []: elif subrow == s.height_-1 and s.yticklabels_ != []:
f.write('%*s' % ( ring.write('%*s' % (
s.xmargin[1], s.xmargin[1],
((si2 if s.y2 else si)(s.ylim_[0]) + s.yunits ((si2 if s.y2 else si)(s.ylim_[0]) + s.yunits
if s.yticklabels_ is None if s.yticklabels_ is None
else s.yticklabels_[0]) else s.yticklabels_[0])
+ ' ')) + ' '))
else: else:
f.write('%*s' % ( ring.write('%*s' % (
s.xmargin[1], '')) s.xmargin[1], ''))
# draw plot! # draw plot!
f.write(s.plot_.draw(subrow)) ring.write(s.plot_.draw(subrow))
# footer # footer
else: else:
@@ -1735,7 +1735,7 @@ def main_(f, csv_paths, *,
# draw xunits? # draw xunits?
if subrow < (1 if s.xticklabels_ != [] else 0): if subrow < (1 if s.xticklabels_ != [] else 0):
f.write('%*s%-*s%*s%*s' % ( ring.write('%*s%-*s%*s%*s' % (
sum(s.xmargin[:2]), '', sum(s.xmargin[:2]), '',
(5 if s.x2 else 4) + len(s.xunits) (5 if s.x2 else 4) + len(s.xunits)
if s.xticklabels_ is None if s.xticklabels_ is None
@@ -1756,11 +1756,11 @@ def main_(f, csv_paths, *,
# draw xsublabel? # draw xsublabel?
elif (subrow < s.ymargin[1] elif (subrow < s.ymargin[1]
or subrow-s.ymargin[1] >= len(s.xlabel_)): or subrow-s.ymargin[1] >= len(s.xlabel_)):
f.write('%*s%*s' % ( ring.write('%*s%*s' % (
sum(s.xmargin[:2]), '', sum(s.xmargin[:2]), '',
s.width_, '')) s.width_, ''))
else: else:
f.write('%*s%s' % ( ring.write('%*s%s' % (
sum(s.xmargin[:2]), '', sum(s.xmargin[:2]), '',
s.xlabel_[subrow-s.ymargin[1]] s.xlabel_[subrow-s.ymargin[1]]
.center(s.width_))) .center(s.width_)))
@@ -1770,23 +1770,23 @@ def main_(f, csv_paths, *,
and row >= ymargin[-1] and row >= ymargin[-1]
and row-ymargin[-1] < len(legend_)): and row-ymargin[-1] < len(legend_)):
j = row-ymargin[-1] j = row-ymargin[-1]
f.write(' %s%s%s' % ( ring.write(' %s%s%s' % (
'\x1b[%sm' % legend_[j][1] if color else '', '\x1b[%sm' % legend_[j][1] if color else '',
legend_[j][0], legend_[j][0],
'\x1b[m' if color else '')) '\x1b[m' if color else ''))
f.writeln() ring.writeln()
# draw xlabel? # draw xlabel?
for line in xlabel_: for line in xlabel_:
f.writeln('%*s%s' % ( ring.writeln('%*s%s' % (
sum(xmargin[:2]), '', sum(xmargin[:2]), '',
line.center(width_-xmargin[1]))) line.center(width_-xmargin[1])))
# draw legend below? # draw legend below?
if legend_below and legend_: if legend_below and legend_:
for i in range(0, len(legend_), legend_cols): for i in range(0, len(legend_), legend_cols):
f.writeln('%*s%s' % ( ring.writeln('%*s%s' % (
max( max(
sum(xmargin[:2]) sum(xmargin[:2])
+ (width_-xmargin[1] + (width_-xmargin[1]
-1
View File
@@ -134,7 +134,6 @@ def main(path='-', *,
lock = th.Lock() lock = th.Lock()
event = th.Event() event = th.Event()
# TODO adopt f -> ring name in all scripts?
def main_(ring): def main_(ring):
try: try:
while True: while True:
-1
View File
@@ -1697,7 +1697,6 @@ def main(path='-', *,
else: else:
curve = ft.lru_cache(16)(lambda w, h: list(naive_curve(w, h))) curve = ft.lru_cache(16)(lambda w, h: list(naive_curve(w, h)))
# TODO adopt f -> ring name in all scripts?
def draw__(ring, width, height): def draw__(ring, width, height):
nonlocal bmap nonlocal bmap
# still waiting on bd init # still waiting on bd init
+9 -9
View File
@@ -908,7 +908,7 @@ def partition_squarify(children, total, x, y, width, height, *,
i = j i = j
def main_(f, csv_paths, *, def main_(ring, csv_paths, *,
by=None, by=None,
fields=None, fields=None,
defines=[], defines=[],
@@ -930,11 +930,11 @@ def main_(f, csv_paths, *,
label=False, label=False,
no_label=False, no_label=False,
**args): **args):
# give f an writeln function # give ring an writeln function
def writeln(s=''): def writeln(s=''):
f.write(s) ring.write(s)
f.write('\n') ring.write('\n')
f.writeln = writeln ring.writeln = writeln
# figure out what color should be # figure out what color should be
if color == 'auto': if color == 'auto':
@@ -1215,19 +1215,19 @@ def main_(f, csv_paths, *,
stat['mean'], stat['stddev'], stat['mean'], stat['stddev'],
stat['min'], stat['max']) stat['min'], stat['max'])
if title and not no_stats: if title and not no_stats:
f.writeln('%s%*s%s' % ( ring.writeln('%s%*s%s' % (
title_, title_,
max(width_-len(stat_)-len(title_), 0), ' ', max(width_-len(stat_)-len(title_), 0), ' ',
stat_)) stat_))
elif title: elif title:
f.writeln(title_) ring.writeln(title_)
elif not no_stats: elif not no_stats:
f.writeln(stat_) ring.writeln(stat_)
# draw canvas # draw canvas
for row in range(canvas.height//canvas.yscale): for row in range(canvas.height//canvas.yscale):
line = canvas.draw(row) line = canvas.draw(row)
f.writeln(line) ring.writeln(line)
def main(csv_paths, *, def main(csv_paths, *,