Skip to content

Commit

Permalink
Set stack to cache since BPF_F_REUSE_STACKID is not set by default (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
keisku authored Jul 12, 2024
1 parent 87e2337 commit 9c4ba26
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions ebpf/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ type eventHandler struct {
func (h *eventHandler) run(ctx context.Context) {
var event bpfEvent
// To delete stack_addresses that is not used recently.
stackIdCache := expirable.NewLRU[int32, struct{}](
stackIdCache := expirable.NewLRU[int32, []*proc.Function](
32, // cache size
func(key int32, _ struct{}) {
func(key int32, _ []*proc.Function) {
slog.Debug("delete stack_addresses", slog.Int("stack_id", int(key)))
if err := h.objs.StackAddresses.Delete(key); err != nil {
slog.Debug("Failed to delete stack_addresses", slog.Any("error", err))
Expand All @@ -46,18 +46,24 @@ func (h *eventHandler) run(ctx context.Context) {
slog.Warn("Failed to read bpf ring buffer", slog.Any("error", err))
continue
}
stack, err := h.lookupStack(ctx, event.StackId)
if err != nil {
slog.Warn(err.Error())
continue
var stack []*proc.Function
var ok bool
var err error
stack, ok = stackIdCache.Get(event.StackId)
if !ok {
stack, err = h.lookupStack(ctx, event.StackId)
if err != nil {
slog.Warn(err.Error())
continue
}
}
h.sendGoroutine(goroutine{
Id: event.GoroutineId,
ObservedAt: time.Now(),
Stack: stack,
Exit: event.Exit,
})
_ = stackIdCache.Add(event.StackId, struct{}{})
_ = stackIdCache.Add(event.StackId, stack)
}
}

Expand Down

0 comments on commit 9c4ba26

Please sign in to comment.