Skip to content

Commit

Permalink
link: Add PerfFd interface function
Browse files Browse the repository at this point in the history
Adding PerfFd interface function that returns perf event
file descriptor for perf event based links.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
  • Loading branch information
olsajiri committed Feb 10, 2024
1 parent 0aee7dd commit 6c7b356
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions link/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ func (cg *progAttachCgroup) Info() (*Info, error) {
return nil, fmt.Errorf("can't get cgroup info: %w", ErrNotSupported)
}

func (cg *progAttachCgroup) PerfFd() (int, error) {
return -1, fmt.Errorf("perf event fd: %w", ErrNotSupported)
}

type linkCgroup struct {
RawLink
}
Expand Down
9 changes: 9 additions & 0 deletions link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type Link interface {

// Prevent external users from implementing this interface.
isLink()

// PerfFd returns perf event file descriptor for perf event based links.
//
// May return an error wrapping ErrNotSupported
PerfFd() (int, error)
}

// NewLinkFromFD creates a link from a raw fd.
Expand Down Expand Up @@ -362,3 +367,7 @@ func (l *RawLink) Info() (*Info, error) {
extra,
}, nil
}

func (l *RawLink) PerfFd() (int, error) {
return -1, fmt.Errorf("perf event fd: %w", ErrNotSupported)
}
16 changes: 16 additions & 0 deletions link/perf_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ func (pi *perfEventIoctl) Info() (*Info, error) {
return nil, fmt.Errorf("perf event ioctl info: %w", ErrNotSupported)
}

func (pi *perfEventIoctl) PerfFd() (int, error) {
dup, err := pi.fd.Dup()
if err != nil {
return -1, fmt.Errorf("can't clone fd: %w", err)
}
return dup.Int(), nil
}

type PerfEventInfo struct {
Type PerfEventInfoType
extra interface{}
Expand Down Expand Up @@ -244,6 +252,14 @@ func (pl *perfEventLink) Info() (*Info, error) {
}, nil
}

func (pl *perfEventLink) PerfFd() (int, error) {
dup, err := pl.fd.Dup()
if err != nil {
return -1, fmt.Errorf("can't clone fd: %w", err)
}
return dup.Int(), nil
}

// attach the given eBPF prog to the perf event stored in pe.
// pe must contain a valid perf event fd.
// prog's type must match the program type stored in pe.
Expand Down
4 changes: 4 additions & 0 deletions link/raw_tracepoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (frt *simpleRawTracepoint) Info() (*Info, error) {
return nil, fmt.Errorf("can't get raw_tracepoint info: %w", ErrNotSupported)
}

func (frt *simpleRawTracepoint) PerfFd() (int, error) {
return -1, fmt.Errorf("perf event fd: %w", ErrNotSupported)
}

type rawTracepoint struct {
RawLink
}
Expand Down

0 comments on commit 6c7b356

Please sign in to comment.