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: pprof parser formatting for rbspy #1454

Merged
merged 5 commits into from
Sep 6, 2022
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
40 changes: 40 additions & 0 deletions pkg/convert/pprof/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pprof

import (
"fmt"
"github.com/pyroscope-io/pyroscope/pkg/storage/tree"
"reflect"
"unsafe"
)

type StackFrameFormatter interface {
format(x *tree.Profile, fn *tree.Function, line *tree.Line) []byte
}

func unsafeStrToSlice(s string) []byte {
return (*[0x7fff0000]byte)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data))[:len(s):len(s)]
}

type UnsafeFunctionNameFormatter struct {
}

func (UnsafeFunctionNameFormatter) format(x *tree.Profile, fn *tree.Function, _ *tree.Line) []byte {
return unsafeStrToSlice(x.StringTable[fn.Name])
}

type RbspyFormatter struct {
}

func (RbspyFormatter) format(x *tree.Profile, fn *tree.Function, line *tree.Line) []byte {
return []byte(fmt.Sprintf("%s:%d - %s",
x.StringTable[fn.Filename],
line.Line,
x.StringTable[fn.Name]))
}

func StackFrameFormatterForSpyName(spyName string) StackFrameFormatter {
if spyName == "rbspy" {
return RbspyFormatter{}
}
return UnsafeFunctionNameFormatter{}
}
50 changes: 25 additions & 25 deletions pkg/convert/pprof/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,46 @@ package pprof
import (
"context"
"fmt"
"io"
"reflect"
"time"
"unsafe"

"github.com/pyroscope-io/pyroscope/pkg/storage"
"github.com/pyroscope-io/pyroscope/pkg/storage/metadata"
"github.com/pyroscope-io/pyroscope/pkg/storage/segment"
"github.com/pyroscope-io/pyroscope/pkg/storage/tree"
"io"
"time"
)

type Parser struct {
putter storage.Putter
spyName string
labels map[string]string
skipExemplars bool
sampleTypes map[string]*tree.SampleTypeConfig
putter storage.Putter
spyName string
labels map[string]string
skipExemplars bool
sampleTypes map[string]*tree.SampleTypeConfig
stackFrameFormatter StackFrameFormatter

cache tree.LabelsCache
sampleTypesFilter func(string) bool
}

type ParserConfig struct {
Putter storage.Putter
SpyName string
Labels map[string]string
SkipExemplars bool
SampleTypes map[string]*tree.SampleTypeConfig
Putter storage.Putter
SpyName string
Labels map[string]string
SkipExemplars bool
SampleTypes map[string]*tree.SampleTypeConfig
StackFrameFormatter StackFrameFormatter
}

func NewParser(config ParserConfig) *Parser {
if config.StackFrameFormatter == nil {
config.StackFrameFormatter = &UnsafeFunctionNameFormatter{}
}
return &Parser{
putter: config.Putter,
spyName: config.SpyName,
labels: config.Labels,
sampleTypes: config.SampleTypes,
skipExemplars: config.SkipExemplars,
putter: config.Putter,
spyName: config.SpyName,
labels: config.Labels,
sampleTypes: config.SampleTypes,
skipExemplars: config.SkipExemplars,
stackFrameFormatter: config.StackFrameFormatter,

cache: make(tree.LabelsCache),
sampleTypesFilter: filterKnownSamples(config.SampleTypes),
Expand Down Expand Up @@ -197,7 +200,8 @@ func (p *Parser) readTrees(x *tree.Profile, c tree.LabelsCache, f tree.Finder) {
if !ok || x.StringTable[fn.Name] == "" {
continue
}
stack = append(stack, unsafeStrToSlice(x.StringTable[fn.Name]))
sf := p.stackFrameFormatter.format(x, fn, loc.Line[j])
stack = append(stack, sf)
}
}
// Insert tree nodes.
Expand All @@ -221,10 +225,6 @@ func (p *Parser) readTrees(x *tree.Profile, c tree.LabelsCache, f tree.Finder) {
}
}

func unsafeStrToSlice(s string) []byte {
return (*[0x7fff0000]byte)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data))[:len(s):len(s)]
}

func labelIndex(p *tree.Profile, labels tree.Labels, key string) int {
for i, label := range labels {
if n, ok := p.ResolveLabelName(label); ok && n == key {
Expand Down
11 changes: 6 additions & 5 deletions pkg/convert/pprof/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ func (p *RawProfile) Parse(ctx context.Context, putter storage.Putter, _ storage
sampleTypes = p.SampleTypeConfig
}
p.parser = NewParser(ParserConfig{
SpyName: md.SpyName,
Labels: md.Key.Labels(),
Putter: putter,
SampleTypes: sampleTypes,
SkipExemplars: p.SkipExemplars,
SpyName: md.SpyName,
Labels: md.Key.Labels(),
Putter: putter,
SampleTypes: sampleTypes,
SkipExemplars: p.SkipExemplars,
StackFrameFormatter: StackFrameFormatterForSpyName(md.SpyName),
})

if p.PreviousProfile != nil {
Expand Down