Files
James Tucker 2d59ed25f7 util/sizetest/symcost: attribute cost across all sections per receiver and function
Adds binary parsing that attributes cost across .text, .rodata,
.gopclntab, and .itablink, giving per-receiver-type and per-function
totals that are far closer to ground truth than the symbol-table-only
view shipped previously.

The previous symcost only saw named symbols (~600 B/flow on the
eventbus measurement program), but the binary actually grows by
~3,200 B/flow when adding a flow. The gap was 80%+ of the cost
sitting in unnamed rodata (runtime type descriptors), pclntab
(per-function metadata), and itab/itablink. This change closes most
of that gap by:

- opening the ELF binary directly (debug/elf), parsing sections,
  symbols, and pclntab (debug/gosym)
- decoding .typelink to find every runtime type descriptor and
  attributing its bytes (descriptor + name string) to the type's name
- decoding .itablink to find every interface table and attributing
  its bytes to the (concrete type, interface) pair
- distributing pclntab section bytes per function (currently a
  fixed-per-function-plus-body-proportional approximation, accurate
  to ~10% of true per-function pclntab cost; can be replaced with
  exact runtime._func parsing later)

New API:
- symcost.Open(path) opens a Go binary and returns a *Binary with
  Sections, Syms, Funcs, Types, and Itabs populated.
- (*Binary).CostByReceiver(name) returns a Cost aggregating every
  symbol/type/itab attributable to a receiver, with a per-section
  breakdown and sorted contribution lists.
- (*Binary).CostByFunction(name) returns a Cost for a single
  function (or one generic template across all instantiations).

The receiver matcher handles Go's symbol-naming conventions:
qualified ('tailscale.com/util/eventbus.Publisher') and short
('eventbus.Publisher') forms; method-receiver mangling
'pkg.(*Type[...])'; generic dictionaries 'pkg..dict.Type[...]'; and
type-descriptor short package names. It is delimiter-aware so a
query for 'main.Foo' does not accidentally match 'main.FooBar'.

The cmd/symcost CLI grows -receiver and -func flags that produce a
section breakdown plus the contributing functions, types, itabs,
and named symbols sorted by descending cost. The default discovery
mode (top-N grouped by template) is unchanged.

Function-side attribution of referenced rodata via instruction
disassembly is intentionally deferred; the current attribution gives
honest, useful answers for the question 'how much does receiver T
cost in this binary' which is the immediate need.

Currently ELF-only (the format symcost is run against in our
measurements). Mach-O and PE can be added without changing the
public API.

Updates #12614

Change-Id: I8d4a4326407220423013ffd97650e54277a155c4
Signed-off-by: James Tucker <[email protected]>
2026-05-05 22:03:05 +00:00
..