-
Notifications
You must be signed in to change notification settings - Fork 20
/
routine_goid.go
153 lines (143 loc) · 3.01 KB
/
routine_goid.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
package routine
import (
"fmt"
"runtime"
"strings"
"sync"
"unsafe"
"github.com/go-eden/routine/g"
)
const (
ptrSize = 4 << (^uintptr(0) >> 63) // unsafe.Sizeof(uintptr(0)) but an ideal const
stackSize = 1024
)
var (
goidOffset uintptr
anchor = []byte("goroutine ")
stackBufPool = sync.Pool{
New: func() interface{} {
buf := make([]byte, 64)
return &buf
},
}
goidOffsetDict = map[string]int64{
"go1.12": 152,
"go1.13": 152,
"go1.14": 152,
"go1.15": 152,
"go1.16": 152,
"go1.17": 152,
}
)
func init() {
var off int64
version := runtime.Version()
for k, v := range goidOffsetDict {
if version == k || strings.HasPrefix(version, k) {
off = v
break
}
}
goidOffset = uintptr(off)
}
// getGoidByStack parse the current goroutine's id from caller stack.
// This function could be very slow(like 3000us/op), but it's very safe.
func getGoidByStack() (goid int64) {
bp := stackBufPool.Get().(*[]byte)
defer stackBufPool.Put(bp)
b := *bp
b = b[:runtime.Stack(b, false)]
goid, _ = findNextGoid(b, 0)
return
}
// getGoidByNative parse the current goroutine's id from G.
// This function could be very fast(like 1ns/op), but it could be failed.
func getGoidByNative() (int64, bool) {
if goidOffset == 0 {
return 0, false
}
tmp := g.G()
if tmp == nil {
return 0, false
}
p := (*int64)(unsafe.Pointer(uintptr(tmp) + goidOffset))
if p == nil {
return 0, false
}
return *p, true
}
// getAllGoidByStack find all goid through stack; WARNING: This function could be very inefficient
func getAllGoidByStack() (goids []int64) {
count := runtime.NumGoroutine()
size := count * stackSize // it's ok?
buf := make([]byte, size)
n := runtime.Stack(buf, true)
buf = buf[:n]
// parse all goids
goids = make([]int64, 0, count+4)
for i := 0; i < len(buf); {
goid, off := findNextGoid(buf, i)
if goid > 0 {
goids = append(goids, goid)
}
i = off
}
return
}
// Find the next goid from `buf[off:]`
func findNextGoid(buf []byte, off int) (goid int64, next int) {
i := off
hit := false
// skip to anchor
acr := anchor
for sb := len(buf) - len(acr); i < sb; {
if buf[i] == acr[0] && buf[i+1] == acr[1] && buf[i+2] == acr[2] && buf[i+3] == acr[3] &&
buf[i+4] == acr[4] && buf[i+5] == acr[5] && buf[i+6] == acr[6] &&
buf[i+7] == acr[7] && buf[i+8] == acr[8] && buf[i+9] == acr[9] {
hit = true
i += len(acr)
break
}
for ; i < len(buf) && buf[i] != '\n'; i++ {
}
i++
}
// return if not hit
if !hit {
return 0, len(buf)
}
// extract goid
var done bool
for ; i < len(buf) && !done; i++ {
switch buf[i] {
case '0':
goid *= 10
case '1':
goid = goid*10 + 1
case '2':
goid = goid*10 + 2
case '3':
goid = goid*10 + 3
case '4':
goid = goid*10 + 4
case '5':
goid = goid*10 + 5
case '6':
goid = goid*10 + 6
case '7':
goid = goid*10 + 7
case '8':
goid = goid*10 + 8
case '9':
goid = goid*10 + 9
case ' ':
done = true
break
default:
goid = 0
fmt.Println("should never be here, any bug happens")
}
}
next = i
return
}