-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.go
168 lines (154 loc) · 4.23 KB
/
detect.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
package detectlock
import (
"fmt"
"sort"
"strings"
)
var debug bool
// EnableDebug enable for debug
func EnableDebug() {
reset()
debug = true
}
// DisableDebug disable for debug
func DisableDebug() {
debug = false
clear()
}
// DetectAcquired detect goroutine that locker acquired.
func DetectAcquired(items map[uint64]LockerStateList) GoroutineLockerList {
l := make(GoroutineLockerList, 0)
for gid, lockers := range items {
lockerAcquired := false
for _, locker := range lockers {
if locker.Status == StatusAcquired {
lockerAcquired = true
}
}
if lockerAcquired {
l = append(l, GoroutineLocker{GoroutineID: gid, Lockers: lockers})
}
}
sort.Sort(l)
return l
}
// DetectLockedEachOther detect goroutine that locked each other.
func DetectLockedEachOther(items map[uint64]LockerStateList) GoroutineLockerList {
l := make(GoroutineLockerList, 0)
for gid, lockers := range items {
sortedLockers := make(LockerStateList, len(lockers))
copy(sortedLockers, lockers)
lockerAcquired := false
acquiredLockers := make([]uintptr, 0)
waiting := false
for _, locker := range sortedLockers {
if locker.Status == StatusAcquired {
acquiredLockers = append(acquiredLockers, locker.LockerPtr)
if !locker.RLocker {
lockerAcquired = true
} else {
loopOtherGID:
for ogid, olockers := range items {
if ogid == gid {
continue
}
waitByOtherGID := false
lockerAcquiredByOtherGID := false
for _, olocker := range olockers {
if olocker.LockerPtr == locker.LockerPtr && olocker.Status == StatusWait && !olocker.RLocker {
waitByOtherGID = true
}
if olocker.Status == StatusAcquired {
lockerAcquiredByOtherGID = true
}
if waitByOtherGID && lockerAcquiredByOtherGID {
lockerAcquired = true
break loopOtherGID
}
}
}
}
} else {
existsInAcquiredLockers := false
for _, acuqiredLocker := range acquiredLockers {
if acuqiredLocker == locker.LockerPtr {
existsInAcquiredLockers = true
break
}
}
if !existsInAcquiredLockers {
waiting = true
}
}
}
if lockerAcquired && waiting {
l = append(l, GoroutineLocker{GoroutineID: gid, Lockers: lockers})
}
}
sort.Sort(l)
return l
}
// DetectReentry detect goroutine that reentry locker occurred.
func DetectReentry(items map[uint64]LockerStateList) GoroutineLockerList {
l := make(GoroutineLockerList, 0)
for gid, lockers := range items {
sortedLockers := make(LockerStateList, len(lockers))
copy(sortedLockers, lockers)
acquiredLockers := make([]uintptr, 0)
for _, locker := range sortedLockers {
if locker.Status == StatusAcquired {
acquiredLockers = append(acquiredLockers, locker.LockerPtr)
} else {
existsInAcquiredLockers := false
for _, acuqiredLocker := range acquiredLockers {
if acuqiredLocker == locker.LockerPtr {
existsInAcquiredLockers = true
break
}
}
if existsInAcquiredLockers {
l = append(l, GoroutineLocker{GoroutineID: gid, Lockers: lockers})
}
}
}
}
sort.Sort(l)
return l
}
// GoroutineLocker goroutine with lockers.
type GoroutineLocker struct {
GoroutineID uint64
Lockers LockerStateList
}
// String of GoroutineLocker, format: goroutine <gid>: [(<locker-id>, <locker-status>), ...]\n, like:
//
// goroutine 53: [(0xc000014080, acquired), (0xc000014088, wait)]
func (l GoroutineLocker) String() string {
return fmt.Sprintf("goroutine %d: %s\n", l.GoroutineID, l.Lockers)
}
// GoroutineLockerList the list of GoroutineLocker
type GoroutineLockerList []GoroutineLocker
func (l GoroutineLockerList) Len() int {
return len(l)
}
func (l GoroutineLockerList) Less(i, j int) bool {
return l[i].GoroutineID < l[j].GoroutineID
}
func (l GoroutineLockerList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
// String of GoroutineLockerList, format: goroutine <gid>: [(<locker-id>, <locker-status>), ...]\n..., like:
//
// goroutine 53: [(0xc000014080, acquired), (0xc000014088, wait)]
//
// goroutine 54: [(0xc000014088, acquired), (0xc000014080, wait)]
func (l GoroutineLockerList) String() string {
if len(l) == 0 {
return ""
}
sb := &strings.Builder{}
for _, glocker := range l {
sb.WriteString(glocker.String())
}
return sb.String()
}