diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/dup_test.go | 24 | ||||
| -rw-r--r-- | internal/eventloop.go | 15 | ||||
| -rw-r--r-- | internal/eventloop_test.go | 74 |
3 files changed, 86 insertions, 27 deletions
diff --git a/internal/dup_test.go b/internal/dup_test.go deleted file mode 100644 index c06adfc..0000000 --- a/internal/dup_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package internal - -import ( - "ior/internal/types" - "syscall" - "testing" -) - -// TODO: Finish this test -func TestDup3(t *testing.T) { - // loop := newEventLoop() - - dup3Event := types.Dup3Event{ - EventType: types.ENTER_DUP3_EVENT, - TraceId: types.SYS_ENTER_DUP3, - Time: 0, - Pid: 1, - Tid: 2, - Fd: 100, - Flags: syscall.O_CLOEXEC, - } - - t.Log(dup3Event.Bytes()) -} diff --git a/internal/eventloop.go b/internal/eventloop.go index b6381ba..f770fb6 100644 --- a/internal/eventloop.go +++ b/internal/eventloop.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "syscall" + "testing" "time" "ior/internal/event" @@ -26,6 +27,7 @@ type eventLoop struct { 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(ev *event.Pair) // Callback to print the event // Statistics numTracepoints uint @@ -43,6 +45,7 @@ func newEventLoop() *eventLoop { files: make(map[int32]file.File), comms: make(map[uint32]string), prevPairTimes: make(map[uint32]uint64), + printCb: func(ev *event.Pair) { fmt.Println(ev); ev.Recycle() }, flamegraph: flamegraph.New(), done: make(chan struct{}), } @@ -68,6 +71,8 @@ func (e *eventLoop) stats() string { return stats } +var T *testing.T + func (e *eventLoop) run(ctx context.Context, rawCh <-chan []byte) { defer close(e.done) @@ -90,8 +95,7 @@ func (e *eventLoop) run(ctx context.Context, rawCh <-chan []byte) { case flags.Get().PprofEnable: ev.Recycle() default: - fmt.Println(ev.String()) - ev.Recycle() + e.printCb(ev) } e.numSyscallsAfterFilter++ } @@ -110,7 +114,10 @@ func (e *eventLoop) events(ctx context.Context, rawCh <-chan []byte) <-chan *eve for { select { - case raw := <-rawCh: + case raw, ok := <-rawCh: + if !ok { + return + } if len(raw) == 0 { continue } @@ -164,6 +171,7 @@ func (e *eventLoop) processRawEvent(raw []byte, ch chan<- *event.Pair) { } func (e *eventLoop) syscallEnter(enterEv event.Event) { + T.Log("syscallenter", enterEv) tid := enterEv.GetTid() if !e.filter.commFilterEnable { e.enterEvs[tid] = event.NewPair(enterEv) @@ -185,6 +193,7 @@ func (e *eventLoop) syscallEnter(enterEv event.Event) { } func (e *eventLoop) syscallExit(exitEv event.Event, ch chan<- *event.Pair) { + T.Log("syscall exit", exitEv) ev, ok := e.enterEvs[exitEv.GetTid()] if !ok { exitEv.Recycle() diff --git a/internal/eventloop_test.go b/internal/eventloop_test.go new file mode 100644 index 0000000..045e8f0 --- /dev/null +++ b/internal/eventloop_test.go @@ -0,0 +1,74 @@ +package internal + +import ( + "context" + "ior/internal/event" + "ior/internal/types" + "syscall" + "testing" +) + +// TODO: Finish this test +func TestEventloop(t *testing.T) { + T = t + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + inCh := make(chan []byte) + outCh := make(chan *event.Pair, 2) + + go func() { + defer close(inCh) + sendOpenFileTracepoints(t, inCh) + }() + + go func() { + for ev := range outCh { + t.Log("Received", ev) + } + }() + + ev := newEventLoop() + ev.printCb = func(ev *event.Pair) { + t.Log("printCb", ev) + outCh <- ev + } + ev.run(ctx, inCh) +} + +func sendOpenFileTracepoints(t *testing.T, ch chan<- []byte) { + enterOpenEvent := types.OpenEvent{ + EventType: types.ENTER_OPEN_EVENT, + TraceId: types.SYS_ENTER_OPENAT, + Time: 123456789, + Pid: 10, + Tid: 10, + Flags: syscall.O_RDWR, + Filename: [types.MAX_FILENAME_LENGTH]byte{}, + Comm: [types.MAX_PROGNAME_LENGTH]byte{}, + } + copy(enterOpenEvent.Filename[:], "testfile.txt") + copy(enterOpenEvent.Comm[:], "testcomm") + + bytes, err := enterOpenEvent.Bytes() + if err != nil { + t.Error(err) + } + t.Log("Sending", enterOpenEvent, bytes) + ch <- bytes + + exitOpenEvent := types.RetEvent{ + EventType: types.EXIT_OPEN_EVENT, + TraceId: types.SYS_EXIT_OPENAT, + Time: 123456789, + Ret: 42, + Pid: 10, + Tid: 10, + } + bytes, err = exitOpenEvent.Bytes() + if err != nil { + t.Error(err) + } + t.Log("Sending", exitOpenEvent, bytes) + ch <- bytes +} |
