Skip to content

Commit

Permalink
Reduce map value copying
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelroquetto committed Nov 29, 2024
1 parent a90e476 commit 16741a5
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions pkg/internal/ebpf/generictracer/generictracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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() {
Expand Down

0 comments on commit 16741a5

Please sign in to comment.