-
Notifications
You must be signed in to change notification settings - Fork 0
/
getg.go
66 lines (58 loc) · 1.13 KB
/
getg.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
package threadMeta
import (
"reflect"
"unsafe"
)
func g_ptr() unsafe.Pointer
var (
g_type reflect.Type
g_goid_offset uintptr
g_labels_offset uintptr
)
func init() {
for _, ts := range typesByString("*runtime.g") {
typ := reflect.TypeOf(0)
rt := (*iface)(unsafe.Pointer(&typ)) // reflect.Type
rt.data = ts
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
}
goidF, ok := typ.FieldByName("goid")
if !ok {
continue
}
labelsF, ok := typ.FieldByName("labels")
if !ok {
continue
}
g_type = typ
g_goid_offset = goidF.Offset
g_labels_offset = labelsF.Offset
break
}
if g_type == nil {
panic("type·runtime·g fetch failed")
}
}
type g struct {
goid uint64
labels *unsafe.Pointer // profiler labels
}
func getg() g {
ptr := g_ptr()
if ptr == nil {
panic("get g pointer fail")
}
return g{
goid: *(*uint64)(unsafe.Pointer(uintptr(ptr) + g_goid_offset)),
labels: (*unsafe.Pointer)(unsafe.Pointer(uintptr(ptr) + g_labels_offset)),
}
}
//go:norace
func (gp g) getLabels() unsafe.Pointer {
return *gp.labels
}
//go:norace
func (gp g) setLabels(labels unsafe.Pointer) {
*gp.labels = labels
}