Skip to content

Commit

Permalink
fix: pprof parser formatting for rbspy (#1454)
Browse files Browse the repository at this point in the history
* fix: pprof parser formatting for rbspy

* revert some changes

* linter

* fix build

* pass formatter explicitly to parser constructor
  • Loading branch information
korniltsev authored Sep 6, 2022
1 parent 90a5bb6 commit ca93c31
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
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

0 comments on commit ca93c31

Please sign in to comment.