Skip to content

Commit ca2c56d

Browse files
cache: improved replacing processes
When replacing a process with the same PID (fork/clone), and ebpf.QueueEventsSize is > 0 we may receive out or order events. For example if the checksum calculation takes too much time. Now we avoid updating the process in this scenario.
1 parent ea13182 commit ca2c56d

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

daemon/procmon/cache_events.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func NewEventsStore() *EventsStore {
8282
// If computing checksums is enabled, new checksums will be computed if needed,
8383
// or reused existing ones otherwise.
8484
func (e *EventsStore) Add(proc *Process) {
85-
log.Debug("[cache] EventsStore.Add() %d, %s, %s, total: %d", proc.ID, proc.Path, proc.Tree, e.Len())
85+
log.Debug("[cache] EventsStore.Add() %d, %s, %s, %d, total: %d", proc.ID, proc.Path, proc.Tree, proc.Starttime, e.Len())
8686
// Add the item to cache ASAP,
8787
// then calculate the checksums if needed.
8888
e.UpdateItem(proc)
@@ -96,25 +96,32 @@ func (e *EventsStore) Add(proc *Process) {
9696

9797
// UpdateItem updates a cache item
9898
func (e *EventsStore) UpdateItem(proc *Process) {
99-
log.Trace("[cache] updateItem() updating events store (total: %d), pid: %d, path: %s, %v", e.Len(), proc.ID, proc.Path, proc.Tree)
99+
log.Trace("[cache] updateItem() updating events store (total: %d), pid: %d, path: %s, %d, %v", e.Len(), proc.ID, proc.Path, proc.Starttime, proc.Tree)
100100
if proc.Path == "" {
101101
return
102102
}
103103
e.mu.Lock()
104+
defer e.mu.Unlock()
105+
106+
oldItem := e.eventByPID[proc.ID]
107+
108+
// Avoid replacing new procs with old ones.
109+
// This can occur when QueueEventsSize is > 0 and computing the checksum takes more time than expected.
110+
if oldItem.Proc.Path != proc.Path && oldItem.Proc.Starttime > proc.Starttime {
111+
log.Trace("skipping out-of-order updateItem: %s (%d) -> %s (%d)", oldItem.Proc.Path, oldItem.Proc.Starttime, proc.Path, proc.Starttime)
112+
return
113+
}
114+
104115
ev := ExecEventItem{
105116
Proc: *proc,
106117
LastSeen: time.Now().UnixNano(),
107118
}
108119
e.eventByPID[proc.ID] = ev
109-
e.mu.Unlock()
110120
}
111121

112122
// ReplaceItem replaces an existing process with a new one.
113123
func (e *EventsStore) ReplaceItem(oldProc, newProc *Process) {
114-
log.Trace("[event inCache, replacement] new: %d, %s -> inCache: %d -> %s - Trees: %s, %s", newProc.ID, newProc.Path, oldProc.ID, oldProc.Path, oldProc.Tree, newProc.Tree)
115-
// Note: in rare occasions, the process being replaced is the older one.
116-
// if oldProc.Starttime > newProc.Starttime {}
117-
//
124+
log.Trace("[event inCache, replacement] new: %d, %s -> inCache: %d -> %s - %d, Trees: %s, %s", newProc.ID, newProc.Path, oldProc.ID, oldProc.Path, newProc.Starttime, oldProc.Tree, newProc.Tree)
118125

119126
newProc.PPID = oldProc.ID
120127
e.UpdateItem(newProc)

0 commit comments

Comments
 (0)