/
command_deadlock_detect.go
177 lines (148 loc) · 3.71 KB
/
command_deadlock_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
169
170
171
172
173
174
175
176
177
package beans
import (
"bufio"
"bytes"
"fmt"
"github.com/lixianmin/gonsole/tools"
"io"
"regexp"
"runtime/pprof"
"sort"
"strconv"
"strings"
)
/********************************************************************
created: 2020-08-02
author: lixianmin
Copyright (C) - All Rights Reserved
*********************************************************************/
type DetectItem struct {
Count int
Text string
waitTime int
title string
}
func DeadlockDetect(args []string, deadlockIgnores []string) string {
deadlockIgnores = checkDeadlockDetectArgs(args, deadlockIgnores)
var isIgnored = false
var title = ""
var list = make([]string, 0, 8)
var itemMap = make(map[string]*DetectItem, 16)
// 匹配title
var titlePattern, _ = regexp.Compile(`goroutine.*\[.*?(\d+) minutes\]:`)
var addressPattern, _= regexp.Compile(`(0x[0-9a-z]+)`)
// 匹配一个调用方法
//var funcPattern, _ = regexp.Compile(`\s*(.*)\(.*\)`)
var err = readPProfGoroutineByLine(func(line string) {
if strings.HasPrefix(line, "goroutine") {
// 此分支是一条记录的开始
isIgnored = false
title = line
list = list[:0]
} else if !isIgnored {
if strings.TrimSpace(line) == "" {
// 此分支是一条记录的结束
var body = strings.Join(list, "<br>")
body = strings.ReplaceAll(body, "\t", " ")
var bodyKey = addressPattern.ReplaceAllString(body, "0x1029")
item, ok := itemMap[bodyKey]
if !ok {
item = &DetectItem{}
itemMap[bodyKey] = item
}
item.Count += 1
// title = "goroutine 105 [IO wait, 17 minutes]:"
match := titlePattern.FindStringSubmatch(title)
if match != nil {
item.Text = title + "<br>" + body
waitTime, _ := strconv.Atoi(match[1])
if waitTime >= item.waitTime {
item.waitTime = waitTime
item.title = title
}
} else if item.Text == "" {
item.Text = title + "<br>" + body
}
} else {
// 此分支处理调用栈的数据行
if len(list) == 0 && isDeadlockIgnored(deadlockIgnores, line) {
isIgnored = true
}
list = append(list, line)
}
}
})
if err != nil {
return err.Error()
}
items := make([]*DetectItem, 0, len(itemMap))
for _, v := range itemMap {
if v.waitTime > 0 {
items = append(items, v)
}
}
sortDetectItems(items)
return tools.ToHtmlTable(items)
}
func isDeadlockIgnored(deadlockIgnores []string, line string) bool {
for _, item := range deadlockIgnores {
if strings.HasPrefix(line, item) {
return true
}
}
return false
}
func checkDeadlockDetectArgs(args []string, deadlockIgnores []string) []string {
// deadlockIgnores
if len(deadlockIgnores) == 0 {
deadlockIgnores = append(deadlockIgnores, "internal/poll.runtime_pollWait(")
}
sort.Strings(deadlockIgnores)
// args
if len(args) >= 2 && strings.Contains(args[1], "-a") {
deadlockIgnores = make([]string, 0)
}
return deadlockIgnores
}
func sortDetectItems(items []*DetectItem) {
sort.Slice(items, func(i, j int) bool {
a, b := items[i], items[j]
if a.waitTime < b.waitTime {
return true
} else if a.waitTime > b.waitTime {
return false
}
if a.Count < b.Count {
return true
} else if a.Count > b.Count {
return false
}
return a.title < b.title
})
}
func readPProfGoroutineByLine(handler func(line string)) error {
const name = "goroutine"
const debug = 2
p := pprof.Lookup(name)
if p == nil {
return fmt.Errorf("can not find pprof type: %q", name)
}
var buff bytes.Buffer
buff.Grow(4096)
err := p.WriteTo(&buff, debug)
if err != nil {
return err
}
reader := bufio.NewReader(&buff)
for {
line, err := reader.ReadString('\n')
if err != nil {
if io.EOF == err {
break
}
return err
}
handler(line)
}
return nil
}