Skip to content

vendor: bump golang-lru to v2 (requires Go >= v1.18 support for generics) #579

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

Merged
merged 1 commit into from
Dec 15, 2022
Merged
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/fatih/color v1.13.0
github.com/google/go-cmp v0.5.8
github.com/google/gops v0.3.25
github.com/hashicorp/golang-lru v0.5.4
github.com/hashicorp/golang-lru/v2 v2.0.1
github.com/iancoleman/strcase v0.2.0
github.com/jpillora/longestcommon v0.0.0-20161227235612-adb9d91ee629
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4=
github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand Down
28 changes: 14 additions & 14 deletions pkg/ksyms/ksyms.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/cilium/tetragon/pkg/logger"

lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
)

type ksym struct {
Expand All @@ -26,7 +26,7 @@ type ksym struct {
// Ksyms is a structure for kernel symbols
type Ksyms struct {
table []ksym
fnCache *lru.Cache
fnCache *lru.Cache[uint64, fnOffsetVal]
}

// FnOffset is a function location (function name + offset)
Expand All @@ -35,6 +35,12 @@ type FnOffset struct {
Offset uint64
}

// fnOffsetVal is used as a value in the FnOffset cache.
type fnOffsetVal struct {
fnOffset *FnOffset
err error
}

// ToString returns a string representation of FnOffset
func (fo *FnOffset) ToString() string {
return fmt.Sprintf("%s()+0x%x", fo.SymName, fo.Offset)
Expand Down Expand Up @@ -108,7 +114,7 @@ func NewKsyms(procfs string) (*Ksyms, error) {
sort.Slice(ksyms.table[:], func(i1, i2 int) bool { return ksyms.table[i1].addr < ksyms.table[i2].addr })
}

fc, err := lru.New(1024)
fc, err := lru.New[uint64, fnOffsetVal](1024)
if err == nil {
ksyms.fnCache = fc
} else {
Expand All @@ -121,26 +127,20 @@ func NewKsyms(procfs string) (*Ksyms, error) {

// GetFnOffset -- returns the FnOffset for a given address
func (k *Ksyms) GetFnOffset(addr uint64) (*FnOffset, error) {
type V struct {
ret *FnOffset
err error
}

// no cache
if k.fnCache == nil {
return k.getFnOffset(addr)
}

// cache hit
if v, ok := k.fnCache.Get(addr); ok {
val := v.(V)
return val.ret, val.err
if ret, ok := k.fnCache.Get(addr); ok {
return ret.fnOffset, ret.err
}

// cache miss
ret, err := k.getFnOffset(addr)
k.fnCache.Add(addr, V{ret: ret, err: err})
return ret, err
fnOffset, err := k.getFnOffset(addr)
k.fnCache.Add(addr, fnOffsetVal{fnOffset: fnOffset, err: err})
return fnOffset, err

}

Expand Down
14 changes: 4 additions & 10 deletions pkg/process/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/metrics/errormetrics"
"github.com/cilium/tetragon/pkg/metrics/mapmetrics"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
)

type Cache struct {
cache *lru.Cache
cache *lru.Cache[string, *ProcessInternal]
deleteChan chan *ProcessInternal
stopChan chan bool
}
Expand Down Expand Up @@ -127,7 +127,7 @@ func (pc *Cache) Purge() {
func NewCache(
processCacheSize int,
) (*Cache, error) {
lruCache, err := lru.New(processCacheSize)
lruCache, err := lru.New[string, *ProcessInternal](processCacheSize)
if err != nil {
return nil, err
}
Expand All @@ -151,18 +151,12 @@ func NewCache(
}

func (pc *Cache) get(processID string) (*ProcessInternal, error) {
entry, ok := pc.cache.Get(processID)
process, ok := pc.cache.Get(processID)
if !ok {
logger.GetLogger().WithField("id in event", processID).Debug("process not found in cache")
errormetrics.ErrorTotalInc(errormetrics.ProcessCacheMissOnGet)
return nil, fmt.Errorf("invalid entry for process ID: %s", processID)
}
process, ok := entry.(*ProcessInternal)
if !ok {
logger.GetLogger().WithField("process entry", entry).Debug("invalid entry in process cache")
errormetrics.ErrorTotalInc(errormetrics.ProcessCacheMissOnGet)
return nil, fmt.Errorf("process with ID %s not found in cache", processID)
}
return process, nil
}

Expand Down
12 changes: 4 additions & 8 deletions pkg/sensors/tracing/generickprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/cilium/tetragon/pkg/sensors"
"github.com/cilium/tetragon/pkg/sensors/base"
"github.com/cilium/tetragon/pkg/sensors/program"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/sirupsen/logrus"

gt "github.com/cilium/tetragon/pkg/generictypes"
Expand Down Expand Up @@ -107,7 +107,7 @@ type genericKprobe struct {
// the map, so that we can merge them when the return event is
// generated. The events are maintained in the map below, using
// the thread_id and the enter ktime as the key.
pendingEvents *lru.Cache
pendingEvents *lru.Cache[pendingEventKey, pendingEvent]

tableId idtable.EntryID

Expand Down Expand Up @@ -465,7 +465,7 @@ func createGenericKprobeSensor(name string, kprobes []v1alpha1.KProbeSpec) (*sen
fqdns: fqdns,
}

kprobeEntry.pendingEvents, err = lru.New(4096)
kprobeEntry.pendingEvents, err = lru.New[pendingEventKey, pendingEvent](4096)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1064,11 +1064,7 @@ func handleGenericKprobe(r *bytes.Reader) ([]observer.Event, error) {
curr := pendingEvent{ev: unix, returnEvent: returnEvent}
key := pendingEventKey{threadId: m.ThreadId, ktimeEnter: ktimeEnter}

if data, exists := gk.pendingEvents.Get(key); exists {
prev, ok := data.(pendingEvent)
if !ok {
return nil, fmt.Errorf("Internal error: wrong type in pendingEvents")
}
if prev, exists := gk.pendingEvents.Get(key); exists {
gk.pendingEvents.Remove(key)
unix, retArg = retprobeMerge(prev, curr)
} else {
Expand Down
150 changes: 0 additions & 150 deletions vendor/github.com/hashicorp/golang-lru/lru.go

This file was deleted.

Loading