-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgreak.go
More file actions
79 lines (64 loc) · 1.69 KB
/
greak.go
File metadata and controls
79 lines (64 loc) · 1.69 KB
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
package greak
import (
"bytes"
"fmt"
"regexp"
"runtime"
"sort"
"strconv"
)
var idPattern = regexp.MustCompile(`^goroutine (\d+)`)
// Size of buffer to use to dump stack, very large apps may need to increase this
var MaxStackTextSize = 500 * 1024
type Entry struct {
Id int
Description []byte
}
func (e Entry) String() string { return string(e.Description) }
// Creates a new snapshot that represents the current set of goroutines
func New() Entries {
// Check against empty entries results in current goroutine list
return Entries{}.Check()
}
// A set of goroutines.
type Entries []Entry
func (es Entries) contains(id int) bool {
for _, e := range es {
if e.Id == id {
return true
}
}
return false
}
// Takes a new snapshot of goroutines and returns a new Entries that contains any routines only in the new snapshot.
func (es Entries) Check() Entries {
var ret Entries
buf := make([]byte, MaxStackTextSize)
i := runtime.Stack(buf, true)
for _, s := range bytes.Split(buf[:i], []byte("\n\n")) {
x := idPattern.FindSubmatch(s)
if id, err := strconv.Atoi(string(x[1])); err != nil {
panic(err.Error())
} else if !es.contains(id) {
ret = append(ret, Entry{Id: id, Description: s})
}
}
sort.Sort(esort(ret))
return ret
}
func (es Entries) String() string {
var b = bytes.NewBuffer(nil)
if len(es) == 1 {
fmt.Fprint(b, "1 Entry")
} else {
fmt.Fprintf(b, "%d Entries", len(es))
}
for _, e := range es {
fmt.Fprintf(b, "\n%s", e)
}
return b.String()
}
type esort Entries
func (es esort) Len() int { return len(es) }
func (es esort) Swap(i, j int) { es[i], es[j] = es[j], es[i] }
func (es esort) Less(i, j int) bool { return es[i].Id < es[j].Id }