Skip to content

Instantly share code, notes, and snippets.

@grantseltzer
Last active February 27, 2025 18:29
Show Gist options
  • Save grantseltzer/74bb893c51b49647cc5315e04b34c381 to your computer and use it in GitHub Desktop.
Save grantseltzer/74bb893c51b49647cc5315e04b34c381 to your computer and use it in GitHub Desktop.

Reproduction Example:

go test -c -gcflags=all="-N -l" -timeout 30s -run ^TestLoadFunctionDefinitions_Heap$ dwarf_repro && ./dwarf_repro.test -test.v
=== RUN   TestLoadFunctionDefinitions_Heap
    dwarf_test.go:94: After 1000 iterations: base alloc = 1934624, final alloc = 340592, diff = 18446744073707957584
--- PASS: TestLoadFunctionDefinitions_Heap (19.92s)
PASS
package main
import (
"debug/dwarf"
"io"
)
func loadFunctionDefinitions(dwarfData *dwarf.Data) {
entryReader := dwarfData.Reader()
var (
entry *dwarf.Entry
err error
)
for {
entry, err = entryReader.Next()
if err == io.EOF || entry == nil {
break
}
}
}
// Find the index of the desired sample type
var inuseSpaceIndex int = -1
for i, st := range prof.SampleType {
if st.Type == "inuse_space" {
fmt.Println("Type for in use:", i)
inuseSpaceIndex = i
break
} else {
fmt.Println("type:", st.Type)
}
}
if inuseSpaceIndex == -1 {
log.Fatalf("sample type not found in profile")
}
// The heap profile contains samples with location info.
// We count objects whose stack traces contain "(*dwarf.Entry)".
var entryCount int64
for _, sample := range prof.Sample {
for _, loc := range sample.Location {
for _, line := range loc.Line {
if strings.Contains(line.Function.Name, ".entry") {
// For the default heap profile, sample.Value[1] is the number of objects.
if sample.Value[inuseSpaceIndex] > 1 {
entryCount += sample.Value[1]
}
}
}
}
}
module dwarf_repro
go 1.24.0
require github.com/google/pprof v0.0.0-20250208200701-d0013a598941
github.com/go-delve/delve v1.24.0 h1:M1auuI7kyfXZm5LMDQEqhqr4koKWOzGKhCgwMxsLQfo=
github.com/go-delve/delve v1.24.0/go.mod h1:yNWXOuo4yslMOOj7O8gIRrf/trDBrFy5ZXwJL4ZzOos=
github.com/google/pprof v0.0.0-20250208200701-d0013a598941 h1:43XjGa6toxLpeksjcxs1jIoIyr+vUfOqY2c6HB4bpoc=
github.com/google/pprof v0.0.0-20250208200701-d0013a598941/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
package main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment