Repository navigation
runtime: fix native allocation event reporting #3043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cpunion
merged 4 commits into
xgo-dev:main
from
cpunion:fix/2923-bdwgc-allocation-counters
Nov 12, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
17da929
runtime: report native managed allocation events in MemStats
cpunion d2da712
runtime: keep unavailable BDWGC live object metrics unset
cpunion 9ee32a6
ci: fall back across official Ubuntu HTTPS mirrors
cpunion af333c3
test: check exact native allocation event counts
cpunion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package build | ||
|
|
||
| import ( | ||
| "os/exec" | ||
| "path/filepath" | ||
| "runtime" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/xgo-dev/llgo/internal/lto" | ||
| ) | ||
|
|
||
| func TestNativeAllocationEventsExactIntegration(t *testing.T) { | ||
| if testing.Short() || (runtime.GOOS != "darwin" && runtime.GOOS != "linux" && runtime.GOOS != "windows") { | ||
| t.Skip("builds and runs a native BDWGC allocation probe") | ||
| } | ||
| root, err := filepath.Abs("../..") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Setenv("LLGO_ROOT", root) | ||
| for _, mode := range []lto.Mode{lto.Off, lto.Full} { | ||
| t.Run(mode.String(), func(t *testing.T) { | ||
| conf := NewDefaultConf(ModeBuild) | ||
| conf.LTO = mode | ||
| conf.OutFile = filepath.Join(t.TempDir(), "allocation-events") | ||
| if runtime.GOOS == "windows" { | ||
| conf.OutFile += ".exe" | ||
| } | ||
| if _, err := Build(Invocation{Args: []string{"."}, Config: conf, Dir: filepath.Join(root, "internal/build/testdata/allocation_events")}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for i := 0; i < 10; i++ { | ||
| out, err := exec.CommandContext(t.Context(), conf.OutFile).CombinedOutput() | ||
| if err != nil || strings.TrimSpace(string(out)) != "allocation events: OK" { | ||
| t.Fatalf("exact allocation events: %v\n%s", err, out) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module allocationevents | ||
|
|
||
| go 1.27.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "runtime" | ||
| ) | ||
|
|
||
| var bytesSink []byte | ||
| var pointerSink *node | ||
| var before, after runtime.MemStats | ||
|
|
||
| type node struct { | ||
| next *node | ||
| data int | ||
| } | ||
|
|
||
| //go:noinline | ||
| func allocate(size int) { | ||
| if size == 0 { | ||
| pointerSink = &node{next: pointerSink, data: 42} | ||
| } else { | ||
| bytesSink = make([]byte, size) | ||
| } | ||
| } | ||
|
|
||
| func main() { | ||
| // Run without testing's background goroutines, whose allocations would | ||
| // contribute to the same process-wide counter during the measurement. | ||
| for _, size := range []int{1, 1024, 65536, 0} { | ||
| for i := 0; i < 100; i++ { | ||
| allocate(size) | ||
| } | ||
| runtime.ReadMemStats(&before) | ||
| runtime.ReadMemStats(&after) | ||
| runtime.ReadMemStats(&before) | ||
| const count = 1000 | ||
| for i := 0; i < count; i++ { | ||
| allocate(size) | ||
| } | ||
| runtime.ReadMemStats(&after) | ||
| if got := after.Mallocs - before.Mallocs; got != count { | ||
| panic(fmt.Sprintf("size=%d: %d allocations report %d events", size, count, got)) | ||
| } | ||
| } | ||
| fmt.Println("allocation events: OK") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| //go:build llgo && !baremetal && !wasm && !nogc | ||
|
|
||
| package metrics_test | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "runtime/metrics" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestBDWGCObjectMetrics(t *testing.T) { | ||
| // BDWGC reports allocation events, but not live/free object counts. | ||
| // The live metric must not silently become a cumulative allocation count. | ||
| runtime.GC() | ||
| samples := []metrics.Sample{ | ||
| {Name: "/gc/heap/allocs:objects"}, | ||
| {Name: "/gc/heap/frees:objects"}, | ||
| {Name: "/gc/heap/objects:objects"}, | ||
| } | ||
| metrics.Read(samples) | ||
| if samples[0].Value.Uint64() == 0 { | ||
| t.Fatal("allocation metric reports no events") | ||
| } | ||
| for _, sample := range samples[1:] { | ||
| if got := sample.Value.Uint64(); got != 0 { | ||
| t.Fatalf("unavailable %s = %d, want 0", sample.Name, got) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //go:build !nogc | ||
|
|
||
| package testing_test | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "testing" | ||
| ) | ||
|
|
||
| var allocationEventSink []byte | ||
| var allocationEventPointerSink *allocationEventNode | ||
|
|
||
| type allocationEventNode struct { | ||
| next *allocationEventNode | ||
| data int | ||
| } | ||
|
|
||
| //go:noinline | ||
| func allocateEventControl(size int) { | ||
| allocationEventSink = make([]byte, size) | ||
| } | ||
|
|
||
| //go:noinline | ||
| func allocatePointerEventControl() { | ||
| allocationEventPointerSink = &allocationEventNode{next: allocationEventPointerSink, data: 42} | ||
| } | ||
|
|
||
| func TestAllocationEventControls(t *testing.T) { | ||
| for _, size := range []int{1, 16, 1024, 65536} { | ||
| if n := testing.AllocsPerRun(100, func() { allocateEventControl(size) }); n < 1 { | ||
|
cpunion marked this conversation as resolved.
Resolved
|
||
| t.Fatalf("escaping %d-byte allocation reports %v allocations, want >= 1", size, n) | ||
| } | ||
| } | ||
| if n := testing.AllocsPerRun(100, allocatePointerEventControl); n < 1 { | ||
| t.Fatalf("escaping pointer-bearing object reports %v allocations, want >= 1", n) | ||
| } | ||
| if n := testing.AllocsPerRun(100, func() { allocationEventSink[0]++ }); n != 0 { | ||
| t.Fatalf("in-place mutation reports %v allocations, want 0", n) | ||
| } | ||
| if n := testing.AllocsPerRun(100, func() { allocateEventControl(0) }); n != 0 { | ||
| t.Fatalf("zero-sized allocation reports %v allocations, want 0", n) | ||
| } | ||
| // Keep snapshot storage outside the measured callback so this checks | ||
| // ReadMemStats itself, independently of caller-side placement decisions. | ||
| var stats runtime.MemStats | ||
| if n := testing.AllocsPerRun(100, func() { | ||
| runtime.ReadMemStats(&stats) | ||
| }); n != 0 { | ||
| t.Fatalf("ReadMemStats reports %v allocations, want 0", n) | ||
| } | ||
| } | ||
|
|
||
| func TestReadMemStatsAllocationEvents(t *testing.T) { | ||
| allocateEventControl(1024) | ||
| var before, after runtime.MemStats | ||
| runtime.ReadMemStats(&before) | ||
| for i := 0; i < 1000; i++ { | ||
| allocateEventControl(1024) | ||
| } | ||
| runtime.ReadMemStats(&after) | ||
| if count := after.Mallocs - before.Mallocs; count < 1000 { | ||
| t.Fatalf("1000 escaping allocations report %d events, want >= 1000", count) | ||
| } | ||
| if after.TotalAlloc <= before.TotalAlloc { | ||
| t.Fatal("escaping allocations did not increase TotalAlloc") | ||
| } | ||
| runtime.KeepAlive(allocationEventSink) | ||
| } | ||
|
|
||
| func TestBenchmarkAllocationEvents(t *testing.T) { | ||
| result := testing.Benchmark(func(b *testing.B) { | ||
| b.ReportAllocs() | ||
| for i := 0; i < b.N; i++ { | ||
| allocateEventControl(1024) | ||
| } | ||
| }) | ||
| if result.AllocsPerOp() < 1 || result.AllocedBytesPerOp() == 0 { | ||
| t.Fatalf("escaping benchmark allocation reports %s", result.MemString()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.