summaryrefslogtreecommitdiff
path: root/internal/eventloop.go
blob: 01b37ebe55ee6dd0e74765e02041076d59e3130a (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package internal

import "C"

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"syscall"
	"time"

	"ior/internal/event"
	"ior/internal/file"
	"ior/internal/flags"
	"ior/internal/flamegraph"
	"ior/internal/types"
	. "ior/internal/types"
)

const sysEnterNameToHandleAtName = "name_to_handle_at"

type eventLoop struct {
	filter         *eventFilter
	enterEvs       map[uint32]*event.Pair      // Temp. store of sys_enter tracepoints per Tid.
	pendingHandles map[uint32]string           // map of TID to pathname from name_to_handle_at
	files          map[int32]file.File         // Track all open files by file descriptor..
	comms          map[uint32]string           // Program or thread name of the current Tid.
	prevPairTimes  map[uint32]uint64           // Previous event's time (to calculate time differences between two events)
	flamegraph     flamegraph.IorDataCollector // Storing all paths in a map structure for analysis
	printCb        func(ep *event.Pair)        // Callback to print the event

	// Statistics
	numTracepoints          uint
	numTracepointMismatches uint
	numSyscalls             uint
	numSyscallsAfterFilter  uint
	startTime               time.Time
	done                    chan struct{}
}

func newEventLoop() *eventLoop {
	return &eventLoop{
		filter:         newEventFilter(),
		enterEvs:       make(map[uint32]*event.Pair),
		pendingHandles: make(map[uint32]string),
		files:          make(map[int32]file.File),
		comms:          make(map[uint32]string),
		prevPairTimes:  make(map[uint32]uint64),
		printCb:        func(ep *event.Pair) { fmt.Println(ep); ep.Recycle() },
		flamegraph:     flamegraph.New(),
		done:           make(chan struct{}),
	}
}

func (e *eventLoop) stats() string {
	fmt.Println("Waiting for stats to be ready")
	<-e.done
	duration := time.Since(e.startTime)

	stats := fmt.Sprintf(
		"Statistics:\n"+
			"\tduration: %v\n"+
			"\ttracepoints: %v (%.2f/s) with %d mismatches (%.2f%%)\n"+
			"\tsyscalls: %d (%.2f/s)\n"+
			"\tsyscalls after filter: %d (%.2f/s)\n",
		duration,
		e.numTracepoints, float64(e.numTracepoints)/duration.Seconds(), e.numTracepointMismatches, (float64(e.numTracepointMismatches)/float64(e.numTracepoints))*100,
		e.numSyscalls, float64(e.numSyscalls)/duration.Seconds(),
		e.numSyscallsAfterFilter, float64(e.numSyscallsAfterFilter)/duration.Seconds(),
	)

	return stats
}

func (e *eventLoop) run(ctx context.Context, rawCh <-chan []byte) {
	defer close(e.done)

	if flags.Get().FlamegraphEnable {
		fmt.Println("Collecting flame graph stats, press Ctrl+C to stop")
		e.flamegraph.Start(ctx)
	}
	if flags.Get().PprofEnable {
		fmt.Println("Profiling, press Ctrl+C to stop")
	}
	if !flags.Get().FlamegraphEnable && !flags.Get().PprofEnable {
		fmt.Println(event.EventStreamHeader)
	}

	e.startTime = time.Now()
	for ep := range e.events(ctx, rawCh) {
		switch {
		case flags.Get().FlamegraphEnable:
			e.flamegraph.Ch <- ep
		case flags.Get().PprofEnable:
			ep.Recycle()
		default:
			e.printCb(ep)
		}
		e.numSyscallsAfterFilter++
	}

	if flags.Get().FlamegraphEnable {
		fmt.Println("Waiting for flamegraph")
		<-e.flamegraph.Done
	}
}

func (e *eventLoop) events(ctx context.Context, rawCh <-chan []byte) <-chan *event.Pair {
	ch := make(chan *event.Pair)

	go func() {
		defer close(ch)

		for {
			select {
			case raw, ok := <-rawCh:
				if !ok {
					return
				}
				if len(raw) == 0 {
					continue
				}
				e.processRawEvent(raw, ch)
			case <-ctx.Done():
				fmt.Println("Stopping event loop")
				return
			default:
				time.Sleep(time.Millisecond * 10)
			}
		}
	}()

	return ch
}

