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

fix(godeltaprof): optionally emmit generics types #70

Merged
merged 5 commits into from
Nov 24, 2023
Merged
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
33 changes: 33 additions & 0 deletions .github/workflows/gotip_cron_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Gotip cron

on:
schedule:
- cron: '37 1 * * *'
workflow_dispatch:

jobs:
go:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Go stable
uses: actions/setup-go@v4
with:
go-version: 1.x
- name: Install Go tip
shell: bash
run: |
go install golang.org/dl/gotip@latest
gotip download
echo "GOROOT=$HOME/sdk/gotip" >> "$GITHUB_ENV"
echo "GOPATH=$HOME/go" >> "$GITHUB_ENV"
echo "$HOME/go/bin" >> "$GITHUB_PATH"
echo "$HOME/sdk/gotip/bin" >> "$GITHUB_PATH"
- name: Build example application
run: go build example/main.go
- name: Run tests
run: |
which go
go version
make test
44 changes: 42 additions & 2 deletions godeltaprof/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,27 @@ type BlockProfiler struct {
// ...
// err := mp.Profile(someWriter)
func NewMutexProfiler() *BlockProfiler {
return &BlockProfiler{runtimeProfile: runtime.MutexProfile, scaleProfile: pprof.ScalerMutexProfile}
return &BlockProfiler{
runtimeProfile: runtime.MutexProfile,
scaleProfile: pprof.ScalerMutexProfile,
impl: pprof.DeltaMutexProfiler{
Options: pprof.ProfileBuilderOptions{
GenericsFrames: true,
},
},
}
}

func NewMutexProfilerWithOptions(options ProfileOptions) *BlockProfiler {
return &BlockProfiler{
runtimeProfile: runtime.MutexProfile,
scaleProfile: pprof.ScalerMutexProfile,
impl: pprof.DeltaMutexProfiler{
Options: pprof.ProfileBuilderOptions{
GenericsFrames: options.GenericsFrames,
},
},
}
}

// NewBlockProfiler creates a new BlockProfiler instance for profiling goroutine blocking events.
Expand All @@ -51,7 +71,27 @@ func NewMutexProfiler() *BlockProfiler {
// ...
// err := bp.Profile(someWriter)
func NewBlockProfiler() *BlockProfiler {
return &BlockProfiler{runtimeProfile: runtime.BlockProfile, scaleProfile: pprof.ScalerBlockProfile}
return &BlockProfiler{
runtimeProfile: runtime.BlockProfile,
scaleProfile: pprof.ScalerBlockProfile,
impl: pprof.DeltaMutexProfiler{
Options: pprof.ProfileBuilderOptions{
GenericsFrames: true,
},
},
}
}

func NewBlockProfilerWithOptions(options ProfileOptions) *BlockProfiler {
return &BlockProfiler{
runtimeProfile: runtime.BlockProfile,
scaleProfile: pprof.ScalerBlockProfile,
impl: pprof.DeltaMutexProfiler{
Options: pprof.ProfileBuilderOptions{
GenericsFrames: options.GenericsFrames,
},
},
}
}

func (d *BlockProfiler) Profile(w io.Writer) error {
Expand Down
188 changes: 188 additions & 0 deletions godeltaprof/compat/generics_go20_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
//go:build go1.18 && !go1.21

package compat

import (
"bytes"
"runtime"
"runtime/pprof"
"sync"
"testing"
"time"

"github.com/grafana/pyroscope-go/godeltaprof"
"github.com/stretchr/testify/require"
)

func genericAllocFunc[T any](n int) []T {
return make([]T, n)
}

func genericBlock[T any](n int) {
for i := 0; i < n; i++ {
m.Lock()
time.Sleep(100 * time.Millisecond)
m.Unlock()
}
}

func triggerGenericBlock() {
const iters = 2
const workers = 10

wg := sync.WaitGroup{}
wg.Add(workers)
for j := 0; j < workers; j++ {
go func() {
genericBlock[int](iters)
wg.Done()
}()
}
wg.Wait()
}

// TestGenerics tests that pre go1.21 we emmit [...] as generics
func TestGenericsShape(t *testing.T) {

prev := runtime.MemProfileRate
runtime.MemProfileRate = 1
runtime.GC()

defer func() {
runtime.MemProfileRate = prev
}()

_ = genericAllocFunc[int](239)

runtime.GC()

const expectedOmmitedShape = "github.com/grafana/pyroscope-go/godeltaprof/compat.TestGenericsShape;github.com/grafana/pyroscope-go/godeltaprof/" +
"compat.genericAllocFunc\\[\\.\\.\\.\\]$"

t.Run("go runtime", func(t *testing.T) {
buffer := bytes.NewBuffer(nil)
err := pprof.WriteHeapProfile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 1, 2048)
})

t.Run("godeltaprof generics enabled by default", func(t *testing.T) {
profiler := godeltaprof.NewHeapProfiler()
buffer := bytes.NewBuffer(nil)
err := profiler.Profile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 1, 2048)
})

t.Run("godeltaprof generics disabled explicitly", func(t *testing.T) {
profiler := godeltaprof.NewHeapProfilerWithOptions(godeltaprof.ProfileOptions{
GenericsFrames: false,
})
buffer := bytes.NewBuffer(nil)
err := profiler.Profile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 1, 2048)
})

t.Run("godeltaprof generics enabled explicitly", func(t *testing.T) {
profiler := godeltaprof.NewHeapProfilerWithOptions(godeltaprof.ProfileOptions{
GenericsFrames: true,
})
buffer := bytes.NewBuffer(nil)
err := profiler.Profile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 1, 2048)
})
}

