/
profile.go
executable file
·186 lines (165 loc) · 4.46 KB
/
profile.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
package gdebug
import (
"bytes"
"fmt"
"github.com/davidforest123/goutil/basic/gerrors"
"github.com/davidforest123/goutil/sys/gfs"
"github.com/google/pprof/profile"
"io/ioutil"
"os"
"runtime/pprof"
"time"
)
/*
Another manual method to use go tool pprof usage
----- Program to debug side -----
Start official pprof server in net/http/pprof
----- Computer with go development env -----
`go tool pprof http://xxx:yyy/debug/pprof/heap?debug=1`
than, input
`web`
It will start browser to open svg report generated from pprof http server
*/
type (
Profile profile.Profile
)
// comes from net/http/pprof func Index(w http.ResponseWriter, r *http.Request)
func getProfileCount() map[string]int {
result := make(map[string]int)
for _, p := range pprof.Profiles() {
result[p.Name()] = p.Count()
}
return result
}
// Capture captures profile and returns content.
// FIXME I am not sure its result equals to web page output results.
func Capture(profile string, cpuCapDur time.Duration) (*Profile, error) {
switch profile {
case "profile":
if cpuCapDur <= 0 {
cpuCapDur = 30 * time.Second
}
f := &bytes.Buffer{}
if err := pprof.StartCPUProfile(f); err != nil {
return nil, err
}
time.Sleep(cpuCapDur)
pprof.StopCPUProfile()
gfs.BytesToFile(f.Bytes(), fmt.Sprintf("cpu-profile-%s.txt", time.Now().String()))
return ParseProfile(f.Bytes())
case "heap", "block", "mutex", "allocs", "goroutine", "threadcreate":
f := &bytes.Buffer{}
if err := pprof.Lookup(profile).WriteTo(f, 2); err != nil {
return nil, err
}
gfs.BytesToFile(f.Bytes(), fmt.Sprintf("%s-profile-%s.txt", profile, time.Now().String()))
return ParseProfile(f.Bytes())
default:
return nil, gerrors.New("unsupported ProfileName %s", profile)
}
}
// CaptureToFile captures profile and save it to file.
// Note: go tool pprof required.
// FIXME I am not sure its result equals to web page output results.
func CaptureToFile(profile string, cpuCapDur time.Duration) (string, error) {
newTemp := func() (*os.File, error) {
f, err := ioutil.TempFile("", "profile-")
if err != nil {
return nil, gerrors.New("Cannot create new temp profile file: %v", err)
}
return f, nil
}
switch profile {
case "profile":
if cpuCapDur <= 0 {
cpuCapDur = 30 * time.Second
}
f, err := newTemp()
if err != nil {
return "", err
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
return "", err
}
time.Sleep(cpuCapDur)
pprof.StopCPUProfile()
return f.Name(), nil
case "heap", "block", "mutex", "allocs", "goroutine", "threadcreate":
f, err := newTemp()
if err != nil {
return "", err
}
defer f.Close()
if err := pprof.Lookup(profile).WriteTo(f, 2); err != nil {
return "", err
}
return f.Name(), nil
default:
return "", gerrors.New("unsupported ProfileName %s", profile)
}
}
// ParseProfile parses buffer into Profile
func ParseProfile(b []byte) (*Profile, error) {
result, err := profile.ParseData(b)
if err != nil {
return nil, err
}
return (*Profile)(result), nil
}
// VerifyProfile verify if b is valid profile content.
func VerifyProfile(b []byte) error {
_, err := profile.ParseData(b)
return err
}
/*
// ToDotGraph convert profile to dot graph,
// which is an image format created by Graphviz.
func (p *Profile) ToDotGraph() ([]byte, error) {
buf := bytes.Buffer{}
if err := (*profile.Profile)(p).Write(&buf); err != nil {
return nil, err
}
result := bytes.Buffer{}
err := driver.PProf(&driver.Options{
Fetch: &fetcher{b: buf.Bytes()},
Flagset: newFlagSet("-dot"),
UI: &fakeUI{},
Writer: &writer{&result},
})
if err != nil {
return nil, err
}
return result.Bytes(), nil
}
// ToSvg convert profile to SVG image.
// FIXME: output image is totally different from "go tool pprof -svg -output imagePath binaryPath profilePath"
func (p *Profile) ToSvg() ([]byte, error) {
buf := bytes.Buffer{}
if err := (*profile.Profile)(p).Write(&buf); err != nil {
return nil, err
}
tempFile, err := newTemp()
if err != nil {
return nil, err
}
tempPath := tempFile.Name()
tempFile.Close()
selfPath, err := gproc.SelfPath()
if err != nil {
return nil, err
}
result := bytes.Buffer{}
err = driver.PProf(&driver.Options{
Fetch: newFetcher(buf.Bytes()),
//-output=*** is necessary, it no output argument, will report error
Flagset: newFlagSet("-svg", "-output="+tempPath, "-source_path="+selfPath),
UI: newFakeUI(),
Writer: newWriter(&result),
})
if err != nil {
return nil, err
}
return result.Bytes(), nil
}
*/