func (e *eventLoop) processRawEvent(raw []byte, ch chan<- *event.Pair) {
	e.numTracepoints++
	switch EventType(raw[0]) {
	case ENTER_OPEN_EVENT:
		if ev, ok := e.filter.openEvent(NewOpenEvent(raw)); ok {
			e.tracepointEntered(ev)
		}
	case EXIT_OPEN_EVENT:
		e.tracepointExited(NewRetEvent(raw), ch)
	case ENTER_FD_EVENT:
		e.tracepointEntered(NewFdEvent(raw))
	case EXIT_FD_EVENT:
		e.tracepointExited(NewFdEvent(raw), ch)
	case ENTER_NULL_EVENT:
		e.tracepointEntered(NewNullEvent(raw))
	case EXIT_NULL_EVENT:
		e.tracepointExited(NewNullEvent(raw), ch)
	case EXIT_RET_EVENT:
		e.tracepointExited(NewRetEvent(raw), ch)
	case ENTER_NAME_EVENT:
		if ev, ok := e.filter.nameEvent(NewNameEvent(raw)); ok {
			e.tracepointEntered(ev)
		}
	case ENTER_PATH_EVENT:
		if ev, ok := e.filter.pathEvent(NewPathEvent(raw)); ok {
			e.tracepointEntered(ev)
		}
	case ENTER_FCNTL_EVENT:
		e.tracepointEntered(NewFcntlEvent(raw))
	case ENTER_OPEN_BY_HANDLE_AT_EVENT:
		e.tracepointEntered(NewOpenByHandleAtEvent(raw))
	case ENTER_DUP3_EVENT:
		e.tracepointEntered(NewDup3Event(raw))
	default:
		panic(fmt.Sprintf("unhandled event type %v: %v", EventType(raw[0]), raw))
	}
}

func (e *eventLoop) tracepointEntered(enterEv event.Event) {
	tid := enterEv.GetTid()
	if !e.filter.commFilterEnable {
		e.enterEvs[tid] = event.NewPair(enterEv)
		return
	}

	switch enterEv.(type) {
	case *OpenEvent:
		e.enterEvs[tid] = event.NewPair(enterEv)
	default:
		// Only, when we have a comm name
		if _, ok := e.comms[tid]; ok {
			e.enterEvs[tid] = event.NewPair(enterEv)
		} else {
			// Probably not an issue.
			fmt.Println("WARN: No comm name for", enterEv, "process probably already vanished?")
		}
	}
}

