-
Notifications
You must be signed in to change notification settings - Fork 0
/
goroutine.go
226 lines (194 loc) · 5.31 KB
/
goroutine.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
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
package pinpoint
import (
"bufio"
"bytes"
"io"
"reflect"
"regexp"
"runtime"
"runtime/pprof"
"strconv"
"strings"
"unsafe"
"github.com/pinpoint-apm/pinpoint-go-agent/asm"
pb "github.com/pinpoint-apm/pinpoint-go-agent/protobuf"
)
var (
goIdOffset uintptr
stateMap map[string]pb.PThreadState
)
func initGoroutine() {
goIdOffset = getOffset()
if goIdOffset > 0 {
Log("cmd").Infof("goroutine id from runtime.g (offset = %d)", goIdOffset)
} else {
Log("cmd").Infof("goroutine id from stack dump")
}
stateMap = make(map[string]pb.PThreadState, 0)
stateMap[""] = pb.PThreadState_THREAD_STATE_UNKNOWN
stateMap["???"] = pb.PThreadState_THREAD_STATE_UNKNOWN
stateMap["idle"] = pb.PThreadState_THREAD_STATE_NEW
stateMap["runnable"] = pb.PThreadState_THREAD_STATE_RUNNABLE
stateMap["running"] = pb.PThreadState_THREAD_STATE_RUNNABLE
stateMap["syscall"] = pb.PThreadState_THREAD_STATE_RUNNABLE
stateMap["copystack"] = pb.PThreadState_THREAD_STATE_RUNNABLE
stateMap["preempted"] = pb.PThreadState_THREAD_STATE_RUNNABLE
stateMap["dead"] = pb.PThreadState_THREAD_STATE_TERMINATED
stateMap["dumping heap"] = pb.PThreadState_THREAD_STATE_BLOCKED
stateMap["garbage collection"] = pb.PThreadState_THREAD_STATE_BLOCKED
stateMap["garbage collection scan"] = pb.PThreadState_THREAD_STATE_BLOCKED
stateMap["force gc (idle)"] = pb.PThreadState_THREAD_STATE_BLOCKED
stateMap["trace reader (blocked)"] = pb.PThreadState_THREAD_STATE_BLOCKED
stateMap["preempted"] = pb.PThreadState_THREAD_STATE_BLOCKED
stateMap["debug call"] = pb.PThreadState_THREAD_STATE_BLOCKED
stateMap["stopping the world"] = pb.PThreadState_THREAD_STATE_BLOCKED
// the rest of the state are considered pb.PThreadState_THREAD_STATE_WAITING
// refer https://github.com/golang/go/blob/master/src/runtime/runtime2.go: waitReasonStrings
}
type goroutine struct {
id int64
header string
state string
buf *bytes.Buffer
span *activeSpanInfo
}
func (g *goroutine) addLine(line string) {
g.buf.WriteString(line)
g.buf.WriteString("\n")
}
func (g *goroutine) threadState() pb.PThreadState {
if s, ok := stateMap[g.state]; ok {
return s
}
return pb.PThreadState_THREAD_STATE_WAITING
}
func (g *goroutine) stackTrace() []string {
return append(make([]string, 0), g.buf.String())
}
func newGoroutine(idStr string, state string, line string) *goroutine {
if id, err := strconv.Atoi(idStr); err == nil {
g := &goroutine{
id: int64(id),
header: "goroutine " + idStr,
state: strings.TrimSpace(strings.Split(state, ",")[0]),
buf: &bytes.Buffer{},
}
g.addLine(line)
return g
} else {
Log("cmd").Errorf("convert goroutine id: %v", err)
}
return nil
}
type goroutineDump struct {
goroutines []*goroutine
}
func (gd *goroutineDump) add(g *goroutine) {
gd.goroutines = append(gd.goroutines, g)
}
func (gd *goroutineDump) search(s string) *goroutine {
for _, g := range gd.goroutines {
if g.header == s {
return g
}
}
return nil
}
func newGoroutineDump() *goroutineDump {
return &goroutineDump{
goroutines: []*goroutine{},
}
}
func dumpGoroutine() (dump *goroutineDump) {
var b bytes.Buffer
buf := bufio.NewWriter(&b)
defer func() {
if e := recover(); e != nil {
Log("cmd").Errorf("profile goroutine: %v", e)
dump = nil
}
}()
if p := pprof.Lookup("goroutine"); p != nil {
if err := p.WriteTo(buf, 2); err != nil {
Log("cmd").Errorf("profile goroutine: %v", err)
return nil
}
}
dump = parseProfile(&b)
return
}
func parseProfile(r io.Reader) *goroutineDump {
dump := newGoroutineDump()
var g *goroutine
scanner := bufio.NewScanner(r)
re := regexp.MustCompile(`^goroutine\s+(\d+)\s+\[(.*)\]:$`)
for scanner.Scan() {
line := scanner.Text()
if g == nil {
if match := re.FindAllStringSubmatch(line, 1); match != nil {
if g = newGoroutine(match[0][1], match[0][2], line); g != nil {
if v, ok := realTimeActiveSpan.Load(g.id); ok {
g.span = v.(*activeSpanInfo)
dump.add(g)
}
}
}
} else {
if line == "" {
g = nil
} else {
g.addLine(line)
}
}
}
if err := scanner.Err(); err != nil {
Log("cmd").Errorf("scan goroutine profile: %v", err)
return nil
}
return dump
}
func curGoroutineID() int64 {
if goIdOffset > 0 {
return goIdFromG()
} else {
return goIdFromDump()
}
}
var prefix = len("goroutine ")
func goIdFromDump() int64 {
b := make([]byte, 64)
b = b[prefix:runtime.Stack(b, false)]
idStr := string(b[:bytes.IndexByte(b, ' ')])
Log("cmd").Debugf("idStr: '%s'", idStr)
id, _ := strconv.ParseInt(idStr, 10, 64)
return id
}
func getOffset() uintptr {
if typ := typeRuntimeG(); typ != nil {
if f, ok := typ.FieldByName("goid"); ok {
return f.Offset
}
}
return 0
}
func typeRuntimeG() reflect.Type {
sections, offsets := typelinks()
//load go types
for i, base := range sections {
for _, offset := range offsets[i] {
typeAddr := add(base, uintptr(offset), "")
typ := reflect.TypeOf(*(*interface{})(unsafe.Pointer(&typeAddr)))
if typ.Kind() == reflect.Ptr && typ.Elem().String() == "runtime.g" {
return typ.Elem()
}
}
}
return nil
}
//go:linkname typelinks reflect.typelinks
func typelinks() (sections []unsafe.Pointer, offset [][]int32)
//go:linkname add reflect.add
func add(p unsafe.Pointer, x uintptr, whySafe string) unsafe.Pointer
func goIdFromG() int64 {
return *(*int64)(unsafe.Pointer(uintptr(asm.Getg()) + goIdOffset))
}