Skip to content

Commit

Permalink
Fix LRU cache usage (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
keisku authored May 23, 2024
1 parent af685b3 commit a94f8ca
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions ebpf/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/cilium/ebpf/ringbuf"
"github.com/go-delve/delve/pkg/proc"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/hashicorp/golang-lru/v2/expirable"
"github.com/keisku/gmon/bininfo"
)

Expand All @@ -26,7 +26,17 @@ type eventHandler struct {

func (h *eventHandler) run(ctx context.Context) {
var event bpfEvent
stackIdCache, _ := lru.New[int32, struct{}](16)
// To delete stack_addresses that is not used recently.
stackIdCache := expirable.NewLRU[int32, struct{}](
32, // cache size
func(key int32, _ struct{}) {
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))
}
},
time.Minute, // TTL of each cache entry
)
for {
if err := h.readRecord(ctx, &event); err != nil {
if errors.Is(err, ringbuf.ErrClosed) {
Expand All @@ -47,13 +57,7 @@ func (h *eventHandler) run(ctx context.Context) {
Stack: stack,
Exit: event.Exit,
})
contains, _ := stackIdCache.ContainsOrAdd(event.StackId, struct{}{})
if !contains {
slog.Debug("delete stack_addresses", slog.Int("stack_id", int(event.StackId)))
if err := h.objs.StackAddresses.Delete(event.StackId); err != nil {
slog.Debug("Failed to delete stack_addresses", slog.Any("error", err))
}
}
_ = stackIdCache.Add(event.StackId, struct{}{})
}
}

Expand Down

0 comments on commit a94f8ca

Please sign in to comment.