func (e *eventLoop) tracepointExited(exitEv event.Event, ch chan<- *event.Pair) {
	ep, ok := e.enterEvs[exitEv.GetTid()]
	if !ok {
		exitEv.Recycle()
		return
	}
	delete(e.enterEvs, exitEv.GetTid())
	ep.ExitEv = exitEv
	e.numSyscalls++

	// Expect ID one lower, otherwise, enter and exit tracepoints
	// don't match up. E.g.:
	// enterEv:SYS_ENTER_OPEN => exitEv:SYS_EXIT_OPEN
	if ep.EnterEv.GetTraceId()-1 != ep.ExitEv.GetTraceId() {
		e.numTracepointMismatches++
		ep.Recycle()
		return
	}

	switch v := ep.EnterEv.(type) {
	case *OpenEvent:
		openEv := ep.EnterEv.(*OpenEvent)
		comm := types.StringValue(openEv.Comm[:])
		if fd := int32(ep.ExitEv.(*RetEvent).Ret); fd >= 0 {
			file := file.NewFd(fd, types.StringValue(openEv.Filename[:]), v.Flags)
			e.files[fd] = file
			ep.File = file
			ep.Comm = comm
		}
		e.comms[openEv.Tid] = comm

	case *NameEvent:
		nameEvent := ep.EnterEv.(*NameEvent)
		ep.File = file.NewOldnameNewname(nameEvent.Oldname[:], nameEvent.Newname[:])
		ep.Comm = e.comm(ep.EnterEv.GetTid())

	case *PathEvent:
		if ep.EnterEv.GetTraceId().Name() == sysEnterNameToHandleAtName {
			retEv, ok := ep.ExitEv.(*types.RetEvent)
			if !ok || retEv.Ret < 0 {
				ep.Recycle()
				return
			}
			pathEv := ep.EnterEv.(*PathEvent)
			pathname := types.StringValue(pathEv.Pathname[:])
			e.pendingHandles[ep.EnterEv.GetTid()] = pathname
			ep.Recycle()
			return
		}

		nameEvent := ep.EnterEv.(*PathEvent)
		if ep.Is(SYS_ENTER_CREAT) {
			if fd := int32(ep.ExitEv.(*RetEvent).Ret); fd >= 0 {
				file := file.NewFd(fd, types.StringValue(nameEvent.Pathname[:]),
					syscall.O_CREAT|syscall.O_WRONLY|syscall.O_TRUNC)
				e.files[fd] = file
				ep.File = file
			}
		} else {
			ep.File = file.NewPathname(nameEvent.Pathname[:])
		}
		ep.Comm = e.comm(ep.EnterEv.GetTid())

	case *FdEvent:
		fd := ep.EnterEv.(*FdEvent).Fd
		if file_, ok := e.files[fd]; ok {
			ep.File = file_
			if ep.Is(SYS_ENTER_CLOSE) {
				delete(e.files, fd)
			}
		} else {
			ep.File = file.NewFdWithPid(fd, v.Pid)
		}
		if ep.Is(SYS_ENTER_CLOSE_RANGE) {
			// close_range provides (first, last), but fd_event only carries the first
			// argument, so we approximate by closing all tracked fds >= first.
			retEv, ok := ep.ExitEv.(*types.RetEvent)
			if ok && retEv.Ret == 0 {
				for fdToClose := range e.files {
					if fdToClose >= fd {
						delete(e.files, fdToClose)
					}
				}
			}
		}
		ep.Comm = e.comm(ep.EnterEv.GetTid())
		if !e.filter.eventPair(ep) {
			ep.Recycle()
			return
		}
		if ep.Is(SYS_ENTER_DUP) || ep.Is(SYS_ENTER_DUP2) {
			fdFile, ok := ep.File.(file.FdFile)
			if !ok {
				panic("expected a file.FdFile")
			}
			// Duplicating fd
			newFd := int32(ep.ExitEv.(*RetEvent).Ret)
			if newFd != -1 {
				e.files[newFd] = fdFile.Dup(newFd)
			}
		}

		if retEv, ok := ep.ExitEv.(*RetEvent); ok {
			ep.Bytes = bytesFromRet(retEv)
		}

	case *Dup3Event:
		dup3Event := ep.EnterEv.(*Dup3Event)
		fd := int32(dup3Event.Fd)
		if file_, ok := e.files[fd]; ok {
			ep.File = file_
		} else {
			ep.File = file.NewFdWithPid(fd, v.Pid)
		}
		ep.Comm = e.comm(ep.EnterEv.GetTid())
		if !e.filter.eventPair(ep) {
			ep.Recycle()
			return
		}
		// Duplicating fd
		fdFile, ok := ep.File.(file.FdFile)
		if !ok {
			panic("expected a file.FdFile")
		}
		newFd := int32(ep.ExitEv.(*RetEvent).Ret)
		if newFd != -1 {
			duppedFdFile := fdFile.Dup(newFd)
			duppedFdFile.AddFlags(dup3Event.Flags & syscall.O_CLOEXEC)
			e.files[newFd] = duppedFdFile
		}

	case *OpenByHandleAtEvent:
		tid := ep.EnterEv.GetTid()
		retEvent, ok := ep.ExitEv.(*RetEvent)
		if !ok {
			panic("expected *types.RetEvent for open_by_handle_at exit")
		}

		if fd := int32(retEvent.Ret); fd >= 0 {
			openByHandleEv := ep.EnterEv.(*OpenByHandleAtEvent)
			if pathname, ok := e.pendingHandles[tid]; ok {
				delete(e.pendingHandles, tid)
				file := file.NewFd(fd, pathname, openByHandleEv.Flags)
				e.files[fd] = file
				ep.File = file
			} else {
				fdFile := file.NewFdWithPid(fd, v.Pid)
				if fdFile.Flags() == file.Flags(-1) {
					fdFile.SetFlags(openByHandleEv.Flags)
				}
				e.files[fd] = fdFile
				ep.File = fdFile
			}
			ep.Comm = e.comm(tid)
		} else {
			ep.Recycle()
			return
		}

	case *NullEvent:
		if ep.Is(SYS_ENTER_IO_URING_SETUP) {
			retEvent, ok := exitEv.(*types.RetEvent)
			if !ok {
				panic("expected *types.RetEvent")
			}
			if fd := int32(retEvent.Ret); fd >= 0 {
				fdFile := file.NewFdWithPid(fd, v.Pid)
				e.files[fd] = fdFile
				ep.File = fdFile
			}
		}
		ep.Comm = e.comm(ep.EnterEv.GetTid())
		if !e.filter.eventPair(ep) {
			ep.Recycle()
			return
		}

	case *FcntlEvent:
		ep.Comm = e.comm(ep.EnterEv.GetTid())
		fd := int32(v.Fd)
		if file_, ok := e.files[fd]; ok {
			ep.File = file_
		} else {
			ep.File = file.NewFdWithPid(fd, v.Pid)
		}
		if !e.filter.eventPair(ep) {
			ep.Recycle()
			return
		}

		retEvent, ok := exitEv.(*types.RetEvent)
		if !ok {
			panic("expected *types.RetEvent")
		}
		// Syscall returned -1, nothing was changed with the fd
		if retEvent.Ret == -1 {
			break
		}

		fdFile, ok := ep.File.(file.FdFile)
		if !ok {
			panic("expected a file.FdFile")
		}

		// See fcntl(2) for implementation details
		switch v.Cmd {
		case syscall.F_SETFL:
			const canChange = syscall.O_APPEND | syscall.O_ASYNC | syscall.O_DIRECT | syscall.O_NOATIME | syscall.O_NONBLOCK
			fdFile.SetFlags((int32(v.Arg) & int32(canChange)))
			ep.File = fdFile
			e.files[fd] = fdFile
		case syscall.F_DUPFD:
			newFd := int32(retEvent.Ret)
			e.files[newFd] = fdFile.Dup(newFd)
		case syscall.F_DUPFD_CLOEXEC:
			newFd := int32(retEvent.Ret)
			duppedFdFile := fdFile.Dup(newFd)
			duppedFdFile.AddFlags(syscall.O_CLOEXEC)
			e.files[newFd] = duppedFdFile
		}

	default:
		panic(fmt.Sprintf("unknown type: %v", v))
	}
	// TODO: implement copy_file_range
	// TODO: open_by_handle_at
	// TODO: mmap, msync...
	// TODO: getcwd?

	prevPairTime, _ := e.prevPairTimes[ep.EnterEv.GetTid()]
	ep.CalculateDurations(prevPairTime)
	e.prevPairTimes[ep.EnterEv.GetTid()] = ep.ExitEv.GetTime()
	ch <- ep
}

func (e *eventLoop) comm(tid uint32) string {
	if comm, ok := e.comms[tid]; ok {
		return comm
	}
	if linkName, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", tid)); err == nil {
		linkName = filepath.Base(linkName)
		e.comms[tid] = linkName
		return linkName
	}
	return ""
}

// bytesFromRet extracts the number of bytes transferred from a RetEvent.
// Returns 0 for nil events, errors (Ret <= 0), or unclassified syscalls.
func bytesFromRet(retEv *types.RetEvent) uint64 {
	if retEv == nil || retEv.Ret <= 0 {
		return 0
	}
	switch retEv.RetType {
	case types.READ_CLASSIFIED, types.WRITE_CLASSIFIED, types.TRANSFER_CLASSIFIED:
		return uint64(retEv.Ret)
	default:
		return 0
	}
}