summaryrefslogtreecommitdiff
path: root/internal/eventloop_state.go
blob: a277e31c2ea4651b6d03481142fc897466fbfa21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package internal

import (
	"cmp"
	"slices"

	"ior/internal/event"
	"ior/internal/file"
)

// fdTracker holds the process's open file-descriptor table and a procfs
// resolution cache for fds that were opened before tracing started.
type fdTracker struct {
	files        map[int32]file.File
	procFdCache  map[uint64]*file.FdFile // procfs-resolved metadata for unknown FDs
	procFdAges   map[uint64]uint64       // access age per cache entry, for LRU eviction
	maxCacheSize int                     // max entries before eviction; 0 = defaultMaxProcFdCacheSize
	age          uint64                  // monotonic counter for LRU ordering
}

// pendingHandleTracker holds unresolved name_to_handle_at pathnames keyed by
// TID until the corresponding open_by_handle_at exit consumes them.
type pendingHandleTracker struct {
	paths        map[uint32]string
	pathAges     map[uint32]uint64
	maxCacheSize int
	age          uint64
}

func newFDTracker(files map[int32]file.File) *fdTracker {
	if files == nil {
		files = make(map[int32]file.File)
	}
	return &fdTracker{
		files:       files,
		procFdCache: make(map[uint64]*file.FdFile),
		procFdAges:  make(map[uint64]uint64),
	}
}

func newPendingHandleTracker() *pendingHandleTracker {
	return &pendingHandleTracker{
		paths:    make(map[uint32]string),
		pathAges: make(map[uint32]uint64),
	}
}

func (t *fdTracker) get(fd int32) (file.File, bool) {
	f, ok := t.files[fd]
	return f, ok
}

func (t *fdTracker) set(fd int32, f file.File) {
	t.files[fd] = f
}

func (t *fdTracker) delete(fd int32) {
	delete(t.files, fd)
}

func (t *fdTracker) closeRangeFrom(first int32) {
	for fd := range t.files {
		if fd >= first {
			delete(t.files, fd)
		}
	}
}

// resolve returns the file.File for fd, checking the fd table first, then the
// procfs cache, and finally resolving via procfs and caching the result.
func (t *fdTracker) resolve(fd int32, pid uint32) file.File {
	if fdFile, ok := t.get(fd); ok {
		return fdFile
	}
	if fd < 0 {
		return file.NewFd(fd, "", -1)
	}
	if cached, ok := t.cachedProcFdFile(fd, pid); ok {
		return cached
	}
	// Cache first procfs resolution to avoid repeated /proc lookups for hot unknown FDs.
	discovered := file.NewFdWithPid(fd, pid)
	t.setProcFdCache(fd, pid, discovered)
	return discovered
}

func (t *fdTracker) cachedProcFdFile(fd int32, pid uint32) (*file.FdFile, bool) {
	if t.procFdCache == nil {
		return nil, false
	}
	key := procFdCacheKey(pid, fd)
	cache, ok := t.procFdCache[key]
	if ok {
		t.age++
		t.procFdAges[key] = t.age
	}
	return cache, ok
}

func (t *fdTracker) setProcFdCache(fd int32, pid uint32, resolved *file.FdFile) {
	if t.procFdCache == nil {
		t.procFdCache = make(map[uint64]*file.FdFile)
		t.procFdAges = make(map[uint64]uint64)
	}
	key := procFdCacheKey(pid, fd)
	t.age++
	t.procFdCache[key] = resolved
	t.procFdAges[key] = t.age
	t.pruneCache()
}

func (t *fdTracker) deleteProcFdCache(fd int32, pid uint32) {
	t.deleteCacheKey(procFdCacheKey(pid, fd))
}

func (t *fdTracker) deleteProcFdCacheFrom(first int32, pid uint32) {
	if t.procFdCache == nil {
		return
	}
	for key := range t.procFdCache {
		cachePid := uint32(key >> 32)
		cacheFd := int32(uint32(key))
		if cachePid == pid && cacheFd >= first {
			t.deleteCacheKey(key)
		}
	}
}

func (t *fdTracker) pruneCache() {
	if t.procFdCache == nil {
		return
	}
	limit := t.cacheLimit()
	if len(t.procFdCache) <= limit {
		return
	}
	trimOldestProcFdEntries(t.procFdCache, t.procFdAges, trimTarget(limit))
}

func (t *fdTracker) cacheLimit() int {
	if t.maxCacheSize > 0 {
		return t.maxCacheSize
	}
	return defaultMaxProcFdCacheSize
}

// deleteCacheKey removes a cache entry by its composite key.
// delete on a nil map is a no-op in Go, so this is safe even before any cache entries are set.
func (t *fdTracker) deleteCacheKey(key uint64) {
	delete(t.procFdCache, key)
	delete(t.procFdAges, key)
}

func (t *pendingHandleTracker) set(tid uint32, pathname string) {
	if t.paths == nil {
		t.paths = make(map[uint32]string)
		t.pathAges = make(map[uint32]uint64)
	}
	t.age++
	t.paths[tid] = pathname
	t.pathAges[tid] = t.age
	t.prune()
}

func (t *pendingHandleTracker) consume(tid uint32) (string, bool) {
	pathname, ok := t.paths[tid]
	if !ok {
		return "", false
	}
	delete(t.paths, tid)
	delete(t.pathAges, tid)
	return pathname, true
}

