-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jthreadparser.go
385 lines (304 loc) · 8.22 KB
/
jthreadparser.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package jthreadparser
import (
"bufio"
"fmt"
"io"
"os"
"regexp"
"strings"
)
func (th ThreadInfo) String() string {
if th.Daemon {
return fmt.Sprintf("Thread Name: '%s' (daemon), ID: '%s', State: '%s'", th.Name, th.ID, th.State)
}
return fmt.Sprintf("Thread Name: '%s', ID: '%s', State: '%s'", th.Name, th.ID, th.State)
}
func extractThreadInfoFromLine(line string) ThreadInfo {
ti := ThreadInfo{
Daemon: strings.Contains(line, " daemon "),
}
if strings.Contains(line, " prio=") {
if rgxp, _ := regexp.Compile(threadNameWithPriorityRgx); rgxp.MatchString(line) {
for _, v := range rgxp.FindAllStringSubmatch(line, -1) {
ti.Name, ti.Priority, ti.ID, ti.NativeID =
v[threadNameRgxGroupIndex], v[threadPriorityRgxGroupIndex], v[threadIDRgxGroupIndex], v[threadNativeIDRgxGroupIndex]
}
}
} else {
if rgxp, _ := regexp.Compile(threadNameRgx); rgxp.MatchString(line) {
for _, v := range rgxp.FindAllStringSubmatch(line, -1) {
ti.Name, ti.ID, ti.NativeID = v[threadNameRgxGroupIndex], v[2], v[3]
}
}
}
return ti
}
func extractThreadState(line string) string {
if rgxp, _ := regexp.Compile(stateRgx); rgxp.MatchString(line) {
if st := strings.Split(line, ":"); st != nil {
state := strings.TrimSpace(st[1])
return strings.Split(state, " ")[0]
}
}
return ""
}
func hasRunnableState(threadHeaderLine string) bool {
rgxp := regexp.MustCompile(runnableStateRgx)
return rgxp.MatchString(threadHeaderLine)
}
func hasSMRInformation(line string) bool {
rgxp := regexp.MustCompile(SMRInfoRgx)
return rgxp.MatchString(line)
}
// ParseFromFile ...
func ParseFromFile(fileName string) (ThreadDump, error) {
file, err := os.Open(fileName)
if err != nil {
return ThreadDump{}, err
}
defer file.Close()
var threadDump ThreadDump
threads := make([]ThreadInfo, 0)
threadDump.Threads = threads
parse(file, &threadDump)
return threadDump, nil
}
func parseSMRIDs(smrInfo string, threadDump *ThreadDump) {
if len(smrInfo) > 0 {
for _, smrInfoLine := range strings.Split(smrInfo, "\n") {
ids := strings.Split(smrInfoLine, ",")
for _, id := range ids {
id := strings.TrimSpace(id)
if len(id) > 0 {
threadDump.SMR = append(threadDump.SMR, strings.TrimSpace(id))
}
}
}
}
}
func parse(r io.Reader, threadDump *ThreadDump) {
scanner := bufio.NewScanner(r)
tlines := make([]string, 0)
for scanner.Scan() {
line := scanner.Text()
tlines = append(tlines, line)
}
for i := 0; i < len(tlines); i++ {
line := tlines[i]
var smrInfo strings.Builder
if hasSMRInformation(line) {
i++
if i < len(tlines) {
line = tlines[i]
} else {
break
}
i++
if i < len(tlines) {
line = tlines[i]
} else {
break
}
for (len(line) > 0) && !strings.HasPrefix(line, "}") {
smrInfo.WriteString(strings.TrimSpace(line))
smrInfo.WriteString("\n")
i++
if i < len(tlines) {
line = tlines[i]
} else {
break
}
}
if line == "}" {
parseSMRIDs(smrInfo.String(), threadDump)
}
}
if hasThreadHeaderInformation(line) {
threadInfo := extractThreadInfoFromLine(line)
threadInfoTogether := ThreadInfo{}
threadTogether := false
if hasRunnableState(line) {
threadInfo.State = "runnable"
}
i++
if i < len(tlines) {
line = tlines[i]
} else {
break
}
// Look for the thread state:
if threadState := extractThreadState(line); len(threadState) != 0 {
threadInfo.State = threadState
if i < len(tlines) {
line = tlines[i]
i++
}
} else {
i--
}
// There could be two threads together without a thread state ...
if hasThreadHeaderInformation(line) {
line2 := line
threadInfoTogether = extractThreadInfoFromLine(line2)
threadTogether = true
if hasRunnableState(line2) {
threadInfoTogether.State = "runnable"
} else if hasWaitingOnState(line2) {
threadInfoTogether.State = "waiting on condition"
}
// *threads = append(*threads, threadInfo2)
i++
}
if i < len(tlines) {
line = tlines[i]
} else {
break
}
var sb strings.Builder
for (len(line) > 0) && !strings.HasPrefix(line, threadInformationBegins) {
sb.WriteString(strings.TrimSpace(line))
sb.WriteString("\n")
i++
if i < len(tlines) {
line = tlines[i]
} else {
break
}
}
stackTrace := sb.String()
if len(stackTrace) > 0 {
threadInfo.StackTrace = stackTrace
}
if threadTogether {
threadDump.Threads = append(threadDump.Threads, threadInfo)
threadDump.Threads = append(threadDump.Threads, threadInfoTogether)
} else {
threadDump.Threads = append(threadDump.Threads, threadInfo)
}
}
}
}
// ParseFrom ...
func ParseFrom(r io.Reader) ThreadDump {
var threadDump ThreadDump
threads := make([]ThreadInfo, 0)
threadDump.Threads = threads
parse(r, &threadDump)
return threadDump
}
func uniqueStackTrace(threadStackTrace []string) []string {
u := make([]string, 0, len(threadStackTrace))
m := make(map[string]bool)
for _, val := range threadStackTrace {
if _, ok := m[val]; !ok {
m[val] = true
u = append(u, val)
}
}
return u
}
func extractJavaMethodNameFromStackTraceLine(stacktraceLine string, rgx *regexp.Regexp) string {
if rgx.MatchString(stacktraceLine) {
fields := strings.Fields(strings.TrimSpace(stacktraceLine))
return strings.Join(fields[1:], " ")
}
return ""
}
// MostUsedMethods ...
func MostUsedMethods(threads *[]ThreadInfo) map[string]int {
rgx := regexp.MustCompile(stackTraceRgxMethodName)
mostUsedMethodsGeneral := make(map[string]int)
for _, th := range *threads {
stackTraceLines := uniqueStackTrace(strings.Split(th.StackTrace, "\n"))
for _, stackTraceLine := range stackTraceLines {
stackTraceLine = extractJavaMethodNameFromStackTraceLine(stackTraceLine, rgx)
if stackTraceLine == "" {
continue
}
mostUsedMethodsGeneral[stackTraceLine]++
}
}
return mostUsedMethodsGeneral
}
// IdenticalStackTrace ...
func IdenticalStackTrace(threads *[]ThreadInfo) map[string]int {
indenticalStackTrace := make(map[string]int)
for _, th := range *threads {
thStack := strings.TrimSpace(th.StackTrace)
indenticalStackTrace[thStack]++
}
return indenticalStackTrace
}
func hasWaitingOnState(threadHeaderLine string) bool {
rgxp := regexp.MustCompile(waitingOnStateRgx)
return rgxp.MatchString(threadHeaderLine)
}
func hasThreadHeaderInformation(threadHeaderLine string) bool {
rgxp := regexp.MustCompile(threadHeaderInfoRgx)
return rgxp.MatchString(threadHeaderLine)
}
func extractSynchronizers(stacktrace string) []Synchronizer {
syncs := make([]Synchronizer, 0)
rgxp := regexp.MustCompile(synchronizerRgx)
for _, stackLine := range strings.Split(stacktrace, "\n") {
if rgxp.MatchString(stackLine) {
sync := Synchronizer{}
for _, group := range rgxp.FindAllStringSubmatch(stackLine, -1) {
sync.State = convertToSyncState(group[1])
sync.ID = group[2]
sync.ObjectName = group[3]
}
syncs = append(syncs, sync)
}
}
return syncs
}
func equalSyncs(a, b []Synchronizer) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func convertToSyncState(textState string) SynchronizerState {
if strings.Contains(textState, "waiting on") {
return WaitingOnState
} else if strings.Contains(textState, "locked") {
return LockedState
} else if strings.Contains(textState, "parking to wait for") {
return ParkingToWaitForState
} else if strings.Contains(textState, "waiting to lock") {
return WaitingToLockState
}
return LockedState
}
// SynchronizersByThread ....
func SynchronizersByThread(threads *[]ThreadInfo) map[ThreadInfo][]Synchronizer {
syncs := make(map[ThreadInfo][]Synchronizer)
for _, th := range *threads {
if len(th.StackTrace) == 0 {
continue
}
threadSyncs := extractSynchronizers(th.StackTrace)
if len(threadSyncs) > 0 {
syncs[th] = threadSyncs
}
}
return syncs
}
// SynchronizersByID ...
func SynchronizersByID(threads *[]ThreadInfo) map[string][]ThreadInfo {
syncsByID := make(map[string][]ThreadInfo)
syncByThread := SynchronizersByThread(threads)
for thread, syncs := range syncByThread {
for i := 0; i < len(syncs); i++ {
sync := syncs[i]
syncsByID[sync.ID] = append(syncsByID[sync.ID], thread)
}
}
return syncsByID
}