Skip to content

Commit

Permalink
bpflbr: Reduce overhead LBR entries caused by ringbuf
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 c105dbb commit 711d415
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
12 changes: 7 additions & 5 deletions bpf/lbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "vmlinux.h"
#include "bpf_helpers.h"
#include "bpf_tracing.h"
#include "bpf_cleanup.h"

#define MAX_LBR_ENTRIES 32

Expand All @@ -19,22 +18,25 @@ struct event {
__u64 func_ip;
} __attribute__((packed));

struct event lbr_events[1] SEC(".data.lbrs");

SEC("fexit")
int BPF_PROG(fexit_fn)
{
struct event *event;
__u64 retval;
int err = 0;
__u32 cpu;

guard_ringbuf(&events, event, &err);
if (!event)
return BPF_OK;
cpu = bpf_get_smp_processor_id();
event = &lbr_events[cpu];

event->nr_bytes = bpf_get_branch_snapshot(event->lbr, sizeof(event->lbr), 0); /* required 5.16 kernel. */
bpf_get_func_ret(ctx, (void *) &retval); /* required 5.17 kernel. */
event->func_ret = retval;
event->func_ip = bpf_get_func_ip(ctx); /* required 5.17 kernel. */

bpf_ringbuf_output(&events, event, sizeof(*event), 0);

return BPF_OK;
}

Expand Down
24 changes: 12 additions & 12 deletions internal/bpflbr/lbr.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import (
"golang.org/x/sys/unix"
)

func Run(reader *ringbuf.Reader, progs *bpfProgs, addr2line *Addr2Line, ksyms *Kallsyms, w io.Writer) error {
type LbrEntry struct {
From uintptr
To uintptr
Flags uint64
}
type Event struct {
Entries [32]LbrEntry
NrBytes int64
Retval int64
FuncIP uintptr
}
type LbrEntry struct {
From uintptr
To uintptr
Flags uint64
}
type Event struct {
Entries [32]LbrEntry
NrBytes int64
Retval int64
FuncIP uintptr
}

func Run(reader *ringbuf.Reader, progs *bpfProgs, addr2line *Addr2Line, ksyms *Kallsyms, w io.Writer) error {
stack := newLBRStack()

var sb strings.Builder
Expand Down
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/signal"
"syscall"
"unsafe"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/ringbuf"
Expand Down Expand Up @@ -77,12 +78,22 @@ func main() {
bpfSpec, err := loadLbr()
assert.NoErr(err, "Failed to load bpf spec: %v")

numCPU, err := ebpf.PossibleCPU()
assert.NoErr(err, "Failed to get possible cpu: %v")

lbrsMapSpec := bpfSpec.Maps[".data.lbrs"]
lbrsMapSpec.ValueSize = uint32(unsafe.Sizeof(bpflbr.Event{})) * uint32(numCPU)
lbrsMapSpec.Contents[0].Value = make([]byte, lbrsMapSpec.ValueSize)
lbrs, err := ebpf.NewMap(lbrsMapSpec)
assert.NoErr(err, "Failed to create lbrs map: %v")

events, err := ebpf.NewMap(bpfSpec.Maps["events"])
assert.NoErr(err, "Failed to create events map: %v")
defer events.Close()

reusedMaps := map[string]*ebpf.Map{
"events": events,
"events": events,
".data.lbrs": lbrs,
}

tracings, err := bpflbr.NewBPFTracing(bpfSpec, reusedMaps, tracingTargets)
Expand Down

0 comments on commit 711d415

Please sign in to comment.