func TestBlock(t *testing.T) {
defer runtime.SetBlockProfileRate(0)

runtime.SetBlockProfileRate(1) // every block

triggerGenericBlock()

const expectedOmmitedShape = "github.com/grafana/pyroscope-go/godeltaprof/compat.triggerGenericBlock.func1;github.com/grafana/pyroscope-go/godeltaprof/" +
"compat\\.genericBlock\\[\\.\\.\\.\\];sync\\.\\(\\*Mutex\\)\\.Lock"

t.Run("go runtime", func(t *testing.T) {
buffer := bytes.NewBuffer(nil)
err := pprof.Lookup("block").WriteTo(buffer, 0)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 19)
})

t.Run("godeltaprof generics enabled by default", func(t *testing.T) {
profiler := godeltaprof.NewBlockProfiler()
buffer := bytes.NewBuffer(nil)
err := profiler.Profile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 19)
})

t.Run("godeltaprof generics disabled explicitly", func(t *testing.T) {
profiler := godeltaprof.NewBlockProfilerWithOptions(godeltaprof.ProfileOptions{
GenericsFrames: false,
})
buffer := bytes.NewBuffer(nil)
err := profiler.Profile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 19)
})

t.Run("godeltaprof generics enabled explicitly", func(t *testing.T) {
profiler := godeltaprof.NewBlockProfilerWithOptions(godeltaprof.ProfileOptions{
GenericsFrames: true,
})
buffer := bytes.NewBuffer(nil)
err := profiler.Profile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 19)
})
}

func TestMutex(t *testing.T) {
prev := runtime.SetMutexProfileFraction(-1)
defer runtime.SetMutexProfileFraction(prev)
runtime.SetMutexProfileFraction(1)

triggerGenericBlock()

const expectedOmmitedShape = "github.com/grafana/pyroscope-go/godeltaprof/compat.triggerGenericBlock.func1;github.com/grafana/pyroscope-go/godeltaprof/" +
"compat\\.genericBlock\\[\\.\\.\\.\\];sync\\.\\(\\*Mutex\\)\\.Unlock"

t.Run("go runtime", func(t *testing.T) {
buffer := bytes.NewBuffer(nil)
err := pprof.Lookup("mutex").WriteTo(buffer, 0)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 19)
})

t.Run("godeltaprof generics enabled by default", func(t *testing.T) {
profiler := godeltaprof.NewMutexProfiler()
buffer := bytes.NewBuffer(nil)
err := profiler.Profile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 19)
})

t.Run("godeltaprof generics disabled explicitly", func(t *testing.T) {
profiler := godeltaprof.NewMutexProfilerWithOptions(godeltaprof.ProfileOptions{
GenericsFrames: false,
})
buffer := bytes.NewBuffer(nil)
err := profiler.Profile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 19)
})

t.Run("godeltaprof generics enabled explicitly", func(t *testing.T) {
profiler := godeltaprof.NewMutexProfilerWithOptions(godeltaprof.ProfileOptions{
GenericsFrames: true,
})
buffer := bytes.NewBuffer(nil)
err := profiler.Profile(buffer)
require.NoError(t, err)
expectStackFrames(t, buffer, expectedOmmitedShape, 19)
})
}
Loading