Skip to content

Commit

Permalink
Move type PID to libpf (open-telemetry#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
rockdaboot authored Aug 27, 2024
1 parent f50ab6b commit dd98f70
Show file tree
Hide file tree
Showing 40 changed files with 152 additions and 161 deletions.
5 changes: 2 additions & 3 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/open-telemetry/opentelemetry-ebpf-profiler/libpf"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/times"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/util"
)

// TraceHash is used for unique identifiers for traces, and is required to be 64-bits
Expand Down Expand Up @@ -55,8 +54,8 @@ type Trace struct {
Frames []Frame
Hash TraceHash
KTime times.KTime
PID util.PID
TID util.PID
PID libpf.PID
TID libpf.PID
APMTraceID libpf.APMTraceID
APMTransactionID libpf.APMTransactionID
}
11 changes: 5 additions & 6 deletions interpreter/apmint/apmint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ import (
"regexp"
"unsafe"

log "github.com/sirupsen/logrus"

"github.com/open-telemetry/opentelemetry-ebpf-profiler/host"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/interpreter"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/libpf"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/libpf/pfelf"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/remotememory"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/util"

log "github.com/sirupsen/logrus"
)

// #include <stdlib.h>
Expand Down Expand Up @@ -108,7 +107,7 @@ type data struct {

var _ interpreter.Data = &data{}

func (d data) Attach(ebpf interpreter.EbpfHandler, pid util.PID,
func (d data) Attach(ebpf interpreter.EbpfHandler, pid libpf.PID,
bias libpf.Address, rm remotememory.RemoteMemory) (interpreter.Instance, error) {
procStorage, err := readProcStorage(rm, bias+d.procStorageElfVA)
if err != nil {
Expand Down Expand Up @@ -146,13 +145,13 @@ type Instance struct {
var _ interpreter.Instance = &Instance{}

// Detach implements the interpreter.Instance interface.
func (i *Instance) Detach(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (i *Instance) Detach(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
return ebpf.DeleteProcData(libpf.APMInt, pid)
}

// NotifyAPMAgent sends out collected traces to the connected APM agent.
func (i *Instance) NotifyAPMAgent(
pid util.PID, rawTrace *host.Trace, umTraceHash libpf.TraceHash, count uint16) {
pid libpf.PID, rawTrace *host.Trace, umTraceHash libpf.TraceHash, count uint16) {
if rawTrace.APMTransactionID == libpf.InvalidAPMSpanID || i.socket == nil {
return
}
Expand Down
5 changes: 2 additions & 3 deletions interpreter/apmint/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/open-telemetry/opentelemetry-ebpf-profiler/libpf"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/libpf/xsync"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/stringutil"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/util"
)

// sendSocket is the shared, unbound socket that we use for communication with
Expand All @@ -40,7 +39,7 @@ type apmAgentSocket struct {
// openAPMAgentSocket opens the APM unix socket in the given PID's root filesystem.
//
// This method never blocks.
func openAPMAgentSocket(pid util.PID, socketPath string) (*apmAgentSocket, error) {
func openAPMAgentSocket(pid libpf.PID, socketPath string) (*apmAgentSocket, error) {
// Ensure that the socket path can't escape our root.
socketPath = filepath.Clean(socketPath)
for _, segment := range strings.Split(socketPath, "/") {
Expand Down Expand Up @@ -123,7 +122,7 @@ func (m *traceCorrMsg) Serialize() []byte {
}

// readProcessOwner reads the effective UID and GID of the target process.
func readProcessOwner(pid util.PID) (euid, egid uint32, err error) {
func readProcessOwner(pid libpf.PID) (euid, egid uint32, err error) {
statusFd, err := os.Open(fmt.Sprintf("/proc/%d/status", pid))
if err != nil {
return 0, 0, fmt.Errorf("failed to open process status: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions interpreter/dotnet/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
log "github.com/sirupsen/logrus"

"github.com/elastic/go-freelru"

"github.com/open-telemetry/opentelemetry-ebpf-profiler/interpreter"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/libpf"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/remotememory"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/util"
)

// #include "../../support/ebpf/types.h"
Expand All @@ -32,7 +32,7 @@ type dotnetData struct {

// method to walk range sections
walkRangeSectionsMethod func(i *dotnetInstance, ebpf interpreter.EbpfHandler,
pid util.PID) error
pid libpf.PID) error

vmStructs struct {
// https://github.com/dotnet/runtime/blob/v7.0.15/src/coreclr/debug/ee/dactable.cpp#L81
Expand Down Expand Up @@ -137,7 +137,7 @@ func (d *dotnetData) String() string {
return fmt.Sprintf("dotnet %d.%d.%d", (ver>>24)&0xff, (ver>>16)&0xff, ver&0xffff)
}

func (d *dotnetData) Attach(ebpf interpreter.EbpfHandler, pid util.PID, bias libpf.Address,
func (d *dotnetData) Attach(ebpf interpreter.EbpfHandler, pid libpf.PID, bias libpf.Address,
rm remotememory.RemoteMemory) (interpreter.Instance, error) {
log.Debugf("Attach PID %d, bias %x", pid, bias)

Expand Down
16 changes: 8 additions & 8 deletions interpreter/dotnet/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (i *dotnetInstance) calculateAndSymbolizeStubID(symbolReporter reporter.Sym
}

// addRange inserts a known memory mapping along with the needed data of it to ebpf maps
func (i *dotnetInstance) addRange(ebpf interpreter.EbpfHandler, pid util.PID,
func (i *dotnetInstance) addRange(ebpf interpreter.EbpfHandler, pid libpf.PID,
lowAddress, highAddress, mapBase libpf.Address, stubTypeOrHdrMap uint64) {
// Inform the unwinder about this range
prefixes, err := lpm.CalculatePrefixList(uint64(lowAddress), uint64(highAddress))
Expand Down Expand Up @@ -221,7 +221,7 @@ func (i *dotnetInstance) addRange(ebpf interpreter.EbpfHandler, pid util.PID,
}

// walkRangeList processes stub ranges from a RangeList
func (i *dotnetInstance) walkRangeList(ebpf interpreter.EbpfHandler, pid util.PID,
func (i *dotnetInstance) walkRangeList(ebpf interpreter.EbpfHandler, pid libpf.PID,
headPtr libpf.Address, codeType uint) {
// This hardcodes the layout of RangeList, Range and RangeListBlock from
// https://github.com/dotnet/runtime/blob/v7.0.15/src/coreclr/inc/utilcode.h#L3556-L3579
Expand Down Expand Up @@ -259,7 +259,7 @@ func (i *dotnetInstance) walkRangeList(ebpf interpreter.EbpfHandler, pid util.PI
}

// addRangeSection processes a RangeSection structure and calls addRange as needed
func (i *dotnetInstance) addRangeSection(ebpf interpreter.EbpfHandler, pid util.PID,
func (i *dotnetInstance) addRangeSection(ebpf interpreter.EbpfHandler, pid libpf.PID,
rangeSection []byte) error {
// Extract interesting fields
vms := &i.d.vmStructs
Expand Down Expand Up @@ -324,7 +324,7 @@ func (i *dotnetInstance) addRangeSection(ebpf interpreter.EbpfHandler, pid util.
}

// walkRangeSectionList adds all RangeSections in a list (dotnet6 and dotnet7)
func (i *dotnetInstance) walkRangeSectionList(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (i *dotnetInstance) walkRangeSectionList(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
vms := &i.d.vmStructs
rangeSection := make([]byte, vms.RangeSection.SizeOf)
// walk the RangeSection list
Expand All @@ -343,7 +343,7 @@ func (i *dotnetInstance) walkRangeSectionList(ebpf interpreter.EbpfHandler, pid

// walkRangeSectionMapFragments walks a RangeSectionMap::RangeSectionFragment list and processes
// the RangeSections from it.
func (i *dotnetInstance) walkRangeSectionMapFragments(ebpf interpreter.EbpfHandler, pid util.PID,
func (i *dotnetInstance) walkRangeSectionMapFragments(ebpf interpreter.EbpfHandler, pid libpf.PID,
fragmentPtr libpf.Address) error {
// https://github.com/dotnet/runtime/blob/v8.0.4/src/coreclr/vm/codeman.h#L974
vms := &i.d.vmStructs
Expand Down Expand Up @@ -372,7 +372,7 @@ func (i *dotnetInstance) walkRangeSectionMapFragments(ebpf interpreter.EbpfHandl
}

// walkRangeSectionMapLevel walks recursively a level index of a RangeSectionMap.
func (i *dotnetInstance) walkRangeSectionMapLevel(ebpf interpreter.EbpfHandler, pid util.PID,
func (i *dotnetInstance) walkRangeSectionMapLevel(ebpf interpreter.EbpfHandler, pid libpf.PID,
levelMapPtr libpf.Address, level uint) error {
// https://github.com/dotnet/runtime/blob/v8.0.4/src/coreclr/vm/codeman.h#L999-L1002
const maxLevel = 5
Expand Down Expand Up @@ -402,7 +402,7 @@ func (i *dotnetInstance) walkRangeSectionMapLevel(ebpf interpreter.EbpfHandler,
}

// walkRangeSectionMap processes a dotnet8 RangeSectionMap to enumerate all RangeSections
func (i *dotnetInstance) walkRangeSectionMap(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (i *dotnetInstance) walkRangeSectionMap(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
i.rangeSectionSeen = make(map[libpf.Address]libpf.Void)
err := i.walkRangeSectionMapLevel(ebpf, pid, i.codeRangeListPtr, 1)
i.rangeSectionSeen = nil
Expand Down Expand Up @@ -537,7 +537,7 @@ func (i *dotnetInstance) getMethod(codeHeaderPtr libpf.Address) (*dotnetMethod,
return method, nil
}

func (i *dotnetInstance) Detach(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (i *dotnetInstance) Detach(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
return ebpf.DeleteProcData(libpf.Dotnet, pid)
}

Expand Down
3 changes: 1 addition & 2 deletions interpreter/hotspot/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/open-telemetry/opentelemetry-ebpf-profiler/lpm"
npsr "github.com/open-telemetry/opentelemetry-ebpf-profiler/nopanicslicereader"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/remotememory"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/util"
)

// hotspotIntrospectionTable contains the resolved ELF symbols for an introspection table
Expand Down Expand Up @@ -338,7 +337,7 @@ func (d *hotspotData) String() string {
// Attach loads to the ebpf program the needed pointers and sizes to unwind given hotspot process.
// As the hotspot unwinder depends on the native unwinder, a part of the cleanup is done by the
// process manager and not the corresponding Detach() function of hotspot objects.
func (d *hotspotData) Attach(_ interpreter.EbpfHandler, _ util.PID, bias libpf.Address,
func (d *hotspotData) Attach(_ interpreter.EbpfHandler, _ libpf.PID, bias libpf.Address,
rm remotememory.RemoteMemory) (ii interpreter.Instance, err error) {
// Each function has four symbols: source filename, class name,
// method name and signature. However, most of them are shared across
Expand Down
8 changes: 4 additions & 4 deletions interpreter/hotspot/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (d *hotspotInstance) getJITInfo(addr libpf.Address,
}

// Detach removes all information regarding a given process from the eBPF maps.
func (d *hotspotInstance) Detach(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (d *hotspotInstance) Detach(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
var err error
if d.mainMappingsInserted {
err = ebpf.DeleteProcData(libpf.HotSpot, pid)
Expand Down Expand Up @@ -599,7 +599,7 @@ func (d *hotspotInstance) gatherHeapInfo(vmd *hotspotVMData) (*heapInfo, error)

// addJitArea inserts an entry into the PID<->interpreter BPF map.
func (d *hotspotInstance) addJitArea(ebpf interpreter.EbpfHandler,
pid util.PID, area jitArea) error {
pid libpf.PID, area jitArea) error {
prefixes, err := lpm.CalculatePrefixList(uint64(area.start), uint64(area.end))
if err != nil {
return fmt.Errorf("LPM prefix calculation error for %x-%x", area.start, area.end)
Expand Down Expand Up @@ -632,7 +632,7 @@ func (d *hotspotInstance) addJitArea(ebpf interpreter.EbpfHandler,
// allows the BPF code to start unwinding even if some more detailed information
// about e.g. stub routines is not yet available.
func (d *hotspotInstance) populateMainMappings(vmd *hotspotVMData,
ebpf interpreter.EbpfHandler, pid util.PID) error {
ebpf interpreter.EbpfHandler, pid libpf.PID) error {
if d.mainMappingsInserted {
// Already populated: nothing to do here.
return nil
Expand Down Expand Up @@ -704,7 +704,7 @@ func (d *hotspotInstance) populateMainMappings(vmd *hotspotVMData,
// stubs map and, if necessary on the architecture, inserts unwinding instructions
// for them in the PID mappings BPF map.
func (d *hotspotInstance) updateStubMappings(vmd *hotspotVMData,
ebpf interpreter.EbpfHandler, pid util.PID) {
ebpf interpreter.EbpfHandler, pid libpf.PID) {
for _, stub := range findStubBounds(vmd, d.bias, d.rm) {
if _, exists := d.stubs[stub.start]; exists {
continue
Expand Down
3 changes: 1 addition & 2 deletions interpreter/instancestubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/open-telemetry/opentelemetry-ebpf-profiler/process"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/reporter"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/tpbase"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/util"
)

// InstanceStubs provides empty implementations of Instance hooks that are
Expand All @@ -26,7 +25,7 @@ func (is *InstanceStubs) SynchronizeMappings(EbpfHandler, reporter.SymbolReporte
return nil
}

func (is *InstanceStubs) UpdateTSDInfo(EbpfHandler, util.PID, tpbase.TSDInfo) error {
func (is *InstanceStubs) UpdateTSDInfo(EbpfHandler, libpf.PID, tpbase.TSDInfo) error {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions interpreter/nodev8/v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ type v8SFI struct {
bytecodeLength uint32
}

func (i *v8Instance) Detach(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (i *v8Instance) Detach(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
err := ebpf.DeleteProcData(libpf.V8, pid)
for prefix := range i.prefixes {
if err2 := ebpf.DeletePidInterpreterMapping(pid, prefix); err2 != nil {
Expand Down Expand Up @@ -1764,7 +1764,7 @@ func mapFramePointerOffset(relBytes uint8) C.u8 {
return C.u8(slotOffset)
}

func (d *v8Data) Attach(ebpf interpreter.EbpfHandler, pid util.PID, _ libpf.Address,
func (d *v8Data) Attach(ebpf interpreter.EbpfHandler, pid libpf.PID, _ libpf.Address,
rm remotememory.RemoteMemory) (interpreter.Instance, error) {
vms := &d.vmStructs
data := C.V8ProcInfo{
Expand Down
3 changes: 1 addition & 2 deletions interpreter/perl/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/open-telemetry/opentelemetry-ebpf-profiler/libpf/pfelf"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/remotememory"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/support"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/util"
)

// #include "../../support/ebpf/types.h"
Expand Down Expand Up @@ -128,7 +127,7 @@ func (d *perlData) String() string {
return fmt.Sprintf("Perl %d.%d.%d", (ver>>16)&0xff, (ver>>8)&0xff, ver&0xff)
}

func (d *perlData) Attach(_ interpreter.EbpfHandler, _ util.PID, bias libpf.Address,
func (d *perlData) Attach(_ interpreter.EbpfHandler, _ libpf.PID, bias libpf.Address,
rm remotememory.RemoteMemory) (interpreter.Instance, error) {
addrToHEK, err := freelru.New[libpf.Address, string](interpreter.LruFunctionCacheSize,
libpf.Address.Hash32)
Expand Down
4 changes: 2 additions & 2 deletions interpreter/perl/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func hashCOPKey(k copKey) uint32 {
return uint32(h ^ xxhash.Sum64String(k.funcName))
}

func (i *perlInstance) UpdateTSDInfo(ebpf interpreter.EbpfHandler, pid util.PID,
func (i *perlInstance) UpdateTSDInfo(ebpf interpreter.EbpfHandler, pid libpf.PID,
tsdInfo tpbase.TSDInfo) error {
d := i.d
stateInTSD := C.u8(0)
Expand Down Expand Up @@ -134,7 +134,7 @@ func (i *perlInstance) UpdateTSDInfo(ebpf interpreter.EbpfHandler, pid util.PID,
return nil
}

func (i *perlInstance) Detach(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (i *perlInstance) Detach(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
if !i.procInfoInserted {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion interpreter/php/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type phpInstance struct {
addrToFunction *freelru.LRU[libpf.Address, *phpFunction]
}

func (i *phpInstance) Detach(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (i *phpInstance) Detach(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
return ebpf.DeleteProcData(libpf.PHP, pid)
}

Expand Down
4 changes: 2 additions & 2 deletions interpreter/php/opcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ type opcacheInstance struct {
prefixes []lpm.Prefix
}

func (i *opcacheInstance) Detach(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (i *opcacheInstance) Detach(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
// Here we just remove the entries relating to the mappings for the
// JIT's memory
var err error
Expand Down Expand Up @@ -240,7 +240,7 @@ func (d *opcacheData) String() string {
return fmt.Sprintf("Opcache %d.%d.%d", (ver>>16)&0xff, (ver>>8)&0xff, ver&0xff)
}

func (d *opcacheData) Attach(_ interpreter.EbpfHandler, _ util.PID, bias libpf.Address,
func (d *opcacheData) Attach(_ interpreter.EbpfHandler, _ libpf.PID, bias libpf.Address,
rm remotememory.RemoteMemory) (interpreter.Instance, error) {
return &opcacheInstance{
d: d,
Expand Down
3 changes: 1 addition & 2 deletions interpreter/php/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/open-telemetry/opentelemetry-ebpf-profiler/libpf/pfelf"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/remotememory"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/support"
"github.com/open-telemetry/opentelemetry-ebpf-profiler/util"
)

// #include "../../support/ebpf/types.h"
Expand Down Expand Up @@ -112,7 +111,7 @@ func (d *phpData) String() string {
return fmt.Sprintf("PHP %d.%d.%d", (ver>>16)&0xff, (ver>>8)&0xff, ver&0xff)
}

func (d *phpData) Attach(ebpf interpreter.EbpfHandler, pid util.PID, bias libpf.Address,
func (d *phpData) Attach(ebpf interpreter.EbpfHandler, pid libpf.PID, bias libpf.Address,
rm remotememory.RemoteMemory) (interpreter.Instance, error) {
addrToFunction, err :=
freelru.New[libpf.Address, *phpFunction](interpreter.LruFunctionCacheSize,
Expand Down
6 changes: 3 additions & 3 deletions interpreter/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (d *pythonData) String() string {
return fmt.Sprintf("Python %d.%d", d.version>>8, d.version&0xff)
}

func (d *pythonData) Attach(_ interpreter.EbpfHandler, _ util.PID, bias libpf.Address,
func (d *pythonData) Attach(_ interpreter.EbpfHandler, _ libpf.PID, bias libpf.Address,
rm remotememory.RemoteMemory) (interpreter.Instance, error) {
addrToCodeObject, err :=
freelru.New[libpf.Address, *pythonCodeObject](interpreter.LruFunctionCacheSize,
Expand Down Expand Up @@ -410,7 +410,7 @@ func (p *pythonInstance) GetAndResetMetrics() ([]metrics.Metric, error) {
}, nil
}

func (p *pythonInstance) UpdateTSDInfo(ebpf interpreter.EbpfHandler, pid util.PID,
func (p *pythonInstance) UpdateTSDInfo(ebpf interpreter.EbpfHandler, pid libpf.PID,
tsdInfo tpbase.TSDInfo) error {
d := p.d
vm := &d.vmStructs
Expand Down Expand Up @@ -447,7 +447,7 @@ func (p *pythonInstance) UpdateTSDInfo(ebpf interpreter.EbpfHandler, pid util.PI
return err
}

func (p *pythonInstance) Detach(ebpf interpreter.EbpfHandler, pid util.PID) error {
func (p *pythonInstance) Detach(ebpf interpreter.EbpfHandler, pid libpf.PID) error {
if !p.procInfoInserted {
return nil
}
Expand Down
Loading

0 comments on commit dd98f70

Please sign in to comment.