scripts: Added -l/--labels to csv.py

This gives csv.py access to a hidden feature in our table renderer used
by some of the other scripts: fields that affect by-field grouping, but
aren't actually printed.

For example, this prevents summing same named functions in different
files, but only shows the function name in the table render:

  $ ./scripts/csv.py lfs.code.csv -bfile -bfunction -lfunction
  function                                size
  lfs_alloc                                398
  lfs_alloc_discard                         31
  lfs_alloc_findfree                        77
  ...

This is especially useful when enumerating results. For example, this
prevents any summing without extra table noise:

  $ ./scripts/csv.py lfs.code.csv -i -bfunction -fsize -lfunction
  function                                size
  lfs_alloc                                398
  lfs_alloc_discard                         31
  lfs_alloc_findfree                        77
  ...

I also tweaked -b/--by field defaults a bit to account to
enumerate/label fields a bit better.
This commit is contained in:
Christopher Haster
2025-02-28 02:07:20 -06:00
parent 748815bb46
commit b2768becaa
9 changed files with 270 additions and 115 deletions

View File

@@ -661,6 +661,9 @@ def table(Result, results, diff_results=None, *,
by=None,
fields=None,
sort=None,
labels=None,
depth=1,
hot=None,
diff=None,
percent=None,
all=False,
@@ -670,8 +673,6 @@ def table(Result, results, diff_results=None, *,
no_total=False,
small_table=False,
summary=False,
depth=1,
hot=None,
**_):
import builtins
all_, all = all, builtins.all
@@ -715,7 +716,7 @@ def table(Result, results, diff_results=None, *,
# header
if not no_header:
header = ['%s%s' % (
','.join(by),
','.join(labels if labels is not None else by),
' (%d added, %d removed)' % (
sum(1 for n in table if n not in diff_table),
sum(1 for n in diff_table if n not in table))
@@ -741,7 +742,9 @@ def table(Result, results, diff_results=None, *,
# entry helper
def table_entry(name, r, diff_r=None):
# prepend name
entry = [name]
# normal entry?
if ((compare is None or r == compare_r)
and not percent
@@ -800,6 +803,7 @@ def table(Result, results, diff_results=None, *,
types[k].ratio(
getattr(r, k, None),
getattr(diff_r, k, None)))))
# append any notes
if hasattr(Result, '_notes') and r is not None:
notes = sorted(getattr(r, Result._notes))
@@ -871,13 +875,22 @@ def table(Result, results, diff_results=None, *,
# and finally by name (diffs may be missing results)
n))
for i, n in enumerate(names_):
for i, name in enumerate(names_):
# find comparable results
r = table_.get(n)
diff_r = diff_table_.get(n)
r = table_.get(name)
diff_r = diff_table_.get(name)
# figure out a good label
if labels is not None:
label = ','.join(str(getattr(r, k)
if getattr(r, k) is not None
else '')
for k in labels)
else:
label = name
# build line
line = table_entry(n, r, diff_r)
line = table_entry(label, r, diff_r)
# add prefixes
line = [x if isinstance(x, tuple) else (x, []) for x in line]
@@ -885,7 +898,7 @@ def table(Result, results, diff_results=None, *,
lines.append(line)
# recurse?
if n in table_ and depth_ > 1:
if name in table_ and depth_ > 1:
table_recurse(
getattr(r, Result._children),
getattr(diff_r, Result._children, None) or [],
@@ -1110,8 +1123,7 @@ def main(obj_paths, *,
depth=depth,
**args)
if args.get('output_json'):
write_csv(args['output_json'], StructResult, results,
json=True,
write_csv(args['output_json'], StructResult, results, json=True,
by=by,
fields=fields,
depth=depth,
@@ -1138,9 +1150,10 @@ def main(obj_paths, *,
# print table
if not args.get('quiet'):
table(StructResult, results, diff_results,
by=by if by is not None else ['struct'],
by=by if by is not None else ['z', 'i', 'struct'],
fields=fields if fields is not None else ['size', 'align'],
sort=sort,
labels=by if by is not None else ['struct'],
depth=depth,
**args)