github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/convert/pprof/format.go (about)

     1  package pprof
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/pyroscope-io/pyroscope/pkg/storage/tree"
     6  	"reflect"
     7  	"unsafe"
     8  )
     9  
    10  type StackFrameFormatter interface {
    11  	format(x *tree.Profile, fn *tree.Function, line *tree.Line) []byte
    12  }
    13  
    14  func unsafeStrToSlice(s string) []byte {
    15  	return (*[0x7fff0000]byte)(unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data))[:len(s):len(s)]
    16  }
    17  
    18  type UnsafeFunctionNameFormatter struct {
    19  }
    20  
    21  func (UnsafeFunctionNameFormatter) format(x *tree.Profile, fn *tree.Function, _ *tree.Line) []byte {
    22  	return unsafeStrToSlice(x.StringTable[fn.Name])
    23  }
    24  
    25  type RbspyFormatter struct {
    26  }
    27  
    28  func (RbspyFormatter) format(x *tree.Profile, fn *tree.Function, line *tree.Line) []byte {
    29  	return []byte(fmt.Sprintf("%s:%d - %s",
    30  		x.StringTable[fn.Filename],
    31  		line.Line,
    32  		x.StringTable[fn.Name]))
    33  }
    34  
    35  func StackFrameFormatterForSpyName(spyName string) StackFrameFormatter {
    36  	if spyName == "rbspy" || spyName == "pyspy" {
    37  		return RbspyFormatter{}
    38  	}
    39  	return UnsafeFunctionNameFormatter{}
    40  }