Skip to content

Commit

Permalink
Fix terminate bug
Browse files Browse the repository at this point in the history
  • Loading branch information
becheran committed Mar 14, 2021
1 parent eda81b4 commit 61e13eb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 46 deletions.
13 changes: 13 additions & 0 deletions internal/collection/collection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package collection

import "strings"

// SliceContains contains true if the input slice contains the provided substring
func SliceContains(ss []string, subString string) bool {
for _, s := range ss {
if strings.Contains(s, subString) {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func ParseStackFrame(reader io.Reader) (routines []Goroutine, err error) {
continue
}

routine.StackTrace = make([]StackFrame, 0)
routine.StackTrace = make([]StackFrame, 0, 8)
for scanner.Scan() {
traceLine := scanner.Text()

Expand Down
38 changes: 18 additions & 20 deletions internal/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,6 @@ func TestParseTrace(t *testing.T) {
assert.False(t, r3.LockedToThread)
}

func Benchmark_ParseTrace(b *testing.B) {
for n := 0; n < b.N; n++ {
model.ParseStackFrame(strings.NewReader(trace_1))
}
}

var trace_2 = `goroutine 268 [runnable, locked to thread]:
syscall.Syscall9(0x7ff9af9b0500, 0x7, 0x1f4, 0xc0000902d8, 0x1, 0xc0000902c8, 0xc000090348, 0xc000090298, 0x0, 0x0, ...)
C:/Program Files/Go/src/runtime/syscall_windows.go:356 +0xf2
Expand All @@ -140,12 +134,6 @@ func TestParseLockedToThread(t *testing.T) {
assert.True(t, r0.LockedToThread)
}

func Benchmark_ParseStackPos(b *testing.B) {
for n := 0; n < b.N; n++ {
model.ParseStackPos("C:/Program Files/Go/src/runtime/syscall_windows.go:356 +0xf2")
}
}

func Test_ParseStackPos_Invalid(t *testing.T) {
_, _, _, err := model.ParseStackPos("C:/Program Files/Go/src/runtime/syscall_windows.go:356 f+0xf2")
assert.NotNil(t, err)
Expand All @@ -161,14 +149,6 @@ func Test_ParseStackPos_Valid(t *testing.T) {
assert.Equal(t, 0xf2, *pos)
}

///usr/local/go/src/net/http/server.go:2969 +0x970

func Benchmark_ParseHeader(b *testing.B) {
for n := 0; n < b.N; n++ {
model.ParseHeader("goroutine 268 [runnable, locked to thread]:")
}
}

func Test_ParseHeader_Invalid(t *testing.T) {
_, err := model.ParseHeader("")
assert.NotNil(t, err)
Expand Down Expand Up @@ -199,3 +179,21 @@ func Test_ParseHeader_Valid(t *testing.T) {
assert.Equal(t, int64(16), result.WaitSinceMin)
assert.Equal(t, true, result.LockedToThread)
}

func Benchmark_ParseTrace(b *testing.B) {
for n := 0; n < b.N; n++ {
model.ParseStackFrame(strings.NewReader(trace_1))
}
}

func Benchmark_ParseStackPos(b *testing.B) {
for n := 0; n < b.N; n++ {
model.ParseStackPos("C:/Program Files/Go/src/runtime/syscall_windows.go:356 +0xf2")
}
}

func Benchmark_ParseHeader(b *testing.B) {
for n := 0; n < b.N; n++ {
model.ParseHeader("goroutine 268 [runnable, locked to thread]:")
}
}
42 changes: 17 additions & 25 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
"sort"
"strings"

"github.com/becheran/roumon/internal/collection"
"github.com/becheran/roumon/internal/model"
"github.com/gizak/termui/v3/widgets"

termui "github.com/gizak/termui/v3"
)

const (
padding = 1
padding = 1
keepRoutineHist = 100
)

var keepRoutineHist = 100

type UI struct {
list *widgets.List
filter *widgets.Paragraph
Expand Down Expand Up @@ -155,15 +155,6 @@ func NewUI() *UI {
return &ui
}

func sliceContains(ss []string, subString string) bool {
for _, s := range ss {
if strings.Contains(s, subString) {
return true
}
}
return false
}

func (ui *UI) updateStatus() {
typeCount := make(map[string]float64)
for i := 0; i < len(ui.origData); i++ {
Expand All @@ -183,7 +174,7 @@ func (ui *UI) updateStatus() {
for idx, t := range types {
data[idx] = typeCount[t]
newLabel := t[:3]
if sliceContains(labels, newLabel) {
if collection.SliceContains(labels, newLabel) {
newLabel = fmt.Sprintf("%s%d", t[:2], uniqueID)
uniqueID++
}
Expand Down Expand Up @@ -213,12 +204,11 @@ func (ui *UI) updateList() {
}
}

// TODO: prevent always doing this!
routineList := make([]string, len(ui.filteredData))
// Update list
ui.list.Rows = make([]string, len(ui.filteredData))
for i := 0; i < len(ui.filteredData); i++ {
routineList[i] = fmt.Sprintf("%05d %s ", ui.filteredData[i].ID, ui.filteredData[i].Status)
ui.list.Rows[i] = fmt.Sprintf("%05d %s ", ui.filteredData[i].ID, ui.filteredData[i].Status)
}
ui.list.Rows = routineList

if len(ui.filteredData) == 0 {
ui.list.SelectedRow = 0
Expand Down Expand Up @@ -294,7 +284,11 @@ func (ui *UI) Run(terminate chan<- error, routinesUpdate <-chan []model.Goroutin
ui.resize(resized.Width, resized.Height)
}
case termui.KeyboardEvent:
ui.handleKeyEvent(evt.ID, terminate, pollEvents)
terminateEvent := ui.handleKeyEvent(evt.ID, pollEvents)
if terminateEvent {
terminate <- nil
return
}
}
case routines := <-routinesUpdate:
// History data size cannot be limited in termui. This is a workaround
Expand All @@ -313,26 +307,23 @@ func (ui *UI) Run(terminate chan<- error, routinesUpdate <-chan []model.Goroutin
}
}

func (ui *UI) handleKeyEvent(keyID string, terminate chan<- error, pollEvents <-chan termui.Event) {
func (ui *UI) handleKeyEvent(keyID string, pollEvents <-chan termui.Event) (terminate bool) {
switch keyID {
case "<C-c>", "<F10>":
terminate <- nil
return
return true
case "<F1>":
termui.Render(ui.grid, ui.legend, ui.help)
e := <-pollEvents
if e.ID == "<C-c>" || e.ID == "<F10>" {
terminate <- nil
return
return true
}
termui.Render(ui.grid, ui.legend)
case "<F2>":
// Pause
termui.Render(ui.grid, ui.legend, ui.paused)
e := <-pollEvents
if e.ID == "<C-c>" || e.ID == "<F10>" {
terminate <- nil
return
return true
}
termui.Render(ui.grid, ui.legend)
case "<Down>":
Expand Down Expand Up @@ -375,4 +366,5 @@ func (ui *UI) handleKeyEvent(keyID string, terminate chan<- error, pollEvents <-
}
ui.updateList()
}
return false
}

0 comments on commit 61e13eb

Please sign in to comment.