scripts: Added ctx.py for finding function contexts

ctx.py reports functions' "contexts", i.e. the sum of the size of all
function parameters and indirect structs, recursively dereferencing
pointers when possible.

The idea is this should give us a rough lower bound on the amount of
state that needs to be allocated to call the function:

  $ ./scripts/ctx.py lfs.o lfs_util.o -Dfunction=lfsr_file_write -z3 -s
  function                size
  lfsr_file_write          596
  |-> lfs                  436
  |   '-> lfs_t            432
  |-> file                 152
  |   '-> lfsr_file_t      148
  |-> buffer                 4
  '-> size                   4
  TOTAL                    596

---

The long story short is that structs.py, while very useful for
introspection, has not been useful as a general metric.

Sure it can give you a rough idea of the impact of small changes to
struct sizes, but it's not uncommon for larger changes to add/remove
structs that have no real impact on the user facing RAM usage. There are
some structs we care about (lfs_t) and some we don't (lfsr_data_t).
Internal-only structs should already be measured by stack.py.

Which raises the question, how do we know which structs we care about?

The idea here is to look at function parameters and chase pointers. This
gives a complicated, but I think reasonable, heuristic. Fortunately
dwarf-info gives us all the necessary info.

Some notes:

- This does _not_ include buffer sizes. Buffer sizes are user
  configurable, so it's sort of up to the user to account for these.

- We include structs once if we find a cycle (lfsr_file_t.o for
  example). Can't really do any better and this at least provides a
  lower bound for complex data-structures.

- We sum all params/fields, but find the max of all functions. Note this
  prevents common types (lfs_t for example) from being counted more than
  once.

- We only include global functions (based on the symbol flag). In theory
  the context of all internal functions should end up in stack.py.

  This can be overridden with --everything.

Note this doesn't replace structs.py. structs.py is still useful for
looking at all structs in the system. ctx.py should just be more useful
for comparing builds at a high level.
This commit is contained in:
Christopher Haster
2024-11-30 01:32:09 -06:00
parent 25814ed5cb
commit 2df97cd858

1232
scripts/ctx.py Executable file

File diff suppressed because it is too large Load Diff