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: add cross-platform perf reader implementation and fix type mismatch #1969

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ OpenTelemetry Go Automatic Instrumentation adheres to [Semantic Versioning](http
- Cache offsets for Go `1.24.1`. ([#1940](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1940))
- Cache offsets for `go.opentelemetry.io/[email protected]`. ([#1948](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1948))
- Cache offsets for `golang.org/x/net` `0.37.0`. ([#1948](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1948))
- Added cross-platform build support for perf reader with Linux implementation using `github.com/cilium/ebpf/perf` and stub implementations for non-Linux platforms. ([#1969](https://github.com/open-telemetry/opentelemetry-go-instrumentation/pull/1969))

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"encoding/binary"
"log/slog"

"github.com/cilium/ebpf/perf"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/auto/internal/pkg/instrumentation/perf"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/structfield"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"

"go.opentelemetry.io/auto/internal/pkg/inject"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/perf"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
"go.opentelemetry.io/auto/internal/pkg/process"
"go.opentelemetry.io/auto/internal/pkg/structfield"

"github.com/Masterminds/semver/v3"
"github.com/cilium/ebpf/perf"
"golang.org/x/sys/unix"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/instrumentation/bpffs/bpfsfs_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import "go.opentelemetry.io/auto/internal/pkg/process"

// Stubs for non-linux systems

func PathForTargetApplication(target *process.TargetDetails) string {
func PathForTargetApplication(target *process.Info) string {
return ""
}

func Mount(target *process.TargetDetails) error {
func Mount(target *process.Info) error {
return nil
}

func Cleanup(target *process.TargetDetails) error {
func Cleanup(target *process.Info) error {
return nil
}
33 changes: 33 additions & 0 deletions internal/pkg/instrumentation/perf/perf_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build linux

package perf

import (
"github.com/cilium/ebpf/perf"
)

// Re-export the types from github.com/cilium/ebpf/perf.
type (
Record = perf.Record
Reader = perf.Reader
ReaderOptions = perf.ReaderOptions
)

// Re-export the functions from github.com/cilium/ebpf/perf.
// NewReader is a re-export of perf.NewReader.
var NewReader = perf.NewReader

// NewReaderWithOptions is a re-export of perf.NewReaderWithOptions.
var NewReaderWithOptions = perf.NewReaderWithOptions

// ErrClosed is a re-export of perf.ErrClosed.
var ErrClosed = perf.ErrClosed

// ErrFlushed is a re-export of perf.ErrFlushed.
var ErrFlushed = perf.ErrFlushed

// IsUnknownEvent is a re-export of perf.IsUnknownEvent.
var IsUnknownEvent = perf.IsUnknownEvent
115 changes: 115 additions & 0 deletions internal/pkg/instrumentation/perf/perf_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build !linux

// Package perf provides stub implementation for non-Linux platforms.
// These stubs allow the code to compile but will not work at runtime.
package perf

import (
"errors"
"os"
"time"

"github.com/cilium/ebpf"
)

// ErrClosed is returned when interacting with a closed Reader.
var ErrClosed = os.ErrClosed

// ErrFlushed is returned when the Reader has been flushed.
var ErrFlushed = errors.New("perf reader flushed")

// Record contains data from a perf event.
type Record struct {
// The CPU this record was generated on.
CPU int

// The data submitted via bpf_perf_event_output.
RawSample []byte

// The number of samples which could not be output, since
// the ring buffer was full.
LostSamples uint64

// The minimum number of bytes remaining in the per-CPU buffer after this Record has been read.
Remaining int
}

// Reader allows reading from a perf event array.
type Reader struct{}

// ReaderOptions control the behavior of the Reader.
type ReaderOptions struct {
// The number of events required in any per CPU buffer before
// Read will process data. This is mutually exclusive with Watermark.
WakeupEvents int
// The number of written bytes required in any per CPU buffer before
// Read will process data. Must be smaller than PerCPUBuffer.
Watermark int
// This perf ring buffer is overwritable, once full the oldest event will be
// overwritten by newest.
Overwritable bool
}

// NewReader creates a new reader with default options.
func NewReader(array *ebpf.Map, perCPUBuffer int) (*Reader, error) {
return nil, errors.New("perf.Reader not supported on non-Linux platforms")
}

// NewReaderWithOptions creates a new reader with the given options.
func NewReaderWithOptions(array *ebpf.Map, perCPUBuffer int, opts ReaderOptions) (*Reader, error) {
return nil, errors.New("perf.Reader not supported on non-Linux platforms")
}

// Close frees resources used by the reader.
func (r *Reader) Close() error {
return errors.New("perf reader already closed")
}

// Read the next record from the perf ring buffer.
func (r *Reader) Read() (Record, error) {
return Record{}, errors.New("perf reader already closed")
}

// ReadInto is like Read but allows reusing the Record.
func (r *Reader) ReadInto(rec *Record) error {
return errors.New("perf reader already closed")
}

// SetDeadline controls how long Read and ReadInto will block.
func (r *Reader) SetDeadline(t time.Time) {
// No-op on non-Linux
}

// Pause stops all notifications from this Reader.
func (r *Reader) Pause() error {
return errors.New("perf reader already closed")
}

// Resume allows this perf reader to emit notifications.
func (r *Reader) Resume() error {
return errors.New("perf reader already closed")
}

// FlushAndClose flushes all pending events and closes the reader.
func (r *Reader) FlushAndClose() error {
return errors.New("perf reader already closed")
}

// Flush unblocks Read/ReadInto and returns pending samples.
func (r *Reader) Flush() error {
return errors.New("perf reader already closed")
}

// BufferSize returns the size in bytes of each per-CPU buffer.
func (r *Reader) BufferSize() int {
return 0
}

// IsUnknownEvent returns true if the error occurred because an
// unknown event was submitted to the perf event ring.
func IsUnknownEvent(err error) bool {
return false
}
2 changes: 1 addition & 1 deletion internal/pkg/instrumentation/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"

"go.opentelemetry.io/collector/pdata/ptrace"

"go.opentelemetry.io/auto/internal/pkg/inject"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/bpffs"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/perf"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe/sampling"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
"go.opentelemetry.io/auto/internal/pkg/process"
Expand Down
15 changes: 10 additions & 5 deletions internal/pkg/instrumentation/utils/kernel_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ import (

func GetLinuxKernelVersion() *semver.Version { return nil }

// KernelLockdown represents different Linux Kernel security lockdown modes.
type KernelLockdown uint8

const (
KernelLockdownNone KernelLockdown = iota + 1 // Linux Kernel security lockdown mode [none]
KernelLockdownIntegrity // Linux Kernel security lockdown mode [integrity]
KernelLockdownConfidentiality // Linux Kernel security lockdown mode [confidentiality]
KernelLockdownOther // Linux Kernel security lockdown mode unknown
// KernelLockdownNone represents the 'none' security lockdown mode.
KernelLockdownNone KernelLockdown = iota + 1
// KernelLockdownIntegrity represents the 'integrity' security lockdown mode.
KernelLockdownIntegrity
// KernelLockdownConfidentiality represents the 'confidentiality' security lockdown mode.
KernelLockdownConfidentiality
// KernelLockdownOther represents an unknown security lockdown mode.
KernelLockdownOther
)

func KernelLockdownMode() KernelLockdown {
return 0
}

func GetCPUCount() (int, error) {
func GetCPUCount() (uint64, error) {
return 0, nil
}

Expand Down
Loading