Skip to content

Commit

Permalink
bpflbr: Enable LBR perf event
Browse files Browse the repository at this point in the history
Signed-off-by: Leon Hwang <[email protected]>
  • Loading branch information
Asphaltt committed Dec 5, 2024
1 parent 3b8d617 commit c105dbb
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
49 changes: 49 additions & 0 deletions internal/bpflbr/lbr_perf_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2024 Leon Hwang.
// SPDX-License-Identifier: Apache-2.0

package bpflbr

import (
"fmt"

"github.com/cilium/ebpf"
"golang.org/x/sys/unix"
)

type LbrPerfEvent struct {
fds []int
}

func OpenLbrPerfEvent() (*LbrPerfEvent, error) {
var p LbrPerfEvent
var err error

defer func() {
if err != nil {
p.Close()
}
}()

numCPU, err := ebpf.PossibleCPU()
if err != nil {
return nil, fmt.Errorf("failed to get number of CPUs: %w", err)
}

p.fds = make([]int, 0, numCPU)
for i := 0; i < numCPU; i++ {
fd, err := openLbrPerfEvent(i)
if err != nil {
return nil, fmt.Errorf("failed to open LBR perf event: %w", err)
}

p.fds = append(p.fds, fd)
}

return &p, nil
}

func (p *LbrPerfEvent) Close() {
for _, fd := range p.fds {
_ = unix.Close(fd)
}
}
26 changes: 26 additions & 0 deletions internal/bpflbr/lbr_perf_event_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 Leon Hwang.
// SPDX-License-Identifier: Apache-2.0

//go:build linux

package bpflbr

import (
"unsafe"

"golang.org/x/sys/unix"
)

func openLbrPerfEvent(cpu int) (int, error) {
var attr unix.PerfEventAttr
attr.Size = uint32(unsafe.Sizeof(attr))
attr.Type = unix.PERF_TYPE_HARDWARE
attr.Config = unix.PERF_COUNT_HW_CPU_CYCLES
attr.Sample = 4000
attr.Bits |= unix.PerfBitFreq
attr.Sample_type = unix.PERF_SAMPLE_BRANCH_STACK
attr.Branch_sample_type = unix.PERF_SAMPLE_BRANCH_KERNEL |
unix.PERF_SAMPLE_BRANCH_ANY

return unix.PerfEventOpen(&attr, -1, cpu, -1, unix.PERF_FLAG_FD_CLOEXEC)
}
14 changes: 14 additions & 0 deletions internal/bpflbr/lbr_perf_event_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Leon Hwang.
// SPDX-License-Identifier: Apache-2.0

//go:build !linux

package bpflbr

import (
"golang.org/x/sys/unix"
)

func openLbrPerfEvent(cpu int) (int, error) {
return 0, unix.EOPNOTSUPP
}
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"context"
"errors"
"log"
"os"
"os/signal"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/cilium/ebpf/ringbuf"
"github.com/knightsc/gapstone"
"golang.org/x/sync/errgroup"
"golang.org/x/sys/unix"

"github.com/Asphaltt/bpflbr/internal/assert"
"github.com/Asphaltt/bpflbr/internal/bpflbr"
Expand Down Expand Up @@ -41,6 +43,13 @@ func main() {
err = bpflbr.DetectBPFFeatures(featBPFSpec)
assert.NoErr(err, "Failed to detect bpf features: %v")

lbrPerfEvents, err := bpflbr.OpenLbrPerfEvent()
if err != nil && errors.Is(err, unix.ENOENT) {
log.Fatalln("LBR is not supported on current system")
}
assert.NoErr(err, "Failed to open LBR perf event: %v")
defer lbrPerfEvents.Close()

kallsyms, err := bpflbr.NewKallsyms()
assert.NoErr(err, "Failed to read /proc/kallsyms: %v")

Expand Down

0 comments on commit c105dbb

Please sign in to comment.