-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
487 lines (414 loc) · 10.2 KB
/
main.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
"text/tabwriter"
"time"
util "github.com/whyrusleeping/stackparse/util"
)
func printHelp() {
helpstr := `
To filter out goroutines from the trace, use the following flags:
--frame-match=FOO or --fm=FOO
print only stacks with frames that contain 'FOO'
--frame-not-match=FOO or --fnm=FOO
print only stacks with no frames containing 'FOO'
--wait-more-than=10m
print only stacks that have been blocked for more than ten minutes
--wait-less-than=10m
print only stacks that have been blocked for less than ten minutes
--state-match=FOO
print only stacks whose state matches 'FOO'
--state-not-match=FOO
print only stacks whose state matches 'FOO'
Output is by default sorted by waittime ascending, to change this use:
--sort=[stacksize,goronum,waittime]
To print a summary of the goroutines in the stack trace, use:
--summary
If your stacks have some prefix to them (like a systemd log prefix) trim it with:
--line-prefix=prefixRegex
To print the output in JSON format, use:
--json or -j
`
fmt.Println(helpstr)
}
func main() {
if len(os.Args) < 2 || os.Args[1] == "-h" || os.Args[1] == "--help" {
fmt.Printf("usage: %s <filter flags> <filename>\n", os.Args[0])
printHelp()
return
}
var filters []util.Filter
var compfunc util.StackCompFunc = util.CompWaitTime
outputType := "full"
formatType := "default"
fname := "-"
var linePrefix string
var repl bool
// parse flags
for _, a := range os.Args[1:] {
if strings.HasPrefix(a, "-") {
parts := strings.Split(a, "=")
var key string
var val string
key = parts[0]
if len(parts) == 2 {
val = parts[1]
}
switch key {
case "--frame-match", "--fm":
filters = append(filters, util.HasFrameMatching(val))
case "--wait-more-than":
d, err := time.ParseDuration(val)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
filters = append(filters, util.TimeGreaterThan(d))
case "--wait-less-than":
d, err := time.ParseDuration(val)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
filters = append(filters, util.Negate(util.TimeGreaterThan(d)))
case "--frame-not-match", "--fnm":
filters = append(filters, util.Negate(util.HasFrameMatching(val)))
case "--state-match":
filters = append(filters, util.MatchState(val))
case "--state-not-match":
filters = append(filters, util.Negate(util.MatchState(val)))
case "--sort":
switch parts[1] {
case "goronum":
compfunc = util.CompGoroNum
case "stacksize":
compfunc = util.CompDepth
case "waittime":
compfunc = util.CompWaitTime
default:
fmt.Println("unknown sorting parameter: ", val)
fmt.Println("options: goronum, stacksize, waittime (default)")
os.Exit(1)
}
case "--line-prefix":
linePrefix = val
case "--repl":
repl = true
case "--output":
switch val {
case "full", "top", "summary":
outputType = val
default:
fmt.Println("unrecognized output type: ", parts[1])
fmt.Println("valid options are: full, top, summary, json")
os.Exit(1)
}
case "--summary", "-s":
outputType = "summary"
case "--json", "-j":
formatType = "json"
case "--suspicious", "--sus":
outputType = "sus"
}
} else {
fname = a
}
}
var r io.Reader
if fname == "-" {
r = os.Stdin
} else {
fi, err := os.Open(fname)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer fi.Close()
r = fi
}
stacks, err := util.ParseStacks(r, linePrefix)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
sorter := util.StackSorter{
Stacks: stacks,
CompFunc: compfunc,
}
sort.Sort(sorter)
var f formatter
switch formatType {
case "default":
f = &defaultFormatter{}
case "json":
f = &jsonFormatter{}
}
stacks = util.ApplyFilters(stacks, filters)
var formatErr error
switch outputType {
case "full":
formatErr = f.formatStacks(os.Stdout, stacks)
case "summary":
formatErr = f.formatSummaries(os.Stdout, summarize(stacks))
case "sus":
suspiciousCheck(util.ApplyFilters(stacks, filters))
default:
fmt.Println("unrecognized output type: ", outputType)
os.Exit(1)
}
if formatErr != nil {
fmt.Println(err)
os.Exit(1)
}
if repl {
runRepl(util.ApplyFilters(stacks, filters))
}
}
type summary struct {
Function string
Count int
}
type formatter interface {
formatSummaries(io.Writer, []summary) error
formatStacks(io.Writer, []*util.Stack) error
}
type defaultFormatter struct{}
func (t *defaultFormatter) formatSummaries(w io.Writer, summaries []summary) error {
tw := tabwriter.NewWriter(w, 8, 4, 2, ' ', 0)
for _, s := range summaries {
fmt.Fprintf(tw, "%s\t%d\n", s.Function, s.Count)
}
tw.Flush()
return nil
}
func (t *defaultFormatter) formatStacks(w io.Writer, stacks []*util.Stack) error {
for _, s := range stacks {
fmt.Fprintln(w, s.String())
}
return nil
}
type jsonFormatter struct{}
func (j *jsonFormatter) formatSummaries(w io.Writer, summaries []summary) error {
return json.NewEncoder(w).Encode(summaries)
}
func (j *jsonFormatter) formatStacks(w io.Writer, stacks []*util.Stack) error {
return json.NewEncoder(w).Encode(stacks)
}
func summarize(stacks []*util.Stack) []summary {
counts := make(map[string]int)
var filtered []*util.Stack
for _, s := range stacks {
f := s.Frames[0].Function
if counts[f] == 0 {
filtered = append(filtered, s)
}
counts[f]++
}
sort.Sort(util.StackSorter{
Stacks: filtered,
CompFunc: func(a, b *util.Stack) bool {
return counts[a.Frames[0].Function] < counts[b.Frames[0].Function]
},
})
var summaries []summary
for _, s := range filtered {
summaries = append(summaries, summary{
Function: s.Frames[0].Function,
Count: counts[s.Frames[0].Function],
})
}
return summaries
}
type framecount struct {
frameKey string
count int
}
// work in progress, trying to come up with an algorithm to point out suspicious stacks
func suspiciousCheck(stacks []*util.Stack) {
sharedFrames := make(map[string][]*util.Stack)
for _, s := range stacks {
for _, f := range s.Frames {
fk := f.FrameKey()
sharedFrames[fk] = append(sharedFrames[fk], s)
}
}
var fcs []framecount
for k, v := range sharedFrames {
fcs = append(fcs, framecount{
frameKey: k,
count: len(v),
})
}
sort.Slice(fcs, func(i, j int) bool {
return fcs[i].count > fcs[j].count
})
for i := 0; i < 20; i++ {
fmt.Printf("%s - %d\n", fcs[i].frameKey, fcs[i].count)
}
for i := 0; i < 5; i++ {
fmt.Println("-------- FRAME SUS STAT ------")
fmt.Printf("%s - %d\n", fcs[i].frameKey, fcs[i].count)
sf := sharedFrames[fcs[i].frameKey]
printUnique(sf)
}
}
func printUnique(stacks []*util.Stack) {
var ftypes []*util.Stack
var bucketed [][]*util.Stack
for _, s := range stacks {
var found bool
for x, ft := range ftypes {
if s.Sameish(ft) {
bucketed[x] = append(bucketed[x], s)
found = true
}
}
if !found {
ftypes = append(ftypes, s)
bucketed = append(bucketed, []*util.Stack{s})
}
}
for x, ft := range ftypes {
fmt.Println("count: ", len(bucketed[x]))
fmt.Println("average wait: ", compWaitStats(bucketed[x]).String())
fmt.Println(ft.String())
fmt.Println()
}
}
type waitStats struct {
Average time.Duration
Max time.Duration
Min time.Duration
Median time.Duration
}
func (ws waitStats) String() string {
return fmt.Sprintf("av/min/max/med: %s/%s/%s/%s\n", ws.Average, ws.Min, ws.Max, ws.Median)
}
func compWaitStats(stacks []*util.Stack) waitStats {
var durations []time.Duration
var min, max, sum time.Duration
for _, s := range stacks {
if min == 0 || s.WaitTime < min {
min = s.WaitTime
}
if s.WaitTime > max {
max = s.WaitTime
}
sum += s.WaitTime
durations = append(durations, s.WaitTime)
}
sort.Slice(durations, func(i, j int) bool {
return durations[i] < durations[j]
})
return waitStats{
Average: sum / time.Duration(len(durations)),
Max: max,
Min: min,
Median: durations[len(durations)/2],
}
}
func frameStat(stacks []*util.Stack) {
frames := make(map[string]int)
for _, s := range stacks {
for _, f := range s.Frames {
frames[fmt.Sprintf("%s:%d\n%s", f.File, f.Line, f.Function)]++
}
}
type frameCount struct {
Line string
Count int
}
var fcs []frameCount
for k, v := range frames {
fcs = append(fcs, frameCount{
Line: k,
Count: v,
})
}
sort.Slice(fcs, func(i, j int) bool {
return fcs[i].Count < fcs[j].Count
})
for _, fc := range fcs {
fmt.Printf("%s\t%d\n", fc.Line, fc.Count)
}
}
func runRepl(input []*util.Stack) {
bynumber := make(map[int]*util.Stack)
for _, i := range input {
bynumber[i.Number] = i
}
stk := [][]*util.Stack{input}
ops := []string{"."}
cur := input
f := &defaultFormatter{}
scan := bufio.NewScanner(os.Stdin)
fmt.Print("stackparse> ")
for scan.Scan() {
parts := strings.Split(scan.Text(), " ")
switch parts[0] {
case "fm", "frame-match":
var filters []util.Filter
for _, p := range parts[1:] {
filters = append(filters, util.HasFrameMatching(strings.TrimSpace(p)))
}
cur = util.ApplyFilters(cur, filters)
stk = append(stk, cur)
ops = append(ops, scan.Text())
case "fnm", "frame-not-match":
var filters []util.Filter
for _, p := range parts[1:] {
filters = append(filters, util.Negate(util.HasFrameMatching(strings.TrimSpace(p))))
}
cur = util.ApplyFilters(cur, filters)
stk = append(stk, cur)
ops = append(ops, scan.Text())
case "s", "summary", "sum":
err := f.formatSummaries(os.Stdout, summarize(cur))
if err != nil {
fmt.Println(err)
}
case "show", "p", "print":
if len(parts) > 1 {
num, err := strconv.Atoi(parts[1])
if err != nil {
fmt.Println(err)
goto end
}
s, ok := bynumber[num]
if !ok {
fmt.Println("no stack found with that number")
goto end
}
f.formatStacks(os.Stdout, []*util.Stack{s})
} else {
f.formatStacks(os.Stdout, cur)
}
case "diff":
for i, op := range ops {
fmt.Printf("%d (%d): %s\n", i, len(stk[i]), op)
}
case "pop":
if len(stk) > 1 {
stk = stk[:len(stk)-1]
ops = ops[:len(ops)-1]
cur = stk[len(stk)-1]
}
case "sus":
// WIP!!!
suspiciousCheck(cur)
case "framestat":
frameStat(cur)
case "unique", "uu":
printUnique(cur)
}
end:
fmt.Print("stackparse> ")
}
}