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

[RFC] introduce periodic BPF programs #924

Closed
wants to merge 1 commit into from
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: 5 additions & 0 deletions internal/unix/types_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const (
EOPNOTSUPP = linux.EOPNOTSUPP
)

const (
PerfBitFreq = linux.PerfBitFreq
)

const (
BPF_F_NO_PREALLOC = linux.BPF_F_NO_PREALLOC
BPF_F_NUMA_NODE = linux.BPF_F_NUMA_NODE
Expand Down Expand Up @@ -58,6 +62,7 @@ const (
PERF_ATTR_SIZE_VER1 = linux.PERF_ATTR_SIZE_VER1
PERF_TYPE_SOFTWARE = linux.PERF_TYPE_SOFTWARE
PERF_TYPE_TRACEPOINT = linux.PERF_TYPE_TRACEPOINT
PERF_COUNT_SW_CPU_CLOCK = linux.PERF_COUNT_SW_CPU_CLOCK
PERF_COUNT_SW_BPF_OUTPUT = linux.PERF_COUNT_SW_BPF_OUTPUT
PERF_EVENT_IOC_DISABLE = linux.PERF_EVENT_IOC_DISABLE
PERF_EVENT_IOC_ENABLE = linux.PERF_EVENT_IOC_ENABLE
Expand Down
5 changes: 5 additions & 0 deletions internal/unix/types_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const (
EOPNOTSUPP
)

const (
PerfBitFreq = iota
)

// Constants are distinct to avoid breaking switch statements.
const (
BPF_F_NO_PREALLOC = iota
Expand Down Expand Up @@ -62,6 +66,7 @@ const (
PERF_ATTR_SIZE_VER1
PERF_TYPE_SOFTWARE
PERF_TYPE_TRACEPOINT
PERF_COUNT_SW_CPU_CLOCK
PERF_COUNT_SW_BPF_OUTPUT
PERF_EVENT_IOC_DISABLE
PERF_EVENT_IOC_ENABLE
Expand Down
25 changes: 25 additions & 0 deletions link/perf_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const (
kretprobeEvent
uprobeEvent
uretprobeEvent
periodicEvent
)

// A perfEvent represents a perf event kernel object. Exactly one eBPF program
Expand Down Expand Up @@ -200,6 +201,10 @@ func attachPerfEvent(pe *perfEvent, prog *ebpf.Program) (Link, error) {
if t := prog.Type(); t != ebpf.TracePoint {
return nil, fmt.Errorf("invalid program type (expected %s): %s", ebpf.TracePoint, t)
}
case periodicEvent:
if t := prog.Type(); t != ebpf.PerfEvent {
return nil, fmt.Errorf("invalid program type (expected %s): %s", ebpf.PerfEvent, t)
}
default:
return nil, fmt.Errorf("unknown perf event type: %d", pe.typ)
}
Expand Down Expand Up @@ -305,6 +310,26 @@ func openTracepointPerfEvent(tid uint64, pid int) (*sys.FD, error) {
return sys.NewFD(fd)
}

// openPeriodicPerfEvent opens a software event based on the CPU clock and
// sampled at specified frequency.
func openPeriodicPerfEvent(frequency uint64, cpu int) (*sys.FD, error) {
attr := unix.PerfEventAttr{
Type: unix.PERF_TYPE_SOFTWARE,
Config: unix.PERF_COUNT_SW_CPU_CLOCK,
Sample_type: unix.PERF_SAMPLE_RAW,
Bits: unix.PerfBitFreq,
Sample: frequency,
Wakeup: 1,
}

fd, err := unix.PerfEventOpen(&attr, -1, cpu, -1, unix.PERF_FLAG_FD_CLOEXEC)
if err != nil {
return nil, fmt.Errorf("opening periodic perf event: %w", err)
}

return sys.NewFD(fd)
}

func sanitizePath(base string, path ...string) (string, error) {
l := filepath.Join(path...)
p := filepath.Join(base, l)
Expand Down
93 changes: 93 additions & 0 deletions link/periodic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package link

import (
"fmt"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/internal"
)

// PeriodicOptions defines additional parameters that will be used
// when loading periodic samplers.
type PeriodicOptions struct {
// Arbitrary value that can be fetched from an eBPF program
// via `bpf_get_attach_cookie()`.
//
// Needs kernel 5.15+.
Cookie uint64
}

// Periodic attaches the given eBPF program to a periodic perf event on given
// cpu and given frequency.
//
// This is mainly useful when doing sample-based tracing or monitoring.
//
// Losing the reference to the resulting Link (tp) will close the Periodic event
// and prevent further execution of prog. The Link must be Closed during
// program shutdown to avoid leaking system resources.
func Periodic(frequency uint64, cpu int, prog *ebpf.Program, opts *PeriodicOptions) (Link, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a number of combinations of pid and cpu that the perf subsystem accepts for periodic events:

  • pid == 0 and cpu == -1
  • pid == 0 and cpu >= 0
  • pid > 0 and cpu == -1
  • pid > 0 and cpu >= 0
  • pid == -1 and cpu >= 0

This function signature only allows to set the CPU . But I do think there is a valid use case where people want to attach an eBPF program via perf only to a single PID - e.g. for debugging purposes.

possibleCPUs, err := internal.PossibleCPUs()
if err != nil {
return nil, err
}

if frequency == 0 {
return nil, fmt.Errorf("frequency can not be 0: %w", errInvalidInput)
}
if cpu < 0 || cpu > possibleCPUs {
return nil, fmt.Errorf("cpu must be greater to 0 and lower than %d: %w", possibleCPUs, errInvalidInput)
}
if prog == nil {
return nil, fmt.Errorf("prog cannot be nil: %w", errInvalidInput)
}
if prog.Type() != ebpf.PerfEvent {
return nil, fmt.Errorf("eBPF program type %s is not a PerfEvent: %w", prog.Type(), errInvalidInput)
}

fd, err := openPeriodicPerfEvent(frequency, cpu)
if err != nil {
return nil, err
}

var cookie uint64
if opts != nil {
cookie = opts.Cookie
}

pe := &perfEvent{
typ: periodicEvent,
cookie: cookie,
fd: fd,
}

lnk, err := attachPerfEvent(pe, prog)
if err != nil {
pe.Close()
return nil, err
}

return lnk, nil
}

// PeriodicAllCpus attaches the given eBPF program to a periodic event fired at
// given frequency on each cpu.
//
// See Periodic above for more information.
func PeriodicAllCpus(frequency uint64, prog *ebpf.Program, opts *PeriodicOptions) ([]Link, error) {
possibleCPUs, err := internal.PossibleCPUs()
if err != nil {
return nil, err
}

links := make([]Link, possibleCPUs)

for i := 0; i < len(links); i++ {
links[i], err = Periodic(frequency, i, prog, opts)
if err != nil {
// Links are automatically closed "some" time after the last reference is lost
return nil, fmt.Errorf("failed to link periodic prog on CPU %d: %w", i, err)
}
}

return links, nil
}