Skip to content
Closed
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
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module go.opentelemetry.io/ebpf-profiler
go 1.22.2

require (
github.com/aws/aws-sdk-go-v2 v1.30.5
github.com/aws/aws-sdk-go-v2/config v1.27.35
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.21
github.com/aws/aws-sdk-go-v2/service/s3 v1.62.0
github.com/cespare/xxhash/v2 v2.3.0
github.com/cilium/ebpf v0.16.0
github.com/elastic/go-freelru v0.15.0
github.com/elastic/go-freelru v0.16.0
github.com/elastic/go-perf v0.0.0-20241016160959-1342461adb4a
github.com/google/uuid v1.6.0
github.com/jsimonetti/rtnetlink v1.4.2
Expand All @@ -29,7 +29,6 @@ require (
)

require (
github.com/aws/aws-sdk-go-v2 v1.30.5 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.4 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.33 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.13 // indirect
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ github.com/aws/aws-sdk-go-v2/credentials v1.17.33 h1:lBHAQQznENv0gLHAZ73ONiTSkCt
github.com/aws/aws-sdk-go-v2/credentials v1.17.33/go.mod h1:MBuqCUOT3ChfLuxNDGyra67eskx7ge9e3YKYBce7wpI=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.13 h1:pfQ2sqNpMVK6xz2RbqLEL0GH87JOwSxPV2rzm8Zsb74=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.13/go.mod h1:NG7RXPUlqfsCLLFfi0+IpKN4sCB9D9fw/qTaSB+xRoU=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.21 h1:sV0doPPsRT7gMP0BnDPwSsysVTV/nKpB/nFmMnz8goE=
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.21/go.mod h1:ictvfJWqE2gkUFDRJVp5VU/TrytuzK88DYcpan7UYuA=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17 h1:pI7Bzt0BJtYA0N/JEC6B8fJ4RBrEMi1LBrkMdFYNSnQ=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17/go.mod h1:Dh5zzJYMtxfIjYW+/evjQ8uj2OyR/ve2KROHGHlSFqE=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.17 h1:Mqr/V5gvrhA2gvgnF42Zh5iMiQNcOYthFYwCyrnuWlc=
Expand Down Expand Up @@ -43,10 +41,8 @@ github.com/cilium/ebpf v0.16.0/go.mod h1:L7u2Blt2jMM/vLAVgjxluxtBKlz3/GWjB0dMOEn
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elastic/go-freelru v0.13.0 h1:TKKY6yCfNNNky7Pj9xZAOEpBcdNgZJfihEftOb55omg=
github.com/elastic/go-freelru v0.13.0/go.mod h1:bSdWT4M0lW79K8QbX6XY2heQYSCqD7THoYf82pT/H3I=
github.com/elastic/go-freelru v0.15.0 h1:Jo1aY8JAvpyxbTDJEudrsBfjFDaALpfVv8mxuh9sfvI=
github.com/elastic/go-freelru v0.15.0/go.mod h1:bSdWT4M0lW79K8QbX6XY2heQYSCqD7THoYf82pT/H3I=
github.com/elastic/go-freelru v0.16.0 h1:gG2HJ1WXN2tNl5/p40JS/l59HjvjRhjyAa+oFTRArYs=
github.com/elastic/go-freelru v0.16.0/go.mod h1:bSdWT4M0lW79K8QbX6XY2heQYSCqD7THoYf82pT/H3I=
github.com/elastic/go-perf v0.0.0-20241016160959-1342461adb4a h1:ymmtaN4bVCmKKeu4XEf6JEWNZKRXPMng1zjpKd+8rCU=
github.com/elastic/go-perf v0.0.0-20241016160959-1342461adb4a/go.mod h1:Nt+pnRYvf0POC+7pXsrv8ubsEOSsaipJP0zlz1Ms1RM=
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
Expand Down
1 change: 1 addition & 0 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ type Trace struct {
TID libpf.PID
APMTraceID libpf.APMTraceID
APMTransactionID libpf.APMTransactionID
CPU int
}
59 changes: 59 additions & 0 deletions libpf/cgroupv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package libpf // import "go.opentelemetry.io/ebpf-profiler/libpf"

import (
"bufio"
"fmt"
"os"
"regexp"

lru "github.com/elastic/go-freelru"
log "github.com/sirupsen/logrus"
)

var (
cgroupv2PathPattern = regexp.MustCompile(`0:.*?:(.*)`)
)

// LookupCgroupv2 returns the cgroupv2 ID for pid.
func LookupCgroupv2(cgrouplru *lru.SyncedLRU[PID, string], pid PID) (string, error) {
id, ok := cgrouplru.Get(pid)
if ok {
return id, nil
}

// Slow path
f, err := os.Open(fmt.Sprintf("/proc/%d/cgroup", pid))
if err != nil {
return "", err
}
defer f.Close()

var genericCgroupv2 string
scanner := bufio.NewScanner(f)
buf := make([]byte, 512)
// Providing a predefined buffer overrides the internal buffer that Scanner uses (4096 bytes).
// We can do that and also set a maximum allocation size on the following call.
// With a maximum of 4096 characters path in the kernel, 8192 should be fine here. We don't
// expect lines in /proc/<PID>/cgroup to be longer than that.
scanner.Buffer(buf, 8192)
var pathParts []string
for scanner.Scan() {
line := scanner.Text()
pathParts = cgroupv2PathPattern.FindStringSubmatch(line)
if pathParts == nil {
log.Debugf("Could not extract cgroupv2 path from line: %s", line)
continue
}
genericCgroupv2 = pathParts[1]
break
}

// Cache the cgroupv2 information.
// To avoid busy lookups, also empty cgroupv2 information is cached.
cgrouplru.Add(pid, genericCgroupv2)

return genericCgroupv2, nil
}
31 changes: 1 addition & 30 deletions libpf/libpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package libpf // import "go.opentelemetry.io/ebpf-profiler/libpf"

import (
"encoding/json"
"fmt"
"math"
"time"
)

Expand All @@ -32,36 +30,9 @@ func NowAsUInt32() uint32 {
return uint32(time.Now().Unix())
}

// UnixTime64 represents nanoseconds or (reduced precision) seconds since epoch.
// UnixTime64 represents nanoseconds since epoch.
type UnixTime64 uint64

func (t UnixTime64) MarshalJSON() ([]byte, error) {
if t > math.MaxUint32 {
// Nanoseconds, ES does not support 'epoch_nanoseconds' so
// we have to pass it a value formatted as 'strict_date_optional_time_nanos'.
out := []byte(fmt.Sprintf("%q",
time.Unix(0, int64(t)).UTC().Format(time.RFC3339Nano)))
return out, nil
}

// Reduced precision seconds-since-the-epoch, ES 'epoch_second' formatter will match these.
out := []byte(fmt.Sprintf("%d", t))
return out, nil
}

// Unix returns the value as seconds since epoch.
func (t UnixTime64) Unix() int64 {
if t > math.MaxUint32 {
// Nanoseconds, convert to seconds-since-the-epoch
return time.Unix(0, int64(t)).Unix()
}

return int64(t)
}

// Compile-time interface checks
var _ json.Marshaler = (*UnixTime64)(nil)

// AddressOrLineno represents a line number in an interpreted file or an offset into
// a native file.
type AddressOrLineno uint64
Expand Down
35 changes: 0 additions & 35 deletions libpf/libpf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
package libpf

import (
"fmt"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestTraceType(t *testing.T) {
Expand Down Expand Up @@ -45,35 +42,3 @@ func TestTraceType(t *testing.T) {
assert.Equal(t, test.str, test.ty.String())
}
}

func TestUnixTime64_MarshalJSON(t *testing.T) {
tests := []struct {
name string
time UnixTime64
want []byte
}{
{
name: "zero",
time: UnixTime64(0),
want: []byte(strconv.Itoa(0)),
},
{
name: "non-zero, seconds since the epoch",
time: UnixTime64(1710349106),
want: []byte(strconv.Itoa(1710349106)),
},
{
name: "non-zero, nanoseconds since the epoch",
time: UnixTime64(1710349106864964685),
want: []byte(fmt.Sprintf("%q", "2024-03-13T16:58:26.864964685Z")),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
b, err := test.time.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, test.want, b)
})
}
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func mainWithExitCode() exitCode {
GRPCStartupBackoffTime: intervals.GRPCStartupBackoffTime(),
GRPCConnectionTimeout: intervals.GRPCConnectionTimeout(),
ReportInterval: intervals.ReportInterval(),
ExecutablesCacheElements: 4096,
ExecutablesCacheElements: 16384,
// Next step: Calculate FramesCacheElements from numCores and samplingRate.
FramesCacheElements: 65536,
CGroupCacheElements: 1024,
Expand Down
9 changes: 7 additions & 2 deletions proc/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,15 @@ func GetKernelModules(modulesPath string,

count++

nFields, _ := fmt.Sscanf(line, "%s %d %d %s %s 0x%x",
nFields, err := fmt.Sscanf(line, "%s %d %d %s %s 0x%x",
&name, &size, &refcount, &dependencies, &state, &address)
if err != nil {
log.Warnf("err parsing line in modules: '%s'", err)
continue
}
if nFields < 6 {
return nil, fmt.Errorf("unexpected line in modules: '%s'", line)
log.Warnf("unexpected line in modules: '%s'", line)
continue
}
if address == 0 {
continue
Expand Down
1 change: 1 addition & 0 deletions reporter/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type TraceEventMeta struct {
Comm string
APMServiceName string
PID, TID libpf.PID
CPU int
}

type TraceReporter interface {
Expand Down
Loading