Skip to content

runtime: add minimal MemProfile allocation records#1902

Draft
cpunion wants to merge 2 commits into
xgo-dev:mainfrom
cpunion:codex/goroot-profile-coverage
Draft

runtime: add minimal MemProfile allocation records#1902
cpunion wants to merge 2 commits into
xgo-dev:mainfrom
cpunion:codex/goroot-profile-coverage

Conversation

@cpunion
Copy link
Copy Markdown
Collaborator

@cpunion cpunion commented May 22, 2026

Summary

  • Track LLGO runtime allocations in fixed MemProfile size-class buckets and expose runtime.MemProfile with the Go public MemProfileRecord/Stack0 shape.
  • Add stable test/go/memprofile coverage for the MemProfile sizing loop and tiny allocation bucket.
  • Remove the now-fixed finprofiled.go GOROOT xfails only; heapsampling.go remains xfailed because stack-attributed heap sampling is still missing.

Tests

  • go test ./test/go/memprofile -run TestRuntimeMemProfileReportsTinyAllocations -count=1
  • (cd runtime && go test ./internal/runtime ./internal/lib/runtime -count=1)
  • go build -tags=dev -o /tmp/llgo-profile ./cmd/llgo
  • /tmp/llgo-profile test -run TestRuntimeMemProfileReportsTinyAllocations -count=1 ./test/go/memprofile
  • go test ./test/goroot -run TestGoRootRunCases -count=1 -timeout=10m -args -goroot "$(go env GOROOT)" -dirs . -case '^finprofiled\\.go$' -xfail test/goroot/no-xfail.yaml -run-timeout=3m -build-timeout=5m
  • go test ./test/goroot -run TestGoRootRunCases -count=1 -timeout=10m -args -goroot "$(go env GOROOT)" -dirs . -case '^finprofiled\\.go$' -run-timeout=3m -build-timeout=5m

Notes

  • Full GOROOT CI is slow/disabled, so this PR uses targeted local GOROOT runs plus normal PR CI from the fork branch.
  • Manual GOROOT workflow_dispatch was not run. If it cannot resolve fork refs, keep relying on normal PR CI rather than pushing a branch to xgo-dev/llgo.
  • Bounded heapsampling.go check with xfail disabled still fails: panic: main.allocInterleaved1:0 want objects in [45000: 55000], got [0 0 0], which is the remaining stack-attributed heap sampling gap.

@cpunion
Copy link
Copy Markdown
Collaborator Author

cpunion commented May 22, 2026

Follow-up for head e68aeb7.

Root cause:

  • The MemProfile allocation counter used a uint64 atomic add in runtime/internal/runtime.recordMemProfileAlloc. On ESP32-C3/riscv32 baremetal this lowered to __atomic_fetch_add_8, which is not provided by the target runtime, so build-cache, Targets, and LLGo embedded/demo jobs failed at link time.
  • runtime/pprof still had an empty pprof_memProfileInternal stub, so the heap-profile path was not wired to the new MemProfile data.

Fix:

  • Split MemProfile counters by build mode: non-baremetal keeps atomic uint64 counters; baremetal uses a plain uintptr counter to avoid 64-bit atomic helper symbols on riscv32.
  • Added Go-version-aware runtime/pprof memProfileInternal adapters: Go 1.23+ uses the internal/profilerecord slice-stack layout, older Go keeps the runtime.MemProfileRecord layout.
  • Added test/go coverage for runtime/pprof heap profile allocation totals in addition to direct runtime.MemProfile coverage.

Local tests:

  • go test ./test/go/memprofile -count=1 -v
  • go test ./test/go -count=1
  • (cd runtime && go test ./internal/runtime ./internal/lib/runtime -count=1)
  • go build -tags=dev -o /tmp/llgo-profile ./cmd/llgo
  • LLGO_ROOT=/Users/lijie/source/goplus/llgo-wt-goroot-profile /tmp/llgo-profile test -run 'TestRuntime(MemProfile|PprofHeapProfile)ReportsTinyAllocations' -count=1 ./test/go/memprofile
  • LLGO_ROOT=/Users/lijie/source/goplus/llgo-wt-goroot-profile /tmp/llgo-profile build -target=esp32c3 -o /tmp/llgo-buildcache.elf -v . (from test/buildcache)
  • LLGO_ROOT=/Users/lijie/source/goplus/llgo-wt-goroot-profile /tmp/llgo-profile build -target=esp32c3 -o /tmp/llgo-target-defer.elf -v ./defer (from _demo/embed/targetsbuild)
  • go test ./cl ./ssa -count=1
  • go test ./test/goroot -run TestGoRootRunCases -count=1 -timeout=10m -args -goroot "$(go env GOROOT)" -dirs . -case '^finprofiled.go$' -xfail test/goroot/no-xfail.yaml -run-timeout=3m -build-timeout=5m
  • git diff --check

CI status as of 2026-05-22 18:34 UTC:

  • No failed checks on the new head.
  • 28 checks have passed, including build-cache ubuntu, Targets ubuntu, Go ubuntu test with embedded emulator env, LLGo ubuntu 1.21/1.24/1.26, coverage ubuntu, install docs ubuntu, fmt, and linux release artifacts.
  • 27 checks are still queued, all macOS jobs. I will keep watching and iterate if any macOS failure appears.

@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants