-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathparse.go
114 lines (93 loc) · 2.08 KB
/
parse.go
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
package main
import (
"bytes"
"regexp"
"sort"
"strconv"
"strings"
)
var (
newline = byte(10)
statusRe = regexp.MustCompile("^goroutine\\s(\\d+)\\s\\[(.*)\\]:")
createdRe = regexp.MustCompile("^created by (.*)")
threadRe = regexp.MustCompile("^threadcreate\\sprofile:\\stotal\\s(\\d+)")
sortKey = "num"
sorters = map[string]sortFn{
"num": func(r1, r2 *Routine) bool {
return r1.Num < r2.Num
},
"state": func(r1, r2 *Routine) bool {
return r1.State < r2.State
},
}
)
type sortFn func(*Routine, *Routine) bool
type Routines []*Routine
func (r Routines) Sort() { sort.Sort(r) }
func (r Routines) Len() int { return len(r) }
func (r Routines) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r Routines) Less(i, j int) bool { return sorters[sortKey](r[i], r[j]) }
type Routine struct {
Num int `json:"no"`
State string `json:"state"`
CreatedBy string `json:"created_by"`
Trace []string `json:"trace"`
}
func ReadRoutines(buf bytes.Buffer) (routines Routines) {
var p *Routine
for {
line, err := buf.ReadString(newline)
if err != nil {
break
}
mg := statusRe.FindStringSubmatch(line)
if len(mg) > 2 {
// new routine block
p = &Routine{}
i, err := strconv.Atoi(mg[1])
if err != nil {
panic(err)
}
p.Num = i
p.State = mg[2]
routines = append(routines, p)
continue
}
mg = createdRe.FindStringSubmatch(line)
if len(mg) > 1 {
p.CreatedBy = mg[1]
}
line = strings.Trim(line, "\n")
if line != "" && p != nil {
p.Trace = append(p.Trace, line)
}
}
return
}
type ThreadCreate struct {
Count int `json:"count"`
Trace []string `json:"trace"`
}
func ReadThreads(buf bytes.Buffer) *ThreadCreate {
t := &ThreadCreate{}
for {
line, err := buf.ReadString(newline)
if err != nil {
break
}
mg := threadRe.FindStringSubmatch(line)
if len(mg) > 1 {
i, err := strconv.Atoi(mg[1])
if err != nil {
panic(err)
}
t.Count = i
continue
}
line = strings.Trim(line, "\n")
if line != "" {
t.Trace = append(t.Trace, line)
}
}
return t
}