Skip to content
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

runtime: add GoID to StackRecord produced by GoroutineProfile() #67369

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/internal/profilerecord/profilerecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package profilerecord

type StackRecord struct {
Stack []uintptr
GoID uint64
}

type MemProfileRecord struct {
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/mprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,7 @@ func mutexevent(cycles int64, skip int) {
// A StackRecord describes a single execution stack.
type StackRecord struct {
Stack0 [32]uintptr // stack trace for this record; ends at first 0 entry
goid uint64 // Goroutine ID for the first record (or 0 if undefined by the struct creator).
}

// Stack returns the stack trace associated with the record,
Expand All @@ -1022,6 +1023,11 @@ func (r *StackRecord) Stack() []uintptr {
return r.Stack0[0:]
}

// GoID returns an identifier for the Goroutine this stack was captured for, or zero if undefined.
func (r *StackRecord) GoID() uint64 {
return r.goid
}

// MemProfileRate controls the fraction of memory allocations
// that are recorded and reported in the memory profile.
// The profiler aims to sample an average of
Expand Down Expand Up @@ -1707,6 +1713,7 @@ func GoroutineProfile(p []StackRecord) (n int, ok bool) {
}
for i, mr := range records[0:n] {
copy(p[i].Stack0[:], mr.Stack)
p[i].goid = mr.GoID
}
return
}
Expand Down Expand Up @@ -1735,6 +1742,7 @@ func saveg(pc, sp uintptr, gp *g, r *profilerecord.StackRecord, pcbuf []uintptr)
n := tracebackPCs(&u, 0, pcbuf)
r.Stack = make([]uintptr, n)
copy(r.Stack, pcbuf)
r.GoID = gp.goid
}

// Stack formats a stack trace of the calling goroutine into buf
Expand Down
17 changes: 15 additions & 2 deletions src/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,19 @@ func TestAppendSliceGrowth(t *testing.T) {
}
}

func TestGoroutineProfileTrivial(t *testing.T) {
func TestGoroutineProfile(t *testing.T) {
// Calling GoroutineProfile twice in a row should find the same number of goroutines,
// but it's possible there are goroutines just about to exit, so we might end up
// with fewer in the second call. Try a few times; it should converge once those
// zombies are gone.
var records []StackRecord
for i := 0; ; i++ {
n1, ok := GoroutineProfile(nil) // should fail, there's at least 1 goroutine
if n1 < 1 || ok {
t.Fatalf("GoroutineProfile(nil) = %d, %v, want >0, false", n1, ok)
}
n2, ok := GoroutineProfile(make([]StackRecord, n1))
records = make([]StackRecord, n1)
n2, ok := GoroutineProfile(records)
if n2 == n1 && ok {
break
}
Expand All @@ -363,6 +365,17 @@ func TestGoroutineProfileTrivial(t *testing.T) {
t.Fatalf("GoroutineProfile not converging")
}
}
if len(records) < 1 {
t.Fatalf("GoroutineProfile hasn't collected any records")
}
for _, record := range records {
if len(record.Stack()) < 1 {
t.Fatalf("GoroutineProfile record is missing a stack trace")
}
if record.GoID() < 1 {
t.Fatalf("GoroutineProfile record is missing a GoID")
}
}
}

func BenchmarkGoroutineProfile(b *testing.B) {
Expand Down