func (t *pendingHandleTracker) delete(tid uint32) {
	delete(t.paths, tid)
	delete(t.pathAges, tid)
}

func (t *pendingHandleTracker) prune() {
	if t.paths == nil {
		return
	}
	limit := t.limit()
	if len(t.paths) <= limit {
		return
	}
	trimOldestPendingHandles(t.paths, t.pathAges, trimTarget(limit))
}

func (t *pendingHandleTracker) limit() int {
	if t.maxCacheSize > 0 {
		return t.maxCacheSize
	}
	return defaultMaxPendingHandleEntries
}

// pairTracker holds the state for matching sys_enter events to their sys_exit
// counterparts and computing inter-syscall durations per TID.
type pairTracker struct {
	enters    map[uint32]*event.Pair // pending enter events, keyed by TID
	enterAges map[uint32]uint64      // insertion order per TID, for LRU eviction
	prevTimes map[uint32]uint64      // previous pair's exit time per TID, for DurationToPrev
	maxSize   int                    // max pending enter events before pruning; 0 = default
	age       uint64                 // monotonic counter for LRU ordering
}

func newPairTracker() pairTracker {
	return pairTracker{
		enters:    make(map[uint32]*event.Pair),
		enterAges: make(map[uint32]uint64),
		prevTimes: make(map[uint32]uint64),
	}
}

// set stores enterEv as a pending enter event for its TID, recycling any
// prior unmatched enter for the same TID, then prunes if over the limit.
// Maps are initialized lazily on first write; consume is safe on a nil map because
// Go map reads on nil return the zero value.
func (p *pairTracker) set(enterEv event.Event) {
	if p.enters == nil {
		p.enters = make(map[uint32]*event.Pair)
		p.enterAges = make(map[uint32]uint64)
		p.prevTimes = make(map[uint32]uint64)
	}
	tid := enterEv.GetTid()
	pair := event.NewPair(enterEv)
	if prev, ok := p.enters[tid]; ok && prev != nil {
		prev.Recycle()
	}
	p.age++
	p.enters[tid] = pair
	p.enterAges[tid] = p.age
	p.prune()
}

// consume removes and returns the pending enter pair for tid.
// Reading a nil map returns the zero value in Go, so this is safe before any set call.
func (p *pairTracker) consume(tid uint32) (*event.Pair, bool) {
	pair, ok := p.enters[tid]
	if !ok {
		return nil, false
	}
	delete(p.enters, tid)
	delete(p.enterAges, tid)
	return pair, true
}

// prevTime returns the exit time of the previous pair for tid, used to compute DurationToPrev.
func (p *pairTracker) prevTime(tid uint32) uint64 {
	return p.prevTimes[tid]
}

// setPrevTime records the exit time of the most recent completed pair for tid.
func (p *pairTracker) setPrevTime(tid uint32, t uint64) {
	if p.prevTimes == nil {
		p.prevTimes = make(map[uint32]uint64)
	}
	p.prevTimes[tid] = t
}

func (p *pairTracker) prune() {
	limit := p.limit()
	if len(p.enters) <= limit {
		return
	}
	trimOldestPendingPairs(p.enters, p.enterAges, trimTarget(limit))
}

func (p *pairTracker) limit() int {
	if p.maxSize > 0 {
		return p.maxSize
	}
	return defaultMaxPendingEnterEvs
}

// trimLRU evicts the oldest entries from state (and their corresponding ages
// entries) until len(state) == targetSize. Keys are compared by their age
// value in ages; smaller age means older. The optional cleanup callback is
// called with each evicted value before it is removed from state — use it to
// recycle pooled objects (e.g. event.Pair.Recycle).
func trimLRU[K comparable, V any](state map[K]V, ages map[K]uint64, targetSize int, cleanup func(V)) {
	excess := len(state) - targetSize
	if excess <= 0 {
		return
	}
	type entry struct {
		key K
		age uint64
	}
	oldest := make([]entry, 0, len(state))
	for k := range state {
		oldest = append(oldest, entry{key: k, age: ages[k]})
	}
	slices.SortFunc(oldest, func(a, b entry) int { return cmp.Compare(a.age, b.age) })
	for _, e := range oldest[:excess] {
		if cleanup != nil {
			cleanup(state[e.key])
		}
		delete(state, e.key)
		delete(ages, e.key)
	}
}

func trimOldestPendingPairs(state map[uint32]*event.Pair, ages map[uint32]uint64, targetSize int) {
	// Recycle evicted pairs back to the pool before deletion.
	trimLRU(state, ages, targetSize, func(pair *event.Pair) {
		if pair != nil {
			pair.Recycle()
		}
	})
}

func trimOldestProcFdEntries(state map[uint64]*file.FdFile, ages map[uint64]uint64, targetSize int) {
	trimLRU(state, ages, targetSize, nil)
}

func trimOldestPendingHandles(state map[uint32]string, ages map[uint32]uint64, targetSize int) {
	trimLRU(state, ages, targetSize, nil)
}

func trimTarget(limit int) int {
	target := limit - limit/cacheTrimDivisor
	if target < 1 {
		return 1
	}
	return target
}

func procFdCacheKey(pid uint32, fd int32) uint64 {
	return uint64(pid)<<32 | uint64(uint32(fd))
}