From 16741a5f84939b75da888901acd366c6bbb4f408 Mon Sep 17 00:00:00 2001 From: Rafael Roquetto Date: Fri, 29 Nov 2024 09:48:40 -0600 Subject: [PATCH] Reduce map value copying --- .../ebpf/generictracer/generictracer.go | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/pkg/internal/ebpf/generictracer/generictracer.go b/pkg/internal/ebpf/generictracer/generictracer.go index db7b5d389..82ebd0a46 100644 --- a/pkg/internal/ebpf/generictracer/generictracer.go +++ b/pkg/internal/ebpf/generictracer/generictracer.go @@ -35,7 +35,8 @@ type libModule struct { } // Hold onto Linux inode numbers of files that are already instrumented, e.g. libssl.so.3 -var instrumentedLibs = make(map[uint64]libModule) +type instrumentedLibs_t map[uint64]*libModule +var instrumentedLibs = make(instrumentedLibs_t) var libsMux sync.Mutex type Tracer struct { @@ -50,6 +51,27 @@ type Tracer struct { ingressFilters map[ifaces.Interface]*netlink.BpfFilter } +func (libs instrumentedLibs_t) at(id uint64) *libModule { + module, ok := libs[id] + + if !ok { + module = &libModule{} + libs[id] = module + } + + return module +} + +func (libs instrumentedLibs_t) find(id uint64) *libModule { + module, ok := libs[id] + + if ok { + return module + } + + return nil +} + func New(cfg *beyla.Config, metrics imetrics.Reporter) *Tracer { log := slog.With("component", "generic.Tracer") return &Tracer{ @@ -377,24 +399,20 @@ func (p *Tracer) RecordInstrumentedLib(id uint64) { libsMux.Lock() defer libsMux.Unlock() - module, ok := instrumentedLibs[id] - if ok { - instrumentedLibs[id] = libModule{closers: module.closers, references: module.references + 1} - p.log.Debug("Recorded instrumented Lib", "ino", id, "module", module) - } else { - module = libModule{references: 1} - instrumentedLibs[id] = module - p.log.Debug("Recorded instrumented Lib", "ino", id, "module", module) - } + module := instrumentedLibs.at(id) + module.references++ + + p.log.Debug("Recorded instrumented Lib", "ino", id, "module", module) } func (p *Tracer) UnlinkInstrumentedLib(id uint64) { libsMux.Lock() defer libsMux.Unlock() - if module, ok := instrumentedLibs[id]; ok { + + if module := instrumentedLibs.find(id); module != nil { p.log.Debug("Unlinking instrumented Lib - before state", "ino", id, "module", module) if module.references > 1 { - instrumentedLibs[id] = libModule{closers: module.closers, references: module.references - 1} + module.references-- } else { for _, c := range module.closers { p.log.Debug("Closing", "closable", c) @@ -410,27 +428,21 @@ func (p *Tracer) UnlinkInstrumentedLib(id uint64) { func (p *Tracer) AddModuleCloser(id uint64, c ...io.Closer) { libsMux.Lock() defer libsMux.Unlock() - module, ok := instrumentedLibs[id] - if !ok { - instrumentedLibs[id] = libModule{closers: c, references: 0} - p.log.Debug("added new module closer", "ino", id, "module", module) - } else { - closers := module.closers - closers = append(closers, c...) - mod := libModule{closers: closers, references: module.references} - instrumentedLibs[id] = mod - p.log.Debug("added module closer", "ino", id, "module", module) - } + module := instrumentedLibs.at(id) + + module.closers = append(module.closers, c...) + + p.log.Debug("added module closer", "ino", id, "module", module) } func (p *Tracer) AlreadyInstrumentedLib(id uint64) bool { libsMux.Lock() defer libsMux.Unlock() - module, ok := instrumentedLibs[id] + module := instrumentedLibs.find(id) p.log.Debug("checking already instrumented Lib", "ino", id, "module", module) - return ok + return module != nil } func (p *Tracer) SetupTC() {