Skip to content

Commit

Permalink
Remove caching from getUserName
Browse files Browse the repository at this point in the history
Profiling has shown that `user.LookupId(id)` is already very fast, and there's
no need to add a cache for this data.
  • Loading branch information
mjwolf committed May 15, 2024
1 parent 3231260 commit 2da9c7d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 64 deletions.
22 changes: 10 additions & 12 deletions x-pack/auditbeat/processors/sessionmd/processdb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ type DB struct {
procfs procfs.Reader
stopChan chan struct{}
removalCandidates rcHeap
namesCache *namesCache
}

func NewDB(reader procfs.Reader, logger logp.Logger) (*DB, error) {
Expand All @@ -205,7 +204,6 @@ func NewDB(reader procfs.Reader, logger logp.Logger) (*DB, error) {
procfs: reader,
stopChan: make(chan struct{}),
removalCandidates: make(rcHeap, 0),
namesCache: newNamesCache(),
}
db.startReaper()
return &db, nil
Expand Down Expand Up @@ -449,12 +447,12 @@ func (db *DB) fullProcessFromDBProcess(p Process) types.Process {
euid := p.Creds.Euid
egid := p.Creds.Egid
ret.User.ID = strconv.FormatUint(uint64(euid), 10)
username, ok := db.namesCache.getUserName(ret.User.ID)
username, ok := getUserName(ret.User.ID)
if ok {
ret.User.Name = username
}
ret.Group.ID = strconv.FormatUint(uint64(egid), 10)
groupname, ok := db.namesCache.getGroupName(ret.Group.ID)
groupname, ok := getGroupName(ret.Group.ID)
if ok {
ret.Group.Name = groupname
}
Expand All @@ -481,12 +479,12 @@ func (db *DB) fillParent(process *types.Process, parent Process) {
process.Parent.WorkingDirectory = parent.Cwd
process.Parent.Interactive = &interactive
process.Parent.User.ID = strconv.FormatUint(uint64(euid), 10)
username, ok := db.namesCache.getUserName(process.Parent.User.ID)
username, ok := getUserName(process.Parent.User.ID)
if ok {
process.Parent.User.Name = username
}
process.Parent.Group.ID = strconv.FormatUint(uint64(egid), 10)
groupname, ok := db.namesCache.getGroupName(process.Parent.Group.ID)
groupname, ok := getGroupName(process.Parent.Group.ID)
if ok {
process.Parent.Group.Name = groupname
}
Expand All @@ -506,12 +504,12 @@ func (db *DB) fillGroupLeader(process *types.Process, groupLeader Process) {
process.GroupLeader.WorkingDirectory = groupLeader.Cwd
process.GroupLeader.Interactive = &interactive
process.GroupLeader.User.ID = strconv.FormatUint(uint64(euid), 10)
username, ok := db.namesCache.getUserName(process.GroupLeader.User.ID)
username, ok := getUserName(process.GroupLeader.User.ID)
if ok {
process.GroupLeader.User.Name = username
}
process.GroupLeader.Group.ID = strconv.FormatUint(uint64(egid), 10)
groupname, ok := db.namesCache.getGroupName(process.GroupLeader.Group.ID)
groupname, ok := getGroupName(process.GroupLeader.Group.ID)
if ok {
process.GroupLeader.Group.Name = groupname
}
Expand All @@ -531,12 +529,12 @@ func (db *DB) fillSessionLeader(process *types.Process, sessionLeader Process) {
process.SessionLeader.WorkingDirectory = sessionLeader.Cwd
process.SessionLeader.Interactive = &interactive
process.SessionLeader.User.ID = strconv.FormatUint(uint64(euid), 10)
username, ok := db.namesCache.getUserName(process.SessionLeader.User.ID)
username, ok := getUserName(process.SessionLeader.User.ID)
if ok {
process.SessionLeader.User.Name = username
}
process.SessionLeader.Group.ID = strconv.FormatUint(uint64(egid), 10)
groupname, ok := db.namesCache.getGroupName(process.SessionLeader.Group.ID)
groupname, ok := getGroupName(process.SessionLeader.Group.ID)
if ok {
process.SessionLeader.Group.Name = groupname
}
Expand All @@ -556,12 +554,12 @@ func (db *DB) fillEntryLeader(process *types.Process, entryType EntryType, entry
process.EntryLeader.WorkingDirectory = entryLeader.Cwd
process.EntryLeader.Interactive = &interactive
process.EntryLeader.User.ID = strconv.FormatUint(uint64(euid), 10)
username, ok := db.namesCache.getUserName(process.EntryLeader.User.ID)
username, ok := getUserName(process.EntryLeader.User.ID)
if ok {
process.EntryLeader.User.Name = username
}
process.EntryLeader.Group.ID = strconv.FormatUint(uint64(egid), 10)
groupname, ok := db.namesCache.getGroupName(process.EntryLeader.Group.ID)
groupname, ok := getGroupName(process.EntryLeader.Group.ID)
if ok {
process.EntryLeader.Group.Name = groupname
}
Expand Down
58 changes: 6 additions & 52 deletions x-pack/auditbeat/processors/sessionmd/processdb/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,22 @@ package processdb

import (
"os/user"
"sync"
)

type cval struct {
name string
found bool
}

type namesCache struct {
mutex sync.RWMutex
users map[string]cval
groups map[string]cval
}

// newNamesCache will return a new namesCache, which can be used to get mappings
// of user and group IDs to names.
func newNamesCache() *namesCache {
u := namesCache{
users: make(map[string]cval),
groups: make(map[string]cval),
}
return &u
}

// getUserName will return the name associated with the user ID, if it exists
func (u *namesCache) getUserName(id string) (string, bool) {
u.mutex.Lock()
defer u.mutex.Unlock()

val, ok := u.users[id]
if ok {
return val.name, val.found
}
func getUserName(id string) (string, bool) {
user, err := user.LookupId(id)
cval := cval{}
if err != nil {
cval.name = ""
cval.found = false
} else {
cval.name = user.Username
cval.found = true
return "", false

Check failure on line 17 in x-pack/auditbeat/processors/sessionmd/processdb/names.go

View workflow job for this annotation

GitHub Actions / lint (linux)

File is not `goimports`-ed with -local github.com/elastic (goimports)
}
return cval.name, cval.found
return user.Username, true
}

// getGroupName will return the name associated with the group ID, if it exists
func (u *namesCache) getGroupName(id string) (string, bool) {
u.mutex.Lock()
defer u.mutex.Unlock()

val, ok := u.groups[id]
if ok {
return val.name, val.found
}
func getGroupName(id string) (string, bool) {
group, err := user.LookupGroupId(id)
cval := cval{}
if err != nil {
cval.name = ""
cval.found = false
} else {
cval.name = group.Name
cval.found = true
return "", false
}
return cval.name, cval.found
return group.Name, true
}

0 comments on commit 2da9c7d

Please sign in to comment.